import importlib
import json
import os
import re
import select
import shutil
import subprocess
import time
import gdb
from gdb.dap.server import capability, request
from gdb.dap.startup import DAPException
from gdb.dap.state import set_thread
server = importlib.import_module("gdb.dap.server")
frames = importlib.import_module("gdb.dap.frames")
_replay_command = HERMIT_REPLAY_COMMAND _replay_target = HERMIT_REPLAY_TARGET _client_fds = HERMIT_CLIENT_FDS _replay_process = None
_setpriv = None
_history = []
_line_breakpoints = []
_arrival_leaders = {}
_stop_address_breakpoints = {}
_line_program = None
_line_breakpoint_problem = None
_suppress_events = False
_advancing = False
_last_stopped = None
_REFUSAL_REQUEST_TIMEOUT = 30.0
_ADVANCE_LIMIT = 100
def _read_client_messages(fd, timeout):
deadline = time.monotonic() + timeout
data = b""
while True:
header_end = data.find(b"\r\n\r\n")
if header_end >= 0:
length = None
for line in data[:header_end].split(b"\r\n"):
name, _, value = line.partition(b":")
if name.strip().lower() == b"content-length":
try:
length = int(value.strip())
except ValueError:
length = None
if length is None or length < 0:
yield None
return
body = data[header_end + 4 :]
if len(body) >= length:
data = body[length:]
try:
message = json.loads(body[:length])
except ValueError:
message = None
yield message if isinstance(message, dict) else None
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return
ready, _, _ = select.select([fd], [], [], remaining)
if not ready:
return
chunk = os.read(fd, 65536)
if not chunk:
return
data += chunk
def _write_all(fd, data):
while data:
data = data[os.write(fd, data) :]
def _write_message(fd, message):
body = json.dumps(message).encode()
_write_all(fd, "Content-Length: {}\r\n\r\n".format(len(body)).encode() + body)
def _refuse(message):
text = "hermit-dap: " + message
if _client_fds is None:
gdb.write(text + "\n", gdb.STDERR)
os._exit(1)
client_in, client_out, client_err = _client_fds
try:
_write_all(client_err, (text + "\n").encode())
except OSError:
pass
request = None
try:
for incoming in _read_client_messages(client_in, _REFUSAL_REQUEST_TIMEOUT):
if incoming is None or incoming.get("type") == "request":
request = incoming
break
except OSError:
request = None
if request is not None:
reply = {
"seq": 1,
"type": "response",
"request_seq": request.get("seq", 0),
"command": request.get("command", ""),
"success": False,
"message": text,
"body": {"error": {"id": 1, "format": text, "showUser": True}},
}
else:
reply = {
"seq": 1,
"type": "event",
"event": "output",
"body": {"category": "important", "output": text + "\n"},
}
try:
_write_message(client_out, reply)
except OSError:
pass
os._exit(1)
def _unsupported_gdb(problem):
_refuse(
"managed replay does not support this GDB ({}): {}. "
"It is tested with GDB 17.2; use --gdb to select a GDB whose DAP "
"interpreter provides these hooks.".format(gdb.VERSION, problem)
)
_missing_hooks = [
name
for name in (
"send_event",
"call_function_later",
"send_gdb",
"send_gdb_with_response",
"_commands",
)
if not hasattr(server, name)
]
if _missing_hooks or not hasattr(gdb, "with_parameter"):
_unsupported_gdb(
"gdb.dap.server lacks {}".format(
", ".join(_missing_hooks or ["(gdb.with_parameter)"])
)
)
if not callable(getattr(frames, "_clear_frame_ids", None)):
_unsupported_gdb("gdb.dap.frames lacks _clear_frame_ids")
for _command in ("attach", "disconnect"):
if _command not in server._commands:
_unsupported_gdb("gdb.dap has no '{}' request".format(_command))
_event_sink_name = next(
(
name
for name in ("_send_event", "send_event")
if callable(getattr(server.Server, name, None))
),
None,
)
if _event_sink_name is None:
_unsupported_gdb("gdb.dap.server.Server has neither _send_event nor send_event")
_original_send_event = getattr(server.Server, _event_sink_name)
def _send_event(self, event, body=None):
global _last_stopped
if _suppress_events:
if event == "stopped":
_last_stopped = dict(body or {})
return
if event == "stopped" and body is not None and "hitBreakpointIds" in body:
body = dict(body)
body["hitBreakpointIds"] = [
breakpoint_id
for breakpoint_id in body["hitBreakpointIds"]
if breakpoint_id > 0
]
_original_send_event(self, event, body)
setattr(server.Server, _event_sink_name, _send_event)
def _recording():
return not _suppress_events or _advancing
_FRAME_IDENTITY_DEPTH = 8
def _frame_identity():
identity = []
try:
frame = gdb.newest_frame()
while frame is not None and len(identity) < _FRAME_IDENTITY_DEPTH:
identity.append((int(frame.pc()), int(frame.read_register("sp"))))
frame = frame.older()
except (gdb.error, AttributeError, ValueError):
pass
return tuple(identity) or None
def _describe_frames(identity):
if identity is None:
return "unknown"
return " <- ".join("{:#x}/sp {:#x}".format(pc, sp) for pc, sp in identity)
_TEST_ONLY_DROP_VARIABLE = "HERMIT_DAP_TEST_ONLY_DROP_LINE_ARRIVAL"
_test_only_drop = None
_test_only_drop_seen = 0
def _parse_test_only_drop():
value = os.environ.get(_TEST_ONLY_DROP_VARIABLE)
if value is None:
return None
match = re.fullmatch(r"([1-9][0-9]*):([1-9][0-9]*)", value)
if match is None:
return value
return (int(match.group(1)), int(match.group(2)))
def _test_only_dropped(line):
global _test_only_drop_seen
if _test_only_drop is None or _test_only_drop[0] != line:
return False
_test_only_drop_seen += 1
return _test_only_drop_seen == _test_only_drop[1]
def _append_line(pc, file, line, thread_id):
if _test_only_dropped(line):
return
_history.append(
{
"pc": pc,
"frame": _frame_identity(),
"file": os.path.realpath(file),
"line": line,
"thread_id": thread_id,
"breakpoint": False,
"breakpoint_ids": [],
"counted": False,
}
)
def _arrival_leader(pc):
if pc not in _arrival_leaders:
_arrival_leaders[pc] = min(
(
breakpoint.number
for breakpoint in _line_breakpoints
if breakpoint.is_valid()
and any(
location.address is not None and int(location.address) == pc
for location in breakpoint.locations
)
),
default=None,
)
return _arrival_leaders[pc]
class _LineBreakpoint(gdb.Breakpoint):
def __init__(self, file, line):
super().__init__(source=file, line=line, internal=True)
self.silent = True
self.file = file
self.line = line
def stop(self):
if not _recording():
return False
try:
pc = int(gdb.newest_frame().pc())
if _arrival_leader(pc) in (None, self.number):
_append_line(
pc,
self.file,
self.line,
gdb.selected_thread().global_num,
)
except (gdb.error, AttributeError):
pass
return _advancing
class _StopAddressBreakpoint(gdb.Breakpoint):
def __init__(self, pc):
super().__init__("*{:#x}".format(pc), internal=True)
self.silent = True
self.pc = pc
def stop(self):
if not _recording():
return False
try:
if int(gdb.newest_frame().pc()) == self.pc:
position = _source_position(None) or {
"pc": self.pc,
"frame": _frame_identity(),
"file": None,
"line": 0,
"thread_id": gdb.selected_thread().global_num,
"breakpoint": False,
"breakpoint_ids": [],
}
position["counted"] = True
_history.append(position)
except (gdb.error, AttributeError):
pass
return False
def _line_table_addresses(program):
decoded = subprocess.run(
["readelf", "--wide", "--debug-dump=decodedline", program],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
).stdout
addresses = set()
for row in decoded.splitlines():
match = re.search(r"(?:^|\s)\d+\s+0x([0-9a-fA-F]+)(?:\s|$)", row)
if match is not None:
addresses.add(int(match.group(1), 16))
return addresses
def _source_lines(addresses):
progspace = gdb.current_progspace()
symtabs = {}
for address in addresses:
symtab = progspace.find_pc_line(address).symtab
if symtab is not None:
block = symtab.static_block()
key = (symtab.fullname(), block.start, block.end)
symtabs.setdefault(key, symtab)
source_lines = set()
for (file, _, _), symtab in symtabs.items():
source_lines.update((file, line) for line in symtab.linetable().source_lines())
return source_lines
def _unknown_source_file(file, line):
try:
gdb.decode_line('"{}":{}'.format(file, line))
except gdb.error as error:
if str(error).startswith("No source file named"):
return str(error)
return None
def _install_line_breakpoints(event=None):
global _line_breakpoint_problem
global _line_program
global _suppress_events
program = gdb.current_progspace().filename
if program is None or program == _line_program or not os.path.isfile(program):
return
_line_program = program
_line_breakpoint_problem = None
problem = None
try:
addresses = _line_table_addresses(program)
except (OSError, subprocess.CalledProcessError) as error:
addresses = set()
problem = "failed to read the line tables of {}: {}".format(program, error)
source_lines = _source_lines(addresses)
if problem is None and not addresses:
problem = "readelf lists no line-table rows in {}; build it with -g".format(
program
)
elif problem is None and not source_lines:
problem = "GDB has no source line at any of the {} addresses in {}".format(
len(addresses), program
)
previous_suppression = _suppress_events
_suppress_events = True
_arrival_leaders.clear()
placed = set()
unplaced = {}
try:
for file, line in sorted(source_lines):
try:
breakpoint = _LineBreakpoint(file, line)
except gdb.error:
unplaced.setdefault(file, line)
continue
if breakpoint.pending:
breakpoint.delete()
unplaced.setdefault(file, line)
else:
placed.add(file)
_line_breakpoints.append(breakpoint)
finally:
_suppress_events = previous_suppression
if problem is None and not placed:
problem = "GDB placed none of the {} source-line breakpoints in {}".format(
len(source_lines), program
)
if problem is None:
for file in sorted(set(unplaced) - placed):
unknown = _unknown_source_file(file, unplaced[file])
if unknown is not None:
problem = "no source-line breakpoint could be set in {}: {}".format(
file, unknown
)
break
if problem is not None:
_line_breakpoint_problem = problem
server.send_event(
"output",
{
"category": "stderr",
"output": "hermit-dap: stepBack and reverseContinue are "
"unavailable: {}\n".format(problem),
},
)
def _require_line_breakpoints():
if _line_breakpoint_problem is not None:
raise DAPException(
"Hermit reverse execution is unavailable: " + _line_breakpoint_problem
)
def _source_position(event):
try:
frame = gdb.newest_frame()
sal = frame.find_sal()
if sal.symtab is None or sal.line <= 0:
return None
breakpoint_ids = []
if isinstance(event, gdb.BreakpointEvent):
breakpoint_ids = [
breakpoint.number
for breakpoint in event.breakpoints
if breakpoint.visible
]
return {
"pc": int(frame.pc()),
"frame": _frame_identity(),
"file": os.path.realpath(sal.symtab.fullname()),
"line": sal.line,
"thread_id": gdb.selected_thread().global_num,
"breakpoint": bool(breakpoint_ids),
"breakpoint_ids": breakpoint_ids,
"counted": False,
}
except (gdb.error, AttributeError):
return None
def _remember_stop(event):
global _suppress_events
if _suppress_events:
return
position = _source_position(event)
if position is None:
return
if (
_history
and _history[-1]["pc"] == position["pc"]
and _history[-1]["thread_id"] == position["thread_id"]
):
_history[-1].update(position)
else:
_history.append(position)
pc = position["pc"]
if _arrival_leader(pc) is None and pc not in _stop_address_breakpoints:
_suppress_events = True
try:
_stop_address_breakpoints[pc] = _StopAddressBreakpoint(pc)
except gdb.error:
pass
finally:
_suppress_events = False
def _wait_for_replay(timeout=5.0):
global _replay_process
if _replay_process is None:
return
try:
_replay_process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
_replay_process.kill()
_replay_process.wait()
_replay_process = None
def _terminate_replay():
if _replay_process is None:
return
_replay_process.kill()
_wait_for_replay(0)
def _start_replay():
global _replay_process
_replay_process = subprocess.Popen(
[_setpriv, "--pdeathsig", "SIGKILL", "--"] + _replay_command,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
close_fds=True,
)
time.sleep(0.5)
status = _replay_process.poll()
if status is not None:
_replay_process = None
raise gdb.error("Hermit replay exited before GDB attached: {}".format(status))
def _connect_replay():
last_error = None
for _ in range(200):
try:
gdb.execute(
"target remote " + _replay_target, from_tty=False, to_string=True
)
return
except gdb.error as error:
last_error = error
time.sleep(0.05)
raise gdb.error("Hermit replay did not accept a GDB connection: " + str(last_error))
def _recording_breakpoints():
return [
breakpoint
for breakpoint in (gdb.breakpoints() or [])
if breakpoint.is_valid()
and breakpoint.enabled
and (
breakpoint.visible
or isinstance(breakpoint, (_LineBreakpoint, _StopAddressBreakpoint))
)
]
def _restart_replay():
with gdb.with_parameter("confirm", False):
try:
gdb.execute("kill", from_tty=False, to_string=True)
except gdb.error:
pass
_wait_for_replay()
try:
gdb.execute("disconnect", from_tty=False, to_string=True)
except gdb.error:
pass
_start_replay()
_connect_replay()
frames._clear_frame_ids(None)
class _WrongFrame(Exception):
pass
def _run_to(pc, occurrence, frame=None):
target = gdb.Breakpoint(
"*{:#x}".format(pc),
type=gdb.BP_BREAKPOINT,
internal=True,
temporary=True,
)
try:
target.ignore_count = max(0, occurrence - 1)
gdb.execute("continue", from_tty=False, to_string=True)
if int(gdb.newest_frame().pc()) != pc:
raise gdb.error("replay did not stop at the requested source position")
landed = _frame_identity()
if frame is not None and landed != frame:
raise _WrongFrame(
"arrival {} at {:#x} has the frames {}, but the history entry "
"was recorded with {}".format(
occurrence,
pc,
_describe_frames(landed),
_describe_frames(frame),
)
)
finally:
if target.is_valid():
target.delete()
class _ArrivalCounter(gdb.Breakpoint):
def __init__(self, pc):
super().__init__("*{:#x}".format(pc), internal=True)
self.silent = True
self.pc = pc
self.arrivals = 0
def stop(self):
try:
if int(gdb.newest_frame().pc()) == self.pc:
self.arrivals += 1
except gdb.error:
pass
return False
def _count_arrivals(pc, marker):
global _suppress_events
disabled_breakpoints = []
counter = None
_suppress_events = True
try:
disabled_breakpoints = _recording_breakpoints()
for breakpoint in disabled_breakpoints:
breakpoint.enabled = False
_restart_replay()
counter = _ArrivalCounter(pc)
if marker is None:
gdb.execute("continue", from_tty=False, to_string=True)
if gdb.selected_inferior().pid != 0:
raise gdb.error("replay stopped before it exited")
else:
_run_to(*marker)
return counter.arrivals
finally:
if counter is not None and counter.is_valid():
counter.delete()
for breakpoint in disabled_breakpoints:
if breakpoint.is_valid():
breakpoint.enabled = True
_suppress_events = False
def _advance_to_a_line():
global _advancing
global _suppress_events
disabled_breakpoints = [
breakpoint
for breakpoint in (gdb.breakpoints() or [])
if breakpoint.is_valid() and breakpoint.enabled and breakpoint.visible
]
_suppress_events = True
_advancing = True
try:
for breakpoint in disabled_breakpoints:
breakpoint.enabled = False
gdb.execute("continue", from_tty=False, to_string=True)
finally:
for breakpoint in disabled_breakpoints:
if breakpoint.is_valid():
breakpoint.enabled = True
_advancing = False
_suppress_events = False
return gdb.selected_inferior().pid != 0
def _line_entry_after(index):
return next(
(
later
for later in range(index + 1, len(_history))
if _arrival_leader(_history[later]["pc"]) is not None
),
None,
)
def _line_entry_reached_after(index):
marker_index = _line_entry_after(index)
for _ in range(_ADVANCE_LIMIT):
if marker_index is not None:
return marker_index
if gdb.selected_inferior().pid == 0 or not _advance_to_a_line():
return None
marker_index = _line_entry_after(index)
if marker_index is None:
raise gdb.error("replay did not reach a source line")
return marker_index
def _occurrence(index, advance=True):
entry = _history[index]
pc = entry["pc"]
if _arrival_leader(pc) is not None:
return sum(1 for earlier in _history[: index + 1] if earlier["pc"] == pc)
if advance:
marker_index = _line_entry_reached_after(index)
else:
marker_index = _line_entry_after(index)
if marker_index is None:
raise gdb.error("no later source line to count arrivals against")
if marker_index is None:
arrivals = _count_arrivals(pc, None)
later = _history[index + 1 :]
else:
marker = _history[marker_index]
arrivals = _count_arrivals(
pc, (marker["pc"], _occurrence(marker_index), marker["frame"])
)
later = _history[index + 1 : marker_index]
occurrence = arrivals - sum(1 for entry_after in later if entry_after["pc"] == pc)
if occurrence < 1:
raise gdb.error("replay never reached the requested source position")
return occurrence
def _rewind(target_index, reason, breakpoint_ids=None, occurrence=None):
global _last_stopped
global _suppress_events
position = None
if target_index >= 0:
position = _history[target_index]
if occurrence is None:
occurrence = _occurrence(target_index)
disabled_breakpoints = []
_last_stopped = None
_suppress_events = True
try:
disabled_breakpoints = _recording_breakpoints()
for breakpoint in disabled_breakpoints:
breakpoint.enabled = False
_restart_replay()
if position is not None:
_run_to(position["pc"], occurrence, position["frame"])
body = dict(_last_stopped or {})
body["reason"] = reason
body["threadId"] = gdb.selected_thread().global_num
body["allThreadsStopped"] = True
if breakpoint_ids is not None:
body["hitBreakpointIds"] = breakpoint_ids
else:
body.pop("hitBreakpointIds", None)
finally:
for breakpoint in disabled_breakpoints:
if breakpoint.is_valid():
breakpoint.enabled = True
_suppress_events = False
if target_index >= 0:
del _history[target_index + 1 :]
else:
_history.clear()
return body
class _StayedAtCurrentStop(Exception):
def __init__(self, message, body):
super().__init__(message)
self.body = body
def _rewind_or_stay(target_index, reason, breakpoint_ids=None):
current_index = len(_history) - 1
current_occurrence = None
if (
target_index >= 0
and current_index >= 0
and _arrival_leader(_history[current_index]["pc"]) is None
):
if _line_entry_reached_after(current_index) is None:
current_occurrence = _occurrence(current_index)
try:
return _rewind(target_index, reason, breakpoint_ids)
except _WrongFrame as wrong:
problem = str(wrong)
if current_index < 0:
raise gdb.error("the rewind landed in the wrong frame: " + problem)
if current_occurrence is None:
current_occurrence = _occurrence(current_index, advance=False)
body = _rewind(current_index, "step", None, current_occurrence)
body["description"] = "Returned to the current stop"
raise _StayedAtCurrentStop(
"Hermit could not reach the earlier stop exactly, so the replay stays "
"at the current stop ({}).".format(problem),
body,
)
def _report_reverse_failure(operation, error):
server.send_event(
"output",
{
"category": "stderr",
"output": "hermit-dap: {} failed: {}\n".format(operation, error),
},
)
server.send_event("terminated")
def _reverse_request(operation, run):
try:
body = server.send_gdb_with_response(run)
except _StayedAtCurrentStop as stayed:
server.call_function_later(
lambda body=stayed.body: server.send_event("stopped", body)
)
raise DAPException(str(stayed))
except DAPException:
raise
except BaseException as error:
server.call_function_later(
lambda error=error: _report_reverse_failure(operation, error)
)
raise
server.call_function_later(lambda: server.send_event("stopped", body))
def _step_back(thread_id, granularity):
if granularity != "statement":
raise DAPException("Hermit stepBack currently supports statement granularity")
_require_line_breakpoints()
set_thread(thread_id)
matching = [
index
for index, position in enumerate(_history)
if position["thread_id"] == thread_id and not position["counted"]
]
target_index = matching[-2] if len(matching) >= 2 else -1
return _rewind_or_stay(target_index, "step")
@capability("supportsStepBack")
@request("stepBack", on_dap_thread=True, defer_events=False)
def step_back(
*, threadId: int, singleThread: bool = False, granularity: str = "statement", **args
):
if singleThread:
raise DAPException("Hermit reverse execution restarts the whole replay")
_reverse_request("stepBack", lambda: _step_back(threadId, granularity))
def _visible_breakpoints_by_pc():
result = {}
for breakpoint in gdb.breakpoints() or []:
if (
not breakpoint.is_valid()
or not breakpoint.enabled
or not breakpoint.visible
):
continue
for location in breakpoint.locations:
if location.enabled and location.address is not None:
result.setdefault(int(location.address), []).append(breakpoint.number)
return result
def _reverse_continue(thread_id):
_require_line_breakpoints()
set_thread(thread_id)
breakpoints = _visible_breakpoints_by_pc()
target_index = next(
(
index
for index in range(len(_history) - 2, -1, -1)
if _history[index]["pc"] in breakpoints
),
-1,
)
if target_index >= 0:
return _rewind_or_stay(
target_index, "breakpoint", breakpoints[_history[target_index]["pc"]]
)
return _rewind_or_stay(target_index, "entry")
@request("reverseContinue", on_dap_thread=True, defer_events=False)
def reverse_continue(*, threadId: int, singleThread: bool = False, **args):
if singleThread:
raise DAPException("Hermit reverse execution restarts the whole replay")
_reverse_request("reverseContinue", lambda: _reverse_continue(threadId))
def _kill_replay_for_disconnect():
global _suppress_events
previous_suppression = _suppress_events
_suppress_events = True
try:
with gdb.with_parameter("confirm", False):
try:
gdb.execute("kill", from_tty=False, to_string=True)
except gdb.error:
pass
_wait_for_replay()
finally:
_suppress_events = previous_suppression
_original_attach = server._commands["attach"]
_original_disconnect = server._commands["disconnect"]
def _attach(**args):
if args.get("target") != _replay_target:
raise DAPException(
"managed replay requires DAP target {}".format(_replay_target)
)
return _original_attach(**args)
def _disconnect(**args):
try:
server.send_gdb_with_response(_kill_replay_for_disconnect)
except Exception:
pass
args["terminateDebuggee"] = False
return _original_disconnect(**args)
server._commands["attach"] = _attach
server._commands["disconnect"] = _disconnect
def _cleanup(event):
_terminate_replay()
_setpriv = (
"/usr/bin/setpriv"
if os.path.isfile("/usr/bin/setpriv")
else shutil.which("setpriv")
)
_missing_tools = [
name
for name, path in (("readelf", shutil.which("readelf")), ("setpriv", _setpriv))
if path is None
]
if _missing_tools:
_refuse(
"managed replay requires readelf and setpriv on PATH (missing: {})".format(
", ".join(_missing_tools)
)
)
_test_only_drop = _parse_test_only_drop()
if isinstance(_test_only_drop, str):
_refuse(
"{} must be LINE:K with positive integers, not {!r}".format(
_TEST_ONLY_DROP_VARIABLE, _test_only_drop
)
)
gdb.events.new_objfile.connect(_install_line_breakpoints)
gdb.events.stop.connect(_remember_stop)
gdb.events.gdb_exiting.connect(_cleanup)
try:
_start_replay()
except Exception as error:
_refuse("failed to start replay: {}".format(error))
if _client_fds is not None:
for _fd in _client_fds:
try:
os.close(_fd)
except OSError:
pass
_client_fds = None