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
281 func read115Executable(tb testing.TB) []byte {
282 zippedDat, err := os.ReadFile("testdata/pcln115.gz")
283 if err != nil {
284 tb.Fatal(err)
285 }
286 var gzReader *gzip.Reader
287 gzReader, err = gzip.NewReader(bytes.NewBuffer(zippedDat))
288 if err != nil {
289 tb.Fatal(err)
290 }
291 var dat []byte
292 dat, err = io.ReadAll(gzReader)
293 if err != nil {
294 tb.Fatal(err)
295 }
296 return dat
297 }
298
299
300 func Test115PclnParsing(t *testing.T) {
301 dat := read115Executable(t)
302 const textStart = 0x1001000
303 pcln := NewLineTable(dat, textStart)
304 tab, err := NewTable(nil, pcln)
305 if err != nil {
306 t.Fatal(err)
307 }
308 var f *Func
309 var pc uint64
310 pc, f, err = tab.LineToPC("/tmp/hello.go", 3)
311 if err != nil {
312 t.Fatal(err)
313 }
314 if pcln.version != ver12 {
315 t.Fatal("Expected pcln to parse as an older version")
316 }
317 if pc != 0x105c280 {
318 t.Fatalf("expect pc = 0x105c280, got 0x%x", pc)
319 }
320 if f.Name != "main.main" {
321 t.Fatalf("expected to parse name as main.main, got %v", f.Name)
322 }
323 }
324
325 var (
326 sinkLineTable *LineTable
327 sinkTable *Table
328 )
329
330 func Benchmark115(b *testing.B) {
331 dat := read115Executable(b)
332 const textStart = 0x1001000
333
334 b.Run("NewLineTable", func(b *testing.B) {
335 b.ReportAllocs()
336 for i := 0; i < b.N; i++ {
337 sinkLineTable = NewLineTable(dat, textStart)
338 }
339 })
340
341 pcln := NewLineTable(dat, textStart)
342 b.Run("NewTable", func(b *testing.B) {
343 b.ReportAllocs()
344 for i := 0; i < b.N; i++ {
345 var err error
346 sinkTable, err = NewTable(nil, pcln)
347 if err != nil {
348 b.Fatal(err)
349 }
350 }
351 })
352
353 tab, err := NewTable(nil, pcln)
354 if err != nil {
355 b.Fatal(err)
356 }
357
358 b.Run("LineToPC", func(b *testing.B) {
359 b.ReportAllocs()
360 for i := 0; i < b.N; i++ {
361 var f *Func
362 var pc uint64
363 pc, f, err = tab.LineToPC("/tmp/hello.go", 3)
364 if err != nil {
365 b.Fatal(err)
366 }
367 if pcln.version != ver12 {
368 b.Fatalf("want version=%d, got %d", ver12, pcln.version)
369 }
370 if pc != 0x105c280 {
371 b.Fatalf("want pc=0x105c280, got 0x%x", pc)
372 }
373 if f.Name != "main.main" {
374 b.Fatalf("want name=main.main, got %q", f.Name)
375 }
376 }
377 })
378
379 b.Run("PCToLine", func(b *testing.B) {
380 b.ReportAllocs()
381 for i := 0; i < b.N; i++ {
382 file, line, fn := tab.PCToLine(0x105c280)
383 if file != "/tmp/hello.go" {
384 b.Fatalf("want name=/tmp/hello.go, got %q", file)
385 }
386 if line != 3 {
387 b.Fatalf("want line=3, got %d", line)
388 }
389 if fn.Name != "main.main" {
390 b.Fatalf("want name=main.main, got %q", fn.Name)
391 }
392 }
393 })
394 }
395
View as plain text