1551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer//===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===// 263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman// 3b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell// The LLVM Compiler Infrastructure 4b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman// 8b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell//===----------------------------------------------------------------------===// 92dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// 10283663a4ed18124980c1a5e3114550638115edd8Reid Spencer// This file implements a handy way of adding debugging information to your 112dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// code, without it being enabled all of the time, and without having to add 122dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// command line options to enable it. 132dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// 142dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// In particular, just wrap your code with the DEBUG() macro, and it will be 152dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// enabled automatically if you specify '-debug' on the command-line. 162dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify 172dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// that your debug code belongs to class "foo". Then, on the command line, you 182dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// can specify '-debug-only=foo' to enable JUST the debug information for the 192dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// foo class. 202dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// 21d3eb106dfacda206b8651d1c39f5a26fc7be09a8Daniel Dunbar// When compiling without assertions, the -debug-* options and all code in 2221edb397b27d4501bca932cf5fce036f4f3c9473Chad Rosier// DEBUG() statements disappears, so it does not affect the runtime of the code. 232dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// 242dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner//===----------------------------------------------------------------------===// 252dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner 26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#ifndef LLVM_SUPPORT_DEBUG_H 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#define LLVM_SUPPORT_DEBUG_H 282dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner 29d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 31b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greeneclass raw_ostream; 32b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene 3370197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// DEBUG_TYPE macro - Files can specify a DEBUG_TYPE as a string, which causes 3470197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// all of their DEBUG statements to be activatable with -debug-only=thatstring. 3570197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner#ifndef DEBUG_TYPE 3670197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner#define DEBUG_TYPE "" 3743ed267db3512823a9698f810be4e64bee227270Daniel Dunbar#endif 38eb95f65f1142d1aac9cac94fb4ff46b96aedcb3aJim Grosbach 39a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner#ifndef NDEBUG 4070197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// DebugFlag - This boolean is set to true if the '-debug' command line option 4170197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// is specified. This should probably not be referenced directly, instead, use 4270197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// the DEBUG macro below. 4370197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// 4470197a3a0413476801622484b9c83cf6f2d6f82cChris Lattnerextern bool DebugFlag; 45eb95f65f1142d1aac9cac94fb4ff46b96aedcb3aJim Grosbach 4670197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// isCurrentDebugType - Return true if the specified string is the debug type 4770197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// specified on the command line, or if none was specified on the command line 4870197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// with the -debug-only=X option. 4970197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// 502dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattnerbool isCurrentDebugType(const char *Type); 51c3c9239d76e2acb01dac8c72323a0b28fadebe4bDaniel Dunbar 52ad2b592cf95e5db34a48c6e2c9b1fe55405355fbChad Rosier/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X 5370197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// option were specified. Note that DebugFlag also needs to be set to true for 5470197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// debug output to be produced. 5570197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// 56ad2b592cf95e5db34a48c6e2c9b1fe55405355fbChad Rosiervoid setCurrentDebugType(const char *Type); 57eb95f65f1142d1aac9cac94fb4ff46b96aedcb3aJim Grosbach 5870197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug 5970197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// information. In the '-debug' option is specified on the commandline, and if 6070197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// this is a debug build, then the code specified as the option to the macro 6170197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// will be executed. Otherwise it will not be. Example: 6270197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// 63b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n"); 6470197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// 6570197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// This will emit the debug information if -debug is present, and -debug-only 6670197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner/// is not specified, or is specified as "bitset". 67c3c9239d76e2acb01dac8c72323a0b28fadebe4bDaniel Dunbar#define DEBUG_WITH_TYPE(TYPE, X) \ 6866b856683fb67796aa77e52d676c4739e8e47d6dJeffrey Yasskin do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \ 6966b856683fb67796aa77e52d676c4739e8e47d6dJeffrey Yasskin } while (0) 7070197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner 7170197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner#else 7270197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner#define isCurrentDebugType(X) (false) 73ad2b592cf95e5db34a48c6e2c9b1fe55405355fbChad Rosier#define setCurrentDebugType(X) 7470197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner#define DEBUG_WITH_TYPE(TYPE, X) do { } while (0) 75c3c9239d76e2acb01dac8c72323a0b28fadebe4bDaniel Dunbar#endif 76c3c9239d76e2acb01dac8c72323a0b28fadebe4bDaniel Dunbar 77b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// EnableDebugBuffering - This defaults to false. If true, the debug 78b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// stream will install signal handlers to dump any buffered debug 79b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// output. It allows clients to selectively allow the debug stream 80b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// to install signal handlers if they are certain there will be no 81b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// conflict. 82b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// 83b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greeneextern bool EnableDebugBuffering; 84b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene 85b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// dbgs() - This returns a reference to a raw_ostream for debugging 86001762bb250b6b9afe22055a2665a03113969695David Greene/// messages. If debugging is disabled it returns errs(). Use it 87b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// like: dbgs() << "foo" << "bar"; 88b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greeneraw_ostream &dbgs(); 89b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene 902dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// DEBUG macro - This macro should be used by passes to emit debug information. 912dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// In the '-debug' option is specified on the commandline, and if this is a 922dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// debug build, then the code specified as the option to the macro will be 932dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// executed. Otherwise it will not be. Example: 942dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// 95b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene// DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n"); 962dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner// 97c3c9239d76e2acb01dac8c72323a0b28fadebe4bDaniel Dunbar#define DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X) 98b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene 99d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 100d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 1012dd93edfc221fe6d1e102cca694da01ca36b5d11Chris Lattner#endif 102