apm_stack_dispatch.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package containers
  2. import (
  3. "debug/dwarf"
  4. "debug/elf"
  5. debugelf "debug/elf"
  6. "fmt"
  7. "github.com/coroot/coroot-node-agent/common"
  8. "github.com/coroot/coroot-node-agent/ebpftracer"
  9. "github.com/coroot/coroot-node-agent/ebpftracer/tracer"
  10. tracerelf "github.com/coroot/coroot-node-agent/ebpftracer/tracer"
  11. "github.com/coroot/coroot-node-agent/proc"
  12. "github.com/coroot/coroot-node-agent/utils"
  13. . "github.com/coroot/coroot-node-agent/utils/modelse"
  14. klog "github.com/sirupsen/logrus"
  15. "golang.org/x/arch/arm64/arm64asm"
  16. "golang.org/x/arch/x86/x86asm"
  17. "io"
  18. "os"
  19. "regexp"
  20. "sort"
  21. )
  22. type uprobesDef struct {
  23. Name string
  24. Offset uint64
  25. EntAddress uint64
  26. RetAddress uint64
  27. }
  28. func (c *Container) AttachStack(tracer *ebpftracer.Tracer, pid uint32) error {
  29. //// 禁用stack
  30. //if tracer.DisableStackTracing() {
  31. // klog.Warnf("StackTrace tracing is disabled")
  32. // return nil
  33. //}
  34. if common.IsOpenFilter() && !common.IsFilterPid(pid) {
  35. klog.Warnf("StackTrace %d tracing is filter", pid)
  36. return nil
  37. }
  38. codeType := c.GetCodeTypeFromCache(pid)
  39. if codeType.IsUnknownCode() {
  40. klog.Warnf("StackTrace %d tracing is IsUnknownCode", pid)
  41. return nil
  42. }
  43. p := c.processes[pid]
  44. if p == nil {
  45. return fmt.Errorf("unknown process %d", pid)
  46. }
  47. if p.stackAttachOnce {
  48. return nil
  49. }
  50. p.stackAttachOnce = true
  51. switch codeType {
  52. case CodeTypeJava:
  53. return c.jvmStackTrace(tracer, pid)
  54. default:
  55. return c.stackTrace(tracer, pid)
  56. }
  57. }
  58. func (c *Container) stackTrace(tracer *ebpftracer.Tracer, pid uint32) error {
  59. p := c.processes[pid]
  60. if p == nil {
  61. return fmt.Errorf("unknown process %d", pid)
  62. }
  63. //if p.stackAttachOnce {
  64. // return nil
  65. //}
  66. binType := "dotnet"
  67. MatchString := ".*HandleFunc|.*main.*|testfun.*|.*serverHandler.*|.*ServeHTTP.*"
  68. dbgpath := ""
  69. WHITE_LIST := os.Getenv("WHITE_LIST")
  70. BIN_TYPE := os.Getenv("BIN_TYPE")
  71. DBG_PATH := os.Getenv("DBG_PATH")
  72. if WHITE_LIST != "" {
  73. MatchString = WHITE_LIST
  74. }
  75. if DBG_PATH != "" {
  76. dbgpath = DBG_PATH
  77. }
  78. if BIN_TYPE != "" {
  79. binType = BIN_TYPE
  80. }
  81. klog.Infoln("[stack] UprobesMatchString:::init", MatchString)
  82. path := proc.Path(uint32(pid), "exe")
  83. var err error
  84. if dbgpath != "" {
  85. c.Uprobes, err = c.getJavaAOTUprobes(binType, path, dbgpath, MatchString)
  86. } else {
  87. c.Uprobes, err = c.getUprobes(path, MatchString)
  88. }
  89. if err != nil {
  90. return err
  91. }
  92. c.UprobesMap = map[string]tracerelf.Uprobe{}
  93. klog.Infoln("[stack] UprobesMap start")
  94. for _, up := range c.Uprobes {
  95. klog.Debugf("[stack] UprobesMap %s %d %d", up.Funcname, up.Address, up.AbsOffset)
  96. c.UprobesMap[fmt.Sprintf("%s-%s", up.Funcname, up.Address+up.AbsOffset)] = up
  97. }
  98. //codeType := c.GetCodeTypeFromCache(pid)
  99. //tracer.InitKProcInfo(pid, c.instanceID, uint16(codeType))
  100. p.stackUprobes = append(p.stackUprobes, tracer.AttachStackUprobes(path, c.Uprobes)...)
  101. p.stackAttachOnce = true
  102. return nil
  103. }
  104. func (c *Container) jvmStackTrace(tracer *ebpftracer.Tracer, pid uint32) error {
  105. p := c.processes[pid]
  106. // check version
  107. libjavaso, err := utils.GetSoPath(pid, "libjava.so", c.getRootfs())
  108. if err != nil {
  109. p.versionFailed = true
  110. klog.WithError(err).Errorf("[jvmStackTrace] Failed get so path")
  111. return err
  112. }
  113. v, err := ebpftracer.GetJvmVersion(libjavaso)
  114. if err != nil {
  115. p.versionFailed = true
  116. klog.WithError(err).Errorf("[jvmStackTrace] Failed get Java version")
  117. return err
  118. }
  119. c.AppInfo.Version = v
  120. major, minor, patch, err := ebpftracer.ParseVersion(v)
  121. klog.Infof("[jvmStackTrace] version: %s (Major: %d, Minor: %d, Patch: %d)", v, major, minor, patch)
  122. if major != 1 || minor != 8 {
  123. p.versionFailed = true
  124. return fmt.Errorf("[jvmStackTrace] Unsupported Java version")
  125. }
  126. err = tracer.JattachJvm(pid, c.AppInfo, c.WhiteSettingInfo.WhiteStackSettingInfo.WhiteList, c.WhiteSettingInfo.WhiteStackSettingInfo.BlackList)
  127. if err != nil {
  128. p.stackStatus.JattachFailure()
  129. return err
  130. } else {
  131. p.stackStatus.JattachSuccess()
  132. }
  133. jvmStackProbes, err := tracer.AttachJVMStackUprobes(pid, c.AppInfo)
  134. if err != nil {
  135. p.stackStatus.StackUprobesFailure()
  136. klog.WithError(err).Errorf("[jvmStackTrace] Failed attach jvm stack.")
  137. return err
  138. }
  139. p.stackUprobes = append(p.stackUprobes, jvmStackProbes...)
  140. p.stackStatus.StackUprobesSuccess()
  141. return nil
  142. }
  143. func (c *Container) getJavaAOTUprobes(binType, path string, dbgpath string, MatchString string) ([]tracer.Uprobe, error) {
  144. uprobes := []tracer.Uprobe{}
  145. elfFile, err := elf.Open(path)
  146. if err != nil {
  147. return nil, err
  148. }
  149. funSection := ".text"
  150. if binType == "dotnet" {
  151. funSection = "__managedcode"
  152. }
  153. textSection := elfFile.Section(funSection)
  154. if textSection == nil {
  155. //fmt.Println("no text section", nil)
  156. return nil, nil
  157. }
  158. textSectionData, err := textSection.Data()
  159. if err != nil {
  160. //fmt.Println("failed to read text section", err)
  161. return nil, err
  162. }
  163. textSectionLen := uint64(len(textSectionData) - 1)
  164. dwarfFile, err := elf.Open(dbgpath)
  165. if err != nil {
  166. return nil, err
  167. }
  168. dwarfData, err := dwarfFile.DWARF()
  169. if err != nil {
  170. return nil, err
  171. }
  172. entryReader := dwarfData.Reader()
  173. // var targetAddress uint64
  174. listEntry := make(map[dwarf.Offset]uprobesDef)
  175. SpecListEntry := []dwarf.Entry{}
  176. for {
  177. entry, err := entryReader.Next()
  178. if err == io.EOF {
  179. // We've reached the end of DWARF entries
  180. break
  181. }
  182. if err != nil {
  183. //log.Fatalf("Error reading entry: %v", err)
  184. klog.Fatalf("Error reading entry: %v", err)
  185. }
  186. if entry == nil {
  187. //log.Println("Warning: a nil entry was returned with no error")
  188. klog.Warn("Warning: a nil entry was returned with no error")
  189. break
  190. }
  191. if entry.Tag == dwarf.TagSubprogram {
  192. // fmt.Printf("entry address: %x, %d\n", entry.Offset, entry.Children)
  193. funName, _ := entry.Val(dwarf.AttrName).(string)
  194. found, _ := regexp.MatchString(MatchString, funName)
  195. if found {
  196. entAddress, _ := entry.Val(dwarf.AttrLowpc).(uint64)
  197. retAddress, _ := entry.Val(dwarf.AttrHighpc).(uint64)
  198. // fmt.Printf("Function %s address: %x, %x\n", funName, address, entry.Offset)
  199. uprobes := uprobesDef{}
  200. uprobes.EntAddress = entAddress
  201. uprobes.RetAddress = retAddress
  202. uprobes.Offset = uint64(entry.Offset)
  203. uprobes.Name = funName
  204. listEntry[entry.Offset] = uprobes
  205. }
  206. specAddr, _ := entry.Val(dwarf.AttrSpecification).(dwarf.Offset)
  207. lowpc := entry.Val(dwarf.AttrLowpc)
  208. if lowpc != nil && specAddr > 0 && lowpc.(uint64) > 0 {
  209. // fmt.Printf("AttrSpecification address: %x, %x\n", specAddr, entry.Offset)
  210. SpecListEntry = append(SpecListEntry, *entry)
  211. }
  212. }
  213. }
  214. for _, v := range SpecListEntry {
  215. specAddr, _ := v.Val(dwarf.AttrSpecification).(dwarf.Offset)
  216. // fmt.Printf("SpecListEntrySpecListEntrySpecListEntry Attach Function: %x\n", specAddr)
  217. _, ok := listEntry[specAddr]
  218. if ok {
  219. vv := listEntry[specAddr]
  220. entAddr := v.Val(dwarf.AttrLowpc)
  221. if entAddr != nil {
  222. vv.EntAddress = entAddr.(uint64)
  223. }
  224. retAddr := v.Val(dwarf.AttrHighpc)
  225. if retAddr != nil {
  226. switch retAddr.(type) {
  227. case uint64:
  228. vv.RetAddress = uint64(retAddr.(uint64))
  229. case int64:
  230. vv.RetAddress = uint64(retAddr.(int64))
  231. default:
  232. //fmt.Println("Unknown type")
  233. }
  234. }
  235. listEntry[specAddr] = vv
  236. }
  237. }
  238. for _, v := range listEntry {
  239. //fmt.Printf("Need Attach Function %s address: %x, %x\n", v.Name, v.EntAddress, v.RetAddress)
  240. sStart := v.EntAddress - textSection.Addr
  241. sSize := v.RetAddress
  242. if v.RetAddress > v.EntAddress {
  243. sSize = v.RetAddress - v.EntAddress
  244. }
  245. sEnd := sStart + sSize
  246. if sEnd > textSectionLen {
  247. continue
  248. }
  249. sBytes := textSectionData[sStart:sEnd]
  250. rbpOffsets := getRbpEnterOffsets(elfFile.Machine, sBytes)
  251. returnOffsets := getReturnOffsets(elfFile.Machine, sBytes)
  252. if rbpOffsets != 0 {
  253. uprobes = append(uprobes, tracer.Uprobe{
  254. Funcname: v.Name, // 函数名
  255. Location: tracer.AtDotNetEntry, // 入口
  256. Address: v.EntAddress, // 函数地址
  257. AbsOffset: uint64(rbpOffsets), // 函数相对 ELF 偏移
  258. RelOffset: 0, // 函数真实偏移
  259. })
  260. } else {
  261. // 函数入口加入待 attach 列表
  262. uprobes = append(uprobes, tracer.Uprobe{
  263. Funcname: v.Name, // 函数名
  264. Location: tracer.AtEntry, // 入口
  265. Address: v.EntAddress, // 函数地址
  266. AbsOffset: 0, // 函数相对 ELF 偏移
  267. RelOffset: 0, // 函数真实偏移
  268. })
  269. }
  270. for _, offset := range returnOffsets {
  271. uprobes = append(uprobes, tracer.Uprobe{
  272. Funcname: v.Name,
  273. Location: tracer.AtRet,
  274. Address: v.EntAddress,
  275. AbsOffset: uint64(offset),
  276. RelOffset: 0,
  277. })
  278. }
  279. }
  280. return uprobes, nil
  281. }
  282. func (c *Container) getUprobes(path string, MatchString string) ([]tracer.Uprobe, error) {
  283. uprobes := []tracer.Uprobe{}
  284. binFile, err := os.Open(path)
  285. if err != nil {
  286. return nil, err
  287. }
  288. // cache := map[string]interface{}{}
  289. // 解析 elf 文件
  290. elfFile, err := debugelf.NewFile(binFile)
  291. if err != nil {
  292. return nil, err
  293. }
  294. // 获取所有符号表
  295. symbols, err := elfFile.Symbols()
  296. if err != nil {
  297. return nil, err
  298. }
  299. sort.Slice(symbols, func(i, j int) bool { return symbols[i].Value < symbols[j].Value })
  300. c.Symbols = symbols
  301. // 符号表组装成键值 map,方便使用
  302. symnames := map[string]debugelf.Symbol{}
  303. for _, symbol := range symbols {
  304. klog.Debugf("[stack] %v %v", symbol.Name, symbol)
  305. symnames[symbol.Name] = symbol
  306. }
  307. textSection := elfFile.Section(".text")
  308. if textSection == nil {
  309. klog.Infoln("[stack] no text section")
  310. return nil, nil
  311. }
  312. textSectionData, err := textSection.Data()
  313. if err != nil {
  314. klog.WithError(err).Errorf("[stack] Failed to read text section")
  315. return nil, nil
  316. }
  317. textSectionLen := uint64(len(textSectionData) - 1)
  318. // 遍历符号表
  319. for _, symbol := range symbols {
  320. if debugelf.ST_TYPE(symbol.Info) != debugelf.STT_FUNC {
  321. continue
  322. }
  323. // fmt.Println("Hello FunName: ", symbol.Name)
  324. // 使用正则表达式匹配函数白名单列表
  325. found, err := regexp.MatchString(MatchString, symbol.Name)
  326. // found, err := regexp.MatchString("main.*", symbol.Name)
  327. if err != nil {
  328. klog.WithError(err).Errorln("[stack] found error")
  329. return nil, err
  330. }
  331. if found {
  332. // 匹配到了加入 attachFuncs 列表
  333. klog.Debugf("[stack] Fuck This: %s, %x", symbol.Name, symbol.Value)
  334. // attachFuncs = append(attachFuncs, symbol.Name)
  335. // 根据函数名拿到当前函数的符号结构体
  336. sym := symnames[symbol.Name]
  337. //if err != nil {
  338. // klog.WithError(err).Errorf("symnames[symbol.Name] %s", symbol.Name)
  339. // return nil, err
  340. //}
  341. address := sym.Value
  342. for _, p := range elfFile.Progs {
  343. if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 {
  344. continue
  345. }
  346. if p.Vaddr <= sym.Value && sym.Value < (p.Vaddr+p.Memsz) {
  347. address = sym.Value - p.Vaddr + p.Off
  348. break
  349. }
  350. }
  351. // 函数入口加入待 attach 列表
  352. uprobes = append(uprobes, tracer.Uprobe{
  353. Funcname: symbol.Name, // 函数名
  354. Location: tracer.AtEntry, // 入口
  355. Address: address, // 函数地址
  356. AbsOffset: 0, // 函数相对 ELF 偏移
  357. RelOffset: 0, // 函数真实偏移
  358. Wanted: true,
  359. })
  360. sStart := sym.Value - textSection.Addr
  361. sEnd := sStart + sym.Size
  362. if sEnd > textSectionLen {
  363. continue
  364. }
  365. sBytes := textSectionData[sStart:sEnd]
  366. returnOffsets := getReturnOffsets(elfFile.Machine, sBytes)
  367. for _, offset := range returnOffsets {
  368. uprobes = append(uprobes, tracer.Uprobe{
  369. Funcname: symbol.Name,
  370. Location: tracer.AtRet,
  371. Address: address,
  372. AbsOffset: uint64(offset),
  373. RelOffset: 0,
  374. })
  375. }
  376. }
  377. }
  378. return uprobes, nil
  379. }
  380. func getRbpEnterOffsets(machine elf.Machine, instructions []byte) int {
  381. switch machine {
  382. case elf.EM_X86_64:
  383. for i := 0; i < len(instructions); {
  384. ins, err := x86asm.Decode(instructions[i:], 64)
  385. if err == nil && ins.Op == x86asm.LEA && ins.Args[0].String() == "RBP" {
  386. klog.Infof("[stack] getRbpEnterOffsets: %v, %s, %s", ins, ins.Args[0].String(), ins.Args[1].String())
  387. return i
  388. }
  389. i += ins.Len
  390. }
  391. case elf.EM_AARCH64:
  392. for i := 0; i < len(instructions); {
  393. ins, err := arm64asm.Decode(instructions[i:])
  394. if err == nil && ins.Op == arm64asm.RET {
  395. return i
  396. }
  397. i += 4
  398. }
  399. }
  400. return 0
  401. }
  402. func getReturnOffsets(machine elf.Machine, instructions []byte) []int {
  403. var res []int
  404. switch machine {
  405. case elf.EM_X86_64:
  406. for i := 0; i < len(instructions); {
  407. ins, err := x86asm.Decode(instructions[i:], 64)
  408. if err == nil && ins.Op == x86asm.RET {
  409. res = append(res, i)
  410. }
  411. i += ins.Len
  412. }
  413. case elf.EM_AARCH64:
  414. for i := 0; i < len(instructions); {
  415. ins, err := arm64asm.Decode(instructions[i:])
  416. if err == nil && ins.Op == arm64asm.RET {
  417. res = append(res, i)
  418. }
  419. i += 4
  420. }
  421. }
  422. return res
  423. }