From patchwork Fri Jun 2 13:05:15 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: 101246 Delivered-To: patch@linaro.org Received: by 10.182.202.35 with SMTP id kf3csp183964obc; Fri, 2 Jun 2017 06:05:32 -0700 (PDT) X-Received: by 10.200.49.81 with SMTP id h17mr2899198qtb.13.1496408732427; Fri, 02 Jun 2017 06:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496408732; cv=none; d=google.com; s=arc-20160816; b=q71BjzByS1itTzsCb89+XIV2pjMp1SjJGxpfR+pGuQ1w3uTGIk+ciGqNruizvGqtHd I0a+9VMWQ8AETwpzD5GiuEj42EE0u8vXcVtgLNkx+OhdAugyxWsRdh8QFC2YQpJJ7kfD gv+NwMrFO41dta3sz8KG07QMUEnNJ9VR57iRWotUVdv0jJtXKlySVlGR1Zd3jKvv9qY8 1rBIEQSBCNjoGAnm0zGCV2KoA/FU8mCl859osvykVH9QyNMgmh0IijsR/SBFgwps7d9h NyAjwk6GOSDg3pf4grXLoGbqxP7iyj5Zpgr6X3UPVhYo8mSMMwtDc0RaX2tfKg26GIFF nSRg== 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=zNhqsXs04UE8WSrEfjYYn9ABWEMnP7OXUGR7L0wc4IHk0gMxK94avdG/JNGvjEjq2+ IvI1T64sVFXS5RLLkT6garZKe+b/B7zzT92VHqXKX68L5sl2h1/kohBnApmHfV88dkAe QfnRZR35QF3BHcJ+eSCfMEyQZkuypdDxRlVMP7qo17MHr0MmxDcRrzfUA5H+3BgSkImV JHim16t6Lf9EQw76G3CL8/kUicwWcUq0RpTCMZeZsL/bIJMJ7mWNKxIyfcDgOaFrPpyG gqzJtWPBr55c9XgUTQG0hs3VO1y6m45SOFrK8wiSykhMEh53w5SQ/GKGpDB1tMnMADkE EV8Q== 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 y74si200558qky.95.2017.06.02.06.05.31 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 02 Jun 2017 06:05:32 -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]:49701 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGmGf-0006IP-Sx for patch@linaro.org; Fri, 02 Jun 2017 09:05:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33947) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dGmGI-0006G3-9u for qemu-devel@nongnu.org; Fri, 02 Jun 2017 09:05:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dGmGD-0002ex-Vm for qemu-devel@nongnu.org; Fri, 02 Jun 2017 09:05:06 -0400 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]:37063) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dGmGD-0002dL-N1 for qemu-devel@nongnu.org; Fri, 02 Jun 2017 09:05:01 -0400 Received: by mail-wm0-x230.google.com with SMTP id d127so26440280wmf.0 for ; Fri, 02 Jun 2017 06:05:01 -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=de7JHAFbSJ0mwTLXXL9Ve8A5+pfObP0N6nRnchhLhbbqAMDXUZO/Aey9ZGk5y398dk LwLK3XnMNpvrpkPonnN9Hslb287aYEX3SGjKBAcr9dj5yxfQ7YH1jp5N5wmv9D8iyj2i Fkl7r5UkrOcVZW91NRGlTNVhGoU9uPl/P73Y0= 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=DgzyxkZq+s96E274WoXyjFbtMvG9RdhlupsBo+vlgAQv2N0XAZZTBtOgRvT5UtOA2i tylmOH0i0+obMnJ4zS0NRu8WEtl7a7plEf0gQaW02UfhzKIfZypiQGLbYSisw1ikhbaA TZhCX9SOJawhZrqG15flgIIRvcBlQMSJETp6jtc2hLgKs31jcPZf7VIUsEDnJg6/juZ4 flNK3FoD1+EXZ/jhoy0516B+wEvClAHlh8jPSrEVd689APcSjoC52C4YYwIH36MpeSJZ OiEN81OnNwJduKRxCsUbeTx4ljH/MqWNI4FEbo/cQfoo9XOysII02k2SbZCctFPht31N tHyw== X-Gm-Message-State: AODbwcBWyiwVJTiOQXIBFj7+4mv/a7BqSrif4k9E+IXaBQSYS9cxsC4t MBv0M/p+n7LfEWO1 X-Received: by 10.28.87.207 with SMTP id l198mr3207150wmb.114.1496408700505; Fri, 02 Jun 2017 06:05:00 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id x133sm2817136wme.0.2017.06.02.06.04.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Jun 2017 06:04:59 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id F006D3E01BB; Fri, 2 Jun 2017 14:05:18 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org, pbonzini@redhat.com, doug16k@gmail.com, imbrenda@linux.vnet.ibm.com Date: Fri, 2 Jun 2017 14:05:15 +0100 Message-Id: <20170602130518.22943-2-alex.bennee@linaro.org> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170602130518.22943-1-alex.bennee@linaro.org> References: <20170602130518.22943-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::230 Subject: [Qemu-devel] [PATCH v3 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 */