From patchwork Thu Jun 1 14:49:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 100938 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp843369qge; Thu, 1 Jun 2017 07:53:16 -0700 (PDT) X-Received: by 10.55.17.11 with SMTP id b11mr2319271qkh.5.1496328796012; Thu, 01 Jun 2017 07:53:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496328796; cv=none; d=google.com; s=arc-20160816; b=Ps1UMAeIjPA4IpG7Nlpl6SvmC/+xrIJYXgbk0xhx2VErWcLjak0vLofT/TLI3w8Spd x5KJctLW1ibqgSTn08ZUVjrk399X/9vGpFABLy+Cfi3oWLMYE4QwXHLJ59VaIpVHm1FK O0aEwz4cN3znquxiKilpTh/2z+C4Hp4HVbsBGMytys26fO1iDcBLItprZEUWwWAvvAYE sOXtJDswSKH7VCBdMdEvm/oUE5hnyITKd8XhSdv+H1RsU+pUGnsEJRwyYAV5qKGdXPlA 633ZIpP3c65+G++CLZKH7K02JG0AP0TVc7cLVX5Na5A3Py2kWbqtw3M93n19GtyBte0Q +vVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=NOnco/Cvx2mg6+/Y/0FweVdPOcj5yDorUWvINKyGrJA=; b=eXTbAzMKwFFv4BMruXjCuiifd7dR9wCCPNVb0CaEjCPnDsgV5KmlL83MwMRCbAGBYR p8d2LcYLp9Eg0FedZiAvQPlt+KDBy5i5zJzt6jsgm4BZKQMq9RBf10091VVwDIintGnF xFJkvXZIhloL0TgqIP08RvZ4e3LLC/HD+ACoE17ImXV6XK611fB9t1wmSmUQu6zpJoNE NjxxGcvLb0+fB1JtyMb2CrSbVptzyUGv/CFoadLSR9Uh5yxVQIco+JPYl4TM7ddUhmYV YMvhUz5tgFoOlDoxLNEjE2D4k9SIZfbFLWr96DxGQBBAEhcy6sGDV3JgJ4/ln1e8Cjzv pvLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id a85si8568483qka.152.2017.06.01.07.53.15 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 01 Jun 2017 07:53:16 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:45065 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGRTN-0007Kp-EC for patch@linaro.org; Thu, 01 Jun 2017 10:53:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43967) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGRPM-0004Y9-JT for qemu-devel@nongnu.org; Thu, 01 Jun 2017 10:49:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGRPK-0002Vc-Ny for qemu-devel@nongnu.org; Thu, 01 Jun 2017 10:49:04 -0400 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]:35591) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dGRPK-0002VC-EE for qemu-devel@nongnu.org; Thu, 01 Jun 2017 10:49:02 -0400 Received: by mail-wm0-x234.google.com with SMTP id b84so161718946wmh.0 for ; Thu, 01 Jun 2017 07:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NOnco/Cvx2mg6+/Y/0FweVdPOcj5yDorUWvINKyGrJA=; b=Y+m2I4E8TmjvkKUcVAjO7hSriNPLrH213SjTL5BLE2Kf5L9HsUhKRBHFJkdZtrXpeM WgVrBYUtZyX12SH0tkncVNzGHGX5xgy3vIrSUkUcwA2pXG3GB9dVP9Kw48N9wMs9lu+J tM0mE8hswicnophvQgKMB+u3Dw17Ybn2bTva8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NOnco/Cvx2mg6+/Y/0FweVdPOcj5yDorUWvINKyGrJA=; b=ceXnY6xkQivgqlYC6QwPdRUAk6XHDs/kZTNGohmPU/jc1wi3x2etj2NekbOGptv4Ub kC7htxdcrI7EA1z/4oZTNLUqTd87v4N6d69upOCGfKG/dXSGTXKL7+xgiRYc53F7S4F4 cFYHXQm8Zn6pHQtw4kTmSCxpl9TOgpVzh0dxLPuvWG+B6q3ZNQroWYYVw+V2+H1CfFqO CE1gxgcqotUr7aIB4p54m21Romh7/i7MZvZfMG/e+0vc2zAuaCOlwlyz4A9Ouy6A6WZ5 txjNfKgLAh1OXsQqOfZ5DBQR77qAjy8TeYq8PgQMVTrtceqViBXLbHLTswiOc1JCjxph z+fg== X-Gm-Message-State: AODbwcDDS7AUarSflOyJ/18oohbL65yblm2wah6q82mgjem+2PxNlPgw fk316cIP1ZEm7j+b X-Received: by 10.28.133.210 with SMTP id h201mr2149655wmd.3.1496328541135; Thu, 01 Jun 2017 07:49:01 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id j65sm25162535wmg.0.2017.06.01.07.48.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Jun 2017 07:48:57 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 5EDCC3E0422; Thu, 1 Jun 2017 15:49:15 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: pbonzini@redhat.com, doug16k@gmail.com, imbrenda@linux.vnet.ibm.com Date: Thu, 1 Jun 2017 15:49:12 +0100 Message-Id: <20170601144915.20778-2-alex.bennee@linaro.org> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170601144915.20778-1-alex.bennee@linaro.org> References: <20170601144915.20778-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::234 Subject: [Qemu-devel] [PATCH v2 1/4] gdbstub: modernise DEBUG_GDB X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Convert the a gdb_debug helper which compiles away to nothing when not used but still ensures the format strings are checked. There is some minor code motion for the incorrect checksum message to report it before we attempt to send the reply. Signed-off-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Greg Kurz --- gdbstub.c | 77 +++++++++++++++++++++++++++------------------------------------ 1 file changed, 33 insertions(+), 44 deletions(-) -- 2.13.0 diff --git a/gdbstub.c b/gdbstub.c index 86eed4f97c..a249846954 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -271,7 +271,20 @@ static int gdb_signal_to_target (int sig) return -1; } -//#define DEBUG_GDB +/* #define DEBUG_GDB */ + +#ifdef DEBUG_GDB +# define DEBUG_GDB_GATE 1 +#else +# define DEBUG_GDB_GATE 0 +#endif + +#define gdb_debug(fmt, ...) do { \ + if (DEBUG_GDB_GATE) { \ + fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ + } \ +} while (0) + typedef struct GDBRegisterState { int base_reg; @@ -547,9 +560,7 @@ static int put_packet_binary(GDBState *s, const char *buf, int len) /* return -1 if error, 0 if OK */ static int put_packet(GDBState *s, const char *buf) { -#ifdef DEBUG_GDB - printf("reply='%s'\n", buf); -#endif + gdb_debug("reply='%s'\n", buf); return put_packet_binary(s, buf, strlen(buf)); } @@ -955,9 +966,9 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf) uint8_t *registers; target_ulong addr, len; -#ifdef DEBUG_GDB - printf("command='%s'\n", line_buf); -#endif + + gdb_debug("command='%s'\n", line_buf); + p = line_buf; ch = *p++; switch(ch) { @@ -1518,17 +1529,14 @@ static void gdb_read_byte(GDBState *s, int ch) /* Waiting for a response to the last packet. If we see the start of a new command then abandon the previous response. */ if (ch == '-') { -#ifdef DEBUG_GDB - printf("Got NACK, retransmitting\n"); -#endif + gdb_debug("Got NACK, retransmitting\n"); put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); + } else if (ch == '+') { + gdb_debug("Got ACK\n"); + } else { + gdb_debug("Got '%c' when expecting ACK/NACK\n", ch); } -#ifdef DEBUG_GDB - else if (ch == '+') - printf("Got ACK\n"); - else - printf("Got '%c' when expecting ACK/NACK\n", ch); -#endif + if (ch == '+' || ch == '$') s->last_packet_len = 0; if (ch != '$') @@ -1549,9 +1557,7 @@ static void gdb_read_byte(GDBState *s, int ch) s->line_sum = 0; s->state = RS_GETLINE; } else { -#ifdef DEBUG_GDB - printf("gdbstub received garbage between packets: 0x%x\n", ch); -#endif + gdb_debug("received garbage between packets: 0x%x\n", ch); } break; case RS_GETLINE: @@ -1567,9 +1573,7 @@ static void gdb_read_byte(GDBState *s, int ch) /* end of command, start of checksum*/ s->state = RS_CHKSUM1; } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { -#ifdef DEBUG_GDB - printf("gdbstub command buffer overrun, dropping command\n"); -#endif + gdb_debug("command buffer overrun, dropping command\n"); s->state = RS_IDLE; } else { /* unescaped command character */ @@ -1583,9 +1587,7 @@ static void gdb_read_byte(GDBState *s, int ch) s->state = RS_CHKSUM1; } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { /* command buffer overrun */ -#ifdef DEBUG_GDB - printf("gdbstub command buffer overrun, dropping command\n"); -#endif + gdb_debug("command buffer overrun, dropping command\n"); s->state = RS_IDLE; } else { /* parse escaped character and leave escape state */ @@ -1597,25 +1599,18 @@ static void gdb_read_byte(GDBState *s, int ch) case RS_GETLINE_RLE: if (ch < ' ') { /* invalid RLE count encoding */ -#ifdef DEBUG_GDB - printf("gdbstub got invalid RLE count: 0x%x\n", ch); -#endif + gdb_debug("got invalid RLE count: 0x%x\n", ch); s->state = RS_GETLINE; } else { /* decode repeat length */ int repeat = (unsigned char)ch - ' ' + 3; if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) { /* that many repeats would overrun the command buffer */ -#ifdef DEBUG_GDB - printf("gdbstub command buffer overrun," - " dropping command\n"); -#endif + gdb_debug("command buffer overrun, dropping command\n"); s->state = RS_IDLE; } else if (s->line_buf_index < 1) { /* got a repeat but we have nothing to repeat */ -#ifdef DEBUG_GDB - printf("gdbstub got invalid RLE sequence\n"); -#endif + gdb_debug("got invalid RLE sequence\n"); s->state = RS_GETLINE; } else { /* repeat the last character */ @@ -1630,9 +1625,7 @@ static void gdb_read_byte(GDBState *s, int ch) case RS_CHKSUM1: /* get high hex digit of checksum */ if (!isxdigit(ch)) { -#ifdef DEBUG_GDB - printf("gdbstub got invalid command checksum digit\n"); -#endif + gdb_debug("got invalid command checksum digit\n"); s->state = RS_GETLINE; break; } @@ -1643,21 +1636,17 @@ static void gdb_read_byte(GDBState *s, int ch) case RS_CHKSUM2: /* get low hex digit of checksum */ if (!isxdigit(ch)) { -#ifdef DEBUG_GDB - printf("gdbstub got invalid command checksum digit\n"); -#endif + gdb_debug("got invalid command checksum digit\n"); s->state = RS_GETLINE; break; } s->line_csum |= fromhex(ch); if (s->line_csum != (s->line_sum & 0xff)) { + gdb_debug("got command packet with incorrect checksum\n"); /* send NAK reply */ reply = '-'; put_buffer(s, &reply, 1); -#ifdef DEBUG_GDB - printf("gdbstub got command packet with incorrect checksum\n"); -#endif s->state = RS_IDLE; } else { /* send ACK reply */