crash_keys.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/common/electron_constants.h"
  17. #include "shell/common/options_switches.h"
  18. #include "third_party/crashpad/crashpad/client/annotation.h"
  19. #include "gin/wrappable.h"
  20. #include "shell/browser/api/electron_api_app.h"
  21. #include "shell/browser/api/electron_api_auto_updater.h"
  22. #include "shell/browser/api/electron_api_browser_view.h"
  23. #include "shell/browser/api/electron_api_cookies.h"
  24. #include "shell/browser/api/electron_api_data_pipe_holder.h"
  25. #include "shell/browser/api/electron_api_debugger.h"
  26. #include "shell/browser/api/electron_api_desktop_capturer.h"
  27. #include "shell/browser/api/electron_api_download_item.h"
  28. #include "shell/browser/api/electron_api_global_shortcut.h"
  29. #include "shell/browser/api/electron_api_in_app_purchase.h"
  30. #include "shell/browser/api/electron_api_menu.h"
  31. #include "shell/browser/api/electron_api_native_theme.h"
  32. #include "shell/browser/api/electron_api_net_log.h"
  33. #include "shell/browser/api/electron_api_notification.h"
  34. #include "shell/browser/api/electron_api_power_monitor.h"
  35. #include "shell/browser/api/electron_api_power_save_blocker.h"
  36. #include "shell/browser/api/electron_api_protocol.h"
  37. #include "shell/browser/api/electron_api_service_worker_context.h"
  38. #include "shell/browser/api/electron_api_session.h"
  39. #include "shell/browser/api/electron_api_system_preferences.h"
  40. #include "shell/browser/api/electron_api_tray.h"
  41. #include "shell/browser/api/electron_api_url_loader.h"
  42. #include "shell/browser/api/electron_api_web_contents.h"
  43. #include "shell/browser/api/electron_api_web_frame_main.h"
  44. #include "shell/browser/api/electron_api_web_request.h"
  45. #include "shell/browser/api/event.h"
  46. #include "shell/common/api/electron_api_native_image.h"
  47. namespace electron {
  48. namespace crash_keys {
  49. namespace {
  50. #if defined(OS_LINUX)
  51. // Breakpad has a flawed system of calculating the number of chunks
  52. // we add 127 bytes to force an extra chunk
  53. constexpr size_t kMaxCrashKeyValueSize = 20479;
  54. #else
  55. constexpr size_t kMaxCrashKeyValueSize = 20320;
  56. #endif
  57. static_assert(kMaxCrashKeyValueSize < crashpad::Annotation::kValueMaxSize,
  58. "max crash key value length above what crashpad supports");
  59. using ExtraCrashKeys =
  60. std::deque<crash_reporter::CrashKeyString<kMaxCrashKeyValueSize>>;
  61. ExtraCrashKeys& GetExtraCrashKeys() {
  62. static base::NoDestructor<ExtraCrashKeys> extra_keys;
  63. return *extra_keys;
  64. }
  65. std::deque<std::string>& GetExtraCrashKeyNames() {
  66. static base::NoDestructor<std::deque<std::string>> crash_key_names;
  67. return *crash_key_names;
  68. }
  69. } // namespace
  70. constexpr uint32_t kMaxCrashKeyNameLength = 40;
  71. #if defined(OS_LINUX)
  72. static_assert(kMaxCrashKeyNameLength <=
  73. crash_reporter::internal::kCrashKeyStorageKeySize,
  74. "max crash key name length above what breakpad supports");
  75. #else
  76. static_assert(kMaxCrashKeyNameLength <= crashpad::Annotation::kNameMaxLength,
  77. "max crash key name length above what crashpad supports");
  78. #endif
  79. void SetCrashKey(const std::string& key, const std::string& value) {
  80. // Chrome DCHECK()s if we try to set an annotation with a name longer than
  81. // the max.
  82. // TODO(nornagon): warn the developer (via console.warn) when this happens.
  83. if (key.size() >= kMaxCrashKeyNameLength)
  84. return;
  85. auto& crash_key_names = GetExtraCrashKeyNames();
  86. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  87. if (iter == crash_key_names.end()) {
  88. crash_key_names.emplace_back(key);
  89. GetExtraCrashKeys().emplace_back(crash_key_names.back().c_str());
  90. iter = crash_key_names.end() - 1;
  91. }
  92. GetExtraCrashKeys()[iter - crash_key_names.begin()].Set(value);
  93. }
  94. void ClearCrashKey(const std::string& key) {
  95. const auto& crash_key_names = GetExtraCrashKeyNames();
  96. auto iter = std::find(crash_key_names.begin(), crash_key_names.end(), key);
  97. if (iter != crash_key_names.end()) {
  98. GetExtraCrashKeys()[iter - crash_key_names.begin()].Clear();
  99. }
  100. }
  101. void GetCrashKeys(std::map<std::string, std::string>* keys) {
  102. const auto& crash_key_names = GetExtraCrashKeyNames();
  103. const auto& crash_keys = GetExtraCrashKeys();
  104. int i = 0;
  105. for (const auto& key : crash_key_names) {
  106. const auto& value = crash_keys[i++];
  107. if (value.is_set()) {
  108. keys->emplace(key, value.value());
  109. }
  110. }
  111. }
  112. namespace {
  113. bool IsRunningAsNode() {
  114. #if BUILDFLAG(ENABLE_RUN_AS_NODE)
  115. if (!electron::fuses::IsRunAsNodeEnabled())
  116. return false;
  117. return base::Environment::Create()->HasVar(electron::kRunAsNode);
  118. #else
  119. return false;
  120. #endif
  121. }
  122. } // namespace
  123. void SetCrashKeysFromCommandLine(const base::CommandLine& command_line) {
  124. #if defined(OS_LINUX)
  125. if (command_line.HasSwitch(switches::kGlobalCrashKeys)) {
  126. std::vector<std::pair<std::string, std::string>> global_crash_keys;
  127. base::SplitStringIntoKeyValuePairs(
  128. command_line.GetSwitchValueASCII(switches::kGlobalCrashKeys), '=', ',',
  129. &global_crash_keys);
  130. for (const auto& pair : global_crash_keys) {
  131. SetCrashKey(pair.first, pair.second);
  132. }
  133. }
  134. #endif
  135. // NB. this is redundant with the 'ptype' key that //components/crash
  136. // reports; it's present for backwards compatibility.
  137. static crash_reporter::CrashKeyString<16> process_type_key("process_type");
  138. if (IsRunningAsNode()) {
  139. process_type_key.Set("node");
  140. } else {
  141. std::string process_type =
  142. command_line.GetSwitchValueASCII(::switches::kProcessType);
  143. if (process_type.empty()) {
  144. process_type_key.Set("browser");
  145. } else {
  146. process_type_key.Set(process_type);
  147. }
  148. }
  149. }
  150. void SetPlatformCrashKey() {
  151. // TODO(nornagon): this is redundant with the 'plat' key that
  152. // //components/crash already includes. Remove it.
  153. static crash_reporter::CrashKeyString<8> platform_key("platform");
  154. #if defined(OS_WIN)
  155. platform_key.Set("win32");
  156. #elif defined(OS_MAC)
  157. platform_key.Set("darwin");
  158. #elif defined(OS_LINUX)
  159. platform_key.Set("linux");
  160. #else
  161. platform_key.Set("unknown");
  162. #endif
  163. }
  164. std::string GetCrashValueForGinWrappable(gin::WrapperInfo* info) {
  165. std::string crash_location;
  166. // Adds a breadcrumb for crashes within gin::WrappableBase::SecondWeakCallback
  167. // (see patch: add_gin_wrappable_crash_key.patch)
  168. // Compares the pointers for the kWrapperInfo within SecondWeakCallback
  169. // with the wrapper info from classes that use gin::Wrappable and
  170. // could potentially retain a reference after deletion.
  171. if (info == &electron::api::WebContents::kWrapperInfo)
  172. crash_location = "WebContents";
  173. else if (info == &electron::api::BrowserView::kWrapperInfo)
  174. crash_location = "BrowserView";
  175. else if (info == &electron::api::Notification::kWrapperInfo)
  176. crash_location = "Notification";
  177. else if (info == &electron::api::Cookies::kWrapperInfo)
  178. crash_location = "Cookies";
  179. #if BUILDFLAG(ENABLE_DESKTOP_CAPTURER)
  180. else if (info == &electron::api::DesktopCapturer::kWrapperInfo)
  181. crash_location = "DesktopCapturer";
  182. #endif
  183. else if (info == &electron::api::NetLog::kWrapperInfo)
  184. crash_location = "NetLog";
  185. else if (info == &electron::api::NativeImage::kWrapperInfo)
  186. crash_location = "NativeImage";
  187. else if (info == &electron::api::Menu::kWrapperInfo)
  188. crash_location = "Menu";
  189. else if (info == &electron::api::PowerMonitor::kWrapperInfo)
  190. crash_location = "PowerMonitor";
  191. else if (info == &electron::api::Protocol::kWrapperInfo)
  192. crash_location = "Protocol";
  193. else if (info == &electron::api::ServiceWorkerContext::kWrapperInfo)
  194. crash_location = "ServiceWorkerContext";
  195. else if (info == &electron::api::WebFrameMain::kWrapperInfo)
  196. crash_location = "WebFrameMain";
  197. else if (info == &electron::api::WebRequest::kWrapperInfo)
  198. crash_location = "WebRequest";
  199. else if (info == &electron::api::SystemPreferences::kWrapperInfo)
  200. crash_location = "SystemPreferences";
  201. else if (info == &electron::api::Session::kWrapperInfo)
  202. crash_location = "Session";
  203. else if (info == &electron::api::DownloadItem::kWrapperInfo)
  204. crash_location = "DownloadItem";
  205. else if (info == &electron::api::NativeTheme::kWrapperInfo)
  206. crash_location = "NativeTheme";
  207. else if (info == &electron::api::Debugger::kWrapperInfo)
  208. crash_location = "Debugger";
  209. else if (info == &electron::api::GlobalShortcut::kWrapperInfo)
  210. crash_location = "GlobalShortcut";
  211. else if (info == &electron::api::InAppPurchase::kWrapperInfo)
  212. crash_location = "InAppPurchase";
  213. else if (info == &electron::api::Tray::kWrapperInfo)
  214. crash_location = "Tray";
  215. else if (info == &electron::api::DataPipeHolder::kWrapperInfo)
  216. crash_location = "DataPipeHolder";
  217. else if (info == &electron::api::AutoUpdater::kWrapperInfo)
  218. crash_location = "AutoUpdater";
  219. else if (info == &electron::api::SimpleURLLoaderWrapper::kWrapperInfo)
  220. crash_location = "SimpleURLLoaderWrapper";
  221. else if (info == &gin_helper::Event::kWrapperInfo)
  222. crash_location = "Event";
  223. else if (info == &electron::api::PowerSaveBlocker::kWrapperInfo)
  224. crash_location = "PowerSaveBlocker";
  225. else if (info == &electron::api::App::kWrapperInfo)
  226. crash_location = "App";
  227. else
  228. crash_location =
  229. "Deleted kWrapperInfo does not match listed component. Please review "
  230. "listed crash keys.";
  231. return crash_location;
  232. }
  233. } // namespace crash_keys
  234. } // namespace electron