Skip to content

Commit

Permalink
Merge pull request #3365 from trailofbits/magic-debugger
Browse files Browse the repository at this point in the history
Adds an interactive debugger for the libmagic DSL
  • Loading branch information
ESultanik authored Jan 13, 2022
2 parents 633414c + 6408ef5 commit 7be7409
Show file tree
Hide file tree
Showing 6 changed files with 881 additions and 59 deletions.
13 changes: 12 additions & 1 deletion polyfile/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from contextlib import ExitStack
import base64
import hashlib
import json
Expand All @@ -14,6 +15,7 @@
from . import polyfile
from .fileutils import PathOrStdin
from .magic import MagicMatcher, MatchContext
from .magic_debugger import Debugger
from .polyfile import __version__


Expand Down Expand Up @@ -54,6 +56,11 @@ def main(argv=None):
help='stop scanning after having found this many matches')
parser.add_argument('--debug', '-d', action='store_true', help='print debug information')
parser.add_argument('--trace', '-dd', action='store_true', help='print extra verbose debug information')
parser.add_argument('--debugger', '-db', action='store_true', help='drop into an interactive debugger for libmagic '
'file definition matching')
parser.add_argument('--no-debug-python', action='store_true', help='by default, the `--debugger` option will break '
'on custom matchers and prompt to debug using '
'PDB. This option will suppress those prompts.')
parser.add_argument('--quiet', '-q', action='store_true', help='suppress all log output (overrides --debug)')
parser.add_argument('--version', '-v', action='store_true', help='print PolyFile\'s version information to STDERR')
parser.add_argument('-dumpversion', action='store_true',
Expand Down Expand Up @@ -110,7 +117,11 @@ def main(argv=None):
exit(1)
return # this is here because linters are dumb and will complain about the next line without it

with path_or_stdin as file_path:
with path_or_stdin as file_path, ExitStack() as stack:
if args.debugger:
stack.enter_context(Debugger(break_on_submatching=not args.no_debug_python))
elif args.no_debug_python:
log.warning("Ignoring `--no-debug-python`; it can only be used with the --debugger option.")
matches = []
try:
if args.only_match_mime:
Expand Down
3 changes: 3 additions & 0 deletions polyfile/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ def __init__(self, source: Iterable[T]):
super().__init__(unique(iter(source), elements=self._set))

def __contains__(self, x: object) -> bool:
if x in self._set:
return True
self._complete()
return x in self._set
Loading

0 comments on commit 7be7409

Please sign in to comment.