netcore.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. package ebpftracer
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "io"
  7. "log"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "debug/dwarf"
  12. "debug/elf"
  13. debugelf "debug/elf"
  14. "fmt"
  15. "github.com/cilium/ebpf/link"
  16. "github.com/coroot/coroot-node-agent/ebpftracer/tracer/aotinject"
  17. )
  18. const (
  19. // goServeHTTP = "net/http.serverHandler.ServeHTTP"
  20. // binPath = "/root/code/jdk8u/build/linux-x86_64-normal-server-release/jdk/lib/amd64/libnio.so"
  21. netcoresymbolsocketRead0 = "SystemNative_Receive"
  22. )
  23. type MemoryMap struct {
  24. Start, End uint64
  25. }
  26. func ReadFirstLineOfMapsFile(pid string) (*MemoryMap, error) {
  27. file, err := os.Open(fmt.Sprintf("/proc/%s/maps", pid))
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer file.Close()
  32. scanner := bufio.NewScanner(file)
  33. if scanner.Scan() {
  34. fields := strings.Fields(scanner.Text())
  35. addresses := strings.Split(fields[0], "-")
  36. if len(addresses) != 2 {
  37. return nil, errors.New("unexpected format in /proc/<pid>/maps")
  38. }
  39. start, err := strconv.ParseUint(addresses[0], 16, 64)
  40. if err != nil {
  41. return nil, err
  42. }
  43. end, err := strconv.ParseUint(addresses[1], 16, 64)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &MemoryMap{
  48. Start: start,
  49. End: end,
  50. }, nil
  51. }
  52. if err := scanner.Err(); err != nil {
  53. return nil, err
  54. }
  55. return nil, errors.New("empty /proc/<pid>/maps")
  56. }
  57. func (t *Tracer) getFunctionOffsetDBG(libPath, functionName string) (elf.Symbol, error) {
  58. dwarfFile, err := elf.Open(libPath)
  59. dwarfData, err := dwarfFile.DWARF()
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. type uprobesDef struct {
  64. Name string
  65. Offset uint64
  66. EntAddress uint64
  67. RetAddress uint64
  68. }
  69. listEntry := make(map[dwarf.Offset]uprobesDef)
  70. SpecListEntry := []dwarf.Entry{}
  71. entryReader := dwarfData.Reader()
  72. for {
  73. entry, err := entryReader.Next()
  74. if err == io.EOF {
  75. // We've reached the end of DWARF entries
  76. break
  77. }
  78. if err != nil {
  79. log.Fatalf("Error reading entry: %v", err)
  80. }
  81. if entry == nil {
  82. log.Println("Warning: a nil entry was returned with no error")
  83. break
  84. }
  85. if entry.Tag == dwarf.TagSubprogram {
  86. // fmt.Printf("entry address: %x, %d\n", entry.Offset, entry.Children)
  87. funName, _ := entry.Val(dwarf.AttrName).(string)
  88. if functionName == funName {
  89. entAddress, _ := entry.Val(dwarf.AttrLowpc).(uint64)
  90. retAddress, _ := entry.Val(dwarf.AttrHighpc).(uint64)
  91. fmt.Printf("Function %s address: %x, %x, %x\n", funName, entAddress, entry.Offset, retAddress)
  92. uprobes := uprobesDef{}
  93. uprobes.EntAddress = entAddress
  94. uprobes.RetAddress = retAddress
  95. uprobes.Offset = uint64(entry.Offset)
  96. uprobes.Name = funName
  97. listEntry[entry.Offset] = uprobes
  98. }
  99. specAddr, _ := entry.Val(dwarf.AttrSpecification).(dwarf.Offset)
  100. lowpc := entry.Val(dwarf.AttrLowpc)
  101. if lowpc != nil && specAddr > 0 && lowpc.(uint64) > 0 {
  102. // fmt.Printf("AttrSpecification address: %x, %x\n", specAddr, entry.Offset)
  103. SpecListEntry = append(SpecListEntry, *entry)
  104. }
  105. }
  106. }
  107. for _, v := range SpecListEntry {
  108. specAddr, _ := v.Val(dwarf.AttrSpecification).(dwarf.Offset)
  109. // fmt.Printf("SpecListEntrySpecListEntrySpecListEntry Attach Function: %x\n", specAddr)
  110. _, ok := listEntry[specAddr]
  111. if ok {
  112. vv := listEntry[specAddr]
  113. entAddr := v.Val(dwarf.AttrLowpc)
  114. if entAddr != nil {
  115. vv.EntAddress = entAddr.(uint64)
  116. }
  117. retAddr := v.Val(dwarf.AttrHighpc)
  118. if retAddr != nil {
  119. switch retAddr.(type) {
  120. case uint64:
  121. vv.RetAddress = uint64(retAddr.(uint64))
  122. case int64:
  123. vv.RetAddress = uint64(retAddr.(int64))
  124. default:
  125. fmt.Println("Unknown type")
  126. }
  127. }
  128. listEntry[specAddr] = vv
  129. }
  130. }
  131. for _, v := range listEntry {
  132. sSize := v.RetAddress
  133. if v.RetAddress > v.EntAddress {
  134. sSize = v.RetAddress - v.EntAddress
  135. }
  136. symbol := elf.Symbol{}
  137. symbol.Name = v.Name
  138. symbol.Value = v.EntAddress
  139. symbol.Size = 180
  140. fmt.Printf("Need Attach Function %s address: %x, %x, %d\n", v.Name, v.EntAddress, v.RetAddress, sSize)
  141. return symbol, nil
  142. }
  143. return elf.Symbol{}, fmt.Errorf("function %s not found", functionName)
  144. }
  145. func (t *Tracer) AttachNetCoreNetReadUprobes(pid uint32) ([]link.Link, error) {
  146. if t.DisableL7Tracing() {
  147. return nil, nil
  148. }
  149. // 根据进程 pid 获取进程的可执行文件路径
  150. procPath, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
  151. if err != nil {
  152. log.Fatalf("Failed to read proc path: %v", err)
  153. }
  154. fmt.Printf("procPath: %s\n", procPath)
  155. var links []link.Link
  156. ex, err := link.OpenExecutable(procPath)
  157. // 获取函数的偏移量
  158. functionSym, err := t.getFunctionOffsetDBG(procPath+".dbg", netcoresymbolsocketRead0)
  159. l, err := ex.Uprobe(functionSym.Name, t.uprobes["SystemNative_Receive"], &link.UprobeOptions{Address: functionSym.Value, Offset: 113})
  160. if err != nil {
  161. fmt.Println("failed to attach SystemNative_Receive uprobe", err, functionSym)
  162. return nil, err
  163. }
  164. links = append(links, l)
  165. if len(links) == 0 {
  166. return nil, nil
  167. }
  168. fmt.Println("netcore uprobes attached, pid is ", pid)
  169. return links, nil
  170. }
  171. func (t *Tracer) AttachNetCoreNetWriteUprobes(pid uint32) ([]link.Link, error) {
  172. if t.DisableL7Tracing() {
  173. return nil, nil
  174. }
  175. // 根据进程 pid 获取进程的可执行文件路径
  176. procPath, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
  177. if err != nil {
  178. log.Fatalf("Failed to read proc path: %v", err)
  179. }
  180. fmt.Printf("procPath: %s\n", procPath)
  181. sendFunctionName := "send@plt"
  182. injectFunctionName := "SystemNative_Send"
  183. // NETCoreAOTInject
  184. netCoreAotInjector := aotinject.AOTInjector{
  185. PID: int(pid),
  186. SendFunctionLibPath: procPath,
  187. SendFunctionName: sendFunctionName,
  188. InjectLibPath: procPath,
  189. InjectFunctionName: injectFunctionName,
  190. CWLibName: "libmylib.so",
  191. CWFunctionName: "asmnop",
  192. CWLibPath: "/data/roger/ebpfdemo/mylib/libmylib.so",
  193. }
  194. hookOffset, err := aotinject.AotInject(netCoreAotInjector)
  195. if (hookOffset == 0) || (err != nil) {
  196. fmt.Println("failed to inject SystemNative_Send", err)
  197. return nil, err
  198. }
  199. var links []link.Link
  200. ex, err := link.OpenExecutable(netCoreAotInjector.CWLibPath)
  201. if err != nil {
  202. return nil, err
  203. }
  204. opt := link.UprobeOptions{
  205. Offset: uint64(hookOffset),
  206. PID: int(pid),
  207. }
  208. upread02, err := ex.Uprobe(netCoreAotInjector.CWFunctionName, t.uprobes["netcore_asmnop"], &opt)
  209. if err != nil {
  210. return nil, err
  211. }
  212. links = append(links, upread02)
  213. if len(links) == 0 {
  214. return nil, nil
  215. }
  216. fmt.Println("netcore client uprobes attached", pid)
  217. return links, nil
  218. }
  219. func contains(array []string, str string) bool {
  220. for _, v := range array {
  221. if v == str {
  222. return true
  223. }
  224. }
  225. return false
  226. }
  227. func SplitByteByDelimiter(data []byte) []byte {
  228. pre := data[:4]
  229. data = data[4:]
  230. // 查找两个字节序列的位置
  231. index1 := bytes.Index(data, []byte{0x55, 0x48, 0x83, 0xEC})
  232. index2 := bytes.Index(data, []byte{0x55, 0x48, 0x81, 0xEC})
  233. // 如果两个都没有找到,返回 nil
  234. if index1 == -1 && index2 == -1 {
  235. return nil
  236. }
  237. // 确定哪个字节序列在前
  238. var startIndex int
  239. if index1 == -1 {
  240. // 只有index2找到了
  241. startIndex = index2
  242. } else if index2 == -1 {
  243. // 只有index1找到了
  244. startIndex = index1
  245. } else {
  246. // 两个都找到了,选择较小的那个
  247. startIndex = min(index1, index2)
  248. }
  249. // 返回从开始到选定的起始位置之前的数据
  250. return append(pre, data[:startIndex]...)
  251. }
  252. func (t *Tracer) AttachNetCoreNetThreadUprobes(pid uint32) []link.Link {
  253. // uprobes := []tracer.Uprobe{}
  254. // 根据进程 pid 获取进程的可执行文件路径
  255. procPath, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
  256. if err != nil {
  257. log.Fatalf("Failed to read proc path: %v", err)
  258. }
  259. fmt.Printf("procPath: %s\n", procPath)
  260. binFile, err := os.Open(procPath + ".dbg")
  261. binFile2, err := os.Open(procPath)
  262. if err != nil {
  263. return nil
  264. }
  265. // cache := map[string]interface{}{}
  266. // 解析 elf 文件
  267. elfFile, _ := debugelf.NewFile(binFile)
  268. elfFile2, _ := debugelf.NewFile(binFile2)
  269. // 获取所有符号表
  270. symbols, _ := elfFile.Symbols()
  271. ex, err := link.OpenExecutable(procPath)
  272. words := []string{
  273. "Microsoft_AspNetCore_Server_Kestrel_Core_Microsoft_AspNetCore_Server_Kestrel_Core_Internal_HttpConnectionMiddleware_1<System___Canon>__OnConnectionAsync", "Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Internal_SocketConnection__DoSend_d__28__MoveNext", "System_Net_Http_System_Net_Http_HttpClient__SendAsync_2",
  274. "System_Net_Http_System_Net_Http_HttpConnection__SendAsync_d__57__MoveNext",
  275. "Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Internal_SocketConnection__Start"}
  276. wordsType := map[string]string{
  277. "Microsoft_AspNetCore_Server_Kestrel_Core_Microsoft_AspNetCore_Server_Kestrel_Core_Internal_HttpConnectionMiddleware_1<System___Canon>__OnConnectionAsync": "OnConnectionAsync",
  278. "Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Internal_SocketConnection__DoSend_d__28__MoveNext": "DoSend",
  279. "System_Net_Http_System_Net_Http_HttpClient__SendAsync_2": "SendAsync1",
  280. "System_Net_Http_System_Net_Http_HttpConnection__SendAsync_d__57__MoveNext": "SendAsync2",
  281. "Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Internal_SocketConnection__Start": "SocketConnectionStart"}
  282. textSection := elfFile2.Section("__managedcode")
  283. textSectionData, err := textSection.Data()
  284. if err != nil {
  285. fmt.Println("failed to read text section", err)
  286. return nil
  287. }
  288. if textSection == nil {
  289. fmt.Println("no text section", nil)
  290. return nil
  291. }
  292. textSectionLen := uint64(len(textSectionData) - 1)
  293. fmt.Printf("textSectionLen %x, %x, %x\n", textSectionLen, textSection.Addr, textSection.Size)
  294. var links []link.Link
  295. for _, sym := range symbols {
  296. exists := contains(words, sym.Name)
  297. if exists {
  298. fmt.Println("dddddddd:", sym.Name, sym.Value, sym.Size, sym.Info, sym)
  299. address := sym.Value
  300. sStart := sym.Value - textSection.Addr
  301. sEnd := sStart + sym.Size
  302. if sym.Size == 0 {
  303. sEnd = sStart + 5000
  304. }
  305. if sEnd > textSectionLen {
  306. fmt.Println("no text section333", nil)
  307. continue
  308. }
  309. sBytes := textSectionData[sStart:sEnd]
  310. if sym.Size == 0 {
  311. sBytes = SplitByteByDelimiter(sBytes)
  312. }
  313. fmt.Printf("dddddddd+++++: %s, %x, %d, %x, %s\n", sym.Name, sym.Value, sym.Size, address, wordsType[sym.Name])
  314. l, err := ex.Uprobe("", t.uprobes[wordsType[sym.Name]+"Start"], &link.UprobeOptions{Address: address})
  315. if err != nil {
  316. fmt.Println("failed to attach uprobe", err, wordsType[sym.Name])
  317. // return nil
  318. }
  319. links = append(links, l)
  320. returnOffsets := getReturnOffsets(elfFile.Machine, sBytes)
  321. for _, offset := range returnOffsets {
  322. fmt.Printf("dddddddd----: %s, %x, %d, %x\n", sym.Name, sym.Value, sym.Size, offset)
  323. l, err := ex.Uprobe("", t.uprobes[wordsType[sym.Name]+"End"], &link.UprobeOptions{Address: address, Offset: uint64(offset)})
  324. if err != nil {
  325. fmt.Println("failed to attach uprobe", err, wordsType[sym.Name])
  326. // return nil
  327. }
  328. links = append(links, l)
  329. }
  330. }
  331. }
  332. if len(links) == 0 {
  333. return nil
  334. }
  335. fmt.Println("netcore uprobes OnConnectionAsyncStart attached, pid is ", pid)
  336. return links
  337. }
  338. // func (t *Tracer) AttachNetCoreNetOnConnectionAsyncStartUprobes(pid uint32, insID utils.ID) []link.Link {
  339. // if t.disableL7Tracing {
  340. // return nil
  341. // }
  342. // var links []link.Link
  343. // ex, err := link.OpenExecutable(libPath)
  344. // // 获取函数的偏移量
  345. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  346. // var addr uint64 = 0x96d700 // Microsoft_AspNetCore_Server_Kestrel_Core_Microsoft_AspNetCore_Server_Kestrel_Core_Internal_HttpConnectionMiddleware_1<System___Canon>__OnConnectionAsync 入口处
  347. // // var addr uint64 = 0x96dc15
  348. // l, err := ex.Uprobe("", t.uprobes["OnConnectionAsyncStart"], &link.UprobeOptions{Address: addr})
  349. // if err != nil {
  350. // fmt.Println("failed to attach OnConnectionAsyncStart uprobe", err)
  351. // return nil
  352. // }
  353. // links = append(links, l)
  354. // if len(links) == 0 {
  355. // return nil
  356. // }
  357. // fmt.Println("netcore uprobes OnConnectionAsyncStart attached, pid is ", pid)
  358. // return links
  359. // }
  360. // func (t *Tracer) AttachNetCoreNetOnConnectionAsyncEndUprobes(pid uint32, insID utils.ID) []link.Link {
  361. // if t.disableL7Tracing {
  362. // return nil
  363. // }
  364. // var links []link.Link
  365. // ex, err := link.OpenExecutable(libPath)
  366. // // 获取函数的偏移量
  367. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  368. // var addr uint64 = 0x96dcbf
  369. // l, err := ex.Uprobe("", t.uprobes["OnConnectionAsyncEnd"], &link.UprobeOptions{Address: addr})
  370. // if err != nil {
  371. // fmt.Println("failed to attach OnConnectionAsyncEnd uprobe", err)
  372. // return nil
  373. // }
  374. // links = append(links, l)
  375. // if len(links) == 0 {
  376. // return nil
  377. // }
  378. // fmt.Println("netcore uprobes OnConnectionAsyncEnd attached, pid is ", pid)
  379. // return links
  380. // }
  381. // func (t *Tracer) AttachNetCoreNetDoSendStartUprobes(pid uint32, insID utils.ID) []link.Link {
  382. // if t.disableL7Tracing {
  383. // return nil
  384. // }
  385. // var links []link.Link
  386. // ex, err := link.OpenExecutable(libPath)
  387. // // 获取函数的偏移量
  388. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  389. // var addr uint64 = 0x260b40 // Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Internal_SocketConnection__DoSend_d__28__MoveNext 入口处
  390. // l, err := ex.Uprobe("", t.uprobes["DoSendStart"], &link.UprobeOptions{Address: addr})
  391. // if err != nil {
  392. // fmt.Println("failed to attach DoSendStart uprobe", err)
  393. // return nil
  394. // }
  395. // links = append(links, l)
  396. // if len(links) == 0 {
  397. // return nil
  398. // }
  399. // fmt.Println("netcore uprobes DoSendStart attached, pid is ", pid)
  400. // return links
  401. // }
  402. // func (t *Tracer) AttachNetCoreNetDoSendEndUprobes(pid uint32, insID utils.ID) []link.Link {
  403. // if t.disableL7Tracing {
  404. // return nil
  405. // }
  406. // var links []link.Link
  407. // ex, _ := link.OpenExecutable(libPath)
  408. // // 获取函数的偏移量
  409. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  410. // var addrArray = []uint64{0x26125d, 0x261295, 0x2612fd, 0x261396, 0x2613f4}
  411. // for _, addr := range addrArray {
  412. // l, err := ex.Uprobe("", t.uprobes["DoSendEnd"], &link.UprobeOptions{Address: addr})
  413. // if err != nil {
  414. // fmt.Println("failed to attach DoSendEnd uprobe", err)
  415. // return nil
  416. // }
  417. // links = append(links, l)
  418. // }
  419. // if len(links) == 0 {
  420. // return nil
  421. // }
  422. // fmt.Println("netcore uprobes DoSendEnd attached, pid is ", pid)
  423. // return links
  424. // }
  425. // func (t *Tracer) AttachNetCoreNetSendAsync1Uprobes(pid uint32, insID utils.ID) []link.Link {
  426. // if t.disableL7Tracing {
  427. // return nil
  428. // }
  429. // var links []link.Link
  430. // ex, err := link.OpenExecutable(libPath)
  431. // // 获取函数的偏移量
  432. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  433. // var addr uint64 = 0x32ab70 // System_Net_Http_System_Net_Http_HttpClient__SendAsync_2 入口处
  434. // l, err := ex.Uprobe("", t.uprobes["SendAsync1"], &link.UprobeOptions{Address: addr})
  435. // if err != nil {
  436. // fmt.Println("failed to attach SendAsync1 uprobe", err)
  437. // return nil
  438. // }
  439. // links = append(links, l)
  440. // if len(links) == 0 {
  441. // return nil
  442. // }
  443. // fmt.Println("netcore uprobes SendAsync1 attached, pid is ", pid)
  444. // return links
  445. // }
  446. // func (t *Tracer) AttachNetCoreNetSendAsync2Uprobes(pid uint32, insID utils.ID) []link.Link {
  447. // if t.disableL7Tracing {
  448. // return nil
  449. // }
  450. // var links []link.Link
  451. // ex, err := link.OpenExecutable(libPath)
  452. // // 获取函数的偏移量
  453. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  454. // var addr uint64 = 0x34ce90 // System_Net_Http_System_Net_Http_HttpConnection__SendAsync_d__57__MoveNext 入口处
  455. // l, err := ex.Uprobe("", t.uprobes["SendAsync2"], &link.UprobeOptions{Address: addr})
  456. // if err != nil {
  457. // fmt.Println("failed to attach SendAsync2 uprobe", err)
  458. // return nil
  459. // }
  460. // links = append(links, l)
  461. // if len(links) == 0 {
  462. // return nil
  463. // }
  464. // fmt.Println("netcore uprobes SendAsync2 attached, pid is ", pid)
  465. // return links
  466. // }
  467. // func (t *Tracer) AttachNetCoreNetSendAsync2EndUprobes(pid uint32, insID utils.ID) []link.Link {
  468. // if t.disableL7Tracing {
  469. // return nil
  470. // }
  471. // var links []link.Link
  472. // ex, _ := link.OpenExecutable(libPath)
  473. // // 获取函数的偏移量
  474. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  475. // var addrArray = []uint64{0x34f56c, 0x34f5af, 0x34f649, 0x34f6e3}
  476. // for _, addr := range addrArray {
  477. // l, err := ex.Uprobe("", t.uprobes["SendAsync2End"], &link.UprobeOptions{Address: addr})
  478. // if err != nil {
  479. // fmt.Println("failed to attach SendAsync2End uprobe", err)
  480. // return nil
  481. // }
  482. // links = append(links, l)
  483. // }
  484. // if len(links) == 0 {
  485. // return nil
  486. // }
  487. // fmt.Println("netcore uprobes SendAsync2End attached, pid is ", pid)
  488. // return links
  489. // }
  490. // func (t *Tracer) AttachNetCoreNetSocketConnectionStartStartUprobes(pid uint32, insID utils.ID) []link.Link {
  491. // if t.disableL7Tracing {
  492. // return nil
  493. // }
  494. // var links []link.Link
  495. // ex, err := link.OpenExecutable(libPath)
  496. // var addr uint64 = 0x25c4c0 // Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Microsoft_AspNetCore_Server_Kestrel_Transport_Sockets_Internal_SocketConnection__Start 入口处
  497. // l, err := ex.Uprobe("", t.uprobes["SocketConnectionStartStart"], &link.UprobeOptions{Address: addr})
  498. // if err != nil {
  499. // fmt.Println("failed to attach SocketConnectionStartStart uprobe", err)
  500. // return nil
  501. // }
  502. // links = append(links, l)
  503. // if len(links) == 0 {
  504. // return nil
  505. // }
  506. // fmt.Println("netcore uprobes SocketConnectionStartStart attached, pid is ", pid)
  507. // return links
  508. // }
  509. // func (t *Tracer) AttachNetCoreNetSocketConnectionStartEndUprobes(pid uint32, insID utils.ID) []link.Link {
  510. // if t.disableL7Tracing {
  511. // return nil
  512. // }
  513. // var links []link.Link
  514. // ex, _ := link.OpenExecutable(libPath)
  515. // // 获取函数的偏移量
  516. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  517. // var addrArray = []uint64{0x25c54e, 0x25c5c7}
  518. // for _, addr := range addrArray {
  519. // l, err := ex.Uprobe("", t.uprobes["SocketConnectionStartEnd"], &link.UprobeOptions{Address: addr})
  520. // if err != nil {
  521. // fmt.Println("failed to attach SocketConnectionStartEnd uprobe", err)
  522. // return nil
  523. // }
  524. // links = append(links, l)
  525. // }
  526. // if len(links) == 0 {
  527. // return nil
  528. // }
  529. // fmt.Println("netcore uprobes SocketConnectionStartEnd attached, pid is ", pid)
  530. // return links
  531. // }
  532. // func (t *Tracer) AttachNetCoreNetThreadUprobes(pid uint32, insID utils.ID) []link.Link {
  533. // if t.disableL7Tracing {
  534. // return nil
  535. // }
  536. // var links []link.Link
  537. // ex, err := link.OpenExecutable(libPath)
  538. // // 获取函数的偏移量
  539. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  540. // fmt.Println("netcore uprobes AttachNetCoreNetThreadUprobes hook value, pid is ", functionSym.Value)
  541. // var addr uint64 = 0x97b766
  542. // l, err := ex.Uprobe("", t.uprobes["AcceptConnectionsAsync"], &link.UprobeOptions{Address: addr})
  543. // if err != nil {
  544. // fmt.Println("failed to attach AcceptConnectionsAsync uprobe", err)
  545. // return nil
  546. // }
  547. // links = append(links, l)
  548. // if len(links) == 0 {
  549. // return nil
  550. // }
  551. // fmt.Println("netcore uprobes AcceptConnectionsAsync attached, pid is ", pid)
  552. // return links
  553. // }
  554. // func (t *Tracer) AttachNetCoreNetCreateContextStartUprobes(pid uint32, insID utils.ID) []link.Link {
  555. // if t.disableL7Tracing {
  556. // return nil
  557. // }
  558. // var links []link.Link
  559. // ex, err := link.OpenExecutable(libPath)
  560. // // 获取函数的偏移量
  561. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  562. // var addr uint64 = 0x97b60a
  563. // l, err := ex.Uprobe("", t.uprobes["CreateContextStart"], &link.UprobeOptions{Address: addr})
  564. // if err != nil {
  565. // fmt.Println("failed to attach CreateContextStart uprobe", err)
  566. // return nil
  567. // }
  568. // links = append(links, l)
  569. // if len(links) == 0 {
  570. // return nil
  571. // }
  572. // fmt.Println("netcore uprobes CreateContextStart attached, pid is ", pid)
  573. // return links
  574. // }
  575. // func (t *Tracer) AttachNetCoreNetReceiveStartUprobes(pid uint32, insID utils.ID) []link.Link {
  576. // if t.disableL7Tracing {
  577. // return nil
  578. // }
  579. // var links []link.Link
  580. // ex, err := link.OpenExecutable(libPath)
  581. // // 获取函数的偏移量
  582. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  583. // var addr uint64 = 0xec7d0
  584. // l, err := ex.Uprobe("", t.uprobes["ReceiveStart"], &link.UprobeOptions{Address: addr})
  585. // if err != nil {
  586. // fmt.Println("failed to attach ReceiveStart uprobe", err)
  587. // return nil
  588. // }
  589. // links = append(links, l)
  590. // if len(links) == 0 {
  591. // return nil
  592. // }
  593. // fmt.Println("netcore uprobes ReceiveStart attached, pid is ", pid)
  594. // return links
  595. // }
  596. // func (t *Tracer) AttachNetCoreNetReceiveEndUprobes(pid uint32, insID utils.ID) []link.Link {
  597. // if t.disableL7Tracing {
  598. // return nil
  599. // }
  600. // var links []link.Link
  601. // ex, err := link.OpenExecutable(libPath)
  602. // // 获取函数的偏移量
  603. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  604. // var addr uint64 = 0xec86e
  605. // l, err := ex.Uprobe("", t.uprobes["ReceiveEnd"], &link.UprobeOptions{Address: addr})
  606. // if err != nil {
  607. // fmt.Println("failed to attach ReceiveEnd uprobe", err)
  608. // return nil
  609. // }
  610. // links = append(links, l)
  611. // if len(links) == 0 {
  612. // return nil
  613. // }
  614. // fmt.Println("netcore uprobes ReceiveEnd attached, pid is ", pid)
  615. // return links
  616. // }
  617. // func (t *Tracer) AttachNetCoreNetSendStartUprobes(pid uint32, insID utils.ID) []link.Link {
  618. // if t.disableL7Tracing {
  619. // return nil
  620. // }
  621. // var links []link.Link
  622. // ex, err := link.OpenExecutable(libPath)
  623. // // 获取函数的偏移量
  624. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  625. // var addr uint64 = 0xeca40
  626. // l, err := ex.Uprobe("", t.uprobes["SendStart"], &link.UprobeOptions{Address: addr})
  627. // if err != nil {
  628. // fmt.Println("failed to attach SendStart uprobe", err)
  629. // return nil
  630. // }
  631. // links = append(links, l)
  632. // if len(links) == 0 {
  633. // return nil
  634. // }
  635. // fmt.Println("netcore uprobes SendStart attached, pid is ", pid)
  636. // return links
  637. // }
  638. // func (t *Tracer) AttachNetCoreNetSendEndUprobes(pid uint32, insID utils.ID) []link.Link {
  639. // if t.disableL7Tracing {
  640. // return nil
  641. // }
  642. // var links []link.Link
  643. // ex, err := link.OpenExecutable(libPath)
  644. // // 获取函数的偏移量
  645. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  646. // var addr uint64 = 0xecad1
  647. // l, err := ex.Uprobe("", t.uprobes["SendEnd"], &link.UprobeOptions{Address: addr})
  648. // if err != nil {
  649. // fmt.Println("failed to attach SendEnd uprobe", err)
  650. // return nil
  651. // }
  652. // links = append(links, l)
  653. // if len(links) == 0 {
  654. // return nil
  655. // }
  656. // fmt.Println("netcore uprobes SendEnd attached, pid is ", pid)
  657. // return links
  658. // }
  659. // func (t *Tracer) AttachNetCoreNetDoReceiveStartUprobes(pid uint32, insID utils.ID) []link.Link {
  660. // if t.disableL7Tracing {
  661. // return nil
  662. // }
  663. // var links []link.Link
  664. // ex, err := link.OpenExecutable(libPath)
  665. // // 获取函数的偏移量
  666. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  667. // var addr uint64 = 0x260150
  668. // l, err := ex.Uprobe("", t.uprobes["DoReceiveStart"], &link.UprobeOptions{Address: addr})
  669. // if err != nil {
  670. // fmt.Println("failed to attach DoReceiveStart uprobe", err)
  671. // return nil
  672. // }
  673. // links = append(links, l)
  674. // if len(links) == 0 {
  675. // return nil
  676. // }
  677. // fmt.Println("netcore uprobes DoReceiveStart attached, pid is ", pid)
  678. // return links
  679. // }
  680. // func (t *Tracer) AttachNetCoreNetDoReceiveEndUprobes(pid uint32, insID utils.ID) []link.Link {
  681. // if t.disableL7Tracing {
  682. // return nil
  683. // }
  684. // var links []link.Link
  685. // ex, _ := link.OpenExecutable(libPath)
  686. // // 获取函数的偏移量
  687. // // memoryMap, _ := ReadFirstLineOfMapsFile(strconv.Itoa(int(pid)))
  688. // var addrArray = []uint64{0x2609ed, 0x260a4f, 0x260aa3, 0x260adb, 0x260b33}
  689. // for _, addr := range addrArray {
  690. // l, err := ex.Uprobe("", t.uprobes["DoReceiveEnd"], &link.UprobeOptions{Address: addr})
  691. // if err != nil {
  692. // fmt.Println("failed to attach DoReceiveEnd uprobe", err)
  693. // return nil
  694. // }
  695. // links = append(links, l)
  696. // }
  697. // if len(links) == 0 {
  698. // return nil
  699. // }
  700. // fmt.Println("netcore uprobes DoReceiveEnd attached, pid is ", pid)
  701. // return links
  702. // }