From 91f63163443b7f89a4d273fbfb48c00265db5443 Mon Sep 17 00:00:00 2001
From: Michael Ensslin
Date: Thu, 19 Mar 2020 10:40:10 +0100
Subject: [PATCH] Add 'press g to attach gdb' feature
---
Action.c | 20 ++++++++++++++++++++
htop.1.in | 3 +++
2 files changed, 23 insertions(+)
diff --git a/Action.c b/Action.c
index 9a7c3c560..afaf50b32 100644
--- a/Action.c
+++ b/Action.c
@@ -392,6 +392,24 @@ static Htop_Reaction actionStrace(State* st) {
return HTOP_REFRESH | HTOP_REDRAW_BAR;
}
+static Htop_Reaction actionGdb(State* st) {
+ Process* p = (Process*) Panel_getSelected(st->panel);
+ if (!p) return HTOP_OK;
+
+ char buf[32];
+ xSnprintf(buf, sizeof(buf), "gdb -p %d", (int) p->pid);
+
+ endwin();
+ int result = system(buf);
+ refresh();
+
+ // we don't really care about the invocation result
+ (void) result;
+
+ CRT_enableDelay();
+ return HTOP_REFRESH | HTOP_REDRAW_BAR;
+}
+
static Htop_Reaction actionTag(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel);
if (!p) return HTOP_OK;
@@ -437,6 +455,7 @@ static const struct { const char* key; const char* info; } helpRight[] = {
{ .key = " i: ", .info = "set IO priority" },
{ .key = " l: ", .info = "list open files with lsof" },
{ .key = " s: ", .info = "trace syscalls with strace" },
+ { .key = " g: ", .info = "debug with gdb" },
{ .key = " ", .info = "" },
{ .key = " F2 C S: ", .info = "setup" },
{ .key = " F1 h: ", .info = "show this help screen" },
@@ -588,6 +607,7 @@ void Action_setBindings(Htop_Action* keys) {
keys[KEY_F(2)] = actionSetup;
keys['l'] = actionLsof;
keys['s'] = actionStrace;
+ keys['g'] = actionGdb;
keys[' '] = actionTag;
keys['\014'] = actionRedraw; // Ctrl+L
keys[KEY_F(1)] = actionHelp;
diff --git a/htop.1.in b/htop.1.in
index 149b1465a..f25a86e68 100644
--- a/htop.1.in
+++ b/htop.1.in
@@ -101,6 +101,9 @@ update of system calls issued by the process.
.B l
Display open files for a process: if lsof(1) is installed, pressing this key
will display the list of file descriptors opened by the process.
+.B g
+Debug process: if gdb(1) is installed, pressing this key will attach it
+to the currently selected process, allowing you to debug it.
.TP
.B F1, h, ?
Go to the help screen