Linux内核编译调试安装必备的工具sudo apt install bc qemu-system clangd bear flex bison pkg-config libncurses-dev libelf-dev libssl-dev build-essential执行编译并生成vscode

Linux内核编译调试安装必备的工具sudo apt install bc qemu-system clangd bear flex bison pkg-config libncurses-dev libelf-dev libssl-dev build-essential执行编译并生成vscode的索引json数据库

cd ~/linux-6.4.13

export ARCH=x86

make x86_64_defconfig

# 配置内核编译选项

make menuconfig

# --> 修改编译选项

# --> 在弹窗界面中找到Processor type and features,回车select

# --> 按N 取消地址随机化 ;[] Randomize the address of the kernel image (KASLR)

# --> 启用内核debug,后退到设置主界面,Kernel hacking —> ,点击enter进入子目录,

# 找到Compile-time checks and compiler options 【可能稍微差异随着版本,类似Debug的配置】 —>

# --> 按Y 打开debug开关 ;

# [*] Compile the kernel with debug info

# [*] Provide GDB scripts for kernel debuggin

# 保存并退出,准备编译内核

# 使用bear建立编译数据库

bear -- make -j$(nproc)如果正常结束,可以看到 compile_commands.json 编译数据库文件。

用Vscode 远程打开源码,并配置索引配置在项目根目录创建 .vscode/settings.json

{

"clangd.arguments": [

"--compile-commands-dir=.",

"--background-index",

"--header-insertion=iwyu",

"--completion-style=detailed",

"-j=4"

],

"C_Cpp.default.compileCommands": "${workspaceFolder}/compile_commands.json"

}此时打开后,vscode应该会提示clangd的插件安装,如果没有,手动安装。

然后会有一段时间的索引,等到十几分钟即可。

可能在搜索的时候,需要排除某些特定的目录,避免被干扰,可以配置 .vscode/settings.json

{

"files.watcherExclude": {

"**/.git/objects/**": true,

"**/.git/subtree-cache/**": true,

"**/node_modules/**": true,

"**/Documentation/**": true,

"**/tools/**": true

},

"search.exclude": {

"**/Documentation": true,

"**/tools": true,

"**/arch/*/boot": true

}

}