Skip to content

Commit

Permalink
gdbremote: Add support for error packet handling
Browse files Browse the repository at this point in the history
Add logic to detect error messages issued by the gdbserver and to
convert them into exceptions.

Signed-off-by: Daniel Thompson <[email protected]>
  • Loading branch information
daniel-thompson committed Nov 16, 2024
1 parent 939d169 commit 005d457
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
24 changes: 24 additions & 0 deletions libdrgn/gdbremote.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ static struct drgn_error *gdb_send_and_receive(int fd, struct gdb_packet *pkt)
if (VERBOSE_PROTOCOL > 1)
fprintf(stderr, "-> +\n");

// We need to make sure we check for errors after sending the
// acknowledgement!
if (pkt->buffer[1] == 'E') {
// Error packets are naturally printable to we just have
// to deframe things slightly to make it look good in the
// error message.
//
// Format is either a two digit number (e.g. $E01#c5) or
// a textual message (e.g. $E.messaeg#c5). We can print
// either of these simply by terminating at the '#' and
// stripping of the . or leading '0' if it exists.

char *msg = (char *) pkt->buffer +
(pkt->buffer[2] == '.' || pkt->buffer[2] == '0' ? 3 :
2);

// strrchr() will always find the '#' because the
// packet has already been subjected to framing checks
*strrchr(msg, '#') = '\0';

return drgn_error_format(DRGN_ERROR_OTHER,
"gdbserver reported error: %s", msg);
}

return NULL;
}

Expand Down
13 changes: 12 additions & 1 deletion tests/test_gdbremote.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
b"$m7fffffee40,16#63": b'$60eef* 7f0*"4077e1f77f0*"d8ef*!7f00#c6',
b"$m7fffffee60,16#65": b'$70ef*!7f0*"1878e1f77f0*"f00cfdf77f00#47',
b"$m7fffffef70,16#67": b'$0*,70065*"0*.#5c',
}

# Null pointer read
b"$m0,64#33" : b'$E01#a6',
}

class GdbMockProcess(multiprocessing.Process):
def __init__(self):
Expand Down Expand Up @@ -124,6 +126,15 @@ def test_gdbremote_read(self):
val, b"`\xee\xff\xff\x7f\x00\x00\x00@w\xe1\xf7\x7f\x00\x00\x00"
)

def test_gdbremote_read_with_error(self):
self.prog.set_gdbremote(self.conn_str)
self.assertRaisesRegex(
Exception,
"gdbserver reported error: 1",
self.prog.read,
0,
64)

def test_gdbremote_getregs(self):
self.prog.set_gdbremote(self.conn_str)
if self.prog.platform.arch != Architecture.AARCH64:
Expand Down

0 comments on commit 005d457

Please sign in to comment.