// The implementation of the predeclared function panic. void runtime·panic(Eface e) { Defer *d; Panic *p; void *pc, *argp; p = runtime·mal(sizeof *p); p->arg = e; p->link = g->panic; p->stackbase = g->stackbase; g->panic = p; for(;;) { d = g->defer; if(d == nil) break; // take defer off list in case of recursive panic g->defer = d->link; g->ispanic = true; // rock for newstack, where reflect.newstackcall ends up argp = d->argp; pc = d->pc; runtime·newstackcall(d->fn, (byte*)d->args, d->siz); freedefer(d); if(p->recovered) { g->panic = p->link; if(g->panic == nil) // must be done with signal g->sig = 0; runtime·free(p); // Pass information about recovering frame to recovery. g->sigcode0 = (uintptr)argp; g->sigcode1 = (uintptr)pc; runtime·mcall(recovery); runtime·throw("recovery failed"); // mcall should not return } } // ran out of deferred calls - old-school panic now runtime·startpanic(); printpanics(g->panic); runtime·dopanic(0); }
// The implementation of the predeclared function panic. void runtime·panic(Eface e) { Defer *d; Panic *p; p = runtime·mal(sizeof *p); p->arg = e; p->link = g->panic; p->stackbase = g->stackbase; g->panic = p; for(;;) { d = g->defer; if(d == nil) break; // take defer off list in case of recursive panic g->defer = d->link; g->ispanic = true; // rock for newstack, where reflect.call ends up reflect·call(d->fn, d->args, d->siz); if(p->recovered) { g->panic = p->link; if(g->panic == nil) // must be done with signal g->sig = 0; runtime·free(p); // put recovering defer back on list // for scheduler to find. d->link = g->defer; g->defer = d; runtime·mcall(recovery); runtime·throw("recovery failed"); // mcall should not return } if(!d->nofree) runtime·free(d); } // ran out of deferred calls - old-school panic now runtime·startpanic(); printpanics(g->panic); runtime·dopanic(0); }
void runtime·sighandler(int32 sig, Siginfo *info, void *ctxt, G *gp) { uintptr *sp; SigTab *t; bool crash; if(sig == SIGPROF) { if(gp != m->g0 && gp != m->gsignal) runtime·sigprof((byte*)SIG_RIP(info, ctxt), (byte*)SIG_RSP(info, ctxt), nil, gp); return; } t = &runtime·sigtab[sig]; if(SIG_CODE0(info, ctxt) != SI_USER && (t->flags & SigPanic)) { if(gp == nil || gp == m->g0) goto Throw; // Make it look like a call to the signal func. // Have to pass arguments out of band since // augmenting the stack frame would break // the unwinding code. gp->sig = sig; gp->sigcode0 = SIG_CODE0(info, ctxt); gp->sigcode1 = SIG_CODE1(info, ctxt); gp->sigpc = SIG_RIP(info, ctxt); #ifdef GOOS_darwin // Work around Leopard bug that doesn't set FPE_INTDIV. // Look at instruction to see if it is a divide. // Not necessary in Snow Leopard (si_code will be != 0). if(sig == SIGFPE && gp->sigcode0 == 0) { byte *pc; pc = (byte*)gp->sigpc; if((pc[0]&0xF0) == 0x40) // 64-bit REX prefix pc++; else if(pc[0] == 0x66) // 16-bit instruction prefix pc++; if(pc[0] == 0xF6 || pc[0] == 0xF7) gp->sigcode0 = FPE_INTDIV; } #endif // Only push runtime·sigpanic if rip != 0. // If rip == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will // make the trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic and we // won't get to see who faulted.) if(SIG_RIP(info, ctxt) != 0) { sp = (uintptr*)SIG_RSP(info, ctxt); *--sp = SIG_RIP(info, ctxt); SIG_RSP(info, ctxt) = (uintptr)sp; } SIG_RIP(info, ctxt) = (uintptr)runtime·sigpanic; return; } if(SIG_CODE0(info, ctxt) == SI_USER || (t->flags & SigNotify)) if(runtime·sigsend(sig)) return; if(t->flags & SigKill) runtime·exit(2); if(!(t->flags & SigThrow)) return; Throw: m->throwing = 1; m->caughtsig = gp; runtime·startpanic(); if(sig < 0 || sig >= NSIG) runtime·printf("Signal %d\n", sig); else runtime·printf("%s\n", runtime·sigtab[sig].name); runtime·printf("PC=%X\n", SIG_RIP(info, ctxt)); if(m->lockedg != nil && m->ncgo > 0 && gp == m->g0) { runtime·printf("signal arrived during cgo execution\n"); gp = m->lockedg; } runtime·printf("\n"); if(runtime·gotraceback(&crash)){ runtime·traceback(SIG_RIP(info, ctxt), SIG_RSP(info, ctxt), 0, gp); runtime·tracebackothers(gp); runtime·printf("\n"); runtime·dumpregs(info, ctxt); } if(crash) runtime·crash(); runtime·exit(2); }
int32 runtime·sighandler(void *v, int8 *note, G *gp) { uintptr *sp; SigTab *t; bool crash; Ureg *ureg; intgo len, n; int32 sig, flags; ureg = (Ureg*)v; // The kernel will never pass us a nil note or ureg so we probably // made a mistake somewhere in runtime·sigtramp. if(ureg == nil || note == nil) { runtime·printf("sighandler: ureg %p note %p\n", ureg, note); goto Throw; } // Check that the note is no more than ERRMAX bytes (including // the trailing NUL). We should never receive a longer note. len = runtime·findnull((byte*)note); if(len > ERRMAX-1) { runtime·printf("sighandler: note is longer than ERRMAX\n"); goto Throw; } // See if the note matches one of the patterns in runtime·sigtab. // Notes that do not match any pattern can be handled at a higher // level by the program but will otherwise be ignored. flags = SigNotify; for(sig = 0; sig < nelem(runtime·sigtab); sig++) { t = &runtime·sigtab[sig]; n = runtime·findnull((byte*)t->name); if(len < n) continue; if(runtime·strncmp((byte*)note, (byte*)t->name, n) == 0) { flags = t->flags; break; } } if(flags & SigGoExit) runtime·exits(note+9); // Strip "go: exit " prefix. if(flags & SigPanic) { // Copy the error string from sigtramp's stack into m->notesig so // we can reliably access it from the panic routines. runtime·memmove(g->m->notesig, note, len+1); gp->sig = sig; gp->sigpc = ureg->ip; // Only push runtime·sigpanic if PC != 0. // // If PC == 0, probably panicked because of a call to a nil func. // Not pushing that onto SP will make the trace look like a call // to runtime·sigpanic instead. (Otherwise the trace will end at // runtime·sigpanic and we won't get to see who faulted). if(ureg->ip != 0) { sp = (uintptr*)ureg->sp; *--sp = ureg->ip; ureg->sp = (uint64)sp; } ureg->ip = (uintptr)runtime·sigpanic; return NCONT; } if(flags & SigNotify) { // TODO(ality): See if os/signal wants it. //if(runtime·sigsend(...)) // return NCONT; } if(flags & SigKill) goto Exit; if(!(flags & SigThrow)) return NCONT; Throw: g->m->throwing = 1; g->m->caughtsig = gp; runtime·startpanic(); runtime·printf("%s\n", note); runtime·printf("PC=%X\n", ureg->ip); runtime·printf("\n"); if(runtime·gotraceback(&crash)) { runtime·goroutineheader(gp); runtime·traceback(ureg->ip, ureg->sp, 0, gp); runtime·tracebackothers(gp); runtime·printf("\n"); runtime·dumpregs(ureg); } if(crash) runtime·crash(); Exit: runtime·goexitsall(note); runtime·exits(note); return NDFLT; // not reached }
void runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) { Ucontext *uc; Sigcontext *r; uintptr *sp; SigTab *t; uc = context; r = &uc->uc_mcontext; if(sig == SIGPROF) { runtime·sigprof((uint8*)r->eip, (uint8*)r->esp, nil, gp); return; } t = &runtime·sigtab[sig]; if(info->si_code != SI_USER && (t->flags & SigPanic)) { if(gp == nil) goto Throw; // Make it look like a call to the signal func. // Have to pass arguments out of band since // augmenting the stack frame would break // the unwinding code. gp->sig = sig; gp->sigcode0 = info->si_code; gp->sigcode1 = ((uintptr*)info)[3]; gp->sigpc = r->eip; // Only push runtime·sigpanic if r->eip != 0. // If r->eip == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will // make the trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic and we // won't get to see who faulted.) if(r->eip != 0) { sp = (uintptr*)r->esp; *--sp = r->eip; r->esp = (uintptr)sp; } r->eip = (uintptr)runtime·sigpanic; return; } if(info->si_code == SI_USER || (t->flags & SigNotify)) if(runtime·sigsend(sig)) return; if(t->flags & SigKill) runtime·exit(2); if(!(t->flags & SigThrow)) return; Throw: runtime·startpanic(); if(sig < 0 || sig >= NSIG) runtime·printf("Signal %d\n", sig); else runtime·printf("%s\n", runtime·sigtab[sig].name); runtime·printf("PC=%X\n", r->eip); runtime·printf("\n"); if(runtime·gotraceback()){ runtime·traceback((void*)r->eip, (void*)r->esp, 0, gp); runtime·tracebackothers(gp); runtime·dumpregs(r); } runtime·exit(2); }
void runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) { Ucontext *uc; Mcontext32 *mc; Regs32 *r; uintptr *sp; byte *pc; SigTab *t; uc = context; mc = uc->uc_mcontext; r = &mc->ss; if(sig == SIGPROF) { if(gp != m->g0 && gp != m->gsignal) runtime·sigprof((uint8*)r->eip, (uint8*)r->esp, nil, gp); return; } t = &runtime·sigtab[sig]; if(info->si_code != SI_USER && (t->flags & SigPanic)) { if(gp == nil || gp == m->g0) goto Throw; // Work around Leopard bug that doesn't set FPE_INTDIV. // Look at instruction to see if it is a divide. // Not necessary in Snow Leopard (si_code will be != 0). if(sig == SIGFPE && info->si_code == 0) { pc = (byte*)r->eip; if(pc[0] == 0x66) // 16-bit instruction prefix pc++; if(pc[0] == 0xF6 || pc[0] == 0xF7) info->si_code = FPE_INTDIV; } // Make it look like a call to the signal func. // Have to pass arguments out of band since // augmenting the stack frame would break // the unwinding code. gp->sig = sig; gp->sigcode0 = info->si_code; gp->sigcode1 = (uintptr)info->si_addr; gp->sigpc = r->eip; // Only push runtime·sigpanic if r->eip != 0. // If r->eip == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will // make the trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic and we // won't get to see who faulted.) if(r->eip != 0) { sp = (uintptr*)r->esp; *--sp = r->eip; r->esp = (uintptr)sp; } r->eip = (uintptr)runtime·sigpanic; return; } if(info->si_code == SI_USER || (t->flags & SigNotify)) if(runtime·sigsend(sig)) return; if(t->flags & SigKill) runtime·exit(2); if(!(t->flags & SigThrow)) return; Throw: runtime·startpanic(); if(sig < 0 || sig >= NSIG){ runtime·printf("Signal %d\n", sig); }else{ runtime·printf("%s\n", runtime·sigtab[sig].name); } runtime·printf("PC=%x\n", r->eip); if(m->lockedg != nil && m->ncgo > 0 && gp == m->g0) { runtime·printf("signal arrived during cgo execution\n"); gp = m->lockedg; } runtime·printf("\n"); if(runtime·gotraceback()){ runtime·traceback((void*)r->eip, (void*)r->esp, 0, gp); runtime·tracebackothers(gp); runtime·dumpregs(r); } runtime·exit(2); }
int32 runtime·sighandler(void *v, int8 *s, G *gp) { bool crash; Ureg *ureg; uintptr *sp; SigTab *sig, *nsig; intgo len, i; if(!s) return NCONT; len = runtime·findnull((byte*)s); if(len <= 4 || runtime·mcmp((byte*)s, (byte*)"sys:", 4) != 0) return NDFLT; nsig = nil; sig = runtime·sigtab; for(i=0; i < NSIG; i++) { if(runtime·strstr((byte*)s, (byte*)sig->name)) { nsig = sig; break; } sig++; } if(nsig == nil) return NDFLT; ureg = v; if(nsig->flags & SigPanic) { if(gp == nil || m->notesig == 0) goto Throw; // Copy the error string from sigtramp's stack into m->notesig so // we can reliably access it from the panic routines. We can't use // runtime·memmove here since it will use SSE instructions for big // copies. The Plan 9 kernel doesn't allow floating point in note // handlers. // // TODO(ality): revert back to memmove when the kernel is fixed. if(len >= ERRMAX) len = ERRMAX-1; for(i = 0; i < len; i++) m->notesig[i] = s[i]; m->notesig[i] = '\0'; gp->sig = i; gp->sigpc = ureg->pc; // Only push runtime·sigpanic if ureg->pc != 0. // If ureg->pc == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will // make the trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic and we // won't get to see who faulted.) if(ureg->pc != 0) { sp = (uintptr*)ureg->sp; *--sp = ureg->pc; ureg->sp = (uint32)sp; } ureg->pc = (uintptr)runtime·sigpanic; return NCONT; } if(!(nsig->flags & SigThrow)) return NDFLT; Throw: m->throwing = 1; m->caughtsig = gp; runtime·startpanic(); runtime·printf("%s\n", s); runtime·printf("PC=%X\n", ureg->pc); runtime·printf("\n"); if(runtime·gotraceback(&crash)) { runtime·traceback(ureg->pc, ureg->sp, 0, gp); runtime·tracebackothers(gp); runtime·dumpregs(ureg); } if(crash) runtime·crash(); runtime·goexitsall(""); runtime·exits(s); return 0; }
void runtime·sighandler(int32 sig, Siginfo *info, void *ctxt, G *gp) { uintptr *sp; SigTab *t; bool crash; if(sig == SIGPROF) { runtime·sigprof((byte*)SIG_RIP(info, ctxt), (byte*)SIG_RSP(info, ctxt), nil, gp, g->m); return; } #ifdef GOOS_darwin // x86-64 has 48-bit virtual addresses. The top 16 bits must echo bit 47. // The hardware delivers a different kind of fault for a malformed address // than it does for an attempt to access a valid but unmapped address. // OS X 10.9.2 mishandles the malformed address case, making it look like // a user-generated signal (like someone ran kill -SEGV ourpid). // We pass user-generated signals to os/signal, or else ignore them. // Doing that here - and returning to the faulting code - results in an // infinite loop. It appears the best we can do is rewrite what the kernel // delivers into something more like the truth. The address used below // has very little chance of being the one that caused the fault, but it is // malformed, it is clearly not a real pointer, and if it does get printed // in real life, people will probably search for it and find this code. // There are no Google hits for b01dfacedebac1e or 0xb01dfacedebac1e // as I type this comment. if(sig == SIGSEGV && SIG_CODE0(info, ctxt) == SI_USER) { SIG_CODE0(info, ctxt) = SI_USER+1; info->si_addr = (void*)(uintptr)0xb01dfacedebac1eULL; } #endif t = &runtime·sigtab[sig]; if(SIG_CODE0(info, ctxt) != SI_USER && (t->flags & SigPanic)) { // Make it look like a call to the signal func. // Have to pass arguments out of band since // augmenting the stack frame would break // the unwinding code. gp->sig = sig; gp->sigcode0 = SIG_CODE0(info, ctxt); gp->sigcode1 = SIG_CODE1(info, ctxt); gp->sigpc = SIG_RIP(info, ctxt); #ifdef GOOS_darwin // Work around Leopard bug that doesn't set FPE_INTDIV. // Look at instruction to see if it is a divide. // Not necessary in Snow Leopard (si_code will be != 0). if(sig == SIGFPE && gp->sigcode0 == 0) { byte *pc; pc = (byte*)gp->sigpc; if((pc[0]&0xF0) == 0x40) // 64-bit REX prefix pc++; else if(pc[0] == 0x66) // 16-bit instruction prefix pc++; if(pc[0] == 0xF6 || pc[0] == 0xF7) gp->sigcode0 = FPE_INTDIV; } #endif // Only push runtime·sigpanic if rip != 0. // If rip == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will // make the trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic and we // won't get to see who faulted.) if(SIG_RIP(info, ctxt) != 0) { sp = (uintptr*)SIG_RSP(info, ctxt); if(sizeof(uintreg) > sizeof(uintptr)) *--sp = 0; *--sp = SIG_RIP(info, ctxt); SIG_RSP(info, ctxt) = (uintptr)sp; } SIG_RIP(info, ctxt) = (uintptr)runtime·sigpanic; return; } if(SIG_CODE0(info, ctxt) == SI_USER || (t->flags & SigNotify)) if(runtime·sigsend(sig)) return; if(t->flags & SigKill) runtime·exit(2); if(!(t->flags & SigThrow)) return; g->m->throwing = 1; g->m->caughtsig = gp; runtime·startpanic(); if(sig < 0 || sig >= NSIG) runtime·printf("Signal %d\n", sig); else runtime·printf("%s\n", runtime·sigtab[sig].name); runtime·printf("PC=%X\n", SIG_RIP(info, ctxt)); if(g->m->lockedg != nil && g->m->ncgo > 0 && gp == g->m->g0) { runtime·printf("signal arrived during cgo execution\n"); gp = g->m->lockedg; } runtime·printf("\n"); if(runtime·gotraceback(&crash)){ runtime·goroutineheader(gp); runtime·traceback(SIG_RIP(info, ctxt), SIG_RSP(info, ctxt), 0, gp); runtime·tracebackothers(gp); runtime·printf("\n"); runtime·dumpregs(info, ctxt); } if(crash) runtime·crash(); runtime·exit(2); }
int32 runtime·sighandler(void *v, int8 *s, G *gp) { Ureg *ureg; uintptr *sp; SigTab *sig, *nsig; int32 len, i; if(!s) return NCONT; len = runtime·findnull((byte*)s); if(len <= 4 || runtime·mcmp((byte*)s, (byte*)"sys:", 4) != 0) return NDFLT; nsig = nil; sig = runtime·sigtab; for(i=0; i < NSIG; i++) { if(runtime·strstr((byte*)s, (byte*)sig->name)) { nsig = sig; break; } sig++; } if(nsig == nil) return NDFLT; ureg = v; if(nsig->flags & SigPanic) { if(gp == nil || m->notesig == 0) goto Throw; // Save error string from sigtramp's stack, // into gsignal->sigcode0, so we can reliably // access it from the panic routines. if(len > ERRMAX) len = ERRMAX; runtime·memmove((void*)m->notesig, (void*)s, len); gp->sig = i; gp->sigpc = ureg->pc; // Only push runtime·sigpanic if ureg->pc != 0. // If ureg->pc == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will // make the trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic and we // won't get to see who faulted.) if(ureg->pc != 0) { sp = (uintptr*)ureg->sp; *--sp = ureg->pc; ureg->sp = (uint32)sp; } ureg->pc = (uintptr)runtime·sigpanic; return NCONT; } if(!(nsig->flags & SigThrow)) return NDFLT; Throw: runtime·startpanic(); runtime·printf("%s\n", s); runtime·printf("PC=%X\n", ureg->pc); runtime·printf("\n"); if(runtime·gotraceback()) { runtime·traceback((void*)ureg->pc, (void*)ureg->sp, 0, gp); runtime·tracebackothers(gp); runtime·dumpregs(ureg); } runtime·goexitsall(""); runtime·exits(s); return 0; }
void runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) { UcontextT *uc = context; McontextT *mc = &uc->uc_mcontext; uintptr *sp; SigTab *t; if(sig == SIGPROF) { runtime·sigprof((uint8*)mc->__gregs[REG_EIP], (uint8*)mc->__gregs[REG_UESP], nil, gp); return; } t = &runtime·sigtab[sig]; if(info->_code != SI_USER && (t->flags & SigPanic)) { if(gp == nil || gp == m->g0) goto Throw; // Make it look like a call to the signal func. // We need to pass arguments out of band since // augmenting the stack frame would break // the unwinding code. gp->sig = sig; gp->sigcode0 = info->_code; gp->sigcode1 = *(uintptr*)&info->_reason[0]; /* _addr */ gp->sigpc = mc->__gregs[REG_EIP]; // Only push runtime·sigpanic if __gregs[REG_EIP] != 0. // If __gregs[REG_EIP] == 0, probably panicked because of a // call to a nil func. Not pushing that onto sp will make the // trace look like a call to runtime·sigpanic instead. // (Otherwise the trace will end at runtime·sigpanic // and we won't get to see who faulted.) if(mc->__gregs[REG_EIP] != 0) { sp = (uintptr*)mc->__gregs[REG_UESP]; *--sp = mc->__gregs[REG_EIP]; mc->__gregs[REG_UESP] = (uintptr)sp; } mc->__gregs[REG_EIP] = (uintptr)runtime·sigpanic; return; } if(info->_code == SI_USER || (t->flags & SigNotify)) if(runtime·sigsend(sig)) return; if(t->flags & SigKill) runtime·exit(2); if(!(t->flags & SigThrow)) return; Throw: runtime·startpanic(); if(sig < 0 || sig >= NSIG) runtime·printf("Signal %d\n", sig); else runtime·printf("%s\n", runtime·sigtab[sig].name); runtime·printf("PC=%X\n", mc->__gregs[REG_EIP]); if(m->lockedg != nil && m->ncgo > 0 && gp == m->g0) { runtime·printf("signal arrived during cgo execution\n"); gp = m->lockedg; } runtime·printf("\n"); if(runtime·gotraceback()){ runtime·traceback((void*)mc->__gregs[REG_EIP], (void*)mc->__gregs[REG_UESP], 0, gp); runtime·tracebackothers(gp); runtime·dumpregs(mc); } runtime·exit(2); }