Commit 83a658ca by Ian Lance Taylor Committed by Ian Lance Taylor

backtrace.c (backtrace_full): When testing whether we can allocate memory...

	* backtrace.c (backtrace_full): When testing whether we can
	allocate memory, call mmap directly, and munmap the memory.

Fixes https://github.com/ianlancetaylor/libbacktrace/issues/13 .

From-SVN: r259434
parent 50ffe7ad
2018-04-17 Ian Lance Taylor <iant@golang.org>
* backtrace.c (backtrace_full): When testing whether we can
allocate memory, call mmap directly, and munmap the memory.
2018-04-04 Jakub Jelinek <jakub@redhat.com> 2018-04-04 Jakub Jelinek <jakub@redhat.com>
PR other/85161 PR other/85161
......
...@@ -32,12 +32,26 @@ POSSIBILITY OF SUCH DAMAGE. */ ...@@ -32,12 +32,26 @@ POSSIBILITY OF SUCH DAMAGE. */
#include "config.h" #include "config.h"
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#if !BACKTRACE_USES_MALLOC
#include <sys/mman.h>
#endif
#include "unwind.h" #include "unwind.h"
#include "backtrace.h" #include "backtrace.h"
#include "backtrace-supported.h"
#include "internal.h" #include "internal.h"
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
#ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
/* The main backtrace_full routine. */ /* The main backtrace_full routine. */
/* Data passed through _Unwind_Backtrace. */ /* Data passed through _Unwind_Backtrace. */
...@@ -104,7 +118,6 @@ backtrace_full (struct backtrace_state *state, int skip, ...@@ -104,7 +118,6 @@ backtrace_full (struct backtrace_state *state, int skip,
backtrace_error_callback error_callback, void *data) backtrace_error_callback error_callback, void *data)
{ {
struct backtrace_data bdata; struct backtrace_data bdata;
void *p;
bdata.skip = skip + 1; bdata.skip = skip + 1;
bdata.state = state; bdata.state = state;
...@@ -113,16 +126,25 @@ backtrace_full (struct backtrace_state *state, int skip, ...@@ -113,16 +126,25 @@ backtrace_full (struct backtrace_state *state, int skip,
bdata.data = data; bdata.data = data;
bdata.ret = 0; bdata.ret = 0;
/* If we can't allocate any memory at all, don't try to produce #if !BACKTRACE_USES_MALLOC
file/line information. */ {
p = backtrace_alloc (state, 4096, NULL, NULL); size_t pagesize;
if (p == NULL) void *page;
bdata.can_alloc = 0;
else /* If we can't allocate any memory at all, don't try to produce
{ file/line information. */
backtrace_free (state, p, 4096, NULL, NULL); pagesize = getpagesize ();
bdata.can_alloc = 1; page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
} MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (page == MAP_FAILED)
bdata.can_alloc = 0;
else
{
munmap (page, pagesize);
bdata.can_alloc = 1;
}
}
#endif
_Unwind_Backtrace (unwind, &bdata); _Unwind_Backtrace (unwind, &bdata);
return bdata.ret; return bdata.ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment