Skip to content

Commit

Permalink
added debug stack
Browse files Browse the repository at this point in the history
  • Loading branch information
hiromaily committed Apr 11, 2018
1 parent c37cb67 commit 931b6e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions runtimes/runtimes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"regexp"
"runtime"
"runtime/debug"
"strings"
)

Expand Down Expand Up @@ -98,3 +99,14 @@ func CurrentFuncV2() []byte {

return b[:i]
}

func DebugStack() {
// Stack returns a formatted stack trace of the goroutine that calls it.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
fmt.Println(string(debug.Stack()))
}

func DebugPrintStack() {
// PrintStack prints to standard error the stack trace returned by runtime.Stack.
debug.PrintStack()
}
20 changes: 20 additions & 0 deletions runtimes/runtimes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"runtime"
"runtime/debug"
"sync"
"testing"
)

Expand Down Expand Up @@ -119,3 +120,22 @@ func TestTraceAllHistory(t *testing.T) {
//01: [Function]TestTraceAllHistory [File]./golibs/runtimes/runtimes_test.go:115
//00: [Function]TraceAllHistory [File]./golibs/runtimes/runtimes.go:63}
}

func TestDebugStack(t *testing.T) {
fmt.Println("TestDebugStack")
wg := &sync.WaitGroup{}

wg.Add(1)
go func() {
DebugStack()
wg.Done()
}()

wg.Add(1)
go func() {
DebugPrintStack()
wg.Done()
}()

wg.Wait()
}

0 comments on commit 931b6e1

Please sign in to comment.