From nobody Tue Feb 10 01:50:14 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580922719791317.07232377383946; Wed, 5 Feb 2020 09:11:59 -0800 (PST) Received: from localhost ([::1]:53666 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izODW-0006YA-9p for importer@patchew.org; Wed, 05 Feb 2020 12:11:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34809) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izOCE-0004mY-7K for qemu-devel@nongnu.org; Wed, 05 Feb 2020 12:10:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izOCC-0004SS-HZ for qemu-devel@nongnu.org; Wed, 05 Feb 2020 12:10:38 -0500 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:37365) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1izOCC-0004MV-AA for qemu-devel@nongnu.org; Wed, 05 Feb 2020 12:10:36 -0500 Received: by mail-wm1-x344.google.com with SMTP id f129so3740207wmf.2 for ; Wed, 05 Feb 2020 09:10:36 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id u14sm535716wrm.51.2020.02.05.09.10.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 09:10:32 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id E81E51FF91; Wed, 5 Feb 2020 17:10:31 +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=/9ttXWj9woxUHGEh9afSaXry0nHjOv95R0fO6zCdrhM=; b=hqURFpbsSZJzJwsTwNij4APwyItSNHyLAn5GeqsP2aacekYv8uHOvU/coVk4J6bTRl XC8UJUbomdplZtIb16JgjEW+GQzkh/i89KxUMBBDTJpA7LLflB989Ky7MhIteXNX05Jh aeoor8MRWtRLidYYcYdFvalLuwY4DlXIBK+CnX6LO3RIAzg4vYSFBrZ/EcWNbrYs1W+S nX1z1H28HNndFNJsB8YHJznecP7825xIOjH+vT9MH29Ghsb2dkLN34UJO+JvqbD80irC 3jMe4VxhObvnAM9sQmFU+K6NO91wiOPHgrieReIjB5tBarbHFtjtyC+i2K7OS6KZOn74 m0dA== 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=/9ttXWj9woxUHGEh9afSaXry0nHjOv95R0fO6zCdrhM=; b=bWUyho75yrYz6yW4q08yRVCi7kL/kfSfJY88F2VQEkm5sNYXGVSMf83bDM3sQmXOx7 /f2TLKj3vsfqRa9Rm0XKXo158UO62Bu5nxTH+Yn4WmQBfRmQ18wdgwf753YeXK0HN5Ql b/5Ydp716loAR9ORKyZ5hSsB/Hj96Fo8X8hEIpyp1BGGtQsVeOC438ZLt92JoRfZP5CV eoYha/kuEMXyItBET9uqVZcb9oCQcpF0kvI1YrrJ4JfeEdqXSQx9KCWRCPwjmORN3VoL WVKM2WXZMaq6KYpWsGvgR9kbscNwOqL+hMmnwIbIS9MdLzLTPzxyYFwtAvENm0zqgJeU KsDg== X-Gm-Message-State: APjAAAXn+nMhIPrPXn/OFCzE3U/CWwc+4G9OCc+y8rfMBwMo3vNcXDtV caJtlRZegioRHsPQwKD6LKbOlg== X-Google-Smtp-Source: APXvYqzQMKe0vEpiJQmZV07w4V0PuCHTKQVjHNQo/Wu93Tl7GSTGrhmORlR/tYvFfR2p5bBDw7nBNA== X-Received: by 2002:a1c:7d8b:: with SMTP id y133mr6960871wmc.165.1580922635180; Wed, 05 Feb 2020 09:10:35 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v6 04/22] gdbstub: move mem_buf to GDBState and use GByteArray Date: Wed, 5 Feb 2020 17:10:13 +0000 Message-Id: <20200205171031.22582-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200205171031.22582-1-alex.bennee@linaro.org> References: <20200205171031.22582-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@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 --- 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 b6dfeac5d1c..49abd2bdc78 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