github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/signal_386.c (about)

     1  // Copyright 2013 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux netbsd openbsd
     6  
     7  #include "runtime.h"
     8  #include "defs_GOOS_GOARCH.h"
     9  #include "os_GOOS.h"
    10  #include "signal_GOOS_GOARCH.h"
    11  #include "signals_GOOS.h"
    12  
    13  void
    14  runtime·dumpregs(Siginfo *info, void *ctxt)
    15  {
    16  	USED(info);
    17  	USED(ctxt);
    18  	
    19  	runtime·printf("eax     %x\n", SIG_EAX(info, ctxt));
    20  	runtime·printf("ebx     %x\n", SIG_EBX(info, ctxt));
    21  	runtime·printf("ecx     %x\n", SIG_ECX(info, ctxt));
    22  	runtime·printf("edx     %x\n", SIG_EDX(info, ctxt));
    23  	runtime·printf("edi     %x\n", SIG_EDI(info, ctxt));
    24  	runtime·printf("esi     %x\n", SIG_ESI(info, ctxt));
    25  	runtime·printf("ebp     %x\n", SIG_EBP(info, ctxt));
    26  	runtime·printf("esp     %x\n", SIG_ESP(info, ctxt));
    27  	runtime·printf("eip     %x\n", SIG_EIP(info, ctxt));
    28  	runtime·printf("eflags  %x\n", SIG_EFLAGS(info, ctxt));
    29  	runtime·printf("cs      %x\n", SIG_CS(info, ctxt));
    30  	runtime·printf("fs      %x\n", SIG_FS(info, ctxt));
    31  	runtime·printf("gs      %x\n", SIG_GS(info, ctxt));
    32  }
    33  
    34  void
    35  runtime·sighandler(int32 sig, Siginfo *info, void *ctxt, G *gp)
    36  {
    37  	uintptr *sp;
    38  	SigTab *t;
    39  	bool crash;
    40  
    41  	if(sig == SIGPROF) {
    42  		runtime·sigprof((byte*)SIG_EIP(info, ctxt), (byte*)SIG_ESP(info, ctxt), nil, gp);
    43  		return;
    44  	}
    45  
    46  	t = &runtime·sigtab[sig];
    47  	if(SIG_CODE0(info, ctxt) != SI_USER && (t->flags & SigPanic)) {
    48  		if(gp == nil || gp == m->g0)
    49  			goto Throw;
    50  
    51  		// Make it look like a call to the signal func.
    52  		// Have to pass arguments out of band since
    53  		// augmenting the stack frame would break
    54  		// the unwinding code.
    55  		gp->sig = sig;
    56  		gp->sigcode0 = SIG_CODE0(info, ctxt);
    57  		gp->sigcode1 = SIG_CODE1(info, ctxt);
    58  		gp->sigpc = SIG_EIP(info, ctxt);
    59  
    60  #ifdef GOOS_darwin
    61  		// Work around Leopard bug that doesn't set FPE_INTDIV.
    62  		// Look at instruction to see if it is a divide.
    63  		// Not necessary in Snow Leopard (si_code will be != 0).
    64  		if(sig == SIGFPE && gp->sigcode0 == 0) {
    65  			byte *pc;
    66  			pc = (byte*)gp->sigpc;
    67  			if(pc[0] == 0x66)	// 16-bit instruction prefix
    68  				pc++;
    69  			if(pc[0] == 0xF6 || pc[0] == 0xF7)
    70  				gp->sigcode0 = FPE_INTDIV;
    71  		}
    72  #endif
    73  
    74  		// Only push runtime·sigpanic if eip != 0.
    75  		// If eip == 0, probably panicked because of a
    76  		// call to a nil func.  Not pushing that onto sp will
    77  		// make the trace look like a call to runtime·sigpanic instead.
    78  		// (Otherwise the trace will end at runtime·sigpanic and we
    79  		// won't get to see who faulted.)
    80  		if(SIG_EIP(info, ctxt) != 0) {
    81  			sp = (uintptr*)SIG_ESP(info, ctxt);
    82  			*--sp = SIG_EIP(info, ctxt);
    83  			SIG_ESP(info, ctxt) = (uintptr)sp;
    84  		}
    85  		SIG_EIP(info, ctxt) = (uintptr)runtime·sigpanic;
    86  		return;
    87  	}
    88  
    89  	if(SIG_CODE0(info, ctxt) == SI_USER || (t->flags & SigNotify))
    90  		if(runtime·sigsend(sig))
    91  			return;
    92  	if(t->flags & SigKill)
    93  		runtime·exit(2);
    94  	if(!(t->flags & SigThrow))
    95  		return;
    96  
    97  Throw:
    98  	m->throwing = 1;
    99  	m->caughtsig = gp;
   100  	runtime·startpanic();
   101  
   102  	if(sig < 0 || sig >= NSIG)
   103  		runtime·printf("Signal %d\n", sig);
   104  	else
   105  		runtime·printf("%s\n", runtime·sigtab[sig].name);
   106  
   107  	runtime·printf("PC=%x\n", SIG_EIP(info, ctxt));
   108  	if(m->lockedg != nil && m->ncgo > 0 && gp == m->g0) {
   109  		runtime·printf("signal arrived during cgo execution\n");
   110  		gp = m->lockedg;
   111  	}	
   112  	runtime·printf("\n");
   113  
   114  	if(runtime·gotraceback(&crash)){
   115  		runtime·traceback(SIG_EIP(info, ctxt), SIG_ESP(info, ctxt), 0, gp);
   116  		runtime·tracebackothers(gp);
   117  		runtime·printf("\n");
   118  		runtime·dumpregs(info, ctxt);
   119  	}
   120  	
   121  	if(crash)
   122  		runtime·crash();
   123  
   124  	runtime·exit(2);
   125  }