compiler: omit field name for embedded fields in reflection string
[official-gcc.git] / libgo / runtime / go-callers.c
blob2eaac68277979f6a161f554f3434cc9df4daf1b1
1 /* go-callers.c -- get callers for Go.
3 Copyright 2012 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. */
7 #include "config.h"
9 #include "backtrace.h"
11 #include "runtime.h"
12 #include "array.h"
14 /* This is set to non-zero when calling backtrace_full. This is used
15 to avoid getting hanging on a recursive lock in dl_iterate_phdr on
16 older versions of glibc when a SIGPROF signal arrives while
17 collecting a backtrace. */
19 static uint32 runtime_in_callers;
21 /* Argument passed to callback function. */
23 struct callers_data
25 Location *locbuf;
26 int skip;
27 int index;
28 int max;
29 int keep_thunks;
32 /* Callback function for backtrace_full. Just collect the locations.
33 Return zero to continue, non-zero to stop. */
35 static int
36 callback (void *data, uintptr_t pc, const char *filename, int lineno,
37 const char *function)
39 struct callers_data *arg = (struct callers_data *) data;
40 Location *loc;
42 /* Skip split stack functions. */
43 if (function != NULL)
45 const char *p;
47 p = function;
48 if (__builtin_strncmp (p, "___", 3) == 0)
49 ++p;
50 if (__builtin_strncmp (p, "__morestack_", 12) == 0)
51 return 0;
53 else if (filename != NULL)
55 const char *p;
57 p = strrchr (filename, '/');
58 if (p == NULL)
59 p = filename;
60 if (__builtin_strncmp (p, "/morestack.S", 12) == 0)
61 return 0;
64 /* Skip thunks and recover functions. There is no equivalent to
65 these functions in the gc toolchain, so returning them here means
66 significantly different results for runtime.Caller(N). */
67 if (function != NULL && !arg->keep_thunks)
69 const char *p;
71 p = function + __builtin_strlen (function);
72 while (p > function && p[-1] >= '0' && p[-1] <= '9')
73 --p;
74 if (p - function > 7 && __builtin_strncmp (p - 7, "..thunk", 7) == 0)
75 return 0;
76 if (p - function > 3 && __builtin_strcmp (p - 3, "..r") == 0)
77 return 0;
78 if (p - function > 6 && __builtin_strcmp (p - 6, "..stub") == 0)
79 return 0;
82 if (arg->skip > 0)
84 --arg->skip;
85 return 0;
88 loc = &arg->locbuf[arg->index];
90 /* On the call to backtrace_full the pc value was most likely
91 decremented if there was a normal call, since the pc referred to
92 the instruction where the call returned and not the call itself.
93 This was done so that the line number referred to the call
94 instruction. To make sure the actual pc from the call stack is
95 used, it is incremented here.
97 In the case of a signal, the pc was not decremented by
98 backtrace_full but still incremented here. That doesn't really
99 hurt anything since the line number is right and the pc refers to
100 the same instruction. */
102 loc->pc = pc + 1;
104 /* The libbacktrace library says that these strings might disappear,
105 but with the current implementation they won't. We can't easily
106 allocate memory here, so for now assume that we can save a
107 pointer to the strings. */
108 loc->filename = runtime_gostringnocopy ((const byte *) filename);
109 loc->function = runtime_gostringnocopy ((const byte *) function);
111 loc->lineno = lineno;
112 ++arg->index;
114 /* There is no point to tracing past certain runtime functions.
115 Stopping the backtrace here can avoid problems on systems that
116 don't provide proper unwind information for makecontext, such as
117 Solaris (http://gcc.gnu.org/PR52583 comment #21). */
118 if (function != NULL)
120 if (__builtin_strcmp (function, "makecontext") == 0)
121 return 1;
122 if (filename != NULL)
124 const char *p;
126 p = strrchr (filename, '/');
127 if (p == NULL)
128 p = filename;
129 if (__builtin_strcmp (p, "/proc.c") == 0)
131 if (__builtin_strcmp (function, "runtime_mstart") == 0)
132 return 1;
134 else if (__builtin_strcmp (p, "/proc.go") == 0)
136 if (__builtin_strcmp (function, "runtime.kickoff") == 0
137 || __builtin_strcmp (function, "runtime.main") == 0)
138 return 1;
143 return arg->index >= arg->max;
146 /* Error callback. */
148 static void
149 error_callback (void *data __attribute__ ((unused)),
150 const char *msg, int errnum)
152 if (errnum == -1)
154 /* No debug info available. Carry on as best we can. */
155 return;
157 if (errnum != 0)
158 runtime_printf ("%s errno %d\n", msg, errnum);
159 runtime_throw (msg);
162 /* Return whether we are already collecting a stack trace. This is
163 called from the signal handler. */
165 bool alreadyInCallers(void)
166 __attribute__ ((no_split_stack));
167 bool alreadyInCallers(void)
168 __asm__ (GOSYM_PREFIX "runtime.alreadyInCallers");
170 bool
171 alreadyInCallers()
173 return runtime_atomicload(&runtime_in_callers) > 0;
176 /* Gather caller PC's. */
178 int32
179 runtime_callers (int32 skip, Location *locbuf, int32 m, bool keep_thunks)
181 struct callers_data data;
183 data.locbuf = locbuf;
184 data.skip = skip + 1;
185 data.index = 0;
186 data.max = m;
187 data.keep_thunks = keep_thunks;
188 runtime_xadd (&runtime_in_callers, 1);
189 backtrace_full (__go_get_backtrace_state (), 0, callback, error_callback,
190 &data);
191 runtime_xadd (&runtime_in_callers, -1);
193 /* For some reason GCC sometimes loses the name of a thunk function
194 at the top of the stack. If we are skipping thunks, skip that
195 one too. */
196 if (!keep_thunks
197 && data.index > 2
198 && locbuf[data.index - 2].function.len == 0
199 && locbuf[data.index - 1].function.str != NULL
200 && __builtin_strcmp ((const char *) locbuf[data.index - 1].function.str,
201 "runtime.kickoff") == 0)
203 locbuf[data.index - 2] = locbuf[data.index - 1];
204 --data.index;
207 return data.index;
210 int Callers (int, struct __go_open_array)
211 __asm__ (GOSYM_PREFIX "runtime.Callers");
214 Callers (int skip, struct __go_open_array pc)
216 Location *locbuf;
217 int ret;
218 int i;
220 /* Note that calling mallocgc here assumes that we are not going to
221 store any allocated Go pointers in the slice. */
222 locbuf = (Location *) runtime_mallocgc (pc.__count * sizeof (Location),
223 nil, false);
225 /* In the Go 1 release runtime.Callers has an off-by-one error,
226 which we can not correct because it would break backward
227 compatibility. Normally we would add 1 to SKIP here, but we
228 don't so that we are compatible. */
229 ret = runtime_callers (skip, locbuf, pc.__count, false);
231 for (i = 0; i < ret; i++)
232 ((uintptr *) pc.__values)[i] = locbuf[i].pc;
234 return ret;