blob: cb764238683491067e9b6b03e0634ab68722cb6d [file] [log] [blame]
Chris Lattner24943d22010-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
Jim Ingham84cdc152010-06-15 19:49:27 +000012#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/StreamFile.h"
14
15#include "ProcessGDBRemote.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20
Greg Claytone005f2c2010-11-06 01:53:30 +000021// 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.
25static LogSP &
26GetLog ()
27{
28 static LogSP g_log_sp;
29 return g_log_sp;
30}
31
32LogSP
Chris Lattner24943d22010-06-08 16:52:24 +000033ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (uint32_t mask)
34{
Greg Claytone005f2c2010-11-06 01:53:30 +000035 LogSP log(GetLog ());
Chris Lattner24943d22010-06-08 16:52:24 +000036 if (log && mask)
37 {
Greg Claytonf3d0b0c2010-10-27 03:32:59 +000038 uint32_t log_mask = log->GetMask().Get();
Chris Lattner24943d22010-06-08 16:52:24 +000039 if ((log_mask & mask) != mask)
Greg Claytone005f2c2010-11-06 01:53:30 +000040 return LogSP();
Chris Lattner24943d22010-06-08 16:52:24 +000041 }
42 return log;
43}
44
Greg Clayton05e4d972012-03-29 01:55:41 +000045LogSP
46ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (uint32_t mask)
47{
48 LogSP log(GetLog ());
49 if (log && log->GetMask().Get() & mask)
50 return log;
51 return LogSP();
52}
53
Chris Lattner24943d22010-06-08 16:52:24 +000054void
Jim Ingham6c530f22012-02-21 02:23:08 +000055ProcessGDBRemoteLog::DisableLog (const char **categories, Stream *feedback_strm)
Caroline Tice926060e2010-10-29 21:48:37 +000056{
Greg Claytone005f2c2010-11-06 01:53:30 +000057 LogSP log (GetLog ());
58 if (log)
Caroline Tice926060e2010-10-29 21:48:37 +000059 {
Greg Clayton990de7b2010-11-18 23:32:35 +000060 uint32_t flag_bits = 0;
61
Jim Ingham6c530f22012-02-21 02:23:08 +000062 if (categories[0] != NULL)
Caroline Tice926060e2010-10-29 21:48:37 +000063 {
Greg Clayton990de7b2010-11-18 23:32:35 +000064 flag_bits = log->GetMask().Get();
Jim Ingham6c530f22012-02-21 02:23:08 +000065 for (size_t i = 0; categories[i] != NULL; ++i)
Caroline Tice926060e2010-10-29 21:48:37 +000066 {
Jim Ingham6c530f22012-02-21 02:23:08 +000067 const char *arg = categories[i];
Greg Clayton990de7b2010-11-18 23:32:35 +000068
69
Greg Claytond5f76bb2011-02-04 18:55:41 +000070 if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~GDBR_LOG_ALL;
71 else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~GDBR_LOG_ASYNC;
72 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~GDBR_LOG_BREAKPOINTS;
73 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~GDBR_LOG_COMM;
74 else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~GDBR_LOG_DEFAULT;
75 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~GDBR_LOG_PACKETS;
76 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY;
77 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT;
78 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG;
79 else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~GDBR_LOG_PROCESS;
80 else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~GDBR_LOG_STEP;
81 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~GDBR_LOG_THREAD;
82 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~GDBR_LOG_VERBOSE;
83 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~GDBR_LOG_WATCHPOINTS;
Greg Clayton990de7b2010-11-18 23:32:35 +000084 else
85 {
86 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
87 ListLogCategories (feedback_strm);
88 }
89
Caroline Tice926060e2010-10-29 21:48:37 +000090 }
Caroline Tice926060e2010-10-29 21:48:37 +000091 }
92
93 if (flag_bits == 0)
Greg Claytone005f2c2010-11-06 01:53:30 +000094 GetLog ().reset();
Caroline Tice926060e2010-10-29 21:48:37 +000095 else
Greg Claytone005f2c2010-11-06 01:53:30 +000096 log->GetMask().Reset (flag_bits);
Caroline Tice926060e2010-10-29 21:48:37 +000097 }
98
99 return;
100}
101
Greg Claytone005f2c2010-11-06 01:53:30 +0000102LogSP
Jim Ingham6c530f22012-02-21 02:23:08 +0000103ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm)
Chris Lattner24943d22010-06-08 16:52:24 +0000104{
Greg Claytone005f2c2010-11-06 01:53:30 +0000105 // Try see if there already is a log - that way we can reuse its settings.
106 // We could reuse the log in toto, but we don't know that the stream is the same.
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000107 uint32_t flag_bits = 0;
Greg Claytone005f2c2010-11-06 01:53:30 +0000108 LogSP log(GetLog ());
109 if (log)
110 flag_bits = log->GetMask().Get();
Greg Claytone005f2c2010-11-06 01:53:30 +0000111
112 // Now make a new log with this stream if one was provided
113 if (log_stream_sp)
114 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000115 log.reset (new Log(log_stream_sp));
Greg Claytone005f2c2010-11-06 01:53:30 +0000116 GetLog () = log;
117 }
118
119 if (log)
Chris Lattner24943d22010-06-08 16:52:24 +0000120 {
Chris Lattner24943d22010-06-08 16:52:24 +0000121 bool got_unknown_category = false;
Jim Ingham6c530f22012-02-21 02:23:08 +0000122 for (size_t i=0; categories[i] != NULL; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000123 {
Jim Ingham6c530f22012-02-21 02:23:08 +0000124 const char *arg = categories[i];
Chris Lattner24943d22010-06-08 16:52:24 +0000125
Greg Claytond5f76bb2011-02-04 18:55:41 +0000126 if (::strcasecmp (arg, "all") == 0 ) flag_bits |= GDBR_LOG_ALL;
127 else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= GDBR_LOG_ASYNC;
128 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= GDBR_LOG_BREAKPOINTS;
129 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= GDBR_LOG_COMM;
130 else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= GDBR_LOG_DEFAULT;
131 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= GDBR_LOG_PACKETS;
132 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= GDBR_LOG_MEMORY;
133 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT;
134 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG;
135 else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= GDBR_LOG_PROCESS;
136 else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= GDBR_LOG_STEP;
137 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= GDBR_LOG_THREAD;
138 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= GDBR_LOG_VERBOSE;
139 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= GDBR_LOG_WATCHPOINTS;
Chris Lattner24943d22010-06-08 16:52:24 +0000140 else
141 {
142 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
143 if (got_unknown_category == false)
144 {
145 got_unknown_category = true;
146 ListLogCategories (feedback_strm);
147 }
148 }
149 }
150 if (flag_bits == 0)
151 flag_bits = GDBR_LOG_DEFAULT;
Greg Claytone005f2c2010-11-06 01:53:30 +0000152 log->GetMask().Reset(flag_bits);
153 log->GetOptions().Reset(log_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000154 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000155 return log;
Chris Lattner24943d22010-06-08 16:52:24 +0000156}
157
158void
159ProcessGDBRemoteLog::ListLogCategories (Stream *strm)
160{
Greg Claytondad26a82011-09-12 04:05:41 +0000161 strm->Printf ("Logging categories for '%s':\n"
162 " all - turn on all available logging categories\n"
163 " async - log asynchronous activity\n"
164 " break - log breakpoints\n"
165 " communication - log communication activity\n"
166 " default - enable the default set of logging categories for liblldb\n"
167 " packets - log gdb remote packets\n"
168 " memory - log memory reads and writes\n"
169 " data-short - log memory bytes for memory reads and writes for short transactions only\n"
170 " data-long - log memory bytes for memory reads and writes for all transactions\n"
171 " process - log process events and activities\n"
172 " thread - log thread events and activities\n"
173 " step - log step related activities\n"
174 " verbose - enable verbose logging\n"
175 " watch - log watchpoint related activities\n", ProcessGDBRemote::GetPluginNameStatic());
Chris Lattner24943d22010-06-08 16:52:24 +0000176}
177
178
179void
180ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...)
181{
Greg Claytone005f2c2010-11-06 01:53:30 +0000182 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask));
Chris Lattner24943d22010-06-08 16:52:24 +0000183 if (log)
184 {
185 va_list args;
186 va_start (args, format);
187 log->VAPrintf (format, args);
188 va_end (args);
189 }
190}