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