[LLVMdev] How to print debug info to stdout?

Dan Liew dan at su-root.co.uk
Tue May 6 23:34:27 PDT 2014


> I am debuging my frontend and I want to print the debug information. Like
> when using
> opt -debug -loop-vectorize xxx.ll
>
> the -debug prints the detailed logs of the loop vectorize pass and that is
> what I want to do in my code.

If you're using LLVM's Command line parser then this is incredibly easy.

Simply #include <llvm/Support/Debug.h>

and then use the facilities that this header file provides. For
example you can use the following macro

DEBUG( dbgs() << "This is a message for debugging\n");

This will cause the message to printed to stderr only if you pass the
-debug command line flag and do a debug build.

An even more useful feature is the following macro

DEBUG_WITH_TYPE("mypass", dbgs() << "This is a message for mypass\n");

If you pass the command line flag -debug-only=mypass

then you will only see messages that choose "mypass" as their type.
This is useful if you are running many passes because the output of
-debug can be quite noisy if you are only interested in a particular
pass.

The header Debug.h header file does expose a few other features so I
recommend you have a read so that you are aware of what features are
available to you so you can pick what is most suitable to you.

Hope that helps.

Thanks,
-- 
Dan Liew
PhD Student - Imperial College London



More information about the llvm-dev mailing list