aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.7/libgo/go/debug/dwarf/line.go
blob: 3ab2f2b30cb57e319a12a66cd30f12c400df5f2d (plain)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// DWARF line number information.

package dwarf

import (
	"errors"
	"path/filepath"
	"sort"
	"strconv"
)

// A Line holds all the available information about the source code
// corresponding to a specific program counter address.
type Line struct {
	Filename      string // source file name
	OpIndex       int    // index of operation in VLIW instruction
	Line          int    // line number
	Column        int    // column number
	ISA           int    // instruction set code
	Discriminator int    // block discriminator
	Stmt          bool   // instruction starts statement
	Block         bool   // instruction starts basic block
	EndPrologue   bool   // instruction ends function prologue
	BeginEpilogue bool   // instruction begins function epilogue
}

// LineForPc returns the line number information for a program counter
// address, if any.  When this returns multiple Line structures in a
// context where only one can be used, the last one is the best.
func (d *Data) LineForPC(pc uint64) ([]*Line, error) {
	for i := range d.unit {
		u := &d.unit[i]
		if u.pc == nil {
			if err := d.readUnitLine(i, u); err != nil {
				return nil, err
			}
		}
		for _, ar := range u.pc {
			if pc >= ar.low && pc < ar.high {
				return d.findLine(u, pc)
			}
		}
	}
	return nil, nil
}

// readUnitLine reads in the line number information for a compilation
// unit.
func (d *Data) readUnitLine(i int, u *unit) error {
	r := d.unitReader(i)
	setLineOff := false
	for {
		e, err := r.Next()
		if err != nil {
			return err
		}
		if e == nil {
			break
		}
		if r.unit != i {
			break
		}
		switch e.Tag {
		case TagCompileUnit, TagSubprogram, TagEntryPoint, TagInlinedSubroutine:
			low, lowok := e.Val(AttrLowpc).(uint64)
			var high uint64
			var highok bool
			switch v := e.Val(AttrHighpc).(type) {
			case uint64:
				high = v
				highok = true
			case int64:
				high = low + uint64(v)
				highok = true
			}
			if lowok && highok {
				u.pc = append(u.pc, addrRange{low, high})
			} else if off, ok := e.Val(AttrRanges).(Offset); ok {
				if err := d.readAddressRanges(off, low, u); err != nil {
					return err
				}
			}
			val := e.Val(AttrStmtList)
			if val != nil {
				if off, ok := val.(int64); ok {
					u.lineoff = Offset(off)
					setLineOff = true
				} else if off, ok := val.(Offset); ok {
					u.lineoff = off
					setLineOff = true
				} else {
					return errors.New("unrecognized format for DW_ATTR_stmt_list")
				}
			}
			if dir, ok := e.Val(AttrCompDir).(string); ok {
				u.dir = dir
			}
		}
	}
	if !setLineOff {
		u.lineoff = Offset(0)
		u.lineoff--
	}
	return nil
}

// readAddressRanges adds address ranges to a unit.
func (d *Data) readAddressRanges(off Offset, base uint64, u *unit) error {
	b := makeBuf(d, u, "ranges", off, d.ranges[off:])
	var highest uint64
	switch u.addrsize {
	case 1:
		highest = 0xff
	case 2:
		highest = 0xffff
	case 4:
		highest = 0xffffffff
	case 8:
		highest = 0xffffffffffffffff
	default:
		return errors.New("unknown address size")
	}
	for {
		if b.err != nil {
			return b.err
		}
		low := b.addr()
		high := b.addr()
		if low == 0 && high == 0 {
			return b.err
		} else if low == highest {
			base = high
		} else {
			u.pc = append(u.pc, addrRange{low + base, high + base})
		}
	}
}

// findLine finds the line information for a PC value, given the unit
// containing the information.
func (d *Data) findLine(u *unit, pc uint64) ([]*Line, error) {
	if u.lines == nil {
		if err := d.parseLine(u); err != nil {
			return nil, err
		}
	}

	for _, ln := range u.lines {
		if pc < ln.addrs[0].pc || pc > ln.addrs[len(ln.addrs)-1].pc {
			continue
		}
		i := sort.Search(len(ln.addrs),
			func(i int) bool { return ln.addrs[i].pc > pc })
		i--
		p := new(Line)
		*p = ln.line
		p.Line = ln.addrs[i].line
		ret := []*Line{p}
		for i++; i < len(ln.addrs) && ln.addrs[i].pc == pc; i++ {
			p = new(Line)
			*p = ln.line
			p.Line = ln.addrs[i].line
			ret = append(ret, p)
		}
		return ret, nil
	}

	return nil, nil
}

// FileLine returns the file name and line number for a program
// counter address, or "", 0 if unknown.
func (d *Data) FileLine(pc uint64) (string, int, error) {
	r, err := d.LineForPC(pc)
	if err != nil {
		return "", 0, err
	}
	if r == nil {
		return "", 0, nil
	}
	ln := r[len(r)-1]
	return ln.Filename, ln.Line, nil
}

// A mapLineInfo holds the PC values and line numbers associated with
// a single Line structure.  This representation is chosen to reduce
// memory usage based on typical debug info.
type mapLineInfo struct {
	line  Line      // line.Line will be zero
	addrs lineAddrs // sorted by PC
}

// A list of lines.  This will be sorted by PC.
type lineAddrs []oneLineInfo

func (p lineAddrs) Len() int           { return len(p) }
func (p lineAddrs) Less(i, j int) bool { return p[i].pc < p[j].pc }
func (p lineAddrs) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// A oneLineInfo is a single PC and line number.
type oneLineInfo struct {
	pc   uint64
	line int
}

// A lineHdr holds the relevant information from a line number
// program header.
type lineHdr struct {
	version       uint16   // version of line number encoding
	minInsnLen    uint8    // minimum instruction length
	maxOpsPerInsn uint8    // maximum number of ops per instruction
	defStmt       bool     // initial value of stmt register
	lineBase      int8     // line adjustment base
	lineRange     uint8    // line adjustment step
	opBase        uint8    // base of special opcode values
	opLen         []uint8  // lengths of standard opcodes
	dirs          []string // directories
	files         []string // file names
}

// parseLine parses the line number information for a compilation unit
func (d *Data) parseLine(u *unit) error {
	if u.lineoff+1 == 0 {
		return errors.New("unknown line offset")
	}
	b := makeBuf(d, u, "line", u.lineoff, d.line[u.lineoff:])
	len := uint64(b.uint32())
	dwarf64 := false
	if len == 0xffffffff {
		len = b.uint64()
		dwarf64 = true
	}
	end := b.off + Offset(len)
	hdr := d.parseLineHdr(u, &b, dwarf64)
	if b.err == nil {
		d.parseLineProgram(u, &b, hdr, end)
	}
	return b.err
}

// parseLineHdr parses a line number program header.
func (d *Data) parseLineHdr(u *unit, b *buf, dwarf64 bool) (hdr lineHdr) {
	hdr.version = b.uint16()
	if hdr.version < 2 || hdr.version > 4 {
		b.error("unsupported DWARF version " + strconv.Itoa(int(hdr.version)))
		return
	}

	var hlen Offset
	if dwarf64 {
		hlen = Offset(b.uint64())
	} else {
		hlen = Offset(b.uint32())
	}
	end := b.off + hlen

	hdr.minInsnLen = b.uint8()
	if hdr.version < 4 {
		hdr.maxOpsPerInsn = 1
	} else {
		hdr.maxOpsPerInsn = b.uint8()
	}

	if b.uint8() == 0 {
		hdr.defStmt = false
	} else {
		hdr.defStmt = true
	}
	hdr.lineBase = int8(b.uint8())
	hdr.lineRange = b.uint8()
	hdr.opBase = b.uint8()
	hdr.opLen = b.bytes(int(hdr.opBase - 1))

	for d := b.string(); len(d) > 0; d = b.string() {
		hdr.dirs = append(hdr.dirs, d)
	}

	for f := b.string(); len(f) > 0; f = b.string() {
		d := b.uint()
		if !filepath.IsAbs(f) {
			if d > 0 {
				if d > uint64(len(hdr.dirs)) {
					b.error("DWARF directory index out of range")
					return
				}
				f = filepath.Join(hdr.dirs[d-1], f)
			} else if u.dir != "" {
				f = filepath.Join(u.dir, f)
			}
		}
		b.uint() // file's last mtime
		b.uint() // file length
		hdr.files = append(hdr.files, f)
	}

	if end > b.off {
		b.bytes(int(end - b.off))
	}

	return
}

// parseLineProgram parses a line program, adding information to
// d.lineInfo as it goes.
func (d *Data) parseLineProgram(u *unit, b *buf, hdr lineHdr, end Offset) {
	address := uint64(0)
	line := 1
	resetLineInfo := Line{
		Filename:      "",
		OpIndex:       0,
		Line:          0,
		Column:        0,
		ISA:           0,
		Discriminator: 0,
		Stmt:          hdr.defStmt,
		Block:         false,
		EndPrologue:   false,
		BeginEpilogue: false,
	}
	if len(hdr.files) > 0 {
		resetLineInfo.Filename = hdr.files[0]
	}
	lineInfo := resetLineInfo

	var lines []mapLineInfo

	minInsnLen := uint64(hdr.minInsnLen)
	maxOpsPerInsn := uint64(hdr.maxOpsPerInsn)
	lineBase := int(hdr.lineBase)
	lineRange := hdr.lineRange
	newLineInfo := true
	for b.off < end && b.err == nil {
		op := b.uint8()
		if op >= hdr.opBase {
			// This is a special opcode.
			op -= hdr.opBase
			advance := uint64(op / hdr.lineRange)
			opIndex := uint64(lineInfo.OpIndex)
			address += minInsnLen * ((opIndex + advance) / maxOpsPerInsn)
			newOpIndex := int((opIndex + advance) % maxOpsPerInsn)
			line += lineBase + int(op%lineRange)
			if newOpIndex != lineInfo.OpIndex {
				lineInfo.OpIndex = newOpIndex
				newLineInfo = true
			}
			lines, lineInfo, newLineInfo = d.addLine(lines, lineInfo, address, line, newLineInfo)
		} else if op == LineExtendedOp {
			c := b.uint()
			op = b.uint8()
			switch op {
			case LineExtEndSequence:
				u.lines = append(u.lines, lines...)
				lineInfo = resetLineInfo
				lines = nil
				newLineInfo = true
			case LineExtSetAddress:
				address = b.addr()
			case LineExtDefineFile:
				f := b.string()
				d := b.uint()
				b.uint() // mtime
				b.uint() // length
				if d > 0 && !filepath.IsAbs(f) {
					if d >= uint64(len(hdr.dirs)) {
						b.error("DWARF directory index out of range")
						return
					}
					f = filepath.Join(hdr.dirs[d-1], f)
				}
				hdr.files = append(hdr.files, f)
			case LineExtSetDiscriminator:
				lineInfo.Discriminator = int(b.uint())
				newLineInfo = true
			default:
				if c > 0 {
					b.bytes(int(c) - 1)
				}
			}
		} else {
			switch op {
			case LineCopy:
				lines, lineInfo, newLineInfo = d.addLine(lines, lineInfo, address, line, newLineInfo)
			case LineAdvancePC:
				advance := b.uint()
				opIndex := uint64(lineInfo.OpIndex)
				address += minInsnLen * ((opIndex + advance) / maxOpsPerInsn)
				newOpIndex := int((opIndex + advance) % maxOpsPerInsn)
				if newOpIndex != lineInfo.OpIndex {
					lineInfo.OpIndex = newOpIndex
					newLineInfo = true
				}
			case LineAdvanceLine:
				line += int(b.int())
			case LineSetFile:
				i := b.uint()
				if i > uint64(len(hdr.files)) {
					b.error("DWARF file number out of range")
					return
				}
				lineInfo.Filename = hdr.files[i-1]
				newLineInfo = true
			case LineSetColumn:
				lineInfo.Column = int(b.uint())
				newLineInfo = true
			case LineNegateStmt:
				lineInfo.Stmt = !lineInfo.Stmt
				newLineInfo = true
			case LineSetBasicBlock:
				lineInfo.Block = true
				newLineInfo = true
			case LineConstAddPC:
				op = 255 - hdr.opBase
				advance := uint64(op / hdr.lineRange)
				opIndex := uint64(lineInfo.OpIndex)
				address += minInsnLen * ((opIndex + advance) / maxOpsPerInsn)
				newOpIndex := int((opIndex + advance) % maxOpsPerInsn)
				if newOpIndex != lineInfo.OpIndex {
					lineInfo.OpIndex = newOpIndex
					newLineInfo = true
				}
			case LineFixedAdvancePC:
				address += uint64(b.uint16())
				if lineInfo.OpIndex != 0 {
					lineInfo.OpIndex = 0
					newLineInfo = true
				}
			case LineSetPrologueEnd:
				lineInfo.EndPrologue = true
				newLineInfo = true
			case LineSetEpilogueBegin:
				lineInfo.BeginEpilogue = true
				newLineInfo = true
			case LineSetISA:
				lineInfo.ISA = int(b.uint())
				newLineInfo = true
			default:
				if int(op) >= len(hdr.opLen) {
					b.error("DWARF line opcode has unknown length")
					return
				}
				for i := hdr.opLen[op-1]; i > 0; i-- {
					b.int()
				}
			}
		}
	}
}

// addLine adds the current address and line to lines using lineInfo.
// If newLineInfo is true this is a new lineInfo.  This returns the
// updated lines, lineInfo, and newLineInfo.
func (d *Data) addLine(lines []mapLineInfo, lineInfo Line, address uint64, line int, newLineInfo bool) ([]mapLineInfo, Line, bool) {
	if newLineInfo {
		if len(lines) > 0 {
			sort.Sort(lines[len(lines)-1].addrs)
			p := &lines[len(lines)-1]
			if len(p.addrs) > 0 && address > p.addrs[len(p.addrs)-1].pc {
				p.addrs = append(p.addrs, oneLineInfo{address, p.addrs[len(p.addrs)-1].line})
			}
		}
		lines = append(lines, mapLineInfo{line: lineInfo})
	}
	p := &lines[len(lines)-1]
	p.addrs = append(p.addrs, oneLineInfo{address, line})

	if lineInfo.Block || lineInfo.EndPrologue || lineInfo.BeginEpilogue || lineInfo.Discriminator != 0 {
		lineInfo.Block = false
		lineInfo.EndPrologue = false
		lineInfo.BeginEpilogue = false
		lineInfo.Discriminator = 0
		newLineInfo = true
	} else {
		newLineInfo = false
	}

	return lines, lineInfo, newLineInfo
}