blob: ff3c46ece3fd5f17432d78535db2fc77a372d691 [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.
25static LogSP &
26GetLog ()
27{
28 static LogSP g_log_sp;
29 return g_log_sp;
30}
31
32LogSP
33ProcessKDPLog::GetLogIfAllCategoriesSet (uint32_t mask)
34{
35 LogSP log(GetLog ());
36 if (log && mask)
37 {
38 uint32_t log_mask = log->GetMask().Get();
39 if ((log_mask & mask) != mask)
40 return LogSP();
41 }
42 return log;
43}
44
45void
Jim Ingham6c530f22012-02-21 02:23:08 +000046ProcessKDPLog::DisableLog (const char **categories, Stream *feedback_strm)
Greg Clayton363be3f2011-07-15 03:27:12 +000047{
48 LogSP log (GetLog ());
49 if (log)
50 {
51 uint32_t flag_bits = 0;
52
Jim Ingham6c530f22012-02-21 02:23:08 +000053 if (categories[0] != NULL)
Greg Clayton363be3f2011-07-15 03:27:12 +000054 {
55 flag_bits = log->GetMask().Get();
Jim Ingham6c530f22012-02-21 02:23:08 +000056 for (size_t i = 0; categories[i] != NULL; ++i)
Greg Clayton363be3f2011-07-15 03:27:12 +000057 {
Jim Ingham6c530f22012-02-21 02:23:08 +000058 const char *arg = categories[i];
Greg Clayton363be3f2011-07-15 03:27:12 +000059
60
61 if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~KDP_LOG_ALL;
62 else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~KDP_LOG_ASYNC;
63 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~KDP_LOG_BREAKPOINTS;
64 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~KDP_LOG_COMM;
65 else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~KDP_LOG_DEFAULT;
66 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~KDP_LOG_PACKETS;
67 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~KDP_LOG_MEMORY;
68 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_SHORT;
69 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_LONG;
70 else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~KDP_LOG_PROCESS;
71 else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~KDP_LOG_STEP;
72 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~KDP_LOG_THREAD;
73 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~KDP_LOG_VERBOSE;
74 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~KDP_LOG_WATCHPOINTS;
75 else
76 {
77 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
78 ListLogCategories (feedback_strm);
79 }
80
81 }
82 }
83
84 if (flag_bits == 0)
85 GetLog ().reset();
86 else
87 log->GetMask().Reset (flag_bits);
88 }
89
90 return;
91}
92
93LogSP
Jim Ingham6c530f22012-02-21 02:23:08 +000094ProcessKDPLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm)
Greg Clayton363be3f2011-07-15 03:27:12 +000095{
96 // Try see if there already is a log - that way we can reuse its settings.
97 // We could reuse the log in toto, but we don't know that the stream is the same.
98 uint32_t flag_bits = 0;
99 LogSP log(GetLog ());
100 if (log)
101 flag_bits = log->GetMask().Get();
102
103 // Now make a new log with this stream if one was provided
104 if (log_stream_sp)
105 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000106 log.reset (new Log(log_stream_sp));
Greg Clayton363be3f2011-07-15 03:27:12 +0000107 GetLog () = log;
108 }
109
110 if (log)
111 {
112 bool got_unknown_category = false;
Jim Ingham6c530f22012-02-21 02:23:08 +0000113 for (size_t i=0; categories[i] != NULL; ++i)
Greg Clayton363be3f2011-07-15 03:27:12 +0000114 {
Jim Ingham6c530f22012-02-21 02:23:08 +0000115 const char *arg = categories[i];
Greg Clayton363be3f2011-07-15 03:27:12 +0000116
117 if (::strcasecmp (arg, "all") == 0 ) flag_bits |= KDP_LOG_ALL;
118 else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= KDP_LOG_ASYNC;
119 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= KDP_LOG_BREAKPOINTS;
120 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= KDP_LOG_COMM;
121 else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= KDP_LOG_DEFAULT;
122 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= KDP_LOG_PACKETS;
123 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= KDP_LOG_MEMORY;
124 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_SHORT;
125 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_LONG;
126 else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= KDP_LOG_PROCESS;
127 else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= KDP_LOG_STEP;
128 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= KDP_LOG_THREAD;
129 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= KDP_LOG_VERBOSE;
130 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= KDP_LOG_WATCHPOINTS;
131 else
132 {
133 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
134 if (got_unknown_category == false)
135 {
136 got_unknown_category = true;
137 ListLogCategories (feedback_strm);
138 }
139 }
140 }
141 if (flag_bits == 0)
142 flag_bits = KDP_LOG_DEFAULT;
143 log->GetMask().Reset(flag_bits);
144 log->GetOptions().Reset(log_options);
145 }
146 return log;
147}
148
149void
150ProcessKDPLog::ListLogCategories (Stream *strm)
151{
Greg Claytondad26a82011-09-12 04:05:41 +0000152 strm->Printf ("Logging categories for '%s':\n"
153 " all - turn on all available logging categories\n"
154 " async - log asynchronous activity\n"
155 " break - log breakpoints\n"
156 " communication - log communication activity\n"
157 " default - enable the default set of logging categories for liblldb\n"
158 " packets - log gdb remote packets\n"
159 " memory - log memory reads and writes\n"
160 " data-short - log memory bytes for memory reads and writes for short transactions only\n"
161 " data-long - log memory bytes for memory reads and writes for all transactions\n"
162 " process - log process events and activities\n"
163 " thread - log thread events and activities\n"
164 " step - log step related activities\n"
165 " verbose - enable verbose logging\n"
166 " watch - log watchpoint related activities\n", ProcessKDP::GetPluginNameStatic());
Greg Clayton363be3f2011-07-15 03:27:12 +0000167}
168
169
170void
171ProcessKDPLog::LogIf (uint32_t mask, const char *format, ...)
172{
173 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (mask));
174 if (log)
175 {
176 va_list args;
177 va_start (args, format);
178 log->VAPrintf (format, args);
179 va_end (args);
180 }
181}