vscode

此文章主要介绍各种vscode插件

clangd

vscode C/C++ 插件默认可以浏览C源码,但是浏览一些大型开源软件会很卡(如linux kernel), 使用 clangd 插件能够顺畅浏览大型开源软件(如linux kernel)

首先,安装 clangd 软件,默认会自动下载。如果没有自动下载,执行如下命令手动下载:

$ sudo apt install clangd

然后,生成 compile_commands.json

A. 在linux4.19 or laster中,linux kernel自带可以生成compile_commands.jsonpython脚本

## based linux5.4
$ cd linux
$ make ARCH=x86 x86_64_defconfig
$ make ARCH=x86                     ## 编译生成bzImage以及autoconf.h
$ ./scripts/gen_compile_commands.py ## 生成compile_commands.json

B. 旧版本的linux kernel没有自带可以生成compile_commands.jsonpython脚本, 比如linux2.6.34,我们可以使用compiledb命令进行生成

更多关于compiledb详细解释,请看官方

## based linux2.6.34
$ cd linux
$ make ARCH=x86 x86_64_defconfig
$ compiledb -n --command-style make ARCH=x86 ## 生成compile_commands.json
$ make ARCH=x86                              ## 编译生成bzImage以及autoconf.h

最后,打开vscode,直接对函数调用和结构体进行跳转以及自动补全

在浏览 linux kernel 源码时,x86_64 架构的 compile_commands.json 能够直接被 clangd 解释, 但是 arm64 架构的 compile_commands.json 无法被 clangd 解释,因为 clangd 无法解释 -mabi=lp64 标志, 所以我们需要为 arm64 架构添加一些额外的配置,如下:

## Linux directory
$ cat .clangd
CompileFlags:
	Remove: -mabi=lp64

Native Debug

通过命令行gdb进行调试,比较不方便,Native Debug插件可以让vscode通过图形化进行调试

首先,在.vscode/launch.json进行配置,如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "gdb",
            "request": "attach",
            "name": "gdb linux",
            "executable": "vmlinux",
            "target": "localhost:1234",
            "remote": true,
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText"
        }
    ]
}

然后,在start_kernel()设置断点,接着进入调试模式,最后通过F5(continue)、F10(next)、F11(step)进行源码级调试。同时在DEBUG CONSOLE窗口中,可以执行gdb命令,如b, c, n, lx-version, lx-dmesg等等, 如图所示:

vscode_gdb

Last updated

Was this helpful?