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.
21 pclinetestBinary string
25 // For now, only works on ELF platforms.
26 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
29 if pclinetestBinary != "" {
33 pclineTempDir, err = ioutil.TempDir("", "pclinetest")
37 if strings.Contains(pclineTempDir, " ") {
38 panic("unexpected space in tempdir")
40 // This command builds pclinetest from pclinetest.asm;
41 // the resulting binary looks like it was built from pclinetest.s,
42 // but we have renamed it to keep it away from the go tool.
43 pclinetestBinary = filepath.Join(pclineTempDir, "pclinetest")
44 command := fmt.Sprintf("go tool 6a -o %s.6 pclinetest.asm && go tool 6l -E main -o %s %s.6",
45 pclinetestBinary, pclinetestBinary, pclinetestBinary)
46 cmd := exec.Command("sh", "-c", command)
47 cmd.Stdout = os.Stdout
48 cmd.Stderr = os.Stderr
49 if err := cmd.Run(); err != nil {
55 func getTable(t *testing.T) *Table {
56 f, tab := crack(os.Args[0], t)
61 func crack(file string, t *testing.T) (*elf.File, *Table) {
63 f, err := elf.Open(file)
67 return parse(file, f, t)
70 func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
71 symdat, err := f.Section(".gosymtab").Data()
74 t.Fatalf("reading %s gosymtab: %v", file, err)
76 pclndat, err := f.Section(".gopclntab").Data()
79 t.Fatalf("reading %s gopclntab: %v", file, err)
82 pcln := NewLineTable(pclndat, f.Section(".text").Addr)
83 tab, err := NewTable(symdat, pcln)
86 t.Fatalf("parsing %s gosymtab: %v", file, err)
92 var goarch = os.Getenv("O")
94 func TestLineFromAline(t *testing.T) {
101 // Find the sym package
102 pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
107 // Walk every absolute line and ensure that we hit every
108 // source line monotonically
109 lastline := make(map[string]int)
111 for i := 0; i < 10000; i++ {
112 path, line := pkg.lineFromAline(i)
113 // Check for end of object
119 } else if final != -1 {
120 t.Fatalf("reached end of package at absolute line %d, but absolute line %d mapped to %s:%d", final, i, path, line)
122 // It's okay to see files multiple times (e.g., sys.a)
127 // Check that the is the next line in path
128 ll, ok := lastline[path]
130 t.Errorf("file %s starts on line %d", path, line)
131 } else if line != ll+1 {
132 t.Errorf("expected next line of file %s to be %d, got %d", path, ll+1, line)
134 lastline[path] = line
137 t.Errorf("never reached end of object")
141 func TestLineAline(t *testing.T) {
148 for _, o := range tab.Files {
149 // A source file can appear multiple times in a
150 // object. alineFromLine will always return alines in
151 // the first file, so track which lines we've seen.
152 found := make(map[string]int)
153 for i := 0; i < 1000; i++ {
154 path, line := o.lineFromAline(i)
159 // cgo files are full of 'Z' symbols, which we don't handle
160 if len(path) > 4 && path[len(path)-4:] == ".cgo" {
164 if minline, ok := found[path]; path != "" && ok {
166 // We've already covered this file
172 a, err := o.alineFromLine(path, line)
174 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)
176 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)
182 func TestPCLine(t *testing.T) {
186 defer os.RemoveAll(pclineTempDir)
188 f, tab := crack(pclinetestBinary, t)
189 text := f.Section(".text")
190 textdat, err := text.Data()
192 t.Fatalf("reading .text: %v", err)
196 sym := tab.LookupFunc("linefrompc")
198 for pc := sym.Entry; pc < sym.End; pc++ {
199 file, line, fn := tab.PCToLine(pc)
200 off := pc - text.Addr // TODO(rsc): should not need off; bug in 8g
201 wantLine += int(textdat[off])
202 t.Logf("off is %d", off)
204 t.Errorf("failed to get line of PC %#x", pc)
205 } else if !strings.HasSuffix(file, "pclinetest.asm") {
206 t.Errorf("expected %s (%s) at PC %#x, got %s (%s)", "pclinetest.asm", sym.Name, pc, file, fn.Name)
207 } else if line != wantLine || fn != sym {
208 t.Errorf("expected :%d (%s) at PC %#x, got :%d (%s)", wantLine, sym.Name, pc, line, fn.Name)
213 sym = tab.LookupFunc("pcfromline")
216 off := uint64(0) // TODO(rsc): should not need off; bug in 8g
217 for pc := sym.Value; pc < sym.End; pc += 2 + uint64(textdat[off]) {
218 file, line, fn := tab.PCToLine(pc)
220 wantLine += int(textdat[off])
221 if line != wantLine {
222 t.Errorf("expected line %d at PC %#x in pcfromline, got %d", wantLine, pc, line)
223 off = pc + 1 - text.Addr
226 if lookupline == -1 {
229 for ; lookupline <= line; lookupline++ {
230 pc2, fn2, err := tab.LineToPC(file, lookupline)
231 if lookupline != line {
232 // Should be nothing on this line
234 t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
236 } else if err != nil {
237 t.Errorf("failed to get PC of line %d: %s", lookupline, err)
238 } else if pc != pc2 {
239 t.Errorf("expected PC %#x (%s) at line %d, got PC %#x (%s)", pc, fn.Name, line, pc2, fn2.Name)
242 off = pc + 1 - text.Addr