Source file
src/debug/gosym/pclntab_test.go
1
2
3
4
5 package gosym
6
7 import (
8 "bytes"
9 "compress/gzip"
10 "debug/elf"
11 "internal/testenv"
12 "io"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "runtime"
17 "strings"
18 "testing"
19 )
20
21 var (
22 pclineTempDir string
23 pclinetestBinary string
24 )
25
26 func dotest(t *testing.T) {
27 testenv.MustHaveGoBuild(t)
28
29 if runtime.GOARCH != "amd64" {
30 t.Skipf("skipping on non-AMD64 system %s", runtime.GOARCH)
31 }
32
33 if runtime.GOOS != "linux" && testing.Short() {
34 t.Skipf("skipping in short mode on non-Linux system %s", runtime.GOARCH)
35 }
36 var err error
37 pclineTempDir, err = os.MkdirTemp("", "pclinetest")
38 if err != nil {
39 t.Fatal(err)
40 }
41 pclinetestBinary = filepath.Join(pclineTempDir, "pclinetest")
42 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", pclinetestBinary)
43 cmd.Dir = "testdata"
44 cmd.Env = append(os.Environ(), "GOOS=linux")
45 cmd.Stdout = os.Stdout
46 cmd.Stderr = os.Stderr
47 if err := cmd.Run(); err != nil {
48 t.Fatal(err)
49 }
50 }
51
52 func endtest() {
53 if pclineTempDir != "" {
54 os.RemoveAll(pclineTempDir)
55 pclineTempDir = ""
56 pclinetestBinary = ""
57 }
58 }
59
60
61
62 func skipIfNotELF(t *testing.T) {
63 switch runtime.GOOS {
64 case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris", "illumos":
65
66 default:
67 t.Skipf("skipping on non-ELF system %s", runtime.GOOS)
68 }
69 }
70
71 func getTable(t *testing.T) *Table {
72 f, tab := crack(os.Args[0], t)
73 f.Close()
74 return tab
75 }
76
77 func crack(file string, t *testing.T) (*elf.File, *Table) {
78
79 f, err := elf.Open(file)
80 if err != nil {
81 t.Fatal(err)
82 }
83 return parse(file, f, t)
84 }
85
86 func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
87 s := f.Section(".gosymtab")
88 if s == nil {
89 t.Skip("no .gosymtab section")
90 }
91 symdat, err := s.Data()
92 if err != nil {
93 f.Close()
94 t.Fatalf("reading %s gosymtab: %v", file, err)
95 }
96 pclndat, err := f.Section(".gopclntab").Data()
97 if err != nil {
98 f.Close()
99 t.Fatalf("reading %s gopclntab: %v", file, err)
100 }
101
102 pcln := NewLineTable(pclndat, f.Section(".text").Addr)
103 tab, err := NewTable(symdat, pcln)
104 if err != nil {
105 f.Close()
106 t.Fatalf("parsing %s gosymtab: %v", file, err)
107 }
108
109 return f, tab
110 }
111
112 func TestLineFromAline(t *testing.T) {
113 skipIfNotELF(t)
114
115 tab := getTable(t)
116 if tab.go12line != nil {
117
118 t.Skip("not relevant to Go 1.2 symbol table")
119 }
120
121
122 pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
123 if pkg == nil {
124 t.Fatalf("nil pkg")
125 }
126
127
128
129 lastline := make(map[string]int)
130 final := -1
131 for i := 0; i < 10000; i++ {
132 path, line := pkg.lineFromAline(i)
133
134 if path == "" {
135 if final == -1 {
136 final = i - 1
137 }
138 continue
139 } else if final != -1 {
140 t.Fatalf("reached end of package at absolute line %d, but absolute line %d mapped to %s:%d", final, i, path, line)
141 }
142
143 if line == 1 {
144 lastline[path] = 1
145 continue
146 }
147
148 ll, ok := lastline[path]
149 if !ok {
150 t.Errorf("file %s starts on line %d", path, line)
151 } else if line != ll+1 {
152 t.Fatalf("expected next line of file %s to be %d, got %d", path, ll+1, line)
153 }
154 lastline[path] = line
155 }
156 if final == -1 {
157 t.Errorf("never reached end of object")
158 }
159 }
160
161 func TestLineAline(t *testing.T) {
162 skipIfNotELF(t)
163
164 tab := getTable(t)
165 if tab.go12line != nil {
166
167 t.Skip("not relevant to Go 1.2 symbol table")
168 }
169
170 for _, o := range tab.Files {
171
172
173
174 found := make(map[string]int)
175 for i := 0; i < 1000; i++ {
176 path, line := o.lineFromAline(i)
177 if path == "" {
178 break
179 }
180
181
182 if len(path) > 4 && path[len(path)-4:] == ".cgo" {
183 continue
184 }
185
186 if minline, ok := found[path]; path != "" && ok {
187 if minline >= line {
188
189 continue
190 }
191 }
192 found[path] = line
193
194 a, err := o.alineFromLine(path, line)
195 if err != nil {
196 t.Errorf("absolute line %d in object %s maps to %s:%d, but mapping that back gives error %s", i, o.Paths[0].Name, path, line, err)
197 } else if a != i {
198 t.Errorf("absolute line %d in object %s maps to %s:%d, which maps back to absolute line %d\n", i, o.Paths[0].Name, path, line, a)
199 }
200 }
201 }
202 }
203
204 func TestPCLine(t *testing.T) {
205 dotest(t)
206 defer endtest()
207
208 f, tab := crack(pclinetestBinary, t)
209 defer f.Close()
210 text := f.Section(".text")
211 textdat, err := text.Data()
212 if err != nil {
213 t.Fatalf("reading .text: %v", err)
214 }
215
216
217 sym := tab.LookupFunc("main.linefrompc")
218 wantLine := 0
219 for pc := sym.Entry; pc < sym.End; pc++ {
220 off := pc - text.Addr
221 if textdat[off] == 255 {
222 break
223 }
224 wantLine += int(textdat[off])
225 t.Logf("off is %d %#x (max %d)", off, textdat[off], sym.End-pc)
226 file, line, fn := tab.PCToLine(pc)
227 if fn == nil {
228 t.Errorf("failed to get line of PC %#x", pc)
229 } else if !strings.HasSuffix(file, "pclinetest.s") || line != wantLine || fn != sym {
230 t.Errorf("PCToLine(%#x) = %s:%d (%s), want %s:%d (%s)", pc, file, line, fn.Name, "pclinetest.s", wantLine, sym.Name)
231 }
232 }
233
234
235 sym = tab.LookupFunc("main.pcfromline")
236 lookupline := -1
237 wantLine = 0
238 off := uint64(0)
239 for pc := sym.Value; pc < sym.End; pc += 2 + uint64(textdat[off]) {
240 file, line, fn := tab.PCToLine(pc)
241 off = pc - text.Addr
242 if textdat[off] == 255 {
243 break
244 }
245 wantLine += int(textdat[off])
246 if line != wantLine {
247 t.Errorf("expected line %d at PC %#x in pcfromline, got %d", wantLine, pc, line)
248 off = pc + 1 - text.Addr
249 continue
250 }
251 if lookupline == -1 {
252 lookupline = line
253 }
254 for ; lookupline <= line; lookupline++ {
255 pc2, fn2, err := tab.LineToPC(file, lookupline)
256 if lookupline != line {
257
258 if err == nil {
259 t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
260 }
261 } else if err != nil {
262 t.Errorf("failed to get PC of line %d: %s", lookupline, err)
263 } else if pc != pc2 {
264 t.Errorf("expected PC %#x (%s) at line %d, got PC %#x (%s)", pc, fn.Name, line, pc2, fn2.Name)
265 }
266 }
267 off = pc + 1 - text.Addr
268 }
269 }
270
271
272
273
274
275
276
277
278
279
280 func read115Executable(tb testing.TB) []byte {
281 zippedDat, err := os.ReadFile("testdata/pcln115.gz")
282 if err != nil {
283 tb.Fatal(err)
284 }
285 var gzReader *gzip.Reader
286 gzReader, err = gzip.NewReader(bytes.NewBuffer(zippedDat))
287 if err != nil {
288 tb.Fatal(err)
289 }
290 var dat []byte
291 dat, err = io.ReadAll(gzReader)
292 if err != nil {
293 tb.Fatal(err)
294 }
295 return dat
296 }
297
298
299 func Test115PclnParsing(t *testing.T) {
300 dat := read115Executable(t)
301 const textStart = 0x1001000
302 pcln := NewLineTable(dat, textStart)
303 tab, err := NewTable(nil, pcln)
304 if err != nil {
305 t.Fatal(err)
306 }
307 var f *Func
308 var pc uint64
309 pc, f, err = tab.LineToPC("/tmp/hello.go", 3)
310 if err != nil {
311 t.Fatal(err)
312 }
313 if pcln.version != ver12 {
314 t.Fatal("Expected pcln to parse as an older version")
315 }
316 if pc != 0x105c280 {
317 t.Fatalf("expect pc = 0x105c280, got 0x%x", pc)
318 }
319 if f.Name != "main.main" {
320 t.Fatalf("expected to parse name as main.main, got %v", f.Name)
321 }
322 }
323
324 var (
325 sinkLineTable *LineTable
326 sinkTable *Table
327 )
328
329 func Benchmark115(b *testing.B) {
330 dat := read115Executable(b)
331 const textStart = 0x1001000
332
333 b.Run("NewLineTable", func(b *testing.B) {
334 b.ReportAllocs()
335 for i := 0; i < b.N; i++ {
336 sinkLineTable = NewLineTable(dat, textStart)
337 }
338 })
339
340 pcln := NewLineTable(dat, textStart)
341 b.Run("NewTable", func(b *testing.B) {
342 b.ReportAllocs()
343 for i := 0; i < b.N; i++ {
344 var err error
345 sinkTable, err = NewTable(nil, pcln)
346 if err != nil {
347 b.Fatal(err)
348 }
349 }
350 })
351
352 tab, err := NewTable(nil, pcln)
353 if err != nil {
354 b.Fatal(err)
355 }
356
357 b.Run("LineToPC", func(b *testing.B) {
358 b.ReportAllocs()
359 for i := 0; i < b.N; i++ {
360 var f *Func
361 var pc uint64
362 pc, f, err = tab.LineToPC("/tmp/hello.go", 3)
363 if err != nil {
364 b.Fatal(err)
365 }
366 if pcln.version != ver12 {
367 b.Fatalf("want version=%d, got %d", ver12, pcln.version)
368 }
369 if pc != 0x105c280 {
370 b.Fatalf("want pc=0x105c280, got 0x%x", pc)
371 }
372 if f.Name != "main.main" {
373 b.Fatalf("want name=main.main, got %q", f.Name)
374 }
375 }
376 })
377
378 b.Run("PCToLine", func(b *testing.B) {
379 b.ReportAllocs()
380 for i := 0; i < b.N; i++ {
381 file, line, fn := tab.PCToLine(0x105c280)
382 if file != "/tmp/hello.go" {
383 b.Fatalf("want name=/tmp/hello.go, got %q", file)
384 }
385 if line != 3 {
386 b.Fatalf("want line=3, got %d", line)
387 }
388 if fn.Name != "main.main" {
389 b.Fatalf("want name=main.main, got %q", fn.Name)
390 }
391 }
392 })
393 }
394
View as plain text