crash_keys.cc 4.7 KB

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