File: builder.go

package info (click to toggle)
delve 1.24.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,092 kB
  • sloc: ansic: 111,943; sh: 169; asm: 141; makefile: 43; python: 23
file content (52 lines) | stat: -rw-r--r-- 1,064 bytes parent folder | download | duplicates (3)
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
// Package dwarfbuilder provides a way to build DWARF sections with
// arbitrary contents.
package dwarfbuilder

import (
	"bytes"
	"debug/dwarf"
	"encoding/binary"
	"fmt"
)

// Builder dwarf builder
type Builder struct {
	info     bytes.Buffer
	loc      bytes.Buffer
	abbrevs  []tagDescr
	tagStack []*tagState
}

// New creates a new DWARF builder.
func New() *Builder {
	b := &Builder{}

	b.info.Write([]byte{
		0x0, 0x0, 0x0, 0x0, // length
		0x4, 0x0, // version
		0x0, 0x0, 0x0, 0x0, // debug_abbrev_offset
		0x8, // address_size
	})

	b.TagOpen(dwarf.TagCompileUnit, "go")
	b.Attr(dwarf.AttrLanguage, uint8(22))

	return b
}

// Build closes b and returns all the dwarf sections.
func (b *Builder) Build() (abbrev, aranges, frame, info, line, pubnames, ranges, str, loc []byte, err error) {
	b.TagClose()

	if len(b.tagStack) > 0 {
		err = fmt.Errorf("unbalanced TagOpen/TagClose %d", len(b.tagStack))
		return
	}

	abbrev = b.makeAbbrevTable()
	info = b.info.Bytes()
	binary.LittleEndian.PutUint32(info, uint32(len(info)-4))
	loc = b.loc.Bytes()

	return
}