CrashHandler.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. # Copyright (c) 2017 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 time
  10. import json
  11. import ssl
  12. import urllib.request
  13. import urllib.error
  14. import shutil
  15. import sys
  16. from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, Qt, QUrl
  17. from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QTextEdit, QGroupBox, QCheckBox, QPushButton
  18. from PyQt5.QtGui import QDesktopServices
  19. from UM.Resources import Resources
  20. from UM.Application import Application
  21. from UM.Logger import Logger
  22. from UM.View.GL.OpenGL import OpenGL
  23. from UM.i18n import i18nCatalog
  24. from UM.Platform import Platform
  25. catalog = i18nCatalog("cura")
  26. MYPY = False
  27. if MYPY:
  28. CuraDebugMode = False
  29. else:
  30. try:
  31. from cura.CuraVersion import CuraDebugMode
  32. except ImportError:
  33. CuraDebugMode = False # [CodeStyle: Reflecting imported value]
  34. # List of exceptions that should be considered "fatal" and abort the program.
  35. # These are primarily some exception types that we simply cannot really recover from
  36. # (MemoryError and SystemError) and exceptions that indicate grave errors in the
  37. # code that cause the Python interpreter to fail (SyntaxError, ImportError).
  38. fatal_exception_types = [
  39. MemoryError,
  40. SyntaxError,
  41. ImportError,
  42. SystemError,
  43. ]
  44. class CrashHandler:
  45. crash_url = "https://stats.ultimaker.com/api/cura"
  46. def __init__(self, exception_type, value, tb, has_started = True):
  47. self.exception_type = exception_type
  48. self.value = value
  49. self.traceback = tb
  50. self.has_started = has_started
  51. self.dialog = None # Don't create a QDialog before there is a QApplication
  52. # While we create the GUI, the information will be stored for sending afterwards
  53. self.data = dict()
  54. self.data["time_stamp"] = time.time()
  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. # If Cura has fully started, we only show fatal errors.
  60. # If Cura has not fully started yet, we always show the early crash dialog. Otherwise, Cura will just crash
  61. # without any information.
  62. if has_started and exception_type not in fatal_exception_types:
  63. return
  64. if not has_started:
  65. self._send_report_checkbox = None
  66. self.early_crash_dialog = self._createEarlyCrashDialog()
  67. self.dialog = QDialog()
  68. self._createDialog()
  69. def _createEarlyCrashDialog(self):
  70. dialog = QDialog()
  71. dialog.setMinimumWidth(500)
  72. dialog.setMinimumHeight(170)
  73. dialog.setWindowTitle(catalog.i18nc("@title:window", "Cura Crashed"))
  74. dialog.finished.connect(self._closeEarlyCrashDialog)
  75. layout = QVBoxLayout(dialog)
  76. label = QLabel()
  77. label.setText(catalog.i18nc("@label crash message", """<p><b>A fatal error has occurred.</p></b>
  78. <p>Unfortunately, Cura 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>
  79. <p>Backups can be found in the configuration folder.</p>
  80. <p>Please send us this Crash Report to fix the problem.</p>
  81. """))
  82. label.setWordWrap(True)
  83. layout.addWidget(label)
  84. # "send report" check box and show details
  85. self._send_report_checkbox = QCheckBox(catalog.i18nc("@action:button", "Send crash report to Ultimaker"), dialog)
  86. self._send_report_checkbox.setChecked(True)
  87. show_details_button = QPushButton(catalog.i18nc("@action:button", "Show detailed crash report"), dialog)
  88. show_details_button.setMaximumWidth(200)
  89. show_details_button.clicked.connect(self._showDetailedReport)
  90. show_configuration_folder_button = QPushButton(catalog.i18nc("@action:button", "Show configuration folder"), dialog)
  91. show_configuration_folder_button.setMaximumWidth(200)
  92. show_configuration_folder_button.clicked.connect(self._showConfigurationFolder)
  93. layout.addWidget(self._send_report_checkbox)
  94. layout.addWidget(show_details_button)
  95. layout.addWidget(show_configuration_folder_button)
  96. # "backup and start clean" and "close" buttons
  97. buttons = QDialogButtonBox()
  98. buttons.addButton(QDialogButtonBox.Close)
  99. buttons.addButton(catalog.i18nc("@action:button", "Backup and Reset Configuration"), QDialogButtonBox.AcceptRole)
  100. buttons.rejected.connect(self._closeEarlyCrashDialog)
  101. buttons.accepted.connect(self._backupAndStartClean)
  102. layout.addWidget(buttons)
  103. return dialog
  104. def _closeEarlyCrashDialog(self):
  105. if self._send_report_checkbox.isChecked():
  106. self._sendCrashReport()
  107. os._exit(1)
  108. def _backupAndStartClean(self):
  109. # backup the current cura directories and create clean ones
  110. from cura.CuraVersion import CuraVersion
  111. from UM.Resources import Resources
  112. # The early crash may happen before those information is set in Resources, so we need to set them here to
  113. # make sure that Resources can find the correct place.
  114. Resources.ApplicationIdentifier = "cura"
  115. Resources.ApplicationVersion = CuraVersion
  116. config_path = Resources.getConfigStoragePath()
  117. data_path = Resources.getDataStoragePath()
  118. cache_path = Resources.getCacheStoragePath()
  119. folders_to_backup = []
  120. folders_to_remove = [] # only cache folder needs to be removed
  121. folders_to_backup.append(config_path)
  122. if data_path != config_path:
  123. folders_to_backup.append(data_path)
  124. # Only remove the cache folder if it's not the same as data or config
  125. if cache_path not in (config_path, data_path):
  126. folders_to_remove.append(cache_path)
  127. for folder in folders_to_remove:
  128. shutil.rmtree(folder, ignore_errors = True)
  129. for folder in folders_to_backup:
  130. base_name = os.path.basename(folder)
  131. root_dir = os.path.dirname(folder)
  132. import datetime
  133. date_now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
  134. idx = 0
  135. file_name = base_name + "_" + date_now
  136. zip_file_path = os.path.join(root_dir, file_name + ".zip")
  137. while os.path.exists(zip_file_path):
  138. idx += 1
  139. file_name = base_name + "_" + date_now + "_" + idx
  140. zip_file_path = os.path.join(root_dir, file_name + ".zip")
  141. try:
  142. # only create the zip backup when the folder exists
  143. if os.path.exists(folder):
  144. # remove the .zip extension because make_archive() adds it
  145. zip_file_path = zip_file_path[:-4]
  146. shutil.make_archive(zip_file_path, "zip", root_dir = root_dir, base_dir = base_name)
  147. # remove the folder only when the backup is successful
  148. shutil.rmtree(folder, ignore_errors = True)
  149. # create an empty folder so Resources will not try to copy the old ones
  150. os.makedirs(folder, 0o0755, exist_ok=True)
  151. except Exception as e:
  152. Logger.logException("e", "Failed to backup [%s] to file [%s]", folder, zip_file_path)
  153. if not self.has_started:
  154. print("Failed to backup [%s] to file [%s]: %s", folder, zip_file_path, e)
  155. self.early_crash_dialog.close()
  156. def _showConfigurationFolder(self):
  157. path = Resources.getConfigStoragePath();
  158. QDesktopServices.openUrl(QUrl.fromLocalFile( path ))
  159. def _showDetailedReport(self):
  160. self.dialog.exec_()
  161. ## Creates a modal dialog.
  162. def _createDialog(self):
  163. self.dialog.setMinimumWidth(640)
  164. self.dialog.setMinimumHeight(640)
  165. self.dialog.setWindowTitle(catalog.i18nc("@title:window", "Crash Report"))
  166. # if the application has not fully started, this will be a detailed report dialog which should not
  167. # close the application when it's closed.
  168. if self.has_started:
  169. self.dialog.finished.connect(self._close)
  170. layout = QVBoxLayout(self.dialog)
  171. layout.addWidget(self._messageWidget())
  172. layout.addWidget(self._informationWidget())
  173. layout.addWidget(self._exceptionInfoWidget())
  174. layout.addWidget(self._logInfoWidget())
  175. layout.addWidget(self._userDescriptionWidget())
  176. layout.addWidget(self._buttonsWidget())
  177. def _close(self):
  178. os._exit(1)
  179. def _messageWidget(self):
  180. label = QLabel()
  181. label.setText(catalog.i18nc("@label crash message", """<p><b>A fatal error has occurred. Please send us this Crash Report to fix the problem</p></b>
  182. <p>Please use the "Send report" button to post a bug report automatically to our servers</p>
  183. """))
  184. return label
  185. def _informationWidget(self):
  186. group = QGroupBox()
  187. group.setTitle(catalog.i18nc("@title:groupbox", "System information"))
  188. layout = QVBoxLayout()
  189. label = QLabel()
  190. try:
  191. from UM.Application import Application
  192. self.cura_version = Application.getInstance().getVersion()
  193. except:
  194. self.cura_version = catalog.i18nc("@label unknown version of Cura", "Unknown")
  195. crash_info = "<b>" + catalog.i18nc("@label Cura version number", "Cura version") + ":</b> " + str(self.cura_version) + "<br/>"
  196. crash_info += "<b>" + catalog.i18nc("@label Type of platform", "Platform") + ":</b> " + str(platform.platform()) + "<br/>"
  197. crash_info += "<b>" + catalog.i18nc("@label", "Qt version") + ":</b> " + str(QT_VERSION_STR) + "<br/>"
  198. crash_info += "<b>" + catalog.i18nc("@label", "PyQt version") + ":</b> " + str(PYQT_VERSION_STR) + "<br/>"
  199. crash_info += "<b>" + catalog.i18nc("@label OpenGL version", "OpenGL") + ":</b> " + str(self._getOpenGLInfo()) + "<br/>"
  200. label.setText(crash_info)
  201. layout.addWidget(label)
  202. group.setLayout(layout)
  203. self.data["cura_version"] = self.cura_version
  204. self.data["os"] = {"type": platform.system(), "version": platform.version()}
  205. self.data["qt_version"] = QT_VERSION_STR
  206. self.data["pyqt_version"] = PYQT_VERSION_STR
  207. return group
  208. def _getOpenGLInfo(self):
  209. opengl_instance = OpenGL.getInstance()
  210. if not opengl_instance:
  211. self.data["opengl"] = {"version": "n/a", "vendor": "n/a", "type": "n/a"}
  212. return catalog.i18nc("@label", "not yet initialised<br/>")
  213. info = "<ul>"
  214. info += catalog.i18nc("@label OpenGL version", "<li>OpenGL Version: {version}</li>").format(version = opengl_instance.getOpenGLVersion())
  215. info += catalog.i18nc("@label OpenGL vendor", "<li>OpenGL Vendor: {vendor}</li>").format(vendor = opengl_instance.getGPUVendorName())
  216. info += catalog.i18nc("@label OpenGL renderer", "<li>OpenGL Renderer: {renderer}</li>").format(renderer = opengl_instance.getGPUType())
  217. info += "</ul>"
  218. self.data["opengl"] = {"version": opengl_instance.getOpenGLVersion(), "vendor": opengl_instance.getGPUVendorName(), "type": opengl_instance.getGPUType()}
  219. return info
  220. def _exceptionInfoWidget(self):
  221. group = QGroupBox()
  222. group.setTitle(catalog.i18nc("@title:groupbox", "Error traceback"))
  223. layout = QVBoxLayout()
  224. text_area = QTextEdit()
  225. trace_list = traceback.format_exception(self.exception_type, self.value, self.traceback)
  226. trace = "".join(trace_list)
  227. text_area.setText(trace)
  228. text_area.setReadOnly(True)
  229. layout.addWidget(text_area)
  230. group.setLayout(layout)
  231. # Parsing all the information to fill the dictionary
  232. summary = ""
  233. if len(trace_list) >= 1:
  234. summary = trace_list[len(trace_list)-1].rstrip("\n")
  235. module = [""]
  236. if len(trace_list) >= 2:
  237. module = trace_list[len(trace_list)-2].rstrip("\n").split("\n")
  238. module_split = module[0].split(", ")
  239. filepath_directory_split = module_split[0].split("\"")
  240. filepath = ""
  241. if len(filepath_directory_split) > 1:
  242. filepath = filepath_directory_split[1]
  243. directory, filename = os.path.split(filepath)
  244. line = ""
  245. if len(module_split) > 1:
  246. line = int(module_split[1].lstrip("line "))
  247. function = ""
  248. if len(module_split) > 2:
  249. function = module_split[2].lstrip("in ")
  250. code = ""
  251. if len(module) > 1:
  252. code = module[1].lstrip(" ")
  253. # Using this workaround for a cross-platform path splitting
  254. split_path = []
  255. folder_name = ""
  256. # Split until reach folder "cura"
  257. while folder_name != "cura":
  258. directory, folder_name = os.path.split(directory)
  259. if not folder_name:
  260. break
  261. split_path.append(folder_name)
  262. # Look for plugins. If it's not a plugin, the current cura version is set
  263. isPlugin = False
  264. module_version = self.cura_version
  265. module_name = "Cura"
  266. if split_path.__contains__("plugins"):
  267. isPlugin = True
  268. # Look backwards until plugin.json is found
  269. directory, name = os.path.split(filepath)
  270. while not os.listdir(directory).__contains__("plugin.json"):
  271. directory, name = os.path.split(directory)
  272. json_metadata_file = os.path.join(directory, "plugin.json")
  273. try:
  274. with open(json_metadata_file, "r", encoding = "utf-8") as f:
  275. try:
  276. metadata = json.loads(f.read())
  277. module_version = metadata["version"]
  278. module_name = metadata["name"]
  279. except json.decoder.JSONDecodeError:
  280. # Not throw new exceptions
  281. Logger.logException("e", "Failed to parse plugin.json for plugin %s", name)
  282. except:
  283. # Not throw new exceptions
  284. pass
  285. exception_dict = dict()
  286. exception_dict["traceback"] = {"summary": summary, "full_trace": trace}
  287. exception_dict["location"] = {"path": filepath, "file": filename, "function": function, "code": code, "line": line,
  288. "module_name": module_name, "version": module_version, "is_plugin": isPlugin}
  289. self.data["exception"] = exception_dict
  290. return group
  291. def _logInfoWidget(self):
  292. group = QGroupBox()
  293. group.setTitle(catalog.i18nc("@title:groupbox", "Logs"))
  294. layout = QVBoxLayout()
  295. text_area = QTextEdit()
  296. tmp_file_fd, tmp_file_path = tempfile.mkstemp(prefix = "cura-crash", text = True)
  297. os.close(tmp_file_fd)
  298. with open(tmp_file_path, "w", encoding = "utf-8") as f:
  299. faulthandler.dump_traceback(f, all_threads=True)
  300. with open(tmp_file_path, "r", encoding = "utf-8") as f:
  301. logdata = f.read()
  302. text_area.setText(logdata)
  303. text_area.setReadOnly(True)
  304. layout.addWidget(text_area)
  305. group.setLayout(layout)
  306. self.data["log"] = logdata
  307. return group
  308. def _userDescriptionWidget(self):
  309. group = QGroupBox()
  310. group.setTitle(catalog.i18nc("@title:groupbox", "User description"))
  311. layout = QVBoxLayout()
  312. # When sending the report, the user comments will be collected
  313. self.user_description_text_area = QTextEdit()
  314. self.user_description_text_area.setFocus(True)
  315. layout.addWidget(self.user_description_text_area)
  316. group.setLayout(layout)
  317. return group
  318. def _buttonsWidget(self):
  319. buttons = QDialogButtonBox()
  320. buttons.addButton(QDialogButtonBox.Close)
  321. # Like above, this will be served as a separate detailed report dialog if the application has not yet been
  322. # fully loaded. In this case, "send report" will be a check box in the early crash dialog, so there is no
  323. # need for this extra button.
  324. if self.has_started:
  325. buttons.addButton(catalog.i18nc("@action:button", "Send report"), QDialogButtonBox.AcceptRole)
  326. buttons.accepted.connect(self._sendCrashReport)
  327. buttons.rejected.connect(self.dialog.close)
  328. return buttons
  329. def _sendCrashReport(self):
  330. # Before sending data, the user comments are stored
  331. self.data["user_info"] = self.user_description_text_area.toPlainText()
  332. # Convert data to bytes
  333. binary_data = json.dumps(self.data).encode("utf-8")
  334. # Submit data
  335. kwoptions = {"data": binary_data, "timeout": 5}
  336. if Platform.isOSX():
  337. kwoptions["context"] = ssl._create_unverified_context()
  338. Logger.log("i", "Sending crash report info to [%s]...", self.crash_url)
  339. if not self.has_started:
  340. print("Sending crash report info to [%s]...\n" % self.crash_url)
  341. try:
  342. f = urllib.request.urlopen(self.crash_url, **kwoptions)
  343. Logger.log("i", "Sent crash report info.")
  344. if not self.has_started:
  345. print("Sent crash report info.\n")
  346. f.close()
  347. except urllib.error.HTTPError as e:
  348. Logger.logException("e", "An HTTP error occurred while trying to send crash report")
  349. if not self.has_started:
  350. print("An HTTP error occurred while trying to send crash report: %s" % e)
  351. except Exception as e: # We don't want any exception to cause problems
  352. Logger.logException("e", "An exception occurred while trying to send crash report")
  353. if not self.has_started:
  354. print("An exception occurred while trying to send crash report: %s" % e)
  355. os._exit(1)
  356. def show(self):
  357. # must run the GUI code on the Qt thread, otherwise the widgets on the dialog won't react correctly.
  358. Application.getInstance().callLater(self._show)
  359. def _show(self):
  360. # When the exception is not in the fatal_exception_types list, the dialog is not created, so we don't need to show it
  361. if self.dialog:
  362. self.dialog.exec_()
  363. os._exit(1)