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