Skip to content

Commit 19853bd

Browse files
committed
Add dp.set_exception_hook to allow users to handle exceptions
1 parent 33bf82a commit 19853bd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/dumpulator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .dumpulator import Dumpulator
1+
from .dumpulator import Dumpulator, ExceptionType, MemoryViolation, ExceptionInfo
22
from .ntsyscalls import syscall

src/dumpulator/dumpulator.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import traceback
55
from enum import Enum
6-
from typing import List, Union, NamedTuple
6+
from typing import List, Union, NamedTuple, Callable
77
import inspect
88
from collections import OrderedDict
99
from dataclasses import dataclass, field
@@ -323,6 +323,7 @@ def __init__(self, minidump_file, *, trace=False, quiet=False, thread_id=None, d
323323
self.exports = self._all_exports()
324324
self._exception = UnicornExceptionInfo()
325325
self._last_exception: Optional[UnicornExceptionInfo] = None
326+
self._exception_hook: Optional[Callable[[ExceptionInfo], Optional[int]]] = None
326327
if not self._quiet:
327328
print("Memory map:")
328329
self.print_memory()
@@ -912,10 +913,23 @@ def allocate(self, size, page_align=False):
912913
self.memory.commit(self.memory.align_page(ptr), self.memory.align_page(size))
913914
return ptr
914915

916+
def set_exception_hook(self, exception_hook: Optional[Callable[[ExceptionInfo], Optional[int]]]):
917+
previous_hook = self._exception_hook
918+
self._exception_hook = exception_hook
919+
return previous_hook
920+
915921
def handle_exception(self):
916922
assert not self._exception._handling
917923
self._exception._handling = True
918924

925+
if self._exception_hook is not None:
926+
hook_result = self._exception_hook(self._exception)
927+
if hook_result is not None:
928+
# Clear the pending exception
929+
self._last_exception = self._exception
930+
self._exception = UnicornExceptionInfo()
931+
return hook_result
932+
919933
if self._exception.type == ExceptionType.ContextSwitch:
920934
self.info(f"context switch, cip: {hex(self.regs.cip)}")
921935
# Clear the pending exception

0 commit comments

Comments
 (0)