xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemoteLog.cpp (revision 2687cd116a0126f10a63fff8a9602a67e283f39f)
1 //===-- ProcessGDBRemoteLog.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 "ProcessGDBRemoteLog.h"
11 
12 #include "lldb/Interpreter/Args.h"
13 #include "lldb/Core/StreamFile.h"
14 
15 #include "ProcessGDBRemote.h"
16 
17 using namespace lldb;
18 using 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.
25 static LogSP &
26 GetLog ()
27 {
28     static LogSP g_log_sp;
29     return g_log_sp;
30 }
31 
32 LogSP
33 ProcessGDBRemoteLog::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 
45 LogSP
46 ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (uint32_t mask)
47 {
48     LogSP log(GetLog ());
49     if (log && log->GetMask().Get() & mask)
50         return log;
51     return LogSP();
52 }
53 
54 void
55 ProcessGDBRemoteLog::DisableLog (const char **categories, Stream *feedback_strm)
56 {
57     LogSP log (GetLog ());
58     if (log)
59     {
60         uint32_t flag_bits = 0;
61 
62         if (categories[0] != NULL)
63         {
64             flag_bits = log->GetMask().Get();
65             for (size_t i = 0; categories[i] != NULL; ++i)
66             {
67                 const char *arg = categories[i];
68 
69 
70                 if      (::strcasecmp (arg, "all")        == 0 ) flag_bits &= ~GDBR_LOG_ALL;
71                 else if (::strcasecmp (arg, "async")      == 0 ) flag_bits &= ~GDBR_LOG_ASYNC;
72                 else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits &= ~GDBR_LOG_BREAKPOINTS;
73                 else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits &= ~GDBR_LOG_COMM;
74                 else if (::strcasecmp (arg, "default")    == 0 ) flag_bits &= ~GDBR_LOG_DEFAULT;
75                 else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits &= ~GDBR_LOG_PACKETS;
76                 else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits &= ~GDBR_LOG_MEMORY;
77                 else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_SHORT;
78                 else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits &= ~GDBR_LOG_MEMORY_DATA_LONG;
79                 else if (::strcasecmp (arg, "process")    == 0 ) flag_bits &= ~GDBR_LOG_PROCESS;
80                 else if (::strcasecmp (arg, "step")       == 0 ) flag_bits &= ~GDBR_LOG_STEP;
81                 else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits &= ~GDBR_LOG_THREAD;
82                 else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits &= ~GDBR_LOG_VERBOSE;
83                 else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits &= ~GDBR_LOG_WATCHPOINTS;
84                 else
85                 {
86                     feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
87                     ListLogCategories (feedback_strm);
88                 }
89 
90             }
91         }
92 
93         if (flag_bits == 0)
94             GetLog ().reset();
95         else
96             log->GetMask().Reset (flag_bits);
97     }
98 
99     return;
100 }
101 
102 LogSP
103 ProcessGDBRemoteLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm)
104 {
105     // Try see if there already is a log - that way we can reuse its settings.
106     // We could reuse the log in toto, but we don't know that the stream is the same.
107     uint32_t flag_bits = 0;
108     LogSP log(GetLog ());
109     if (log)
110         flag_bits = log->GetMask().Get();
111 
112     // Now make a new log with this stream if one was provided
113     if (log_stream_sp)
114     {
115         log.reset (new Log(log_stream_sp));
116         GetLog () = log;
117     }
118 
119     if (log)
120     {
121         bool got_unknown_category = false;
122         for (size_t i=0; categories[i] != NULL; ++i)
123         {
124             const char *arg = categories[i];
125 
126             if      (::strcasecmp (arg, "all")        == 0 ) flag_bits |= GDBR_LOG_ALL;
127             else if (::strcasecmp (arg, "async")      == 0 ) flag_bits |= GDBR_LOG_ASYNC;
128             else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits |= GDBR_LOG_BREAKPOINTS;
129             else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits |= GDBR_LOG_COMM;
130             else if (::strcasecmp (arg, "default")    == 0 ) flag_bits |= GDBR_LOG_DEFAULT;
131             else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits |= GDBR_LOG_PACKETS;
132             else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits |= GDBR_LOG_MEMORY;
133             else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_SHORT;
134             else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits |= GDBR_LOG_MEMORY_DATA_LONG;
135             else if (::strcasecmp (arg, "process")    == 0 ) flag_bits |= GDBR_LOG_PROCESS;
136             else if (::strcasecmp (arg, "step")       == 0 ) flag_bits |= GDBR_LOG_STEP;
137             else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits |= GDBR_LOG_THREAD;
138             else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits |= GDBR_LOG_VERBOSE;
139             else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits |= GDBR_LOG_WATCHPOINTS;
140             else
141             {
142                 feedback_strm->Printf("error: unrecognized log category '%s'\n", arg);
143                 if (got_unknown_category == false)
144                 {
145                     got_unknown_category = true;
146                     ListLogCategories (feedback_strm);
147                 }
148             }
149         }
150         if (flag_bits == 0)
151             flag_bits = GDBR_LOG_DEFAULT;
152         log->GetMask().Reset(flag_bits);
153         log->GetOptions().Reset(log_options);
154     }
155     return log;
156 }
157 
158 void
159 ProcessGDBRemoteLog::ListLogCategories (Stream *strm)
160 {
161     strm->Printf ("Logging categories for '%s':\n"
162                   "  all - turn on all available logging categories\n"
163                   "  async - log asynchronous activity\n"
164                   "  break - log breakpoints\n"
165                   "  communication - log communication activity\n"
166                   "  default - enable the default set of logging categories for liblldb\n"
167                   "  packets - log gdb remote packets\n"
168                   "  memory - log memory reads and writes\n"
169                   "  data-short - log memory bytes for memory reads and writes for short transactions only\n"
170                   "  data-long - log memory bytes for memory reads and writes for all transactions\n"
171                   "  process - log process events and activities\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", ProcessGDBRemote::GetPluginNameStatic());
176 }
177 
178 
179 void
180 ProcessGDBRemoteLog::LogIf (uint32_t mask, const char *format, ...)
181 {
182     LogSP log (ProcessGDBRemoteLog::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