crash_keys.cc 4.8 KB

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