1 //===-- StructuredDataDarwinLog.cpp ---------------------------------------===//
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 "StructuredDataDarwinLog.h"
10 
11 #include <cstring>
12 
13 #include <memory>
14 #include <sstream>
15 
16 #include "lldb/Breakpoint/StoppointCallbackContext.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Host/OptionParser.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Interpreter/CommandObjectMultiword.h"
23 #include "lldb/Interpreter/CommandReturnObject.h"
24 #include "lldb/Interpreter/OptionArgParser.h"
25 #include "lldb/Interpreter/OptionValueProperties.h"
26 #include "lldb/Interpreter/OptionValueString.h"
27 #include "lldb/Interpreter/Property.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/ThreadPlanCallOnFunctionExit.h"
31 #include "lldb/Utility/Log.h"
32 #include "lldb/Utility/RegularExpression.h"
33 
34 #define DARWIN_LOG_TYPE_VALUE "DarwinLog"
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 
39 LLDB_PLUGIN_DEFINE(StructuredDataDarwinLog)
40 
41 #pragma mark -
42 #pragma mark Anonymous Namespace
43 
44 // Anonymous namespace
45 
46 namespace sddarwinlog_private {
47 const uint64_t NANOS_PER_MICRO = 1000;
48 const uint64_t NANOS_PER_MILLI = NANOS_PER_MICRO * 1000;
49 const uint64_t NANOS_PER_SECOND = NANOS_PER_MILLI * 1000;
50 const uint64_t NANOS_PER_MINUTE = NANOS_PER_SECOND * 60;
51 const uint64_t NANOS_PER_HOUR = NANOS_PER_MINUTE * 60;
52 
53 static bool DEFAULT_FILTER_FALLTHROUGH_ACCEPTS = true;
54 
55 /// Global, sticky enable switch.  If true, the user has explicitly
56 /// run the enable command.  When a process launches or is attached to,
57 /// we will enable DarwinLog if either the settings for auto-enable is
58 /// on, or if the user had explicitly run enable at some point prior
59 /// to the launch/attach.
60 static bool s_is_explicitly_enabled;
61 
62 class EnableOptions;
63 using EnableOptionsSP = std::shared_ptr<EnableOptions>;
64 
65 using OptionsMap =
66     std::map<DebuggerWP, EnableOptionsSP, std::owner_less<DebuggerWP>>;
67 
68 static OptionsMap &GetGlobalOptionsMap() {
69   static OptionsMap s_options_map;
70   return s_options_map;
71 }
72 
73 static std::mutex &GetGlobalOptionsMapLock() {
74   static std::mutex s_options_map_lock;
75   return s_options_map_lock;
76 }
77 
78 EnableOptionsSP GetGlobalEnableOptions(const DebuggerSP &debugger_sp) {
79   if (!debugger_sp)
80     return EnableOptionsSP();
81 
82   std::lock_guard<std::mutex> locker(GetGlobalOptionsMapLock());
83   OptionsMap &options_map = GetGlobalOptionsMap();
84   DebuggerWP debugger_wp(debugger_sp);
85   auto find_it = options_map.find(debugger_wp);
86   if (find_it != options_map.end())
87     return find_it->second;
88   else
89     return EnableOptionsSP();
90 }
91 
92 void SetGlobalEnableOptions(const DebuggerSP &debugger_sp,
93                             const EnableOptionsSP &options_sp) {
94   std::lock_guard<std::mutex> locker(GetGlobalOptionsMapLock());
95   OptionsMap &options_map = GetGlobalOptionsMap();
96   DebuggerWP debugger_wp(debugger_sp);
97   auto find_it = options_map.find(debugger_wp);
98   if (find_it != options_map.end())
99     find_it->second = options_sp;
100   else
101     options_map.insert(std::make_pair(debugger_wp, options_sp));
102 }
103 
104 #pragma mark -
105 #pragma mark Settings Handling
106 
107 /// Code to handle the StructuredDataDarwinLog settings
108 
109 #define LLDB_PROPERTIES_darwinlog
110 #include "StructuredDataDarwinLogProperties.inc"
111 
112 enum {
113 #define LLDB_PROPERTIES_darwinlog
114 #include "StructuredDataDarwinLogPropertiesEnum.inc"
115 };
116 
117 class StructuredDataDarwinLogProperties : public Properties {
118 public:
119   static ConstString &GetSettingName() {
120     static ConstString g_setting_name("darwin-log");
121     return g_setting_name;
122   }
123 
124   StructuredDataDarwinLogProperties() : Properties() {
125     m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
126     m_collection_sp->Initialize(g_darwinlog_properties);
127   }
128 
129   ~StructuredDataDarwinLogProperties() override = default;
130 
131   bool GetEnableOnStartup() const {
132     const uint32_t idx = ePropertyEnableOnStartup;
133     return m_collection_sp->GetPropertyAtIndexAsBoolean(
134         nullptr, idx, g_darwinlog_properties[idx].default_uint_value != 0);
135   }
136 
137   llvm::StringRef GetAutoEnableOptions() const {
138     const uint32_t idx = ePropertyAutoEnableOptions;
139     return m_collection_sp->GetPropertyAtIndexAsString(
140         nullptr, idx, g_darwinlog_properties[idx].default_cstr_value);
141   }
142 
143   const char *GetLoggingModuleName() const { return "libsystem_trace.dylib"; }
144 };
145 
146 static StructuredDataDarwinLogProperties &GetGlobalProperties() {
147   static StructuredDataDarwinLogProperties g_settings;
148   return g_settings;
149 }
150 
151 const char *const s_filter_attributes[] = {
152     "activity",       // current activity
153     "activity-chain", // entire activity chain, each level separated by ':'
154     "category",       // category of the log message
155     "message",        // message contents, fully expanded
156     "subsystem"       // subsystem of the log message
157 
158     // Consider implementing this action as it would be cheaper to filter.
159     // "message" requires always formatting the message, which is a waste of
160     // cycles if it ends up being rejected. "format",      // format string
161     // used to format message text
162 };
163 
164 static ConstString GetDarwinLogTypeName() {
165   static const ConstString s_key_name("DarwinLog");
166   return s_key_name;
167 }
168 
169 static ConstString GetLogEventType() {
170   static const ConstString s_event_type("log");
171   return s_event_type;
172 }
173 
174 class FilterRule;
175 using FilterRuleSP = std::shared_ptr<FilterRule>;
176 
177 class FilterRule {
178 public:
179   virtual ~FilterRule() = default;
180 
181   using OperationCreationFunc =
182       std::function<FilterRuleSP(bool accept, size_t attribute_index,
183                                  const std::string &op_arg, Status &error)>;
184 
185   static void RegisterOperation(ConstString operation,
186                                 const OperationCreationFunc &creation_func) {
187     GetCreationFuncMap().insert(std::make_pair(operation, creation_func));
188   }
189 
190   static FilterRuleSP CreateRule(bool match_accepts, size_t attribute,
191                                  ConstString operation,
192                                  const std::string &op_arg, Status &error) {
193     // Find the creation func for this type of filter rule.
194     auto map = GetCreationFuncMap();
195     auto find_it = map.find(operation);
196     if (find_it == map.end()) {
197       error.SetErrorStringWithFormat("unknown filter operation \""
198                                      "%s\"",
199                                      operation.GetCString());
200       return FilterRuleSP();
201     }
202 
203     return find_it->second(match_accepts, attribute, op_arg, error);
204   }
205 
206   StructuredData::ObjectSP Serialize() const {
207     StructuredData::Dictionary *dict_p = new StructuredData::Dictionary();
208 
209     // Indicate whether this is an accept or reject rule.
210     dict_p->AddBooleanItem("accept", m_accept);
211 
212     // Indicate which attribute of the message this filter references. This can
213     // drop into the rule-specific DoSerialization if we get to the point where
214     // not all FilterRule derived classes work on an attribute.  (e.g. logical
215     // and/or and other compound operations).
216     dict_p->AddStringItem("attribute", s_filter_attributes[m_attribute_index]);
217 
218     // Indicate the type of the rule.
219     dict_p->AddStringItem("type", GetOperationType().GetCString());
220 
221     // Let the rule add its own specific details here.
222     DoSerialization(*dict_p);
223 
224     return StructuredData::ObjectSP(dict_p);
225   }
226 
227   virtual void Dump(Stream &stream) const = 0;
228 
229   ConstString GetOperationType() const { return m_operation; }
230 
231 protected:
232   FilterRule(bool accept, size_t attribute_index, ConstString operation)
233       : m_accept(accept), m_attribute_index(attribute_index),
234         m_operation(operation) {}
235 
236   virtual void DoSerialization(StructuredData::Dictionary &dict) const = 0;
237 
238   bool GetMatchAccepts() const { return m_accept; }
239 
240   const char *GetFilterAttribute() const {
241     return s_filter_attributes[m_attribute_index];
242   }
243 
244 private:
245   using CreationFuncMap = std::map<ConstString, OperationCreationFunc>;
246 
247   static CreationFuncMap &GetCreationFuncMap() {
248     static CreationFuncMap s_map;
249     return s_map;
250   }
251 
252   const bool m_accept;
253   const size_t m_attribute_index;
254   const ConstString m_operation;
255 };
256 
257 using FilterRules = std::vector<FilterRuleSP>;
258 
259 class RegexFilterRule : public FilterRule {
260 public:
261   static void RegisterOperation() {
262     FilterRule::RegisterOperation(StaticGetOperation(), CreateOperation);
263   }
264 
265   void Dump(Stream &stream) const override {
266     stream.Printf("%s %s regex %s", GetMatchAccepts() ? "accept" : "reject",
267                   GetFilterAttribute(), m_regex_text.c_str());
268   }
269 
270 protected:
271   void DoSerialization(StructuredData::Dictionary &dict) const override {
272     dict.AddStringItem("regex", m_regex_text);
273   }
274 
275 private:
276   static FilterRuleSP CreateOperation(bool accept, size_t attribute_index,
277                                       const std::string &op_arg,
278                                       Status &error) {
279     // We treat the op_arg as a regex.  Validate it.
280     if (op_arg.empty()) {
281       error.SetErrorString("regex filter type requires a regex "
282                            "argument");
283       return FilterRuleSP();
284     }
285 
286     // Instantiate the regex so we can report any errors.
287     auto regex = RegularExpression(op_arg);
288     if (llvm::Error err = regex.GetError()) {
289       error.SetErrorString(llvm::toString(std::move(err)));
290       return FilterRuleSP();
291     }
292 
293     // We passed all our checks, this appears fine.
294     error.Clear();
295     return FilterRuleSP(new RegexFilterRule(accept, attribute_index, op_arg));
296   }
297 
298   static ConstString StaticGetOperation() {
299     static ConstString s_operation("regex");
300     return s_operation;
301   }
302 
303   RegexFilterRule(bool accept, size_t attribute_index,
304                   const std::string &regex_text)
305       : FilterRule(accept, attribute_index, StaticGetOperation()),
306         m_regex_text(regex_text) {}
307 
308   const std::string m_regex_text;
309 };
310 
311 class ExactMatchFilterRule : public FilterRule {
312 public:
313   static void RegisterOperation() {
314     FilterRule::RegisterOperation(StaticGetOperation(), CreateOperation);
315   }
316 
317   void Dump(Stream &stream) const override {
318     stream.Printf("%s %s match %s", GetMatchAccepts() ? "accept" : "reject",
319                   GetFilterAttribute(), m_match_text.c_str());
320   }
321 
322 protected:
323   void DoSerialization(StructuredData::Dictionary &dict) const override {
324     dict.AddStringItem("exact_text", m_match_text);
325   }
326 
327 private:
328   static FilterRuleSP CreateOperation(bool accept, size_t attribute_index,
329                                       const std::string &op_arg,
330                                       Status &error) {
331     if (op_arg.empty()) {
332       error.SetErrorString("exact match filter type requires an "
333                            "argument containing the text that must "
334                            "match the specified message attribute.");
335       return FilterRuleSP();
336     }
337 
338     error.Clear();
339     return FilterRuleSP(
340         new ExactMatchFilterRule(accept, attribute_index, op_arg));
341   }
342 
343   static ConstString StaticGetOperation() {
344     static ConstString s_operation("match");
345     return s_operation;
346   }
347 
348   ExactMatchFilterRule(bool accept, size_t attribute_index,
349                        const std::string &match_text)
350       : FilterRule(accept, attribute_index, StaticGetOperation()),
351         m_match_text(match_text) {}
352 
353   const std::string m_match_text;
354 };
355 
356 static void RegisterFilterOperations() {
357   ExactMatchFilterRule::RegisterOperation();
358   RegexFilterRule::RegisterOperation();
359 }
360 
361 // =========================================================================
362 // Commands
363 // =========================================================================
364 
365 /// Provides the main on-off switch for enabling darwin logging.
366 ///
367 /// It is valid to run the enable command when logging is already enabled.
368 /// This resets the logging with whatever settings are currently set.
369 
370 static constexpr OptionDefinition g_enable_option_table[] = {
371     // Source stream include/exclude options (the first-level filter). This one
372     // should be made as small as possible as everything that goes through here
373     // must be processed by the process monitor.
374     {LLDB_OPT_SET_ALL, false, "any-process", 'a', OptionParser::eNoArgument,
375      nullptr, {}, 0, eArgTypeNone,
376      "Specifies log messages from other related processes should be "
377      "included."},
378     {LLDB_OPT_SET_ALL, false, "debug", 'd', OptionParser::eNoArgument, nullptr,
379      {}, 0, eArgTypeNone,
380      "Specifies debug-level log messages should be included.  Specifying"
381      " --debug implies --info."},
382     {LLDB_OPT_SET_ALL, false, "info", 'i', OptionParser::eNoArgument, nullptr,
383      {}, 0, eArgTypeNone,
384      "Specifies info-level log messages should be included."},
385     {LLDB_OPT_SET_ALL, false, "filter", 'f', OptionParser::eRequiredArgument,
386      nullptr, {}, 0, eArgRawInput,
387      // There doesn't appear to be a great way for me to have these multi-line,
388      // formatted tables in help.  This looks mostly right but there are extra
389      // linefeeds added at seemingly random spots, and indentation isn't
390      // handled properly on those lines.
391      "Appends a filter rule to the log message filter chain.  Multiple "
392      "rules may be added by specifying this option multiple times, "
393      "once per filter rule.  Filter rules are processed in the order "
394      "they are specified, with the --no-match-accepts setting used "
395      "for any message that doesn't match one of the rules.\n"
396      "\n"
397      "    Filter spec format:\n"
398      "\n"
399      "    --filter \"{action} {attribute} {op}\"\n"
400      "\n"
401      "    {action} :=\n"
402      "      accept |\n"
403      "      reject\n"
404      "\n"
405      "    {attribute} :=\n"
406      "       activity       |  // message's most-derived activity\n"
407      "       activity-chain |  // message's {parent}:{child} activity\n"
408      "       category       |  // message's category\n"
409      "       message        |  // message's expanded contents\n"
410      "       subsystem      |  // message's subsystem\n"
411      "\n"
412      "    {op} :=\n"
413      "      match {exact-match-text} |\n"
414      "      regex {search-regex}\n"
415      "\n"
416      "The regex flavor used is the C++ std::regex ECMAScript format.  "
417      "Prefer character classes like [[:digit:]] to \\d and the like, as "
418      "getting the backslashes escaped through properly is error-prone."},
419     {LLDB_OPT_SET_ALL, false, "live-stream", 'l',
420      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
421      "Specify whether logging events are live-streamed or buffered.  "
422      "True indicates live streaming, false indicates buffered.  The "
423      "default is true (live streaming).  Live streaming will deliver "
424      "log messages with less delay, but buffered capture mode has less "
425      "of an observer effect."},
426     {LLDB_OPT_SET_ALL, false, "no-match-accepts", 'n',
427      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
428      "Specify whether a log message that doesn't match any filter rule "
429      "is accepted or rejected, where true indicates accept.  The "
430      "default is true."},
431     {LLDB_OPT_SET_ALL, false, "echo-to-stderr", 'e',
432      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
433      "Specify whether os_log()/NSLog() messages are echoed to the "
434      "target program's stderr.  When DarwinLog is enabled, we shut off "
435      "the mirroring of os_log()/NSLog() to the program's stderr.  "
436      "Setting this flag to true will restore the stderr mirroring."
437      "The default is false."},
438     {LLDB_OPT_SET_ALL, false, "broadcast-events", 'b',
439      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
440      "Specify if the plugin should broadcast events.  Broadcasting "
441      "log events is a requirement for displaying the log entries in "
442      "LLDB command-line.  It is also required if LLDB clients want to "
443      "process log events.  The default is true."},
444     // Message formatting options
445     {LLDB_OPT_SET_ALL, false, "timestamp-relative", 'r',
446      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
447      "Include timestamp in the message header when printing a log "
448      "message.  The timestamp is relative to the first displayed "
449      "message."},
450     {LLDB_OPT_SET_ALL, false, "subsystem", 's', OptionParser::eNoArgument,
451      nullptr, {}, 0, eArgTypeNone,
452      "Include the subsystem in the message header when displaying "
453      "a log message."},
454     {LLDB_OPT_SET_ALL, false, "category", 'c', OptionParser::eNoArgument,
455      nullptr, {}, 0, eArgTypeNone,
456      "Include the category in the message header when displaying "
457      "a log message."},
458     {LLDB_OPT_SET_ALL, false, "activity-chain", 'C', OptionParser::eNoArgument,
459      nullptr, {}, 0, eArgTypeNone,
460      "Include the activity parent-child chain in the message header "
461      "when displaying a log message.  The activity hierarchy is "
462      "displayed as {grandparent-activity}:"
463      "{parent-activity}:{activity}[:...]."},
464     {LLDB_OPT_SET_ALL, false, "all-fields", 'A', OptionParser::eNoArgument,
465      nullptr, {}, 0, eArgTypeNone,
466      "Shortcut to specify that all header fields should be displayed."}};
467 
468 class EnableOptions : public Options {
469 public:
470   EnableOptions()
471       : Options(),
472         m_filter_fall_through_accepts(DEFAULT_FILTER_FALLTHROUGH_ACCEPTS),
473         m_filter_rules() {}
474 
475   void OptionParsingStarting(ExecutionContext *execution_context) override {
476     m_include_debug_level = false;
477     m_include_info_level = false;
478     m_include_any_process = false;
479     m_filter_fall_through_accepts = DEFAULT_FILTER_FALLTHROUGH_ACCEPTS;
480     m_echo_to_stderr = false;
481     m_display_timestamp_relative = false;
482     m_display_subsystem = false;
483     m_display_category = false;
484     m_display_activity_chain = false;
485     m_broadcast_events = true;
486     m_live_stream = true;
487     m_filter_rules.clear();
488   }
489 
490   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
491                         ExecutionContext *execution_context) override {
492     Status error;
493 
494     const int short_option = m_getopt_table[option_idx].val;
495     switch (short_option) {
496     case 'a':
497       m_include_any_process = true;
498       break;
499 
500     case 'A':
501       m_display_timestamp_relative = true;
502       m_display_category = true;
503       m_display_subsystem = true;
504       m_display_activity_chain = true;
505       break;
506 
507     case 'b':
508       m_broadcast_events =
509           OptionArgParser::ToBoolean(option_arg, true, nullptr);
510       break;
511 
512     case 'c':
513       m_display_category = true;
514       break;
515 
516     case 'C':
517       m_display_activity_chain = true;
518       break;
519 
520     case 'd':
521       m_include_debug_level = true;
522       break;
523 
524     case 'e':
525       m_echo_to_stderr = OptionArgParser::ToBoolean(option_arg, false, nullptr);
526       break;
527 
528     case 'f':
529       return ParseFilterRule(option_arg);
530 
531     case 'i':
532       m_include_info_level = true;
533       break;
534 
535     case 'l':
536       m_live_stream = OptionArgParser::ToBoolean(option_arg, false, nullptr);
537       break;
538 
539     case 'n':
540       m_filter_fall_through_accepts =
541           OptionArgParser::ToBoolean(option_arg, true, nullptr);
542       break;
543 
544     case 'r':
545       m_display_timestamp_relative = true;
546       break;
547 
548     case 's':
549       m_display_subsystem = true;
550       break;
551 
552     default:
553       error.SetErrorStringWithFormat("unsupported option '%c'", short_option);
554     }
555     return error;
556   }
557 
558   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
559     return llvm::makeArrayRef(g_enable_option_table);
560   }
561 
562   StructuredData::DictionarySP BuildConfigurationData(bool enabled) {
563     StructuredData::DictionarySP config_sp(new StructuredData::Dictionary());
564 
565     // Set the basic enabled state.
566     config_sp->AddBooleanItem("enabled", enabled);
567 
568     // If we're disabled, there's nothing more to add.
569     if (!enabled)
570       return config_sp;
571 
572     // Handle source stream flags.
573     auto source_flags_sp =
574         StructuredData::DictionarySP(new StructuredData::Dictionary());
575     config_sp->AddItem("source-flags", source_flags_sp);
576 
577     source_flags_sp->AddBooleanItem("any-process", m_include_any_process);
578     source_flags_sp->AddBooleanItem("debug-level", m_include_debug_level);
579     // The debug-level flag, if set, implies info-level.
580     source_flags_sp->AddBooleanItem("info-level", m_include_info_level ||
581                                                       m_include_debug_level);
582     source_flags_sp->AddBooleanItem("live-stream", m_live_stream);
583 
584     // Specify default filter rule (the fall-through)
585     config_sp->AddBooleanItem("filter-fall-through-accepts",
586                               m_filter_fall_through_accepts);
587 
588     // Handle filter rules
589     if (!m_filter_rules.empty()) {
590       auto json_filter_rules_sp =
591           StructuredData::ArraySP(new StructuredData::Array);
592       config_sp->AddItem("filter-rules", json_filter_rules_sp);
593       for (auto &rule_sp : m_filter_rules) {
594         if (!rule_sp)
595           continue;
596         json_filter_rules_sp->AddItem(rule_sp->Serialize());
597       }
598     }
599     return config_sp;
600   }
601 
602   bool GetIncludeDebugLevel() const { return m_include_debug_level; }
603 
604   bool GetIncludeInfoLevel() const {
605     // Specifying debug level implies info level.
606     return m_include_info_level || m_include_debug_level;
607   }
608 
609   const FilterRules &GetFilterRules() const { return m_filter_rules; }
610 
611   bool GetFallthroughAccepts() const { return m_filter_fall_through_accepts; }
612 
613   bool GetEchoToStdErr() const { return m_echo_to_stderr; }
614 
615   bool GetDisplayTimestampRelative() const {
616     return m_display_timestamp_relative;
617   }
618 
619   bool GetDisplaySubsystem() const { return m_display_subsystem; }
620   bool GetDisplayCategory() const { return m_display_category; }
621   bool GetDisplayActivityChain() const { return m_display_activity_chain; }
622 
623   bool GetDisplayAnyHeaderFields() const {
624     return m_display_timestamp_relative || m_display_activity_chain ||
625            m_display_subsystem || m_display_category;
626   }
627 
628   bool GetBroadcastEvents() const { return m_broadcast_events; }
629 
630 private:
631   Status ParseFilterRule(llvm::StringRef rule_text) {
632     Status error;
633 
634     if (rule_text.empty()) {
635       error.SetErrorString("invalid rule_text");
636       return error;
637     }
638 
639     // filter spec format:
640     //
641     // {action} {attribute} {op}
642     //
643     // {action} :=
644     //   accept |
645     //   reject
646     //
647     // {attribute} :=
648     //   category       |
649     //   subsystem      |
650     //   activity       |
651     //   activity-chain |
652     //   message        |
653     //   format
654     //
655     // {op} :=
656     //   match {exact-match-text} |
657     //   regex {search-regex}
658 
659     // Parse action.
660     auto action_end_pos = rule_text.find(' ');
661     if (action_end_pos == std::string::npos) {
662       error.SetErrorStringWithFormat("could not parse filter rule "
663                                      "action from \"%s\"",
664                                      rule_text.str().c_str());
665       return error;
666     }
667     auto action = rule_text.substr(0, action_end_pos);
668     bool accept;
669     if (action == "accept")
670       accept = true;
671     else if (action == "reject")
672       accept = false;
673     else {
674       error.SetErrorString("filter action must be \"accept\" or \"deny\"");
675       return error;
676     }
677 
678     // parse attribute
679     auto attribute_end_pos = rule_text.find(" ", action_end_pos + 1);
680     if (attribute_end_pos == std::string::npos) {
681       error.SetErrorStringWithFormat("could not parse filter rule "
682                                      "attribute from \"%s\"",
683                                      rule_text.str().c_str());
684       return error;
685     }
686     auto attribute = rule_text.substr(action_end_pos + 1,
687                                       attribute_end_pos - (action_end_pos + 1));
688     auto attribute_index = MatchAttributeIndex(attribute);
689     if (attribute_index < 0) {
690       error.SetErrorStringWithFormat("filter rule attribute unknown: "
691                                      "%s",
692                                      attribute.str().c_str());
693       return error;
694     }
695 
696     // parse operation
697     auto operation_end_pos = rule_text.find(" ", attribute_end_pos + 1);
698     auto operation = rule_text.substr(
699         attribute_end_pos + 1, operation_end_pos - (attribute_end_pos + 1));
700 
701     // add filter spec
702     auto rule_sp = FilterRule::CreateRule(
703         accept, attribute_index, ConstString(operation),
704         std::string(rule_text.substr(operation_end_pos + 1)), error);
705 
706     if (rule_sp && error.Success())
707       m_filter_rules.push_back(rule_sp);
708 
709     return error;
710   }
711 
712   int MatchAttributeIndex(llvm::StringRef attribute_name) const {
713     for (const auto &Item : llvm::enumerate(s_filter_attributes)) {
714       if (attribute_name == Item.value())
715         return Item.index();
716     }
717 
718     // We didn't match anything.
719     return -1;
720   }
721 
722   bool m_include_debug_level = false;
723   bool m_include_info_level = false;
724   bool m_include_any_process = false;
725   bool m_filter_fall_through_accepts;
726   bool m_echo_to_stderr = false;
727   bool m_display_timestamp_relative = false;
728   bool m_display_subsystem = false;
729   bool m_display_category = false;
730   bool m_display_activity_chain = false;
731   bool m_broadcast_events = true;
732   bool m_live_stream = true;
733   FilterRules m_filter_rules;
734 };
735 
736 class EnableCommand : public CommandObjectParsed {
737 public:
738   EnableCommand(CommandInterpreter &interpreter, bool enable, const char *name,
739                 const char *help, const char *syntax)
740       : CommandObjectParsed(interpreter, name, help, syntax), m_enable(enable),
741         m_options_sp(enable ? new EnableOptions() : nullptr) {}
742 
743 protected:
744   void AppendStrictSourcesWarning(CommandReturnObject &result,
745                                   const char *source_name) {
746     if (!source_name)
747       return;
748 
749     // Check if we're *not* using strict sources.  If not, then the user is
750     // going to get debug-level info anyways, probably not what they're
751     // expecting. Unfortunately we can only fix this by adding an env var,
752     // which would have had to have happened already.  Thus, a warning is the
753     // best we can do here.
754     StreamString stream;
755     stream.Printf("darwin-log source settings specify to exclude "
756                   "%s messages, but setting "
757                   "'plugin.structured-data.darwin-log."
758                   "strict-sources' is disabled.  This process will "
759                   "automatically have %s messages included.  Enable"
760                   " the property and relaunch the target binary to have"
761                   " these messages excluded.",
762                   source_name, source_name);
763     result.AppendWarning(stream.GetString());
764   }
765 
766   bool DoExecute(Args &command, CommandReturnObject &result) override {
767     // First off, set the global sticky state of enable/disable based on this
768     // command execution.
769     s_is_explicitly_enabled = m_enable;
770 
771     // Next, if this is an enable, save off the option data. We will need it
772     // later if a process hasn't been launched or attached yet.
773     if (m_enable) {
774       // Save off enabled configuration so we can apply these parsed options
775       // the next time an attach or launch occurs.
776       DebuggerSP debugger_sp =
777           GetCommandInterpreter().GetDebugger().shared_from_this();
778       SetGlobalEnableOptions(debugger_sp, m_options_sp);
779     }
780 
781     // Now check if we have a running process.  If so, we should instruct the
782     // process monitor to enable/disable DarwinLog support now.
783     Target &target = GetSelectedOrDummyTarget();
784 
785     // Grab the active process.
786     auto process_sp = target.GetProcessSP();
787     if (!process_sp) {
788       // No active process, so there is nothing more to do right now.
789       result.SetStatus(eReturnStatusSuccessFinishNoResult);
790       return true;
791     }
792 
793     // If the process is no longer alive, we can't do this now. We'll catch it
794     // the next time the process is started up.
795     if (!process_sp->IsAlive()) {
796       result.SetStatus(eReturnStatusSuccessFinishNoResult);
797       return true;
798     }
799 
800     // Get the plugin for the process.
801     auto plugin_sp =
802         process_sp->GetStructuredDataPlugin(GetDarwinLogTypeName());
803     if (!plugin_sp ||
804         (plugin_sp->GetPluginName() !=
805          StructuredDataDarwinLog::GetStaticPluginName().GetStringRef())) {
806       result.AppendError("failed to get StructuredDataPlugin for "
807                          "the process");
808     }
809     StructuredDataDarwinLog &plugin =
810         *static_cast<StructuredDataDarwinLog *>(plugin_sp.get());
811 
812     if (m_enable) {
813       // Hook up the breakpoint for the process that detects when libtrace has
814       // been sufficiently initialized to really start the os_log stream.  This
815       // is insurance to assure us that logging is really enabled.  Requesting
816       // that logging be enabled for a process before libtrace is initialized
817       // results in a scenario where no errors occur, but no logging is
818       // captured, either.  This step is to eliminate that possibility.
819       plugin.AddInitCompletionHook(*process_sp);
820     }
821 
822     // Send configuration to the feature by way of the process. Construct the
823     // options we will use.
824     auto config_sp = m_options_sp->BuildConfigurationData(m_enable);
825     const Status error =
826         process_sp->ConfigureStructuredData(GetDarwinLogTypeName(), config_sp);
827 
828     // Report results.
829     if (!error.Success()) {
830       result.AppendError(error.AsCString());
831       // Our configuration failed, so we're definitely disabled.
832       plugin.SetEnabled(false);
833     } else {
834       result.SetStatus(eReturnStatusSuccessFinishNoResult);
835       // Our configuration succeeded, so we're enabled/disabled per whichever
836       // one this command is setup to do.
837       plugin.SetEnabled(m_enable);
838     }
839     return result.Succeeded();
840   }
841 
842   Options *GetOptions() override {
843     // We don't have options when this represents disable.
844     return m_enable ? m_options_sp.get() : nullptr;
845   }
846 
847 private:
848   const bool m_enable;
849   EnableOptionsSP m_options_sp;
850 };
851 
852 /// Provides the status command.
853 class StatusCommand : public CommandObjectParsed {
854 public:
855   StatusCommand(CommandInterpreter &interpreter)
856       : CommandObjectParsed(interpreter, "status",
857                             "Show whether Darwin log supported is available"
858                             " and enabled.",
859                             "plugin structured-data darwin-log status") {}
860 
861 protected:
862   bool DoExecute(Args &command, CommandReturnObject &result) override {
863     auto &stream = result.GetOutputStream();
864 
865     // Figure out if we've got a process.  If so, we can tell if DarwinLog is
866     // available for that process.
867     Target &target = GetSelectedOrDummyTarget();
868     auto process_sp = target.GetProcessSP();
869     if (!process_sp) {
870       stream.PutCString("Availability: unknown (requires process)\n");
871       stream.PutCString("Enabled: not applicable "
872                         "(requires process)\n");
873     } else {
874       auto plugin_sp =
875           process_sp->GetStructuredDataPlugin(GetDarwinLogTypeName());
876       stream.Printf("Availability: %s\n",
877                     plugin_sp ? "available" : "unavailable");
878       ConstString plugin_name = StructuredDataDarwinLog::GetStaticPluginName();
879       const bool enabled =
880           plugin_sp ? plugin_sp->GetEnabled(plugin_name) : false;
881       stream.Printf("Enabled: %s\n", enabled ? "true" : "false");
882     }
883 
884     // Display filter settings.
885     DebuggerSP debugger_sp =
886         GetCommandInterpreter().GetDebugger().shared_from_this();
887     auto options_sp = GetGlobalEnableOptions(debugger_sp);
888     if (!options_sp) {
889       // Nothing more to do.
890       result.SetStatus(eReturnStatusSuccessFinishResult);
891       return true;
892     }
893 
894     // Print filter rules
895     stream.PutCString("DarwinLog filter rules:\n");
896 
897     stream.IndentMore();
898 
899     if (options_sp->GetFilterRules().empty()) {
900       stream.Indent();
901       stream.PutCString("none\n");
902     } else {
903       // Print each of the filter rules.
904       int rule_number = 0;
905       for (auto rule_sp : options_sp->GetFilterRules()) {
906         ++rule_number;
907         if (!rule_sp)
908           continue;
909 
910         stream.Indent();
911         stream.Printf("%02d: ", rule_number);
912         rule_sp->Dump(stream);
913         stream.PutChar('\n');
914       }
915     }
916     stream.IndentLess();
917 
918     // Print no-match handling.
919     stream.Indent();
920     stream.Printf("no-match behavior: %s\n",
921                   options_sp->GetFallthroughAccepts() ? "accept" : "reject");
922 
923     result.SetStatus(eReturnStatusSuccessFinishResult);
924     return true;
925   }
926 };
927 
928 /// Provides the darwin-log base command
929 class BaseCommand : public CommandObjectMultiword {
930 public:
931   BaseCommand(CommandInterpreter &interpreter)
932       : CommandObjectMultiword(interpreter, "plugin structured-data darwin-log",
933                                "Commands for configuring Darwin os_log "
934                                "support.",
935                                "") {
936     // enable
937     auto enable_help = "Enable Darwin log collection, or re-enable "
938                        "with modified configuration.";
939     auto enable_syntax = "plugin structured-data darwin-log enable";
940     auto enable_cmd_sp = CommandObjectSP(
941         new EnableCommand(interpreter,
942                           true, // enable
943                           "enable", enable_help, enable_syntax));
944     LoadSubCommand("enable", enable_cmd_sp);
945 
946     // disable
947     auto disable_help = "Disable Darwin log collection.";
948     auto disable_syntax = "plugin structured-data darwin-log disable";
949     auto disable_cmd_sp = CommandObjectSP(
950         new EnableCommand(interpreter,
951                           false, // disable
952                           "disable", disable_help, disable_syntax));
953     LoadSubCommand("disable", disable_cmd_sp);
954 
955     // status
956     auto status_cmd_sp = CommandObjectSP(new StatusCommand(interpreter));
957     LoadSubCommand("status", status_cmd_sp);
958   }
959 };
960 
961 EnableOptionsSP ParseAutoEnableOptions(Status &error, Debugger &debugger) {
962   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
963   // We are abusing the options data model here so that we can parse options
964   // without requiring the Debugger instance.
965 
966   // We have an empty execution context at this point.  We only want to parse
967   // options, and we don't need any context to do this here. In fact, we want
968   // to be able to parse the enable options before having any context.
969   ExecutionContext exe_ctx;
970 
971   EnableOptionsSP options_sp(new EnableOptions());
972   options_sp->NotifyOptionParsingStarting(&exe_ctx);
973 
974   CommandReturnObject result(debugger.GetUseColor());
975 
976   // Parse the arguments.
977   auto options_property_sp =
978       debugger.GetPropertyValue(nullptr, "plugin.structured-data.darwin-log."
979                                          "auto-enable-options",
980                                 false, error);
981   if (!error.Success())
982     return EnableOptionsSP();
983   if (!options_property_sp) {
984     error.SetErrorString("failed to find option setting for "
985                          "plugin.structured-data.darwin-log.");
986     return EnableOptionsSP();
987   }
988 
989   const char *enable_options =
990       options_property_sp->GetAsString()->GetCurrentValue();
991   Args args(enable_options);
992   if (args.GetArgumentCount() > 0) {
993     // Eliminate the initial '--' that would be required to set the settings
994     // that themselves include '-' and/or '--'.
995     const char *first_arg = args.GetArgumentAtIndex(0);
996     if (first_arg && (strcmp(first_arg, "--") == 0))
997       args.Shift();
998   }
999 
1000   bool require_validation = false;
1001   llvm::Expected<Args> args_or =
1002       options_sp->Parse(args, &exe_ctx, PlatformSP(), require_validation);
1003   if (!args_or) {
1004     LLDB_LOG_ERROR(
1005         log, args_or.takeError(),
1006         "Parsing plugin.structured-data.darwin-log.auto-enable-options value "
1007         "failed: {0}");
1008     return EnableOptionsSP();
1009   }
1010 
1011   if (!options_sp->VerifyOptions(result))
1012     return EnableOptionsSP();
1013 
1014   // We successfully parsed and validated the options.
1015   return options_sp;
1016 }
1017 
1018 bool RunEnableCommand(CommandInterpreter &interpreter) {
1019   StreamString command_stream;
1020 
1021   command_stream << "plugin structured-data darwin-log enable";
1022   auto enable_options = GetGlobalProperties().GetAutoEnableOptions();
1023   if (!enable_options.empty()) {
1024     command_stream << ' ';
1025     command_stream << enable_options;
1026   }
1027 
1028   // Run the command.
1029   CommandReturnObject return_object(interpreter.GetDebugger().GetUseColor());
1030   interpreter.HandleCommand(command_stream.GetData(), eLazyBoolNo,
1031                             return_object);
1032   return return_object.Succeeded();
1033 }
1034 }
1035 using namespace sddarwinlog_private;
1036 
1037 #pragma mark -
1038 #pragma mark Public static API
1039 
1040 // Public static API
1041 
1042 void StructuredDataDarwinLog::Initialize() {
1043   RegisterFilterOperations();
1044   PluginManager::RegisterPlugin(
1045       GetStaticPluginName(), "Darwin os_log() and os_activity() support",
1046       &CreateInstance, &DebuggerInitialize, &FilterLaunchInfo);
1047 }
1048 
1049 void StructuredDataDarwinLog::Terminate() {
1050   PluginManager::UnregisterPlugin(&CreateInstance);
1051 }
1052 
1053 ConstString StructuredDataDarwinLog::GetStaticPluginName() {
1054   static ConstString s_plugin_name("darwin-log");
1055   return s_plugin_name;
1056 }
1057 
1058 #pragma mark -
1059 #pragma mark StructuredDataPlugin API
1060 
1061 // StructuredDataPlugin API
1062 
1063 bool StructuredDataDarwinLog::SupportsStructuredDataType(
1064     ConstString type_name) {
1065   return type_name == GetDarwinLogTypeName();
1066 }
1067 
1068 void StructuredDataDarwinLog::HandleArrivalOfStructuredData(
1069     Process &process, ConstString type_name,
1070     const StructuredData::ObjectSP &object_sp) {
1071   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1072   if (log) {
1073     StreamString json_stream;
1074     if (object_sp)
1075       object_sp->Dump(json_stream);
1076     else
1077       json_stream.PutCString("<null>");
1078     LLDB_LOGF(log, "StructuredDataDarwinLog::%s() called with json: %s",
1079               __FUNCTION__, json_stream.GetData());
1080   }
1081 
1082   // Ignore empty structured data.
1083   if (!object_sp) {
1084     LLDB_LOGF(log,
1085               "StructuredDataDarwinLog::%s() StructuredData object "
1086               "is null",
1087               __FUNCTION__);
1088     return;
1089   }
1090 
1091   // Ignore any data that isn't for us.
1092   if (type_name != GetDarwinLogTypeName()) {
1093     LLDB_LOGF(log,
1094               "StructuredDataDarwinLog::%s() StructuredData type "
1095               "expected to be %s but was %s, ignoring",
1096               __FUNCTION__, GetDarwinLogTypeName().AsCString(),
1097               type_name.AsCString());
1098     return;
1099   }
1100 
1101   // Broadcast the structured data event if we have that enabled. This is the
1102   // way that the outside world (all clients) get access to this data.  This
1103   // plugin sets policy as to whether we do that.
1104   DebuggerSP debugger_sp = process.GetTarget().GetDebugger().shared_from_this();
1105   auto options_sp = GetGlobalEnableOptions(debugger_sp);
1106   if (options_sp && options_sp->GetBroadcastEvents()) {
1107     LLDB_LOGF(log, "StructuredDataDarwinLog::%s() broadcasting event",
1108               __FUNCTION__);
1109     process.BroadcastStructuredData(object_sp, shared_from_this());
1110   }
1111 
1112   // Later, hang on to a configurable amount of these and allow commands to
1113   // inspect, including showing backtraces.
1114 }
1115 
1116 static void SetErrorWithJSON(Status &error, const char *message,
1117                              StructuredData::Object &object) {
1118   if (!message) {
1119     error.SetErrorString("Internal error: message not set.");
1120     return;
1121   }
1122 
1123   StreamString object_stream;
1124   object.Dump(object_stream);
1125   object_stream.Flush();
1126 
1127   error.SetErrorStringWithFormat("%s: %s", message, object_stream.GetData());
1128 }
1129 
1130 Status StructuredDataDarwinLog::GetDescription(
1131     const StructuredData::ObjectSP &object_sp, lldb_private::Stream &stream) {
1132   Status error;
1133 
1134   if (!object_sp) {
1135     error.SetErrorString("No structured data.");
1136     return error;
1137   }
1138 
1139   // Log message payload objects will be dictionaries.
1140   const StructuredData::Dictionary *dictionary = object_sp->GetAsDictionary();
1141   if (!dictionary) {
1142     SetErrorWithJSON(error, "Structured data should have been a dictionary "
1143                             "but wasn't",
1144                      *object_sp);
1145     return error;
1146   }
1147 
1148   // Validate this is really a message for our plugin.
1149   ConstString type_name;
1150   if (!dictionary->GetValueForKeyAsString("type", type_name)) {
1151     SetErrorWithJSON(error, "Structured data doesn't contain mandatory "
1152                             "type field",
1153                      *object_sp);
1154     return error;
1155   }
1156 
1157   if (type_name != GetDarwinLogTypeName()) {
1158     // This is okay - it simply means the data we received is not a log
1159     // message.  We'll just format it as is.
1160     object_sp->Dump(stream);
1161     return error;
1162   }
1163 
1164   // DarwinLog dictionaries store their data
1165   // in an array with key name "events".
1166   StructuredData::Array *events = nullptr;
1167   if (!dictionary->GetValueForKeyAsArray("events", events) || !events) {
1168     SetErrorWithJSON(error, "Log structured data is missing mandatory "
1169                             "'events' field, expected to be an array",
1170                      *object_sp);
1171     return error;
1172   }
1173 
1174   events->ForEach(
1175       [&stream, &error, &object_sp, this](StructuredData::Object *object) {
1176         if (!object) {
1177           // Invalid.  Stop iterating.
1178           SetErrorWithJSON(error, "Log event entry is null", *object_sp);
1179           return false;
1180         }
1181 
1182         auto event = object->GetAsDictionary();
1183         if (!event) {
1184           // Invalid, stop iterating.
1185           SetErrorWithJSON(error, "Log event is not a dictionary", *object_sp);
1186           return false;
1187         }
1188 
1189         // If we haven't already grabbed the first timestamp value, do that
1190         // now.
1191         if (!m_recorded_first_timestamp) {
1192           uint64_t timestamp = 0;
1193           if (event->GetValueForKeyAsInteger("timestamp", timestamp)) {
1194             m_first_timestamp_seen = timestamp;
1195             m_recorded_first_timestamp = true;
1196           }
1197         }
1198 
1199         HandleDisplayOfEvent(*event, stream);
1200         return true;
1201       });
1202 
1203   stream.Flush();
1204   return error;
1205 }
1206 
1207 bool StructuredDataDarwinLog::GetEnabled(ConstString type_name) const {
1208   if (type_name == GetStaticPluginName())
1209     return m_is_enabled;
1210   else
1211     return false;
1212 }
1213 
1214 void StructuredDataDarwinLog::SetEnabled(bool enabled) {
1215   m_is_enabled = enabled;
1216 }
1217 
1218 void StructuredDataDarwinLog::ModulesDidLoad(Process &process,
1219                                              ModuleList &module_list) {
1220   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1221   LLDB_LOGF(log, "StructuredDataDarwinLog::%s called (process uid %u)",
1222             __FUNCTION__, process.GetUniqueID());
1223 
1224   // Check if we should enable the darwin log support on startup/attach.
1225   if (!GetGlobalProperties().GetEnableOnStartup() &&
1226       !s_is_explicitly_enabled) {
1227     // We're neither auto-enabled or explicitly enabled, so we shouldn't try to
1228     // enable here.
1229     LLDB_LOGF(log,
1230               "StructuredDataDarwinLog::%s not applicable, we're not "
1231               "enabled (process uid %u)",
1232               __FUNCTION__, process.GetUniqueID());
1233     return;
1234   }
1235 
1236   // If we already added the breakpoint, we've got nothing left to do.
1237   {
1238     std::lock_guard<std::mutex> locker(m_added_breakpoint_mutex);
1239     if (m_added_breakpoint) {
1240       LLDB_LOGF(log,
1241                 "StructuredDataDarwinLog::%s process uid %u's "
1242                 "post-libtrace-init breakpoint is already set",
1243                 __FUNCTION__, process.GetUniqueID());
1244       return;
1245     }
1246   }
1247 
1248   // The logging support module name, specifies the name of the image name that
1249   // must be loaded into the debugged process before we can try to enable
1250   // logging.
1251   const char *logging_module_cstr =
1252       GetGlobalProperties().GetLoggingModuleName();
1253   if (!logging_module_cstr || (logging_module_cstr[0] == 0)) {
1254     // We need this.  Bail.
1255     LLDB_LOGF(log,
1256               "StructuredDataDarwinLog::%s no logging module name "
1257               "specified, we don't know where to set a breakpoint "
1258               "(process uid %u)",
1259               __FUNCTION__, process.GetUniqueID());
1260     return;
1261   }
1262 
1263   const ConstString logging_module_name = ConstString(logging_module_cstr);
1264 
1265   // We need to see libtrace in the list of modules before we can enable
1266   // tracing for the target process.
1267   bool found_logging_support_module = false;
1268   for (size_t i = 0; i < module_list.GetSize(); ++i) {
1269     auto module_sp = module_list.GetModuleAtIndex(i);
1270     if (!module_sp)
1271       continue;
1272 
1273     auto &file_spec = module_sp->GetFileSpec();
1274     found_logging_support_module =
1275         (file_spec.GetLastPathComponent() == logging_module_name);
1276     if (found_logging_support_module)
1277       break;
1278   }
1279 
1280   if (!found_logging_support_module) {
1281     LLDB_LOGF(log,
1282               "StructuredDataDarwinLog::%s logging module %s "
1283               "has not yet been loaded, can't set a breakpoint "
1284               "yet (process uid %u)",
1285               __FUNCTION__, logging_module_name.AsCString(),
1286               process.GetUniqueID());
1287     return;
1288   }
1289 
1290   // Time to enqueue the breakpoint so we can wait for logging support to be
1291   // initialized before we try to tap the libtrace stream.
1292   AddInitCompletionHook(process);
1293   LLDB_LOGF(log,
1294             "StructuredDataDarwinLog::%s post-init hook breakpoint "
1295             "set for logging module %s (process uid %u)",
1296             __FUNCTION__, logging_module_name.AsCString(),
1297             process.GetUniqueID());
1298 
1299   // We need to try the enable here as well, which will succeed in the event
1300   // that we're attaching to (rather than launching) the process and the
1301   // process is already past initialization time.  In that case, the completion
1302   // breakpoint will never get hit and therefore won't start that way.  It
1303   // doesn't hurt much beyond a bit of bandwidth if we end up doing this twice.
1304   // It hurts much more if we don't get the logging enabled when the user
1305   // expects it.
1306   EnableNow();
1307 }
1308 
1309 // public destructor
1310 
1311 StructuredDataDarwinLog::~StructuredDataDarwinLog() {
1312   if (m_breakpoint_id != LLDB_INVALID_BREAK_ID) {
1313     ProcessSP process_sp(GetProcess());
1314     if (process_sp) {
1315       process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id);
1316       m_breakpoint_id = LLDB_INVALID_BREAK_ID;
1317     }
1318   }
1319 }
1320 
1321 #pragma mark -
1322 #pragma mark Private instance methods
1323 
1324 // Private constructors
1325 
1326 StructuredDataDarwinLog::StructuredDataDarwinLog(const ProcessWP &process_wp)
1327     : StructuredDataPlugin(process_wp), m_recorded_first_timestamp(false),
1328       m_first_timestamp_seen(0), m_is_enabled(false),
1329       m_added_breakpoint_mutex(), m_added_breakpoint(),
1330       m_breakpoint_id(LLDB_INVALID_BREAK_ID) {}
1331 
1332 // Private static methods
1333 
1334 StructuredDataPluginSP
1335 StructuredDataDarwinLog::CreateInstance(Process &process) {
1336   // Currently only Apple targets support the os_log/os_activity protocol.
1337   if (process.GetTarget().GetArchitecture().GetTriple().getVendor() ==
1338       llvm::Triple::VendorType::Apple) {
1339     auto process_wp = ProcessWP(process.shared_from_this());
1340     return StructuredDataPluginSP(new StructuredDataDarwinLog(process_wp));
1341   } else {
1342     return StructuredDataPluginSP();
1343   }
1344 }
1345 
1346 void StructuredDataDarwinLog::DebuggerInitialize(Debugger &debugger) {
1347   // Setup parent class first.
1348   StructuredDataPlugin::InitializeBasePluginForDebugger(debugger);
1349 
1350   // Get parent command.
1351   auto &interpreter = debugger.GetCommandInterpreter();
1352   llvm::StringRef parent_command_text = "plugin structured-data";
1353   auto parent_command =
1354       interpreter.GetCommandObjectForCommand(parent_command_text);
1355   if (!parent_command) {
1356     // Ut oh, parent failed to create parent command.
1357     // TODO log
1358     return;
1359   }
1360 
1361   auto command_name = "darwin-log";
1362   auto command_sp = CommandObjectSP(new BaseCommand(interpreter));
1363   bool result = parent_command->LoadSubCommand(command_name, command_sp);
1364   if (!result) {
1365     // TODO log it once we setup structured data logging
1366   }
1367 
1368   if (!PluginManager::GetSettingForPlatformPlugin(
1369           debugger, StructuredDataDarwinLogProperties::GetSettingName())) {
1370     const bool is_global_setting = true;
1371     PluginManager::CreateSettingForStructuredDataPlugin(
1372         debugger, GetGlobalProperties().GetValueProperties(),
1373         ConstString("Properties for the darwin-log"
1374                     " plug-in."),
1375         is_global_setting);
1376   }
1377 }
1378 
1379 Status StructuredDataDarwinLog::FilterLaunchInfo(ProcessLaunchInfo &launch_info,
1380                                                  Target *target) {
1381   Status error;
1382 
1383   // If we're not debugging this launched process, there's nothing for us to do
1384   // here.
1385   if (!launch_info.GetFlags().AnySet(eLaunchFlagDebug))
1386     return error;
1387 
1388   // Darwin os_log() support automatically adds debug-level and info-level
1389   // messages when a debugger is attached to a process.  However, with
1390   // integrated support for debugging built into the command-line LLDB, the
1391   // user may specifically set to *not* include debug-level and info-level
1392   // content.  When the user is using the integrated log support, we want to
1393   // put the kabosh on that automatic adding of info and debug level. This is
1394   // done by adding an environment variable to the process on launch. (This
1395   // also means it is not possible to suppress this behavior if attaching to an
1396   // already-running app).
1397   // Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
1398 
1399   // If the target architecture is not one that supports DarwinLog, we have
1400   // nothing to do here.
1401   auto &triple = target ? target->GetArchitecture().GetTriple()
1402                         : launch_info.GetArchitecture().GetTriple();
1403   if (triple.getVendor() != llvm::Triple::Apple) {
1404     return error;
1405   }
1406 
1407   // If DarwinLog is not enabled (either by explicit user command or via the
1408   // auto-enable option), then we have nothing to do.
1409   if (!GetGlobalProperties().GetEnableOnStartup() &&
1410       !s_is_explicitly_enabled) {
1411     // Nothing to do, DarwinLog is not enabled.
1412     return error;
1413   }
1414 
1415   // If we don't have parsed configuration info, that implies we have enable-
1416   // on-startup set up, but we haven't yet attempted to run the enable command.
1417   if (!target) {
1418     // We really can't do this without a target.  We need to be able to get to
1419     // the debugger to get the proper options to do this right.
1420     // TODO log.
1421     error.SetErrorString("requires a target to auto-enable DarwinLog.");
1422     return error;
1423   }
1424 
1425   DebuggerSP debugger_sp = target->GetDebugger().shared_from_this();
1426   auto options_sp = GetGlobalEnableOptions(debugger_sp);
1427   if (!options_sp && debugger_sp) {
1428     options_sp = ParseAutoEnableOptions(error, *debugger_sp.get());
1429     if (!options_sp || !error.Success())
1430       return error;
1431 
1432     // We already parsed the options, save them now so we don't generate them
1433     // again until the user runs the command manually.
1434     SetGlobalEnableOptions(debugger_sp, options_sp);
1435   }
1436 
1437   if (!options_sp->GetEchoToStdErr()) {
1438     // The user doesn't want to see os_log/NSLog messages echo to stderr. That
1439     // mechanism is entirely separate from the DarwinLog support. By default we
1440     // don't want to get it via stderr, because that would be in duplicate of
1441     // the explicit log support here.
1442 
1443     // Here we need to strip out any OS_ACTIVITY_DT_MODE setting to prevent
1444     // echoing of os_log()/NSLog() to stderr in the target program.
1445     launch_info.GetEnvironment().erase("OS_ACTIVITY_DT_MODE");
1446 
1447     // We will also set the env var that tells any downstream launcher from
1448     // adding OS_ACTIVITY_DT_MODE.
1449     launch_info.GetEnvironment()["IDE_DISABLED_OS_ACTIVITY_DT_MODE"] = "1";
1450   }
1451 
1452   // Set the OS_ACTIVITY_MODE env var appropriately to enable/disable debug and
1453   // info level messages.
1454   const char *env_var_value;
1455   if (options_sp->GetIncludeDebugLevel())
1456     env_var_value = "debug";
1457   else if (options_sp->GetIncludeInfoLevel())
1458     env_var_value = "info";
1459   else
1460     env_var_value = "default";
1461 
1462   launch_info.GetEnvironment()["OS_ACTIVITY_MODE"] = env_var_value;
1463 
1464   return error;
1465 }
1466 
1467 bool StructuredDataDarwinLog::InitCompletionHookCallback(
1468     void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
1469     lldb::user_id_t break_loc_id) {
1470   // We hit the init function.  We now want to enqueue our new thread plan,
1471   // which will in turn enqueue a StepOut thread plan. When the StepOut
1472   // finishes and control returns to our new thread plan, that is the time when
1473   // we can execute our logic to enable the logging support.
1474 
1475   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1476   LLDB_LOGF(log, "StructuredDataDarwinLog::%s() called", __FUNCTION__);
1477 
1478   // Get the current thread.
1479   if (!context) {
1480     LLDB_LOGF(log,
1481               "StructuredDataDarwinLog::%s() warning: no context, "
1482               "ignoring",
1483               __FUNCTION__);
1484     return false;
1485   }
1486 
1487   // Get the plugin from the process.
1488   auto process_sp = context->exe_ctx_ref.GetProcessSP();
1489   if (!process_sp) {
1490     LLDB_LOGF(log,
1491               "StructuredDataDarwinLog::%s() warning: invalid "
1492               "process in context, ignoring",
1493               __FUNCTION__);
1494     return false;
1495   }
1496   LLDB_LOGF(log, "StructuredDataDarwinLog::%s() call is for process uid %d",
1497             __FUNCTION__, process_sp->GetUniqueID());
1498 
1499   auto plugin_sp = process_sp->GetStructuredDataPlugin(GetDarwinLogTypeName());
1500   if (!plugin_sp) {
1501     LLDB_LOGF(log,
1502               "StructuredDataDarwinLog::%s() warning: no plugin for "
1503               "feature %s in process uid %u",
1504               __FUNCTION__, GetDarwinLogTypeName().AsCString(),
1505               process_sp->GetUniqueID());
1506     return false;
1507   }
1508 
1509   // Create the callback for when the thread plan completes.
1510   bool called_enable_method = false;
1511   const auto process_uid = process_sp->GetUniqueID();
1512 
1513   std::weak_ptr<StructuredDataPlugin> plugin_wp(plugin_sp);
1514   ThreadPlanCallOnFunctionExit::Callback callback =
1515       [plugin_wp, &called_enable_method, log, process_uid]() {
1516         LLDB_LOGF(log,
1517                   "StructuredDataDarwinLog::post-init callback: "
1518                   "called (process uid %u)",
1519                   process_uid);
1520 
1521         auto strong_plugin_sp = plugin_wp.lock();
1522         if (!strong_plugin_sp) {
1523           LLDB_LOGF(log,
1524                     "StructuredDataDarwinLog::post-init callback: "
1525                     "plugin no longer exists, ignoring (process "
1526                     "uid %u)",
1527                     process_uid);
1528           return;
1529         }
1530         // Make sure we only call it once, just in case the thread plan hits
1531         // the breakpoint twice.
1532         if (!called_enable_method) {
1533           LLDB_LOGF(log,
1534                     "StructuredDataDarwinLog::post-init callback: "
1535                     "calling EnableNow() (process uid %u)",
1536                     process_uid);
1537           static_cast<StructuredDataDarwinLog *>(strong_plugin_sp.get())
1538               ->EnableNow();
1539           called_enable_method = true;
1540         } else {
1541           // Our breakpoint was hit more than once.  Unexpected but no harm
1542           // done.  Log it.
1543           LLDB_LOGF(log,
1544                     "StructuredDataDarwinLog::post-init callback: "
1545                     "skipping EnableNow(), already called by "
1546                     "callback [we hit this more than once] "
1547                     "(process uid %u)",
1548                     process_uid);
1549         }
1550       };
1551 
1552   // Grab the current thread.
1553   auto thread_sp = context->exe_ctx_ref.GetThreadSP();
1554   if (!thread_sp) {
1555     LLDB_LOGF(log,
1556               "StructuredDataDarwinLog::%s() warning: failed to "
1557               "retrieve the current thread from the execution "
1558               "context, nowhere to run the thread plan (process uid "
1559               "%u)",
1560               __FUNCTION__, process_sp->GetUniqueID());
1561     return false;
1562   }
1563 
1564   // Queue the thread plan.
1565   auto thread_plan_sp =
1566       ThreadPlanSP(new ThreadPlanCallOnFunctionExit(*thread_sp, callback));
1567   const bool abort_other_plans = false;
1568   thread_sp->QueueThreadPlan(thread_plan_sp, abort_other_plans);
1569   LLDB_LOGF(log,
1570             "StructuredDataDarwinLog::%s() queuing thread plan on "
1571             "trace library init method entry (process uid %u)",
1572             __FUNCTION__, process_sp->GetUniqueID());
1573 
1574   // We return false here to indicate that it isn't a public stop.
1575   return false;
1576 }
1577 
1578 void StructuredDataDarwinLog::AddInitCompletionHook(Process &process) {
1579   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1580   LLDB_LOGF(log, "StructuredDataDarwinLog::%s() called (process uid %u)",
1581             __FUNCTION__, process.GetUniqueID());
1582 
1583   // Make sure we haven't already done this.
1584   {
1585     std::lock_guard<std::mutex> locker(m_added_breakpoint_mutex);
1586     if (m_added_breakpoint) {
1587       LLDB_LOGF(log,
1588                 "StructuredDataDarwinLog::%s() ignoring request, "
1589                 "breakpoint already set (process uid %u)",
1590                 __FUNCTION__, process.GetUniqueID());
1591       return;
1592     }
1593 
1594     // We're about to do this, don't let anybody else try to do it.
1595     m_added_breakpoint = true;
1596   }
1597 
1598   // Set a breakpoint for the process that will kick in when libtrace has
1599   // finished its initialization.
1600   Target &target = process.GetTarget();
1601 
1602   // Build up the module list.
1603   FileSpecList module_spec_list;
1604   auto module_file_spec =
1605       FileSpec(GetGlobalProperties().GetLoggingModuleName());
1606   module_spec_list.Append(module_file_spec);
1607 
1608   // We aren't specifying a source file set.
1609   FileSpecList *source_spec_list = nullptr;
1610 
1611   const char *func_name = "_libtrace_init";
1612   const lldb::addr_t offset = 0;
1613   const LazyBool skip_prologue = eLazyBoolCalculate;
1614   // This is an internal breakpoint - the user shouldn't see it.
1615   const bool internal = true;
1616   const bool hardware = false;
1617 
1618   auto breakpoint_sp = target.CreateBreakpoint(
1619       &module_spec_list, source_spec_list, func_name, eFunctionNameTypeFull,
1620       eLanguageTypeC, offset, skip_prologue, internal, hardware);
1621   if (!breakpoint_sp) {
1622     // Huh?  Bail here.
1623     LLDB_LOGF(log,
1624               "StructuredDataDarwinLog::%s() failed to set "
1625               "breakpoint in module %s, function %s (process uid %u)",
1626               __FUNCTION__, GetGlobalProperties().GetLoggingModuleName(),
1627               func_name, process.GetUniqueID());
1628     return;
1629   }
1630 
1631   // Set our callback.
1632   breakpoint_sp->SetCallback(InitCompletionHookCallback, nullptr);
1633   m_breakpoint_id = breakpoint_sp->GetID();
1634   LLDB_LOGF(log,
1635             "StructuredDataDarwinLog::%s() breakpoint set in module %s,"
1636             "function %s (process uid %u)",
1637             __FUNCTION__, GetGlobalProperties().GetLoggingModuleName(),
1638             func_name, process.GetUniqueID());
1639 }
1640 
1641 void StructuredDataDarwinLog::DumpTimestamp(Stream &stream,
1642                                             uint64_t timestamp) {
1643   const uint64_t delta_nanos = timestamp - m_first_timestamp_seen;
1644 
1645   const uint64_t hours = delta_nanos / NANOS_PER_HOUR;
1646   uint64_t nanos_remaining = delta_nanos % NANOS_PER_HOUR;
1647 
1648   const uint64_t minutes = nanos_remaining / NANOS_PER_MINUTE;
1649   nanos_remaining = nanos_remaining % NANOS_PER_MINUTE;
1650 
1651   const uint64_t seconds = nanos_remaining / NANOS_PER_SECOND;
1652   nanos_remaining = nanos_remaining % NANOS_PER_SECOND;
1653 
1654   stream.Printf("%02" PRIu64 ":%02" PRIu64 ":%02" PRIu64 ".%09" PRIu64, hours,
1655                 minutes, seconds, nanos_remaining);
1656 }
1657 
1658 size_t
1659 StructuredDataDarwinLog::DumpHeader(Stream &output_stream,
1660                                     const StructuredData::Dictionary &event) {
1661   StreamString stream;
1662 
1663   ProcessSP process_sp = GetProcess();
1664   if (!process_sp) {
1665     // TODO log
1666     return 0;
1667   }
1668 
1669   DebuggerSP debugger_sp =
1670       process_sp->GetTarget().GetDebugger().shared_from_this();
1671   if (!debugger_sp) {
1672     // TODO log
1673     return 0;
1674   }
1675 
1676   auto options_sp = GetGlobalEnableOptions(debugger_sp);
1677   if (!options_sp) {
1678     // TODO log
1679     return 0;
1680   }
1681 
1682   // Check if we should even display a header.
1683   if (!options_sp->GetDisplayAnyHeaderFields())
1684     return 0;
1685 
1686   stream.PutChar('[');
1687 
1688   int header_count = 0;
1689   if (options_sp->GetDisplayTimestampRelative()) {
1690     uint64_t timestamp = 0;
1691     if (event.GetValueForKeyAsInteger("timestamp", timestamp)) {
1692       DumpTimestamp(stream, timestamp);
1693       ++header_count;
1694     }
1695   }
1696 
1697   if (options_sp->GetDisplayActivityChain()) {
1698     llvm::StringRef activity_chain;
1699     if (event.GetValueForKeyAsString("activity-chain", activity_chain) &&
1700         !activity_chain.empty()) {
1701       if (header_count > 0)
1702         stream.PutChar(',');
1703 
1704       // Display the activity chain, from parent-most to child-most activity,
1705       // separated by a colon (:).
1706       stream.PutCString("activity-chain=");
1707       stream.PutCString(activity_chain);
1708       ++header_count;
1709     }
1710   }
1711 
1712   if (options_sp->GetDisplaySubsystem()) {
1713     llvm::StringRef subsystem;
1714     if (event.GetValueForKeyAsString("subsystem", subsystem) &&
1715         !subsystem.empty()) {
1716       if (header_count > 0)
1717         stream.PutChar(',');
1718       stream.PutCString("subsystem=");
1719       stream.PutCString(subsystem);
1720       ++header_count;
1721     }
1722   }
1723 
1724   if (options_sp->GetDisplayCategory()) {
1725     llvm::StringRef category;
1726     if (event.GetValueForKeyAsString("category", category) &&
1727         !category.empty()) {
1728       if (header_count > 0)
1729         stream.PutChar(',');
1730       stream.PutCString("category=");
1731       stream.PutCString(category);
1732       ++header_count;
1733     }
1734   }
1735   stream.PutCString("] ");
1736 
1737   output_stream.PutCString(stream.GetString());
1738 
1739   return stream.GetSize();
1740 }
1741 
1742 size_t StructuredDataDarwinLog::HandleDisplayOfEvent(
1743     const StructuredData::Dictionary &event, Stream &stream) {
1744   // Check the type of the event.
1745   ConstString event_type;
1746   if (!event.GetValueForKeyAsString("type", event_type)) {
1747     // Hmm, we expected to get events that describe what they are.  Continue
1748     // anyway.
1749     return 0;
1750   }
1751 
1752   if (event_type != GetLogEventType())
1753     return 0;
1754 
1755   size_t total_bytes = 0;
1756 
1757   // Grab the message content.
1758   llvm::StringRef message;
1759   if (!event.GetValueForKeyAsString("message", message))
1760     return true;
1761 
1762   // Display the log entry.
1763   const auto len = message.size();
1764 
1765   total_bytes += DumpHeader(stream, event);
1766 
1767   stream.Write(message.data(), len);
1768   total_bytes += len;
1769 
1770   // Add an end of line.
1771   stream.PutChar('\n');
1772   total_bytes += sizeof(char);
1773 
1774   return total_bytes;
1775 }
1776 
1777 void StructuredDataDarwinLog::EnableNow() {
1778   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1779   LLDB_LOGF(log, "StructuredDataDarwinLog::%s() called", __FUNCTION__);
1780 
1781   // Run the enable command.
1782   auto process_sp = GetProcess();
1783   if (!process_sp) {
1784     // Nothing to do.
1785     LLDB_LOGF(log,
1786               "StructuredDataDarwinLog::%s() warning: failed to get "
1787               "valid process, skipping",
1788               __FUNCTION__);
1789     return;
1790   }
1791   LLDB_LOGF(log, "StructuredDataDarwinLog::%s() call is for process uid %u",
1792             __FUNCTION__, process_sp->GetUniqueID());
1793 
1794   // If we have configuration data, we can directly enable it now. Otherwise,
1795   // we need to run through the command interpreter to parse the auto-run
1796   // options (which is the only way we get here without having already-parsed
1797   // configuration data).
1798   DebuggerSP debugger_sp =
1799       process_sp->GetTarget().GetDebugger().shared_from_this();
1800   if (!debugger_sp) {
1801     LLDB_LOGF(log,
1802               "StructuredDataDarwinLog::%s() warning: failed to get "
1803               "debugger shared pointer, skipping (process uid %u)",
1804               __FUNCTION__, process_sp->GetUniqueID());
1805     return;
1806   }
1807 
1808   auto options_sp = GetGlobalEnableOptions(debugger_sp);
1809   if (!options_sp) {
1810     // We haven't run the enable command yet.  Just do that now, it'll take
1811     // care of the rest.
1812     auto &interpreter = debugger_sp->GetCommandInterpreter();
1813     const bool success = RunEnableCommand(interpreter);
1814     if (log) {
1815       if (success)
1816         LLDB_LOGF(log,
1817                   "StructuredDataDarwinLog::%s() ran enable command "
1818                   "successfully for (process uid %u)",
1819                   __FUNCTION__, process_sp->GetUniqueID());
1820       else
1821         LLDB_LOGF(log,
1822                   "StructuredDataDarwinLog::%s() error: running "
1823                   "enable command failed (process uid %u)",
1824                   __FUNCTION__, process_sp->GetUniqueID());
1825     }
1826     // Report failures to the debugger error stream.
1827     auto error_stream_sp = debugger_sp->GetAsyncErrorStream();
1828     if (error_stream_sp) {
1829       error_stream_sp->Printf("failed to configure DarwinLog "
1830                               "support\n");
1831       error_stream_sp->Flush();
1832     }
1833     return;
1834   }
1835 
1836   // We've previously been enabled. We will re-enable now with the previously
1837   // specified options.
1838   auto config_sp = options_sp->BuildConfigurationData(true);
1839   if (!config_sp) {
1840     LLDB_LOGF(log,
1841               "StructuredDataDarwinLog::%s() warning: failed to "
1842               "build configuration data for enable options, skipping "
1843               "(process uid %u)",
1844               __FUNCTION__, process_sp->GetUniqueID());
1845     return;
1846   }
1847 
1848   // We can run it directly.
1849   // Send configuration to the feature by way of the process.
1850   const Status error =
1851       process_sp->ConfigureStructuredData(GetDarwinLogTypeName(), config_sp);
1852 
1853   // Report results.
1854   if (!error.Success()) {
1855     LLDB_LOGF(log,
1856               "StructuredDataDarwinLog::%s() "
1857               "ConfigureStructuredData() call failed "
1858               "(process uid %u): %s",
1859               __FUNCTION__, process_sp->GetUniqueID(), error.AsCString());
1860     auto error_stream_sp = debugger_sp->GetAsyncErrorStream();
1861     if (error_stream_sp) {
1862       error_stream_sp->Printf("failed to configure DarwinLog "
1863                               "support: %s\n",
1864                               error.AsCString());
1865       error_stream_sp->Flush();
1866     }
1867     m_is_enabled = false;
1868   } else {
1869     m_is_enabled = true;
1870     LLDB_LOGF(log,
1871               "StructuredDataDarwinLog::%s() success via direct "
1872               "configuration (process uid %u)",
1873               __FUNCTION__, process_sp->GetUniqueID());
1874   }
1875 }
1876