go-caller.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* go-caller.c -- look up function/file/line/entry info
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. /* Implement runtime.Caller. */
  6. #include <stdint.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include "backtrace.h"
  11. #include "runtime.h"
  12. /* Get the function name, file name, and line number for a PC value.
  13. We use the backtrace library to get this. */
  14. /* Data structure to gather file/line information. */
  15. struct caller
  16. {
  17. String fn;
  18. String file;
  19. intgo line;
  20. intgo index;
  21. intgo frames;
  22. bool more;
  23. };
  24. /* Collect file/line information for a PC value. If this is called
  25. more than once, due to inlined functions, we record the number of
  26. inlined frames but return file/func/line for the last call, as
  27. that is usually the most useful one. */
  28. static int
  29. callback (void *data, uintptr_t pc __attribute__ ((unused)),
  30. const char *filename, int lineno, const char *function)
  31. {
  32. struct caller *c = (struct caller *) data;
  33. /* We want to make sure we return at least one frame. If we already
  34. have at least one frame, see if we should skip this one. */
  35. if (c->frames > 0
  36. && function != NULL
  37. && runtime_skipInCallback (function, NULL))
  38. return 0;
  39. /* If we already have a frame, don't increment frames if we should
  40. skip that one. */
  41. if (c->frames == 0
  42. || c->fn.len == 0
  43. || !runtime_skipInCallback ((const char *) c->fn.str, NULL))
  44. c->frames++;
  45. /* The libbacktrace library says that these strings might disappear,
  46. but with the current implementation they won't. We can't easily
  47. allocate memory here, so for now assume that we can save a
  48. pointer to the strings. */
  49. c->fn = runtime_gostringnocopy ((const byte *) function);
  50. c->file = runtime_gostringnocopy ((const byte *) filename);
  51. c->line = lineno;
  52. if (c->index == 0)
  53. {
  54. /* If we should skip the frame we have, then see if we can get
  55. another one. */
  56. if (c->fn.len > 0
  57. && runtime_skipInCallback((const char *) c->fn.str, NULL))
  58. return 0;
  59. return 1;
  60. }
  61. if (c->index > 0)
  62. --c->index;
  63. return 0;
  64. }
  65. /* The error callback for backtrace_pcinfo and backtrace_syminfo. */
  66. static void
  67. error_callback (void *data __attribute__ ((unused)),
  68. const char *msg, int errnum)
  69. {
  70. if (errnum == -1)
  71. return;
  72. if (errnum > 0)
  73. runtime_printf ("%s errno %d\n", msg, errnum);
  74. runtime_throw (msg);
  75. }
  76. /* The backtrace library state. */
  77. static void *back_state;
  78. /* A lock to control creating back_state. */
  79. static uint32 back_state_lock;
  80. /* The program arguments. */
  81. extern Slice runtime_get_args(void);
  82. /* Fetch back_state, creating it if necessary. */
  83. struct backtrace_state *
  84. __go_get_backtrace_state ()
  85. {
  86. uint32 set;
  87. /* We may not have a g here, so we can't use runtime_lock. */
  88. set = 0;
  89. while (!__atomic_compare_exchange_n (&back_state_lock, &set, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
  90. {
  91. runtime_osyield ();
  92. set = 0;
  93. }
  94. if (back_state == NULL)
  95. {
  96. Slice args;
  97. const char *filename;
  98. struct stat s;
  99. args = runtime_get_args();
  100. filename = NULL;
  101. if (args.__count > 0)
  102. filename = (const char*)((String*)args.__values)[0].str;
  103. /* If there is no '/' in FILENAME, it was found on PATH, and
  104. might not be the same as the file with the same name in the
  105. current directory. */
  106. if (filename != NULL && __builtin_strchr (filename, '/') == NULL)
  107. filename = NULL;
  108. /* If the file is small, then it's not the real executable.
  109. This is specifically to deal with Docker, which uses a bogus
  110. argv[0] (http://gcc.gnu.org/PR61895). It would be nice to
  111. have a better check for whether this file is the real
  112. executable. */
  113. if (filename != NULL && (stat (filename, &s) < 0 || s.st_size < 1024))
  114. filename = NULL;
  115. back_state = backtrace_create_state (filename, 1, error_callback, NULL);
  116. }
  117. __atomic_store_n (&back_state_lock, 0, __ATOMIC_RELEASE);
  118. return back_state;
  119. }
  120. /* Return function/file/line/nframes information for PC. The index
  121. parameter is the entry on the stack of inlined functions; -1 means
  122. the last one, with *nframes set to the count of inlined frames for
  123. this PC. If index is not -1, more is whether there are more frames
  124. after this one. */
  125. static _Bool
  126. __go_file_line (uintptr pc, int index, bool more, String *fn, String *file, intgo *line, intgo *nframes)
  127. {
  128. struct caller c;
  129. struct backtrace_state *state;
  130. runtime_memclr (&c, sizeof c);
  131. c.index = index;
  132. c.more = more;
  133. c.frames = 0;
  134. runtime_xadd (&__go_runtime_in_callers, 1);
  135. state = __go_get_backtrace_state ();
  136. runtime_xadd (&__go_runtime_in_callers, -1);
  137. backtrace_pcinfo (state, pc, callback, error_callback, &c);
  138. *fn = c.fn;
  139. *file = c.file;
  140. *line = c.line;
  141. *nframes = c.frames;
  142. // If backtrace_pcinfo didn't get the function name from the debug
  143. // info, try to get it from the symbol table.
  144. if (fn->len == 0)
  145. backtrace_syminfo (state, pc, __go_syminfo_fnname_callback,
  146. error_callback, fn);
  147. return c.file.len > 0;
  148. }
  149. /* Collect symbol information. */
  150. static void
  151. syminfo_callback (void *data, uintptr_t pc __attribute__ ((unused)),
  152. const char *symname __attribute__ ((unused)),
  153. uintptr_t address, uintptr_t size __attribute__ ((unused)))
  154. {
  155. uintptr_t *pval = (uintptr_t *) data;
  156. *pval = address;
  157. }
  158. /* Set *VAL to the value of the symbol for PC. */
  159. static _Bool
  160. __go_symbol_value (uintptr pc, uintptr *val)
  161. {
  162. struct backtrace_state *state;
  163. *val = 0;
  164. runtime_xadd (&__go_runtime_in_callers, 1);
  165. state = __go_get_backtrace_state ();
  166. runtime_xadd (&__go_runtime_in_callers, -1);
  167. backtrace_syminfo (state, pc, syminfo_callback,
  168. error_callback, val);
  169. return *val != 0;
  170. }
  171. /* The values returned by runtime.Caller. */
  172. struct caller_ret
  173. {
  174. uintptr_t pc;
  175. String file;
  176. intgo line;
  177. _Bool ok;
  178. };
  179. struct caller_ret Caller (intgo n) __asm__ (GOSYM_PREFIX "runtime.Caller");
  180. /* Implement runtime.Caller. */
  181. struct caller_ret
  182. Caller (intgo skip)
  183. {
  184. struct caller_ret ret;
  185. Location loc;
  186. int32 n;
  187. runtime_memclr (&ret, sizeof ret);
  188. n = runtime_callers (skip + 1, &loc, 1, false);
  189. if (n < 1 || loc.pc == 0)
  190. return ret;
  191. ret.pc = loc.pc;
  192. ret.file = loc.filename;
  193. ret.line = loc.lineno;
  194. ret.ok = 1;
  195. return ret;
  196. }
  197. /* Look up the function name, file name, and line number for a PC. */
  198. struct funcfileline_return
  199. runtime_funcfileline (uintptr targetpc, int32 index, bool more)
  200. {
  201. struct funcfileline_return ret;
  202. if (!__go_file_line (targetpc, index, more, &ret.retfn, &ret.retfile,
  203. &ret.retline, &ret.retframes))
  204. runtime_memclr (&ret, sizeof ret);
  205. return ret;
  206. }
  207. /* Return the entry point of a function. */
  208. uintptr runtime_funcentry(uintptr)
  209. __asm__ (GOSYM_PREFIX "runtime.funcentry");
  210. uintptr
  211. runtime_funcentry (uintptr pc)
  212. {
  213. uintptr val;
  214. if (!__go_symbol_value (pc, &val))
  215. return 0;
  216. return val;
  217. }