public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [rfc][1/3] Remote core file generation: BFD support
@ 2011-10-21 18:56 Ulrich Weigand
  2011-10-21 19:54 ` Mark Kettenis
  2011-11-09 16:00 ` Pedro Alves
  0 siblings, 2 replies; 6+ messages in thread
From: Ulrich Weigand @ 2011-10-21 18:56 UTC (permalink / raw)
  To: gdb-patches; +Cc: binutils

Hello,

when implementing support for core file generation with remote targets
in GDB I ran into the problem that the elfcore_write_prpsinfo and
elfcore_write_prstatus are only defined for native targets.  This is
even though they could be implemented for remote targets if the
back-end provides the backend_write_core_note callback.

The following patch provides those functions unconditionally, so that
they can use the back-end callback if present, and return NULL to
signal failure if the callback is not available.

The patch in addition implements the callback for ARM Linux.

Bye,
Ulrich


ChangeLog:

	* elf.c (elfcore_write_prpsinfo): Provide unconditionally.
	Return NULL if core file generation is unsupported.
	(elfcore_write_prstatus): Likewise.
	* elf32-arm.c (elf32_arm_nabi_write_core_note): New function.
	(elf_backend_write_core_note): Define.


Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.544
diff -u -p -r1.544 elf.c
--- bfd/elf.c	30 Sep 2011 10:39:44 -0000	1.544
+++ bfd/elf.c	21 Oct 2011 16:49:52 -0000
@@ -8863,7 +8863,6 @@ elfcore_write_note (bfd *abfd,
   return buf;
 }
 
-#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
 char *
 elfcore_write_prpsinfo (bfd  *abfd,
 			char *buf,
@@ -8871,7 +8870,6 @@ elfcore_write_prpsinfo (bfd  *abfd,
 			const char *fname,
 			const char *psargs)
 {
-  const char *note_name = "CORE";
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   if (bed->elf_backend_write_core_note != NULL)
@@ -8883,6 +8881,7 @@ elfcore_write_prpsinfo (bfd  *abfd,
 	return ret;
     }
 
+#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   if (bed->s->elfclass == ELFCLASS32)
     {
@@ -8898,7 +8897,7 @@ elfcore_write_prpsinfo (bfd  *abfd,
       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
       return elfcore_write_note (abfd, buf, bufsiz,
-				 note_name, note_type, &data, sizeof (data));
+				 "CORE", note_type, &data, sizeof (data));
     }
   else
 #endif
@@ -8915,12 +8914,14 @@ elfcore_write_prpsinfo (bfd  *abfd,
       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
       return elfcore_write_note (abfd, buf, bufsiz,
-				 note_name, note_type, &data, sizeof (data));
+				 "CORE", note_type, &data, sizeof (data));
     }
-}
 #endif	/* PSINFO_T or PRPSINFO_T */
 
-#if defined (HAVE_PRSTATUS_T)
+  free (buf);
+  return NULL;
+}
+
 char *
 elfcore_write_prstatus (bfd *abfd,
 			char *buf,
@@ -8929,7 +8930,6 @@ elfcore_write_prstatus (bfd *abfd,
 			int cursig,
 			const void *gregs)
 {
-  const char *note_name = "CORE";
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   if (bed->elf_backend_write_core_note != NULL)
@@ -8942,6 +8942,7 @@ elfcore_write_prstatus (bfd *abfd,
 	return ret;
     }
 
+#if defined (HAVE_PRSTATUS_T)
 #if defined (HAVE_PRSTATUS32_T)
   if (bed->s->elfclass == ELFCLASS32)
     {
@@ -8951,7 +8952,7 @@ elfcore_write_prstatus (bfd *abfd,
       prstat.pr_pid = pid;
       prstat.pr_cursig = cursig;
       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
-      return elfcore_write_note (abfd, buf, bufsiz, note_name,
+      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
 				 NT_PRSTATUS, &prstat, sizeof (prstat));
     }
   else
@@ -8963,12 +8964,15 @@ elfcore_write_prstatus (bfd *abfd,
       prstat.pr_pid = pid;
       prstat.pr_cursig = cursig;
       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
-      return elfcore_write_note (abfd, buf, bufsiz, note_name,
+      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
 				 NT_PRSTATUS, &prstat, sizeof (prstat));
     }
-}
 #endif /* HAVE_PRSTATUS_T */
 
+  free (buf);
+  return NULL;
+}
+
 #if defined (HAVE_LWPSTATUS_T)
 char *
 elfcore_write_lwpstatus (bfd *abfd,
Index: bfd/elf32-arm.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-arm.c,v
retrieving revision 1.278
diff -u -p -r1.278 elf32-arm.c
--- bfd/elf32-arm.c	19 Oct 2011 07:17:13 -0000	1.278
+++ bfd/elf32-arm.c	21 Oct 2011 16:49:54 -0000
@@ -1986,6 +1986,55 @@ elf32_arm_nabi_grok_psinfo (bfd *abfd, E
   return TRUE;
 }
 
+static char *
+elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
+				int note_type, ...)
+{
+  switch (note_type)
+    {
+    default:
+      return NULL;
+
+    case NT_PRPSINFO:
+      {
+	char data[124];
+	va_list ap;
+
+	va_start (ap, note_type);
+	memset (data, 0, 28);
+	strncpy (data + 28, va_arg (ap, const char *), 16);
+	strncpy (data + 44, va_arg (ap, const char *), 80);
+	va_end (ap);
+
+	return elfcore_write_note (abfd, buf, bufsiz,
+				   "CORE", note_type, data, sizeof (data));
+      }
+
+    case NT_PRSTATUS:
+      {
+	char data[148];
+	va_list ap;
+	long pid;
+	int cursig;
+	const void *greg;
+
+	va_start (ap, note_type);
+	memset (data, 0, 72);
+	pid = va_arg (ap, long);
+	bfd_put_32 (abfd, pid, data + 24);
+	cursig = va_arg (ap, int);
+	bfd_put_16 (abfd, cursig, data + 12);
+	greg = va_arg (ap, const void *);
+	memcpy (data + 72, greg, 72);
+	memset (data + 144, 0, 4);
+	va_end (ap);
+
+	return elfcore_write_note (abfd, buf, bufsiz,
+				   "CORE", note_type, data, sizeof (data));
+      }
+    }
+}
+
 #define TARGET_LITTLE_SYM               bfd_elf32_littlearm_vec
 #define TARGET_LITTLE_NAME              "elf32-littlearm"
 #define TARGET_BIG_SYM                  bfd_elf32_bigarm_vec
@@ -1993,6 +2042,7 @@ elf32_arm_nabi_grok_psinfo (bfd *abfd, E
 
 #define elf_backend_grok_prstatus	elf32_arm_nabi_grok_prstatus
 #define elf_backend_grok_psinfo		elf32_arm_nabi_grok_psinfo
+#define elf_backend_write_core_note	elf32_arm_nabi_write_core_note
 
 typedef unsigned long int insn32;
 typedef unsigned short int insn16;
-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rfc][1/3] Remote core file generation: BFD support
  2011-10-21 18:56 [rfc][1/3] Remote core file generation: BFD support Ulrich Weigand
@ 2011-10-21 19:54 ` Mark Kettenis
  2011-10-21 20:53   ` Ulrich Weigand
  2011-11-09 16:00 ` Pedro Alves
  1 sibling, 1 reply; 6+ messages in thread
From: Mark Kettenis @ 2011-10-21 19:54 UTC (permalink / raw)
  To: uweigand; +Cc: gdb-patches, binutils

> 
> Hello,
> 
> when implementing support for core file generation with remote targets
> in GDB I ran into the problem that the elfcore_write_prpsinfo and
> elfcore_write_prstatus are only defined for native targets.  This is
> even though they could be implemented for remote targets if the
> back-end provides the backend_write_core_note callback.
> 
> The following patch provides those functions unconditionally, so that
> they can use the back-end callback if present, and return NULL to
> signal failure if the callback is not available.
> 
> The patch in addition implements the callback for ARM Linux.

There's a bit of a problem with this diff.  You're installing the
callback into the generic ARM vector, but the notes you're writing out
are (almost certainly) Linux-specific.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rfc][1/3] Remote core file generation: BFD support
  2011-10-21 19:54 ` Mark Kettenis
@ 2011-10-21 20:53   ` Ulrich Weigand
  0 siblings, 0 replies; 6+ messages in thread
From: Ulrich Weigand @ 2011-10-21 20:53 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches, binutils

Mark Kettenis wrote:
> > The patch in addition implements the callback for ARM Linux.
> 
> There's a bit of a problem with this diff.  You're installing the
> callback into the generic ARM vector, but the notes you're writing out
> are (almost certainly) Linux-specific.

Well, the routine is used to write notes named "CORE" with a note type
that is passed in as parameter (NT_PRPSINFO or NT_PRSTATUS).  I think
the only OS on ARM that uses this particular note name/type is Linux.
(OpenBSD for example uses completely different notes ...)

Do you know of any OS that uses a note of the same name and type,
but with different contents?

Interestingly enough, at the same place where I installed the new
routine, routines to *read* those core file notes were already present
(elf32_arm_nabi_grok_prstatus, elf32_arm_nabi_grok_psinfo).

Also, the PPC implementation of elf_backend_write_core_note is likewise
installed into the generic PowerPC vector.

Bye,
Ulrich

-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rfc][1/3] Remote core file generation: BFD support
  2011-10-21 18:56 [rfc][1/3] Remote core file generation: BFD support Ulrich Weigand
  2011-10-21 19:54 ` Mark Kettenis
@ 2011-11-09 16:00 ` Pedro Alves
  2011-11-09 18:18   ` Ulrich Weigand
  1 sibling, 1 reply; 6+ messages in thread
From: Pedro Alves @ 2011-11-09 16:00 UTC (permalink / raw)
  To: gdb-patches; +Cc: Ulrich Weigand, binutils

On Friday 21 October 2011 19:56:09, Ulrich Weigand wrote:

> Index: bfd/elf32-arm.c
> ===================================================================
> RCS file: /cvs/src/src/bfd/elf32-arm.c,v
> retrieving revision 1.278
> diff -u -p -r1.278 elf32-arm.c
> --- bfd/elf32-arm.c	19 Oct 2011 07:17:13 -0000	1.278
> +++ bfd/elf32-arm.c	21 Oct 2011 16:49:54 -0000
> @@ -1986,6 +1986,55 @@ elf32_arm_nabi_grok_psinfo (bfd *abfd, E
>    return TRUE;
>  }
>  
> +static char *
> +elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
> +				int note_type, ...)
> +{
> +  switch (note_type)
> +    {
> +    default:
> +      return NULL;
> +
> +    case NT_PRPSINFO:
> +      {
> +	char data[124];
> +	va_list ap;
> +
> +	va_start (ap, note_type);
> +	memset (data, 0, 28);
> +	strncpy (data + 28, va_arg (ap, const char *), 16);
> +	strncpy (data + 44, va_arg (ap, const char *), 80);

The `data' buffer should be zero initialized, to avoid
leaving garbage on the core, when the passed in fname and
psargs are less than 16 and 80 bytes long respectively.
It's true the caller does that itself today, but
your patch 2 (rightly) makes it not do so anymore.

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rfc][1/3] Remote core file generation: BFD support
  2011-11-09 16:00 ` Pedro Alves
@ 2011-11-09 18:18   ` Ulrich Weigand
  2012-01-16 22:31     ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: Ulrich Weigand @ 2011-11-09 18:18 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, binutils

Pedro Alves wrote:
> On Friday 21 October 2011 19:56:09, Ulrich Weigand wrote:
> 
> > Index: bfd/elf32-arm.c
> > ===================================================================
> > RCS file: /cvs/src/src/bfd/elf32-arm.c,v
> > retrieving revision 1.278
> > diff -u -p -r1.278 elf32-arm.c
> > --- bfd/elf32-arm.c	19 Oct 2011 07:17:13 -0000	1.278
> > +++ bfd/elf32-arm.c	21 Oct 2011 16:49:54 -0000
> > @@ -1986,6 +1986,55 @@ elf32_arm_nabi_grok_psinfo (bfd *abfd, E
> >    return TRUE;
> >  }
> >  
> > +static char *
> > +elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
> > +				int note_type, ...)
> > +{
> > +  switch (note_type)
> > +    {
> > +    default:
> > +      return NULL;
> > +
> > +    case NT_PRPSINFO:
> > +      {
> > +	char data[124];
> > +	va_list ap;
> > +
> > +	va_start (ap, note_type);
> > +	memset (data, 0, 28);
> > +	strncpy (data + 28, va_arg (ap, const char *), 16);
> > +	strncpy (data + 44, va_arg (ap, const char *), 80);
> 
> The `data' buffer should be zero initialized, to avoid
> leaving garbage on the core, when the passed in fname and
> psargs are less than 16 and 80 bytes long respectively.
> It's true the caller does that itself today, but
> your patch 2 (rightly) makes it not do so anymore.

OK, good point.  I've just copied this from the PowerPC implementation,
which needs to be fixed as well ...   I'll provide an updated patch.

Thanks,
Ulrich

-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rfc][1/3] Remote core file generation: BFD support
  2011-11-09 18:18   ` Ulrich Weigand
@ 2012-01-16 22:31     ` Alan Modra
  0 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2012-01-16 22:31 UTC (permalink / raw)
  To: Ulrich Weigand; +Cc: Pedro Alves, gdb-patches, binutils

On Wed, Nov 09, 2011 at 07:18:26PM +0100, Ulrich Weigand wrote:
> OK, good point.  I've just copied this from the PowerPC implementation,
> which needs to be fixed as well ...   I'll provide an updated patch.

Applied.

	* elf32-ppc.c (ppc_elf_write_core_note <NT_PRPSINFO>): Don't leave
	trailing garbage in the note.
	* elf64-ppc.c (ppc64_elf_write_core_note <NT_PRPSINFO>): Likewise.

Index: bfd/elf32-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-ppc.c,v
retrieving revision 1.306
diff -u -p -r1.306 elf32-ppc.c
--- bfd/elf32-ppc.c	3 Dec 2011 00:55:49 -0000	1.306
+++ bfd/elf32-ppc.c	16 Jan 2012 22:25:06 -0000
@@ -1920,7 +1920,7 @@ ppc_elf_write_core_note (bfd *abfd, char
 	va_list ap;
 
 	va_start (ap, note_type);
-	memset (data, 0, 32);
+	memset (data, 0, sizeof (data));
 	strncpy (data + 32, va_arg (ap, const char *), 16);
 	strncpy (data + 48, va_arg (ap, const char *), 80);
 	va_end (ap);
Index: bfd/elf64-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-ppc.c,v
retrieving revision 1.373
diff -u -p -r1.373 elf64-ppc.c
--- bfd/elf64-ppc.c	11 Jan 2012 09:28:44 -0000	1.373
+++ bfd/elf64-ppc.c	16 Jan 2012 22:25:12 -0000
@@ -2721,7 +2721,7 @@ ppc64_elf_write_core_note (bfd *abfd, ch
 	va_list ap;
 
 	va_start (ap, note_type);
-	memset (data, 0, 40);
+	memset (data, 0, sizeof (data));
 	strncpy (data + 40, va_arg (ap, const char *), 16);
 	strncpy (data + 56, va_arg (ap, const char *), 80);
 	va_end (ap);

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-01-16 22:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-21 18:56 [rfc][1/3] Remote core file generation: BFD support Ulrich Weigand
2011-10-21 19:54 ` Mark Kettenis
2011-10-21 20:53   ` Ulrich Weigand
2011-11-09 16:00 ` Pedro Alves
2011-11-09 18:18   ` Ulrich Weigand
2012-01-16 22:31     ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).