root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# gcc testgdb.c
//查看代码
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# vim testgdb.c#include <stdio.h>
int sum(int m);
int main(){
int *pi;
int i, n =0;
pi =&i;
int arr[3]={10, 20, 30};
pi = arr;
sum(50);for(i =0; i <=50; i++){
n += i;}
printf("The sum of 1-50 is %d \n", n);}
int sum(int m){
int i, n =0;for(i =1; i <= m; i++){
n += i;}
printf("The sum of 1-50 is %d \n", n);return n;
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# ls
a.out car.c hello.c lib make project test.c testgdb testgdb.c
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# ./a.out
The sum of 1-50 is 1275
The sum of 1-50 is 1275
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# gdb testgdb
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2)12.1
Copyright (C)2022 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-linux-gnu".
Type "show configuration"for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type"help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from testgdb...
(gdb) b *12
Breakpoint 1 at 0xc
(gdb) b sum
Breakpoint 2 at 0x800120e
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0xc
Cannot insert breakpoint 2.
Cannot access memory at address 0x800120e
(gdb)quit
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# cgdb(gdb) b main
Note: breakpoint 1 also set at pc 0x8001169.
Breakpoint 3 at 0x8001169
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001169
(gdb) condition 1 i ==5(gdb)r
Starting program: /root/bestidiocs2024/ch02/c/a.out
warning: opening /proc/PID/mem filefor lwp 188.188 failed: No such file or directory (2)
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001169
(gdb) d 1(gdb) d 3(gdb) d 10(gdb)run
Starting program: /root/bestidiocs2024/ch02/c/a.out
warning: opening /proc/PID/mem filefor lwp 185.185 failed: No such file or directory (2)
Failed to read a valid object file image from memory.
The sum of 1-50 is 1275
The sum of 1-50 is 1275[Inferior 1(process 185) exited normally]
The sum of 1-50 is 1275(gdb) b main //尝试单步运行
Breakpoint 1 at 0x8001175: file testgdb.c, line 6.
(gdb) step
The program is not being run.
(gdb) run
Starting program: /root/bestidiocs2024/ch02/c/testgdb
warning: opening /proc/PID/mem filefor lwp 220.220 failed: No such file or directory (2)
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001169
(gdb) c
Continuing.
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001169
Command aborted.
(gdb) step
Single stepping untilexit from function _start,
which has no line number information.
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001169
Command aborted.