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