-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow checking for functions anywhere in the stack #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,10 +34,10 @@ const _defaultBufferSize = 64 * 1024 // 64 KiB | |
|
||
// Stack represents a single Goroutine's stack. | ||
type Stack struct { | ||
id int | ||
state string | ||
firstFunction string | ||
fullStack *bytes.Buffer | ||
id int | ||
state string | ||
functions []string | ||
fullStack *bytes.Buffer | ||
} | ||
|
||
// ID returns the goroutine ID. | ||
|
@@ -57,13 +57,21 @@ func (s Stack) Full() string { | |
|
||
// FirstFunction returns the name of the first function on the stack. | ||
func (s Stack) FirstFunction() string { | ||
return s.firstFunction | ||
if len(s.functions) > 0 { | ||
return s.functions[0] | ||
} | ||
return "" | ||
} | ||
|
||
// AllFunctions returns the names of all functions on the stack. | ||
func (s Stack) AllFunctions() []string { | ||
return s.functions | ||
} | ||
|
||
func (s Stack) String() string { | ||
return fmt.Sprintf( | ||
"Goroutine %v in state %v, with %v on top of the stack:\n%s", | ||
s.id, s.state, s.firstFunction, s.Full()) | ||
s.id, s.state, s.FirstFunction(), s.Full()) | ||
} | ||
|
||
func getStacks(all bool) []Stack { | ||
|
@@ -82,7 +90,6 @@ func getStacks(all bool) []Stack { | |
} | ||
|
||
// If we see the goroutine header, start a new stack. | ||
isFirstLine := false | ||
if strings.HasPrefix(line, "goroutine ") { | ||
// flush any previous stack | ||
if curStack != nil { | ||
|
@@ -94,11 +101,10 @@ func getStacks(all bool) []Stack { | |
state: goState, | ||
fullStack: &bytes.Buffer{}, | ||
} | ||
isFirstLine = true | ||
} | ||
curStack.fullStack.WriteString(line) | ||
if !isFirstLine && curStack.firstFunction == "" { | ||
curStack.firstFunction = parseFirstFunc(line) | ||
if f := parseFunc(line); f != "" { | ||
curStack.functions = append(curStack.functions, f) | ||
} | ||
Comment on lines
+106
to
108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like it's trying to parse every line, should we instead parse only the lines known to be functions? I think the format is:
|
||
} | ||
|
||
|
@@ -127,12 +133,15 @@ func getStackBuffer(all bool) []byte { | |
} | ||
} | ||
|
||
func parseFirstFunc(line string) string { | ||
func parseFunc(line string) string { | ||
line = strings.TrimSpace(line) | ||
if idx := strings.LastIndex(line, "("); idx > 0 { | ||
return line[:idx] | ||
} | ||
panic(fmt.Sprintf("function calls missing parents: %q", line)) | ||
if idx := strings.LastIndex(line, "created by"); idx >= 0 { | ||
return strings.TrimPrefix(line, "created by ") | ||
} | ||
Comment on lines
+141
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since it's not part of the stack, may be better to skip parsing these till it's required |
||
return "" | ||
} | ||
|
||
// parseGoStackHeader parses a stack header that looks like: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you update the tests in
stacks_test.go
to verify the behaviour ofAllFunctions
as well?