Flipper Zero: writing and debugging in CLion November 1, 2022 on Savely Krasovsky's blog

A while ago I got my own Flipper Zero and immediately tried to write own simple application. Unfortunately they officially support only one editor — VS Code.

I like JetBrains products and use them every day: from GoLand and PhpStorm to Rider and PyCharm. So why would I don’t use CLion to write and debug Flipper Zero’s apps?

CLion is a cross-platform IDE for C and C++ from JetBrains. And while it does support some features for embedded development, there are still some caveats, especially with debugging.

Setup core functionality

CLion syntax highlighting and autocomplete engine works out of the box only with CMake, but it supports compilation database, which fbt can generate. So to start:

  1. Clone Flipper Zero firmware repo:

    git clone --recursive https://github.com/flipperdevices/flipperzero-firmware.git
  2. Build firmware, FAPs and create compile database:

    ./fbt faps
    ./fbt firmware_cdb
  3. Copy compile_commands.json from build/latest to the project root.

  4. Open this file with CLion.

  5. Go and find compile_commands.json file at build/latest again, then click right mouse button and choose Load Compilation Database Project.

  6. Remove compile_commands.json from the project root.

  7. Go to File -> Settings -> Build, Execution, Deployment -> Toolchains and create toolkit:

    • C Compiler: toolchain/x86_64-linux/bin/arm-none-eabi-gcc
    • С++ Compiler: toolchain/x86_64-linux/bin/arm-none-eabi-g++
    • Debugger: toolchain/x86_64-linux/bin/arm-none-eabi-gdb-py
  8. It should try to sync a project at Build tab successfully.

Possible problems and their solutions

After this troubleshooting CLion should work as expected: highlighting and autocompleting your code.

Setup debugging

  1. Go to Run Configuration and create your own with type Remote Debug:

    • Pick debugger from toolchain you created before.
    • Fill 'target remote' args with content from output of ./fbt get_blackmagic
    • Point to symbol file: build/latest/firmware.elf
  2. Create a .gdbinit file with this content in the project directory:

    set confirm off
    set trace-commands on
    define target remote
    target extended-remote $arg0
    set mem inaccessible-by-default off
    source debug/flipperapps.py
  3. Then create a .gdbinit file in your home directory:

    set auto-load safe-path /path/to/your/project
  4. Before first debug session you have to attach Flipper Zero:

    toolchain/x86_64-linux/bin/arm-none-eabi-gdb-py -q -ex "target remote `./fbt get_blackmagic`" -ex "monitor swdp_scan" -ex "attach 1" -ex "quit" build/latest/firmware.elf
  5. Now you can set breakpoints and try to debug using the configuration you create. Ensure that your debugger connects successfully, otherwise try to reboot Flipper and start again from step 4.

  6. (Optionally) After successful session of debug you can edit your debug configuration and add in “Before launch” block “External tool” to automate step 4.

UPD: 10 november 2022

Guide has been updated after patches from Flipper Devices team! Now it should work with fewer tricks.