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_log_sp the first time this function is
25 // called.
26 static bool g_log_enabled = false;
27 static Log * g_log = NULL;
28 static Log *
29 GetLog ()
30 {
31     if (!g_log_enabled)
32         return NULL;
33     return g_log;
34 }
35 
36 
37 Log *
38 ProcessPOSIXLog::GetLogIfAllCategoriesSet (uint32_t mask)
39 {
40     Log *log(GetLog ());
41     if (log && mask)
42     {
43         uint32_t log_mask = log->GetMask().Get();
44         if ((log_mask & mask) != mask)
45             return NULL;
46     }
47     return log;
48 }
49 
50 void
51 ProcessPOSIXLog::DisableLog (const char **args, Stream *feedback_strm)
52 {
53     Log *log (GetLog ());
54     if (log)
55     {
56         uint32_t flag_bits = 0;
57 
58         flag_bits = log->GetMask().Get();
59         for (; args[0]; args++)
60         {
61             const char *arg = args[0];
62 
63             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);
83             }
84         }
85 
86         log->GetMask().Reset (flag_bits);
87         if (flag_bits == 0)
88             g_log_enabled = false;
89     }
90 
91     return;
92 }
93 
94 Log *
95 ProcessPOSIXLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **args, Stream *feedback_strm)
96 {
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;
100     if (g_log)
101         flag_bits = g_log->GetMask().Get();
102 
103     // Now make a new log with this stream if one was provided
104     if (log_stream_sp)
105     {
106         if (g_log)
107             g_log->SetStream(log_stream_sp);
108         else
109             g_log = new Log(log_stream_sp);
110     }
111 
112     if (g_log)
113     {
114         bool got_unknown_category = false;
115         for (; args[0]; args++)
116         {
117             const char *arg = args[0];
118 
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;
147         g_log->GetMask().Reset(flag_bits);
148         g_log->GetOptions().Reset(log_options);
149         g_log_enabled = true;
150     }
151     return g_log;
152 }
153 
154 void
155 ProcessPOSIXLog::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 
179 void
180 ProcessPOSIXLog::LogIf (uint32_t mask, const char *format, ...)
181 {
182     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (mask));
183     if (log)
184     {
185         va_list args;
186         va_start (args, format);
187         log->VAPrintf (format, args);
188         va_end (args);
189     }
190 }
191 
192 int ProcessPOSIXLog::m_nestinglevel;
193 const char *ProcessPOSIXLog::m_pluginname = "";
194