filter.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package tracer
  2. import (
  3. "github.com/cilium/ebpf"
  4. "github.com/cilium/ebpf/asm"
  5. "github.com/coroot/coroot-node-agent/common"
  6. )
  7. func PidFilter(collectionSpec *ebpf.CollectionSpec) {
  8. filterPid, isZero := common.GetFilterPid()
  9. if !isZero {
  10. type Editor struct {
  11. instructions *asm.Instructions
  12. ReferenceOffsets map[string][]int
  13. }
  14. for _, prog := range collectionSpec.Programs {
  15. //klog.Infof("[%v] [%v] [%v]", prog.Type, prog.Name, prog.SectionName)
  16. //fmt.Println("collectionSpec.Program:", prog.Name, prog.SectionName, prog.Type)
  17. insns := &prog.Instructions
  18. refs := insns.ReferenceOffsets()
  19. edit := &Editor{insns, refs}
  20. indices := edit.ReferenceOffsets["filter_pid"]
  21. //fmt.Println("len(indices):", len(indices))
  22. if len(indices) == 0 {
  23. continue
  24. }
  25. ldDWImm := asm.LoadImmOp(asm.DWord)
  26. for _, index := range indices {
  27. load := &(*edit.instructions)[index]
  28. if load.OpCode != ldDWImm {
  29. continue
  30. //return errors.Errorf("symbol %v: load: found %v instead of %v", symbol, load.OpCode, ldDWImm)
  31. }
  32. load.Constant = int64(filterPid)
  33. }
  34. }
  35. }
  36. }