New entry points for writing Linux NT_PRPSINFO notes.

GDB wants to be able to write more complete NT_PRPSINFO Linux notes in
core files.  Currently only a few fields are filled in -- fname,
psargs.  The current interface, elfcore_write_note is va_args based.
This gets ugly and hard to manage when you expand from those 2 args to
many more.  Another point we'd like to address is that core generation
should be host independent, and not depend on the default configured
target.  IOW, with GDB on host X, connected to a GDBserver running on
host Y, we want the gcore command to generate correct cores for Y, not
X.  This should be true even if neither X nor Y are the default target
for that bfd/GDB build (e.g., a --target=Z --enable-targets=all
build).

One complication is that different kernels/core ABIs have different
prpsinfo_t (and prstatus_t, etc.) structures.  Since almost duplicate
bfd target vectors for an arch is not desirable (say, one bare metal,
one Solaris, one GNU/Linux, all for the same arch), one solution would
be to make each write_core_note hooks themselves know for which OS
they are generating the core for.  To know which OS it is, I could
picture things like looking at elf_elfheader (abfd)->e_ident[EI_OSABI]
and other possible identifying bits in the bfd.  I think that'd get
ugly fast, introduces core generation ordering issues, and, well, GDB
already has the necessary info handy (for core reading, GDB does look
at EI_OSABI and other identifying bits in its osabi "sniff"ing
machinery, but also queries the running target, and other non-bfd
bits).

Since GDB has the needed info already, I believe the simplest solution
to all this, is the one below.

Add a new "struct elf_internal_linux_prpsinfo" structure that is the
internal representation of Linux prpsinfo.  The Linux-speficic (but
host-independent) bits of GDB that drive bfd into generating core
notes will instanciate an object of this type, fill it in, and then
call a bfd function (separate from the bfd vector) that swaps the
object to the proper external representation appropriate for the Linux
arch the core is being generated for, and then writes the note.

Most Linux ports have the same prpsinfo_t type (layed out in memory as
per their ABIs, naturally).  Be default, for Linux, GDB will call
these two new functions:

/* Linux/most 32-bit archs.  */
extern char *elfcore_write_linux_prpsinfo32
  (bfd *, char *, int *, const struct elf_internal_linux_prpsinfo *);

/* Linux/most 64-bit archs.  */
extern char *elfcore_write_linux_prpsinfo64
  (bfd *, char *, int *, const struct elf_internal_linux_prpsinfo *);

GDB will select which depending on the target's pointer width.

PowerPC 32-bit however has a slightly different structure, so it gets
its own entry point:

/* Linux/PPC32 uses different layout compared to most archs.  */
extern char *elfcore_write_ppc_linux_prpsinfo32
  (bfd *, char *, int *, const struct elf_internal_linux_prpsinfo *);

Tested on x86_64 Fedora 17 by building GDB with --enable-targets=all,
in both 64-bit, and -m32 modes, and by running GDBs testsuite with
each build.  No regressions.

I haven't audited all Linux ports to see if like PPC their prpsinfo_t
types are not like the others'.

bfd/
2013-01-31  Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* Makefile.in (SOURCE_HFILES): Add `elf-linux-psinfo.h'.
	* elf-bfd.h (elf_internal_linux_prpsinfo): New structure
	declaration.
	(elfcore_write_linux_prpsinfo32, elfcore_write_linux_prpsinfo64)
	(elfcore_write_ppc32_linux_prpsinfo32): New declarations.
	* elf-linux-psinfo.h: New file.
	* elf.c: Include elf-linux-psinfo.h.
	(elfcore_write_linux_prpsinfo32, elfcore_write_linux_prpsinfo64):
	New functions.
	* elf32-ppc.c: Include `elf-linux-psinfo.h'.
	(elf_external_ppc_linux_prpsinfo32): New structure declaration.
	(PPC_LINUX_PRPSINFO32_SWAP_FIELDS): New macro.
	(elfcore_write_ppc_linux_prpsinfo32): New function.
Put more info in NT_PRPSINFO Linux notes

Currently only a few fields of prpsinfo_t on Linux (and elsewhere too)
are filled in when generating NT_PRPSINFO notes on core files --
namely, fname, psargs.  E.g., ppid, pgrg and sid are missing.  We
could do better.  The previous bfd patch added a new interface that
allows passing in the full prpsinfo_t struct to bfd.  This patch makes
GDB use it.

Most Linux kernel ports have the same prpsinfo_t type (layed out in
memory as per their ABIs, naturally).  Be default, for Linux, we'll
call these two new functions:

/* Linux/most 32-bit archs.  */
extern char *elfcore_write_linux_prpsinfo32
  (bfd *, char *, int *, const struct elf_internal_linux_prpsinfo *);

/* Linux/most 64-bit archs.  */
extern char *elfcore_write_linux_prpsinfo64
  (bfd *, char *, int *, const struct elf_internal_linux_prpsinfo *);

GDB will select which depending on the target's pointer width.

PowerPC 32-bit however has a slightly different structure, so it gets
its own entry point:

/* Linux/PPC32 uses different layout compared to most archs.  */
extern char *elfcore_write_ppc_linux_prpsinfo32
  (bfd *, char *, int *, const struct elf_internal_linux_prpsinfo *);

We add add a new gdbarch hook that PPC taps into, to override the
default bfd elfcore hook selection.

Tested on x86_64 Fedora 17 by building GDB with --enable-targets=all,
in both 64-bit, and -m32 modes, and by running GDBs testsuite with
each build.  No regressions.

I haven't audited all Linux ports to see if like PPC their prpsinfo_t
types are not like the others'.

I just noticed, right before pushing send, that linux_fill_prpsinfo
does a "stat" call on the host, while that should be a call on the
target.  Unfortunately, there's no target_fileio_stat method.
Fortunately, the RSP bits are already in place, and just we need to
add the target method and hook it to remote_fileio_stat on the remote
target (and to the host stat for native targets).

Meanwhile, comments on the whole approach?

2013-01-31  Sergio Durigan Junior  <sergiodj@redhat.com>
	    Denys Vlasenko  <dvlasenk@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* gdbarch.sh (elfcore_write_linux_prpsinfo): New F hook.
	(struct elf_internal_linux_prpsinfo): Forward declare.
	* gdbarch.h, gdbarch.c: Regenerate.
	* linux-tdep.c: Include `cli/cli-utils.h'.
	(linux_fill_prpsinfo): New function.
	(linux_make_corefile_notes): Use linux_fill_prpsinfo.  If there's
	an elfcore_write_linux_prpsinfo hook, use it, otherwise, use
	elfcore_write_linux_prpsinfo32 or elfcore_write_linux_prpsinfo64
	depending on gdbarch pointer bitness.
	* ppc-linux-tdep.c: Include elf-bfd.h.
	(ppc_linux_init_abi): Hook in elfcore_write_ppc_linux_prpsinfo32
	on 32-bit.