Table of contents
  1. Debugging with VS Code
    1. Running the debugger
    2. Breakpoints
    3. Stepping

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.

Run and debug xv6

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

GDB breaks on main

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.

Set a breakpoint

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

Continue execution

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

Inspect variables and register

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.

Step over a few instructions

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.

Hover over variables with cursor

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

Stop debugging

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.


This site uses Just the Docs, a documentation theme for Jekyll.