tracing.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package link
  2. import (
  3. "fmt"
  4. "github.com/cilium/ebpf"
  5. "github.com/cilium/ebpf/btf"
  6. "github.com/cilium/ebpf/internal/sys"
  7. )
  8. type tracing struct {
  9. RawLink
  10. }
  11. func (f *tracing) Update(new *ebpf.Program) error {
  12. return fmt.Errorf("tracing update: %w", ErrNotSupported)
  13. }
  14. // AttachFreplace attaches the given eBPF program to the function it replaces.
  15. //
  16. // The program and name can either be provided at link time, or can be provided
  17. // at program load time. If they were provided at load time, they should be nil
  18. // and empty respectively here, as they will be ignored by the kernel.
  19. // Examples:
  20. //
  21. // AttachFreplace(dispatcher, "function", replacement)
  22. // AttachFreplace(nil, "", replacement)
  23. func AttachFreplace(targetProg *ebpf.Program, name string, prog *ebpf.Program) (Link, error) {
  24. if (name == "") != (targetProg == nil) {
  25. return nil, fmt.Errorf("must provide both or neither of name and targetProg: %w", errInvalidInput)
  26. }
  27. if prog == nil {
  28. return nil, fmt.Errorf("prog cannot be nil: %w", errInvalidInput)
  29. }
  30. if prog.Type() != ebpf.Extension {
  31. return nil, fmt.Errorf("eBPF program type %s is not an Extension: %w", prog.Type(), errInvalidInput)
  32. }
  33. var (
  34. target int
  35. typeID btf.TypeID
  36. )
  37. if targetProg != nil {
  38. btfHandle, err := targetProg.Handle()
  39. if err != nil {
  40. return nil, err
  41. }
  42. defer btfHandle.Close()
  43. spec, err := btfHandle.Spec(nil)
  44. if err != nil {
  45. return nil, err
  46. }
  47. var function *btf.Func
  48. if err := spec.TypeByName(name, &function); err != nil {
  49. return nil, err
  50. }
  51. target = targetProg.FD()
  52. typeID, err = spec.TypeID(function)
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. link, err := AttachRawLink(RawLinkOptions{
  58. Target: target,
  59. Program: prog,
  60. Attach: ebpf.AttachNone,
  61. BTF: typeID,
  62. })
  63. if err != nil {
  64. return nil, err
  65. }
  66. return &tracing{*link}, nil
  67. }
  68. type TracingOptions struct {
  69. // Program must be of type Tracing with attach type
  70. // AttachTraceFEntry/AttachTraceFExit/AttachModifyReturn or
  71. // AttachTraceRawTp.
  72. Program *ebpf.Program
  73. }
  74. type LSMOptions struct {
  75. // Program must be of type LSM with attach type
  76. // AttachLSMMac.
  77. Program *ebpf.Program
  78. }
  79. // attachBTFID links all BPF program types (Tracing/LSM) that they attach to a btf_id.
  80. func attachBTFID(program *ebpf.Program) (Link, error) {
  81. if program.FD() < 0 {
  82. return nil, fmt.Errorf("invalid program %w", sys.ErrClosedFd)
  83. }
  84. fd, err := sys.RawTracepointOpen(&sys.RawTracepointOpenAttr{
  85. ProgFd: uint32(program.FD()),
  86. })
  87. if err != nil {
  88. return nil, err
  89. }
  90. raw := RawLink{fd: fd}
  91. info, err := raw.Info()
  92. if err != nil {
  93. raw.Close()
  94. return nil, err
  95. }
  96. if info.Type == RawTracepointType {
  97. // Sadness upon sadness: a Tracing program with AttachRawTp returns
  98. // a raw_tracepoint link. Other types return a tracing link.
  99. return &rawTracepoint{raw}, nil
  100. }
  101. return &tracing{RawLink: RawLink{fd: fd}}, nil
  102. }
  103. // AttachTracing links a tracing (fentry/fexit/fmod_ret) BPF program or
  104. // a BTF-powered raw tracepoint (tp_btf) BPF Program to a BPF hook defined
  105. // in kernel modules.
  106. func AttachTracing(opts TracingOptions) (Link, error) {
  107. if t := opts.Program.Type(); t != ebpf.Tracing {
  108. return nil, fmt.Errorf("invalid program type %s, expected Tracing", t)
  109. }
  110. return attachBTFID(opts.Program)
  111. }
  112. // AttachLSM links a Linux security module (LSM) BPF Program to a BPF
  113. // hook defined in kernel modules.
  114. func AttachLSM(opts LSMOptions) (Link, error) {
  115. if t := opts.Program.Type(); t != ebpf.LSM {
  116. return nil, fmt.Errorf("invalid program type %s, expected LSM", t)
  117. }
  118. return attachBTFID(opts.Program)
  119. }