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