C_lecture_6

C_lecture_6 - CS 11 C track: lecture 6 n Last week: pointer...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 11 C track: lecture 6 n Last week: pointer arithmetic n This week: n The gdb program n struct n typedef n linked lists gdb for debugging (1) n gdb : the G nu D e B ugger n http://www.cs.caltech.edu/courses/cs11/ material/c/mike/misc/gdb.html n Use when program core dumps n or when want to walk through execution of program line-by-line gdb for debugging (1) n gdb : the G nu D e B ugger n http://www.cs.caltech.edu/courses/cs11/ material/c/mike/misc/gdb.html n Use when program core dumps n or when want to walk through execution of program line-by-line gdb for debugging (2) n Before using gdb : n Must compile C code with additional flag: -g n This puts all the source code into the binary executable n Then can execute as: gdb myprogram n Brings up an interpreted environment gdb for debugging (3) gdb> run n Program runs... n If all is well, program exits successfully, returning you to prompt n If there is ( e.g. ) a core dump, gdb will tell you and abort the program gdb for debugging (4) n If your program needs command-line arguments, e.g. myprogram 1 2 3 , then you should do this in gdb : gdb> run 1 2 3 n This will run myprogram with the command-line arguments 1 , 2 , and 3 gdb basic commands (1) n Stack backtrace (" where ") n Your program core dumps n Where was the last line in the program that was executed before the core dump? n That's what the where command tells you gdb basic commands (2) gdb> where #0 0x4006cb26 in free () from /lib/libc.so.6 #1 0x4006ca0d in free () from /lib/libc.so.6 #2 0x8048951 in board_updater (array=0x8049bd0, ncells=2) at 1dCA2.c:148 #3 0x80486be in main (argc=3, argv=0xbffff7b4) at 1dCA2.c:44 #4 0x40035a52 in __libc_start_main () from /lib/ libc.so.6 stack backtrace last call in your code last call gdb basic commands (3) n Look for topmost location in stack backtrace that corresponds to your code n Watch out for n freeing memory you didn't allocate n accessing arrays beyond their maximum elements n dereferencing pointers that don't point to part of a malloc() ed block gdb basic commands (4) n break , continue , next , step commands n break causes execution to stop on a given line gdb> break foo.c: 100 (setting a breakpoint) n continue resumes execution from that point n next executes the next line, then stops n step executes the next statement n goes into functions if necessary ( next doesn't) gdb basic commands (5)...
View Full Document

Page1 / 75

C_lecture_6 - CS 11 C track: lecture 6 n Last week: pointer...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online