1 //===-- CommandObjectLog.cpp ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CommandObjectLog.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Host/OptionParser.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13 #include "lldb/Interpreter/OptionArgParser.h"
14 #include "lldb/Interpreter/Options.h"
15 #include "lldb/Utility/Args.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/Timer.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 #define LLDB_OPTIONS_log
25 #include "CommandOptions.inc"
26 
27 /// Common completion logic for log enable/disable.
28 static void CompleteEnableDisable(CompletionRequest &request) {
29   size_t arg_index = request.GetCursorIndex();
30   if (arg_index == 0) { // We got: log enable/disable x[tab]
31     for (llvm::StringRef channel : Log::ListChannels())
32       request.TryCompleteCurrentArg(channel);
33   } else if (arg_index >= 1) { // We got: log enable/disable channel x[tab]
34     llvm::StringRef channel = request.GetParsedLine().GetArgumentAtIndex(0);
35     Log::ForEachChannelCategory(
36         channel, [&request](llvm::StringRef name, llvm::StringRef desc) {
37           request.TryCompleteCurrentArg(name, desc);
38         });
39   }
40 }
41 
42 class CommandObjectLogEnable : public CommandObjectParsed {
43 public:
44   // Constructors and Destructors
45   CommandObjectLogEnable(CommandInterpreter &interpreter)
46       : CommandObjectParsed(interpreter, "log enable",
47                             "Enable logging for a single log channel.",
48                             nullptr),
49         m_options() {
50     CommandArgumentEntry arg1;
51     CommandArgumentEntry arg2;
52     CommandArgumentData channel_arg;
53     CommandArgumentData category_arg;
54 
55     // Define the first (and only) variant of this arg.
56     channel_arg.arg_type = eArgTypeLogChannel;
57     channel_arg.arg_repetition = eArgRepeatPlain;
58 
59     // There is only one variant this argument could be; put it into the
60     // argument entry.
61     arg1.push_back(channel_arg);
62 
63     category_arg.arg_type = eArgTypeLogCategory;
64     category_arg.arg_repetition = eArgRepeatPlus;
65 
66     arg2.push_back(category_arg);
67 
68     // Push the data for the first argument into the m_arguments vector.
69     m_arguments.push_back(arg1);
70     m_arguments.push_back(arg2);
71   }
72 
73   ~CommandObjectLogEnable() override = default;
74 
75   Options *GetOptions() override { return &m_options; }
76 
77   class CommandOptions : public Options {
78   public:
79     CommandOptions() : Options(), log_file(), log_options(0) {}
80 
81     ~CommandOptions() override = default;
82 
83     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
84                           ExecutionContext *execution_context) override {
85       Status error;
86       const int short_option = m_getopt_table[option_idx].val;
87 
88       switch (short_option) {
89       case 'f':
90         log_file.SetFile(option_arg, FileSpec::Style::native);
91         FileSystem::Instance().Resolve(log_file);
92         break;
93       case 't':
94         log_options |= LLDB_LOG_OPTION_THREADSAFE;
95         break;
96       case 'v':
97         log_options |= LLDB_LOG_OPTION_VERBOSE;
98         break;
99       case 's':
100         log_options |= LLDB_LOG_OPTION_PREPEND_SEQUENCE;
101         break;
102       case 'T':
103         log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP;
104         break;
105       case 'p':
106         log_options |= LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD;
107         break;
108       case 'n':
109         log_options |= LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
110         break;
111       case 'S':
112         log_options |= LLDB_LOG_OPTION_BACKTRACE;
113         break;
114       case 'a':
115         log_options |= LLDB_LOG_OPTION_APPEND;
116         break;
117       case 'F':
118         log_options |= LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION;
119         break;
120       default:
121         llvm_unreachable("Unimplemented option");
122       }
123 
124       return error;
125     }
126 
127     void OptionParsingStarting(ExecutionContext *execution_context) override {
128       log_file.Clear();
129       log_options = 0;
130     }
131 
132     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
133       return llvm::makeArrayRef(g_log_options);
134     }
135 
136     // Instance variables to hold the values for command options.
137 
138     FileSpec log_file;
139     uint32_t log_options;
140   };
141 
142   void
143   HandleArgumentCompletion(CompletionRequest &request,
144                            OptionElementVector &opt_element_vector) override {
145     CompleteEnableDisable(request);
146   }
147 
148 protected:
149   bool DoExecute(Args &args, CommandReturnObject &result) override {
150     if (args.GetArgumentCount() < 2) {
151       result.AppendErrorWithFormat(
152           "%s takes a log channel and one or more log types.\n",
153           m_cmd_name.c_str());
154       result.SetStatus(eReturnStatusFailed);
155       return false;
156     }
157 
158     // Store into a std::string since we're about to shift the channel off.
159     const std::string channel = args[0].ref();
160     args.Shift(); // Shift off the channel
161     char log_file[PATH_MAX];
162     if (m_options.log_file)
163       m_options.log_file.GetPath(log_file, sizeof(log_file));
164     else
165       log_file[0] = '\0';
166 
167     std::string error;
168     llvm::raw_string_ostream error_stream(error);
169     bool success =
170         GetDebugger().EnableLog(channel, args.GetArgumentArrayRef(), log_file,
171                                 m_options.log_options, error_stream);
172     result.GetErrorStream() << error_stream.str();
173 
174     if (success)
175       result.SetStatus(eReturnStatusSuccessFinishNoResult);
176     else
177       result.SetStatus(eReturnStatusFailed);
178     return result.Succeeded();
179   }
180 
181   CommandOptions m_options;
182 };
183 
184 class CommandObjectLogDisable : public CommandObjectParsed {
185 public:
186   // Constructors and Destructors
187   CommandObjectLogDisable(CommandInterpreter &interpreter)
188       : CommandObjectParsed(interpreter, "log disable",
189                             "Disable one or more log channel categories.",
190                             nullptr) {
191     CommandArgumentEntry arg1;
192     CommandArgumentEntry arg2;
193     CommandArgumentData channel_arg;
194     CommandArgumentData category_arg;
195 
196     // Define the first (and only) variant of this arg.
197     channel_arg.arg_type = eArgTypeLogChannel;
198     channel_arg.arg_repetition = eArgRepeatPlain;
199 
200     // There is only one variant this argument could be; put it into the
201     // argument entry.
202     arg1.push_back(channel_arg);
203 
204     category_arg.arg_type = eArgTypeLogCategory;
205     category_arg.arg_repetition = eArgRepeatPlus;
206 
207     arg2.push_back(category_arg);
208 
209     // Push the data for the first argument into the m_arguments vector.
210     m_arguments.push_back(arg1);
211     m_arguments.push_back(arg2);
212   }
213 
214   ~CommandObjectLogDisable() override = default;
215 
216   void
217   HandleArgumentCompletion(CompletionRequest &request,
218                            OptionElementVector &opt_element_vector) override {
219     CompleteEnableDisable(request);
220   }
221 
222 protected:
223   bool DoExecute(Args &args, CommandReturnObject &result) override {
224     if (args.empty()) {
225       result.AppendErrorWithFormat(
226           "%s takes a log channel and one or more log types.\n",
227           m_cmd_name.c_str());
228       result.SetStatus(eReturnStatusFailed);
229       return false;
230     }
231 
232     const std::string channel = args[0].ref();
233     args.Shift(); // Shift off the channel
234     if (channel == "all") {
235       Log::DisableAllLogChannels();
236       result.SetStatus(eReturnStatusSuccessFinishNoResult);
237     } else {
238       std::string error;
239       llvm::raw_string_ostream error_stream(error);
240       if (Log::DisableLogChannel(channel, args.GetArgumentArrayRef(),
241                                  error_stream))
242         result.SetStatus(eReturnStatusSuccessFinishNoResult);
243       result.GetErrorStream() << error_stream.str();
244     }
245     return result.Succeeded();
246   }
247 };
248 
249 class CommandObjectLogList : public CommandObjectParsed {
250 public:
251   // Constructors and Destructors
252   CommandObjectLogList(CommandInterpreter &interpreter)
253       : CommandObjectParsed(interpreter, "log list",
254                             "List the log categories for one or more log "
255                             "channels.  If none specified, lists them all.",
256                             nullptr) {
257     CommandArgumentEntry arg;
258     CommandArgumentData channel_arg;
259 
260     // Define the first (and only) variant of this arg.
261     channel_arg.arg_type = eArgTypeLogChannel;
262     channel_arg.arg_repetition = eArgRepeatStar;
263 
264     // There is only one variant this argument could be; put it into the
265     // argument entry.
266     arg.push_back(channel_arg);
267 
268     // Push the data for the first argument into the m_arguments vector.
269     m_arguments.push_back(arg);
270   }
271 
272   ~CommandObjectLogList() override = default;
273 
274   void
275   HandleArgumentCompletion(CompletionRequest &request,
276                            OptionElementVector &opt_element_vector) override {
277     for (llvm::StringRef channel : Log::ListChannels())
278       request.TryCompleteCurrentArg(channel);
279   }
280 
281 protected:
282   bool DoExecute(Args &args, CommandReturnObject &result) override {
283     std::string output;
284     llvm::raw_string_ostream output_stream(output);
285     if (args.empty()) {
286       Log::ListAllLogChannels(output_stream);
287       result.SetStatus(eReturnStatusSuccessFinishResult);
288     } else {
289       bool success = true;
290       for (const auto &entry : args.entries())
291         success =
292             success && Log::ListChannelCategories(entry.ref(), output_stream);
293       if (success)
294         result.SetStatus(eReturnStatusSuccessFinishResult);
295     }
296     result.GetOutputStream() << output_stream.str();
297     return result.Succeeded();
298   }
299 };
300 
301 class CommandObjectLogTimer : public CommandObjectParsed {
302 public:
303   // Constructors and Destructors
304   CommandObjectLogTimer(CommandInterpreter &interpreter)
305       : CommandObjectParsed(interpreter, "log timers",
306                             "Enable, disable, dump, and reset LLDB internal "
307                             "performance timers.",
308                             "log timers < enable <depth> | disable | dump | "
309                             "increment <bool> | reset >") {}
310 
311   ~CommandObjectLogTimer() override = default;
312 
313 protected:
314   bool DoExecute(Args &args, CommandReturnObject &result) override {
315     result.SetStatus(eReturnStatusFailed);
316 
317     if (args.GetArgumentCount() == 1) {
318       auto sub_command = args[0].ref();
319 
320       if (sub_command.equals_lower("enable")) {
321         Timer::SetDisplayDepth(UINT32_MAX);
322         result.SetStatus(eReturnStatusSuccessFinishNoResult);
323       } else if (sub_command.equals_lower("disable")) {
324         Timer::DumpCategoryTimes(&result.GetOutputStream());
325         Timer::SetDisplayDepth(0);
326         result.SetStatus(eReturnStatusSuccessFinishResult);
327       } else if (sub_command.equals_lower("dump")) {
328         Timer::DumpCategoryTimes(&result.GetOutputStream());
329         result.SetStatus(eReturnStatusSuccessFinishResult);
330       } else if (sub_command.equals_lower("reset")) {
331         Timer::ResetCategoryTimes();
332         result.SetStatus(eReturnStatusSuccessFinishResult);
333       }
334     } else if (args.GetArgumentCount() == 2) {
335       auto sub_command = args[0].ref();
336       auto param = args[1].ref();
337 
338       if (sub_command.equals_lower("enable")) {
339         uint32_t depth;
340         if (param.consumeInteger(0, depth)) {
341           result.AppendError(
342               "Could not convert enable depth to an unsigned integer.");
343         } else {
344           Timer::SetDisplayDepth(depth);
345           result.SetStatus(eReturnStatusSuccessFinishNoResult);
346         }
347       } else if (sub_command.equals_lower("increment")) {
348         bool success;
349         bool increment = OptionArgParser::ToBoolean(param, false, &success);
350         if (success) {
351           Timer::SetQuiet(!increment);
352           result.SetStatus(eReturnStatusSuccessFinishNoResult);
353         } else
354           result.AppendError("Could not convert increment value to boolean.");
355       }
356     }
357 
358     if (!result.Succeeded()) {
359       result.AppendError("Missing subcommand");
360       result.AppendErrorWithFormat("Usage: %s\n", m_cmd_syntax.c_str());
361     }
362     return result.Succeeded();
363   }
364 };
365 
366 CommandObjectLog::CommandObjectLog(CommandInterpreter &interpreter)
367     : CommandObjectMultiword(interpreter, "log",
368                              "Commands controlling LLDB internal logging.",
369                              "log <subcommand> [<command-options>]") {
370   LoadSubCommand("enable",
371                  CommandObjectSP(new CommandObjectLogEnable(interpreter)));
372   LoadSubCommand("disable",
373                  CommandObjectSP(new CommandObjectLogDisable(interpreter)));
374   LoadSubCommand("list",
375                  CommandObjectSP(new CommandObjectLogList(interpreter)));
376   LoadSubCommand("timers",
377                  CommandObjectSP(new CommandObjectLogTimer(interpreter)));
378 }
379 
380 CommandObjectLog::~CommandObjectLog() = default;
381