Re: GDB 15/16 crashing in add_thread_silent()

Simon Marchi via Gdb <[email protected]> Fri, 14 Nov 2025 14:25:37 -0500
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>

On 2025-11-14 14:20, Paul Smith via Gdb wrote:
> I investigated more and the problem is that GDB cannot determine the
> PID of the core file.  I checked the inferiors list and it looks
> correct, but the ptid value passed into add_thread_silent() has a bad
> PID:
> 
> (gdb) fr 8
> #8  0x00000000013817cd in add_thread_silent (targ=0x3ed1ae0, ptid=...)
>     at gdb/thread.c:311
> warning: 311    gdb/thread.c: No such file or directory
> (gdb) p ptid
> $6 = {
>   m_pid = 1,
>   m_lwp = 0,
>   m_tid = 0
> }
> 
> This is apparently because we entered this code in corelow.c:
> 
>    if (inferior_ptid == null_ptid)
>      {
>        /* Either we found no .reg/NN section, and hence we have a
>           non-threaded core (single-threaded, from gdb's perspective),
>           or for some reason add_to_thread_list couldn't determine
>           which was the "main" thread.  The latter case shouldn't
>           usually happen, but we're dealing with input here, which can
>           always be broken in different ways.  */
>        thread_info *thread = first_thread_of_inferior (inf);
> 
>        if (thread == NULL)
>          thread = add_thread_silent (target, ptid_t (CORELOW_PID));
> 
>        switch_to_thread (thread);
>      }
> 
> It appears that add_thread_silent() doesn't work properly with
> CORELOW_PID (1).
> 
> Just to note, my program is decidedly NOT single-threaded; there are
> 20+ active threads in it.
> 
> I see that this (still in corelow.c:core_target_open()) returns the
> correct PID:
> 
>    int pid = bfd_core_file_pid (current_program_space->core_bfd ());
> 
> so I think it's a bug that when we invoke add_thread_silent() above we
> use CORELOW_PID instead of just pid.

If your core is threaded, you shouldn't get to that fallback "if" at
all.  This is where GDB should add all your threads:

  /* Build up thread list from BFD sections, and possibly set the
     current thread to the .reg/NN section matching the .reg
     section.  */
  asection *reg_sect
    = bfd_get_section_by_name (current_program_space->core_bfd (), ".reg");
  for (asection *sect : gdb_bfd_sections (current_program_space->core_bfd ()))
    add_to_thread_list (sect, reg_sect, inf);

If this doesn't add any threads, then you need to dig to understand why
BFD doesn't create the .reg pseudo sections.

Simon