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.
5 // Package debug contains facilities for programs to debug themselves while
19 centerDot = []byte("·")
24 // PrintStack prints to standard error the stack trace returned by Stack.
26 os.Stderr.Write(stack())
29 // Stack returns a formatted stack trace of the goroutine that calls it.
30 // For each routine, it includes the source line information and PC value,
31 // then attempts to discover, for Go functions, the calling function or
32 // method and the text of the line containing the invocation.
34 // This function is deprecated. Use package runtime's Stack instead.
39 // stack implements Stack, skipping 2 frames
41 buf := new(bytes.Buffer) // the returned data
42 // As we loop, we open files and read them. These variables record the currently
46 for i := 2; ; i++ { // Caller we care about is the user, 2 frames up
47 pc, file, line, ok := runtime.Caller(i)
51 // Print this much at least. If we can't find the source, it won't show.
52 fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
54 data, err := ioutil.ReadFile(file)
58 lines = bytes.Split(data, []byte{'\n'})
61 line-- // in stack trace, lines are 1-indexed but our array is 0-indexed
62 fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
67 // source returns a space-trimmed slice of the n'th line.
68 func source(lines [][]byte, n int) []byte {
69 if n < 0 || n >= len(lines) {
72 return bytes.Trim(lines[n], " \t")
75 // function returns, if possible, the name of the function containing the PC.
76 func function(pc uintptr) []byte {
77 fn := runtime.FuncForPC(pc)
81 name := []byte(fn.Name())
82 // The name includes the path name to the package, which is unnecessary
83 // since the file name is already included. Plus, it has center dots.
85 // runtime/debug.*T·ptrmethod
88 // Since the package path might contains dots (e.g. code.google.com/...),
89 // we first remove the path prefix if there is one.
90 if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 {
91 name = name[lastslash+1:]
93 if period := bytes.Index(name, dot); period >= 0 {
94 name = name[period+1:]
96 name = bytes.Replace(name, centerDot, dot, -1)