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