This source file includes following definitions.
- runtime·gentraceback
- runtime·printcreatedby
- runtime·traceback
- runtime·callers
#include "runtime.h"
#include "arch_GOARCH.h"
#include "malloc.h"
#include "funcdata.h"
void runtime·sigpanic(void);
void runtime·newproc(void);
void runtime·deferproc(void);
int32
runtime·gentraceback(uintptr pc0, uintptr sp0, uintptr lr0, G *gp, int32 skip, uintptr *pcbuf, int32 max, bool (*callback)(Stkframe*, void*), void *v, bool printall)
{
int32 i, n, nprint, line, gotraceback;
uintptr x, tracepc, sparg;
bool waspanic, wasnewproc, printing;
Func *f, *flr;
Stkframe frame;
Stktop *stk;
String file;
Panic *panic;
Defer *defer;
gotraceback = runtime·gotraceback(nil);
if(pc0 == ~(uintptr)0 && sp0 == ~(uintptr)0) {
if(gp->syscallstack != (uintptr)nil) {
pc0 = gp->syscallpc;
sp0 = gp->syscallsp;
lr0 = 0;
} else {
pc0 = gp->sched.pc;
sp0 = gp->sched.sp;
lr0 = gp->sched.lr;
}
}
nprint = 0;
runtime·memclr((byte*)&frame, sizeof frame);
frame.pc = pc0;
frame.lr = lr0;
frame.sp = sp0;
waspanic = false;
wasnewproc = false;
printing = pcbuf==nil && callback==nil;
panic = gp->panic;
defer = gp->defer;
while(defer != nil && defer->argp == NoArgs)
defer = defer->link;
while(panic != nil && panic->defer == nil)
panic = panic->link;
if(frame.pc == 0) {
frame.pc = frame.lr;
frame.lr = 0;
}
f = runtime·findfunc(frame.pc);
if(f == nil) {
if(callback != nil) {
runtime·printf("runtime: unknown pc %p\n", frame.pc);
runtime·throw("unknown pc");
}
return 0;
}
frame.fn = f;
n = 0;
stk = (Stktop*)gp->stackbase;
while(n < max) {
if(frame.pc == (uintptr)runtime·lessstack) {
frame.pc = stk->gobuf.pc;
frame.sp = stk->gobuf.sp;
frame.lr = 0;
frame.fp = 0;
if(printing && runtime·showframe(nil, gp))
runtime·printf("----- stack segment boundary -----\n");
stk = (Stktop*)stk->stackbase;
f = runtime·findfunc(frame.pc);
if(f == nil) {
runtime·printf("runtime: unknown pc %p after stack split\n", frame.pc);
if(callback != nil)
runtime·throw("unknown pc");
}
frame.fn = f;
continue;
}
f = frame.fn;
if(frame.fp == 0)
frame.fp = frame.sp + runtime·funcspdelta(f, frame.pc);
if(runtime·topofstack(f)) {
frame.lr = 0;
flr = nil;
} else if(f->entry == (uintptr)runtime·jmpdefer) {
if(callback != nil)
runtime·throw("traceback_arm: found jmpdefer when tracing with callback");
frame.lr = 0;
flr = nil;
} else {
if((n == 0 && frame.sp < frame.fp) || frame.lr == 0)
frame.lr = *(uintptr*)frame.sp;
flr = runtime·findfunc(frame.lr);
if(flr == nil) {
runtime·printf("runtime: unexpected return pc for %s called from %p\n", runtime·funcname(f), frame.lr);
if(callback != nil)
runtime·throw("unknown caller pc");
}
}
frame.varp = (byte*)frame.fp;
if(callback != nil || printing) {
frame.argp = (byte*)frame.fp + sizeof(uintptr);
if(f->args != ArgsSizeUnknown)
frame.arglen = f->args;
else if(flr == nil)
frame.arglen = 0;
else if(frame.lr == (uintptr)runtime·lessstack)
frame.arglen = stk->argsize;
else if((i = runtime·funcarglen(flr, frame.lr)) >= 0)
frame.arglen = i;
else {
runtime·printf("runtime: unknown argument frame size for %s called from %p [%s]\n",
runtime·funcname(f), frame.lr, flr ? runtime·funcname(flr) : "?");
if(callback != nil)
runtime·throw("invalid stack");
frame.arglen = 0;
}
}
sparg = 0;
if(frame.fp != frame.sp) {
sparg = frame.sp + sizeof(uintreg);
if(wasnewproc)
sparg += 3*sizeof(uintreg);
}
frame.continpc = frame.pc;
if(waspanic) {
if(panic != nil && panic->defer->argp == (byte*)sparg)
frame.continpc = (uintptr)panic->defer->pc;
else if(defer != nil && defer->argp == (byte*)sparg)
frame.continpc = (uintptr)defer->pc;
else
frame.continpc = 0;
}
while(panic != nil && (panic->defer == nil || panic->defer->argp == (byte*)sparg || panic->defer->argp == NoArgs))
panic = panic->link;
while(defer != nil && (defer->argp == (byte*)sparg || defer->argp == NoArgs))
defer = defer->link;
if(skip > 0) {
skip--;
goto skipped;
}
if(pcbuf != nil)
pcbuf[n] = frame.pc;
if(callback != nil) {
if(!callback(&frame, v))
return n;
}
if(printing) {
if(printall || runtime·showframe(f, gp)) {
tracepc = frame.pc;
if(n > 0 && frame.pc > f->entry && !waspanic)
tracepc -= sizeof(uintptr);
runtime·printf("%s(", runtime·funcname(f));
for(i = 0; i < frame.arglen/sizeof(uintptr); i++) {
if(i >= 10) {
runtime·prints(", ...");
break;
}
if(i != 0)
runtime·prints(", ");
runtime·printhex(((uintptr*)frame.argp)[i]);
}
runtime·prints(")\n");
line = runtime·funcline(f, tracepc, &file);
runtime·printf("\t%S:%d", file, line);
if(frame.pc > f->entry)
runtime·printf(" +%p", (uintptr)(frame.pc - f->entry));
if(m->throwing > 0 && gp == m->curg || gotraceback >= 2)
runtime·printf(" fp=%p sp=%p", frame.fp, frame.sp);
runtime·printf("\n");
nprint++;
}
}
n++;
skipped:
waspanic = f->entry == (uintptr)runtime·sigpanic;
wasnewproc = f->entry == (uintptr)runtime·newproc || f->entry == (uintptr)runtime·deferproc;
if(flr == nil)
break;
frame.pc = frame.lr;
frame.fn = flr;
frame.lr = 0;
frame.sp = frame.fp;
frame.fp = 0;
if(waspanic) {
x = *(uintptr*)frame.sp;
frame.sp += 4;
frame.fn = f = runtime·findfunc(frame.pc);
if(f == nil)
frame.pc = x;
else if(f->frame == 0)
frame.lr = x;
}
}
if(pcbuf == nil && callback == nil)
n = nprint;
if(callback != nil && n < max && defer != nil) {
if(defer != nil)
runtime·printf("runtime: g%D: leftover defer argp=%p pc=%p\n", gp->goid, defer->argp, defer->pc);
if(panic != nil)
runtime·printf("runtime: g%D: leftover panic argp=%p pc=%p\n", gp->goid, panic->defer->argp, panic->defer->pc);
for(defer = gp->defer; defer != nil; defer = defer->link)
runtime·printf("\tdefer %p argp=%p pc=%p\n", defer, defer->argp, defer->pc);
for(panic = gp->panic; panic != nil; panic = panic->link) {
runtime·printf("\tpanic %p defer %p", panic, panic->defer);
if(panic->defer != nil)
runtime·printf(" argp=%p pc=%p", panic->defer->argp, panic->defer->pc);
runtime·printf("\n");
}
runtime·throw("traceback has leftover defers or panics");
}
return n;
}
void
runtime·printcreatedby(G *gp)
{
int32 line;
uintptr pc, tracepc;
Func *f;
String file;
if((pc = gp->gopc) != 0 && (f = runtime·findfunc(pc)) != nil &&
runtime·showframe(f, gp) && gp->goid != 1) {
runtime·printf("created by %s\n", runtime·funcname(f));
tracepc = pc;
if(pc > f->entry)
tracepc -= PCQuantum;
line = runtime·funcline(f, tracepc, &file);
runtime·printf("\t%S:%d", file, line);
if(pc > f->entry)
runtime·printf(" +%p", (uintptr)(pc - f->entry));
runtime·printf("\n");
}
}
void
runtime·traceback(uintptr pc, uintptr sp, uintptr lr, G *gp)
{
int32 n;
if(gp->status == Gsyscall) {
pc = gp->syscallpc;
sp = gp->syscallsp;
lr = 0;
}
n = runtime·gentraceback(pc, sp, lr, gp, 0, nil, TracebackMaxFrames, nil, nil, false);
if(n == 0)
runtime·gentraceback(pc, sp, lr, gp, 0, nil, TracebackMaxFrames, nil, nil, true);
if(n == TracebackMaxFrames)
runtime·printf("...additional frames elided...\n");
runtime·printcreatedby(gp);
}
int32
runtime·callers(int32 skip, uintptr *pcbuf, int32 m)
{
uintptr pc, sp;
sp = runtime·getcallersp(&skip);
pc = (uintptr)runtime·getcallerpc(&skip);
return runtime·gentraceback(pc, sp, 0, g, skip, pcbuf, m, nil, nil, false);
}