lldb-log.cpp revision 269024
1//===-- lldb-log.cpp --------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "lldb/lldb-private-log.h" 11 12// C Includes 13// C++ Includes 14// Other libraries and framework includes 15// Project includes 16#include "lldb/Interpreter/Args.h" 17#include "lldb/Core/Log.h" 18#include "lldb/Core/StreamFile.h" 19#include <string.h> 20 21using namespace lldb; 22using namespace lldb_private; 23 24 25// We want to avoid global constructors where code needs to be run so here we 26// control access to our static g_log_sp by hiding it in a singleton function 27// that will construct the static g_lob_sp the first time this function is 28// called. 29 30static bool g_log_enabled = false; 31static Log * g_log = NULL; 32static Log * 33GetLog () 34{ 35 if (!g_log_enabled) 36 return NULL; 37 return g_log; 38} 39 40uint32_t 41lldb_private::GetLogMask () 42{ 43 Log *log(GetLog ()); 44 if (log) 45 return log->GetMask().Get(); 46 return 0; 47} 48 49bool 50lldb_private::IsLogVerbose () 51{ 52 uint32_t mask = GetLogMask(); 53 return (mask & LIBLLDB_LOG_VERBOSE); 54} 55 56Log * 57lldb_private::GetLogIfAllCategoriesSet (uint32_t mask) 58{ 59 Log *log(GetLog ()); 60 if (log && mask) 61 { 62 uint32_t log_mask = log->GetMask().Get(); 63 if ((log_mask & mask) != mask) 64 return NULL; 65 } 66 return log; 67} 68 69void 70lldb_private::LogIfAllCategoriesSet (uint32_t mask, const char *format, ...) 71{ 72 Log *log(GetLogIfAllCategoriesSet (mask)); 73 if (log) 74 { 75 va_list args; 76 va_start (args, format); 77 log->VAPrintf (format, args); 78 va_end (args); 79 } 80} 81 82void 83lldb_private::LogIfAnyCategoriesSet (uint32_t mask, const char *format, ...) 84{ 85 Log *log(GetLogIfAnyCategoriesSet (mask)); 86 if (log) 87 { 88 va_list args; 89 va_start (args, format); 90 log->VAPrintf (format, args); 91 va_end (args); 92 } 93} 94 95Log * 96lldb_private::GetLogIfAnyCategoriesSet (uint32_t mask) 97{ 98 Log *log(GetLog ()); 99 if (log && mask && (mask & log->GetMask().Get())) 100 return log; 101 return NULL; 102} 103 104void 105lldb_private::DisableLog (const char **categories, Stream *feedback_strm) 106{ 107 Log *log(GetLog ()); 108 109 if (log) 110 { 111 uint32_t flag_bits = 0; 112 if (categories[0] != NULL) 113 { 114 flag_bits = log->GetMask().Get(); 115 for (size_t i = 0; categories[i] != NULL; ++i) 116 { 117 const char *arg = categories[i]; 118 119 if (0 == ::strcasecmp(arg, "all")) flag_bits &= ~LIBLLDB_LOG_ALL; 120 else if (0 == ::strcasecmp(arg, "api")) flag_bits &= ~LIBLLDB_LOG_API; 121 else if (0 == ::strncasecmp(arg, "break", 5)) flag_bits &= ~LIBLLDB_LOG_BREAKPOINTS; 122 else if (0 == ::strcasecmp(arg, "commands")) flag_bits &= ~LIBLLDB_LOG_COMMANDS; 123 else if (0 == ::strcasecmp(arg, "default")) flag_bits &= ~LIBLLDB_LOG_DEFAULT; 124 else if (0 == ::strcasecmp(arg, "dyld")) flag_bits &= ~LIBLLDB_LOG_DYNAMIC_LOADER; 125 else if (0 == ::strncasecmp(arg, "event", 5)) flag_bits &= ~LIBLLDB_LOG_EVENTS; 126 else if (0 == ::strncasecmp(arg, "expr", 4)) flag_bits &= ~LIBLLDB_LOG_EXPRESSIONS; 127 else if (0 == ::strncasecmp(arg, "object", 6)) flag_bits &= ~LIBLLDB_LOG_OBJECT; 128 else if (0 == ::strcasecmp(arg, "process")) flag_bits &= ~LIBLLDB_LOG_PROCESS; 129 else if (0 == ::strcasecmp(arg, "platform")) flag_bits &= ~LIBLLDB_LOG_PLATFORM; 130 else if (0 == ::strcasecmp(arg, "script")) flag_bits &= ~LIBLLDB_LOG_SCRIPT; 131 else if (0 == ::strcasecmp(arg, "state")) flag_bits &= ~LIBLLDB_LOG_STATE; 132 else if (0 == ::strcasecmp(arg, "step")) flag_bits &= ~LIBLLDB_LOG_STEP; 133 else if (0 == ::strcasecmp(arg, "thread")) flag_bits &= ~LIBLLDB_LOG_THREAD; 134 else if (0 == ::strcasecmp(arg, "target")) flag_bits &= ~LIBLLDB_LOG_TARGET; 135 else if (0 == ::strcasecmp(arg, "verbose")) flag_bits &= ~LIBLLDB_LOG_VERBOSE; 136 else if (0 == ::strncasecmp(arg, "watch", 5)) flag_bits &= ~LIBLLDB_LOG_WATCHPOINTS; 137 else if (0 == ::strncasecmp(arg, "temp", 4)) flag_bits &= ~LIBLLDB_LOG_TEMPORARY; 138 else if (0 == ::strncasecmp(arg, "comm", 4)) flag_bits &= ~LIBLLDB_LOG_COMMUNICATION; 139 else if (0 == ::strncasecmp(arg, "conn", 4)) flag_bits &= ~LIBLLDB_LOG_CONNECTION; 140 else if (0 == ::strncasecmp(arg, "host", 4)) flag_bits &= ~LIBLLDB_LOG_HOST; 141 else if (0 == ::strncasecmp(arg, "unwind", 6)) flag_bits &= ~LIBLLDB_LOG_UNWIND; 142 else if (0 == ::strncasecmp(arg, "types", 5)) flag_bits &= ~LIBLLDB_LOG_TYPES; 143 else if (0 == ::strncasecmp(arg, "symbol", 6)) flag_bits &= ~LIBLLDB_LOG_SYMBOLS; 144 else if (0 == ::strcasecmp(arg, "system-runtime")) flag_bits &= ~LIBLLDB_LOG_SYSTEM_RUNTIME; 145 else if (0 == ::strncasecmp(arg, "module", 6)) flag_bits &= ~LIBLLDB_LOG_MODULES; 146 else if (0 == ::strncasecmp(arg, "mmap", 4)) flag_bits &= ~LIBLLDB_LOG_MMAP; 147 else if (0 == ::strcasecmp(arg, "os")) flag_bits &= ~LIBLLDB_LOG_OS; 148 else 149 { 150 feedback_strm->Printf ("error: unrecognized log category '%s'\n", arg); 151 ListLogCategories (feedback_strm); 152 return; 153 } 154 155 } 156 } 157 log->GetMask().Reset (flag_bits); 158 if (flag_bits == 0) 159 { 160 log->SetStream(lldb::StreamSP()); 161 g_log_enabled = false; 162 } 163 } 164 165 return; 166} 167 168Log * 169lldb_private::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm) 170{ 171 // Try see if there already is a log - that way we can reuse its settings. 172 // We could reuse the log in toto, but we don't know that the stream is the same. 173 uint32_t flag_bits; 174 if (g_log) 175 flag_bits = g_log->GetMask().Get(); 176 else 177 flag_bits = 0; 178 179 // Now make a new log with this stream if one was provided 180 if (log_stream_sp) 181 { 182 if (g_log) 183 g_log->SetStream(log_stream_sp); 184 else 185 g_log = new Log(log_stream_sp); 186 } 187 188 if (g_log) 189 { 190 for (size_t i=0; categories[i] != NULL; ++i) 191 { 192 const char *arg = categories[i]; 193 194 if (0 == ::strcasecmp(arg, "all")) flag_bits |= LIBLLDB_LOG_ALL; 195 else if (0 == ::strcasecmp(arg, "api")) flag_bits |= LIBLLDB_LOG_API; 196 else if (0 == ::strncasecmp(arg, "break", 5)) flag_bits |= LIBLLDB_LOG_BREAKPOINTS; 197 else if (0 == ::strcasecmp(arg, "commands")) flag_bits |= LIBLLDB_LOG_COMMANDS; 198 else if (0 == ::strncasecmp(arg, "commu", 5)) flag_bits |= LIBLLDB_LOG_COMMUNICATION; 199 else if (0 == ::strncasecmp(arg, "conn", 4)) flag_bits |= LIBLLDB_LOG_CONNECTION; 200 else if (0 == ::strcasecmp(arg, "default")) flag_bits |= LIBLLDB_LOG_DEFAULT; 201 else if (0 == ::strcasecmp(arg, "dyld")) flag_bits |= LIBLLDB_LOG_DYNAMIC_LOADER; 202 else if (0 == ::strncasecmp(arg, "event", 5)) flag_bits |= LIBLLDB_LOG_EVENTS; 203 else if (0 == ::strncasecmp(arg, "expr", 4)) flag_bits |= LIBLLDB_LOG_EXPRESSIONS; 204 else if (0 == ::strncasecmp(arg, "host", 4)) flag_bits |= LIBLLDB_LOG_HOST; 205 else if (0 == ::strncasecmp(arg, "mmap", 4)) flag_bits |= LIBLLDB_LOG_MMAP; 206 else if (0 == ::strncasecmp(arg, "module", 6)) flag_bits |= LIBLLDB_LOG_MODULES; 207 else if (0 == ::strncasecmp(arg, "object", 6)) flag_bits |= LIBLLDB_LOG_OBJECT; 208 else if (0 == ::strcasecmp(arg, "os")) flag_bits |= LIBLLDB_LOG_OS; 209 else if (0 == ::strcasecmp(arg, "platform")) flag_bits |= LIBLLDB_LOG_PLATFORM; 210 else if (0 == ::strcasecmp(arg, "process")) flag_bits |= LIBLLDB_LOG_PROCESS; 211 else if (0 == ::strcasecmp(arg, "script")) flag_bits |= LIBLLDB_LOG_SCRIPT; 212 else if (0 == ::strcasecmp(arg, "state")) flag_bits |= LIBLLDB_LOG_STATE; 213 else if (0 == ::strcasecmp(arg, "step")) flag_bits |= LIBLLDB_LOG_STEP; 214 else if (0 == ::strncasecmp(arg, "symbol", 6)) flag_bits |= LIBLLDB_LOG_SYMBOLS; 215 else if (0 == ::strcasecmp(arg, "system-runtime")) flag_bits |= LIBLLDB_LOG_SYSTEM_RUNTIME; 216 else if (0 == ::strcasecmp(arg, "target")) flag_bits |= LIBLLDB_LOG_TARGET; 217 else if (0 == ::strncasecmp(arg, "temp", 4)) flag_bits |= LIBLLDB_LOG_TEMPORARY; 218 else if (0 == ::strcasecmp(arg, "thread")) flag_bits |= LIBLLDB_LOG_THREAD; 219 else if (0 == ::strncasecmp(arg, "types", 5)) flag_bits |= LIBLLDB_LOG_TYPES; 220 else if (0 == ::strncasecmp(arg, "unwind", 6)) flag_bits |= LIBLLDB_LOG_UNWIND; 221 else if (0 == ::strcasecmp(arg, "verbose")) flag_bits |= LIBLLDB_LOG_VERBOSE; 222 else if (0 == ::strncasecmp(arg, "watch", 5)) flag_bits |= LIBLLDB_LOG_WATCHPOINTS; 223 else 224 { 225 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); 226 ListLogCategories (feedback_strm); 227 return g_log; 228 } 229 } 230 231 g_log->GetMask().Reset(flag_bits); 232 g_log->GetOptions().Reset(log_options); 233 } 234 g_log_enabled = true; 235 return g_log; 236} 237 238 239void 240lldb_private::ListLogCategories (Stream *strm) 241{ 242 strm->Printf("Logging categories for 'lldb':\n" 243 " all - turn on all available logging categories\n" 244 " api - enable logging of API calls and return values\n" 245 " break - log breakpoints\n" 246 " commands - log command argument parsing\n" 247 " communication - log communication activities\n" 248 " connection - log connection details\n" 249 " default - enable the default set of logging categories for liblldb\n" 250 " dyld - log shared library related activities\n" 251 " events - log broadcaster, listener and event queue activities\n" 252 " expr - log expressions\n" 253 " host - log host activities\n" 254 " mmap - log mmap related activities\n" 255 " module - log module activities such as when modules are created, detroyed, replaced, and more\n" 256 " object - log object construction/destruction for important objects\n" 257 " os - log OperatingSystem plugin related activities\n" 258 " platform - log platform events and activities\n" 259 " process - log process events and activities\n" 260 " script - log events about the script interpreter\n" 261 " state - log private and public process state changes\n" 262 " step - log step related activities\n" 263 " symbol - log symbol related issues and warnings\n" 264 " system-runtime - log system runtime events\n" 265 " target - log target events and activities\n" 266 " thread - log thread events and activities\n" 267 " types - log type system related activities\n" 268 " unwind - log stack unwind activities\n" 269 " verbose - enable verbose logging\n" 270 " watch - log watchpoint related activities\n"); 271} 272