]> git.ipfire.org Git - thirdparty/gcc.git/blame - libbacktrace/backtrace.c
backtrace.c: Include backtrace-supported.h before checking BACKTRACE_USES_MALLOC.
[thirdparty/gcc.git] / libbacktrace / backtrace.c
CommitLineData
eff02e4f 1/* backtrace.c -- Entry point for stack backtrace library.
85ec4feb 2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
eff02e4f
ILT
3 Written by Ian Lance Taylor, Google.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 (1) Redistributions of source code must retain the above copyright
84ebf639 10 notice, this list of conditions and the following disclaimer.
eff02e4f
ILT
11
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
84ebf639
CL
15 distribution.
16
eff02e4f
ILT
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.
20
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY OF SUCH DAMAGE. */
32
33#include "config.h"
34
83a658ca 35#include <unistd.h>
50809ff9
HPN
36#include <sys/types.h>
37
c36af1b4
ILT
38#include "backtrace-supported.h"
39
83a658ca
ILT
40#if !BACKTRACE_USES_MALLOC
41#include <sys/mman.h>
42#endif
43
eff02e4f
ILT
44#include "unwind.h"
45#include "backtrace.h"
c478516b 46#include "internal.h"
eff02e4f 47
83a658ca
ILT
48#ifndef MAP_ANONYMOUS
49#define MAP_ANONYMOUS MAP_ANON
50#endif
51
52#ifndef MAP_FAILED
53#define MAP_FAILED ((void *)-1)
54#endif
55
eff02e4f
ILT
56/* The main backtrace_full routine. */
57
58/* Data passed through _Unwind_Backtrace. */
59
60struct backtrace_data
61{
62 /* Number of frames to skip. */
63 int skip;
64 /* Library state. */
65 struct backtrace_state *state;
66 /* Callback routine. */
67 backtrace_full_callback callback;
68 /* Error callback routine. */
69 backtrace_error_callback error_callback;
70 /* Data to pass to callback routines. */
71 void *data;
72 /* Value to return from backtrace_full. */
73 int ret;
c478516b
ILT
74 /* Whether there is any memory available. */
75 int can_alloc;
eff02e4f
ILT
76};
77
78/* Unwind library callback routine. This is passed to
79 _Unwind_Backtrace. */
80
81static _Unwind_Reason_Code
82unwind (struct _Unwind_Context *context, void *vdata)
83{
84 struct backtrace_data *bdata = (struct backtrace_data *) vdata;
85 uintptr_t pc;
86 int ip_before_insn = 0;
87
88#ifdef HAVE_GETIPINFO
89 pc = _Unwind_GetIPInfo (context, &ip_before_insn);
90#else
91 pc = _Unwind_GetIP (context);
92#endif
93
94 if (bdata->skip > 0)
95 {
96 --bdata->skip;
97 return _URC_NO_REASON;
98 }
99
100 if (!ip_before_insn)
101 --pc;
102
c478516b
ILT
103 if (!bdata->can_alloc)
104 bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
105 else
106 bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
107 bdata->error_callback, bdata->data);
eff02e4f
ILT
108 if (bdata->ret != 0)
109 return _URC_END_OF_STACK;
110
111 return _URC_NO_REASON;
112}
113
114/* Get a stack backtrace. */
115
116int
117backtrace_full (struct backtrace_state *state, int skip,
118 backtrace_full_callback callback,
119 backtrace_error_callback error_callback, void *data)
120{
121 struct backtrace_data bdata;
122
123 bdata.skip = skip + 1;
124 bdata.state = state;
125 bdata.callback = callback;
126 bdata.error_callback = error_callback;
127 bdata.data = data;
128 bdata.ret = 0;
c478516b 129
83a658ca
ILT
130#if !BACKTRACE_USES_MALLOC
131 {
132 size_t pagesize;
133 void *page;
134
135 /* If we can't allocate any memory at all, don't try to produce
136 file/line information. */
137 pagesize = getpagesize ();
138 page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
139 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
140 if (page == MAP_FAILED)
141 bdata.can_alloc = 0;
142 else
143 {
144 munmap (page, pagesize);
145 bdata.can_alloc = 1;
146 }
147 }
148#endif
c478516b 149
eff02e4f
ILT
150 _Unwind_Backtrace (unwind, &bdata);
151 return bdata.ret;
152}