Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-222 CPT: Debug/71 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to talk about the various approaches to testing.

Similar presentations


Presentation on theme: "240-222 CPT: Debug/71 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to talk about the various approaches to testing."— Presentation transcript:

1 240-222 CPT: Debug/71 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to talk about the various approaches to testing and debugging code. 7. Testing and Debugging

2 240-222 CPT: Debug/72 Overview: 1.Testing 2.Debugging 3.Types of Errors 4.Compile Time Errors 5.Before Debugging 6.The Debugging Process 7.Debugging Advice 8.Check List 9.Common Bugs 10.Runtime Debugging Aids

3 240-222 CPT: Debug/73 1. Testing l Testing is a systematic process that attempts to increase your confidence that a program is correct. l "Testing can only show the presence of bugs, not prove their absence." l Two kinds: 1.1.Black-box Testing 1.2.Glass-box Testing

4 240-222 CPT: Debug/74 1.1. Black-box Testing l Consider only the input/output behaviour of each function. l Devise test data based on specification. –easy values (trivial cases) –typical values (easily checkable by hand) –extreme values (limits in specification) –illegal values (check reliability)

5 240-222 CPT: Debug/75 Black-box Test Example l Consider a program to print a summary of occurrences of words in a text file. l Data cases: –empty file –file with no words –file with one word –file with one word many times –file with same word twice on line –small files of random words –large text where results known

6 240-222 CPT: Debug/76 1.2. Glass-box Testing l Examine the internal structure of functions. l Excellent method for debugging small functions. l Example switch (a) { case 1: x = 3; break; case 2: if (b = 0) x = 2; else x = 4; break; case 3: while (c > 0) process(c); break; }

7 240-222 CPT: Debug/77 2. Debugging l Debugging is the process of locating and removing errors. l It has been estimated that 85% of debugging time is spent locating errors and only 15% spent fixing them.

8 240-222 CPT: Debug/78 3. Types of Errors l compile-time errors l run-time crashes l incorrect results

9 240-222 CPT: Debug/79 4. Compile Time Errors Use the -Wall option of gcc : gcc -Wall -o example example.c l Extra warnings include: –not using a locally defined variable –not declaring a function

10 240-222 CPT: Debug/710 5. Before Debugging To make the debugging process easier: l Use existing (tested) code. l Develop the program in a modular fashion (first debug functions separately and then debug combination). continued

11 240-222 CPT: Debug/711 l Develop functions systematically –one at a time –remember dependencies for ordering l Document your code. l Program for clarity: –“Will I understand this in two weeks time?”

12 240-222 CPT: Debug/712 6. The Debugging Process 1. Identify/describe the error. 2. Gather data about program behaviour. 3. Guess/locate what caused the error. 4. Test your guess. 5. If guess was not correct, go to step 2.

13 240-222 CPT: Debug/713 7. Debugging Advice l Keep a record of debugging. l Save a copy of your program before you attempt to fix it. –Restore the saved program if the fix does not work. l Make sure you are using a listing of the current version of the program. continued

14 240-222 CPT: Debug/714 l Learn from negative results (eliminate possible error sources). l Don't assume that: –the computer is at fault –the system software is faulty l Compare the program with its comments. continued

15 240-222 CPT: Debug/715 l Compare data that gives right answers with data that gives wrong answers. l Look for a pattern. l Find the simplest data that causes the error. l Execute your program by hand on the wrong data. continued

16 240-222 CPT: Debug/716 Insert diagnostic printf() statements (more on this later). Only use a few printf() statements for debugging at any one time. l Find the earliest point at which a variable contains a wrong value or control reaches the wrong part of the program. continued

17 240-222 CPT: Debug/717 l Copy program; simplify program to simplest form that gives the error. l Keep good/long test cases in files for easy reuse. l Explain your program to someone else. l Use stubs (dummy functions). continued

18 240-222 CPT: Debug/718 Stub Example #include float complex_input(void); void process(float val); int main() { float val; val = complex_input(); process(val); return 0; } continued

19 240-222 CPT: Debug/719 float complex_input(void) /* Rather than do complex input, return a typical value. This function is a stub. */ { return 1.11111111; } void process(float val) /* Process input This function is being tested. */ { : /* actual code */ }

20 240-222 CPT: Debug/720 8. Check List l Are all comments terminated? l Have all variables been initialized? l Does every loop terminate? l Are function parameters correctly defined? l Are parentheses where you want them?

21 240-222 CPT: Debug/721 9. Common Bugs l Array index out of bounds Unintended side effects e.g. assignment to globals, a = b instead of a == b l Use of uninitialised variables case fall-through in a switch continued

22 240-222 CPT: Debug/722 l Pointers (memory leaks) Reversed logic in conditions e.g. x 5 l Macros (not included in this course, since they are so tricky to use)

23 240-222 CPT: Debug/723 10. Run-time Debugging Aids 10.1.Where to put printf() s? 10.2.Conditional Compilation 10.3 assert 10.4.Source-level Debugging

24 240-222 CPT: Debug/724 10.1. Where to put printf() s? l At the start of functions –print input parameters l At the return points of functions –print output parameters and return value l At the start of each loop printf() s must identify themselves printf() s should be conditional

25 240-222 CPT: Debug/725 10.2. Conditional Compilation l It is tiresome to keep adding and removing comments around printfs. l Conditional compilation: –selectively compile portions of the program –select which printfs to compile using a flag

26 240-222 CPT: Debug/726 Example #include #define TEST int foo(int); int main() { int n = 1; : #ifdef TEST printf("Reached main(); n = %d\n", n); #endif : foo(n); : } continued compiled, depending on the presence of TEST

27 240-222 CPT: Debug/727 int foo(int n) { n = n + 2; #ifdef TEST printf("Reached foo(); n = %d\n", n); #endif : } compiled, depending on the presence of TEST

28 240-222 CPT: Debug/728 Easier Switching Remove the #define line, and define TEST as part of the compilation: gcc -DTEST -o examp examp.c This is the same as having #define TEST inside examp.c

29 240-222 CPT: Debug/729 Fancier #if #include #define TESTFLAG 2 int main() { int n1 = 1, n2 = 2, n3 = 3; : #if TESTFLAG == 1 printf("In main(); n1 = %d\n", n1); #elif TESTFLAG == 2 printf("In main(); n2 = %d\n", n2); #else printf("In main(); n3 = %d\n", n3); #endif : foo(n1); }

30 240-222 CPT: Debug/730 10.3. assert l For testing the truth of assertions about the program. assert calls can include any C expression: assert(x ==y); assert(ptr != NULL); l If the evaluation is 0 (false) at runtime, an error message is produced, and execution is aborted.

31 240-222 CPT: Debug/731 Example #include #include int main() { int x = 9, y = 8; assert(x == (y+1)); foo(x, y); assert(x > y); bar(y, x); return 0; } continued

32 240-222 CPT: Debug/732 If NDEBUG is defined (either using #define, or -D ), then the asserts are ignored. l Only use asserts for debugging.

33 240-222 CPT: Debug/733 10.4. Source-level Debugging l Run your program within a debugging environment, where its execution can be examined as it progresses. l Popular UNIX tools: –gdb –dbx –sdb, adb

34 240-222 CPT: Debug/734 10.4.1. gdb gdb is the interactive source-level debugger from the GNU Project. Information on gdb : –man gdb –gdb -help –ginfo (not supported on calvin)

35 240-222 CPT: Debug/735 gdb Overview a)Main Features b)How to Use gdb c)Strategies for Use d)Basic gdb Commands e) lowercase.c f)Execution g)Using gdb on lowercase

36 240-222 CPT: Debug/736 10.4.1.a. Main Features l source-level breakpoints l source line stepping l monitoring variables l analysing core dumps

37 240-222 CPT: Debug/737 10.4.1.b. How to Use gdb 1) Compile with the -g option of gcc : $ gcc -g -o examp examp.c 2) Execute the object code within gdb : $ gdb examp

38 240-222 CPT: Debug/738 10.4.1.c. Strategies for Use l For non-terminating programs l For programs that give wrong answers

39 240-222 CPT: Debug/739 For Programs that Don't Terminate 1) Start the code: run 2) Wait; interrupt execution: ctrl-c 3) Find location: where –list –print 4) Step through the code: step

40 240-222 CPT: Debug/740 For Programs that Give Wrong Answers 1)Think about the error. 2)Put traces and/or breakpoints into the code. 3)Run the code. 4a)Examine trace output. 4b)At each breakpoint, step through the code. continued

41 240-222 CPT: Debug/741 5)Print variables and list code often: –print p.data –print account[] –list foo 6)If the code looks okay, continue execution. 7)Delete traces and/or breakpoints that are no longer needed.

42 240-222 CPT: Debug/742 10.4.1.d.Basic gdb Commands 1. Help: –help continued

43 240-222 CPT: Debug/743 2. Break points: –break break point at start of function –break break point at line –watch break point when variable is changed –ctrl-c interrupt execution continued

44 240-222 CPT: Debug/744 3. Manipulating traces and break points: –info break display the sequence numbers of the active break points –delete remove break point with that number continued

45 240-222 CPT: Debug/745 4. Displaying: –list list function –list list next 10 lines –list, list the range of lines –print print the variable's value –where display current calls continued

46 240-222 CPT: Debug/746 5. Moving on: –step execute the next source line –next step but proceed through a function call –cont continue execution –run begin execution of program –quit quit gdb

47 240-222 CPT: Debug/747 10.4.1.e. lowercase.c /* convert the input to lowercase */ #include #include #define SIZE 1024 void lower(char lin[]); int main() { char line[SIZE]; while (gets(line) != NULL) { lower(line); puts(line); } return 0; } continued

48 240-222 CPT: Debug/748 void lower(char lin[]) /* convert any uppercase letters to lowercase */ { int count = 0; while (lin[count] != '\0') if (isupper(lin[count])) { lin[count] = tolower(lin[count]); count++; } }

49 240-222 CPT: Debug/749 10.4.1.f. Execution $ gcc -o lowercase lowercase.c $ lowercase GGGGG ggggg ggg $ Run ‘suspended’; I had to type ctrl-c

50 240-222 CPT: Debug/750 10.4.1.g. Using gdb on lowercase l $ gcc -g -o lowercase lowercase.c l $ gdb lowercase l What's the error?

51 240-222 CPT: Debug/751 catsix{26}% gcc -g -o lowercase lowercase.c catsix{27}% gdb lowercase GDB is free... (gdb) run Starting program: /home/ad/temp/lowercase GGGGG ggggg ggg Program received signal SIGINT, Interrupt. 0x8000573 in lower (lin=0xbffff84c "ggg") at lowercase.c:28 28 while(lin[count] != '\0') continued Run ‘suspended’; I had to type ctrl-c

52 240-222 CPT: Debug/752 (gdb) where #0 0x8000573 in lower (lin=0xbffff84c "ggg") at lowercase.c:28 #1 0x800052c in main () at lowercase.c:16 #2 0x80004a4 in ___crt_dummy__ () #3 0x9d in ?? () Cannot access memory at address 0x33. (gdb) list lower 20 } 21 22 23 24 void lower(char lin[]) 25 { 26 int count = 0; 27 28 while(lin[count] != '\0') 29 if (isupper(lin[count])) { continued

53 240-222 CPT: Debug/753 (gdb) list 28 23 24 void lower(char lin[]) 25 { 26 int count = 0; 27 28 while(lin[count] != '\0') 29 if (isupper(lin[count])) { 30 lin[count] = tolower(lin[count]); 31 count++; 32 } continued

54 240-222 CPT: Debug/754 (gdb) step 29 if (isupper(lin[count])) { (gdb) print count $1 = 0 (gdb) print lin $2 = 0xbffff84c "ggg" (gdb) print lin[0] $3 = 103 'g' (gdb) step 32 } continued

55 240-222 CPT: Debug/755 (gdb) step 28 while(lin[count] != '\0') (gdb) step 29 if (isupper(lin[count])) { (gdb) step 32 } (gdb) print count $4 = 0 continued

56 240-222 CPT: Debug/756 (gdb) step 28 while(lin[count] != '\0') (gdb) step 29 if (isupper(lin[count])) { (gdb) step 32 } (gdb) print count $5 = 0 (gdb) quit The program is running. Quit anyway (and kill it)? (y or n) y catsix{29}%

57 240-222 CPT: Debug/757 gdb with Breakpoints catsix{30}% gdb lowercase GDB is free... (gdb) break lower Breakpoint 1 at 0x8000566: file lowercase.c, line 26. (gdb) run Starting program: /home/ad/temp/lowercase GGGGG continued

58 240-222 CPT: Debug/758 Breakpoint 1, lower (lin=0xbffff84c "GGGGG") at lowercase.c:26 26 int count = 0; (gdb) cont Continuing. ggggg ggg continued

59 240-222 CPT: Debug/759 Breakpoint 1, lower (lin=0xbffff84c "ggg") at lowercase.c:26 26 int count = 0; (gdb) cont Continuing. Program received signal SIGINT, Interrupt. 0x8000596 in lower (lin=0xbffff84c "ggg") at lowercase.c:29 29 if (isupper(lin[count])) { (gdb) quit The program is running. Quit anyway (and kill it)? (y or n) y catsix{31}% Run ‘suspended’; I had to type ctrl-c


Download ppt "240-222 CPT: Debug/71 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to talk about the various approaches to testing."

Similar presentations


Ads by Google