Debug Linux kernel with GDB
Clone a Linux tree and run make ARCH=arm defconfig
to make a generic kernel configuration suited for qemu. Now edit the kernel configuration (.config
) and add the following lines at the bottom:
Now run make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j$(nproc)
to compile the kernel. If you get asked about anything, just press enter to use the standard value.
Copy the output zImage (arch/arm/boot/zImage
) to efi/boot/bootarm.efi
on your EFI partition folder in your qemu directory.
Run sudo apt-get install gdb-multiarch
to install GDB on Ubuntu. gdb-mutliarch
is required because normal gdb
package doesn't have support for ARM.
Open up the terminal you want GDB to run in, and change directory to your Linux compilation directory. Then run gdb-multiarch vmlinux
., it will open GDB you and you can now connect to a target with target remote localhost:1234
. At this point GDB will wait for qemu to start. After that you can now debug with qemu, there are tutorials online to show you how to do this.
Go to the directory where your qemu files are located, start qemu as described in Qemu emulation, only change is that you need to add a -s
parameter, this lets qemu know that it starts a GDB server.
Debug Linux kernel within Visual Studio Code
The following steps have to be performed in your Linux source directory.
Create a file called tasks.json
in the directory .vscode
and paste the following contents into it:
You may want to change line 26 and 34, as they point to the directory where your qemu files are located.
See https://go.microsoft.com/fwlink/?LinkId=733558 for the documentation about the tasks.json
format
Create a file called launch.json
in the directory .vscode
and paste the following contents into it:
Use IntelliSense to learn about possible attributes. Hover to view descriptions of existing attributes. For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
Create a file called c_cpp_properties.json
in the directory .vscode
and paste the following contents into it:
Press F5 to start debugging. The following steps will be performed:
Compile Kernel
Copy zImage to qemu EFI partition
Launch qemu
Start GDB debugging
The following keys are important:
F9 for creating a breakpoint
F10 for going a step forward
F11 for stepping into a function
F12 to step out of a function
Run arm32 UEFI in a virtual machine.
Emulating a arm32 UEFI device is useful for developing Linux and debugging it.
In the GDB Debugging page you can find instructions on how to compile Linux for this virtual machine.
A premade ZIP with all required files can be found at the bottom.
Run the following commands to install the required packages.
You will need other stuff too, but that is probably already installed. (e.g. git)
You need the source code of edk2 and acpica.
Go to your source directory and run the following commands.
Your output OVMF firmware file for qemu is$WORKSPACE/Build/ArmVirtQemu-ARM/RELEASEGCC5/FV/QEMU
_EFI.fd
Create a directory, where you want your files to be in. Put your QEMU_EFI.fd
firmware file in this directory, compiled in the previous section. Now run the following commands to create some disk images:
Now create a directory named boot
. This will be your EFI partition. You can now easily place your EFI files in there.
To start your virtual machine run the following command, and make sure qemu-system-arm
is installed.
This will run qemu with 4 virtual CPU cores. They are Coretx-A15 cores. Used because it works.
The following ZIP includes all files setup in their proper location. In addition its EFI partition folder has a UEFI shell in it. To run it either execute the run.sh
file or enter the command described in Run qemu.
Links where the above compiling information is from: