- Notifications
You must be signed in to change notification settings - Fork47
Hunter is a flexible code tracing toolkit.
License
ionelmc/python-hunter
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
docs | |
---|---|
tests | |
package |
Hunter is a flexible code tracing toolkit, not for measuring coverage, but for debugging, logging, inspection and othernefarious purposes. It has asimple Python API,aconvenient terminal API andaCLI tool to attach to processes.
- Free software: BSD 2-Clause License
pip install hunter
https://python-hunter.readthedocs.io/
Basic use involves passing various filters to thetrace
option. An example:
importhunterhunter.trace(module='posixpath',action=hunter.CallPrinter)importosos.path.join('a','b')
That would result in:
>>> os.path.join('a','b') /usr/lib/python3.6/posixpath.py:75 call => join(a='a') /usr/lib/python3.6/posixpath.py:80 line a = os.fspath(a) /usr/lib/python3.6/posixpath.py:81 line sep = _get_sep(a) /usr/lib/python3.6/posixpath.py:41 call => _get_sep(path='a') /usr/lib/python3.6/posixpath.py:42 line if isinstance(path, bytes): /usr/lib/python3.6/posixpath.py:45 line return '/' /usr/lib/python3.6/posixpath.py:45 return <= _get_sep: '/' /usr/lib/python3.6/posixpath.py:82 line path = a /usr/lib/python3.6/posixpath.py:83 line try: /usr/lib/python3.6/posixpath.py:84 line if not p: /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:87 line if b.startswith(sep): /usr/lib/python3.6/posixpath.py:89 line elif not path or path.endswith(sep): /usr/lib/python3.6/posixpath.py:92 line path += sep + b /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:96 line return path /usr/lib/python3.6/posixpath.py:96 return <= join: 'a/b''a/b'
In a terminal it would look like:
Another useful scenario is to ignore all standard modules and force colors to make them stay even if the output isredirected to a file.
importhunterhunter.trace(stdlib=False,action=hunter.CallPrinter(force_colors=True))
Output format can be controlled with "actions". There's an alternativeCodePrinter
action that doesn't handlenesting (it was the default action until Hunter 2.0).
If filters match then action will be run. Example:
importhunterhunter.trace(module='posixpath',action=hunter.CodePrinter)importosos.path.join('a','b')
That would result in:
>>> os.path.join('a','b') /usr/lib/python3.6/posixpath.py:75 call def join(a, *p): /usr/lib/python3.6/posixpath.py:80 line a = os.fspath(a) /usr/lib/python3.6/posixpath.py:81 line sep = _get_sep(a) /usr/lib/python3.6/posixpath.py:41 call def _get_sep(path): /usr/lib/python3.6/posixpath.py:42 line if isinstance(path, bytes): /usr/lib/python3.6/posixpath.py:45 line return '/' /usr/lib/python3.6/posixpath.py:45 return return '/' ... return value: '/' /usr/lib/python3.6/posixpath.py:82 line path = a /usr/lib/python3.6/posixpath.py:83 line try: /usr/lib/python3.6/posixpath.py:84 line if not p: /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:87 line if b.startswith(sep): /usr/lib/python3.6/posixpath.py:89 line elif not path or path.endswith(sep): /usr/lib/python3.6/posixpath.py:92 line path += sep + b /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:96 line return path /usr/lib/python3.6/posixpath.py:96 return return path ... return value: 'a/b''a/b'
- or in a terminal:
Another useful action is theVarsPrinter
:
importhunter# note that this kind of invocation will also use the default `CallPrinter` actionhunter.trace(hunter.Q(module='posixpath',action=hunter.VarsPrinter('path')))importosos.path.join('a','b')
That would result in:
>>> os.path.join('a','b') /usr/lib/python3.6/posixpath.py:75 call => join(a='a') /usr/lib/python3.6/posixpath.py:80 line a = os.fspath(a) /usr/lib/python3.6/posixpath.py:81 line sep = _get_sep(a) /usr/lib/python3.6/posixpath.py:41 call [path => 'a'] /usr/lib/python3.6/posixpath.py:41 call => _get_sep(path='a') /usr/lib/python3.6/posixpath.py:42 line [path => 'a'] /usr/lib/python3.6/posixpath.py:42 line if isinstance(path, bytes): /usr/lib/python3.6/posixpath.py:45 line [path => 'a'] /usr/lib/python3.6/posixpath.py:45 line return '/' /usr/lib/python3.6/posixpath.py:45 return [path => 'a'] /usr/lib/python3.6/posixpath.py:45 return <= _get_sep: '/' /usr/lib/python3.6/posixpath.py:82 line path = a /usr/lib/python3.6/posixpath.py:83 line [path => 'a'] /usr/lib/python3.6/posixpath.py:83 line try: /usr/lib/python3.6/posixpath.py:84 line [path => 'a'] /usr/lib/python3.6/posixpath.py:84 line if not p: /usr/lib/python3.6/posixpath.py:86 line [path => 'a'] /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:87 line [path => 'a'] /usr/lib/python3.6/posixpath.py:87 line if b.startswith(sep): /usr/lib/python3.6/posixpath.py:89 line [path => 'a'] /usr/lib/python3.6/posixpath.py:89 line elif not path or path.endswith(sep): /usr/lib/python3.6/posixpath.py:92 line [path => 'a'] /usr/lib/python3.6/posixpath.py:92 line path += sep + b /usr/lib/python3.6/posixpath.py:86 line [path => 'a/b'] /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:96 line [path => 'a/b'] /usr/lib/python3.6/posixpath.py:96 line return path /usr/lib/python3.6/posixpath.py:96 return [path => 'a/b'] /usr/lib/python3.6/posixpath.py:96 return <= join: 'a/b''a/b'
In a terminal it would look like:
You can give it a tree-like configuration where you can optionally configure specific actions for parts of thetree (like dumping variables or a pdb set_trace):
fromhunterimporttrace,Q,DebuggerfrompdbimportPdbtrace(# drop into a Pdb session if ``foo.bar()`` is calledQ(module="foo",function="bar",kind="call",action=Debugger(klass=Pdb))|# orQ(# show code that contains "mumbo.jumbo" on the current linelambdaevent:event.locals.get("mumbo")=="jumbo",# and it's not in Python's stdlibstdlib=False,# and it contains "mumbo" on the current linesource__contains="mumbo" ))importfoofoo.func()
With afoo.py
like this:
defbar():execution_will_get_stopped# cause we get a Pdb session heredeffunc():mumbo=1mumbo="jumbo"print("not shown in trace")print(mumbo)mumbo=2print(mumbo)# not shown in tracebar()
We get:
>>> foo.func()not shown in trace /home/ionel/osp/python-hunter/foo.py:8 line print(mumbo)jumbo /home/ionel/osp/python-hunter/foo.py:9 line mumbo = 22 /home/ionel/osp/python-hunter/foo.py:1 call def bar():> /home/ionel/osp/python-hunter/foo.py(2)bar()-> execution_will_get_stopped # cause we get a Pdb session here(Pdb)
In a terminal it would look like:
In similar fashion tostrace
Hunter can trace other processes, eg:
hunter-trace --gdb -p 123
If you wanna play it safe (no messy GDB) then add this in your code:
from hunter import remoteremote.install()
Then you can do:
hunter-trace -p 123
Seedocs on the remote feature.
Note: Windows ain't supported.
For your convenience environment variable activation is available. Just run your app like this:
PYTHONHUNTER="module='os.path'" python yourapp.py
On Windows you'd do something like:
set PYTHONHUNTER=module='os.path'python yourapp.py
The activation works with a clever.pth
file that checks for that env var presence and before your app runs does somethinglike this:
from hunter import *trace(<whatever-you-had-in-the-PYTHONHUNTER-env-var>)
Note that Hunter is activated even if the env var is empty, eg:PYTHONHUNTER=""
.
Sometimes you always use the same options (likestdlib=False
orforce_colors=True
). To save typing you canset something like this in your environment:
PYTHONHUNTERCONFIG="stdlib=False,force_colors=True"
This is the same asPYTHONHUNTER="stdlib=False,action=CallPrinter(force_colors=True)"
.
Notes:
Setting
PYTHONHUNTERCONFIG
alone doesn't activate hunter.All the options for the builtin actions are supported.
Although using predicates is supported it can be problematic. Example of setup that won't trace anything:
PYTHONHUNTERCONFIG="Q(module_startswith='django')"PYTHONHUNTER="Q(module_startswith='celery')"
which is the equivalent of:
PYTHONHUNTER="Q(module_startswith='django'),Q(module_startswith='celery')"
which is the equivalent of:
PYTHONHUNTER="Q(module_startswith='django')&Q(module_startswith='celery')"
Hunter supports a flexible query DSL, see theintroduction.
To run the all tests run:
tox
Hunter doesn't do everything. As a design goal of this library some things are made intentionally austere and verbose (to avoid complexity,confusion and inconsistency). This has few consequences:
- There areOperators but there's no negation operator.Instead you're expected to negate a Query object, eg:
~Q(module='re')
. - There are no specialized operators or filters - all filters behave exactly the same. For example:
- No filter for packages. You're expected to filter by module with an operator.
- No filter for arguments, return values or variables. You're expected to write your own filter function and deal with the problemsof poking into objects.
- Layering is minimal. There's are somehelpers that dosome argument processing and conversions to save you some typing but that's about it.
- The library doesn't try to hide the mechanics of tracing in Python - it's 1:1 regarding what Python sends to a trace function if you'd beusingsys.settrace.
- Doesn't have any storage. You are expected to redirect output to a file.
You should look at it like it's a tool to help you understand and debug big applications, or a framework ridding you of the boring parts ofsettrace, not something that helps you learn Python.
There's some obvious overlap withsmiley but there are few fundamental differences:
Complexity. Smiley is simply over-engineered:
- It uses IPC and a SQL database.
- It has a webserver. Lots of dependencies.
- It uses threads. Side-effects and subtle bugs are introduced in your code.
- It records everything. Tries to dump any variable. Often fails and stops working.
Why do you need all that just to debug some stuff in a terminal? Simply put, it's a nice idea but the design choices workagainst you when you're already neck-deep into debugging your own code. In my experience Smiley has been very buggy andunreliable. Your mileage may vary of course.
Tracing long running code. This will make Smiley record lots of data, making it unusable.
Now because Smiley records everything, you'd think it's better suited for short programs. But alas, if your program runsquickly then it's pointless to record the execution. You can just run it again.
It seems there's only one situation where it's reasonable to use Smiley: tracing io-bound apps remotely. Those apps don'texecute lots of code, they just wait on network so Smiley's storage won't blow out of proportion and tracing overhead mightbe acceptable.
Use-cases. It seems to me Smiley's purpose is not really debugging code, but more of a "non interactive monitoring" tool.
In contrast, Hunter is very simple:
Few dependencies.
Low overhead (tracing/filtering code has an optional Cython extension).
No storage. This simplifies lots of things.
The only cost is that you might need to run the code multiple times to get the filtering/actions right. This means Hunter isnot really suited for "post-mortem" debugging. If you can't reproduce the problem anymore then Hunter won't be of much help.
Pytrace is another tracer tool. It seems quite similar to Smiley - it uses a sqlitedatabase for the events, threads and IPC, thus it's reasonable to expect the same kind of problems.
snoop is a refined version ofPySnooper. Both aremore suited to tracing small programs or functions as the output is more verbose and less suited to the needs of tracing a big applicationwhere Hunter provides more flexible setup, filtering capabilities, speed and brevity.
For purposes of debuggingcoverage is a great tool but only as far as "debuggingby looking at what code is (not) run". Checking branch coverage is good but it will only get you as far.
From the other perspective, you'd be wondering if you could use Hunter to measure coverage-like things. You could do it butfor that purpose Hunter is very "rough": it has no builtin storage. You'd have to implement your own storage. You can do itbut it wouldn't give you any advantage over making your own tracer if you don't need to "pre-filter" whatever you'rerecording.
In other words, filtering events is the main selling point of Hunter - it's fast (cython implementation) and the query API isflexible enough.
Noteworthy usages or Hunter (submit a PR with your project if you built a tool that relies on hunter):
- Crunch-io/diagnose - a runtime instrumentation library.
- talwrii/huntrace - an alternative cli (similar to ltrace).
- anki-code/xunter - a profiling tool made specifically forthe xonsh shell.
More projects using it athttps://github.com/ionelmc/python-hunter/network/dependents
About
Hunter is a flexible code tracing toolkit.