[LLVMdev] [PATCH] Implement dbgs()

Chris Lattner clattner at apple.com
Fri Dec 18 17:56:41 PST 2009


On Dec 17, 2009, at 4:02 PM, David Greene wrote:
> Here's the patch to provide dbgs().  By default it works just like  
> errs().
> When -debug-buffer-size=N (N > 0) is set, it buffers output sent to  
> it and
> dumps it at program termination via a signal handler.
>
> Please review.  Thanks!
>
>                                  -Dave
>
> Index: include/llvm/Support/Debug.h
> ===================================================================
> --- include/llvm/Support/Debug.h	(revision 91557)

Ok.

> +++ lib/Support/Debug.cpp	(working copy)
> @@ -25,6 +25,9 @@
>
> #include "llvm/Support/CommandLine.h"
> #include "llvm/Support/Debug.h"
> +#include "llvm/Support/circular_raw_ostream.h"
> +#include "llvm/System/Signals.h"
> +
> using namespace llvm;
>
> // All Debug.h functionality is a no-op in NDEBUG mode.
> @@ -37,6 +40,15 @@
> Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
>       cl::location(DebugFlag));
>
> +// -debug-buffer-size - This is a command line op0tion to set the  
> size
> +// of the debug stream circular buffer.  The value is the number of
> +// characters to save.
> +static cl::opt<unsigned>
> +DebugBufferSize("debug-buffer-size",
> +                llvm::cl::desc("Save last N characters of debug  
> output "

How about "Buffer the last N characters of debug output until program  
termination".  This should probably also be cl::Hidden.

> +// Signal handlers - dump debug output on termination.
> +static void debug_user_sig_handler(void *Cookie)
> +{
> +  llvm::circular_raw_ostream *logout =
> +    dynamic_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());

Please do not use dynamic_cast, we're trying to eliminate the last  
RTTI use in the compiler.

> +/// dbgs - Return a circular-buffered debug stream.
> +raw_ostream &llvm::dbgs() {
> +  static circular_raw_ostream strm(errs(), DebugBufferSize);
> +
> +  static bool initialized = false;
> +  if (!initialized) {
> +    initialized = true;

This isn't thread safe.  A way to fix this is to do something like:

static struct dbgstream {
   circular_raw_ostream strm;

   dbgstream() : strm(errs(), DebugBufferSize) {
     your one time init stuff here.
   }
} thestrm;

return thestrm.strm;

Please commit with the appropriate fixes when we settle on the stream  
design.

-Chris



More information about the llvm-dev mailing list