stack_tags_main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build !js
  2. package gls
  3. // This file is used for standard Go builds, which have the expected runtime support
  4. import (
  5. "reflect"
  6. "runtime"
  7. )
  8. func init() {
  9. setEntries := func(f func(uint, func()), v int8) {
  10. pc_lookup[reflect.ValueOf(f).Pointer()] = v
  11. if v >= 0 {
  12. mark_lookup[v] = f
  13. }
  14. }
  15. setEntries(markS, -0x1)
  16. setEntries(mark0, 0x0)
  17. setEntries(mark1, 0x1)
  18. setEntries(mark2, 0x2)
  19. setEntries(mark3, 0x3)
  20. setEntries(mark4, 0x4)
  21. setEntries(mark5, 0x5)
  22. setEntries(mark6, 0x6)
  23. setEntries(mark7, 0x7)
  24. setEntries(mark8, 0x8)
  25. setEntries(mark9, 0x9)
  26. setEntries(markA, 0xa)
  27. setEntries(markB, 0xb)
  28. setEntries(markC, 0xc)
  29. setEntries(markD, 0xd)
  30. setEntries(markE, 0xe)
  31. setEntries(markF, 0xf)
  32. }
  33. func currentStack(skip int) []uintptr {
  34. stack := make([]uintptr, maxCallers)
  35. return stack[:runtime.Callers(3+skip, stack)]
  36. }
  37. func readStackTags(skip int) (tags []uint) {
  38. stack := currentStack(skip)
  39. var current_tag uint
  40. for _, pc := range stack {
  41. pc = runtime.FuncForPC(pc).Entry()
  42. val, ok := pc_lookup[pc]
  43. if !ok {
  44. continue
  45. }
  46. if val < 0 {
  47. tags = append(tags, current_tag)
  48. current_tag = 0
  49. continue
  50. }
  51. current_tag <<= bitWidth
  52. current_tag += uint(val)
  53. }
  54. return
  55. }