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. */
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. */
36 callback (void *data
, uintptr_t pc
__attribute__ ((unused
)),
37 const char *filename
, int lineno
, const char *function
)
39 struct caller
*c
= (struct caller
*) data
;
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
);
58 /* The error callback for backtrace_pcinfo and backtrace_syminfo. */
61 error_callback (void *data
__attribute__ ((unused
)),
62 const char *msg
, int errnum
)
67 runtime_printf ("%s errno %d\n", msg
, errnum
);
71 /* The backtrace library state. */
73 static void *back_state
;
75 /* A lock to control creating back_state. */
77 static Lock back_state_lock
;
79 /* The program arguments. */
81 extern Slice
runtime_get_args(void);
83 /* Fetch back_state, creating it if necessary. */
85 struct backtrace_state
*
86 __go_get_backtrace_state ()
88 runtime_lock (&back_state_lock
);
89 if (back_state
== NULL
)
95 args
= runtime_get_args();
98 filename
= (const char*)((String
*)args
.__values
)[0].str
;
100 /* If there is no '/' in FILENAME, it was found on PATH, and
101 might not be the same as the file with the same name in the
102 current directory. */
103 if (filename
!= NULL
&& __builtin_strchr (filename
, '/') == NULL
)
106 /* If the file is small, then it's not the real executable.
107 This is specifically to deal with Docker, which uses a bogus
108 argv[0] (http://gcc.gnu.org/PR61895). It would be nice to
109 have a better check for whether this file is the real
111 if (stat (filename
, &s
) < 0 || s
.st_size
< 1024)
114 back_state
= backtrace_create_state (filename
, 1, error_callback
, NULL
);
116 runtime_unlock (&back_state_lock
);
120 /* Return function/file/line information for PC. The index parameter
121 is the entry on the stack of inlined functions; -1 means the last
125 __go_file_line (uintptr pc
, int index
, String
*fn
, String
*file
, intgo
*line
)
129 runtime_memclr (&c
, sizeof c
);
131 backtrace_pcinfo (__go_get_backtrace_state (), pc
, callback
,
136 return c
.file
.len
> 0;
139 /* Collect symbol information. */
142 syminfo_callback (void *data
, uintptr_t pc
__attribute__ ((unused
)),
143 const char *symname
__attribute__ ((unused
)),
144 uintptr_t address
, uintptr_t size
__attribute__ ((unused
)))
146 uintptr_t *pval
= (uintptr_t *) data
;
151 /* Set *VAL to the value of the symbol for PC. */
154 __go_symbol_value (uintptr_t pc
, uintptr_t *val
)
157 backtrace_syminfo (__go_get_backtrace_state (), pc
, syminfo_callback
,
158 error_callback
, val
);
162 /* The values returned by runtime.Caller. */
172 struct caller_ret
Caller (int n
) __asm__ (GOSYM_PREFIX
"runtime.Caller");
174 Func
*FuncForPC (uintptr_t) __asm__ (GOSYM_PREFIX
"runtime.FuncForPC");
176 /* Implement runtime.Caller. */
181 struct caller_ret ret
;
185 runtime_memclr (&ret
, sizeof ret
);
186 n
= runtime_callers (skip
+ 1, &loc
, 1, false);
187 if (n
< 1 || loc
.pc
== 0)
190 ret
.file
= loc
.filename
;
191 ret
.line
= loc
.lineno
;
196 /* Implement runtime.FuncForPC. */
199 FuncForPC (uintptr_t pc
)
207 if (!__go_file_line (pc
, -1, &fn
, &file
, &line
))
210 ret
= (Func
*) runtime_malloc (sizeof (*ret
));
213 if (__go_symbol_value (pc
, &val
))
221 /* Look up the file and line information for a PC within a
224 struct funcline_go_return
230 struct funcline_go_return
231 runtime_funcline_go (Func
*f
, uintptr targetpc
)
232 __asm__ (GOSYM_PREFIX
"runtime.funcline_go");
234 struct funcline_go_return
235 runtime_funcline_go (Func
*f
__attribute__((unused
)), uintptr targetpc
)
237 struct funcline_go_return ret
;
240 if (!__go_file_line (targetpc
, -1, &fn
, &ret
.retfile
, &ret
.retline
))
241 runtime_memclr (&ret
, sizeof ret
);
245 /* Return the name of a function. */
246 String
runtime_funcname_go (Func
*f
)
247 __asm__ (GOSYM_PREFIX
"runtime.funcname_go");
250 runtime_funcname_go (Func
*f
)
253 return runtime_gostringnocopy ((const byte
*) "");
257 /* Return the entry point of a function. */
258 uintptr
runtime_funcentry_go(Func
*f
)
259 __asm__ (GOSYM_PREFIX
"runtime.funcentry_go");
262 runtime_funcentry_go (Func
*f
)
267 /* Look up file and line information for Frames.Next. */
269 struct funcframe_return
276 struct funcframe_return
277 runtime_funcframe (uintptr pc
, int index
)
278 __asm__ (GOSYM_PREFIX
"runtime.funcframe");
280 struct funcframe_return
281 runtime_funcframe (uintptr pc
, int index
)
283 struct funcframe_return ret
;
288 // Subtract 1 from PC to undo the 1 we added in callback in go-callers.c.
291 if (!__go_file_line (pc
, index
, &fn
, &ret
.retfile
, &ret
.retline
))
292 runtime_memclr (&ret
, sizeof ret
);
295 func
= (Func
*) runtime_malloc (sizeof (*func
));
298 if (__go_symbol_value (pc
, &val
))