Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Log.h" |
| 18 | #include "lldb/Core/StreamFile.h" |
Eli Friedman | ce6fe88 | 2010-06-10 04:51:35 +0000 | [diff] [blame] | 19 | #include <string.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 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 | static LogSP & |
| 30 | GetLog () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 32 | static LogSP g_log_sp; |
| 33 | return g_log_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | uint32_t |
| 37 | lldb_private::GetLogMask () |
| 38 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 39 | LogSP log(GetLog ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | if (log) |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 41 | return log->GetMask().Get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | lldb_private::IsLogVerbose () |
| 47 | { |
| 48 | uint32_t mask = GetLogMask(); |
| 49 | return (mask & LIBLLDB_LOG_VERBOSE); |
| 50 | } |
| 51 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 52 | LogSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | lldb_private::GetLogIfAllCategoriesSet (uint32_t mask) |
| 54 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 55 | LogSP log(GetLog ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | if (log && mask) |
| 57 | { |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 58 | uint32_t log_mask = log->GetMask().Get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | if ((log_mask & mask) != mask) |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 60 | return LogSP(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | } |
| 62 | return log; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | lldb_private::LogIfAllCategoriesSet (uint32_t mask, const char *format, ...) |
| 67 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 68 | LogSP log(GetLogIfAllCategoriesSet (mask)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | if (log) |
| 70 | { |
| 71 | va_list args; |
| 72 | va_start (args, format); |
| 73 | log->VAPrintf (format, args); |
| 74 | va_end (args); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | lldb_private::LogIfAnyCategoriesSet (uint32_t mask, const char *format, ...) |
| 80 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 81 | LogSP log(GetLogIfAnyCategoriesSet (mask)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | if (log) |
| 83 | { |
| 84 | va_list args; |
| 85 | va_start (args, format); |
| 86 | log->VAPrintf (format, args); |
| 87 | va_end (args); |
| 88 | } |
| 89 | } |
| 90 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 91 | LogSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | lldb_private::GetLogIfAnyCategoriesSet (uint32_t mask) |
| 93 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 94 | LogSP log(GetLog ()); |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 95 | if (log && mask && (mask & log->GetMask().Get())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | return log; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 97 | return LogSP(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 101 | lldb_private::DisableLog (Args &args, Stream *feedback_strm) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 103 | LogSP log(GetLog ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 105 | if (log) |
| 106 | { |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 107 | uint32_t flag_bits = 0; |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 108 | const size_t argc = args.GetArgumentCount (); |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 109 | if (argc > 0) |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 110 | { |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 111 | flag_bits = log->GetMask().Get(); |
| 112 | for (size_t i = 0; i < argc; ++i) |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 113 | { |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 114 | const char *arg = args.GetArgumentAtIndex (i); |
| 115 | |
Greg Clayton | d5f76bb | 2011-02-04 18:55:41 +0000 | [diff] [blame] | 116 | if (0 == ::strcasecmp(arg, "all")) flag_bits &= ~LIBLLDB_LOG_ALL; |
| 117 | else if (0 == ::strcasecmp(arg, "api")) flag_bits &= ~LIBLLDB_LOG_API; |
| 118 | else if (0 == ::strncasecmp(arg, "break", 5)) flag_bits &= ~LIBLLDB_LOG_BREAKPOINTS; |
| 119 | else if (0 == ::strcasecmp(arg, "commands")) flag_bits &= ~LIBLLDB_LOG_COMMANDS; |
| 120 | else if (0 == ::strcasecmp(arg, "default")) flag_bits &= ~LIBLLDB_LOG_DEFAULT; |
| 121 | else if (0 == ::strcasecmp(arg, "dyld")) flag_bits &= ~LIBLLDB_LOG_DYNAMIC_LOADER; |
| 122 | else if (0 == ::strncasecmp(arg, "event", 5)) flag_bits &= ~LIBLLDB_LOG_EVENTS; |
| 123 | else if (0 == ::strncasecmp(arg, "expr", 4)) flag_bits &= ~LIBLLDB_LOG_EXPRESSIONS; |
| 124 | else if (0 == ::strncasecmp(arg, "object", 6)) flag_bits &= ~LIBLLDB_LOG_OBJECT; |
| 125 | else if (0 == ::strcasecmp(arg, "process")) flag_bits &= ~LIBLLDB_LOG_PROCESS; |
| 126 | else if (0 == ::strcasecmp(arg, "script")) flag_bits &= ~LIBLLDB_LOG_SCRIPT; |
| 127 | else if (0 == ::strcasecmp(arg, "state")) flag_bits &= ~LIBLLDB_LOG_STATE; |
| 128 | else if (0 == ::strcasecmp(arg, "step")) flag_bits &= ~LIBLLDB_LOG_STEP; |
| 129 | else if (0 == ::strcasecmp(arg, "thread")) flag_bits &= ~LIBLLDB_LOG_THREAD; |
| 130 | else if (0 == ::strcasecmp(arg, "verbose")) flag_bits &= ~LIBLLDB_LOG_VERBOSE; |
| 131 | else if (0 == ::strncasecmp(arg, "watch", 5)) flag_bits &= ~LIBLLDB_LOG_WATCHPOINTS; |
| 132 | else if (0 == ::strncasecmp(arg, "temp", 4)) flag_bits &= ~LIBLLDB_LOG_TEMPORARY; |
| 133 | else if (0 == ::strncasecmp(arg, "comm", 4)) flag_bits &= ~LIBLLDB_LOG_COMMUNICATION; |
| 134 | else if (0 == ::strncasecmp(arg, "conn", 4)) flag_bits &= ~LIBLLDB_LOG_CONNECTION; |
| 135 | else if (0 == ::strncasecmp(arg, "host", 4)) flag_bits &= ~LIBLLDB_LOG_HOST; |
| 136 | else if (0 == ::strncasecmp(arg, "unwind", 6)) flag_bits &= ~LIBLLDB_LOG_UNWIND; |
Enrico Granata | de4059f | 2011-07-22 17:03:19 +0000 | [diff] [blame] | 137 | else if (0 == ::strncasecmp(arg, "types", 5)) flag_bits &= ~LIBLLDB_LOG_TYPES; |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 138 | else |
| 139 | { |
| 140 | feedback_strm->Printf ("error: unrecognized log category '%s'\n", arg); |
| 141 | ListLogCategories (feedback_strm); |
| 142 | return; |
| 143 | } |
| 144 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 145 | } |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 146 | } |
| 147 | if (flag_bits == 0) |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 148 | GetLog ().reset(); |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 149 | else |
| 150 | log->GetMask().Reset (flag_bits); |
| 151 | } |
| 152 | |
| 153 | return; |
| 154 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 156 | LogSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | lldb_private::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm) |
| 158 | { |
| 159 | // Try see if there already is a log - that way we can reuse its settings. |
| 160 | // We could reuse the log in toto, but we don't know that the stream is the same. |
| 161 | uint32_t flag_bits; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 162 | LogSP log(GetLog ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | if (log) |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 164 | flag_bits = log->GetMask().Get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | else |
| 166 | flag_bits = 0; |
| 167 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 168 | // Now make a new log with this stream if one was provided |
| 169 | if (log_stream_sp) |
| 170 | { |
| 171 | log = make_shared<Log>(log_stream_sp); |
| 172 | GetLog () = log; |
| 173 | } |
| 174 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | if (log) |
| 176 | { |
| 177 | bool got_unknown_category = false; |
| 178 | const size_t argc = args.GetArgumentCount(); |
| 179 | for (size_t i=0; i<argc; ++i) |
| 180 | { |
| 181 | const char *arg = args.GetArgumentAtIndex(i); |
| 182 | |
Greg Clayton | d5f76bb | 2011-02-04 18:55:41 +0000 | [diff] [blame] | 183 | if (0 == ::strcasecmp(arg, "all")) flag_bits |= LIBLLDB_LOG_ALL; |
| 184 | else if (0 == ::strcasecmp(arg, "api")) flag_bits |= LIBLLDB_LOG_API; |
| 185 | else if (0 == ::strncasecmp(arg, "break", 5)) flag_bits |= LIBLLDB_LOG_BREAKPOINTS; |
| 186 | else if (0 == ::strcasecmp(arg, "commands")) flag_bits |= LIBLLDB_LOG_COMMANDS; |
| 187 | else if (0 == ::strcasecmp(arg, "default")) flag_bits |= LIBLLDB_LOG_DEFAULT; |
| 188 | else if (0 == ::strcasecmp(arg, "dyld")) flag_bits |= LIBLLDB_LOG_DYNAMIC_LOADER; |
| 189 | else if (0 == ::strncasecmp(arg, "event", 5)) flag_bits |= LIBLLDB_LOG_EVENTS; |
| 190 | else if (0 == ::strncasecmp(arg, "expr", 4)) flag_bits |= LIBLLDB_LOG_EXPRESSIONS; |
| 191 | else if (0 == ::strncasecmp(arg, "object", 6)) flag_bits |= LIBLLDB_LOG_OBJECT; |
| 192 | else if (0 == ::strcasecmp(arg, "process")) flag_bits |= LIBLLDB_LOG_PROCESS; |
| 193 | else if (0 == ::strcasecmp(arg, "script")) flag_bits |= LIBLLDB_LOG_SCRIPT; |
| 194 | else if (0 == ::strcasecmp(arg, "state")) flag_bits |= LIBLLDB_LOG_STATE; |
| 195 | else if (0 == ::strcasecmp(arg, "step")) flag_bits |= LIBLLDB_LOG_STEP; |
| 196 | else if (0 == ::strcasecmp(arg, "thread")) flag_bits |= LIBLLDB_LOG_THREAD; |
| 197 | else if (0 == ::strcasecmp(arg, "verbose")) flag_bits |= LIBLLDB_LOG_VERBOSE; |
| 198 | else if (0 == ::strncasecmp(arg, "watch", 5)) flag_bits |= LIBLLDB_LOG_WATCHPOINTS; |
| 199 | else if (0 == ::strncasecmp(arg, "temp", 4)) flag_bits |= LIBLLDB_LOG_TEMPORARY; |
| 200 | else if (0 == ::strncasecmp(arg, "comm", 4)) flag_bits |= LIBLLDB_LOG_COMMUNICATION; |
| 201 | else if (0 == ::strncasecmp(arg, "conn", 4)) flag_bits |= LIBLLDB_LOG_CONNECTION; |
| 202 | else if (0 == ::strncasecmp(arg, "host", 4)) flag_bits |= LIBLLDB_LOG_HOST; |
| 203 | else if (0 == ::strncasecmp(arg, "unwind", 6)) flag_bits |= LIBLLDB_LOG_UNWIND; |
Enrico Granata | de4059f | 2011-07-22 17:03:19 +0000 | [diff] [blame] | 204 | else if (0 == ::strncasecmp(arg, "types", 5)) flag_bits |= LIBLLDB_LOG_TYPES; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | else |
| 206 | { |
| 207 | feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); |
| 208 | if (got_unknown_category == false) |
| 209 | { |
| 210 | got_unknown_category = true; |
| 211 | ListLogCategories (feedback_strm); |
| 212 | return log; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 217 | log->GetMask().Reset(flag_bits); |
| 218 | log->GetOptions().Reset(log_options); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | } |
| 220 | return log; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void |
| 225 | lldb_private::ListLogCategories (Stream *strm) |
| 226 | { |
| 227 | strm->Printf("Logging categories for 'lldb':\n" |
| 228 | "\tall - turn on all available logging categories\n" |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 229 | "\tapi - enable logging of API calls and return values\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | "\tdefault - enable the default set of logging categories for liblldb\n" |
| 231 | "\tbreak - log breakpoints\n" |
| 232 | "\tevents - log broadcaster, listener and event queue activities\n" |
| 233 | "\texpr - log expressions\n" |
| 234 | "\tobject - log object construction/destruction for important objects\n" |
| 235 | "\tprocess - log process events and activities\n" |
| 236 | "\tthread - log thread events and activities\n" |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 237 | "\tscript - log events about the script interpreter\n" |
Jim Ingham | c446fe9 | 2011-05-07 01:03:33 +0000 | [diff] [blame] | 238 | "\tdyld - log shared library related activities\n" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | "\tstate - log private and public process state changes\n" |
| 240 | "\tstep - log step related activities\n" |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 241 | "\tunwind - log stack unwind activities\n" |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 242 | "\tverbose - enable verbose logging\n" |
Enrico Granata | de4059f | 2011-07-22 17:03:19 +0000 | [diff] [blame] | 243 | "\twatch - log watchpoint related activities\n" |
| 244 | "\ttypes - log type system related activities\n"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | } |