runtime: enable precise GC checks when using stack maps
[gcc.git] / libgo / runtime / go-caller.c
1 /* go-caller.c -- look up function/file/line/entry info
2
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
6
7 /* Implement runtime.Caller. */
8
9 #include <stdint.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 #include "backtrace.h"
15
16 #include "runtime.h"
17
18 /* Get the function name, file name, and line number for a PC value.
19 We use the backtrace library to get this. */
20
21 /* Data structure to gather file/line information. */
22
23 struct caller
24 {
25 String fn;
26 String file;
27 intgo line;
28 intgo index;
29 };
30
31 /* Collect file/line information for a PC value. If this is called
32 more than once, due to inlined functions, we use the last call, as
33 that is usually the most useful one. */
34
35 static int
36 callback (void *data, uintptr_t pc __attribute__ ((unused)),
37 const char *filename, int lineno, const char *function)
38 {
39 struct caller *c = (struct caller *) data;
40
41 /* The libbacktrace library says that these strings might disappear,
42 but with the current implementation they won't. We can't easily
43 allocate memory here, so for now assume that we can save a
44 pointer to the strings. */
45 c->fn = runtime_gostringnocopy ((const byte *) function);
46 c->file = runtime_gostringnocopy ((const byte *) filename);
47 c->line = lineno;
48
49 if (c->index == 0)
50 return 1;
51
52 if (c->index > 0)
53 --c->index;
54
55 return 0;
56 }
57
58 /* The error callback for backtrace_pcinfo and backtrace_syminfo. */
59
60 static void
61 error_callback (void *data __attribute__ ((unused)),
62 const char *msg, int errnum)
63 {
64 if (errnum == -1)
65 return;
66 if (errnum > 0)
67 runtime_printf ("%s errno %d\n", msg, errnum);
68 runtime_throw (msg);
69 }
70
71 /* The backtrace library state. */
72
73 static void *back_state;
74
75 /* A lock to control creating back_state. */
76
77 static uint32 back_state_lock;
78
79 /* The program arguments. */
80
81 extern Slice runtime_get_args(void);
82
83 /* Fetch back_state, creating it if necessary. */
84
85 struct backtrace_state *
86 __go_get_backtrace_state ()
87 {
88 uint32 set;
89
90 /* We may not have a g here, so we can't use runtime_lock. */
91 set = 0;
92 while (!__atomic_compare_exchange_n (&back_state_lock, &set, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
93 {
94 runtime_osyield ();
95 set = 0;
96 }
97 if (back_state == NULL)
98 {
99 Slice args;
100 const char *filename;
101 struct stat s;
102
103 args = runtime_get_args();
104 filename = NULL;
105 if (args.__count > 0)
106 filename = (const char*)((String*)args.__values)[0].str;
107
108 /* If there is no '/' in FILENAME, it was found on PATH, and
109 might not be the same as the file with the same name in the
110 current directory. */
111 if (filename != NULL && __builtin_strchr (filename, '/') == NULL)
112 filename = NULL;
113
114 /* If the file is small, then it's not the real executable.
115 This is specifically to deal with Docker, which uses a bogus
116 argv[0] (http://gcc.gnu.org/PR61895). It would be nice to
117 have a better check for whether this file is the real
118 executable. */
119 if (filename != NULL && (stat (filename, &s) < 0 || s.st_size < 1024))
120 filename = NULL;
121
122 back_state = backtrace_create_state (filename, 1, error_callback, NULL);
123 }
124 __atomic_store_n (&back_state_lock, 0, __ATOMIC_RELEASE);
125 return back_state;
126 }
127
128 /* Return function/file/line information for PC. The index parameter
129 is the entry on the stack of inlined functions; -1 means the last
130 one. */
131
132 static _Bool
133 __go_file_line (uintptr pc, int index, String *fn, String *file, intgo *line)
134 {
135 struct caller c;
136 struct backtrace_state *state;
137
138 runtime_memclr (&c, sizeof c);
139 c.index = index;
140 runtime_xadd (&__go_runtime_in_callers, 1);
141 state = __go_get_backtrace_state ();
142 runtime_xadd (&__go_runtime_in_callers, -1);
143 backtrace_pcinfo (state, pc, callback, error_callback, &c);
144 *fn = c.fn;
145 *file = c.file;
146 *line = c.line;
147
148 // If backtrace_pcinfo didn't get the function name from the debug
149 // info, try to get it from the symbol table.
150 if (fn->len == 0)
151 backtrace_syminfo (state, pc, __go_syminfo_fnname_callback,
152 error_callback, fn);
153
154 return c.file.len > 0;
155 }
156
157 /* Collect symbol information. */
158
159 static void
160 syminfo_callback (void *data, uintptr_t pc __attribute__ ((unused)),
161 const char *symname __attribute__ ((unused)),
162 uintptr_t address, uintptr_t size __attribute__ ((unused)))
163 {
164 uintptr_t *pval = (uintptr_t *) data;
165
166 *pval = address;
167 }
168
169 /* Set *VAL to the value of the symbol for PC. */
170
171 static _Bool
172 __go_symbol_value (uintptr pc, uintptr *val)
173 {
174 struct backtrace_state *state;
175
176 *val = 0;
177 runtime_xadd (&__go_runtime_in_callers, 1);
178 state = __go_get_backtrace_state ();
179 runtime_xadd (&__go_runtime_in_callers, -1);
180 backtrace_syminfo (state, pc, syminfo_callback,
181 error_callback, val);
182 return *val != 0;
183 }
184
185 /* The values returned by runtime.Caller. */
186
187 struct caller_ret
188 {
189 uintptr_t pc;
190 String file;
191 intgo line;
192 _Bool ok;
193 };
194
195 struct caller_ret Caller (intgo n) __asm__ (GOSYM_PREFIX "runtime.Caller");
196
197 /* Implement runtime.Caller. */
198
199 struct caller_ret
200 Caller (intgo skip)
201 {
202 struct caller_ret ret;
203 Location loc;
204 int32 n;
205
206 runtime_memclr (&ret, sizeof ret);
207 n = runtime_callers (skip + 1, &loc, 1, false);
208 if (n < 1 || loc.pc == 0)
209 return ret;
210 ret.pc = loc.pc;
211 ret.file = loc.filename;
212 ret.line = loc.lineno;
213 ret.ok = 1;
214 return ret;
215 }
216
217 /* Look up the function name, file name, and line number for a PC. */
218
219 struct funcfileline_return
220 runtime_funcfileline (uintptr targetpc, int32 index)
221 {
222 struct funcfileline_return ret;
223
224 if (!__go_file_line (targetpc, index, &ret.retfn, &ret.retfile,
225 &ret.retline))
226 runtime_memclr (&ret, sizeof ret);
227 return ret;
228 }
229
230 /* Return the entry point of a function. */
231 uintptr runtime_funcentry(uintptr)
232 __asm__ (GOSYM_PREFIX "runtime.funcentry");
233
234 uintptr
235 runtime_funcentry (uintptr pc)
236 {
237 uintptr val;
238
239 if (!__go_symbol_value (pc, &val))
240 return 0;
241 return val;
242 }