* [PATCH] gdb: move bfd_open_from_target_memory to gdb_bfd
@ 2020-12-08 9:34 Mihails Strasuns via Gdb-patches
2020-12-09 21:31 ` Tom Tromey
0 siblings, 1 reply; 3+ messages in thread
From: Mihails Strasuns via Gdb-patches @ 2020-12-08 9:34 UTC (permalink / raw)
To: gdb-patches
This function allows to create a BFD handle using an accessible memory
range in a target memory. It is currently contained in a JIT module but
this functionality may be of wider usefullness - for example, reading
ELF binaries contained within a core dump.
gdb/ChangeLog:
2020-12-07 Mihails Strasuns <miihails.strasuns@intel.com>
* jit.c (mem_bfd*, bfd_open_from_target_memory): Removed.
* gdb_bfd.h (gdb_bfd_open_from_target_memory): New function.
* gdb_bfd.c (mem_bfd*, gdb_bfd_open_from_target_memory): New functions.
---
gdb/gdb_bfd.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++
gdb/gdb_bfd.h | 7 +++++
gdb/jit.c | 85 ++-------------------------------------------------
3 files changed, 94 insertions(+), 83 deletions(-)
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 58d177cf7df..28dedca2530 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -207,6 +207,91 @@ gdb_bfd_has_target_filename (struct bfd *abfd)
return is_target_filename (bfd_get_filename (abfd));
}
+/* For `gdb_bfd_open_from_target_memory`. */
+
+struct target_buffer
+{
+ CORE_ADDR base;
+ ULONGEST size;
+};
+
+/* For `gdb_bfd_open_from_target_memory`. Opening the file is a no-op. */
+
+static void *
+mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
+{
+ return open_closure;
+}
+
+/* For `gdb_bfd_open_from_target_memory`. Closing the file is just freeing the
+ base/size pair on our side. */
+
+static int
+mem_bfd_iovec_close (struct bfd *abfd, void *stream)
+{
+ xfree (stream);
+
+ /* Zero means success. */
+ return 0;
+}
+
+/* For `gdb_bfd_open_from_target_memory`. For reading the file, we just need to
+ pass through to target_read_memory and fix up the arguments and return
+ values. */
+
+static file_ptr
+mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
+ file_ptr nbytes, file_ptr offset)
+{
+ int err;
+ struct target_buffer *buffer = (struct target_buffer *) stream;
+
+ /* If this read will read all of the file, limit it to just the rest. */
+ if (offset + nbytes > buffer->size)
+ nbytes = buffer->size - offset;
+
+ /* If there are no more bytes left, we've reached EOF. */
+ if (nbytes == 0)
+ return 0;
+
+ err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
+ if (err)
+ return -1;
+
+ return nbytes;
+}
+
+/* For `gdb_bfd_open_from_target_memory`. For statting the file, we only
+ support the st_size attribute. */
+
+static int
+mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
+{
+ struct target_buffer *buffer = (struct target_buffer*) stream;
+
+ memset (sb, 0, sizeof (struct stat));
+ sb->st_size = buffer->size;
+ return 0;
+}
+
+/* See gdb_bfd.h. */
+
+gdb_bfd_ref_ptr
+gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
+ const char *target,
+ const char *filename)
+{
+ struct target_buffer *buffer = XNEW (struct target_buffer);
+
+ buffer->base = addr;
+ buffer->size = size;
+ return gdb_bfd_openr_iovec (filename ? filename : "<in-memory>", target,
+ mem_bfd_iovec_open,
+ buffer,
+ mem_bfd_iovec_pread,
+ mem_bfd_iovec_close,
+ mem_bfd_iovec_stat);
+}
/* Return the system error number corresponding to ERRNUM. */
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index 0f450567d50..e622cd88257 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -194,6 +194,13 @@ int gdb_bfd_requires_relocations (bfd *abfd);
bool gdb_bfd_get_full_section_contents (bfd *abfd, asection *section,
gdb::byte_vector *contents);
+/* Create and initialize a BFD handle from a target in-memory range. */
+
+gdb_bfd_ref_ptr
+gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
+ const char *target,
+ const char *filename = nullptr);
+
/* Range adapter for a BFD's sections.
To be used as:
diff --git a/gdb/jit.c b/gdb/jit.c
index b373947a7f9..28594e36fc4 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -73,86 +73,6 @@ show_jit_debug (struct ui_file *file, int from_tty,
fprintf_filtered (file, _("JIT debugging is %s.\n"), value);
}
-struct target_buffer
-{
- CORE_ADDR base;
- ULONGEST size;
-};
-
-/* Opening the file is a no-op. */
-
-static void *
-mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
-{
- return open_closure;
-}
-
-/* Closing the file is just freeing the base/size pair on our side. */
-
-static int
-mem_bfd_iovec_close (struct bfd *abfd, void *stream)
-{
- xfree (stream);
-
- /* Zero means success. */
- return 0;
-}
-
-/* For reading the file, we just need to pass through to target_read_memory and
- fix up the arguments and return values. */
-
-static file_ptr
-mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
- file_ptr nbytes, file_ptr offset)
-{
- int err;
- struct target_buffer *buffer = (struct target_buffer *) stream;
-
- /* If this read will read all of the file, limit it to just the rest. */
- if (offset + nbytes > buffer->size)
- nbytes = buffer->size - offset;
-
- /* If there are no more bytes left, we've reached EOF. */
- if (nbytes == 0)
- return 0;
-
- err = target_read_memory (buffer->base + offset, (gdb_byte *) buf, nbytes);
- if (err)
- return -1;
-
- return nbytes;
-}
-
-/* For statting the file, we only support the st_size attribute. */
-
-static int
-mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
-{
- struct target_buffer *buffer = (struct target_buffer*) stream;
-
- memset (sb, 0, sizeof (struct stat));
- sb->st_size = buffer->size;
- return 0;
-}
-
-/* Open a BFD from the target's memory. */
-
-static gdb_bfd_ref_ptr
-bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
- const char *target)
-{
- struct target_buffer *buffer = XNEW (struct target_buffer);
-
- buffer->base = addr;
- buffer->size = size;
- return gdb_bfd_openr_iovec ("<in-memory>", target,
- mem_bfd_iovec_open,
- buffer,
- mem_bfd_iovec_pread,
- mem_bfd_iovec_close,
- mem_bfd_iovec_stat);
-}
-
struct jit_reader
{
jit_reader (struct gdb_reader_funcs *f, gdb_dlhandle_up &&h)
@@ -777,9 +697,8 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
paddress (gdbarch, code_entry->symfile_addr),
pulongest (code_entry->symfile_size));
- gdb_bfd_ref_ptr nbfd (bfd_open_from_target_memory (code_entry->symfile_addr,
- code_entry->symfile_size,
- gnutarget));
+ gdb_bfd_ref_ptr nbfd (gdb_bfd_open_from_target_memory (
+ code_entry->symfile_addr, code_entry->symfile_size, gnutarget));
if (nbfd == NULL)
{
puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
--
2.17.1
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] gdb: move bfd_open_from_target_memory to gdb_bfd
2020-12-08 9:34 [PATCH] gdb: move bfd_open_from_target_memory to gdb_bfd Mihails Strasuns via Gdb-patches
@ 2020-12-09 21:31 ` Tom Tromey
2020-12-10 10:17 ` Strasuns, Mihails via Gdb-patches
0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2020-12-09 21:31 UTC (permalink / raw)
To: Mihails Strasuns via Gdb-patches
>>>>> "Mihails" == Mihails Strasuns via Gdb-patches <gdb-patches@sourceware.org> writes:
Mihails> This function allows to create a BFD handle using an accessible memory
Mihails> range in a target memory. It is currently contained in a JIT module but
Mihails> this functionality may be of wider usefullness - for example, reading
Mihails> ELF binaries contained within a core dump.
Mihails> gdb/ChangeLog:
Mihails> 2020-12-07 Mihails Strasuns <miihails.strasuns@intel.com>
Mihails> * jit.c (mem_bfd*, bfd_open_from_target_memory): Removed.
Mihails> * gdb_bfd.h (gdb_bfd_open_from_target_memory): New function.
Mihails> * gdb_bfd.c (mem_bfd*, gdb_bfd_open_from_target_memory): New functions.
Hi. Thank you for the patch.
It looks good to me. I found a couple of minor nits, but the patch is
OK with these fixed.
Mihails> +/* Create and initialize a BFD handle from a target in-memory range. */
Mihails> +
Mihails> +gdb_bfd_ref_ptr
Mihails> +gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
Mihails> + const char *target,
Mihails> + const char *filename = nullptr);
In the style style, the return type isn't separated from the function
name in a declaration.
Mihails> + gdb_bfd_ref_ptr nbfd (gdb_bfd_open_from_target_memory (
Mihails> + code_entry->symfile_addr, code_entry->symfile_size, gnutarget));
The formatting here looks weird. Normally breaks would occur before "(".
Tom
^ permalink raw reply [flat|nested] 3+ messages in thread* RE: [PATCH] gdb: move bfd_open_from_target_memory to gdb_bfd
2020-12-09 21:31 ` Tom Tromey
@ 2020-12-10 10:17 ` Strasuns, Mihails via Gdb-patches
0 siblings, 0 replies; 3+ messages in thread
From: Strasuns, Mihails via Gdb-patches @ 2020-12-10 10:17 UTC (permalink / raw)
To: Tom Tromey, Mihails Strasuns via Gdb-patches
Amended and pushed as 15cc148fb8.
> -----Original Message-----
> From: Tom Tromey <tom@tromey.com>
> Sent: Wednesday, December 9, 2020 10:31 PM
> To: Mihails Strasuns via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Strasuns, Mihails <mihails.strasuns@intel.com>
> Subject: Re: [PATCH] gdb: move bfd_open_from_target_memory to
> gdb_bfd
>
> >>>>> "Mihails" == Mihails Strasuns via Gdb-patches <gdb-
> patches@sourceware.org> writes:
>
> Mihails> This function allows to create a BFD handle using an accessible
> Mihails> memory range in a target memory. It is currently contained in
> Mihails> a JIT module but this functionality may be of wider usefullness
> Mihails> - for example, reading ELF binaries contained within a core dump.
>
> Mihails> gdb/ChangeLog:
> Mihails> 2020-12-07 Mihails Strasuns <miihails.strasuns@intel.com>
>
> Mihails> * jit.c (mem_bfd*, bfd_open_from_target_memory):
> Removed.
> Mihails> * gdb_bfd.h (gdb_bfd_open_from_target_memory): New
> function.
> Mihails> * gdb_bfd.c (mem_bfd*,
> gdb_bfd_open_from_target_memory): New functions.
>
> Hi. Thank you for the patch.
> It looks good to me. I found a couple of minor nits, but the patch is OK with
> these fixed.
>
> Mihails> +/* Create and initialize a BFD handle from a target in-memory
> Mihails> +range. */
> Mihails> +
> Mihails> +gdb_bfd_ref_ptr
> Mihails> +gdb_bfd_open_from_target_memory (CORE_ADDR addr,
> ULONGEST size,
> Mihails> + const char *target,
> Mihails> + const char *filename = nullptr);
>
> In the style style, the return type isn't separated from the function name in a
> declaration.
>
> Mihails> + gdb_bfd_ref_ptr nbfd (gdb_bfd_open_from_target_memory (
> Mihails> + code_entry->symfile_addr, code_entry->symfile_size,
> Mihails> + gnutarget));
>
> The formatting here looks weird. Normally breaks would occur before "(".
>
> Tom
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-12-10 10:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-08 9:34 [PATCH] gdb: move bfd_open_from_target_memory to gdb_bfd Mihails Strasuns via Gdb-patches
2020-12-09 21:31 ` Tom Tromey
2020-12-10 10:17 ` Strasuns, Mihails via Gdb-patches
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox