void findfunc(cons_t *root, char *key, int count) { switch (root->type) { case L_K: if (root->car != NULL) { findfunc(root->car, key, count); } if (root->cdr != NULL) { findfunc(root->cdr, key, count); } break; case FUNC: if (strcmp(root->cvalue, key) == 0) { root->type = RFUNC; root->ivalue = count; } if (root->cdr != NULL) { findfunc(root->cdr, key, count); } break; default: if (root->cdr != NULL) { findfunc(root->cdr, key, count); } } }
void runtime·Caller(intgo skip, uintptr retpc, String retfile, intgo retline, bool retbool) { retpc = 0; FLUSH(&retpc); retfile.str = 0; retfile.len = 0; FLUSH(&retfile); retline = 0; FLUSH(&retline); retbool = 0; FLUSH(&retbool); #line 73 "/home/14/ren/source/golang/go/src/pkg/runtime/runtime1.goc" Func *f, *g; uintptr pc; uintptr rpc[2]; /* * Ask for two PCs: the one we were asked for * and what it called, so that we can see if it * "called" sigpanic. */ retpc = 0; if(runtime·callers(1+skip-1, rpc, 2) < 2) { retfile = runtime·emptystring; retline = 0; retbool = false; } else if((f = runtime·findfunc(rpc[1])) == nil) { retfile = runtime·emptystring; retline = 0; retbool = true; // have retpc at least } else { retpc = rpc[1]; pc = retpc; g = runtime·findfunc(rpc[0]); if(pc > f->entry && (g == nil || g->entry != (uintptr)runtime·sigpanic)) pc--; retline = runtime·funcline(f, pc, &retfile); retbool = true; } FLUSH(&retpc); FLUSH(&retfile); FLUSH(&retline); FLUSH(&retbool); }
void dumphistogram(void) { int i, h, n; PC *x; Func *f, **ff; if(!histograms) return; // assign counts to functions. for(h = 0; h < Ncounters; h++) { for(x = counters[h]; x != NULL; x = x->next) { f = findfunc(x->pc); if(f) { f->onstack += x->count; f->leaf += x->count; } f = findfunc(x->callerpc); if(f) f->leaf -= x->count; } } // build array ff = malloc(nfunc*sizeof ff[0]); if(ff == nil) sysfatal("out of memory"); n = 0; for(h = 0; h < nelem(func); h++) for(f = func[h]; f != NULL; f = f->next) ff[n++] = f; // sort by leaf counts qsort(ff, nfunc, sizeof ff[0], compareleaf); // print. fprint(2, "%d samples (avg %.1g threads)\n", nsample, (double)nsamplethread/nsample); for(i = 0; i < nfunc; i++) { f = ff[i]; fprint(2, "%6.2f%%\t", 100.0*(double)f->leaf/nsample); if(stacks) fprint(2, "%6.2f%%\t", 100.0*(double)f->onstack/nsample); fprint(2, "%s\n", f->s.name); } }
void runtime·Caller(intgo skip, uintptr retpc, String retfile, intgo retline, bool retbool) { retpc = 0; FLUSH(&retpc); retfile.str = 0; retfile.len = 0; FLUSH(&retfile); retline = 0; FLUSH(&retline); retbool = 0; FLUSH(&retbool); #line 73 "C:\Users\ADMINI~1\AppData\Local\Temp\2\makerelease686069423\go\src\pkg\runtime\runtime1.goc" Func *f, *g; uintptr pc; uintptr rpc[2]; /* * Ask for two PCs: the one we were asked for * and what it called, so that we can see if it * "called" sigpanic. */ retpc = 0; if(runtime·callers(1+skip-1, rpc, 2) < 2) { retfile = runtime·emptystring; retline = 0; retbool = false; } else if((f = runtime·findfunc(rpc[1])) == nil) { retfile = runtime·emptystring; retline = 0; retbool = true; // have retpc at least } else { retpc = rpc[1]; pc = retpc; g = runtime·findfunc(rpc[0]); if(pc > f->entry && (g == nil || g->entry != (uintptr)runtime·sigpanic)) pc--; retline = runtime·funcline(f, pc, &retfile); retbool = true; } FLUSH(&retpc); FLUSH(&retfile); FLUSH(&retline); FLUSH(&retbool); }
/* * define function. Returns 1 if function is being undefined (t == 0) and * function did not exist, returns 0 otherwise. */ int define(const char *name, struct op *t) { uint32_t nhash; struct tbl *tp; bool was_set = false; nhash = hash(name); if (t != NULL && !tobool(t->u.ksh_func)) { /* drop same-name aliases for POSIX functions */ if ((tp = ktsearch(&aliases, name, nhash))) ktdelete(tp); } while (/* CONSTCOND */ 1) { tp = findfunc(name, nhash, true); /* because findfunc:create=true */ mkssert(tp != NULL); if (tp->flag & ISSET) was_set = true; /* * If this function is currently being executed, we zap * this table entry so findfunc() won't see it */ if (tp->flag & FINUSE) { tp->name[0] = '\0'; /* ensure it won't be found */ tp->flag &= ~DEFINED; tp->flag |= FDELETE; } else break; } if (tp->flag & ALLOC) { tp->flag &= ~(ISSET|ALLOC); tfree(tp->val.t, tp->areap); } if (t == NULL) { /* undefine */ ktdelete(tp); return (was_set ? 0 : 1); } tp->val.t = tcopy(t->left, tp->areap); tp->flag |= (ISSET|ALLOC); if (t->u.ksh_func) tp->flag |= FKSH; return (0); }
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"); } }
int def(cons_t *next) { int count; cons_t *now = NULL; now = next->cdr; count = getfunc(now->cvalue); if (g_fa[count].key == NULL) { g_fa[count].key = (char*)malloc(strlen(now->cvalue) + 1); strcpy(g_fa[count].key, now->cvalue); } next = now; now = now->cdr; g_fa[count].exp = now->cdr; findfunc(now->cdr, next->cvalue, count); next = now->car; findarg(now->cdr, next); now->cdr = NULL; printf("define %s\n",g_fa[count].key); return 0; }
/* * find command * either function, hashed command, or built-in (in that order) */ struct tbl * findcom(const char *name, int flags) { static struct tbl temp; uint32_t h = hash(name); struct tbl *tp = NULL, *tbi; /* insert if not found */ unsigned char insert = Flag(FTRACKALL); /* for function autoloading */ char *fpath; union mksh_cchack npath; if (vstrchr(name, '/')) { insert = 0; /* prevent FPATH search below */ flags &= ~FC_FUNC; goto Search; } tbi = (flags & FC_BI) ? ktsearch(&builtins, name, h) : NULL; /* * POSIX says special builtins first, then functions, then * POSIX regular builtins, then search path... */ if ((flags & FC_SPECBI) && tbi && (tbi->flag & SPEC_BI)) tp = tbi; if (!tp && (flags & FC_FUNC)) { tp = findfunc(name, h, false); if (tp && !(tp->flag & ISSET)) { if ((fpath = str_val(global("FPATH"))) == null) { tp->u.fpath = NULL; tp->u2.errnov = ENOENT; } else tp->u.fpath = search_path(name, fpath, R_OK, &tp->u2.errnov); } } if (!tp && (flags & FC_REGBI) && tbi && (tbi->flag & REG_BI)) tp = tbi; if (!tp && (flags & FC_UNREGBI) && tbi) tp = tbi; if (!tp && (flags & FC_PATH) && !(flags & FC_DEFPATH)) { tp = ktsearch(&taliases, name, h); if (tp && (tp->flag & ISSET) && ksh_access(tp->val.s, X_OK) != 0) { if (tp->flag & ALLOC) { tp->flag &= ~ALLOC; afree(tp->val.s, APERM); } tp->flag &= ~ISSET; } } Search: if ((!tp || (tp->type == CTALIAS && !(tp->flag&ISSET))) && (flags & FC_PATH)) { if (!tp) { if (insert && !(flags & FC_DEFPATH)) { tp = ktenter(&taliases, name, h); tp->type = CTALIAS; } else { tp = &temp; tp->type = CEXEC; } /* make ~ISSET */ tp->flag = DEFINED; } npath.ro = search_path(name, (flags & FC_DEFPATH) ? def_path : path, X_OK, &tp->u2.errnov); if (npath.ro) { strdupx(tp->val.s, npath.ro, APERM); if (npath.ro != name) afree(npath.rw, ATEMP); tp->flag |= ISSET|ALLOC; } else if ((flags & FC_FUNC) && (fpath = str_val(global("FPATH"))) != null && (npath.ro = search_path(name, fpath, R_OK, &tp->u2.errnov)) != NULL) { /* * An undocumented feature of AT&T ksh is that * it searches FPATH if a command is not found, * even if the command hasn't been set up as an * autoloaded function (ie, no typeset -uf). */ tp = &temp; tp->type = CFUNC; /* make ~ISSET */ tp->flag = DEFINED; tp->u.fpath = npath.ro; } } return (tp); }
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; }
/* * Call the appropriate user-defined routine to handle an object action. * Returns the value that the routine returned. */ VALUE objcall(int action, VALUE *v1, VALUE *v2, VALUE *v3) { FUNC *fp; /* function to call */ STATIC OBJECTACTIONS *oap; /* object to call for */ struct objectinfo *oip; /* information about action */ long index; /* index of function (negative if undefined) */ VALUE val; /* return value */ VALUE tmp; /* temp value */ char name[SYMBOLSIZE+1+1]; /* full name of user routine to call */ size_t namestr_len; /* length of the namestr() return string */ char *namestr_ret; /* namestr() return string */ size_t opi_name_len; /* length of the oip name */ /* initialize VALUEs */ val.v_subtype = V_NOSUBTYPE; tmp.v_subtype = V_NOSUBTYPE; if ((unsigned)action > OBJ_MAXFUNC) { math_error("Illegal action for object call"); /*NOTREACHED*/ } oip = &objectinfo[action]; if (v1->v_type == V_OBJ) { oap = v1->v_obj->o_actions; } else if (v2->v_type == V_OBJ) { oap = v2->v_obj->o_actions; } else { math_error("Object routine called with non-object"); /*NOTREACHED*/ } index = oap->oa_indices[action]; if (index < 0) { namestr_ret = namestr(&objectnames, oap->oa_index); if (namestr_ret == NULL) { math_error("namestr returned NULL!!!"); /*NOTREACHED*/ } namestr_len = strlen(namestr_ret); opi_name_len = strlen(oip->name); if (namestr_len > (size_t)SYMBOLSIZE-1-opi_name_len) { math_error("namestr returned a strong too long!!!"); /*NOTREACHED*/ } name[0] = '\0'; strncpy(name, namestr_ret, namestr_len+1); strcat(name, "_"); strncat(name, oip->name, opi_name_len+1); index = adduserfunc(name); oap->oa_indices[action] = index; } fp = NULL; if (index >= 0) fp = findfunc(index); if (fp == NULL) { switch (oip->error) { case ERR_PRINT: objprint(v1->v_obj); val.v_type = V_NULL; break; case ERR_CMP: val.v_type = V_INT; if (v1->v_type != v2->v_type) { val.v_int = 1; return val; } val.v_int = objcmp(v1->v_obj, v2->v_obj); break; case ERR_TEST: val.v_type = V_INT; val.v_int = objtest(v1->v_obj); break; case ERR_POW: if (v2->v_type != V_NUM) { math_error("Non-real power"); /*NOTREACHED*/ } val = objpowi(v1, v2->v_num); break; case ERR_ONE: val.v_type = V_NUM; val.v_num = qlink(&_qone_); break; case ERR_INC: tmp.v_type = V_NUM; tmp.v_num = &_qone_; val = objcall(OBJ_ADD, v1, &tmp, NULL_VALUE); break; case ERR_DEC: tmp.v_type = V_NUM; tmp.v_num = &_qone_; val = objcall(OBJ_SUB, v1, &tmp, NULL_VALUE); break; case ERR_SQUARE: val = objcall(OBJ_MUL, v1, v1, NULL_VALUE); break; case ERR_VALUE: copyvalue(v1, &val); break; case ERR_ASSIGN: copyvalue(v2, &tmp); tmp.v_subtype |= v1->v_subtype; freevalue(v1); *v1 = tmp; val.v_type = V_NULL; break; default: math_error("Function \"%s\" is undefined", namefunc(index)); /*NOTREACHED*/ } return val; } switch (oip->args) { case 0: break; case 1: ++stack; stack->v_addr = v1; stack->v_type = V_ADDR; break; case 2: ++stack; stack->v_addr = v1; stack->v_type = V_ADDR; ++stack; stack->v_addr = v2; stack->v_type = V_ADDR; break; case 3: ++stack; stack->v_addr = v1; stack->v_type = V_ADDR; ++stack; stack->v_addr = v2; stack->v_type = V_ADDR; ++stack; stack->v_addr = v3; stack->v_type = V_ADDR; break; default: math_error("Bad number of args to calculate"); /*NOTREACHED*/ } calculate(fp, oip->args); switch (oip->retval) { case A_VALUE: return *stack--; case A_UNDEF: freevalue(stack--); val.v_type = V_NULL; break; case A_INT: if ((stack->v_type != V_NUM) || qisfrac(stack->v_num)) { math_error("Integer return value required"); /*NOTREACHED*/ } index = qtoi(stack->v_num); qfree(stack->v_num); stack--; val.v_type = V_INT; val.v_int = index; break; default: math_error("Bad object return"); /*NOTREACHED*/ } return val; }
// 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. static int32 gentraceback(byte *pc0, byte *sp, 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; pc = (uintptr)pc0; lr = 0; fp = nil; // 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 { // Print during crash. // main+0xf /home/rsc/go/src/runtime/x.go:23 // main(0x1, 0x2, 0x3) runtime·printf("%S", f->name); if(pc > f->entry) runtime·printf("+%p", (uintptr)(pc - f->entry)); tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry) tracepc--; runtime·printf(" %S:%d\n", f->src, runtime·funcline(f, tracepc)); runtime·printf("\t%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"); n++; } 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; } if(pcbuf == nil && (pc = g->gopc) != 0 && (f = runtime·findfunc(pc)) != nil) { runtime·printf("----- goroutine created by -----\n%S", f->name); if(pc > f->entry) runtime·printf("+%p", (uintptr)(pc - f->entry)); tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry) tracepc--; runtime·printf(" %S:%d\n", f->src, runtime·funcline(f, tracepc)); } 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, 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; }
static int comexec(struct op *t, struct tbl * volatile tp, const char **ap, volatile int flags, volatile int *xerrok) { int i; volatile int rv = 0; const char *cp; const char **lastp; /* Must be static (XXX but why?) */ static struct op texec; int type_flags; bool resetspec; int fcflags = FC_BI|FC_FUNC|FC_PATH; struct block *l_expand, *l_assign; int optc; const char *exec_argv0 = NULL; bool exec_clrenv = false; /* snag the last argument for $_ */ if (Flag(FTALKING) && *(lastp = ap)) { /* * XXX not the same as AT&T ksh, which only seems to set $_ * after a newline (but not in functions/dot scripts, but in * interactive and script) - perhaps save last arg here and * set it in shell()?. */ while (*++lastp) ; /* setstr() can't fail here */ setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp, KSH_RETURN_ERROR); } /** * Deal with the shell builtins builtin, exec and command since * they can be followed by other commands. This must be done before * we know if we should create a local block which must be done * before we can do a path search (in case the assignments change * PATH). * Odd cases: * FOO=bar exec >/dev/null FOO is kept but not exported * FOO=bar exec foobar FOO is exported * FOO=bar command exec >/dev/null FOO is neither kept nor exported * FOO=bar command FOO is neither kept nor exported * PATH=... foobar use new PATH in foobar search */ resetspec = false; while (tp && tp->type == CSHELL) { /* undo effects of command */ fcflags = FC_BI|FC_FUNC|FC_PATH; if (tp->val.f == c_builtin) { if ((cp = *++ap) == NULL || (!strcmp(cp, "--") && (cp = *++ap) == NULL)) { tp = NULL; break; } if ((tp = findcom(cp, FC_BI)) == NULL) errorf("%s: %s: %s", Tbuiltin, cp, "not a builtin"); if (tp->type == CSHELL && (tp->val.f == c_cat #ifdef MKSH_PRINTF_BUILTIN || tp->val.f == c_printf #endif )) break; continue; } else if (tp->val.f == c_exec) { if (ap[1] == NULL) break; ksh_getopt_reset(&builtin_opt, GF_ERROR); while ((optc = ksh_getopt(ap, &builtin_opt, "a:c")) != -1) switch (optc) { case 'a': exec_argv0 = builtin_opt.optarg; break; case 'c': exec_clrenv = true; /* ensure we can actually do this */ resetspec = true; break; default: rv = 2; goto Leave; } ap += builtin_opt.optind; flags |= XEXEC; } else if (tp->val.f == c_command) { bool saw_p = false; /* * Ugly dealing with options in two places (here * and in c_command(), but such is life) */ ksh_getopt_reset(&builtin_opt, 0); while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p') saw_p = true; if (optc != -1) /* command -vV or something */ break; /* don't look for functions */ fcflags = FC_BI|FC_PATH; if (saw_p) { if (Flag(FRESTRICTED)) { warningf(true, "%s: %s", "command -p", "restricted"); rv = 1; goto Leave; } fcflags |= FC_DEFPATH; } ap += builtin_opt.optind; /* * POSIX says special builtins lose their status * if accessed using command. */ resetspec = true; if (!ap[0]) { /* ensure command with no args exits with 0 */ subst_exstat = 0; break; } } else if (tp->val.f == c_cat) { /* if we have any flags, do not use the builtin */ if (ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' && /* argument, begins with -, is not - or -- */ (ap[1][1] != '-' || ap[1][2] != '\0')) { struct tbl *ext_cat; ext_cat = findcom(Tcat, FC_PATH | FC_FUNC); if (ext_cat && (ext_cat->type != CTALIAS || (ext_cat->flag & ISSET))) tp = ext_cat; } break; #ifdef MKSH_PRINTF_BUILTIN } else if (tp->val.f == c_printf) { struct tbl *ext_printf; ext_printf = findcom(Tprintf, FC_PATH | FC_FUNC); if (ext_printf && (ext_printf->type != CTALIAS || (ext_printf->flag & ISSET))) tp = ext_printf; break; #endif } else if (tp->val.f == c_trap) { t->u.evalflags &= ~DOTCOMEXEC; break; } else break; tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC)); } if (t->u.evalflags & DOTCOMEXEC) flags |= XEXEC; l_expand = e->loc; if (!resetspec && (!ap[0] || (tp && (tp->flag & KEEPASN)))) type_flags = 0; else { /* create new variable/function block */ newblock(); /* ksh functions don't keep assignments, POSIX functions do. */ if (!resetspec && tp && tp->type == CFUNC && !(tp->flag & FKSH)) type_flags = EXPORT; else type_flags = LOCAL|LOCAL_COPY|EXPORT; } l_assign = e->loc; if (exec_clrenv) l_assign->flags |= BF_STOPENV; if (Flag(FEXPORT)) type_flags |= EXPORT; if (Flag(FXTRACE)) change_xtrace(2, false); for (i = 0; t->vars[i]; i++) { /* do NOT lookup in the new var/fn block just created */ e->loc = l_expand; cp = evalstr(t->vars[i], DOASNTILDE | DOSCALAR); e->loc = l_assign; if (Flag(FXTRACE)) { const char *ccp; ccp = skip_varname(cp, true); if (*ccp == '+') ++ccp; if (*ccp == '=') ++ccp; shf_write(cp, ccp - cp, shl_xtrace); print_value_quoted(shl_xtrace, ccp); shf_putc(' ', shl_xtrace); } /* but assign in there as usual */ typeset(cp, type_flags, 0, 0, 0); } if (Flag(FXTRACE)) { change_xtrace(2, false); if (ap[rv = 0]) { xtrace_ap_loop: print_value_quoted(shl_xtrace, ap[rv]); if (ap[++rv]) { shf_putc(' ', shl_xtrace); goto xtrace_ap_loop; } } change_xtrace(1, false); } if ((cp = *ap) == NULL) { rv = subst_exstat; goto Leave; } else if (!tp) { if (Flag(FRESTRICTED) && vstrchr(cp, '/')) { warningf(true, "%s: %s", cp, "restricted"); rv = 1; goto Leave; } tp = findcom(cp, fcflags); } switch (tp->type) { /* shell built-in */ case CSHELL: do_call_builtin: rv = call_builtin(tp, (const char **)ap, null, resetspec); if (resetspec && tp->val.f == c_shift) { l_expand->argc = l_assign->argc; l_expand->argv = l_assign->argv; } break; /* function call */ case CFUNC: { volatile uint32_t old_inuse; const char * volatile old_kshname; volatile uint8_t old_flags[FNFLAGS]; if (!(tp->flag & ISSET)) { struct tbl *ftp; if (!tp->u.fpath) { rv = (tp->u2.errnov == ENOENT) ? 127 : 126; warningf(true, "%s: %s %s: %s", cp, "can't find", "function definition file", cstrerror(tp->u2.errnov)); break; } if (include(tp->u.fpath, 0, NULL, false) < 0) { if (!strcmp(cp, Tcat)) { no_cat_in_FPATH: tp = findcom(Tcat, FC_BI); goto do_call_builtin; } #ifdef MKSH_PRINTF_BUILTIN if (!strcmp(cp, Tprintf)) { no_printf_in_FPATH: tp = findcom(Tprintf, FC_BI); goto do_call_builtin; } #endif warningf(true, "%s: %s %s %s: %s", cp, "can't open", "function definition file", tp->u.fpath, cstrerror(errno)); rv = 127; break; } if (!(ftp = findfunc(cp, hash(cp), false)) || !(ftp->flag & ISSET)) { if (!strcmp(cp, Tcat)) goto no_cat_in_FPATH; #ifdef MKSH_PRINTF_BUILTIN if (!strcmp(cp, Tprintf)) goto no_printf_in_FPATH; #endif warningf(true, "%s: %s %s", cp, "function not defined by", tp->u.fpath); rv = 127; break; } tp = ftp; } /* * ksh functions set $0 to function name, POSIX * functions leave $0 unchanged. */ old_kshname = kshname; if (tp->flag & FKSH) kshname = ap[0]; else ap[0] = kshname; e->loc->argv = ap; for (i = 0; *ap++ != NULL; i++) ; e->loc->argc = i - 1; /* * ksh-style functions handle getopts sanely, * Bourne/POSIX functions are insane... */ if (tp->flag & FKSH) { e->loc->flags |= BF_DOGETOPTS; e->loc->getopts_state = user_opt; getopts_reset(1); } for (type_flags = 0; type_flags < FNFLAGS; ++type_flags) old_flags[type_flags] = shell_flags[type_flags]; change_xtrace((Flag(FXTRACEREC) ? Flag(FXTRACE) : 0) | ((tp->flag & TRACE) ? 1 : 0), false); old_inuse = tp->flag & FINUSE; tp->flag |= FINUSE; e->type = E_FUNC; if (!(i = kshsetjmp(e->jbuf))) { execute(tp->val.t, flags & XERROK, NULL); i = LRETURN; } kshname = old_kshname; change_xtrace(old_flags[(int)FXTRACE], false); #ifndef MKSH_LEGACY_MODE if (tp->flag & FKSH) { /* Korn style functions restore Flags on return */ old_flags[(int)FXTRACE] = Flag(FXTRACE); for (type_flags = 0; type_flags < FNFLAGS; ++type_flags) shell_flags[type_flags] = old_flags[type_flags]; } #endif tp->flag = (tp->flag & ~FINUSE) | old_inuse; /* * Were we deleted while executing? If so, free the * execution tree. */ if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) { if (tp->flag & ALLOC) { tp->flag &= ~ALLOC; tfree(tp->val.t, tp->areap); } tp->flag = 0; } switch (i) { case LRETURN: case LERROR: rv = exstat & 0xFF; break; case LINTR: case LEXIT: case LLEAVE: case LSHELL: quitenv(NULL); unwind(i); /* NOTREACHED */ default: quitenv(NULL); internal_errorf("%s %d", "CFUNC", i); } break; } /* executable command */ case CEXEC: /* tracked alias */ case CTALIAS: if (!(tp->flag&ISSET)) { if (tp->u2.errnov == ENOENT) { rv = 127; warningf(true, "%s: %s", cp, "not found"); } else { rv = 126; warningf(true, "%s: %s: %s", cp, "can't execute", cstrerror(tp->u2.errnov)); } break; } /* set $_ to program's full path */ /* setstr() can't fail here */ setstr(typeset("_", LOCAL | EXPORT, 0, INTEGER, 0), tp->val.s, KSH_RETURN_ERROR); /* to fork, we set up a TEXEC node and call execute */ texec.type = TEXEC; /* for vistree/dumptree */ texec.left = t; texec.str = tp->val.s; texec.args = ap; /* in this case we do not fork, of course */ if (flags & XEXEC) { if (exec_argv0) texec.args[0] = exec_argv0; j_exit(); if (!(flags & XBGND) #ifndef MKSH_UNEMPLOYED || Flag(FMONITOR) #endif ) { setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG); setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG); } } rv = exchild(&texec, flags, xerrok, -1); break; } Leave: if (flags & XEXEC) { exstat = rv & 0xFF; unwind(LLEAVE); } return (rv); }
// If the top segment of the stack contains an uncopyable // frame, return -1. Otherwise return the number of frames // in the top segment, all of which are copyable. static int32 copyabletopsegment(G *gp) { CopyableInfo cinfo; Defer *d; Func *f; FuncVal *fn; StackMap *stackmap; cinfo.stk = (byte*)gp->stackguard - StackGuard; cinfo.base = (byte*)gp->stackbase + sizeof(Stktop); cinfo.frames = 0; // Check that each frame is copyable. As a side effect, // count the frames. runtime·gentraceback(~(uintptr)0, ~(uintptr)0, 0, gp, 0, nil, 0x7fffffff, checkframecopy, &cinfo, false); if(StackDebug >= 1 && cinfo.frames != -1) runtime·printf("copystack: %d copyable frames\n", cinfo.frames); // Check to make sure all Defers are copyable for(d = gp->defer; d != nil; d = d->link) { if(cinfo.stk <= (byte*)d && (byte*)d < cinfo.base) { // Defer is on the stack. Its copyableness has // been established during stack walking. // For now, this only happens with the Defer in runtime.main. continue; } if(d->argp < cinfo.stk || cinfo.base <= d->argp) break; // a defer for the next segment fn = d->fn; if(fn == nil) // See issue 8047 continue; f = runtime·findfunc((uintptr)fn->fn); if(f == nil) return -1; // Check to make sure we have an args pointer map for the defer's args. // We only need the args map, but we check // for the locals map also, because when the locals map // isn't provided it means the ptr map came from C and // C (particularly, cgo) lies to us. See issue 7695. stackmap = runtime·funcdata(f, FUNCDATA_ArgsPointerMaps); if(stackmap == nil || stackmap->n <= 0) return -1; stackmap = runtime·funcdata(f, FUNCDATA_LocalsPointerMaps); if(stackmap == nil || stackmap->n <= 0) return -1; if(cinfo.stk <= (byte*)fn && (byte*)fn < cinfo.base) { // FuncVal is on the stack. Again, its copyableness // was established during stack walking. continue; } // The FuncVal may have pointers in it, but fortunately for us // the compiler won't put pointers into the stack in a // heap-allocated FuncVal. // One day if we do need to check this, we'll need maps of the // pointerness of the closure args. The only place we have that map // right now is in the gc program for the FuncVal. Ugh. } return cinfo.frames; }
void runtime·FuncForPC(uintptr pc, void *retf) { retf = runtime·findfunc(pc); FLUSH(&retf); }
// 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; }
void BXmistags() { macro m("BXmistags_redoana",true); findfunc(); // run(); }
static int comexec(struct op *t, struct tbl * volatile tp, const char **ap, volatile int flags, volatile int *xerrok) { int i; volatile int rv = 0; const char *cp; const char **lastp; /* Must be static (XXX but why?) */ static struct op texec; int type_flags; bool keepasn_ok; int fcflags = FC_BI|FC_FUNC|FC_PATH; bool bourne_function_call = false; struct block *l_expand, *l_assign; /* * snag the last argument for $_ XXX not the same as AT&T ksh, * which only seems to set $_ after a newline (but not in * functions/dot scripts, but in interactive and script) - * perhaps save last arg here and set it in shell()?. */ if (Flag(FTALKING) && *(lastp = ap)) { while (*++lastp) ; /* setstr() can't fail here */ setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp, KSH_RETURN_ERROR); } /** * Deal with the shell builtins builtin, exec and command since * they can be followed by other commands. This must be done before * we know if we should create a local block which must be done * before we can do a path search (in case the assignments change * PATH). * Odd cases: * FOO=bar exec >/dev/null FOO is kept but not exported * FOO=bar exec foobar FOO is exported * FOO=bar command exec >/dev/null FOO is neither kept nor exported * FOO=bar command FOO is neither kept nor exported * PATH=... foobar use new PATH in foobar search */ keepasn_ok = true; while (tp && tp->type == CSHELL) { /* undo effects of command */ fcflags = FC_BI|FC_FUNC|FC_PATH; if (tp->val.f == c_builtin) { if ((cp = *++ap) == NULL || (!strcmp(cp, "--") && (cp = *++ap) == NULL)) { tp = NULL; break; } if ((tp = findcom(cp, FC_BI)) == NULL) errorf("%s: %s: %s", Tbuiltin, cp, "not a builtin"); continue; } else if (tp->val.f == c_exec) { if (ap[1] == NULL) break; ap++; flags |= XEXEC; } else if (tp->val.f == c_command) { int optc, saw_p = 0; /* * Ugly dealing with options in two places (here * and in c_command(), but such is life) */ ksh_getopt_reset(&builtin_opt, 0); while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p') saw_p = 1; if (optc != EOF) /* command -vV or something */ break; /* don't look for functions */ fcflags = FC_BI|FC_PATH; if (saw_p) { if (Flag(FRESTRICTED)) { warningf(true, "%s: %s", "command -p", "restricted"); rv = 1; goto Leave; } fcflags |= FC_DEFPATH; } ap += builtin_opt.optind; /* * POSIX says special builtins lose their status * if accessed using command. */ keepasn_ok = false; if (!ap[0]) { /* ensure command with no args exits with 0 */ subst_exstat = 0; break; } #ifndef MKSH_NO_EXTERNAL_CAT } else if (tp->val.f == c_cat) { /* * if we have any flags, do not use the builtin * in theory, we could allow -u, but that would * mean to use ksh_getopt here and possibly ad- * ded complexity and more code and isn't worth * additional hassle (and the builtin must call * ksh_getopt already but can't come back here) */ if (ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' && /* argument, begins with -, is not - or -- */ (ap[1][1] != '-' || ap[1][2] != '\0')) /* don't look for builtins or functions */ fcflags = FC_PATH; else /* go on, use the builtin */ break; #endif #if !defined(MKSH_SMALL) } else if (tp->val.f == c_trap) { t->u.evalflags &= ~DOTCOMEXEC; break; #endif } else break; tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC)); } #if !defined(MKSH_SMALL) if (t->u.evalflags & DOTCOMEXEC) flags |= XEXEC; #endif l_expand = e->loc; if (keepasn_ok && (!ap[0] || (tp && (tp->flag & KEEPASN)))) type_flags = 0; else { /* create new variable/function block */ newblock(); /* ksh functions don't keep assignments, POSIX functions do. */ if (keepasn_ok && tp && tp->type == CFUNC && !(tp->flag & FKSH)) { bourne_function_call = true; type_flags = EXPORT; } else type_flags = LOCAL|LOCAL_COPY|EXPORT; } l_assign = e->loc; if (Flag(FEXPORT)) type_flags |= EXPORT; for (i = 0; t->vars[i]; i++) { /* do NOT lookup in the new var/fn block just created */ e->loc = l_expand; cp = evalstr(t->vars[i], DOASNTILDE); e->loc = l_assign; /* but assign in there as usual */ if (Flag(FXTRACE)) { if (i == 0) shf_puts(substitute(str_val(global("PS4")), 0), shl_out); shf_fprintf(shl_out, "%s%c", cp, t->vars[i + 1] ? ' ' : '\n'); if (!t->vars[i + 1]) shf_flush(shl_out); } typeset(cp, type_flags, 0, 0, 0); if (bourne_function_call && !(type_flags & EXPORT)) typeset(cp, LOCAL|LOCAL_COPY|EXPORT, 0, 0, 0); } if ((cp = *ap) == NULL) { rv = subst_exstat; goto Leave; } else if (!tp) { if (Flag(FRESTRICTED) && vstrchr(cp, '/')) { warningf(true, "%s: %s", cp, "restricted"); rv = 1; goto Leave; } tp = findcom(cp, fcflags); } switch (tp->type) { /* shell built-in */ case CSHELL: rv = call_builtin(tp, (const char **)ap, null); if (!keepasn_ok && tp->val.f == c_shift) { l_expand->argc = l_assign->argc; l_expand->argv = l_assign->argv; } break; /* function call */ case CFUNC: { volatile unsigned char old_xflag; volatile uint32_t old_inuse; const char * volatile old_kshname; if (!(tp->flag & ISSET)) { struct tbl *ftp; if (!tp->u.fpath) { rv = (tp->u2.errnov == ENOENT) ? 127 : 126; warningf(true, "%s: %s %s: %s", cp, "can't find", "function definition file", cstrerror(tp->u2.errnov)); break; } if (include(tp->u.fpath, 0, NULL, false) < 0) { rv = errno; warningf(true, "%s: %s %s %s: %s", cp, "can't open", "function definition file", tp->u.fpath, cstrerror(rv)); rv = 127; break; } if (!(ftp = findfunc(cp, hash(cp), false)) || !(ftp->flag & ISSET)) { warningf(true, "%s: %s %s", cp, "function not defined by", tp->u.fpath); rv = 127; break; } tp = ftp; } /* * ksh functions set $0 to function name, POSIX * functions leave $0 unchanged. */ old_kshname = kshname; if (tp->flag & FKSH) kshname = ap[0]; else ap[0] = kshname; e->loc->argv = ap; for (i = 0; *ap++ != NULL; i++) ; e->loc->argc = i - 1; /* * ksh-style functions handle getopts sanely, * Bourne/POSIX functions are insane... */ if (tp->flag & FKSH) { e->loc->flags |= BF_DOGETOPTS; e->loc->getopts_state = user_opt; getopts_reset(1); } old_xflag = Flag(FXTRACE); Flag(FXTRACE) |= tp->flag & TRACE ? 1 : 0; old_inuse = tp->flag & FINUSE; tp->flag |= FINUSE; e->type = E_FUNC; if (!(i = kshsetjmp(e->jbuf))) { execute(tp->val.t, flags & XERROK, NULL); i = LRETURN; } kshname = old_kshname; Flag(FXTRACE) = old_xflag; tp->flag = (tp->flag & ~FINUSE) | old_inuse; /* * Were we deleted while executing? If so, free the * execution tree. TODO: Unfortunately, the table entry * is never re-used until the lookup table is expanded. */ if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) { if (tp->flag & ALLOC) { tp->flag &= ~ALLOC; tfree(tp->val.t, tp->areap); } tp->flag = 0; } switch (i) { case LRETURN: case LERROR: rv = exstat & 0xFF; break; case LINTR: case LEXIT: case LLEAVE: case LSHELL: quitenv(NULL); unwind(i); /* NOTREACHED */ default: quitenv(NULL); internal_errorf("%s %d", "CFUNC", i); } break; } /* executable command */ case CEXEC: /* tracked alias */ case CTALIAS: if (!(tp->flag&ISSET)) { if (tp->u2.errnov == ENOENT) { rv = 127; warningf(true, "%s: %s", cp, "not found"); } else { rv = 126; warningf(true, "%s: %s: %s", cp, "can't execute", cstrerror(tp->u2.errnov)); } break; } /* set $_ to programme's full path */ /* setstr() can't fail here */ setstr(typeset("_", LOCAL|EXPORT, 0, INTEGER, 0), tp->val.s, KSH_RETURN_ERROR); if (flags&XEXEC) { j_exit(); if (!(flags&XBGND) #ifndef MKSH_UNEMPLOYED || Flag(FMONITOR) #endif ) { setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG); setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG); } } /* to fork we set up a TEXEC node and call execute */ texec.type = TEXEC; /* for tprint */ texec.left = t; texec.str = tp->val.s; texec.args = ap; rv = exchild(&texec, flags, xerrok, -1); break; } Leave: if (flags & XEXEC) { exstat = rv & 0xFF; unwind(LLEAVE); } return (rv); }
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; }
static int32 gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 m) { byte *p; int32 i, n, iter; uintptr pc, lr, tracepc; Stktop *stk; Func *f; pc = (uintptr)pc0; lr = (uintptr)lr0; // 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 < m; iter++) { // iter avoids looping forever if(pc == (uintptr)·lessstack) { // Hit top of stack segment. Unwind to next segment. pc = (uintptr)stk->gobuf.pc; sp = stk->gobuf.sp; lr = *(uintptr*)sp; stk = (Stktop*)stk->stackbase; continue; } if(pc <= 0x1000 || (f = findfunc(pc-4)) == nil) { // TODO: Check for closure. break; } // Found an actual function worth reporting. if(skip > 0) skip--; else if(pcbuf != nil) pcbuf[n++] = pc; else { // Print during crash. // main+0xf /home/rsc/go/src/runtime/x.go:23 // main(0x1, 0x2, 0x3) printf("%S", f->name); if(pc > f->entry) printf("+%p", (uintptr)(pc - f->entry)); tracepc = pc; // back up to CALL instruction for funcline. if(n > 0 && pc > f->entry) tracepc -= sizeof(uintptr); printf(" %S:%d\n", f->src, funcline(f, tracepc)); printf("\t%S(", f->name); for(i = 0; i < f->args; i++) { if(i != 0) prints(", "); ·printhex(((uintptr*)sp)[1+i]); if(i >= 4) { prints(", ..."); break; } } prints(")\n"); n++; } if(lr == 0) lr = *(uintptr*)sp; pc = lr; lr = 0; if(f->frame >= 0) sp += f->frame; } return n; }