crash_keys.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/string_split.h"
  13. #include "base/strings/stringprintf.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/browser/javascript_environment.h"
  19. #include "shell/common/electron_constants.h"
  20. #include "shell/common/node_includes.h"
  21. #include "shell/common/options_switches.h"
  22. #include "shell/common/process_util.h"
  23. #include "third_party/crashpad/crashpad/client/annotation.h"
  24. namespace electron::crash_keys {
  25. namespace {
  26. constexpr size_t kMaxCrashKeyValueSize = 20320;
  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. static_assert(kMaxCrashKeyNameLength <= crashpad::Annotation::kNameMaxLength,
  42. "max crash key name length above what crashpad supports");
  43. void SetCrashKey(const std::string& key, const std::string& value) {
  44. // Chrome DCHECK()s if we try to set an annotation with a name longer than
  45. // the max.
  46. if (key.size() >= kMaxCrashKeyNameLength) {
  47. node::Environment* env =
  48. node::Environment::GetCurrent(JavascriptEnvironment::GetIsolate());
  49. EmitWarning(
  50. env,
  51. base::StringPrintf("The crash key name, \"%s\", is longer than %" PRIu32
  52. " bytes, ignoring it.",
  53. key.c_str(), kMaxCrashKeyNameLength),
  54. "electron");
  55. return;
  56. }
  57. auto& crash_key_names = GetExtraCrashKeyNames();
  58. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  59. if (iter == crash_key_names.end()) {
  60. crash_key_names.emplace_back(key);
  61. GetExtraCrashKeys().emplace_back(crash_key_names.back().c_str());
  62. iter = crash_key_names.end() - 1;
  63. }
  64. GetExtraCrashKeys()[iter - crash_key_names.begin()].Set(value);
  65. }
  66. void ClearCrashKey(const std::string& key) {
  67. const 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. GetExtraCrashKeys()[iter - crash_key_names.begin()].Clear();
  71. }
  72. }
  73. void GetCrashKeys(std::map<std::string, std::string>* keys) {
  74. const auto& crash_key_names = GetExtraCrashKeyNames();
  75. const auto& crash_keys = GetExtraCrashKeys();
  76. int i = 0;
  77. for (const auto& key : crash_key_names) {
  78. const auto& value = crash_keys[i++];
  79. if (value.is_set()) {
  80. keys->emplace(key, value.value());
  81. }
  82. }
  83. }
  84. namespace {
  85. bool IsRunningAsNode() {
  86. return electron::fuses::IsRunAsNodeEnabled() &&
  87. base::Environment::Create()->HasVar(electron::kRunAsNode);
  88. }
  89. } // namespace
  90. void SetCrashKeysFromCommandLine(const base::CommandLine& command_line) {
  91. // NB. this is redundant with the 'ptype' key that //components/crash
  92. // reports; it's present for backwards compatibility.
  93. static crash_reporter::CrashKeyString<16> process_type_key("process_type");
  94. if (IsRunningAsNode()) {
  95. process_type_key.Set("node");
  96. } else {
  97. std::string process_type =
  98. command_line.GetSwitchValueASCII(::switches::kProcessType);
  99. if (process_type.empty()) {
  100. process_type_key.Set("browser");
  101. } else {
  102. process_type_key.Set(process_type);
  103. }
  104. }
  105. }
  106. void SetPlatformCrashKey() {
  107. // TODO(nornagon): this is redundant with the 'plat' key that
  108. // //components/crash already includes. Remove it.
  109. static crash_reporter::CrashKeyString<8> platform_key("platform");
  110. #if BUILDFLAG(IS_WIN)
  111. platform_key.Set("win32");
  112. #elif BUILDFLAG(IS_MAC)
  113. platform_key.Set("darwin");
  114. #elif BUILDFLAG(IS_LINUX)
  115. platform_key.Set("linux");
  116. #else
  117. platform_key.Set("unknown");
  118. #endif
  119. }
  120. } // namespace electron::crash_keys