Build ardupilot for debugging with optimizations disabled

Hi everyone

  1. I’ve configured and built ardupilot for sitl successfully with this command: ./waf configure --board sitl --debug
  2. I am able to debug the code with GDB
  3. But during debug session I’ve found that some variables are optimized out.

How can I tell waf to disable optimization for debug build?

Hello,

Which part are you referring to ? debug flag should handle most case beside AP_Math libraries. For those, you will need to change manually the

#pragma GCC optimize("O2")

to

#pragma GCC optimize("O0")

In the header.

Hi @khancyr . Thanks for a quick reply.

I’m trying to debug this function:
NavEKF3_core::correctEkfOriginHeight()
from libraries/AP_NavEKF3/AP_NavEKF3_Measurements.cpp file
To learn EK3_OGN_HGT_MASK parameter behavior.

Firstly I thought that this is because I was running SITL from autotests code. So I tried different ways:

  1. Debugging from native gdb + sim_vehicle.py.
  2. Attach from VS Code extentions + sim_vehicle.py
  3. Attach from VS Code + autotest.py
  4. Use gdbserver instead of gdb and attach from other terminal with gdb
  5. Use gdbserver and attach from VS Code.

In all cases I got this weird results:

  1. Variables originHgtObsVar and ratio are <optimized out>
  2. And execution is jumping randomly inside function sometimes. When I use step over or next command from gdb.

Next thing that I did was adding log to the function:

GCS_SEND_TEXT(MAV_SEVERITY_INFO, "originHgtObsVar: %f, ratio: %f", originHgtObsVar, ratio);

And also I forced #pragma GCC optimize("O0") in libraries/AP_NavEKF3/AP_NavEKF3_core.h file.

Log works. But anyway I got <optimized out> in debugger.

Here is an example:

What am I doing wrong?