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