!REDIRECT “https://docs.px4.io/master/ja/debug/simulation_debugging.html

Simulation Debugging

As the simulation is running on the host machine, all the desktop development tools are available.

CLANG Address Sanitizer (Mac OS, Linux)

The Clang address sanitizer can help to find alignment (bus) errors and other memory faults like segmentation faults. The command below sets the right compile options.

  1. make clean # only required on first address sanitizer run after a normal build
  2. PX4_ASAN=1 make px4_sitl jmavsim

Valgrind

  1. brew install valgrind

or

  1. sudo apt-get install valgrind

To use valgrind during the SITL simulation:

  1. make px4_sitl_default jmavsim___valgrind

Start combinations

SITL can be launched with and without debugger attached and with either jMAVSim or Gazebo as simulation backend. This results in the start options below:

  1. make px4_sitl_default jmavsim
  2. make px4_sitl_default jmavsim___gdb
  3. make px4_sitl_default jmavsim___lldb
  4. make px4_sitl_default gazebo
  5. make px4_sitl_default gazebo___gdb
  6. make px4_sitl_default gazebo___lldb

where the last parameter is the <viewer_model_debugger> triplet (using three underscores implies the default ‘iris’ model). This will start the debugger and launch the SITL application. In order to break into the debugger shell and halt the execution, hit CTRL-C:

  1. Process 16529 stopped
  2. * thread #1: tid = 0x114e6d, 0x00007fff90f4430a libsystem_kernel.dylib`__read_nocancel + 10, name = 'px4', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  3. frame #0: 0x00007fff90f4430a libsystem_kernel.dylib`__read_nocancel + 10
  4. libsystem_kernel.dylib`__read_nocancel:
  5. -> 0x7fff90f4430a <+10>: jae 0x7fff90f44314 ; <+20>
  6. 0x7fff90f4430c <+12>: movq %rax, %rdi
  7. 0x7fff90f4430f <+15>: jmp 0x7fff90f3fc53 ; cerror_nocancel
  8. 0x7fff90f44314 <+20>: retq
  9. (lldb)

In order to not have the DriverFrameworks scheduling interfere with the debugging session SIGCONT should be masked in LLDB and GDB:

  1. (lldb) process handle SIGCONT -n false -p false -s false

Or in the case of GDB:

  1. (gdb) handle SIGCONT noprint nostop

After that the The lldb or gdb shells behave like normal sessions, please refer to the LLDB / GDB documentation.

The last parameter, the <viewer_model_debugger> triplet, is actually passed to make in the build directory, so

  1. make px4_sitl_default jmavsim___gdb

is equivalent with

  1. make px4_sitl_default # Configure with cmake
  2. make -C build/px4_sitl_default jmavsim___gdb

A full list of the available make targets in the build directory can be obtained with:

  1. make help

but for your convenience, a list with just the <viewer_model_debugger> triplets is printed with the command

  1. make list_vmd_make_targets

Compiler optimization

It is possible to suppress compiler optimization for given executables and/or modules (as added by cmake with add_executable or add_library) when configuring for posix_sitl_*. This can be handy when it is necessary to step through code with a debugger or print variables that would otherwise be optimized out.

To do so, set the environment variable PX4_NO_OPTIMIZATION to be a semi-colon separated list of regular expressions that match the targets that need to be compiled without optimization. This environment variable is ignored when the configuration isn’t posix_sitl_*.

For example,

  1. export PX4_NO_OPTIMIZATION='px4;^modules__uORB;^modules__systemlib$'

would suppress optimization of the targets: platforms_posixpx4_layer, modulessystemlib, modulesuORB, examplespx4_simple_app, modules**uORB_uORB_tests and px4.

The targets that can be matched with these regular expressions can be printed with the command:

  1. make -C build/posix_sitl_* list_cmake_targets