1 /*
2 debug/gosym parses a Go 1.2 pclntab and answers function and line lookups.
3 4 The package compiled but nothing exercised it: no test in the tree called
5 NewLineTable or NewTable, so the whole parser (header, functab, _func records,
6 the pc-value program decoder, the file table) was unverified. This fixture
7 builds a pclntab BY HAND - two functions, two files, both line programs - and
8 asserts what the table says, which is the only way to test the parser without
9 shipping a Go binary.
10 11 Writing it found a CODEC bug, fixed in the same round: a pointer that closes a
12 cycle back into the returned object's own slice was not fixed up, because the
13 decode-side seen table grew an extra entry per struct and per pointer, so every
14 later back-reference resolved by position to the WRONG object. `NewTable`'s
15 Table pointed at a recycled copy of the caller's LineTable and the process died
16 in interface dispatch ("no impl for Uint64"). The fixture is the reproduction
17 that pinned it and now passes; see the work log, round 104.
18 19 File-table note, learned the hard way: the first word of the file table is the
20 table's own LENGTH, not the file count - a table with two files is three words
21 (length, offset, offset) and file number N is read at 4*N. Written as "2" the
22 second file is unreachable and the Uint32 read runs off the end.
23 24 25 debug/gosym parses a Go 1.2 pclntab and answers function and line lookups.
26 27 The package compiled but nothing exercised it: no test in the tree called
28 NewLineTable or NewTable, so the whole parser (header, functab, _func records,
29 the pc-value program decoder, the file table) was unverified. This fixture
30 builds a pclntab BY HAND - two functions, two files, both line programs - and
31 asserts what the table says, which is the only way to test the parser without
32 shipping a Go binary.
33 34 Layout written here, all of it in the format parsePclnTab/go12Funcs/
35 go12PCToFile/go12PCToLine read (ver12, little endian, ptrsize 8, quantum 1):
36 37 0x000 0xfffffffb magic, two zero bytes, quantum 1, pointer size 8
38 0x008 nfunctab = 2
39 0x010 functab: (pc, funcoff) for each func, then a sentinel PC:
40 0x1000/0x080, 0x1020/0x0C0, sentinel 0x1040
41 0x038 fileoff = 0x100
42 0x080 _func main.alpha: entry 0x1000, nameoff 0x180, deferreturn 8,
43 pcfile 0x1C0, pcln 0x1C2
44 0x0C0 _func main.beta: entry 0x1020, nameoff 0x190, pcfile 0x1C4,
45 pcln 0x1C6
46 0x100 filetab: count 2, offsets 0x1A0 and 0x1B0
47 0x180 "main.alpha", 0x190 "main.beta", 0x1A0 "alpha.mx", 0x1B0 "beta.mx"
48 0x1C0 pc-value programs, one (value delta, pc delta) varint pair each:
49 file alpha 04 20 (file 1), line alpha 0A 20 (line 4)
50 file beta 06 20 (file 2), line beta 14 20 (line 9)
51 52 The varints are zigzag: 0x04 -> +2, 0x0A -> +5, 0x06 -> +3, 0x14 -> +10, and
53 the value starts at -1, so alpha reports file 1 / line 4 and beta file 2 /
54 line 9, with a pc delta of 32 covering the function.
55 */
56 package main
57 58 import (
59 "debug/gosym"
60 )
61 62 // table is the pclntab under construction. The writes go through a receiver
63 // because appending can move the backing array: the new HEADER has to be
64 // stored where the slice is owned.
65 type table struct {
66 d []byte
67 }
68 69 func (b *table) put8(off int32, v byte) {
70 b.d[off] = v
71 }
72 73 func (b *table) put32(off int32, v uint32) {
74 for i := int32(0); i < 4; i++ {
75 b.d[off+i] = byte(v >> (8 * uint32(i)))
76 }
77 }
78 79 func (b *table) put64(off int32, v uint64) {
80 for i := int32(0); i < 8; i++ {
81 b.d[off+i] = byte(v >> (8 * uint32(i)))
82 }
83 }
84 85 func (b *table) putStr(off int32, s string) {
86 for i := int32(0); i < int32(len(s)); i++ {
87 b.d[off+i] = s[i]
88 }
89 b.d[off+int32(len(s))] = 0
90 }
91 92 func build() (d []byte) {
93 d = []byte{:512}
94 for i := int32(0); i < 512; i++ {
95 d[i] = 0
96 }
97 b := table{d: d}
98 99 // Header: magic, two zeros, quantum, pointer size.
100 b.put8(0, 0xfb)
101 b.put8(1, 0xff)
102 b.put8(2, 0xff)
103 b.put8(3, 0xff)
104 b.put8(6, 1)
105 b.put8(7, 8)
106 b.put64(8, 2) // nfunctab
107 108 // functab: pc/funcoff pairs then the sentinel PC.
109 b.put64(0x010, 0x1000)
110 b.put64(0x018, 0x080)
111 b.put64(0x020, 0x1020)
112 b.put64(0x028, 0x0C0)
113 b.put64(0x030, 0x1040)
114 b.put32(0x038, 0x100) // fileoff
115 116 // _func main.alpha at 0x080: entry, nameoff, args, deferreturn, pcfile, pcln.
117 b.put64(0x080, 0x1000)
118 b.put32(0x088, 0x180)
119 b.put32(0x090, 8)
120 b.put32(0x098, 0x1C0)
121 b.put32(0x09C, 0x1C2)
122 123 // _func main.beta at 0x0C0.
124 b.put64(0x0C0, 0x1020)
125 b.put32(0x0C8, 0x190)
126 b.put32(0x0D8, 0x1C4)
127 b.put32(0x0DC, 0x1C6)
128 129 // File table: the first word is the table's own LENGTH (a table with two
130 // files is three words: length, offset, offset) and a file number N is read
131 // at 4*N, so the pc-value programs below use 1 and 2. Getting this wrong is
132 // how the first version of this fixture failed: it wrote 2 for two files,
133 // which makes file 2 unreachable and the Uint32 read run off the end.
134 b.put32(0x100, 3)
135 b.put32(0x104, 0x1A0)
136 b.put32(0x108, 0x1B0)
137 138 b.putStr(0x180, "main.alpha")
139 b.putStr(0x190, "main.beta")
140 b.putStr(0x1A0, "alpha.mx")
141 b.putStr(0x1B0, "beta.mx")
142 143 // pc-value programs.
144 b.put8(0x1C0, 0x04)
145 b.put8(0x1C1, 0x20)
146 b.put8(0x1C2, 0x0A)
147 b.put8(0x1C3, 0x20)
148 b.put8(0x1C4, 0x06)
149 b.put8(0x1C5, 0x20)
150 b.put8(0x1C6, 0x14)
151 b.put8(0x1C7, 0x20)
152 153 return b.d
154 }
155 156 func main() {
157 // Termination: a batch program must still be stoppable, so the entry
158 // handles the channel the runtime closes on SIGINT/SIGTERM.
159 select {
160 case <-shutdown:
161 return
162 default:
163 }
164 165 lt := gosym.NewLineTable(build(), 0x1000)
166 tbl, err := gosym.NewTable(nil, lt)
167 if err != nil {
168 panic("NewTable: " | err.Error())
169 }
170 if len(tbl.Funcs) != 2 {
171 panic("func count " | int32(len(tbl.Funcs)).String())
172 }
173 174 a := tbl.LookupFunc("main.alpha")
175 if a == nil {
176 panic("main.alpha not found")
177 }
178 if a.Entry != 0x1000 || a.End != 0x1020 {
179 panic("alpha bounds " | a.Entry.String() | "-" | a.End.String())
180 }
181 if a.FrameSize != 8 {
182 panic("alpha frame size " | a.FrameSize.String())
183 }
184 b := tbl.LookupFunc("main.beta")
185 if b == nil {
186 panic("main.beta not found")
187 }
188 if b.Entry != 0x1020 || b.End != 0x1040 {
189 panic("beta bounds " | b.Entry.String() | "-" | b.End.String())
190 }
191 if tbl.LookupFunc("main.nope") != nil {
192 panic("a function that is not in the table was found")
193 }
194 195 if f := tbl.PCToFunc(0x1010); f == nil || f.Sym.Name != "main.alpha" {
196 panic("PCToFunc 0x1010 did not resolve to main.alpha")
197 }
198 if f := tbl.PCToFunc(0x1030); f == nil || f.Sym.Name != "main.beta" {
199 panic("PCToFunc 0x1030 did not resolve to main.beta")
200 }
201 if tbl.PCToFunc(0x1040) != nil {
202 panic("PCToFunc found a function at the sentinel PC")
203 }
204 if tbl.PCToFunc(0x0fff) != nil {
205 panic("PCToFunc found a function below the first entry")
206 }
207 208 file, line, fn := tbl.PCToLine(0x1004)
209 if fn == nil || file != "alpha.mx" || line != 4 {
210 panic("PCToLine 0x1004 = " | file | ":" | line.String())
211 }
212 file, line, fn = tbl.PCToLine(0x1030)
213 if fn == nil || file != "beta.mx" || line != 9 {
214 panic("PCToLine 0x1030 = " | file | ":" | line.String())
215 }
216 _, _, fn = tbl.PCToLine(0x2000)
217 if fn != nil {
218 panic("PCToLine resolved a pc outside the table")
219 }
220 221 // PCToLine on the LineTable itself is the deprecated path into the same
222 // decoder, and it must agree with the Table.
223 if lt.PCToLine(0x1004) != 4 || lt.PCToLine(0x1030) != 9 {
224 panic("LineTable.PCToLine disagrees with Table.PCToLine")
225 }
226 227 println("gosym ok")
228 }
229