Debian Codegraph @

src/runtime/testdata/testprogcgo/catchpanic.go

raw
1 // Copyright 2017 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 //go:build !plan9 && !windows
6 // +build !plan9,!windows
7
8 package main
9
10 /*
11 #include <signal.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 static void abrthandler(int signum) {
16 if (signum == SIGABRT) {
17 exit(0); // success
18 }
19 }
20
21 void registerAbortHandler() {
22 struct sigaction act;
23 memset(&act, 0, sizeof act);
24 act.sa_handler = abrthandler;
25 sigaction(SIGABRT, &act, NULL);
26 }
27
28 static void __attribute__ ((constructor)) sigsetup(void) {
29 if (getenv("CGOCATCHPANIC_EARLY_HANDLER") == NULL)
30 return;
31 registerAbortHandler();
32 }
33 */
34 import "C"
35 import "os"
36
37 func init() {
38 register("CgoCatchPanic", CgoCatchPanic)
39 }
40
41 // Test that the SIGABRT raised by panic can be caught by an early signal handler.
42 func CgoCatchPanic() {
43 if _, ok := os.LookupEnv("CGOCATCHPANIC_EARLY_HANDLER"); !ok {
44 C.registerAbortHandler()
45 }
46 panic("catch me")
47 }