| 1 |
// Copyright 2015 Marc-Antoine Ruel. All rights reserved. |
| 2 |
// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 |
// that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package stack analyzes stack dump of Go processes and simplifies it. |
| 6 |
// |
| 7 |
// It is mostly useful on servers will large number of identical goroutines, |
| 8 |
// making the crash dump harder to read than strictly necessary. |
| 9 |
package stack |
| 10 |
|
| 11 |
import ( |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"net/url" |
| 15 |
"os" |
| 16 |
"sort" |
| 17 |
"strings" |
| 18 |
"unicode" |
| 19 |
"unicode/utf8" |
| 20 |
) |
| 21 |
|
| 22 |
// Func is a function call in a goroutine stack trace. |
| 23 |
type Func struct { |
| 24 |
// Complete is the complete reference. It can be ambiguous in case where a |
| 25 |
// path contains dots. |
| 26 |
Complete string |
| 27 |
// ImportPath is the directory name for this function reference, or "main" if |
| 28 |
// it was in package main. The package name may not match. |
| 29 |
ImportPath string |
| 30 |
// DirName is the directory name containing the package in which the function |
| 31 |
// is. Normally this matches the package name, but sometimes there's smartass |
| 32 |
// folks that use a different directory name than the package name. |
| 33 |
DirName string |
| 34 |
// Name is the function name or fully quality method name. |
| 35 |
Name string |
| 36 |
// IsExported is true if the function is exported. |
| 37 |
IsExported bool |
| 38 |
// IsPkgMain is true if it is in the main package. |
| 39 |
IsPkgMain bool |
| 40 |
|
| 41 |
// Disallow initialization with unnamed parameters. |
| 42 |
_ struct{} |
| 43 |
} |
| 44 |
|
| 45 |
// Init parses the raw function call line from a goroutine stack trace. |
| 46 |
// |
| 47 |
// Go stack traces print a mangled function call, this wrapper unmangle the |
| 48 |
// string before printing and adds other filtering methods. |
| 49 |
// |
| 50 |
// The main caveat is that for calls in package main, the package import URL is |
| 51 |
// left out. |
| 52 |
func (f *Func) Init(raw string) error { |
| 53 |
// Format can be: |
| 54 |
// - gopkg.in/yaml%2ev2.(*Struct).Method (handling dots is tricky) |
| 55 |
// - main.funcĀ·001 (go statements) |
| 56 |
// - foo (C code) |
| 57 |
// |
| 58 |
// The function is optimized to reduce its memory usage. |
| 59 |
endPkg := 0 |
| 60 |
if lastSlash := strings.LastIndexByte(raw, '/'); lastSlash != -1 { |
| 61 |
// Cut the path elements. |
| 62 |
r := strings.IndexByte(raw[lastSlash+1:], '.') |
| 63 |
if r == -1 { |
| 64 |
return errors.New("bad function reference: expected to have at least one dot") |
| 65 |
} |
| 66 |
endPkg = lastSlash + r + 1 |
| 67 |
} else { |
| 68 |
// It's fine if there's no dot, it happens in C code in go1.4 and lower. |
| 69 |
endPkg = strings.IndexByte(raw, '.') |
| 70 |
} |
| 71 |
// Only the path part is escaped. |
| 72 |
var err error |
| 73 |
if f.Complete, err = url.QueryUnescape(raw); err != nil { |
| 74 |
return fmt.Errorf("bad function reference: %w", err) |
| 75 |
} |
| 76 |
// Update the index in the unescaped string. |
| 77 |
endPkg += len(f.Complete) - len(raw) |
| 78 |
if endPkg != -1 { |
| 79 |
f.ImportPath = f.Complete[:endPkg] |
| 80 |
} |
| 81 |
f.Name = f.Complete[endPkg+1:] |
| 82 |
if idx := strings.LastIndexByte(f.Name, ' '); idx > -1 { |
| 83 |
cut := f.Name[:idx] |
| 84 |
// TODO(go1.20): switch to strings.CutSuffix |
| 85 |
const inGoroutineSuffix = " in goroutine" |
| 86 |
if strings.HasSuffix(cut, inGoroutineSuffix) { |
| 87 |
f.Name = strings.TrimSuffix(cut, inGoroutineSuffix) |
| 88 |
} |
| 89 |
} |
| 90 |
f.DirName = f.ImportPath |
| 91 |
if i := strings.LastIndexByte(f.DirName, '/'); i != -1 { |
| 92 |
f.DirName = f.DirName[i+1:] |
| 93 |
} |
| 94 |
if f.ImportPath == "main" { |
| 95 |
f.IsPkgMain = true |
| 96 |
// Consider main.main to be exported. |
| 97 |
if f.Name == "main" { |
| 98 |
f.IsExported = true |
| 99 |
} |
| 100 |
} else { |
| 101 |
parts := strings.Split(f.Name, ".") |
| 102 |
r, _ := utf8.DecodeRuneInString(parts[len(parts)-1]) |
| 103 |
f.IsExported = unicode.ToUpper(r) == r |
| 104 |
} |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
// String returns Complete. |
| 109 |
func (f *Func) String() string { |
| 110 |
return f.Complete |
| 111 |
} |
| 112 |
|
| 113 |
// Arg is an argument on a Call. |
| 114 |
type Arg struct { |
| 115 |
// IsAggregate is true if the argument is an aggregate type. If true, the |
| 116 |
// argument does not contain a value itself, but contains a set of nested |
| 117 |
// argument fields. If false, the argument contains a single scalar value. |
| 118 |
IsAggregate bool |
| 119 |
|
| 120 |
// The following are set if IsAggregate == false. |
| 121 |
|
| 122 |
// Name is a pseudo name given to the argument. |
| 123 |
Name string |
| 124 |
// Value is the raw value as found in the stack trace |
| 125 |
Value uint64 |
| 126 |
// IsPtr is true if we guess it's a pointer. It's only a guess, it can be |
| 127 |
// easily confused by a bitmask. |
| 128 |
IsPtr bool |
| 129 |
// IsOffsetTooLarge is true if the argument's frame offset was too large, |
| 130 |
// preventing the argument from being printed in the stack trace. |
| 131 |
IsOffsetTooLarge bool |
| 132 |
|
| 133 |
// IsInaccurate determines if Value is inaccurate. Stacks could have inaccurate values |
| 134 |
// for arguments passed in registers. Go 1.18 prints a ? for these values. |
| 135 |
IsInaccurate bool |
| 136 |
|
| 137 |
// The following are set if IsAggregate == true. |
| 138 |
|
| 139 |
// Fields are the fields/elements of aggregate-typed arguments. |
| 140 |
Fields Args |
| 141 |
|
| 142 |
// Disallow initialization with unnamed parameters. |
| 143 |
_ struct{} |
| 144 |
} |
| 145 |
|
| 146 |
const zeroToNine = "0123456789" |
| 147 |
|
| 148 |
// String prints the argument as the name if present, otherwise as the value. |
| 149 |
func (a *Arg) String() string { |
| 150 |
if a.Name != "" { |
| 151 |
return a.Name |
| 152 |
} |
| 153 |
if a.IsOffsetTooLarge { |
| 154 |
return "_" |
| 155 |
} |
| 156 |
if a.IsAggregate { |
| 157 |
return "{" + a.Fields.String() + "}" |
| 158 |
} |
| 159 |
if a.Value < uint64(len(zeroToNine)) { |
| 160 |
return zeroToNine[a.Value : a.Value+1] |
| 161 |
} |
| 162 |
return fmt.Sprintf("0x%x", a.Value) |
| 163 |
} |
| 164 |
|
| 165 |
const ( |
| 166 |
// The pointer floor can be below 1MiB (!) on Windows. |
| 167 |
// Assumes all values are above 512KiB and positive are pointers; assuming |
| 168 |
// that above half the memory is kernel memory. |
| 169 |
// |
| 170 |
// This is not always true but this should be good enough to help |
| 171 |
// implementing AnyPointer. |
| 172 |
pointerFloor = 512 * 1024 |
| 173 |
// Assume the stack was generated with the same bitness (32 vs 64) than the |
| 174 |
// code processing it. |
| 175 |
pointerCeiling = uint64((^uint(0)) >> 1) |
| 176 |
) |
| 177 |
|
| 178 |
// equal returns true only if both arguments are exactly equal. |
| 179 |
func (a *Arg) equal(r *Arg) bool { |
| 180 |
return a.similar(r, ExactFlags) |
| 181 |
} |
| 182 |
|
| 183 |
// similar returns true if the two Arg are equal or almost but not quite equal. |
| 184 |
func (a *Arg) similar(r *Arg, similar Similarity) bool { |
| 185 |
if a.IsAggregate != r.IsAggregate { |
| 186 |
return false |
| 187 |
} |
| 188 |
if a.IsAggregate { |
| 189 |
return a.Fields.similar(&r.Fields, similar) |
| 190 |
} |
| 191 |
switch similar { |
| 192 |
case ExactFlags, ExactLines: |
| 193 |
if a.Name != r.Name { |
| 194 |
return false |
| 195 |
} |
| 196 |
if a.IsOffsetTooLarge != r.IsOffsetTooLarge { |
| 197 |
return false |
| 198 |
} |
| 199 |
if a.IsPtr != r.IsPtr { |
| 200 |
return false |
| 201 |
} |
| 202 |
return a.Value == r.Value |
| 203 |
case AnyValue: |
| 204 |
return true |
| 205 |
case AnyPointer: |
| 206 |
if a.IsOffsetTooLarge != r.IsOffsetTooLarge { |
| 207 |
return false |
| 208 |
} |
| 209 |
if a.IsPtr != r.IsPtr { |
| 210 |
return false |
| 211 |
} |
| 212 |
return a.IsPtr || a.Value == r.Value |
| 213 |
default: |
| 214 |
return false |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Args is a series of function call arguments. |
| 219 |
type Args struct { |
| 220 |
// Values is the arguments as shown on the stack trace. They are mangled via |
| 221 |
// simplification. |
| 222 |
Values []Arg |
| 223 |
// Processed is the arguments generated from processing the source files. It |
| 224 |
// can have a length lower than Values. |
| 225 |
Processed []string |
| 226 |
// Elided when set means there was a trailing ", ...". |
| 227 |
Elided bool |
| 228 |
|
| 229 |
// Disallow initialization with unnamed parameters. |
| 230 |
_ struct{} |
| 231 |
} |
| 232 |
|
| 233 |
func (a *Args) String() string { |
| 234 |
var v []string |
| 235 |
if len(a.Processed) != 0 { |
| 236 |
v = a.Processed |
| 237 |
} else { |
| 238 |
v = make([]string, 0, len(a.Values)) |
| 239 |
for _, item := range a.Values { |
| 240 |
v = append(v, item.String()) |
| 241 |
} |
| 242 |
} |
| 243 |
if a.Elided { |
| 244 |
v = append(v, "...") |
| 245 |
} |
| 246 |
return strings.Join(v, ", ") |
| 247 |
} |
| 248 |
|
| 249 |
// equal returns true only if both arguments are exactly equal. |
| 250 |
func (a *Args) equal(r *Args) bool { |
| 251 |
if a.Elided != r.Elided || len(a.Values) != len(r.Values) { |
| 252 |
return false |
| 253 |
} |
| 254 |
for i, l := range a.Values { |
| 255 |
if !l.equal(&r.Values[i]) { |
| 256 |
return false |
| 257 |
} |
| 258 |
} |
| 259 |
return true |
| 260 |
} |
| 261 |
|
| 262 |
// similar returns true if the two Args are equal or almost but not quite |
| 263 |
// equal. |
| 264 |
func (a *Args) similar(r *Args, similar Similarity) bool { |
| 265 |
if a.Elided != r.Elided || len(a.Values) != len(r.Values) { |
| 266 |
return false |
| 267 |
} |
| 268 |
for i, l := range a.Values { |
| 269 |
if !l.similar(&r.Values[i], similar) { |
| 270 |
return false |
| 271 |
} |
| 272 |
} |
| 273 |
return true |
| 274 |
} |
| 275 |
|
| 276 |
// merge merges two similar Args, zapping out differences. |
| 277 |
func (a *Args) merge(r *Args) Args { |
| 278 |
out := Args{ |
| 279 |
Values: make([]Arg, len(a.Values)), |
| 280 |
Elided: a.Elided, |
| 281 |
} |
| 282 |
for i, l := range a.Values { |
| 283 |
rv := &r.Values[i] |
| 284 |
if l.IsAggregate { |
| 285 |
out.Values[i].IsAggregate = true |
| 286 |
out.Values[i].Fields = l.Fields.merge(&rv.Fields) |
| 287 |
} else if !l.equal(rv) { |
| 288 |
out.Values[i].Name = "*" |
| 289 |
out.Values[i].Value = l.Value |
| 290 |
out.Values[i].IsPtr = l.IsPtr |
| 291 |
} else { |
| 292 |
out.Values[i] = l |
| 293 |
} |
| 294 |
} |
| 295 |
return out |
| 296 |
} |
| 297 |
|
| 298 |
// walk traverses all non-aggregate arguments in the Args struct, calling the |
| 299 |
// provided visitor function with each Arg. |
| 300 |
func (a *Args) walk(visitor func(arg *Arg)) { |
| 301 |
for i := range a.Values { |
| 302 |
arg := &a.Values[i] |
| 303 |
if arg.IsAggregate { |
| 304 |
arg.Fields.walk(visitor) |
| 305 |
} else { |
| 306 |
visitor(arg) |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
// Location is the source location, if determined. |
| 312 |
type Location int |
| 313 |
|
| 314 |
const ( |
| 315 |
// LocationUnknown is the default value when Opts.GuessPaths was false. |
| 316 |
LocationUnknown Location = iota |
| 317 |
// GoMod is a go module, it is outside $GOPATH and is inside a directory |
| 318 |
// containing a go.mod file. This is considered a local copy. |
| 319 |
GoMod |
| 320 |
// GOPATH is in $GOPATH/src. This is either a dependency fetched via |
| 321 |
// GO111MODULE=off or intentionally fetched this way. There is no guaranteed |
| 322 |
// that the local copy is pristine. |
| 323 |
GOPATH |
| 324 |
// GoPkg is in $GOPATH/pkg/mod. This is a dependency fetched via go module. |
| 325 |
// It is considered to be an unmodified external dependency. |
| 326 |
GoPkg |
| 327 |
// Stdlib is when it is a Go standard library function. This includes the 'go |
| 328 |
// test' generated main executable. |
| 329 |
Stdlib |
| 330 |
|
| 331 |
lastLocation |
| 332 |
) |
| 333 |
|
| 334 |
// Call is an item in the stack trace. |
| 335 |
// |
| 336 |
// All paths in this struct are in POSIX format, using "/" as path separator. |
| 337 |
type Call struct { |
| 338 |
// The following are initialized on the first line of the call stack. |
| 339 |
|
| 340 |
// Func is the fully qualified function name (encoded). |
| 341 |
Func Func |
| 342 |
// Args is the call arguments. |
| 343 |
Args Args |
| 344 |
|
| 345 |
// The following are initialized on the second line of the call stack. |
| 346 |
|
| 347 |
// RemoteSrcPath is the full path name of the source file as seen in the |
| 348 |
// trace. |
| 349 |
RemoteSrcPath string |
| 350 |
// Line is the line number. |
| 351 |
Line int |
| 352 |
// SrcName is the base file name of the source file. |
| 353 |
SrcName string |
| 354 |
// DirSrc is one directory plus the file name of the source file. It is a |
| 355 |
// subset of RemoteSrcPath. |
| 356 |
DirSrc string |
| 357 |
|
| 358 |
// The following are only set if Opts.GuessPaths was set. |
| 359 |
|
| 360 |
// LocalSrcPath is the full path name of the source file as seen in the host, |
| 361 |
// if found. |
| 362 |
LocalSrcPath string |
| 363 |
// RelSrcPath is the relative path to GOROOT, GOPATH or LocalGoMods. |
| 364 |
RelSrcPath string |
| 365 |
// ImportPath is the fully qualified import path as found on disk (when |
| 366 |
// Opts.GuessPaths was set). Defaults to Func.ImportPath otherwise. |
| 367 |
// |
| 368 |
// In the case of package "main", it returns the underlying path to the main |
| 369 |
// package instead of "main" if Opts.GuessPaths was set. |
| 370 |
ImportPath string |
| 371 |
// Location is the source location, if determined. |
| 372 |
Location Location |
| 373 |
|
| 374 |
// Disallow initialization with unnamed parameters. |
| 375 |
_ struct{} |
| 376 |
} |
| 377 |
|
| 378 |
// Init initializes RemoteSrcPath, SrcName, DirName and Line. |
| 379 |
// |
| 380 |
// For test main, it initializes Location only with Stdlib. |
| 381 |
// |
| 382 |
// It does its best educated guess for ImportPath. |
| 383 |
func (c *Call) init(srcPath string, line int) { |
| 384 |
c.Line = line |
| 385 |
if srcPath != "" { |
| 386 |
c.RemoteSrcPath = srcPath |
| 387 |
if i := strings.LastIndexByte(c.RemoteSrcPath, '/'); i != -1 { |
| 388 |
c.SrcName = c.RemoteSrcPath[i+1:] |
| 389 |
if i = strings.LastIndexByte(c.RemoteSrcPath[:i], '/'); i != -1 { |
| 390 |
c.DirSrc = c.RemoteSrcPath[i+1:] |
| 391 |
} |
| 392 |
} |
| 393 |
if c.DirSrc == testMainSrc { |
| 394 |
// Consider _test/_testmain.go as stdlib since it's injected by "go test". |
| 395 |
c.Location = Stdlib |
| 396 |
} |
| 397 |
} |
| 398 |
c.ImportPath = c.Func.ImportPath |
| 399 |
} |
| 400 |
|
| 401 |
const testMainSrc = "_test" + string(os.PathSeparator) + "_testmain.go" |
| 402 |
|
| 403 |
// updateLocations initializes LocalSrcPath, RelSrcPath, Location and ImportPath. |
| 404 |
// |
| 405 |
// goroot, localgoroot, localgomod, gomodImportPath and gopaths are expected to |
| 406 |
// be in "/" format even on Windows. They must not have a trailing "/". |
| 407 |
// |
| 408 |
// Returns true if a match was found. |
| 409 |
func (c *Call) updateLocations(goroot, localgoroot string, localgomods, gopaths map[string]string) bool { |
| 410 |
// TODO(maruel): Reduce memory allocations. |
| 411 |
if c.RemoteSrcPath == "" { |
| 412 |
return false |
| 413 |
} |
| 414 |
// Check GOROOT first. |
| 415 |
if goroot != "" { |
| 416 |
if prefix := goroot + "/src/"; strings.HasPrefix(c.RemoteSrcPath, prefix) { |
| 417 |
// Replace remote GOROOT with local GOROOT. |
| 418 |
c.RelSrcPath = c.RemoteSrcPath[len(prefix):] |
| 419 |
c.LocalSrcPath = pathJoin(localgoroot, "src", c.RelSrcPath) |
| 420 |
if i := strings.LastIndexByte(c.RelSrcPath, '/'); i != -1 { |
| 421 |
c.ImportPath = c.RelSrcPath[:i] |
| 422 |
} |
| 423 |
if c.Location == LocationUnknown { |
| 424 |
c.Location = Stdlib |
| 425 |
} |
| 426 |
return true |
| 427 |
} |
| 428 |
} |
| 429 |
// Check GOPATH. |
| 430 |
// TODO(maruel): Sort for deterministic behavior? |
| 431 |
for prefix, dest := range gopaths { |
| 432 |
if p := prefix + "/src/"; strings.HasPrefix(c.RemoteSrcPath, p) { |
| 433 |
c.RelSrcPath = c.RemoteSrcPath[len(p):] |
| 434 |
c.LocalSrcPath = pathJoin(dest, "src", c.RelSrcPath) |
| 435 |
if i := strings.LastIndexByte(c.RelSrcPath, '/'); i != -1 { |
| 436 |
c.ImportPath = c.RelSrcPath[:i] |
| 437 |
} |
| 438 |
if c.Location == LocationUnknown { |
| 439 |
c.Location = GOPATH |
| 440 |
} |
| 441 |
return true |
| 442 |
} |
| 443 |
// For modules, the path has to be altered, as it contains the version. |
| 444 |
if p := prefix + "/pkg/mod/"; strings.HasPrefix(c.RemoteSrcPath, p) { |
| 445 |
c.RelSrcPath = c.RemoteSrcPath[len(p):] |
| 446 |
c.LocalSrcPath = pathJoin(dest, "pkg/mod", c.RelSrcPath) |
| 447 |
if i := strings.LastIndexByte(c.RelSrcPath, '/'); i != -1 { |
| 448 |
c.ImportPath = c.RelSrcPath[:i] |
| 449 |
} |
| 450 |
if c.Location == LocationUnknown { |
| 451 |
c.Location = GoPkg |
| 452 |
} |
| 453 |
return true |
| 454 |
} |
| 455 |
} |
| 456 |
// Check Go modules. |
| 457 |
// Go module path detection only works with stack traces created on the local |
| 458 |
// file system. |
| 459 |
for prefix, pkg := range localgomods { |
| 460 |
if strings.HasPrefix(c.RemoteSrcPath, prefix+"/") { |
| 461 |
c.RelSrcPath = c.RemoteSrcPath[len(prefix)+1:] |
| 462 |
c.LocalSrcPath = c.RemoteSrcPath |
| 463 |
if i := strings.LastIndexByte(c.RelSrcPath, '/'); i != -1 { |
| 464 |
c.ImportPath = pkg + "/" + c.RelSrcPath[:i] |
| 465 |
} else { |
| 466 |
c.ImportPath = pkg |
| 467 |
} |
| 468 |
if c.Location == LocationUnknown { |
| 469 |
c.Location = GoMod |
| 470 |
} |
| 471 |
return true |
| 472 |
} |
| 473 |
} |
| 474 |
// Maybe the path is just absolute and exists? |
| 475 |
return false |
| 476 |
} |
| 477 |
|
| 478 |
// equal returns true only if both calls are exactly equal. |
| 479 |
func (c *Call) equal(r *Call) bool { |
| 480 |
return c.Line == r.Line && c.Func.Complete == r.Func.Complete && c.RemoteSrcPath == r.RemoteSrcPath && c.Args.equal(&r.Args) |
| 481 |
} |
| 482 |
|
| 483 |
// similar returns true if the two Call are equal or almost but not quite |
| 484 |
// equal. |
| 485 |
func (c *Call) similar(r *Call, similar Similarity) bool { |
| 486 |
return c.Line == r.Line && c.Func.Complete == r.Func.Complete && c.RemoteSrcPath == r.RemoteSrcPath && c.Args.similar(&r.Args, similar) |
| 487 |
} |
| 488 |
|
| 489 |
// merge merges two similar Call, zapping out differences. |
| 490 |
func (c *Call) merge(r *Call) Call { |
| 491 |
return Call{ |
| 492 |
Func: c.Func, |
| 493 |
Args: c.Args.merge(&r.Args), |
| 494 |
RemoteSrcPath: c.RemoteSrcPath, |
| 495 |
Line: c.Line, |
| 496 |
SrcName: c.SrcName, |
| 497 |
DirSrc: c.DirSrc, |
| 498 |
LocalSrcPath: c.LocalSrcPath, |
| 499 |
RelSrcPath: c.RelSrcPath, |
| 500 |
ImportPath: c.ImportPath, |
| 501 |
Location: c.Location, |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
// Stack is a call stack. |
| 506 |
type Stack struct { |
| 507 |
// Calls is the call stack. First is original function, last is leaf |
| 508 |
// function. |
| 509 |
Calls []Call |
| 510 |
// Elided is set when there's >100 items in Stack, currently hardcoded in |
| 511 |
// package runtime. |
| 512 |
Elided bool |
| 513 |
|
| 514 |
// Disallow initialization with unnamed parameters. |
| 515 |
_ struct{} |
| 516 |
} |
| 517 |
|
| 518 |
// equal returns true on if both call stacks are exactly equal. |
| 519 |
func (s *Stack) equal(r *Stack) bool { |
| 520 |
if len(s.Calls) != len(r.Calls) || s.Elided != r.Elided { |
| 521 |
return false |
| 522 |
} |
| 523 |
for i := range s.Calls { |
| 524 |
if !s.Calls[i].equal(&r.Calls[i]) { |
| 525 |
return false |
| 526 |
} |
| 527 |
} |
| 528 |
return true |
| 529 |
} |
| 530 |
|
| 531 |
// similar returns true if the two Stack are equal or almost but not quite |
| 532 |
// equal. |
| 533 |
func (s *Stack) similar(r *Stack, similar Similarity) bool { |
| 534 |
if len(s.Calls) != len(r.Calls) || s.Elided != r.Elided { |
| 535 |
return false |
| 536 |
} |
| 537 |
for i := range s.Calls { |
| 538 |
if !s.Calls[i].similar(&r.Calls[i], similar) { |
| 539 |
return false |
| 540 |
} |
| 541 |
} |
| 542 |
return true |
| 543 |
} |
| 544 |
|
| 545 |
// merge merges two similar Stack, zapping out differences. |
| 546 |
func (s *Stack) merge(r *Stack) *Stack { |
| 547 |
// Assumes similar stacks have the same length. |
| 548 |
out := &Stack{ |
| 549 |
Calls: make([]Call, len(s.Calls)), |
| 550 |
Elided: s.Elided, |
| 551 |
} |
| 552 |
for i := range s.Calls { |
| 553 |
out.Calls[i] = s.Calls[i].merge(&r.Calls[i]) |
| 554 |
} |
| 555 |
return out |
| 556 |
} |
| 557 |
|
| 558 |
// less compares two Stack, where the ones that are less are more |
| 559 |
// important, so they come up front. |
| 560 |
// |
| 561 |
// A Stack with more private functions is 'less' so it is at the top. |
| 562 |
// Inversely, a Stack with only public functions is 'more' so it is at the |
| 563 |
// bottom. |
| 564 |
func (s *Stack) less(r *Stack) bool { |
| 565 |
lLoc := [lastLocation]int{} |
| 566 |
rLoc := [lastLocation]int{} |
| 567 |
lMain := 0 |
| 568 |
rMain := 0 |
| 569 |
for _, c := range s.Calls { |
| 570 |
lLoc[c.Location]++ |
| 571 |
if c.Func.IsPkgMain { |
| 572 |
lMain++ |
| 573 |
} |
| 574 |
} |
| 575 |
for _, s := range r.Calls { |
| 576 |
rLoc[s.Location]++ |
| 577 |
if s.Func.IsPkgMain { |
| 578 |
rMain++ |
| 579 |
} |
| 580 |
} |
| 581 |
if lMain > rMain { |
| 582 |
return true |
| 583 |
} |
| 584 |
if lMain < rMain { |
| 585 |
return false |
| 586 |
} |
| 587 |
for i := 1; i < int(lastLocation); i++ { |
| 588 |
if lLoc[i] > rLoc[i] { |
| 589 |
return true |
| 590 |
} |
| 591 |
if lLoc[i] < rLoc[i] { |
| 592 |
return false |
| 593 |
} |
| 594 |
} |
| 595 |
// Check unknown code type last. |
| 596 |
if lLoc[LocationUnknown] > rLoc[LocationUnknown] { |
| 597 |
return true |
| 598 |
} |
| 599 |
if lLoc[LocationUnknown] < rLoc[LocationUnknown] { |
| 600 |
return false |
| 601 |
} |
| 602 |
|
| 603 |
// Stack lengths are the same and they are mostly of the same kind of location. |
| 604 |
for x := range s.Calls { |
| 605 |
if s.Calls[x].Func.Complete < r.Calls[x].Func.Complete { |
| 606 |
return true |
| 607 |
} |
| 608 |
if s.Calls[x].Func.Complete > r.Calls[x].Func.Complete { |
| 609 |
return false |
| 610 |
} |
| 611 |
if s.Calls[x].DirSrc < r.Calls[x].DirSrc { |
| 612 |
return true |
| 613 |
} |
| 614 |
if s.Calls[x].DirSrc > r.Calls[x].DirSrc { |
| 615 |
return false |
| 616 |
} |
| 617 |
if s.Calls[x].Line < r.Calls[x].Line { |
| 618 |
return true |
| 619 |
} |
| 620 |
if s.Calls[x].Line > r.Calls[x].Line { |
| 621 |
return false |
| 622 |
} |
| 623 |
} |
| 624 |
// Stacks are the same. |
| 625 |
return false |
| 626 |
} |
| 627 |
|
| 628 |
// updateLocations calls updateLocations on each call frame and returns true if |
| 629 |
// they were all resolved. |
| 630 |
func (s *Stack) updateLocations(goroot, localgoroot string, localgomods, gopaths map[string]string) bool { |
| 631 |
// If there were none, it was "resolved". |
| 632 |
r := true |
| 633 |
for i := range s.Calls { |
| 634 |
r = s.Calls[i].updateLocations(goroot, localgoroot, localgomods, gopaths) && r |
| 635 |
} |
| 636 |
return r |
| 637 |
} |
| 638 |
|
| 639 |
// Signature represents the signature of one or multiple goroutines. |
| 640 |
// |
| 641 |
// It is effectively the stack trace plus the goroutine internal bits, like |
| 642 |
// it's state, if it is thread locked, which call site created this goroutine, |
| 643 |
// etc. |
| 644 |
type Signature struct { |
| 645 |
// State is the goroutine state at the time of the snapshot. |
| 646 |
// |
| 647 |
// Use git grep 'gopark(|unlock)\(' to find them all plus everything listed |
| 648 |
// in runtime/traceback.go. Valid values includes: |
| 649 |
// - chan send, chan receive, select |
| 650 |
// - finalizer wait, mark wait (idle), |
| 651 |
// - Concurrent GC wait, GC sweep wait, force gc (idle) |
| 652 |
// - IO wait, panicwait |
| 653 |
// - semacquire, semarelease |
| 654 |
// - sleep, timer goroutine (idle) |
| 655 |
// - trace reader (blocked) |
| 656 |
// Stuck cases: |
| 657 |
// - chan send (nil chan), chan receive (nil chan), select (no cases) |
| 658 |
// Runnable states: |
| 659 |
// - idle, runnable, running, syscall, waiting, dead, enqueue, copystack, |
| 660 |
// Scan states: |
| 661 |
// - scan, scanrunnable, scanrunning, scansyscall, scanwaiting, scandead, |
| 662 |
// scanenqueue |
| 663 |
// |
| 664 |
// When running under the race detector, the values are 'running' or |
| 665 |
// 'finished'. |
| 666 |
State string |
| 667 |
// CreatedBy is the call stack that created this goroutine, if applicable. |
| 668 |
// |
| 669 |
// Normally, the stack is a single Call. |
| 670 |
// |
| 671 |
// When the race detector is enabled, a full stack snapshot is available. |
| 672 |
CreatedBy Stack |
| 673 |
// SleepMin is the wait time in minutes, if applicable. |
| 674 |
// |
| 675 |
// Not set when running under the race detector. |
| 676 |
SleepMin int |
| 677 |
// SleepMax is the wait time in minutes, if applicable. |
| 678 |
// |
| 679 |
// Not set when running under the race detector. |
| 680 |
SleepMax int |
| 681 |
// Stack is the call stack. |
| 682 |
Stack Stack |
| 683 |
// Locked is set if the goroutine was locked to an OS thread. |
| 684 |
// |
| 685 |
// Not set when running under the race detector. |
| 686 |
Locked bool |
| 687 |
|
| 688 |
// Disallow initialization with unnamed parameters. |
| 689 |
_ struct{} |
| 690 |
} |
| 691 |
|
| 692 |
// equal returns true only if both signatures are exactly equal. |
| 693 |
func (s *Signature) equal(r *Signature) bool { |
| 694 |
if s.State != r.State || !s.CreatedBy.equal(&r.CreatedBy) || s.Locked != r.Locked || s.SleepMin != r.SleepMin || s.SleepMax != r.SleepMax { |
| 695 |
return false |
| 696 |
} |
| 697 |
return s.Stack.equal(&r.Stack) |
| 698 |
} |
| 699 |
|
| 700 |
// similar returns true if the two Signature are equal or almost but not quite |
| 701 |
// equal. |
| 702 |
func (s *Signature) similar(r *Signature, similar Similarity) bool { |
| 703 |
if s.State != r.State || !s.CreatedBy.similar(&r.CreatedBy, similar) { |
| 704 |
return false |
| 705 |
} |
| 706 |
if similar == ExactFlags && s.Locked != r.Locked { |
| 707 |
return false |
| 708 |
} |
| 709 |
return s.Stack.similar(&r.Stack, similar) |
| 710 |
} |
| 711 |
|
| 712 |
// merge merges two similar Signature, zapping out differences. |
| 713 |
func (s *Signature) merge(r *Signature) *Signature { |
| 714 |
min := s.SleepMin |
| 715 |
if r.SleepMin < min { |
| 716 |
min = r.SleepMin |
| 717 |
} |
| 718 |
max := s.SleepMax |
| 719 |
if r.SleepMax > max { |
| 720 |
max = r.SleepMax |
| 721 |
} |
| 722 |
return &Signature{ |
| 723 |
State: s.State, // Drop right side. |
| 724 |
CreatedBy: s.CreatedBy, // Drop right side. |
| 725 |
SleepMin: min, |
| 726 |
SleepMax: max, |
| 727 |
Stack: *s.Stack.merge(&r.Stack), |
| 728 |
Locked: s.Locked || r.Locked, // TODO(maruel): This is weirdo. |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
// less compares two Signature, where the ones that are less are more |
| 733 |
// important, so they come up front. A Signature with more private functions is |
| 734 |
// 'less' so it is at the top. Inversely, a Signature with only public |
| 735 |
// functions is 'more' so it is at the bottom. |
| 736 |
func (s *Signature) less(r *Signature) bool { |
| 737 |
if s.Stack.less(&r.Stack) { |
| 738 |
return true |
| 739 |
} |
| 740 |
if r.Stack.less(&s.Stack) { |
| 741 |
return false |
| 742 |
} |
| 743 |
if s.Locked && !r.Locked { |
| 744 |
return true |
| 745 |
} |
| 746 |
if r.Locked && !s.Locked { |
| 747 |
return false |
| 748 |
} |
| 749 |
if s.State < r.State { |
| 750 |
return true |
| 751 |
} |
| 752 |
if s.State > r.State { |
| 753 |
return false |
| 754 |
} |
| 755 |
return false |
| 756 |
} |
| 757 |
|
| 758 |
// SleepString returns a string "N-M minutes" if the goroutine(s) slept for a |
| 759 |
// long time. |
| 760 |
// |
| 761 |
// Returns an empty string otherwise. |
| 762 |
func (s *Signature) SleepString() string { |
| 763 |
if s.SleepMax == 0 { |
| 764 |
return "" |
| 765 |
} |
| 766 |
if s.SleepMin != s.SleepMax { |
| 767 |
return fmt.Sprintf("%d~%d minutes", s.SleepMin, s.SleepMax) |
| 768 |
} |
| 769 |
return fmt.Sprintf("%d minutes", s.SleepMax) |
| 770 |
} |
| 771 |
|
| 772 |
// updateLocations calls updateLocations on both CreatedBy and Stack and |
| 773 |
// returns true if they were both resolved. |
| 774 |
func (s *Signature) updateLocations(goroot, localgoroot string, localgomods, gopaths map[string]string) bool { |
| 775 |
r := s.CreatedBy.updateLocations(goroot, localgoroot, localgomods, gopaths) |
| 776 |
r = s.Stack.updateLocations(goroot, localgoroot, localgomods, gopaths) && r |
| 777 |
return r |
| 778 |
} |
| 779 |
|
| 780 |
// Goroutine represents the state of one goroutine, including the stack trace. |
| 781 |
type Goroutine struct { |
| 782 |
// Signature is the stack trace, internal bits, state, which call site |
| 783 |
// created it, etc. |
| 784 |
Signature |
| 785 |
// ID is the goroutine id. |
| 786 |
ID int |
| 787 |
// First is the goroutine first printed, normally the one that crashed. |
| 788 |
First bool |
| 789 |
|
| 790 |
// RaceWrite is true if a race condition was detected, and this goroutine was |
| 791 |
// race on a write operation, otherwise it was a read. |
| 792 |
RaceWrite bool |
| 793 |
// RaceAddr is set to the address when a data race condition was detected. |
| 794 |
// Otherwise it is 0. |
| 795 |
RaceAddr uint64 |
| 796 |
|
| 797 |
// Disallow initialization with unnamed parameters. |
| 798 |
_ struct{} |
| 799 |
} |
| 800 |
|
| 801 |
// Private stuff. |
| 802 |
|
| 803 |
// nameArguments is a post-processing step where Args are 'named' with numbers. |
| 804 |
func nameArguments(goroutines []*Goroutine) { |
| 805 |
// Set a name for any pointer occurring more than once. |
| 806 |
type object struct { |
| 807 |
args []*Arg |
| 808 |
inPrimary bool |
| 809 |
} |
| 810 |
objects := map[uint64]object{} |
| 811 |
// Enumerate all the arguments. |
| 812 |
primary := true |
| 813 |
visit := func(arg *Arg) { |
| 814 |
if arg.IsPtr { |
| 815 |
objects[arg.Value] = object{ |
| 816 |
args: append(objects[arg.Value].args, arg), |
| 817 |
inPrimary: objects[arg.Value].inPrimary || primary, |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
for i, g := range goroutines { |
| 822 |
primary = i == 0 |
| 823 |
for _, c := range g.Stack.Calls { |
| 824 |
c.Args.walk(visit) |
| 825 |
} |
| 826 |
// CreatedBy.Args is never set. |
| 827 |
} |
| 828 |
order := make(uint64Slice, 0, len(objects)/2) |
| 829 |
for k, obj := range objects { |
| 830 |
if len(obj.args) > 1 && obj.inPrimary { |
| 831 |
order = append(order, k) |
| 832 |
} |
| 833 |
} |
| 834 |
sort.Sort(order) |
| 835 |
nextID := 1 |
| 836 |
for _, k := range order { |
| 837 |
for _, arg := range objects[k].args { |
| 838 |
arg.Name = fmt.Sprintf("#%d", nextID) |
| 839 |
} |
| 840 |
nextID++ |
| 841 |
} |
| 842 |
|
| 843 |
// Now do the rest. This is done so the output is deterministic. |
| 844 |
order = make(uint64Slice, 0, len(objects)) |
| 845 |
for k := range objects { |
| 846 |
order = append(order, k) |
| 847 |
} |
| 848 |
sort.Sort(order) |
| 849 |
for _, k := range order { |
| 850 |
// Process the remaining pointers, they were not referenced by primary |
| 851 |
// thread so will have higher IDs. |
| 852 |
if objects[k].inPrimary { |
| 853 |
continue |
| 854 |
} |
| 855 |
for _, arg := range objects[k].args { |
| 856 |
arg.Name = fmt.Sprintf("#%d", nextID) |
| 857 |
} |
| 858 |
nextID++ |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
func pathJoin(s ...string) string { |
| 863 |
return strings.Join(s, "/") |
| 864 |
} |
| 865 |
|
| 866 |
type uint64Slice []uint64 |
| 867 |
|
| 868 |
func (a uint64Slice) Len() int { return len(a) } |
| 869 |
func (a uint64Slice) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 870 |
func (a uint64Slice) Less(i, j int) bool { return a[i] < a[j] } |