CallerStack returns the call stack for the calling function, up to depth frames deep, skipping the provided number of frames, not including Callers itself. If zero, depth defaults to 8.
(skip, depth int)
| 135 | // |
| 136 | // If zero, depth defaults to 8. |
| 137 | func CallerStack(skip, depth int) Stack { |
| 138 | if depth <= 0 { |
| 139 | depth = _defaultCallersDepth |
| 140 | } |
| 141 | |
| 142 | pcs := make([]uintptr, depth) |
| 143 | |
| 144 | // +2 to skip this frame and runtime.Callers. |
| 145 | n := runtime.Callers(skip+2, pcs) |
| 146 | pcs = pcs[:n] // truncate to number of frames actually read |
| 147 | |
| 148 | result := make([]Frame, 0, n) |
| 149 | frames := runtime.CallersFrames(pcs) |
| 150 | for f, more := frames.Next(); more; f, more = frames.Next() { |
| 151 | result = append(result, Frame{ |
| 152 | Function: sanitize(f.Function), |
| 153 | File: f.File, |
| 154 | Line: f.Line, |
| 155 | }) |
| 156 | } |
| 157 | return result |
| 158 | } |