Featured

C compiler flags for build debugging

I’ve been working on compiling a particularly recalcitrant C program recently, about which more later.  A large part of the difficulty is that I’m also having to learn the how to get enough output from the C compiler (in this case GCC) to figure out exactly what is going wrong.

Here are some compiler flags that make this easier:

-v for “verbose” output, prints compiler version information, the exactly command line used by Make to invoke the compiler.  It also prints the list of search paths used for #include directives, which is extremely useful.

​​​​​​​​​​​​-​H to print the full name of every included header file, so you can figure out exactly which version of stdio.h is missing the L_cuserid definition.

Running make with -jn to run multiple builds in parallel makes this sort of output very confusing, because the output from different builds is interleaved in no particular order.  I solved this by running make -j4 to get a quick but impossible to read build, then just make to give one build with a clean log.  The second build is fast because it is incremental, only the parts of the system that failed to build the first time will be recompiled.

Millisecond integer overflows

This was useful to me, maybe it will be useful to you:

2^31 – 1 = 2147483647 = highest signed 32 bit integer

2147483647 milliseconds = 24.855 days

2^32 – 1 = 4294967296 = highest unsigned 32 bit integer

4294967296 milliseconds = 49.710 days

If you are debugging a problem that has a change in behavior around 25-26 days, or at about 50 days (say for example that your product includes an automatic backup system, which gets into a loop and attempts to backup constantly if you set an interval above 25 days), one place to look is for integer overflow of a 32 bit millisecond counter somewhere.  Something like this is probably behind these two different (!) bugs in the Boeing 787 control avionics:

https://www.seattletimes.com/business/boeing-aerospace/faa-orders-787-safety-fix-reboot-power-once-in-a-while/ (reboot every 22 days -> 2^31 milliseconds)

https://www.theguardian.com/business/2015/may/01/us-aviation-authority-boeing-787-dreamliner-bug-could-cause-loss-of-control (reboot every 248 days -> 2^31 hundredths of a second)