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 (Args &args, Stream *feedback_strm)
48 {
49     LogSP log (GetLog ());
50     if (log)
51     {
52         uint32_t flag_bits = 0;
53 
54         const size_t argc = args.GetArgumentCount ();
55         if (argc > 0)
56         {
57             flag_bits = log->GetMask().Get();
58             for (size_t i = 0; i < argc; ++i)
59             {
60                 const char *arg = args.GetArgumentAtIndex (i);
61 
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         }
87 
88         if (flag_bits == 0)
89             GetLog ().reset();
90         else
91             log->GetMask().Reset (flag_bits);
92     }
93 
94     return;
95 }
96 
97 LogSP
98 ProcessPOSIXLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, Args &args, Stream *feedback_strm)
99 {
100     // Try see if there already is a log - that way we can reuse its settings.
101     // We could reuse the log in toto, but we don't know that the stream is the same.
102     uint32_t flag_bits = 0;
103     LogSP log(GetLog ());
104     if (log)
105         flag_bits = log->GetMask().Get();
106 
107     // Now make a new log with this stream if one was provided
108     if (log_stream_sp)
109     {
110         log = make_shared<Log>(log_stream_sp);
111         GetLog () = log;
112     }
113 
114     if (log)
115     {
116         bool got_unknown_category = false;
117         const size_t argc = args.GetArgumentCount();
118         for (size_t i=0; i<argc; ++i)
119         {
120             const char *arg = args.GetArgumentAtIndex(i);
121 
122             if      (::strcasecmp (arg, "all")        == 0 ) flag_bits |= POSIX_LOG_ALL;
123             else if (::strcasecmp (arg, "async")      == 0 ) flag_bits |= POSIX_LOG_ASYNC;
124             else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits |= POSIX_LOG_BREAKPOINTS;
125             else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits |= POSIX_LOG_COMM;
126             else if (::strcasecmp (arg, "default")    == 0 ) flag_bits |= POSIX_LOG_DEFAULT;
127             else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits |= POSIX_LOG_PACKETS;
128             else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits |= POSIX_LOG_MEMORY;
129             else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= POSIX_LOG_MEMORY_DATA_SHORT;
130             else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits |= POSIX_LOG_MEMORY_DATA_LONG;
131             else if (::strcasecmp (arg, "process")    == 0 ) flag_bits |= POSIX_LOG_PROCESS;
132             else if (::strcasecmp (arg, "ptrace")     == 0 ) flag_bits |= POSIX_LOG_PTRACE;
133             else if (::strcasecmp (arg, "registers")  == 0 ) flag_bits |= POSIX_LOG_REGISTERS;
134             else if (::strcasecmp (arg, "step")       == 0 ) flag_bits |= POSIX_LOG_STEP;
135             else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits |= POSIX_LOG_THREAD;
136             else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits |= POSIX_LOG_VERBOSE;
137             else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits |= POSIX_LOG_WATCHPOINTS;
138             else
139             {
140                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
141                 if (got_unknown_category == false)
142                 {
143                     got_unknown_category = true;
144                     ListLogCategories (feedback_strm);
145                 }
146             }
147         }
148         if (flag_bits == 0)
149             flag_bits = POSIX_LOG_DEFAULT;
150         log->GetMask().Reset(flag_bits);
151         log->GetOptions().Reset(log_options);
152     }
153     return log;
154 }
155 
156 void
157 ProcessPOSIXLog::ListLogCategories (Stream *strm)
158 {
159     strm->Printf ("Logging categories for '%s':\n"
160                   "  all - turn on all available logging categories\n"
161                   "  async - log asynchronous activity\n"
162                   "  break - log breakpoints\n"
163                   "  communication - log communication activity\n"
164                   "  default - enable the default set of logging categories for liblldb\n"
165                   "  packets - log gdb remote packets\n"
166                   "  memory - log memory reads and writes\n"
167                   "  data-short - log memory bytes for memory reads and writes for short transactions only\n"
168                   "  data-long - log memory bytes for memory reads and writes for all transactions\n"
169                   "  process - log process events and activities\n"
170 #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
171                   "  ptrace - log all calls to ptrace\n"
172 #endif
173                   "  registers - log register read/writes\n"
174                   "  thread - log thread events and activities\n"
175                   "  step - log step related activities\n"
176                   "  verbose - enable verbose logging\n"
177                   "  watch - log watchpoint related activities\n", ProcessPOSIXLog::m_pluginname);
178 }
179 
180 
181 void
182 ProcessPOSIXLog::LogIf (uint32_t mask, const char *format, ...)
183 {
184     LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (mask));
185     if (log)
186     {
187         va_list args;
188         va_start (args, format);
189         log->VAPrintf (format, args);
190         va_end (args);
191     }
192 }
193 
194 int ProcessPOSIXLog::m_nestinglevel;
195 const char *ProcessPOSIXLog::m_pluginname = "";
196