I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 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 / debugging / debugger / memory checker
/ 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 / run the debugger with the executable jjj $ cc g sample.c $ 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
/ MEMORY CHECKER
/ ERRORS REGARDING MEMORY AND MEMORY DEBUGGER C / The C compiler does not check memory access error and memory leaks / familiarity with Assembly Language OS / Designed also for OS and device drivers C / errors regarding memory happen quite often / buffer overrun / memory leak 2 / free doubly / read from uninitialized memory / The memory debugger is for these troubles
dbx Sun / attached in Sun C Compiler Valgrind LinuxMacOS X / for Linux, MacOS X Electric Fence (efence) LinuxWindows*BSD / for Linux, Windows, *BSD google-perftools Google / performance profiler by Google / checks only for memory leaks LinuxWindows*BSDMacOS X / for Linux, Windows, *BSD, MacOS X
/ EXAMPLE OF MEMORY LEAK / source code $ cat leak.c #include <stdlib.h> #include <string.h> void mem_leak() { char *str = (char*)malloc(sizeof(char) * 32); strncpy(str, Hello, World!, 32); } void main(void) { mem_leak(); }
DBX / DETECT MEMORY LEAK IN DBX / enable check for memory leaks (dbx) check -memuse (dbx) run... (: 1 : 32 ) ====== ====== ======== ========================= 32 1 0x220d8 mem_leak < main
/ READ FROM UNINITIALIZED MEMORY / source code $ cat mcopy.c #include <stdlib.h> #include <string.h> void mem_copy() { char *hello = (char*)malloc(sizeof(char) * 32); strncpy(hello, Hello, World!, 32); char *str = (char*)malloc(sizeof(char) * 32); strncpy(hello, str, 32); /* */ } void main(void) { mem_copy(); }
DBX / DETECT INVALID MEMORY ACCESS IN DBX / enable checks for memory access (dbx) check -access (dbx) run... 1 0x22180 0x22180 32 : [1] mem_copy() 8 "mcopy.c [2] main() 14 "mcopy.c stopped in mem_copy : 9 "mcopy.c 9 strncpy(hello, str, 32);
/ PRACTICE
DBX / LET'S USE DBX dbx / let's use dbx / debugger function / memory checker function
MAIN() Exercise: PRINTS MAIN() ARGUMENTS / prints main() arguments.! #include <stdio.h>! int main(int argc, char* argv[])! {! int i;! for(i = 0; i < argc; i++)! {! }! }! printf("argv[%d]: %s\n", i, argv[i]);!
EXERCISE: DYNAMIC ALLOCATION OF 2-D ARRAY malloc() / use malloc() twice.! char array[m][n + 1] / take 2 arguments and create char array[m][n + 1].! array[0] "ABC..." / Fill "ABC..." in array [0].! array[1] "BCD..." / Fill with shifted string like "BCD..." in array[1] and after.! array[x]nul/ Terminate with NUL string in each array[x].! print / print the two arguments.! array[x] print / print each array[x] as one line.!
/ ANSWER 1. #include <stdio.h>! 2. #include <stdlib.h>! 3. void! 4. main(int argc, char **argv)! 5. {! 6. int m, n;! 7. int i, j;! 8. char **array;! 9. if (argc < 3)! 10. {! 11. printf ("%s <m> <n>\n", argv[0]);! 12. exit (1);! 13. }! 14. m = atoi (argv[1]);! 15. n = atoi (argv[2]);! 1. printf ("m = %d, n = %d\n", m, n);! 2. array = (char **) malloc (m * sizeof (char *));! 3. for (i = 0; i < m; i++)! 4. array[i] = (char *) malloc (n + 1);! 5. for (i = 0; i < m; i++)! 6. {! 7. for (j = 0; j < n; j++)! 8. array[i][j] = 'A' + i + j;! 9. array[i][n] = '\0';! 10. }! 11. for (i = 0; i < m; i++)! 12. printf ("%02d: %s\n", i, array[i]);! 13. }!
: / EXERCISE: CALCULATE FIBONACCI ARRAY #include <stdio.h> #include <stdlib.h> int fibonacci (int n) { } int i; int F[3]; F[0] = 0; F[1] = 1; for (i = 0; i < n; i++) { printf ("F_%d = %d + %d = %d \n", + F[1]); } i + 2, F[0], F[1], F[0] F[2] = F[0] + F[1]; F[0] = F[1]; F[1] = F[2]; return F[1]; int main (int argc, char **argv) { int n = 10; int ret; if (argc >= 2) n = atoi (argv[1]); ret = fibonacci (n); printf ("fibo[%d] = %d\n", n, ret); return 0; }
/ MARKOV CHAIN ALGORITHM / generate text without sense (for fun) / read ordinary text / split to words / create dictionary that records chain of words / records chains that happened at least once / generate text randomly along with the dictionary / the text will have no sense.