1 / 19

CS241 Systems Programming

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

leroy
Download Presentation

CS241 Systems Programming

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS241 Systems Programming Discussion Section 10/9/06 - 10/13/06

  2. Outline • smp5 Quiz: passwd - • gdb • gprof • asserts • Midterm review

  3. 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)

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

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

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

  7. Basic commands • quit • Exit from GDB • help [name] • Show information about GDB command name, or general information about using GDB

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

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

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

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

  12. 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]

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

  14. Example 1000 -> int a(void) { int i=0,g=0; while(i++<100000) { g+=i; } return g; } 1000 -> int b(void) {

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

  16. Asserts • Useful for debugging your program #include <assert.h> void assert(scalar expression); • If expression evaluates to 0 your program will stop

  17. 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 */ . . . }

  18. Midterm Review • Format: • Multiple choice • Possibly a few short answer • Homework 1 is semi-representative; actual midterm will be harder, longer, and more complete.

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

More Related