From nobody Thu Nov 13 03:23:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1576084079; cv=none; d=zohomail.com; s=zohoarc; b=hEMsmEJ8t0UAXC3wKsfLfSL8QzIXFHztDBQWZarM5XBsIc+f1j33hIcW5cQJaZYLs0RMs1lMm58nkM6hsI8C4mCgDU0b6zlUuZJeGNY7fPu7KR5S6JSjhe9ghF14bBalyiiz6GWaR97HO0J0TyNpX6mvgkUM5Aa9prNBx/PGOsg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576084079; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=10ri5OPZ3wXmAnewtDMagCSgDZyQ+hI5ZaV97yVftek=; b=I1CysInzHTe9ZIB1LeO69vXkSIxNIyQ0Cf6gxEaxI9ub8iwiQI6YM8OzkpaA2bws8r80rag+jK3noFDw3HC1QvulGdf3ili1tbH3FO7oFGlZJwTLAqWexbWL6ZRj5MfeWuUoRMmDd6vymQYIduWqhTiZf4U6eql9SBr8cPyUJbo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 157608407918323.02777232465587; Wed, 11 Dec 2019 09:07:59 -0800 (PST) Received: from localhost ([::1]:46330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1if5Sv-00071p-AC for importer@patchew.org; Wed, 11 Dec 2019 12:07:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:38191) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1if5QW-0004uh-Rs for qemu-devel@nongnu.org; Wed, 11 Dec 2019 12:05:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1if5QV-000345-1O for qemu-devel@nongnu.org; Wed, 11 Dec 2019 12:05:28 -0500 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:52736) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1if5QU-00032r-O8 for qemu-devel@nongnu.org; Wed, 11 Dec 2019 12:05:26 -0500 Received: by mail-wm1-x341.google.com with SMTP id p9so7965661wmc.2 for ; Wed, 11 Dec 2019 09:05:26 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id v3sm2868697wru.32.2019.12.11.09.05.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Dec 2019 09:05:24 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 97A1D1FF91; Wed, 11 Dec 2019 17:05:21 +0000 (GMT) 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=10ri5OPZ3wXmAnewtDMagCSgDZyQ+hI5ZaV97yVftek=; b=Bc7xrHh0KYq77Ym7OBgBEyaNlc/xIsOrO0NI34t4NZcKerf9MgOWguMKaUNY5z9pOm A4BvOegLz286AOX/rGIzZCeEY3lwL1uVwe1cGHWIPUwa6S2gnti4LACCpJZ19W/7JPR6 SWhUdVmKMP55LWDBe/p8RpTLFg5sE2Q0a6utJHSMNnHMQwdea3qYQyX7kdUD9NL35qya EMHJnkEU34CDSTZOfOQ1rZVmiE9hFoner13NVR2ac2ilRr63+5vIihxxmXQeb4+zIMPh QnOAc47Bs8AiOCPv1intd8LQQWiEen8h0QKLbIhaH5YFPClHLUBZIQ52HYmtluHR1gCR ijcw== 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=10ri5OPZ3wXmAnewtDMagCSgDZyQ+hI5ZaV97yVftek=; b=jNuTFBSQYilEJhITdV6X51jooMpw3ySHn7d6JCPwf4lNGneX1MzG77lwkBt8CJKaSg VqLHA/Ym95VVd7a3cQbt6usOn9wV2OsClIOkmzEzihOXjvqUYRAlDI2ag/fJhSyXBwba ktMYvrm5KeZfJIOdZ7K23fu6E0WaEEIbY4Mf1rGqr5FMOxDV8CIotF/iVOXO6Y/fl0V6 kktF/neDMNyyJei2c/pqu20X9AD+QIURQdVf+zSkw86gyqwUd5x8OABFAor+575qXGMg ynrBHdZ6dZQfGQxe436ka0HKZu3bnVjwhVOwlYy2EOjfB249QBnNXvdAbs/EKUPchrtw SMRA== X-Gm-Message-State: APjAAAUq3XQMAyOIgy0R6h8kvird04Gdc1MoO3MIba89YAF14yI8qGtd 4sC7D/aB/+kPYyveowYVLXO0+w== X-Google-Smtp-Source: APXvYqy8NNTElkFbDc7hLlvC0hwJSF83szBLlzh31c2Clv7PcIybGjDl9liS5vH+WM5vgBie4oAp4w== X-Received: by 2002:a7b:c1cc:: with SMTP id a12mr859717wmj.53.1576083925545; Wed, 11 Dec 2019 09:05:25 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v3 04/20] gdbstub: move mem_buf to GDBState and use GByteArray Date: Wed, 11 Dec 2019 17:05:04 +0000 Message-Id: <20191211170520.7747-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191211170520.7747-1-alex.bennee@linaro.org> References: <20191211170520.7747-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::341 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: damien.hedde@greensocs.com, luis.machado@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , richard.henderson@linaro.org, alan.hayward@arm.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) This is in preparation for further re-factoring of the register API with the rest of the code. Theoretically the read register function could overwrite the MAX_PACKET_LENGTH buffer although currently all registers are well within the size range. Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Richard Henderson Reviewed-by: Damien Hedde Tested-by: Damien Hedde --- v3 - fixed up email on Damien's tags --- gdbstub.c | 56 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/gdbstub.c b/gdbstub.c index 1816e7af827..ba63710cdcf 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -367,6 +367,7 @@ typedef struct GDBState { char syscall_buf[256]; gdb_syscall_complete_cb current_syscall_cb; GString *str_buf; + GByteArray *mem_buf; } GDBState; =20 /* By default use no IRQs and no timers while single stepping so as to @@ -382,6 +383,7 @@ static void init_gdbserver_state(void) memset(&gdbserver_state, 0, sizeof(GDBState)); gdbserver_state.init =3D true; gdbserver_state.str_buf =3D g_string_new(NULL); + gdbserver_state.mem_buf =3D g_byte_array_sized_new(MAX_PACKET_LENGTH); } =20 #ifndef CONFIG_USER_ONLY @@ -576,12 +578,13 @@ static void memtohex(GString *buf, const uint8_t *mem= , int len) g_string_append_c(buf, '\0'); } =20 -static void hextomem(uint8_t *mem, const char *buf, int len) +static void hextomem(GByteArray *mem, const char *buf, int len) { int i; =20 for(i =3D 0; i < len; i++) { - mem[i] =3D (fromhex(buf[0]) << 4) | fromhex(buf[1]); + guint8 byte =3D fromhex(buf[0]) << 4 | fromhex(buf[1]); + g_byte_array_append(mem, &byte, 1); buf +=3D 2; } } @@ -1412,7 +1415,6 @@ static int cmd_parse_params(const char *data, const c= har *schema, typedef struct GdbCmdContext { GdbCmdVariant *params; int num_params; - uint8_t mem_buf[MAX_PACKET_LENGTH]; } GdbCmdContext; =20 typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx); @@ -1503,6 +1505,7 @@ static void run_cmd_parser(const char *data, const Gd= bCmdParseEntry *cmd) } =20 g_string_set_size(gdbserver_state.str_buf, 0); + g_byte_array_set_size(gdbserver_state.mem_buf, 0); =20 /* In case there was an error during the command parsing we must * send a NULL packet to indicate the command is not supported */ @@ -1715,8 +1718,8 @@ static void handle_set_reg(GdbCmdContext *gdb_ctx, vo= id *user_ctx) } =20 reg_size =3D strlen(gdb_ctx->params[1].data) / 2; - hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size); - gdb_write_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf, + hextomem(gdbserver_state.mem_buf, gdb_ctx->params[1].data, reg_size); + gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->dat= a, gdb_ctx->params[0].val_ull); put_packet("OK"); } @@ -1735,14 +1738,17 @@ static void handle_get_reg(GdbCmdContext *gdb_ctx, = void *user_ctx) return; } =20 - reg_size =3D gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf, + reg_size =3D gdb_read_register(gdbserver_state.g_cpu, + gdbserver_state.mem_buf->data, gdb_ctx->params[0].val_ull); if (!reg_size) { put_packet("E14"); return; + } else { + g_byte_array_set_size(gdbserver_state.mem_buf, reg_size); } =20 - memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, reg_size); + memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_s= ize); put_strbuf(); } =20 @@ -1759,11 +1765,11 @@ static void handle_write_mem(GdbCmdContext *gdb_ctx= , void *user_ctx) return; } =20 - hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data, + hextomem(gdbserver_state.mem_buf, gdb_ctx->params[2].data, gdb_ctx->params[1].val_ull); if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].v= al_ull, - gdb_ctx->mem_buf, - gdb_ctx->params[1].val_ull, true)) { + gdbserver_state.mem_buf->data, + gdbserver_state.mem_buf->len, true)) { put_packet("E14"); return; } @@ -1784,14 +1790,17 @@ static void handle_read_mem(GdbCmdContext *gdb_ctx,= void *user_ctx) return; } =20 + g_byte_array_set_size(gdbserver_state.mem_buf, gdb_ctx->params[1].val_= ull); + if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].v= al_ull, - gdb_ctx->mem_buf, - gdb_ctx->params[1].val_ull, false)) { + gdbserver_state.mem_buf->data, + gdbserver_state.mem_buf->len, false)) { put_packet("E14"); return; } =20 - memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1]= .val_ull); + memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, + gdbserver_state.mem_buf->len); put_strbuf(); } =20 @@ -1806,9 +1815,9 @@ static void handle_write_all_regs(GdbCmdContext *gdb_= ctx, void *user_ctx) } =20 cpu_synchronize_state(gdbserver_state.g_cpu); - registers =3D gdb_ctx->mem_buf; len =3D strlen(gdb_ctx->params[0].data) / 2; - hextomem(registers, gdb_ctx->params[0].data, len); + hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len); + registers =3D gdbserver_state.mem_buf->data; for (addr =3D 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len >= 0; addr++) { reg_size =3D gdb_write_register(gdbserver_state.g_cpu, registers, = addr); @@ -1825,11 +1834,14 @@ static void handle_read_all_regs(GdbCmdContext *gdb= _ctx, void *user_ctx) cpu_synchronize_state(gdbserver_state.g_cpu); len =3D 0; for (addr =3D 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++)= { - len +=3D gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf= + len, + len +=3D gdb_read_register(gdbserver_state.g_cpu, + gdbserver_state.mem_buf->data + len, addr); } + /* FIXME: This is after the fact sizing */ + g_byte_array_set_size(gdbserver_state.mem_buf, len); =20 - memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, len); + memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len); put_strbuf(); } =20 @@ -2078,6 +2090,7 @@ static void handle_query_offsets(GdbCmdContext *gdb_c= tx, void *user_ctx) #else static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx) { + const guint8 zero =3D 0; int len; =20 if (!gdb_ctx->num_params) { @@ -2092,11 +2105,12 @@ static void handle_query_rcmd(GdbCmdContext *gdb_ct= x, void *user_ctx) } =20 len =3D len / 2; - hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len); - gdb_ctx->mem_buf[len++] =3D 0; - qemu_chr_be_write(gdbserver_state.mon_chr, gdb_ctx->mem_buf, len); + g_byte_array_set_size(gdbserver_state.mem_buf, len); + hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len); + g_byte_array_append(gdbserver_state.mem_buf, &zero, 1); + qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->da= ta, + gdbserver_state.mem_buf->len); put_packet("OK"); - } #endif =20 --=20 2.20.1