...

Source file src/pkg/runtime/debug/stack.go

     1	// Copyright 2011 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package debug contains facilities for programs to debug themselves while
     6	// they are running.
     7	package debug
     8	
     9	import (
    10		"os"
    11		"runtime"
    12	)
    13	
    14	// PrintStack prints to standard error the stack trace returned by runtime.Stack.
    15	func PrintStack() {
    16		os.Stderr.Write(Stack())
    17	}
    18	
    19	// Stack returns a formatted stack trace of the goroutine that calls it.
    20	// It calls runtime.Stack with a large enough buffer to capture the entire trace.
    21	func Stack() []byte {
    22		buf := make([]byte, 1024)
    23		for {
    24			n := runtime.Stack(buf, false)
    25			if n < len(buf) {
    26				return buf[:n]
    27			}
    28			buf = make([]byte, 2*len(buf))
    29		}
    30	}
    31	

View as plain text