From patchwork Wed May 31 15:09:32 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: 100797 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp370885qge; Wed, 31 May 2017 08:12:25 -0700 (PDT) X-Received: by 10.55.95.5 with SMTP id t5mr30109036qkb.64.1496243545821; Wed, 31 May 2017 08:12:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496243545; cv=none; d=google.com; s=arc-20160816; b=MnBPPD7JSb57dzC06GuprK/DyLh5j7e0kb+YApNyjgG8Z3bEokwKSzXdd+aTHywNkm d8CTtKtCqEWFBFLFGi1GMsnuALXfQbSJd9rXhyThVgCmbHI0IhNMPZO19/CgApEjWmEC 2Wd6etuxL4OqR6ufkxa2oc+WIOvtKVRY2Ce7wuG+tOyyZtQJuRESgB2co9LoZnh5e2at CPuH67eFVg9XIIRUdsJ6ktP0XI5jPQmV6UXXIvsrhMGnpYG/GSlo49spu6ZeEW2Z+a0A QwPNm+BF0z+aWPLpZJ9oYFLrvKbeDTP/wavVK0WsrxCfXYxh1iLrVP17aqXBJJuYbrZL gVGg== 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=7KFhDR0nMsc+M7xnsT5SMXY6H1oyT2gD10ARpOoMEW0=; b=jyXjs6Wn4dzFyHsF1GMpRxUAQGr1dxCOsokgizwGchchdjPOEpwoME1uBZvlt33Ain GDaSB0M5dUTKLXEMuCrj8DHoHpbiUa7df9Am4ecpC3cufpmbRzhmqYOF14qrO0GnhnTr JUBzsfn/HfoMl6oGc2AbUVUWRSw6sLPJBnUKlLSELplNxufvxxT1gj9p42+O5rT9ccte EBa0dy4YB3pkCUN3yOVT4bsPxQaSyYUsfuddT+dk0pYD76JlJYMl6Zt9TR0WuGjJq2vY FUYVb6PST5l7itTdh8BjzvVw3ueoqB8VC9wuh7gocbTnzmzEQjY7wK6W7QVXMNWTaDxt oLxA== 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 g1si16731317qtf.280.2017.05.31.08.12.25 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 31 May 2017 08:12:25 -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]:60102 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG5IN-0001Df-3o for patch@linaro.org; Wed, 31 May 2017 11:12:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45193) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG5FS-0007sz-BF for qemu-devel@nongnu.org; Wed, 31 May 2017 11:09:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG5FP-0005Yk-RL for qemu-devel@nongnu.org; Wed, 31 May 2017 11:09:22 -0400 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]:35324) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dG5FP-0005WS-G3 for qemu-devel@nongnu.org; Wed, 31 May 2017 11:09:19 -0400 Received: by mail-wm0-x22f.google.com with SMTP id b84so122842756wmh.0 for ; Wed, 31 May 2017 08:09:19 -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=7KFhDR0nMsc+M7xnsT5SMXY6H1oyT2gD10ARpOoMEW0=; b=FKGxcMZhkkE0676ShronwvDOGTROtmTx4Et0xfIE0zmcP/e7RAzNzu+2wz9Vw4nTw3 UlfDT484bUsBIb43Ndg7CygAJmdIPBdTP0vQhRRqTgp6sqEFXOhoDV6lBaPJc2QNFbBX OqmtVfFNhukcCjuqp8ZjC615J5CUZwRv3JK3M= 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=7KFhDR0nMsc+M7xnsT5SMXY6H1oyT2gD10ARpOoMEW0=; b=g7xeI+4Y/kO8vPHukL+kFdVV0u3Vv2q9Hn61ybrf1/1q2i9+P4vnK+i4dQCxZyioEc upyMTsi6iadazdfxAZBG7iIuK7nsuMEylGZNH0phZvgUJQvTmD3hAK3eNUEVlpEh3Lbl +EB/6hjZg23azU5CCKo57QtO12gtu2MxfH/w08NKGLLOyUSB/p/CNMTw5Nv1B6GPgcjx uH8m3kSGKjxED/e781XseHvukzwzOMfZmuiRLE8Eedxq4BNdmsgfV01+Mj4Wau2AiRdH mpDD6QpFgI3FAIG4YWt+1HhNXm9u++Mjr6X9f3lvjZk6eSMNdsRpLPu28CgH3UxH143I VLzQ== X-Gm-Message-State: AODbwcAl1Si9aWSZXC0uRDRHuI9csGRdPcH7zLqYu1Jt+/bj/tMH6IAw mDk45EhMXKk50dA/ X-Received: by 10.28.136.83 with SMTP id k80mr5402034wmd.38.1496243358399; Wed, 31 May 2017 08:09:18 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id m11sm24080607wmg.34.2017.05.31.08.09.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 May 2017 08:09:16 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id A67463E01C6; Wed, 31 May 2017 16:09:33 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: pbonzini@redhat.com, doug16k@gmail.com, imbrenda@linux.vnet.ibm.com Date: Wed, 31 May 2017 16:09:32 +0100 Message-Id: <20170531150933.10156-2-alex.bennee@linaro.org> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170531150933.10156-1-alex.bennee@linaro.org> References: <20170531150933.10156-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::22f Subject: [Qemu-devel] [PATCH v1 1/2] 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 --- gdbstub.c | 77 +++++++++++++++++++++++++++------------------------------------ 1 file changed, 33 insertions(+), 44 deletions(-) -- 2.13.0 Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Greg Kurz 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 */