10b57cec5SDimitry Andric //===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements a handy way of adding debugging information to your 100b57cec5SDimitry Andric // code, without it being enabled all of the time, and without having to add 110b57cec5SDimitry Andric // command line options to enable it. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will 140b57cec5SDimitry Andric // be enabled automatically if you specify '-debug' on the command-line. 150b57cec5SDimitry Andric // LLVM_DEBUG() requires the DEBUG_TYPE macro to be defined. Set it to "foo" 160b57cec5SDimitry Andric // specify that your debug code belongs to class "foo". Be careful that you only 170b57cec5SDimitry Andric // do this after including Debug.h and not around any #include of headers. 180b57cec5SDimitry Andric // Headers should define and undef the macro acround the code that needs to use 190b57cec5SDimitry Andric // the LLVM_DEBUG() macro. Then, on the command line, you can specify 200b57cec5SDimitry Andric // '-debug-only=foo' to enable JUST the debug information for the foo class. 210b57cec5SDimitry Andric // 220b57cec5SDimitry Andric // When compiling without assertions, the -debug-* options and all code in 230b57cec5SDimitry Andric // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the 240b57cec5SDimitry Andric // code. 250b57cec5SDimitry Andric // 260b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #ifndef LLVM_SUPPORT_DEBUG_H 290b57cec5SDimitry Andric #define LLVM_SUPPORT_DEBUG_H 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric namespace llvm { 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric class raw_ostream; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric #ifndef NDEBUG 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric /// isCurrentDebugType - Return true if the specified string is the debug type 380b57cec5SDimitry Andric /// specified on the command line, or if none was specified on the command line 390b57cec5SDimitry Andric /// with the -debug-only=X option. 400b57cec5SDimitry Andric /// 410b57cec5SDimitry Andric bool isCurrentDebugType(const char *Type); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric /// setCurrentDebugType - Set the current debug type, as if the -debug-only=X 440b57cec5SDimitry Andric /// option were specified. Note that DebugFlag also needs to be set to true for 450b57cec5SDimitry Andric /// debug output to be produced. 460b57cec5SDimitry Andric /// 470b57cec5SDimitry Andric void setCurrentDebugType(const char *Type); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric /// setCurrentDebugTypes - Set the current debug type, as if the 500b57cec5SDimitry Andric /// -debug-only=X,Y,Z option were specified. Note that DebugFlag 510b57cec5SDimitry Andric /// also needs to be set to true for debug output to be produced. 520b57cec5SDimitry Andric /// 530b57cec5SDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric /// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug 565f757f3fSDimitry Andric /// information. If the '-debug' option is specified on the commandline, and if 570b57cec5SDimitry Andric /// this is a debug build, then the code specified as the option to the macro 580b57cec5SDimitry Andric /// will be executed. Otherwise it will not be. Example: 590b57cec5SDimitry Andric /// 600b57cec5SDimitry Andric /// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n"); 610b57cec5SDimitry Andric /// 620b57cec5SDimitry Andric /// This will emit the debug information if -debug is present, and -debug-only 630b57cec5SDimitry Andric /// is not specified, or is specified as "bitset". 640b57cec5SDimitry Andric #define DEBUG_WITH_TYPE(TYPE, X) \ 650b57cec5SDimitry Andric do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \ 660b57cec5SDimitry Andric } while (false) 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric #else 690b57cec5SDimitry Andric #define isCurrentDebugType(X) (false) 7081ad6265SDimitry Andric #define setCurrentDebugType(X) do { (void)(X); } while (false) 7181ad6265SDimitry Andric #define setCurrentDebugTypes(X, N) do { (void)(X); (void)(N); } while (false) 720b57cec5SDimitry Andric #define DEBUG_WITH_TYPE(TYPE, X) do { } while (false) 730b57cec5SDimitry Andric #endif 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric /// This boolean is set to true if the '-debug' command line option 760b57cec5SDimitry Andric /// is specified. This should probably not be referenced directly, instead, use 770b57cec5SDimitry Andric /// the DEBUG macro below. 780b57cec5SDimitry Andric /// 790b57cec5SDimitry Andric extern bool DebugFlag; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric /// EnableDebugBuffering - This defaults to false. If true, the debug 820b57cec5SDimitry Andric /// stream will install signal handlers to dump any buffered debug 830b57cec5SDimitry Andric /// output. It allows clients to selectively allow the debug stream 840b57cec5SDimitry Andric /// to install signal handlers if they are certain there will be no 850b57cec5SDimitry Andric /// conflict. 860b57cec5SDimitry Andric /// 870b57cec5SDimitry Andric extern bool EnableDebugBuffering; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric /// dbgs() - This returns a reference to a raw_ostream for debugging 900b57cec5SDimitry Andric /// messages. If debugging is disabled it returns errs(). Use it 910b57cec5SDimitry Andric /// like: dbgs() << "foo" << "bar"; 920b57cec5SDimitry Andric raw_ostream &dbgs(); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric // DEBUG macro - This macro should be used by passes to emit debug information. 955f757f3fSDimitry Andric // If the '-debug' option is specified on the commandline, and if this is a 960b57cec5SDimitry Andric // debug build, then the code specified as the option to the macro will be 970b57cec5SDimitry Andric // executed. Otherwise it will not be. Example: 980b57cec5SDimitry Andric // 990b57cec5SDimitry Andric // LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n"); 1000b57cec5SDimitry Andric // 1010b57cec5SDimitry Andric #define LLVM_DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X) 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric } // end namespace llvm 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric #endif // LLVM_SUPPORT_DEBUG_H 106