Title: Debugging and Profiling
1Debugging and Profiling
2Agenda
- Debugging
- printf()
- preprocessor directives
- gdb,xxgdb, gvd
- strace, ltrace
- debauch, electric fence
- gjdb
- Profiling
- gprof
3DEBUGGING
4Printf()
- __FILE__
- __LINE__
- __FUNCTION__
- getpid() for thread
5Example
/ File b.c / include ltstdio.hgt main()
printf(File s Function s Line d\n,
__FILE__,__FUNCTION__,__LINE__)
b38tra_at_psi b38tra ./a.out File b.c Function
main Line 5
6Preprocessor Directive
- define, undef
- ifdef, ifndef, if, elif, else, endif
7define, undef
define A define B 100 define C hello undef A
8Logical Directive
ifdef MACRO lines to evaluate if MACRO is
defined else lines to evaluate if MACRO is not
defined endif ifndef MACRO lines to evaluate
if MACRO is not defined else lines to evaluate
if MACRO is defined endif
Macros can be defined or undefined with the -D'
and -U' command line options when you compile
the program
9ONCE ONLY HEADER
/ File foo.h / ifndef FILE_FOO_SEEN define
FILE_FOO_SEEN the entire file endif
10Logical Directive (cont)
if expression elif expression else endi
f
Example if defined (__vax__) defined
(__ns16000__) if BUFSIZE gt 1024
11GDB (GNU debugger)
- Compiling
- Compile with g flag
- Starting GDB
- gdb
- gdb myprog
- gdb myprog core
- gdb myprog ltpidgt
12Frequently used command
- Break point
- break filefunction
- break fileline
- clear filefunction
- clear fileline
- info break
- delete number
- Run
- run command-line-argument
13Frequently used command
- step, next
- finish, continue, until
- print
- list
- file
- help
- quit
14XXGDB
15GVD(GNU visual debugger)
16GVD (Cont.)
17Strace
- System call tracer
- can spawn new process to trace
- can be invoked on an existing process
- Support various OS
IRIXÃ’
18Strace
- strace (Linux,Windows NT, Windows2000)
- trace (HP-UX 10.20 and below)
- ktrace (BSD)
- tusc (HP-UX 11.0 and later)
- truss (Solaris2.6 and later, AIX, SCO-Unixware)
- par (Irix)
19Strace
- No need to be recompiled
- No need source code
- Output are printed on stderr or the file that
specific with o option
20Output
- SYSNAME(PARAMETER) RETURN_VALUE
- open("/dev/null", O_RDONLY) 3
- open("/foo/bar", O_RDONLY) -1 ENOENT (No such
file or directory) - Display flag
- open("xyzzy", O_WRONLYO_APPENDO_CREAT, 0666)
3
21Output
- Display structure
- lstat("/dev/null",st_modeS_IFCHR0666,
st_rdevmakedev(1, 3), ...) 0 - lstat("/foo/bar", 0xb004) -1 ENOENT (No such
file or directory) - Display 32 first string
- read(3, "root00System Administrator/"...,
1024) 422
22Output
- Display array
- getgroups(32, 100, 0) 2
- Display bit-sets
- sigprocmask(SIG_BLOCK, CHLD TTOU, ) 0
- sigprocmask(SIG_UNBLOCK, , NULL) 0
23Ltrace
- Library call tracer
- can spawn new process to trace
- can be invoked on an existing process
- Can not trace script language
- Its use is very similar to Strace
24Memory leak checker
- debauch -a program argument
- Electric Fence
- compile with lefence
- ef program argument
- export LD_PRELOADlibefence.so.0.0
- program argument
25GJDB
- Support various languages
- C, C, Java, Pascal, Assembly, Lex-Yacc
- Perl, Python, xml, xsl, html, MPI, etc.
- Support various platform
- UNIX, Windows
26PROFILING
27GPROF
- Compiling
- with option -pg
- Running
- program argument
- Analyze
- gprof program gmon.out
28GPROF Result
Each sample counts as 0.01 seconds.
cumulative self self total
time seconds seconds calls us/call
us/call name 89.47 1.36 1.36 2
680000.00 680000.00 b 9.87 1.51 0.15
1 150000.00 150000.00 c 0.66 1.52
0.01 1 10000.00 690000.00 a
29(No Transcript)