diff --git a/src/debugger_ui.cpp b/src/debugger_ui.cpp index 8d1b332..87069cc 100644 --- a/src/debugger_ui.cpp +++ b/src/debugger_ui.cpp @@ -36,6 +36,11 @@ static GtkWidget *g_btn_run = nullptr, *g_btn_cont = nullptr, static std::string g_output_seen; // target output already shown static std::string g_log_seen; static bool g_was_running = false; +// One-shot status note (e.g. "Step Out refused: already in main") that +// survives the poll tick's status rewrite until the session state +// changes. Set by an action handler, consumed in sync_ui_from_session. +static std::string g_status_note; +static DbgState g_status_note_state = DbgState::Idle; static DebuggerSettings load_settings_once() { if (!g_dbg_settings_loaded) { @@ -81,10 +86,125 @@ static void refresh_buttons() { } // --- list refresh ------------------------------------------------------- +// +// The 150 ms poll used to clear and rebuild every list on every tick, +// which wiped the breakpoint row selection almost as fast as the user +// clicked it (Edit…/Remove then found nothing selected) and could +// swallow a quick Enabled-checkbox click mid-rebuild. Two defences: +// 1. sync_ui_from_session() only rebuilds a list when its contents +// actually changed (fingerprint below), so an idle window keeps +// stable rows and a stable selection. +// 2. refresh_bp_list() / the watch list preserve the selection +// across a rebuild that does happen (by breakpoint id / watch +// name, falling back to the row index for staged id-0 rows). +// The toggle handler additionally flips the checkbox optimistically +// before the gdb round-trip, so a quick click is never lost. + +static std::string lists_fingerprint() { + if (!g_session) return ""; + std::string f; + for (const auto& b : g_session->breakpoints()) { + f += "B"; + f += std::to_string(b.id); + f += '|'; + f += b.location; + f += '|'; + f += b.enabled ? '1' : '0'; + f += '|'; + f += std::to_string(b.hits); + f += '|'; + f += bp_kind_name(b.kind); + f += '|'; + f += b.condition; + f += '|'; + f += std::to_string(b.ignore_count); + f += '|'; + f += b.log_message; + f += '|'; + f += b.group; + f += '\n'; + } + for (const auto& v : g_session->locals()) { + f += "L"; + f += v.name; + f += '|'; + f += v.type; + f += '|'; + f += v.value; + f += '\n'; + } + for (const auto& v : g_session->watches()) { + f += "W"; + f += v.name; + f += '|'; + f += v.type; + f += '|'; + f += v.value; + f += '\n'; + } + for (const auto& fr : g_session->frames()) { + f += "F"; + f += std::to_string(fr.level); + f += '|'; + f += fr.function; + f += '|'; + f += fr.file; + f += '|'; + f += std::to_string(fr.line); + f += '\n'; + } + return f; +} + +static std::string g_last_lists_fp; static void refresh_bp_list() { if (!g_bp_store || !g_session) return; + // Preserve the horizontal scroll offset across the rebuild, like + // the selection below: rebuilding the store can otherwise leave + // the view scrolled to the far right (the Location column and the + // `#` column end up off-screen left), and the offset then sticks + // because nothing ever scrolls it back. + GtkWidget* view0 = g_dbg_window ? (GtkWidget*)g_object_get_data( + G_OBJECT(g_dbg_window), "bp_view") + : nullptr; + GtkAdjustment* hadj = nullptr; + double hadj_value = 0; + if (view0) { + // The treeview implements GtkScrollable, so the scrolled + // window adds it directly (no intermediate viewport). + GtkWidget* scw = gtk_widget_get_parent(view0); + if (scw && GTK_IS_SCROLLED_WINDOW(scw)) { + hadj = gtk_scrolled_window_get_hadjustment( + GTK_SCROLLED_WINDOW(scw)); + if (hadj) hadj_value = gtk_adjustment_get_value(hadj); + } + } + // Remember the selection so it survives the rebuild: the breakpoint + // id for live rows, the row index for staged (id 0) rows, which all + // share one id. + GtkWidget* view = g_dbg_window ? (GtkWidget*)g_object_get_data( + G_OBJECT(g_dbg_window), "bp_view") + : nullptr; + int sel_id = -1, sel_idx = -1; + GtkTreeSelection* sel = nullptr; + if (view) { + sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + GtkTreeModel* model = nullptr; + GtkTreeIter it; + if (gtk_tree_selection_get_selected(sel, &model, &it)) { + gtk_tree_model_get(model, &it, 0, &sel_id, -1); + GtkTreePath* tp = gtk_tree_model_get_path(model, &it); + if (tp) { + sel_idx = gtk_tree_path_get_indices(tp)[0]; + gtk_tree_path_free(tp); + } + } + } gtk_list_store_clear(g_bp_store); + GtkTreeIter restore_it; + bool have_restore = false; + int row = 0; for (const auto& b : g_session->breakpoints()) { GtkTreeIter it; gtk_list_store_append(g_bp_store, &it); @@ -102,19 +222,60 @@ static void refresh_bp_list() { b.enabled ? TRUE : FALSE, 3, b.hits, 4, bp_kind_name(b.kind), 5, b.condition.c_str(), 6, b.ignore_count, 7, msg.c_str(), -1); + if (!have_restore && sel_id >= 0 && + ((sel_id > 0 && b.id == sel_id) || + (sel_id == 0 && row == sel_idx))) { + restore_it = it; + have_restore = true; + } + ++row; } + if (have_restore && sel) + gtk_tree_selection_select_iter(sel, &restore_it); + // Restore the horizontal scroll offset the rebuild may have moved + // (see above): the view must not jump to the far right on its own. + if (hadj) gtk_adjustment_set_value(hadj, hadj_value); } static void refresh_var_list(GtkListStore* store, const std::vector& vars) { if (!store) return; + // Preserve the watch selection across the rebuild (Remove acts on + // the selected watch); locals have no selection-dependent action. + GtkTreeSelection* sel = nullptr; + std::string sel_name; + if (store == g_watch_store && g_dbg_window) { + GtkWidget* view = (GtkWidget*)g_object_get_data( + G_OBJECT(g_dbg_window), "watch_view"); + if (view) { + sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + GtkTreeModel* model = nullptr; + GtkTreeIter it; + if (gtk_tree_selection_get_selected(sel, &model, &it)) { + gchar* name = nullptr; + gtk_tree_model_get(model, &it, 0, &name, -1); + if (name) { + sel_name = name; + g_free(name); + } + } + } + } gtk_list_store_clear(store); + GtkTreeIter restore_it; + bool have_restore = false; for (const auto& v : vars) { GtkTreeIter it; gtk_list_store_append(store, &it); gtk_list_store_set(store, &it, 0, v.name.c_str(), 1, v.type.c_str(), 2, v.value.c_str(), -1); + if (!have_restore && !sel_name.empty() && v.name == sel_name) { + restore_it = it; + have_restore = true; + } } + if (have_restore && sel) + gtk_tree_selection_select_iter(sel, &restore_it); } static void refresh_frames_list() { @@ -160,13 +321,20 @@ static void refresh_output() { } } -// Called on every poll tick and after every user action. +// Called on every poll tick and after every user action. The lists are +// rebuilt only when their contents actually changed (fingerprint) — +// rebuilding every 150 ms used to wipe the row selection and swallow +// quick checkbox clicks. static void sync_ui_from_session() { if (!g_session) return; - refresh_bp_list(); - refresh_var_list(g_locals_store, g_session->locals()); - refresh_var_list(g_watch_store, g_session->watches()); - refresh_frames_list(); + std::string fp = lists_fingerprint(); + if (fp != g_last_lists_fp) { + g_last_lists_fp = fp; + refresh_bp_list(); + refresh_var_list(g_locals_store, g_session->locals()); + refresh_var_list(g_watch_store, g_session->watches()); + refresh_frames_list(); + } refresh_output(); refresh_buttons(); DbgState st = g_session->state(); @@ -191,6 +359,14 @@ static void sync_ui_from_session() { break; case DbgState::Error: s = "Error: " + g_session->last_error(); break; } + // A one-shot note (Step Out refused, …) overrides the state text + // until the session state changes. + if (!g_status_note.empty()) { + if (st == g_status_note_state) + s = g_status_note; + else + g_status_note.clear(); + } set_status(s); if (st == DbgState::Stopped && !stop.frame.file.empty()) { std::string src = stop.frame.file + ":" + @@ -249,13 +425,12 @@ static void on_run_clicked(GtkButton*, gpointer) { show_error(g_dbg_window, "Enter a target executable path first."); return; } - if (g_session->state() == DbgState::Stopped || - g_session->state() == DbgState::Exited) { - // Already loaded: restart from the beginning. - if (!g_session->restart(err)) show_error(g_dbg_window, err); - } else { - if (!g_session->start(exe, args, err)) show_error(g_dbg_window, err); - } + // Re-run the loaded target, or load and run the Target entry when + // it names a different executable (or different args): Run from + // Stopped/Exited used to restart the already-loaded program and + // ignore the Target field entirely. + if (!g_session->restart_or_start(exe, args, err)) + show_error(g_dbg_window, err); sync_ui_from_session(); } @@ -267,7 +442,20 @@ static void on_step_clicked(GtkButton*, gpointer which) { if (!std::strcmp(w, "cont")) ok = g_session->cont(err); else if (!std::strcmp(w, "over")) ok = g_session->step_over(err); else if (!std::strcmp(w, "into")) ok = g_session->step_into(err); - else if (!std::strcmp(w, "out")) ok = g_session->step_out(err); + else if (!std::strcmp(w, "out")) { + // In the outermost frame (main) there is no caller to return + // to: say so in the status line instead of popping gdb's + // `"finish" not meaningful…` modal error dialog. + if (!g_session->can_step_out()) { + g_status_note = + "Step Out: already in the outermost frame (main) — " + "nothing to return to. Use Continue."; + g_status_note_state = g_session->state(); + sync_ui_from_session(); + return; + } + ok = g_session->step_out(err); + } if (!ok) show_error(g_dbg_window, err); sync_ui_from_session(); } @@ -586,9 +774,15 @@ static void on_bp_toggled(GtkCellRendererToggle*, gchar* path, gpointer) { gboolean enabled = FALSE; gtk_tree_model_get(GTK_TREE_MODEL(g_bp_store), &it, 0, &id, 2, &enabled, -1); + // Optimistic update: flip the checkbox in the list at once, so a + // quick click is never lost to the poll rebuild; the backend call + // follows, and on failure the checkbox flips back. + gtk_list_store_set(g_bp_store, &it, 2, !enabled ? TRUE : FALSE, -1); std::string err; if (g_session->is_alive() && id > 0) { if (!g_session->enable_breakpoint(id, !enabled, err)) { + gtk_list_store_set(g_bp_store, &it, 2, + enabled ? TRUE : FALSE, -1); show_error(g_dbg_window, err); return; } @@ -733,6 +927,10 @@ static GtkWidget* make_tree_view(GtkListStore* store, const char* cols[], int ncols, bool toggle_col) { GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); + // No interactive search: with it on, typing while a list has focus + // opens GTK's search popup instead of reaching the entry above the + // list (the watch entry's clicks could land on the watch list). + gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), FALSE); for (int i = 0; i < ncols; ++i) { GtkCellRenderer* rend = nullptr; if (toggle_col && i == 2) { @@ -742,12 +940,20 @@ static GtkWidget* make_tree_view(GtkListStore* store, GtkTreeViewColumn* col = gtk_tree_view_column_new_with_attributes( cols[i], rend, "active", i, nullptr); + gtk_tree_view_column_set_resizable(col, TRUE); gtk_tree_view_append_column(GTK_TREE_VIEW(view), col); } else { rend = gtk_cell_renderer_text_new(); GtkTreeViewColumn* col = gtk_tree_view_column_new_with_attributes( cols[i], rend, "text", i, nullptr); + gtk_tree_view_column_set_resizable(col, TRUE); + // The breakpoint Location column gets room to show + // file:line instead of being squeezed to a few pixels. + if (toggle_col && i == 1) { + gtk_tree_view_column_set_min_width(col, 140); + gtk_tree_view_column_set_expand(col, TRUE); + } gtk_tree_view_append_column(GTK_TREE_VIEW(view), col); } } @@ -908,14 +1114,29 @@ void debugger_window_open(GtkWidget* parent) { GtkWidget* view = make_tree_view(g_bp_store, cols, 8, true); g_object_set_data(G_OBJECT(win), "bp_view", view); GtkWidget* sc = gtk_scrolled_window_new(nullptr, nullptr); + // The pane is wide enough to show the entry and the columns + // (the old 380 px default squeezed the entry to ~50 px); the + // scrolled window keeps a minimum width and scrolls + // horizontally instead of clipping the columns away. + gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(sc), + 480); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sc), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); gtk_container_add(GTK_CONTAINER(sc), view); gtk_box_pack_start(GTK_BOX(box), sc, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(box), - gtk_label_new("Breakpoints (toggle Enabled to " - "enable/disable; Add… / Edit… for " - "conditional, ignore-count and " - "logpoint handlers)"), - FALSE, FALSE, 0); + // The hint must wrap: as a single unwrapped line it is ~700px + // wide, which forced this whole pane's minimum width to ~734px + // — wider than the paned's 560px slot — so GTK right-aligned + // the overflowing pane and the `#` and Location columns ended + // up off-screen left (bug a). A wrapping label's minimum width + // is just its longest word, so the pane now fits its slot. + GtkWidget* bp_hint = gtk_label_new( + "Breakpoints (toggle Enabled to enable/disable; Add… / " + "Edit… for conditional, ignore-count and logpoint handlers)"); + gtk_label_set_line_wrap(GTK_LABEL(bp_hint), TRUE); + gtk_label_set_xalign(GTK_LABEL(bp_hint), 0.0f); + gtk_box_pack_start(GTK_BOX(box), bp_hint, FALSE, FALSE, 0); } // Locals + watches pane @@ -984,7 +1205,7 @@ void debugger_window_open(GtkWidget* parent) { gtk_box_pack_start(GTK_BOX(vbox), osc, FALSE, TRUE, 0); gtk_widget_show_all(win); - gtk_paned_set_position(GTK_PANED(mid), 380); + gtk_paned_set_position(GTK_PANED(mid), 560); g_timeout_add(150, on_dbg_poll, nullptr); // Restore saved breakpoints into the list (they apply on Run). if (g_session) { @@ -1101,7 +1322,11 @@ bool debugger_autorun(const std::string& exe, const std::string& bp_loc, } if (g_session->state() == DbgState::Stopped || g_session->state() == DbgState::Exited) { - if (!g_session->restart(err)) return false; + // Re-run the loaded target, or load and run the given one when + // it differs (same rule as the Run button). + std::string args = g_args_entry ? entry_text(g_args_entry) + : g_session->args(); + if (!g_session->restart_or_start(exe, args, err)) return false; } else { if (!g_session->start(exe, "", err)) return false; }