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