| 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 symbolzscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 19 | "mathscip-go gomod github.com/golang/go/src go1.24.0 math/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 21 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 22 | |
| 23 | "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/" |
| 24 | "github.com/google/pprof/internal/proftestscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/" |
| 25 | "github.com/google/pprof/profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/" |
| 26 | ) |
| 27 | |
| 28 | func TestSymbolzURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/TestSymbolzURL().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 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 := symbolzscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolz().(try); got != want { |
| 52 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().(`symbolz(%s)=%s, want "%s"`, try, got, want) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestSymbolizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/TestSymbolize().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 58 | s := pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.MappingSourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/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 := testProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/testProfile().(hasFunctions) |
| 70 | |
| 71 | if err := Symbolizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/Symbolize().(p, force, s, fetchSymbolsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/fetchSymbols()., &proftestscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/.TestUIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#{Tscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/proftest`/TestUI#T.: t}); err != nil { |
| 72 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("symbolz: %v", err) |
| 73 | continue |
| 74 | } |
| 75 | var wantSym, wantNoSym []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location# |
| 76 | if force || !hasFunctions { |
| 77 | wantNoSym = p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location.[:1] |
| 78 | wantSym = p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location.[1:] |
| 79 | } else { |
| 80 | wantNoSym = p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location. |
| 81 | } |
| 82 | |
| 83 | if err := checkSymbolizedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/checkSymbolized().(wantSym, true); err != nil { |
| 84 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err) |
| 85 | } |
| 86 | if err := checkSymbolizedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/checkSymbolized().(wantNoSym, false); err != nil { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func testProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/testProfile().(hasFunctions bool) *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile# { |
| 94 | m := []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#{ |
| 95 | { |
| 96 | IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#ID.: 1, |
| 97 | Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Start.: 0x1000, |
| 98 | Limitscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Limit.: 0x5000, |
| 99 | BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID.: "buildid", |
| 100 | HasFunctionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#HasFunctions.: hasFunctions, |
| 101 | }, |
| 102 | } |
| 103 | p := &profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#{ |
| 104 | Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location.: []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#{ |
| 105 | {IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 1, Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: m[0], Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address.: 0x1000}, |
| 106 | {IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 2, Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: m[0], Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address.: 0x2000}, |
| 107 | {IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 3, Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: m[0], Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address.: 0x3000}, |
| 108 | {IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 4, Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: m[0], Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address.: 0x4000}, |
| 109 | }, |
| 110 | Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.: m, |
| 111 | } |
| 112 | |
| 113 | return p |
| 114 | } |
| 115 | |
| 116 | func checkSymbolizedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/checkSymbolized().(locs []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#, wantSymbolized bool) error { |
| 117 | for _, loc := range locs { |
| 118 | if !wantSymbolized && len(loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) != 0 { |
| 119 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("unexpected symbolization for %#x: %v", loc.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address., loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) |
| 120 | } |
| 121 | if wantSymbolized { |
| 122 | if len(loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) != 1 { |
| 123 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("expected symbolization for %#x: %v", loc.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address., loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) |
| 124 | } |
| 125 | address := loc.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address. - loc.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping..Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Start. |
| 126 | if got, want := loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[0].Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function..Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name., fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%#x", address); got != want { |
| 127 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("symbolz %#x, got %s, want %s", address, got, want) |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func fetchSymbolsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/fetchSymbols().(source, post string) ([]byte, error) { |
| 135 | var symbolz string |
| 136 | |
| 137 | addresses := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.24.0 strings/Split().(post, "+") |
| 138 | // Do not symbolize the first symbol. |
| 139 | for _, address := range addresses[1:] { |
| 140 | symbolz += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s\t%s\n", address, address) |
| 141 | } |
| 142 | return []byte(symbolz), nil |
| 143 | } |
| 144 | |
| 145 | func TestAdjustscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/TestAdjust().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 146 | for _, tc := range []struct { |
| 147 | addr uint64 |
| 148 | offset int64 |
| 149 | wantAdj uint64 |
| 150 | wantOverflow bool |
| 151 | }{{mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint64., 0, mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint64., false}, |
| 152 | {mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint64., 1, 0, true}, |
| 153 | {mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint64. - 1, 1, mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint64., false}, |
| 154 | {mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxUint64. - 1, 2, 0, true}, |
| 155 | {mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxInt64. + 1, mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MaxInt64., mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.24.0 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 | {-mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MinInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MinInt64., mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MinInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MinInt64., 0, false}, |
| 163 | {-mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MinInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MinInt64. + 1, mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MinInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MinInt64., 1, false}, |
| 164 | {-mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MinInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MinInt64. - 1, mathscip-go gomod github.com/golang/go/src go1.24.0 math/.MinInt64scip-go gomod github.com/golang/go/src go1.24.0 math/MinInt64., 0, true}, |
| 165 | } { |
| 166 | if adj, overflow := adjustscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/adjust().(tc.addr, tc.offset); adj != tc.wantAdj || overflow != tc.wantOverflow { |
| 167 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("adjust(%d, %d) = (%d, %t), want (%d, %t)", tc.addr, tc.offset, adj, overflow, tc.wantAdj, tc.wantOverflow) |
| 168 | } |
| 169 | } |
| 170 | } |