crash_keys.cc 4.9 KB

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