command/terminal: Printing stack trace of all the goroutines (#1212)

Add -t falg to "goroutines" command. For example, "goroutines -t" will
print all the goroutines along with the stack trace.
This commit is contained in:
Chandrashekhara A
2018-05-22 23:32:43 +05:30
committed by Derek Parker
parent d2c48bac35
commit 3c794eb306
2 changed files with 28 additions and 14 deletions

View File

@ -199,13 +199,14 @@ Called with more arguments it will execute a command on the specified goroutine.
## goroutines ## goroutines
List program goroutines. List program goroutines.
goroutines [-u (default: user location)|-r (runtime location)|-g (go statement location)] goroutines [-u (default: user location)|-r (runtime location)|-g (go statement location) ] [ -t (stack trace)]
Print out info for every goroutine. The flag controls what information is shown along with each goroutine: Print out info for every goroutine. The flag controls what information is shown along with each goroutine:
-u displays location of topmost stackframe in user code -u displays location of topmost stackframe in user code
-r displays location of topmost stackframe (including frames inside private runtime functions) -r displays location of topmost stackframe (including frames inside private runtime functions)
-g displays location of go instruction that created the goroutine -g displays location of go instruction that created the goroutine
-t displays stack trace of goroutine
If no flag is specified the default is -u. If no flag is specified the default is -u.

View File

@ -142,13 +142,14 @@ See also: "help on", "help cond" and "help clear"`},
If called with the linespec argument it will delete all the breakpoints matching the linespec. If linespec is omitted all breakpoints are deleted.`}, If called with the linespec argument it will delete all the breakpoints matching the linespec. If linespec is omitted all breakpoints are deleted.`},
{aliases: []string{"goroutines"}, cmdFn: goroutines, helpMsg: `List program goroutines. {aliases: []string{"goroutines"}, cmdFn: goroutines, helpMsg: `List program goroutines.
goroutines [-u (default: user location)|-r (runtime location)|-g (go statement location)] goroutines [-u (default: user location)|-r (runtime location)|-g (go statement location) ] [ -t (stack trace)]
Print out info for every goroutine. The flag controls what information is shown along with each goroutine: Print out info for every goroutine. The flag controls what information is shown along with each goroutine:
-u displays location of topmost stackframe in user code -u displays location of topmost stackframe in user code
-r displays location of topmost stackframe (including frames inside private runtime functions) -r displays location of topmost stackframe (including frames inside private runtime functions)
-g displays location of go instruction that created the goroutine -g displays location of go instruction that created the goroutine
-t displays stack trace of goroutine
If no flag is specified the default is -u.`}, If no flag is specified the default is -u.`},
{aliases: []string{"goroutine"}, allowedPrefixes: onPrefix, cmdFn: c.goroutine, helpMsg: `Shows or changes current goroutine {aliases: []string{"goroutine"}, allowedPrefixes: onPrefix, cmdFn: c.goroutine, helpMsg: `Shows or changes current goroutine
@ -525,22 +526,27 @@ func (a byGoroutineID) Less(i, j int) bool { return a[i].ID < a[j].ID }
func goroutines(t *Term, ctx callContext, argstr string) error { func goroutines(t *Term, ctx callContext, argstr string) error {
args := strings.Split(argstr, " ") args := strings.Split(argstr, " ")
var fgl = fglUserCurrent var fgl = fglUserCurrent
bPrintStack := false
switch len(args) { switch len(args) {
case 0: case 0:
// nothing to do // nothing to do
case 1: case 1, 2:
switch args[0] { for _, arg := range args {
case "-u": switch arg {
fgl = fglUserCurrent case "-u":
case "-r": fgl = fglUserCurrent
fgl = fglRuntimeCurrent case "-r":
case "-g": fgl = fglRuntimeCurrent
fgl = fglGo case "-g":
case "": fgl = fglGo
// nothing to do case "-t":
default: bPrintStack = true
return fmt.Errorf("wrong argument: '%s'", args[0]) case "":
// nothing to do
default:
return fmt.Errorf("wrong argument: '%s'", arg)
}
} }
default: default:
return fmt.Errorf("too many arguments") return fmt.Errorf("too many arguments")
@ -561,6 +567,13 @@ func goroutines(t *Term, ctx callContext, argstr string) error {
prefix = "* " prefix = "* "
} }
fmt.Printf("%sGoroutine %s\n", prefix, formatGoroutine(g, fgl)) fmt.Printf("%sGoroutine %s\n", prefix, formatGoroutine(g, fgl))
if bPrintStack {
stack, err := t.client.Stacktrace(g.ID, 10, nil)
if err != nil {
return err
}
printStack(stack, "\t", false)
}
} }
return nil return nil
} }