emdbg.debug.remote.gdb
1# Copyright (c) 2023, Auterion AG 2# SPDX-License-Identifier: BSD-3-Clause 3 4from __future__ import annotations 5from contextlib import contextmanager 6import time 7 8 9class Interface: 10 """ 11 The interface of all GDB remote access implementations. 12 """ 13 14 def __init__(self, backend: "emdbg.debug.ProbeBackend"): 15 self.backend = backend 16 self.interrupt_nesting = 0 17 self.interrupted = True 18 self.type = "base" 19 20 def execute(self, command: str, timeout: float = 1, to_string: bool = False) -> str | None: 21 """ 22 Executes a command on the GDB command prompt and waits for a response. 23 If `to_string` is set, it will return the response as a string. 24 25 :param command: The command string to send to the GDB prompt. 26 :param timeout: How long to wait for a response in seconds. 27 :param to_string: Capture the response as a string and return it. 28 """ 29 raise NotImplementedError 30 31 @contextmanager 32 def interrupt_continue(self): 33 """ 34 Interrupts and yields, then continues. 35 If the target was already interrupted before entering this scope, it 36 will not continue automatically, so that this context can be used in 37 nested calls. 38 """ 39 try: 40 interrupted = self.interrupt_and_wait() 41 self.interrupt_nesting += 1 42 yield 43 finally: 44 self.interrupt_nesting -= 1 45 if self.interrupt_nesting == 0 and interrupted: 46 self.continue_nowait() 47 48 def interrupt_and_wait(self) -> bool: 49 """ 50 Interrupt the program and wait until it stops. 51 52 :return: `True` if GDB has been interrupted, `False` if it was already interrupted. 53 """ 54 if not self.interrupted: 55 self.execute("interrupt") 56 self.interrupted = True 57 time.sleep(0.2) 58 return True 59 return False 60 61 def continue_nowait(self) -> bool: 62 """ 63 Continue the program. Do not wait until it stops again. 64 65 :return: `True` if GDB continues running, `False` if it was already running. 66 """ 67 if self.interrupted: 68 self.execute("continue&") 69 self.interrupted = False 70 time.sleep(0.2) 71 return True 72 return False 73 74 def quit(self): 75 """Terminate GDB.""" 76 raise NotImplementedError
class
Interface:
10class Interface: 11 """ 12 The interface of all GDB remote access implementations. 13 """ 14 15 def __init__(self, backend: "emdbg.debug.ProbeBackend"): 16 self.backend = backend 17 self.interrupt_nesting = 0 18 self.interrupted = True 19 self.type = "base" 20 21 def execute(self, command: str, timeout: float = 1, to_string: bool = False) -> str | None: 22 """ 23 Executes a command on the GDB command prompt and waits for a response. 24 If `to_string` is set, it will return the response as a string. 25 26 :param command: The command string to send to the GDB prompt. 27 :param timeout: How long to wait for a response in seconds. 28 :param to_string: Capture the response as a string and return it. 29 """ 30 raise NotImplementedError 31 32 @contextmanager 33 def interrupt_continue(self): 34 """ 35 Interrupts and yields, then continues. 36 If the target was already interrupted before entering this scope, it 37 will not continue automatically, so that this context can be used in 38 nested calls. 39 """ 40 try: 41 interrupted = self.interrupt_and_wait() 42 self.interrupt_nesting += 1 43 yield 44 finally: 45 self.interrupt_nesting -= 1 46 if self.interrupt_nesting == 0 and interrupted: 47 self.continue_nowait() 48 49 def interrupt_and_wait(self) -> bool: 50 """ 51 Interrupt the program and wait until it stops. 52 53 :return: `True` if GDB has been interrupted, `False` if it was already interrupted. 54 """ 55 if not self.interrupted: 56 self.execute("interrupt") 57 self.interrupted = True 58 time.sleep(0.2) 59 return True 60 return False 61 62 def continue_nowait(self) -> bool: 63 """ 64 Continue the program. Do not wait until it stops again. 65 66 :return: `True` if GDB continues running, `False` if it was already running. 67 """ 68 if self.interrupted: 69 self.execute("continue&") 70 self.interrupted = False 71 time.sleep(0.2) 72 return True 73 return False 74 75 def quit(self): 76 """Terminate GDB.""" 77 raise NotImplementedError
The interface of all GDB remote access implementations.
Interface(backend: emdbg.debug.backend.ProbeBackend)
def
execute( self, command: str, timeout: float = 1, to_string: bool = False) -> str | None:
21 def execute(self, command: str, timeout: float = 1, to_string: bool = False) -> str | None: 22 """ 23 Executes a command on the GDB command prompt and waits for a response. 24 If `to_string` is set, it will return the response as a string. 25 26 :param command: The command string to send to the GDB prompt. 27 :param timeout: How long to wait for a response in seconds. 28 :param to_string: Capture the response as a string and return it. 29 """ 30 raise NotImplementedError
Executes a command on the GDB command prompt and waits for a response.
If to_string
is set, it will return the response as a string.
Parameters
- command: The command string to send to the GDB prompt.
- timeout: How long to wait for a response in seconds.
- to_string: Capture the response as a string and return it.
@contextmanager
def
interrupt_continue(self):
32 @contextmanager 33 def interrupt_continue(self): 34 """ 35 Interrupts and yields, then continues. 36 If the target was already interrupted before entering this scope, it 37 will not continue automatically, so that this context can be used in 38 nested calls. 39 """ 40 try: 41 interrupted = self.interrupt_and_wait() 42 self.interrupt_nesting += 1 43 yield 44 finally: 45 self.interrupt_nesting -= 1 46 if self.interrupt_nesting == 0 and interrupted: 47 self.continue_nowait()
Interrupts and yields, then continues. If the target was already interrupted before entering this scope, it will not continue automatically, so that this context can be used in nested calls.
def
interrupt_and_wait(self) -> bool:
49 def interrupt_and_wait(self) -> bool: 50 """ 51 Interrupt the program and wait until it stops. 52 53 :return: `True` if GDB has been interrupted, `False` if it was already interrupted. 54 """ 55 if not self.interrupted: 56 self.execute("interrupt") 57 self.interrupted = True 58 time.sleep(0.2) 59 return True 60 return False
Interrupt the program and wait until it stops.
Returns
True
if GDB has been interrupted,False
if it was already interrupted.
def
continue_nowait(self) -> bool:
62 def continue_nowait(self) -> bool: 63 """ 64 Continue the program. Do not wait until it stops again. 65 66 :return: `True` if GDB continues running, `False` if it was already running. 67 """ 68 if self.interrupted: 69 self.execute("continue&") 70 self.interrupted = False 71 time.sleep(0.2) 72 return True 73 return False
Continue the program. Do not wait until it stops again.
Returns
True
if GDB continues running,False
if it was already running.