Table of contents
Debugging with VS Code
Running the debugger
To run and debug xv6, switch to the “Run and Debug” tab on the left, and click the green button to start debugging xv6, as shown in the screenshot below.

The debugger (GDB) will stop execution of xv6 at the start of the main function in kernel/main.c.

Breakpoints
We can set our own breakpoints to make the debugger stop on a certain location in our code.
Let’s set a breakpoint on the syscall function. Open the kernel/syscall.c file in VS Code, and set a breakpoint by clicking left of the line number of a certain line, as shown below.

Now we can continue execution of xv6 until this breakpoint is hit, by clicking the continue button.

The debugger should now hit our breakpoint, and we can inspect local variables and registers on the left.

Stepping
We set our breakpoint before variables num and p were initialized, hence the <optimized out> values. Let’s step a few lines by using the “step over” button, as shown below.

Now values have been assigned to local variables num and p, as can be seen on the left. You can also hover your cursor over variables within the code, and its value will be shown.

This concludes the quick tutorial on debugging. You can stop the debugger using the red stop button.

This should also stop xv6 automatically. If xv6 is still running, you can switch to the terminal where xv6 is running and press CTRL+A x.