Debian Codegraph @

internal/symbolz/symbolz_test.go

raw symbols SCIP
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
15 package symbolz
16
17 import (
18 "fmt"
19 "math"
20 "strings"
21 "testing"
22
23 "github.com/google/pprof/internal/plugin"
24 "github.com/google/pprof/internal/proftest"
25 "github.com/google/pprof/profile"
26 )
27
28 func TestSymbolzURL(t *testing.T) {
29 for try, want := range map[string]string{
30 "http://host:8000/profilez": "http://host:8000/symbolz",
31 "http://host:8000/profilez?seconds=5": "http://host:8000/symbolz",
32 "http://host:8000/profilez?seconds=5&format=proto": "http://host:8000/symbolz",
33 "http://host:8000/heapz?format=legacy": "http://host:8000/symbolz",
34 "http://host:8000/some/deeper/path/profilez?seconds=5": "http://host:8000/some/deeper/path/symbolz",
35 "http://host:8000/debug/pprof/profile": "http://host:8000/debug/pprof/symbol",
36 "http://host:8000/debug/pprof/profile?seconds=10": "http://host:8000/debug/pprof/symbol",
37 "http://host:8000/debug/pprof/heap": "http://host:8000/debug/pprof/symbol",
38 "http://some.host:8080/some/deeper/path/debug/pprof/endpoint?param=value": "http://some.host:8080/some/deeper/path/debug/pprof/symbol",
39 "http://host:8000/pprof/profile": "http://host:8000/pprof/symbol",
40 "http://host:8000/pprof/profile?seconds=15": "http://host:8000/pprof/symbol",
41 "http://host:8000/pprof/heap": "http://host:8000/pprof/symbol",
42 "http://host:8000/debug/pprof/block": "http://host:8000/debug/pprof/symbol",
43 "http://host:8000/debug/pprof/trace?seconds=5": "http://host:8000/debug/pprof/symbol",
44 "http://host:8000/debug/pprof/mutex": "http://host:8000/debug/pprof/symbol",
45 "http://host/whatever/pprof/heap": "http://host/whatever/pprof/symbol",
46 "http://host/whatever/pprof/growth": "http://host/whatever/pprof/symbol",
47 "http://host/whatever/pprof/profile": "http://host/whatever/pprof/symbol",
48 "http://host/whatever/pprof/pmuprofile": "http://host/whatever/pprof/symbol",
49 "http://host/whatever/pprof/contention": "http://host/whatever/pprof/symbol",
50 } {
51 if got := symbolz(try); got != want {
52 t.Errorf(`symbolz(%s)=%s, want "%s"`, try, got, want)
53 }
54 }
55 }
56
57 func TestSymbolize(t *testing.T) {
58 s := plugin.MappingSources{
59 "buildid": []struct {
60 Source string
61 Start uint64
62 }{
63 {Source: "http://localhost:80/profilez"},
64 },
65 }
66
67 for _, hasFunctions := range []bool{false, true} {
68 for _, force := range []bool{false, true} {
69 p := testProfile(hasFunctions)
70
71 if err := Symbolize(p, force, s, fetchSymbols, &proftest.TestUI{T: t}); err != nil {
72 t.Errorf("symbolz: %v", err)
73 continue
74 }
75 var wantSym, wantNoSym []*profile.Location
76 if force || !hasFunctions {
77 wantNoSym = p.Location[:1]
78 wantSym = p.Location[1:]
79 } else {
80 wantNoSym = p.Location
81 }
82
83 if err := checkSymbolized(wantSym, true); err != nil {
84 t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
85 }
86 if err := checkSymbolized(wantNoSym, false); err != nil {
87 t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
88 }
89 }
90 }
91 }
92
93 func testProfile(hasFunctions bool) *profile.Profile {
94 m := []*profile.Mapping{
95 {
96 ID: 1,
97 Start: 0x1000,
98 Limit: 0x5000,
99 BuildID: "buildid",
100 HasFunctions: hasFunctions,
101 },
102 }
103 p := &profile.Profile{
104 Location: []*profile.Location{
105 {ID: 1, Mapping: m[0], Address: 0x1000},
106 {ID: 2, Mapping: m[0], Address: 0x2000},
107 {ID: 3, Mapping: m[0], Address: 0x3000},
108 {ID: 4, Mapping: m[0], Address: 0x4000},
109 },
110 Mapping: m,
111 }
112
113 return p
114 }
115
116 func checkSymbolized(locs []*profile.Location, wantSymbolized bool) error {
117 for _, loc := range locs {
118 if !wantSymbolized && len(loc.Line) != 0 {
119 return fmt.Errorf("unexpected symbolization for %#x: %v", loc.Address, loc.Line)
120 }
121 if wantSymbolized {
122 if len(loc.Line) != 1 {
123 return fmt.Errorf("expected symbolization for %#x: %v", loc.Address, loc.Line)
124 }
125 address := loc.Address - loc.Mapping.Start
126 if got, want := loc.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
127 return fmt.Errorf("symbolz %#x, got %s, want %s", address, got, want)
128 }
129 }
130 }
131 return nil
132 }
133
134 func fetchSymbols(source, post string) ([]byte, error) {
135 var symbolz string
136
137 addresses := strings.Split(post, "+")
138 // Do not symbolize the first symbol.
139 for _, address := range addresses[1:] {
140 symbolz += fmt.Sprintf("%s\t%s\n", address, address)
141 }
142 return []byte(symbolz), nil
143 }
144
145 func TestAdjust(t *testing.T) {
146 for _, tc := range []struct {
147 addr uint64
148 offset int64
149 wantAdj uint64
150 wantOverflow bool
151 }{{math.MaxUint64, 0, math.MaxUint64, false},
152 {math.MaxUint64, 1, 0, true},
153 {math.MaxUint64 - 1, 1, math.MaxUint64, false},
154 {math.MaxUint64 - 1, 2, 0, true},
155 {math.MaxInt64 + 1, math.MaxInt64, math.MaxUint64, false},
156 {0, 0, 0, false},
157 {0, -1, 0, true},
158 {1, -1, 0, false},
159 {2, -1, 1, false},
160 {2, -2, 0, false},
161 {2, -3, 0, true},
162 {-math.MinInt64, math.MinInt64, 0, false},
163 {-math.MinInt64 + 1, math.MinInt64, 1, false},
164 {-math.MinInt64 - 1, math.MinInt64, 0, true},
165 } {
166 if adj, overflow := adjust(tc.addr, tc.offset); adj != tc.wantAdj || overflow != tc.wantOverflow {
167 t.Errorf("adjust(%d, %d) = (%d, %t), want (%d, %t)", tc.addr, tc.offset, adj, overflow, tc.wantAdj, tc.wantOverflow)
168 }
169 }
170 }