* backtrace.c (backtrace_full): When testing whether we can
[official-gcc.git] / libbacktrace / backtrace.c
blob319edcf12eb963065a1ca405f8dc6c64fd44d69d
1 /* backtrace.c -- Entry point for stack backtrace library.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 #include "config.h"
35 #include <unistd.h>
36 #include <sys/types.h>
38 #if !BACKTRACE_USES_MALLOC
39 #include <sys/mman.h>
40 #endif
42 #include "unwind.h"
43 #include "backtrace.h"
44 #include "backtrace-supported.h"
45 #include "internal.h"
47 #ifndef MAP_ANONYMOUS
48 #define MAP_ANONYMOUS MAP_ANON
49 #endif
51 #ifndef MAP_FAILED
52 #define MAP_FAILED ((void *)-1)
53 #endif
55 /* The main backtrace_full routine. */
57 /* Data passed through _Unwind_Backtrace. */
59 struct backtrace_data
61 /* Number of frames to skip. */
62 int skip;
63 /* Library state. */
64 struct backtrace_state *state;
65 /* Callback routine. */
66 backtrace_full_callback callback;
67 /* Error callback routine. */
68 backtrace_error_callback error_callback;
69 /* Data to pass to callback routines. */
70 void *data;
71 /* Value to return from backtrace_full. */
72 int ret;
73 /* Whether there is any memory available. */
74 int can_alloc;
77 /* Unwind library callback routine. This is passed to
78 _Unwind_Backtrace. */
80 static _Unwind_Reason_Code
81 unwind (struct _Unwind_Context *context, void *vdata)
83 struct backtrace_data *bdata = (struct backtrace_data *) vdata;
84 uintptr_t pc;
85 int ip_before_insn = 0;
87 #ifdef HAVE_GETIPINFO
88 pc = _Unwind_GetIPInfo (context, &ip_before_insn);
89 #else
90 pc = _Unwind_GetIP (context);
91 #endif
93 if (bdata->skip > 0)
95 --bdata->skip;
96 return _URC_NO_REASON;
99 if (!ip_before_insn)
100 --pc;
102 if (!bdata->can_alloc)
103 bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
104 else
105 bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
106 bdata->error_callback, bdata->data);
107 if (bdata->ret != 0)
108 return _URC_END_OF_STACK;
110 return _URC_NO_REASON;
113 /* Get a stack backtrace. */
116 backtrace_full (struct backtrace_state *state, int skip,
117 backtrace_full_callback callback,
118 backtrace_error_callback error_callback, void *data)
120 struct backtrace_data bdata;
122 bdata.skip = skip + 1;
123 bdata.state = state;
124 bdata.callback = callback;
125 bdata.error_callback = error_callback;
126 bdata.data = data;
127 bdata.ret = 0;
129 #if !BACKTRACE_USES_MALLOC
131 size_t pagesize;
132 void *page;
134 /* If we can't allocate any memory at all, don't try to produce
135 file/line information. */
136 pagesize = getpagesize ();
137 page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
138 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
139 if (page == MAP_FAILED)
140 bdata.can_alloc = 0;
141 else
143 munmap (page, pagesize);
144 bdata.can_alloc = 1;
147 #endif
149 _Unwind_Backtrace (unwind, &bdata);
150 return bdata.ret;