* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[official-gcc.git] / libbacktrace / fileline.c
blobe577ecc9061602a48def60d1fddaa02845a2ed6c
1 /* fileline.c -- Get file and line number information in a backtrace.
2 Copyright (C) 2012 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 <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
40 #include "backtrace.h"
41 #include "internal.h"
43 /* Initialize the fileline information from the executable. Returns 1
44 on success, 0 on failure. */
46 static int
47 fileline_initialize (struct backtrace_state *state,
48 backtrace_error_callback error_callback, void *data)
50 int failed;
51 fileline fileline_fn;
52 int descriptor;
54 failed = state->fileline_initialization_failed;
56 if (state->threaded)
58 /* Use __sync_bool_compare_and_swap to do an atomic load. */
59 while (!__sync_bool_compare_and_swap
60 (&state->fileline_initialization_failed, failed, failed))
61 failed = state->fileline_initialization_failed;
64 if (failed)
66 error_callback (data, "failed to read executable information", -1);
67 return 0;
70 fileline_fn = state->fileline_fn;
71 if (state->threaded)
73 while (!__sync_bool_compare_and_swap (&state->fileline_fn, fileline_fn,
74 fileline_fn))
75 fileline_fn = state->fileline_fn;
77 if (fileline_fn != NULL)
78 return 1;
80 /* We have not initialized the information. Do it now. */
82 if (state->filename != NULL)
83 descriptor = backtrace_open (state->filename, error_callback, data, NULL);
84 else
85 descriptor = backtrace_open ("/proc/self/exe", error_callback, data, NULL);
86 if (descriptor < 0)
87 failed = 1;
89 if (!failed)
91 if (!backtrace_initialize (state, descriptor, error_callback, data,
92 &fileline_fn))
93 failed = 1;
96 if (failed)
98 if (!state->threaded)
99 state->fileline_initialization_failed = 1;
100 else
101 __sync_bool_compare_and_swap (&state->fileline_initialization_failed,
102 0, failed);
103 return 0;
106 if (!state->threaded)
107 state->fileline_fn = fileline_fn;
108 else
110 __sync_bool_compare_and_swap (&state->fileline_fn, NULL, fileline_fn);
112 /* At this point we know that state->fileline_fn is not NULL.
113 Either we stored our value, or some other thread stored its
114 value. If some other thread stored its value, we leak the
115 one we just initialized. Either way, state->fileline_fn is
116 initialized. The compare_and_swap is a full memory barrier,
117 so we should have full access to that value even if it was
118 created by another thread. */
121 return 1;
124 /* Given a PC, find the file name, line number, and function name. */
127 backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
128 backtrace_full_callback callback,
129 backtrace_error_callback error_callback, void *data)
131 if (!fileline_initialize (state, error_callback, data))
132 return 0;
134 if (state->fileline_initialization_failed)
135 return 0;
137 return state->fileline_fn (state, pc, callback, error_callback, data);
140 /* Given a PC, find the symbol for it, and its value. */
143 backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
144 backtrace_syminfo_callback callback,
145 backtrace_error_callback error_callback, void *data)
147 if (!fileline_initialize (state, error_callback, data))
148 return 0;
150 if (state->fileline_initialization_failed)
151 return 0;
153 state->syminfo_fn (state, pc, callback, error_callback, data);
154 return 1;