From nobody Tue Feb 10 00:59:29 2026 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=1576843889; cv=none; d=zohomail.com; s=zohoarc; b=na2Iea0rlRUHFbHFoHDuVC65Dt50M6eDSMQeBMSL/03E5ps6+5JQhJXW+Rly6l6l6hiwND/jWv4lc4qgZXY4hKjt9E19B1Qh5pz1PMjQ9t/OLg8+EYswPwD5fMoxzm+mhwRVbo3cKAG3v7fydd2IRDVq4PCFGyQjVh3PHlaYmpM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576843889; 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=Zc6kvGdtDLz2ojH8CUPfJdv2oY/VOoynhGQP1OaS620=; b=Uf+NVm2ui58q05xqGfrZzDBGUZ5rp9Pn8CPdM6wYjzerAZRL77tKTgp+X4dXam3WXnnbbvTPuV5eJEpMDfNs1SBnqRZyIui5r0gsa3lZbMB1B6DAsW01ez0w3E5C+yC61LN4dY9mXVUpRYGJNF8mzJKNi3B+2kCnzg8Ks3EjMc4= 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 1576843889812324.0574746919049; Fri, 20 Dec 2019 04:11:29 -0800 (PST) Received: from localhost ([::1]:54078 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iiH7w-0004VE-GS for importer@patchew.org; Fri, 20 Dec 2019 07:11:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:52571) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iiH1Y-0004gw-1D for qemu-devel@nongnu.org; Fri, 20 Dec 2019 07:04:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iiH1W-0007fw-1a for qemu-devel@nongnu.org; Fri, 20 Dec 2019 07:04:51 -0500 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:36430) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iiH1V-0007ak-FF for qemu-devel@nongnu.org; Fri, 20 Dec 2019 07:04:49 -0500 Received: by mail-wm1-x344.google.com with SMTP id p17so8990524wma.1 for ; Fri, 20 Dec 2019 04:04:49 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id b67sm10038051wmc.38.2019.12.20.04.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2019 04:04:42 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 3F9AB1FF91; Fri, 20 Dec 2019 12:04:39 +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=Zc6kvGdtDLz2ojH8CUPfJdv2oY/VOoynhGQP1OaS620=; b=UGdI7+HZhPUey0/nxd9li+tSmYYOvpsFqXiegs2Ajtxn22KbYgx4qIgI6tgHjfgv6o bHUPldO21h6X3LfUDZsdKmuXS8KS5fKVtUkQtJYsEeIxZtOoJ+mxyDexX6eGnrw676DL IRCT+a85SxMC1ovNXUlZ9dm2nbDP6qpnxGlOIlPemgZegV6bpX/5MKrTRgYCqtLqUpsl RnD9WV/px5dQjDlYwxEK+2/j7+QtBBALzrM7bOPfAWwTEFvIZ2XSlFLApbqwxCeameBr gYp6I9DJ07y46Ntyz+VuQl1nP7KXv3s6e3u56v+hN6N0xyjAY60cgpiuaUXNajzuZPFa beew== 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=Zc6kvGdtDLz2ojH8CUPfJdv2oY/VOoynhGQP1OaS620=; b=e6c3EJfZ1wjLBJ6uZoGdjLl9gGPrYagn9J+Uoc7S3+h26Tb7Qo0SNY0G6jeMkerSVK I9VQoQjYsuG0aAO6ZMKYLzORCzJL3kPRNaAGh/IKfK8Kd3SRiA3l2c+mOC4cef9qYRLv XG7WXxp8eWK1uudA6LADDPYqa7v9EtpSzoJDhREhKL4dKFS7dFi8AuB4agk808yJAKIt 1yhHIE38obQoHm1Xg32TKqS9vUF5xyeb0wI4iMyBSmmQMt336yBis/aec8x3PwXEHV+C wEg4JtB7ORT8TgyvoM0vknDCfgfuGtVFyN1YNtyXcfQ6yEODz9XZK/A1FqpuLLGIIeB5 cbfA== X-Gm-Message-State: APjAAAWI8KGOmeeD2H06XjrtP5dfHvaE3zlaEMdPMII3t2yVmiVoTzuT Fy72ZX7t+0q88RPp00i6g+PQ5TyT1Cg= X-Google-Smtp-Source: APXvYqz2Q786Jp6y8vbE/GFJ2slj+nnK9ojQMu6WXCzUuq3ADFMzewMcezwn3l502hfb48g4/VNpDQ== X-Received: by 2002:a7b:c346:: with SMTP id l6mr3426414wmj.100.1576843488144; Fri, 20 Dec 2019 04:04:48 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v4 04/21] gdbstub: move mem_buf to GDBState and use GByteArray Date: Fri, 20 Dec 2019 12:04:21 +0000 Message-Id: <20191220120438.16114-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191220120438.16114-1-alex.bennee@linaro.org> References: <20191220120438.16114-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::344 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 , 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 --- v4 - don't set_set to len on rcmd (it's zeroed before we get here) 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 80d99c209ff..736e3cc1052 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) { @@ -2091,12 +2104,13 @@ static void handle_query_rcmd(GdbCmdContext *gdb_ct= x, void *user_ctx) return; } =20 + g_assert(gdbserver_state.mem_buf->len =3D=3D 0); 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); + 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