Get rid of DEBUG macro, supporting code, and documentation,
[pintos-anon] / src / lib / debug.c
1 #include <debug.h>
2 #include <stdarg.h>
3 #include <stdbool.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <string.h>
7 #ifdef KERNEL
8 #include "threads/init.h"
9 #include "threads/interrupt.h"
10 #include "devices/serial.h"
11 #else
12 #include <syscall.h>
13 #endif
14
15 /* Halts the OS or user program, printing the source file name,
16    line number, and function name, plus a user-specific
17    message. */
18 void
19 debug_panic (const char *file, int line, const char *function,
20              const char *message, ...)
21 {
22   va_list args;
23
24 #ifdef KERNEL
25   intr_disable ();
26 #endif
27
28 #ifdef KERNEL
29   printf ("Kernel PANIC at %s:%d in %s(): ", file, line, function);
30 #else
31   printf ("User process panic at %s:%d in %s(): ", file, line, function);
32 #endif
33
34   va_start (args, message);
35   vprintf (message, args);
36   printf ("\n");
37   va_end (args);
38
39   debug_backtrace ();
40
41   printf ("The `backtrace' program can make call stacks useful.\n"
42           "Read \"Backtraces\" in the \"Debugging Tools\" chapter\n"
43           "of the Pintos documentation for more information.\n");
44   
45 #ifdef KERNEL
46   serial_flush ();
47   if (power_off_when_done)
48     power_off ();
49   for (;;);
50 #else
51   exit (1);
52 #endif
53 }
54
55 /* Prints the call stack, that is, a list of addresses, one in
56    each of the functions we are nested within.  gdb or addr2line
57    may be applied to kernel.o to translate these into file names,
58    line numbers, and function names.  */
59 void
60 debug_backtrace (void) 
61 {
62   void **frame;
63   
64   printf ("Call stack:");
65   for (frame = __builtin_frame_address (0);
66        frame != NULL && frame[0] != NULL;
67        frame = frame[0]) 
68     printf (" %p", frame[1]);
69   printf (".\n");
70 }