1. Goals for this week
-
Reminder of tools for examining binary files (gdb in particular)
-
Learn about manual pages, and using
man
andapropos
2. Starting Point Code
There is no starting point code this week. Instead, let’s revisit
what we worked on last week and review some assembly debugging
resources using the mystery
program.
Start by going back to your week06
weekly lab directory:
$ cd ~/cs31/weeklylab/week06 $ ls Makefile mystery* README simplefuncs.c
3. Review of gdb assembly
Let’s try out the mystery
program from last week and in gdb
again:
-
Let’s run it and see what it does.
-
Next, lets run it in
gdb
and examine its code:$ gdb ./mystery (gdb) layout asm (gdb) break main (gdb) run (gdb) disas main
-
What does main’s control flow look like?
-
Let’s add some break points around function calls and in functions.
-
Let’s examine some state around functions.
-
We can print out values on the stack using
x
and a stack memory address:(gdb) x/a address # /a: "examine memory contents as an address" (gdb) x/s address # /s: "examine memory contents as a string" (gdb) x/wd address # /wd: "examine memory contents as a 32-bit decimal"
4. man and manpages
First, we are going to learn how to use man to read manual pages, and how to use apropos to find commands: man and apropos
Next, let’s look at the man page for strcmp and for scanf to see what they are telling us about these functions.
$ man scanf
$ man 3 scanf # or explicitly specify the manual section:
# (C library function scanf is in section 3 of the manual)
$ man strcmp
apropos
is a command for finding the names of other commands or library
functions. It is useful if you cannot remember the name of a library
function or command but you know what it does. Suppose that we cannot
remember strcmp
, we could try to find it using apropos:
$ apropos compare
5. Handy References
-
GDB for Assembly (from the gdb Guide). (assembly debugging and x command)
-
Section 3.2 and Section 3.5 of textbook (assembly debugging, print, display, info and x commands)
-
Tools for examining phases of compiling and running C programs