1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package symbolz
import (
"fmt"
"math"
"strings"
"testing"
"github.com/google/pprof/internal/plugin"
"github.com/google/pprof/internal/proftest"
"github.com/google/pprof/profile"
)
func TestSymbolzURL(t *testing.T) {
for try, want := range map[string]string{
"http://host:8000/profilez": "http://host:8000/symbolz",
"http://host:8000/profilez?seconds=5": "http://host:8000/symbolz",
"http://host:8000/profilez?seconds=5&format=proto": "http://host:8000/symbolz",
"http://host:8000/heapz?format=legacy": "http://host:8000/symbolz",
"http://host:8000/debug/pprof/profile": "http://host:8000/debug/pprof/symbol",
"http://host:8000/debug/pprof/profile?seconds=10": "http://host:8000/debug/pprof/symbol",
"http://host:8000/debug/pprof/heap": "http://host:8000/debug/pprof/symbol",
"http://some.host:8080/some/deeper/path/debug/pprof/endpoint?param=value": "http://some.host:8080/some/deeper/path/debug/pprof/symbol",
"http://host:8000/pprof/profile": "http://host:8000/pprof/symbol",
"http://host:8000/pprof/profile?seconds=15": "http://host:8000/pprof/symbol",
"http://host:8000/pprof/heap": "http://host:8000/pprof/symbol",
"http://host:8000/debug/pprof/block": "http://host:8000/debug/pprof/symbol",
"http://host:8000/debug/pprof/trace?seconds=5": "http://host:8000/debug/pprof/symbol",
"http://host:8000/debug/pprof/mutex": "http://host:8000/debug/pprof/symbol",
"http://host/whatever/pprof/heap": "http://host/whatever/pprof/symbol",
"http://host/whatever/pprof/growth": "http://host/whatever/pprof/symbol",
"http://host/whatever/pprof/profile": "http://host/whatever/pprof/symbol",
"http://host/whatever/pprof/pmuprofile": "http://host/whatever/pprof/symbol",
"http://host/whatever/pprof/contention": "http://host/whatever/pprof/symbol",
} {
if got := symbolz(try); got != want {
t.Errorf(`symbolz(%s)=%s, want "%s"`, try, got, want)
}
}
}
func TestSymbolize(t *testing.T) {
s := plugin.MappingSources{
"buildid": []struct {
Source string
Start uint64
}{
{Source: "http://localhost:80/profilez"},
},
}
for _, hasFunctions := range []bool{false, true} {
for _, force := range []bool{false, true} {
p := testProfile(hasFunctions)
if err := Symbolize(p, force, s, fetchSymbols, &proftest.TestUI{T: t}); err != nil {
t.Errorf("symbolz: %v", err)
continue
}
var wantSym, wantNoSym []*profile.Location
if force || !hasFunctions {
wantNoSym = p.Location[:1]
wantSym = p.Location[1:]
} else {
wantNoSym = p.Location
}
if err := checkSymbolized(wantSym, true); err != nil {
t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
}
if err := checkSymbolized(wantNoSym, false); err != nil {
t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
}
}
}
}
func testProfile(hasFunctions bool) *profile.Profile {
m := []*profile.Mapping{
{
ID: 1,
Start: 0x1000,
Limit: 0x5000,
BuildID: "buildid",
HasFunctions: hasFunctions,
},
}
p := &profile.Profile{
Location: []*profile.Location{
{ID: 1, Mapping: m[0], Address: 0x1000},
{ID: 2, Mapping: m[0], Address: 0x2000},
{ID: 3, Mapping: m[0], Address: 0x3000},
{ID: 4, Mapping: m[0], Address: 0x4000},
},
Mapping: m,
}
return p
}
func checkSymbolized(locs []*profile.Location, wantSymbolized bool) error {
for _, loc := range locs {
if !wantSymbolized && len(loc.Line) != 0 {
return fmt.Errorf("unexpected symbolization for %#x: %v", loc.Address, loc.Line)
}
if wantSymbolized {
if len(loc.Line) != 1 {
return fmt.Errorf("expected symbolization for %#x: %v", loc.Address, loc.Line)
}
address := loc.Address - loc.Mapping.Start
if got, want := loc.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
return fmt.Errorf("symbolz %#x, got %s, want %s", address, got, want)
}
}
}
return nil
}
func fetchSymbols(source, post string) ([]byte, error) {
var symbolz string
addresses := strings.Split(post, "+")
// Do not symbolize the first symbol.
for _, address := range addresses[1:] {
symbolz += fmt.Sprintf("%s\t%s\n", address, address)
}
return []byte(symbolz), nil
}
func TestAdjust(t *testing.T) {
for _, tc := range []struct {
addr uint64
offset int64
wantAdj uint64
wantOverflow bool
}{{math.MaxUint64, 0, math.MaxUint64, false},
{math.MaxUint64, 1, 0, true},
{math.MaxUint64 - 1, 1, math.MaxUint64, false},
{math.MaxUint64 - 1, 2, 0, true},
{math.MaxInt64 + 1, math.MaxInt64, math.MaxUint64, false},
{0, 0, 0, false},
{0, -1, 0, true},
{1, -1, 0, false},
{2, -1, 1, false},
{2, -2, 0, false},
{2, -3, 0, true},
{-math.MinInt64, math.MinInt64, 0, false},
{-math.MinInt64 + 1, math.MinInt64, 1, false},
{-math.MinInt64 - 1, math.MinInt64, 0, true},
} {
if adj, overflow := adjust(tc.addr, tc.offset); adj != tc.wantAdj || overflow != tc.wantOverflow {
t.Errorf("adjust(%d, %d) = (%d, %t), want (%d, %t)", tc.addr, tc.offset, adj, overflow, tc.wantAdj, tc.wantOverflow)
}
}
}
|