CrashHandler.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import platform
  4. import traceback
  5. import faulthandler
  6. import tempfile
  7. import os
  8. import os.path
  9. import uuid
  10. import json
  11. import locale
  12. from typing import cast, Any
  13. try:
  14. from sentry_sdk.hub import Hub
  15. from sentry_sdk.utils import event_from_exception
  16. from sentry_sdk import configure_scope, add_breadcrumb
  17. with_sentry_sdk = True
  18. except ImportError:
  19. with_sentry_sdk = False
  20. from PyQt6.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, QUrl
  21. from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QTextEdit, QGroupBox, QCheckBox, QPushButton
  22. from PyQt6.QtGui import QDesktopServices
  23. from UM.Application import Application
  24. from UM.Logger import Logger
  25. from UM.View.GL.OpenGL import OpenGL
  26. from UM.i18n import i18nCatalog
  27. from UM.Resources import Resources
  28. from cura import ApplicationMetadata
  29. catalog = i18nCatalog("cura")
  30. home_dir = os.path.expanduser("~")
  31. MYPY = False
  32. if MYPY:
  33. CuraDebugMode = False
  34. else:
  35. try:
  36. from cura.CuraVersion import CuraDebugMode
  37. except ImportError:
  38. CuraDebugMode = False # [CodeStyle: Reflecting imported value]
  39. # List of exceptions that should not be considered "fatal" and abort the program.
  40. # These are primarily some exception types that we simply skip
  41. skip_exception_types = [
  42. SystemExit,
  43. KeyboardInterrupt,
  44. GeneratorExit
  45. ]
  46. class CrashHandler:
  47. def __init__(self, exception_type, value, tb, has_started = True):
  48. self.exception_type = exception_type
  49. self.value = value
  50. self.traceback = tb
  51. self.has_started = has_started
  52. self.dialog = None # Don't create a QDialog before there is a QApplication
  53. self.cura_version = None
  54. self.cura_locale = None
  55. Logger.log("c", "An uncaught error has occurred!")
  56. for line in traceback.format_exception(exception_type, value, tb):
  57. for part in line.rstrip("\n").split("\n"):
  58. Logger.log("c", part)
  59. self.data = {}
  60. # If Cura has fully started, we only show fatal errors.
  61. # If Cura has not fully started yet, we always show the early crash dialog. Otherwise, Cura will just crash
  62. # without any information.
  63. if has_started and exception_type in skip_exception_types:
  64. return
  65. if with_sentry_sdk:
  66. with configure_scope() as scope:
  67. scope.set_tag("during_startup", not has_started)
  68. if not has_started:
  69. self._send_report_checkbox = None
  70. self.early_crash_dialog = self._createEarlyCrashDialog()
  71. self.dialog = QDialog()
  72. self._createDialog()
  73. @staticmethod
  74. def pruneSensitiveData(obj: Any) -> Any:
  75. if isinstance(obj, str):
  76. return obj.replace("\\\\", "\\").replace(home_dir, "<user_home>")
  77. if isinstance(obj, list):
  78. return [CrashHandler.pruneSensitiveData(item) for item in obj]
  79. if isinstance(obj, dict):
  80. return {k: CrashHandler.pruneSensitiveData(v) for k, v in obj.items()}
  81. return obj
  82. @staticmethod
  83. def sentryBeforeSend(event, hint):
  84. return CrashHandler.pruneSensitiveData(event)
  85. def _createEarlyCrashDialog(self):
  86. dialog = QDialog()
  87. dialog.setMinimumWidth(500)
  88. dialog.setMinimumHeight(170)
  89. dialog.setWindowTitle(catalog.i18nc("@title:window", "Cura can't start"))
  90. dialog.finished.connect(self._closeEarlyCrashDialog)
  91. layout = QVBoxLayout(dialog)
  92. label = QLabel()
  93. label.setText(catalog.i18nc("@label crash message", """<p><b>Oops, Ultimaker Cura has encountered something that doesn't seem right.</p></b>
  94. <p>We encountered an unrecoverable error during start up. It was possibly caused by some incorrect configuration files. We suggest to backup and reset your configuration.</p>
  95. <p>Backups can be found in the configuration folder.</p>
  96. <p>Please send us this Crash Report to fix the problem.</p>
  97. """))
  98. label.setWordWrap(True)
  99. layout.addWidget(label)
  100. # "send report" check box and show details
  101. self._send_report_checkbox = QCheckBox(catalog.i18nc("@action:button", "Send crash report to Ultimaker"), dialog)
  102. self._send_report_checkbox.setChecked(True)
  103. show_details_button = QPushButton(catalog.i18nc("@action:button", "Show detailed crash report"), dialog)
  104. show_details_button.setMaximumWidth(200)
  105. show_details_button.clicked.connect(self._showDetailedReport)
  106. show_configuration_folder_button = QPushButton(catalog.i18nc("@action:button", "Show configuration folder"), dialog)
  107. show_configuration_folder_button.setMaximumWidth(200)
  108. show_configuration_folder_button.clicked.connect(self._showConfigurationFolder)
  109. layout.addWidget(self._send_report_checkbox)
  110. layout.addWidget(show_details_button)
  111. layout.addWidget(show_configuration_folder_button)
  112. # "backup and start clean" and "close" buttons
  113. buttons = QDialogButtonBox()
  114. buttons.addButton(QDialogButtonBox.StandardButton.Close)
  115. buttons.addButton(catalog.i18nc("@action:button", "Backup and Reset Configuration"), QDialogButtonBox.ButtonRole.AcceptRole)
  116. buttons.rejected.connect(self._closeEarlyCrashDialog)
  117. buttons.accepted.connect(self._backupAndStartClean)
  118. layout.addWidget(buttons)
  119. return dialog
  120. def _closeEarlyCrashDialog(self):
  121. if self._send_report_checkbox.isChecked():
  122. self._sendCrashReport()
  123. os._exit(1)
  124. def _backupAndStartClean(self):
  125. """Backup the current resource directories and create clean ones."""
  126. Resources.factoryReset()
  127. self.early_crash_dialog.close()
  128. def _showConfigurationFolder(self):
  129. path = Resources.getConfigStoragePath()
  130. QDesktopServices.openUrl(QUrl.fromLocalFile( path ))
  131. def _showDetailedReport(self):
  132. self.dialog.exec()
  133. def _createDialog(self):
  134. """Creates a modal dialog."""
  135. self.dialog.setMinimumWidth(640)
  136. self.dialog.setMinimumHeight(640)
  137. self.dialog.setWindowTitle(catalog.i18nc("@title:window", "Crash Report"))
  138. # if the application has not fully started, this will be a detailed report dialog which should not
  139. # close the application when it's closed.
  140. if self.has_started:
  141. self.dialog.finished.connect(self._close)
  142. layout = QVBoxLayout(self.dialog)
  143. layout.addWidget(self._messageWidget())
  144. layout.addWidget(self._informationWidget())
  145. layout.addWidget(self._exceptionInfoWidget())
  146. layout.addWidget(self._logInfoWidget())
  147. layout.addWidget(self._buttonsWidget())
  148. def _close(self):
  149. os._exit(1)
  150. def _messageWidget(self):
  151. label = QLabel()
  152. label.setText(catalog.i18nc("@label crash message", """<p><b>A fatal error has occurred in Cura. Please send us this Crash Report to fix the problem</p></b>
  153. <p>Please use the "Send report" button to post a bug report automatically to our servers</p>
  154. """))
  155. return label
  156. def _informationWidget(self):
  157. group = QGroupBox()
  158. group.setTitle(catalog.i18nc("@title:groupbox", "System information"))
  159. layout = QVBoxLayout()
  160. label = QLabel()
  161. try:
  162. from UM.Application import Application
  163. self.cura_version = Application.getInstance().getVersion()
  164. self.cura_locale = Application.getInstance().getPreferences().getValue("general/language")
  165. except:
  166. self.cura_version = catalog.i18nc("@label unknown version of Cura", "Unknown")
  167. self.cura_locale = "??_??"
  168. self.data["cura_version"] = self.cura_version
  169. self.data["os"] = {"type": platform.system(), "version": platform.version()}
  170. self.data["qt_version"] = QT_VERSION_STR
  171. self.data["pyqt_version"] = PYQT_VERSION_STR
  172. self.data["locale_os"] = locale.getlocale(locale.LC_MESSAGES)[0] if hasattr(locale, "LC_MESSAGES") else \
  173. locale.getdefaultlocale()[0]
  174. self.data["locale_cura"] = self.cura_locale
  175. try:
  176. from cura.CuraApplication import CuraApplication
  177. plugins = CuraApplication.getInstance().getPluginRegistry()
  178. self.data["plugins"] = {
  179. plugin_id: plugins.getMetaData(plugin_id)["plugin"]["version"]
  180. for plugin_id in plugins.getInstalledPlugins() if not plugins.isBundledPlugin(plugin_id)
  181. }
  182. except:
  183. self.data["plugins"] = {"[FAILED]": "0.0.0"}
  184. crash_info = "<b>" + catalog.i18nc("@label Cura version number", "Cura version") + ":</b> " + str(self.cura_version) + "<br/>"
  185. crash_info += "<b>" + catalog.i18nc("@label", "Cura language") + ":</b> " + str(self.cura_locale) + "<br/>"
  186. crash_info += "<b>" + catalog.i18nc("@label", "OS language") + ":</b> " + str(self.data["locale_os"]) + "<br/>"
  187. crash_info += "<b>" + catalog.i18nc("@label Type of platform", "Platform") + ":</b> " + str(platform.platform()) + "<br/>"
  188. crash_info += "<b>" + catalog.i18nc("@label", "Qt version") + ":</b> " + str(QT_VERSION_STR) + "<br/>"
  189. crash_info += "<b>" + catalog.i18nc("@label", "PyQt version") + ":</b> " + str(PYQT_VERSION_STR) + "<br/>"
  190. crash_info += "<b>" + catalog.i18nc("@label OpenGL version", "OpenGL") + ":</b> " + str(self._getOpenGLInfo()) + "<br/>"
  191. label.setText(crash_info)
  192. layout.addWidget(label)
  193. group.setLayout(layout)
  194. if with_sentry_sdk:
  195. with configure_scope() as scope:
  196. scope.set_tag("qt_version", QT_VERSION_STR)
  197. scope.set_tag("pyqt_version", PYQT_VERSION_STR)
  198. scope.set_tag("os", platform.system())
  199. scope.set_tag("os_version", platform.version())
  200. scope.set_tag("locale_os", self.data["locale_os"])
  201. scope.set_tag("locale_cura", self.cura_locale)
  202. scope.set_tag("is_enterprise", ApplicationMetadata.IsEnterpriseVersion)
  203. scope.set_context("plugins", self.data["plugins"])
  204. user_id = uuid.getnode() # On all of Cura's supported platforms, this returns the MAC address which is pseudonymical information (!= anonymous).
  205. user_id %= 2 ** 16 # So to make it anonymous, apply a bitmask selecting only the last 16 bits.
  206. # This prevents it from being traceable to a specific user but still gives somewhat of an idea of whether it's just the same user hitting the same crash over and over again, or if it's widespread.
  207. scope.set_user({"id": str(user_id)})
  208. return group
  209. def _getOpenGLInfo(self):
  210. opengl_instance = OpenGL.getInstance()
  211. if not opengl_instance:
  212. self.data["opengl"] = {"version": "n/a", "vendor": "n/a", "type": "n/a"}
  213. return catalog.i18nc("@label", "Not yet initialized<br/>")
  214. info = "<ul>"
  215. info += catalog.i18nc("@label OpenGL version", "<li>OpenGL Version: {version}</li>").format(version = opengl_instance.getOpenGLVersion())
  216. info += catalog.i18nc("@label OpenGL vendor", "<li>OpenGL Vendor: {vendor}</li>").format(vendor = opengl_instance.getGPUVendorName())
  217. info += catalog.i18nc("@label OpenGL renderer", "<li>OpenGL Renderer: {renderer}</li>").format(renderer = opengl_instance.getGPUType())
  218. info += "</ul>"
  219. self.data["opengl"] = {"version": opengl_instance.getOpenGLVersion(), "vendor": opengl_instance.getGPUVendorName(), "type": opengl_instance.getGPUType()}
  220. active_machine_definition_id = "unknown"
  221. active_machine_manufacturer = "unknown"
  222. try:
  223. from cura.CuraApplication import CuraApplication
  224. application = cast(CuraApplication, Application.getInstance())
  225. machine_manager = application.getMachineManager()
  226. global_stack = machine_manager.activeMachine
  227. if global_stack is None:
  228. active_machine_definition_id = "empty"
  229. active_machine_manufacturer = "empty"
  230. else:
  231. active_machine_definition_id = global_stack.definition.getId()
  232. active_machine_manufacturer = global_stack.definition.getMetaDataEntry("manufacturer", "unknown")
  233. except:
  234. pass
  235. if with_sentry_sdk:
  236. with configure_scope() as scope:
  237. scope.set_tag("opengl_version", opengl_instance.getOpenGLVersion())
  238. scope.set_tag("gpu_vendor", opengl_instance.getGPUVendorName())
  239. scope.set_tag("gpu_type", opengl_instance.getGPUType())
  240. scope.set_tag("active_machine", active_machine_definition_id)
  241. scope.set_tag("active_machine_manufacturer", active_machine_manufacturer)
  242. return info
  243. def _exceptionInfoWidget(self):
  244. group = QGroupBox()
  245. group.setTitle(catalog.i18nc("@title:groupbox", "Error traceback"))
  246. layout = QVBoxLayout()
  247. text_area = QTextEdit()
  248. trace_list = traceback.format_exception(self.exception_type, self.value, self.traceback)
  249. trace = "".join(trace_list)
  250. text_area.setText(trace)
  251. text_area.setReadOnly(True)
  252. layout.addWidget(text_area)
  253. group.setLayout(layout)
  254. # Parsing all the information to fill the dictionary
  255. summary = ""
  256. if len(trace_list) >= 1:
  257. summary = trace_list[len(trace_list)-1].rstrip("\n")
  258. module = [""]
  259. if len(trace_list) >= 2:
  260. module = trace_list[len(trace_list)-2].rstrip("\n").split("\n")
  261. module_split = module[0].split(", ")
  262. filepath_directory_split = module_split[0].split("\"")
  263. filepath = ""
  264. if len(filepath_directory_split) > 1:
  265. filepath = filepath_directory_split[1]
  266. directory, filename = os.path.split(filepath)
  267. line = ""
  268. if len(module_split) > 1:
  269. line = int(module_split[1].lstrip("line "))
  270. function = ""
  271. if len(module_split) > 2:
  272. function = module_split[2].lstrip("in ")
  273. code = ""
  274. if len(module) > 1:
  275. code = module[1].lstrip(" ")
  276. # Using this workaround for a cross-platform path splitting
  277. split_path = []
  278. folder_name = ""
  279. # Split until reach folder "cura"
  280. while folder_name != "cura":
  281. directory, folder_name = os.path.split(directory)
  282. if not folder_name:
  283. break
  284. split_path.append(folder_name)
  285. # Look for plugins. If it's not a plugin, the current cura version is set
  286. isPlugin = False
  287. module_version = self.cura_version
  288. module_name = "Cura"
  289. if split_path.__contains__("plugins"):
  290. isPlugin = True
  291. # Look backwards until plugin.json is found
  292. directory, name = os.path.split(filepath)
  293. while not os.listdir(directory).__contains__("plugin.json"):
  294. directory, name = os.path.split(directory)
  295. json_metadata_file = os.path.join(directory, "plugin.json")
  296. try:
  297. with open(json_metadata_file, "r", encoding = "utf-8") as f:
  298. try:
  299. metadata = json.loads(f.read())
  300. module_version = metadata["version"]
  301. module_name = metadata["name"]
  302. except json.decoder.JSONDecodeError:
  303. # Not throw new exceptions
  304. Logger.logException("e", "Failed to parse plugin.json for plugin %s", name)
  305. except:
  306. # Not throw new exceptions
  307. pass
  308. exception_dict = dict()
  309. exception_dict["traceback"] = {"summary": summary, "full_trace": trace}
  310. exception_dict["location"] = {"path": filepath, "file": filename, "function": function, "code": code, "line": line,
  311. "module_name": module_name, "version": module_version, "is_plugin": isPlugin}
  312. self.data["exception"] = exception_dict
  313. if with_sentry_sdk:
  314. with configure_scope() as scope:
  315. scope.set_tag("is_plugin", isPlugin)
  316. scope.set_tag("module", module_name)
  317. return group
  318. def _logInfoWidget(self):
  319. group = QGroupBox()
  320. group.setTitle(catalog.i18nc("@title:groupbox", "Logs"))
  321. layout = QVBoxLayout()
  322. text_area = QTextEdit()
  323. tmp_file_fd, tmp_file_path = tempfile.mkstemp(prefix = "cura-crash", text = True)
  324. os.close(tmp_file_fd)
  325. with open(tmp_file_path, "w", encoding = "utf-8") as f:
  326. faulthandler.dump_traceback(f, all_threads=True)
  327. with open(tmp_file_path, "r", encoding = "utf-8") as f:
  328. logdata = f.read()
  329. text_area.setText(logdata)
  330. text_area.setReadOnly(True)
  331. layout.addWidget(text_area)
  332. group.setLayout(layout)
  333. self.data["log"] = logdata
  334. return group
  335. def _buttonsWidget(self):
  336. buttons = QDialogButtonBox()
  337. buttons.addButton(QDialogButtonBox.StandardButton.Close)
  338. # Like above, this will be served as a separate detailed report dialog if the application has not yet been
  339. # fully loaded. In this case, "send report" will be a check box in the early crash dialog, so there is no
  340. # need for this extra button.
  341. if self.has_started:
  342. buttons.addButton(catalog.i18nc("@action:button", "Send report"), QDialogButtonBox.ButtonRole.AcceptRole)
  343. buttons.accepted.connect(self._sendCrashReport)
  344. buttons.rejected.connect(self.dialog.close)
  345. return buttons
  346. def _sendCrashReport(self):
  347. if with_sentry_sdk:
  348. try:
  349. hub = Hub.current
  350. if not Logger.getLoggers():
  351. # No loggers have been loaded yet, so we don't have any breadcrumbs :(
  352. # So add them manually so we at least have some info...
  353. add_breadcrumb(level = "info", message = "SentryLogging was not initialised yet")
  354. for log_type, line in Logger.getUnloggedLines():
  355. add_breadcrumb(message=line)
  356. event, hint = event_from_exception((self.exception_type, self.value, self.traceback))
  357. hub.capture_event(event, hint=hint)
  358. hub.flush()
  359. except Exception as e: # We don't want any exception to cause problems
  360. Logger.logException("e", "An exception occurred while trying to send crash report")
  361. if not self.has_started:
  362. print("An exception occurred while trying to send crash report: %s" % e)
  363. else:
  364. msg = "SentrySDK is not available and the report could not be sent."
  365. Logger.logException("e", msg)
  366. if not self.has_started:
  367. print(msg)
  368. print("Exception type: {}".format(self.exception_type))
  369. print("Value: {}".format(self.value))
  370. print("Traceback: {}".format(self.traceback))
  371. os._exit(1)
  372. def show(self):
  373. # must run the GUI code on the Qt thread, otherwise the widgets on the dialog won't react correctly.
  374. Application.getInstance().callLater(self._show)
  375. def _show(self):
  376. # When the exception is in the skip_exception_types list, the dialog is not created, so we don't need to show it
  377. if self.dialog:
  378. self.dialog.exec()
  379. os._exit(1)