crash_keys.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <utility>
  7. #include <vector>
  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 {
  23. namespace crash_keys {
  24. namespace {
  25. #if defined(OS_LINUX)
  26. // Breakpad has a flawed system of calculating the number of chunks
  27. // we add 127 bytes to force an extra chunk
  28. constexpr size_t kMaxCrashKeyValueSize = 20479;
  29. #else
  30. constexpr size_t kMaxCrashKeyValueSize = 20320;
  31. #endif
  32. static_assert(kMaxCrashKeyValueSize < crashpad::Annotation::kValueMaxSize,
  33. "max crash key value length above what crashpad supports");
  34. using ExtraCrashKeys =
  35. std::deque<crash_reporter::CrashKeyString<kMaxCrashKeyValueSize>>;
  36. ExtraCrashKeys& GetExtraCrashKeys() {
  37. static base::NoDestructor<ExtraCrashKeys> extra_keys;
  38. return *extra_keys;
  39. }
  40. std::deque<std::string>& GetExtraCrashKeyNames() {
  41. static base::NoDestructor<std::deque<std::string>> crash_key_names;
  42. return *crash_key_names;
  43. }
  44. } // namespace
  45. constexpr uint32_t kMaxCrashKeyNameLength = 40;
  46. #if defined(OS_LINUX)
  47. static_assert(kMaxCrashKeyNameLength <=
  48. crash_reporter::internal::kCrashKeyStorageKeySize,
  49. "max crash key name length above what breakpad supports");
  50. #else
  51. static_assert(kMaxCrashKeyNameLength <= crashpad::Annotation::kNameMaxLength,
  52. "max crash key name length above what crashpad supports");
  53. #endif
  54. void SetCrashKey(const std::string& key, const std::string& value) {
  55. // Chrome DCHECK()s if we try to set an annotation with a name longer than
  56. // the max.
  57. if (key.size() >= kMaxCrashKeyNameLength) {
  58. node::Environment* env =
  59. node::Environment::GetCurrent(JavascriptEnvironment::GetIsolate());
  60. EmitWarning(env,
  61. "The crash key name, \"" + key + "\", is longer than " +
  62. std::to_string(kMaxCrashKeyNameLength) +
  63. " bytes, ignoring it.",
  64. "electron");
  65. return;
  66. }
  67. auto& crash_key_names = GetExtraCrashKeyNames();
  68. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  69. if (iter == crash_key_names.end()) {
  70. crash_key_names.emplace_back(key);
  71. GetExtraCrashKeys().emplace_back(crash_key_names.back().c_str());
  72. iter = crash_key_names.end() - 1;
  73. }
  74. GetExtraCrashKeys()[iter - crash_key_names.begin()].Set(value);
  75. }
  76. void ClearCrashKey(const std::string& key) {
  77. const auto& crash_key_names = GetExtraCrashKeyNames();
  78. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  79. if (iter != crash_key_names.end()) {
  80. GetExtraCrashKeys()[iter - crash_key_names.begin()].Clear();
  81. }
  82. }
  83. void GetCrashKeys(std::map<std::string, std::string>* keys) {
  84. const auto& crash_key_names = GetExtraCrashKeyNames();
  85. const auto& crash_keys = GetExtraCrashKeys();
  86. int i = 0;
  87. for (const auto& key : crash_key_names) {
  88. const auto& value = crash_keys[i++];
  89. if (value.is_set()) {
  90. keys->emplace(key, value.value());
  91. }
  92. }
  93. }
  94. namespace {
  95. bool IsRunningAsNode() {
  96. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  97. if (!electron::fuses::IsRunAsNodeEnabled())
  98. return false;
  99. return base::Environment::Create()->HasVar(electron::kRunAsNode);
  100. #else
  101. return false;
  102. #endif
  103. }
  104. } // namespace
  105. void SetCrashKeysFromCommandLine(const base::CommandLine& command_line) {
  106. #if defined(OS_LINUX)
  107. if (command_line.HasSwitch(switches::kGlobalCrashKeys)) {
  108. std::vector<std::pair<std::string, std::string>> global_crash_keys;
  109. base::SplitStringIntoKeyValuePairs(
  110. command_line.GetSwitchValueASCII(switches::kGlobalCrashKeys), '=', ',',
  111. &global_crash_keys);
  112. for (const auto& pair : global_crash_keys) {
  113. SetCrashKey(pair.first, pair.second);
  114. }
  115. }
  116. #endif
  117. // NB. this is redundant with the 'ptype' key that //components/crash
  118. // reports; it's present for backwards compatibility.
  119. static crash_reporter::CrashKeyString<16> process_type_key("process_type");
  120. if (IsRunningAsNode()) {
  121. process_type_key.Set("node");
  122. } else {
  123. std::string process_type =
  124. command_line.GetSwitchValueASCII(::switches::kProcessType);
  125. if (process_type.empty()) {
  126. process_type_key.Set("browser");
  127. } else {
  128. process_type_key.Set(process_type);
  129. }
  130. }
  131. }
  132. void SetPlatformCrashKey() {
  133. // TODO(nornagon): this is redundant with the 'plat' key that
  134. // //components/crash already includes. Remove it.
  135. static crash_reporter::CrashKeyString<8> platform_key("platform");
  136. #if defined(OS_WIN)
  137. platform_key.Set("win32");
  138. #elif defined(OS_MAC)
  139. platform_key.Set("darwin");
  140. #elif defined(OS_LINUX)
  141. platform_key.Set("linux");
  142. #else
  143. platform_key.Set("unknown");
  144. #endif
  145. }
  146. } // namespace crash_keys
  147. } // namespace electron