PKUOS - Pintos
Pintos source browser for PKU Operating System course
debug.c
Go to the documentation of this file.
1#include <debug.h>
2#include <console.h>
3#include <stdarg.h>
4#include <stdbool.h>
5#include <stddef.h>
6#include <stdio.h>
7#include <string.h>
8#include "threads/init.h"
9#include "threads/interrupt.h"
10#include "threads/thread.h"
11#include "threads/switch.h"
12#include "threads/vaddr.h"
13#include "devices/serial.h"
14#include "devices/shutdown.h"
15
16/** Halts the OS, printing the source file name, line number, and
17 function name, plus a user-specific message. */
18void
19debug_panic (const char *file, int line, const char *function,
20 const char *message, ...)
21{
22 static int level;
23 va_list args;
24
25 intr_disable ();
27
28 level++;
29 if (level == 1)
30 {
31 printf ("Kernel PANIC at %s:%d in %s(): ", file, line, function);
32
33 va_start (args, message);
34 vprintf (message, args);
35 printf ("\n");
36 va_end (args);
37
39 }
40 else if (level == 2)
41 printf ("Kernel PANIC recursion at %s:%d in %s().\n",
42 file, line, function);
43 else
44 {
45 /* Don't print anything: that's probably why we recursed. */
46 }
47
48 serial_flush ();
49 shutdown ();
50 for (;;);
51}
52
53/** Print call stack of a thread.
54 The thread may be running, ready, or blocked. */
55static void
56print_stacktrace(struct thread *t, void *aux UNUSED)
57{
58 void *retaddr = NULL, **frame = NULL;
59 const char *status = "UNKNOWN";
60
61 switch (t->status) {
62 case THREAD_RUNNING:
63 status = "RUNNING";
64 break;
65
66 case THREAD_READY:
67 status = "READY";
68 break;
69
70 case THREAD_BLOCKED:
71 status = "BLOCKED";
72 break;
73
74 default:
75 break;
76 }
77
78 printf ("Call stack of thread `%s' (status %s):", t->name, status);
79
80 if (t == thread_current())
81 {
82 frame = __builtin_frame_address (1);
83 retaddr = __builtin_return_address (0);
84 }
85 else
86 {
87 /* Retrieve the values of the base and instruction pointers
88 as they were saved when this thread called switch_threads. */
89 struct switch_threads_frame * saved_frame;
90
91 saved_frame = (struct switch_threads_frame *)t->stack;
92
93 /* Skip threads if they have been added to the all threads
94 list, but have never been scheduled.
95 We can identify because their `stack' member either points
96 at the top of their kernel stack page, or the
97 switch_threads_frame's 'eip' member points at switch_entry.
98 See also threads.c. */
99 if (t->stack == (uint8_t *)t + PGSIZE || saved_frame->eip == switch_entry)
100 {
101 printf (" thread was never scheduled.\n");
102 return;
103 }
104
105 frame = (void **) saved_frame->ebp;
106 retaddr = (void *) saved_frame->eip;
107 }
108
109 printf (" %p", retaddr);
110 for (; (uintptr_t) frame >= 0x1000 && frame[0] != NULL; frame = frame[0])
111 printf (" %p", frame[1]);
112 printf (".\n");
113}
114
115/** Prints call stack of all threads. */
116void
118{
119 enum intr_level oldlevel = intr_disable ();
120
122 intr_set_level (oldlevel);
123}
void debug_backtrace(void)
Prints the call stack, that is, a list of addresses, one in each of the functions we are nested withi...
Definition: debug.c:13
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
enum intr_level intr_disable(void)
Disables interrupts and returns the previous interrupt status.
Definition: interrupt.c:104
enum intr_level intr_set_level(enum intr_level level)
Enables or disables interrupts as specified by LEVEL and returns the previous interrupt status.
Definition: interrupt.c:81
intr_level
Interrupts on or off?
Definition: interrupt.h:9
void console_panic(void)
Notifies the console that a kernel panic is underway, which warns it to avoid trying to take the cons...
Definition: console.c:74
int vprintf(const char *format, va_list args)
The standard vprintf() function, which is like printf() but uses a va_list.
Definition: console.c:126
void debug_panic(const char *file, int line, const char *function, const char *message,...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
Definition: debug.c:19
void debug_backtrace_all(void)
Prints call stack of all threads.
Definition: debug.c:117
static void print_stacktrace(struct thread *t, void *aux UNUSED)
Print call stack of a thread.
Definition: debug.c:56
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
void serial_flush(void)
Flushes anything in the serial buffer out the port in polling mode.
Definition: serial.c:135
void shutdown(void)
Shuts down the machine in the way configured by shutdown_configure().
Definition: shutdown.c:29
#define va_end(LIST)
Definition: stdarg.h:10
#define va_start(LIST, ARG)
Definition: stdarg.h:9
__builtin_va_list va_list
GCC has <stdarg.h> functionality as built-ins, so all we need is to use it.
Definition: stdarg.h:7
#define NULL
Definition: stddef.h:4
uint32_t uintptr_t
Definition: stdint.h:36
unsigned char uint8_t
Definition: stdint.h:20
An open file.
Definition: file.c:8
switch_thread()'s stack frame.
Definition: switch.h:7
void(* eip)(void)
16: Return address.
Definition: switch.h:12
uint32_t ebp
8: Saved ebp.
Definition: switch.h:10
A kernel thread or user process.
Definition: thread.h:84
uint8_t * stack
Saved stack pointer.
Definition: thread.h:89
char name[16]
Name (for debugging purposes).
Definition: thread.h:88
enum thread_status status
Thread state.
Definition: thread.h:87
void switch_entry(void)
struct thread * thread_current(void)
Returns the running thread.
Definition: thread.c:256
void thread_foreach(thread_action_func *func, void *aux)
Invoke function 'func' on all threads, passing along 'aux'.
Definition: thread.c:320
@ THREAD_BLOCKED
Waiting for an event to trigger.
Definition: thread.h:13
@ THREAD_RUNNING
Running thread.
Definition: thread.h:11
@ THREAD_READY
Not running but ready to run.
Definition: thread.h:12
#define PGSIZE
Bytes in a page.
Definition: vaddr.h:20