blob: c07263e891ca09d75508fc8eeca61f7816a62be7 [file] [log] [blame]
Johnny Chenac51e9f2011-10-11 21:21:57 +00001//===-- ProcessLinuxLog.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 "ProcessLinuxLog.h"
11
12#include "lldb/Interpreter/Args.h"
13#include "lldb/Core/StreamFile.h"
14
15#include "ProcessLinux.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
33ProcessLinuxLog::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
46ProcessLinuxLog::DisableLog (Args &args, Stream *feedback_strm)
47{
48 LogSP log (GetLog ());
49 if (log)
50 {
51 uint32_t flag_bits = 0;
52
53 const size_t argc = args.GetArgumentCount ();
54 if (argc > 0)
55 {
56 flag_bits = log->GetMask().Get();
57 for (size_t i = 0; i < argc; ++i)
58 {
59 const char *arg = args.GetArgumentAtIndex (i);
60
61
62 if (::strcasecmp (arg, "all") == 0 ) flag_bits &= ~LINUX_LOG_ALL;
63 else if (::strcasecmp (arg, "async") == 0 ) flag_bits &= ~LINUX_LOG_ASYNC;
64 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits &= ~LINUX_LOG_BREAKPOINTS;
65 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits &= ~LINUX_LOG_COMM;
66 else if (::strcasecmp (arg, "default") == 0 ) flag_bits &= ~LINUX_LOG_DEFAULT;
67 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits &= ~LINUX_LOG_PACKETS;
68 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits &= ~LINUX_LOG_MEMORY;
69 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~LINUX_LOG_MEMORY_DATA_SHORT;
70 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits &= ~LINUX_LOG_MEMORY_DATA_LONG;
71 else if (::strcasecmp (arg, "process") == 0 ) flag_bits &= ~LINUX_LOG_PROCESS;
Johnny Chen3bf3a9d2011-10-18 18:09:30 +000072 else if (::strcasecmp (arg, "ptrace") == 0 ) flag_bits &= ~LINUX_LOG_PTRACE;
73 else if (::strcasecmp (arg, "registers") == 0 ) flag_bits &= ~LINUX_LOG_REGISTERS;
Johnny Chenac51e9f2011-10-11 21:21:57 +000074 else if (::strcasecmp (arg, "step") == 0 ) flag_bits &= ~LINUX_LOG_STEP;
75 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits &= ~LINUX_LOG_THREAD;
76 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits &= ~LINUX_LOG_VERBOSE;
77 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits &= ~LINUX_LOG_WATCHPOINTS;
78 else
79 {
80 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
81 ListLogCategories (feedback_strm);
82 }
83
84 }
85 }
86
87 if (flag_bits == 0)
88 GetLog ().reset();
89 else
90 log->GetMask().Reset (flag_bits);
91 }
92
93 return;
94}
95
96LogSP
97ProcessLinuxLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
98{
99 // Try see if there already is a log - that way we can reuse its settings.
100 // We could reuse the log in toto, but we don't know that the stream is the same.
101 uint32_t flag_bits = 0;
102 LogSP log(GetLog ());
103 if (log)
104 flag_bits = log->GetMask().Get();
105
106 // Now make a new log with this stream if one was provided
107 if (log_stream_sp)
108 {
109 log = make_shared<Log>(log_stream_sp);
110 GetLog () = log;
111 }
112
113 if (log)
114 {
115 bool got_unknown_category = false;
116 const size_t argc = args.GetArgumentCount();
117 for (size_t i=0; i<argc; ++i)
118 {
119 const char *arg = args.GetArgumentAtIndex(i);
120
121 if (::strcasecmp (arg, "all") == 0 ) flag_bits |= LINUX_LOG_ALL;
122 else if (::strcasecmp (arg, "async") == 0 ) flag_bits |= LINUX_LOG_ASYNC;
123 else if (::strncasecmp (arg, "break", 5) == 0 ) flag_bits |= LINUX_LOG_BREAKPOINTS;
124 else if (::strncasecmp (arg, "comm", 4) == 0 ) flag_bits |= LINUX_LOG_COMM;
125 else if (::strcasecmp (arg, "default") == 0 ) flag_bits |= LINUX_LOG_DEFAULT;
126 else if (::strcasecmp (arg, "packets") == 0 ) flag_bits |= LINUX_LOG_PACKETS;
127 else if (::strcasecmp (arg, "memory") == 0 ) flag_bits |= LINUX_LOG_MEMORY;
128 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= LINUX_LOG_MEMORY_DATA_SHORT;
129 else if (::strcasecmp (arg, "data-long") == 0 ) flag_bits |= LINUX_LOG_MEMORY_DATA_LONG;
130 else if (::strcasecmp (arg, "process") == 0 ) flag_bits |= LINUX_LOG_PROCESS;
Johnny Chen3bf3a9d2011-10-18 18:09:30 +0000131 else if (::strcasecmp (arg, "ptrace") == 0 ) flag_bits |= LINUX_LOG_PTRACE;
132 else if (::strcasecmp (arg, "registers") == 0 ) flag_bits |= LINUX_LOG_REGISTERS;
Johnny Chenac51e9f2011-10-11 21:21:57 +0000133 else if (::strcasecmp (arg, "step") == 0 ) flag_bits |= LINUX_LOG_STEP;
134 else if (::strcasecmp (arg, "thread") == 0 ) flag_bits |= LINUX_LOG_THREAD;
135 else if (::strcasecmp (arg, "verbose") == 0 ) flag_bits |= LINUX_LOG_VERBOSE;
136 else if (::strncasecmp (arg, "watch", 5) == 0 ) flag_bits |= LINUX_LOG_WATCHPOINTS;
137 else
138 {
139 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
140 if (got_unknown_category == false)
141 {
142 got_unknown_category = true;
143 ListLogCategories (feedback_strm);
144 }
145 }
146 }
147 if (flag_bits == 0)
148 flag_bits = LINUX_LOG_DEFAULT;
149 log->GetMask().Reset(flag_bits);
150 log->GetOptions().Reset(log_options);
151 }
152 return log;
153}
154
155void
156ProcessLinuxLog::ListLogCategories (Stream *strm)
157{
158 strm->Printf ("Logging categories for '%s':\n"
159 " all - turn on all available logging categories\n"
160 " async - log asynchronous activity\n"
161 " break - log breakpoints\n"
162 " communication - log communication activity\n"
163 " default - enable the default set of logging categories for liblldb\n"
164 " packets - log gdb remote packets\n"
165 " memory - log memory reads and writes\n"
166 " data-short - log memory bytes for memory reads and writes for short transactions only\n"
167 " data-long - log memory bytes for memory reads and writes for all transactions\n"
168 " process - log process events and activities\n"
Johnny Chen3bf3a9d2011-10-18 18:09:30 +0000169#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
170 " ptrace - log all calls to ptrace\n"
171#endif
172 " registers - log register read/writes\n"
Johnny Chenac51e9f2011-10-11 21:21:57 +0000173 " thread - log thread events and activities\n"
174 " step - log step related activities\n"
175 " verbose - enable verbose logging\n"
176 " watch - log watchpoint related activities\n", ProcessLinux::GetPluginNameStatic());
177}
178
179
180void
181ProcessLinuxLog::LogIf (uint32_t mask, const char *format, ...)
182{
183 LogSP log (ProcessLinuxLog::GetLogIfAllCategoriesSet (mask));
184 if (log)
185 {
186 va_list args;
187 va_start (args, format);
188 log->VAPrintf (format, args);
189 va_end (args);
190 }
191}
Johnny Chen3bf3a9d2011-10-18 18:09:30 +0000192
193int ProcessLinuxLog::m_nestinglevel;