How do I turn off _all_ optimizations in Clang++? - debugging

I'm trying to debug some C++11 code, and LLDB is being unhelpful. The code looks roughly like this:
void f(my_type dt) {
try {
g(h(dt));
}
catch ( /* reasonable exception type here */ ) {
}
}
When I place a breakpoint on the g(h(dt)) line, LLDB insists that the value of dt is unavailable. It most certainly cannot have been elided away, as it is used in the implementation of h as input to some database queries.
I use CMake, and it compiles using the following flags:
CXX_FLAGS = -g -O0 -fPIC -std=c++11 -stdlib=libc++ -Wall
I confirmed (using make VERBOSE=true) that these flags are, indeed, being used to build the project. As far as I can determine, full debugging information should be included and all optimizations turned off. This clearly is not the case. What other flags can I add to force Clang to keep all parameters and variables available throughout the calling stack?
Unfortunately, small test cases using small files and functions do not reproduce this problem: most of the time, the variable is preserved as I expect.
I'm working on a Mac running Yosemite.
$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

That's most likely a bug in the compiler. At -O0 it should always keep variables live during their defining block. It actually probably is, but somebody lost track of where it was somewhere in the compiler pipeline.
If you can cons up some example that shows this issue which you don't mind sharing with the llvm folks, then please file a bug either with the llvm bugzilla (http://llvm.org/bugs/) or with the Apple llvm team at http://bugreport.apple.com.

Related

AVX and newer intrinsics with GCC on Mac; what assembler would one need?

I have been tweaking GCC 6.3.0 to get it to use the libc++ runtime instead of libstdc++ so that g++ can be used without worrying about C++ runtime incompatibilities:
https://github.com/RJVB/macstrop/tree/master/lang/gcc6
The tweak works, I can build and run KDE software using g++ against Qt5 and KF5 frameworks (and everything else) built with various clang versions.
What doesn't work is generating code that uses AVX and presumably most or all newer intrinsic instructions.
This is not a new issue that's never been invoked on here; it's answered here for instance: How to use AVX/pclmulqdq on Mac OS X
Evidently one can configure gcc to call the linked script instead of the actual as executable.
But can gcc not be configured to use another assembler altogether, like nasm, and would that solve this issue?

CUDA 7.0, invalid argument '-std=c++11' not allowed with 'C/ObjC'

I've recently downloaded CUDA 7 and set it up to work with my project. On Mac, CUDA 7 requires clang to be the host compiler.
Now, I'm using a number of C++11 features. I've enabled these with -std=c++11 passed to nvcc this works. However, if I pass -Xcompiler -std=c++11 to nvcc, I get the following error regardless of if I also passed -std=c++11 by itself. The error is:
"invalid argument '-std=c++11' not allowed with 'C/ObjC'"
It seems like this should work, it certainly does with GCC. Anyone have a workaround. Otherwise, I'll file a bug report with Nvidia.
nvcc -dryrun ... will show what commands nvcc will execute. I don't currently have access to CUDA 7, only 6.5, but mine issues, among other things, two commands that compiles generated C source. This code generated by cudafe and have to be compiled by C compiler, but -Xcompiler adds options for both C and C++ modes.
I guess difference with my gcc situation is that I'm getting a warning while you have an error (this is exactly how gcc and clang differs in that case). For both compilers I don't see any way to suppress it, so I guess you have to fix your .cmake files to omit -Xcompiler options. This options shouldn't be used for language standard, just some very compiler-specific things.
Of course it doesn't work. You are specifying C++ options while using a C or Objective-C compiler. The source files must be C++ or Objective-C++.

What flags or environment variables can I pass to Clang to get maximum debugging on both BSD and Linux?

I'm interested in answers, approaches, and ideas out of the box. At a high level, the main page is pretty sparse and they mainly list -g, with one level, suggesting that -O0 is also either very helpful or essential.
But I'm wondering what other clang flags can be given to give maximum debugging. Is there an equivalent to gcc's -ggdb3 which includes some of the source or annotations directly in the object output? Or could there be? Is it possible and helpful to recompile the OS and its original libraries to have debugging (and if so, if I'm using Debian, can I have it write the debugging into the main .deb package instead of putting a separate debugged .deb package which stores debugging data in /usr/lib/debug?)? Will a static build of a binary affect the ability to see a good stacktrace? And is there anything that needs to be done to ensure that addr2line works well? Is it needed to compile all libraries (even glibc) with clang to get the maximum debugging benefit? I note that there is a project to recompile Debian with clang, and otherwise am open to a distribution that does so or otherwise places emphasis on debugging.
On Linux there are also options like an LD_PRELOAD set to /lib/libSegFault.so, or a set of LD_LIBRARY_PATH reassignments to /usr/lib/debug instead of the usual /usr/lib location (including redirecting libc itself to the debugged version). Is there a central place or external sources for answers to this question of how to enhance debuggability of a binary? The bigger mystery is clang, since I see in the long gcc man page that there are various options which can increase debugging (or reduce optimisations), but on the other hand the documentation for clang only shows a smaller set. It's possible that clang will accept more options than given, including gcc flags (which may either translate to a no-op or to more debugging - hard to tell without a canonical source of information).
Also from a package build perspective, since an external package may not respect CFLAGS, I've redirected /usr/bin/strip to be a no-op command that always succeeds, but other ideas on ensuring compliance are suggested (I believe that pkgsrc does a good job of wrapping gcc and the linker in shell scripts - useful to insert mandatory flags). Also there may be various ld options that can be passed to increase debugging of the outputted target. Also, it's quite possible that BSD (including FreeBSD 10, based upon clang) may have a different linking architecture which could make it easier to request and find debugged symbols in the generated libraries and executables.
To take debugging more broadly defined, I've set LD_WARN=yes, LD_DEBUG=unused, SEGFAULT_SIGNALS="all", LD_PRELOAD=.../libSegFault.so (as mentioned above), and LD_BIND_NOW=yes. Also I believe I can prefer that gcc search for libraries in /usr/lib/debug - above the standard search paths using strategic -Bs. Also, using --whole-archive for a static build might ensure that more objects are included in the linked output. There's also ulimit -c unlimited, and on Linux a nice way to differentiate core files like:
sysctl -w kernel.core_pattern="core.%t.SIG-%s.PID-%p.ID-%g-%u.%h.%E"
For gcc I've used and seen flags like: -O0 -fno-omit-frame-pointer -fverbose-asm -ggdb3 -mno-omit-leaf-frame-pointer -mtune=generic -fvar-tracking -D_GLIBCXX_DEBUG=1 -frecord-gcc-switches -femit-class-debug-always -fmath-errno -fno-eliminate-unused-debug-symbols -fno-eliminate-unused-debug-types -fno-merge-debug-strings -mieee-fp -mtune=generic -static-libgcc -fexceptions -fvar-tracking -fbounds-check -rdynamic -UNDEBUG -DDEBUG=1 (-ffreestanding -static-libgcc -pass-exit-codes) -fno-stack-check (since I believe I've read that the latter can interfere with debugging)
Other flags are there for other reasons but the emphasis is to be on maximum debugging. With all or most of the above, it's unclear to what extent clang would support or use there, or whether there are other options.
Clang does not support the -ggdb3 flag, only -g, as you have noticed. If you try to use it, you'll get the message:
clang: warning: argument unused during compilation: '-ggdb3'
so you can run your entire command line through Clang and it will tell you which of those GCC flags it supports and which it does not, some will print warnings, others may error out, but Clang will not silently ignore them. Here are the ones that Clang rejected when I tried your long command: -static-libgcc and -pass-exit-codes.
As pointed out in another SO answer, clang -cc1 --help can be used to list supported compilation flags, where we see the following which may be of interest to you:
-disable-llvm-optzns: Don't run LLVM optimization passes
-fno-elide-constructors: Disable C++ copy constructor elision
-mdisable-fp-elim: Disable frame pointer elimination optimization

Using compile flag -ffunction-sections with debug symbols

I am compiling a C file using the gcc flag -ffunction-sections, to move every function into it's own section. The assembler is throwing the error:
job_queue.s:2395: Error: operation combines symbols in different segments
The compiler's assembly output at line 2395 is given here:
.section .debug_ranges,info
.Ldebug_ranges0:
.4byte .LBB7-.Ltext0
The symbol LBB7 is in the function (and thus the section) named ".text.add_event_handler"
The symbol Ltext0 is in the (otherwise empty) section named: ".text"
GCC --version gives:
pic30-elf-gcc.exe (GCC) 4.0.3 (dsPIC30, Microchip v3_30) (B) Build date: Jun 29 2011
If I use the compiler flag -g0 (to turn off debug info) everything compiles and runs perfectly.
My question:
Is this GCC output clearly wrong? It seems to me that GCC should have calculated the symbol LBB7's offset from the beginning of the .add_even_handler section instead of the .text section.
I suspect I am misunderstanding something because I cannot find anyone having the same difficulty on the Google.
The GCC output is definitely wrong. Perhaps it's fixed in newer GCC versions. If you can't upgrade you compiler, try compiling with -gdwarf-2 or, failing that, with -gdwarf-2 -gstrict-dwarf (for -gstrict-dwarf you'll have to upgrade the compiler too).
What this option does is to instruct GCC to generate (strict) DWARF2, which does not include non-contiguous address ranges support, introduced in DWARF3.
Of course, this may degrade the debugging information quality somewhat, YMMV.

build fail with gcc4.6 but not with gcc4.5

we here use -Werror=unused-but-set-variable gcc compiler option while building our code. Apparently the people using gcc4.5 can build it "even though" there are variables which are set but not used afterwards. But I using gcc4.6 cannot build the code. Is that particular gcc option not implemented in gcc4.5?
thanks in advance
The GCC 4.6 warnings are improved w.r.t. to those produced by GCC 4.5. But you could just use -Wunused-but-set-variable to get warnings, not errors, and more importantly correct your source code (perhaps by removing that useless variable).

Resources