Posts by asad akbar

#embedded #software #linux

Linux kernel & VS Code

If you are using VSCode for Linux kernel development, following setup will be useful:

  1. Clone https://github.com/aakbar5/handy-kernel_modules. It contains the
    • Makefile: to build kernel modules
    • .devcontainer: to setup vscode for remote development inside the Docker container
    • .vscode: From tasks build and run Qemu.
  2. Start vscode
  3. Open the folder having linux kernel development project
  4. Select: Remote-Containers: Open Folder in Container… from the popup menu.
  5. Select the folder selected in #2
  6. Keep an eye on the vscode status bar while it is showing Opening Remote…
  7. Once vscode is setup, your project will be ready for development in container environment.
  8. Press CTRL+P for Tasks: Run Task
  9. Select kmod – build task to build a kernel module setup in Makefile.
  10. Press CTRL+P for Tasks: Run Task
  11. Select Generate compile_commands.json from the list of tasks.
  12. Press CTRL+P for Tasks: Run Task
  13. Select Run QEMU and wait for QEMU to be ready for use
  14. Press CTRL+P for Tasks: Run Task and select kmod – load

    • This command will copy built kernel module to QEMU.
  15. Get back to QEMU shell in vscode terminal and use kernel module commands to test your new kernel module.
  16. Once you are done with development, use Dev Container: Close Remote Connection option.

kernel_in_vscode

Setting up include-what-you-need (IWYU)

Use following instructions to install IWYU (https://include-what-you-use.org/) on Ubuntu 18.04.3 LTS:

sudo apt-get update
sudo apt-get install -y build-essential cmake git zlib1g-dev libncurses5-dev llvm-6.0-dev libclang-6.0-dev libclang-6.0-dev clang-6.0
git clone https://github.com/include-what-you-use/include-what-you-use.git iwyu.git
cd iwyu.git
git checkout clang_6.0
mkdir -p build
cd build
cmake -DIWYU_LLVM_ROOT_PATH=/usr/lib/llvm-6.0 ..
make 
make install

Once installation is complete, you will find `include-what-you-need`

# include-what-you-use --version
include-what-you-use 0.10 (git:a1878c4) based on clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

To execute include-what-you-need:

Build your cpp project with cmake and pass on an extra parameter `-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE` to cmake. It will produceĀ  `compile_commands.json` file in your build folder for the project.

  • Execute `iwyu_tool.py -p </path/to/folder/having/compile_command.json>`. It may take time depending upon the code size.
  • `fix_includes` can be used to apply recommended changes.

To use it in VSCode add following to the `tasks.json`

{
    "label": "CXX Analysis -- iwyu",
    "detail": "Run IWYU tool for CPP project",
    "command": "iwyu_tool.py",
    "args": [
        "-p",
        "${workspaceRoot}/build"
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": true,
        "panel": "shared",
        "showReuseMessage": true,
        "clear": false
    },
    "problemMatcher": "$gcc"
}

Ref: https://gist.github.com/aakbar5/268a2072138345893b7f82590dcc3d26

symlinks in virtualbox

If you are using Windows in Virtualbox ontop of Linux based (i.e: Ubuntu) machine, you might have seen this problem that accessing symlinks in windows does not work. Instead of showing contents after resolving Linux symlinks, you will end up with “Protocol error”.

This error can be fixed using vBoxManage by setting a boolean property (SharedFoldersEnableSymlinksCreate) against each folder shared with VirtualBox based machine.

VBoxManage setextradata <VirtualMachineName> VBoxInternal2/SharedFoldersEnableSymlinksCreate/<SharedFolderName> 1

After executing above command, verify your change with following:

VBoxManage getextradata <VirtualMachineName> enumerate

VisualStudio code & compile_commands.json

If you are working with C/C++ in VSCode, you will find that VSCode keeps on showing

"${workspaceFolder}/build/compile_commands.json" could not be found. 'includePath' from c_cpp_properties.json will be used instead.

This message is generated by vscode-cpptools. Although it is an optional file however vscode-cpptools keeps on showing error message that it is missing.

This message can easily be fixed if you are using CMake. To do so simply add following to your CMake configuration file.

# Generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)