github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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 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  		if(gp != m->g0 && gp != m->gsignal)
    43  			runtime·sigprof((byte*)SIG_EIP(info, ctxt), (byte*)SIG_ESP(info, ctxt), nil, gp);
    44  		return;
    45  	}
    46  
    47  	t = &runtime·sigtab[sig];
    48  	if(SIG_CODE0(info, ctxt) != SI_USER && (t->flags & SigPanic)) {
    49  		if(gp == nil || gp == m->g0)
    50  			goto Throw;
    51  
    52  		// Make it look like a call to the signal func.
    53  		// Have to pass arguments out of band since
    54  		// augmenting the stack frame would break
    55  		// the unwinding code.
    56  		gp->sig = sig;
    57  		gp->sigcode0 = SIG_CODE0(info, ctxt);
    58  		gp->sigcode1 = SIG_CODE1(info, ctxt);
    59  		gp->sigpc = SIG_EIP(info, ctxt);
    60  
    61  #ifdef GOOS_darwin
    62  		// Work around Leopard bug that doesn't set FPE_INTDIV.
    63  		// Look at instruction to see if it is a divide.
    64  		// Not necessary in Snow Leopard (si_code will be != 0).
    65  		if(sig == SIGFPE && gp->sigcode0 == 0) {
    66  			byte *pc;
    67  			pc = (byte*)gp->sigpc;
    68  			if(pc[0] == 0x66)	// 16-bit instruction prefix
    69  				pc++;
    70  			if(pc[0] == 0xF6 || pc[0] == 0xF7)
    71  				gp->sigcode0 = FPE_INTDIV;
    72  		}
    73  #endif
    74  
    75  		// Only push runtime·sigpanic if eip != 0.
    76  		// If eip == 0, probably panicked because of a
    77  		// call to a nil func.  Not pushing that onto sp will
    78  		// make the trace look like a call to runtime·sigpanic instead.
    79  		// (Otherwise the trace will end at runtime·sigpanic and we
    80  		// won't get to see who faulted.)
    81  		if(SIG_EIP(info, ctxt) != 0) {
    82  			sp = (uintptr*)SIG_ESP(info, ctxt);
    83  			*--sp = SIG_EIP(info, ctxt);
    84  			SIG_ESP(info, ctxt) = (uintptr)sp;
    85  		}
    86  		SIG_EIP(info, ctxt) = (uintptr)runtime·sigpanic;
    87  		return;
    88  	}
    89  
    90  	if(SIG_CODE0(info, ctxt) == SI_USER || (t->flags & SigNotify))
    91  		if(runtime·sigsend(sig))
    92  			return;
    93  	if(t->flags & SigKill)
    94  		runtime·exit(2);
    95  	if(!(t->flags & SigThrow))
    96  		return;
    97  
    98  Throw:
    99  	runtime·startpanic();
   100  
   101  	if(sig < 0 || sig >= NSIG)
   102  		runtime·printf("Signal %d\n", sig);
   103  	else
   104  		runtime·printf("%s\n", runtime·sigtab[sig].name);
   105  
   106  	runtime·printf("PC=%x\n", SIG_EIP(info, ctxt));
   107  	if(m->lockedg != nil && m->ncgo > 0 && gp == m->g0) {
   108  		runtime·printf("signal arrived during cgo execution\n");
   109  		gp = m->lockedg;
   110  	}	
   111  	runtime·printf("\n");
   112  
   113  	if(runtime·gotraceback(&crash)){
   114  		runtime·traceback((void*)SIG_EIP(info, ctxt), (void*)SIG_ESP(info, ctxt), 0, gp);
   115  		runtime·tracebackothers(gp);
   116  		runtime·dumpregs(info, ctxt);
   117  	}
   118  	
   119  	if(crash)
   120  		runtime·crash();
   121  
   122  	runtime·exit(2);
   123  }