【Command】GDB 调试尝试

ubuntu@ubuntu:~/Documents/test$ cat test.c 
#include <stdio.h>

void sayHello()
{
	printf("Hello~");
}

int main()
{
	int i = 0, a[3];
	for(i = 0; i < 3; i++)
	{
		a[i] = i;
		printf("%d\n", a[i]);
	}

	return 0;
}

GDB 命令

Here are some of the most frequently needed GDB commands:

break [file:]function
    Set a breakpoint at function (in file).

run [arglist]
    Start your program (with arglist, if specified).

bt  Backtrace: display the program stack.

print expr
    Display the value of an expression.

c   Continue running your program (after stopping, e.g. at a breakpoint).

next
    Execute next program line (after stopping); step over any function calls in the line.

edit [file:]function
    look at the program line where it is presently stopped.

list [file:]function
    type the text of the program in the vicinity of where it is presently stopped.

step
    Execute next program line (after stopping); step into any function calls in the line.

help [name]
    Show information about GDB command name, or general information about using GDB.

quit
    Exit from GDB.

安装 GDB

sudo apt-get install gdb

编译程序

ubuntu@ubuntu:~/Documents/tmp$ sudo gcc test.c -g -o test # -g 参数加入调试信息
ubuntu@ubuntu:~/Documents/tmp$ ls
forkProject  forkProject.c  test  test.c
ubuntu@ubuntu:~/Documents/tmp$ ./test 
0
1
2
Hello~
-rwxr-xr-x 1 root root 12024 Aug  9 01:49 test* # 加 -g 参数
-rwxr-xr-x 1 root root  9416 Aug  9 03:30 test2* # 不添加 -g 参数

调试 core 文件

不完整的,有错误的,不能运行成功的可执行文件。

ubuntu@ubuntu:~/Documents/tmp$ cat test_err.c 
#include <stdio.h>
int main()
{
	int* temp = NULL;
	*temp = 0;
	return 0;
}

ubuntu@ubuntu:~/Documents/tmp$ sudo gcc -g test_err.c 
ubuntu@ubuntu:~/Documents/tmp$ ./a.out 
Segmentation fault (core dumped)
ubuntu@ubuntu:~/Documents/tmp$ ls
a.out  forkProject  forkProject.c  gdb.txt  test  test.c  test_err.c

并未生成 core 文件。

查看对当前 shell 所做的限制:

ubuntu@ubuntu:~/Documents/tmp$ ulimit -a
core file size          (blocks, -c) 0		# corefiles 无法生成
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 14786
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 14786
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

在这里插入图片描述
解除 corefiles 限制。

ubuntu@ubuntu:~/Documents/tmp$ ulimit -c unlimited 
ubuntu@ubuntu:~/Documents/tmp$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 14786
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 14786
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

注意用户权限的问题:

ubuntu@ubuntu:~/Documents/tmp$ ls
a.out  forkProject  forkProject.c  gdb.txt  test  test.c  test_err.c
ubuntu@ubuntu:~/Documents/tmp$ sudo ./a.out 
Segmentation fault
ubuntu@ubuntu:~/Documents/tmp$ ls
a.out  core  forkProject  forkProject.c  gdb.txt  test  test.c  test_err.c

定位到出错位置:

ubuntu@ubuntu:~/Documents/tmp$ sudo gdb ./a.out core 
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 ./a.out...
[New LWP 4139]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000aaaad37a2728 in main () at test_err.c:5
5		*temp = 0;
(gdb) 

调试正在运行的程序

ubuntu@ubuntu:~/Documents/tmp$ sudo gcc test_for.c -g -o test_for
ubuntu@ubuntu:~/Documents/tmp$ cat test_for.c 
#include <stdio.h>
int main()
{
	int i = 0;
	while(++i)
	{
		// printf("%d\n", i);
		;
	}
}
ubuntu@ubuntu:~/Documents/tmp$ ./test_for &
[1] 2137
ubuntu@ubuntu:~/Documents/tmp$ sudo gdb -p 2137
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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".
Attaching to process 2145
Reading symbols from /home/ubuntu/Documents/tmp/test_for...
Reading symbols from /lib/aarch64-linux-gnu/libc.so.6...
Reading symbols from /usr/lib/debug//lib/aarch64-linux-gnu/libc-2.31.so...
Reading symbols from /lib/ld-linux-aarch64.so.1...
(No debugging symbols found in /lib/ld-linux-aarch64.so.1)
main () at test_for.c:5
5		while(++i)

gdb -p pid

调试程序

  • r: 运行程序
ubuntu@ubuntu:~/Documents/tmp$ sudo gdb test 
(gdb) r # r [run] 运行程序
Starting program: /home/ubuntu/Documents/tmp/test 
0
1
2
Hello~
[Inferior 1 (process 3546) exited with code 0320]
(gdb) 
  • q:退出
  • list:查看源代码
  • b [function_name/ line/ ]
  • info [b/ ] :查看断电情况
  • n:继续运行下一行
(gdb) list 1,20
1	#include <stdio.h>
2	
3	void sayHello()
4	{
5		printf("Hello~\n");
6	}
7	
8	void main()
9	{
10		int i = 0, a[3];
11		for(i = 0; i < 3; i++)
12		{
13			a[i] = i;
14			printf("%d\n", a[i]);
15		}
16	
17		sayHello();
18	
19	}
(gdb) b main
Breakpoint 1 at 0x8a4: file test.c, line 9.
(gdb) b 12
Breakpoint 2 at 0x8c4: file test.c, line 13.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000000008a4 in main at test.c:9
2       breakpoint     keep y   0x00000000000008c4 in main at test.c:13
(gdb) r
Starting program: /home/ubuntu/Documents/tmp/test 

Breakpoint 1, main () at test.c:9
9	{
(gdb) n
10		int i = 0, a[3];
(gdb) n
11		for(i = 0; i < 3; i++)
(gdb) n

Breakpoint 2, main () at test.c:13
13			a[i] = i;
(gdb) n
14			printf("%d\n", a[i]);
  • p xxx:查看变量值
(gdb) break 13
Breakpoint 1 at 0x8c4: file test.c, line 13.
(gdb) r
Starting program: /home/ubuntu/Documents/tmp/test 

Breakpoint 1, main () at test.c:13
13			a[i] = i;
(gdb) p a[i]
$1 = -135802796
(gdb) n
14			printf("%d\n", a[i]);
(gdb) p a[i]
$2 = 0
(gdb) p &a[i]
$3 = (int *) 0xfffffffff4f8
  • s:进入某个函数
(gdb) b 17
Breakpoint 1 at 0x910: file test.c, line 17.
(gdb) r
Starting program: /home/ubuntu/Documents/tmp/test 
0
1
2

Breakpoint 1, main () at test.c:17
17		sayHello();
(gdb) s
sayHello () at test.c:5
5		printf("Hello~\n");
(gdb) n
Hello~
6	}
(gdb) n
main () at test.c:19
19	}
  • c:程序继续运行,直到下一个断点
(gdb) b 11
Breakpoint 1 at 0x8bc: file test.c, line 11.
(gdb) b 17
Breakpoint 2 at 0x910: file test.c, line 17.
(gdb) r
Starting program: /home/ubuntu/Documents/tmp/test 

Breakpoint 1, main () at test.c:11
11		for(i = 0; i < 3; i++)
(gdb) c
Continuing.
0
1
2

Breakpoint 2, main () at test.c:17
17		sayHello();
  • shell cmd
(gdb) shell ls
forkProject  forkProject.c  test  test.c
  • set logging on
(gdb) set logging on
Copying output to gdb.txt.
Copying debug output to gdb.txt.
(gdb) shell ls
forkProject  forkProject.c  gdb.txt  test  test.c
ubuntu@ubuntu:~/Documents/tmp$ cat gdb.txt 
12		{
13			a[i] = i;
14			printf("%d\n", a[i]);
15		}
16	
17		sayHello();
18	
19	}
Note: breakpoint 2 also set at pc 0xaaaaaaaaa910.
Breakpoint 3 at 0xaaaaaaaaa910: file test.c, line 17.
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000aaaaaaaaa8bc in main at test.c:11
	breakpoint already hit 1 time
2       breakpoint     keep y   0x0000aaaaaaaaa910 in main at test.c:17
	breakpoint already hit 1 time
3       breakpoint     keep y   0x0000aaaaaaaaa910 in main at test.c:17
Continuing.
[Inferior 1 (process 3640) exited with code 0320]
The program is not being run.
  • watchpoint
(gdb) b 13
Breakpoint 1 at 0x8c4: file test.c, line 13.
(gdb) r
Starting program: /home/ubuntu/Documents/tmp/test 

Breakpoint 1, main () at test.c:13
13			a[i] = i;
(gdb) p &i
$1 = (int *) 0xfffffffff4f4
(gdb) watch *0xfffffffff4f4
Hardware watchpoint 2: *0xfffffffff4f4
(gdb) info watchpoints 
Num     Type           Disp Enb Address            What
2       hw watchpoint  keep y                      *0xfffffffff4f4
(gdb) n
14			printf("%d\n", a[i]);
(gdb) n
0
11		for(i = 0; i < 3; i++)
(gdb) n

Hardware watchpoint 2: *0xfffffffff4f4

Old value = 0
New value = 1
0x0000aaaaaaaaa904 in main () at test.c:11
11		for(i = 0; i < 3; i++)

在x86架构Linux上使用qemu+gdb调试aarch64的内核

apt install gdb-multiarch

gdb-multiarch --tui vmlinux #-tui表示gdb工具以ui的方式展示。
  1. 启动虚拟机
qemu-system-aarch64 -m 1024 -cpu max,sve=on,sve256=on -M virt,gic-version=3,its=on,iommu=smmuv3\
			-nographic $SMP -kernel arch/arm64/boot/Image \
			-append "noinintrd sched_debug root=/dev/vda rootfstype=ext4 rw crashkernel=256M loglevel=8" \
			-drive if=none,file=$rootfs_image,id=hd0 \
			-device virtio-blk-device,drive=hd0 \
			--fsdev local,id=kmod_dev,path=./kmodules,security_model=none \
			-device virtio-9p-pci,fsdev=kmod_dev,mount_tag=kmod_mount\
			-S -s #冻结cpu 使用端口接收GDB
  1. 在另一个端口中启动 GDB
gdb-multiarch vmlinux
(gdb) set architecture aarch64
(gdb) target remote localhost:xxxx
(gdb) b start_kernel
(gdb) c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jia ming

感谢欣赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值