Debugging 15-441 Computer Networks Sep. 26, 2007 Seunghwan Hong Project 1 Finished 1. Start working on Project 2 a) many debugging issues for this project b) start early (time is short) 2. Questions a) don’t hesitate to contact staff members b) send to staff-441@cs.cmu.edu or post on bboard Overview 1. What is Debugging? 2. Debugging Strategies 3. Debugging Tools Printf(), GDB, Smart Logging, Electric Fence, Valgrind, and GDB on emacs What is Debugging? 1. Everybody writes bugs Fixing bugs is a part of your assignment Writing a clean code is a part of debugging process 2. Things to think about what causes the bug? how do you find it? how can you avoid it? How to avoid Bug? 1. Write a clean design a) What type of data structure to use b) How different modules interact c) Document it before actual coding! 2. Use assertion / sanity check Debugging Strategy #1 Debug requires deep thinking… If you have no idea what causes the error…. - Make hypothesis (assumptions) on your code Don’t just change your code Think of the architecture, data structure, etc Use pen & paper to organize your thinking Debugging Strategy #2 Don’t think too much ALONE! - Use your teammate, course staff Ask to someone who don’t know your code - Ask to TA’s: you need to carefully tell what is wrong Debugging Strategy #3 Writing and testing code incrementally - Recommend to write a new code, test it, and then integrate with the previous work - Prevents combination of errors from different modules - Look at the recent modifications if you find a new bug - svn diff Helps a LOT Debugging Strategy #4 You are writing a complex code for this class - thousands of lines total - thousands of packets are sent/received at a second - various data structures are involved in one program Catching a bug by stepping through? - DUMP information (to be discussed later…) - Search information to find the oddity Debugging Strategy #5 Take a BREAK! sometimes, it is more efficient to rest your brain and return to work later Suggested Options: a) nap – dangerous if you are spending the whole night b) shower – you can help removing the common myth: “CS people don’t take a shower” c) running – from the former recitation note Printf(“We found a bug!\n”) 1. Easy to write printf()! a) Easy to implement: unless you don’t know C… b) Easy to catch where the bug happens 2. printf() is not easy a) You may need to insert printf() b/w every instruction b) Difficult to catch why the bug happens c) You may print all data structures, possible? d) Not a safe way to debug GDB 1. Be familiar with it a) We provide basic commands b) Search on the web if you forget…. c) You must be familiar with this from 15-213! 2. Easier to find what causes the bug GDB - Continued 1. GDB can be a bad choice! a) network programming b) multi-thread programming 2. Specific for 15-441 - GDB stops a program at a specific location - On communication, stopping one program means ‘connection lost’ GDB Commands Control Executions run <cmd-line args> break <func> stepi nexti Get Info backtrace print <expr> Info registers /locals list up/down Find more commands on Google! GDB Help 1. For debugging, always compile with –g, and no optimizations. 2. Two ways to run GDB a) gdb binary (to run binary on gdb) b) gdb binary core-file (to debug crashed program) c) Type ‘unlimit coredumpsize’ to get core files. 3. Use GDB on emacs Log Files 1. Main Idea: dump useful information - create a log file - what information should be dumped? 2. Efficient at catching logic bugs 3. Effective when it is hard to generate the same outcome again (network programming) - the order or arriving packets can be different from each simulation, therefore different result Log Files Example /* example code for P2 */ #define FILENAME “441_p2_dummy_” // file name /* flag: dump if TRUE, take no action otherwise modify the value before compile */ int dump_flag = FALSE; /* initialization */ /* function prototypes */ void dump_packet (struct packet *p); void dump_graph(struct graph *g); Log Files Example - Continue int receive_pkt (struct packet *p) { … /* some code */ /* dump packet info to check everything is OK */ dump_packet(p); /* process graph */ /* dump graph info to check everything is OK */ dump_graph(graph); … } Electric Fence 1. Finds memory-related errors in your program - e.g.: writing out of bounds, use after free… 2. Compile your program using –lefence 3. Demo….. Valgrind 1. Type valgrind <program name> -ax 2. Demo… Using GDB on Emacs The commands/keystrokes to make it happen: 1. Compile with -g and *NO* -O2 or -O3 2. build with a "make" 3. emacs sircd.c (or any other source file) 4. CTRL+x and then '3' (open a right frame) 5. CTRL+x and then 'o' (switch cursor to right frame) 6. ESC+x and then "gdb" and hit enter 7. Type in the name of your binary *only*, like "smtpsend" and hit enter 8. Set any break points you want, then type "run params ...", for example "run andrew.cmu.edu" and hit enter 9. Use GDB with your code!! (next, step, print, display...) Using GDB on Emacs Note the arrow in the left source file window shows the line being executed! Questions? Have a Nice Day!