libbacktrace/:
[platform/upstream/gcc.git] / libbacktrace / fileline.c
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.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9     (1) Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer. 
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
15     distribution.  
16     
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
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.  */
32
33 #include "config.h"
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38
39 #include "backtrace.h"
40 #include "internal.h"
41
42 /* Initialize the fileline information from the executable.  Returns 1
43    on success, 0 on failure.  */
44
45 static int
46 fileline_initialize (struct backtrace_state *state,
47                      backtrace_error_callback error_callback, void *data)
48 {
49   int failed;
50   fileline fileline_fn;
51   int descriptor;
52
53   failed = state->fileline_initialization_failed;
54
55   if (state->threaded)
56     {
57       /* Use __sync_bool_compare_and_swap to do an atomic load.  */
58       while (!__sync_bool_compare_and_swap
59              (&state->fileline_initialization_failed, failed, failed))
60         failed = state->fileline_initialization_failed;
61     }
62
63   if (failed)
64     {
65       error_callback (data, "failed to read executable information", 0);
66       return 0;
67     }
68
69   fileline_fn = state->fileline_fn;
70   if (state->threaded)
71     {
72       while (!__sync_bool_compare_and_swap (&state->fileline_fn, fileline_fn,
73                                             fileline_fn))
74         fileline_fn = state->fileline_fn;
75     }
76   if (fileline_fn != NULL)
77     return 1;
78
79   /* We have not initialized the information.  Do it now.  */
80
81   if (state->filename != NULL)
82     descriptor = backtrace_open (state->filename, error_callback, data);
83   else
84     descriptor = backtrace_open ("/proc/self/exe", error_callback, data);
85   if (descriptor < 0)
86     failed = 1;
87
88   if (!failed)
89     {
90       if (!backtrace_initialize (state, descriptor, error_callback, data,
91                                  &fileline_fn))
92         failed = 1;
93     }
94
95   if (failed)
96     {
97       if (!state->threaded)
98         state->fileline_initialization_failed = 1;
99       else
100         __sync_bool_compare_and_swap (&state->fileline_initialization_failed,
101                                       0, failed);
102       return 0;
103     }
104
105   if (!state->threaded)
106     state->fileline_fn = fileline_fn;
107   else
108     {
109       __sync_bool_compare_and_swap (&state->fileline_fn, NULL, fileline_fn);
110
111       /* At this point we know that state->fileline_fn is not NULL.
112          Either we stored our value, or some other thread stored its
113          value.  If some other thread stored its value, we leak the
114          one we just initialized.  Either way, state->fileline_fn is
115          initialized.  The compare_and_swap is a full memory barrier,
116          so we should have full access to that value even if it was
117          created by another thread.  */
118     }
119
120   return 1;
121 }
122
123 /* Given a PC, find the file name, line number, and function name.  */
124
125 int
126 backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
127                   backtrace_full_callback callback,
128                   backtrace_error_callback error_callback, void *data)
129 {
130   if (!fileline_initialize (state, error_callback, data))
131     return 0;
132
133   if (state->fileline_initialization_failed)
134     return 0;
135
136   return state->fileline_fn (state, pc, callback, error_callback, data);
137 }
138
139 /* Given a PC, find the symbol for it, and its value.  */
140
141 int
142 backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
143                    backtrace_syminfo_callback callback,
144                    backtrace_error_callback error_callback, void *data)
145 {
146   if (!fileline_initialize (state, error_callback, data))
147     return 0;
148
149   if (state->fileline_initialization_failed)
150     return 0;
151
152   state->syminfo_fn (state, pc, callback, error_callback, data);
153   return 1;
154 }