void DreamGenContext::obicons() { uint8 value1, value2; getanyad(&value1, &value2); if (value1 == 0xff) { showframe((Frame *)segRef(data.word(kIcons2)).ptr(0, 0), 260, 1, 1, 0); } else { showframe((Frame *)segRef(data.word(kIcons2)).ptr(0, 0), 210, 1, 4, 0); } }
void DreamGenContext::showallex() { data.word(kListpos) = kExlist; memset(segRef(data.word(kBuffers)).ptr(kExlist, 100 * 5), 0xff, 100 * 5); data.word(kFrsegment) = data.word(kExtras); data.word(kDataad) = kExframedata; data.word(kFramesad) = kExframes; data.byte(kCurrentex) = 0; DynObject *objects = (DynObject *)segRef(data.word(kExtras)).ptr(kExdata, sizeof(DynObject)); for (size_t i = 0; i < 100; ++i, ++data.byte(kCurrentex)) { DynObject *object = objects + i; if (object->mapad[0] == 0xff) continue; if (object->currentLocation != data.byte(kReallocation)) continue; if (getmapad(object->mapad) == 0) continue; data.word(kCurrentframe) = 3 * data.byte(kCurrentex); uint8 width, height; calcfrframe(&width, &height); uint16 x, y; finalframe(&x, &y); if ((width != 0) || (height != 0)) { showframe((Frame *)segRef(data.word(kFrsegment)).ptr(0, 0), x + data.word(kMapadx), y + data.word(kMapady), data.word(kCurrentframe) & 0xff, 0); ObjPos *objPos = (ObjPos *)segRef(data.word(kBuffers)).ptr(data.word(kListpos), sizeof(ObjPos)); objPos->xMin = data.byte(kSavex); objPos->yMin = data.byte(kSavey); objPos->xMax = data.byte(kSavesize + 0) + data.byte(kSavex); objPos->yMax = data.byte(kSavesize + 1) + data.byte(kSavey); objPos->index = i; data.word(kListpos) += sizeof(ObjPos); } } }
void DreamGenContext::showallfree() { data.word(kListpos) = kFreelist; ObjPos *listPos = (ObjPos *)segRef(data.word(kBuffers)).ptr(kFreelist, 80 * sizeof(ObjPos)); memset(listPos, 0xff, 80 * sizeof(ObjPos)); data.word(kFrsegment) = data.word(kFreeframes); data.word(kDataad) = kFrframedata; data.word(kFramesad) = kFrframes; data.byte(kCurrentfree) = 0; const DynObject *freeObjects = (const DynObject *)segRef(data.word(kFreedat)).ptr(0, 0); for(size_t i = 0; i < 80; ++i) { uint8 mapad = getmapad(freeObjects[i].mapad); if (mapad != 0) { data.word(kCurrentframe) = 3 * data.byte(kCurrentfree); uint8 width, height; calcfrframe(&width, &height); uint16 x, y; finalframe(&x, &y); if ((width != 0) || (height != 0)) { x += data.word(kMapadx); y += data.word(kMapady); showframe((Frame *)segRef(data.word(kFrsegment)).ptr(0, 0), x, y, data.word(kCurrentframe) & 0xff, 0); ObjPos *objPos = (ObjPos *)segRef(data.word(kBuffers)).ptr(data.word(kListpos), sizeof(ObjPos)); objPos->xMin = data.byte(kSavex); objPos->yMin = data.byte(kSavey); objPos->xMax = data.byte(kSavex) + data.byte(kSavesize+0); objPos->yMax = data.byte(kSavey) + data.byte(kSavesize+1); objPos->index = i; data.word(kListpos) += sizeof(ObjPos); } } ++data.byte(kCurrentfree); } }
void DreamGenContext::crosshair() { uint8 frame; if ((data.byte(kCommandtype) != 3) && (data.byte(kCommandtype) < 10)) { frame = 9; } else { frame = 29; } const Frame *src = (const Frame *)segRef(data.word(kIcons1)).ptr(0, 0); showframe(src, kZoomx + 24, kZoomy + 19, frame, 0); }
void DreamGenContext::showpointer() { showblink(); const Frame *icons1 = ((const Frame *)segRef(data.word(kIcons1)).ptr(0, 0)); uint16 x = data.word(kMousex); data.word(kOldpointerx) = data.word(kMousex); uint16 y = data.word(kMousey); data.word(kOldpointery) = data.word(kMousey); if (data.byte(kPickup) == 1) { const Frame *frames; if (data.byte(kObjecttype) != 4) frames = (const Frame *)segRef(data.word(kFreeframes)).ptr(0, 0); else frames = (const Frame *)segRef(data.word(kExtras)).ptr(0, 0); const Frame *frame = frames + (3 * data.byte(kItemframe) + 1); uint8 width = frame->width; uint8 height = frame->height; if (width < 12) width = 12; if (height < 12) height = 12; data.byte(kPointerxs) = width; data.byte(kPointerys) = height; uint16 xMin = (x >= width / 2) ? x - width / 2 : 0; uint16 yMin = (y >= height / 2) ? y - height / 2 : 0; data.word(kOldpointerx) = xMin; data.word(kOldpointery) = yMin; multiget(segRef(data.word(kBuffers)).ptr(kPointerback, 0), xMin, yMin, width, height); showframe(frames, x, y, 3 * data.byte(kItemframe) + 1, 128); showframe(icons1, x, y, 3, 128); } else { const Frame *frame = icons1 + (data.byte(kPointerframe) + 20); uint8 width = frame->width; uint8 height = frame->height; if (width < 12) width = 12; if (height < 12) height = 12; data.byte(kPointerxs) = width; data.byte(kPointerys) = height; multiget(segRef(data.word(kBuffers)).ptr(kPointerback, 0), x, y, width, height); showframe(icons1, x, y, data.byte(kPointerframe) + 20, 0); } }
void DreamGenContext::obpicture() { if (data.byte(kObjecttype) == 1) return; Frame *frames; if (data.byte(kObjecttype) == 4) frames = (Frame *)segRef(data.word(kExtras)).ptr(0, 0); else frames = (Frame *)segRef(data.word(kFreeframes)).ptr(0, 0); uint8 frame = 3 * data.byte(kCommand) + 1; showframe(frames, 160, 68, frame, 0x80); }
void DreamGenContext::showblink() { if (data.byte(kManisoffscreen) == 1) return; ++data.byte(kBlinkcount); if (data.byte(kShadeson) != 0) return; if (data.byte(kReallocation) >= 50) // eyesshut return; if (data.byte(kBlinkcount) != 3) return; data.byte(kBlinkcount) = 0; uint8 blinkFrame = data.byte(kBlinkframe); ++blinkFrame; // Implicit %256 data.byte(kBlinkframe) = blinkFrame; if (blinkFrame > 6) blinkFrame = 6; static const uint8 blinkTab[] = { 16,18,18,17,16,16,16 }; uint8 width, height; showframe((Frame *)segRef(data.word(kIcons1)).ptr(0, 0), 44, 32, blinkTab[blinkFrame], 0, &width, &height); }
void runtime·printcreatedby(G *gp) { int32 line; uintptr pc, tracepc; Func *f; String file; // Show what created goroutine, except main goroutine (goid 1). 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; // back up to CALL instruction for funcline. 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 DreamGenContext::showallobs() { data.word(kListpos) = kSetlist; memset(segRef(data.word(kBuffers)).ptr(kSetlist, 0), 0xff, 128 * 5); data.word(kFrsegment) = data.word(kSetframes); data.word(kDataad) = kFramedata; data.word(kFramesad) = kFrames; const Frame *frames = (const Frame *)segRef(data.word(kFrsegment)).ptr(0, 0); SetObject *setEntries = (SetObject *)segRef(data.word(kSetdat)).ptr(0, 128 * sizeof(SetObject)); for (size_t i = 0; i < 128; ++i) { SetObject *setEntry = setEntries + i; if (getmapad(setEntry->mapad) == 0) continue; uint8 currentFrame = setEntry->b18[0]; data.word(kCurrentframe) = currentFrame; if (currentFrame == 0xff) continue; calcfrframe(); uint16 x, y; finalframe(&x, &y); setEntry->index = setEntry->b18[0]; if ((setEntry->type == 0) && (setEntry->priority != 5) && (setEntry->priority != 6)) { x += data.word(kMapadx); y += data.word(kMapady); showframe(frames, x, y, data.word(kCurrentframe), 0); } else makebackob(setEntry); ObjPos *objPos = (ObjPos *)segRef(data.word(kBuffers)).ptr(data.word(kListpos), sizeof(ObjPos)); objPos->xMin = data.byte(kSavex); objPos->yMin = data.byte(kSavey); objPos->xMax = data.byte(kSavex) + data.byte(kSavesize+0); objPos->yMax = data.byte(kSavey) + data.byte(kSavesize+1); objPos->index = i; data.word(kListpos) += sizeof(ObjPos); } }
int32 runtime·gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max) { int32 i, n, iter; uintptr pc, lr, tracepc, x; byte *fp, *p; bool waspanic; Stktop *stk; Func *f; pc = (uintptr)pc0; lr = (uintptr)lr0; fp = nil; waspanic = false; // If the PC is goexit, the goroutine hasn't started yet. if(pc == (uintptr)runtime·goexit) { pc = (uintptr)g->entry; lr = (uintptr)runtime·goexit; } // If the PC is zero, it's likely a nil function call. // Start in the caller's frame. if(pc == 0) { pc = lr; lr = 0; } n = 0; stk = (Stktop*)g->stackbase; for(iter = 0; iter < 100 && n < max; iter++) { // iter avoids looping forever // Typically: // pc is the PC of the running function. // sp is the stack pointer at that program counter. // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown. // stk is the stack containing sp. // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp. if(pc == (uintptr)runtime·lessstack) { // Hit top of stack segment. Unwind to next segment. pc = (uintptr)stk->gobuf.pc; sp = stk->gobuf.sp; lr = 0; fp = nil; if(pcbuf == nil) runtime·printf("----- stack segment boundary -----\n"); stk = (Stktop*)stk->stackbase; continue; } if(pc <= 0x1000 || (f = runtime·findfunc(pc)) == nil) { // Dangerous, but worthwhile: see if this is a closure by // decoding the instruction stream. // // We check p < p+4 to avoid wrapping and faulting if // we have lost track of where we are. p = (byte*)pc; if((pc&3) == 0 && p < p+4 && runtime·mheap.arena_start < p && p+4 < runtime·mheap.arena_used) { x = *(uintptr*)p; if((x&0xfffff000) == 0xe49df000) { // End of closure: // MOVW.P frame(R13), R15 pc = *(uintptr*)sp; lr = 0; sp += x & 0xfff; fp = nil; continue; } if((x&0xfffff000) == 0xe52de000 && lr == (uintptr)runtime·goexit) { // Beginning of closure. // Closure at top of stack, not yet started. p += 5*4; if((x&0xfff) != 4) { // argument copying p += 7*4; } if((byte*)pc < p && p < p+4 && p+4 < runtime·mheap.arena_used) { pc = *(uintptr*)p; fp = nil; continue; } } } break; } // Found an actual function. if(lr == 0) lr = *(uintptr*)sp; if(fp == nil) { fp = sp; if(pc > f->entry && f->frame >= 0) fp += f->frame; } if(skip > 0) skip--; else if(pcbuf != nil) pcbuf[n++] = pc; else { if(showframe(f)) { // Print during crash. // main(0x1, 0x2, 0x3) // /home/rsc/go/src/runtime/x.go:23 +0xf tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry && !waspanic) tracepc -= sizeof(uintptr); runtime·printf("%S(", f->name); for(i = 0; i < f->args; i++) { if(i != 0) runtime·prints(", "); runtime·printhex(((uintptr*)fp)[1+i]); if(i >= 4) { runtime·prints(", ..."); break; } } runtime·prints(")\n"); runtime·printf("\t%S:%d", f->src, runtime·funcline(f, tracepc)); if(pc > f->entry) runtime·printf(" +%p", (uintptr)(pc - f->entry)); runtime·printf("\n"); } n++; } waspanic = f->entry == (uintptr)runtime·sigpanic; if(pcbuf == nil && f->entry == (uintptr)runtime·newstack && g == m->g0) { runtime·printf("----- newstack called from goroutine %d -----\n", m->curg->goid); pc = (uintptr)m->morepc; sp = (byte*)m->moreargp - sizeof(void*); lr = (uintptr)m->morebuf.pc; fp = m->morebuf.sp; g = m->curg; stk = (Stktop*)g->stackbase; continue; } if(pcbuf == nil && f->entry == (uintptr)runtime·lessstack && g == m->g0) { runtime·printf("----- lessstack called from goroutine %d -----\n", m->curg->goid); g = m->curg; stk = (Stktop*)g->stackbase; sp = stk->gobuf.sp; pc = (uintptr)stk->gobuf.pc; fp = nil; lr = 0; continue; } // Unwind to next frame. pc = lr; lr = 0; sp = fp; fp = nil; // If this was div or divu or mod or modu, the caller had // an extra 8 bytes on its stack. Adjust sp. if(f->entry == (uintptr)_div || f->entry == (uintptr)_divu || f->entry == (uintptr)_mod || f->entry == (uintptr)_modu) sp += 8; // If this was deferproc or newproc, the caller had an extra 12. if(f->entry == (uintptr)runtime·deferproc || f->entry == (uintptr)runtime·newproc) sp += 12; } if(pcbuf == nil && (pc = g->gopc) != 0 && (f = runtime·findfunc(pc)) != nil && g->goid != 1) { runtime·printf("created by %S\n", f->name); tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry) tracepc -= sizeof(uintptr); runtime·printf("\t%S:%d", f->src, runtime·funcline(f, tracepc)); if(pc > f->entry) runtime·printf(" +%p", (uintptr)(pc - f->entry)); runtime·printf("\n"); } return n; }
void DreamGenContext::showpanel() { Frame *frame = (Frame *)segRef(data.word(kIcons1)).ptr(0, sizeof(Frame)); showframe(frame, 72, 0, 19, 0); showframe(frame, 192, 0, 19, 0); }
// Generic traceback. Handles runtime stack prints (pcbuf == nil), // the runtime.Callers function (pcbuf != nil), as well as the garbage // collector (callback != nil). A little clunky to merge these, but avoids // duplicating the code and all its subtlety. int32 runtime·gentraceback(uintptr pc0, uintptr sp0, uintptr lr0, G *gp, int32 skip, uintptr *pcbuf, int32 max, void (*callback)(Stkframe*, void*), void *v, bool printall) { int32 i, n, nprint, line; uintptr tracepc; bool waspanic, printing; Func *f, *flr; Stkframe frame; Stktop *stk; String file; USED(lr0); nprint = 0; runtime·memclr((byte*)&frame, sizeof frame); frame.pc = pc0; frame.sp = sp0; waspanic = false; printing = pcbuf==nil && callback==nil; // If the PC is zero, it's likely a nil function call. // Start in the caller's frame. if(frame.pc == 0) { frame.pc = *(uintptr*)frame.sp; frame.sp += sizeof(uintptr); } 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) { // Typically: // pc is the PC of the running function. // sp is the stack pointer at that program counter. // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown. // stk is the stack containing sp. // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp. if(frame.pc == (uintptr)runtime·lessstack) { // Hit top of stack segment. Unwind to next segment. frame.pc = stk->gobuf.pc; frame.sp = stk->gobuf.sp; frame.lr = 0; frame.fp = 0; frame.fn = nil; 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; // Found an actual function. // Derive frame pointer and link register. if(frame.fp == 0) { frame.fp = frame.sp + runtime·funcspdelta(f, frame.pc); frame.fp += sizeof(uintptr); // caller PC } if(runtime·topofstack(f)) { frame.lr = 0; flr = nil; } else { if(frame.lr == 0) frame.lr = ((uintptr*)frame.fp)[-1]; 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 - sizeof(uintptr); // Derive size of arguments. // Most functions have a fixed-size argument block, // so we can use metadata about the function f. // Not all, though: there are some variadic functions // in package runtime and reflect, and for those we use call-specific // metadata recorded by f's caller. if(callback != nil || printing) { frame.argp = (byte*)frame.fp; 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; } } if(skip > 0) { skip--; goto skipped; } if(pcbuf != nil) pcbuf[n] = frame.pc; if(callback != nil) callback(&frame, v); if(printing) { if(printall || runtime·showframe(f, gp)) { // Print during crash. // main(0x1, 0x2, 0x3) // /home/rsc/go/src/runtime/x.go:23 +0xf // tracepc = frame.pc; // back up to CALL instruction for funcline. if(n > 0 && frame.pc > f->entry && !waspanic) tracepc--; runtime·printf("%s(", runtime·funcname(f)); for(i = 0; i < frame.arglen/sizeof(uintptr); i++) { if(i >= 5) { 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 && gp == m->curg) runtime·printf(" fp=%p", frame.fp); runtime·printf("\n"); nprint++; } } n++; skipped: waspanic = f->entry == (uintptr)runtime·sigpanic; // Do not unwind past the bottom of the stack. if(flr == nil) break; // Unwind to next frame. frame.fn = flr; frame.pc = frame.lr; frame.lr = 0; frame.sp = frame.fp; frame.fp = 0; } if(pcbuf == nil && callback == nil) n = nprint; return n; }
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) { // Signal to fetch saved values from gp. 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 the PC is zero, it's likely a nil function call. // Start in the caller's frame. 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) { // Typically: // pc is the PC of the running function. // sp is the stack pointer at that program counter. // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown. // stk is the stack containing sp. // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp. if(frame.pc == (uintptr)runtime·lessstack) { // Hit top of stack segment. Unwind to next segment. 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; // Found an actual function. // Derive frame pointer and link register. 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) { // jmpdefer modifies SP/LR/PC non-atomically. // If a profiling interrupt arrives during jmpdefer, // the stack unwind may see a mismatched register set // and get confused. Stop if we see PC within jmpdefer // to avoid that confusion. // See golang.org/issue/8153. // This check can be deleted if jmpdefer is changed // to restore all three atomically using pop. 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; // Derive size of arguments. // Most functions have a fixed-size argument block, // so we can use metadata about the function f. // Not all, though: there are some variadic functions // in package runtime and reflect, and for those we use call-specific // metadata recorded by f's caller. 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; } } // Determine function SP where deferproc would find its arguments. // On ARM that's just the standard bottom-of-stack plus 1 word for // the saved LR. If the previous frame was a direct call to newproc/deferproc, // however, the SP is three words lower than normal. // If the function has no frame at all - perhaps it just started, or perhaps // it is a leaf with no local variables - then we cannot possibly find its // SP in a defer, and we might confuse its SP for its caller's SP, so // set sparg=0 in that case. sparg = 0; if(frame.fp != frame.sp) { sparg = frame.sp + sizeof(uintreg); if(wasnewproc) sparg += 3*sizeof(uintreg); } // Determine frame's 'continuation PC', where it can continue. // Normally this is the return address on the stack, but if sigpanic // is immediately below this function on the stack, then the frame // stopped executing due to a trap, and frame.pc is probably not // a safe point for looking up liveness information. In this panicking case, // the function either doesn't return at all (if it has no defers or if the // defers do not recover) or it returns from one of the calls to // deferproc a second time (if the corresponding deferred func recovers). // It suffices to assume that the most recent deferproc is the one that // returns; everything live at earlier deferprocs is still live at that one. 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; } // Unwind our local panic & defer stacks past this frame. 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)) { // Print during crash. // main(0x1, 0x2, 0x3) // /home/rsc/go/src/runtime/x.go:23 +0xf tracepc = frame.pc; // back up to CALL instruction for funcline. 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; // Do not unwind past the bottom of the stack. if(flr == nil) break; // Unwind to next frame. frame.pc = frame.lr; frame.fn = flr; frame.lr = 0; frame.sp = frame.fp; frame.fp = 0; // sighandler saves the lr on stack before faking a call to sigpanic 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; // For rationale, see long comment in traceback_x86.c. 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; }
// Generic traceback. Handles runtime stack prints (pcbuf == nil), // the runtime.Callers function (pcbuf != nil), as well as the garbage // collector (callback != nil). A little clunky to merge these, but avoids // duplicating the code and all its subtlety. 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 tracepc, sparg; bool waspanic, wasnewproc, printing; Func *f, *flr; Stkframe frame; Stktop *stk; String file; Panic *panic; Defer *defer; USED(lr0); gotraceback = runtime·gotraceback(nil); if(pc0 == ~(uintptr)0 && sp0 == ~(uintptr)0) { // Signal to fetch saved values from gp. if(gp->syscallstack != (uintptr)nil) { pc0 = gp->syscallpc; sp0 = gp->syscallsp; } else { pc0 = gp->sched.pc; sp0 = gp->sched.sp; } } nprint = 0; runtime·memclr((byte*)&frame, sizeof frame); frame.pc = pc0; 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 the PC is zero, it's likely a nil function call. // Start in the caller's frame. if(frame.pc == 0) { frame.pc = *(uintptr*)frame.sp; frame.sp += sizeof(uintreg); } 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) { // Typically: // pc is the PC of the running function. // sp is the stack pointer at that program counter. // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown. // stk is the stack containing sp. // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp. if(frame.pc == (uintptr)runtime·lessstack) { // Hit top of stack segment. Unwind to next segment. frame.pc = stk->gobuf.pc; frame.sp = stk->gobuf.sp; frame.lr = 0; frame.fp = 0; frame.fn = nil; 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; #ifdef GOOS_windows // Windows exception handlers run on the actual g stack (there is room // dedicated to this below the usual "bottom of stack"), not on a separate // stack. As a result, we have to be able to unwind past the exception // handler when called to unwind during stack growth inside the handler. // Recognize the frame at the call to sighandler in sigtramp and unwind // using the context argument passed to the call. This is awful. if(f != nil && f->entry == (uintptr)runtime·sigtramp && frame.pc > f->entry) { Context *r; // Invoke callback so that stack copier sees an uncopyable frame. if(callback != nil) { frame.continpc = frame.pc; frame.argp = nil; frame.arglen = 0; if(!callback(&frame, v)) return n; } r = (Context*)((uintptr*)frame.sp)[1]; #ifdef GOARCH_amd64 frame.pc = r->Rip; frame.sp = r->Rsp; #else frame.pc = r->Eip; frame.sp = r->Esp; #endif frame.lr = 0; frame.fp = 0; frame.fn = nil; if(printing && runtime·showframe(nil, gp)) runtime·printf("----- exception handler -----\n"); f = runtime·findfunc(frame.pc); if(f == nil) { runtime·printf("runtime: unknown pc %p after exception handler\n", frame.pc); if(callback != nil) runtime·throw("unknown pc"); } frame.fn = f; continue; } #endif // Found an actual function. // Derive frame pointer and link register. if(frame.fp == 0) { frame.fp = frame.sp + runtime·funcspdelta(f, frame.pc); frame.fp += sizeof(uintreg); // caller PC } if(runtime·topofstack(f)) { frame.lr = 0; flr = nil; } else { if(frame.lr == 0) frame.lr = ((uintreg*)frame.fp)[-1]; 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 - sizeof(uintreg); // Derive size of arguments. // Most functions have a fixed-size argument block, // so we can use metadata about the function f. // Not all, though: there are some variadic functions // in package runtime and reflect, and for those we use call-specific // metadata recorded by f's caller. if(callback != nil || printing) { frame.argp = (byte*)frame.fp; 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; } } // Determine function SP where deferproc would find its arguments. // On x86 that's just the standard bottom-of-stack, so SP exactly. // If the previous frame was a direct call to newproc/deferproc, however, // the SP is two words lower than normal. sparg = frame.sp; if(wasnewproc) sparg += 2*sizeof(uintptr); // Determine frame's 'continuation PC', where it can continue. // Normally this is the return address on the stack, but if sigpanic // is immediately below this function on the stack, then the frame // stopped executing due to a trap, and frame.pc is probably not // a safe point for looking up liveness information. In this panicking case, // the function either doesn't return at all (if it has no defers or if the // defers do not recover) or it returns from one of the calls to // deferproc a second time (if the corresponding deferred func recovers). // It suffices to assume that the most recent deferproc is the one that // returns; everything live at earlier deferprocs is still live at that one. 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; } // Unwind our local panic & defer stacks past this frame. 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)) { // Print during crash. // main(0x1, 0x2, 0x3) // /home/rsc/go/src/runtime/x.go:23 +0xf // tracepc = frame.pc; // back up to CALL instruction for funcline. if(n > 0 && frame.pc > f->entry && !waspanic) tracepc--; 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_c(((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(g->m->throwing > 0 && gp == g->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; // Do not unwind past the bottom of the stack. if(flr == nil) break; // Unwind to next frame. frame.fn = flr; frame.pc = frame.lr; frame.lr = 0; frame.sp = frame.fp; frame.fp = 0; } if(pcbuf == nil && callback == nil) n = nprint; // If callback != nil, we're being called to gather stack information during // garbage collection or stack growth. In that context, require that we used // up the entire defer stack. If not, then there is a bug somewhere and the // garbage collection or stack growth may not have seen the correct picture // of the stack. Crash now instead of silently executing the garbage collection // or stack copy incorrectly and setting up for a mysterious crash later. // // Note that panic != nil is okay here: there can be leftover panics, // because the defers on the panic stack do not nest in frame order as // they do on the defer stack. If you have: // // frame 1 defers d1 // frame 2 defers d2 // frame 3 defers d3 // frame 4 panics // frame 4's panic starts running defers // frame 5, running d3, defers d4 // frame 5 panics // frame 5's panic starts running defers // frame 6, running d4, garbage collects // frame 6, running d2, garbage collects // // During the execution of d4, the panic stack is d4 -> d3, which // is nested properly, and we'll treat frame 3 as resumable, because we // can find d3. (And in fact frame 3 is resumable. If d4 recovers // and frame 5 continues running, d3, d3 can recover and we'll // resume execution in (returning from) frame 3.) // // During the execution of d2, however, the panic stack is d2 -> d3, // which is inverted. The scan will match d2 to frame 2 but having // d2 on the stack until then means it will not match d3 to frame 3. // This is okay: if we're running d2, then all the defers after d2 have // completed and their corresponding frames are dead. Not finding d3 // for frame 3 means we'll set frame 3's continpc == 0, which is correct // (frame 3 is dead). At the end of the walk the panic stack can thus // contain defers (d3 in this case) for dead frames. The inversion here // always indicates a dead frame, and the effect of the inversion on the // scan is to hide those dead frames, so the scan is still okay: // what's left on the panic stack are exactly (and only) the dead frames. // // We require callback != nil here because only when callback != nil // do we know that gentraceback is being called in a "must be correct" // context as opposed to a "best effort" context. The tracebacks with // callbacks only happen when everything is stopped nicely. // At other times, such as when gathering a stack for a profiling signal // or when printing a traceback during a crash, everything may not be // stopped nicely, and the stack walk may not be able to complete. // It's okay in those situations not to use up the entire defer stack: // incomplete information then is still better than nothing. 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; }
// Generic traceback. Handles runtime stack prints (pcbuf == nil) // as well as the runtime.Callers function (pcbuf != nil). // A little clunky to merge the two but avoids duplicating // the code and all its subtlety. int32 runtime·gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max) { byte *p; int32 i, n, iter, sawnewstack; uintptr pc, lr, tracepc; byte *fp; Stktop *stk; Func *f; bool waspanic; USED(lr0); pc = (uintptr)pc0; lr = 0; fp = nil; waspanic = false; // If the PC is goexit, the goroutine hasn't started yet. if(pc0 == g->sched.pc && sp == g->sched.sp && pc0 == (byte*)runtime·goexit) { fp = sp; lr = pc; pc = (uintptr)g->entry; } // If the PC is zero, it's likely a nil function call. // Start in the caller's frame. if(pc == 0) { pc = lr; lr = 0; } // If the PC is zero, it's likely a nil function call. // Start in the caller's frame. if(pc == 0) { pc = *(uintptr*)sp; sp += sizeof(uintptr); } n = 0; sawnewstack = 0; stk = (Stktop*)g->stackbase; for(iter = 0; iter < 100 && n < max; iter++) { // iter avoids looping forever // Typically: // pc is the PC of the running function. // sp is the stack pointer at that program counter. // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown. // stk is the stack containing sp. // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp. if(pc == (uintptr)runtime·lessstack) { // Hit top of stack segment. Unwind to next segment. pc = (uintptr)stk->gobuf.pc; sp = stk->gobuf.sp; lr = 0; fp = nil; if(pcbuf == nil) runtime·printf("----- stack segment boundary -----\n"); stk = (Stktop*)stk->stackbase; continue; } if(pc <= 0x1000 || (f = runtime·findfunc(pc)) == nil) { // Dangerous, but worthwhile: see if this is a closure: // ADDQ $wwxxyyzz, SP; RET // [48] 81 c4 zz yy xx ww c3 // The 0x48 byte is only on amd64. p = (byte*)pc; // We check p < p+8 to avoid wrapping and faulting if we lose track. if(runtime·mheap.arena_start < p && p < p+8 && p+8 < runtime·mheap.arena_used && // pointer in allocated memory (sizeof(uintptr) != 8 || *p++ == 0x48) && // skip 0x48 byte on amd64 p[0] == 0x81 && p[1] == 0xc4 && p[6] == 0xc3) { sp += *(uint32*)(p+2); pc = *(uintptr*)sp; sp += sizeof(uintptr); lr = 0; fp = nil; continue; } // Closure at top of stack, not yet started. if(lr == (uintptr)runtime·goexit && (pc = isclosureentry(pc)) != 0) { fp = sp; continue; } // Unknown pc: stop. break; } // Found an actual function. if(fp == nil) { fp = sp; if(pc > f->entry && f->frame >= sizeof(uintptr)) fp += f->frame - sizeof(uintptr); if(lr == 0) lr = *(uintptr*)fp; fp += sizeof(uintptr); } else if(lr == 0) lr = *(uintptr*)fp; if(skip > 0) skip--; else if(pcbuf != nil) pcbuf[n++] = pc; else { if(runtime·showframe(f)) { // Print during crash. // main(0x1, 0x2, 0x3) // /home/rsc/go/src/runtime/x.go:23 +0xf // tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry && !waspanic) tracepc--; runtime·printf("%S(", f->name); for(i = 0; i < f->args; i++) { if(i != 0) runtime·prints(", "); runtime·printhex(((uintptr*)fp)[i]); if(i >= 4) { runtime·prints(", ..."); break; } } runtime·prints(")\n"); runtime·printf("\t%S:%d", f->src, runtime·funcline(f, tracepc)); if(pc > f->entry) runtime·printf(" +%p", (uintptr)(pc - f->entry)); runtime·printf("\n"); } n++; } waspanic = f->entry == (uintptr)runtime·sigpanic; if(f->entry == (uintptr)runtime·deferproc || f->entry == (uintptr)runtime·newproc) fp += 2*sizeof(uintptr); if(f->entry == (uintptr)runtime·newstack) sawnewstack = 1; if(pcbuf == nil && f->entry == (uintptr)runtime·morestack && g == m->g0 && sawnewstack) { // The fact that we saw newstack means that morestack // has managed to record its information in m, so we can // use it to keep unwinding the stack. runtime·printf("----- morestack called from goroutine %d -----\n", m->curg->goid); pc = (uintptr)m->morepc; sp = m->morebuf.sp - sizeof(void*); lr = (uintptr)m->morebuf.pc; fp = m->morebuf.sp; sawnewstack = 0; g = m->curg; stk = (Stktop*)g->stackbase; continue; } if(pcbuf == nil && f->entry == (uintptr)runtime·lessstack && g == m->g0) { // Lessstack is running on scheduler stack. Switch to original goroutine. runtime·printf("----- lessstack called from goroutine %d -----\n", m->curg->goid); g = m->curg; stk = (Stktop*)g->stackbase; sp = stk->gobuf.sp; pc = (uintptr)stk->gobuf.pc; fp = nil; lr = 0; continue; } // Unwind to next frame. pc = lr; lr = 0; sp = fp; fp = nil; } // Show what created goroutine, except main goroutine (goid 1). if(pcbuf == nil && (pc = g->gopc) != 0 && (f = runtime·findfunc(pc)) != nil && g->goid != 1) { runtime·printf("created by %S\n", f->name); tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry) tracepc--; runtime·printf("\t%S:%d", f->src, runtime·funcline(f, tracepc)); if(pc > f->entry) runtime·printf(" +%p", (uintptr)(pc - f->entry)); runtime·printf("\n"); } return n; }