From patchwork Thu Aug 24 16:39:08 2023 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: 1825514 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=AULp9ltE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RWpl62K6Jz1yfF for ; Fri, 25 Aug 2023 02:41:54 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDMz-0006a6-Uw; Thu, 24 Aug 2023 12:39:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDMx-0006Ym-Tk for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:39:39 -0400 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qZDMe-00089V-Bs for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:39:39 -0400 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-31c71898109so1179611f8f.2 for ; Thu, 24 Aug 2023 09:39:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692895159; x=1693499959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7n08jET4sLnSCRFO+uf5UKlQj+0C0tBz07A/O5rXTPI=; b=AULp9ltE+Upm0Zre8jkcTgPU+A9Gye0v54BDFw2rxzq2XLwYSVAkNr1xZVteEC4y0T J06x0UhcaORGFiPCpN+QG69ccZge7skEiIMEyu5WfDRT9XjE9G9ZqDlePKKMTdq9xWMW RzhwLo6FnI0POZ1Y34l+saOalojyJbp5OOBJjD7Y92W8P1kAYMLYb7C21yZxk4dYztgd +0A/vRG8kz4nHRxJxWDkpxocTIkQAiXCdSGIgrO8xHECyYBHgUuzFGphy1oSYeZNcwqS v0fcbxRNr4Ldgj1qYIDv5EAADPrkFMur17fPjx3RPU2/qd9Kfuba9WmljjlUOJOLGbYB DWNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692895159; x=1693499959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7n08jET4sLnSCRFO+uf5UKlQj+0C0tBz07A/O5rXTPI=; b=BSqMLkhtQKY/CMIV4qtkDm+Gw6wFh54ScBOkbhhha0S+Qdb9VA9SvZz7zK/cYsKL20 4NtFnhVDDWB7VjQ3003Dq86DkHWtYxXFXewWtZOfPuj648DmX3jIzalk8QbjWD49wZ4p oYZddAiutY9CbrPOnqGSNCqNL/uQlGUsXKoDb+7PRdlXiOvpkejp8MtsC+F7SO4KXWKY 4BSWeTjintuNNcbTel3LFMs584f5aWa8KDji6O4oG+lLxpnEV6OyiNWItf3eEczXLheV MwNDk0ExI3tmDeVNYm9pG3Y59B9UHOLCJPZ/1JYhQ9SbUYavOIUmEiXq4uDDxgvkX0D5 9tDA== X-Gm-Message-State: AOJu0YxHf3NnDLkMY8r7FOsnKtor+cQq2gaebF7Jr09OYGPenjDYb+2t 2km6G22RRufMXhrdhm5zVFb28Q== X-Google-Smtp-Source: AGHT+IE/uT6AAWjY+YJfQ3CQBWsLmndNLh5bKVdNARnSbDWwuz1WOne4sa5zPgTupWDDPbYqNrxoIg== X-Received: by 2002:a5d:4e4b:0:b0:319:7a91:7107 with SMTP id r11-20020a5d4e4b000000b003197a917107mr12172529wrt.48.1692895158833; Thu, 24 Aug 2023 09:39:18 -0700 (PDT) Received: from zen.linaroharston ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id i12-20020adffdcc000000b0031ae8d86af4sm20385986wrs.103.2023.08.24.09.39.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 09:39:17 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 67CBD1FFC5; Thu, 24 Aug 2023 17:39:11 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Beraldo Leal , Nicholas Piggin , Richard Henderson , Markus Armbruster , Yonggang Luo , qemu-ppc@nongnu.org, David Gibson , Daniel Henrique Barboza , qemu-s390x@nongnu.org, Akihiko Odaki , =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-arm@nongnu.org, Juan Quintela , Thomas Huth , Peter Maydell , Ilya Leoshkevich , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , David Hildenbrand , Greg Kurz , Wainer dos Santos Moschetta , =?utf-8?q?Philippe_Mathi?= =?utf-8?q?eu-Daud=C3=A9?= , =?utf-8?q?C=C3=A9dric_Le_Goa?= =?utf-8?q?ter?= Subject: [PATCH v2 10/12] gdbstub: refactor get_feature_xml Date: Thu, 24 Aug 2023 17:39:08 +0100 Message-Id: <20230824163910.1737079-11-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230824163910.1737079-1-alex.bennee@linaro.org> References: <20230824163910.1737079-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::433; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x433.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Try to bring up the code to more modern standards by: - use dynamic GString built xml over a fixed buffer - use autofree to save on explicit g_free() calls - don't hand hack strstr to find the delimiter Signed-off-by: Alex Bennée --- v2 - avoid needless g_strndup for copy of annex --- gdbstub/internals.h | 2 +- gdbstub/gdbstub.c | 63 +++++++++++++++++++++------------------------ 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/gdbstub/internals.h b/gdbstub/internals.h index f2b46cce41..4876ebd74f 100644 --- a/gdbstub/internals.h +++ b/gdbstub/internals.h @@ -33,7 +33,7 @@ typedef struct GDBProcess { uint32_t pid; bool attached; - char target_xml[1024]; + char *target_xml; } GDBProcess; enum RSState { diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 8e9bc17e07..31a2451f27 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -354,54 +354,51 @@ static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) static const char *get_feature_xml(const char *p, const char **newp, GDBProcess *process) { - size_t len; int i; const char *name; CPUState *cpu = gdb_get_first_cpu_in_process(process); CPUClass *cc = CPU_GET_CLASS(cpu); - len = 0; - while (p[len] && p[len] != ':') - len++; - *newp = p + len; + /* ‘qXfer:features:read:annex:offset,length' */ + char *term = g_strstr_len(p, -1, ":"); + size_t len = term - p; + g_autofree char *annex = g_strndup(p, len); + /* leave newp at offset,length for the rest of the params */ + *newp = term + 1; - name = NULL; - if (strncmp(p, "target.xml", len) == 0) { - char *buf = process->target_xml; - const size_t buf_sz = sizeof(process->target_xml); - /* Generate the XML description for this CPU. */ - if (!buf[0]) { + name = NULL; + if (g_strcmp0(annex, "target.xml") == 0) { + if (!process->target_xml) { GDBRegisterState *r; + GString *xml = g_string_new(""); + + g_string_append(xml, + "" + ""); - pstrcat(buf, buf_sz, - "" - "" - ""); if (cc->gdb_arch_name) { - gchar *arch = cc->gdb_arch_name(cpu); - pstrcat(buf, buf_sz, ""); - pstrcat(buf, buf_sz, arch); - pstrcat(buf, buf_sz, ""); - g_free(arch); + g_autofree gchar *arch = cc->gdb_arch_name(cpu); + g_string_append_printf(xml, + "%s", + arch); } - pstrcat(buf, buf_sz, "gdb_core_xml_file); - pstrcat(buf, buf_sz, "\"/>"); + g_string_append(xml, "gdb_core_xml_file); + g_string_append(xml, "\"/>"); for (r = cpu->gdb_regs; r; r = r->next) { - pstrcat(buf, buf_sz, "xml); - pstrcat(buf, buf_sz, "\"/>"); + g_string_append(xml, "xml); + g_string_append(xml, "\"/>"); } - pstrcat(buf, buf_sz, ""); + g_string_append(xml, ""); + + process->target_xml = g_string_free(xml, false); + return process->target_xml; } - return buf; } if (cc->gdb_get_dynamic_xml) { - char *xmlname = g_strndup(p, len); - const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); - - g_free(xmlname); + const char *xml = cc->gdb_get_dynamic_xml(cpu, annex); if (xml) { return xml; } @@ -2245,6 +2242,6 @@ void gdb_create_default_process(GDBState *s) process = &s->processes[s->process_num - 1]; process->pid = pid; process->attached = false; - process->target_xml[0] = '\0'; + process->target_xml = NULL; }