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
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
15-441 Computer Networks
Sep. 26, 2007
1. Start working on Project 2
a) many debugging issues for this project
b) start early (time is short)
a) don’t hesitate to contact staff members
b) send to [email protected] or post on bboard
1. What is Debugging?
2. Debugging Strategies
3. Debugging Tools
Printf(), GDB, Smart Logging, Electric Fence, Valgrind, and GDB on emacs
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?
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
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
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
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
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
Take a BREAK!
sometimes, it is more efficient to rest your brain and return to work later
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
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
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
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’
Find more commands on Google!
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
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
/* 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);
int receive_pkt (struct packet *p)
… /* some code */
/* dump packet info to check everything is OK */
/* process graph */
/* dump graph info to check everything is OK */
1. Finds memory-related errors in your program
- e.g.: writing out of bounds, use after free…
2. Compile your program using –lefence
1. Type valgrind <program name> -ax
The commands/keystrokes to make it happen:
1. Compile with -g and *NO* -O2 or -O32. 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 enter7. Type in the name of your binary *only*, like "smtpsend" and hit enter8. Set any break points you want, then type "run params ...", forexample "run andrew.cmu.edu" and hit enter9. Use GDB with your code!! (next, step, print, display...)
Note the arrow in the left source file window shows the line being executed!