tls.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. package ebpftracer
  2. import (
  3. "bufio"
  4. "bytes"
  5. "debug/buildinfo"
  6. "debug/elf"
  7. "errors"
  8. "fmt"
  9. "os"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "github.com/cilium/ebpf"
  14. "github.com/cilium/ebpf/link"
  15. "github.com/coroot/coroot-node-agent/ebpftracer/tracer"
  16. "github.com/coroot/coroot-node-agent/proc"
  17. . "github.com/coroot/coroot-node-agent/utils/modelse"
  18. klog "github.com/sirupsen/logrus"
  19. "golang.org/x/arch/arm64/arm64asm"
  20. "golang.org/x/arch/x86/x86asm"
  21. "golang.org/x/mod/semver"
  22. )
  23. const (
  24. minSupportedGoVersion = "v1.15.0"
  25. goTlsWriteSymbol = "crypto/tls.(*Conn).Write"
  26. goTlsReadSymbol = "crypto/tls.(*Conn).Read"
  27. goExecute = "runtime.execute"
  28. goNewproc1 = "runtime.newproc1"
  29. goRunqget = "runtime.runqget"
  30. goGoready = "runtime.goready"
  31. goServeHTTP = "net/http.serverHandler.ServeHTTP"
  32. goTransport = "net/http.(*Transport).roundTrip"
  33. goHeaderWriteSubset = "net/http.Header.writeSubset"
  34. goGrpcServerHandleStream = "google.golang.org/grpc.(*Server).handleStream"
  35. goGrpcHttp2OperateHeader = "google.golang.org/grpc/internal/transport.(*http2Server).operateHeaders"
  36. goGrpcServerWritestatus = "google.golang.org/grpc/internal/transport.(*http2Server).WriteStatus"
  37. goGrpcClientConnInvoke = "google.golang.org/grpc.(*ClientConn).Invoke"
  38. goGrpcClientLoopyHeaderHandler = "google.golang.org/grpc/internal/transport.(*loopyWriter).headerHandler"
  39. goGrpcHttp2ClientNewStream = "google.golang.org/grpc/internal/transport.(*http2Client).NewStream"
  40. goReadContinuedLineSlice = "net/textproto.(*Reader).readContinuedLineSlice"
  41. goGocqlSessionExecuteQuery = "github.com/gocql/gocql.(*Session).executeQuery"
  42. goGocqlSessionExecuteQueryV2 = "github.com/apache/cassandra-gocql-driver/v2.(*Session).executeQuery"
  43. goKafkaWriterWriteMessages = "github.com/segmentio/kafka-go.(*Writer).WriteMessages"
  44. goKafkaReaderFetchMessage = "github.com/segmentio/kafka-go.(*Reader).FetchMessage"
  45. goBuntdbTxSet = "github.com/tidwall/buntdb.(*Tx).Set"
  46. goBuntdbTxGet = "github.com/tidwall/buntdb.(*Tx).Get"
  47. goBuntdbTxDelete = "github.com/tidwall/buntdb.(*Tx).Delete"
  48. goLeveldbDBPutRec = "github.com/syndtr/goleveldb/leveldb.(*DB).putRec"
  49. goLeveldbDBGet = "github.com/syndtr/goleveldb/leveldb.(*DB).Get"
  50. )
  51. var (
  52. opensslVersionRe = regexp.MustCompile(`OpenSSL\s(\d\.\d+\.\d+)`)
  53. )
  54. func (t *Tracer) AttachOpenSslUprobes(pid uint32) ([]link.Link, error) {
  55. if t.DisableL7Tracing() {
  56. return nil, nil
  57. }
  58. libPath, version := getSslLibPathAndVersion(pid)
  59. if libPath == "" || version == "" {
  60. return nil, nil
  61. }
  62. log := func(msg string, err error) {
  63. if err != nil {
  64. for _, s := range []string{"no such file or directory", "no such process", "permission denied"} {
  65. if strings.HasSuffix(err.Error(), s) {
  66. return
  67. }
  68. }
  69. klog.Errorf("pid=%d libssl_version=%s: %s: %s", pid, version, msg, err)
  70. return
  71. }
  72. klog.Infof("pid=%d libssl_version=%s: %s", pid, version, msg)
  73. }
  74. exe, err := link.OpenExecutable(libPath)
  75. if err != nil {
  76. log("failed to open executable", err)
  77. return nil, err
  78. }
  79. var links []link.Link
  80. writeEnter := "openssl_SSL_write_enter"
  81. readEnter := "openssl_SSL_read_enter"
  82. readExEnter := "openssl_SSL_read_ex_enter"
  83. readExit := "openssl_SSL_read_exit"
  84. switch {
  85. case semver.Compare(version, "v3.0.0") >= 0:
  86. writeEnter = "openssl_SSL_write_enter_v3_0"
  87. readEnter = "openssl_SSL_read_enter_v3_0"
  88. readExEnter = "openssl_SSL_read_ex_enter_v3_0"
  89. case semver.Compare(version, "v1.1.1") >= 0:
  90. writeEnter = "openssl_SSL_write_enter_v1_1_1"
  91. readEnter = "openssl_SSL_read_enter_v1_1_1"
  92. readExEnter = "openssl_SSL_read_ex_enter_v1_1_1"
  93. }
  94. type prog struct {
  95. symbol string
  96. uprobe string
  97. uretprobe string
  98. }
  99. progs := []prog{
  100. {symbol: "SSL_write", uprobe: writeEnter},
  101. {symbol: "SSL_read", uprobe: readEnter},
  102. {symbol: "SSL_read", uretprobe: readExit},
  103. }
  104. if semver.Compare(version, "v1.1.1") >= 0 {
  105. progs = append(progs, []prog{
  106. {symbol: "SSL_write_ex", uprobe: writeEnter},
  107. {symbol: "SSL_read_ex", uprobe: readExEnter},
  108. {symbol: "SSL_read_ex", uretprobe: readExit},
  109. }...)
  110. }
  111. for _, p := range progs {
  112. if p.uprobe != "" {
  113. l, err := exe.Uprobe(p.symbol, t.uprobes[p.uprobe], nil)
  114. klog.Infoln("fucktls crypto/tls uprobes attached", p.symbol)
  115. if err != nil {
  116. //log("failed to attach uprobe", err)
  117. klog.Infoln("fucktls crypto/tls uprobes attached error", p.symbol)
  118. return nil, err
  119. }
  120. links = append(links, l)
  121. }
  122. if p.uretprobe != "" {
  123. klog.Infoln("fucktls crypto/tls uprobes attached ret", p.symbol)
  124. l, err := exe.Uretprobe(p.symbol, t.uprobes[p.uretprobe], nil)
  125. if err != nil {
  126. klog.Infoln("fucktls crypto/tls uprobes attached ret error", p.symbol)
  127. //log("failed to attach uretprobe", err)
  128. return nil, err
  129. }
  130. links = append(links, l)
  131. }
  132. }
  133. //log("libssl uprobes attached", nil)
  134. return links, nil
  135. }
  136. func (t *Tracer) AttachGoTlsUprobes(pid uint32, appInfo *AppInfo, codeType uint16) ([]link.Link, error) {
  137. if t.DisableL7Tracing() {
  138. return nil, nil
  139. }
  140. path := proc.Path(pid, "exe")
  141. instanceID := appInfo.InstanceIdHash.HashtVal
  142. appID := appInfo.AppIdHash.HashtVal
  143. var err error
  144. var name, version string
  145. var major, minor, revision int
  146. log := func(msg string, err error) {
  147. if err != nil {
  148. for _, s := range []string{"not a Go executable", "no such file or directory", "no such process", "permission denied"} {
  149. if strings.HasSuffix(err.Error(), s) {
  150. return
  151. }
  152. }
  153. klog.Errorf("pid=%d golang_app=%s golang_version=%s: %s: %s", pid, name, version, msg, err)
  154. return
  155. }
  156. klog.Infof("pid=%d golang_app=%s golang_version=%s: %s", pid, name, version, msg)
  157. }
  158. bi, err := buildinfo.ReadFile(path)
  159. if err != nil {
  160. log("failed to read build info", err)
  161. return nil, err
  162. }
  163. // isGolangApp = true
  164. name, err = os.Readlink(path)
  165. if err != nil {
  166. log("failed to read name", err)
  167. return nil, err
  168. }
  169. version = strings.Replace(bi.GoVersion, "go", "v", 1)
  170. if semver.Compare(version, minSupportedGoVersion) < 0 {
  171. log(fmt.Sprintf("go_versions below %s are not supported", minSupportedGoVersion), nil)
  172. return nil, err
  173. }
  174. appInfo.Version = version
  175. ef, err := elf.Open(path)
  176. if err != nil {
  177. log("failed to open as elf binary", err)
  178. return nil, err
  179. }
  180. defer ef.Close()
  181. symbols, err := ef.Symbols()
  182. if err != nil {
  183. if errors.Is(err, elf.ErrNoSymbols) {
  184. log("no symbol section", err)
  185. return nil, err
  186. }
  187. log("failed to read symbols", err)
  188. return nil, err
  189. }
  190. textSection := ef.Section(".text")
  191. if textSection == nil {
  192. log("no text section", err)
  193. return nil, err
  194. }
  195. textSectionData, err := textSection.Data()
  196. if err != nil {
  197. log("failed to read text section", err)
  198. return nil, err
  199. }
  200. textSectionLen := uint64(len(textSectionData) - 1)
  201. exe, err := link.OpenExecutable(path)
  202. if err != nil {
  203. log("failed to open executable", err)
  204. return nil, err
  205. }
  206. // 检测 gRPC 版本
  207. var grpcMajorVersion, grpcMinorVersion int
  208. for _, dep := range bi.Deps {
  209. if strings.Contains(dep.Path, "grpc") {
  210. klog.Infoln("Found gRPC dependency:", dep.Path, "version:", dep.Version)
  211. // 解析版本号
  212. version := dep.Version
  213. if version != "" {
  214. // 移除可能的 "v" 前缀
  215. version = strings.TrimPrefix(version, "v")
  216. parts := strings.Split(version, ".")
  217. if len(parts) >= 2 {
  218. major, err := strconv.Atoi(parts[0])
  219. if err != nil {
  220. klog.WithError(err).Warnf("Error parsing major version from %s", parts[0])
  221. continue
  222. }
  223. minor, err := strconv.Atoi(parts[1])
  224. if err != nil {
  225. klog.WithError(err).Warnf("Error parsing minor version from %s", parts[1])
  226. continue
  227. }
  228. klog.Infof("Detected gRPC version: %d.%d for PID %d", major, minor, pid)
  229. grpcMajorVersion = major
  230. grpcMinorVersion = minor
  231. // // 根据版本选择相应的探针策略
  232. // if major == 1 && minor >= 69 {
  233. // klog.Infof("Using modern gRPC handler for version %d.%d", major, minor)
  234. // } else {
  235. // klog.Infof("Using legacy gRPC handler for version %d.%d", major, minor)
  236. // }
  237. }
  238. }
  239. }
  240. }
  241. offset, ok := tracer.GetOffset(tracer.NewID("std", "runtime", "g", "goid"), path)
  242. if ok {
  243. klog.Infof("[AttachGoTlsUprobes] STEP 11.1: Successfully got goid offset=%d", offset)
  244. } else {
  245. klog.Errorf("[AttachGoTlsUprobes] STEP 11.2: Failed to get goid offset, pid=%d, version=%s", pid, bi.GoVersion)
  246. }
  247. // 获取 runtime.p.goidcache 偏移量
  248. klog.Infof("[AttachGoTlsUprobes] STEP 11.3: Getting offset for runtime.p.goidcache")
  249. goidcacheOffset, okGoidcache := tracer.GetOffset(tracer.NewID("std", "runtime", "p", "goidcache"), path)
  250. if okGoidcache {
  251. klog.Infof("[AttachGoTlsUprobes] STEP 11.4: Successfully got goidcache offset=%d", goidcacheOffset)
  252. } else {
  253. klog.Warnf("[AttachGoTlsUprobes] STEP 11.5: Failed to get goidcache offset, pid=%d, version=%s, using fallback", pid, bi.GoVersion)
  254. goidcacheOffset = 384 // fallback value
  255. }
  256. // 获取 runtime.p.runnext 偏移量
  257. klog.Infof("[AttachGoTlsUprobes] STEP 11.6: Getting offset for runtime.p.runnext")
  258. runnextOffset, okRunnext := tracer.GetOffset(tracer.NewID("std", "runtime", "p", "runnext"), path)
  259. if okRunnext {
  260. klog.Infof("[AttachGoTlsUprobes] STEP 11.7: Successfully got runnext offset=%d", runnextOffset)
  261. } else {
  262. klog.Warnf("[AttachGoTlsUprobes] STEP 11.8: Failed to get runnext offset, pid=%d, version=%s, using fallback", pid, bi.GoVersion)
  263. runnextOffset = 2456 // fallback value
  264. }
  265. klog.Infof("[AttachGoTlsUprobes] STEP 12: Getting offset for runtime.hmap.buckets")
  266. bucketsOff, ok2 := tracer.GetOffset(tracer.NewID("std", "runtime", "hmap", "buckets"), path)
  267. // Go 1.24+ 使用新的 map 实现(Swiss Tables),使用 internal/runtime/maps.Map 而不是 runtime.hmap
  268. if !ok2 {
  269. klog.Errorf("[AttachGoTlsUprobes] STEP 12.2: Failed to get buckets offset, pid=%d, version=%s", pid, bi.GoVersion)
  270. klog.Infof("[AttachGoTlsUprobes] STEP 12.3: Trying Swiss Tables maps.Map.dirPtr for Go 1.24+")
  271. // Go 1.24+ Swiss Tables 使用 internal/runtime/maps.Map 结构体
  272. // 结构体字段:used uint64, seed uintptr, dirPtr unsafe.Pointer (相当于旧的 buckets)
  273. // 尝试获取 maps.Map.dirPtr 的偏移量
  274. // 注意:DWARF 中的包路径可能是 "internal/runtime/maps" 或 "internal/runtime/maps.Map"
  275. swissFields := []struct {
  276. pkg string
  277. structName string
  278. field string
  279. }{
  280. {"internal/runtime/maps", "Map", "dirPtr"},
  281. {"internal.runtime.maps", "Map", "dirPtr"},
  282. {"maps", "Map", "dirPtr"},
  283. }
  284. for _, sf := range swissFields {
  285. // 尝试不同的包路径格式
  286. swissOff, swissOk := tracer.GetOffset(tracer.NewID("std", sf.pkg, sf.structName, sf.field), path)
  287. if swissOk {
  288. klog.Infof("[AttachGoTlsUprobes] STEP 12.4: Found Swiss Tables field '%s.%s.%s' with offset=%d", sf.pkg, sf.structName, sf.field, swissOff)
  289. bucketsOff = swissOff
  290. ok2 = true
  291. break
  292. } else {
  293. klog.Debugf("[AttachGoTlsUprobes] STEP 12.4: Trying Swiss Tables field '%s.%s.%s' not found", sf.pkg, sf.structName, sf.field)
  294. }
  295. }
  296. // 如果还是找不到,尝试旧的 hmap 字段作为备选
  297. if !ok2 {
  298. klog.Infof("[AttachGoTlsUprobes] STEP 12.5: Trying alternative hmap field names")
  299. alternativeFields := []string{"table", "swiss", "swissTable", "buckets1", "oldbuckets", "bmap", "extra"}
  300. for _, fieldName := range alternativeFields {
  301. altOff, altOk := tracer.GetOffset(tracer.NewID("std", "runtime", "hmap", fieldName), path)
  302. if altOk {
  303. klog.Infof("[AttachGoTlsUprobes] STEP 12.6: Found alternative field '%s' with offset=%d", fieldName, altOff)
  304. bucketsOff = altOff
  305. ok2 = true
  306. break
  307. }
  308. }
  309. }
  310. if !ok2 {
  311. klog.Errorf("[AttachGoTlsUprobes] STEP 12.7: All attempts failed, Go 1.24+ Swiss Tables map structure not found")
  312. // 根据源码分析,maps.Map 结构体布局(64-bit):
  313. // - used uint64 (8 bytes, offset 0)
  314. // - seed uintptr (8 bytes, offset 8)
  315. // - dirPtr unsafe.Pointer (8 bytes, offset 16) <- 相当于旧的 buckets
  316. // 如果 DWARF 查找失败,使用硬编码的偏移量作为 fallback
  317. // 注意:这需要确认目标系统是 64-bit,且结构体对齐正确
  318. klog.Warnf("[AttachGoTlsUprobes] STEP 12.8: Using hardcoded offset for maps.Map.dirPtr (offset=16 on 64-bit)")
  319. klog.Warnf("[AttachGoTlsUprobes] STEP 12.9: This assumes: used(uint64@0) + seed(uintptr@8) + dirPtr(unsafe.Pointer@16)")
  320. // 检查 Go 版本是否 >= 1.24
  321. realVersion := strings.Replace(bi.GoVersion, "go", "", 1)
  322. parts := strings.Split(realVersion, ".")
  323. if len(parts) >= 2 {
  324. major, _ = strconv.Atoi(parts[0])
  325. minor, _ = strconv.Atoi(parts[1])
  326. if major > 1 || (major == 1 && minor >= 24) {
  327. // Go 1.24+ 使用 Swiss Tables,maps.Map.dirPtr 在 offset 16 (64-bit)
  328. // 假设是 64-bit 系统(大多数生产环境)
  329. bucketsOff = 16
  330. ok2 = true
  331. klog.Infof("[AttachGoTlsUprobes] STEP 12.10: Using hardcoded offset=%d for Go %s (Swiss Tables)", bucketsOff, bi.GoVersion)
  332. } else {
  333. klog.Errorf("[AttachGoTlsUprobes] STEP 12.11: Go version < 1.24 but buckets not found, this is unexpected")
  334. bucketsOff = 0
  335. }
  336. } else {
  337. klog.Errorf("[AttachGoTlsUprobes] STEP 12.12: Failed to parse Go version: %s", bi.GoVersion)
  338. bucketsOff = 0
  339. }
  340. }
  341. } else {
  342. klog.Infof("[AttachGoTlsUprobes] STEP 12.1: Successfully got buckets offset=%d", bucketsOff)
  343. }
  344. klog.Infof("[AttachGoTlsUprobes] STEP 13: Checking if both offsets are valid, goid_ok=%v, buckets_ok=%v", ok, ok2)
  345. // Go 1.24+ 兼容:如果 goid 成功但 buckets 失败,仍然继续(但记录警告)
  346. if ok {
  347. if !ok2 {
  348. klog.Warnf("[AttachGoTlsUprobes] STEP 13.0: buckets offset missing for Go 1.24+, but continuing with goid only")
  349. // 对于 Go 1.24,可能需要调整后续逻辑,暂时允许继续
  350. }
  351. klog.Infof("[AttachGoTlsUprobes] STEP 13.1: Both offsets valid, proceeding with version encoding")
  352. klog.Infof("[AttachGoTlsUprobes] STEP 14: Parsing Go version string")
  353. realVersion := strings.Replace(bi.GoVersion, "go", "", 1)
  354. klog.Infof("[AttachGoTlsUprobes] STEP 14.1: Real version string=%s", realVersion)
  355. parts := strings.Split(realVersion, ".")
  356. if len(parts) >= 2 {
  357. major, err = strconv.Atoi(parts[0])
  358. if err != nil {
  359. log("Error converting major version:", err)
  360. return nil, err
  361. }
  362. minor, err = strconv.Atoi(parts[1])
  363. if err != nil {
  364. log("Error converting minor version:", err)
  365. return nil, err
  366. }
  367. if len(parts) >= 3 {
  368. revision, err = strconv.Atoi(parts[2])
  369. if err != nil {
  370. log("Error converting revision version:", err)
  371. }
  372. }
  373. klog.Infof("[AttachGoTlsUprobes] Parsed version, major=%d, minor=%d, revision=%d", major, minor, revision)
  374. goVersion := ((major & 0xFF) << 16) + ((minor & 0xFF) << 8) + min(revision, 255)
  375. klog.Infof("[AttachGoTlsUprobes] Initializing EbpfProcInfo structure")
  376. info := EbpfProcInfo{}
  377. info.Version = uint32(goVersion)
  378. info.Offsets[OFFSET_IDX_GOID_RUNTIME_G] = uint16(offset)
  379. info.Offsets[OFFSET_IDX_P_GOIDCACHE] = uint16(goidcacheOffset)
  380. info.Offsets[OFFSET_IDX_P_RUNNEXT] = uint16(runnextOffset)
  381. info.NetTCPConnItab = uint64(0)
  382. info.CryptoTLSConnItab = uint64(0)
  383. info.CredentialsSyscallConnItab = uint64(0)
  384. info.InstanceId = instanceID
  385. info.AppId = appID
  386. info.CodeType = codeType
  387. if major == 1 && minor >= 24 {
  388. info.UseSwissMap = uint64(1)
  389. }
  390. if grpcMajorVersion >= 1 && grpcMinorVersion >= 60 {
  391. info.IsNewFramePos = 1
  392. klog.Infof("[AttachGoTlsUprobes] Using new frame position for gRPC >= 1.60")
  393. } else {
  394. info.IsNewFramePos = 0
  395. klog.Infof("[AttachGoTlsUprobes] Using old frame position for gRPC < 1.60")
  396. }
  397. // go
  398. info.BucketsPtrPos = bucketsOff
  399. if bucketsOff == 0 {
  400. klog.Warnf("[AttachGoTlsUprobes] STEP 15.3: BucketsPtrPos=0 (Go 1.24+ may not use buckets field)")
  401. } else {
  402. klog.Infof("[AttachGoTlsUprobes] STEP 15.3: Basic info initialized, BucketsPtrPos=%d", bucketsOff)
  403. }
  404. klog.Infof("[AttachGoTlsUprobes] STEP 16: Getting offsets for HTTP, gRPC and Kafka fields")
  405. fields := map[*uint64]tracer.ID{
  406. &info.MethodPtrPos: tracer.NewID("std", "net/http", "Request", "Method"),
  407. &info.UrlPtrPos: tracer.NewID("std", "net/http", "Request", "URL"),
  408. &info.PathPtrPos: tracer.NewID("std", "net/url", "URL", "Path"),
  409. &info.StatusCodePos: tracer.NewID("std", "net/http", "response", "status"),
  410. &info.RequestHostPos: tracer.NewID("std", "net/http", "Request", "Host"),
  411. &info.UrlHostPos: tracer.NewID("std", "net/url", "URL", "Host"),
  412. &info.ProtoPos: tracer.NewID("std", "net/http", "Request", "Proto"),
  413. &info.CtxPtrPos: tracer.NewID("std", "net/http", "Request", "ctx"),
  414. &info.HeadersPtrPos: tracer.NewID("std", "net/http", "Request", "Header"),
  415. &info.HttpClientNextidPos: tracer.NewID("google.golang.org/grpc", "google.golang.org/grpc/internal/transport", "http2Client", "nextID"),
  416. &info.StreamMethodPtrPos: tracer.NewID("google.golang.org/grpc", "google.golang.org/grpc/internal/transport", "Stream", "method"),
  417. &info.StreamCtxPos: tracer.NewID("google.golang.org/grpc", "google.golang.org/grpc/internal/transport", "Stream", "ctx"),
  418. &info.IoWriterBufPtrPos: tracer.NewID("std", "bufio", "Writer", "buf"),
  419. &info.IoWriterNPos: tracer.NewID("std", "bufio", "Writer", "n"),
  420. // Kafka Message fields
  421. &info.KafkaMessageKeyPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Key"),
  422. &info.KafkaMessageTopicPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Topic"),
  423. &info.KafkaMessageHeadersPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Headers"),
  424. &info.KafkaMessageTimePos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Time"),
  425. &info.KafkaMessagePartitionPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Partition"),
  426. &info.KafkaMessageOffsetPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Offset"),
  427. &info.KafkaMessageValuePos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Value"),
  428. // Kafka Writer fields
  429. &info.KafkaWriterTopicPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Writer", "Topic"),
  430. // Try both Brokers and Addr fields - Addr is a string, Brokers is []string
  431. &info.KafkaWriterAddrPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Writer", "Addr"),
  432. // Kafka Reader fields
  433. // Note: Reader.config is unexported, try both "config" and "Config"
  434. &info.KafkaReaderConfigPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Reader", "config"),
  435. &info.KafkaReaderConfigGroupIDPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "ReaderConfig", "GroupID"),
  436. &info.KafkaReaderConfigTopicsPos: tracer.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "ReaderConfig", "topics"),
  437. // net.TCPAddr offsets (standard library)
  438. &info.TcpAddrIPOffset: tracer.NewID("std", "net", "TCPAddr", "IP"),
  439. &info.TcpAddrPortOffset: tracer.NewID("std", "net", "TCPAddr", "Port"),
  440. }
  441. successCount := 0
  442. failCount := 0
  443. for field, id := range fields {
  444. off, ok := tracer.GetOffset(id, path)
  445. if !ok {
  446. klog.Warnf("[AttachGoTlsUprobes] STEP 16.1: Failed to get offset for ID: %v (PkgPath=%s, Struct=%s, Field=%s)", id, id.PkgPath, id.Struct, id.Field)
  447. failCount++
  448. } else {
  449. successCount++
  450. klog.Debugf("[AttachGoTlsUprobes] STEP 16.2: Got offset for %s.%s.%s = %d", id.PkgPath, id.Struct, id.Field, off)
  451. }
  452. *field = off
  453. }
  454. klog.Infof("[AttachGoTlsUprobes] Field offset collection completed, success=%d, failed=%d", successCount, failCount)
  455. // 获取内存地址
  456. if appInfo.GoProcCache.StartAddr == 0 && appInfo.GoProcCache.EndAddr == 0 {
  457. klog.Infof("[AttachGoTlsUprobes] Cache empty, calling Allocate")
  458. allocDetails, allocErr := tracer.Allocate(int(pid))
  459. if allocErr != nil {
  460. return nil, allocErr
  461. }
  462. if allocDetails != nil {
  463. appInfo.GoProcCache.StartAddr = allocDetails.StartAddr
  464. appInfo.GoProcCache.EndAddr = allocDetails.EndAddr
  465. klog.Infof("[AttachGoTlsUprobes] Allocate succeeded, StartAddr=0x%x, EndAddr=0x%x", allocDetails.StartAddr, allocDetails.EndAddr)
  466. } else {
  467. klog.Warnf("[AttachGoTlsUprobes] Allocate returned nil")
  468. }
  469. } else {
  470. klog.Infof("[AttachGoTlsUprobes] Using cached addresses, StartAddr=0x%x, EndAddr=0x%x", appInfo.GoProcCache.StartAddr, appInfo.GoProcCache.EndAddr)
  471. }
  472. info.StartAddr = appInfo.GoProcCache.StartAddr
  473. info.EndAddr = appInfo.GoProcCache.EndAddr
  474. klog.Debugln("Major:", major)
  475. klog.Debugln("Minor:", minor)
  476. klog.Debugln("Revision:", revision)
  477. klog.Debugln("goVersion", goVersion)
  478. klog.WithField("pid", pid).Debugln("info.StartAddr", info.StartAddr)
  479. klog.WithField("pid", pid).Debugln("info.EndAddr", info.EndAddr)
  480. // 构建 sysvc 值:{app_name_len}:app_name:{appServiceType_len}:appServiceType:{SysTagLen}[:SysTag]
  481. // appServiceType 固定为 "APPLICATION"
  482. appName := appInfo.AppName
  483. nodeInfo := NODE_INFO.GetNodeInfo()
  484. sysTag := ""
  485. if nodeInfo != nil {
  486. sysTag = nodeInfo.SysTag
  487. }
  488. appServiceType := "APPLICATION"
  489. appNameLen := len(appName)
  490. appServiceTypeLen := len(appServiceType)
  491. sysTagLen := len(sysTag)
  492. // 限制长度,避免超出数组大小
  493. if appNameLen > 32 {
  494. appNameLen = 32
  495. appName = appName[:32]
  496. }
  497. if sysTagLen > 32 {
  498. sysTagLen = 32
  499. sysTag = sysTag[:32]
  500. }
  501. // 格式:{app_name_len}:app_name:{appServiceType_len}:appServiceType[:{SysTagLen}:SysTag]
  502. // 如果 sysTag 为空,则为 {app_name_len}:app_name:{appServiceType_len}:appServiceType
  503. // 如果 sysTag 不为空,则为 {app_name_len}:app_name:{appServiceType_len}:appServiceType:{SysTagLen}:SysTag
  504. // 长度不足2位时前面补0,例如:08:service:12:APPLICATION 或 08:service:12:APPLICATION:03:tag
  505. var sysvcStr string
  506. if sysTagLen == 0 {
  507. sysvcStr = fmt.Sprintf("%02d:%s:%02d:%s", appNameLen, appName, appServiceTypeLen, appServiceType)
  508. } else {
  509. sysvcStr = fmt.Sprintf("%02d:%s:%02d:%s:%02d:%s", appNameLen, appName, appServiceTypeLen, appServiceType, sysTagLen, sysTag)
  510. }
  511. // 复制到字节数组,确保不超过 76 字节
  512. sysvcBytes := []byte(sysvcStr)
  513. if len(sysvcBytes) > 76 {
  514. sysvcBytes = sysvcBytes[:76]
  515. }
  516. copy(info.Sysvc[:], sysvcBytes)
  517. klog.Debugf("[AttachGoTlsUprobes] Sysvc constructed: %s", sysvcStr)
  518. klog.Infof("[AttachGoTlsUprobes] Updating proc_info map")
  519. _, err = tracer.UpdateProcInfoToMap(t.collection, pid, info)
  520. if err != nil {
  521. klog.Error("failed to update program info", err)
  522. return nil, err
  523. }
  524. klog.Infof("[AttachGoTlsUprobes] Proc_info map updated successfully")
  525. appInfo.EBPFProcInfo = &info
  526. } else {
  527. klog.Errorf("[AttachGoTlsUprobes] Skipping proc_info initialization due to missing offsets, goid_ok=%v, buckets_ok=%v", ok, ok2)
  528. if !ok {
  529. klog.Errorf("[AttachGoTlsUprobes] runtime.g.goid offset missing - this is critical!")
  530. }
  531. if !ok2 {
  532. klog.Errorf("[AttachGoTlsUprobes] runtime.hmap.buckets offset missing - Go 1.24+ may use new map implementation")
  533. }
  534. }
  535. }
  536. klog.Infof("[AttachGoTlsUprobes] Starting symbol matching and uprobe attachment, total symbols=%d", len(symbols))
  537. var links []link.Link
  538. matchedSymbols := 0
  539. for i, s := range symbols {
  540. if elf.ST_TYPE(s.Info) != elf.STT_FUNC || s.Size == 0 {
  541. continue
  542. }
  543. switch s.Name {
  544. case goTlsWriteSymbol, goTlsReadSymbol:
  545. matchedSymbols++
  546. klog.Infof("[AttachGoTlsUprobes] STEP 19.1: Matched TLS symbol: %s (index=%d)", s.Name, i)
  547. case goExecute:
  548. matchedSymbols++
  549. klog.Infof("[AttachGoTlsUprobes] STEP 19.2: Matched runtime.execute symbol (index=%d)", i)
  550. case goNewproc1:
  551. matchedSymbols++
  552. klog.Infof("[AttachGoTlsUprobes] STEP 19.3: Matched runtime.newproc1 symbol (index=%d)", i)
  553. case goRunqget:
  554. matchedSymbols++
  555. klog.Infof("[AttachGoTlsUprobes] STEP 19.4: Matched runtime.runqget symbol (index=%d)", i)
  556. case goServeHTTP:
  557. matchedSymbols++
  558. klog.Infof("[AttachGoTlsUprobes] STEP 19.6: Matched net/http.serverHandler.ServeHTTP symbol (index=%d)", i)
  559. case goTransport, goHeaderWriteSubset:
  560. matchedSymbols++
  561. klog.Infof("[AttachGoTlsUprobes] STEP 19.7: Matched net/http.Transport.roundTrip writeSubset symbol (index=%d)", i)
  562. case goGrpcClientConnInvoke:
  563. matchedSymbols++
  564. klog.Infof("[AttachGoTlsUprobes] STEP 19.8: Matched gRPC ClientConn.Invoke symbol (index=%d)", i)
  565. case goGrpcHttp2OperateHeader, goGrpcServerHandleStream, goGrpcServerWritestatus, goGrpcClientLoopyHeaderHandler, goGrpcHttp2ClientNewStream:
  566. matchedSymbols++
  567. klog.Infof("[AttachGoTlsUprobes] STEP 19.9: Matched gRPC symbol: %s (index=%d)", s.Name, i)
  568. case goGocqlSessionExecuteQuery:
  569. matchedSymbols++
  570. klog.Infof("[AttachGoTlsUprobes] STEP 19.10: Matched gocql Session.executeQuery symbol (index=%d)", i)
  571. case goGocqlSessionExecuteQueryV2:
  572. matchedSymbols++
  573. klog.Infof("[AttachGoTlsUprobes] STEP 19.11: Matched cassandra Session.executeQuery symbol (index=%d)", i)
  574. case goKafkaWriterWriteMessages:
  575. matchedSymbols++
  576. klog.Infof("[AttachGoTlsUprobes] STEP 19.13: Matched kafka Writer.WriteMessages symbol (index=%d)", i)
  577. case goKafkaReaderFetchMessage:
  578. matchedSymbols++
  579. klog.Infof("[AttachGoTlsUprobes] STEP 19.14: Matched kafka Reader.FetchMessage symbol (index=%d)", i)
  580. case goBuntdbTxSet:
  581. matchedSymbols++
  582. klog.Infof("[AttachGoTlsUprobes] STEP 19.17: Matched buntdb Tx.Set symbol (index=%d)", i)
  583. case goBuntdbTxGet:
  584. matchedSymbols++
  585. klog.Infof("[AttachGoTlsUprobes] STEP 19.18: Matched buntdb Tx.Get symbol (index=%d)", i)
  586. case goBuntdbTxDelete:
  587. matchedSymbols++
  588. klog.Infof("[AttachGoTlsUprobes] STEP 19.19: Matched buntdb Tx.Delete symbol (index=%d)", i)
  589. case goLeveldbDBPutRec:
  590. // putRec 是 Put 和 Delete 的底层方法,通过 keyType 参数区分操作类型
  591. matchedSymbols++
  592. klog.Infof("[AttachGoTlsUprobes] STEP 19.20: Matched leveldb DB.putRec symbol (index=%d)", i)
  593. case goLeveldbDBGet:
  594. matchedSymbols++
  595. klog.Infof("[AttachGoTlsUprobes] STEP 19.21: Matched leveldb DB.Get symbol (index=%d)", i)
  596. case goReadContinuedLineSlice:
  597. default:
  598. continue
  599. }
  600. klog.Debugf("[AttachGoTlsUprobes] Processing symbol %s, Value=0x%x, Size=%d", s.Name, s.Value, s.Size)
  601. address := s.Value
  602. for _, p := range ef.Progs {
  603. if p.Type != elf.PT_LOAD || (p.Flags&elf.PF_X) == 0 {
  604. continue
  605. }
  606. if p.Vaddr <= s.Value && s.Value < (p.Vaddr+p.Memsz) {
  607. address = s.Value - p.Vaddr + p.Off
  608. break
  609. }
  610. }
  611. //fmt.Println("s.Name-----:", s.Name)
  612. switch s.Name {
  613. case goExecute:
  614. klog.Infof("[AttachGoTlsUprobes] STEP 20: Attaching uprobe for runtime.execute, address=0x%x", address)
  615. l, err := attachUprobe(exe, s.Name, "runtime_execute", t.uprobes, address, "failed to attach write_enter uprobe", true)
  616. if err != nil {
  617. klog.Infoln("runtime.execute no")
  618. return nil, err
  619. }
  620. klog.Infof("[AttachGoTlsUprobes] STEP 20.2: Successfully attached runtime.execute uprobe")
  621. klog.Infoln("runtime.execute ok")
  622. links = append(links, l)
  623. case goNewproc1:
  624. klog.Infof("[AttachGoTlsUprobes] STEP 21: Attaching uprobe for runtime.newproc1, address=0x%x", address)
  625. retLinks, err := attachUprobeWithReturns(exe, s.Name, "enter_runtime_newproc1", "exit_runtime_newproc1", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach newproc1 uprobe", true)
  626. if err != nil {
  627. log("failed to attach newproc1 uprobe", err)
  628. return nil, err
  629. }
  630. links = append(links, retLinks...)
  631. klog.Infof("[AttachGoTlsUprobes] STEP 21.2: Successfully attached enter_runtime_newproc1 uprobe")
  632. case goRunqget:
  633. l, err := attachUprobe(exe, s.Name, "enter_runtime_runqget", t.uprobes, address, "failed to attach goRunqget uprobe", true)
  634. if err != nil {
  635. log("failed to attach goRunqget uprobe", err)
  636. return nil, err
  637. }
  638. links = append(links, l)
  639. //case goGoready:
  640. //klog.Infof("[AttachGoTlsUprobes] STEP 22: Attaching uprobe for runtime.goready, address=0x%x", address)
  641. //l, err := attachUprobe(exe, s.Name, "runtime_goready", t.uprobes, address, "failed to attach runtime.goready uprobe", true)
  642. //if err != nil {
  643. // klog.Infoln("runtime.goready no")
  644. // return nil, err
  645. //}
  646. //klog.Infof("[AttachGoTlsUprobes] STEP 22.2: Successfully attached runtime.goready uprobe")
  647. //klog.Infoln("runtime.goready ok")
  648. //links = append(links, l)
  649. case goGrpcClientConnInvoke:
  650. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_ClientConn_Invoke", "uprobe_ClientConn_Invoke_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_ClientConn_Invoke uprobe", true)
  651. if err != nil {
  652. return nil, err
  653. }
  654. if retLinks != nil {
  655. klog.Infoln("uprobe_ClientConn_Invoke ok")
  656. links = append(links, retLinks...)
  657. }
  658. case goGrpcClientLoopyHeaderHandler:
  659. l, err := attachUprobe(exe, s.Name, "uprobe_LoopyWriter_HeaderHandler", t.uprobes, address, "failed to attach uprobe_LoopyWriter_HeaderHandler uprobe", false)
  660. if err == nil && l != nil {
  661. klog.Infoln("uprobe_LoopyWriter_HeaderHandler ok")
  662. links = append(links, l)
  663. }
  664. case goGrpcHttp2ClientNewStream:
  665. l, err := attachUprobe(exe, s.Name, "uprobe_http2Client_NewStream", t.uprobes, address, "failed to attach uprobe_http2Client_NewStream uprobe", false)
  666. if err == nil && l != nil {
  667. klog.Infoln("uprobe_http2Client_NewStream ok")
  668. links = append(links, l)
  669. }
  670. case goGrpcHttp2OperateHeader:
  671. l, err := attachUprobe(exe, s.Name, "uprobe_http2Server_operateHeader", t.uprobes, address, "failed to attach uprobe_http2Server_operateHeader uprobe", false)
  672. if err == nil && l != nil {
  673. klog.Infoln("uprobe_http2Server_operateHeader ok")
  674. links = append(links, l)
  675. }
  676. // case goGrpcServerWritestatus:
  677. // // 根据 gRPC 版本选择相应的 WriteStatus 探针
  678. // l, err := exe.Uprobe(s.Name, t.uprobes["uprobe_http2Server_WriteStatus"], &link.UprobeOptions{Address: address})
  679. // if err != nil {
  680. // klog.WithError(err).Errorf("failed to attach uprobe_http2Server_WriteStatus uprobe")
  681. // continue
  682. // }
  683. // links = append(links, l)
  684. case goGrpcServerHandleStream:
  685. // 根据 gRPC 版本选择相应的探针
  686. probeName := t.selectGRPCServerProbe(grpcMajorVersion, grpcMinorVersion)
  687. retLinks, err := attachUprobeWithReturns(exe, s.Name, probeName, "uprobe_server_handleStream_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, fmt.Sprintf("failed to attach %s uprobe", probeName), true)
  688. if err != nil {
  689. return nil, err
  690. }
  691. if retLinks != nil {
  692. klog.Infof("%s ok (gRPC v%d.%d)", probeName, grpcMajorVersion, grpcMinorVersion)
  693. klog.Infoln("google.golang.org/grpc.(*Server).handleStream ok----")
  694. links = append(links, retLinks...)
  695. }
  696. case goServeHTTP:
  697. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_HandlerFunc_ServeHTTP", "uprobe_HandlerFunc_ServeHTTP_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_HandlerFunc_ServeHTTP uprobe", true)
  698. if err != nil {
  699. return nil, err
  700. }
  701. if retLinks != nil {
  702. klog.Infoln("net/http.serverHandler.ServeHTTP ok")
  703. links = append(links, retLinks...)
  704. }
  705. case goTransport:
  706. if t.DisableE2ETracing() {
  707. continue
  708. }
  709. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_Transport_roundTrip", "uprobe_Transport_roundTrip_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach write_enter uprobe", true)
  710. if err != nil {
  711. return nil, err
  712. }
  713. if retLinks != nil {
  714. klog.Infoln("net/http.uprobe_Transport_roundTrip ok")
  715. links = append(links, retLinks...)
  716. }
  717. case goHeaderWriteSubset:
  718. if t.DisableE2ETracing() {
  719. continue
  720. }
  721. l, err := attachUprobe(exe, s.Name, "uprobe_writeSubset", t.uprobes, address, "failed to attach write_enter uprobe", true)
  722. if err != nil {
  723. klog.WithError(err).Errorln("failed to attach uprobe_writeSubset")
  724. return nil, err
  725. }
  726. klog.Infoln("net/http.Header.writeSubset ok")
  727. links = append(links, l)
  728. case goTlsWriteSymbol:
  729. klog.Infoln("fucktls goTlsWriteSymbol crypto/tls uprobes attached")
  730. l, err := attachUprobe(exe, s.Name, "go_crypto_tls_write_enter", t.uprobes, address, "failed to attach write_enter uprobe", true)
  731. if err != nil {
  732. klog.WithError(err).Errorln("failed to attach write_enter uprobe")
  733. return nil, err
  734. }
  735. links = append(links, l)
  736. case goTlsReadSymbol:
  737. klog.Infoln("fucktls goTlsReadSymbol crypto/tls uprobes attached")
  738. retLinks, err := attachUprobeWithReturns(exe, s.Name, "go_crypto_tls_read_enter", "go_crypto_tls_read_exit", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach read_enter uprobe", true)
  739. if err != nil {
  740. klog.WithError(err).Errorln("failed to attach read_enter uprobe")
  741. return nil, err
  742. }
  743. if retLinks != nil {
  744. links = append(links, retLinks...)
  745. }
  746. case goReadContinuedLineSlice:
  747. //if major == 1 && minor >= 24 {
  748. retLinks, err := attachUprobeWithReturns(exe, s.Name, "", "uprobe_textproto_Reader_readContinuedLineSlice_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach read_exit uprobe", true)
  749. if err != nil {
  750. return nil, err
  751. }
  752. if retLinks != nil {
  753. links = append(links, retLinks...)
  754. }
  755. //}
  756. case goGocqlSessionExecuteQuery:
  757. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_Session_executeQuery", "uprobe_Session_executeQuery_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_Session_executeQuery uprobe", true)
  758. if err != nil {
  759. return nil, err
  760. }
  761. if retLinks != nil {
  762. klog.Infoln("uprobe_Session_executeQuery ok")
  763. links = append(links, retLinks...)
  764. }
  765. case goGocqlSessionExecuteQueryV2:
  766. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_Session_executeQuery_cassandra", "uprobe_Session_executeQuery_cassandra_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_Session_executeQuery_cassandra uprobe", true)
  767. if err != nil {
  768. return nil, err
  769. }
  770. if retLinks != nil {
  771. klog.Infoln("uprobe_Session_executeQuery_cassandra ok")
  772. links = append(links, retLinks...)
  773. }
  774. case goKafkaWriterWriteMessages:
  775. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_WriteMessages", "uprobe_WriteMessages_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_WriteMessages uprobe", true)
  776. if err != nil {
  777. return nil, err
  778. }
  779. if retLinks != nil {
  780. klog.Infoln("uprobe_WriteMessages ok")
  781. links = append(links, retLinks...)
  782. }
  783. case goKafkaReaderFetchMessage:
  784. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_FetchMessage", "uprobe_FetchMessage_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_FetchMessage uprobe", true)
  785. if err != nil {
  786. return nil, err
  787. }
  788. if retLinks != nil {
  789. klog.Infoln("uprobe_FetchMessage ok")
  790. links = append(links, retLinks...)
  791. }
  792. case goBuntdbTxSet:
  793. // Tx.Set 需要入口和返回探针,每个操作生成独立的 span
  794. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_Tx_Set", "uprobe_Tx_Set_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_Tx_Set uprobe", true)
  795. if err != nil {
  796. return nil, err
  797. }
  798. if retLinks != nil {
  799. klog.Infoln("uprobe_Tx_Set ok")
  800. links = append(links, retLinks...)
  801. }
  802. case goBuntdbTxGet:
  803. // Tx.Get 需要入口和返回探针,每个操作生成独立的 span
  804. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_Tx_Get", "uprobe_Tx_Get_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_Tx_Get uprobe", true)
  805. if err != nil {
  806. return nil, err
  807. }
  808. if retLinks != nil {
  809. klog.Infoln("uprobe_Tx_Get ok")
  810. links = append(links, retLinks...)
  811. }
  812. case goBuntdbTxDelete:
  813. // Tx.Delete 需要入口和返回探针,每个操作生成独立的 span
  814. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_Tx_Delete", "uprobe_Tx_Delete_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_Tx_Delete uprobe", true)
  815. if err != nil {
  816. return nil, err
  817. }
  818. if retLinks != nil {
  819. klog.Infoln("uprobe_Tx_Delete ok")
  820. links = append(links, retLinks...)
  821. }
  822. case goLeveldbDBPutRec:
  823. // DB.putRec 是 Put 和 Delete 的底层方法,需要入口和返回探针
  824. // 通过 keyType 参数(第一个参数)区分是 Put 还是 Delete 操作
  825. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_DB_putRec", "uprobe_DB_putRec_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_DB_putRec uprobe", true)
  826. if err != nil {
  827. return nil, err
  828. }
  829. if retLinks != nil {
  830. klog.Infoln("uprobe_DB_putRec ok")
  831. links = append(links, retLinks...)
  832. }
  833. case goLeveldbDBGet:
  834. // DB.Get 需要入口和返回探针,每个操作生成独立的 span
  835. retLinks, err := attachUprobeWithReturns(exe, s.Name, "uprobe_DB_Get", "uprobe_DB_Get_Returns", t.uprobes, address, s, textSection, textSectionLen, textSectionData, ef.Machine, "failed to attach uprobe_DB_Get uprobe", true)
  836. if err != nil {
  837. return nil, err
  838. }
  839. if retLinks != nil {
  840. klog.Infoln("uprobe_DB_Get ok")
  841. links = append(links, retLinks...)
  842. }
  843. }
  844. }
  845. klog.Infof("[AttachGoTlsUprobes] Symbol processing completed, matched symbols=%d, total links=%d", matchedSymbols, len(links))
  846. if len(links) == 0 {
  847. klog.Errorf("[AttachGoTlsUprobes] No uprobes attached, returning error")
  848. return nil, err
  849. }
  850. klog.Infof("[AttachGoTlsUprobes] Function completed successfully, attached %d uprobes", len(links))
  851. klog.Infoln("crypto/tls uprobes attached")
  852. return links, nil
  853. }
  854. func getSslLibPathAndVersion(pid uint32) (string, string) {
  855. f, err := os.Open(proc.Path(pid, "maps"))
  856. if err != nil {
  857. return "", ""
  858. }
  859. defer f.Close()
  860. scanner := bufio.NewScanner(f)
  861. scanner.Split(bufio.ScanLines)
  862. var libsslPath, libcryptoPath string
  863. for scanner.Scan() {
  864. parts := strings.Fields(scanner.Text())
  865. if len(parts) <= 5 {
  866. continue
  867. }
  868. libPath := parts[5]
  869. switch {
  870. case libsslPath == "" && strings.Contains(libPath, "libssl.so"):
  871. fullPath := proc.Path(pid, "root", libPath)
  872. if _, err = os.Stat(fullPath); err == nil {
  873. libsslPath = fullPath
  874. }
  875. case libcryptoPath == "" && strings.Contains(libPath, "libcrypto.so"):
  876. fullPath := proc.Path(pid, "root", libPath)
  877. if _, err = os.Stat(fullPath); err == nil {
  878. libcryptoPath = fullPath
  879. }
  880. default:
  881. continue
  882. }
  883. if libsslPath != "" && libcryptoPath != "" {
  884. break
  885. }
  886. }
  887. if libsslPath == "" || libcryptoPath == "" {
  888. return "", ""
  889. }
  890. ef, err := elf.Open(libcryptoPath)
  891. if err != nil {
  892. return "", ""
  893. }
  894. defer ef.Close()
  895. rodataSection := ef.Section(".rodata")
  896. if rodataSection == nil {
  897. return "", ""
  898. }
  899. rodataSectionData, err := rodataSection.Data()
  900. if err != nil {
  901. return "", ""
  902. }
  903. var version string
  904. for _, b := range bytes.Split(rodataSectionData, []byte("\x00")) {
  905. if len(b) == 0 {
  906. continue
  907. }
  908. s := string(b)
  909. if !strings.HasPrefix(s, "OpenSSL") {
  910. continue
  911. }
  912. if m := opensslVersionRe.FindStringSubmatch(s); len(m) > 1 {
  913. version = m[1]
  914. }
  915. }
  916. return libsslPath, "v" + version
  917. }
  918. // selectGRPCServerProbe 根据 gRPC 版本选择服务端探针
  919. func (t *Tracer) selectGRPCServerProbe(major, minor int) string {
  920. // 根据 gRPC 版本选择相应的探针
  921. if major == 1 && minor >= 69 {
  922. // 现代版本 (>= 1.69.0) 使用新的探针
  923. klog.Infof("Selecting modern gRPC server probe for version %d.%d", major, minor)
  924. return "uprobe_server_handleStream2"
  925. } else {
  926. // 传统版本 (< 1.69.0) 使用旧的探针
  927. klog.Infof("Selecting legacy gRPC server probe for version %d.%d", major, minor)
  928. return "uprobe_server_handleStream"
  929. }
  930. }
  931. // attachUprobe 附加单个 uprobe,处理错误
  932. func attachUprobe(exe *link.Executable, symbolName string, probeName string, uprobes map[string]*ebpf.Program, address uint64, onError string, returnOnError bool) (link.Link, error) {
  933. l, err := exe.Uprobe(symbolName, uprobes[probeName], &link.UprobeOptions{Address: address})
  934. if err != nil {
  935. if returnOnError {
  936. klog.WithError(err).Errorf("failed to attach %s uprobe: %s", probeName, onError)
  937. return nil, err
  938. } else {
  939. klog.WithError(err).Errorf("failed to attach %s uprobe: %s", probeName, onError)
  940. return nil, nil
  941. }
  942. }
  943. return l, nil
  944. }
  945. // attachUprobeWithReturns 附加 uprobe 并附加返回探针
  946. // enterProbeName 为空字符串时,只附加返回探针
  947. func attachUprobeWithReturns(exe *link.Executable, symbolName string, enterProbeName, returnProbeName string, uprobes map[string]*ebpf.Program, address uint64, s elf.Symbol, textSection *elf.Section, textSectionLen uint64, textSectionData []byte, machine elf.Machine, onError string, returnOnError bool) ([]link.Link, error) {
  948. var links []link.Link
  949. // 附加入口探针(如果提供了入口探针名称)
  950. if enterProbeName != "" {
  951. l, err := attachUprobe(exe, symbolName, enterProbeName, uprobes, address, onError, returnOnError)
  952. if err != nil {
  953. return nil, err
  954. }
  955. if l == nil {
  956. return nil, nil
  957. }
  958. links = append(links, l)
  959. }
  960. // 计算符号在 text section 中的位置
  961. sStart := s.Value - textSection.Addr
  962. sEnd := sStart + s.Size
  963. if sEnd > textSectionLen {
  964. return links, nil
  965. }
  966. // 读取符号字节码
  967. sBytes := textSectionData[sStart:sEnd]
  968. returnOffsets := getReturnOffsets(machine, sBytes)
  969. if len(returnOffsets) == 0 {
  970. if returnOnError {
  971. err := fmt.Errorf("failed to attach %s: no return offsets found", returnProbeName)
  972. klog.Errorln(err)
  973. return nil, err
  974. }
  975. return links, nil
  976. }
  977. // 为每个返回点附加探针
  978. for _, offset := range returnOffsets {
  979. l, err := exe.Uprobe(symbolName, uprobes[returnProbeName], &link.UprobeOptions{Address: address, Offset: uint64(offset)})
  980. if err != nil {
  981. if returnOnError {
  982. klog.WithError(err).Errorf("failed to attach %s uprobe", returnProbeName)
  983. return nil, err
  984. }
  985. continue
  986. }
  987. links = append(links, l)
  988. }
  989. return links, nil
  990. }
  991. func getReturnOffsets(machine elf.Machine, instructions []byte) []int {
  992. var res []int
  993. switch machine {
  994. case elf.EM_X86_64:
  995. for i := 0; i < len(instructions); {
  996. ins, err := x86asm.Decode(instructions[i:], 64)
  997. if err == nil && ins.Op == x86asm.RET {
  998. res = append(res, i)
  999. }
  1000. i += ins.Len
  1001. }
  1002. case elf.EM_AARCH64:
  1003. for i := 0; i < len(instructions); {
  1004. ins, err := arm64asm.Decode(instructions[i:])
  1005. if err == nil && ins.Op == arm64asm.RET {
  1006. res = append(res, i)
  1007. }
  1008. i += 4
  1009. }
  1010. }
  1011. return res
  1012. }
  1013. func min(a, b int) int {
  1014. if a < b {
  1015. return a
  1016. }
  1017. return b
  1018. }