crash_keys.cc 4.2 KB

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