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. */
10 #include <sys/types.h>
14 #include "backtrace.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. */
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. */
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
);
51 /* The error callback for backtrace_pcinfo and backtrace_syminfo. */
54 error_callback (void *data
__attribute__ ((unused
)),
55 const char *msg
, int errnum
)
60 runtime_printf ("%s errno %d\n", msg
, errnum
);
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
)
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
88 if (__builtin_strchr (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
96 if (stat (filename
, &s
) < 0 || s
.st_size
< 1024)
99 back_state
= backtrace_create_state (filename
, 1, error_callback
, NULL
);
101 runtime_unlock (&back_state_lock
);
105 /* Return function/file/line information for PC. */
108 __go_file_line (uintptr pc
, String
*fn
, String
*file
, intgo
*line
)
112 runtime_memclr (&c
, sizeof c
);
113 backtrace_pcinfo (__go_get_backtrace_state (), pc
, callback
,
118 return c
.file
.len
> 0;
121 /* Collect symbol information. */
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
;
133 /* Set *VAL to the value of the symbol for PC. */
136 __go_symbol_value (uintptr_t pc
, uintptr_t *val
)
139 backtrace_syminfo (__go_get_backtrace_state (), pc
, syminfo_callback
,
140 error_callback
, val
);
144 /* The values returned by runtime.Caller. */
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. */
163 struct caller_ret ret
;
167 runtime_memclr (&ret
, sizeof ret
);
168 n
= runtime_callers (skip
+ 1, &loc
, 1, false);
169 if (n
< 1 || loc
.pc
== 0)
172 ret
.file
= loc
.filename
;
173 ret
.line
= loc
.lineno
;
178 /* Implement runtime.FuncForPC. */
181 FuncForPC (uintptr_t pc
)
189 if (!__go_file_line (pc
, &fn
, &file
, &line
))
192 ret
= (Func
*) runtime_malloc (sizeof (*ret
));
195 if (__go_symbol_value (pc
, &val
))
203 /* Look up the file and line information for a PC within a
206 struct funcline_go_return
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
;
222 if (!__go_file_line (targetpc
, &fn
, &ret
.retfile
, &ret
.retline
))
223 runtime_memclr (&ret
, sizeof ret
);
227 /* Return the name of a function. */
228 String
runtime_funcname_go (Func
*f
)
229 __asm__ (GOSYM_PREFIX
"runtime.funcname_go");
232 runtime_funcname_go (Func
*f
)
235 return runtime_gostringnocopy ((const byte
*) "");
239 /* Return the entry point of a function. */
240 uintptr
runtime_funcentry_go(Func
*f
)
241 __asm__ (GOSYM_PREFIX
"runtime.funcentry_go");
244 runtime_funcentry_go (Func
*f
)