Debugging - PowerPoint PPT Presentation

About This Presentation
Title:

Debugging

Description:

a) many debugging issues for this project. b) start early (time is short) 2. Questions ... 4. CTRL x and then '3' (open a right frame) ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 25
Provided by: seunghw
Learn more at: http://www.cs.cmu.edu
Category:
Tags: aright | debugging

less

Transcript and Presenter's Notes

Title: Debugging


1
Debugging
  • 15-441 Computer Networks
  • Sep. 26, 2007
  • Seunghwan Hong

2
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) dont hesitate to contact staff members
  • b) send to staff-441_at_cs.cmu.edu or post on bboard

3
Overview
  • 1. What is Debugging?
  • 2. Debugging Strategies
  • 3. Debugging Tools
  • Printf(), GDB, Smart Logging, Electric Fence,
    Valgrind, and GDB on emacs

4
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?

5
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

6
Debugging Strategy 1
  • Debug requires deep thinking
  • If you have no idea what causes the error.
  • - Make hypothesis (assumptions) on your code
  • Dont just change your code
  • Think of the architecture, data structure, etc
  • Use pen paper to organize your thinking

7
Debugging Strategy 2
  • Dont think too much ALONE!
  • - Use your teammate, course staff
  • Ask to someone who dont know your code
  • - Ask to TAs you need to carefully tell what
    is wrong

8
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

9
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

10
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 dont take a shower
  • c) running from the former recitation note

11
Printf(We found a bug!\n)
  • 1. Easy to write printf()!
  • a) Easy to implement unless you dont 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

12
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

13
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

14
GDB Commands
  • Control Executions
  • run ltcmd-line argsgt
  • break ltfuncgt
  • stepi
  • nexti
  • Get Info
  • backtrace
  • print ltexprgt
  • Info registers /locals
  • list
  • up/down

Find more commands on Google!
15
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

16
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

17
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)

18
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)

19
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..

20
Valgrind
  • 1. Type valgrind ltprogram namegt -ax
  • 2. Demo

21
Using GDB on Emacs
  • 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. CTRLx and then '3'  (open a
    right frame)5. CTRLx and then 'o'  (switch
    cursor to right frame)6. ESCx 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...)

22
Using GDB on Emacs
Note the arrow in the left source file window
shows the line being executed!
23
Questions?
24
Have a Nice Day!
Write a Comment
User Comments (0)
About PowerShow.com