1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // For now, only works on ELF platforms.
16 // TODO: convert to work with new go tool
17 return false && runtime.GOOS == "linux" && runtime.GOARCH == "amd64"
20 func getTable(t *testing.T) *Table {
21 f, tab := crack(os.Args[0], t)
26 func crack(file string, t *testing.T) (*elf.File, *Table) {
28 f, err := elf.Open(file)
32 return parse(file, f, t)
35 func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
36 symdat, err := f.Section(".gosymtab").Data()
39 t.Fatalf("reading %s gosymtab: %v", file, err)
41 pclndat, err := f.Section(".gopclntab").Data()
44 t.Fatalf("reading %s gopclntab: %v", file, err)
47 pcln := NewLineTable(pclndat, f.Section(".text").Addr)
48 tab, err := NewTable(symdat, pcln)
51 t.Fatalf("parsing %s gosymtab: %v", file, err)
57 var goarch = os.Getenv("O")
59 func TestLineFromAline(t *testing.T) {
66 // Find the sym package
67 pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
72 // Walk every absolute line and ensure that we hit every
73 // source line monotonically
74 lastline := make(map[string]int)
76 for i := 0; i < 10000; i++ {
77 path, line := pkg.lineFromAline(i)
78 // Check for end of object
84 } else if final != -1 {
85 t.Fatalf("reached end of package at absolute line %d, but absolute line %d mapped to %s:%d", final, i, path, line)
87 // It's okay to see files multiple times (e.g., sys.a)
92 // Check that the is the next line in path
93 ll, ok := lastline[path]
95 t.Errorf("file %s starts on line %d", path, line)
96 } else if line != ll+1 {
97 t.Errorf("expected next line of file %s to be %d, got %d", path, ll+1, line)
102 t.Errorf("never reached end of object")
106 func TestLineAline(t *testing.T) {
113 for _, o := range tab.Files {
114 // A source file can appear multiple times in a
115 // object. alineFromLine will always return alines in
116 // the first file, so track which lines we've seen.
117 found := make(map[string]int)
118 for i := 0; i < 1000; i++ {
119 path, line := o.lineFromAline(i)
124 // cgo files are full of 'Z' symbols, which we don't handle
125 if len(path) > 4 && path[len(path)-4:] == ".cgo" {
129 if minline, ok := found[path]; path != "" && ok {
131 // We've already covered this file
137 a, err := o.alineFromLine(path, line)
139 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)
141 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)
147 func TestPCLine(t *testing.T) {
152 f, tab := crack("_test/pclinetest", t)
153 text := f.Section(".text")
154 textdat, err := text.Data()
156 t.Fatalf("reading .text: %v", err)
160 sym := tab.LookupFunc("linefrompc")
162 for pc := sym.Entry; pc < sym.End; pc++ {
163 file, line, fn := tab.PCToLine(pc)
164 off := pc - text.Addr // TODO(rsc): should not need off; bug in 8g
165 wantLine += int(textdat[off])
167 t.Errorf("failed to get line of PC %#x", pc)
168 } else if len(file) < 12 || file[len(file)-12:] != "pclinetest.s" || line != wantLine || fn != sym {
169 t.Errorf("expected %s:%d (%s) at PC %#x, got %s:%d (%s)", "pclinetest.s", wantLine, sym.Name, pc, file, line, fn.Name)
174 sym = tab.LookupFunc("pcfromline")
177 off := uint64(0) // TODO(rsc): should not need off; bug in 8g
178 for pc := sym.Value; pc < sym.End; pc += 2 + uint64(textdat[off]) {
179 file, line, fn := tab.PCToLine(pc)
181 wantLine += int(textdat[off])
182 if line != wantLine {
183 t.Errorf("expected line %d at PC %#x in pcfromline, got %d", wantLine, pc, line)
184 off = pc + 1 - text.Addr
187 if lookupline == -1 {
190 for ; lookupline <= line; lookupline++ {
191 pc2, fn2, err := tab.LineToPC(file, lookupline)
192 if lookupline != line {
193 // Should be nothing on this line
195 t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
197 } else if err != nil {
198 t.Errorf("failed to get PC of line %d: %s", lookupline, err)
199 } else if pc != pc2 {
200 t.Errorf("expected PC %#x (%s) at line %d, got PC %#x (%s)", pc, fn.Name, line, pc2, fn2.Name)
203 off = pc + 1 - text.Addr