xref: /minix/minix/lib/libsys/panic.c (revision 7f5f010b)
1 #include <stdlib.h>
2 #include <signal.h>
3 #include <unistd.h>
4 #include <stdarg.h>
5 #include <minix/sysutil.h>
6 
7 #include "syslib.h"
8 
9 void panic_hook(void);
10 
11 __weak_alias(panic_hook, __panic_hook);
12 
13 void __panic_hook(void)
14 {
15 	;
16 }
17 
18 /*===========================================================================*
19  *				panic				     *
20  *===========================================================================*/
21 void panic(const char *fmt, ...)
22 {
23 /* Something awful has happened. Panics are caused when an internal
24  * inconsistency is detected, e.g., a programming error or illegal
25  * value of a defined constant.
26  */
27   endpoint_t me = NONE;
28   char name[20];
29   int priv_flags;
30   void (*suicide)(void);
31   va_list args;
32 
33   if(sys_whoami(&me, name, sizeof(name), &priv_flags) == OK && me != NONE)
34 	printf("%s(%d): panic: ", name, me);
35   else
36 	printf("(sys_whoami failed): panic: ");
37 
38   if(fmt) {
39 	va_start(args, fmt);
40 	vprintf(fmt, args);
41 	va_end(args);
42   } else {
43 	printf("no message\n");
44   }
45   printf("\n");
46 
47   printf("syslib:panic.c: stacktrace: ");
48   util_stacktrace();
49 
50   panic_hook();
51 
52   /* Try exit */
53   _exit(1);
54 
55   /* Try to signal ourself */
56   abort();
57 
58   /* If exiting nicely through PM fails for some reason, try to
59    * commit suicide. E.g., message to PM might fail due to deadlock.
60    */
61   suicide = (void (*)(void)) -1;
62   suicide();
63 
64   /* If committing suicide fails for some reason, hang. */
65   for(;;) { }
66 }
67 
68