| 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 symbolizer provides a routine to populate a profile with |
| 16 |
// symbol, file and line number information. It relies on the |
| 17 |
// addr2liner and demangle packages to do the actual work. |
| 18 |
package symbolizer |
| 19 |
|
| 20 |
import ( |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"net/http" |
| 24 |
"net/url" |
| 25 |
"path/filepath" |
| 26 |
"strings" |
| 27 |
|
| 28 |
"github.com/google/pprof/internal/binutils" |
| 29 |
"github.com/google/pprof/internal/plugin" |
| 30 |
"github.com/google/pprof/internal/symbolz" |
| 31 |
"github.com/google/pprof/profile" |
| 32 |
"github.com/ianlancetaylor/demangle" |
| 33 |
) |
| 34 |
|
| 35 |
// Symbolizer implements the plugin.Symbolize interface. |
| 36 |
type Symbolizer struct { |
| 37 |
Obj plugin.ObjTool |
| 38 |
UI plugin.UI |
| 39 |
Transport http.RoundTripper |
| 40 |
} |
| 41 |
|
| 42 |
// test taps for dependency injection |
| 43 |
var symbolzSymbolize = symbolz.Symbolize |
| 44 |
var localSymbolize = doLocalSymbolize |
| 45 |
var demangleFunction = Demangle |
| 46 |
|
| 47 |
// Symbolize attempts to symbolize profile p. First uses binutils on |
| 48 |
// local binaries; if the source is a URL it attempts to get any |
| 49 |
// missed entries using symbolz. |
| 50 |
func (s *Symbolizer) Symbolize(mode string, sources plugin.MappingSources, p *profile.Profile) error { |
| 51 |
remote, local, fast, force, demanglerMode := true, true, false, false, "" |
| 52 |
for _, o := range strings.Split(strings.ToLower(mode), ":") { |
| 53 |
switch o { |
| 54 |
case "": |
| 55 |
continue |
| 56 |
case "none", "no": |
| 57 |
return nil |
| 58 |
case "local": |
| 59 |
remote, local = false, true |
| 60 |
case "fastlocal": |
| 61 |
remote, local, fast = false, true, true |
| 62 |
case "remote": |
| 63 |
remote, local = true, false |
| 64 |
case "force": |
| 65 |
force = true |
| 66 |
default: |
| 67 |
switch d := strings.TrimPrefix(o, "demangle="); d { |
| 68 |
case "full", "none", "templates": |
| 69 |
demanglerMode = d |
| 70 |
force = true |
| 71 |
continue |
| 72 |
case "default": |
| 73 |
continue |
| 74 |
} |
| 75 |
s.UI.PrintErr("ignoring unrecognized symbolization option: " + mode) |
| 76 |
s.UI.PrintErr("expecting -symbolize=[local|fastlocal|remote|none][:force][:demangle=[none|full|templates|default]") |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
var err error |
| 81 |
if local { |
| 82 |
// Symbolize locally using binutils. |
| 83 |
if err = localSymbolize(p, fast, force, s.Obj, s.UI); err != nil { |
| 84 |
s.UI.PrintErr("local symbolization: " + err.Error()) |
| 85 |
} |
| 86 |
} |
| 87 |
if remote { |
| 88 |
post := func(source, post string) ([]byte, error) { |
| 89 |
return postURL(source, post, s.Transport) |
| 90 |
} |
| 91 |
if err = symbolzSymbolize(p, force, sources, post, s.UI); err != nil { |
| 92 |
return err // Ran out of options. |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
demangleFunction(p, force, demanglerMode) |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
// postURL issues a POST to a URL over HTTP. |
| 101 |
func postURL(source, post string, tr http.RoundTripper) ([]byte, error) { |
| 102 |
client := &http.Client{ |
| 103 |
Transport: tr, |
| 104 |
} |
| 105 |
resp, err := client.Post(source, "application/octet-stream", strings.NewReader(post)) |
| 106 |
if err != nil { |
| 107 |
return nil, fmt.Errorf("http post %s: %v", source, err) |
| 108 |
} |
| 109 |
defer resp.Body.Close() |
| 110 |
if resp.StatusCode != http.StatusOK { |
| 111 |
return nil, fmt.Errorf("http post %s: %v", source, statusCodeError(resp)) |
| 112 |
} |
| 113 |
return io.ReadAll(resp.Body) |
| 114 |
} |
| 115 |
|
| 116 |
func statusCodeError(resp *http.Response) error { |
| 117 |
if resp.Header.Get("X-Go-Pprof") != "" && strings.Contains(resp.Header.Get("Content-Type"), "text/plain") { |
| 118 |
// error is from pprof endpoint |
| 119 |
if body, err := io.ReadAll(resp.Body); err == nil { |
| 120 |
return fmt.Errorf("server response: %s - %s", resp.Status, body) |
| 121 |
} |
| 122 |
} |
| 123 |
return fmt.Errorf("server response: %s", resp.Status) |
| 124 |
} |
| 125 |
|
| 126 |
// doLocalSymbolize adds symbol and line number information to all locations |
| 127 |
// in a profile. mode enables some options to control |
| 128 |
// symbolization. |
| 129 |
func doLocalSymbolize(prof *profile.Profile, fast, force bool, obj plugin.ObjTool, ui plugin.UI) error { |
| 130 |
if fast { |
| 131 |
if bu, ok := obj.(*binutils.Binutils); ok { |
| 132 |
bu.SetFastSymbolization(true) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
functions := map[profile.Function]*profile.Function{} |
| 137 |
addFunction := func(f *profile.Function) *profile.Function { |
| 138 |
if fp := functions[*f]; fp != nil { |
| 139 |
return fp |
| 140 |
} |
| 141 |
functions[*f] = f |
| 142 |
f.ID = uint64(len(prof.Function)) + 1 |
| 143 |
prof.Function = append(prof.Function, f) |
| 144 |
return f |
| 145 |
} |
| 146 |
|
| 147 |
missingBinaries := false |
| 148 |
mappingLocs := map[*profile.Mapping][]*profile.Location{} |
| 149 |
for _, l := range prof.Location { |
| 150 |
mappingLocs[l.Mapping] = append(mappingLocs[l.Mapping], l) |
| 151 |
} |
| 152 |
for midx, m := range prof.Mapping { |
| 153 |
locs := mappingLocs[m] |
| 154 |
if len(locs) == 0 { |
| 155 |
// The mapping is dangling and has no locations pointing to it. |
| 156 |
continue |
| 157 |
} |
| 158 |
// Do not attempt to re-symbolize a mapping that has already been symbolized. |
| 159 |
if !force && (m.HasFunctions || m.HasFilenames || m.HasLineNumbers) { |
| 160 |
continue |
| 161 |
} |
| 162 |
if m.File == "" { |
| 163 |
if midx == 0 { |
| 164 |
ui.PrintErr("Main binary filename not available.") |
| 165 |
continue |
| 166 |
} |
| 167 |
missingBinaries = true |
| 168 |
continue |
| 169 |
} |
| 170 |
if m.Unsymbolizable() { |
| 171 |
// Skip well-known system mappings |
| 172 |
continue |
| 173 |
} |
| 174 |
if m.BuildID == "" { |
| 175 |
if u, err := url.Parse(m.File); err == nil && u.IsAbs() && strings.Contains(strings.ToLower(u.Scheme), "http") { |
| 176 |
// Skip mappings pointing to a source URL |
| 177 |
continue |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
name := filepath.Base(m.File) |
| 182 |
if m.BuildID != "" { |
| 183 |
name += fmt.Sprintf(" (build ID %s)", m.BuildID) |
| 184 |
} |
| 185 |
f, err := obj.Open(m.File, m.Start, m.Limit, m.Offset, m.KernelRelocationSymbol) |
| 186 |
if err != nil { |
| 187 |
ui.PrintErr("Local symbolization failed for ", name, ": ", err) |
| 188 |
missingBinaries = true |
| 189 |
continue |
| 190 |
} |
| 191 |
if fid := f.BuildID(); m.BuildID != "" && fid != "" && fid != m.BuildID { |
| 192 |
ui.PrintErr("Local symbolization failed for ", name, ": build ID mismatch") |
| 193 |
f.Close() |
| 194 |
continue |
| 195 |
} |
| 196 |
symbolizeOneMapping(m, locs, f, addFunction) |
| 197 |
f.Close() |
| 198 |
} |
| 199 |
|
| 200 |
if missingBinaries { |
| 201 |
ui.PrintErr("Some binary filenames not available. Symbolization may be incomplete.\n" + |
| 202 |
"Try setting PPROF_BINARY_PATH to the search path for local binaries.") |
| 203 |
} |
| 204 |
return nil |
| 205 |
} |
| 206 |
|
| 207 |
func symbolizeOneMapping(m *profile.Mapping, locs []*profile.Location, obj plugin.ObjFile, addFunction func(*profile.Function) *profile.Function) { |
| 208 |
for _, l := range locs { |
| 209 |
stack, err := obj.SourceLine(l.Address) |
| 210 |
if err != nil || len(stack) == 0 { |
| 211 |
// No answers from addr2line. |
| 212 |
continue |
| 213 |
} |
| 214 |
|
| 215 |
l.Line = make([]profile.Line, len(stack)) |
| 216 |
l.IsFolded = false |
| 217 |
for i, frame := range stack { |
| 218 |
if frame.Func != "" { |
| 219 |
m.HasFunctions = true |
| 220 |
} |
| 221 |
if frame.File != "" { |
| 222 |
m.HasFilenames = true |
| 223 |
} |
| 224 |
if frame.Line != 0 { |
| 225 |
m.HasLineNumbers = true |
| 226 |
} |
| 227 |
f := addFunction(&profile.Function{ |
| 228 |
Name: frame.Func, |
| 229 |
SystemName: frame.Func, |
| 230 |
Filename: frame.File, |
| 231 |
StartLine: int64(frame.StartLine), |
| 232 |
}) |
| 233 |
l.Line[i] = profile.Line{ |
| 234 |
Function: f, |
| 235 |
Line: int64(frame.Line), |
| 236 |
Column: int64(frame.Column), |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if len(stack) > 0 { |
| 241 |
m.HasInlineFrames = true |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// Demangle updates the function names in a profile with demangled C++ |
| 247 |
// names, simplified according to demanglerMode. If force is set, |
| 248 |
// overwrite any names that appear already demangled. |
| 249 |
func Demangle(prof *profile.Profile, force bool, demanglerMode string) { |
| 250 |
if force { |
| 251 |
// Remove the current demangled names to force demangling |
| 252 |
for _, f := range prof.Function { |
| 253 |
if f.Name != "" && f.SystemName != "" { |
| 254 |
f.Name = f.SystemName |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
options := demanglerModeToOptions(demanglerMode) |
| 260 |
// Bail out fast to avoid any parsing, if we really don't want any demangling. |
| 261 |
if len(options) == 0 { |
| 262 |
return |
| 263 |
} |
| 264 |
for _, fn := range prof.Function { |
| 265 |
demangleSingleFunction(fn, options) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func demanglerModeToOptions(demanglerMode string) []demangle.Option { |
| 270 |
switch demanglerMode { |
| 271 |
case "": // demangled, simplified: no parameters, no templates, no return type |
| 272 |
return []demangle.Option{demangle.NoParams, demangle.NoEnclosingParams, demangle.NoTemplateParams} |
| 273 |
case "templates": // demangled, simplified: no parameters, no return type |
| 274 |
return []demangle.Option{demangle.NoParams, demangle.NoEnclosingParams} |
| 275 |
case "full": |
| 276 |
return []demangle.Option{demangle.NoClones} |
| 277 |
case "none": // no demangling |
| 278 |
return []demangle.Option{} |
| 279 |
} |
| 280 |
|
| 281 |
panic(fmt.Sprintf("unknown demanglerMode %s", demanglerMode)) |
| 282 |
} |
| 283 |
|
| 284 |
func demangleSingleFunction(fn *profile.Function, opts []demangle.Option) { |
| 285 |
if fn.Name != "" && fn.SystemName != fn.Name { |
| 286 |
return // Already demangled. |
| 287 |
} |
| 288 |
if demangled := demangle.Filter(fn.SystemName, opts...); demangled != fn.SystemName { |
| 289 |
fn.Name = demangled |
| 290 |
return |
| 291 |
} |
| 292 |
|
| 293 |
// OSX has all the symbols prefixed with extra '_' so lets try |
| 294 |
// once more without it |
| 295 |
if strings.HasPrefix(fn.SystemName, "_") { |
| 296 |
if demangled := demangle.Filter(fn.SystemName[1:], opts...); demangled != fn.SystemName[1:] { |
| 297 |
fn.Name = demangled |
| 298 |
return |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// Could not demangle. Apply heuristics in case the name is |
| 303 |
// already demangled. |
| 304 |
name := fn.SystemName |
| 305 |
if looksLikeDemangledCPlusPlus(name) { |
| 306 |
for _, o := range opts { |
| 307 |
switch o { |
| 308 |
case demangle.NoParams: |
| 309 |
name = removeMatching(name, '(', ')') |
| 310 |
case demangle.NoTemplateParams: |
| 311 |
name = removeMatching(name, '<', '>') |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
fn.Name = name |
| 316 |
} |
| 317 |
|
| 318 |
// looksLikeDemangledCPlusPlus is a heuristic to decide if a name is |
| 319 |
// the result of demangling C++. If so, further heuristics will be |
| 320 |
// applied to simplify the name. |
| 321 |
func looksLikeDemangledCPlusPlus(demangled string) bool { |
| 322 |
// Skip java names of the form "class.<init>". |
| 323 |
if strings.Contains(demangled, ".<") { |
| 324 |
return false |
| 325 |
} |
| 326 |
// Skip Go names of the form "foo.(*Bar[...]).Method". |
| 327 |
if strings.Contains(demangled, "]).") { |
| 328 |
return false |
| 329 |
} |
| 330 |
return strings.ContainsAny(demangled, "<>[]") || strings.Contains(demangled, "::") |
| 331 |
} |
| 332 |
|
| 333 |
// removeMatching removes nested instances of start..end from name. |
| 334 |
func removeMatching(name string, start, end byte) string { |
| 335 |
s := string(start) + string(end) |
| 336 |
var nesting, first, current int |
| 337 |
for index := strings.IndexAny(name[current:], s); index != -1; index = strings.IndexAny(name[current:], s) { |
| 338 |
switch current += index; name[current] { |
| 339 |
case start: |
| 340 |
nesting++ |
| 341 |
if nesting == 1 { |
| 342 |
first = current |
| 343 |
} |
| 344 |
case end: |
| 345 |
nesting-- |
| 346 |
switch { |
| 347 |
case nesting < 0: |
| 348 |
return name // Mismatch, abort |
| 349 |
case nesting == 0: |
| 350 |
name = name[:first] + name[current+1:] |
| 351 |
current = first - 1 |
| 352 |
} |
| 353 |
} |
| 354 |
current++ |
| 355 |
} |
| 356 |
return name |
| 357 |
} |