github.com/searKing/golang/go@v1.2.117/os/signal/cgo/signal.go (about) 1 // Copyright 2020 The searKing Author. 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 //go:build cgo 6 7 package cgo 8 9 /* 10 #cgo CXXFLAGS: -I${SRCDIR}/include/ 11 #cgo windows CXXFLAGS: -g 12 #cgo !windows CXXFLAGS: -g -D_GNU_SOURCE 13 #cgo linux LDFLAGS: -ldl 14 15 #include "signal.cgo.h" 16 #include <stdio.h> 17 #include <stdlib.h> // Needed for C.free 18 19 */ 20 import "C" 21 import ( 22 "unsafe" 23 24 _ "github.com/searKing/golang/go/os/signal/cgo/include" 25 ) 26 27 // SetSig act as signal.Notify, which invokes the Go signal handler. 28 // https://godoc.org/os/signal#hdr-Go_programs_that_use_cgo_or_SWIG 29 func SetSig(sig int) { 30 C.CGO_SignalHandlerSetSig(C.int(sig)) 31 } 32 33 // SetSignalDumpToFd redirect log to fd, -1 if not set; muted if < 0. 34 func SetSignalDumpToFd(fd int) { C.CGO_SignalHandlerSetSignalDumpToFd(C.int(fd)) } 35 36 // SetBacktraceDumpToFile set dump file path of stacktrace when signal is triggered, nop if not set. 37 func SetBacktraceDumpToFile(name string) { 38 cs := C.CString(name) 39 defer C.free(unsafe.Pointer(cs)) 40 C.CGO_SignalHandlerSetStacktraceDumpToFile(cs) 41 } 42 43 // DumpPreviousStacktrace dumps human readable stacktrace to fd, which is set by SetSignalDumpToFd. 44 func DumpPreviousStacktrace() { 45 C.CGO_SignalHandlerDumpPreviousStacktrace() 46 } 47 48 // PreviousStacktrace returns a human readable stacktrace 49 func PreviousStacktrace() string { 50 stacktraceChars := C.CGO_PreviousStacktrace() 51 defer C.free(unsafe.Pointer(stacktraceChars)) 52 return C.GoString(stacktraceChars) 53 } 54 55 // SetSigInvokeChain sets a rule to raise signal to {to} and wait until {wait}, done with sleep {sleepInSeconds}s 56 func SetSigInvokeChain(from, to, wait, sleepInSeconds int) { 57 C.CGO_SetSigInvokeChain(C.int(from), C.int(to), C.int(wait), C.int(sleepInSeconds)) 58 }