gdb: add core file name to 'info inferiors' output

This commit builds on the previous commit.  In the future I am
proposing to move the core file BFD from the program_space into the
core_target.  In the last commit I updated 'maint info program-spaces'
to remove the core file name from the output.

In this commit I'm adding the core file name to the 'info inferiors'
output.

My proposal is to add the core file as auxiliary information beneath
an inferior's line in the 'info inferiors' output.  We already do
this vfork parent/child information.

The alternative would be to add the core file as an additional column
in the 'info inferiors' output, indeed, I did initially propose this:

  https://inbox.sourceware.org/gdb-patches/e3e040272a0f8f5fd826298331da4c19d01f3a5e.1757615333.git.aburgess@redhat.com

But the problem with this is that the 'info inferiors' output can
easily become very long, and the line wrapping gets very messy, making
the output much harder to parse.  The feedback on this initial
approach wasn't super strong, so I'm trying the auxiliary information
approach to see if this is liked more.

The new output looks like this:

  (gdb) info inferiors
    Num  Description       Connection           Executable
  * 1    process 54313     1 (core)             /tmp/executable
          core file /tmp/core.54313

The only other option I can think of, if this approach is not liked,
would be to add an entirely new command, 'info core-files', with
output like:

    Num  Core File
  * 1    /tmp/corefile.core

The 'Num' column here would just be the inferior number again.  In
effect this new command is just splitting the 'info inferiors' into
two commands.

I extended gdb.base/corefile.exp to check the current output style,
and updated the gdb.multi/multi-target-info-inferiors.exp test to take
the new output into account.

Approved-By: Tom Tromey <tom@tromey.com>
diff --git a/gdb/NEWS b/gdb/NEWS
index ff66adb..aa1105c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -38,6 +38,11 @@
 maintenance info program-spaces
   This command no longer displays the core file name.
 
+info inferiors
+  If an inferior has a core file loaded, then this will be displayed
+  as an additional line under the inferior's table entry in the
+  output.
+
 * Changed remote packets
 
 single-inf-arg in qSupported
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 1e1893f..a955f72 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3367,6 +3367,7 @@
 To find out what inferiors exist at any moment, use @w{@code{info
 inferiors}}:
 
+@anchor{info inferiors command}
 @table @code
 @kindex info inferiors [ @var{id}@dots{} ]
 @item info inferiors
@@ -3390,10 +3391,14 @@
 
 @item
 the name of the executable the inferior is running.
-
 @end enumerate
 
 @noindent
+If an inferior is currently debugging a core file, then an additional
+line will be printed after the line describing the inferior, this
+additional line gives the file name of the core file being debugged.
+
+@noindent
 An asterisk @samp{*} preceding the @value{GDBN} inferior number
 indicates the current inferior.
 
@@ -3405,7 +3410,19 @@
 (@value{GDBP}) info inferiors
   Num  Description       Connection                      Executable
 * 1    process 3401      1 (native)                      goodbye
+  2    process 2307      2 (extended0remote host:10000)  hello
+@end smallexample
+
+Adding an additional inferior that is debugging a core file causes the
+additional core file line to be displayed, for example@:
+
+@smallexample
+(@value{GDBP}) info inferiors
+  Num  Description       Connection                      Executable
+* 1    process 3401      1 (native)                      goodbye
   2    process 2307      2 (extended-remote host:10000)  hello
+  3    process 1578      3 (core)                        broken
+        core file core.1578
 @end smallexample
 
 To get information about the current inferior, use @code{inferior}:
@@ -22070,6 +22087,9 @@
 the program is running.  To do this, use the @code{kill} command
 (@pxref{Kill Process, ,Killing the Child Process}).
 
+To see which core files are loaded into which inferiors, use the
+@kbd{info inferiors} command (@pxref{info inferiors command}).
+
 @kindex add-symbol-file
 @cindex dynamic linking
 @item add-symbol-file @var{filename} @r{[} -readnow @r{|} -readnever @r{]} @r{[} -o @var{offset} @r{]} @r{[} @var{textaddress} @r{]} @r{[} -s @var{section} @var{address} @dots{} @r{]}
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 78ecb16..a387d3c 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -634,6 +634,13 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
 	  uiout->text (_("\n\tis vfork parent of inferior "));
 	  uiout->field_signed ("vfork-child", inf->vfork_child->num);
 	}
+      if (inf->pspace->core_bfd () != nullptr)
+	{
+	  uiout->text (_("\n\tcore file "));
+	  uiout->field_string ("core-file",
+			       bfd_get_filename (inf->pspace->core_bfd ()),
+			       file_name_style.style ());
+	}
 
       uiout->text ("\n");
     }
diff --git a/gdb/testsuite/gdb.base/corefile.exp b/gdb/testsuite/gdb.base/corefile.exp
index ae1e8bf..6e44df84 100644
--- a/gdb/testsuite/gdb.base/corefile.exp
+++ b/gdb/testsuite/gdb.base/corefile.exp
@@ -60,7 +60,7 @@
 	    -re -wrap "Couldn't find .* registers in core file.*" {
 		fail "$gdb_test_name (couldn't find regs)"
 	    }
-	    -re -wrap "Core was generated by `[string_to_regexp $corefile]'\\.\r\n.*\#0  \[^\r\n\]+\(\).*" {
+	    -re -wrap "Core was generated by `[string_to_regexp $binfile]'\\.\r\n.*\#0  \[^\r\n\]+\(\).*" {
 		pass $gdb_test_name
 	    }
 	    -re -wrap "Core was generated by .*\r\n\#0  .*\(\).*" {
@@ -75,6 +75,16 @@
 		fail "$gdb_test_name (core not loaded)"
 	    }
 	}
+
+	# COREFILE will have whitespace quoted ready for use on the command
+	# line.  But for the 'info inferiors' output whitespace is not quoted,
+	# so remote the quoting now.
+	regsub -all "\\\\ " $corefile " " corefile
+	gdb_test "info inferiors" \
+	    [multi_line \
+		 "\\* 1\[^\r\n\]+\\(core\\)\\s+.*[string_to_regexp $binfile]\\s*" \
+		 "\\s+core file [string_to_regexp $corefile]"] \
+	    "$testname, info inferiors"
     }
 }
 
diff --git a/gdb/testsuite/gdb.multi/multi-target-info-inferiors.exp b/gdb/testsuite/gdb.multi/multi-target-info-inferiors.exp
index e03dc60..b9cd212 100644
--- a/gdb/testsuite/gdb.multi/multi-target-info-inferiors.exp
+++ b/gdb/testsuite/gdb.multi/multi-target-info-inferiors.exp
@@ -75,7 +75,7 @@
     }
 
     set ws "\[ \t\]+"
-    global decimal binfile
+    global decimal binfile gcorefile
 
     # Test "info connections" and "info inferior" by switching to each
     # inferior one by one.
@@ -105,15 +105,19 @@
 			]
 	    }
 
+	    set gcorefile_re [string_to_regexp $gcorefile]
+
 	    gdb_test "info inferiors" \
 		[multi_line \
 		     "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
 		     "[inf_desc 1 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
 		     "[inf_desc 2 $inf]2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
 		     "[inf_desc 3 $inf]3 \\(core\\)${ws}${binfile}${ws}" \
+		     "${ws}core file ${gcorefile_re}" \
 		     "[inf_desc 4 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
 		     "[inf_desc 5 $inf]4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
 		     "[inf_desc 6 $inf]5 \\(core\\)${ws}${binfile}${ws}" \
+		     "${ws}core file ${gcorefile_re}"
 		    ]
 
 	    if { $::run_python_tests } {