From b9695773ce7b57dc8cf7831dada88b0519c5a397 Mon Sep 17 00:00:00 2001 From: Srimanta Barua Date: Fri, 4 Aug 2017 22:48:43 +0530 Subject: [PATCH] added =!detach, and replaced eprintfs with io->cb_printf (#8125) * added =!detach, and replaced eprintfs with io->cb_printf * keep error in eprintf --- libr/debug/p/debug_gdb.c | 3 +++ libr/io/p/io_gdb.c | 22 +++++++++++++++++----- shlr/gdb/include/gdbclient/commands.h | 2 +- shlr/gdb/src/gdbclient/core.c | 15 ++++----------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/libr/debug/p/debug_gdb.c b/libr/debug/p/debug_gdb.c index 94289c4db0..f7db12e213 100644 --- a/libr/debug/p/debug_gdb.c +++ b/libr/debug/p/debug_gdb.c @@ -347,6 +347,9 @@ static int r_debug_gdb_attach(RDebug *dbg, int pid) { } static int r_debug_gdb_detach(RDebug *dbg, int pid) { + if (pid <= 0 || !desc->stub_features.multiprocess) { + return gdbr_detach (desc); + } return gdbr_detach_pid (desc, pid); } diff --git a/libr/io/p/io_gdb.c b/libr/io/p/io_gdb.c index b68a9fad0f..d4658a1615 100644 --- a/libr/io/p/io_gdb.c +++ b/libr/io/p/io_gdb.c @@ -202,6 +202,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) { " =!pkt s - send packet 's'\n" " =!monitor cmd - hex-encode monitor command and pass" " to target interpreter\n" + " =!detach [pid] - detach from remote/detach specific pid\n" " =!inv.reg - invalidate reg cache\n" " =!pktsz - get max packet size used\n" " =!pktsz bytes - set max. packet size as 'bytes' bytes\n" @@ -212,8 +213,8 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) { if (!strncmp (cmd, "pktsz", 5)) { const char *ptr = r_str_chop_ro (cmd + 5); if (!isdigit (*ptr)) { - eprintf ("packet size: %u bytes\n", - desc->stub_features.pkt_sz); + io->cb_printf ("packet size: %u bytes\n", + desc->stub_features.pkt_sz); return true; } ut32 pktsz; @@ -224,13 +225,24 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) { desc->stub_features.pkt_sz = R_MAX (pktsz, 64); // min = 64 return true; } + if (!strncmp (cmd, "detach", 6)) { + int pid; + if (!isspace (cmd[6]) || !desc->stub_features.multiprocess) { + return gdbr_detach (desc) >= 0; + } + cmd = r_str_chop_ro (cmd + 6); + if (!*cmd || !(pid = strtoul (cmd, NULL, 10))) { + return gdbr_detach (desc) >= 0; + } + return gdbr_detach_pid (desc, pid) >= 0; + } if (!strncmp (cmd, "pkt ", 4)) { if (send_msg (desc, cmd + 4) == -1) { return false; } int r = read_packet (desc); desc->data[desc->data_len] = '\0'; - eprintf ("reply:\n\n%s\n", desc->data); + io->cb_printf ("reply:\n%s\n", desc->data); if (!desc->no_ack) { eprintf ("[waiting for ack]\n"); } @@ -248,7 +260,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) { if (!isspace (cmd[8])) { qrcmd = "help"; } - if (gdbr_send_qRcmd (desc, qrcmd) < 0) { + if (gdbr_send_qRcmd (desc, qrcmd, io->cb_printf) < 0) { eprintf ("remote error\n"); return false; } @@ -281,7 +293,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) { } } } - eprintf ("%s\n", file); + io->cb_printf ("%s\n", file); free (file); return true; } diff --git a/shlr/gdb/include/gdbclient/commands.h b/shlr/gdb/include/gdbclient/commands.h index d96c39486d..a1d4dada42 100644 --- a/shlr/gdb/include/gdbclient/commands.h +++ b/shlr/gdb/include/gdbclient/commands.h @@ -46,7 +46,7 @@ int gdbr_check_vcont(libgdbr_t *g); * remote target's interpreter. * \returns 0 on success and -1 on failure */ -int gdbr_send_qRcmd(libgdbr_t *g, const char *cmd); +int gdbr_send_qRcmd(libgdbr_t *g, const char *cmd, void (*cb_printf) (const char *, ...)); /*! * \brief attaches to a process diff --git a/shlr/gdb/src/gdbclient/core.c b/shlr/gdb/src/gdbclient/core.c index 49d356c3f0..9d92f77154 100644 --- a/shlr/gdb/src/gdbclient/core.c +++ b/shlr/gdb/src/gdbclient/core.c @@ -337,19 +337,12 @@ int gdbr_detach(libgdbr_t *g) { return -1; } reg_cache.valid = false; - - if (g->stub_features.multiprocess) { - if (g->pid <= 0) { - return -1; - } - return gdbr_detach_pid (g, g->pid); - } - ret = send_msg (g, "D"); if (ret < 0) { return -1; } - return 0; + // Disconnect + return gdbr_disconnect (g); } int gdbr_detach_pid(libgdbr_t *g, int pid) { @@ -1055,7 +1048,7 @@ void gdbr_invalidate_reg_cache() { reg_cache.valid = false; } -int gdbr_send_qRcmd(libgdbr_t *g, const char *cmd) { +int gdbr_send_qRcmd(libgdbr_t *g, const char *cmd, void (*cb_printf) (const char *fmt, ...)) { if (!g || !cmd) { return -1; } @@ -1096,7 +1089,7 @@ int gdbr_send_qRcmd(libgdbr_t *g, const char *cmd) { // Console output from gdbserver unpack_hex (g->data + 1, g->data_len - 1, g->data + 1); g->data[g->data_len - 1] = '\0'; - eprintf ("%s", g->data + 1); + cb_printf ("%s", g->data + 1); } if (read_packet (g) < 0) { free (buf);