嵌入式调试

    The autopilots running PX4 support debugging via GDB or LLDB.

    The command below will list the largest static allocations:

    This NSH command provides the remaining free memory:

      And the top command shows the stack usage per application:

      1. top

      Dynamic heap allocations can be traced on POSIX in SITL with .

      Install Instructions

      Ubuntu:
      1. sudo apt-get install google-perftools libgoogle-perftools-dev

      Start heap profiling

      First of all, build the firmware as follows:

      Start jmavsim: ./Tools/jmavsim_run.sh

      In another terminal, type:

      1. cd build_posix_sitl_default/tmp
      2. export HEAPPROFILE=/tmp/heapprofile.hprof
      3. export HEAP_PROFILE_TIME_INTERVAL=30
      Fedora:
      1. pprof --pdf ../src/firmware/posix/px4 /tmp/heapprofile.hprof.0001.heap > heap.pdf
      Ubuntu:
      1. env LD_PRELOAD=/usr/lib/libtcmalloc.so ../src/firmware/posix/px4 ../../posix-configs/SITL/init/lpe/iris

      It will generate a pdf with a graph of the heap allocations.
      The numbers in the graph will all be zero, because they are in MB. Just look at the percentages instead. They show the live memory (of the node and the subtree), meaning the memory that was still in use at the end.

      See the gperftools docs for more information.

      Debugging Hard Faults in NuttX

      A hard fault is a state when the operating system detects that it has no valid instructions to execute. This is typically the case when key areas in RAM have been corrupted. A typical scenario is when incorrect memory access smashed the stack and the processor sees that the address in memory is not a valid address for the microprocessors’s RAM.

      • NuttX maintains two stacks: The IRQ stack for interrupt processing and the user stack
      • The stack grows downward. So the highest address in the example below is 0x20021060, the size is 0x11f4 (4596 bytes) and consequently the lowest address is 0x2001fe6c.

      To decode the hardfault, load the exact binary into the debugger:

      1. arm-none-eabi-gdb build_px4fmu-v2_default/src/firmware/nuttx/firmware_nuttx
      1. (gdb) info line *0x0808439f
      2. Line 77 of "../src/modules/systemlib/mavlink_log.c" starts at address 0x8084398 <mavlink_vasprintf+36>
      3. and ends at 0x80843a0 <mavlink_vasprintf+44>.
      1. (gdb) info line *0x08087c4e
      2. Line 311 of "../src/modules/uORB/uORBDevices_nuttx.cpp"