One code
#include<iostream> using namespace std; int func(int n){ int result=0; for(int i=1;i<=n;i++){ result+=i; } return result; } int main(){ int arr[10]; arr[0]=0; arr[1]=1; for(int i=2;i<10;i++){ arr[i]=arr[i-1]+arr[i-2]; } cout<<"arr[9]"<<arr[9]<<endl; cout<<"func(9)"<<func(9)<<endl; return 0; }
2. Compile and run
[root@localhost charpter05]# g++ -g -o test 0504.cpp [root@localhost charpter05]# ./test arr[9]34 func(9)45
Three gdb debugging
1. Enter gdb test command to start gdb
[root@localhost charpter05]# gdb test GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /root/omc++/charpter05/test...done. (gdb)
2 input l, list the source code
(gdb) l 3 int func(int n){ 4 int result=0; 5 for(int i=1;i<=n;i++){ 6 result+=i; 7 } 8 return result; 9 } 10 int main(){ 11 int arr[10]; 12 arr[0]=0;
Press Enter to repeat the last command
(gdb) 13 arr[1]=1; 14 for(int i=2;i<10;i++){ 15 arr[i]=arr[i-1]+arr[i-2]; 16 } 17 cout<<"arr[9]"<<arr[9]<<endl; 18 cout<<"func(9)"<<func(9)<<endl; 19 return 0; 20 }
4 ﹣ execute ﹣ b 15, indicating to set breakpoint at source line 15
(gdb) b 15 Breakpoint 1 at 0x40090b: file 0504.cpp, line 15.
5 execute b func, indicating that the breakpoint is set at the entrance of function func
(gdb) b func Breakpoint 2 at 0x4008c4: file 0504.cpp, line 4.
6. Execute info break to view the breakpoint information
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000040090b in main() at 0504.cpp:15 2 breakpoint keep y 0x00000000004008c4 in func(int) at 0504.cpp:4
7 execute r command, which is the abbreviation of run command
(gdb) r Starting program: /root/omc++/charpter05/test Breakpoint 1, main () at 0504.cpp:15 15 arr[i]=arr[i-1]+arr[i-2]; Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.1.x86_64 libstdc++-4.8.5-16.el7_4.1.x86_64
The program stops at the breakpoint.
8 ﹣ input ﹣ n, indicating the execution of a single statement, and the abbreviation of next command
(gdb) n 14 for(int i=2;i<10;i++){
8. Enter p I and p arr[i] to print the values of variables I and arr[i], respectively.
(gdb) p i $1 = 2 (gdb) p arr[i] $2 = 1
9 input bt, view function stack
(gdb) bt #0 func (n=9) at 0504.cpp:5 #1 0x000000000040096b in main () at 0504.cpp:18
10 enter finish to exit the function
(gdb) finish Run till exit from #0 func (n=9) at 0504.cpp:5 0x000000000040096b in main () at 0504.cpp:18 18 cout<<"func(9)"<<func(9)<<endl; Value returned is $3 = 45
11 input q, end debugging
(gdb) q A debugging session is active. Inferior 1 [process 1095] will be killed. Quit anyway? (y or n) y