[chbot] Chchrobotics Digest, Vol 166, Issue 2 - Debugging code that works unoptimized

Bevin Brett bevin_brett at hotmail.com
Fri Jul 2 04:03:52 BST 2021

This falls into two classes

1) Is the code supposed to behave EXACTLY the same each time it runs, or
2) Are there external events being processed whose timing etc. might be a problem?

Start by making a repeatable small example of the problem - if you can't even do this, you just have to do the following with what you have, possibly adding code to try to force a repeatable order

Study this example with such static and dynamic analysis tools as you have - there are many of them out there, from very cheap to very expensive

Add logging to the code - if this makes the problem go away, identify exactly which add made the problem go away.  I have even addded code to connect two processes, one running the opt version and one the noopt version, and had them sync and exchange intermediate results until they differed.

Run the debug and the opt version.  Compare the logs.  The logs should diverge after the bug has happened.
Add more logging just before the divergence
Repeat until you know where the problem is

Lots of possible causes, and mostly too hard to find by hand except in very small fragments of code...

Uninitialized variables, indexing over the ends of stack arrays, volatile, pointer aliasing, returning addresses of local variables as pointers, changes from source code evaluation order, changes in the accuracy of floating point operations, using freed pointers, incorrect management of multi-thread shared variables, ...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20210702/012a1183/attachment.html>

More information about the Chchrobotics mailing list