-
Notifications
You must be signed in to change notification settings - Fork 4
/
dwarf.go
414 lines (361 loc) · 9.96 KB
/
dwarf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package wzprof
import (
"debug/dwarf"
"errors"
"fmt"
"io"
"log"
"math"
"sort"
"sync"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
)
// buildDwarfSymbolizer constructs a Symbolizer instance from the DWARF sections
// of the given WebAssembly module.
func buildDwarfSymbolizer(parser dwarfparser) symbolizer {
return newDwarfmapper(parser)
}
type sourceOffsetRange = [2]uint64
type subprogram struct {
Entry *dwarf.Entry
CU *dwarf.Entry
Inlines []entryRanges
Namespace string
}
type entryRanges struct {
entry *dwarf.Entry
ranges []sourceOffsetRange
}
type subprogramRange struct {
Range sourceOffsetRange
Subprogram *subprogram
}
type dwarfmapper struct {
d *dwarf.Data
subprograms []subprogramRange
// once value used to limit the logging output on error
onceSourceOffsetNotFound sync.Once
}
const (
debugInfo = ".debug_info"
debugLine = ".debug_line"
debugStr = ".debug_str"
debugAbbrev = ".debug_abbrev"
debugRanges = ".debug_ranges"
)
func newDwarfparser(module wazero.CompiledModule) (dwarfparser, error) {
sections := module.CustomSections()
var info, line, ranges, str, abbrev []byte
for _, section := range sections {
log.Printf("dwarf: found section %s", section.Name())
switch section.Name() {
case debugInfo:
info = section.Data()
case debugLine:
line = section.Data()
case debugStr:
str = section.Data()
case debugAbbrev:
abbrev = section.Data()
case debugRanges:
ranges = section.Data()
}
}
d, err := dwarf.New(abbrev, nil, nil, info, line, nil, ranges, str)
if err != nil {
return dwarfparser{}, fmt.Errorf("dwarf: %w", err)
}
r := d.Reader()
return dwarfparser{d: d, r: r}, nil
}
func newDwarfParserFromBin(wasmbin []byte) (dwarfparser, error) {
info := wasmCustomSection(wasmbin, debugInfo)
line := wasmCustomSection(wasmbin, debugLine)
ranges := wasmCustomSection(wasmbin, debugRanges)
str := wasmCustomSection(wasmbin, debugStr)
abbrev := wasmCustomSection(wasmbin, debugAbbrev)
d, err := dwarf.New(abbrev, nil, nil, info, line, nil, ranges, str)
if err != nil {
return dwarfparser{}, fmt.Errorf("dwarf: %w", err)
}
r := d.Reader()
return dwarfparser{d: d, r: r}, nil
}
func newDwarfmapper(p dwarfparser) *dwarfmapper {
subprograms := p.Parse()
log.Printf("dwarf: parsed %d subprogramm ranges", len(subprograms))
return &dwarfmapper{
d: p.d,
subprograms: subprograms,
}
}
type dwarfparser struct {
d *dwarf.Data
r *dwarf.Reader
subprograms []subprogramRange
}
func (d *dwarfparser) Parse() []subprogramRange {
for {
ent, err := d.r.Next()
if err != nil || ent == nil {
break
}
if ent.Tag == dwarf.TagCompileUnit {
d.parseCompileUnit(ent, "")
} else {
d.r.SkipChildren()
}
}
return d.subprograms
}
func (d *dwarfparser) parseCompileUnit(cu *dwarf.Entry, ns string) {
// Assumption is that r has just read the top level entry of the CU (or
// possibly a namespace), that is cu.
d.parseAny(cu, ns, cu)
}
func (d *dwarfparser) parseAny(cu *dwarf.Entry, ns string, e *dwarf.Entry) {
// Assumption is that r has just read the top level entry e.
for e.Children {
ent, err := d.r.Next()
if err != nil || ent == nil {
return
}
switch ent.Tag {
case 0:
// end of block
return
case dwarf.TagSubprogram:
d.parseSubprogram(cu, ns, ent)
case dwarf.TagNamespace:
d.parseNamespace(cu, ns, ent)
default:
d.parseAny(cu, ns, ent)
}
}
}
func (d *dwarfparser) parseNamespace(cu *dwarf.Entry, ns string, e *dwarf.Entry) {
// Assumption is that r has just read the top level entry of this
// namespace, which is e.
name, ok := e.Val(dwarf.AttrName).(string)
if ok {
ns += name + ":"
}
d.parseCompileUnit(cu, ns)
}
func (d *dwarfparser) parseSubprogram(cu *dwarf.Entry, ns string, e *dwarf.Entry) {
// Assumption is r has just read the top entry of the subprogram, which
// is e.
var inlines []entryRanges
for e.Children {
ent, err := d.r.Next()
if err != nil || ent == nil {
break
}
if ent.Tag == 0 {
break
}
if ent.Tag != dwarf.TagInlinedSubroutine {
d.r.SkipChildren()
continue
}
ranges, err := d.d.Ranges(ent)
if err != nil {
d.r.SkipChildren()
continue
}
inlines = append(inlines, entryRanges{ent, ranges})
// Inlines can have children that describe which variables were
// used during inlining.
d.r.SkipChildren()
}
ranges, err := d.d.Ranges(e)
if err != nil {
log.Printf("dwarf: failed to read ranges: %s\n", err)
return
}
spgm := &subprogram{
Entry: e,
CU: cu,
Inlines: inlines,
Namespace: ns,
}
if len(ranges) == 0 {
// If there is no range provided by dwarf, attach this
// subprogram to an artificial empty range unlikely to be used.
// This is so that we still have a record of the function in the
// subprograms collection, as that's where the name resolution
// for inline functions searches for the inlined function.
// Notably, it's likely that a subprogram without range
// represent a function that has only been inlined. This
// situation is temporary until we rework the subprograms data
// structure.
ranges = append(ranges, sourceOffsetRange{math.MaxUint64, math.MaxUint64})
}
for _, pcr := range ranges {
d.subprograms = append(d.subprograms, subprogramRange{
Range: pcr,
Subprogram: spgm,
})
}
}
func (d *dwarfmapper) Locations(fn experimental.InternalFunction, pc experimental.ProgramCounter) (uint64, []location) {
offset := fn.SourceOffsetForPC(pc)
if offset == 0 {
return offset, nil
}
// TODO: replace with binary search
var spgm *subprogram
for _, sr := range d.subprograms {
if sr.Range[0] <= offset && offset <= sr.Range[1] {
spgm = sr.Subprogram
break
}
}
if spgm == nil {
d.onceSourceOffsetNotFound.Do(func() {
log.Printf("dwarf: no subprogram ranges found for source offset %d (silencing similar errors now)", offset)
})
return offset, nil
}
lr, err := d.d.LineReader(spgm.CU)
if err != nil || lr == nil {
log.Printf("dwarf: failed to read lines: %s\n", err)
return offset, nil
}
// TODO: cache this
var lines []line
var le dwarf.LineEntry
for {
pos := lr.Tell()
err = lr.Next(&le)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
log.Printf("dwarf: failed to iterate on lines: %s\n", err)
break
}
lines = append(lines, line{Pos: pos, Address: le.Address})
}
sort.Slice(lines, func(i, j int) bool { return lines[i].Address < lines[j].Address })
i := sort.Search(len(lines), func(i int) bool { return lines[i].Address >= offset })
if i == len(lines) {
// no line information for this source offset.
log.Printf("dwarf: no line information for source offset %d", offset)
return offset, nil
}
l := lines[i]
if l.Address != offset {
// https://github.com/stealthrocket/wazero/blob/867459d7d5ed988a55452d6317ff3cc8451b8ff0/internal/wasmdebug/dwarf.go#L141-L150
// If the address doesn't match exactly, the previous
// entry is the one that contains the instruction.
// That can happen anytime as the DWARF spec allows
// it, and other tools can handle it in this way
// conventionally
// https://github.com/gimli-rs/addr2line/blob/3a2dbaf84551a06a429f26e9c96071bb409b371f/src/lib.rs#L236-L242
// https://github.com/kateinoigakukun/wasminspect/blob/f29f052f1b03104da9f702508ac0c1bbc3530ae4/crates/debugger/src/dwarf/mod.rs#L453-L459
if i-1 < 0 {
log.Printf("dwarf: first line address does not match source (line=%d offset=%d)", l.Address, offset)
return offset, nil
}
l = lines[i-1]
}
lr.Seek(l.Pos)
err = lr.Next(&le)
if err != nil {
// l.Pos was created from parsing dwarf, should not
// happen.
panic("BUG: l.Pos was created from parsing dwarf but got error: " + err.Error())
}
human, stable := d.namesForSubprogram(spgm.Entry, spgm)
locations := make([]location, 0, 1+len(spgm.Inlines))
locations = append(locations, location{
File: le.File.Name,
Line: int64(le.Line),
Column: int64(le.Column),
Inlined: false,
HumanName: human,
StableName: stable,
})
if len(spgm.Inlines) > 0 {
files := lr.Files()
for i := len(spgm.Inlines) - 1; i >= 0; i-- {
er := spgm.Inlines[i]
fileIdx, ok := er.entry.Val(dwarf.AttrCallFile).(int64)
if !ok || fileIdx >= int64(len(files)) || !offsetInRanges(er.ranges, offset) {
continue
}
file := files[fileIdx]
line, _ := er.entry.Val(dwarf.AttrCallLine).(int64)
col, _ := er.entry.Val(dwarf.AttrCallLine).(int64)
human, stable := d.namesForSubprogram(er.entry, nil)
locations = append(locations, location{
File: file.Name,
Line: line,
Column: col,
Inlined: true,
StableName: stable,
HumanName: human,
})
}
}
return offset, locations
}
func offsetInRanges(ranges []sourceOffsetRange, offset uint64) bool {
for _, x := range ranges {
if x[0] <= offset && offset <= x[1] {
return true
}
}
return false
}
// line is used to cache line entries for a given compilation unit.
type line struct {
Pos dwarf.LineReaderPos
Address uint64
}
// Returns a human-readable name and the name the most likely to match the one
// used in the wasm module. Walks up the inlining chain.
//
// Subprogram is optional. This function will look for the associated subprogram
// if spgm is nil.
func (d *dwarfmapper) namesForSubprogram(e *dwarf.Entry, spgm *subprogram) (string, string) {
// If an inlined function, grab the name from the origin.
var err error
r := d.d.Reader()
for {
ao, ok := e.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !ok {
break
}
r.Seek(ao)
e, err = r.Next()
if err != nil {
// malformed dwarf
break
}
}
// TODO: index
if spgm == nil {
for _, s := range d.subprograms {
if s.Subprogram.Entry.Offset == e.Offset {
spgm = s.Subprogram
break
}
}
}
var ns string
if spgm != nil {
ns = spgm.Namespace
// } else {
// panic("spgm not found")
}
name, _ := e.Val(dwarf.AttrName).(string)
name = ns + name
stableName, ok := e.Val(dwarf.AttrLinkageName).(string)
if !ok {
stableName = name
}
return name, stableName
}