public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* libbacktrace patch committed: Graceful fallback if out of memory
@ 2015-09-08 17:15 Ian Lance Taylor
  2015-09-09  0:35 ` Hans-Peter Nilsson
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2015-09-08 17:15 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 820 bytes --]

I've committed this libbacktrace patch to mainline to do a graceful
fallback if no memory can be allocated.  In that case we print out the
PC addresses without trying to resolve file/line information.  This is
imperfect but better than the earlier behaviour of producing a series
of error messages.  Tested with libbacktrace and Go testsuites.
Committed to mainline.

Ian

2015-09-08  Ian Lance Taylor  <iant@google.com>

PR other/67457
* backtrace.c: #include "internal.h".
(struct backtrace_data): Add can_alloc field.
(unwind): If can_alloc is false, don't try to get file/line
information.
(backtrace_full): Set can_alloc field in bdata.
* alloc.c (backtrace_alloc): Don't call error_callback if it is
NULL.
* mmap.c (backtrace_alloc): Likewise.
* internal.h: Update comments for backtrace_alloc and
backtrace_free.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4200 bytes --]

Index: alloc.c
===================================================================
--- alloc.c	(revision 227528)
+++ alloc.c	(working copy)
@@ -44,7 +44,8 @@ POSSIBILITY OF SUCH DAMAGE.  */
    backtrace functions may not be safely invoked from a signal
    handler.  */
 
-/* Allocate memory like malloc.  */
+/* Allocate memory like malloc.  If ERROR_CALLBACK is NULL, don't
+   report an error.  */
 
 void *
 backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED,
@@ -55,7 +56,10 @@ backtrace_alloc (struct backtrace_state
 
   ret = malloc (size);
   if (ret == NULL)
-    error_callback (data, "malloc", errno);
+    {
+      if (error_callback)
+	error_callback (data, "malloc", errno);
+    }
   return ret;
 }
 
Index: backtrace.c
===================================================================
--- backtrace.c	(revision 227528)
+++ backtrace.c	(working copy)
@@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.  */
 
 #include "unwind.h"
 #include "backtrace.h"
+#include "internal.h"
 
 /* The main backtrace_full routine.  */
 
@@ -53,6 +54,8 @@ struct backtrace_data
   void *data;
   /* Value to return from backtrace_full.  */
   int ret;
+  /* Whether there is any memory available.  */
+  int can_alloc;
 };
 
 /* Unwind library callback routine.  This is passed to
@@ -80,8 +83,11 @@ unwind (struct _Unwind_Context *context,
   if (!ip_before_insn)
     --pc;
 
-  bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
-				 bdata->error_callback, bdata->data);
+  if (!bdata->can_alloc)
+    bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
+  else
+    bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
+				   bdata->error_callback, bdata->data);
   if (bdata->ret != 0)
     return _URC_END_OF_STACK;
 
@@ -96,6 +102,7 @@ backtrace_full (struct backtrace_state *
 		backtrace_error_callback error_callback, void *data)
 {
   struct backtrace_data bdata;
+  void *p;
 
   bdata.skip = skip + 1;
   bdata.state = state;
@@ -103,6 +110,18 @@ backtrace_full (struct backtrace_state *
   bdata.error_callback = error_callback;
   bdata.data = data;
   bdata.ret = 0;
+
+  /* If we can't allocate any memory at all, don't try to produce
+     file/line information.  */
+  p = backtrace_alloc (state, 4096, NULL, NULL);
+  if (p == NULL)
+    bdata.can_alloc = 0;
+  else
+    {
+      backtrace_free (state, p, 4096, NULL, NULL);
+      bdata.can_alloc = 1;
+    }
+
   _Unwind_Backtrace (unwind, &bdata);
   return bdata.ret;
 }
Index: internal.h
===================================================================
--- internal.h	(revision 227528)
+++ internal.h	(working copy)
@@ -201,13 +201,15 @@ extern int backtrace_close (int descript
 extern void backtrace_qsort (void *base, size_t count, size_t size,
 			     int (*compar) (const void *, const void *));
 
-/* Allocate memory.  This is like malloc.  */
+/* Allocate memory.  This is like malloc.  If ERROR_CALLBACK is NULL,
+   this does not report an error, it just returns NULL.  */
 
 extern void *backtrace_alloc (struct backtrace_state *state, size_t size,
 			      backtrace_error_callback error_callback,
 			      void *data) ATTRIBUTE_MALLOC;
 
-/* Free memory allocated by backtrace_alloc.  */
+/* Free memory allocated by backtrace_alloc.  If ERROR_CALLBACK is
+   NULL, this does not report an error.  */
 
 extern void backtrace_free (struct backtrace_state *state, void *mem,
 			    size_t size,
Index: mmap.c
===================================================================
--- mmap.c	(revision 227529)
+++ mmap.c	(working copy)
@@ -77,7 +77,8 @@ backtrace_free_locked (struct backtrace_
     }
 }
 
-/* Allocate memory like malloc.  */
+/* Allocate memory like malloc.  If ERROR_CALLBACK is NULL, don't
+   report an error.  */
 
 void *
 backtrace_alloc (struct backtrace_state *state,
@@ -140,7 +141,10 @@ backtrace_alloc (struct backtrace_state
       page = mmap (NULL, asksize, PROT_READ | PROT_WRITE,
 		   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
       if (page == MAP_FAILED)
-	error_callback (data, "mmap", errno);
+	{
+	  if (error_callback)
+	    error_callback (data, "mmap", errno);
+	}
       else
 	{
 	  size = (size + 7) & ~ (size_t) 7;

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

* Re: libbacktrace patch committed: Graceful fallback if out of memory
  2015-09-08 17:15 libbacktrace patch committed: Graceful fallback if out of memory Ian Lance Taylor
@ 2015-09-09  0:35 ` Hans-Peter Nilsson
  2015-09-09  4:12   ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Hans-Peter Nilsson @ 2015-09-09  0:35 UTC (permalink / raw)
  To: iant; +Cc: gcc-patches

> From: Ian Lance Taylor <iant@google.com>
> Date: Tue, 8 Sep 2015 18:46:21 +0200


> PR other/67457
> * backtrace.c: #include "internal.h".
> (struct backtrace_data): Add can_alloc field.
> (unwind): If can_alloc is false, don't try to get file/line
> information.
> (backtrace_full): Set can_alloc field in bdata.
> * alloc.c (backtrace_alloc): Don't call error_callback if it is
> NULL.
> * mmap.c (backtrace_alloc): Likewise.
> * internal.h: Update comments for backtrace_alloc and
> backtrace_free.

> Index: backtrace.c
> ===================================================================
> --- backtrace.c	(revision 227528)
> +++ backtrace.c	(working copy)
> @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.  */
>  
>  #include "unwind.h"
>  #include "backtrace.h"
> +#include "internal.h"
>  
>  /* The main backtrace_full routine.  */
>  


I don't know about your environment, but for me (cross from
x86_64-linux to cris-elf) that causes a:

/bin/sh ./libtool --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I/tmp/hpautotest-gcc0/gcc/libbacktrace  -I /tmp/hpautotest-gcc0/gcc/libbacktrace/../include -I /tmp/hpautotest-gcc0/gcc/libbacktrace/../libgcc -I ../libgcc  -funwind-tables -frandom-seed=backtrace.lo -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wmissing-format-attribute -Wcast-qual  -g -O2 -c -o backtrace.lo /tmp/hpautotest-gcc0/gcc/libbacktrace/backtrace.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I/tmp/hpautotest-gcc0/gcc/libbacktrace -I /tmp/hpautotest-gcc0/gcc/libbacktrace/../include -I /tmp/hpautotest-gcc0/gcc/libbacktrace/../libgcc -I ../libgcc -funwind-tables -frandom-seed=backtrace.lo -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wmissing-format-attribute -Wcast-qual -g -O2 -c /tmp/hpautotest-gcc0/gcc/libbacktrace/backtrace.c  -fPIC -DPIC -o .libs/backtrace.o
In file included from /tmp/hpautotest-gcc0/gcc/libbacktrace/backtrace.c:37:
/tmp/hpautotest-gcc0/gcc/libbacktrace/internal.h:182: error: expected declaration specifiers or '...' before 'off_t'
make[3]: *** [backtrace.lo] Error 1
make[3]: Leaving directory `/tmp/hpautotest-gcc0/cris-elf/gccobj/libbacktrace'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/tmp/hpautotest-gcc0/cris-elf/gccobj/libbacktrace'
make[1]: *** [all-libbacktrace] Error 2
make[1]: Leaving directory `/tmp/hpautotest-gcc0/cris-elf/gccobj'
make: *** [all] Error 2

I've committed the following as obvious, following the pattern
of the other files including internal.h, after observing
all-libbacktrace (i.e. built for the host) complete.

libbacktrace:
	* backtrace.c: #include <sys/types.h>.

Index: backtrace.c
===================================================================
--- backtrace.c	(revision 227567)
+++ backtrace.c	(working copy)
@@ -32,6 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.  */
 
 #include "config.h"
 
+#include <sys/types.h>
+
 #include "unwind.h"
 #include "backtrace.h"
 #include "internal.h"

brgds, H-P

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

* Re: libbacktrace patch committed: Graceful fallback if out of memory
  2015-09-09  0:35 ` Hans-Peter Nilsson
@ 2015-09-09  4:12   ` Ian Lance Taylor
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2015-09-09  4:12 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: gcc-patches

On Tue, Sep 8, 2015 at 5:00 PM, Hans-Peter Nilsson
<hans-peter.nilsson@axis.com> wrote:
>
> I've committed the following as obvious, following the pattern
> of the other files including internal.h, after observing
> all-libbacktrace (i.e. built for the host) complete.
>
> libbacktrace:
>         * backtrace.c: #include <sys/types.h>.

Thanks.

Ian

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

end of thread, other threads:[~2015-09-09  0:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-08 17:15 libbacktrace patch committed: Graceful fallback if out of memory Ian Lance Taylor
2015-09-09  0:35 ` Hans-Peter Nilsson
2015-09-09  4:12   ` Ian Lance Taylor

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).