Use hunter for line tracing.

This commit is contained in:
Florian Bruhin 2015-03-31 19:02:42 +02:00
parent a98060e020
commit eb3b0b960f
4 changed files with 38 additions and 34 deletions

View File

@ -36,6 +36,10 @@ from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox
from PyQt5.QtGui import QDesktopServices, QPixmap, QIcon from PyQt5.QtGui import QDesktopServices, QPixmap, QIcon
from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl, from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl,
QObject, Qt, QSocketNotifier) QObject, Qt, QSocketNotifier)
try:
import hunter
except ImportError:
hunter = None
import qutebrowser import qutebrowser
import qutebrowser.resources # pylint: disable=unused-import import qutebrowser.resources # pylint: disable=unused-import
@ -50,7 +54,7 @@ from qutebrowser.misc import (crashdialog, readline, ipc, earlyinit,
from qutebrowser.misc import utilcmds # pylint: disable=unused-import from qutebrowser.misc import utilcmds # pylint: disable=unused-import
from qutebrowser.keyinput import modeman from qutebrowser.keyinput import modeman
from qutebrowser.utils import (log, version, message, utils, qtutils, urlutils, from qutebrowser.utils import (log, version, message, utils, qtutils, urlutils,
debug, objreg, usertypes, standarddir) objreg, usertypes, standarddir)
# We import utilcmds to run the cmdutils.register decorators. # We import utilcmds to run the cmdutils.register decorators.
@ -924,6 +928,10 @@ class Application(QApplication):
"""Extend QApplication::exit to log the event.""" """Extend QApplication::exit to log the event."""
log.destroy.debug("Now calling QApplication::exit.") log.destroy.debug("Now calling QApplication::exit.")
if self._args.debug_exit: if self._args.debug_exit:
print("Now logging late shutdown.", file=sys.stderr) if hunter is None:
debug.trace_lines(True) print("Not logging late shutdown because hunter could not be "
"imported!", file=sys.stderr)
else:
print("Now logging late shutdown.", file=sys.stderr)
hunter.trace()
super().exit(status) super().exit(status)

View File

@ -20,6 +20,13 @@
At this point we can be sure we have all python 3.4 features available. At this point we can be sure we have all python 3.4 features available.
""" """
try:
# Importing hunter to register its atexit handler early so it gets called
# late.
import hunter # pylint: disable=import-error,unused-import
except ImportError:
hunter = None
import os import os
import sys import sys
import faulthandler import faulthandler

View File

@ -24,6 +24,10 @@ import functools
import types import types
from PyQt5.QtCore import QCoreApplication from PyQt5.QtCore import QCoreApplication
try:
import hunter
except ImportError:
hunter = None
from qutebrowser.utils import log, objreg, usertypes from qutebrowser.utils import log, objreg, usertypes
from qutebrowser.commands import cmdutils, runners, cmdexc from qutebrowser.commands import cmdutils, runners, cmdexc
@ -119,6 +123,22 @@ def debug_console():
con_widget.show() con_widget.show()
@cmdutils.register(debug=True, maxsplit=0)
def debug_trace(expr=""):
"""Trace executed code via hunter.
Args:
expr: What to trace, passed to hunter.
"""
if hunter is None:
raise cmdexc.CommandError("You need to install 'hunter' to use this "
"command!")
try:
eval('hunter.trace({})'.format(expr))
except Exception as e:
raise cmdexc.CommandError("{}: {}".format(e.__class__.__name__, e))
@cmdutils.register(hide=True) @cmdutils.register(hide=True)
def fooled(): def fooled():
"""Turn off april's fools.""" """Turn off april's fools."""

View File

@ -20,7 +20,6 @@
"""Utilities used for debugging.""" """Utilities used for debugging."""
import re import re
import sys
import inspect import inspect
import functools import functools
import datetime import datetime
@ -87,36 +86,6 @@ def log_signals(obj):
connect_log_slot(obj) connect_log_slot(obj)
def trace_lines(do_trace):
"""Turn on/off printing each executed line.
Args:
do_trace: Whether to start tracing (True) or stop it (False).
"""
def trace(frame, event, arg):
"""Trace function passed to sys.settrace.
Return:
Itself, so tracing continues.
"""
if sys is not None:
loc = '{}:{}'.format(frame.f_code.co_filename, frame.f_lineno)
if arg is not None:
arg = utils.compact_text(str(arg), 200)
else:
arg = ''
print("{:11} {:80} {}".format(event, loc, arg), file=sys.stderr)
return trace
else:
# When tracing while shutting down, it seems sys can be None
# sometimes... if that's the case, we stop tracing.
return None
if do_trace:
sys.settrace(trace)
else:
sys.settrace(None)
def qenum_key(base, value, add_base=False, klass=None): def qenum_key(base, value, add_base=False, klass=None):
"""Convert a Qt Enum value to its key as a string. """Convert a Qt Enum value to its key as a string.