• Save
Linux User Space Debugging & Profiling
Upcoming SlideShare
Loading in...5
×
 

Linux User Space Debugging & Profiling

on

  • 6,569 views

 

Statistics

Views

Total Views
6,569
Views on SlideShare
6,021
Embed Views
548

Actions

Likes
5
Downloads
0
Comments
0

8 Embeds 548

http://profession.sarika-pugs.com 305
http://sysplay.in 225
http://www.sysplay.in 7
http://shrishti 3
http://localhost 3
http://www.linkedin.com 3
http://ivalmiki.com 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Linux User Space Debugging & Profiling Linux User Space Debugging & Profiling Presentation Transcript

  • Debugging & Profiling © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> All Rights Reserved.
  • What to Expect? Debugging Various Techniques Using Debugger: gdb Profiling using Program Checkers Code Coverage Tools Performance Analyzers © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 2 All Rights Reserved.
  • Debugging © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 3 All Rights Reserved.
  • Ways of Debugging Basic: Printing Querying Kernel Windows: /proc, /sys Using Commands like ipcs Tracing: strace Debugger: gdb © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 4 All Rights Reserved.
  • Debugger: gdb Text mode debugger Repetition of previous command Very powerful All kind of options: Breakpoint, Watch, ... Extensive Help GUI interfaces: ddd, ... © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 5 All Rights Reserved.
  • 'gdb' Usage Compile with -g option gcc -g file.c -o file Run gdb (with desired options) gdb [options] ./file [core file] Pass command line arguments by setting args set args <cmd_line_args> Run the program by Typing c, or run <cmd_line_args> For debugging: break, watch, backtrace, … For help: help <command> © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 6 All Rights Reserved.
  • gdb: Other ways of Running step [ count ] – step into sub-routines next – run over sub-routines in a go finish – run till current function returns return – make selected stack frame return to its caller jump <address> – continue program at specified line or address © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 7 All Rights Reserved.
  • gdb: Breakpoints info break – Show list & status of breakpoints Setting breakpoints (returns its number) [t]break <function> [ if <expr> ] [t]break [<file>:]<line> Breakpoint Operations disable <break_no> enable <break_no> delete <break_no> commands <break_no> Execute gdb commands on reaching <break_no> © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 8 All Rights Reserved.
  • gdb: Examining the Stack Stack grows down Each call state is referred as a frame backtrace – Show stack frames frame <frame_no> – Select <frame_no> down – Select callee's frame up – Select caller's frame info args – Show args of current frame info locals – Show locals of current frame © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 9 All Rights Reserved.
  • gdb: Examining Source Files list <line> Print 10 lines around <line> list [<filename>:]<function> Print 10 lines around beginning of <function> list <start>,<end> Print lines from <start> to <end> list Print 10 more lines © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 10 All Rights Reserved.
  • gdb: Examining Data print[/xduotcf] <expr> Evaluate the valid C <expr> within current frame set <var> = <expr> Assign <expr> to <var> in the current scope Variables starting with $ are local to gdb display <expr> Print <expr> whenever program stops undisplay Cancels all previous display requests © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 11 All Rights Reserved.
  • gdb: Other Examinations Watchpoints / Catchpoints info watchpoints (Same as info break) watch <expr> - Break on Write watchpoint rwatch <expr> - Break on Read watchpoint awatch <expr> - Break on Both watchpoint Default is hardware watchpoint Registers: info registers Set-able options: info set © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 12 All Rights Reserved.
  • gdb: Shortcuts Just enough of command To make it unique Added with tab completion First letter only for break, delete, run, continue, step, next, print Repeat last by <Enter> Executing the same set of initial commands Put them in .gdbinit -x <gdb command file> © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 13 All Rights Reserved.
  • gdb: Miscellaneous Set editmode for gdb command line editmode [ emacs | vi | dumb ] Execute a shell command shell <cmd> Print command history history Set logging set logging <on | off> set logging file <log_file> (Default: gdb.txt) © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 14 All Rights Reserved.
  • gdb: Thread Debugging Automatic notification of new threads Switching among threads thread <thread_no> Auto switching on stop by breakpoint or signal Inquiry on existing threads info threads Applying command to a list of threads thread apply <thread_no> | all <cmd> Thread specific breakpoints Example: break <line> thread <thread_no> © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 15 All Rights Reserved.
  • gdb: Debugging Strategies Oh no!!! Another tool to learn Worth learning a new tool Segmentation faults & Core dumps Execution intercepted by gdb Allows examining the state, backtrace, etc Debugging is Narrowing Down Reaching close & then stepping © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 16 All Rights Reserved.
  • Let's Debug Demo: debugging_cprog.c Debug: find_the_bug.c Thread Debugging: multi_threads.c © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 17 All Rights Reserved.
  • Profiling © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 18 All Rights Reserved.
  • W's of Profiling Analyzing & Understanding Programs Many varieties Coding Style Checker Logic Analysis Memory Analysis Execution Time Profiling Run Time Error Capture Execution Flow Analysis © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 19 All Rights Reserved.
  • Profiling Categories Program Check Static (splint) Dynamic (memwatch, ElectricFence → DUMA) Code Coverage (Dynamic) Execution Flow using test vectors (gcov) Performance Analysis (Dynamic) Execution Time using test vectors (gprof) All in One: valgrind © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 20 All Rights Reserved.
  • Static Program Checkers Compiler itself Really fast But too naive Other Extreme: Formal Verification Tools Extensive But needs huge resources Preference: In between solutions Example: splint © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 21 All Rights Reserved.
  • 'splint' detects Memory related Problems Dereferencing a possibly null pointer, Using possibly undefined storage Returning storage that is not properly defined Dangling references, Memory leaks, Buffer overflow, ... Dangerous Coding Likely infinite loops, fall-through cases or incomplete switches Suspicious statements, Dangerous aliasing Dangerous macro implementations or invocations Type mismatches with greater precision and flexibility, ... Specific Violations of Customized naming conventions Information hiding Modifications & Usage of global variable as per specified interfaces ... © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 22 All Rights Reserved.
  • 'splint' Usage Run splint (with desired options) splint [options] file[.c] Prints out all the messages about the file On standard output Errors on standard error (using -usestderr) Some interesting options -weak for lesser messages -I <incl_dirs> Try 'splint splint_cprog' © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 23 All Rights Reserved.
  • Dynamic Program Checkers Memory Debugging mtrace memwatch ElectricFence → DUMA ... Replaces the memory allocation functions Like malloc, free, ... By the ones to keep track of the same © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 24 All Rights Reserved.
  • Code Coverage Dynamic Execution Flow Analysis Done by actually running the Program Usually done with sets of test vectors Providing the Test Code Coverage Coverage of: Line of Code, Branches, Blocks, ... Typically provides Entity executed or not executed % or times of execution of various entities Tool: gcov Could provide performance info when used with gprof © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 25 All Rights Reserved.
  • 'gcov' Usage Compile with -fprofile-arcs -ftest-coverage options gcc -fprofile-arcs -ftest-coverage file.c -o file Execute the program with the test vectors ./file [ <cmd_line_args> ] Generates .gcda (Data) & .gcno (Graph) files Run gcov (with desired options) gcov [options] file.c Interesting options: --all-blocks --branch-probabilities Generates coverage output in .gcov file Try with gcov_cprog.c © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 26 All Rights Reserved.
  • Performance Analysis Dynamic Execution Time Analysis Done by actually running the Program Usually done with sets of test vectors Providing the Test Performance Analysis Typically provides info on Functions Time Spent, Times Called Call Graph Profile Data Performance Bottlenecks Tool: gprof © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 27 All Rights Reserved.
  • 'gprof' Usage Compile with -pg option gcc -pg file.c -o file Run the program ./file [ <cmd_line_args> ] Generates gmon.out file Run gprof (with desired options) gprof [options] ./file [gmon.out] Dumps profiling info on stdout Try with gprof_cprog.c © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 28 All Rights Reserved.
  • Interesting 'gprof' Options -b # Brief output -p # Flat Profile -q # Call Graph Analysis -A # Print with Annotated source code -x # Annotate all lines -l # Line-by-line profiling -s # Sum -z # Display unused functions -r, -R <mapfile> # Reordering suggestions © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 29 All Rights Reserved.
  • Tool Suite: valgrind Provides a number of debugging & profiling tools Most popular being Memcheck Best behaviour without optimization Other possibly useful tools Cachegrind: Profiler for cache (miss) and branch (misprediction) events Callgrind: Profiler to show cost relationships across function calls, optionally with cache simulation Massif: Space profiler to explore in detail which parts of your program allocate memory Helgrind: Debugging tool for threaded programs Looks for various kinds of synchronisation errors in code That uses the POSIX pthreads API GUI frontend: valkyrie © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 30 All Rights Reserved.
  • valgrind: Memcheck detects Touching “shouldn’t” memories Overrunning heap block boundaries Reading/Writing freed/unallocated memory Memory leaks Not freeing heap blocks Incorrect freeing of memory Double-freeing heap blocks Using values before initialization © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 31 All Rights Reserved.
  • 'valgrind' Usage Compile with -g option gcc -g file.c -o file Run using valgrind & desired options valgrind [options] ./file [ <cmd_line_args> ] By default, runs Memcheck Profiles out the memory usage of the program For using other tools of valgrind, add --tool=<toolname> Try with valgrind_cprog.c © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 32 All Rights Reserved.
  • What all have we learnt? Debugging Various Techniques Using Debugger: gdb Profiling using Program Checkers splint, mtrace, memwatch, ... Code Coverage Tools gcov Performance Analyzers gprof, valgrind, ... © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 33 All Rights Reserved.
  • Any Queries? © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 34 All Rights Reserved.