B Linux GDB
GDB Linux GNU GPL Linux Distribution Linux E-B.1 gcc g++ -g EB_01.cpp EB_01.cpp E/EB/EB_01.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /**** :EB_01.cpp : *****/ #include <iostream> using namespace std; void cal_ele(int i,int *add_ary,int *mul_ary); void main(void) { int add_ary[5]={0}; int mul_ary[5]={1}; int i; for(i=1;i<10;i++) cal_ele(i,add_ary,mul_ary); for(i=0;i<5;i++) cout << "add_ary[" << i << "]=" << add_ary[i] << endl; cout << "---------------------\n"; for(i=0;i<5;i++) cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; } void cal_ele(int i,int *add_ary,int *mul_ary) { E-B-2
Linux GDB B 20 21 22 } add_ary[i]=add_ary[i-1]+i; mul_ary[i]=mul_ary[i-1]*i; add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 segment fault (1) 0~4 add_ary 0!~4! mul_ary (2) add_ary 0,1,3,6,10 segment fault -g EB_01 g++ -g EB_01.cpp -o EB_01 (3) -g EB_01 E-B-3
E-B.2 -g EB_01 jhchen@aho:~/c_cpp/e/eb$ EB_01 GNU Red Hat Linux 7.x (5.0rh-15) (MI_OUT) Copyright 2001 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... () GDB (1) GDB EB_01 (2) () GDB (3) -q quit E-B-4
Linux GDB B () quit jhchen@aho:~/c_cpp/e/eb$ Tab qu Tab quit E-B.3 list run shell Linux shell GDB exit shell linux Linux shell shell ls help help all list list () list 1 /**** :EB_01.cpp : *****/ 2 #include <iostream> 3 using namespace std; 4 void cal_ele(int i,int *add_ary,int *mul_ary); 5 void main(void) 6 { E-B-5
7 int add_ary[5]={0}; 8 int mul_ary[5]={1}; 9 int i; 10 for(i=1;i<10;i++) () list 11 cal_ele(i,add_ary,mul_ary); 12 for(i=0;i<5;i++) 13 cout << "add_ary[" << i << "]=" << add_ary[i] << endl; 14 cout << "---------------------\n"; 15 for(i=0;i<5;i++) 16 cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; 17 } 18 void cal_ele(int i,int *add_ary,int *mul_ary) 19 { 20 add_ary[i]=add_ary[i-1]+i; () list 21 mul_ary[i]=mul_ary[i-1]*i; 22 } () quit jhchen@aho:~/c_cpp/e/eb$ (1) list 10 EB_01 22 (2) 3 list () list 7,19 7 int add_ary[5]={0}; 8 int mul_ary[5]={1}; 9 int i; 10 for(i=1;i<10;i++) 11 cal_ele(i,add_ary,mul_ary); 12 for(i=0;i<5;i++) 13 cout << "add_ary[" << i << "]=" << add_ary[i] << endl; 14 cout << "---------------------\n"; E-B-6
Linux GDB B 15 for(i=0;i<5;i++) 16 cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; 17 } 18 void cal_ele(int i,int *add_ary,int *mul_ary) 19 { () quit jhchen@aho:~/c_cpp/e/eb$ 7~19 list 7,19 run run () run Starting program: /root/c_cpp/e/eb/eb_01 add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 Program received signal SIGSEGV, Segmentation fault. 0x00000015 in?? () () quit A debugging session is active. Do you still want to close the debugger?(y or n) y E-B-7
run run EB_01 Ctrl + C shell Linux Shell GDB GDB Linux Shell shell Linux Shell () shell jhchen@aho:~/c_cpp/e/eb$ ls EB_01 EB_01.cpp EB_01_1.cpp EB_02.cpp jhchen@aho:~/c_cpp/e/eb$ exit exit () quit jhchen@aho:~/c_cpp/e/eb$ shell Linux Shell GDB GDB shell exit GDB quit shell shell linux Linux shell Linux Shell GDB GDB Linux Shell shell () shell ls EB_01 EB_01.cpp EB_01_1.cpp EB_02.cpp () quit jhchen@aho:~/c_cpp/e/eb$ E-B-8
Linux GDB B GDB shell ls Linux shell ls help help () help run Start debugged program. You may specify arguments to give it. Args may include "*", or "[...]"; they are expanded using "sh". Input and output redirection with ">", "<", or ">>" are also allowed. With no arguments, uses arguments last specified (with "run" or "set args"). To cancel previous arguments and run with no arguments, use "set args" without arguments. () quit jhchen@aho:~/c_cpp/e/eb$ GDB help run run help all all () help all ni -- Step one instruction si -- Step one instruction exactly E-B-9
stepping -- Specify single-stepping behavior at a tracepoint tp -- Set a tracepoint at a specified line or function or address where -- Print backtrace of all stack frames ws -- Specify single-stepping behavior at a tracepoint append binary -- Append target code/data to a raw binary file append memory -- Append contents of memory to a raw binary file append value -- Append the value of an expression to a raw binary file awatch -- Set a watchpoint for an expression break -- Set breakpoint at specified line or function catch -- Set catchpoints to catch events clear -- Clear breakpoint at specified line or function commands -- Set commands to be executed when a breakpoint is hit condition -- Specify breakpoint number N to break only if COND is true delete -- Delete some breakpoints or auto-display expressions disable -- Disable some breakpoints enable -- Enable some breakpoints hbreak -- Set a hardware assisted breakpoint ignore -- Set ignore-count of breakpoint number N to COUNT rbreak -- Set a breakpoint for all functions matching REGEXP rwatch -- Set a read watchpoint for an expression tbreak -- Set a temporary breakpoint ---Type <return> to continue, or q <return> to quit--- q Enter Enter E-B.4 E-B-10
Linux GDB B GDB break break break watch break () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () quit A debugging session is active. Do you still want to close the debugger?(y or n) y jhchen@aho:~/c_cpp/e/eb$ (1) break 11 11 (2) run 11 11 E-B-11
break () break cal_ele Breakpoint 1 at 0x80487e1: file EB_01.cpp, line 20. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, cal_ele (i=1, add_ary=0xbffffaa8, mul_ary=0xbffffa94) at EB_01.cpp:20 20 add_ary[i]=add_ary[i-1]+i; () (1) break cal_ele cal_ele (2) run cal_ele (3) cal_ele mul_ary=0xbffffa94) (i=1, add_ary=0xbffffaa8, break 100 100 EB_01 11 9 i>=5 E-B-12
Linux GDB B () break 11 if i>=5 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () break 11 if i>=5 i>=5 11 () break cal_ele if i>=5 Breakpoint 1 at 0x80487e1: file EB_01.cpp, line 20. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, cal_ele (i=5, add_ary=0xbffffaa8, mul_ary=0xbffffa94) at EB_01.cpp:20 20 add_ary[i]=add_ary[i-1]+i; () break cal_ele if i>=5 cal_ele i>=5 watch GDB watch point watch point break point GDB E-B-13
watch point watch point watch point watch i > 4 EB_02.cpp EB_02.cpp E/EB/EB_02.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /**** :EB_02.cpp : *****/ #include <iostream> using namespace std; void main(void) { int a,b,i; a=0; b=0; for(i=1;i<10;i++) { a=b+10; b=a+10; a=b+25; b=a+100; } cout << "a=" << a << "\t" << "b=" << b << endl; } a>500 break point 11 13 watch point jhchen@aho:~/c_cpp/e/eb$ g++ -g EB_02.cpp -o EB_02 jhchen@aho:~/c_cpp/e/eb$ -q EB_02 () watch a > 500 No symbol "a" in current context. () break 8 Breakpoint 1 at 0x804865d: file EB_02.cpp, line 8. () run E-B-14
Linux GDB B Starting program: /root/c_cpp/e/eb/eb_02 Breakpoint 1, main () at EB_02.cpp:8 8 b=0; () watch a > 500 Hardware watchpoint 2: a > 500 () continue Continuing. Hardware watchpoint 2: a > 500 Old value = false New value = true main () at EB_02.cpp:12 12 b=a+10; () (1) watch a>500 GDB a a 6 (2) break point 8 break 8 GDB 8 a (3) watch a>500 GDB GDB a (4) continue GDB continue watch point 12 a>500 11 E-B.5 GDB break point watch point E-B-15
print whatis () break 11 if add_ary[0]>100 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $1 = 6 () whatis i type = int () (1) add_ary add_ary 0,1,3,6,10 100 (120,720,5040,40320,362880) add_ary[0]>100 (2) run add_ary[0]>100 (3) i print i (4) print i GDB $1=6 i 6 (5) i whatis i GDB i i int E-B-16
Linux GDB B E-B.6 continue step next () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $1 = 1 () continue Continuing. Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $2 = 2 () next Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () print i $3 = 3 () step cal_ele (i=3, add_ary=0xbffffaa8, mul_ary=0xbffffa94) at EB_01.cpp:20 E-B-17
20 add_ary[i]=add_ary[i-1]+i; () step 21 mul_ary[i]=mul_ary[i-1]*i; () (1) 11 (2) run 11 (3) continue 10 11 (4) next (5) step cal_ele 20 (6) step 21 E-B.7 break clear delete info breakpoints disable enable E-B-18
Linux GDB B info breakpoints info breakpoints () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break 22 Breakpoint 2 at 0x8048833: file EB_01.cpp, line 22. () break cal_ele Breakpoint 3 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x08048833 in cal_ele(int, int *, int *) at EB_01.cpp:22 3 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () break 3 info breakpoints GDB Num Type breakpoint watchpoint Disp Enb Address What dis: keep: y: n: E-B-19
() run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () info breakpoint Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 breakpoint already hit 1 time 2 breakpoint keep y 0x08048833 in cal_ele(int, int *, int *) at EB_01.cpp:22 3 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () 1 breakpoint already hit 1 time clear clear () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 E-B-20
Linux GDB B () clear 11 Deleted breakpoint 1 () info breakpoints Num Type Disp Enb Address What 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () clear cal_ele Deleted breakpoint 2 () info breakpoints No breakpoints or watchpoints. () (1) clear 11 11 (2) clear cal_ele cal_ele clear delete delete () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 E-B-21
() delete 2 () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 () delete 2 2 break cal_ele disable disable () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () disable 2 () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep n 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () disable 2 2 Enb n E-B-22
Linux GDB B disable enable enable () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () break cal_ele Breakpoint 2 at 0x80487e1: file EB_01.cpp, line 20. () disable () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep n 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep n 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () enable 2 () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep n 0x08048688 in main at EB_01.cpp:11 2 breakpoint keep y 0x080487e1 in cal_ele(int, int *, int *) at EB_01.cpp:20 () enable 2 2 Enb y E-B-23
enable enable once GDB enable once () break 11 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. () info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x08048688 in main at EB_01.cpp:11 () enable once 1 () info breakpoints Num Type Disp Enb Address What 1 breakpoint dis y 0x08048688 in main at EB_01.cpp:11 () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); () info breakpoints Num Type Disp Enb Address What 1 breakpoint dis n 0x08048688 in main at EB_01.cpp:11 breakpoint already hit 1 time () continue Continuing. add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 E-B-24
Linux GDB B add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 Program received signal SIGSEGV, Segmentation fault. 0x00000015 in?? () () (1) break 11 Disp keep Enb y (2) enable once 1 1 (3) Disp dis Enb y (4) run (5) Disp dis Enb n (6) continue E-B.8 1 EB_01.cpp E-B-25
jhchen@aho:~/c_cpp/e/eb$ g++ EB_01.cpp -o EB_01 jhchen@aho:~/c_cpp/e/eb$./eb_01 add_ary[0]=120 add_ary[1]=720 add_ary[2]=5040 add_ary[3]=40320 add_ary[4]=362880 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 Segment fault jhchen@aho:~/c_cpp/e/eb$ 2 Segment fault add_ary jhchen@aho:~/c_cpp/e/eb$ g++ -g EB_01.cpp -o EB_01 -g 3 GDB () 4 add_ary add_ary cal_ele 11 add_ary[0]>100 add_ary 100 () break 11 if add_ary[0]>100 Breakpoint 1 at 0x8048688: file EB_01.cpp, line 11. E-B-26
Linux GDB B 5 run () run Starting program: /root/c_cpp/e/eb/eb_01 Breakpoint 1, main () at EB_01.cpp:11 11 cal_ele(i,add_ary,mul_ary); 6 11 add_arr[0] 100 100 i () print i $1 = 6 7 i=6 add_arr[0] add_arr[6] mul_arr[6] GDB 5 () quit A debugging session is active. Do you still want to close the debugger?(y or n) y 8 EB_01_1.cpp 1 2 3 4 5 /**** :EB_01_1.cpp : *****/ #include <iostream> using namespace std; void cal_ele(int i,int *add_ary,int *mul_ary); void main(void) E-B-27
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 { int add_ary[5]={0}; int mul_ary[5]={1}; int i; for(i=1;i<5;i++) cal_ele(i,add_ary,mul_ary); for(i=0;i<5;i++) cout << "add_ary[" << i << "]=" << add_ary[i] << endl; cout << "---------------------\n"; for(i=0;i<5;i++) cout << "mul_ary[" << i << "]=" << mul_ary[i] << endl; } void cal_ele(int i,int *add_ary,int *mul_ary) { add_ary[i]=add_ary[i-1]+i; mul_ary[i]=mul_ary[i-1]*i; } 9 EB_01_1.cpp Segment fault jhchen@aho:~/c_cpp/e/eb$ g++ EB_01_1.cpp -o EB_01_1 jhchen@aho:~/c_cpp/e/eb$./eb_01_1 add_ary[0]=0 add_ary[1]=1 add_ary[2]=3 add_ary[3]=6 add_ary[4]=10 --------------------- mul_ary[0]=1 mul_ary[1]=1 mul_ary[2]=2 mul_ary[3]=6 mul_ary[4]=24 E-B-28