blob: d042a8f5c58353e762fb24aff307470056982165 [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;
Greg Clayton990de7b2010-11-18 23:32:35 +0000137 else
138 {
139 feedback_strm->Printf ("error: unrecognized log category '%s'\n", arg);
140 ListLogCategories (feedback_strm);
141 return;
142 }
143
Caroline Tice926060e2010-10-29 21:48:37 +0000144 }
Caroline Tice926060e2010-10-29 21:48:37 +0000145 }
146 if (flag_bits == 0)
Greg Claytone005f2c2010-11-06 01:53:30 +0000147 GetLog ().reset();
Caroline Tice926060e2010-10-29 21:48:37 +0000148 else
149 log->GetMask().Reset (flag_bits);
150 }
151
152 return;
153}
Chris Lattner24943d22010-06-08 16:52:24 +0000154
Greg Claytone005f2c2010-11-06 01:53:30 +0000155LogSP
Chris Lattner24943d22010-06-08 16:52:24 +0000156lldb_private::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
157{
158 // Try see if there already is a log - that way we can reuse its settings.
159 // We could reuse the log in toto, but we don't know that the stream is the same.
160 uint32_t flag_bits;
Greg Claytone005f2c2010-11-06 01:53:30 +0000161 LogSP log(GetLog ());
Chris Lattner24943d22010-06-08 16:52:24 +0000162 if (log)
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000163 flag_bits = log->GetMask().Get();
Chris Lattner24943d22010-06-08 16:52:24 +0000164 else
165 flag_bits = 0;
166
Greg Claytone005f2c2010-11-06 01:53:30 +0000167 // Now make a new log with this stream if one was provided
168 if (log_stream_sp)
169 {
170 log = make_shared<Log>(log_stream_sp);
171 GetLog () = log;
172 }
173
Chris Lattner24943d22010-06-08 16:52:24 +0000174 if (log)
175 {
176 bool got_unknown_category = false;
177 const size_t argc = args.GetArgumentCount();
178 for (size_t i=0; i<argc; ++i)
179 {
180 const char *arg = args.GetArgumentAtIndex(i);
181
Greg Claytond5f76bb2011-02-04 18:55:41 +0000182 if (0 == ::strcasecmp(arg, "all")) flag_bits |= LIBLLDB_LOG_ALL;
183 else if (0 == ::strcasecmp(arg, "api")) flag_bits |= LIBLLDB_LOG_API;
184 else if (0 == ::strncasecmp(arg, "break", 5)) flag_bits |= LIBLLDB_LOG_BREAKPOINTS;
185 else if (0 == ::strcasecmp(arg, "commands")) flag_bits |= LIBLLDB_LOG_COMMANDS;
186 else if (0 == ::strcasecmp(arg, "default")) flag_bits |= LIBLLDB_LOG_DEFAULT;
187 else if (0 == ::strcasecmp(arg, "dyld")) flag_bits |= LIBLLDB_LOG_DYNAMIC_LOADER;
188 else if (0 == ::strncasecmp(arg, "event", 5)) flag_bits |= LIBLLDB_LOG_EVENTS;
189 else if (0 == ::strncasecmp(arg, "expr", 4)) flag_bits |= LIBLLDB_LOG_EXPRESSIONS;
190 else if (0 == ::strncasecmp(arg, "object", 6)) flag_bits |= LIBLLDB_LOG_OBJECT;
191 else if (0 == ::strcasecmp(arg, "process")) flag_bits |= LIBLLDB_LOG_PROCESS;
192 else if (0 == ::strcasecmp(arg, "script")) flag_bits |= LIBLLDB_LOG_SCRIPT;
193 else if (0 == ::strcasecmp(arg, "state")) flag_bits |= LIBLLDB_LOG_STATE;
194 else if (0 == ::strcasecmp(arg, "step")) flag_bits |= LIBLLDB_LOG_STEP;
195 else if (0 == ::strcasecmp(arg, "thread")) flag_bits |= LIBLLDB_LOG_THREAD;
196 else if (0 == ::strcasecmp(arg, "verbose")) flag_bits |= LIBLLDB_LOG_VERBOSE;
197 else if (0 == ::strncasecmp(arg, "watch", 5)) flag_bits |= LIBLLDB_LOG_WATCHPOINTS;
198 else if (0 == ::strncasecmp(arg, "temp", 4)) flag_bits |= LIBLLDB_LOG_TEMPORARY;
199 else if (0 == ::strncasecmp(arg, "comm", 4)) flag_bits |= LIBLLDB_LOG_COMMUNICATION;
200 else if (0 == ::strncasecmp(arg, "conn", 4)) flag_bits |= LIBLLDB_LOG_CONNECTION;
201 else if (0 == ::strncasecmp(arg, "host", 4)) flag_bits |= LIBLLDB_LOG_HOST;
202 else if (0 == ::strncasecmp(arg, "unwind", 6)) flag_bits |= LIBLLDB_LOG_UNWIND;
Chris Lattner24943d22010-06-08 16:52:24 +0000203 else
204 {
205 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
206 if (got_unknown_category == false)
207 {
208 got_unknown_category = true;
209 ListLogCategories (feedback_strm);
210 return log;
211 }
212 }
213 }
214
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000215 log->GetMask().Reset(flag_bits);
216 log->GetOptions().Reset(log_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000217 }
218 return log;
219}
220
221
222void
223lldb_private::ListLogCategories (Stream *strm)
224{
225 strm->Printf("Logging categories for 'lldb':\n"
226 "\tall - turn on all available logging categories\n"
Caroline Tice7826c882010-10-26 03:11:13 +0000227 "\tapi - enable logging of API calls and return values\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000228 "\tdefault - enable the default set of logging categories for liblldb\n"
229 "\tbreak - log breakpoints\n"
230 "\tevents - log broadcaster, listener and event queue activities\n"
231 "\texpr - log expressions\n"
232 "\tobject - log object construction/destruction for important objects\n"
233 "\tprocess - log process events and activities\n"
234 "\tthread - log thread events and activities\n"
Caroline Tice2ade6112010-11-10 19:18:14 +0000235 "\tscript - log events about the script interpreter\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000236 "\tshlib - log shared library related activities\n"
237 "\tstate - log private and public process state changes\n"
238 "\tstep - log step related activities\n"
Jason Molenda8280cbe2010-10-25 11:12:07 +0000239 "\tunwind - log stack unwind activities\n"
Greg Clayton5d187e52011-01-08 20:28:42 +0000240 "\tverbose - enable verbose logging\n"
Chris Lattner24943d22010-06-08 16:52:24 +0000241 "\twatch - log watchpoint related activities\n");
242}