Re: [PATCH 1/2] gdb: use inferior::threads instead of all_threads and a PID filter

Tom de Vries <[email protected]>
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
On 8/6/26 10:54 PM, Andrew Burgess wrote:
> I noticed a few places where we iterated over `all_threads ()` but
> then immediately filtered by PID.  It would be better to instead
> iterate over `inferior::threads ()` and remove the PID filtering.
> 
> These should be equivalent as a single inferior should contain every
> thread with a given PID, and should only contain threads with that
> given PID.
> 
> While editing the `for (...)` loops I have replaced 'auto' with
> 'thread_info' for additional type clarity.
> 
> There should be no user visible changes after this commit.

Hi Andrew,

this LGTM.

FWIW, I had a Claude Code session review this, and I asked it to find 
other locations in need of the same treatment, and it found 
sync_threadlists in gdb/aix-thread.c, where we can replace 
all_threads_safe with inferior::threads_safe ().  But that requires 
testing on AIX, so I suppose it's not something that is easily added to 
this patch.

Approved-By: Tom de Vries <[email protected]>

Thanks,
- Tom

> ---
>   gdb/mi/mi-main.c | 18 +++++++-----------
>   1 file changed, 7 insertions(+), 11 deletions(-)
> 
> diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
> index 8b6da41ffeb..903c6a5f411 100644
> --- a/gdb/mi/mi-main.c
> +++ b/gdb/mi/mi-main.c
> @@ -359,14 +359,11 @@ mi_cmd_exec_interrupt (const char *command, const char *const *argv, int argc)
>         scoped_disable_commit_resumed disable_commit_resumed
>   	("interrupting all threads of thread group");
>   
> -      for (auto &thread : all_threads ())
> +      for (thread_info &thread : inf->threads ())
>   	{
>   	  if (thread.state () != THREAD_RUNNING)
>   	    continue;
>   
> -	  if (thread.ptid.pid () != inf->pid)
> -	    continue;
> -
>   	  target_stop (thread.ptid);
>   	}
>       }
> @@ -603,14 +600,13 @@ print_one_inferior (struct inferior *inferior, bool recurse,
>   
>         if (inferior->pid != 0)
>   	{
> -	  for (auto &ti : all_threads ())
> -	    if (ti.ptid.pid () == inferior->pid)
> -	      {
> -		int core = target_core_of_thread (ti.ptid);
> +	  for (thread_info &ti : inferior->threads ())
> +	    {
> +	      int core = target_core_of_thread (ti.ptid);
>   
> -		if (core != -1)
> -		  cores.insert (core);
> -	      }
> +	      if (core != -1)
> +		cores.insert (core);
> +	    }
>   	}
>   
>         if (!cores.empty ())
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.