Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
Cc: gdb-patches@sourceware.org, Luis Machado <luis.machado@amd.com>,
	Luis Machado <luis.machado.foss@gmail.com>,
	Andrew Burgess <aburgess@redhat.com>,
	Yury Khrustalev <yury.khrustalev@arm.com>,
	Pedro Alves <pedro@palves.net>, Tom Tromey <tom@tromey.com>
Subject: Re: [PATCH v1 02/10] gdb: rely on the first alive thread TPID when reading Linux procfs files
Date: Mon, 13 Jul 2026 10:11:43 +0100	[thread overview]
Message-ID: <c13c9a69-dcac-4f9c-adfa-d8405bcc1c67@arm.com> (raw)
In-Reply-To: <87o6gg1xrq.fsf@linaro.org>

On 09/07/2026 07:29, Thiago Jung Bauermann wrote:
> Matthieu Longo <matthieu.longo@arm.com> writes:
> 
>> On Linux, /proc/<pid> is keyed by the thread-group leader PID. When
>> the leader has exited, some /proc/<pid>/... entries become unavailable
>> even though another thread is still alive. This can happen, for instance,
>> when the main thread calls pthread_exit() and another thread continues
>> the execution (existing test: gcore-stale-thread).
>>
>> This causes GDB to fail to read procfs entries such as cmdline, cwd,
>> exe, maps, and smaps when it uses 'current_inferior ()->pid' after the
>> thread-group leader has exited.
>>
>> Fix this by adding inferior::first_alive_thread(), which returns the
>> PTID of the first non-exited thread of the inferior. Use its LWP ID
>> when accessing procfs entries that only need a representative live LWP
>> belonging to the process.
>>
>> Update linux_info_proc, linux_process_address_in_memtag_page, and
>> linux_find_memory_regions_full to use this live-thread LWP ID instead
>> of the inferior PID when constructing procfs paths.
>>
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31207
>> ---
>>  gdb/inferior.c   | 12 ++++++++++++
>>  gdb/inferior.h   |  9 +++++++++
>>  gdb/linux-tdep.c | 38 ++++++++++++++++++--------------------
>>  3 files changed, 39 insertions(+), 20 deletions(-)
> 
> It's a pity that the patch originally proposed to solve this bug:
> 
> https://inbox.sourceware.org/gdb-patches/20250412201140.31510-1-dominik.b.czarnota@gmail.com/
> 
> was approved but never committed. But your version is more
> comprehensive, since it also makes the change for other procfs files.
> It also explicitly looks for a live thread.
> 
> I have a couple of comments below, but regardless:
> 
> Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
> 
>> diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
>> index 155d6e874d9..b89f4ee0717 100644
>> --- a/gdb/linux-tdep.c
>> +++ b/gdb/linux-tdep.c
>> @@ -842,9 +842,7 @@ static void
>>  linux_info_proc (struct gdbarch *gdbarch, const char *args,
>>  		 enum info_proc_what what)
>>  {
>> -  /* A long is used for pid instead of an int to avoid a loss of precision
>> -     compiler warning from the output of strtoul.  */
>> -  long pid;
>> +  ptid_t ptid;
>>    int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
>>    int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
>>    int environ_f = (what == IP_ENVIRON || what == IP_ALL);
>> @@ -859,7 +857,8 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
>>      {
>>        char *tem;
>>  
>> -      pid = strtoul (args, &tem, 10);
>> +      auto pid = strtoul (args, &tem, 10);
> 
> The actual type of pid here will be long, right? Unless there's a an
> advantage to using auto, I think in this case the code is clearer if the
> type is explicitly mentioned.
> 

Fixed.

>> +      ptid = ptid_t (pid, pid);
>>        args = tem;
>>      }
>>    else
>> @@ -869,17 +868,17 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
>>        if (current_inferior ()->fake_pid_p)
>>  	error (_("Can't determine the current process's PID: you must name one."));
>>  
>> -      pid = current_inferior ()->pid;
>> +      ptid = current_inferior ()->first_alive_thread ();
>>      }
>>  
>>    args = skip_spaces (args);
>>    if (args && args[0])
>>      error (_("Too many parameters: %s"), args);
>>  
>> -  gdb_printf (_("process %ld\n"), pid);
>> +  gdb_printf (_("process %d\n"), ptid.pid ());
>>    if (cmdline_f)
>>      {
>> -      xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
>> +      xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", ptid.lwp ());
>>        gdb_byte *buffer;
>>        LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer);
>>  
>> @@ -901,7 +900,7 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
>>      }
>>    if (cwd_f)
>>      {
>> -      xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
>> +      xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", ptid.lwp ());
> 
> proc_pid_cwd(5) says:
> 
>   In a multithreaded process, the contents of this symbolic
>   link are not available if the main thread has already
>   terminated (typically by calling pthread_exit(3)).
> 
> I think it's ok to leave the code as is for consistency, but it's worth
> mentioning this detail in a comment.
> 

The manual seems incomplete for others threads will still have those files available until they
exit. I could not find a written proof in the manual or
https://docs.kernel.org/filesystems/proc.html, but the files are still valid in the non-exited threads.

See also the reply from Simon about this.

>>        std::optional<std::string> contents
>>  	= target_fileio_readlink (NULL, filename, &target_errno);
>>        if (contents.has_value ())
>> @@ -911,7 +910,7 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
>>      }
>>    if (environ_f)
>>      {
>> -      xsnprintf (filename, sizeof filename, "/proc/%ld/environ", pid);
>> +      xsnprintf (filename, sizeof filename, "/proc/%ld/environ", ptid.lwp ());
>>        gdb_byte *buffer;
>>        LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer);
>>  
>> @@ -935,7 +934,7 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
>>      }
>>    if (exe_f)
>>      {
>> -      xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
>> +      xsnprintf (filename, sizeof filename, "/proc/%ld/exe", ptid.lwp ());
> 
> Same comment here. proc_pid_exe(5) has the same observation.
> 

See answer above.

>>        std::optional<std::string> contents
>>  	= target_fileio_readlink (NULL, filename, &target_errno);
>>        if (contents.has_value ())
> 

Matthieu

  reply	other threads:[~2026-07-13  9:13 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 15:48 [PATCH v1 00/10] gdb: bugfix 31207 and various refactoring in linux-tdep Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 01/10] gdb/linux-tdep: change linux_fill_prpsinfo to return bool Matthieu Longo
2026-07-09  6:26   ` Thiago Jung Bauermann
2026-07-09 12:23     ` Simon Marchi
2026-07-27 14:38       ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 02/10] gdb: rely on the first alive thread TPID when reading Linux procfs files Matthieu Longo
2026-07-09  6:29   ` Thiago Jung Bauermann
2026-07-13  9:11     ` Matthieu Longo [this message]
2026-07-09 14:24   ` Simon Marchi
2026-07-14 15:47     ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 03/10] target_fileio_read_stralloc: add an optional length parameter Matthieu Longo
2026-07-09  6:30   ` Thiago Jung Bauermann
2026-07-13 15:26     ` Matthieu Longo
2026-07-24  2:50       ` Thiago Jung Bauermann
2026-07-27 14:52         ` Matthieu Longo
2026-07-29  1:57           ` Thiago Jung Bauermann
2026-07-21 21:28   ` Luis
2026-07-27 14:48     ` Matthieu Longo
2026-07-27 14:53     ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 04/10] gdb support: add gdb::replace algorithm for iterators and ranges Matthieu Longo
2026-07-09  6:30   ` Thiago Jung Bauermann
2026-07-10 21:21   ` Kevin Buettner
2026-07-13 15:51     ` Matthieu Longo
2026-07-21 21:33   ` Luis
2026-07-27 14:58     ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 05/10] gdb: introduce helper class file_reader_t Matthieu Longo
2026-07-09  6:33   ` Thiago Jung Bauermann
2026-07-13 17:17     ` Matthieu Longo
2026-07-10 21:43   ` Kevin Buettner
2026-07-13 16:31     ` Matthieu Longo
2026-07-13 15:50   ` Schimpe, Christina
2026-07-13 17:12     ` Matthieu Longo
2026-07-22 16:36       ` Joos, Christina
2026-07-27 15:13         ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 06/10] gdb/linux-tdep: migrate linux_info_proc to file_reader_t Matthieu Longo
2026-07-09  6:35   ` Thiago Jung Bauermann
2026-07-13 17:20     ` Matthieu Longo
2026-07-21 21:43   ` Luis
2026-07-27 17:11     ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 07/10] gdb/linux-tdep: migrate linux_find_memory_regions_full " Matthieu Longo
2026-07-09  6:36   ` Thiago Jung Bauermann
2026-07-14  8:40     ` Matthieu Longo
2026-07-24  2:51       ` Thiago Jung Bauermann
2026-07-21 21:47   ` Luis
2026-07-27 17:14     ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 08/10] gdb/linux-tdep: migrate parse_smaps_data " Matthieu Longo
2026-07-09  6:41   ` Thiago Jung Bauermann
2026-07-14  8:49     ` Matthieu Longo
2026-07-24  2:52       ` Thiago Jung Bauermann
2026-07-21 21:49   ` Luis
2026-07-27 17:17     ` Matthieu Longo
2026-07-07 15:48 ` [PATCH v1 09/10] gdb/linux-tdep: parse ProtectionKey in /proc/PID/smaps Matthieu Longo
2026-07-09  6:42   ` Thiago Jung Bauermann
2026-07-14  9:12     ` Matthieu Longo
2026-07-14  9:29       ` Matthieu Longo
2026-07-24  2:58         ` Thiago Jung Bauermann
2026-07-24 10:27           ` Yury Khrustalev
2026-07-25  6:40             ` Thiago Jung Bauermann
2026-07-27  8:22               ` Yury Khrustalev
2026-07-29  1:10                 ` Thiago Jung Bauermann
2026-07-21 21:57   ` Luis
2026-07-27 17:54     ` Matthieu Longo
2026-07-07 15:49 ` [PATCH v1 10/10] gdb/linux: add helpers to read AT_HWCAP3 and AT_HWCAP4 Matthieu Longo
2026-07-09  6:44   ` Thiago Jung Bauermann
2026-07-21 21:58     ` Luis
2026-07-27 17:32       ` Matthieu Longo

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=c13c9a69-dcac-4f9c-adfa-d8405bcc1c67@arm.com \
    --to=matthieu.longo@arm.com \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=luis.machado.foss@gmail.com \
    --cc=luis.machado@amd.com \
    --cc=pedro@palves.net \
    --cc=thiago.bauermann@linaro.org \
    --cc=tom@tromey.com \
    --cc=yury.khrustalev@arm.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