2016-07-13 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / libgo / runtime / go-caller.c
blobd6901e07377c5c68dea043dbb872dd363865ee57
1 /* go-caller.c -- runtime.Caller and runtime.FuncForPC for Go.
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. */
7 /* Implement runtime.Caller. */
9 #include <stdint.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
14 #include "backtrace.h"
16 #include "runtime.h"
18 /* Get the function name, file name, and line number for a PC value.
19 We use the backtrace library to get this. */
21 /* Data structure to gather file/line information. */
23 struct caller
25 String fn;
26 String file;
27 intgo line;
30 /* Collect file/line information for a PC value. If this is called
31 more than once, due to inlined functions, we use the last call, as
32 that is usually the most useful one. */
34 static int
35 callback (void *data, uintptr_t pc __attribute__ ((unused)),
36 const char *filename, int lineno, const char *function)
38 struct caller *c = (struct caller *) data;
40 /* The libbacktrace library says that these strings might disappear,
41 but with the current implementation they won't. We can't easily
42 allocate memory here, so for now assume that we can save a
43 pointer to the strings. */
44 c->fn = runtime_gostringnocopy ((const byte *) function);
45 c->file = runtime_gostringnocopy ((const byte *) filename);
46 c->line = lineno;
48 return 0;
51 /* The error callback for backtrace_pcinfo and backtrace_syminfo. */
53 static void
54 error_callback (void *data __attribute__ ((unused)),
55 const char *msg, int errnum)
57 if (errnum == -1)
58 return;
59 if (errnum > 0)
60 runtime_printf ("%s errno %d\n", msg, errnum);
61 runtime_throw (msg);
64 /* The backtrace library state. */
66 static void *back_state;
68 /* A lock to control creating back_state. */
70 static Lock back_state_lock;
72 /* Fetch back_state, creating it if necessary. */
74 struct backtrace_state *
75 __go_get_backtrace_state ()
77 runtime_lock (&back_state_lock);
78 if (back_state == NULL)
80 const char *filename;
81 struct stat s;
83 filename = (const char *) runtime_progname ();
85 /* If there is no '/' in FILENAME, it was found on PATH, and
86 might not be the same as the file with the same name in the
87 current directory. */
88 if (__builtin_strchr (filename, '/') == NULL)
89 filename = NULL;
91 /* If the file is small, then it's not the real executable.
92 This is specifically to deal with Docker, which uses a bogus
93 argv[0] (http://gcc.gnu.org/PR61895). It would be nice to
94 have a better check for whether this file is the real
95 executable. */
96 if (stat (filename, &s) < 0 || s.st_size < 1024)
97 filename = NULL;
99 back_state = backtrace_create_state (filename, 1, error_callback, NULL);
101 runtime_unlock (&back_state_lock);
102 return back_state;
105 /* Return function/file/line information for PC. */
107 _Bool
108 __go_file_line (uintptr pc, String *fn, String *file, intgo *line)
110 struct caller c;
112 runtime_memclr (&c, sizeof c);
113 backtrace_pcinfo (__go_get_backtrace_state (), pc, callback,
114 error_callback, &c);
115 *fn = c.fn;
116 *file = c.file;
117 *line = c.line;
118 return c.file.len > 0;
121 /* Collect symbol information. */
123 static void
124 syminfo_callback (void *data, uintptr_t pc __attribute__ ((unused)),
125 const char *symname __attribute__ ((unused)),
126 uintptr_t address, uintptr_t size __attribute__ ((unused)))
128 uintptr_t *pval = (uintptr_t *) data;
130 *pval = address;
133 /* Set *VAL to the value of the symbol for PC. */
135 static _Bool
136 __go_symbol_value (uintptr_t pc, uintptr_t *val)
138 *val = 0;
139 backtrace_syminfo (__go_get_backtrace_state (), pc, syminfo_callback,
140 error_callback, val);
141 return *val != 0;
144 /* The values returned by runtime.Caller. */
146 struct caller_ret
148 uintptr_t pc;
149 String file;
150 intgo line;
151 _Bool ok;
154 struct caller_ret Caller (int n) __asm__ (GOSYM_PREFIX "runtime.Caller");
156 Func *FuncForPC (uintptr_t) __asm__ (GOSYM_PREFIX "runtime.FuncForPC");
158 /* Implement runtime.Caller. */
160 struct caller_ret
161 Caller (int skip)
163 struct caller_ret ret;
164 Location loc;
165 int32 n;
167 runtime_memclr (&ret, sizeof ret);
168 n = runtime_callers (skip + 1, &loc, 1, false);
169 if (n < 1 || loc.pc == 0)
170 return ret;
171 ret.pc = loc.pc;
172 ret.file = loc.filename;
173 ret.line = loc.lineno;
174 ret.ok = 1;
175 return ret;
178 /* Implement runtime.FuncForPC. */
180 Func *
181 FuncForPC (uintptr_t pc)
183 Func *ret;
184 String fn;
185 String file;
186 intgo line;
187 uintptr_t val;
189 if (!__go_file_line (pc, &fn, &file, &line))
190 return NULL;
192 ret = (Func *) runtime_malloc (sizeof (*ret));
193 ret->name = fn;
195 if (__go_symbol_value (pc, &val))
196 ret->entry = val;
197 else
198 ret->entry = 0;
200 return ret;
203 /* Look up the file and line information for a PC within a
204 function. */
206 struct funcline_go_return
208 String retfile;
209 intgo retline;
212 struct funcline_go_return
213 runtime_funcline_go (Func *f, uintptr targetpc)
214 __asm__ (GOSYM_PREFIX "runtime.funcline_go");
216 struct funcline_go_return
217 runtime_funcline_go (Func *f __attribute__((unused)), uintptr targetpc)
219 struct funcline_go_return ret;
220 String fn;
222 if (!__go_file_line (targetpc, &fn, &ret.retfile, &ret.retline))
223 runtime_memclr (&ret, sizeof ret);
224 return ret;
227 /* Return the name of a function. */
228 String runtime_funcname_go (Func *f)
229 __asm__ (GOSYM_PREFIX "runtime.funcname_go");
231 String
232 runtime_funcname_go (Func *f)
234 if (f == NULL)
235 return runtime_gostringnocopy ((const byte *) "");
236 return f->name;
239 /* Return the entry point of a function. */
240 uintptr runtime_funcentry_go(Func *f)
241 __asm__ (GOSYM_PREFIX "runtime.funcentry_go");
243 uintptr
244 runtime_funcentry_go (Func *f)
246 return f->entry;