crash_keys.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2020 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/crash_keys.h"
  5. #include <deque>
  6. #include <map>
  7. #include <string>
  8. #include "base/command_line.h"
  9. #include "base/environment.h"
  10. #include "base/no_destructor.h"
  11. #include "base/strings/string_split.h"
  12. #include "components/crash/core/common/crash_key.h"
  13. #include "content/public/common/content_switches.h"
  14. #include "electron/buildflags/buildflags.h"
  15. #include "electron/fuses.h"
  16. #include "shell/browser/javascript_environment.h"
  17. #include "shell/common/electron_constants.h"
  18. #include "shell/common/node_includes.h"
  19. #include "shell/common/options_switches.h"
  20. #include "shell/common/process_util.h"
  21. #include "third_party/crashpad/crashpad/client/annotation.h"
  22. namespace electron::crash_keys {
  23. namespace {
  24. constexpr size_t kMaxCrashKeyValueSize = 20320;
  25. static_assert(kMaxCrashKeyValueSize < crashpad::Annotation::kValueMaxSize,
  26. "max crash key value length above what crashpad supports");
  27. using ExtraCrashKeys =
  28. std::deque<crash_reporter::CrashKeyString<kMaxCrashKeyValueSize>>;
  29. ExtraCrashKeys& GetExtraCrashKeys() {
  30. static base::NoDestructor<ExtraCrashKeys> extra_keys;
  31. return *extra_keys;
  32. }
  33. std::deque<std::string>& GetExtraCrashKeyNames() {
  34. static base::NoDestructor<std::deque<std::string>> crash_key_names;
  35. return *crash_key_names;
  36. }
  37. } // namespace
  38. constexpr uint32_t kMaxCrashKeyNameLength = 40;
  39. static_assert(kMaxCrashKeyNameLength <= crashpad::Annotation::kNameMaxLength,
  40. "max crash key name length above what crashpad supports");
  41. void SetCrashKey(const std::string& key, const std::string& value) {
  42. // Chrome DCHECK()s if we try to set an annotation with a name longer than
  43. // the max.
  44. if (key.size() >= kMaxCrashKeyNameLength) {
  45. node::Environment* env =
  46. node::Environment::GetCurrent(JavascriptEnvironment::GetIsolate());
  47. EmitWarning(env,
  48. "The crash key name, \"" + key + "\", is longer than " +
  49. std::to_string(kMaxCrashKeyNameLength) +
  50. " bytes, ignoring it.",
  51. "electron");
  52. return;
  53. }
  54. auto& crash_key_names = GetExtraCrashKeyNames();
  55. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  56. if (iter == crash_key_names.end()) {
  57. crash_key_names.emplace_back(key);
  58. GetExtraCrashKeys().emplace_back(crash_key_names.back().c_str());
  59. iter = crash_key_names.end() - 1;
  60. }
  61. GetExtraCrashKeys()[iter - crash_key_names.begin()].Set(value);
  62. }
  63. void ClearCrashKey(const std::string& key) {
  64. const auto& crash_key_names = GetExtraCrashKeyNames();
  65. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  66. if (iter != crash_key_names.end()) {
  67. GetExtraCrashKeys()[iter - crash_key_names.begin()].Clear();
  68. }
  69. }
  70. void GetCrashKeys(std::map<std::string, std::string>* keys) {
  71. const auto& crash_key_names = GetExtraCrashKeyNames();
  72. const auto& crash_keys = GetExtraCrashKeys();
  73. int i = 0;
  74. for (const auto& key : crash_key_names) {
  75. const auto& value = crash_keys[i++];
  76. if (value.is_set()) {
  77. keys->emplace(key, value.value());
  78. }
  79. }
  80. }
  81. namespace {
  82. bool IsRunningAsNode() {
  83. return electron::fuses::IsRunAsNodeEnabled() &&
  84. base::Environment::Create()->HasVar(electron::kRunAsNode);
  85. }
  86. } // namespace
  87. void SetCrashKeysFromCommandLine(const base::CommandLine& command_line) {
  88. // NB. this is redundant with the 'ptype' key that //components/crash
  89. // reports; it's present for backwards compatibility.
  90. static crash_reporter::CrashKeyString<16> process_type_key("process_type");
  91. if (IsRunningAsNode()) {
  92. process_type_key.Set("node");
  93. } else {
  94. std::string process_type =
  95. command_line.GetSwitchValueASCII(::switches::kProcessType);
  96. if (process_type.empty()) {
  97. process_type_key.Set("browser");
  98. } else {
  99. process_type_key.Set(process_type);
  100. }
  101. }
  102. }
  103. void SetPlatformCrashKey() {
  104. // TODO(nornagon): this is redundant with the 'plat' key that
  105. // //components/crash already includes. Remove it.
  106. static crash_reporter::CrashKeyString<8> platform_key("platform");
  107. #if BUILDFLAG(IS_WIN)
  108. platform_key.Set("win32");
  109. #elif BUILDFLAG(IS_MAC)
  110. platform_key.Set("darwin");
  111. #elif BUILDFLAG(IS_LINUX)
  112. platform_key.Set("linux");
  113. #else
  114. platform_key.Set("unknown");
  115. #endif
  116. }
  117. } // namespace electron::crash_keys