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: 716558 Delivered-To: patch@linaro.org Received: by 2002:adf:f747:0:b0:317:ecd7:513f with SMTP id z7csp1444241wrp; Thu, 24 Aug 2023 09:41:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG9RZsZofv891nZ2MVjr8udHWY4jQMl+lsQm/fPiHg4gQhbZ4KJSVd0fe/r5ITHC48t3bGQ X-Received: by 2002:a9d:7d10:0:b0:6bc:b6a6:24f4 with SMTP id v16-20020a9d7d10000000b006bcb6a624f4mr2776945otn.34.1692895313347; Thu, 24 Aug 2023 09:41:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692895313; cv=none; d=google.com; s=arc-20160816; b=axoiphS0FMThkgd1qZFXFgB0TNWgnpQ78yYZr+yN5rsSUKpfK1dj3HyLP7gBu9O01N V4a+nSUOiIgUi1NQEbXD/IzQfrUUmgbsRVk6fPbpq8zR2aocq78kEl9ZwobAxTusj6yb PPW7e0NmESB/fprmQH3KQe9Wk+n1klh1k1VBiYmpUhMIDWzzUgN12ijb09DKJ4xFsEa+ zQ8J9rU8UeNT24/VqSef68wkwegKoEMqXRvgLoDGVn8bAoUUXqIMgZ4DxmfcxgxKeKTV b+ka+zNQB7a0J6SpwRFSUmj5VAWlnI9V7R7oepZRMfFOq6qhbhYU2JG/vzlWEnpHjtjo Wj+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7n08jET4sLnSCRFO+uf5UKlQj+0C0tBz07A/O5rXTPI=; fh=xfIA8VPYm4frMifZMc5Z1SVcvuTb3HMwCNSmJWBjt2E=; b=RXDq/m0sqa22dmbS+5fNE3vJigSY2kRT5DYwTQPe6R6K46rDVChfh2jRNaZaQbNvI3 LYzy1J2hcLywejQEzDdAw+m+63vkeLcMCpK8ILkTMFaP3Cxd2M22xzzljxI6MV0Ii/O5 d55J2dvEn/eHap1/q7MKsfp8eN779ex+Lj8loZ3tSiBBpYzLehw/lpVYJIPWRZioGIsw 5guPhLOJz6a4RBocQN+DZBuDn+zy7v4obeld/+wLmWIhc1bJXOUygq1PAaUnQIj1ERvC n1nyMIK/JBbwhjGze8O5ZxEYt2B2GoDeoR8Ac3hTgTFK1EtlrWNE8MgGBNkp7O5kGkt9 M/cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AULp9ltE; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id o21-20020ac87c55000000b004109625bdf4si5262816qtv.602.2023.08.24.09.41.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 24 Aug 2023 09:41:53 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AULp9ltE; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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-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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.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; }