Skip to content

Commit 7be7409

Browse files
authored
Merge pull request #3365 from trailofbits/magic-debugger
Adds an interactive debugger for the libmagic DSL
2 parents 633414c + 6408ef5 commit 7be7409

File tree

6 files changed

+881
-59
lines changed

6 files changed

+881
-59
lines changed

polyfile/__main__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
from contextlib import ExitStack
23
import base64
34
import hashlib
45
import json
@@ -14,6 +15,7 @@
1415
from . import polyfile
1516
from .fileutils import PathOrStdin
1617
from .magic import MagicMatcher, MatchContext
18+
from .magic_debugger import Debugger
1719
from .polyfile import __version__
1820

1921

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

113-
with path_or_stdin as file_path:
120+
with path_or_stdin as file_path, ExitStack() as stack:
121+
if args.debugger:
122+
stack.enter_context(Debugger(break_on_submatching=not args.no_debug_python))
123+
elif args.no_debug_python:
124+
log.warning("Ignoring `--no-debug-python`; it can only be used with the --debugger option.")
114125
matches = []
115126
try:
116127
if args.only_match_mime:

polyfile/iterators.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ def __init__(self, source: Iterable[T]):
6666
super().__init__(unique(iter(source), elements=self._set))
6767

6868
def __contains__(self, x: object) -> bool:
69+
if x in self._set:
70+
return True
71+
self._complete()
6972
return x in self._set

0 commit comments

Comments
 (0)