CrashHandler.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # Copyright (c) 2022 UltiMaker
  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("opengl_version_short", opengl_instance.getOpenGLVersionShort())
  239. scope.set_tag("gpu_vendor", opengl_instance.getGPUVendorName())
  240. scope.set_tag("gpu_type", opengl_instance.getGPUType())
  241. scope.set_tag("active_machine", active_machine_definition_id)
  242. scope.set_tag("active_machine_manufacturer", active_machine_manufacturer)
  243. return info
  244. def _exceptionInfoWidget(self):
  245. group = QGroupBox()
  246. group.setTitle(catalog.i18nc("@title:groupbox", "Error traceback"))
  247. layout = QVBoxLayout()
  248. text_area = QTextEdit()
  249. trace_list = traceback.format_exception(self.exception_type, self.value, self.traceback)
  250. trace = "".join(trace_list)
  251. text_area.setText(trace)
  252. text_area.setReadOnly(True)
  253. layout.addWidget(text_area)
  254. group.setLayout(layout)
  255. # Parsing all the information to fill the dictionary
  256. summary = ""
  257. if len(trace_list) >= 1:
  258. summary = trace_list[len(trace_list)-1].rstrip("\n")
  259. module = [""]
  260. if len(trace_list) >= 2:
  261. module = trace_list[len(trace_list)-2].rstrip("\n").split("\n")
  262. module_split = module[0].split(", ")
  263. filepath_directory_split = module_split[0].split("\"")
  264. filepath = ""
  265. if len(filepath_directory_split) > 1:
  266. filepath = filepath_directory_split[1]
  267. directory, filename = os.path.split(filepath)
  268. line = ""
  269. if len(module_split) > 1:
  270. line = int(module_split[1].lstrip("line "))
  271. function = ""
  272. if len(module_split) > 2:
  273. function = module_split[2].lstrip("in ")
  274. code = ""
  275. if len(module) > 1:
  276. code = module[1].lstrip(" ")
  277. # Using this workaround for a cross-platform path splitting
  278. split_path = []
  279. folder_name = ""
  280. # Split until reach folder "cura"
  281. while folder_name != "cura":
  282. directory, folder_name = os.path.split(directory)
  283. if not folder_name:
  284. break
  285. split_path.append(folder_name)
  286. # Look for plugins. If it's not a plugin, the current cura version is set
  287. isPlugin = False
  288. module_version = self.cura_version
  289. module_name = "Cura"
  290. if split_path.__contains__("plugins"):
  291. isPlugin = True
  292. # Look backwards until plugin.json is found
  293. directory, name = os.path.split(filepath)
  294. while not os.listdir(directory).__contains__("plugin.json"):
  295. directory, name = os.path.split(directory)
  296. json_metadata_file = os.path.join(directory, "plugin.json")
  297. try:
  298. with open(json_metadata_file, "r", encoding = "utf-8") as f:
  299. try:
  300. metadata = json.loads(f.read())
  301. module_version = metadata["version"]
  302. module_name = metadata["name"]
  303. except json.decoder.JSONDecodeError:
  304. # Not throw new exceptions
  305. Logger.logException("e", "Failed to parse plugin.json for plugin %s", name)
  306. except:
  307. # Not throw new exceptions
  308. pass
  309. exception_dict = dict()
  310. exception_dict["traceback"] = {"summary": summary, "full_trace": trace}
  311. exception_dict["location"] = {"path": filepath, "file": filename, "function": function, "code": code, "line": line,
  312. "module_name": module_name, "version": module_version, "is_plugin": isPlugin}
  313. self.data["exception"] = exception_dict
  314. if with_sentry_sdk:
  315. with configure_scope() as scope:
  316. scope.set_tag("is_plugin", isPlugin)
  317. scope.set_tag("module", module_name)
  318. return group
  319. def _logInfoWidget(self):
  320. group = QGroupBox()
  321. group.setTitle(catalog.i18nc("@title:groupbox", "Logs"))
  322. layout = QVBoxLayout()
  323. text_area = QTextEdit()
  324. tmp_file_fd, tmp_file_path = tempfile.mkstemp(prefix = "cura-crash", text = True)
  325. os.close(tmp_file_fd)
  326. with open(tmp_file_path, "w", encoding = "utf-8") as f:
  327. faulthandler.dump_traceback(f, all_threads=True)
  328. with open(tmp_file_path, "r", encoding = "utf-8") as f:
  329. logdata = f.read()
  330. text_area.setText(logdata)
  331. text_area.setReadOnly(True)
  332. layout.addWidget(text_area)
  333. group.setLayout(layout)
  334. self.data["log"] = logdata
  335. return group
  336. def _buttonsWidget(self):
  337. buttons = QDialogButtonBox()
  338. buttons.addButton(QDialogButtonBox.StandardButton.Close)
  339. # Like above, this will be served as a separate detailed report dialog if the application has not yet been
  340. # fully loaded. In this case, "send report" will be a check box in the early crash dialog, so there is no
  341. # need for this extra button.
  342. if self.has_started:
  343. buttons.addButton(catalog.i18nc("@action:button", "Send report"), QDialogButtonBox.ButtonRole.AcceptRole)
  344. buttons.accepted.connect(self._sendCrashReport)
  345. buttons.rejected.connect(self.dialog.close)
  346. return buttons
  347. def _sendCrashReport(self):
  348. if with_sentry_sdk:
  349. try:
  350. hub = Hub.current
  351. if not Logger.getLoggers():
  352. # No loggers have been loaded yet, so we don't have any breadcrumbs :(
  353. # So add them manually so we at least have some info...
  354. add_breadcrumb(level = "info", message = "SentryLogging was not initialised yet")
  355. for log_type, line in Logger.getUnloggedLines():
  356. add_breadcrumb(message=line)
  357. event, hint = event_from_exception((self.exception_type, self.value, self.traceback))
  358. hub.capture_event(event, hint=hint)
  359. hub.flush()
  360. except Exception as e: # We don't want any exception to cause problems
  361. Logger.logException("e", "An exception occurred while trying to send crash report")
  362. if not self.has_started:
  363. print("An exception occurred while trying to send crash report: %s" % e)
  364. else:
  365. msg = "SentrySDK is not available and the report could not be sent."
  366. Logger.logException("e", msg)
  367. if not self.has_started:
  368. print(msg)
  369. print("Exception type: {}".format(self.exception_type))
  370. print("Value: {}".format(self.value))
  371. print("Traceback: {}".format(self.traceback))
  372. os._exit(1)
  373. def show(self):
  374. # must run the GUI code on the Qt thread, otherwise the widgets on the dialog won't react correctly.
  375. Application.getInstance().callLater(self._show)
  376. def _show(self):
  377. # When the exception is in the skip_exception_types list, the dialog is not created, so we don't need to show it
  378. if self.dialog:
  379. self.dialog.exec()
  380. os._exit(1)