public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Aktemur, Tankut Baris" <tankut.baris.aktemur@intel.com>
To: Pedro Alves <pedro@palves.net>, Tom Tromey <tromey@adacore.com>,
	"Andrew Burgess" <aburgess@redhat.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al
Date: Tue, 18 Jul 2023 08:48:23 +0000	[thread overview]
Message-ID: <DM4PR11MB73039AD0722418C95F2F4530C438A@DM4PR11MB7303.namprd11.prod.outlook.com> (raw)
In-Reply-To: <586e6a45-988f-6fc9-2bb2-da50f0b53d74@palves.net>

On Monday, July 17, 2023 8:16 PM, Pedro Alves wrote:
> On 2023-07-17 18:12, Tom Tromey via Gdb-patches wrote:
> >>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
> >
> > Andrew> Turns out, 'start' doesn't do anything special.  You call
> > Andrew> scoped_restore_current_inferior_for_memory in order to setup
> > Andrew> inferior_ptid, but at the point 'start' calls 'main_name', inferior_ptid
> > Andrew> is null_ptid, so any memory accesses will fail.
> >
> > Andrew> I suspect the reason this is OK is that the memory read is actually
> > Andrew> hitting the objfile, I did try 'set trust-readonly-sections off', but
> > Andrew> everything still appears to work, I'm not sure why.
> >
> > I think it's because, before the inferior is started, memory accesses
> > reach exec_target::xfer_partial, which calls section_table_xfer_memory_partial.
> >
> > Andrew> Or we could use 'scoped_restore_current_inferior' or
> > Andrew> 'scoped_restore_current_inferior_for_memory' depending on whether
> > Andrew> inferior::pid is 0 or not.
> > ...
> > Andrew> I think for things like 'read_memory' it is fine if we just add a check
> > Andrew> and raise a Python error if the inferior is not yet active, but as I
> > Andrew> argue above, I don't think this is good enough for 'main_name'.
> >
> > I think it's valid to read or search memory before the inferior started.
> > It's even valid to write memory when 'set write on'.
> >
> > So, let me know what you think of the appended, which tries to handle
> > the pid==0 case in all these spots.
> 
> 
> I'm thinking that we should just remove the ptid parameter and the
> any_thread_of_inferior calls completely, and make
> scoped_restore_current_inferior_for_memory switch inferior_ptid to
> a pid ptid.
> 
> I was thinking of suggesting this before, but stopped short because I was a
> little worried that some port might be assuming inferior_ptid points at a
> thread in the xfer_partial memory access routines.  We know that anything
> that supports forks must not assume that, due to how detach_breakpoints works.
> I looked at a number of xfer_partial implementations, and didn't see
> anything that is looking at inferior_ptid in a way that would misbehave.  I'm
> thinking that we could go forward with this and just fix ports if they break.
> 
> While on some ports like on AMD GPU we have things like thread-specific
> address spaces, and so when accessing memory for those address spaces,
> we must have the right thread context (via inferior_ptid) selected, in
> Inferior.read_memory, we only have the inferior, so this means that this API as
> is can't be used to access such address spaces.  IOW, it can only be used
> to access the global address space that is visible to both the
> CPU and the GPUs.  If we end up using scoped_restore_current_inferior_for_memory
> later to set up the context to read memory from a specific thread, then
> we can add an alternative ctor that takes a thread_info pointer, and make
> inferior_ptid point to the thread, for example.
> 
> As for pointing inferior_ptid to a pid-ptid, I don't think it is strange,
> we do it in other places.  Note that gdbserver handles memory accesses
> in a similar "without a thread" way, see:
> 
>     commit 7f8acedeebe295fc8cc1d11ed971cbfc1942618c
>     gdbserver: track current process as well as current thread
> 
> Note how gdb_read_memory uses set_desired_process, and then switch_to_process
> switches current_thread to nullptr.

On a related topic, would you mind commenting on 

  https://sourceware.org/pipermail/gdb-patches/2023-June/200347.html ?

In the downstream debugger of Intel, we see a similar problem.  For a
certain address space, a thread context is needed.  Are you in more favor
of letting the core leave the thread unset and making the targets 
responsible for setting the thread context?

> 
> I ran the testsuite on native and extended-gdbserver (x86_64 GNU/Linux),
> and saw no regressions.
> 
> From a63bdf461caac057fac40aab5533d4372d8e1a3d Mon Sep 17 00:00:00 2001
> From: Pedro Alves <pedro@palves.net>
> Date: Mon, 17 Jul 2023 18:31:02 +0100
> Subject: [PATCH] No ptid
> 
> Change-Id: I11309c5ddbbb51a4594cf63c21b3858bfd9aed19
> ---
>  gdb/inferior.h           | 4 ++--
>  gdb/proc-service.c       | 3 +--
>  gdb/python/py-inferior.c | 8 ++++----
>  3 files changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/gdb/inferior.h b/gdb/inferior.h
> index 98501652a27..94387ff4792 100644
> --- a/gdb/inferior.h
> +++ b/gdb/inferior.h
> @@ -771,12 +771,12 @@ class scoped_restore_current_inferior_for_memory
>    /* Save the current globals and switch to the given inferior and the
>       inferior's program space.  PTID must name a thread in INF, it is
>       used as the new inferior_ptid.  */

Nit: Reference to PTID in the comment should be removed.

Thanks
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

  parent reply	other threads:[~2023-07-18  8:48 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-14 17:06 [PATCH v4 0/6] Fix some Python Inferior methods Tom Tromey
2023-07-14 17:06 ` [PATCH v4 1/6] Minor cleanups in py-inferior.exp Tom Tromey
2023-07-14 17:06 ` [PATCH v4 2/6] Refactor py-inferior.exp Tom Tromey
2023-07-14 17:06 ` [PATCH v4 3/6] Rename Python variable in py-inferior.exp Tom Tromey
2023-07-14 17:06 ` [PATCH v4 4/6] Remove obsolete comment from gdbthread.h Tom Tromey
2023-07-14 17:06 ` [PATCH v4 5/6] Introduce scoped_restore_current_inferior_for_memory Tom Tromey
2023-07-14 17:06 ` [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al Tom Tromey
2023-07-15 13:31   ` Andrew Burgess
2023-07-17 17:12     ` Tom Tromey
2023-07-17 18:15       ` Pedro Alves
2023-07-17 18:44         ` Tom Tromey
2023-07-18 12:20           ` [PATCH] Fix gdb.Inferior.read_memory without execution (PR dap/30644) (was: Re: [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al) Pedro Alves
2023-07-18 17:25             ` [PATCH] Fix gdb.Inferior.read_memory without execution (PR dap/30644) Tom Tromey
2023-07-19 13:14               ` Pedro Alves
2023-07-18  8:48         ` Aktemur, Tankut Baris [this message]
2023-07-18 12:26           ` [PATCH v4 6/6] Use correct inferior in Inferior.read_memory et al Pedro Alves
2023-07-18 13:19             ` Aktemur, Tankut Baris
2023-07-14 18:09 ` [PATCH v4 0/6] Fix some Python Inferior methods Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DM4PR11MB73039AD0722418C95F2F4530C438A@DM4PR11MB7303.namprd11.prod.outlook.com \
    --to=tankut.baris.aktemur@intel.com \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).