From patchwork Wed Jul 12 10:52:13 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: 107476 Delivered-To: patch@linaro.org Received: by 10.140.101.44 with SMTP id t41csp648415qge; Wed, 12 Jul 2017 03:55:28 -0700 (PDT) X-Received: by 10.55.110.133 with SMTP id j127mr5628875qkc.92.1499856928499; Wed, 12 Jul 2017 03:55:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499856928; cv=none; d=google.com; s=arc-20160816; b=uI2oxYzVZCwxn32z38Kcz2I9XSn5zFHBsmflEXtwje1TsGcKsfZ+VOJcpvEV1gj7rA GSLXlFsZPf1n+bruk6gLxiEx1lf8N4rSQL40ZcA2pV+s8Hhvhml1GX2EMuCVe6kXsOS9 13hZbOWTb5u7UYfrw1jH2+zNFQhHs3lkDmJTSj40EcgOzRlqSDT6Z0A8kIT68uF7zp9g L6iA7YtUQ8kUyfVF5GApwweDTOxFcdEz4JI7lH1ki3ywUCb0YJlH5gxRcu1HdTnWTY3i ngJvQ6k2YCxsHFyjMgBPc83v7+nksOO8n5p6xj527r/J3EsMzqdq8OQ8wAqG28FTGr6S hkZg== 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=IMqTMgNti8BoQzaOjugYl05GbQiU0olK4bgHdDtryW4=; b=pw1382qRijxYkZSvMVcn+O0Ii76Nq1S+HQ7h3jfQr36YPa9SqbJXutBVF0At5rZTRL vwnIyy4iHwRZ5UwUlYEW6hTLaIazrUt996JkaB3vclVT9wA5gRHFKsLi/pzOGPF/yIy4 +lNMl3Fm4lutGUrC959dInpkOuUVXGREBYQvXkjjmHbZxHBhRnmFjCfZTWKAb2eREVN8 OS3yLe9/5T54xlDZIWuz4SOoHOk71e12Wr9qGjCEZ/YDrmmKZb6AsOA3QY4o/DGBVyq/ +8uAVQGSUJm4ljwmjRyKUINZHF/GHaO4vcFGtcFFcNP9BIDFNaZSQN48rDLBaJ+Cei6A 8L/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.b=Sue6pbIR; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 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. [208.118.235.17]) by mx.google.com with ESMTPS id u5si1967548qtu.171.2017.07.12.03.55.27 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 12 Jul 2017 03:55:28 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.b=Sue6pbIR; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 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]:51696 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVFIj-0006Hr-HF for patch@linaro.org; Wed, 12 Jul 2017 06:55:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32896) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVFGm-0004h1-5e for qemu-devel@nongnu.org; Wed, 12 Jul 2017 06:53:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVFGj-0001zC-3m for qemu-devel@nongnu.org; Wed, 12 Jul 2017 06:53:24 -0400 Received: from mail-wr0-f170.google.com ([209.85.128.170]:32807) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dVFGi-0001ym-Ql for qemu-devel@nongnu.org; Wed, 12 Jul 2017 06:53:21 -0400 Received: by mail-wr0-f170.google.com with SMTP id r103so28274364wrb.0 for ; Wed, 12 Jul 2017 03:53:20 -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=IMqTMgNti8BoQzaOjugYl05GbQiU0olK4bgHdDtryW4=; b=Sue6pbIRs9peyf5UiyWe7GGYMnf3Z0jkP3b6JKbXAyCrfXCEnlBfmylSi6+FhsiZIF Bh/Jalvy4yV5/gBQONGE/8DDbmLjfxjxE58G8oBYM+L1ZpD0X+fe4iPc5oFps8uo7k98 CEQLDpQ6iXs56lduZ2DzQmzljrJbfIVMOHN7s= 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=IMqTMgNti8BoQzaOjugYl05GbQiU0olK4bgHdDtryW4=; b=ciSDT2718n4pggtKXuY52dS9rFIa+D6yQz/tuNrXZBpSqikBy1jEUuTj/Uxc/dvU0v OBaoY7TnRQMlsVbJf+UJVQgNObu32cpwlz4MApDEJlyP1yBBQbw6WYwqwrj2tBa5JRHm 58OyiU+4vL6vECeJSHift/rHkYcTPatX3DizNEv6S0aiN69SiQb3mUUuym3T+BZVfAwn whkZLcGVgpsEg4TPn8j1B1tQdCGGrQJuhPa374mj5HModZ2G9O6o2Y9rWZxutiFxkWeN jF7XHh2dMaBQn5Nph6bJezga6lH3dNvZLEAUIOpMM5MTEpQbS1r5zALYikLeY9saL+nP F5Zg== X-Gm-Message-State: AIVw113EOMCSnZO1PezxA39xn6lz71sjecjTTK4AhAYc3mDof3+LME4M 73njBSrzOXyXVAnS X-Received: by 10.28.23.1 with SMTP id 1mr2215984wmx.106.1499856739582; Wed, 12 Jul 2017 03:52:19 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id e17sm1958582wma.19.2017.07.12.03.52.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jul 2017 03:52:17 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 0C9CD3E07A0; Wed, 12 Jul 2017 11:52:17 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: pbonzini@redhat.com Date: Wed, 12 Jul 2017 11:52:13 +0100 Message-Id: <20170712105216.747-2-alex.bennee@linaro.org> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170712105216.747-1-alex.bennee@linaro.org> References: <20170712105216.747-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.128.170 Subject: [Qemu-devel] [PATCH v4 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 ec4e4b25be..31db2309aa 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -272,7 +272,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; @@ -548,9 +561,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)); } @@ -956,9 +967,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) { @@ -1519,17 +1530,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 != '$') @@ -1550,9 +1558,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: @@ -1568,9 +1574,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 */ @@ -1584,9 +1588,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 */ @@ -1598,25 +1600,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 */ @@ -1631,9 +1626,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; } @@ -1644,21 +1637,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 */