xref: /minix/minix/lib/libsys/panic.c (revision 0a6a1f1d)
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   int init_flags;
31   void (*suicide)(void);
32   va_list args;
33 
34   if(sys_whoami(&me, name, sizeof(name), &priv_flags, &init_flags) == OK && me != NONE)
35 	printf("%s(%d): panic: ", name, me);
36   else
37 	printf("(sys_whoami failed): panic: ");
38 
39   if(fmt) {
40 	va_start(args, fmt);
41 	vprintf(fmt, args);
42 	va_end(args);
43   } else {
44 	printf("no message\n");
45   }
46   printf("\n");
47 
48   printf("syslib:panic.c: stacktrace: ");
49   util_stacktrace();
50 
51   panic_hook();
52 
53   /* Try exit */
54   _exit(1);
55 
56   /* Try to signal ourself */
57   abort();
58 
59   /* If exiting nicely through PM fails for some reason, try to
60    * commit suicide. E.g., message to PM might fail due to deadlock.
61    */
62   suicide = (void (*)(void)) -1;
63   suicide();
64 
65   /* If committing suicide fails for some reason, hang. */
66   for(;;) { }
67 }
68 
69