xref: /minix3/minix/lib/libsys/panic.c (revision 76bf77a21f01f3eb5a218cc711a04895ded2676d)
1433d6423SLionel Sambuc #include <stdlib.h>
2433d6423SLionel Sambuc #include <signal.h>
3433d6423SLionel Sambuc #include <unistd.h>
4433d6423SLionel Sambuc #include <stdarg.h>
5433d6423SLionel Sambuc #include <minix/sysutil.h>
6433d6423SLionel Sambuc 
7433d6423SLionel Sambuc #include "syslib.h"
8433d6423SLionel Sambuc 
9433d6423SLionel Sambuc void panic_hook(void);
10433d6423SLionel Sambuc 
11433d6423SLionel Sambuc __weak_alias(panic_hook, __panic_hook);
12433d6423SLionel Sambuc 
__panic_hook(void)13433d6423SLionel Sambuc void __panic_hook(void)
14433d6423SLionel Sambuc {
15433d6423SLionel Sambuc 	;
16433d6423SLionel Sambuc }
17433d6423SLionel Sambuc 
18433d6423SLionel Sambuc /*===========================================================================*
19433d6423SLionel Sambuc  *				panic				     *
20433d6423SLionel Sambuc  *===========================================================================*/
panic(const char * fmt,...)21433d6423SLionel Sambuc void panic(const char *fmt, ...)
22433d6423SLionel Sambuc {
23433d6423SLionel Sambuc /* Something awful has happened. Panics are caused when an internal
24433d6423SLionel Sambuc  * inconsistency is detected, e.g., a programming error or illegal
25433d6423SLionel Sambuc  * value of a defined constant.
26433d6423SLionel Sambuc  */
27433d6423SLionel Sambuc   endpoint_t me = NONE;
28433d6423SLionel Sambuc   char name[20];
29433d6423SLionel Sambuc   int priv_flags;
30*76bf77a2SCristiano Giuffrida   int init_flags;
31433d6423SLionel Sambuc   void (*suicide)(void);
32433d6423SLionel Sambuc   va_list args;
33433d6423SLionel Sambuc 
34*76bf77a2SCristiano Giuffrida   if(sys_whoami(&me, name, sizeof(name), &priv_flags, &init_flags) == OK && me != NONE)
35433d6423SLionel Sambuc 	printf("%s(%d): panic: ", name, me);
36433d6423SLionel Sambuc   else
37433d6423SLionel Sambuc 	printf("(sys_whoami failed): panic: ");
38433d6423SLionel Sambuc 
39433d6423SLionel Sambuc   if(fmt) {
40433d6423SLionel Sambuc 	va_start(args, fmt);
41433d6423SLionel Sambuc 	vprintf(fmt, args);
42433d6423SLionel Sambuc 	va_end(args);
43433d6423SLionel Sambuc   } else {
44433d6423SLionel Sambuc 	printf("no message\n");
45433d6423SLionel Sambuc   }
46433d6423SLionel Sambuc   printf("\n");
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc   printf("syslib:panic.c: stacktrace: ");
49433d6423SLionel Sambuc   util_stacktrace();
50433d6423SLionel Sambuc 
51433d6423SLionel Sambuc   panic_hook();
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc   /* Try exit */
54433d6423SLionel Sambuc   _exit(1);
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc   /* Try to signal ourself */
57433d6423SLionel Sambuc   abort();
58433d6423SLionel Sambuc 
59433d6423SLionel Sambuc   /* If exiting nicely through PM fails for some reason, try to
60433d6423SLionel Sambuc    * commit suicide. E.g., message to PM might fail due to deadlock.
61433d6423SLionel Sambuc    */
62433d6423SLionel Sambuc   suicide = (void (*)(void)) -1;
63433d6423SLionel Sambuc   suicide();
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc   /* If committing suicide fails for some reason, hang. */
66433d6423SLionel Sambuc   for(;;) { }
67433d6423SLionel Sambuc }
68433d6423SLionel Sambuc 
69