1 //===-- 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 
18 using namespace lldb;
19 using 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
24 // that will construct the static g_lob_sp the first time this function is
25 // called.
26 static LogSP &
27 GetLog ()
28 {
29     static LogSP g_log_sp;
30     return g_log_sp;
31 }
32 
33 LogSP
34 ProcessPOSIXLog::GetLogIfAllCategoriesSet (uint32_t mask)
35 {
36     LogSP log(GetLog ());
37     if (log && mask)
38     {
39         uint32_t log_mask = log->GetMask().Get();
40         if ((log_mask & mask) != mask)
41             return LogSP();
42     }
43     return log;
44 }
45 
46 void
47 ProcessPOSIXLog::DisableLog (const char **args, Stream *feedback_strm)
48 {
49     LogSP log (GetLog ());
50     if (log)
51     {
52         uint32_t flag_bits = 0;
53 
54         flag_bits = log->GetMask().Get();
55         for (; args[0]; args++)
56         {
57             const char *arg = args[0];
58 
59             if      (::strcasecmp (arg, "all")        == 0 ) flag_bits &= ~POSIX_LOG_ALL;
60             else if (::strcasecmp (arg, "async")      == 0 ) flag_bits &= ~POSIX_LOG_ASYNC;
61             else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits &= ~POSIX_LOG_BREAKPOINTS;
62             else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits &= ~POSIX_LOG_COMM;
63             else if (::strcasecmp (arg, "default")    == 0 ) flag_bits &= ~POSIX_LOG_DEFAULT;
64             else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits &= ~POSIX_LOG_PACKETS;
65             else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits &= ~POSIX_LOG_MEMORY;
66             else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~POSIX_LOG_MEMORY_DATA_SHORT;
67             else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits &= ~POSIX_LOG_MEMORY_DATA_LONG;
68             else if (::strcasecmp (arg, "process")    == 0 ) flag_bits &= ~POSIX_LOG_PROCESS;
69             else if (::strcasecmp (arg, "ptrace")     == 0 ) flag_bits &= ~POSIX_LOG_PTRACE;
70             else if (::strcasecmp (arg, "registers")  == 0 ) flag_bits &= ~POSIX_LOG_REGISTERS;
71             else if (::strcasecmp (arg, "step")       == 0 ) flag_bits &= ~POSIX_LOG_STEP;
72             else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits &= ~POSIX_LOG_THREAD;
73             else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits &= ~POSIX_LOG_VERBOSE;
74             else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits &= ~POSIX_LOG_WATCHPOINTS;
75             else
76             {
77                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
78                 ListLogCategories (feedback_strm);
79             }
80         }
81 
82         if (flag_bits == 0)
83             GetLog ().reset();
84         else
85             log->GetMask().Reset (flag_bits);
86     }
87 
88     return;
89 }
90 
91 LogSP
92 ProcessPOSIXLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **args, Stream *feedback_strm)
93 {
94     // Try see if there already is a log - that way we can reuse its settings.
95     // We could reuse the log in toto, but we don't know that the stream is the same.
96     uint32_t flag_bits = 0;
97     LogSP log(GetLog ());
98     if (log)
99         flag_bits = log->GetMask().Get();
100 
101     // Now make a new log with this stream if one was provided
102     if (log_stream_sp)
103     {
104         log = LogSP(new Log(log_stream_sp));
105         GetLog () = log;
106     }
107 
108     if (log)
109     {
110         bool got_unknown_category = false;
111         for (; args[0]; args++)
112         {
113             const char *arg = args[0];
114 
115             if      (::strcasecmp (arg, "all")        == 0 ) flag_bits |= POSIX_LOG_ALL;
116             else if (::strcasecmp (arg, "async")      == 0 ) flag_bits |= POSIX_LOG_ASYNC;
117             else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits |= POSIX_LOG_BREAKPOINTS;
118             else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits |= POSIX_LOG_COMM;
119             else if (::strcasecmp (arg, "default")    == 0 ) flag_bits |= POSIX_LOG_DEFAULT;
120             else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits |= POSIX_LOG_PACKETS;
121             else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits |= POSIX_LOG_MEMORY;
122             else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= POSIX_LOG_MEMORY_DATA_SHORT;
123             else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits |= POSIX_LOG_MEMORY_DATA_LONG;
124             else if (::strcasecmp (arg, "process")    == 0 ) flag_bits |= POSIX_LOG_PROCESS;
125             else if (::strcasecmp (arg, "ptrace")     == 0 ) flag_bits |= POSIX_LOG_PTRACE;
126             else if (::strcasecmp (arg, "registers")  == 0 ) flag_bits |= POSIX_LOG_REGISTERS;
127             else if (::strcasecmp (arg, "step")       == 0 ) flag_bits |= POSIX_LOG_STEP;
128             else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits |= POSIX_LOG_THREAD;
129             else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits |= POSIX_LOG_VERBOSE;
130             else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits |= POSIX_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 = POSIX_LOG_DEFAULT;
143         log->GetMask().Reset(flag_bits);
144         log->GetOptions().Reset(log_options);
145     }
146     return log;
147 }
148 
149 void
150 ProcessPOSIXLog::ListLogCategories (Stream *strm)
151 {
152     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 #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
164                   "  ptrace - log all calls to ptrace\n"
165 #endif
166                   "  registers - log register read/writes\n"
167                   "  thread - log thread events and activities\n"
168                   "  step - log step related activities\n"
169                   "  verbose - enable verbose logging\n"
170                   "  watch - log watchpoint related activities\n", ProcessPOSIXLog::m_pluginname);
171 }
172 
173 
174 void
175 ProcessPOSIXLog::LogIf (uint32_t mask, const char *format, ...)
176 {
177     LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (mask));
178     if (log)
179     {
180         va_list args;
181         va_start (args, format);
182         log->VAPrintf (format, args);
183         va_end (args);
184     }
185 }
186 
187 int ProcessPOSIXLog::m_nestinglevel;
188 const char *ProcessPOSIXLog::m_pluginname = "";
189