apm_stack_dispatch.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. libjvmso, err := utils.GetSoPath(pid, "libjvm.so", c.getRootfs())
  114. if err != nil {
  115. klog.WithError(err).Errorf("[jvmStackTrace] Failed get so path")
  116. return err
  117. }
  118. v, err := ebpftracer.GetJvmVersion(libjavaso, libjvmso)
  119. if err != nil {
  120. p.versionFailed = true
  121. klog.WithError(err).Errorf("[jvmStackTrace] Failed get Java version")
  122. return err
  123. }
  124. c.AppInfo.Version = v
  125. major, minor, patch, err := ebpftracer.ParseVersion(v)
  126. klog.Infof("[jvmStackTrace] version: %s (Major: %d, Minor: %d, Patch: %d)", v, major, minor, patch)
  127. if major != 1 || minor != 8 {
  128. p.versionFailed = true
  129. return fmt.Errorf("[jvmStackTrace] Unsupported Java version")
  130. }
  131. err = tracer.JattachJvm(pid, c.AppInfo, c.WhiteSettingInfo.WhiteStackSettingInfo.WhiteList, c.WhiteSettingInfo.WhiteStackSettingInfo.BlackList, c.getRootfs())
  132. if err != nil {
  133. p.stackStatus.JattachFailure()
  134. return err
  135. } else {
  136. p.stackStatus.JattachSuccess()
  137. }
  138. jvmStackProbes, err := tracer.AttachJVMStackUprobes(pid, c.AppInfo, c.getRootfs())
  139. if err != nil {
  140. p.stackStatus.StackUprobesFailure()
  141. klog.WithError(err).Errorf("[jvmStackTrace] Failed attach jvm stack.")
  142. return err
  143. }
  144. p.stackUprobes = append(p.stackUprobes, jvmStackProbes...)
  145. p.stackStatus.StackUprobesSuccess()
  146. return nil
  147. }
  148. func (c *Container) getJavaAOTUprobes(binType, path string, dbgpath string, MatchString string) ([]tracer.Uprobe, error) {
  149. uprobes := []tracer.Uprobe{}
  150. elfFile, err := elf.Open(path)
  151. if err != nil {
  152. return nil, err
  153. }
  154. funSection := ".text"
  155. if binType == "dotnet" {
  156. funSection = "__managedcode"
  157. }
  158. textSection := elfFile.Section(funSection)
  159. if textSection == nil {
  160. //fmt.Println("no text section", nil)
  161. return nil, nil
  162. }
  163. textSectionData, err := textSection.Data()
  164. if err != nil {
  165. //fmt.Println("failed to read text section", err)
  166. return nil, err
  167. }
  168. textSectionLen := uint64(len(textSectionData) - 1)
  169. dwarfFile, err := elf.Open(dbgpath)
  170. if err != nil {
  171. return nil, err
  172. }
  173. dwarfData, err := dwarfFile.DWARF()
  174. if err != nil {
  175. return nil, err
  176. }
  177. entryReader := dwarfData.Reader()
  178. // var targetAddress uint64
  179. listEntry := make(map[dwarf.Offset]uprobesDef)
  180. SpecListEntry := []dwarf.Entry{}
  181. for {
  182. entry, err := entryReader.Next()
  183. if err == io.EOF {
  184. // We've reached the end of DWARF entries
  185. break
  186. }
  187. if err != nil {
  188. //log.Fatalf("Error reading entry: %v", err)
  189. klog.Fatalf("Error reading entry: %v", err)
  190. }
  191. if entry == nil {
  192. //log.Println("Warning: a nil entry was returned with no error")
  193. klog.Warn("Warning: a nil entry was returned with no error")
  194. break
  195. }
  196. if entry.Tag == dwarf.TagSubprogram {
  197. // fmt.Printf("entry address: %x, %d\n", entry.Offset, entry.Children)
  198. funName, _ := entry.Val(dwarf.AttrName).(string)
  199. found, _ := regexp.MatchString(MatchString, funName)
  200. if found {
  201. entAddress, _ := entry.Val(dwarf.AttrLowpc).(uint64)
  202. retAddress, _ := entry.Val(dwarf.AttrHighpc).(uint64)
  203. // fmt.Printf("Function %s address: %x, %x\n", funName, address, entry.Offset)
  204. uprobes := uprobesDef{}
  205. uprobes.EntAddress = entAddress
  206. uprobes.RetAddress = retAddress
  207. uprobes.Offset = uint64(entry.Offset)
  208. uprobes.Name = funName
  209. listEntry[entry.Offset] = uprobes
  210. }
  211. specAddr, _ := entry.Val(dwarf.AttrSpecification).(dwarf.Offset)
  212. lowpc := entry.Val(dwarf.AttrLowpc)
  213. if lowpc != nil && specAddr > 0 && lowpc.(uint64) > 0 {
  214. // fmt.Printf("AttrSpecification address: %x, %x\n", specAddr, entry.Offset)
  215. SpecListEntry = append(SpecListEntry, *entry)
  216. }
  217. }
  218. }
  219. for _, v := range SpecListEntry {
  220. specAddr, _ := v.Val(dwarf.AttrSpecification).(dwarf.Offset)
  221. // fmt.Printf("SpecListEntrySpecListEntrySpecListEntry Attach Function: %x\n", specAddr)
  222. _, ok := listEntry[specAddr]
  223. if ok {
  224. vv := listEntry[specAddr]
  225. entAddr := v.Val(dwarf.AttrLowpc)
  226. if entAddr != nil {
  227. vv.EntAddress = entAddr.(uint64)
  228. }
  229. retAddr := v.Val(dwarf.AttrHighpc)
  230. if retAddr != nil {
  231. switch retAddr.(type) {
  232. case uint64:
  233. vv.RetAddress = uint64(retAddr.(uint64))
  234. case int64:
  235. vv.RetAddress = uint64(retAddr.(int64))
  236. default:
  237. //fmt.Println("Unknown type")
  238. }
  239. }
  240. listEntry[specAddr] = vv
  241. }
  242. }
  243. for _, v := range listEntry {
  244. //fmt.Printf("Need Attach Function %s address: %x, %x\n", v.Name, v.EntAddress, v.RetAddress)
  245. sStart := v.EntAddress - textSection.Addr
  246. sSize := v.RetAddress
  247. if v.RetAddress > v.EntAddress {
  248. sSize = v.RetAddress - v.EntAddress
  249. }
  250. sEnd := sStart + sSize
  251. if sEnd > textSectionLen {
  252. continue
  253. }
  254. sBytes := textSectionData[sStart:sEnd]
  255. rbpOffsets := getRbpEnterOffsets(elfFile.Machine, sBytes)
  256. returnOffsets := getReturnOffsets(elfFile.Machine, sBytes)
  257. if rbpOffsets != 0 {
  258. uprobes = append(uprobes, tracer.Uprobe{
  259. Funcname: v.Name, // 函数名
  260. Location: tracer.AtDotNetEntry, // 入口
  261. Address: v.EntAddress, // 函数地址
  262. AbsOffset: uint64(rbpOffsets), // 函数相对 ELF 偏移
  263. RelOffset: 0, // 函数真实偏移
  264. })
  265. } else {
  266. // 函数入口加入待 attach 列表
  267. uprobes = append(uprobes, tracer.Uprobe{
  268. Funcname: v.Name, // 函数名
  269. Location: tracer.AtEntry, // 入口
  270. Address: v.EntAddress, // 函数地址
  271. AbsOffset: 0, // 函数相对 ELF 偏移
  272. RelOffset: 0, // 函数真实偏移
  273. })
  274. }
  275. for _, offset := range returnOffsets {
  276. uprobes = append(uprobes, tracer.Uprobe{
  277. Funcname: v.Name,
  278. Location: tracer.AtRet,
  279. Address: v.EntAddress,
  280. AbsOffset: uint64(offset),
  281. RelOffset: 0,
  282. })
  283. }
  284. }
  285. return uprobes, nil
  286. }
  287. func (c *Container) getUprobes(path string, MatchString string) ([]tracer.Uprobe, error) {
  288. uprobes := []tracer.Uprobe{}
  289. binFile, err := os.Open(path)
  290. if err != nil {
  291. return nil, err
  292. }
  293. // cache := map[string]interface{}{}
  294. // 解析 elf 文件
  295. elfFile, err := debugelf.NewFile(binFile)
  296. if err != nil {
  297. return nil, err
  298. }
  299. // 获取所有符号表
  300. symbols, err := elfFile.Symbols()
  301. if err != nil {
  302. return nil, err
  303. }
  304. sort.Slice(symbols, func(i, j int) bool { return symbols[i].Value < symbols[j].Value })
  305. c.Symbols = symbols
  306. // 符号表组装成键值 map,方便使用
  307. symnames := map[string]debugelf.Symbol{}
  308. for _, symbol := range symbols {
  309. klog.Debugf("[stack] %v %v", symbol.Name, symbol)
  310. symnames[symbol.Name] = symbol
  311. }
  312. textSection := elfFile.Section(".text")
  313. if textSection == nil {
  314. klog.Infoln("[stack] no text section")
  315. return nil, nil
  316. }
  317. textSectionData, err := textSection.Data()
  318. if err != nil {
  319. klog.WithError(err).Errorf("[stack] Failed to read text section")
  320. return nil, nil
  321. }
  322. textSectionLen := uint64(len(textSectionData) - 1)
  323. // 遍历符号表
  324. for _, symbol := range symbols {
  325. if debugelf.ST_TYPE(symbol.Info) != debugelf.STT_FUNC {
  326. continue
  327. }
  328. // fmt.Println("Hello FunName: ", symbol.Name)
  329. // 使用正则表达式匹配函数白名单列表
  330. found, err := regexp.MatchString(MatchString, symbol.Name)
  331. // found, err := regexp.MatchString("main.*", symbol.Name)
  332. if err != nil {
  333. klog.WithError(err).Errorln("[stack] found error")
  334. return nil, err
  335. }
  336. if found {
  337. // 匹配到了加入 attachFuncs 列表
  338. klog.Debugf("[stack] Fuck This: %s, %x", symbol.Name, symbol.Value)
  339. // attachFuncs = append(attachFuncs, symbol.Name)
  340. // 根据函数名拿到当前函数的符号结构体
  341. sym := symnames[symbol.Name]
  342. //if err != nil {
  343. // klog.WithError(err).Errorf("symnames[symbol.Name] %s", symbol.Name)
  344. // return nil, err
  345. //}
  346. address := sym.Value
  347. for _, p := range elfFile.Progs {
  348. if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 {
  349. continue
  350. }
  351. if p.Vaddr <= sym.Value && sym.Value < (p.Vaddr+p.Memsz) {
  352. address = sym.Value - p.Vaddr + p.Off
  353. break
  354. }
  355. }
  356. // 函数入口加入待 attach 列表
  357. uprobes = append(uprobes, tracer.Uprobe{
  358. Funcname: symbol.Name, // 函数名
  359. Location: tracer.AtEntry, // 入口
  360. Address: address, // 函数地址
  361. AbsOffset: 0, // 函数相对 ELF 偏移
  362. RelOffset: 0, // 函数真实偏移
  363. Wanted: true,
  364. })
  365. sStart := sym.Value - textSection.Addr
  366. sEnd := sStart + sym.Size
  367. if sEnd > textSectionLen {
  368. continue
  369. }
  370. sBytes := textSectionData[sStart:sEnd]
  371. returnOffsets := getReturnOffsets(elfFile.Machine, sBytes)
  372. for _, offset := range returnOffsets {
  373. uprobes = append(uprobes, tracer.Uprobe{
  374. Funcname: symbol.Name,
  375. Location: tracer.AtRet,
  376. Address: address,
  377. AbsOffset: uint64(offset),
  378. RelOffset: 0,
  379. })
  380. }
  381. }
  382. }
  383. return uprobes, nil
  384. }
  385. func getRbpEnterOffsets(machine elf.Machine, instructions []byte) int {
  386. switch machine {
  387. case elf.EM_X86_64:
  388. for i := 0; i < len(instructions); {
  389. ins, err := x86asm.Decode(instructions[i:], 64)
  390. if err == nil && ins.Op == x86asm.LEA && ins.Args[0].String() == "RBP" {
  391. klog.Infof("[stack] getRbpEnterOffsets: %v, %s, %s", ins, ins.Args[0].String(), ins.Args[1].String())
  392. return i
  393. }
  394. i += ins.Len
  395. }
  396. case elf.EM_AARCH64:
  397. for i := 0; i < len(instructions); {
  398. ins, err := arm64asm.Decode(instructions[i:])
  399. if err == nil && ins.Op == arm64asm.RET {
  400. return i
  401. }
  402. i += 4
  403. }
  404. }
  405. return 0
  406. }
  407. func getReturnOffsets(machine elf.Machine, instructions []byte) []int {
  408. var res []int
  409. switch machine {
  410. case elf.EM_X86_64:
  411. for i := 0; i < len(instructions); {
  412. ins, err := x86asm.Decode(instructions[i:], 64)
  413. if err == nil && ins.Op == x86asm.RET {
  414. res = append(res, i)
  415. }
  416. i += ins.Len
  417. }
  418. case elf.EM_AARCH64:
  419. for i := 0; i < len(instructions); {
  420. ins, err := arm64asm.Decode(instructions[i:])
  421. if err == nil && ins.Op == arm64asm.RET {
  422. res = append(res, i)
  423. }
  424. i += 4
  425. }
  426. }
  427. return res
  428. }