sync.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package pkgbits
  5. import (
  6. "fmt"
  7. "runtime"
  8. "strings"
  9. )
  10. // fmtFrames formats a backtrace for reporting reader/writer desyncs.
  11. func fmtFrames(pcs ...uintptr) []string {
  12. res := make([]string, 0, len(pcs))
  13. walkFrames(pcs, func(file string, line int, name string, offset uintptr) {
  14. // Trim package from function name. It's just redundant noise.
  15. name = strings.TrimPrefix(name, "cmd/compile/internal/noder.")
  16. res = append(res, fmt.Sprintf("%s:%v: %s +0x%v", file, line, name, offset))
  17. })
  18. return res
  19. }
  20. type frameVisitor func(file string, line int, name string, offset uintptr)
  21. // walkFrames calls visit for each call frame represented by pcs.
  22. //
  23. // pcs should be a slice of PCs, as returned by runtime.Callers.
  24. func walkFrames(pcs []uintptr, visit frameVisitor) {
  25. if len(pcs) == 0 {
  26. return
  27. }
  28. frames := runtime.CallersFrames(pcs)
  29. for {
  30. frame, more := frames.Next()
  31. visit(frame.File, frame.Line, frame.Function, frame.PC-frame.Entry)
  32. if !more {
  33. return
  34. }
  35. }
  36. }
  37. // SyncMarker is an enum type that represents markers that may be
  38. // written to export data to ensure the reader and writer stay
  39. // synchronized.
  40. type SyncMarker int
  41. //go:generate stringer -type=SyncMarker -trimprefix=Sync
  42. const (
  43. _ SyncMarker = iota
  44. // Public markers (known to go/types importers).
  45. // Low-level coding markers.
  46. SyncEOF
  47. SyncBool
  48. SyncInt64
  49. SyncUint64
  50. SyncString
  51. SyncValue
  52. SyncVal
  53. SyncRelocs
  54. SyncReloc
  55. SyncUseReloc
  56. // Higher-level object and type markers.
  57. SyncPublic
  58. SyncPos
  59. SyncPosBase
  60. SyncObject
  61. SyncObject1
  62. SyncPkg
  63. SyncPkgDef
  64. SyncMethod
  65. SyncType
  66. SyncTypeIdx
  67. SyncTypeParamNames
  68. SyncSignature
  69. SyncParams
  70. SyncParam
  71. SyncCodeObj
  72. SyncSym
  73. SyncLocalIdent
  74. SyncSelector
  75. // Private markers (only known to cmd/compile).
  76. SyncPrivate
  77. SyncFuncExt
  78. SyncVarExt
  79. SyncTypeExt
  80. SyncPragma
  81. SyncExprList
  82. SyncExprs
  83. SyncExpr
  84. SyncExprType
  85. SyncAssign
  86. SyncOp
  87. SyncFuncLit
  88. SyncCompLit
  89. SyncDecl
  90. SyncFuncBody
  91. SyncOpenScope
  92. SyncCloseScope
  93. SyncCloseAnotherScope
  94. SyncDeclNames
  95. SyncDeclName
  96. SyncStmts
  97. SyncBlockStmt
  98. SyncIfStmt
  99. SyncForStmt
  100. SyncSwitchStmt
  101. SyncRangeStmt
  102. SyncCaseClause
  103. SyncCommClause
  104. SyncSelectStmt
  105. SyncDecls
  106. SyncLabeledStmt
  107. SyncUseObjLocal
  108. SyncAddLocal
  109. SyncLinkname
  110. SyncStmt1
  111. SyncStmtsEnd
  112. SyncLabel
  113. SyncOptLabel
  114. SyncMultiExpr
  115. SyncRType
  116. SyncConvRTTI
  117. )