offset.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package tracer
  2. import (
  3. "debug/dwarf"
  4. "debug/elf"
  5. "errors"
  6. "fmt"
  7. "github.com/cilium/ebpf"
  8. "github.com/coroot/coroot-node-agent/utils"
  9. . "github.com/coroot/coroot-node-agent/utils/modelse"
  10. "github.com/coroot/coroot-node-agent/utils/try"
  11. klog "github.com/sirupsen/logrus"
  12. "io"
  13. "net"
  14. "os"
  15. "strings"
  16. )
  17. const (
  18. offsetInferServerAddr = "127.0.0.1"
  19. offsetInferServerPort = 54583
  20. bufferSize = 16
  21. )
  22. type ID struct {
  23. // ModPath is the module path containing the struct field package.
  24. //
  25. // If set to "std", the struct field belongs to the standard Go library.
  26. ModPath string
  27. // PkgPath package import path containing the struct field.
  28. PkgPath string
  29. // Struct is the name of the struct containing the field.
  30. Struct string
  31. // Field is the field name.
  32. Field string
  33. }
  34. func kernelOffsetInferServer(listener net.Listener) error {
  35. klog.Info("[eBPF Kernel Adapt] kernel_offset_infer_server started.")
  36. //cpuOnlineCount := runtime.NumCPU()
  37. try.Go(func() {
  38. for {
  39. conn, err := listener.Accept()
  40. if err != nil {
  41. klog.Errorf("[eBPF Kernel Adapt] Fail to accept client request: %v", err)
  42. return
  43. }
  44. //go handleConnection(conn)
  45. try.GoParams(handleConnection, utils.CatchFn, conn)
  46. }
  47. }, utils.CatchFn)
  48. return nil
  49. }
  50. func handleConnection(conn net.Conn) {
  51. defer conn.Close()
  52. buffer := make([]byte, bufferSize)
  53. for {
  54. n, err := conn.Read(buffer)
  55. if err != nil {
  56. if err == io.EOF {
  57. klog.Errorf("[eBPF Kernel Adapt] client connection closed: %v", err)
  58. return
  59. }
  60. klog.Errorf("[eBPF Kernel Adapt] Error reading from connection: %v", err)
  61. return
  62. }
  63. //if n == 0 {
  64. // *clientCount++
  65. // break
  66. //}
  67. request := strings.TrimSpace(string(buffer[:n]))
  68. klog.Infof("[eBPF Kernel Adapt] Request received: %v", request)
  69. if request == "hello" {
  70. _, err := conn.Write([]byte("OK"))
  71. if err != nil {
  72. klog.Errorf("[eBPF Kernel Adapt] Error writing response: %v", err)
  73. break
  74. }
  75. }
  76. }
  77. }
  78. func kernelOffsetInferClient() error {
  79. conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", offsetInferServerAddr, offsetInferServerPort))
  80. if err != nil {
  81. return fmt.Errorf("failed to connect to server: %v", err)
  82. }
  83. defer conn.Close()
  84. klog.Infoln("[eBPF Kernel Adapt] kernel_offset_infer_client started.")
  85. request := "hello"
  86. _, err = conn.Write([]byte(request))
  87. if err != nil {
  88. return fmt.Errorf("failed to send request to server: %v", err)
  89. }
  90. buffer := make([]byte, bufferSize)
  91. for {
  92. n, err := conn.Read(buffer)
  93. if err != nil {
  94. klog.Errorf("[eBPF Kernel Adapt] Error reading from connection: %v", err)
  95. break
  96. }
  97. if n == 0 {
  98. break
  99. }
  100. response := strings.TrimSpace(string(buffer[:n]))
  101. klog.Infof("[eBPF Kernel Adapt] Response received: %v", response)
  102. if response == "OK" {
  103. break
  104. }
  105. }
  106. klog.Infoln("[eBPF Kernel Adapt] kernel_offset_infer_client finished.")
  107. return nil
  108. }
  109. func NewID(mod, pkg, strct, field string) ID {
  110. return ID{ModPath: mod, PkgPath: pkg, Struct: strct, Field: field}
  111. }
  112. func gotoEntry(r *dwarf.Reader, tag dwarf.Tag, name string) bool {
  113. _, err := findEntry(r, tag, name)
  114. return err == nil
  115. }
  116. // findEntry returns the DWARF entry with a tag equal to name read from r. An
  117. // error is returned if the entry cannot be found.
  118. func findEntry(r *dwarf.Reader, tag dwarf.Tag, name string) (*dwarf.Entry, error) {
  119. for {
  120. entry, err := r.Next()
  121. if err == io.EOF || entry == nil {
  122. break
  123. }
  124. if entry.Tag == tag {
  125. if f, ok := entryField(entry, dwarf.AttrName); ok {
  126. if name == f.Val.(string) {
  127. return entry, nil
  128. }
  129. }
  130. }
  131. }
  132. return nil, errors.New("not found")
  133. }
  134. // entryField returns the DWARF field from DWARF entry e that has the passed
  135. // DWARF attribute a.
  136. func entryField(e *dwarf.Entry, a dwarf.Attr) (dwarf.Field, bool) {
  137. for _, f := range e.Field {
  138. if f.Attr == a {
  139. return f, true
  140. }
  141. }
  142. return dwarf.Field{}, false
  143. }
  144. func GetOffset(id ID, path string) (uint64, bool) {
  145. strct := fmt.Sprintf("%s.%s", id.PkgPath, id.Struct)
  146. elfF, err := elf.Open(path)
  147. if err != nil {
  148. os.Exit(1)
  149. }
  150. defer elfF.Close()
  151. data, err := elfF.DWARF()
  152. fmt.Println(err)
  153. r := data.Reader()
  154. if !gotoEntry(r, dwarf.TagStructType, strct) {
  155. return 0, false
  156. }
  157. e, err := findEntry(r, dwarf.TagMember, id.Field)
  158. if err != nil {
  159. return 0, false
  160. }
  161. f, ok := entryField(e, dwarf.AttrDataMemberLoc)
  162. if !ok {
  163. return 0, false
  164. }
  165. return uint64(f.Val.(int64)), true
  166. }
  167. func UpdateProcInfoToMap(collection *ebpf.Collection, pid uint32, proc any) (int, error) {
  168. return bpf_table_set_value(collection, MAP_PROC_INFO_MAP_NAME, pid, proc)
  169. }
  170. func DelProcInfoFromMap(collection *ebpf.Collection, pid uint32) (int, error) {
  171. return bpf_table_delete_key(collection, MAP_PROC_INFO_MAP_NAME, pid)
  172. }