blob: 7fd58026c28c30db22a40ec286f2d8734c38f031 [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
45void
Caroline Tice926060e2010-10-29 21:48:37 +000046ProcessGDBRemoteLog::DisableLog (Args &args, Stream *feedback_strm)
47{
Greg Claytone005f2c2010-11-06 01:53:30 +000048 LogSP log (GetLog ());
49 if (log)
Caroline Tice926060e2010-10-29 21:48:37 +000050 {
Greg Clayton990de7b2010-11-18 23:32:35 +000051 uint32_t flag_bits = 0;
52
Caroline Tice926060e2010-10-29 21:48:37 +000053 const size_t argc = args.GetArgumentCount ();
Greg Clayton990de7b2010-11-18 23:32:35 +000054 if (argc > 0)
Caroline Tice926060e2010-10-29 21:48:37 +000055 {
Greg Clayton990de7b2010-11-18 23:32:35 +000056 flag_bits = log->GetMask().Get();
57 for (size_t i = 0; i < argc; ++i)
Caroline Tice926060e2010-10-29 21:48:37 +000058 {
Greg Clayton990de7b2010-11-18 23:32:35 +000059 const char *arg = args.GetArgumentAtIndex (i);
60
61
Greg Claytond5f76bb2011-02-04 18:55:41 +000062 if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~GDBR_LOG_ALL;
63 else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~GDBR_LOG_ASYNC;
64 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~GDBR_LOG_BREAKPOINTS;
65 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~GDBR_LOG_COMM;
66 else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~GDBR_LOG_DEFAULT;
67 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~GDBR_LOG_PACKETS;
68 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY;
69 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT;
70 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG;
71 else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~GDBR_LOG_PROCESS;
72 else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~GDBR_LOG_STEP;
73 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~GDBR_LOG_THREAD;
74 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~GDBR_LOG_VERBOSE;
75 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~GDBR_LOG_WATCHPOINTS;
Greg Clayton990de7b2010-11-18 23:32:35 +000076 else
77 {
78 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
79 ListLogCategories (feedback_strm);
80 }
81
Caroline Tice926060e2010-10-29 21:48:37 +000082 }
Caroline Tice926060e2010-10-29 21:48:37 +000083 }
84
85 if (flag_bits == 0)
Greg Claytone005f2c2010-11-06 01:53:30 +000086 GetLog ().reset();
Caroline Tice926060e2010-10-29 21:48:37 +000087 else
Greg Claytone005f2c2010-11-06 01:53:30 +000088 log->GetMask().Reset (flag_bits);
Caroline Tice926060e2010-10-29 21:48:37 +000089 }
90
91 return;
92}
93
Greg Claytone005f2c2010-11-06 01:53:30 +000094LogSP
Chris Lattner24943d22010-06-08 16:52:24 +000095ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
96{
Greg Claytone005f2c2010-11-06 01:53:30 +000097 // Try see if there already is a log - that way we can reuse its settings.
98 // 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 +000099 uint32_t flag_bits = 0;
Greg Claytone005f2c2010-11-06 01:53:30 +0000100 LogSP log(GetLog ());
101 if (log)
102 flag_bits = log->GetMask().Get();
Greg Claytone005f2c2010-11-06 01:53:30 +0000103
104 // Now make a new log with this stream if one was provided
105 if (log_stream_sp)
106 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000107 log.reset (new Log(log_stream_sp));
Greg Claytone005f2c2010-11-06 01:53:30 +0000108 GetLog () = log;
109 }
110
111 if (log)
Chris Lattner24943d22010-06-08 16:52:24 +0000112 {
Chris Lattner24943d22010-06-08 16:52:24 +0000113 bool got_unknown_category = false;
114 const size_t argc = args.GetArgumentCount();
115 for (size_t i=0; i<argc; ++i)
116 {
117 const char *arg = args.GetArgumentAtIndex(i);
118
Greg Claytond5f76bb2011-02-04 18:55:41 +0000119 if (::strcasecmp (arg, "all") == 0 ) flag_bits |= GDBR_LOG_ALL;
120 else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= GDBR_LOG_ASYNC;
121 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= GDBR_LOG_BREAKPOINTS;
122 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= GDBR_LOG_COMM;
123 else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= GDBR_LOG_DEFAULT;
124 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= GDBR_LOG_PACKETS;
125 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= GDBR_LOG_MEMORY;
126 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT;
127 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG;
128 else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= GDBR_LOG_PROCESS;
129 else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= GDBR_LOG_STEP;
130 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= GDBR_LOG_THREAD;
131 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= GDBR_LOG_VERBOSE;
132 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= GDBR_LOG_WATCHPOINTS;
Chris Lattner24943d22010-06-08 16:52:24 +0000133 else
134 {
135 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
136 if (got_unknown_category == false)
137 {
138 got_unknown_category = true;
139 ListLogCategories (feedback_strm);
140 }
141 }
142 }
143 if (flag_bits == 0)
144 flag_bits = GDBR_LOG_DEFAULT;
Greg Claytone005f2c2010-11-06 01:53:30 +0000145 log->GetMask().Reset(flag_bits);
146 log->GetOptions().Reset(log_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000147 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000148 return log;
Chris Lattner24943d22010-06-08 16:52:24 +0000149}
150
151void
152ProcessGDBRemoteLog::ListLogCategories (Stream *strm)
153{
Greg Claytondad26a82011-09-12 04:05:41 +0000154 strm->Printf ("Logging categories for '%s':\n"
155 " all - turn on all available logging categories\n"
156 " async - log asynchronous activity\n"
157 " break - log breakpoints\n"
158 " communication - log communication activity\n"
159 " default - enable the default set of logging categories for liblldb\n"
160 " packets - log gdb remote packets\n"
161 " memory - log memory reads and writes\n"
162 " data-short - log memory bytes for memory reads and writes for short transactions only\n"
163 " data-long - log memory bytes for memory reads and writes for all transactions\n"
164 " process - log process events and activities\n"
165 " thread - log thread events and activities\n"
166 " step - log step related activities\n"
167 " verbose - enable verbose logging\n"
168 " watch - log watchpoint related activities\n", ProcessGDBRemote::GetPluginNameStatic());
Chris Lattner24943d22010-06-08 16:52:24 +0000169}
170
171
172void
173ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...)
174{
Greg Claytone005f2c2010-11-06 01:53:30 +0000175 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (mask));
Chris Lattner24943d22010-06-08 16:52:24 +0000176 if (log)
177 {
178 va_list args;
179 va_start (args, format);
180 log->VAPrintf (format, args);
181 va_end (args);
182 }
183}