Enabling -debug and -debug-only flags in LLVM application

I was trying to enable the -debug and -debug-only= options in my LLVM application (not a LLVM pass, but an application, like opt), but I never managed to enable them. Everytime I use it on the CLI, the result is:

myProgram: Unknown command line argument '-debug-only=init'. Try: './myProgram --help'

I compiled both the Debug and the RelWithDebInfo builds of clang 11.0.1 (43ff75f2c3feef64f9d73328230d34dac8832a91) (with -DLLVM_ENABLE_ASSERTIONS=On, as suggested by other threads even though it should be automatically set with Debug). Both times the applications returns the same error above.

I encountered this in the CommandLine 2.0 Manual and was wondering, if you have to use a cl::opt for both -debug and -debug-only=. My understanding was that they are automatically included if you create a Debug build of your application. In the LLVM source code I could only trace it back to cl::HideUnrelatedOptions where the initDebugOptions function is called to dereference three ManagedStatic objects, but I don’t understand how to work with them.

My question: What is the step-by-step approach to include the -debug and -debug-only= options for a custom application built on top of LLVM? Do I have to manually create new cl::opt objects like shown in the CommandLine 2.0 Manual, or is this supposed to be included automatically? If the latter is true, how do I achieve this? I want to be able to use the LLVM_DEBUG and DEBUG_WITH_TYPE macros for my programs Debug build.

Are you linking against the Support library? That’s where the cl::opt for -debug and -debug-only are defined.

The program compiles without any problems. My CMakeLists.txt contains the following piece:

llvm_map_components_to_libnames(llvm_libs support core irreader)

Right, I should have figured that :slight_smile:
initDebugOptions does look like what’s needed. If you’re calling cl::ParseCommandLineOptions then it should be calling initDebugOptions for you.

The other likely problem is that you’re linking against a Support library that was not built with assertions enabled.

When I compiled clang, I made sure to enable it. The following piece of code is from my build script I use to generate different builds of clang:

#!/usr/bin/bash

# Usage: ./build.sh llvm-11 Debug /usr/local/dbg
#                      ^      ^          ^
#                   folder  type  CMAKE_INSTALL_PREFIX

mkdir -p build/$1 && cd build/$1
cmake -G Ninja \
      -DCMAKE_BUILD_TYPE=$2 \
      -DCMAKE_INSTALL_PREFIX=$3 \
      -DLLVM_ENABLE_ASSERTIONS=ON \
      -DLLVM_ENABLE_PROJECTS="clang" \
      -DLLVM_TARGETS_TO_BUILD="X86" \
      -DLLVM_PARALLEL_LINK_JOBS="1" \
      ../../llvm/

# Add the following lines for adding runtimes as well
#     -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi;libunwind" \
#     -DLLVM_RUNTIME_TARGETS="x86_64-pc-linux-gnu" \

And I call it via ./build.sh llvm-11 Debug /usr/local/dbg/

I am out of ideas. It looks like what you are doing should work. I think you will need to do some deeper debugging.
Obviously opt works; one tactic would be to start with opt.cpp and just keep removing things until it starts giving you the same error.

I still did not manage to include them in my project. However, I decided to simply implement them myself since it is not too much code and it allows me to enable debug prints in my application without having to build two versions of LLVM (Release & Debug). Thank you for your time, @pogo59 !