GDB Sample usage: Steps to complete tutorial 1. Download buggy.c and place it on your AFS space so that it can be compiled and executed on the common platform. 2. Create a test file with a sentence of less than 30 characters, ending with a period. "Hello, world." or some other simple text is fine. Name the file in_test. 3. Compile the file using the -g flag. This places debugging markers in the executable for GDB to use. % gcc -Wall -std=c99 -g buggy.c -o test 4. In GDB, there are a few commands that will be used almost exclusively. They are as follows: break Enter the line where you would like execution to halt. ( Ex. breakpoint 11 ) display Enter the name of the variable or memory you would like to see updated after each line. You may specify array contents, variables, or data in pointers. ( Ex. display len or display str[3] ) undisplay The reverse of above. Displaying too many variables will clutter your view and you may want to stop monitoring certain variables as you change scope. step Advance one line in the execution, memory modified by the line will be updated on the next step. quit Exit GDB. Sometimes it will ask you to kill the process you are running if you quit mid execution. Follow the prompts to do this. Now that we know commands, we can do a couple debug runs. As downloaded, the file will run without error as long as the sentence in the in_test file is appropriate. If it does not, review step 2 above. 5. Type the following command to start gdb on the program: % gdb test 6. GDB will start and will say that it successfully read symbols from the path for the test executable. 7. Next, we need to set a breakpoint so the file will halt execution. Typically you want to locate the source of an error or questionable code and set the breakpoint before that. For the first time let's just set the breakpoint at the entrance to the main function. (gdb) break 11 GDB will acknowledge that you set the breakpoint. 8. Start execution with input redirected from in_test. (gdb) run < in_test test starts execution and stops at the first statement in main. 9. Step through the lines using step to see how GDB works as it moves through the code. Try displaying variables that you come across to make sure you can interpret how GDB shows them. 10. Once you reach the while loop at line 41, you can follow the program as it tests, prints, and stores each character from the input. When you get bored of stepping, type quit and then y to terminate in the middle of execution. Finding the source of a segmentation fault 1. Modify buggy.c according to the comments. Comment line 27 and uncomment line 30. 2. Compile the same way and run with ./test < in_test. You should see a segmentation fault. 3. Start GDB. % gdb test 4. Set a breakpoint again, this time at the while loop. (gdb) break 41 5. Display the variables that are important to the execution. (gdb) display c 6. Step through the while loop and you will see a segmentation fault when the program tries to store the first character in the string pointed to by *cp. This happens because the declaration char *str does not allocate memory for the string. Since an int is declared immediately after, there is memory allocated only for the pointer, not for any successive chars.