From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id CvJRBfdS9V9fUwAAWB0awg (envelope-from ) for ; Wed, 06 Jan 2021 01:04:39 -0500 Received: by simark.ca (Postfix, from userid 112) id 05D351F0AA; Wed, 6 Jan 2021 01:04:39 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 953561E99A for ; Wed, 6 Jan 2021 01:04:38 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C76EF3857829; Wed, 6 Jan 2021 06:04:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C76EF3857829 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1609913077; bh=VZGUHTcx5OeGB8a928x9fouuqGaVSymygbBcwlYyufM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=DblPI7Z+6IomX9a5qYt70U+Xv3go1PzFUXc0+p0ot3smwqZZBH/SlcUz1wcNSekeG 3p/2KfrD9blB/NTXSTYTNcSck+nqpchwZ3fzYsPUNJ5mU+armjZb6P1tuagAvZFm6o sZyqG7Q/BHHIVTMxNNcaSI5ZdMDH970rTYF8H9m0= Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) by sourceware.org (Postfix) with ESMTP id 0E7483857C7F for ; Wed, 6 Jan 2021 06:04:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0E7483857C7F Received: from vapier.lan (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 87415335D9F for ; Wed, 6 Jan 2021 06:04:34 +0000 (UTC) To: gdb-patches@sourceware.org Subject: [PATCH] gdb/sim: add support for exporting memory map Date: Wed, 6 Jan 2021 01:04:33 -0500 Message-Id: <20210106060433.12043-1-vapier@gentoo.org> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mike Frysinger via Gdb-patches Reply-To: Mike Frysinger Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" This allows gdb to quickly dump & process the memory map that the sim knows about. This isn't fully accurate, but is largely limited by the gdb memory map format. While the sim supports RWX bits, gdb can only handle RW or RO regions. --- gdb/remote-sim.c | 16 +++++++++++ include/gdb/remote-sim.h | 9 +++++++ sim/common/sim-core.c | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) gdb/: * remote-sim.c: Include memory-map.h. (gdbsim_target): Define memory_map override. (gdbsim_target::memory_map): Define. include/gdb/: * remote-sim.h (sim_memory_map): Define. sim/common/: * sim-core.c (sim_memory_map): Define. diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c index c4f3913edbad..e44add538afb 100644 --- a/gdb/remote-sim.c +++ b/gdb/remote-sim.c @@ -42,6 +42,7 @@ #include "readline/readline.h" #include "gdbthread.h" #include "gdbsupport/byte-vector.h" +#include "memory-map.h" /* Prototypes */ @@ -164,6 +165,7 @@ struct gdbsim_target final bool has_all_memory () override; bool has_memory () override; + std::vector memory_map () override; private: sim_inferior_data *get_inferior_data_by_ptid (ptid_t ptid, @@ -1270,6 +1272,20 @@ gdbsim_target::has_memory () return true; } +std::vector +gdbsim_target::memory_map () +{ + struct sim_inferior_data *sim_data + = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); + std::vector result; + gdb::unique_xmalloc_ptr text(sim_memory_map (sim_data->gdbsim_desc)); + + if (text) + result = parse_memory_map (text.get ()); + + return result; +} + void _initialize_remote_sim (); void _initialize_remote_sim () diff --git a/include/gdb/remote-sim.h b/include/gdb/remote-sim.h index 73fb670c17e5..a3ba3aa36cd2 100644 --- a/include/gdb/remote-sim.h +++ b/include/gdb/remote-sim.h @@ -213,6 +213,15 @@ int sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length); void sim_info (SIM_DESC sd, int verbose); +/* Return a memory map in XML format. + + The caller must free the returned string. + + For details on the format, see GDB's Memory Map Format documentation. */ + +char *sim_memory_map (SIM_DESC sd); + + /* Run (or resume) the simulated program. STEP, when non-zero indicates that only a single simulator cycle diff --git a/sim/common/sim-core.c b/sim/common/sim-core.c index 74369aa99fe2..eb40d2588f58 100644 --- a/sim/common/sim-core.c +++ b/sim/common/sim-core.c @@ -452,6 +452,64 @@ sim_core_translate (sim_core_mapping *mapping, } +#if EXTERN_SIM_CORE_P +char * +sim_memory_map (SIM_DESC sd) +{ + sim_core *core = STATE_CORE (sd); + unsigned map; + char *s1, *s2, *entry; + + s1 = xstrdup ( + "\n" + "\n" + "\n"); + + for (map = 0; + map < nr_maps; + map++) + { + sim_core_mapping *mapping = core->common.map[map].first; + + while (mapping != NULL) + { + if (mapping->level != 0) + goto next; + + entry = xasprintf ("\n", + mapping->base, mapping->nr_bytes); + /* The sim memory map is organized by access, not by addresses. + So a RWX memory map will have three independent mappings. + GDB's format cannot support overlapping regions, so we have + to filter those out. + + Further, GDB can only handle RX ("rom") or RWX ("ram") mappings. + We just emit "ram" everywhere to keep it simple. If GDB ever + gains support for more stuff, we can expand this. + + Using strstr is kind of hacky, but as long as the map is not huge + (we're talking <10K), should be fine. */ + if (strstr (s1, entry) == NULL) + { + s2 = concat (s1, entry, NULL); + free (s1); + s1 = s2; + } + free (entry); + + next: + mapping = mapping->next; + } + } + + s2 = concat (s1, "", NULL); + free (s1); + return s2; +} +#endif + + #if EXTERN_SIM_CORE_P unsigned sim_core_read_buffer (SIM_DESC sd, -- 2.28.0