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