Commit 980f9a0a by Ian Lance Taylor

runtime: copy signal code from Go 1.7 runtime

    
    Add a little shell script to auto-generate runtime.sigtable from the
    known signal names.
    
    Force the main package to always import the runtime package.  Otherwise
    some runtime package global variables may never be initialized.
    
    Set the syscallsp and syscallpc fields of g when entering a syscall, so
    that the runtime package knows when a g is executing a syscall.
    
    Fix runtime.funcPC to avoid dead store elimination of the interface
    value when the function is inlined.
    
    Reviewed-on: https://go-review.googlesource.com/33025

From-SVN: r242060
parent 071af74d
cac897bd27885c18a16dacfe27d5efd4526455c5 449e918b0f93d3e3339edcec21a5bc157f548e54
The first line of this file holds the git revision number of the last The first line of this file holds the git revision number of the last
merge done from the gofrontend repository. merge done from the gofrontend repository.
...@@ -3791,7 +3791,7 @@ Unary_expression::do_flatten(Gogo* gogo, Named_object*, ...@@ -3791,7 +3791,7 @@ Unary_expression::do_flatten(Gogo* gogo, Named_object*,
this->escapes_ = false; this->escapes_ = false;
// When compiling the runtime, the address operator does not // When compiling the runtime, the address operator does not
// cause local variables to escapes. When escape analysis // cause local variables to escape. When escape analysis
// becomes the default, this should be changed to make it an // becomes the default, this should be changed to make it an
// error if we have an address operator that escapes. // error if we have an address operator that escapes.
if (gogo->compiling_runtime() && gogo->package_name() == "runtime") if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
......
...@@ -394,6 +394,7 @@ void ...@@ -394,6 +394,7 @@ void
Gogo::import_package(const std::string& filename, Gogo::import_package(const std::string& filename,
const std::string& local_name, const std::string& local_name,
bool is_local_name_exported, bool is_local_name_exported,
bool must_exist,
Location location) Location location)
{ {
if (filename.empty()) if (filename.empty())
...@@ -497,7 +498,8 @@ Gogo::import_package(const std::string& filename, ...@@ -497,7 +498,8 @@ Gogo::import_package(const std::string& filename,
this->relative_import_path_); this->relative_import_path_);
if (stream == NULL) if (stream == NULL)
{ {
go_error_at(location, "import file %qs not found", filename.c_str()); if (must_exist)
go_error_at(location, "import file %qs not found", filename.c_str());
return; return;
} }
...@@ -2179,6 +2181,14 @@ Gogo::is_thunk(const Named_object* no) ...@@ -2179,6 +2181,14 @@ Gogo::is_thunk(const Named_object* no)
void void
Gogo::define_global_names() Gogo::define_global_names()
{ {
if (this->is_main_package())
{
// Every Go program has to import the runtime package, so that
// it is properly initialized.
this->import_package("runtime", "_", false, false,
Linemap::predeclared_location());
}
for (Bindings::const_declarations_iterator p = for (Bindings::const_declarations_iterator p =
this->globals_->begin_declarations(); this->globals_->begin_declarations();
p != this->globals_->end_declarations(); p != this->globals_->end_declarations();
......
...@@ -301,7 +301,7 @@ class Gogo ...@@ -301,7 +301,7 @@ class Gogo
// the declarations are added to the global scope. // the declarations are added to the global scope.
void void
import_package(const std::string& filename, const std::string& local_name, import_package(const std::string& filename, const std::string& local_name,
bool is_local_name_exported, Location); bool is_local_name_exported, bool must_exist, Location);
// Whether we are the global binding level. // Whether we are the global binding level.
bool bool
......
...@@ -5722,7 +5722,7 @@ Parse::import_spec(void*) ...@@ -5722,7 +5722,7 @@ Parse::import_spec(void*)
} }
this->gogo_->import_package(token->string_value(), local_name, this->gogo_->import_package(token->string_value(), local_name,
is_local_name_exported, location); is_local_name_exported, true, location);
this->advance_token(); this->advance_token();
} }
......
...@@ -480,14 +480,12 @@ runtime_files = \ ...@@ -480,14 +480,12 @@ runtime_files = \
runtime/print.c \ runtime/print.c \
runtime/proc.c \ runtime/proc.c \
runtime/runtime_c.c \ runtime/runtime_c.c \
runtime/signal_unix.c \
runtime/thread.c \ runtime/thread.c \
$(runtime_thread_files) \ $(runtime_thread_files) \
runtime/yield.c \ runtime/yield.c \
$(rtems_task_variable_add_file) \ $(rtems_task_variable_add_file) \
malloc.c \ malloc.c \
runtime1.c \ runtime1.c \
sigqueue.c \
$(runtime_getncpu_file) $(runtime_getncpu_file)
goc2c.$(OBJEXT): runtime/goc2c.c goc2c.$(OBJEXT): runtime/goc2c.c
...@@ -504,10 +502,6 @@ runtime1.c: $(srcdir)/runtime/runtime1.goc goc2c ...@@ -504,10 +502,6 @@ runtime1.c: $(srcdir)/runtime/runtime1.goc goc2c
./goc2c $< > $@.tmp ./goc2c $< > $@.tmp
mv -f $@.tmp $@ mv -f $@.tmp $@
sigqueue.c: $(srcdir)/runtime/sigqueue.goc goc2c
./goc2c --go-pkgpath os_signal $< > $@.tmp
mv -f $@.tmp $@
%.c: $(srcdir)/runtime/%.goc goc2c %.c: $(srcdir)/runtime/%.goc goc2c
./goc2c $< > $@.tmp ./goc2c $< > $@.tmp
mv -f $@.tmp $@ mv -f $@.tmp $@
...@@ -572,6 +566,12 @@ s-runtime_sysinfo: $(srcdir)/mkrsysinfo.sh gen-sysinfo.go ...@@ -572,6 +566,12 @@ s-runtime_sysinfo: $(srcdir)/mkrsysinfo.sh gen-sysinfo.go
$(SHELL) $(srcdir)/mvifdiff.sh tmp-runtime_sysinfo.go runtime_sysinfo.go $(SHELL) $(srcdir)/mvifdiff.sh tmp-runtime_sysinfo.go runtime_sysinfo.go
$(STAMP) $@ $(STAMP) $@
sigtab.go: s-sigtab; @true
s-sigtab: $(srcdir)/mksigtab.sh gen-sysinfo.go
GOOS=$(GOOS) $(SHELL) $(srcdir)/mksigtab.sh > tmp-sigtab.go
$(SHELL) $(srcdir)/mvifdiff.sh tmp-sigtab.go sigtab.go
$(STAMP) $@
runtime.inc: s-runtime-inc; @true runtime.inc: s-runtime-inc; @true
s-runtime-inc: runtime.lo Makefile s-runtime-inc: runtime.lo Makefile
rm -f runtime.inc.tmp2 rm -f runtime.inc.tmp2
...@@ -1038,7 +1038,7 @@ $(foreach package,$(PACKAGES),$(eval $(call PACKAGE_template,$(package)))) ...@@ -1038,7 +1038,7 @@ $(foreach package,$(PACKAGES),$(eval $(call PACKAGE_template,$(package))))
math_lo_GOCFLAGS = $(MATH_FLAG) math_lo_GOCFLAGS = $(MATH_FLAG)
# Add the generated file runtime_sysinfo.go to the runtime package. # Add the generated file runtime_sysinfo.go to the runtime package.
extra_go_files_runtime = runtime_sysinfo.go extra_go_files_runtime = runtime_sysinfo.go sigtab.go
runtime.lo.dep: $(extra_go_files_runtime) runtime.lo.dep: $(extra_go_files_runtime)
# Add generated files to the syscall package. # Add generated files to the syscall package.
......
...@@ -203,9 +203,8 @@ am__objects_5 = go-assert.lo go-breakpoint.lo go-caller.lo \ ...@@ -203,9 +203,8 @@ am__objects_5 = go-assert.lo go-breakpoint.lo go-caller.lo \
go-unwind.lo go-varargs.lo env_posix.lo heapdump.lo mcache.lo \ go-unwind.lo go-varargs.lo env_posix.lo heapdump.lo mcache.lo \
mcentral.lo $(am__objects_1) mfixalloc.lo mgc0.lo mheap.lo \ mcentral.lo $(am__objects_1) mfixalloc.lo mgc0.lo mheap.lo \
msize.lo panic.lo parfor.lo print.lo proc.lo runtime_c.lo \ msize.lo panic.lo parfor.lo print.lo proc.lo runtime_c.lo \
signal_unix.lo thread.lo $(am__objects_2) yield.lo \ thread.lo $(am__objects_2) yield.lo $(am__objects_3) malloc.lo \
$(am__objects_3) malloc.lo runtime1.lo sigqueue.lo \ runtime1.lo $(am__objects_4)
$(am__objects_4)
am_libgo_llgo_la_OBJECTS = $(am__objects_5) am_libgo_llgo_la_OBJECTS = $(am__objects_5)
libgo_llgo_la_OBJECTS = $(am_libgo_llgo_la_OBJECTS) libgo_llgo_la_OBJECTS = $(am_libgo_llgo_la_OBJECTS)
libgo_llgo_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \ libgo_llgo_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
...@@ -828,14 +827,12 @@ runtime_files = \ ...@@ -828,14 +827,12 @@ runtime_files = \
runtime/print.c \ runtime/print.c \
runtime/proc.c \ runtime/proc.c \
runtime/runtime_c.c \ runtime/runtime_c.c \
runtime/signal_unix.c \
runtime/thread.c \ runtime/thread.c \
$(runtime_thread_files) \ $(runtime_thread_files) \
runtime/yield.c \ runtime/yield.c \
$(rtems_task_variable_add_file) \ $(rtems_task_variable_add_file) \
malloc.c \ malloc.c \
runtime1.c \ runtime1.c \
sigqueue.c \
$(runtime_getncpu_file) $(runtime_getncpu_file)
noinst_DATA = zstdpkglist.go noinst_DATA = zstdpkglist.go
...@@ -1141,7 +1138,7 @@ CHECK_DEPS = $(toolexeclibgo_DATA) $(toolexeclibgoarchive_DATA) \ ...@@ -1141,7 +1138,7 @@ CHECK_DEPS = $(toolexeclibgo_DATA) $(toolexeclibgoarchive_DATA) \
math_lo_GOCFLAGS = $(MATH_FLAG) math_lo_GOCFLAGS = $(MATH_FLAG)
# Add the generated file runtime_sysinfo.go to the runtime package. # Add the generated file runtime_sysinfo.go to the runtime package.
extra_go_files_runtime = runtime_sysinfo.go extra_go_files_runtime = runtime_sysinfo.go sigtab.go
# Add generated files to the syscall package. # Add generated files to the syscall package.
extra_go_files_syscall = \ extra_go_files_syscall = \
...@@ -1525,8 +1522,6 @@ distclean-compile: ...@@ -1525,8 +1522,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rtems-task-variable-add.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rtems-task-variable-add.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/runtime1.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/runtime1.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/runtime_c.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/runtime_c.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signal_unix.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sigqueue.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread-linux.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread-linux.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread-sema.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread-sema.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread.Plo@am__quote@
...@@ -1966,13 +1961,6 @@ runtime_c.lo: runtime/runtime_c.c ...@@ -1966,13 +1961,6 @@ runtime_c.lo: runtime/runtime_c.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o runtime_c.lo `test -f 'runtime/runtime_c.c' || echo '$(srcdir)/'`runtime/runtime_c.c @am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o runtime_c.lo `test -f 'runtime/runtime_c.c' || echo '$(srcdir)/'`runtime/runtime_c.c
signal_unix.lo: runtime/signal_unix.c
@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT signal_unix.lo -MD -MP -MF $(DEPDIR)/signal_unix.Tpo -c -o signal_unix.lo `test -f 'runtime/signal_unix.c' || echo '$(srcdir)/'`runtime/signal_unix.c
@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/signal_unix.Tpo $(DEPDIR)/signal_unix.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='runtime/signal_unix.c' object='signal_unix.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o signal_unix.lo `test -f 'runtime/signal_unix.c' || echo '$(srcdir)/'`runtime/signal_unix.c
thread.lo: runtime/thread.c thread.lo: runtime/thread.c
@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT thread.lo -MD -MP -MF $(DEPDIR)/thread.Tpo -c -o thread.lo `test -f 'runtime/thread.c' || echo '$(srcdir)/'`runtime/thread.c @am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT thread.lo -MD -MP -MF $(DEPDIR)/thread.Tpo -c -o thread.lo `test -f 'runtime/thread.c' || echo '$(srcdir)/'`runtime/thread.c
@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/thread.Tpo $(DEPDIR)/thread.Plo @am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/thread.Tpo $(DEPDIR)/thread.Plo
...@@ -3177,10 +3165,6 @@ runtime1.c: $(srcdir)/runtime/runtime1.goc goc2c ...@@ -3177,10 +3165,6 @@ runtime1.c: $(srcdir)/runtime/runtime1.goc goc2c
./goc2c $< > $@.tmp ./goc2c $< > $@.tmp
mv -f $@.tmp $@ mv -f $@.tmp $@
sigqueue.c: $(srcdir)/runtime/sigqueue.goc goc2c
./goc2c --go-pkgpath os_signal $< > $@.tmp
mv -f $@.tmp $@
%.c: $(srcdir)/runtime/%.goc goc2c %.c: $(srcdir)/runtime/%.goc goc2c
./goc2c $< > $@.tmp ./goc2c $< > $@.tmp
mv -f $@.tmp $@ mv -f $@.tmp $@
...@@ -3245,6 +3229,12 @@ s-runtime_sysinfo: $(srcdir)/mkrsysinfo.sh gen-sysinfo.go ...@@ -3245,6 +3229,12 @@ s-runtime_sysinfo: $(srcdir)/mkrsysinfo.sh gen-sysinfo.go
$(SHELL) $(srcdir)/mvifdiff.sh tmp-runtime_sysinfo.go runtime_sysinfo.go $(SHELL) $(srcdir)/mvifdiff.sh tmp-runtime_sysinfo.go runtime_sysinfo.go
$(STAMP) $@ $(STAMP) $@
sigtab.go: s-sigtab; @true
s-sigtab: $(srcdir)/mksigtab.sh gen-sysinfo.go
GOOS=$(GOOS) $(SHELL) $(srcdir)/mksigtab.sh > tmp-sigtab.go
$(SHELL) $(srcdir)/mvifdiff.sh tmp-sigtab.go sigtab.go
$(STAMP) $@
runtime.inc: s-runtime-inc; @true runtime.inc: s-runtime-inc; @true
s-runtime-inc: runtime.lo Makefile s-runtime-inc: runtime.lo Makefile
rm -f runtime.inc.tmp2 rm -f runtime.inc.tmp2
......
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
// Calling panic with one of the errors below will call errorString.Error
// which will call mallocgc to concatenate strings. That will fail if
// malloc is locked, causing a confusing error message. Throw a better
// error message instead.
func panicCheckMalloc(err error) {
gp := getg()
if gp != nil && gp.m != nil && gp.m.mallocing != 0 {
throw(string(err.(errorString)))
}
}
var indexError = error(errorString("index out of range"))
func panicindex() {
panicCheckMalloc(indexError)
panic(indexError)
}
var sliceError = error(errorString("slice bounds out of range"))
func panicslice() {
panicCheckMalloc(sliceError)
panic(sliceError)
}
var divideError = error(errorString("integer divide by zero"))
func panicdivide() {
panicCheckMalloc(divideError)
panic(divideError)
}
var overflowError = error(errorString("integer overflow"))
func panicoverflow() {
panicCheckMalloc(overflowError)
panic(overflowError)
}
var floatError = error(errorString("floating point error"))
func panicfloat() {
panicCheckMalloc(floatError)
panic(floatError)
}
var memoryError = error(errorString("invalid memory address or nil pointer dereference"))
func panicmem() {
panicCheckMalloc(memoryError)
panic(memoryError)
}
func throwreturn() {
throw("no return at end of a typed function - compiler is broken")
}
func throwinit() {
throw("recursive call during initialization - linker skew")
}
//go:nosplit
func canpanic(gp *g) bool {
// Note that g is m->gsignal, different from gp.
// Note also that g->m can change at preemption, so m can go stale
// if this function ever makes a function call.
_g_ := getg()
_m_ := _g_.m
// Is it okay for gp to panic instead of crashing the program?
// Yes, as long as it is running Go code, not runtime code,
// and not stuck in a system call.
if gp == nil || gp != _m_.curg {
return false
}
if _m_.locks-_m_.softfloat != 0 || _m_.mallocing != 0 || _m_.throwing != 0 || _m_.preemptoff != "" || _m_.dying != 0 {
return false
}
status := readgstatus(gp)
if status&^_Gscan != _Grunning || gp.syscallsp != 0 {
return false
}
return true
}
...@@ -326,8 +326,8 @@ type g struct { ...@@ -326,8 +326,8 @@ type g struct {
m *m // current m; offset known to arm liblink m *m // current m; offset known to arm liblink
// Not for gccgo: stackAlloc uintptr // stack allocation is [stack.lo,stack.lo+stackAlloc) // Not for gccgo: stackAlloc uintptr // stack allocation is [stack.lo,stack.lo+stackAlloc)
// Not for gccgo: sched gobuf // Not for gccgo: sched gobuf
// Not for gccgo: syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc
// Not for gccgo: syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc
// Not for gccgo: stkbar []stkbar // stack barriers, from low to high (see top of mstkbar.go) // Not for gccgo: stkbar []stkbar // stack barriers, from low to high (see top of mstkbar.go)
// Not for gccgo: stkbarPos uintptr // index of lowest stack barrier not hit // Not for gccgo: stkbarPos uintptr // index of lowest stack barrier not hit
// Not for gccgo: stktopsp uintptr // expected sp at top of stack, to check in traceback // Not for gccgo: stktopsp uintptr // expected sp at top of stack, to check in traceback
......
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package runtime
import (
_ "unsafe" // For go:linkname.
)
// Temporary for gccgo's C code to call:
//go:linkname initsig runtime.initsig
//go:linkname crash runtime.crash
//go:linkname resetcpuprofiler runtime.resetcpuprofiler
//extern setitimer
func setitimer(which int32, new *_itimerval, old *_itimerval) int32
type sigTabT struct {
flags int32
name string
}
const (
_SIG_DFL uintptr = 0
_SIG_IGN uintptr = 1
)
// Stores the signal handlers registered before Go installed its own.
// These signal handlers will be invoked in cases where Go doesn't want to
// handle a particular signal (e.g., signal occurred on a non-Go thread).
// See sigfwdgo() for more information on when the signals are forwarded.
//
// Signal forwarding is currently available only on Darwin and Linux.
var fwdSig [_NSIG]uintptr
// sigmask represents a general signal mask compatible with the GOOS
// specific sigset types: the signal numbered x is represented by bit x-1
// to match the representation expected by sigprocmask.
type sigmask [(_NSIG + 31) / 32]uint32
// channels for synchronizing signal mask updates with the signal mask
// thread
var (
disableSigChan chan uint32
enableSigChan chan uint32
maskUpdatedChan chan struct{}
)
func init() {
// _NSIG is the number of signals on this operating system.
// sigtable should describe what to do for all the possible signals.
if len(sigtable) != _NSIG {
print("runtime: len(sigtable)=", len(sigtable), " _NSIG=", _NSIG, "\n")
throw("bad sigtable len")
}
}
var signalsOK bool
// Initialize signals.
// Called by libpreinit so runtime may not be initialized.
//go:nosplit
//go:nowritebarrierrec
func initsig(preinit bool) {
if !preinit {
// It's now OK for signal handlers to run.
signalsOK = true
}
// For c-archive/c-shared this is called by libpreinit with
// preinit == true.
if (isarchive || islibrary) && !preinit {
return
}
for i := int32(0); i < _NSIG; i++ {
t := &sigtable[i]
if t.flags == 0 || t.flags&_SigDefault != 0 {
continue
}
fwdSig[i] = getsig(i)
if !sigInstallGoHandler(i) {
// Even if we are not installing a signal handler,
// set SA_ONSTACK if necessary.
if fwdSig[i] != _SIG_DFL && fwdSig[i] != _SIG_IGN {
setsigstack(i)
}
continue
}
t.flags |= _SigHandling
setsig(i, funcPC(sigtramp), true)
}
}
//go:nosplit
//go:nowritebarrierrec
func sigInstallGoHandler(sig int32) bool {
// For some signals, we respect an inherited SIG_IGN handler
// rather than insist on installing our own default handler.
// Even these signals can be fetched using the os/signal package.
switch sig {
case _SIGHUP, _SIGINT:
if fwdSig[sig] == _SIG_IGN {
return false
}
}
t := &sigtable[sig]
if t.flags&_SigSetStack != 0 {
return false
}
// When built using c-archive or c-shared, only install signal
// handlers for synchronous signals.
if (isarchive || islibrary) && t.flags&_SigPanic == 0 {
return false
}
return true
}
func sigenable(sig uint32) {
if sig >= uint32(len(sigtable)) {
return
}
t := &sigtable[sig]
if t.flags&_SigNotify != 0 {
ensureSigM()
enableSigChan <- sig
<-maskUpdatedChan
if t.flags&_SigHandling == 0 {
t.flags |= _SigHandling
fwdSig[sig] = getsig(int32(sig))
setsig(int32(sig), funcPC(sigtramp), true)
}
}
}
func sigdisable(sig uint32) {
if sig >= uint32(len(sigtable)) {
return
}
t := &sigtable[sig]
if t.flags&_SigNotify != 0 {
ensureSigM()
disableSigChan <- sig
<-maskUpdatedChan
// If initsig does not install a signal handler for a
// signal, then to go back to the state before Notify
// we should remove the one we installed.
if !sigInstallGoHandler(int32(sig)) {
t.flags &^= _SigHandling
setsig(int32(sig), fwdSig[sig], true)
}
}
}
func sigignore(sig uint32) {
if sig >= uint32(len(sigtable)) {
return
}
t := &sigtable[sig]
if t.flags&_SigNotify != 0 {
t.flags &^= _SigHandling
setsig(int32(sig), _SIG_IGN, true)
}
}
func resetcpuprofiler(hz int32) {
var it _itimerval
if hz == 0 {
setitimer(_ITIMER_PROF, &it, nil)
} else {
it.it_interval.tv_sec = 0
it.it_interval.set_usec(1000000 / hz)
it.it_value = it.it_interval
setitimer(_ITIMER_PROF, &it, nil)
}
_g_ := getg()
_g_.m.profilehz = hz
}
func sigpipe() {
if sigsend(_SIGPIPE) {
return
}
dieFromSignal(_SIGPIPE)
}
// dieFromSignal kills the program with a signal.
// This provides the expected exit status for the shell.
// This is only called with fatal signals expected to kill the process.
//go:nosplit
//go:nowritebarrierrec
func dieFromSignal(sig int32) {
setsig(sig, _SIG_DFL, false)
updatesigmask(sigmask{})
raise(sig)
// That should have killed us. On some systems, though, raise
// sends the signal to the whole process rather than to just
// the current thread, which means that the signal may not yet
// have been delivered. Give other threads a chance to run and
// pick up the signal.
osyield()
osyield()
osyield()
// If we are still somehow running, just exit with the wrong status.
exit(2)
}
// raisebadsignal is called when a signal is received on a non-Go
// thread, and the Go program does not want to handle it (that is, the
// program has not called os/signal.Notify for the signal).
func raisebadsignal(sig int32, c *sigctxt) {
if sig == _SIGPROF {
// Ignore profiling signals that arrive on non-Go threads.
return
}
var handler uintptr
if sig >= _NSIG {
handler = _SIG_DFL
} else {
handler = fwdSig[sig]
}
// Reset the signal handler and raise the signal.
// We are currently running inside a signal handler, so the
// signal is blocked. We need to unblock it before raising the
// signal, or the signal we raise will be ignored until we return
// from the signal handler. We know that the signal was unblocked
// before entering the handler, or else we would not have received
// it. That means that we don't have to worry about blocking it
// again.
unblocksig(sig)
setsig(sig, handler, false)
// If we're linked into a non-Go program we want to try to
// avoid modifying the original context in which the signal
// was raised. If the handler is the default, we know it
// is non-recoverable, so we don't have to worry about
// re-installing sighandler. At this point we can just
// return and the signal will be re-raised and caught by
// the default handler with the correct context.
if (isarchive || islibrary) && handler == _SIG_DFL && c.sigcode() != _SI_USER {
return
}
raise(sig)
// If the signal didn't cause the program to exit, restore the
// Go signal handler and carry on.
//
// We may receive another instance of the signal before we
// restore the Go handler, but that is not so bad: we know
// that the Go program has been ignoring the signal.
setsig(sig, funcPC(sigtramp), true)
}
func crash() {
dieFromSignal(_SIGABRT)
}
// ensureSigM starts one global, sleeping thread to make sure at least one thread
// is available to catch signals enabled for os/signal.
func ensureSigM() {
if maskUpdatedChan != nil {
return
}
maskUpdatedChan = make(chan struct{})
disableSigChan = make(chan uint32)
enableSigChan = make(chan uint32)
go func() {
// Signal masks are per-thread, so make sure this goroutine stays on one
// thread.
LockOSThread()
defer UnlockOSThread()
// The sigBlocked mask contains the signals not active for os/signal,
// initially all signals except the essential. When signal.Notify()/Stop is called,
// sigenable/sigdisable in turn notify this thread to update its signal
// mask accordingly.
var sigBlocked sigmask
for i := range sigBlocked {
sigBlocked[i] = ^uint32(0)
}
for i := range sigtable {
if sigtable[i].flags&_SigUnblock != 0 {
sigBlocked[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
}
}
updatesigmask(sigBlocked)
for {
select {
case sig := <-enableSigChan:
if b := sig - 1; sig > 0 {
sigBlocked[b/32] &^= (1 << (b & 31))
}
case sig := <-disableSigChan:
if b := sig - 1; sig > 0 {
sigBlocked[b/32] |= (1 << (b & 31))
}
}
updatesigmask(sigBlocked)
maskUpdatedChan <- struct{}{}
}
}()
}
// This runs on a foreign stack, without an m or a g. No stack split.
//go:nosplit
//go:norace
//go:nowritebarrierrec
func badsignal(sig uintptr, c *sigctxt) {
needm()
if !sigsend(uint32(sig)) {
// A foreign thread received the signal sig, and the
// Go code does not want to handle it.
raisebadsignal(int32(sig), c)
}
dropm()
}
...@@ -2,24 +2,19 @@ ...@@ -2,24 +2,19 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build ignore // +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build darwin dragonfly freebsd linux netbsd openbsd
package runtime package runtime
import "unsafe" import "unsafe"
//go:noescape
func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer)
// Determines if the signal should be handled by Go and if not, forwards the // Determines if the signal should be handled by Go and if not, forwards the
// signal to the handler that was installed before Go's. Returns whether the // signal to the handler that was installed before Go's. Returns whether the
// signal was forwarded. // signal was forwarded.
// This is called by the signal handler, and the world may be stopped. // This is called by the signal handler, and the world may be stopped.
//go:nosplit //go:nosplit
//go:nowritebarrierrec //go:nowritebarrierrec
func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool { func sigfwdgo(sig uint32, info *_siginfo_t, ctx unsafe.Pointer) bool {
if sig >= uint32(len(sigtable)) { if sig >= uint32(len(sigtable)) {
return false return false
} }
...@@ -52,7 +47,7 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool { ...@@ -52,7 +47,7 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool {
} }
// Only forward synchronous signals. // Only forward synchronous signals.
c := &sigctxt{info, ctx} c := sigctxt{info, ctx}
if c.sigcode() == _SI_USER || flags&_SigPanic == 0 { if c.sigcode() == _SI_USER || flags&_SigPanic == 0 {
return false return false
} }
......
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package runtime
import (
"unsafe"
)
// Functions for gccgo to support signal handling. In the gc runtime
// these are written in OS-specific files and in assembler.
//extern sigaction
func sigaction(signum int32, act *_sigaction, oact *_sigaction) int32
//extern sigprocmask
func sigprocmask(how int32, set *_sigset_t, oldset *_sigset_t) int32
// The argument should be simply *_sigset_t, but that fails on GNU/Linux
// which sometimes uses _sigset_t and sometimes uses ___sigset_t.
//extern sigfillset
func sigfillset(set unsafe.Pointer) int32
//extern sigemptyset
func sigemptyset(set *_sigset_t) int32
//extern sigaddset
func sigaddset(set *_sigset_t, signum int32) int32
//extern sigaltstack
func sigaltstack(ss *_stack_t, oss *_stack_t) int32
//extern raise
func raise(sig int32) int32
//extern getpid
func getpid() _pid_t
//extern kill
func kill(pid _pid_t, sig int32) int32
type sigctxt struct {
info *_siginfo_t
ctxt unsafe.Pointer
}
func (c *sigctxt) sigcode() uint64 { return uint64(c.info.si_code) }
//go:nosplit
func sigblock() {
var set _sigset_t
sigfillset(unsafe.Pointer(&set))
sigprocmask(_SIG_SETMASK, &set, nil)
}
//go:nosplit
//go:nowritebarrierrec
func setsig(i int32, fn uintptr, restart bool) {
var sa _sigaction
sa.sa_flags = _SA_SIGINFO
// For gccgo we do not set SA_ONSTACK for a signal that can
// cause a panic. Instead, we trust that the split stack has
// enough room to start the signal handler. This is because
// otherwise we have no good way to switch back to the
// original stack before panicing.
if sigtable[i].flags&_SigPanic == 0 {
sa.sa_flags |= _SA_ONSTACK
}
if restart {
sa.sa_flags |= _SA_RESTART
}
sigfillset(unsafe.Pointer(&sa.sa_mask))
setSigactionHandler(&sa, fn)
sigaction(i, &sa, nil)
}
//go:nosplit
//go:nowritebarrierrec
func setsigstack(i int32) {
var sa _sigaction
sigaction(i, nil, &sa)
handler := getSigactionHandler(&sa)
if handler == 0 || handler == _SIG_DFL || handler == _SIG_IGN || sa.sa_flags&_SA_ONSTACK != 0 {
return
}
if sigtable[i].flags&_SigPanic != 0 {
return
}
sa.sa_flags |= _SA_ONSTACK
sigaction(i, &sa, nil)
}
//go:nosplit
//go:nowritebarrierrec
func getsig(i int32) uintptr {
var sa _sigaction
if sigaction(i, nil, &sa) < 0 {
// On GNU/Linux glibc rejects attempts to call
// sigaction with signal 32 (SIGCANCEL) or 33 (SIGSETXID).
if GOOS == "linux" && (i == 32 || i == 33) {
return _SIG_DFL
}
throw("sigaction read failure")
}
return getSigactionHandler(&sa)
}
//go:nosplit
//go:nowritebarrierrec
func updatesigmask(m sigmask) {
var mask _sigset_t
sigemptyset(&mask)
for i := int32(0); i < _NSIG; i++ {
if m[(i-1)/32]&(1<<((uint(i)-1)&31)) != 0 {
sigaddset(&mask, i)
}
}
sigprocmask(_SIG_SETMASK, &mask, nil)
}
func unblocksig(sig int32) {
var mask _sigset_t
sigemptyset(&mask)
sigaddset(&mask, sig)
sigprocmask(_SIG_UNBLOCK, &mask, nil)
}
//go:nosplit
//go:nowritebarrierrec
func raiseproc(sig int32) {
kill(getpid(), sig)
}
//go:nosplit
//go:nowritebarrierrec
func sigfwd(fn uintptr, sig uint32, info *_siginfo_t, ctx unsafe.Pointer) {
f1 := &[1]uintptr{fn}
f2 := *(*func(uint32, *_siginfo_t, unsafe.Pointer))(unsafe.Pointer(&f1))
f2(sig, info, ctx)
}
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
package runtime
import (
"unsafe"
)
// crashing is the number of m's we have waited for when implementing
// GOTRACEBACK=crash when a signal is received.
var crashing int32
// sighandler is invoked when a signal occurs. The global g will be
// set to a gsignal goroutine and we will be running on the alternate
// signal stack. The parameter g will be the value of the global g
// when the signal occurred. The sig, info, and ctxt parameters are
// from the system signal handler: they are the parameters passed when
// the SA is passed to the sigaction system call.
//
// The garbage collector may have stopped the world, so write barriers
// are not allowed.
//
//go:nowritebarrierrec
func sighandler(sig uint32, info *_siginfo_t, ctxt unsafe.Pointer, gp *g) {
_g_ := getg()
c := sigctxt{info, ctxt}
if sig == _SIGPROF {
sigprof()
return
}
sigfault, sigpc := getSiginfo(info, ctxt)
flags := int32(_SigThrow)
if sig < uint32(len(sigtable)) {
flags = sigtable[sig].flags
}
if c.sigcode() != _SI_USER && flags&_SigPanic != 0 {
// Emulate gc by passing arguments out of band,
// although we don't really have to.
gp.sig = sig
gp.sigcode0 = uintptr(c.sigcode())
gp.sigcode1 = sigfault
gp.sigpc = sigpc
setg(gp)
// All signals were blocked due to the sigaction mask;
// unblock them.
var set _sigset_t
sigfillset(unsafe.Pointer(&set))
sigprocmask(_SIG_UNBLOCK, &set, nil)
sigpanic()
throw("sigpanic returned")
}
if c.sigcode() == _SI_USER || flags&_SigNotify != 0 {
if sigsend(sig) {
return
}
}
if c.sigcode() == _SI_USER && signal_ignored(sig) {
return
}
if flags&_SigKill != 0 {
dieFromSignal(int32(sig))
}
if flags&_SigThrow == 0 {
return
}
_g_.m.throwing = 1
_g_.m.caughtsig.set(gp)
if crashing == 0 {
startpanic()
}
if sig < uint32(len(sigtable)) {
print(sigtable[sig].name, "\n")
} else {
print("Signal ", sig, "\n")
}
if sigpc != 0 {
print("PC=", hex(sigpc), " ")
}
print("m=", _g_.m.id, " sigcode=", c.sigcode(), "\n")
if _g_.m.lockedg != nil && _g_.m.ncgo > 0 && gp == _g_.m.g0 {
print("signal arrived during cgo execution\n")
gp = _g_.m.lockedg
}
print("\n")
level, _, docrash := gotraceback()
if level > 0 {
goroutineheader(gp)
traceback(0)
if crashing == 0 {
tracebackothers(gp)
print("\n")
}
dumpregs(info, ctxt)
}
if docrash {
crashing++
if crashing < mcount() {
// There are other m's that need to dump their stacks.
// Relay SIGQUIT to the next m by sending it to the current process.
// All m's that have already received SIGQUIT have signal masks blocking
// receipt of any signals, so the SIGQUIT will go to an m that hasn't seen it yet.
// When the last m receives the SIGQUIT, it will fall through to the call to
// crash below. Just in case the relaying gets botched, each m involved in
// the relay sleeps for 5 seconds and then does the crash/exit itself.
// In expected operation, the last m has received the SIGQUIT and run
// crash/exit and the process is gone, all long before any of the
// 5-second sleeps have finished.
print("\n-----\n\n")
raiseproc(_SIGQUIT)
usleep(5 * 1000 * 1000)
}
crash()
}
exit(2)
}
...@@ -2,19 +2,20 @@ ...@@ -2,19 +2,20 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build ignore // +build darwin dragonfly freebsd linux netbsd openbsd solaris
// +build dragonfly linux netbsd
package runtime package runtime
import "unsafe" import "unsafe"
// For gccgo, use go:linkname so the C signal handler can call this one.
//go:linkname sigtrampgo runtime.sigtrampgo
// Continuation of the (assembly) sigtramp() logic. // Continuation of the (assembly) sigtramp() logic.
// This may be called with the world stopped. // This may be called with the world stopped.
//go:nosplit //go:nosplit
//go:nowritebarrierrec //go:nowritebarrierrec
func sigtrampgo(sig uint32, info *siginfo, ctx unsafe.Pointer) { func sigtrampgo(sig uint32, info *_siginfo_t, ctx unsafe.Pointer) {
if sigfwdgo(sig, info, ctx) { if sigfwdgo(sig, info, ctx) {
return return
} }
...@@ -32,28 +33,6 @@ func sigtrampgo(sig uint32, info *siginfo, ctx unsafe.Pointer) { ...@@ -32,28 +33,6 @@ func sigtrampgo(sig uint32, info *siginfo, ctx unsafe.Pointer) {
return return
} }
// If some non-Go code called sigaltstack, adjust.
sp := uintptr(unsafe.Pointer(&sig))
if sp < g.m.gsignal.stack.lo || sp >= g.m.gsignal.stack.hi {
var st sigaltstackt
sigaltstack(nil, &st)
if st.ss_flags&_SS_DISABLE != 0 {
setg(nil)
cgocallback(unsafe.Pointer(funcPC(noSignalStack)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig), 0)
}
stsp := uintptr(unsafe.Pointer(st.ss_sp))
if sp < stsp || sp >= stsp+st.ss_size {
setg(nil)
cgocallback(unsafe.Pointer(funcPC(sigNotOnStack)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig), 0)
}
g.m.gsignal.stack.lo = stsp
g.m.gsignal.stack.hi = stsp + st.ss_size
g.m.gsignal.stackguard0 = stsp + _StackGuard
g.m.gsignal.stackguard1 = stsp + _StackGuard
g.m.gsignal.stackAlloc = st.ss_size
g.m.gsignal.stktopsp = getcallersp(unsafe.Pointer(&sig))
}
setg(g.m.gsignal) setg(g.m.gsignal)
sighandler(sig, info, ctx, g) sighandler(sig, info, ctx, g)
setg(g) setg(g)
......
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package runtime
import _ "unsafe" // for go:linkname
//go:linkname os_sigpipe os.sigpipe
func os_sigpipe() {
systemstack(sigpipe)
}
func signame(sig uint32) string {
if sig >= uint32(len(sigtable)) {
return ""
}
return sigtable[sig].name
}
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package runtime
import _ "unsafe" // For go:linkname.
// For gccgo, C code has to call sigpanic, so we have to export it.
//go:linkname sigpanic runtime.sigpanic
func sigpanic() {
g := getg()
if !canpanic(g) {
throw("unexpected signal during runtime execution")
}
switch g.sig {
case _SIGBUS:
if g.sigcode0 == _BUS_ADRERR && g.sigcode1 < 0x1000 || g.paniconfault {
panicmem()
}
print("unexpected fault address ", hex(g.sigcode1), "\n")
throw("fault")
case _SIGSEGV:
if (g.sigcode0 == 0 || g.sigcode0 == _SEGV_MAPERR || g.sigcode0 == _SEGV_ACCERR) && g.sigcode1 < 0x1000 || g.paniconfault {
panicmem()
}
print("unexpected fault address ", hex(g.sigcode1), "\n")
throw("fault")
case _SIGFPE:
switch g.sigcode0 {
case _FPE_INTDIV:
panicdivide()
case _FPE_INTOVF:
panicoverflow()
}
panicfloat()
}
if g.sig >= uint32(len(sigtable)) {
// can't happen: we looked up g.sig in sigtable to decide to call sigpanic
throw("unexpected signal value")
}
panic(errorString(sigtable[g.sig].name))
}
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file implements runtime support for signal handling.
//
// Most synchronization primitives are not available from
// the signal handler (it cannot block, allocate memory, or use locks)
// so the handler communicates with a processing goroutine
// via struct sig, below.
//
// sigsend is called by the signal handler to queue a new signal.
// signal_recv is called by the Go program to receive a newly queued signal.
// Synchronization between sigsend and signal_recv is based on the sig.state
// variable. It can be in 3 states: sigIdle, sigReceiving and sigSending.
// sigReceiving means that signal_recv is blocked on sig.Note and there are no
// new pending signals.
// sigSending means that sig.mask *may* contain new pending signals,
// signal_recv can't be blocked in this state.
// sigIdle means that there are no new pending signals and signal_recv is not blocked.
// Transitions between states are done atomically with CAS.
// When signal_recv is unblocked, it resets sig.Note and rechecks sig.mask.
// If several sigsends and signal_recv execute concurrently, it can lead to
// unnecessary rechecks of sig.mask, but it cannot lead to missed signals
// nor deadlocks.
// +build !plan9
package runtime
import (
"runtime/internal/atomic"
_ "unsafe" // for go:linkname
)
var sig struct {
note note
mask [(_NSIG + 31) / 32]uint32
wanted [(_NSIG + 31) / 32]uint32
ignored [(_NSIG + 31) / 32]uint32
recv [(_NSIG + 31) / 32]uint32
state uint32
inuse bool
}
const (
sigIdle = iota
sigReceiving
sigSending
)
// Called from sighandler to send a signal back out of the signal handling thread.
// Reports whether the signal was sent. If not, the caller typically crashes the program.
func sigsend(s uint32) bool {
bit := uint32(1) << uint(s&31)
if !sig.inuse || s >= uint32(32*len(sig.wanted)) || sig.wanted[s/32]&bit == 0 {
return false
}
// Add signal to outgoing queue.
for {
mask := sig.mask[s/32]
if mask&bit != 0 {
return true // signal already in queue
}
if atomic.Cas(&sig.mask[s/32], mask, mask|bit) {
break
}
}
// Notify receiver that queue has new bit.
Send:
for {
switch atomic.Load(&sig.state) {
default:
throw("sigsend: inconsistent state")
case sigIdle:
if atomic.Cas(&sig.state, sigIdle, sigSending) {
break Send
}
case sigSending:
// notification already pending
break Send
case sigReceiving:
if atomic.Cas(&sig.state, sigReceiving, sigIdle) {
notewakeup(&sig.note)
break Send
}
}
}
return true
}
// Called to receive the next queued signal.
// Must only be called from a single goroutine at a time.
//go:linkname signal_recv os_signal.signal_recv
func signal_recv() uint32 {
for {
// Serve any signals from local copy.
for i := uint32(0); i < _NSIG; i++ {
if sig.recv[i/32]&(1<<(i&31)) != 0 {
sig.recv[i/32] &^= 1 << (i & 31)
return i
}
}
// Wait for updates to be available from signal sender.
Receive:
for {
switch atomic.Load(&sig.state) {
default:
throw("signal_recv: inconsistent state")
case sigIdle:
if atomic.Cas(&sig.state, sigIdle, sigReceiving) {
notetsleepg(&sig.note, -1)
noteclear(&sig.note)
break Receive
}
case sigSending:
if atomic.Cas(&sig.state, sigSending, sigIdle) {
break Receive
}
}
}
// Incorporate updates from sender into local copy.
for i := range sig.mask {
sig.recv[i] = atomic.Xchg(&sig.mask[i], 0)
}
}
}
// Must only be called from a single goroutine at a time.
//go:linkname signal_enable os_signal.signal_enable
func signal_enable(s uint32) {
if !sig.inuse {
// The first call to signal_enable is for us
// to use for initialization. It does not pass
// signal information in m.
sig.inuse = true // enable reception of signals; cannot disable
noteclear(&sig.note)
return
}
if s >= uint32(len(sig.wanted)*32) {
return
}
sig.wanted[s/32] |= 1 << (s & 31)
sig.ignored[s/32] &^= 1 << (s & 31)
sigenable(s)
}
// Must only be called from a single goroutine at a time.
//go:linkname signal_disable os_signal.signal_disable
func signal_disable(s uint32) {
if s >= uint32(len(sig.wanted)*32) {
return
}
sig.wanted[s/32] &^= 1 << (s & 31)
sigdisable(s)
}
// Must only be called from a single goroutine at a time.
//go:linkname signal_ignore os_signal.signal_ignore
func signal_ignore(s uint32) {
if s >= uint32(len(sig.wanted)*32) {
return
}
sig.wanted[s/32] &^= 1 << (s & 31)
sig.ignored[s/32] |= 1 << (s & 31)
sigignore(s)
}
// Checked by signal handlers.
func signal_ignored(s uint32) bool {
return sig.ignored[s/32]&(1<<(s&31)) != 0
}
...@@ -241,9 +241,15 @@ func newarray(*_type, int) unsafe.Pointer ...@@ -241,9 +241,15 @@ func newarray(*_type, int) unsafe.Pointer
// funcPC returns the entry PC of the function f. // funcPC returns the entry PC of the function f.
// It assumes that f is a func value. Otherwise the behavior is undefined. // It assumes that f is a func value. Otherwise the behavior is undefined.
// For gccgo here unless and until we port proc.go. // For gccgo here unless and until we port proc.go.
// Note that this differs from the gc implementation; the gc implementation
// adds sys.PtrSize to the address of the interface value, but GCC's
// alias analysis decides that that can not be a reference to the second
// field of the interface, and in some cases it drops the initialization
// of the second field as a dead store.
//go:nosplit //go:nosplit
func funcPC(f interface{}) uintptr { func funcPC(f interface{}) uintptr {
return **(**uintptr)(add(unsafe.Pointer(&f), sys.PtrSize)) i := (*iface)(unsafe.Pointer(&f))
return **(**uintptr)(i.data)
} }
// typedmemmove copies a typed value. // typedmemmove copies a typed value.
...@@ -489,3 +495,27 @@ var zerobase uintptr ...@@ -489,3 +495,27 @@ var zerobase uintptr
func getZerobase() *uintptr { func getZerobase() *uintptr {
return &zerobase return &zerobase
} }
// Temporary for gccgo until we port proc.go.
func needm()
func dropm()
func sigprof()
func mcount() int32
// Signal trampoline, written in C.
func sigtramp()
// The sa_handler field is generally hidden in a union, so use C accessors.
func getSigactionHandler(*_sigaction) uintptr
func setSigactionHandler(*_sigaction, uintptr)
// Retrieve fields from the siginfo_t and ucontext_t pointers passed
// to a signal handler using C, as they are often hidden in a union.
// Returns and, if available, PC where signal occurred.
func getSiginfo(*_siginfo_t, unsafe.Pointer) (sigaddr uintptr, sigpc uintptr)
// Implemented in C for gccgo.
func dumpregs(*_siginfo_t, unsafe.Pointer)
// Temporary for gccgo until we port panic.go.
func startpanic()
...@@ -41,6 +41,11 @@ echo $timeval | \ ...@@ -41,6 +41,11 @@ echo $timeval | \
sed -e 's/type _timeval /type timeval /' \ sed -e 's/type _timeval /type timeval /' \
-e 's/tv_sec *[a-zA-Z0-9_]*/tv_sec timeval_sec_t/' \ -e 's/tv_sec *[a-zA-Z0-9_]*/tv_sec timeval_sec_t/' \
-e 's/tv_usec *[a-zA-Z0-9_]*/tv_usec timeval_usec_t/' >> ${OUT} -e 's/tv_usec *[a-zA-Z0-9_]*/tv_usec timeval_usec_t/' >> ${OUT}
echo >> ${OUT}
echo "func (tv *timeval) set_usec(x int32) {" >> ${OUT}
echo " tv.tv_usec = timeval_usec_t(x)" >> ${OUT}
echo "}" >> ${OUT}
timespec=`grep '^type _timespec ' gen-sysinfo.go || true` timespec=`grep '^type _timespec ' gen-sysinfo.go || true`
if test "$timespec" = ""; then if test "$timespec" = ""; then
# IRIX 6.5 has __timespec instead. # IRIX 6.5 has __timespec instead.
......
#!/bin/sh
# Copyright 2016 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Create sigtab.go from gen-sysinfo.go.
# This shell scripts creates the sigtab.go file, which maps signals to
# their dispositions. We generate a file so that we can build a
# composite literal that only refers to signals that are defined on
# this system.
# This script simply writes to standard output.
set -e
echo '// Generated by mksigtab.sh. Do not edit.'
echo
echo 'package runtime'
echo
echo 'var sigtable = [...]sigTabT{'
# Handle signals valid on all Unix systems.
echo ' 0: {0, "SIGNONE: no trap"},'
echo ' _SIGHUP: {_SigNotify + _SigKill, "SIGHUP: terminal line hangup"},'
echo ' _SIGINT: {_SigNotify + _SigKill, "SIGINT: interrupt"},'
echo ' _SIGQUIT: {_SigNotify + _SigThrow, "SIGQUIT: quit"},'
echo ' _SIGILL: {_SigThrow + _SigUnblock, "SIGILL: illegal instruction"},'
echo ' _SIGTRAP: {_SigThrow + _SigUnblock, "SIGTRAP: trace trap"},'
echo ' _SIGABRT: {_SigNotify + _SigThrow, "SIGABRT: abort"},'
echo ' _SIGBUS: {_SigPanic + _SigUnblock, "SIGBUS: bus error"},'
echo ' _SIGFPE: {_SigPanic + _SigUnblock, "SIGFPE: floating-point exception"},'
echo ' _SIGKILL: {0, "SIGKILL: kill"},'
echo ' _SIGUSR1: {_SigNotify, "SIGUSR1: user-defined signal 1"},'
echo ' _SIGSEGV: {_SigPanic + _SigUnblock, "SIGSEGV: segmentation violation"},'
echo ' _SIGUSR2: {_SigNotify, "SIGUSR2: user-defined signal 2"},'
echo ' _SIGPIPE: {_SigNotify, "SIGPIPE: write to broken pipe"},'
echo ' _SIGALRM: {_SigNotify, "SIGALRM: alarm clock"},'
echo ' _SIGTERM: {_SigNotify + _SigKill, "SIGTERM: termination"},'
echo ' _SIGCHLD: {_SigNotify + _SigUnblock, "SIGCHLD: child status has changed"},'
echo ' _SIGCONT: {_SigNotify + _SigDefault, "SIGCONT: continue"},'
echo ' _SIGSTOP: {0, "SIGSTOP: stop"},'
echo ' _SIGTSTP: {_SigNotify + _SigDefault, "SIGTSTP: keyboard stop"},'
echo ' _SIGTTIN: {_SigNotify + _SigDefault, "SIGTTIN: background read from tty"},'
echo ' _SIGTTOU: {_SigNotify + _SigDefault, "SIGTTOU: background write to tty"},'
echo ' _SIGURG: {_SigNotify, "SIGURG: urgent condition on socket"},'
echo ' _SIGXCPU: {_SigNotify, "SIGXCPU: cpu limit exceeded"},'
echo ' _SIGXFSZ: {_SigNotify, "SIGXFSZ: file size limit exceeded"},'
echo ' _SIGVTALRM: {_SigNotify, "SIGVTALRM: virtual alarm clock"},'
echo ' _SIGPROF: {_SigNotify + _SigUnblock, "SIGPROF: profiling alarm clock"},'
echo ' _SIGWINCH: {_SigNotify, "SIGWINCH: window size change"},'
echo ' _SIGSYS: {_SigThrow, "SIGSYS: bad system call"},'
# Handle signals that are not supported on all systems.
checksig() {
if grep 'const $1 = ' gen-sysinfo.go >/dev/null 2>&1; then
echo " $1: $2,"
fi
}
checksig _SIGSTKFLT ' {_SigThrow + _SigUnblock, "SIGSTKFLT: stack fault"}'
checksig _SIGIO ' {_SigNotify, "SIGIO: i/o now possible"}'
checksig _SIGPWR ' {_SigNotify, "SIGPWR: power failure restart"}'
checksig _SIGEMT ' {_SigThrow, "SIGEMT: emulate instruction executed"}'
checksig _SIGINFO ' {_SigNotify, "SIGINFO: status request from keyboard"}'
checksig _SIGTHR ' {_SigNotify, "SIGTHR: reserved"}'
checksig _SIGPOLL ' {_SigNotify, "SIGPOLL: pollable event occurred"}'
checksig _SIGWAITING '{_SigNotify, "SIGWAITING: reserved signal no longer used by"}'
checksig _SIGLWP ' {_SigNotify, "SIGLWP: reserved signal no longer used by"}'
checksig _SIGFREEZE ' {_SigNotify, "SIGFREEZE: special signal used by CPR"}'
checksig _SIGTHAW ' {_SigNotify, "SIGTHAW: special signal used by CPR"}'
checksig _SIGCANCEL ' {_SigSetStack + _SigUnblock, "SIGCANCEL: reserved signal for thread cancellation"}'
checksig _SIGXRES ' {_SigNotify, "SIGXRES: resource control exceeded"}'
checksig _SIGJVM1 ' {_SigNotify, "SIGJVM1: reserved signal for Java Virtual Machine"}'
checksig _SIGJVM2 ' {_SigNotify, "SIGJVM2: reserved signal for Java Virtual Machine"}'
# Special handling of signals 32 and 33 on GNU/Linux systems,
# because they are special to glibc.
if test "${GOOS}" = "linux"; then
echo ' 32: {_SigSetStack + _SigUnblock, "signal 32"}, /* SIGCANCEL; see issue 6997 */'
echo ' 33: {_SigSetStack + _SigUnblock, "signal 33"}, /* SIGSETXID; see issues 3871, 9400, 12498 */'
fi
nsig=`grep 'const _*NSIG = [0-9]*$' gen-sysinfo.go | sed -e 's/.* = \([0-9]*\)/\1/'`
i=1
while test "$i" -lt "$nsig"; do
if ! grep "const _SIG.* = $i" gen-sysinfo.go >/dev/null 2>&1; then
if test "${GOOS}" != "linux" || test "$i" -ne 32 -a "$i" -ne 33; then
echo " $i: {_SigNotify, \"signal $i\"},"
fi
fi
i=`expr "$i" + 1`
done
echo '}'
...@@ -122,9 +122,13 @@ runtime_dopanic(int32 unused __attribute__ ((unused))) ...@@ -122,9 +122,13 @@ runtime_dopanic(int32 unused __attribute__ ((unused)))
int32 t; int32 t;
g = runtime_g(); g = runtime_g();
if(g->sig != 0) if(g->sig != 0) {
runtime_printf("[signal %x code=%p addr=%p]\n", runtime_printf("[signal %x code=%p addr=%p",
g->sig, (void*)g->sigcode0, (void*)g->sigcode1); g->sig, (void*)g->sigcode0, (void*)g->sigcode1);
if (g->sigpc != 0)
runtime_printf(" pc=%p", g->sigpc);
runtime_printf("]\n");
}
if((t = runtime_gotraceback(&crash)) > 0){ if((t = runtime_gotraceback(&crash)) > 0){
if(g != runtime_m()->g0) { if(g != runtime_m()->g0) {
......
...@@ -2022,7 +2022,8 @@ goexit0(G *gp) ...@@ -2022,7 +2022,8 @@ goexit0(G *gp)
// entersyscall is going to return immediately after. // entersyscall is going to return immediately after.
void runtime_entersyscall(int32) __attribute__ ((no_split_stack)); void runtime_entersyscall(int32) __attribute__ ((no_split_stack));
static void doentersyscall(void) __attribute__ ((no_split_stack, noinline)); static void doentersyscall(uintptr, uintptr)
__attribute__ ((no_split_stack, noinline));
void void
runtime_entersyscall(int32 dummy __attribute__ ((unused))) runtime_entersyscall(int32 dummy __attribute__ ((unused)))
...@@ -2040,11 +2041,12 @@ runtime_entersyscall(int32 dummy __attribute__ ((unused))) ...@@ -2040,11 +2041,12 @@ runtime_entersyscall(int32 dummy __attribute__ ((unused)))
// callee-saved registers to access the TLS variable g. We // callee-saved registers to access the TLS variable g. We
// don't want to put the ucontext_t on the stack because it is // don't want to put the ucontext_t on the stack because it is
// large and we can not split the stack here. // large and we can not split the stack here.
doentersyscall(); doentersyscall((uintptr)runtime_getcallerpc(&dummy),
(uintptr)runtime_getcallersp(&dummy));
} }
static void static void
doentersyscall() doentersyscall(uintptr pc, uintptr sp)
{ {
// Disable preemption because during this function g is in _Gsyscall status, // Disable preemption because during this function g is in _Gsyscall status,
// but can have inconsistent g->sched, do not let GC observe it. // but can have inconsistent g->sched, do not let GC observe it.
...@@ -2067,6 +2069,9 @@ doentersyscall() ...@@ -2067,6 +2069,9 @@ doentersyscall()
} }
#endif #endif
g->syscallsp = sp;
g->syscallpc = pc;
g->atomicstatus = _Gsyscall; g->atomicstatus = _Gsyscall;
if(runtime_atomicload(&runtime_sched.sysmonwait)) { // TODO: fast atomic if(runtime_atomicload(&runtime_sched.sysmonwait)) { // TODO: fast atomic
...@@ -2118,6 +2123,9 @@ runtime_entersyscallblock(int32 dummy __attribute__ ((unused))) ...@@ -2118,6 +2123,9 @@ runtime_entersyscallblock(int32 dummy __attribute__ ((unused)))
// held in registers will be seen by the garbage collector. // held in registers will be seen by the garbage collector.
getcontext(ucontext_arg(&g->gcregs[0])); getcontext(ucontext_arg(&g->gcregs[0]));
g->syscallpc = (uintptr)runtime_getcallerpc(&dummy);
g->syscallsp = (uintptr)runtime_getcallersp(&dummy);
g->atomicstatus = _Gsyscall; g->atomicstatus = _Gsyscall;
p = releasep(); p = releasep();
...@@ -2155,6 +2163,7 @@ runtime_exitsyscall(int32 dummy __attribute__ ((unused))) ...@@ -2155,6 +2163,7 @@ runtime_exitsyscall(int32 dummy __attribute__ ((unused)))
#endif #endif
gp->gcnextsp = nil; gp->gcnextsp = nil;
runtime_memclr(&gp->gcregs[0], sizeof gp->gcregs); runtime_memclr(&gp->gcregs[0], sizeof gp->gcregs);
gp->syscallsp = 0;
gp->m->locks--; gp->m->locks--;
return; return;
} }
...@@ -2176,6 +2185,8 @@ runtime_exitsyscall(int32 dummy __attribute__ ((unused))) ...@@ -2176,6 +2185,8 @@ runtime_exitsyscall(int32 dummy __attribute__ ((unused)))
gp->gcnextsp = nil; gp->gcnextsp = nil;
runtime_memclr(&gp->gcregs[0], sizeof gp->gcregs); runtime_memclr(&gp->gcregs[0], sizeof gp->gcregs);
gp->syscallsp = 0;
// Note that this gp->m might be different than the earlier // Note that this gp->m might be different than the earlier
// gp->m after returning from runtime_mcall. // gp->m after returning from runtime_mcall.
((P*)gp->m->p)->syscalltick++; ((P*)gp->m->p)->syscalltick++;
......
...@@ -282,10 +282,8 @@ void* runtime_mal(uintptr); ...@@ -282,10 +282,8 @@ void* runtime_mal(uintptr);
String runtime_gostringnocopy(const byte*) String runtime_gostringnocopy(const byte*)
__asm__ (GOSYM_PREFIX "runtime.gostringnocopy"); __asm__ (GOSYM_PREFIX "runtime.gostringnocopy");
void runtime_schedinit(void); void runtime_schedinit(void);
void runtime_initsig(bool); void runtime_initsig(bool)
void runtime_sigenable(uint32 sig); __asm__ (GOSYM_PREFIX "runtime.initsig");
void runtime_sigdisable(uint32 sig);
void runtime_sigignore(uint32 sig);
int32 runtime_gotraceback(bool *crash); int32 runtime_gotraceback(bool *crash);
void runtime_goroutineheader(G*) void runtime_goroutineheader(G*)
__asm__ (GOSYM_PREFIX "runtime.goroutineheader"); __asm__ (GOSYM_PREFIX "runtime.goroutineheader");
...@@ -303,8 +301,10 @@ G* runtime_malg(int32, byte**, uintptr*); ...@@ -303,8 +301,10 @@ G* runtime_malg(int32, byte**, uintptr*);
void runtime_mpreinit(M*); void runtime_mpreinit(M*);
void runtime_minit(void); void runtime_minit(void);
void runtime_unminit(void); void runtime_unminit(void);
void runtime_needm(void); void runtime_needm(void)
void runtime_dropm(void); __asm__ (GOSYM_PREFIX "runtime.needm");
void runtime_dropm(void)
__asm__ (GOSYM_PREFIX "runtime.dropm");
void runtime_signalstack(byte*, int32); void runtime_signalstack(byte*, int32);
MCache* runtime_allocmcache(void); MCache* runtime_allocmcache(void);
void runtime_freemcache(MCache*); void runtime_freemcache(MCache*);
...@@ -312,8 +312,9 @@ void runtime_mallocinit(void); ...@@ -312,8 +312,9 @@ void runtime_mallocinit(void);
void runtime_mprofinit(void); void runtime_mprofinit(void);
#define runtime_malloc(s) __go_alloc(s) #define runtime_malloc(s) __go_alloc(s)
#define runtime_free(p) __go_free(p) #define runtime_free(p) __go_free(p)
#define runtime_getcallersp(p) __builtin_frame_address(1) #define runtime_getcallersp(p) __builtin_frame_address(0)
int32 runtime_mcount(void); int32 runtime_mcount(void)
__asm__ (GOSYM_PREFIX "runtime.mcount");
int32 runtime_gcount(void); int32 runtime_gcount(void);
void runtime_mcall(void(*)(G*)); void runtime_mcall(void(*)(G*));
uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1"); uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1");
...@@ -339,7 +340,8 @@ int32 runtime_round2(int32 x); // round x up to a power of 2. ...@@ -339,7 +340,8 @@ int32 runtime_round2(int32 x); // round x up to a power of 2.
#define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST) #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
#define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST) #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
void runtime_setg(G*); void runtime_setg(G*)
__asm__ (GOSYM_PREFIX "runtime.setg");
void runtime_newextram(void); void runtime_newextram(void);
#define runtime_exit(s) exit(s) #define runtime_exit(s) exit(s)
#define runtime_breakpoint() __builtin_trap() #define runtime_breakpoint() __builtin_trap()
...@@ -358,19 +360,20 @@ void runtime_entersyscallblock(int32) ...@@ -358,19 +360,20 @@ void runtime_entersyscallblock(int32)
void runtime_exitsyscall(int32) void runtime_exitsyscall(int32)
__asm__ (GOSYM_PREFIX "runtime.exitsyscall"); __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
G* __go_go(void (*pfn)(void*), void*); G* __go_go(void (*pfn)(void*), void*);
void siginit(void);
bool __go_sigsend(int32 sig);
int32 runtime_callers(int32, Location*, int32, bool keep_callers); int32 runtime_callers(int32, Location*, int32, bool keep_callers);
int64 runtime_nanotime(void) // monotonic time int64 runtime_nanotime(void) // monotonic time
__asm__(GOSYM_PREFIX "runtime.nanotime"); __asm__(GOSYM_PREFIX "runtime.nanotime");
int64 runtime_unixnanotime(void) // real time, can skip int64 runtime_unixnanotime(void) // real time, can skip
__asm__ (GOSYM_PREFIX "runtime.unixnanotime"); __asm__ (GOSYM_PREFIX "runtime.unixnanotime");
void runtime_dopanic(int32) __attribute__ ((noreturn)); void runtime_dopanic(int32) __attribute__ ((noreturn));
void runtime_startpanic(void); void runtime_startpanic(void)
__asm__ (GOSYM_PREFIX "runtime.startpanic");
void runtime_freezetheworld(void); void runtime_freezetheworld(void);
void runtime_unwindstack(G*, byte*); void runtime_unwindstack(G*, byte*);
void runtime_sigprof(); void runtime_sigprof()
void runtime_resetcpuprofiler(int32); __asm__ (GOSYM_PREFIX "runtime.sigprof");
void runtime_resetcpuprofiler(int32)
__asm__ (GOSYM_PREFIX "runtime.resetcpuprofiler");
void runtime_setcpuprofilerate_m(int32) void runtime_setcpuprofilerate_m(int32)
__asm__ (GOSYM_PREFIX "runtime.setcpuprofilerate_m"); __asm__ (GOSYM_PREFIX "runtime.setcpuprofilerate_m");
void runtime_cpuprofAdd(Slice) void runtime_cpuprofAdd(Slice)
...@@ -385,7 +388,8 @@ void runtime_blockevent(int64, int32); ...@@ -385,7 +388,8 @@ void runtime_blockevent(int64, int32);
extern int64 runtime_blockprofilerate; extern int64 runtime_blockprofilerate;
G* runtime_netpoll(bool) G* runtime_netpoll(bool)
__asm__ (GOSYM_PREFIX "runtime.netpoll"); __asm__ (GOSYM_PREFIX "runtime.netpoll");
void runtime_crash(void); void runtime_crash(void)
__asm__ (GOSYM_PREFIX "runtime.crash");
void runtime_parsedebugvars(void) void runtime_parsedebugvars(void)
__asm__(GOSYM_PREFIX "runtime.parsedebugvars"); __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
void _rt0_go(void); void _rt0_go(void);
......
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
#include <sys/time.h>
#include "runtime.h"
#include "defs.h"
#include "signal_unix.h"
extern SigTab runtime_sigtab[];
void
runtime_initsig(bool preinit)
{
int32 i;
SigTab *t;
// For c-archive/c-shared this is called by go-libmain.c with
// preinit == true.
if(runtime_isarchive && !preinit)
return;
// First call: basic setup.
for(i = 0; runtime_sigtab[i].sig != -1; i++) {
t = &runtime_sigtab[i];
if((t->flags == 0) || (t->flags & _SigDefault))
continue;
t->fwdsig = runtime_getsig(i);
// For some signals, we respect an inherited SIG_IGN handler
// rather than insist on installing our own default handler.
// Even these signals can be fetched using the os/signal package.
switch(t->sig) {
case SIGHUP:
case SIGINT:
if(t->fwdsig == GO_SIG_IGN) {
continue;
}
}
if(runtime_isarchive && (t->flags&_SigPanic) == 0)
continue;
t->flags |= _SigHandling;
runtime_setsig(i, runtime_sighandler, true);
}
}
void
runtime_sigenable(uint32 sig)
{
int32 i;
SigTab *t;
t = nil;
for(i = 0; runtime_sigtab[i].sig != -1; i++) {
if(runtime_sigtab[i].sig == (int32)sig) {
t = &runtime_sigtab[i];
break;
}
}
if(t == nil)
return;
if((t->flags & _SigNotify) && !(t->flags & _SigHandling)) {
t->flags |= _SigHandling;
t->fwdsig = runtime_getsig(i);
runtime_setsig(i, runtime_sighandler, true);
}
}
void
runtime_sigdisable(uint32 sig)
{
int32 i;
SigTab *t;
t = nil;
for(i = 0; runtime_sigtab[i].sig != -1; i++) {
if(runtime_sigtab[i].sig == (int32)sig) {
t = &runtime_sigtab[i];
break;
}
}
if(t == nil)
return;
if((sig == SIGHUP || sig == SIGINT) && t->fwdsig == GO_SIG_IGN) {
t->flags &= ~_SigHandling;
runtime_setsig(i, t->fwdsig, true);
}
}
void
runtime_sigignore(uint32 sig)
{
int32 i;
SigTab *t;
t = nil;
for(i = 0; runtime_sigtab[i].sig != -1; i++) {
if(runtime_sigtab[i].sig == (int32)sig) {
t = &runtime_sigtab[i];
break;
}
}
if(t == nil)
return;
if((t->flags & _SigNotify) != 0) {
t->flags &= ~_SigHandling;
runtime_setsig(i, GO_SIG_IGN, true);
}
}
void
runtime_resetcpuprofiler(int32 hz)
{
struct itimerval it;
runtime_memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime_setitimer(ITIMER_PROF, &it, nil);
} else {
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime_setitimer(ITIMER_PROF, &it, nil);
}
runtime_m()->profilehz = hz;
}
void
runtime_unblocksignals(void)
{
sigset_t sigset_none;
sigemptyset(&sigset_none);
pthread_sigmask(SIG_SETMASK, &sigset_none, nil);
}
void
runtime_crash(void)
{
int32 i;
#ifdef GOOS_darwin
// OS X core dumps are linear dumps of the mapped memory,
// from the first virtual byte to the last, with zeros in the gaps.
// Because of the way we arrange the address space on 64-bit systems,
// this means the OS X core file will be >128 GB and even on a zippy
// workstation can take OS X well over an hour to write (uninterruptible).
// Save users from making that mistake.
if(sizeof(void*) == 8)
return;
#endif
runtime_unblocksignals();
for(i = 0; runtime_sigtab[i].sig != -1; i++)
if(runtime_sigtab[i].sig == SIGABRT)
break;
runtime_setsig(i, GO_SIG_DFL, false);
runtime_raise(SIGABRT);
}
void
runtime_raise(int32 sig)
{
raise(sig);
}
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include <signal.h>
#define GO_SIG_DFL ((void*)SIG_DFL)
#define GO_SIG_IGN ((void*)SIG_IGN)
#ifdef SA_SIGINFO
typedef siginfo_t Siginfo;
#else
typedef void *Siginfo;
#endif
typedef void GoSighandler(int32, Siginfo*, void*, G*);
void runtime_setsig(int32, GoSighandler*, bool);
GoSighandler* runtime_getsig(int32);
void runtime_sighandler(int32 sig, Siginfo *info, void *context, G *gp);
void runtime_raise(int32);
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file implements runtime support for signal handling.
//
// Most synchronization primitives are not available from
// the signal handler (it cannot block, allocate memory, or use locks)
// so the handler communicates with a processing goroutine
// via struct sig, below.
//
// sigsend() is called by the signal handler to queue a new signal.
// signal_recv() is called by the Go program to receive a newly queued signal.
// Synchronization between sigsend() and signal_recv() is based on the sig.state
// variable. It can be in 3 states: 0, HASWAITER and HASSIGNAL.
// HASWAITER means that signal_recv() is blocked on sig.Note and there are no
// new pending signals.
// HASSIGNAL means that sig.mask *may* contain new pending signals,
// signal_recv() can't be blocked in this state.
// 0 means that there are no new pending signals and signal_recv() is not blocked.
// Transitions between states are done atomically with CAS.
// When signal_recv() is unblocked, it resets sig.Note and rechecks sig.mask.
// If several sigsend()'s and signal_recv() execute concurrently, it can lead to
// unnecessary rechecks of sig.mask, but must not lead to missed signals
// nor deadlocks.
package signal
#include "config.h"
#include "runtime.h"
#include "arch.h"
#include "malloc.h"
#include "defs.h"
static struct {
Note;
uint32 mask[(NSIG+31)/32];
uint32 wanted[(NSIG+31)/32];
uint32 state;
bool inuse;
} sig;
enum {
HASWAITER = 1,
HASSIGNAL = 2,
};
// Called from sighandler to send a signal back out of the signal handling thread.
bool
__go_sigsend(int32 s)
{
uint32 bit, mask, old, new;
if(!sig.inuse || s < 0 || (size_t)s >= 32*nelem(sig.wanted) || !(sig.wanted[s/32]&(1U<<(s&31))))
return false;
bit = 1 << (s&31);
for(;;) {
mask = sig.mask[s/32];
if(mask & bit)
break; // signal already in queue
if(runtime_cas(&sig.mask[s/32], mask, mask|bit)) {
// Added to queue.
// Only send a wakeup if the receiver needs a kick.
for(;;) {
old = runtime_atomicload(&sig.state);
if(old == HASSIGNAL)
break;
if(old == HASWAITER)
new = 0;
else // if(old == 0)
new = HASSIGNAL;
if(runtime_cas(&sig.state, old, new)) {
if (old == HASWAITER)
runtime_notewakeup(&sig);
break;
}
}
break;
}
}
return true;
}
// Called to receive the next queued signal.
// Must only be called from a single goroutine at a time.
func signal_recv() (m uint32) {
static uint32 recv[nelem(sig.mask)];
uint32 i, old, new;
for(;;) {
// Serve from local copy if there are bits left.
for(i=0; i<NSIG; i++) {
if(recv[i/32]&(1U<<(i&31))) {
recv[i/32] ^= 1U<<(i&31);
m = i;
goto done;
}
}
// Check and update sig.state.
for(;;) {
old = runtime_atomicload(&sig.state);
if(old == HASWAITER)
runtime_throw("inconsistent state in signal_recv");
if(old == HASSIGNAL)
new = 0;
else // if(old == 0)
new = HASWAITER;
if(runtime_cas(&sig.state, old, new)) {
if (new == HASWAITER) {
runtime_notetsleepg(&sig, -1);
runtime_noteclear(&sig);
}
break;
}
}
// Get a new local copy.
for(i=0; (size_t)i<nelem(sig.mask); i++) {
for(;;) {
m = sig.mask[i];
if(runtime_cas(&sig.mask[i], m, 0))
break;
}
recv[i] = m;
}
}
done:;
// goc requires that we fall off the end of functions
// that return values instead of using our own return
// statements.
}
// Must only be called from a single goroutine at a time.
func signal_enable(s uint32) {
if(!sig.inuse) {
// The first call to signal_enable is for us
// to use for initialization. It does not pass
// signal information in m.
sig.inuse = true; // enable reception of signals; cannot disable
runtime_noteclear(&sig);
return;
}
if(s >= nelem(sig.wanted)*32)
return;
sig.wanted[s/32] |= 1U<<(s&31);
runtime_sigenable(s);
}
// Must only be called from a single goroutine at a time.
func signal_disable(s uint32) {
if(s >= nelem(sig.wanted)*32)
return;
sig.wanted[s/32] &= ~(1U<<(s&31));
runtime_sigdisable(s);
}
// Must only be called from a single goroutine at a time.
func signal_ignore(s uint32) {
if (s >= nelem(sig.wanted)*32)
return;
sig.wanted[s/32] &= ~(1U<<(s&31));
runtime_sigignore(s);
}
// This runs on a foreign stack, without an m or a g. No stack split.
void
runtime_badsignal(int sig)
{
__go_sigsend(sig);
}
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "runtime.h" #include "runtime.h"
#include "defs.h" #include "defs.h"
#include "signal_unix.h"
// Linux futex. // Linux futex.
......
...@@ -37,6 +37,9 @@ runtime_procyield (uint32 cnt) ...@@ -37,6 +37,9 @@ runtime_procyield (uint32 cnt)
/* Ask the OS to reschedule this thread. */ /* Ask the OS to reschedule this thread. */
void runtime_osyield(void)
__attribute__ ((no_split_stack));
void void
runtime_osyield (void) runtime_osyield (void)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment