|
| 1 | +import gdb |
| 2 | +from typing import Sequence, Callable, Optional |
| 3 | +from dbgtools.main import get_pie_base |
| 4 | +from dbgtools.logger import Logger |
| 5 | + |
| 6 | + |
| 7 | +def get_all_breakpoints() -> Sequence[gdb.Breakpoint]: |
| 8 | + return gdb.breakpoints() |
| 9 | + |
| 10 | + |
| 11 | +def convert_to_gdb_bp_str(ptr: Optional[int] = None, |
| 12 | + func_name: Optional[str] = None, |
| 13 | + offset: Optional[int] = None) -> str: |
| 14 | + if ptr is not None: |
| 15 | + assert func_name is None and offset is None |
| 16 | + return f"*{hex(ptr)}" |
| 17 | + elif func_name is not None and offset is not None: |
| 18 | + return f"*({func_name}+{hex(offset)})" |
| 19 | + else: |
| 20 | + msg = "Breakpoint string has to consist of either <pointer> or " \ |
| 21 | + + "<func_name, offset>" |
| 22 | + raise ValueError(msg) |
| 23 | + |
| 24 | + |
| 25 | +class CustomBreakpoint(gdb.Breakpoint): |
| 26 | + _action_funcs: list[Callable[[], None]] |
| 27 | + _explicit_stop: bool |
| 28 | + |
| 29 | + def __init__(self, |
| 30 | + bp_str: str, |
| 31 | + action_funcs: Optional[list[Callable[[], None]]] = None, |
| 32 | + enabled: bool = True, |
| 33 | + explicit_stop: bool = False, |
| 34 | + temporary: bool = False): |
| 35 | + super().__init__(bp_str, temporary=temporary) |
| 36 | + if action_funcs is None: |
| 37 | + action_funcs = [] |
| 38 | + self._make_unique() |
| 39 | + self._bp_str = bp_str |
| 40 | + self.enabled = enabled |
| 41 | + self._explicit_stop = explicit_stop |
| 42 | + self._action_funcs = action_funcs |
| 43 | + self._cond_func = None |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def create_pt_bp(cls, ptr, *args, **kwargs): |
| 47 | + return cls(convert_to_gdb_bp_str(ptr=ptr), *args, **kwargs) |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def create_pie_bp(cls, ptr, *args, **kwargs): |
| 51 | + return cls(convert_to_gdb_bp_str(ptr=get_pie_base() + ptr), |
| 52 | + *args, **kwargs) |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def create_func_off_bp(cls, func_name, offset, *args, **kwargs): |
| 56 | + return cls(convert_to_gdb_bp_str(func_name=func_name, offset=offset), |
| 57 | + *args, **kwargs) |
| 58 | + |
| 59 | + def _make_unique(self): |
| 60 | + for bp in get_all_breakpoints(): |
| 61 | + if bp.number != self.number: |
| 62 | + bp.delete() |
| 63 | + |
| 64 | + def set_condition_func(self, cond_func): |
| 65 | + self._cond_func = cond_func |
| 66 | + |
| 67 | + def reset_condition_func(self): |
| 68 | + self._cond_func = None |
| 69 | + |
| 70 | + def stop(self): |
| 71 | + for bp_stop_func in self._action_funcs: |
| 72 | + bp_stop_func() |
| 73 | + if self._explicit_stop: |
| 74 | + return True |
| 75 | + if self._cond_func is not None: |
| 76 | + return self._cond_func() |
| 77 | + return False |
| 78 | + |
| 79 | + |
| 80 | +class LogBreakpoint(CustomBreakpoint): |
| 81 | + logger_func: Callable[[], bytes] |
| 82 | + |
| 83 | + def __init__(self, |
| 84 | + bp_str: str, |
| 85 | + logger_func: Callable[[], bytes], |
| 86 | + action_funcs: Optional[list[Callable[[], None]]] =None, |
| 87 | + enabled_default: bool = True, |
| 88 | + explicit_stop: bool = False, |
| 89 | + temporary: bool = False): |
| 90 | + super().__init__(bp_str, |
| 91 | + action_funcs, |
| 92 | + enabled_default, |
| 93 | + explicit_stop, |
| 94 | + temporary) |
| 95 | + self._logger_func = logger_func |
| 96 | + |
| 97 | + def stop(self): |
| 98 | + logger = Logger.get_instance() |
| 99 | + log = self._logger_func() |
| 100 | + if len(log) != 0: |
| 101 | + logger.log_line(log) |
| 102 | + return super().stop() |
| 103 | + |
| 104 | + |
| 105 | +class ActionBreakpoint(CustomBreakpoint): |
| 106 | + def __init__(self, |
| 107 | + bp_str: str, |
| 108 | + action_funcs: Optional[list[Callable[[], None]]] , |
| 109 | + explicit_stop: bool = False): |
| 110 | + super().__init__(bp_str, |
| 111 | + action_funcs, |
| 112 | + enabled_default=True, |
| 113 | + explicit_stop=explicit_stop, |
| 114 | + temporary=False) |
0 commit comments