go-caller.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* go-caller.c -- runtime.Caller and runtime.FuncForPC for Go.
  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. };
  21. /* Collect file/line information for a PC value. If this is called
  22. more than once, due to inlined functions, we use the last call, as
  23. that is usually the most useful one. */
  24. static int
  25. callback (void *data, uintptr_t pc __attribute__ ((unused)),
  26. const char *filename, int lineno, const char *function)
  27. {
  28. struct caller *c = (struct caller *) data;
  29. /* The libbacktrace library says that these strings might disappear,
  30. but with the current implementation they won't. We can't easily
  31. allocate memory here, so for now assume that we can save a
  32. pointer to the strings. */
  33. c->fn = runtime_gostringnocopy ((const byte *) function);
  34. c->file = runtime_gostringnocopy ((const byte *) filename);
  35. c->line = lineno;
  36. return 0;
  37. }
  38. /* The error callback for backtrace_pcinfo and backtrace_syminfo. */
  39. static void
  40. error_callback (void *data __attribute__ ((unused)),
  41. const char *msg, int errnum)
  42. {
  43. if (errnum == -1)
  44. return;
  45. if (errnum > 0)
  46. runtime_printf ("%s errno %d\n", msg, errnum);
  47. runtime_throw (msg);
  48. }
  49. /* The backtrace library state. */
  50. static void *back_state;
  51. /* A lock to control creating back_state. */
  52. static Lock back_state_lock;
  53. /* Fetch back_state, creating it if necessary. */
  54. struct backtrace_state *
  55. __go_get_backtrace_state ()
  56. {
  57. runtime_lock (&back_state_lock);
  58. if (back_state == NULL)
  59. {
  60. const char *filename;
  61. struct stat s;
  62. filename = (const char *) runtime_progname ();
  63. /* If there is no '/' in FILENAME, it was found on PATH, and
  64. might not be the same as the file with the same name in the
  65. current directory. */
  66. if (__builtin_strchr (filename, '/') == NULL)
  67. filename = NULL;
  68. /* If the file is small, then it's not the real executable.
  69. This is specifically to deal with Docker, which uses a bogus
  70. argv[0] (http://gcc.gnu.org/PR61895). It would be nice to
  71. have a better check for whether this file is the real
  72. executable. */
  73. if (stat (filename, &s) < 0 || s.st_size < 1024)
  74. filename = NULL;
  75. back_state = backtrace_create_state (filename, 1, error_callback, NULL);
  76. }
  77. runtime_unlock (&back_state_lock);
  78. return back_state;
  79. }
  80. /* Return function/file/line information for PC. */
  81. _Bool
  82. __go_file_line (uintptr pc, String *fn, String *file, intgo *line)
  83. {
  84. struct caller c;
  85. runtime_memclr (&c, sizeof c);
  86. backtrace_pcinfo (__go_get_backtrace_state (), pc, callback,
  87. error_callback, &c);
  88. *fn = c.fn;
  89. *file = c.file;
  90. *line = c.line;
  91. return c.file.len > 0;
  92. }
  93. /* Collect symbol information. */
  94. static void
  95. syminfo_callback (void *data, uintptr_t pc __attribute__ ((unused)),
  96. const char *symname __attribute__ ((unused)),
  97. uintptr_t address, uintptr_t size __attribute__ ((unused)))
  98. {
  99. uintptr_t *pval = (uintptr_t *) data;
  100. *pval = address;
  101. }
  102. /* Set *VAL to the value of the symbol for PC. */
  103. static _Bool
  104. __go_symbol_value (uintptr_t pc, uintptr_t *val)
  105. {
  106. *val = 0;
  107. backtrace_syminfo (__go_get_backtrace_state (), pc, syminfo_callback,
  108. error_callback, val);
  109. return *val != 0;
  110. }
  111. /* The values returned by runtime.Caller. */
  112. struct caller_ret
  113. {
  114. uintptr_t pc;
  115. String file;
  116. intgo line;
  117. _Bool ok;
  118. };
  119. struct caller_ret Caller (int n) __asm__ (GOSYM_PREFIX "runtime.Caller");
  120. Func *FuncForPC (uintptr_t) __asm__ (GOSYM_PREFIX "runtime.FuncForPC");
  121. /* Implement runtime.Caller. */
  122. struct caller_ret
  123. Caller (int skip)
  124. {
  125. struct caller_ret ret;
  126. Location loc;
  127. int32 n;
  128. runtime_memclr (&ret, sizeof ret);
  129. n = runtime_callers (skip + 1, &loc, 1, false);
  130. if (n < 1 || loc.pc == 0)
  131. return ret;
  132. ret.pc = loc.pc;
  133. ret.file = loc.filename;
  134. ret.line = loc.lineno;
  135. ret.ok = 1;
  136. return ret;
  137. }
  138. /* Implement runtime.FuncForPC. */
  139. Func *
  140. FuncForPC (uintptr_t pc)
  141. {
  142. Func *ret;
  143. String fn;
  144. String file;
  145. intgo line;
  146. uintptr_t val;
  147. if (!__go_file_line (pc, &fn, &file, &line))
  148. return NULL;
  149. ret = (Func *) runtime_malloc (sizeof (*ret));
  150. ret->name = fn;
  151. if (__go_symbol_value (pc, &val))
  152. ret->entry = val;
  153. else
  154. ret->entry = 0;
  155. return ret;
  156. }
  157. /* Look up the file and line information for a PC within a
  158. function. */
  159. struct funcline_go_return
  160. {
  161. String retfile;
  162. intgo retline;
  163. };
  164. struct funcline_go_return
  165. runtime_funcline_go (Func *f, uintptr targetpc)
  166. __asm__ (GOSYM_PREFIX "runtime.funcline_go");
  167. struct funcline_go_return
  168. runtime_funcline_go (Func *f __attribute__((unused)), uintptr targetpc)
  169. {
  170. struct funcline_go_return ret;
  171. String fn;
  172. if (!__go_file_line (targetpc, &fn, &ret.retfile, &ret.retline))
  173. runtime_memclr (&ret, sizeof ret);
  174. return ret;
  175. }
  176. /* Return the name of a function. */
  177. String runtime_funcname_go (Func *f)
  178. __asm__ (GOSYM_PREFIX "runtime.funcname_go");
  179. String
  180. runtime_funcname_go (Func *f)
  181. {
  182. if (f == NULL)
  183. return runtime_gostringnocopy ((const byte *) "");
  184. return f->name;
  185. }
  186. /* Return the entry point of a function. */
  187. uintptr runtime_funcentry_go(Func *f)
  188. __asm__ (GOSYM_PREFIX "runtime.funcentry_go");
  189. uintptr
  190. runtime_funcentry_go (Func *f)
  191. {
  192. return f->entry;
  193. }