|
| 1 | +package traceparser |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strconv" |
| 6 | +) |
| 7 | + |
| 8 | +var blockSeparator = []byte("\n\n") |
| 9 | +var lineSeparator = []byte("\n") |
| 10 | + |
| 11 | +// Parses multi-stacktrace text dump produced by runtime.Stack([]byte, all=true). |
| 12 | +// The parser prioritizes performance but requires the input to be well-formed in order to return correct data. |
| 13 | +// See https://github.com/golang/go/blob/go1.20.4/src/runtime/mprof.go#L1191 |
| 14 | +func Parse(data []byte) TraceCollection { |
| 15 | + var it = TraceCollection{} |
| 16 | + if len(data) > 0 { |
| 17 | + it.blocks = bytes.Split(data, blockSeparator) |
| 18 | + } |
| 19 | + return it |
| 20 | +} |
| 21 | + |
| 22 | +type TraceCollection struct { |
| 23 | + blocks [][]byte |
| 24 | +} |
| 25 | + |
| 26 | +func (it TraceCollection) Length() int { |
| 27 | + return len(it.blocks) |
| 28 | +} |
| 29 | + |
| 30 | +// Returns the stacktrace item at the given index. |
| 31 | +func (it *TraceCollection) Item(i int) Trace { |
| 32 | + // The first item may have a leading data separator and the last one may have a trailing one. |
| 33 | + // Note: Trim() doesn't make a copy for single-character cutset under 0x80. It will just slice the original. |
| 34 | + var data []byte |
| 35 | + switch { |
| 36 | + case i == 0: |
| 37 | + data = bytes.TrimLeft(it.blocks[i], "\n") |
| 38 | + case i == len(it.blocks)-1: |
| 39 | + data = bytes.TrimRight(it.blocks[i], "\n") |
| 40 | + default: |
| 41 | + data = it.blocks[i] |
| 42 | + } |
| 43 | + |
| 44 | + var splitAt = bytes.IndexByte(data, '\n') |
| 45 | + if splitAt < 0 { |
| 46 | + return Trace{header: data} |
| 47 | + } |
| 48 | + |
| 49 | + return Trace{ |
| 50 | + header: data[:splitAt], |
| 51 | + data: data[splitAt+1:], |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Trace represents a single stacktrace block, identified by a Goroutine ID and a sequence of Frames. |
| 56 | +type Trace struct { |
| 57 | + header []byte |
| 58 | + data []byte |
| 59 | +} |
| 60 | + |
| 61 | +var goroutinePrefix = []byte("goroutine ") |
| 62 | + |
| 63 | +// GoID parses the Goroutine ID from the header. |
| 64 | +func (t *Trace) GoID() (id uint64) { |
| 65 | + if bytes.HasPrefix(t.header, goroutinePrefix) { |
| 66 | + var line = t.header[len(goroutinePrefix):] |
| 67 | + var splitAt = bytes.IndexByte(line, ' ') |
| 68 | + if splitAt >= 0 { |
| 69 | + id, _ = strconv.ParseUint(string(line[:splitAt]), 10, 64) |
| 70 | + } |
| 71 | + } |
| 72 | + return id |
| 73 | +} |
| 74 | + |
| 75 | +// UniqueIdentifier can be used as a map key to identify the trace. |
| 76 | +func (t *Trace) UniqueIdentifier() []byte { |
| 77 | + return t.data |
| 78 | +} |
| 79 | + |
| 80 | +func (t *Trace) Frames() FrameIterator { |
| 81 | + var lines = bytes.Split(t.data, lineSeparator) |
| 82 | + return FrameIterator{lines: lines, i: 0, len: len(lines)} |
| 83 | +} |
| 84 | + |
| 85 | +func (t *Trace) FramesReversed() ReverseFrameIterator { |
| 86 | + var lines = bytes.Split(t.data, lineSeparator) |
| 87 | + return ReverseFrameIterator{lines: lines, i: len(lines)} |
| 88 | +} |
| 89 | + |
| 90 | +const framesElided = "...additional frames elided..." |
| 91 | + |
| 92 | +// FrameIterator iterates over stack frames. |
| 93 | +type FrameIterator struct { |
| 94 | + lines [][]byte |
| 95 | + i int |
| 96 | + len int |
| 97 | +} |
| 98 | + |
| 99 | +// Next returns the next frame, or nil if there are none. |
| 100 | +func (it *FrameIterator) Next() Frame { |
| 101 | + return Frame{it.popLine(), it.popLine()} |
| 102 | +} |
| 103 | + |
| 104 | +func (it *FrameIterator) popLine() []byte { |
| 105 | + switch { |
| 106 | + case it.i >= it.len: |
| 107 | + return nil |
| 108 | + case string(it.lines[it.i]) == framesElided: |
| 109 | + it.i++ |
| 110 | + return it.popLine() |
| 111 | + default: |
| 112 | + it.i++ |
| 113 | + return it.lines[it.i-1] |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// HasNext return true if there are values to be read. |
| 118 | +func (it *FrameIterator) HasNext() bool { |
| 119 | + return it.i < it.len |
| 120 | +} |
| 121 | + |
| 122 | +// LengthUpperBound returns the maximum number of elements this stacks may contain. |
| 123 | +// The actual number may be lower because of elided frames. As such, the returned value |
| 124 | +// cannot be used to iterate over the frames but may be used to reserve capacity. |
| 125 | +func (it *FrameIterator) LengthUpperBound() int { |
| 126 | + return it.len / 2 |
| 127 | +} |
| 128 | + |
| 129 | +// ReverseFrameIterator iterates over stack frames in reverse order. |
| 130 | +type ReverseFrameIterator struct { |
| 131 | + lines [][]byte |
| 132 | + i int |
| 133 | +} |
| 134 | + |
| 135 | +// Next returns the next frame, or nil if there are none. |
| 136 | +func (it *ReverseFrameIterator) Next() Frame { |
| 137 | + var line2 = it.popLine() |
| 138 | + return Frame{it.popLine(), line2} |
| 139 | +} |
| 140 | + |
| 141 | +func (it *ReverseFrameIterator) popLine() []byte { |
| 142 | + it.i-- |
| 143 | + switch { |
| 144 | + case it.i < 0: |
| 145 | + return nil |
| 146 | + case string(it.lines[it.i]) == framesElided: |
| 147 | + return it.popLine() |
| 148 | + default: |
| 149 | + return it.lines[it.i] |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// HasNext return true if there are values to be read. |
| 154 | +func (it *ReverseFrameIterator) HasNext() bool { |
| 155 | + return it.i > 1 |
| 156 | +} |
| 157 | + |
| 158 | +// LengthUpperBound returns the maximum number of elements this stacks may contain. |
| 159 | +// The actual number may be lower because of elided frames. As such, the returned value |
| 160 | +// cannot be used to iterate over the frames but may be used to reserve capacity. |
| 161 | +func (it *ReverseFrameIterator) LengthUpperBound() int { |
| 162 | + return len(it.lines) / 2 |
| 163 | +} |
| 164 | + |
| 165 | +type Frame struct { |
| 166 | + line1 []byte |
| 167 | + line2 []byte |
| 168 | +} |
| 169 | + |
| 170 | +// UniqueIdentifier can be used as a map key to identify the frame. |
| 171 | +func (f *Frame) UniqueIdentifier() []byte { |
| 172 | + // line2 contains file path, line number and program-counter offset from the beginning of a function |
| 173 | + // e.g. C:/Users/name/scoop/apps/go/current/src/testing/testing.go:1906 +0x63a |
| 174 | + return f.line2 |
| 175 | +} |
| 176 | + |
| 177 | +var createdByPrefix = []byte("created by ") |
| 178 | + |
| 179 | +func (f *Frame) Func() []byte { |
| 180 | + if bytes.HasPrefix(f.line1, createdByPrefix) { |
| 181 | + return f.line1[len(createdByPrefix):] |
| 182 | + } |
| 183 | + |
| 184 | + var end = bytes.LastIndexByte(f.line1, '(') |
| 185 | + if end >= 0 { |
| 186 | + return f.line1[:end] |
| 187 | + } |
| 188 | + |
| 189 | + return f.line1 |
| 190 | +} |
| 191 | + |
| 192 | +func (f *Frame) File() (path []byte, lineNumber int) { |
| 193 | + var line = f.line2 |
| 194 | + if len(line) > 0 && line[0] == '\t' { |
| 195 | + line = line[1:] |
| 196 | + } |
| 197 | + |
| 198 | + var splitAt = bytes.IndexByte(line, ' ') |
| 199 | + if splitAt >= 0 { |
| 200 | + line = line[:splitAt] |
| 201 | + } |
| 202 | + |
| 203 | + splitAt = bytes.LastIndexByte(line, ':') |
| 204 | + if splitAt < 0 { |
| 205 | + return line, 0 |
| 206 | + } |
| 207 | + |
| 208 | + lineNumber, _ = strconv.Atoi(string(line[splitAt+1:])) |
| 209 | + return line[:splitAt], lineNumber |
| 210 | +} |
0 commit comments