Bug 5 (was still present): Run from Stopped re-ran the stored script and ignored the Target/Args entries. _on_run() now compares the entries with the loaded target and starts the new target when they differ. Bug 8, staged-row variant (found by reviewing the live screenshot): staged breakpoints share id 0, so a checkbox toggle / Remove / Edit on one staged row landed on the first staged breakpoint instead — the store row flipped but the session flag went to the wrong bp, and the next refresh showed the wrong row unchecked. Session lookups now match by (id, location) via _find_bp(); the UI tracks the selected row location and the refresh re-selects by (id, location). Tests: test_run_from_stopped_uses_new_target, test_staged_toggle_hits_the_right_row. Full suite 28/28 under Xvfb; Stop on a Gtk.main() debuggee verified live (0.02s, reaped). Docstrings in debugger.py/debugger_ui.py updated (they are the debugger docs; architecture.md already points at them).
790 lines
33 KiB
Python
790 lines
33 KiB
Python
"""Debugger window — GTK UI over the pdb session (debugger.py).
|
|
|
|
Mirrors the C debugger window layout (src/debugger_ui.cpp): a
|
|
toolbar (Run / Continue / Step Over / Step Into / Step Out / Stop /
|
|
Restart / Settings), a Target row (script + args + Browse), a status
|
|
line, a middle pane with the breakpoint list on the left and
|
|
Locals / Watches / Stack on the right, and the debuggee's output
|
|
console at the bottom. The window polls the session on a 200ms GTK
|
|
timeout; all pdb traffic stays in the backend.
|
|
|
|
Placement note (vs C): the C window is a separate top-level window
|
|
opened from the Debug menu, and so is this one — same placement. It
|
|
is opened from the Code window's Debug menu (the menu bar lives in
|
|
the Code window in this port) and its target defaults to the
|
|
generated script.
|
|
|
|
The eight documented C debugger bugs, UI half (session half in
|
|
debugger.py):
|
|
|
|
1. Breakpoint pane too narrow — the pane requests 420px wide and
|
|
the Location column 320px minimum, so ``file:line`` is readable.
|
|
2. List selection doesn't stick — _refresh_breakpoints() updates
|
|
store rows IN PLACE by breakpoint id (never rebuilds the model)
|
|
and re-selects the previously selected id afterwards; the 150ms
|
|
poll rebuild that lost selection in C cannot happen here.
|
|
3. Step Out in main() modal — no modal exists; session.step_out()
|
|
degrades to Continue at module level.
|
|
4. Stop times out on a GUI app — session.stop() kills the process
|
|
group; the Stop button just calls it.
|
|
5. Run from Stopped/Exited ignores Target — Run re-reads the Target
|
|
and Args entries on every press: if either changed since the
|
|
session started, it starts the new target (session.start);
|
|
otherwise it re-runs the stored one (session.run). There is no
|
|
stale-target path.
|
|
6. break-on-main temp breakpoint persisted — no temp breakpoints
|
|
exist (see debugger.py).
|
|
7. Watch clicks open a search popup — the watch and locals trees
|
|
have search disabled (set_enable_search(False)) and there is no
|
|
SearchEntry anywhere in this window, so clicking a row can only
|
|
select it.
|
|
8. Enabled-checkbox toggles ignored — the toggle handler applies
|
|
the change to the session FIRST and then updates the store row
|
|
directly; the row is never rebuilt under it. Staged rows share
|
|
id 0, so the toggle/remove/edit look the row up by
|
|
(id, location), not id alone — otherwise the change lands on
|
|
the first staged breakpoint instead of the clicked row.
|
|
"""
|
|
|
|
import os
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import Gdk, GLib, Gtk # noqa: E402
|
|
|
|
from lightgui.debugger import (PdbSession, STATE_EXITED, STATE_IDLE, # noqa: E402
|
|
STATE_RUNNING, STATE_STOPPED,
|
|
parse_breakpoint_location)
|
|
|
|
# The UI never waits longer than this for a stop after an exec
|
|
# command; a longer-running target is picked up by the poll.
|
|
UI_EXEC_TIMEOUT_S = 0.5
|
|
|
|
# Bug 1: breakpoint pane wide enough to read file:line.
|
|
BP_PANE_MIN_WIDTH = 420
|
|
BP_LOCATION_MIN_WIDTH = 320
|
|
|
|
|
|
class DebuggerWindow:
|
|
"""The debugger top-level window. Owns one PdbSession."""
|
|
|
|
def __init__(self, parent=None, target=None, on_status=None,
|
|
on_stop_location=None):
|
|
self.session = PdbSession()
|
|
self.on_status = on_status
|
|
# Called with (file, line) on every fresh stop, so the app
|
|
# can scroll the Code window to the stopped line.
|
|
self.on_stop_location = on_stop_location
|
|
self._selected_bp_id = None
|
|
self._selected_bp_location = None
|
|
self._output_shown = 0
|
|
self._last_stop_seq = 0
|
|
self._poll_id = None
|
|
|
|
self.window = Gtk.Window(title="LightGUI — Debugger")
|
|
self.window.set_default_size(1100, 700)
|
|
if parent is not None:
|
|
self.window.set_transient_for(parent)
|
|
self.window.connect("delete-event", self._on_delete)
|
|
self.window.connect("key-press-event", self._on_key_press)
|
|
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
|
|
self.window.add(vbox)
|
|
|
|
toolbar = self._build_toolbar()
|
|
vbox.pack_start(toolbar, False, False, 0)
|
|
vbox.pack_start(self._build_target_row(), False, False, 0)
|
|
|
|
self.status_label = Gtk.Label(label="Idle", xalign=0)
|
|
self.status_label.set_margin_start(6)
|
|
vbox.pack_start(self.status_label, False, False, 0)
|
|
self.source_label = Gtk.Label(label="", xalign=0)
|
|
self.source_label.set_margin_start(6)
|
|
vbox.pack_start(self.source_label, False, False, 0)
|
|
|
|
mid = Gtk.Paned(orientation=Gtk.Orientation.HORIZONTAL)
|
|
mid.set_position(430)
|
|
mid.pack1(self._build_breakpoint_pane(), True, False)
|
|
mid.pack2(self._build_inspect_pane(), True, False)
|
|
vbox.pack_start(mid, True, True, 0)
|
|
|
|
# Phase 7: margins so the label cannot crowd the middle
|
|
# pane above it (it used to butt directly against it, under
|
|
# the old bottom-placed "Breakpoints" label).
|
|
self.output_label = Gtk.Label(label="Output", xalign=0)
|
|
self.output_label.set_margin_start(6)
|
|
self.output_label.set_margin_top(6)
|
|
vbox.pack_start(self.output_label, False, False, 0)
|
|
self.output_view = Gtk.TextView()
|
|
self.output_view.set_editable(False)
|
|
self.output_view.set_monospace(True)
|
|
self.output_buffer = self.output_view.get_buffer()
|
|
output_scroll = Gtk.ScrolledWindow()
|
|
output_scroll.set_policy(Gtk.PolicyType.AUTOMATIC,
|
|
Gtk.PolicyType.AUTOMATIC)
|
|
output_scroll.set_size_request(-1, 150)
|
|
output_scroll.add(self.output_view)
|
|
vbox.pack_start(output_scroll, False, True, 0)
|
|
|
|
vbox.show_all()
|
|
if target:
|
|
self.set_target(target)
|
|
self._update_buttons()
|
|
self._poll_id = GLib.timeout_add(200, self._poll)
|
|
|
|
# --- construction -------------------------------------------------------
|
|
|
|
def _build_toolbar(self):
|
|
toolbar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
|
|
spacing=4)
|
|
toolbar.set_margin_start(4)
|
|
toolbar.set_margin_end(4)
|
|
toolbar.set_margin_top(4)
|
|
toolbar.set_margin_bottom(4)
|
|
self._toolbar_buttons = {}
|
|
for label, name, callback in (
|
|
("Run", "run", self._on_run),
|
|
("Continue (F5)", "continue", self._on_continue),
|
|
("Step Over (F10)", "step_over", self._on_step_over),
|
|
("Step Into (F11)", "step_into", self._on_step_into),
|
|
("Step Out", "step_out", self._on_step_out),
|
|
("Stop", "stop", self._on_stop),
|
|
("Restart", "restart", self._on_restart),
|
|
("Settings", "settings", self._on_settings)):
|
|
button = Gtk.Button(label=label)
|
|
button.connect("clicked", lambda _b, cb=callback: cb())
|
|
toolbar.pack_start(button, False, False, 0)
|
|
self._toolbar_buttons[name] = button
|
|
return toolbar
|
|
|
|
def _build_target_row(self):
|
|
row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
|
|
row.set_margin_start(4)
|
|
row.set_margin_end(4)
|
|
row.set_margin_bottom(4)
|
|
row.pack_start(Gtk.Label(label="Target:"), False, False, 0)
|
|
self.target_entry = Gtk.Entry()
|
|
self.target_entry.set_hexpand(True)
|
|
row.pack_start(self.target_entry, True, True, 0)
|
|
browse = Gtk.Button(label="Browse…")
|
|
browse.connect("clicked", self._on_browse)
|
|
row.pack_start(browse, False, False, 0)
|
|
row.pack_start(Gtk.Label(label="Args:"), False, False, 0)
|
|
self.args_entry = Gtk.Entry()
|
|
self.args_entry.set_hexpand(True)
|
|
row.pack_start(self.args_entry, True, True, 0)
|
|
return row
|
|
|
|
def _build_breakpoint_pane(self):
|
|
"""Left pane: add/edit/remove row + breakpoint list. Bug 1:
|
|
420px minimum width, 320px Location column."""
|
|
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=4)
|
|
box.set_margin_start(4)
|
|
box.set_margin_end(4)
|
|
|
|
# Phase 7: the pane label sits ABOVE its list (like "Locals"
|
|
# in the right pane). It used to sit below the list, directly
|
|
# above the "Output" label, and the two labels read as one
|
|
# cramped/overlapping block at the bottom-left.
|
|
self.bp_label = Gtk.Label(label="Breakpoints", xalign=0)
|
|
box.pack_start(self.bp_label, False, False, 0)
|
|
|
|
row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
|
|
self.bp_entry = Gtk.Entry()
|
|
self.bp_entry.set_placeholder_text("file.py:line")
|
|
self.bp_entry.set_hexpand(True)
|
|
self.bp_entry.connect("activate", lambda _e: self._on_bp_add())
|
|
row.pack_start(self.bp_entry, True, True, 0)
|
|
self.bp_add_button = Gtk.Button(label="Add")
|
|
self.bp_add_button.connect("clicked",
|
|
lambda _b: self._on_bp_add())
|
|
row.pack_start(self.bp_add_button, False, False, 0)
|
|
self.bp_edit_button = Gtk.Button(label="Edit…")
|
|
self.bp_edit_button.connect("clicked",
|
|
lambda _b: self._on_bp_edit())
|
|
row.pack_start(self.bp_edit_button, False, False, 0)
|
|
self.bp_remove_button = Gtk.Button(label="Remove")
|
|
self.bp_remove_button.connect("clicked",
|
|
lambda _b: self._on_bp_remove())
|
|
row.pack_start(self.bp_remove_button, False, False, 0)
|
|
box.pack_start(row, False, False, 0)
|
|
|
|
# Columns: id, enabled, location, hits.
|
|
self.bp_store = Gtk.ListStore(int, bool, str, int)
|
|
tree = Gtk.TreeView(model=self.bp_store)
|
|
tree.set_headers_visible(True)
|
|
tree.set_enable_search(False)
|
|
toggle = Gtk.CellRendererToggle()
|
|
toggle.connect("toggled", self._on_bp_toggled)
|
|
col = Gtk.TreeViewColumn("On", toggle, active=1)
|
|
col.set_min_width(40)
|
|
tree.append_column(col)
|
|
text = Gtk.CellRendererText()
|
|
col = Gtk.TreeViewColumn("Location", text, text=2)
|
|
col.set_min_width(BP_LOCATION_MIN_WIDTH)
|
|
col.set_expand(True)
|
|
tree.append_column(col)
|
|
hits = Gtk.CellRendererText()
|
|
col = Gtk.TreeViewColumn("Hits", hits, text=3)
|
|
col.set_min_width(48)
|
|
tree.append_column(col)
|
|
tree.get_selection().connect("changed",
|
|
self._on_bp_selection_changed)
|
|
self.bp_tree = tree
|
|
|
|
scrolled = Gtk.ScrolledWindow()
|
|
scrolled.set_policy(Gtk.PolicyType.AUTOMATIC,
|
|
Gtk.PolicyType.AUTOMATIC)
|
|
scrolled.set_size_request(BP_PANE_MIN_WIDTH, -1)
|
|
scrolled.add(tree)
|
|
box.pack_start(scrolled, True, True, 0)
|
|
return box
|
|
|
|
def _build_inspect_pane(self):
|
|
"""Right pane: Locals, Watches (entry row + list), Stack."""
|
|
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=4)
|
|
box.set_margin_start(4)
|
|
box.set_margin_end(4)
|
|
|
|
box.pack_start(Gtk.Label(label="Locals", xalign=0),
|
|
False, False, 0)
|
|
# Columns: name, type, value.
|
|
self.locals_store = Gtk.ListStore(str, str, str)
|
|
tree = Gtk.TreeView(model=self.locals_store)
|
|
# Bug 7: no search popup — search disabled, no SearchEntry.
|
|
tree.set_enable_search(False)
|
|
for title, index in (("Name", 0), ("Type", 1), ("Value", 2)):
|
|
renderer = Gtk.CellRendererText()
|
|
col = Gtk.TreeViewColumn(title, renderer, text=index)
|
|
if index == 2:
|
|
col.set_expand(True)
|
|
tree.append_column(col)
|
|
locals_scroll = Gtk.ScrolledWindow()
|
|
locals_scroll.set_policy(Gtk.PolicyType.AUTOMATIC,
|
|
Gtk.PolicyType.AUTOMATIC)
|
|
locals_scroll.set_size_request(-1, 140)
|
|
locals_scroll.add(tree)
|
|
box.pack_start(locals_scroll, True, True, 0)
|
|
|
|
watch_row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
|
|
spacing=4)
|
|
self.watch_entry = Gtk.Entry()
|
|
self.watch_entry.set_placeholder_text("watch expression")
|
|
self.watch_entry.set_hexpand(True)
|
|
self.watch_entry.connect("activate",
|
|
lambda _e: self._on_watch_add())
|
|
watch_row.pack_start(self.watch_entry, True, True, 0)
|
|
watch_add = Gtk.Button(label="Add Watch")
|
|
watch_add.connect("clicked", lambda _b: self._on_watch_add())
|
|
watch_row.pack_start(watch_add, False, False, 0)
|
|
watch_remove = Gtk.Button(label="Remove")
|
|
watch_remove.connect("clicked",
|
|
lambda _b: self._on_watch_remove())
|
|
watch_row.pack_start(watch_remove, False, False, 0)
|
|
box.pack_start(watch_row, False, False, 0)
|
|
|
|
# Columns: expression, value.
|
|
self.watch_store = Gtk.ListStore(str, str)
|
|
self.watch_tree = Gtk.TreeView(model=self.watch_store)
|
|
# Bug 7: clicking a watch row must NOT open a search popup.
|
|
self.watch_tree.set_enable_search(False)
|
|
for title, index in (("Watch", 0), ("Value", 1)):
|
|
renderer = Gtk.CellRendererText()
|
|
col = Gtk.TreeViewColumn(title, renderer, text=index)
|
|
if index == 1:
|
|
col.set_expand(True)
|
|
self.watch_tree.append_column(col)
|
|
watch_scroll = Gtk.ScrolledWindow()
|
|
watch_scroll.set_policy(Gtk.PolicyType.AUTOMATIC,
|
|
Gtk.PolicyType.AUTOMATIC)
|
|
watch_scroll.set_size_request(-1, 120)
|
|
watch_scroll.add(self.watch_tree)
|
|
box.pack_start(watch_scroll, True, True, 0)
|
|
|
|
box.pack_start(Gtk.Label(label="Stack", xalign=0),
|
|
False, False, 0)
|
|
# Columns: level, function, location.
|
|
self.stack_store = Gtk.ListStore(int, str, str)
|
|
stack_tree = Gtk.TreeView(model=self.stack_store)
|
|
stack_tree.set_enable_search(False)
|
|
for title, index in (("Lvl", 0), ("Function", 1),
|
|
("Location", 2)):
|
|
renderer = Gtk.CellRendererText()
|
|
col = Gtk.TreeViewColumn(title, renderer, text=index)
|
|
if index == 2:
|
|
col.set_expand(True)
|
|
stack_tree.append_column(col)
|
|
stack_scroll = Gtk.ScrolledWindow()
|
|
stack_scroll.set_policy(Gtk.PolicyType.AUTOMATIC,
|
|
Gtk.PolicyType.AUTOMATIC)
|
|
stack_scroll.set_size_request(-1, 120)
|
|
stack_scroll.add(stack_tree)
|
|
box.pack_start(stack_scroll, True, True, 0)
|
|
return box
|
|
|
|
# --- visibility / target --------------------------------------------------
|
|
|
|
def show(self):
|
|
self.window.show_all()
|
|
self.window.present()
|
|
|
|
def hide(self):
|
|
self.window.hide()
|
|
|
|
def is_visible(self):
|
|
return self.window.get_visible()
|
|
|
|
def set_target(self, path):
|
|
if path:
|
|
self.target_entry.set_text(path)
|
|
|
|
def get_target(self):
|
|
return self.target_entry.get_text().strip()
|
|
|
|
def toggle_breakpoint(self, file, line):
|
|
"""Toggle a breakpoint at (*file*, *line*) — the shared
|
|
session entry point for the Code window / Debug menu. With
|
|
a live session the change applies at once, otherwise it is
|
|
staged. Returns True when a breakpoint is now set."""
|
|
now_set = self.session.toggle_breakpoint(file, line)
|
|
self._refresh_breakpoints()
|
|
self._update_buttons()
|
|
return now_set
|
|
|
|
def has_breakpoint(self, file, line):
|
|
return self.session.has_breakpoint(file, line)
|
|
|
|
def breakpoint_lines(self, file):
|
|
return self.session.breakpoint_lines(file)
|
|
|
|
# --- toolbar actions -------------------------------------------------------
|
|
|
|
def _on_run(self):
|
|
state = self.session.state
|
|
if state == STATE_IDLE:
|
|
target = self.get_target()
|
|
if not target:
|
|
self._status("No target — Generate first, or Browse "
|
|
"to a script")
|
|
return
|
|
self._output_shown = 0
|
|
self.output_buffer.set_text("")
|
|
self.session.clear_output()
|
|
if not self.session.start(target,
|
|
self.args_entry.get_text()):
|
|
self._status("Start failed: %s"
|
|
% self.session.last_error)
|
|
else:
|
|
self._status("Stopped at entry — %s"
|
|
% self._frame_text())
|
|
else:
|
|
# Bug 5: Run from Stopped/Exited honours the CURRENT
|
|
# Target/Args entries, not just the stored target. If
|
|
# the user edited either field, start that target
|
|
# instead of re-running the loaded executable (the C
|
|
# bug: Run silently re-ran the old program).
|
|
target = self.get_target()
|
|
args = self.args_entry.get_text()
|
|
self._output_shown = 0
|
|
self.output_buffer.set_text("")
|
|
self.session.clear_output()
|
|
if target and (os.path.abspath(target)
|
|
!= self.session.script_path
|
|
or args != self.session.args):
|
|
ok = self.session.start(target, args)
|
|
else:
|
|
ok = self.session.run()
|
|
if not ok:
|
|
self._status("Run failed: %s"
|
|
% self.session.last_error)
|
|
else:
|
|
self._status("Restarted — %s" % self._frame_text())
|
|
self._after_state_change()
|
|
|
|
def _on_continue(self):
|
|
if self.session.cont(timeout_s=UI_EXEC_TIMEOUT_S):
|
|
self._status("Continued — %s" % self._frame_text())
|
|
else:
|
|
self._status("Continue failed: %s"
|
|
% self.session.last_error)
|
|
self._after_state_change()
|
|
|
|
def _on_step_over(self):
|
|
if self.session.step_over(timeout_s=UI_EXEC_TIMEOUT_S):
|
|
self._status("Stepped over — %s" % self._frame_text())
|
|
else:
|
|
self._status("Step Over failed: %s"
|
|
% self.session.last_error)
|
|
self._after_state_change()
|
|
|
|
def _on_step_into(self):
|
|
if self.session.step_into(timeout_s=UI_EXEC_TIMEOUT_S):
|
|
self._status("Stepped into — %s" % self._frame_text())
|
|
else:
|
|
self._status("Step Into failed: %s"
|
|
% self.session.last_error)
|
|
self._after_state_change()
|
|
|
|
def _on_step_out(self):
|
|
if self.session.step_out(timeout_s=UI_EXEC_TIMEOUT_S):
|
|
self._status("Stepped out — %s" % self._frame_text())
|
|
else:
|
|
self._status("Step Out failed: %s"
|
|
% self.session.last_error)
|
|
self._after_state_change()
|
|
|
|
def _on_stop(self):
|
|
# Bug 4: session.stop() kills the process group when the
|
|
# debuggee is running, so this cannot hang on a GUI app.
|
|
self.session.stop()
|
|
self._status("Stopped")
|
|
self._after_state_change()
|
|
|
|
def _on_restart(self):
|
|
if not self.session.restart():
|
|
self._status("Restart failed: %s"
|
|
% self.session.last_error)
|
|
else:
|
|
self._status("Restarted — %s" % self._frame_text())
|
|
self._after_state_change()
|
|
|
|
def _on_settings(self):
|
|
dialog = Gtk.Dialog(title="Debugger Settings",
|
|
parent=self.window,
|
|
flags=Gtk.DialogFlags.MODAL
|
|
| Gtk.DialogFlags.DESTROY_WITH_PARENT)
|
|
dialog.add_button("Close", Gtk.ResponseType.CLOSE)
|
|
area = dialog.get_content_area()
|
|
settings = self.session.settings
|
|
stop_entry = Gtk.CheckButton(
|
|
label="Stop on entry (stop at the first line on start)")
|
|
stop_entry.set_active(settings.stop_on_entry)
|
|
area.pack_start(stop_entry, False, False, 4)
|
|
break_main = Gtk.CheckButton(
|
|
label="Break on main (persistent breakpoint on def main)")
|
|
break_main.set_active(settings.break_on_main)
|
|
area.pack_start(break_main, False, False, 4)
|
|
area.show_all()
|
|
dialog.run()
|
|
settings.stop_on_entry = stop_entry.get_active()
|
|
settings.break_on_main = break_main.get_active()
|
|
dialog.destroy()
|
|
|
|
def _on_browse(self):
|
|
dialog = Gtk.FileChooserDialog(
|
|
title="Choose script to debug", parent=self.window,
|
|
action=Gtk.FileChooserAction.OPEN,
|
|
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
|
Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT))
|
|
if dialog.run() == Gtk.ResponseType.ACCEPT:
|
|
self.set_target(dialog.get_filename())
|
|
dialog.destroy()
|
|
|
|
def _on_key_press(self, _window, event):
|
|
key_name = Gdk.keyval_name(event.keyval)
|
|
if key_name == "F5":
|
|
self._on_continue()
|
|
return True
|
|
if key_name == "F10":
|
|
self._on_step_over()
|
|
return True
|
|
if key_name == "F11":
|
|
self._on_step_into()
|
|
return True
|
|
return False
|
|
|
|
# --- breakpoints ------------------------------------------------------------
|
|
|
|
def _on_bp_add(self):
|
|
location = self.bp_entry.get_text().strip()
|
|
if not location:
|
|
return
|
|
if parse_breakpoint_location(location) is None:
|
|
self._status("Bad breakpoint location: %r (want "
|
|
"file.py:line)" % location)
|
|
return
|
|
bp = self.session.add_breakpoint(location)
|
|
if bp is None:
|
|
self._status("Cannot set breakpoint: %s"
|
|
% self.session.last_error)
|
|
else:
|
|
self.bp_entry.set_text("")
|
|
self._status("Breakpoint %s at %s"
|
|
% (bp.id or "(staged)", location))
|
|
self._refresh_breakpoints()
|
|
self._update_buttons()
|
|
|
|
def _on_bp_remove(self):
|
|
bp_id = self._selected_bp_id
|
|
if bp_id is None:
|
|
return
|
|
if self.session.remove_breakpoint(
|
|
bp_id, self._selected_bp_location):
|
|
self._selected_bp_id = None
|
|
self._selected_bp_location = None
|
|
else:
|
|
self._status("Cannot remove breakpoint: %s"
|
|
% self.session.last_error)
|
|
self._refresh_breakpoints()
|
|
self._update_buttons()
|
|
|
|
def _on_bp_edit(self):
|
|
bp_id = self._selected_bp_id
|
|
if bp_id is None:
|
|
return
|
|
bp = self.session.find_breakpoint_by_id(
|
|
bp_id, self._selected_bp_location)
|
|
if bp is None:
|
|
return
|
|
dialog = Gtk.Dialog(title="Edit Breakpoint %d" % bp_id,
|
|
parent=self.window,
|
|
flags=Gtk.DialogFlags.MODAL
|
|
| Gtk.DialogFlags.DESTROY_WITH_PARENT)
|
|
dialog.add_button("Cancel", Gtk.ResponseType.CANCEL)
|
|
dialog.add_button("Apply", Gtk.ResponseType.APPLY)
|
|
area = dialog.get_content_area()
|
|
area.pack_start(Gtk.Label(label="Location: %s" % bp.location,
|
|
xalign=0), False, False, 4)
|
|
cond_row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
|
|
spacing=4)
|
|
cond_row.pack_start(Gtk.Label(label="Condition:"),
|
|
False, False, 0)
|
|
cond_entry = Gtk.Entry()
|
|
cond_entry.set_text(bp.condition)
|
|
cond_entry.set_hexpand(True)
|
|
cond_row.pack_start(cond_entry, True, True, 0)
|
|
area.pack_start(cond_row, False, False, 4)
|
|
ignore_row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
|
|
spacing=4)
|
|
ignore_row.pack_start(Gtk.Label(label="Ignore first N hits:"),
|
|
False, False, 0)
|
|
ignore_entry = Gtk.Entry()
|
|
ignore_entry.set_text(str(bp.ignore_count))
|
|
ignore_row.pack_start(ignore_entry, False, False, 0)
|
|
area.pack_start(ignore_row, False, False, 4)
|
|
area.show_all()
|
|
if dialog.run() == Gtk.ResponseType.APPLY:
|
|
try:
|
|
ignore = int(ignore_entry.get_text().strip() or 0)
|
|
except ValueError:
|
|
ignore = bp.ignore_count
|
|
bp.condition = cond_entry.get_text().strip()
|
|
bp.ignore_count = max(0, ignore)
|
|
# Live: re-insert with the new options (pdb has no
|
|
# in-place edit); staged: the stored options are used
|
|
# on the next Run.
|
|
if bp.id and self.session.state == STATE_STOPPED:
|
|
self.session.remove_breakpoint(bp.id)
|
|
new_bp = self.session.add_breakpoint(
|
|
bp.location, condition=bp.condition,
|
|
ignore_count=bp.ignore_count,
|
|
enabled=bp.enabled)
|
|
if new_bp is not None:
|
|
# Re-inserted under a new pdb id: keep the
|
|
# selection on the new row.
|
|
self._selected_bp_id = new_bp.id
|
|
dialog.destroy()
|
|
self._refresh_breakpoints()
|
|
|
|
def _on_bp_toggled(self, _renderer, path):
|
|
"""Bug 8: apply the toggle to the session FIRST, then update
|
|
the store row directly — never rebuild the model under the
|
|
click, so the toggle cannot be lost. The row's location is
|
|
passed too: staged rows share id 0, and without it the
|
|
toggle would land on the first staged breakpoint instead
|
|
of the clicked row."""
|
|
tree_iter = self.bp_store.get_iter(path)
|
|
bp_id = self.bp_store.get_value(tree_iter, 0)
|
|
location = self.bp_store.get_value(tree_iter, 2)
|
|
current = self.bp_store.get_value(tree_iter, 1)
|
|
if self.session.enable_breakpoint(bp_id, not current, location):
|
|
self.bp_store.set_value(tree_iter, 1, not current)
|
|
else:
|
|
self._status("Cannot toggle breakpoint: %s"
|
|
% self.session.last_error)
|
|
|
|
def _on_bp_selection_changed(self, selection):
|
|
_model, tree_iter = selection.get_selected()
|
|
if tree_iter is None:
|
|
self._selected_bp_id = None
|
|
self._selected_bp_location = None
|
|
else:
|
|
self._selected_bp_id = self.bp_store.get_value(tree_iter, 0)
|
|
self._selected_bp_location = self.bp_store.get_value(
|
|
tree_iter, 2)
|
|
self._update_buttons()
|
|
|
|
def _refresh_breakpoints(self):
|
|
"""Bug 2: update rows IN PLACE by breakpoint id — add rows
|
|
for new breakpoints, drop rows for removed ones, update the
|
|
rest. The model is never rebuilt, so the selection (kept as
|
|
an id) survives every refresh."""
|
|
wanted = {bp.id: bp for bp in self.session.breakpoints()}
|
|
# Staged breakpoints share id 0; key them by location.
|
|
staged = {bp.location: bp for bp in self.session.breakpoints()
|
|
if bp.id == 0}
|
|
existing = {}
|
|
tree_iter = self.bp_store.get_iter_first()
|
|
while tree_iter is not None:
|
|
bp_id = self.bp_store.get_value(tree_iter, 0)
|
|
location = self.bp_store.get_value(tree_iter, 2)
|
|
existing[(bp_id, location)] = tree_iter
|
|
tree_iter = self.bp_store.iter_next(tree_iter)
|
|
for bp in self.session.breakpoints():
|
|
key = (bp.id, bp.location)
|
|
row = (bp.id, bp.enabled, bp.location, bp.hits)
|
|
if key in existing:
|
|
tree_iter = existing.pop(key)
|
|
self.bp_store.set_row(tree_iter, row)
|
|
else:
|
|
self.bp_store.append(row)
|
|
for tree_iter in existing.values():
|
|
self.bp_store.remove(tree_iter)
|
|
# Restore the selection by (id, location) (bug 2) — by id
|
|
# alone a staged row would re-select the first staged row,
|
|
# since staged rows share id 0.
|
|
if self._selected_bp_id is not None:
|
|
tree_iter = self.bp_store.get_iter_first()
|
|
while tree_iter is not None:
|
|
if self.bp_store.get_value(tree_iter, 0) \
|
|
== self._selected_bp_id \
|
|
and (self._selected_bp_location is None
|
|
or self.bp_store.get_value(tree_iter, 2)
|
|
== self._selected_bp_location):
|
|
self.bp_tree.get_selection().select_iter(tree_iter)
|
|
break
|
|
tree_iter = self.bp_store.iter_next(tree_iter)
|
|
|
|
# --- watches ------------------------------------------------------------------
|
|
|
|
def _on_watch_add(self):
|
|
expr = self.watch_entry.get_text().strip()
|
|
if not expr:
|
|
return
|
|
if self.session.add_watch(expr):
|
|
self.watch_entry.set_text("")
|
|
self._refresh_watches()
|
|
self._update_buttons()
|
|
|
|
def _on_watch_remove(self):
|
|
selection = self.watch_tree.get_selection()
|
|
_model, tree_iter = selection.get_selected()
|
|
if tree_iter is None:
|
|
return
|
|
expr = self.watch_store.get_value(tree_iter, 0)
|
|
self.session.remove_watch(expr)
|
|
self._refresh_watches()
|
|
|
|
def _refresh_watches(self):
|
|
self.watch_store.clear()
|
|
for watch in self.session.watches():
|
|
self.watch_store.append([watch.name, watch.value])
|
|
|
|
# --- refresh / poll ---------------------------------------------------------------
|
|
|
|
def _refresh_locals(self):
|
|
self.locals_store.clear()
|
|
for var in self.session.locals():
|
|
self.locals_store.append([var.name, var.type_name,
|
|
var.value])
|
|
|
|
def _refresh_stack(self):
|
|
self.stack_store.clear()
|
|
for frame in self.session.frames():
|
|
self.stack_store.append(
|
|
[frame.level, frame.function,
|
|
"%s:%d" % (frame.file, frame.line)])
|
|
|
|
def _refresh_all(self):
|
|
self._refresh_breakpoints()
|
|
self._refresh_locals()
|
|
self._refresh_watches()
|
|
self._refresh_stack()
|
|
|
|
def _frame_text(self):
|
|
stop = self.session.last_stop
|
|
frame = stop.frame
|
|
if not frame.file:
|
|
return "state: %s" % self.session.state
|
|
return "%s:%d %s (%s)" % (os.path.basename(frame.file),
|
|
frame.line, frame.function,
|
|
stop.reason or self.session.state)
|
|
|
|
def _status(self, text):
|
|
self.status_label.set_text(text)
|
|
if self.on_status is not None:
|
|
self.on_status(text)
|
|
|
|
def _after_state_change(self):
|
|
# Sync hit counts from pdb so the Hits column is accurate.
|
|
if self.session.state == STATE_STOPPED:
|
|
self.session.refresh_breakpoints()
|
|
self._refresh_all()
|
|
self._update_buttons()
|
|
self._drain_output()
|
|
|
|
def _drain_output(self):
|
|
"""Append new debuggee output to the console."""
|
|
text = self.session.target_output()
|
|
if len(text) > self._output_shown:
|
|
chunk = text[self._output_shown:]
|
|
self._output_shown = len(text)
|
|
end = self.output_buffer.get_end_iter()
|
|
self.output_buffer.insert(end, chunk)
|
|
end = self.output_buffer.get_end_iter()
|
|
self.output_view.scroll_to_iter(end, 0.0, False, 0.0, 0.0)
|
|
|
|
def _poll(self):
|
|
"""200ms poll: drain output; on a fresh stop, refresh the
|
|
panes and tell the app where execution stopped."""
|
|
if not self.is_visible():
|
|
return True
|
|
self._drain_output()
|
|
if self.session.poll_events():
|
|
self._after_state_change()
|
|
stop = self.session.last_stop
|
|
self._status("Stopped — %s" % self._frame_text())
|
|
if stop.frame.file and self.on_stop_location is not None:
|
|
self.on_stop_location(stop.frame.file, stop.frame.line)
|
|
elif self.session.state == STATE_RUNNING:
|
|
self.status_label.set_text("Running…")
|
|
self._update_buttons()
|
|
return True
|
|
|
|
def _update_buttons(self):
|
|
state = self.session.state
|
|
buttons = self._toolbar_buttons
|
|
buttons["run"].set_sensitive(state in (STATE_IDLE,
|
|
STATE_STOPPED,
|
|
STATE_EXITED))
|
|
for name in ("continue", "step_over", "step_into", "step_out"):
|
|
buttons[name].set_sensitive(state == STATE_STOPPED)
|
|
buttons["stop"].set_sensitive(state in (STATE_RUNNING,
|
|
STATE_STOPPED))
|
|
buttons["restart"].set_sensitive(state != STATE_IDLE)
|
|
# Breakpoint add/remove needs pdb at a prompt (or no session
|
|
# yet, when the entry is staged for the next Run).
|
|
can_edit_bp = state in (STATE_IDLE, STATE_STOPPED, STATE_EXITED)
|
|
self.bp_add_button.set_sensitive(can_edit_bp)
|
|
self.bp_edit_button.set_sensitive(
|
|
can_edit_bp and self._selected_bp_id is not None)
|
|
self.bp_remove_button.set_sensitive(
|
|
can_edit_bp and self._selected_bp_id is not None)
|
|
|
|
def _on_delete(self, _window, _event):
|
|
# Closing the window hides it; the session keeps its
|
|
# breakpoints (C behaviour) and is only ended by Stop or
|
|
# when the designer quits.
|
|
self.hide()
|
|
return True
|
|
|
|
def destroy(self):
|
|
if self._poll_id is not None:
|
|
GLib.source_remove(self._poll_id)
|
|
self._poll_id = None
|
|
self.session.shutdown()
|
|
self.window.destroy()
|