文章目录
参考文档:
- 官网:http://www.sourceware.org/gdb/
- Documentation: https://sourceware.org/gdb/current/onlinedocs/gdb/
- 离线版本下载:https://sourceware.org/gdb/current/onlinedocs/
- man:https://manpages.org/gdb
- Command, Variable, and Function Index
Documentation页面,把第1章(Sample,或者说QuickStart)、第2章读读,三到五章练练就入门了。后面的根据工作需要再学习。
常用指令,可以简单参考man帮助页:
Here are some of the most frequently needed GDB commands:
break...
run...
help指令显示的信息也很全,比如help set print
一. 环境
vscode远程调试
目标linux上需要安装gdb和gbdserver,当然一般都默认安装好的:
sudo apt install gdb
sudo apt install gdbserver
VScode安装Remote Development插件。
Linux端记着防火墙允许22端口出入,ubuntu执行命令如下:
sudo ufw allow 22
ssh连接成功后,需要在linux端安装c/c++ vscode插件(插件页面会显示install in ssh),路径/home/用户名/.vscode-server/extensions/
建议生成rsa密钥对,不然老是输入password很麻烦:
starr@starr-VirtualBox:~/.ssh$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/starr/.ssh/id_rsa): /home/starr/.ssh/vscode_rsa
starr@starr-VirtualBox:~/.ssh$ ll
total 20
drwx------ 2 starr starr 4096 3月 17 11:23 ./
drwxr-xr-x 26 starr starr 4096 3月 17 11:09 ../
-rw-r--r-- 1 starr starr 222 3月 17 09:45 known_hosts
-rw------- 1 starr starr 1675 3月 17 11:23 vscode_rsa
-rw-r--r-- 1 starr starr 404 3月 17 11:23 vscode_rsa.pub
starr@starr-VirtualBox:~/.ssh$ starr@starr-VirtualBox:~/.ssh$ cat vscode_rsa.pub >> authorized_keys
如上,公钥添加到authorized_keys文件,私钥则放到windows,并配置C:\Users\用户名\\.ssh\config
:
Host 192.168.56.101
HostName 192.168.56.101
User starr
PreferredAuthentications publickey
IdentityFile C:\Users\starr\.ssh\vscode_rsa
二. 文档目录
记一些比较有用的官方文档。
2. Getting In and Out of GDB
2.1 启动GDB,常规启动elf、挂载进程、分析core dump等启动方法。
gdb program
gdb program core
gdb --args gcc -O2 -c foo.c
# attach
gdb program 1234
gdb -p 1234
# 补充
exec-file program
2.3. Shell Commands
2.4. Logging
3. GDB commands
3.3. 命令补全,敲两下tab,命令、参数、变量都可以提示。
3.5. 帮助,help等。注意info用来查询目标debugee状态,show用来查询gdb的状态。
info
常用查询:
- frame查看栈帧信息,;
- files/target, 还有proc mappings,查询内存信息;
4. Running Programs Under GDB
4.2 Starting your Program, start命令的作用相当于在main设置一个临时断点,然后调用’ run '命令,需要有调试信息。
starti 相比start,是在入口点断下。
Attach
4.7 Debugging an Already-running Process
./a.out & # 后台运行
gdb -p PID
info file
可以查看当前debuggee信息。
inferior
4.9 Debugging Multiple Inferiors Connections and Programs
GDB支持多个调试会话。相关命令有inferior,maint …
快照
4.12 Setting a Bookmark to Return to Later
又叫checkpoint,Bookmark ,很有用的两个命令:checkpoint, restart
。
5. Stopping and Continuing
GDB的4种断点
- Breakpoints, Watchpoints, and Catchpoints, 主要命令有break、watch,catch。
还有一种断点,tracepoints, 在第13章。
关于break执行断点,要清楚如何指定location,即行数、文件函数、以及地址来下断点,其次是条件断点、一次性断点、硬件断点:
break [file.c:]1
break … [[-force-condition] if cond] 条件断点,也可以用condition命令
tbreak 一次性/临时断点
hbreak 硬件断点
thbreak
结合正则表达式:
void func1(){}
void func2(){}
void func3(){}
// rbreak [file:]func*
// rbreak . 所有函数下断
5.1.7, 断下时执行命令
5.1.9 一个很有用的保存断点功能
gef➤ save breakpoints b.txt
Saved to file 'b.txt'.
gef➤ shell cat b.txt
break main
break /home/starr/Documents/CProject/ElfTest/test.c:func1
commands
silent
printf "break"
end
watchpoint其实就是硬件读写断点,衍生还有rwatch, awatch指令,断下时显示如下:
Hardware watchpoint 2: nNum
Old value = 0x0
New value = 0x1
main () at /home/starr/Documents/CProject/ElfTest/test.c:9
9 return 0;
catch/tcatch 命令用于捕捉事件,比如:
throw/catch 异常
load/unload 动态库加载/卸载
syscall 系统调用
5.2 Continuing and Stepping
常用的,n/s/c指令,即步过/步入/继续。
gdb默认不会进入不带调试信息的函数,比如printf,执行set step-mode on
可以si步入printf。
finish
执行到下一个return, until/u
执行到指定的location。另一种是文档17.4的return RETVALUE
指令,可以直接返回。
以指令为单位执行:si/ni COUNT
skip boring
, 跳过执行, 比如func(boring())
,s就会直接进入func。
8. Examining the Stack
8.2 backtrace/bt,栈回溯。常用参数:-full
,
8.3 frame/up/down 切换栈帧。
10. Examining Data(!!!)
一个显示stl容器的插件:http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
cat dbinit_stl_views-1.03.txt >> ~/.gdbinit
建议先执行set print pretty on
10.6 Examining Memory
这一节就是常用的x命令,掌握得越熟越好。
x [/nfu] addr
n, the repeat count
counting by units u
f, the display format
https://sourceware.org/gdb/current/onlinedocs/gdb/Output-Formats.html
‘x’ hex, default
‘d’, decimal 十进制
‘u’, 无符号十进制
‘o’, octal八进制
‘t’,2进制
‘a’, address
‘c’, char
‘f’, float
‘s’,
‘i’ (for machine instructions)
‘m’ (for displaying memory tags)
u unit
b Bytes.
h Halfwords (two bytes).
w Words (four bytes). This is the initial default.
g Giant words (eight bytes).
addr https://sourceware.org/gdb/current/onlinedocs/gdb/Expressions.html
10.20 Core File
关于core文件的系统限制:ulimit -a 命令,有一个core file size项,限制了转储文件的大小。
取消core文件限制:
ulimit -c unlimited
其次设置core生成路径:
sudo bash -c 'echo %e.core.%p > /proc/sys/kernel/core_pattern'
比如program(PID 1111)崩溃了,生成core.1111崩溃转储,就可以分析了:
gdb program ./program.core.1111
10.23 Search Memory
find指令搜索内存顶呱呱
find [/sn] start_addr, +len, val1 [, val2, …]
find [/sn] start_addr, end_addr, val1 [, val2, …]
s, search query size
The size of each search query value.
b bytes
h halfwords (two bytes)
w words (four bytes)
g giant words (eight bytes)
n, maximum number of finds
示例:
char hello[] = "hello-hello";
// find str1, +sizeof(str1), "bcd"
static struct { char c; short s; int i; }
__attribute__ ((packed)) mixed
= { 'c', 0x1234, 0x87654321 };
//find &mixed, +sizeof(mixed), (char) 'c', (short) 0x1234, (int) 0x87654321
17. Altering Execution(!!!)
这一部分有修改变量值的教程
17.1 Assignment to Variables, print 或 set var
17.5, call/print func()
直接调用函数。
其它待补充
-
- Examining Source Files, 比如list指令记录在这里
-
- Debugging Remote Programs
-
- TUI界面
三. 插件
gef
$ wget -O ~/.gdbinit-gef.py -q https://gef.blah.cat/py
$ echo source ~/.gdbinit-gef.py >> ~/.gdbinit
pwndbg
peda
四. 其它
Data练习
#include <stdio.h>
#include <wchar.h>
#include <vector>
int func2()
{
int b = 2;
return 2;
}
int func1()
{
int a = 1;
func2();
return 1;
}
struct S{
int m_n;
};
class C
{
private:
/* data */
int n_private;
public:
int n_public;
C(/* args */);
};
C::C(/* args */)
{
}
int main(void)
{
// 1. string
char str1[] = {"abcd"}; // x/s str1
wchar_t str2[] = {L"abcd"};
// p sizeof(wchar_t)
// x/ws str2
// static
// p s_num
static int s_num=10;
// array
// set print elements number-of-elements-max/unlimited
// set print array-indexes on
char arrChars[10] = {"abcdefg"};
// p arrChars
// p arrChars[1]@3 // bcd
// x/4xb arrChars // 0x61 0x62 0x63 0x64
// x/4xw arrChars // 0x64636261 0x00676665 0x50000000 0x89a41523
// x/4db arrChars // 97 98 99 100
// x/4ub arrChars // 97 98 99 100
// x/4ob arrChars // 0141 0142 0143 0144
// x/4tb arrChars // 01100001 01100010 01100011 01100100
// struct
// whatis s
// ptype s
// i variables ^s$
// set print pretty on
S s;
// set print object on
// print c
// whatis c
// ptype c
C c;
// function
func1(); // bt full
// 2. stl
// sudo find / -name "*libstdcxx*"--> /usr/share/gcc-8/python/libstdcxx
// python
// import sys
// sys.path.insert(0, '/home/maude/gdb_printers/python')
// from libstdcxx.v6.printers import register_libstdcxx_printers
// register_libstdcxx_printers (None)
// end
// p vec
// http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
std::vector<int> vec(10, 1); // p vec
return 0;
}