Wording.
[pintos-anon] / doc / debug.texi
1 @node Debugging Tools
2 @appendix Debugging Tools
3
4 Many tools lie at your disposal for debugging Pintos.  This appendix
5 introduces you to a few of them.
6
7 @menu
8 * printf::                      
9 * ASSERT::                      
10 * Function and Parameter Attributes::  
11 * Backtraces::                  
12 * GDB::                         
13 * Triple Faults::               
14 * Modifying Bochs::             
15 * Debugging Tips::              
16 @end menu
17
18 @node printf
19 @section @code{printf()}
20
21 Don't underestimate the value of @func{printf}.  The way
22 @func{printf} is implemented in Pintos, you can call it from
23 practically anywhere in the kernel, whether it's in a kernel thread or
24 an interrupt handler, almost regardless of what locks are held (but see
25 @ref{printf Reboots} for a counterexample).
26
27 @func{printf} is useful for more than just examining data.
28 It can also help figure out when and where something goes wrong, even
29 when the kernel crashes or panics without a useful error message.  The
30 strategy is to sprinkle calls to @func{print} with different strings
31 (e.g.@: @code{"<1>"}, @code{"<2>"}, @dots{}) throughout the pieces of
32 code you suspect are failing.  If you don't even see @code{<1>} printed,
33 then something bad happened before that point, if you see @code{<1>}
34 but not @code{<2>}, then something bad happened between those two
35 points, and so on.  Based on what you learn, you can then insert more
36 @func{printf} calls in the new, smaller region of code you suspect.
37 Eventually you can narrow the problem down to a single statement.
38 @xref{Triple Faults}, for a related technique.
39
40 @node ASSERT
41 @section @code{ASSERT}
42
43 Assertions are useful because they can catch problems early, before
44 they'd otherwise be noticed.  Ideally, each function should begin with a
45 set of assertions that check its arguments for validity.  (Initializers
46 for functions' local variables are evaluated before assertions are
47 checked, so be careful not to assume that an argument is valid in an
48 initializer.)  You can also sprinkle assertions throughout the body of
49 functions in places where you suspect things are likely to go wrong.
50 They are especially useful for checking loop invariants.
51
52 Pintos provides the @code{ASSERT} macro, defined in @file{<debug.h>},
53 for checking assertions.
54
55 @defmac ASSERT (expression)
56 Tests the value of @var{expression}.  If it evaluates to zero (false),
57 the kernel panics.  The panic message includes the expression that
58 failed, its file and line number, and a backtrace, which should help you
59 to find the problem.  @xref{Backtraces}, for more information.
60 @end defmac
61
62 @node Function and Parameter Attributes
63 @section Function and Parameter Attributes
64
65 These macros defined in @file{<debug.h>} tell the compiler special
66 attributes of a function or function parameter.  Their expansions are
67 GCC-specific.
68
69 @defmac UNUSED
70 Appended to a function parameter to tell the compiler that the
71 parameter might not be used within the function.  It suppresses the
72 warning that would otherwise appear.
73 @end defmac
74
75 @defmac NO_RETURN
76 Appended to a function prototype to tell the compiler that the
77 function never returns.  It allows the compiler to fine-tune its
78 warnings and its code generation.
79 @end defmac
80
81 @defmac NO_INLINE
82 Appended to a function prototype to tell the compiler to never emit
83 the function in-line.  Occasionally useful to improve the quality of
84 backtraces (see below).
85 @end defmac
86
87 @defmac PRINTF_FORMAT (@var{format}, @var{first})
88 Appended to a function prototype to tell the compiler that the function
89 takes a @func{printf}-like format string as the argument numbered
90 @var{format} (starting from 1) and that the corresponding value
91 arguments start at the argument numbered @var{first}.  This lets the
92 compiler tell you if you pass the wrong argument types.
93 @end defmac
94
95 @node Backtraces
96 @section Backtraces
97
98 When the kernel panics, it prints a ``backtrace,'' that is, a summary
99 of how your program got where it is, as a list of addresses inside the
100 functions that were running at the time of the panic.  You can also
101 insert a call to @func{debug_backtrace}, prototyped in
102 @file{<debug.h>}, to print a backtrace at any point in your code.
103
104 The addresses in a backtrace are listed as raw hexadecimal numbers,
105 which are difficult to interpret.  We provide a tool called
106 @command{backtrace} to translate these into function names and source
107 file line numbers.
108 Give it the name of your @file{kernel.o} as the first argument and the
109 hexadecimal numbers composing the backtrace (including the @samp{0x}
110 prefixes) as the remaining arguments.  It outputs the function name
111 and source file line numbers that correspond to each address.  
112
113 If the translated form of a backtrace is garbled, or doesn't make
114 sense (e.g.@: function A is listed above function B, but B doesn't
115 call A), then it's a good sign that you're corrupting a kernel
116 thread's stack, because the backtrace is extracted from the stack.
117 Alternatively, it could be that the @file{kernel.o} you passed to
118 @command{backtrace} is not the same kernel that produced
119 the backtrace.
120
121 Sometimes backtraces can be confusing without implying corruption.
122 Compiler optimizations can cause surprising behavior.  When a function
123 has called another function as its final action (a @dfn{tail call}), the
124 calling function may not appear in a backtrace at all.  Similarly, when
125 function A calls another function B that never returns, the compiler may
126 optimize such that an unrelated function C appears in the backtrace
127 instead of A.  Function C is simply the function that happens to be in
128 memory just after A.  In the threads project, this is commonly seen in
129 backtraces for test failures; see @ref{The pass function fails, ,
130 @func{pass} Fails}, for more information.
131
132 @menu
133 * Backtrace Example::           
134 @end menu
135
136 @node Backtrace Example
137 @subsection Example
138
139 Here's an example.  Suppose that Pintos printed out this following call
140 stack, which is taken from an actual Pintos submission for the file
141 system project:
142
143 @example
144 Call stack: 0xc0106eff 0xc01102fb 0xc010dc22 0xc010cf67 0xc0102319
145 0xc010325a 0x804812c 0x8048a96 0x8048ac8.
146 @end example
147
148 You would then invoke the @command{backtrace} utility like shown below,
149 cutting and pasting the backtrace information into the command line.
150 This assumes that @file{kernel.o} is in the current directory.  You
151 would of course enter all of the following on a single shell command
152 line, even though that would overflow our margins here:
153
154 @example
155 backtrace kernel.o 0xc0106eff 0xc01102fb 0xc010dc22 0xc010cf67 
156 0xc0102319 0xc010325a 0x804812c 0x8048a96 0x8048ac8
157 @end example
158
159 The backtrace output would then look something like this:
160
161 @example
162 0xc0106eff: debug_panic (lib/debug.c:86)
163 0xc01102fb: file_seek (filesys/file.c:405)
164 0xc010dc22: seek (userprog/syscall.c:744)
165 0xc010cf67: syscall_handler (userprog/syscall.c:444)
166 0xc0102319: intr_handler (threads/interrupt.c:334)
167 0xc010325a: intr_entry (threads/intr-stubs.S:38)
168 0x0804812c: (unknown)
169 0x08048a96: (unknown)
170 0x08048ac8: (unknown)
171 @end example
172
173 (You will probably not see exactly the same addresses if you run the
174 command above on your own kernel binary, because the source code you
175 compiled and the compiler you used are probably different.)
176
177 The first line in the backtrace refers to @func{debug_panic}, the
178 function that implements kernel panics.  Because backtraces commonly
179 result from kernel panics, @func{debug_panic} will often be the first
180 function shown in a backtrace.
181
182 The second line shows @func{file_seek} as the function that panicked,
183 in this case as the result of an assertion failure.  In the source code
184 tree used for this example, line 405 of @file{filesys/file.c} is the
185 assertion
186
187 @example
188 ASSERT (file_ofs >= 0);
189 @end example
190
191 @noindent
192 (This line was also cited in the assertion failure message.)
193 Thus, @func{file_seek} panicked because it passed a negative file offset
194 argument.
195
196 The third line indicates that @func{seek} called @func{file_seek},
197 presumably without validating the offset argument.  In this submission,
198 @func{seek} implements the @code{seek} system call.
199
200 The fourth line shows that @func{syscall_handler}, the system call
201 handler, invoked @func{seek}.
202
203 The fifth and sixth lines are the interrupt handler entry path.
204
205 The remaining lines are for addresses below @code{PHYS_BASE}.  This
206 means that they refer to addresses in the user program, not in the
207 kernel.  If you know what user program was running when the kernel
208 panicked, you can re-run @command{backtrace} on the user program, like
209 so: (typing the command on a single line, of course):
210
211 @example
212 backtrace tests/filesys/extended/grow-too-big 0xc0106eff 0xc01102fb
213 0xc010dc22 0xc010cf67 0xc0102319 0xc010325a 0x804812c 0x8048a96
214 0x8048ac8
215 @end example
216
217 The results look like this:
218
219 @example
220 0xc0106eff: (unknown)
221 0xc01102fb: (unknown)
222 0xc010dc22: (unknown)
223 0xc010cf67: (unknown)
224 0xc0102319: (unknown)
225 0xc010325a: (unknown)
226 0x0804812c: test_main (...xtended/grow-too-big.c:20)
227 0x08048a96: main (tests/main.c:10)
228 0x08048ac8: _start (lib/user/entry.c:9)
229 @end example
230
231 You can even specify both the kernel and the user program names on
232 the command line, like so:
233
234 @example
235 backtrace kernel.o tests/filesys/extended/grow-too-big 0xc0106eff
236 0xc01102fb 0xc010dc22 0xc010cf67 0xc0102319 0xc010325a 0x804812c
237 0x8048a96 0x8048ac8
238 @end example
239
240 The result is a combined backtrace:
241
242 @example
243 In kernel.o:
244 0xc0106eff: debug_panic (lib/debug.c:86)
245 0xc01102fb: file_seek (filesys/file.c:405)
246 0xc010dc22: seek (userprog/syscall.c:744)
247 0xc010cf67: syscall_handler (userprog/syscall.c:444)
248 0xc0102319: intr_handler (threads/interrupt.c:334)
249 0xc010325a: intr_entry (threads/intr-stubs.S:38)
250 In tests/filesys/extended/grow-too-big:
251 0x0804812c: test_main (...xtended/grow-too-big.c:20)
252 0x08048a96: main (tests/main.c:10)
253 0x08048ac8: _start (lib/user/entry.c:9)
254 @end example
255
256 Here's an extra tip for anyone who read this far: @command{backtrace}
257 is smart enough to strip the @code{Call stack:} header and @samp{.}
258 trailer from the command line if you include them.  This can save you
259 a little bit of trouble in cutting and pasting.  Thus, the following
260 command prints the same output as the first one we used:
261
262 @example
263 backtrace kernel.o Call stack: 0xc0106eff 0xc01102fb 0xc010dc22
264 0xc010cf67 0xc0102319 0xc010325a 0x804812c 0x8048a96 0x8048ac8.
265 @end example
266
267 @node GDB
268 @section GDB
269
270 You can run Pintos under the supervision of the GDB debugger.
271 First, start Pintos with the @option{--gdb} option, e.g.@:
272 @command{pintos --gdb -- run mytest}.  Second, open a second terminal on
273 the same machine and
274 use @command{pintos-gdb} to invoke GDB on
275 @file{kernel.o}:@footnote{@command{pintos-gdb} is a wrapper around
276 @command{gdb} (80@var{x}86) or @command{i386-elf-gdb} (SPARC) that loads
277 the Pintos macros at startup.}
278 @example
279 pintos-gdb kernel.o
280 @end example
281 @noindent and issue the following GDB command:
282 @example
283 target remote localhost:1234
284 @end example
285
286 Now GDB is connected to the simulator over a local
287 network connection.  You can now issue any normal GDB
288 commands.  If you issue the @samp{c} command, the simulated BIOS will take
289 control, load Pintos, and then Pintos will run in the usual way.  You
290 can pause the process at any point with @key{Ctrl+C}.
291
292 @menu
293 * Using GDB::                   
294 * Example GDB Session::         
295 * Debugging User Programs::     
296 * GDB FAQ::                     
297 @end menu
298
299 @node Using GDB
300 @subsection Using GDB
301
302 You can read the GDB manual by typing @code{info gdb} at a
303 terminal command prompt.  Here's a few commonly useful GDB commands:
304
305 @deffn {GDB Command} c
306 Continues execution until @key{Ctrl+C} or the next breakpoint.
307 @end deffn
308
309 @deffn {GDB Command} break function
310 @deffnx {GDB Command} break file:line
311 @deffnx {GDB Command} break *address
312 Sets a breakpoint at @var{function}, at @var{line} within @var{file}, or
313 @var{address}.
314 (Use a @samp{0x} prefix to specify an address in hex.)
315
316 Use @code{break main} to make GDB stop when Pintos starts running.
317 @end deffn
318
319 @deffn {GDB Command} p expression
320 Evaluates the given @var{expression} and prints its value.
321 If the expression contains a function call, that function will actually
322 be executed.
323 @end deffn
324
325 @deffn {GDB Command} l *address
326 Lists a few lines of code around @var{address}.
327 (Use a @samp{0x} prefix to specify an address in hex.)
328 @end deffn
329
330 @deffn {GDB Command} bt
331 Prints a stack backtrace similar to that output by the
332 @command{backtrace} program described above.
333 @end deffn
334
335 @deffn {GDB Command} p/a address
336 Prints the name of the function or variable that occupies @var{address}.
337 (Use a @samp{0x} prefix to specify an address in hex.)
338 @end deffn
339
340 @deffn {GDB Command} diassemble function
341 Disassembles @var{function}.
342 @end deffn
343
344 We also provide a set of macros specialized for debugging Pintos,
345 written by Godmar Back @email{gback@@cs.vt.edu}.  You can type
346 @code{help user-defined} for basic help with the macros.  Here is an
347 overview of their functionality, based on Godmar's documentation:
348
349 @deffn {GDB Macro} debugpintos
350 Attach debugger to a waiting pintos process on the same machine.
351 Shorthand for @code{target remote localhost:1234}.
352 @end deffn
353
354 @deffn {GDB Macro} dumplist list type element
355 Prints the elements of @var{list}, which should be a @code{struct} list
356 that contains elements of the given @var{type} (without the word
357 @code{struct}) in which @var{element} is the @struct{list_elem} member
358 that links the elements.
359
360 Example: @code{dumplist all_list thread all_elem} prints all elements of
361 @struct{thread} that are linked in @code{struct list all_list} using the
362 @code{struct list_elem all_elem} which is part of @struct{thread}.
363 @end deffn
364
365 @deffn {GDB Macro} btthread thread
366 Shows the backtrace of @var{thread}, which is a pointer to the
367 @struct{thread} of the thread whose backtrace it should show.  For the
368 current thread, this is identical to the @code{bt} (backtrace) command.
369 It also works for any thread suspended in @func{schedule},
370 provided you know where its kernel stack page is located.
371 @end deffn
372
373 @deffn {GDB Macro} btthreadlist list element
374 Shows the backtraces of all threads in @var{list}, the @struct{list} in
375 which the threads are kept.  Specify @var{element} as the
376 @struct{list_elem} field used inside @struct{thread} to link the threads
377 together.
378
379 Example: @code{btthreadlist all_list all_elem} shows the backtraces of
380 all threads contained in @code{struct list all_list}, linked together by
381 @code{all_elem}.  This command is useful to determine where your threads
382 are stuck when a deadlock occurs.  Please see the example scenario below.
383 @end deffn
384
385 @deffn {GDB Macro} btpagefault
386 Print a backtrace of the current thread after a page fault exception.
387 Normally, when a page fault exception occurs, GDB will stop
388 with a message that might say:
389
390 @example
391 Program received signal 0, Signal 0.
392 0xc0102320 in intr0e_stub ()
393 @end example
394
395 In that case, the @code{bt} command might not give a useful
396 backtrace.  Use @code{btpagefault} instead.
397
398 You may also use @code{btpagefault} for page faults that occur in a user
399 process.  In this case, you may also wish to load the user program's
400 symbol table (@pxref{Debugging User Programs}).
401 @end deffn
402
403 @deffn {GDB Macro} hook-stop
404 GDB invokes this macro every time the simulation stops, which Bochs will
405 do for every processor exception, among other reasons.  If the
406 simulation stops due to a page fault, @code{hook-stop} will print a
407 message that says and explains further whether the page fault occurred
408 in the kernel or in user code.
409
410 If the exception occurred from user code, @code{hook-stop} will say:
411 @example
412 pintos-debug: a page fault exception occurred in user mode
413 pintos-debug: hit 'c' to continue, or 's' to step to intr_handler
414 @end example
415
416 In Project 2, a page fault in a user process leads to the termination of
417 the process.  You should expect those page faults to occur in the
418 robustness tests where we test that your kernel properly terminates
419 processes that try to access invalid addresses.  To debug those, set a
420 break point in @func{page_fault} in @file{exception.c}, which you will
421 need to modify accordingly.
422
423 In Project 3, a page fault in a user process no longer automatically
424 leads to the termination of a process.  Instead, it may require reading in
425 data for the page the process was trying to access, either
426 because it was swapped out or because this is the first time it's
427 accessed.  In either case, you will reach @func{page_fault} and need to
428 take the appropriate action there.
429
430 If the page fault did not occur in user mode while executing a user
431 process, then it occurred in kernel mode while executing kernel code.
432 In this case, @code{hook-stop} will print this message:
433 @example
434 pintos-debug: a page fault occurred in kernel mode
435 @end example
436 followed by the output of the @code{btpagefault} command.
437
438 Before Project 3, a page fault exception in kernel code is always a bug
439 in your kernel, because your kernel should never crash.  Starting with
440 Project 3, the situation will change if you use @func{get_user} and
441 @func{put_user} strategy to verify user memory accesses
442 (@pxref{Accessing User Memory}).
443 @end deffn
444
445 @node Example GDB Session
446 @subsection Example GDB Session
447
448 This section narrates a sample GDB session, provided by Godmar Back.
449 This example illustrates how one might debug a Project 1 solution in
450 which occasionally a thread that calls @func{timer_sleep} is not woken
451 up.  With this bug, tests such as @code{mlfqs_load_1} get stuck.
452
453 This session was captured with a slightly older version of Bochs and the
454 GDB macros for Pintos, so it looks slightly different than it would now.
455 Program output is shown in normal type, user input in @strong{strong}
456 type.
457
458 First, I start Pintos:
459
460 @smallexample
461 $ @strong{pintos -v --gdb -- -q -mlfqs run mlfqs-load-1}
462 Writing command line to /tmp/gDAlqTB5Uf.dsk...
463 bochs -q
464 ========================================================================
465                        Bochs x86 Emulator 2.2.5
466              Build from CVS snapshot on December 30, 2005
467 ========================================================================
468 00000000000i[     ] reading configuration from bochsrc.txt
469 00000000000i[     ] Enabled gdbstub
470 00000000000i[     ] installing nogui module as the Bochs GUI
471 00000000000i[     ] using log file bochsout.txt
472 Waiting for gdb connection on localhost:1234
473 @end smallexample
474
475 @noindent Then, I open a second window on the same machine and start GDB:
476
477 @smallexample
478 $ @strong{pintos-gdb kernel.o}
479 GNU gdb Red Hat Linux (6.3.0.0-1.84rh)
480 Copyright 2004 Free Software Foundation, Inc.
481 GDB is free software, covered by the GNU General Public License, and you are
482 welcome to change it and/or distribute copies of it under certain conditions.
483 Type "show copying" to see the conditions.
484 There is absolutely no warranty for GDB.  Type "show warranty" for details.
485 This GDB was configured as "i386-redhat-linux-gnu"...
486 Using host libthread_db library "/lib/libthread_db.so.1".
487 @end smallexample
488
489 @noindent Then, I tell GDB to attach to the waiting Pintos emulator:
490
491 @smallexample
492 (gdb) @strong{debugpintos}
493 Remote debugging using localhost:1234
494 0x0000fff0 in ?? ()
495 Reply contains invalid hex digit 78
496 @end smallexample
497
498 @noindent Now I tell Pintos to run by executing @code{c} (short for
499 @code{continue}) twice:
500
501 @smallexample
502 (gdb) @strong{c}
503 Continuing.
504 Reply contains invalid hex digit 78
505 (gdb) @strong{c}
506 Continuing.
507 @end smallexample
508
509 @noindent Now Pintos will continue and output:
510
511 @smallexample
512 Pintos booting with 4,096 kB RAM...
513 Kernel command line: -q -mlfqs run mlfqs-load-1
514 374 pages available in kernel pool.
515 373 pages available in user pool.
516 Calibrating timer...  102,400 loops/s.
517 Boot complete.
518 Executing 'mlfqs-load-1':
519 (mlfqs-load-1) begin
520 (mlfqs-load-1) spinning for up to 45 seconds, please wait...
521 (mlfqs-load-1) load average rose to 0.5 after 42 seconds
522 (mlfqs-load-1) sleeping for another 10 seconds, please wait...
523 @end smallexample
524
525 @noindent 
526 @dots{}until it gets stuck because of the bug I had introduced.  I hit
527 @key{Ctrl+C} in the debugger window:
528
529 @smallexample
530 Program received signal 0, Signal 0.
531 0xc010168c in next_thread_to_run () at ../../threads/thread.c:649
532 649       while (i <= PRI_MAX && list_empty (&ready_list[i]))
533 (gdb) 
534 @end smallexample
535
536 @noindent 
537 The thread that was running when I interrupted Pintos was the idle
538 thread.  If I run @code{backtrace}, it shows this backtrace:
539
540 @smallexample
541 (gdb) @strong{bt}
542 #0  0xc010168c in next_thread_to_run () at ../../threads/thread.c:649
543 #1  0xc0101778 in schedule () at ../../threads/thread.c:714
544 #2  0xc0100f8f in thread_block () at ../../threads/thread.c:324
545 #3  0xc0101419 in idle (aux=0x0) at ../../threads/thread.c:551
546 #4  0xc010145a in kernel_thread (function=0xc01013ff , aux=0x0)
547     at ../../threads/thread.c:575
548 #5  0x00000000 in ?? ()
549 @end smallexample
550
551 @noindent 
552 Not terribly useful.  What I really like to know is what's up with the
553 other thread (or threads).  Since I keep all threads in a linked list
554 called @code{all_list}, linked together by a @struct{list_elem} member
555 named @code{all_elem}, I can use the @code{btthreadlist} macro from the
556 macro library I wrote.  @code{btthreadlist} iterates through the list of
557 threads and prints the backtrace for each thread:
558
559 @smallexample
560 (gdb) @strong{btthreadlist all_list all_elem}
561 pintos-debug: dumping backtrace of thread 'main' @@0xc002f000
562 #0  0xc0101820 in schedule () at ../../threads/thread.c:722
563 #1  0xc0100f8f in thread_block () at ../../threads/thread.c:324
564 #2  0xc0104755 in timer_sleep (ticks=1000) at ../../devices/timer.c:141
565 #3  0xc010bf7c in test_mlfqs_load_1 () at ../../tests/threads/mlfqs-load-1.c:49
566 #4  0xc010aabb in run_test (name=0xc0007d8c "mlfqs-load-1")
567     at ../../tests/threads/tests.c:50
568 #5  0xc0100647 in run_task (argv=0xc0110d28) at ../../threads/init.c:281
569 #6  0xc0100721 in run_actions (argv=0xc0110d28) at ../../threads/init.c:331
570 #7  0xc01000c7 in main () at ../../threads/init.c:140
571
572 pintos-debug: dumping backtrace of thread 'idle' @@0xc0116000
573 #0  0xc010168c in next_thread_to_run () at ../../threads/thread.c:649
574 #1  0xc0101778 in schedule () at ../../threads/thread.c:714
575 #2  0xc0100f8f in thread_block () at ../../threads/thread.c:324
576 #3  0xc0101419 in idle (aux=0x0) at ../../threads/thread.c:551
577 #4  0xc010145a in kernel_thread (function=0xc01013ff , aux=0x0)
578     at ../../threads/thread.c:575
579 #5  0x00000000 in ?? ()
580 @end smallexample
581
582 @noindent 
583 In this case, there are only two threads, the idle thread and the main
584 thread.  The kernel stack pages (to which the @struct{thread} points)
585 are at @t{0xc0116000} and @t{0xc002f000}, respectively.  The main thread
586 is stuck in @func{timer_sleep}, called from @code{test_mlfqs_load_1}.
587
588 Knowing where threads are stuck can be tremendously useful, for instance
589 when diagnosing deadlocks or unexplained hangs.
590
591 @node Debugging User Programs
592 @subsection Debugging User Programs
593
594 You can also use GDB to debug a user program running under
595 Pintos.  Start by issuing this GDB command to load the
596 program's symbol table:
597 @example
598 add-symbol-file @var{program}
599 @end example
600 @noindent
601 where @var{program} is the name of the program's executable (in the host
602 file system, not in the Pintos file system).  After this, you should be
603 able to debug the user program the same way you would the kernel, by
604 placing breakpoints, inspecting data, etc.  Your actions apply to every
605 user program running in Pintos, not just to the one you want to debug,
606 so be careful in interpreting the results.  Also, a name that appears in
607 both the kernel and the user program will actually refer to the kernel
608 name.  (The latter problem can be avoided by giving the user executable
609 name on the GDB command line, instead of @file{kernel.o}, and then using
610 @code{add-symbol-file} to load @file{kernel.o}.)
611
612 @node GDB FAQ
613 @subsection FAQ
614
615 @table @asis
616 @item GDB can't connect to Bochs.
617
618 If the @command{target remote} command fails, then make sure that both
619 GDB and @command{pintos} are running on the same machine by
620 running @command{hostname} in each terminal.  If the names printed
621 differ, then you need to open a new terminal for GDB on the
622 machine running @command{pintos}.
623
624 @item GDB doesn't recognize any of the macros.
625
626 If you start GDB with @command{pintos-gdb}, it should load the Pintos
627 macros automatically.  If you start GDB some other way, then you must
628 issue the command @code{source @var{pintosdir}/src/misc/gdb-macros},
629 where @var{pintosdir} is the root of your Pintos directory, before you
630 can use them.
631
632 @item Can I debug Pintos with DDD?
633
634 Yes, you can.  DDD invokes GDB as a subprocess, so you'll need to tell
635 it to invokes @command{pintos-gdb} instead:
636 @example
637 ddd --gdb --debugger pintos-gdb
638 @end example
639
640 @item Can I use GDB inside Emacs?
641
642 Yes, you can.  Emacs has special support for running GDB as a
643 subprocess.  Type @kbd{M-x gdb} and enter your @command{pintos-gdb}
644 command at the prompt.  The Emacs manual has information on how to use
645 its debugging features in a section titled ``Debuggers.''
646
647 @item GDB is doing something weird.
648
649 If you notice strange behavior while using GDB, there
650 are three possibilities: a bug in your
651 modified Pintos, a bug in Bochs's
652 interface to GDB or in GDB itself, or
653 a bug in the original Pintos code.  The first and second
654 are quite likely, and you should seriously consider both.  We hope
655 that the third is less likely, but it is also possible.
656 @end table
657
658 @node Triple Faults
659 @section Triple Faults
660
661 When a CPU exception handler, such as a page fault handler, cannot be
662 invoked because it is missing or defective, the CPU will try to invoke
663 the ``double fault'' handler.  If the double fault handler is itself
664 missing or defective, that's called a ``triple fault.''  A triple fault
665 causes an immediate CPU reset.
666
667 Thus, if you get yourself into a situation where the machine reboots in
668 a loop, that's probably a ``triple fault.''  In a triple fault
669 situation, you might not be able to use @func{printf} for debugging,
670 because the reboots might be happening even before everything needed for
671 @func{printf} is initialized.
672
673 There are at least two ways to debug triple faults.  First, you can run
674 Pintos in Bochs under GDB (@pxref{GDB}).  If Bochs has been built
675 properly for Pintos, a triple fault under GDB will cause it to print the
676 message ``Triple fault: stopping for gdb'' on the console and break into
677 the debugger.  (If Bochs is not running under GDB, a triple fault will
678 still cause it to reboot.)  You can then inspect where Pintos stopped,
679 which is where the triple fault occurred.
680
681 Another option is what I call ``debugging by infinite loop.''
682 Pick a place in the Pintos code, insert the infinite loop
683 @code{for (;;);} there, and recompile and run.  There are two likely
684 possibilities:
685
686 @itemize @bullet
687 @item
688 The machine hangs without rebooting.  If this happens, you know that
689 the infinite loop is running.  That means that whatever caused the
690 reboot must be @emph{after} the place you inserted the infinite loop.
691 Now move the infinite loop later in the code sequence.
692
693 @item
694 The machine reboots in a loop.  If this happens, you know that the
695 machine didn't make it to the infinite loop.  Thus, whatever caused the
696 reboot must be @emph{before} the place you inserted the infinite loop.
697 Now move the infinite loop earlier in the code sequence.
698 @end itemize
699
700 If you move around the infinite loop in a ``binary search'' fashion, you
701 can use this technique to pin down the exact spot that everything goes
702 wrong.  It should only take a few minutes at most.
703
704 @node Modifying Bochs
705 @section Modifying Bochs
706
707 An advanced debugging technique is to modify and recompile the
708 simulator.  This proves useful when the simulated hardware has more
709 information than it makes available to the OS.  For example, page
710 faults have a long list of potential causes, but the hardware does not
711 report to the OS exactly which one is the particular cause.
712 Furthermore, a bug in the kernel's handling of page faults can easily
713 lead to recursive faults, but a ``triple fault'' will cause the CPU to
714 reset itself, which is hardly conducive to debugging.
715
716 In a case like this, you might appreciate being able to make Bochs
717 print out more debug information, such as the exact type of fault that
718 occurred.  It's not very hard.  You start by retrieving the source
719 code for Bochs 2.2.6 from @uref{http://bochs.sourceforge.net} and
720 extracting it into a directory.  Then read
721 @file{pintos/src/misc/bochs-2.2.6.README} and apply the patches needed.
722 Then run @file{./configure}, supplying the options you want (some
723 suggestions are in the patch file).  Finally, run @command{make}.
724 This will compile Bochs and eventually produce a new binary
725 @file{bochs}.  To use your @file{bochs} binary with @command{pintos},
726 put it in your @env{PATH}, and make sure that it is earlier than
727 @file{/usr/class/cs140/`uname -m`/bochs}.
728
729 Of course, to get any good out of this you'll have to actually modify
730 Bochs.  Instructions for doing this are firmly out of the scope of
731 this document.  However, if you want to debug page faults as suggested
732 above, a good place to start adding @func{printf}s is
733 @func{BX_CPU_C::dtranslate_linear} in @file{cpu/paging.cc}.
734
735 @node Debugging Tips
736 @section Tips
737
738 The page allocator in @file{threads/palloc.c} and the block allocator in
739 @file{threads/malloc.c} clear all the bytes in memory to
740 @t{0xcc} at time of free.  Thus, if you see an attempt to
741 dereference a pointer like @t{0xcccccccc}, or some other reference to
742 @t{0xcc}, there's a good chance you're trying to reuse a page that's
743 already been freed.  Also, byte @t{0xcc} is the CPU opcode for ``invoke
744 interrupt 3,'' so if you see an error like @code{Interrupt 0x03 (#BP
745 Breakpoint Exception)}, then Pintos tried to execute code in a freed page or
746 block.
747
748 An assertion failure on the expression @code{sec_no < d->capacity}
749 indicates that Pintos tried to access a file through an inode that has
750 been closed and freed.  Freeing an inode clears its starting sector
751 number to @t{0xcccccccc}, which is not a valid sector number for disks
752 smaller than about 1.6 TB.