1// Copyright 2014 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package binutils
16
17import (
18	"fmt"
19	"regexp"
20	"testing"
21
22	"github.com/google/pprof/internal/plugin"
23)
24
25// TestFindSymbols tests the FindSymbols routine using a hardcoded nm output.
26func TestFindSymbols(t *testing.T) {
27	type testcase struct {
28		query, syms string
29		want        []plugin.Sym
30	}
31
32	testsyms := `0000000000001000 t lineA001
330000000000001000 t lineA002
340000000000001000 t line1000
350000000000002000 t line200A
360000000000002000 t line2000
370000000000002000 t line200B
380000000000003000 t line3000
390000000000003000 t _ZNK4DumbclEPKc
400000000000003000 t lineB00C
410000000000003000 t line300D
420000000000004000 t _the_end
43	`
44	testcases := []testcase{
45		{
46			"line.*[AC]",
47			testsyms,
48			[]plugin.Sym{
49				{Name: []string{"lineA001"}, File: "object.o", Start: 0x1000, End: 0x1FFF},
50				{Name: []string{"line200A"}, File: "object.o", Start: 0x2000, End: 0x2FFF},
51				{Name: []string{"lineB00C"}, File: "object.o", Start: 0x3000, End: 0x3FFF},
52			},
53		},
54		{
55			"Dumb::operator",
56			testsyms,
57			[]plugin.Sym{
58				{Name: []string{"Dumb::operator()(char const*) const"}, File: "object.o", Start: 0x3000, End: 0x3FFF},
59			},
60		},
61	}
62
63	for _, tc := range testcases {
64		syms, err := findSymbols([]byte(tc.syms), "object.o", regexp.MustCompile(tc.query), 0)
65		if err != nil {
66			t.Fatalf("%q: findSymbols: %v", tc.query, err)
67		}
68		if err := checkSymbol(syms, tc.want); err != nil {
69			t.Errorf("%q: %v", tc.query, err)
70		}
71	}
72}
73
74func checkSymbol(got []*plugin.Sym, want []plugin.Sym) error {
75	if len(got) != len(want) {
76		return fmt.Errorf("unexpected number of symbols %d (want %d)", len(got), len(want))
77	}
78
79	for i, g := range got {
80		w := want[i]
81		if len(g.Name) != len(w.Name) {
82			return fmt.Errorf("names, got %d, want %d", len(g.Name), len(w.Name))
83		}
84		for n := range g.Name {
85			if g.Name[n] != w.Name[n] {
86				return fmt.Errorf("name %d, got %q, want %q", n, g.Name[n], w.Name[n])
87			}
88		}
89		if g.File != w.File {
90			return fmt.Errorf("filename, got %q, want %q", g.File, w.File)
91		}
92		if g.Start != w.Start {
93			return fmt.Errorf("start address, got %#x, want %#x", g.Start, w.Start)
94		}
95		if g.End != w.End {
96			return fmt.Errorf("end address, got %#x, want %#x", g.End, w.End)
97		}
98	}
99	return nil
100}
101
102// TestFunctionAssembly tests the FunctionAssembly routine by using a
103// fake objdump script.
104func TestFunctionAssembly(t *testing.T) {
105	type testcase struct {
106		s    plugin.Sym
107		asm  string
108		want []plugin.Inst
109	}
110	testcases := []testcase{
111		{
112			plugin.Sym{Name: []string{"symbol1"}, Start: 0x1000, End: 0x1FFF},
113			"  1000: instruction one\n  1001: instruction two\n  1002: instruction three\n  1003: instruction four",
114			[]plugin.Inst{
115				{Addr: 0x1000, Text: "instruction one"},
116				{Addr: 0x1001, Text: "instruction two"},
117				{Addr: 0x1002, Text: "instruction three"},
118				{Addr: 0x1003, Text: "instruction four"},
119			},
120		},
121		{
122			plugin.Sym{Name: []string{"symbol2"}, Start: 0x2000, End: 0x2FFF},
123			"  2000: instruction one\n  2001: instruction two",
124			[]plugin.Inst{
125				{Addr: 0x2000, Text: "instruction one"},
126				{Addr: 0x2001, Text: "instruction two"},
127			},
128		},
129		{
130			plugin.Sym{Name: []string{"_main"}, Start: 0x30000, End: 0x3FFF},
131			"_main:\n; /tmp/hello.c:3\n30001:	push   %rbp",
132			[]plugin.Inst{
133				{Addr: 0x30001, Text: "push   %rbp", Function: "_main", File: "/tmp/hello.c", Line: 3},
134			},
135		},
136		{
137			plugin.Sym{Name: []string{"main"}, Start: 0x4000, End: 0x4FFF},
138			"000000000040052d <main>:\nmain():\n/tmp/hello.c:3\n40001:	push   %rbp",
139			[]plugin.Inst{
140				{Addr: 0x40001, Text: "push   %rbp", Function: "main", File: "/tmp/hello.c", Line: 3},
141			},
142		},
143	}
144
145	for _, tc := range testcases {
146		insts, err := disassemble([]byte(tc.asm))
147		if err != nil {
148			t.Fatalf("FunctionAssembly: %v", err)
149		}
150
151		if len(insts) != len(tc.want) {
152			t.Errorf("Unexpected number of assembly instructions %d (want %d)\n", len(insts), len(tc.want))
153		}
154		for i := range insts {
155			if insts[i] != tc.want[i] {
156				t.Errorf("Expected symbol %v, got %v\n", tc.want[i], insts[i])
157			}
158		}
159	}
160}
161