[LLVMdev] [PATCH] Implement dbgs()

David Greene dag at cray.com
Fri Dec 18 18:36:39 PST 2009


On Friday 18 December 2009 19:56, Chris Lattner wrote:


> > +// -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.

Ok.

> > +// 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.

Do you want me to use dyn_cast?  That means I'll have to add some more
stuff to raw_ostream and circular_raw_ostream.

Or I think I can just assume (Yikes!) that if the signal handler is
invoked it will really be a circular_raw_ostream since the handler
should (!) only be set up in debug mode.

That scares me a bit, though.

> > +/// 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;

Ok.

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

All right.

                                      -Dave



More information about the llvm-dev mailing list