blob: b02dd25d5f10af12d4304662363d6ce0cf3f1b15 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/StreamFile.h"
Eli Friedmance6fe882010-06-10 04:51:35 +000019#include <string.h>
Chris Lattner24943d22010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24
Greg Claytone005f2c2010-11-06 01:53:30 +000025// 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.
29static LogSP &
30GetLog ()
Chris Lattner24943d22010-06-08 16:52:24 +000031{
Greg Claytone005f2c2010-11-06 01:53:30 +000032 static LogSP g_log_sp;
33 return g_log_sp;
Chris Lattner24943d22010-06-08 16:52:24 +000034}
35
36uint32_t
37lldb_private::GetLogMask ()
38{
Greg Claytone005f2c2010-11-06 01:53:30 +000039 LogSP log(GetLog ());
Chris Lattner24943d22010-06-08 16:52:24 +000040 if (log)
Greg Claytonf3d0b0c2010-10-27 03:32:59 +000041 return log->GetMask().Get();
Chris Lattner24943d22010-06-08 16:52:24 +000042 return 0;
43}
44
45bool
46lldb_private::IsLogVerbose ()
47{
48 uint32_t mask = GetLogMask();
49 return (mask & LIBLLDB_LOG_VERBOSE);
50}
51
Greg Claytone005f2c2010-11-06 01:53:30 +000052LogSP
Chris Lattner24943d22010-06-08 16:52:24 +000053lldb_private::GetLogIfAllCategoriesSet (uint32_t mask)
54{
Greg Claytone005f2c2010-11-06 01:53:30 +000055 LogSP log(GetLog ());
Chris Lattner24943d22010-06-08 16:52:24 +000056 if (log && mask)
57 {
Greg Claytonf3d0b0c2010-10-27 03:32:59 +000058 uint32_t log_mask = log->GetMask().Get();
Chris Lattner24943d22010-06-08 16:52:24 +000059 if ((log_mask & mask) != mask)
Greg Claytone005f2c2010-11-06 01:53:30 +000060 return LogSP();
Chris Lattner24943d22010-06-08 16:52:24 +000061 }
62 return log;
63}
64
65void
66lldb_private::LogIfAllCategoriesSet (uint32_t mask, const char *format, ...)
67{
Greg Claytone005f2c2010-11-06 01:53:30 +000068 LogSP log(GetLogIfAllCategoriesSet (mask));
Chris Lattner24943d22010-06-08 16:52:24 +000069 if (log)
70 {
71 va_list args;
72 va_start (args, format);
73 log->VAPrintf (format, args);
74 va_end (args);
75 }
76}
77
78void
79lldb_private::LogIfAnyCategoriesSet (uint32_t mask, const char *format, ...)
80{
Greg Claytone005f2c2010-11-06 01:53:30 +000081 LogSP log(GetLogIfAnyCategoriesSet (mask));
Chris Lattner24943d22010-06-08 16:52:24 +000082 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 Claytone005f2c2010-11-06 01:53:30 +000091LogSP
Chris Lattner24943d22010-06-08 16:52:24 +000092lldb_private::GetLogIfAnyCategoriesSet (uint32_t mask)
93{
Greg Claytone005f2c2010-11-06 01:53:30 +000094 LogSP log(GetLog ());
Greg Claytonf3d0b0c2010-10-27 03:32:59 +000095 if (log && mask && (mask & log->GetMask().Get()))
Chris Lattner24943d22010-06-08 16:52:24 +000096 return log;
Greg Claytone005f2c2010-11-06 01:53:30 +000097 return LogSP();
Chris Lattner24943d22010-06-08 16:52:24 +000098}
99
100void
Caroline Tice926060e2010-10-29 21:48:37 +0000101lldb_private::DisableLog (Args &args, Stream *feedback_strm)
Chris Lattner24943d22010-06-08 16:52:24 +0000102{
Greg Claytone005f2c2010-11-06 01:53:30 +0000103 LogSP log(GetLog ());
Chris Lattner24943d22010-06-08 16:52:24 +0000104
Caroline Tice926060e2010-10-29 21:48:37 +0000105 if (log)
106 {
Greg Clayton990de7b2010-11-18 23:32:35 +0000107 uint32_t flag_bits = 0;
Caroline Tice926060e2010-10-29 21:48:37 +0000108 const size_t argc = args.GetArgumentCount ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000109 if (argc > 0)
Caroline Tice926060e2010-10-29 21:48:37 +0000110 {
Greg Clayton990de7b2010-11-18 23:32:35 +0000111 flag_bits = log->GetMask().Get();
112 for (size_t i = 0; i < argc; ++i)
Caroline Tice926060e2010-10-29 21:48:37 +0000113 {
Greg Clayton990de7b2010-11-18 23:32:35 +0000114 const char *arg = args.GetArgumentAtIndex (i);
115
Greg Claytond5f76bb2011-02-04 18:55:41 +0000116 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 Granatade4059f2011-07-22 17:03:19 +0000137 else if (0 == ::strncasecmp(arg, "types", 5)) flag_bits &= ~LIBLLDB_LOG_TYPES;
Greg Claytoned11c1e2011-09-29 23:41:34 +0000138 else if (0 == ::strncasecmp(arg, "symbol", 6)) flag_bits &= ~LIBLLDB_LOG_SYMBOLS;
Greg Clayton990de7b2010-11-18 23:32:35 +0000139 else
140 {
141 feedback_strm->Printf ("error: unrecognized log category '%s'\n", arg);
142 ListLogCategories (feedback_strm);
143 return;
144 }
145
Caroline Tice926060e2010-10-29 21:48:37 +0000146 }
Caroline Tice926060e2010-10-29 21:48:37 +0000147 }
148 if (flag_bits == 0)
Greg Claytone005f2c2010-11-06 01:53:30 +0000149 GetLog ().reset();
Caroline Tice926060e2010-10-29 21:48:37 +0000150 else
151 log->GetMask().Reset (flag_bits);
152 }
153
154 return;
155}
Chris Lattner24943d22010-06-08 16:52:24 +0000156
Greg Claytone005f2c2010-11-06 01:53:30 +0000157LogSP
Chris Lattner24943d22010-06-08 16:52:24 +0000158lldb_private::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
159{
160 // Try see if there already is a log - that way we can reuse its settings.
161 // We could reuse the log in toto, but we don't know that the stream is the same.
162 uint32_t flag_bits;
Greg Claytone005f2c2010-11-06 01:53:30 +0000163 LogSP log(GetLog ());
Chris Lattner24943d22010-06-08 16:52:24 +0000164 if (log)
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000165 flag_bits = log->GetMask().Get();
Chris Lattner24943d22010-06-08 16:52:24 +0000166 else
167 flag_bits = 0;
168
Greg Claytone005f2c2010-11-06 01:53:30 +0000169 // Now make a new log with this stream if one was provided
170 if (log_stream_sp)
171 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000172 log.reset (new Log(log_stream_sp));
Greg Claytone005f2c2010-11-06 01:53:30 +0000173 GetLog () = log;
174 }
175
Chris Lattner24943d22010-06-08 16:52:24 +0000176 if (log)
177 {
178 bool got_unknown_category = false;
179 const size_t argc = args.GetArgumentCount();
180 for (size_t i=0; i<argc; ++i)
181 {
182 const char *arg = args.GetArgumentAtIndex(i);
183
Greg Claytond5f76bb2011-02-04 18:55:41 +0000184 if (0 == ::strcasecmp(arg, "all")) flag_bits |= LIBLLDB_LOG_ALL;
185 else if (0 == ::strcasecmp(arg, "api")) flag_bits |= LIBLLDB_LOG_API;
186 else if (0 == ::strncasecmp(arg, "break", 5)) flag_bits |= LIBLLDB_LOG_BREAKPOINTS;
187 else if (0 == ::strcasecmp(arg, "commands")) flag_bits |= LIBLLDB_LOG_COMMANDS;
188 else if (0 == ::strcasecmp(arg, "default")) flag_bits |= LIBLLDB_LOG_DEFAULT;
189 else if (0 == ::strcasecmp(arg, "dyld")) flag_bits |= LIBLLDB_LOG_DYNAMIC_LOADER;
190 else if (0 == ::strncasecmp(arg, "event", 5)) flag_bits |= LIBLLDB_LOG_EVENTS;
191 else if (0 == ::strncasecmp(arg, "expr", 4)) flag_bits |= LIBLLDB_LOG_EXPRESSIONS;
192 else if (0 == ::strncasecmp(arg, "object", 6)) flag_bits |= LIBLLDB_LOG_OBJECT;
193 else if (0 == ::strcasecmp(arg, "process")) flag_bits |= LIBLLDB_LOG_PROCESS;
194 else if (0 == ::strcasecmp(arg, "script")) flag_bits |= LIBLLDB_LOG_SCRIPT;
195 else if (0 == ::strcasecmp(arg, "state")) flag_bits |= LIBLLDB_LOG_STATE;
196 else if (0 == ::strcasecmp(arg, "step")) flag_bits |= LIBLLDB_LOG_STEP;
197 else if (0 == ::strcasecmp(arg, "thread")) flag_bits |= LIBLLDB_LOG_THREAD;
198 else if (0 == ::strcasecmp(arg, "verbose")) flag_bits |= LIBLLDB_LOG_VERBOSE;
199 else if (0 == ::strncasecmp(arg, "watch", 5)) flag_bits |= LIBLLDB_LOG_WATCHPOINTS;
200 else if (0 == ::strncasecmp(arg, "temp", 4)) flag_bits |= LIBLLDB_LOG_TEMPORARY;
201 else if (0 == ::strncasecmp(arg, "comm", 4)) flag_bits |= LIBLLDB_LOG_COMMUNICATION;
202 else if (0 == ::strncasecmp(arg, "conn", 4)) flag_bits |= LIBLLDB_LOG_CONNECTION;
203 else if (0 == ::strncasecmp(arg, "host", 4)) flag_bits |= LIBLLDB_LOG_HOST;
204 else if (0 == ::strncasecmp(arg, "unwind", 6)) flag_bits |= LIBLLDB_LOG_UNWIND;
Enrico Granatade4059f2011-07-22 17:03:19 +0000205 else if (0 == ::strncasecmp(arg, "types", 5)) flag_bits |= LIBLLDB_LOG_TYPES;
Greg Claytoned11c1e2011-09-29 23:41:34 +0000206 else if (0 == ::strncasecmp(arg, "symbol", 6)) flag_bits |= LIBLLDB_LOG_SYMBOLS;
Chris Lattner24943d22010-06-08 16:52:24 +0000207 else
208 {
209 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
210 if (got_unknown_category == false)
211 {
212 got_unknown_category = true;
213 ListLogCategories (feedback_strm);
214 return log;
215 }
216 }
217 }
218
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000219 log->GetMask().Reset(flag_bits);
220 log->GetOptions().Reset(log_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000221 }
222 return log;
223}
224
225
226void
227lldb_private::ListLogCategories (Stream *strm)
228{
229 strm->Printf("Logging categories for 'lldb':\n"
Greg Claytondad26a82011-09-12 04:05:41 +0000230 " all - turn on all available logging categories\n"
231 " api - enable logging of API calls and return values\n"
232 " command - log command argument parsing\n"
233 " default - enable the default set of logging categories for liblldb\n"
234 " break - log breakpoints\n"
235 " events - log broadcaster, listener and event queue activities\n"
236 " expr - log expressions\n"
237 " object - log object construction/destruction for important objects\n"
238 " process - log process events and activities\n"
239 " thread - log thread events and activities\n"
240 " script - log events about the script interpreter\n"
241 " dyld - log shared library related activities\n"
242 " state - log private and public process state changes\n"
243 " step - log step related activities\n"
244 " unwind - log stack unwind activities\n"
245 " verbose - enable verbose logging\n"
Greg Claytoned11c1e2011-09-29 23:41:34 +0000246 " symbol - log symbol related issues and warnings\n"
Greg Claytondad26a82011-09-12 04:05:41 +0000247 " watch - log watchpoint related activities\n"
248 " types - log type system related activities\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000249}