I am working with a large and rather mature C++ project (10+ years of development, 150k+ SLOC, 3k+ test cases, www.sumo-sim.org). We recently discovered that the program behavior changes in an unexpected manner when putting in one seemingly innocent print statement (std::cout << "foo\n";) in a specific location. The objdump output also shows large changes in the generated code depending on the presence of that print statement.
Currently our best guess is that this is related to undefined behavior and compiler optimization (as discussed in an post by John Regehr). This assumption is supported by our observation that the effect of the print statement is subject to optimization level. Since the application runs in single thread concurrency should not be an issue.
To debug undefined behavior we have used clang with flags -fsanitize=undefined,unsigned-interger-overflow,address,integer and gotten rid of all the indicated problems. We have also fixed all problems indicated by the clang static analyzer but the problem remains (curiously with gcc, clang and msvc but with slightly different results).
Now we are at a loss of ideas on how to best continue with our debugging efforts. Due to the non-locality of the effects of the print statement we don't even know where to start with a code review.
Question 1: What tools would you recommend for doing static and runtime analysis of potential problem spots (similar to the clang tools described above)?
Question 2: What mechanisms other than the combination of undefined behavior + compiler optimization are likely candidates for the observed effect (non-functional statements changing program behavior)?