Debian Codegraph @

internal/report/source_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Copyright 2017 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
15 package report
16
17 import (
18 "fmt"
19 "os"
20 "path/filepath"
21 "regexp"
22 "runtime"
23 "strings"
24 "testing"
25
26 "github.com/google/pprof/internal/binutils"
27 "github.com/google/pprof/profile"
28 )
29
30 func TestWebList(t *testing.T) {
•31 t.Skip("https://github.com/google/pprof/issues/650")
32 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
33 t.Skip("weblist only tested on x86-64 linux")
34 }
35
36 cpu := readProfile(filepath.Join("testdata", "sample.cpu"), t)
37 rpt := New(cpu, &Options{
38 OutputFormat: WebList,
39 Symbol: regexp.MustCompile("busyLoop"),
40 SampleValue: func(v []int64) int64 { return v[1] },
41 SampleUnit: cpu.SampleType[1].Unit,
42 })
43 result, err := MakeWebList(rpt, &binutils.Binutils{}, -1)
44 if err != nil {
45 t.Fatalf("could not generate weblist: %v", err)
46 }
47 output := fmt.Sprint(result)
48
49 for _, expect := range []string{"func busyLoop", "call.*mapassign"} {
50 if match, _ := regexp.MatchString(expect, output); !match {
51 t.Errorf("weblist output does not contain '%s':\n%s", expect, output)
52 }
53 }
54 }
55
56 func TestSourceSyntheticAddress(t *testing.T) {
57 testSourceMapping(t, true)
58 }
59
60 func TestSourceMissingMapping(t *testing.T) {
61 testSourceMapping(t, false)
62 }
63
64 // testSourceMapping checks that source info is found even when no applicable
65 // Mapping/objectFile exists. The locations used in the test are either zero
66 // (if zeroAddress is true), or non-zero (otherwise).
67 func testSourceMapping(t *testing.T, zeroAddress bool) {
68 nextAddr := uint64(0)
69
70 makeLoc := func(name, fname string, line int64) *profile.Location {
71 if !zeroAddress {
72 nextAddr++
73 }
74 return &profile.Location{
75 Address: nextAddr,
76 Line: []profile.Line{
77 {
78 Function: &profile.Function{Name: name, Filename: fname},
79 Line: line,
80 },
81 },
82 }
83 }
84
85 // Create profile that will need synthetic addresses since it has no mappings.
86 foo100 := makeLoc("foo", "foo.go", 100)
87 bar50 := makeLoc("bar", "bar.go", 50)
88 prof := &profile.Profile{
89 Sample: []*profile.Sample{
90 {
91 Value: []int64{9},
92 Location: []*profile.Location{foo100, bar50},
93 },
94 {
95 Value: []int64{17},
96 Location: []*profile.Location{bar50},
97 },
98 },
99 }
100 rpt := &Report{
101 prof: prof,
102 options: &Options{
103 Symbol: regexp.MustCompile("foo|bar"),
104 SampleValue: func(s []int64) int64 { return s[0] },
105 },
106 formatValue: func(v int64) string { return fmt.Sprint(v) },
107 }
108
109 result, err := MakeWebList(rpt, nil, -1)
110 if err != nil {
111 t.Fatalf("MakeWebList returned unexpected error: %v", err)
112 }
113 got := fmt.Sprint(result)
114
115 expect := regexp.MustCompile(
116 `(?s)` + // Allow "." to match newline
117 `bar\.go.* 50\b.* 17 +26 .*` +
118 `foo\.go.* 100\b.* 9 +9 `)
119 if !expect.MatchString(got) {
120 t.Errorf("expected regular expression %v does not match output:\n%s\n", expect, got)
121 }
122 }
123
124 func TestOpenSourceFile(t *testing.T) {
125 tempdir, err := os.MkdirTemp("", "")
126 if err != nil {
127 t.Fatalf("failed to create temp dir: %v", err)
128 }
129 const lsep = string(filepath.ListSeparator)
130 for _, tc := range []struct {
131 desc string
132 searchPath string
133 trimPath string
134 fs []string
135 path string
136 wantPath string // If empty, error is wanted.
137 }{
138 {
139 desc: "exact absolute path is found",
140 fs: []string{"foo/bar.cc"},
141 path: "$dir/foo/bar.cc",
142 wantPath: "$dir/foo/bar.cc",
143 },
144 {
145 desc: "exact relative path is found",
146 searchPath: "$dir",
147 fs: []string{"foo/bar.cc"},
148 path: "foo/bar.cc",
149 wantPath: "$dir/foo/bar.cc",
150 },
151 {
152 desc: "multiple search path",
153 searchPath: "some/path" + lsep + "$dir",
154 fs: []string{"foo/bar.cc"},
155 path: "foo/bar.cc",
156 wantPath: "$dir/foo/bar.cc",
157 },
158 {
159 desc: "relative path is found in parent dir",
160 searchPath: "$dir/foo/bar",
161 fs: []string{"bar.cc", "foo/bar/baz.cc"},
162 path: "bar.cc",
163 wantPath: "$dir/bar.cc",
164 },
165 {
166 desc: "trims configured prefix",
167 searchPath: "$dir",
168 trimPath: "some-path" + lsep + "/some/remote/path",
169 fs: []string{"my-project/foo/bar.cc"},
170 path: "/some/remote/path/my-project/foo/bar.cc",
171 wantPath: "$dir/my-project/foo/bar.cc",
172 },
173 {
174 desc: "trims heuristically",
175 searchPath: "$dir/my-project",
176 fs: []string{"my-project/foo/bar.cc"},
177 path: "/some/remote/path/my-project/foo/bar.cc",
178 wantPath: "$dir/my-project/foo/bar.cc",
179 },
180 {
181 desc: "error when not found",
182 path: "foo.cc",
183 },
184 } {
185 t.Run(tc.desc, func(t *testing.T) {
186 defer func() {
187 if err := os.RemoveAll(tempdir); err != nil {
188 t.Fatalf("failed to remove dir %q: %v", tempdir, err)
189 }
190 }()
191 for _, f := range tc.fs {
192 path := filepath.Join(tempdir, filepath.FromSlash(f))
193 dir := filepath.Dir(path)
194 if err := os.MkdirAll(dir, 0755); err != nil {
195 t.Fatalf("failed to create dir %q: %v", dir, err)
196 }
197 if err := os.WriteFile(path, nil, 0644); err != nil {
198 t.Fatalf("failed to create file %q: %v", path, err)
199 }
200 }
201 tc.searchPath = filepath.FromSlash(strings.ReplaceAll(tc.searchPath, "$dir", tempdir))
202 tc.path = filepath.FromSlash(strings.Replace(tc.path, "$dir", tempdir, 1))
203 tc.wantPath = filepath.FromSlash(strings.Replace(tc.wantPath, "$dir", tempdir, 1))
204 if file, err := openSourceFile(tc.path, tc.searchPath, tc.trimPath); err != nil && tc.wantPath != "" {
205 t.Errorf("openSourceFile(%q, %q, %q) = err %v, want path %q", tc.path, tc.searchPath, tc.trimPath, err, tc.wantPath)
206 } else if err == nil {
207 defer file.Close()
208 gotPath := file.Name()
209 if tc.wantPath == "" {
210 t.Errorf("openSourceFile(%q, %q, %q) = %q, want error", tc.path, tc.searchPath, tc.trimPath, gotPath)
211 } else if gotPath != tc.wantPath {
212 t.Errorf("openSourceFile(%q, %q, %q) = %q, want path %q", tc.path, tc.searchPath, tc.trimPath, gotPath, tc.wantPath)
213 }
214 }
215 })
216 }
217 }
218
219 func TestIndentation(t *testing.T) {
220 for _, c := range []struct {
221 str string
222 wantIndent int
223 }{
224 {"", 0},
225 {"foobar", 0},
226 {" foo", 2},
227 {"\tfoo", 8},
228 {"\t foo", 9},
229 {" \tfoo", 8},
230 {" \tfoo", 8},
231 {" \tfoo", 16},
232 } {
233 if n := indentation(c.str); n != c.wantIndent {
234 t.Errorf("indentation(%v): got %d, want %d", c.str, n, c.wantIndent)
235 }
236 }
237 }
238
239 func TestRightPad(t *testing.T) {
240 for _, c := range []struct {
241 pad int
242 in string
243 expect string
244 }{
245 {0, "", ""},
246 {4, "", " "},
247 {4, "x", "x "},
248 {4, "abcd", "abcd"}, // No padding because of overflow
249 {4, "abcde", "abcde"}, // No padding because of overflow
250 {10, "\tx", " x "},
251 {10, "w\txy\tz", "w xy z"},
252 {20, "w\txy\tz", "w xy z "},
253 } {
254 out := rightPad(c.in, c.pad)
255 if out != c.expect {
256 t.Errorf("rightPad(%q, %d): got %q, want %q", c.in, c.pad, out, c.expect)
257 }
258 }
259 }
260
261 func readProfile(fname string, t *testing.T) *profile.Profile {
262 file, err := os.Open(fname)
263 if err != nil {
264 t.Fatalf("%s: could not open profile: %v", fname, err)
265 }
266 defer file.Close()
267 p, err := profile.Parse(file)
268 if err != nil {
269 t.Fatalf("%s: could not parse profile: %v", fname, err)
270 }
271
272 // Fix file names so they do not include absolute path names.
273 fix := func(s string) string {
274 const testdir = "/internal/report/"
275 pos := strings.Index(s, testdir)
276 if pos == -1 {
277 return s
278 }
279 return s[pos+len(testdir):]
280 }
281 for _, m := range p.Mapping {
282 m.File = fix(m.File)
283 }
284 for _, f := range p.Function {
285 f.Filename = fix(f.Filename)
286 }
287
288 return p
289 }