1 /* go-caller.c -- look up function/file/line/entry info
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 uint32 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 ()
90 /* We may not have a g here, so we can't use runtime_lock. */
92 while (!__atomic_compare_exchange_n (&back_state_lock
, &set
, 1, false, __ATOMIC_ACQUIRE
, __ATOMIC_RELAXED
))
97 if (back_state
== NULL
)
100 const char *filename
;
103 args
= runtime_get_args();
105 if (args
.__count
> 0)
106 filename
= (const char*)((String
*)args
.__values
)[0].str
;
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
)
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
119 if (stat (filename
, &s
) < 0 || s
.st_size
< 1024)
122 back_state
= backtrace_create_state (filename
, 1, error_callback
, NULL
);
124 __atomic_store_n (&back_state_lock
, 0, __ATOMIC_RELEASE
);
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
133 __go_file_line (uintptr pc
, int index
, String
*fn
, String
*file
, intgo
*line
)
137 runtime_memclr (&c
, sizeof c
);
139 backtrace_pcinfo (__go_get_backtrace_state (), pc
, callback
,
144 return c
.file
.len
> 0;
147 /* Collect symbol information. */
150 syminfo_callback (void *data
, uintptr_t pc
__attribute__ ((unused
)),
151 const char *symname
__attribute__ ((unused
)),
152 uintptr_t address
, uintptr_t size
__attribute__ ((unused
)))
154 uintptr_t *pval
= (uintptr_t *) data
;
159 /* Set *VAL to the value of the symbol for PC. */
162 __go_symbol_value (uintptr pc
, uintptr
*val
)
165 backtrace_syminfo (__go_get_backtrace_state (), pc
, syminfo_callback
,
166 error_callback
, val
);
170 /* The values returned by runtime.Caller. */
180 struct caller_ret
Caller (int n
) __asm__ (GOSYM_PREFIX
"runtime.Caller");
182 /* Implement runtime.Caller. */
187 struct caller_ret ret
;
191 runtime_memclr (&ret
, sizeof ret
);
192 n
= runtime_callers (skip
+ 1, &loc
, 1, false);
193 if (n
< 1 || loc
.pc
== 0)
196 ret
.file
= loc
.filename
;
197 ret
.line
= loc
.lineno
;
202 /* Look up the function name, file name, and line number for a PC. */
204 struct funcfileline_return
211 struct funcfileline_return
212 runtime_funcfileline (uintptr targetpc
, int32 index
)
213 __asm__ (GOSYM_PREFIX
"runtime.funcfileline");
215 struct funcfileline_return
216 runtime_funcfileline (uintptr targetpc
, int32 index
)
218 struct funcfileline_return ret
;
220 if (!__go_file_line (targetpc
, index
, &ret
.retfn
, &ret
.retfile
,
222 runtime_memclr (&ret
, sizeof ret
);
226 /* Return the entry point of a function. */
227 uintptr
runtime_funcentry(uintptr
)
228 __asm__ (GOSYM_PREFIX
"runtime.funcentry");
231 runtime_funcentry (uintptr pc
)
235 if (!__go_symbol_value (pc
, &val
))