1.. _develop_debug: 2 3Debugging 4######### 5 6.. _application_debugging: 7 8Application Debugging 9********************* 10 11This section is a quick hands-on reference to start debugging your 12application with QEMU. Most content in this section is already covered in 13`QEMU`_ and `GNU_Debugger`_ reference manuals. 14 15.. _QEMU: http://wiki.qemu.org/Main_Page 16 17.. _GNU_Debugger: http://www.gnu.org/software/gdb 18 19In this quick reference, you'll find shortcuts, specific environmental 20variables, and parameters that can help you to quickly set up your debugging 21environment. 22 23The simplest way to debug an application running in QEMU is using the GNU 24Debugger and setting a local GDB server in your development system through QEMU. 25 26You will need an :abbr:`ELF (Executable and Linkable Format)` binary image for 27debugging purposes. The build system generates the image in the build 28directory. By default, the kernel binary name is :file:`zephyr.elf`. The name 29can be changed using :kconfig:option:`CONFIG_KERNEL_BIN_NAME`. 30 31GDB server 32========== 33 34We will use the standard 1234 TCP port to open a :abbr:`GDB (GNU Debugger)` 35server instance. This port number can be changed for a port that best suits the 36development environment. There are multiple ways to do this. Each way starts a 37QEMU instance with the processor halted at startup and with a GDB server 38instance listening for a connection. 39 40Running QEMU directly 41~~~~~~~~~~~~~~~~~~~~~ 42 43You can run QEMU to listen for a "gdb connection" before it starts executing any 44code to debug it. 45 46.. code-block:: bash 47 48 qemu -s -S <image> 49 50will setup Qemu to listen on port 1234 and wait for a GDB connection to it. 51 52The options used above have the following meaning: 53 54* ``-S`` Do not start CPU at startup; rather, you must type 'c' in the 55 monitor. 56* ``-s`` Shorthand for :literal:`-gdb tcp::1234`: open a GDB server on 57 TCP port 1234. 58 59 60Running QEMU via :command:`ninja` 61~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 62 63Run the following inside the build directory of an application: 64 65.. code-block:: console 66 67 ninja debugserver 68 69QEMU will write the console output to the path specified in 70:makevar:`${QEMU_PIPE}` via CMake, typically :file:`qemu-fifo` within the build 71directory. You may monitor this file during the run with :command:`tail -f 72qemu-fifo`. 73 74Running QEMU via :command:`west` 75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 76 77Run the following from your project root: 78 79.. code-block:: console 80 81 west build -t debugserver_qemu 82 83QEMU will write the console output to the terminal from which you invoked 84:command:`west`. 85 86Configuring the :command:`gdbserver` listening device 87~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 88 89The Kconfig option :kconfig:option:`CONFIG_QEMU_GDBSERVER_LISTEN_DEV` controls 90the listening device, which can be a TCP port number or a path to a character 91device. GDB releases 9.0 and newer also support Unix domain sockets. 92 93If the option is unset, then the QEMU invocation will lack a ``-s`` or a 94``-gdb`` parameter. You can then use the :envvar:`QEMU_EXTRA_FLAGS` shell 95environment variable to pass in your own listen device configuration. 96 97GDB client 98========== 99 100Connect to the server by running :command:`gdb` and giving these commands: 101 102.. code-block:: bash 103 104 $ path/to/gdb path/to/zephyr.elf 105 (gdb) target remote localhost:1234 106 (gdb) dir ZEPHYR_BASE 107 108.. note:: 109 110 Substitute the correct :ref:`ZEPHYR_BASE <important-build-vars>` for your 111 system. 112 113You can use a local GDB configuration :file:`.gdbinit` to initialize your GDB 114instance on every run. Your home directory is a typical location for 115:file:`.gdbinit`, but you can configure GDB to load from other locations, 116including the directory from which you invoked :command:`gdb`. This example 117file performs the same configuration as above: 118 119.. code-block:: none 120 121 target remote localhost:1234 122 dir ZEPHYR_BASE 123 124Alternate interfaces 125~~~~~~~~~~~~~~~~~~~~ 126 127GDB provides a curses-based interface that runs in the terminal. Pass the ``--tui`` 128option when invoking :command:`gdb` or give the ``tui enable`` command within 129:command:`gdb`. 130 131.. note:: 132 133 The GDB version on your development system might not support the ``--tui`` 134 option. Please make sure you use the GDB binary from the SDK which 135 corresponds to the toolchain that has been used to build the binary. 136 137Finally, the command below connects to the GDB server using the :abbr:`DDD 138(Data Display Debugger)`, a graphical frontend for GDB. The following command 139loads the symbol table from the ELF binary file, in this instance, 140:file:`zephyr.elf`. 141 142.. code-block:: bash 143 144 ddd --gdb --debugger "gdb zephyr.elf" 145 146Both commands execute :command:`gdb`. The command name might 147change depending on the toolchain you are using and your cross-development 148tools. 149 150:command:`ddd` may not be installed in your 151development system by default. Follow your system instructions to install 152it. For example, use :command:`sudo apt-get install ddd` on an Ubuntu system. 153 154Debugging 155========= 156 157As configured above, when you connect the GDB client, the application will be 158stopped at system startup. You may set breakpoints, step through code, etc. as 159when running the application directly within :command:`gdb`. 160 161.. note:: 162 163 :command:`gdb` will not print the system console output as the application runs, 164 unlike when you run a native application in GDB directly. If you just 165 :command:`continue` after connecting the client, the application will run, 166 but nothing will appear to happen. Check the console output as described 167 above. 168 169Debug with Eclipse 170****************** 171 172Overview 173======== 174 175CMake supports generating a project description file that can be imported into 176the Eclipse Integrated Development Environment (IDE) and used for graphical 177debugging. 178 179The `GNU MCU Eclipse plug-ins`_ provide a mechanism to debug ARM projects in 180Eclipse with pyOCD, Segger J-Link, and OpenOCD debugging tools. 181 182The following tutorial demonstrates how to debug a Zephyr application in 183Eclipse with pyOCD in Windows. It assumes you have already installed the GCC 184ARM Embedded toolchain and pyOCD. 185 186Set Up the Eclipse Development Environment 187========================================== 188 189#. Download and install `Eclipse IDE for C/C++ Developers`_. 190 191#. In Eclipse, install the `GNU MCU Eclipse plug-ins`_ by opening the menu 192 ``Window->Eclipse Marketplace...``, searching for ``GNU MCU Eclipse``, and 193 clicking ``Install`` on the matching result. 194 195#. Configure the path to the pyOCD GDB server by opening the menu 196 ``Window->Preferences``, navigating to ``MCU``, and setting the ``Global 197 pyOCD Path``. 198 199Generate and Import an Eclipse Project 200====================================== 201 202#. Set up a GNU Arm Embedded toolchain as described in 203 :ref:`toolchain_gnuarmemb`. 204 205#. Navigate to a folder outside of the Zephyr tree to build your application. 206 207 .. code-block:: console 208 209 # On Windows 210 cd %userprofile% 211 212 .. note:: 213 If the build directory is a subdirectory of the source directory, as is 214 usually done in Zephyr, CMake will warn: 215 216 "The build directory is a subdirectory of the source directory. 217 218 This is not supported well by Eclipse. It is strongly recommended to use 219 a build directory which is a sibling of the source directory." 220 221#. Configure your application with CMake and build it with ninja. Note the 222 different CMake generator specified by the ``-G"Eclipse CDT4 - Ninja"`` 223 argument. This will generate an Eclipse project description file, 224 :file:`.project`, in addition to the usual ninja build files. 225 226 .. zephyr-app-commands:: 227 :tool: all 228 :zephyr-app: samples/synchronization 229 :host-os: win 230 :board: frdm_k64f 231 :gen-args: -G"Eclipse CDT4 - Ninja" 232 :goals: build 233 :compact: 234 235#. In Eclipse, import your generated project by opening the menu 236 ``File->Import...`` and selecting the option ``Existing Projects into 237 Workspace``. Browse to your application build directory in the choice, 238 ``Select root directory:``. Check the box for your project in the list of 239 projects found and click the ``Finish`` button. 240 241Create a Debugger Configuration 242=============================== 243 244#. Open the menu ``Run->Debug Configurations...``. 245 246#. Select ``GDB PyOCD Debugging``, click the ``New`` button, and configure the 247 following options: 248 249 - In the Main tab: 250 251 - Project: ``my_zephyr_app@build`` 252 - C/C++ Application: :file:`zephyr/zephyr.elf` 253 254 - In the Debugger tab: 255 256 - pyOCD Setup 257 258 - Executable path: :file:`${pyocd_path}\\${pyocd_executable}` 259 - Uncheck "Allocate console for semihosting" 260 261 - Board Setup 262 263 - Bus speed: 8000000 Hz 264 - Uncheck "Enable semihosting" 265 266 - GDB Client Setup 267 268 - Executable path example (use your ``GNUARMEMB_TOOLCHAIN_PATH``): 269 :file:`C:\\gcc-arm-none-eabi-6_2017-q2-update\\bin\\arm-none-eabi-gdb.exe` 270 271 - In the SVD Path tab: 272 273 - File path: :file:`<workspace 274 top>\\modules\\hal\\nxp\\mcux\\devices\\MK64F12\\MK64F12.xml` 275 276 .. note:: 277 This is optional. It provides the SoC's memory-mapped register 278 addresses and bitfields to the debugger. 279 280#. Click the ``Debug`` button to start debugging. 281 282RTOS Awareness 283============== 284 285Support for Zephyr RTOS awareness is implemented in `pyOCD v0.11.0`_ and later. 286It is compatible with GDB PyOCD Debugging in Eclipse, but you must enable 287CONFIG_DEBUG_THREAD_INFO=y in your application. 288 289Debugging I2C communication 290*************************** 291 292There is a possibility to log all or some of the I2C transactions done by the application. 293This feature is enabled by the Kconfig option :kconfig:option:`CONFIG_I2C_DUMP_MESSAGES`, but it 294uses the :c:macro:`LOG_DBG` function to print the contents so the 295:kconfig:option:`CONFIG_I2C_LOG_LEVEL_DBG` option must also be enabled. 296 297The sample output of the dump looks like this:: 298 299 D: I2C msg: io_i2c_ctrl7_port0, addr=50 300 D: W len=01: 00 301 D: R Sr P len=08: 302 D: contents: 303 D: 43 42 41 00 00 00 00 00 |CBA..... 304 305The first line indicates the I2C controller and the target address of the transaction. 306In above example, the I2C controller is named ``io_i2c_ctrl7_port0`` and the target device address 307is ``0x50`` 308 309.. note:: 310 311 the address, length and contents values are in hexadecimal, but lack the ``0x`` prefix 312 313Next lines contain messages, both sent and received. The contents of write messages is 314always shown, while the content of read messages is controlled by a parameter to the 315function ``i2c_dump_msgs_rw``. This function is available for use by user, but is also 316called internally by ``i2c_transfer`` API function with read content dump enabled. 317Before the length parameter, the header of the message is printed using abbreviations: 318 319 - W - write message 320 - R - read message 321 - Sr - restart bit 322 - P - stop bit 323 324The above example shows one write message with byte ``0x00`` representing the address of register to 325read from the I2C target. After that the log shows the length of received message and following 326that, the bytes read from the target ``43 42 41 00 00 00 00 00``. 327The content dump consist of both the hex and ASCII representation. 328 329Filtering the I2C communication dump 330==================================== 331 332By default, all I2C communication is logged between all I2C controllers and I2C targets. 333It may litter the log with unrelated devices and make it difficult to effectively debug the 334communication with a device of interest. 335 336Enable the Kconfig option :kconfig:option:`CONFIG_I2C_DUMP_MESSAGES_ALLOWLIST` to create an 337allowlist of I2C targets to log. 338The allowlist of devices is configured using the devicetree, for example:: 339 340 / { 341 i2c { 342 display0: some-display@a { 343 ... 344 }; 345 sensor3: some-sensor@b { 346 ... 347 }; 348 }; 349 350 i2c-dump-allowlist { 351 compatible = "zephyr,i2c-dump-allowlist"; 352 devices = < &display0 >, < &sensor3 >; 353 }; 354 }; 355 356The filters nodes are identified by the compatible string with ``zephyr,i2c-dump-allowlist`` value. 357The devices are selected using the ``devices`` property with phandles to the devices on the I2C bus. 358 359In the above example, the communication with device ``display0`` and ``sensor3`` will be displayed 360in the log. 361 362 363 364.. _Eclipse IDE for C/C++ Developers: https://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/oxygen2 365.. _GNU MCU Eclipse plug-ins: https://gnu-mcu-eclipse.github.io/plugins/install/ 366.. _pyOCD v0.11.0: https://github.com/mbedmicro/pyOCD/releases/tag/v0.11.0 367