I117 II I117 PROGRAMMING PRACTICE II SOFTWARE DEVELOPMENT ENV. 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu@jaist.ac.jp
/ SCHEDULE 1. 2011/06/07(Tue) / Basic of Programming 2. 2011/06/09(Thu) / Fundamental data structures 3. 2011/06/14(Tue) / Memory Management 1 4. 2011/06/16(Thu) / Memory Management 2 5. 2011/06/21(Tue) / Debugging 6. 2011/06/23(Thu) / Software Development Env. 1 7. 2011/06/28(Tue) / Software Development Env. 2 8. 2011/06/30(Thu) / Data Structure : Tree 9. 2011/07/05(Tue) / Data Structure: Hash 10. 2011/07/07(Thu) / Understanding Programs 1 11. 2011/07/12(Tue) / Understanding Programs 2 12. 2011/07/14(Thu) / Script Language 1 13. 2011/07/19(Tue) / Script Language 2 14. 2011/07/21(Thu) / Other Languages 15. 2011/07/26(Tue) / Examination
/ FINAL REPORT free topic : decide topic by 7/5(Tue) report us of the topic by e-mail: i117report@jaist.ac.jp / Sound file converter / web crawler (CalDAV) calendar program new shell, editor, window manager rsync encrypt/decrypt, rsync markov-chain program choose good theme TAask TA and me The final report must have deep consideration
/ TODAY'S INDEX / dubugging / debugging method printf / printf debug / debugger / memory checker
/ DEBUGGING METHOD
PRINTF / PRINTF DEBUG / print inside states of the program to debug Cprintf / use printf printf / called "printf debug" even in languages other than C / pros / simple / can be used when debugger cannot be used / cons printf / printf function can affect results printf / situation such as it works with printf and doesn't without printf / happens in multithread programs and device drivers printf / you have to (be able to) compile the program / compilation takes time
/ A PROGRAM WITH BUG / the function has some bugs int fibonacci(int n) { int num1 = 0; int num2 = 1; int tmp; int i; for (i = 0; i < n; i++) { tmp = num2; num1 = num2; num2 += tmp; } The definition of fibonacci numbers: F 0 = 0, F 1 = 1 F n+2 = F n + F n+1 } return num1;
PRINTS EACH STEPS printf / an example with printf int fibonacci(int n) { int num1 = 0; int num2 = 1; int tmp; int i; for (i = 0; i < n; i++) { tmp = num2; printf( tmp = %d\n, tmp); num1 = num2; printf( num1 = %d\n, num1); } printf( num2 = %d + %d\n, num2, tmp); num2 += tmp; printf( num2 = %d\n\n, num2); } return num1;
PRINTF / OUTPUTS fibonacci(1): results of fibonacci (1) tmp = 1 /* F0 = 0 (incorrect) */ num1 = 1 /* F1 = 1 (correct) */ num2 = 1 + 1 /* F2 = F0 + F1 = 0 + 1 (incorrect)*/ num2 = 2 /* F2 = 1 (incorrect) */ 2 / two parts seem incorrect for (i = 0; i < n; i++) { tmp = num1; /* tmp = num2; / bug fixed */ num1 = num2; num2 += tmp; }
/ USE OF PREPROCESSOR printf / cumbersome to remove printfs / must remove all... / sometimes necessary to revive the printf DEBUGprintf / use of DEBUG macro int fibonacci(int n) { int num1 = 0, num2 = 1, tmp, i; for (i = 0; i < n; i++) { #ifdef DEBUG printf( num1 = %d, num2 = %d\n, num1, num2); #endif tmp = num2, num1 = num2, num2 += tmp; } return num1; }
DEBUG / WITH "DEBUG" #define DEBUG /define in program #define DEBUG int fibonacci(int n) {... } -DDEBUG / define in compilation $ ls fib.c $ cc DDEBUG fib.c / see the difference $ cc DDEBUG E fib.c $ cc E fib.c
/ USE OF MACRO, PREPROCESSOR DEBUGprintf / define a macro that runs printf only when "DEBUG" is defined. #ifdef DEBUG #define D(X) printf X #else #define D(X) #endif / usage D(( num1 = %d\n, num1)); /* (())*/
/ WHEN LOTS OF LOGS ARE PRINTED... tee / output both to the display and to a file $./a.out tee output.txt / only to a file $./a.out > output.txt
/ DEBUGGER
DBX / DEBUG USING DBX dbx / what is dbx Unix / source level debugger Sun / attached in Sun compiler C, C++, Java, Fortran 77, Fortran 99 / what can be done / step runs for each line / trace of function call / set a break point / display and set a variable / display a register / display contents of memory etc...
DBX -g / with g option, debugging info is attached in the executable file $ cc g sample.c / run the debugger with the executable $ dbx./a.out a.out ld.so.1 libc.so.1 / display help (dbx) help... (dbx) help stop... # # stop
/ EXAMPLE fib.c and fibmain.c fib.c 1 int fibonacci(int n) 2 { 3 int num1 = 0, num2 = 1; 4 int tmp, i; 5 6 for (i = 0; i < n; i++) 7 { 8 tmp = num1; 9 num1 = num2; 10 num2 += tmp; 11 } 12 13 return num1; 14 } fibmain.c 1 #include <stdio.h> 2 3 extern int fibonacci(int n); 4 5 void main(void) 6 { 7 int f10 = fibonacci(10); 8 printf( F(10) = %d\n, 9 f10); 10 }
NUM1, NUM2 / DISPLAY VARIABLE dbx / compile, start dbx $ cc g -c fib.c $ cc g fib.o fibmain.c $ dbx a.out fib.c9 set a break point (dbx) stop at fib.c:9 (2) stop at fib.c :9 / run the program on the debugger (dbx) run... stopped in fibonacci : 9 fib.c 9 num1 = num2; num1, num2 / print variables (dbx) print num1, num2 num1 = 0 num2 = 1
/ STEP RUNS AND ASSIGNMENT 1 execute for a single line (dbx) next stoopped in fibonacci : 6 fib.c 6 for (i = 0; i < n; i++) (dbx) print num1, num2 num1 = 1 num2 = 1 1next step / what's the difference in "next" and "step"? num1 0 / assign a value (dbx) assign num1 = 0 (dbx) print num1 num1 = 0
/ PRINT CALL STACK AND CHANGE FRAME / display call stack and print a register called "program counter" (dbx) where =>[1] fibonacci(n = 10), 6 fib.c [2] main(), 7 fibmain.c (dbx) print fx $pc $pc = 0x10c84 main / go back to main() (dbx) frame 2 :main 7 int f10 = fibonacci(10) (dbx) where [1] fibonacci(n = 10), 6 fib.c =>[2] main(), 7 fibmain.c (dbx) print fx $pc $pc = 0x10cd8
STACK / HOW STACK WORKS PUSH POP SPARC : SP x86 : SP(16bit)/ESP(32bit)/RSP(64bit) SPARC: FP x86 : BP(16bit)/EBP(32bit)/RBP(64bit) high-address
/ CONTINUE AND DELETE BREAKPOINTS / continue the run (dbx) cont stoopped in fibonacci : 9 fib.c 9 num1 = num2; / delete the break points and continue (dbx) delete all (dbx) cont F(10) = 55 1
DBX / EXITING DBX / exit (dbx) exit
/ PRACTICE
DBX / LET'S USE DBX dbx / let's use dbx / debbuger / memory checker