VSCode integration

Debug Linux kernel within Visual Studio Code

Create configuration files

The following steps have to be performed in your Linux source directory.

Create tasks.json

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

tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build Kernel for ARM",
      "type": "shell",
      "command": "make",
      "args": [
          "ARCH=arm",
          "CROSS_COMPILE=arm-linux-gnueabihf-",
          "-j6"
      ],
      "group": {
          "kind": "build",
          "isDefault": true
      },
      "problemMatcher": [
          "$gcc"
      ]
    },
    {
      "label": "Copy zImage",
      "command": "cp",
      "args": [
        "arch/arm/boot/zImage",
        "../emulation/boot/efi/boot/bootarm.efi"
      ],
      "dependsOn":["Build Kernel for ARM"]
    },
    {
      "label": "Run qemu",
      "command": "/usr/bin/qemu-system-arm -m 1024 -cpu cortex-a15 -M virt -pflash flash0.img -pflash flash1.img -nographic -drive file=fat:rw:boot/ -smp '4' -s",
      "options": {
        "cwd": "${workspaceFolder}/../emulation"
      },
      "type": "shell",
      "isBackground": true,
      "problemMatcher": [
        {
          "pattern": [
            {
              "regexp": ".",
              "file": 1,
              "location": 2,
              "message": 3
          }
          ],
          "background": {
            "activeOnStart": true,
            "beginsPattern": ".",
            "endsPattern": ".",
          }
        }
      ],
      "dependsOn":["Copy zImage"]
    },
    {
      "label": "Terminate All Tasks",
      "command": "echo ${input:terminate}",
      "type": "shell",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "terminate",
      "type": "command",
      "command": "workbench.action.tasks.terminate",
      "args": "terminateAll"
    }
  ]
} 

Create launch.json

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

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Run",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/vmlinux",
            "miDebuggerServerAddress": "localhost:1234",
            "miDebuggerPath": "/usr/bin/gdb-multiarch",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "targetArchitecture": "arm",
            "preLaunchTask": "Run qemu",
            "postDebugTask": "Terminate All Tasks",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Create c_cpp_properties.json

Create a file called c_cpp_properties.json in the directory .vscode and paste the following contents into it:

c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}",
                "${workspaceFolder}/include",
                "${workspaceFolder}/include/uapi",
                "${workspaceFolder}/include/generated",
                "${workspaceFolder}/arch/arm/include",
                "${workspaceFolder}/arch/arm/include/uapi",
                "${workspaceFolder}/arch/arm/include/generated"
            ],
            "defines": [
                "__KERNEL__"
            ],
            "compilerPath": "/usr/bin/arm-linux-gnueabihf-gcc",
            "cStandard": "gnu17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-arm",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "${workspaceFolder}/include",
                    "${workspaceFolder}/mm",
                    "${workspaceFolder}/fs",
                    "${workspaceFolder}/kernel"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

Debug

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

Last updated