gdb 使用

1.计算一个结构体成员offset

p/x &((struct A *)0)->b 其中A表示结构体,b是成员

2.输入被调试程序输入参数:

r -sysdir /home/skyeye/android_test -system initrd_96M.img -data userdata.img -ramdisk  ramdisk.img -kernel zImage -show-kernel -verbose

3.breakpoint

 b cpu_arm_exec or

(gdb) b  *0x40000000
Breakpoint 3 at 0x40000000

查看断点信息:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000057bf50 in qemu_cpu_exec at /home/skyeye/s2e/s2e/qemu_android/cpus.c:562
    breakpoint already hit 2 times
2       breakpoint     keep y   0x0000000000513bb0 in cpu_arm_exec at /home/skyeye/s2e/s2e/qemu_android/cpu-exec.c:248
    breakpoint already hit 1 time
3       breakpoint     keep y   0x0000000040000000

4.查看某个变量

(gdb)  p/x *(struct CPUARMState *)0x0x1d9a010


5.查看某个地址的值:

(gdb) x/20i 0x40000000

6.查看寄存器

(gdb) info  registers 

7.查看变量类型:

(gdb) ptype env

(gdb) whatis env

8.查找变量:

(gdb) info variables env (查找env变量,可以着正则式);info variables查看所以变量。类似的查看函数,info functions 以及info  functions REX

9.查看类型定义:

(gdb) info types env 使用方法跟上面类似

10.signals :

info  signals

handle signal keywords...
Change the way GDB handles signal signal. signalcan be the number of a signal or its name (with or without the `SIG' at the beginning); a list of signal numbers of the form `low-high'; or the word `all', meaning all theknown signals. The keywords say what change to make.

The keywords allowed by the handle command can be abbreviated.Their full names are:

nostop
GDB should not stop your program when this signal happens. It maystill print a message telling you that the signal has come in.

stop
GDB should stop your program when this signal happens. This impliesthe print keyword as well.

print
GDB should print a message when this signal happens.

noprint
GDB should not mention the occurrence of the signal at all. Thisimplies the nostop keyword as well.

pass

noignore
GDB should allow your program to see this signal; your programcan handle the signal, or else it may terminate if the signal is fataland not handled. pass and noignore are synonyms.

nopass

ignore

GDB should not allow your program to see this signal.nopass and ignore are synonyms.

core dump:

If you can enable core dumps on that system, just run the program, let it crash, then pull the core dump off the target machine onto your development machine and load it into a GDB built to debug the target architecture - that should tell you exactly where the crash occurred. Just use GDB's core command to load the core file into the debugger.

  • To enable core dumps on the target:

    ulimit -c unlimited
  • pseudo-files that control how the core file will be named (cat these to see the current configuration, write to them to change the configuration):

    /proc/sys/kernel/core_pattern
    /proc/sys/kernel/core_uses_pid

On my system, once core dumps are enabled, a crashing program will write a file simply named "core" in the working directory. That's probably good enough for your purposes, but changing how the core dump file is named lets you keep a history of core dumps if that's necessary (maybe for a more intermittent problem).



### GDB调试工具的使用指南 #### 启用GDB并设置调试环境 为了启用GDB调试器,需要先编译带有调试信息的程序。可以通过`gcc`命令添加`-g`选项来生成调试信息[^2]。例如: ```bash gcc -g program.c -o program ``` 随后可以启动GDB调试器,并加载目标程序: ```bash gdb ./program ``` 如果需要向被调试程序传递参数,则可以在运行时通过`run`命令指定这些参数[^1]: ```bash (gdb) run arg1 arg2 ``` #### 使用GDB命令进行调试 在GDB环境中,有多种命令可以帮助开发者查看程序的状态、控制执行流以及分析错误。 ##### 查看源码和状态信息 使用`list`命令可以查看当前文件中的源代码片段[^4]: ```bash (gdb) list ``` 此命令会显示当前上下文附近的代码行数,默认为10行。 ##### 断点管理 断点用于暂停程序以便进一步检查其行为。创建断点的方法如下: ```bash (gdb) break main ``` 这将在函数`main`入口处设置一个断点。还可以通过行号设置断点: ```bash (gdb) break filename.c:line_number ``` 删除特定编号的断点可通过以下方式实现: ```bash (gdb) delete breakpoint_number ``` ##### 单步执行 单步执行允许逐条语句跟踪程序逻辑。以下是常用的单步执行命令: - `step`: 进入子函数继续调试。 - `next`: 不进入子函数而跳过它。 - `continue`: 继续执行直到遇到下一个断点。 ##### 变量监控与表达式求值 利用`print`命令能够打印变量或表达式的值: ```bash (gdb) print variable_name ``` 对于复杂的数据结构或者指针内容,可能还需要借助格式化输出功能[^2]: ```bash (gdb) print/x address_value # 显示十六进制地址 ``` #### 分析Core Dump文件 当应用程序异常终止时可能会生成core dump文件。要使用GDB对此类文件进行分析,需按照下面的方式操作[^3]: ```bash gdb executable_file core_dump_file ``` 之后即可像正常调试一样探索崩溃时刻的信息,比如回溯调用链(`bt`)、检视寄存器状态等。 --- ### 常见GDB命令总结表 | 功能 | 对应命令 | |---------------------|------------------------------| | 开始调试 | gdb target_program | | 执行程序 | (gdb) run | | 列出源代码 | (gdb) list | | 设置断点 | (gdb) break function_or_line| | 删除断点 | (gdb) delete breakpoint_num | | 步骤前进(不进入函数)| (gdb) next | | 步骤前进(进入函数内部)|(gdb) step | | 继续执行到下一断点 | (gdb) continue | | 打印变量 | (gdb) print varname | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值