这里提供一个简单的C语言程序作为示例:

#include <stdio.h>

int main() {
    int a = 1;
    int b = 2;
    int sum = a + b;
    printf("The sum of %d and %d is %d\n", a, b, sum);
    return 0;
}

首先,在终端中使用gcc编译该程序:

gcc -g -o test test.c

其中,-g选项表示生成调试信息。

接下来,使用gdb启动程序:

gdb test

使用r命令运行程序,可以看到程序正常输出:

(gdb) r
Starting program: /path/to/test 
The sum of 1 and 2 is 3
[Inferior 1 (process 1234) exited normally]

接下来,使用layout命令打开GDB的TUI界面,在左侧窗口中可以看到程序的源代码,右侧窗口中是调试信息:

(gdb) layout

在源代码中设置断点:

(gdb) b 8

使用c命令继续运行程序,程序会在第8行停下来:

(gdb) c
Continuing.

Breakpoint 1, main () at test.c:8
8           printf("The sum of %d and %d is %d\n", a, b, sum);

使用info命令查看当前的变量值:

(gdb) info locals
a = 1
b = 2
sum = 3

可以看到,a、b、sum三个变量的值都正确。

接下来,可以使用next命令逐行执行程序:

(gdb) next
The sum of 1 and 2 is 3
9           return 0;

使用finish命令运行到函数结束:

(gdb) finish
Run till exit from #0  main () at test.c:9

最后,使用quit命令退出GDB调试:

(gdb) q
gdb+pwndbg扫描一个简单的C语言

原文地址: http://www.cveoy.top/t/topic/b7W1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录