vscode阅读linux源码
方法一 一站式配置vscode
目前存在的问题:仍然有一些红线报错,但是不影响跳转定义
vscode 安装插件
- Remote-SSH:本机安装
- C/C++ 插件:remote安装(在remote安装插件的方法:先Remote-SSH连接上remote)
编译内核源码
[编译内核(简单版)](# 编译内核(简单版))
获取.vscode和生成compile_commands.json
然后就可以了
完成之后打开某参与编译的c文件后,需要一段时间(大概5分钟)等建立索引,然后就可以支持跳转、悬停显示了
方法二Global(找不到u32等的定义)
u32等的定义在v5.10中有22处的样子,所以其真正的定义来源要看你的编译,而这种方法没有利用编译信息
远程主机安装 global 工具
apt install global
安装完之后,确认两个二进制文件,global,gtags, 一般在 /usr/bin/ 目录下。有这两个文件,就说明 OK 。
Linux 源码下载,库支持下载和生成
- 源码放到远程的目录
- 库支持参见编译的环境依赖
- 还是有头文件需要编译才生成:
I don’t have any idea where to look for these files:
#include <generated/timeconst.h> /* /include/linux/jiffies.h */
#include <generated/bounds.h>
#include <generated/autoconf.h> /* /include/linux/kconfig.h */
#include <generated/asm-offsets.h>
解答:
You’ll find the header files used by your build in /lib/modules/$(uname -r)/build/
, see for example
find /lib/modules/$(uname -r)/build/ -name timeconst.h
All these files are generated during the build, in various ways; timeconst.h
is built by kernel/time/timeconst.bc
.
/lib/modules/$(uname -r)/build/
stores the generated headers (and a few other files) corresponding to the running kernel; the intention is to make them available for external module builds in particular. If you’re building a new kernel, you’ll find the generated files in your build tree (after a kernel build, or an in-tree-module build).
vscode 安装插件
- Remote-SSH:本机
- C/C++ 插件:remote(在remote安装插件的方法:先Remote-SSH连接上remote),如果首次安装在remote上需要配置
- C/C++ GNU Global:用于符号解析,remote
vscode 配置 global 路径
vscode 的settings(ssh)里:
在 vscode 的 settings.json 配置里,指定 global 的相关路径。
"gnuGlobal.globalExecutable": "/usr/bin/global",
"gnuGlobal.gtagsExecutable": "/usr/bin/gtags",
// 指明生成的符号表存放在哪个位置
"gnuGlobal.objDirPrefix": "/mnt/.global"
注意:”gnuGlobal.objDirPrefix” 的路径必须要手动创建好(一个这个名字的空文件即可),如果不存在,会导致后续 Rebuild 的失败。
比如:
在当前目录下创建一个.global文件夹,里面创建一个gnuGlobal.objDirPrefix文件,然后配置”gnuGlobal.objDirPrefix”: “.global”
测试是否成功执行远程主机上的 Gtags
测试一下安装配置的是否正确。shift + command + P 把命令面板掉出来,执行 Global: Show GNU Global Version 命令,看是否能成功显示版本。在右下角显示版本号,那么就说明一切就绪:
global (GNU GLOBAL) 6.6.4
生成符号表
shift + command + P 把命令面板调出来,执行 Global: Rebuild Gtags Database 命令。等待右下角的通知,如果显示:
Build tag files successfully
那么就说明符号表解析完成了。符号表生成成功会在 “gnuGlobal.objDirPrefix” 的路径里生成三个文件:
root@ubuntu20:/mnt/opensource/linux-3.10# ll -lh /mnt/.global/mnt/opensource/linux-3.10/
total 395M
drwxr-xr-x 2 root root 4.0K Feb 14 19:06 ./
drwxr-xr-x 3 root root 4.0K Feb 14 18:33 ../
-rw-r--r-- 1 root root 7.6M Feb 14 19:06 GPATH
-rw-r--r-- 1 root root 278M Feb 14 19:06 GRTAGS
-rw-r--r-- 1 root root 109M Feb 14 19:06 GTAGS
1.2.3.4.5.6.7.
方法三clangd(无法定义跳转)
vscode 安装插件
- Remote-SSH:本机
- clangd:remote和本机(在remote安装插件的方法:先Remote-SSH连接上remote),如果首次安装在remote上需要配置
安装clangd
在VSCode Extension组件页搜索clangd,在插件介绍界面点击安装即可。
需要注意的是clangd有两个安装选项,一个是安装到本地,也就是windows系统上,也可以选择安装到远程服务器上,比如页面显示可以看到有个”Install in SSH: 192.168.50.170”安装选项,这两个都需要安装。
linux远程服务器上的clangd默认是安装到~/.vscode-server/目录下。
VSCode在安装linux版本的clangd时是在github上下载安装包然后通过ssh导入到服务器上,正常途径访问不了github的同学这一步可能会超时安装失败,可以通过其他途径到clangd的 github发布页 按平台下载安装包,安装包在linux系统上解压出来,然后手动拷贝到对应系统目录即可。如果没有系统权限,参考vscode默认方式安装到自己的home目录也可以(可能需要自己导出路径到环境变量)。
需要注意的是如果VSCode之前安装过C++ Intellisense插件需要禁用或者卸载掉,因为会和clangd插件有冲突。
vscode 插件配置
配置clangd
在已安装的Extension组件页选中clangd,点击图标旁边的齿轮打开设置页,User和Remote标签页中的Clangd Arguments都按照下面设置(点击Add Item,一个item输入下面的一行)
–compile-commands-dir=${workspaceFolder}
–background-index
–completion-style=detailed
–header-insertion=never
-log=info
设置完后关掉设置页面即可,vs会自动保存。
生成compile_commands.json
在当前目录就会生成compile_commands.json:
bear make
或
触发clangd工作
内核源码根目录,也就是compile_commands.json所在的目录,这个目录就作为我们的worksapce了。
在左边文件列表里双击任何一个.c内核源文件(需要是参与编译的),这个时候你就可以看到最下面状态栏clangd开始执行indexing了,也就是解析workspace下compile_commands.json文件里描述的所有源文件,创建索引数据库(保存在workspace目录下的.cache/clangd目录下),待所有索引文件创建完成再回到代码窗口可以看到#include后面的头文件名下面都有了下划线,这个时候代码里的函数、变量、宏定义和头文件就可以通过Ctrl+鼠标左键点击来跳转查看定义了,指针悬停在这些地方也会显示出预览了。
相比source insight创建index,感觉clangd要快不少,而且创建的index数据库也比较小。后续如果文件改动了,clangd在启动的时候会自动重新同步并生成新的index,但是如果新增了文件的话就需要重新执行bear make去生成新的compile_commands.json,这样新的文件才会被索引。
编译内核(简单版)
[详细版(含安装内核)](# How to compile and install Linux Kernel 5.16.9)
在远程主机上
[安装编译环境](# Step 4. Install the required compilers and other tools)
下载源码和解压缩
wget -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.9.tar.xz
unxz -v linux-5.16.9.tar.xz
tar xvf linux-5.16.9.tar
- 配置内核features:照抄远程主机linux内核的
cd linux-5.16.9
cp -v /boot/config-$(uname -r) .config
- 编译(在linux-5.16.9目录下)
make
或者指定线程数目
make -j $(nproc) #系统核数
修改源码后编译也是make,亲测初次编译后,8核编译时间2分钟
[详细版编译安装内核]How to compile and install Linux Kernel 5.16.9
How to compile and install Linux Kernel 5.16.9 from source code - nixCraft (cyberciti.biz)
This step by step howt o covers compiling Linux kernel version 5.16.9 under an Ubuntu or Debian Linux. The following instructions successfully tested on an RHEL CentOS 7/8 (and clones), Debian Linux, Ubuntu Linux and Fedora Linux 31/32. However, instructions remain the same for any other Linux distribution.
The procedure to build (compile) and install the latest Linux kernel from source is as follows:
- Grab the latest kernel from kernel.org
- Verify kernel
- Untar the kernel tarball
- Copy existing Linux kernel config file
- Compile and build Linux kernel 5.16.9
- Install Linux kernel and modules (drivers)
- Update Grub configuration
- Reboot the system
你将需要至少 12 GB 的本地可用磁盘空间来完成内核的编译过程
Step 1. Get the latest Linux kernel source code
The filename would be linux-x.y.z.tar.xz, where x.y.z is actual Linux kernel version number. For example file linux-5.16.9.tar.xz represents Linux kernel version 5.16.9. Use the wget command to download Linux kernel source code:$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.9.tar.xz
Step 2. Extract tar.xz file
You really don’t have to extract the source code in /usr/src. You can extract the source code in your $HOME directory or any other directory using the following unzx command or xz command:$ unxz -v linux-5.16.9.tar.xz
OR$ xz -d -v linux-5.16.9.tar.xz
这个是得到.tar文件
Verify Linux kernel tartball with pgp(如果不用安装而只是编译的话不用这步)
First grab the PGP signature for linux-5.16.9.tar:$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.9.tar.sign
Try to verify it:$ gpg --verify linux-5.16.9.tar.sign
Sample outputs:
gpg: assuming signed data in 'linux-5.16.9.tar'
gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
gpg: using RSA key 79BE3E4300411886
gpg: Can't check signature: No public key
Grab the public key from the PGP keyserver in order to verify the signature i.e. RSA key ID 79BE3E4300411886 (from the above outputs):$ gpg --recv-keys 79BE3E4300411886
Sample outputs:
gpg: key 79BE3E4300411886: 7 duplicate signatures removed
gpg: key 79BE3E4300411886: 172 signatures not checked due to missing keys
gpg: /home/vivek/.gnupg/trustdb.gpg: trustdb created
gpg: key 79BE3E4300411886: public key "Linus Torvalds <torvalds@kernel.org>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1
Now verify gpg key again with the gpg command:$ gpg --verify linux-5.16.9.tar.sign
Sample outputs:
gpg: assuming signed data in 'linux-5.16.9.tar'
gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
gpg: using RSA key 79BE3E4300411886
gpg: Good signature from "Linus Torvalds <torvalds@kernel.org>" [unknown]
gpg: aka "Linus Torvalds <torvalds@linux-foundation.org>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: ABAF 11C6 5A29 70B1 30AB E3C4 79BE 3E43 0041 1886
If you do not get “BAD signature” output from the “gpg –verify” command, untar/extract the Linux kernel tarball using the tar command, enter:$ tar xvf linux-5.16.9.tar
Step 3. Configure the Linux kernel features and modules
Before start building the kernel, one must configure Linux kernel features. You must also specify which kernel modules (drivers) needed for your system. The task can be overwhelming for a new user. I suggest that you copy existing config file using the cp command:$ cd linux-5.16.9
$ cp -v /boot/config-$(uname -r) .config
Sample outputs:
'/boot/config-4.15.0-30-generic' -> '.config'
Step 4. Install the required compilers and other tools
You must have development tools such as GCC compilers and related tools installed to compile the Linux kernel.
How to install GCC and development tools on a Debian/Ubuntu Linux
Type the following apt command or apt-get command to install the same:$ sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev
See “Ubuntu Linux Install GNU GCC Compiler and Development Environment” for more info.
How to install GCC and development tools on a CentOS/RHEL/Oracle/Scientific Linux
Try yum command:$ sudo yum group install "Development Tools"
OR$ sudo yum groupinstall "Development Tools"
Additional packages too:$ sudo yum install ncurses-devel bison flex elfutils-libelf-devel openssl-devel
How to install GCC and development tools on a Fedora Linux
Run the following dnf command:$ sudo dnf group install "Development Tools"$ sudo dnf install ncurses-devel bison flex elfutils-libelf-devel openssl-devel
Step 5. Configuring the kernel[optional]
Now you can start the kernel configuration by typing any one of the following command in source code directory:
- $ make menuconfig – Text based color menus, radiolists & dialogs. This option also useful on remote server if you wanna compile kernel remotely.
- $ make xconfig – X windows (Qt) based configuration tool, works best under KDE desktop
- $ make gconfig – X windows (Gtk) based configuration tool, works best under Gnome Dekstop.
For example, run make menuconfig command launches following screen:$ make menuconfig
You have to select different options as per your need. Each configuration option has HELP button associated with it so select help button to get help. Please note that ‘make menuconfig’ is optional. I used it here to demonstration purpose only. You can enable or disable certain features or kernel driver with this option. It is easy to remove support for a device driver or option and end up with a broken kernel. For example, if the ext4 driver is removed from the kernel configuration file, a system may not boot. When in doubt, just leave support in the kernel.
Step 5. How to compile a Linux Kernel
build
Start compiling and tocreate a compressed kernel image, enter:$ make
To speed up compile time, pass the -j as follows:## use 4 core/thread ##
$ make -j 4
## get thread or cpu core count using nproc command ##
$ make -j $(nproc)
Compiling and building the Linux kernel going take a significant amount of time. The build time depends upon your system’s resources such as available CPU core and the current system load. So have some patience.
build之前会有很多选项要你选择(网络、IO、…),我一路默认(除了内核压缩模式,我下载的是.xz所以选择对应的)
Install the Linux kernel modules
$ sudo make modules_install
Install the Linux kernel
So far we have compiled the Linux kernel and installed kernel modules. It is time to install the kernel itself:$ sudo make install
It will install three files into /boot directory as well as modification to your kernel grub configuration file:
- initramfs-5.16.9.img
- System.map-5.16.9
- vmlinuz-5.16.9
Step 6. Update grub config
You need to modify Grub 2 boot loader configurations. Type the following command at a shell prompt as per your Linux distro:
CentOS/RHEL/Oracle/Scientific and Fedora Linux
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg$ sudo grubby --set-default /boot/vmlinuz-5.16.9`
You can confirm the details with the following commands:
`grubby --info=ALL | moregrubby --default-indexgrubby --default-kernel
Debian/Ubuntu Linux
The following commands are optional as make install does everything for your but included here for historical reasons only:$ sudo update-initramfs -c -k 5.16.9$ sudo update-grub
How to build and install the latest Linux kernel from source code
You have compiled a Linux kernel. The process takes some time, however now you have a custom Linux kernel for your system. Let us reboot the system.
Reboot Linux computer and boot into your new kernel
Just issue the reboot command or shutdown command:# reboot
Verify new Linux kernel version after reboot:$ uname -mrs
Sample outputs:
Linux 5.16.9 x86_64
Conclusion – Linux Compile Kernel version 5.16.9
Configurations! You completed various steps to build the Linux kernel from source code and compiled kernel should be running on your system. I strongly suggest that you always keep backup of essential data and visit the kernel.org page here for more info.