stack returns a nicely formatted stack frame, skipping skip frames.
(skip int)
| 112 | |
| 113 | // stack returns a nicely formatted stack frame, skipping skip frames. |
| 114 | func stack(skip int) []byte { |
| 115 | buf := new(bytes.Buffer) // the returned data |
| 116 | // As we loop, we open files and read them. These variables record the currently |
| 117 | // loaded file. |
| 118 | var ( |
| 119 | nLine string |
| 120 | lastFile string |
| 121 | err error |
| 122 | ) |
| 123 | for i := skip; ; i++ { // Skip the expected number of frames |
| 124 | pc, file, line, ok := runtime.Caller(i) |
| 125 | if !ok { |
| 126 | break |
| 127 | } |
| 128 | // Print this much at least. If we can't find the source, it won't show. |
| 129 | fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) |
| 130 | if file != lastFile { |
| 131 | nLine, err = readNthLine(file, line-1) |
| 132 | if err != nil { |
| 133 | continue |
| 134 | } |
| 135 | lastFile = file |
| 136 | } |
| 137 | fmt.Fprintf(buf, "\t%s: %s\n", function(pc), cmp.Or(nLine, dunno)) |
| 138 | } |
| 139 | return buf.Bytes() |
| 140 | } |
| 141 | |
| 142 | // readNthLine reads the nth line from the file. |
| 143 | // It returns the trimmed content of the line if found, |