快速搭建 linux 源码调试环境
东东 Lv4

本文主要介绍如何快速搭建 linux 源码调试环境

Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
ARCH := arm64 
CROSS_COMPILE := aarch64-linux-gnu-

KDIR = linux-5.18.2
KSRC_URL = https://mirrors.tuna.tsinghua.edu.cn/kernel/v5.x/$(KDIR).tar.xz

KIMG = $(KDIR)/arch/arm64/boot/Image
DTB = mydtb/my.dto
DTS = $(DTB:.dto=.dts)

all: $(KDIR) $(KDIR)/.config
make -C $(KDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) Image -j8

$(KIMG):
make all

$(KDIR):
@echo "Download $(KDIR)"
@wget -c $(KSRC_URL) && tar -xf $(KDIR).tar.xz

$(KDIR)/.config:
make defconfig

# 默认配置并开启调试功能
defconfig:
make -C $(KDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) defconfig
$(KDIR)/scripts/config --file $(KDIR)/.config \
--set-val CONFIG_DEBUG_INFO y \
--enable CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT \
--disable CONFIG_DEBUG_INFO_NONE \
--disable CONFIG_DEBUG_INFO_SPLIT \
--disable CONFIG_DEBUG_INFO_BTF \
--disable CONFIG_DEBUG_INFO_REDUCED \
--disable CONFIG_DEBUG_INFO_COMPRESSED \
--disable CONFIG_GDB_SCRIPTS \
--disable CONFIG_DEBUG_EFI
make -C $(KDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) oldconfig

menuconfig:
make -C $(KDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) menuconfig

$(DTS):
mkdir -p $(dir $@)
qemu-system-aarch64 -M virt,dumpdtb=$(DTB) -nographic
dtc -I dtb -O dts $(DTB) > $@

dtb: $(DTS)
dtc -I dts -O dtb $< -o $(DTB)

$(DTB):
make dtb

qemu: $(KIMG) $(DTB)
qemu-system-aarch64 -M virt -cpu cortex-a53 -smp 2 -m 2048 -kernel $(KIMG) -dtb $(DTB) -nographic

debug: $(KIMG) $(DTB)
qemu-system-aarch64 -M virt -cpu cortex-a53 -smp 2 -m 2048 -kernel $(KIMG) -dtb $(DTB) -nographic -S -s

PHONY += dtb all defconfig qemu debug menuconfig
.PHONY: $(PHONY)

直接执行 make debug

然后配置 vscode

.vscode/launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/linux-5.18.2/vmlinux",
"args": [],
"stopAtEntry": false, // 在进入debug 后暂停程序
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, // 调试对象启动控制台
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb-multiarch",
"miDebuggerServerAddress": "localhost:1234",
}
]
}

前提环境

对于 Ubuntu 需安装

1
2
sudo apt-get install gcc-aarch64-linux-gnu
sudo apt-get install libncurses5-dev build-essential git bison flex libssl-dev gdb-multiarch

gdb-multiarch 适用于多平台的调试, 真不错

参考文献

1

源文件来自于

 评论