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. */
12 #include "go-string.h"
14 /* Get the function name, file name, and line number for a PC value.
15 We use the DWARF debug information to get this. Rather than write
16 a whole new library in C, we use the existing Go library.
17 Unfortunately, the Go library is only available if the debug/elf
18 package is imported (we use debug/elf for both ELF and Mach-O, in
19 this case). We arrange for the debug/elf package to register
20 itself, and tweak the various packages that need this information
21 to import debug/elf where possible. */
23 /* The function that returns function/file/line information. */
25 typedef _Bool (*infofn_type
) (uintptr_t, struct __go_string
*,
26 struct __go_string
*, int *);
27 static infofn_type infofn
;
29 /* The function that returns the value of a symbol, used to get the
30 entry address of a function. */
32 typedef _Bool (*symvalfn_type
) (struct __go_string
, uintptr_t *);
33 static symvalfn_type symvalfn
;
35 /* This is called by debug/elf to register the function that returns
36 function/file/line information. */
38 void RegisterDebugLookup (infofn_type
, symvalfn_type
)
39 __asm__ ("runtime.RegisterDebugLookup");
42 RegisterDebugLookup (infofn_type pi
, symvalfn_type ps
)
48 /* Return function/file/line information for PC. */
51 __go_file_line (uintptr_t pc
, struct __go_string
*fn
, struct __go_string
*file
,
56 return infofn (pc
, fn
, file
, line
);
59 /* Return the value of a symbol. */
62 __go_symbol_value (struct __go_string sym
, uintptr_t *val
)
66 return symvalfn (sym
, val
);
69 /* The values returned by runtime.Caller. */
74 struct __go_string file
;
79 struct caller_ret
Caller (int n
) asm ("runtime.Caller");
81 Func
*FuncForPC (uintptr_t) asm ("runtime.FuncForPC");
83 /* Implement runtime.Caller. */
88 struct caller_ret ret
;
91 struct __go_string fn
;
93 runtime_memclr (&ret
, sizeof ret
);
94 n
= runtime_callers (skip
+ 1, &pc
, 1);
98 ret
.ok
= __go_file_line (pc
, &fn
, &ret
.file
, &ret
.line
);
102 /* Implement runtime.FuncForPC. */
105 FuncForPC (uintptr_t pc
)
108 struct __go_string fn
;
109 struct __go_string file
;
113 if (!__go_file_line (pc
, &fn
, &file
, &line
))
115 if (!__go_symbol_value (fn
, &val
))
118 ret
= (Func
*) runtime_malloc (sizeof (*ret
));
124 /* Look up the file and line information for a PC within a
127 struct funcline_go_return
129 struct __go_string retfile
;
133 struct funcline_go_return
134 runtime_funcline_go (Func
*f
, uintptr targetpc
)
135 __asm__ ("runtime.funcline_go");
137 struct funcline_go_return
138 runtime_funcline_go (Func
*f
__attribute__((unused
)), uintptr targetpc
)
140 struct funcline_go_return ret
;
141 struct __go_string fn
;
143 if (!__go_file_line (targetpc
, &fn
, &ret
.retfile
, &ret
.retline
))
144 runtime_memclr (&ret
, sizeof ret
);