190 likes | 293 Views
CS241 Systems Programming. Discussion Section 10/9/06 - 10/13/06. Outline. smp5 Quiz: passwd - gdb gprof asserts Midterm review. Starting GDB. First, compile your program with the “-g” flag (meaning “produce debugging information”) Three ways to start GDB gdb program
E N D
CS241 Systems Programming Discussion Section 10/9/06 - 10/13/06
Outline • smp5 Quiz: passwd - • gdb • gprof • asserts • Midterm review
Starting GDB • First, compile your program with the “-g” flag (meaning “produce debugging information”) • Three ways to start GDB • gdb program • gdb program core (useful after segfault) • gdb program pid (when debugging a running process)
Basic commands • run [arglist] • Start your program (with arglist, if specified) • break [file:]function • Set a breakpoint at function (in file) • bt • Backtrace: display the program stack • print expr • Display the value of an expression
Basic commands • c • Continue running your program (after stopping, e.g. at a breakpoint) • next • Execute next program line (after stopping); step over any function calls in the line • step • Execute next program line (after stopping); step into any function calls in the line
Basic commands • edit [file:]function • look at the program line where it is presently stopped • list [file:]function • type the text of the program in the vicinity of where it is presently stopped
Basic commands • quit • Exit from GDB • help [name] • Show information about GDB command name, or general information about using GDB
gprof • Profiling tool to show the ‘hotspots’ of your program • Compile and link with -pg • Here are examples: • cc -g -c myprog.c utils.c -pg • cc -o myprog myprog.o utils.o -pg • The `-pg' option also works with a command that both compiles and links: • cc -o myprog myprog.c utils.c -g -pg
gprof data • `gmon.out' file is written in the program's current working directory at the time it exits • your program must exit normally: by returning from main or by calling exit
Using gprof • gprof options [executable-file [profile-data-files...]] [> outfile] • Options include time spent in function, call graph, … • http://sourceware.org/binutils/docs-2.16/gprof/index.html
Flat profile Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 33.34 0.02 0.02 7208 0 0 open 16.67 0.03 0.01 244 0.04 0.12 offtime 16.67 0.04 0.01 8 1.25 1.25 memccpy 16.67 0.05 0.01 7 1.43 1.43 write 16.67 0.06 0.01 mcount 0.00 0.06 0.00 1 0.00 50.00 main
Call Graph index % time self children called name <spontaneous> [1] 100.0 0.00 0.05 start [1] 0.00 0.05 1/1 main [2]
Annotated Source Listing • gcc example1.c -g -pg -o example1 -O2 -lc • Re-run the application as before: • ./example1 50000 • Your gprof command is now: • gprof example1 gmon.out -A
Example 1000 -> int a(void) { int i=0,g=0; while(i++<100000) { g+=i; } return g; } 1000 -> int b(void) {
Asserts • Need to check some invariant in our program • E.g.: size is >= 0 • Assert statement checks boolean condition • If true, passes with no effect • If false, prints message and terminates the program
Asserts • Useful for debugging your program #include <assert.h> void assert(scalar expression); • If expression evaluates to 0 your program will stop
Assert example #include <assert.h> my_func() { . . . /* expect the list to be filled here. */ size = list_size(&my_list); assert (size > 0); /* we depend on the list having some elements */ /* do important stuff down here with the list */ . . . }
Midterm Review • Format: • Multiple choice • Possibly a few short answer • Homework 1 is semi-representative; actual midterm will be harder, longer, and more complete.
Midterm Review • Topics: • Note: All percentages are very approximate and subject to change • 10% basic C • 15% “restart and general” (libraries?) • 15% signals • 15% processes • 15% threads • 15% semaphores and mutexes