blob: 53b08d53c2469310d1460a5bc1c3f0d9ac0f5da2 [file] [log] [blame]
Greg Clayton269f91e2011-07-15 18:02:58 +00001//===-- ProcessKDPLog.cpp ---------------------------------------*- C++ -*-===//
Greg Clayton363be3f2011-07-15 03:27:12 +00002//
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 "ProcessKDPLog.h"
11
12#include "lldb/Interpreter/Args.h"
13#include "lldb/Core/StreamFile.h"
14
15#include "ProcessKDP.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20
21// We want to avoid global constructors where code needs to be run so here we
22// control access to our static g_log_sp by hiding it in a singleton function
23// that will construct the static g_lob_sp the first time this function is
24// called.
Greg Clayton952e9dc2013-03-27 23:08:40 +000025static bool g_log_enabled = false;
26static Log * g_log = NULL;
27static Log *
Greg Clayton363be3f2011-07-15 03:27:12 +000028GetLog ()
29{
Greg Clayton952e9dc2013-03-27 23:08:40 +000030 if (!g_log_enabled)
31 return NULL;
32 return g_log;
Greg Clayton363be3f2011-07-15 03:27:12 +000033}
34
Greg Clayton952e9dc2013-03-27 23:08:40 +000035Log *
Greg Clayton363be3f2011-07-15 03:27:12 +000036ProcessKDPLog::GetLogIfAllCategoriesSet (uint32_t mask)
37{
Greg Clayton952e9dc2013-03-27 23:08:40 +000038 Log *log(GetLog ());
Greg Clayton363be3f2011-07-15 03:27:12 +000039 if (log && mask)
40 {
41 uint32_t log_mask = log->GetMask().Get();
42 if ((log_mask & mask) != mask)
Greg Clayton952e9dc2013-03-27 23:08:40 +000043 return NULL;
Greg Clayton363be3f2011-07-15 03:27:12 +000044 }
45 return log;
46}
47
48void
Jim Ingham6c530f22012-02-21 02:23:08 +000049ProcessKDPLog::DisableLog (const char **categories, Stream *feedback_strm)
Greg Clayton363be3f2011-07-15 03:27:12 +000050{
Greg Clayton952e9dc2013-03-27 23:08:40 +000051 Log *log (GetLog ());
Greg Clayton363be3f2011-07-15 03:27:12 +000052 if (log)
53 {
54 uint32_t flag_bits = 0;
55
Jim Ingham6c530f22012-02-21 02:23:08 +000056 if (categories[0] != NULL)
Greg Clayton363be3f2011-07-15 03:27:12 +000057 {
58 flag_bits = log->GetMask().Get();
Jim Ingham6c530f22012-02-21 02:23:08 +000059 for (size_t i = 0; categories[i] != NULL; ++i)
Greg Clayton363be3f2011-07-15 03:27:12 +000060 {
Jim Ingham6c530f22012-02-21 02:23:08 +000061 const char *arg = categories[i];
Greg Clayton363be3f2011-07-15 03:27:12 +000062
63
64 if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~KDP_LOG_ALL;
65 else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~KDP_LOG_ASYNC;
66 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~KDP_LOG_BREAKPOINTS;
67 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~KDP_LOG_COMM;
68 else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~KDP_LOG_DEFAULT;
69 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~KDP_LOG_PACKETS;
70 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~KDP_LOG_MEMORY;
71 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_SHORT;
72 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_LONG;
73 else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~KDP_LOG_PROCESS;
74 else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~KDP_LOG_STEP;
75 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~KDP_LOG_THREAD;
76 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~KDP_LOG_VERBOSE;
77 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~KDP_LOG_WATCHPOINTS;
78 else
79 {
80 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
81 ListLogCategories (feedback_strm);
82 }
83
84 }
85 }
86
Greg Clayton952e9dc2013-03-27 23:08:40 +000087 log->GetMask().Reset (flag_bits);
Greg Clayton363be3f2011-07-15 03:27:12 +000088 if (flag_bits == 0)
Greg Clayton952e9dc2013-03-27 23:08:40 +000089 g_log_enabled = false;
Greg Clayton363be3f2011-07-15 03:27:12 +000090 }
91
92 return;
93}
94
Greg Clayton952e9dc2013-03-27 23:08:40 +000095Log *
Jim Ingham6c530f22012-02-21 02:23:08 +000096ProcessKDPLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm)
Greg Clayton363be3f2011-07-15 03:27:12 +000097{
98 // Try see if there already is a log - that way we can reuse its settings.
99 // We could reuse the log in toto, but we don't know that the stream is the same.
100 uint32_t flag_bits = 0;
Greg Clayton952e9dc2013-03-27 23:08:40 +0000101 if (g_log)
102 flag_bits = g_log->GetMask().Get();
Greg Clayton363be3f2011-07-15 03:27:12 +0000103
104 // Now make a new log with this stream if one was provided
105 if (log_stream_sp)
106 {
Greg Clayton952e9dc2013-03-27 23:08:40 +0000107 if (g_log)
108 g_log->SetStream(log_stream_sp);
109 else
110 g_log = new Log(log_stream_sp);
Greg Clayton363be3f2011-07-15 03:27:12 +0000111 }
112
Greg Clayton952e9dc2013-03-27 23:08:40 +0000113 if (g_log)
Greg Clayton363be3f2011-07-15 03:27:12 +0000114 {
115 bool got_unknown_category = false;
Jim Ingham6c530f22012-02-21 02:23:08 +0000116 for (size_t i=0; categories[i] != NULL; ++i)
Greg Clayton363be3f2011-07-15 03:27:12 +0000117 {
Jim Ingham6c530f22012-02-21 02:23:08 +0000118 const char *arg = categories[i];
Greg Clayton363be3f2011-07-15 03:27:12 +0000119
120 if (::strcasecmp (arg, "all") == 0 ) flag_bits |= KDP_LOG_ALL;
121 else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= KDP_LOG_ASYNC;
122 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= KDP_LOG_BREAKPOINTS;
123 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= KDP_LOG_COMM;
124 else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= KDP_LOG_DEFAULT;
125 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= KDP_LOG_PACKETS;
126 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= KDP_LOG_MEMORY;
127 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_SHORT;
128 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_LONG;
129 else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= KDP_LOG_PROCESS;
130 else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= KDP_LOG_STEP;
131 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= KDP_LOG_THREAD;
132 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= KDP_LOG_VERBOSE;
133 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= KDP_LOG_WATCHPOINTS;
134 else
135 {
136 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
137 if (got_unknown_category == false)
138 {
139 got_unknown_category = true;
140 ListLogCategories (feedback_strm);
141 }
142 }
143 }
144 if (flag_bits == 0)
145 flag_bits = KDP_LOG_DEFAULT;
Greg Clayton952e9dc2013-03-27 23:08:40 +0000146 g_log->GetMask().Reset(flag_bits);
147 g_log->GetOptions().Reset(log_options);
Greg Clayton363be3f2011-07-15 03:27:12 +0000148 }
Greg Clayton952e9dc2013-03-27 23:08:40 +0000149 g_log_enabled = true;
150 return g_log;
Greg Clayton363be3f2011-07-15 03:27:12 +0000151}
152
153void
154ProcessKDPLog::ListLogCategories (Stream *strm)
155{
Greg Claytondad26a82011-09-12 04:05:41 +0000156 strm->Printf ("Logging categories for '%s':\n"
157 " all - turn on all available logging categories\n"
158 " async - log asynchronous activity\n"
159 " break - log breakpoints\n"
160 " communication - log communication activity\n"
161 " default - enable the default set of logging categories for liblldb\n"
162 " packets - log gdb remote packets\n"
163 " memory - log memory reads and writes\n"
164 " data-short - log memory bytes for memory reads and writes for short transactions only\n"
165 " data-long - log memory bytes for memory reads and writes for all transactions\n"
166 " process - log process events and activities\n"
167 " thread - log thread events and activities\n"
168 " step - log step related activities\n"
169 " verbose - enable verbose logging\n"
170 " watch - log watchpoint related activities\n", ProcessKDP::GetPluginNameStatic());
Greg Clayton363be3f2011-07-15 03:27:12 +0000171}
172
173
174void
175ProcessKDPLog::LogIf (uint32_t mask, const char *format, ...)
176{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000177 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (mask));
Greg Clayton363be3f2011-07-15 03:27:12 +0000178 if (log)
179 {
180 va_list args;
181 va_start (args, format);
182 log->VAPrintf (format, args);
183 va_end (args);
184 }
185}