filter.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package tracer
  2. import (
  3. "fmt"
  4. "github.com/cilium/ebpf"
  5. "github.com/cilium/ebpf/asm"
  6. "github.com/coroot/coroot-node-agent/common"
  7. )
  8. func PidFilter(collectionSpec *ebpf.CollectionSpec) {
  9. filterPid, ok := common.GetFilterPid()
  10. if ok {
  11. type Editor struct {
  12. instructions *asm.Instructions
  13. ReferenceOffsets map[string][]int
  14. }
  15. for _, prog := range collectionSpec.Programs {
  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. }