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