Home | History | Annotate | Download | only in runtime
      1 // Copyright 2010 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 package runtime
      6 
      7 import (
      8 	"runtime/internal/sys"
      9 	"unsafe"
     10 )
     11 
     12 // May run during STW, so write barriers are not allowed.
     13 //
     14 //go:nowritebarrierrec
     15 func sighandler(_ureg *ureg, note *byte, gp *g) int {
     16 	_g_ := getg()
     17 	var t sigTabT
     18 	var docrash bool
     19 	var sig int
     20 	var flags int
     21 	var level int32
     22 
     23 	c := &sigctxt{_ureg}
     24 	notestr := gostringnocopy(note)
     25 
     26 	// The kernel will never pass us a nil note or ureg so we probably
     27 	// made a mistake somewhere in sigtramp.
     28 	if _ureg == nil || note == nil {
     29 		print("sighandler: ureg ", _ureg, " note ", note, "\n")
     30 		goto Throw
     31 	}
     32 	// Check that the note is no more than ERRMAX bytes (including
     33 	// the trailing NUL). We should never receive a longer note.
     34 	if len(notestr) > _ERRMAX-1 {
     35 		print("sighandler: note is longer than ERRMAX\n")
     36 		goto Throw
     37 	}
     38 	// See if the note matches one of the patterns in sigtab.
     39 	// Notes that do not match any pattern can be handled at a higher
     40 	// level by the program but will otherwise be ignored.
     41 	flags = _SigNotify
     42 	for sig, t = range sigtable {
     43 		if hasprefix(notestr, t.name) {
     44 			flags = t.flags
     45 			break
     46 		}
     47 	}
     48 	if flags&_SigPanic != 0 && gp.throwsplit {
     49 		// We can't safely sigpanic because it may grow the
     50 		// stack. Abort in the signal handler instead.
     51 		flags = (flags &^ _SigPanic) | _SigThrow
     52 	}
     53 	if flags&_SigGoExit != 0 {
     54 		exits((*byte)(add(unsafe.Pointer(note), 9))) // Strip "go: exit " prefix.
     55 	}
     56 	if flags&_SigPanic != 0 {
     57 		// Copy the error string from sigtramp's stack into m->notesig so
     58 		// we can reliably access it from the panic routines.
     59 		memmove(unsafe.Pointer(_g_.m.notesig), unsafe.Pointer(note), uintptr(len(notestr)+1))
     60 		gp.sig = uint32(sig)
     61 		gp.sigpc = c.pc()
     62 
     63 		pc := c.pc()
     64 		sp := c.sp()
     65 
     66 		// If we don't recognize the PC as code
     67 		// but we do recognize the top pointer on the stack as code,
     68 		// then assume this was a call to non-code and treat like
     69 		// pc == 0, to make unwinding show the context.
     70 		if pc != 0 && !findfunc(pc).valid() && findfunc(*(*uintptr)(unsafe.Pointer(sp))).valid() {
     71 			pc = 0
     72 		}
     73 
     74 		// IF LR exists, sigpanictramp must save it to the stack
     75 		// before entry to sigpanic so that panics in leaf
     76 		// functions are correctly handled. This will smash
     77 		// the stack frame but we're not going back there
     78 		// anyway.
     79 		if usesLR {
     80 			c.savelr(c.lr())
     81 		}
     82 
     83 		// If PC == 0, probably panicked because of a call to a nil func.
     84 		// Not faking that as the return address will make the trace look like a call
     85 		// to sigpanic instead. (Otherwise the trace will end at
     86 		// sigpanic and we won't get to see who faulted).
     87 		if pc != 0 {
     88 			if usesLR {
     89 				c.setlr(pc)
     90 			} else {
     91 				if sys.RegSize > sys.PtrSize {
     92 					sp -= sys.PtrSize
     93 					*(*uintptr)(unsafe.Pointer(sp)) = 0
     94 				}
     95 				sp -= sys.PtrSize
     96 				*(*uintptr)(unsafe.Pointer(sp)) = pc
     97 				c.setsp(sp)
     98 			}
     99 		}
    100 		if usesLR {
    101 			c.setpc(funcPC(sigpanictramp))
    102 		} else {
    103 			c.setpc(funcPC(sigpanic))
    104 		}
    105 		return _NCONT
    106 	}
    107 	if flags&_SigNotify != 0 {
    108 		if ignoredNote(note) {
    109 			return _NCONT
    110 		}
    111 		if sendNote(note) {
    112 			return _NCONT
    113 		}
    114 	}
    115 	if flags&_SigKill != 0 {
    116 		goto Exit
    117 	}
    118 	if flags&_SigThrow == 0 {
    119 		return _NCONT
    120 	}
    121 Throw:
    122 	_g_.m.throwing = 1
    123 	_g_.m.caughtsig.set(gp)
    124 	startpanic()
    125 	print(notestr, "\n")
    126 	print("PC=", hex(c.pc()), "\n")
    127 	print("\n")
    128 	level, _, docrash = gotraceback()
    129 	if level > 0 {
    130 		goroutineheader(gp)
    131 		tracebacktrap(c.pc(), c.sp(), c.lr(), gp)
    132 		tracebackothers(gp)
    133 		print("\n")
    134 		dumpregs(_ureg)
    135 	}
    136 	if docrash {
    137 		crash()
    138 	}
    139 Exit:
    140 	goexitsall(note)
    141 	exits(note)
    142 	return _NDFLT // not reached
    143 }
    144 
    145 func sigenable(sig uint32) {
    146 }
    147 
    148 func sigdisable(sig uint32) {
    149 }
    150 
    151 func sigignore(sig uint32) {
    152 }
    153 
    154 func setProcessCPUProfiler(hz int32) {
    155 }
    156 
    157 func setThreadCPUProfiler(hz int32) {
    158 	// TODO: Enable profiling interrupts.
    159 	getg().m.profilehz = hz
    160 }
    161 
    162 // gsignalStack is unused on Plan 9.
    163 type gsignalStack struct{}
    164