DOC PREVIEW
CMU CS 15441 - DEBUGGING IN THE REAL WORLD

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Debugging IN THE REAL WORLDOutlineWhat can go wrong?Error Types and Project 1Save yourself a headache!Slide 6Reality: errors will happenSlide 8Valgrind debugging toolValgrind: Example ErrorsRunning Example in ValgrindSlide 12GDB: GNU Project DebuggerHow to use GDBGDB: reading a core fileGDB: Being interactive w/ EMACSGDB: useful commandsWrapup: Questions anyone?DEBUGGING IN THE REAL WORLD15-441: Recitation 4OutlineWhat can go wrong?How can we avoid errors?What tools are available to debug errors?ValgrindGDBWhat can go wrong?Project 1: IRC server, what are our components?managing connections (e.g., sockets)handling clients (e.g., client pool)handling data (e.g., buffers and strings manipulation)IRC protocol (e.g., RFC … JOIN, PART, MSG)What kind of errors can we have? (2 major types)Logical error vs. fault (crashing)Error Types and Project 1What logic errors do you need to be careful of?IRC protocol following the RFCHandling socket information properlyWhat faults do you need to be careful of?Memory copying (e.g., buffer to buffer)String manipulation (e.g., handling client messages)Array accesses (e.g., your client pool)Socket descriptorsSave yourself a headache!First and foremost: practice smart programming to avoid faults.CHECK RETURN CODES!Bad: read(fd, &buffer, nbtr);Good: if((nbytes=read(fd, &buffer, nbtr))==-1)Use safe functions: snprintf(good) vs. sprintf(bad)Check pointers before use: if(clientfd!=NULL) { … }OutlineWhat can go wrong?How can we avoid errors?What tools are available to debug errors?valgrindstraceGDBReality: errors will happenWe are all human (I think!), bugs will occurGoal: find and terminate them as fast as possibleDon’t: toss printf()’s everywhere and hope for the best, this takes a long timeDo: use a great set of tools for debuggingSaves time  saves points!Saves headache  saves sanity!OutlineWhat can go wrong?How can we avoid errors?What tools are available to debug errors?ValgrindGDBValgrind debugging toolGoal: detect memory errorsAccesses outside of memory bounds Memory leaksGreat for finding errors that would only show during harsh test casesYes, we will use harsher test cases than checkpoint 1 and checkpoint 2 for final grading!Valgrind: Example ErrorsCan you find two errors in this program?#include <stdlib.h> void f(void) { int* x = malloc(10 * sizeof(int)); x[10] = 0;}int main(void) { f(); return 0; } 1. Invalid memory access2. Memory never free()’dRunning Example in ValgrindRunning valgrind with the program:valgrind --leak-check=yes myprog arg1 arg2Invalid access output (error 1):Memory leak output (error 2):==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) ==19182== by 0x8048385: f (a.c:5) ==19182== by 0x80483AB: main (a.c:11) ==19182== Invalid write of size 4 ==19182== at 0x804838F: f (example.c:6) ==19182== by 0x80483AB: main (example.c:11) Process IDWhere the error occursSize of the leakOutlineWhat can go wrong?How can we avoid errors?What tools are available to debug errors?ValgrindGDBGDB: GNU Project DebuggerThe best debugging tool for your projects!Segfaulting? No problem.You can step through your program, line by line and monitor any memory!Seriously, it doesn’t get any better than thisHow to use GDBTwo major ways:Read a core dump step through a programGetting a segfault and just want to determine where it happened?Get a core file, run: ulimit –c unlimitedCause the program to segfaultMUST MUST MUST: enable –g flag when compilingGDB: reading a core fileEnable core dumping and run:Open the core in GDB:$ ulimit -c unlimited$ ./cache_sim config.example0 < trace.example0….Segmentation fault (core dumped)$ gdb cache_sim core…#0 0x08049bae in memory::load (…, …) at cache_sim.cc:252252 if(!d_tag_store[i][index].valid) {(gdb) backtrace#0 0x08049bae in memory::load (…, …) at cache_sim.cc:252#1 0x0804a3e2 in handle_load_reference (…) at cache_sim.cc:366#2 0x0804b63e in main (…, …) at cache_sim.cc:562Function where the segfault occurs (load)Line where the segfault occursHow we got thereGDB: Being interactive w/ EMACSYou can step through your code with EMACSYou use VIM? No problem, so do I … use EMACS just to debug!How to run in EMACS:emacs <source_file.c>ctrl+x+3 (splits screen)ctrl+x+o (moves cursor to right side of screen)esc+x (brings up line at bottom)gdb (type in bottom and hit enter)hit enter 1 more time! (fix executable file name if needed)GDB: useful commandsUseful commands for you to know:Start the program: run <arg1> <arg2> …Create breakpoint: break <line> OR break <function>Goto next line: nextStep into a function: stepCheck a variable value: print <variable name>Display a variable value: display <variable name>Wrapup: Questions anyone?Questions on debugging?Valgrind, GDB…Questions on project 1?IRC protocol, sockets, client pool, buffers…General course questions?Ethernet, wireless, physical layer, application


View Full Document

CMU CS 15441 - DEBUGGING IN THE REAL WORLD

Documents in this Course
lecture

lecture

34 pages

lecture

lecture

38 pages

lecture

lecture

18 pages

lecture

lecture

28 pages

lecture

lecture

11 pages

Lecture

Lecture

64 pages

lecture

lecture

10 pages

lecture

lecture

19 pages

Lecture 6

Lecture 6

43 pages

Exam

Exam

14 pages

lecture

lecture

38 pages

Debugging

Debugging

23 pages

lecture

lecture

60 pages

review

review

27 pages

lecture

lecture

12 pages

The Web

The Web

28 pages

Lecture

Lecture

40 pages

lecture

lecture

42 pages

lecture

lecture

9 pages

lecture

lecture

10 pages

lecture

lecture

49 pages

lecture

lecture

26 pages

Project

Project

5 pages

lecture

lecture

40 pages

lecture

lecture

9 pages

lecture

lecture

41 pages

lecture

lecture

32 pages

lecture

lecture

36 pages

lecture

lecture

34 pages

lecture

lecture

45 pages

lecture

lecture

26 pages

lecture

lecture

6 pages

lecture

lecture

51 pages

Project

Project

16 pages

lecture

lecture

44 pages

lecture

lecture

13 pages

lecture

lecture

42 pages

lecture

lecture

36 pages

Project

Project

13 pages

Project

Project

33 pages

lecture

lecture

43 pages

lecture

lecture

49 pages

Load more
Download DEBUGGING IN THE REAL WORLD
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view DEBUGGING IN THE REAL WORLD and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view DEBUGGING IN THE REAL WORLD 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?