blob: d673ab4880bd00d53cb075201c4063b7cf8217bd [file] [log] [blame]
Johnny Chen2341d352012-01-05 21:48:15 +00001//===-- ProcessPOSIXLog.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 "ProcessPOSIXLog.h"
11
12#include "lldb/Interpreter/Args.h"
13#include "lldb/Core/StreamFile.h"
14
15#include "ProcessPOSIX.h"
16#include "ProcessPOSIXLog.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21
22// We want to avoid global constructors where code needs to be run so here we
23// control access to our static g_log_sp by hiding it in a singleton function
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000024// that will construct the static g_log_sp the first time this function is
Johnny Chen2341d352012-01-05 21:48:15 +000025// called.
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000026static bool g_log_enabled = false;
27static Log * g_log = NULL;
28static Log *
Johnny Chen2341d352012-01-05 21:48:15 +000029GetLog ()
30{
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000031 if (!g_log_enabled)
32 return NULL;
33 return g_log;
Johnny Chen2341d352012-01-05 21:48:15 +000034}
35
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000036
37Log *
Johnny Chen2341d352012-01-05 21:48:15 +000038ProcessPOSIXLog::GetLogIfAllCategoriesSet (uint32_t mask)
39{
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000040 Log *log(GetLog ());
Johnny Chen2341d352012-01-05 21:48:15 +000041 if (log && mask)
42 {
43 uint32_t log_mask = log->GetMask().Get();
44 if ((log_mask & mask) != mask)
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000045 return NULL;
Johnny Chen2341d352012-01-05 21:48:15 +000046 }
47 return log;
48}
49
50void
Greg Claytone5eaa302012-02-21 18:40:07 +000051ProcessPOSIXLog::DisableLog (const char **args, Stream *feedback_strm)
Johnny Chen2341d352012-01-05 21:48:15 +000052{
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000053 Log *log (GetLog ());
Johnny Chen2341d352012-01-05 21:48:15 +000054 if (log)
55 {
56 uint32_t flag_bits = 0;
57
Greg Claytone5eaa302012-02-21 18:40:07 +000058 flag_bits = log->GetMask().Get();
59 for (; args[0]; args++)
Johnny Chen2341d352012-01-05 21:48:15 +000060 {
Greg Claytone5eaa302012-02-21 18:40:07 +000061 const char *arg = args[0];
Johnny Chen2341d352012-01-05 21:48:15 +000062
Greg Claytone5eaa302012-02-21 18:40:07 +000063 if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~POSIX_LOG_ALL;
64 else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~POSIX_LOG_ASYNC;
65 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~POSIX_LOG_BREAKPOINTS;
66 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~POSIX_LOG_COMM;
67 else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~POSIX_LOG_DEFAULT;
68 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~POSIX_LOG_PACKETS;
69 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~POSIX_LOG_MEMORY;
70 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~POSIX_LOG_MEMORY_DATA_SHORT;
71 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~POSIX_LOG_MEMORY_DATA_LONG;
72 else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~POSIX_LOG_PROCESS;
73 else if (::strcasecmp (arg, "ptrace") == 0 ) flag_bits &= ~POSIX_LOG_PTRACE;
74 else if (::strcasecmp (arg, "registers") == 0 ) flag_bits &= ~POSIX_LOG_REGISTERS;
75 else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~POSIX_LOG_STEP;
76 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~POSIX_LOG_THREAD;
77 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~POSIX_LOG_VERBOSE;
78 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~POSIX_LOG_WATCHPOINTS;
79 else
80 {
81 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
82 ListLogCategories (feedback_strm);
Johnny Chen2341d352012-01-05 21:48:15 +000083 }
84 }
85
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000086 log->GetMask().Reset (flag_bits);
Johnny Chen2341d352012-01-05 21:48:15 +000087 if (flag_bits == 0)
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000088 g_log_enabled = false;
Johnny Chen2341d352012-01-05 21:48:15 +000089 }
90
91 return;
92}
93
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +000094Log *
Greg Claytone5eaa302012-02-21 18:40:07 +000095ProcessPOSIXLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **args, Stream *feedback_strm)
Johnny Chen2341d352012-01-05 21:48:15 +000096{
97 // 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.
99 uint32_t flag_bits = 0;
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +0000100 if (g_log)
101 flag_bits = g_log->GetMask().Get();
Johnny Chen2341d352012-01-05 21:48:15 +0000102
103 // Now make a new log with this stream if one was provided
104 if (log_stream_sp)
105 {
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +0000106 if (g_log)
107 g_log->SetStream(log_stream_sp);
108 else
109 g_log = new Log(log_stream_sp);
Johnny Chen2341d352012-01-05 21:48:15 +0000110 }
111
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +0000112 if (g_log)
Johnny Chen2341d352012-01-05 21:48:15 +0000113 {
114 bool got_unknown_category = false;
Greg Claytone5eaa302012-02-21 18:40:07 +0000115 for (; args[0]; args++)
Johnny Chen2341d352012-01-05 21:48:15 +0000116 {
Greg Claytone5eaa302012-02-21 18:40:07 +0000117 const char *arg = args[0];
Johnny Chen2341d352012-01-05 21:48:15 +0000118
119 if (::strcasecmp (arg, "all") == 0 ) flag_bits |= POSIX_LOG_ALL;
120 else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= POSIX_LOG_ASYNC;
121 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= POSIX_LOG_BREAKPOINTS;
122 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= POSIX_LOG_COMM;
123 else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= POSIX_LOG_DEFAULT;
124 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= POSIX_LOG_PACKETS;
125 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= POSIX_LOG_MEMORY;
126 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= POSIX_LOG_MEMORY_DATA_SHORT;
127 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= POSIX_LOG_MEMORY_DATA_LONG;
128 else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= POSIX_LOG_PROCESS;
129 else if (::strcasecmp (arg, "ptrace") == 0 ) flag_bits |= POSIX_LOG_PTRACE;
130 else if (::strcasecmp (arg, "registers") == 0 ) flag_bits |= POSIX_LOG_REGISTERS;
131 else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= POSIX_LOG_STEP;
132 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= POSIX_LOG_THREAD;
133 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= POSIX_LOG_VERBOSE;
134 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= POSIX_LOG_WATCHPOINTS;
135 else
136 {
137 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
138 if (got_unknown_category == false)
139 {
140 got_unknown_category = true;
141 ListLogCategories (feedback_strm);
142 }
143 }
144 }
145 if (flag_bits == 0)
146 flag_bits = POSIX_LOG_DEFAULT;
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +0000147 g_log->GetMask().Reset(flag_bits);
148 g_log->GetOptions().Reset(log_options);
149 g_log_enabled = true;
Johnny Chen2341d352012-01-05 21:48:15 +0000150 }
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +0000151 return g_log;
Johnny Chen2341d352012-01-05 21:48:15 +0000152}
153
154void
155ProcessPOSIXLog::ListLogCategories (Stream *strm)
156{
157 strm->Printf ("Logging categories for '%s':\n"
158 " all - turn on all available logging categories\n"
159 " async - log asynchronous activity\n"
160 " break - log breakpoints\n"
161 " communication - log communication activity\n"
162 " default - enable the default set of logging categories for liblldb\n"
163 " packets - log gdb remote packets\n"
164 " memory - log memory reads and writes\n"
165 " data-short - log memory bytes for memory reads and writes for short transactions only\n"
166 " data-long - log memory bytes for memory reads and writes for all transactions\n"
167 " process - log process events and activities\n"
168#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
169 " ptrace - log all calls to ptrace\n"
170#endif
171 " registers - log register read/writes\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", ProcessPOSIXLog::m_pluginname);
176}
177
178
179void
180ProcessPOSIXLog::LogIf (uint32_t mask, const char *format, ...)
181{
Ashok Thirumurthid8f6b642013-03-28 16:02:31 +0000182 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (mask));
Johnny Chen2341d352012-01-05 21:48:15 +0000183 if (log)
184 {
185 va_list args;
186 va_start (args, format);
187 log->VAPrintf (format, args);
188 va_end (args);
189 }
190}
191
192int ProcessPOSIXLog::m_nestinglevel;
193const char *ProcessPOSIXLog::m_pluginname = "";