Docsity

Using the Debugger: A Comprehensive Guide to Debugging Programs with GDB and NACHOS, Study notes of Operating Systems

Partial preview of the text

Download Using the Debugger: A Comprehensive Guide to Debugging Programs with GDB and NACHOS and more Study notes Operating Systems in PDF only on Docsity!

Using the Debugger

Michael Jantz

Dr. Prasad Kulkarni

Debugger

โ—

What is it

โ€“ a powerful tool that supports examination of

your program during execution.

โ—

Idea behind debugging programs.

โ€“ Creates additional symbol tables that permit

tracking program behavior and relating it

back to the source files

โ—

Some common debuggers for UNIX/Linux

โ€“ gdb, sdb ,dbx etc.

Compilation for gdb

โ—

Code must be compiled with the g option

โ€“ gcc g o file1 file1.c file2.c file3.o

โ—

Which files can you debug?

โ€“ You can debug file1.c, file 2.c

โ€“ you cannot debug file3.o (in the above

example)

โ—

Non compatibility of O option

โ€“ optimization is not (always) compatible

โ€“ Reason : Optimizer rearranges code

Getting NACHOS

โ— Download a NACHOS .tar file from here:

  • (^) http://people.eecs.ku.edu/~mjantz/678labs/lab02/
  • (^) Use the terminal to untar the archive: โ— (^) tar xvzf nachos.tar.gz

โ— IMPORTANT: Notes on building NACHOS

  • (^) This archive of NACHOS uses libraries that are only available on the machines in Eaton Hall, and will not build on other machines. Also, some additional setup of your local environment is necessary in order to build this archive: โ— (^) Open ~/.bashrc in an editor, and add the following two lines: - (^) export PYTHONPATH=/usr/local/lib/python2.5/site-packages:$PYTHONPATH - (^) export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH โ— (^) Now, source your ~/.bachrc file. (In a terminal, type): - (^) source ~/.bashrc
  • (^) The commands in ~/.bashrc are executed every time you login to your EECS account. Therefore, you will only need to do this just this once.

Using GDB with NACHOS

โ—

Starting GDB:

โ€“ gdb program

โ—

The executable for NACHOS is in

nachos/userprog. So, to start nachos under

GDB (from the nachos/test directory), type:

โ€“ gdb ../userprog/nachos

Breakpoints

โ— (^) break (b)

  • (^) Sets a breakpoint in program execution
  • (^) tbreak (tb) temporary breakpoint. Exists until it is hit for the first time โ— (^) Breakpoint syntax
  • (^) b linenumber
  • (^) b functionname
  • (^) b lineorfunction if condition
  • (^) b filename: line number โ— (^) info breakpoints โ€“ Gives information on all active breakpoints โ— (^) delete (d) โ€“ Deletes the specified breakpoint number (e.g., d 1)

Running NACHOS Under GDB

โ— run (r) - runs the loaded program under GDB

โ— Can also specify arguments and I/O redirection now, e.g:

  • (^) r arg1 arg2 < input > output โ—

In our case, the program NACHOS simulates is provided as

an argument:

  • (^) r -x matmult โ— (^) matmult2 is a simple matrix multiplication program. It forks exactly one child process and NACHOS simulates the execution of both the parent and the child.

Common GDB Commands

โ— When you hit the breakpoint you should see:

  • (^) Breakpoint 1, System_Fork () at ../userprog/systemcall.cc:

โ— Now, gdb has stopped execution of NACHOS. Try the

following commands:

  • (^) list (l) will list the source code around where execution has stopped โ— (^) Alternatively, l from-line-number, to-line-number will display the source code in between two given line numbers
  • (^) backtrace (bt) prints a backtrace of all stack frames โ— (^) From this, you can tell how you got to where you are from the main. The output here says you are in System_Fork, which was called from do_system_call, which was called from ExceptionHandler, etc., etc. โ— (^) This is very helpful in determining the control flow of a given functionality in NACHOS.

Control Flow

โ— continue (c)

  • (^) Continue until the next breakpoint is reached, the program terminates, or an error occurs (Don't use this just yet, we've got a few more commands to try).

โ— next (n)

  • (^) Execute one instruction. Step over function calls.

โ— step (s)

  • (^) Execute one instruction. Step into function calls.

โ— kill (k)

  • (^) Kills the program being debugged (does not exit gdb โ€“ preserves everything else from the session, i.e., breakpoints.)

Inspecting and Assigning

โ— Continue to the end of the System_Fork function

  • (^) Set a breakpoint on the line of the final return statement in System_Fork and use continue

โ— Now, if you read the code in this function, you'll see it creates

a new Thread (t) using the t->Fork method. To look at t's

properties (or any object's) use the following:

  • (^) print (p) t โ€“ Prints the value of some variable
  • (^) whatis t โ€“ Prints the type of t
  • (^) ptype t โ€“ Prints fields for the type t

Finishing Up

โ— To finish this example, continue until matmult2 (and,

subsequently, the NACHOS simulation) finishes.

โ— Notice the difference in the output

  • (^) The first couple lines show the ThreadID's of the threads NACHOS executed and their return values.
  • (^) One of these should correspond to the ThreadID you set in the System_Fork system call while debugging. โ—

Type quit or <ctrl-d> to exit gdb

GDB References

โ— The Unix manual is a good quick reference for common GDB

commands:

  • (^) At a terminal, type: man gdb

โ— While running GDB, help will give you any information you

need for any command:

  • (^) help (h) command

โ— If you need to do some heavy lifting with GDB, the official

documentation for users is at this website:

  • (^) http://sourceware.org/gdb/current/onlinedocs/gdb_toc.html