Home | History | Annotate | Download | only in runtime
      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 package runtime
      6 
      7 import "unsafe"
      8 
      9 type sigctxt struct {
     10 	info *siginfo
     11 	ctxt unsafe.Pointer
     12 }
     13 
     14 //go:nosplit
     15 //go:nowritebarrierrec
     16 func (c *sigctxt) regs() *regs32 { return &(*ucontext)(c.ctxt).uc_mcontext.ss }
     17 
     18 func (c *sigctxt) eax() uint32 { return c.regs().eax }
     19 func (c *sigctxt) ebx() uint32 { return c.regs().ebx }
     20 func (c *sigctxt) ecx() uint32 { return c.regs().ecx }
     21 func (c *sigctxt) edx() uint32 { return c.regs().edx }
     22 func (c *sigctxt) edi() uint32 { return c.regs().edi }
     23 func (c *sigctxt) esi() uint32 { return c.regs().esi }
     24 func (c *sigctxt) ebp() uint32 { return c.regs().ebp }
     25 func (c *sigctxt) esp() uint32 { return c.regs().esp }
     26 
     27 //go:nosplit
     28 //go:nowritebarrierrec
     29 func (c *sigctxt) eip() uint32 { return c.regs().eip }
     30 
     31 func (c *sigctxt) eflags() uint32  { return c.regs().eflags }
     32 func (c *sigctxt) cs() uint32      { return c.regs().cs }
     33 func (c *sigctxt) fs() uint32      { return c.regs().fs }
     34 func (c *sigctxt) gs() uint32      { return c.regs().gs }
     35 func (c *sigctxt) sigcode() uint32 { return uint32(c.info.si_code) }
     36 func (c *sigctxt) sigaddr() uint32 { return c.info.si_addr }
     37 
     38 func (c *sigctxt) set_eip(x uint32)     { c.regs().eip = x }
     39 func (c *sigctxt) set_esp(x uint32)     { c.regs().esp = x }
     40 func (c *sigctxt) set_sigcode(x uint32) { c.info.si_code = int32(x) }
     41 func (c *sigctxt) set_sigaddr(x uint32) { c.info.si_addr = x }
     42 
     43 func (c *sigctxt) fixsigcode(sig uint32) {
     44 	switch sig {
     45 	case _SIGTRAP:
     46 		// OS X sets c.sigcode() == TRAP_BRKPT unconditionally for all SIGTRAPs,
     47 		// leaving no way to distinguish a breakpoint-induced SIGTRAP
     48 		// from an asynchronous signal SIGTRAP.
     49 		// They all look breakpoint-induced by default.
     50 		// Try looking at the code to see if it's a breakpoint.
     51 		// The assumption is that we're very unlikely to get an
     52 		// asynchronous SIGTRAP at just the moment that the
     53 		// PC started to point at unmapped memory.
     54 		pc := uintptr(c.eip())
     55 		// OS X will leave the pc just after the INT 3 instruction.
     56 		// INT 3 is usually 1 byte, but there is a 2-byte form.
     57 		code := (*[2]byte)(unsafe.Pointer(pc - 2))
     58 		if code[1] != 0xCC && (code[0] != 0xCD || code[1] != 3) {
     59 			// SIGTRAP on something other than INT 3.
     60 			c.set_sigcode(_SI_USER)
     61 		}
     62 	}
     63 }
     64