ade704335d14b843ec67b3d2b151f1bbb687f968
[platform/upstream/gcc48.git] / libgo / go / debug / gosym / pclntab_test.go
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.
4
5 package gosym
6
7 import (
8         "debug/elf"
9         "fmt"
10         "io/ioutil"
11         "os"
12         "os/exec"
13         "path/filepath"
14         "runtime"
15         "strings"
16         "testing"
17 )
18
19 var (
20         pclineTempDir    string
21         pclinetestBinary string
22 )
23
24 func dotest() bool {
25         // For now, only works on ELF platforms.
26         if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
27                 return false
28         }
29         if pclinetestBinary != "" {
30                 return true
31         }
32         var err error
33         pclineTempDir, err = ioutil.TempDir("", "pclinetest")
34         if err != nil {
35                 panic(err)
36         }
37         if strings.Contains(pclineTempDir, " ") {
38                 panic("unexpected space in tempdir")
39         }
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 {
50                 panic(err)
51         }
52         return true
53 }
54
55 func getTable(t *testing.T) *Table {
56         f, tab := crack(os.Args[0], t)
57         f.Close()
58         return tab
59 }
60
61 func crack(file string, t *testing.T) (*elf.File, *Table) {
62         // Open self
63         f, err := elf.Open(file)
64         if err != nil {
65                 t.Fatal(err)
66         }
67         return parse(file, f, t)
68 }
69
70 func parse(file string, f *elf.File, t *testing.T) (*elf.File, *Table) {
71         symdat, err := f.Section(".gosymtab").Data()
72         if err != nil {
73                 f.Close()
74                 t.Fatalf("reading %s gosymtab: %v", file, err)
75         }
76         pclndat, err := f.Section(".gopclntab").Data()
77         if err != nil {
78                 f.Close()
79                 t.Fatalf("reading %s gopclntab: %v", file, err)
80         }
81
82         pcln := NewLineTable(pclndat, f.Section(".text").Addr)
83         tab, err := NewTable(symdat, pcln)
84         if err != nil {
85                 f.Close()
86                 t.Fatalf("parsing %s gosymtab: %v", file, err)
87         }
88
89         return f, tab
90 }
91
92 var goarch = os.Getenv("O")
93
94 func TestLineFromAline(t *testing.T) {
95         if !dotest() {
96                 return
97         }
98
99         tab := getTable(t)
100
101         // Find the sym package
102         pkg := tab.LookupFunc("debug/gosym.TestLineFromAline").Obj
103         if pkg == nil {
104                 t.Fatalf("nil pkg")
105         }
106
107         // Walk every absolute line and ensure that we hit every
108         // source line monotonically
109         lastline := make(map[string]int)
110         final := -1
111         for i := 0; i < 10000; i++ {
112                 path, line := pkg.lineFromAline(i)
113                 // Check for end of object
114                 if path == "" {
115                         if final == -1 {
116                                 final = i - 1
117                         }
118                         continue
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)
121                 }
122                 // It's okay to see files multiple times (e.g., sys.a)
123                 if line == 1 {
124                         lastline[path] = 1
125                         continue
126                 }
127                 // Check that the is the next line in path
128                 ll, ok := lastline[path]
129                 if !ok {
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)
133                 }
134                 lastline[path] = line
135         }
136         if final == -1 {
137                 t.Errorf("never reached end of object")
138         }
139 }
140
141 func TestLineAline(t *testing.T) {
142         if !dotest() {
143                 return
144         }
145
146         tab := getTable(t)
147
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)
155                         if path == "" {
156                                 break
157                         }
158
159                         // cgo files are full of 'Z' symbols, which we don't handle
160                         if len(path) > 4 && path[len(path)-4:] == ".cgo" {
161                                 continue
162                         }
163
164                         if minline, ok := found[path]; path != "" && ok {
165                                 if minline >= line {
166                                         // We've already covered this file
167                                         continue
168                                 }
169                         }
170                         found[path] = line
171
172                         a, err := o.alineFromLine(path, line)
173                         if err != nil {
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)
175                         } else if a != i {
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)
177                         }
178                 }
179         }
180 }
181
182 func TestPCLine(t *testing.T) {
183         if !dotest() {
184                 return
185         }
186         defer os.RemoveAll(pclineTempDir)
187
188         f, tab := crack(pclinetestBinary, t)
189         text := f.Section(".text")
190         textdat, err := text.Data()
191         if err != nil {
192                 t.Fatalf("reading .text: %v", err)
193         }
194
195         // Test PCToLine
196         sym := tab.LookupFunc("linefrompc")
197         wantLine := 0
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)
203                 if fn == nil {
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)
209                 }
210         }
211
212         // Test LineToPC
213         sym = tab.LookupFunc("pcfromline")
214         lookupline := -1
215         wantLine = 0
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)
219                 off = pc - text.Addr
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
224                         continue
225                 }
226                 if lookupline == -1 {
227                         lookupline = line
228                 }
229                 for ; lookupline <= line; lookupline++ {
230                         pc2, fn2, err := tab.LineToPC(file, lookupline)
231                         if lookupline != line {
232                                 // Should be nothing on this line
233                                 if err == nil {
234                                         t.Errorf("expected no PC at line %d, got %#x (%s)", lookupline, pc2, fn2.Name)
235                                 }
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)
240                         }
241                 }
242                 off = pc + 1 - text.Addr
243         }
244 }