blob: 899037ae98aa4851799728842ff0255ea23269bd [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ProcessGDBRemoteLog.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 "ProcessGDBRemoteLog.h"
11
Robert Flack5f4b6c72015-03-11 21:14:22 +000012#include <mutex>
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/StreamFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
17#include "ProcessGDBRemote.h"
18
19using namespace lldb;
20using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000021using namespace lldb_private::process_gdb_remote;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Greg Clayton2d4edfb2010-11-06 01:53:30 +000023// We want to avoid global constructors where code needs to be run so here we
24// control access to our static g_log_sp by hiding it in a singleton function
Kate Stoneb9c1b512016-09-06 20:57:50 +000025// that will construct the static g_lob_sp the first time this function is
Greg Clayton2d4edfb2010-11-06 01:53:30 +000026// called.
Greg Clayton5160ce52013-03-27 23:08:40 +000027static bool g_log_enabled = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000028static Log *g_log = NULL;
29static Log *GetLog() {
30 if (!g_log_enabled)
Greg Clayton5160ce52013-03-27 23:08:40 +000031 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 return g_log;
Greg Clayton2687cd12012-03-29 01:55:41 +000033}
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035void ProcessGDBRemoteLog::Initialize() {
36 static ConstString g_name("gdb-remote");
37 static std::once_flag g_once_flag;
Greg Clayton99d0faf2010-11-18 23:32:35 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 std::call_once(g_once_flag, []() {
40 Log::Callbacks log_callbacks = {DisableLog, EnableLog, ListLogCategories};
41
42 Log::RegisterLogChannel(g_name, log_callbacks);
43 });
Caroline Tice20ad3c42010-10-29 21:48:37 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046Log *ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(uint32_t mask) {
47 Log *log(GetLog());
48 if (log && mask) {
49 uint32_t log_mask = log->GetMask().Get();
50 if ((log_mask & mask) != mask)
51 return NULL;
52 }
53 return log;
54}
55
56Log *ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(uint32_t mask) {
57 Log *log(GetLog());
58 if (log && log->GetMask().Get() & mask)
59 return log;
60 return NULL;
61}
62
63void ProcessGDBRemoteLog::DisableLog(const char **categories,
64 Stream *feedback_strm) {
65 Log *log(GetLog());
66 if (log) {
Greg Clayton1a65ae12011-01-25 23:55:37 +000067 uint32_t flag_bits = 0;
Greg Clayton2d4edfb2010-11-06 01:53:30 +000068
Zachary Turnerbbae6a92016-10-04 00:09:44 +000069 if (categories && categories[0]) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 flag_bits = log->GetMask().Get();
71 for (size_t i = 0; categories[i] != NULL; ++i) {
72 const char *arg = categories[i];
Greg Clayton2d4edfb2010-11-06 01:53:30 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 if (::strcasecmp(arg, "all") == 0)
75 flag_bits &= ~GDBR_LOG_ALL;
76 else if (::strcasecmp(arg, "async") == 0)
77 flag_bits &= ~GDBR_LOG_ASYNC;
78 else if (::strncasecmp(arg, "break", 5) == 0)
79 flag_bits &= ~GDBR_LOG_BREAKPOINTS;
80 else if (::strncasecmp(arg, "comm", 4) == 0)
81 flag_bits &= ~GDBR_LOG_COMM;
82 else if (::strcasecmp(arg, "default") == 0)
83 flag_bits &= ~GDBR_LOG_DEFAULT;
84 else if (::strcasecmp(arg, "packets") == 0)
85 flag_bits &= ~GDBR_LOG_PACKETS;
86 else if (::strcasecmp(arg, "memory") == 0)
87 flag_bits &= ~GDBR_LOG_MEMORY;
88 else if (::strcasecmp(arg, "data-short") == 0)
89 flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT;
90 else if (::strcasecmp(arg, "data-long") == 0)
91 flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG;
92 else if (::strcasecmp(arg, "process") == 0)
93 flag_bits &= ~GDBR_LOG_PROCESS;
94 else if (::strcasecmp(arg, "step") == 0)
95 flag_bits &= ~GDBR_LOG_STEP;
96 else if (::strcasecmp(arg, "thread") == 0)
97 flag_bits &= ~GDBR_LOG_THREAD;
98 else if (::strcasecmp(arg, "verbose") == 0)
99 flag_bits &= ~GDBR_LOG_VERBOSE;
100 else if (::strncasecmp(arg, "watch", 5) == 0)
101 flag_bits &= ~GDBR_LOG_WATCHPOINTS;
102 else {
103 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
104 ListLogCategories(feedback_strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108
109 if (flag_bits == 0)
110 g_log_enabled = false;
111 else
112 log->GetMask().Reset(flag_bits);
113 }
114
115 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118Log *ProcessGDBRemoteLog::EnableLog(StreamSP &log_stream_sp,
119 uint32_t log_options,
120 const char **categories,
121 Stream *feedback_strm) {
122 // Try see if there already is a log - that way we can reuse its settings.
123 // We could reuse the log in toto, but we don't know that the stream is the
124 // same.
125 uint32_t flag_bits = 0;
126 if (g_log)
127 flag_bits = g_log->GetMask().Get();
128
129 // Now make a new log with this stream if one was provided
130 if (log_stream_sp) {
131 if (g_log)
132 g_log->SetStream(log_stream_sp);
133 else
134 g_log = new Log(log_stream_sp);
135 }
136
137 if (g_log) {
138 bool got_unknown_category = false;
139 for (size_t i = 0; categories[i] != NULL; ++i) {
140 const char *arg = categories[i];
141
142 if (::strcasecmp(arg, "all") == 0)
143 flag_bits |= GDBR_LOG_ALL;
144 else if (::strcasecmp(arg, "async") == 0)
145 flag_bits |= GDBR_LOG_ASYNC;
146 else if (::strncasecmp(arg, "break", 5) == 0)
147 flag_bits |= GDBR_LOG_BREAKPOINTS;
148 else if (::strncasecmp(arg, "comm", 4) == 0)
149 flag_bits |= GDBR_LOG_COMM;
150 else if (::strcasecmp(arg, "default") == 0)
151 flag_bits |= GDBR_LOG_DEFAULT;
152 else if (::strcasecmp(arg, "packets") == 0)
153 flag_bits |= GDBR_LOG_PACKETS;
154 else if (::strcasecmp(arg, "memory") == 0)
155 flag_bits |= GDBR_LOG_MEMORY;
156 else if (::strcasecmp(arg, "data-short") == 0)
157 flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT;
158 else if (::strcasecmp(arg, "data-long") == 0)
159 flag_bits |= GDBR_LOG_MEMORY_DATA_LONG;
160 else if (::strcasecmp(arg, "process") == 0)
161 flag_bits |= GDBR_LOG_PROCESS;
162 else if (::strcasecmp(arg, "step") == 0)
163 flag_bits |= GDBR_LOG_STEP;
164 else if (::strcasecmp(arg, "thread") == 0)
165 flag_bits |= GDBR_LOG_THREAD;
166 else if (::strcasecmp(arg, "verbose") == 0)
167 flag_bits |= GDBR_LOG_VERBOSE;
168 else if (::strncasecmp(arg, "watch", 5) == 0)
169 flag_bits |= GDBR_LOG_WATCHPOINTS;
170 else {
171 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
172 if (got_unknown_category == false) {
173 got_unknown_category = true;
174 ListLogCategories(feedback_strm);
175 }
176 }
177 }
178 if (flag_bits == 0)
179 flag_bits = GDBR_LOG_DEFAULT;
180 g_log->GetMask().Reset(flag_bits);
181 g_log->GetOptions().Reset(log_options);
182 }
183 g_log_enabled = true;
184 return g_log;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187void ProcessGDBRemoteLog::ListLogCategories(Stream *strm) {
188 strm->Printf(
189 "Logging categories for '%s':\n"
190 " all - turn on all available logging categories\n"
191 " async - log asynchronous activity\n"
192 " break - log breakpoints\n"
193 " communication - log communication activity\n"
194 " default - enable the default set of logging categories for liblldb\n"
195 " packets - log gdb remote packets\n"
196 " memory - log memory reads and writes\n"
197 " data-short - log memory bytes for memory reads and writes for short "
198 "transactions only\n"
199 " data-long - log memory bytes for memory reads and writes for all "
200 "transactions\n"
201 " process - log process events and activities\n"
202 " thread - log thread events and activities\n"
203 " step - log step related activities\n"
204 " verbose - enable verbose logging\n"
205 " watch - log watchpoint related activities\n",
206 ProcessGDBRemote::GetPluginNameStatic().GetCString());
207}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209void ProcessGDBRemoteLog::LogIf(uint32_t mask, const char *format, ...) {
210 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(mask));
211 if (log) {
212 va_list args;
213 va_start(args, format);
214 log->VAPrintf(format, args);
215 va_end(args);
216 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}