fileline.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* fileline.c -- Get file and line number information in a backtrace.
  2. Copyright (C) 2012-2021 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #if defined (HAVE_KERN_PROC_ARGS) || defined (HAVE_KERN_PROC)
  35. #include <sys/sysctl.h>
  36. #endif
  37. #ifdef HAVE_MACH_O_DYLD_H
  38. #include <mach-o/dyld.h>
  39. #endif
  40. #include "backtrace.h"
  41. #include "internal.h"
  42. #ifndef HAVE_GETEXECNAME
  43. #define getexecname() NULL
  44. #endif
  45. #if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC)
  46. #define sysctl_exec_name1(state, error_callback, data) NULL
  47. #define sysctl_exec_name2(state, error_callback, data) NULL
  48. #else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
  49. static char *
  50. sysctl_exec_name (struct backtrace_state *state,
  51. int mib0, int mib1, int mib2, int mib3,
  52. backtrace_error_callback error_callback, void *data)
  53. {
  54. int mib[4];
  55. size_t len;
  56. char *name;
  57. size_t rlen;
  58. mib[0] = mib0;
  59. mib[1] = mib1;
  60. mib[2] = mib2;
  61. mib[3] = mib3;
  62. if (sysctl (mib, 4, NULL, &len, NULL, 0) < 0)
  63. return NULL;
  64. name = (char *) backtrace_alloc (state, len, error_callback, data);
  65. if (name == NULL)
  66. return NULL;
  67. rlen = len;
  68. if (sysctl (mib, 4, name, &rlen, NULL, 0) < 0)
  69. {
  70. backtrace_free (state, name, len, error_callback, data);
  71. return NULL;
  72. }
  73. return name;
  74. }
  75. #ifdef HAVE_KERN_PROC_ARGS
  76. static char *
  77. sysctl_exec_name1 (struct backtrace_state *state,
  78. backtrace_error_callback error_callback, void *data)
  79. {
  80. /* This variant is used on NetBSD. */
  81. return sysctl_exec_name (state, CTL_KERN, KERN_PROC_ARGS, -1,
  82. KERN_PROC_PATHNAME, error_callback, data);
  83. }
  84. #else
  85. #define sysctl_exec_name1(state, error_callback, data) NULL
  86. #endif
  87. #ifdef HAVE_KERN_PROC
  88. static char *
  89. sysctl_exec_name2 (struct backtrace_state *state,
  90. backtrace_error_callback error_callback, void *data)
  91. {
  92. /* This variant is used on FreeBSD. */
  93. return sysctl_exec_name (state, CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,
  94. error_callback, data);
  95. }
  96. #else
  97. #define sysctl_exec_name2(state, error_callback, data) NULL
  98. #endif
  99. #endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
  100. #ifdef HAVE_MACH_O_DYLD_H
  101. static char *
  102. macho_get_executable_path (struct backtrace_state *state,
  103. backtrace_error_callback error_callback, void *data)
  104. {
  105. uint32_t len;
  106. char *name;
  107. len = 0;
  108. if (_NSGetExecutablePath (NULL, &len) == 0)
  109. return NULL;
  110. name = (char *) backtrace_alloc (state, len, error_callback, data);
  111. if (name == NULL)
  112. return NULL;
  113. if (_NSGetExecutablePath (name, &len) != 0)
  114. {
  115. backtrace_free (state, name, len, error_callback, data);
  116. return NULL;
  117. }
  118. return name;
  119. }
  120. #else /* !defined (HAVE_MACH_O_DYLD_H) */
  121. #define macho_get_executable_path(state, error_callback, data) NULL
  122. #endif /* !defined (HAVE_MACH_O_DYLD_H) */
  123. /* Initialize the fileline information from the executable. Returns 1
  124. on success, 0 on failure. */
  125. static int
  126. fileline_initialize (struct backtrace_state *state,
  127. backtrace_error_callback error_callback, void *data)
  128. {
  129. int failed;
  130. fileline fileline_fn;
  131. int pass;
  132. int called_error_callback;
  133. int descriptor;
  134. const char *filename;
  135. char buf[64];
  136. if (!state->threaded)
  137. failed = state->fileline_initialization_failed;
  138. else
  139. failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);
  140. if (failed)
  141. {
  142. error_callback (data, "failed to read executable information", -1);
  143. return 0;
  144. }
  145. if (!state->threaded)
  146. fileline_fn = state->fileline_fn;
  147. else
  148. fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  149. if (fileline_fn != NULL)
  150. return 1;
  151. /* We have not initialized the information. Do it now. */
  152. descriptor = -1;
  153. called_error_callback = 0;
  154. for (pass = 0; pass < 8; ++pass)
  155. {
  156. int does_not_exist;
  157. switch (pass)
  158. {
  159. case 0:
  160. filename = state->filename;
  161. break;
  162. case 1:
  163. filename = getexecname ();
  164. break;
  165. case 2:
  166. filename = "/proc/self/exe";
  167. break;
  168. case 3:
  169. filename = "/proc/curproc/file";
  170. break;
  171. case 4:
  172. snprintf (buf, sizeof (buf), "/proc/%ld/object/a.out",
  173. (long) getpid ());
  174. filename = buf;
  175. break;
  176. case 5:
  177. filename = sysctl_exec_name1 (state, error_callback, data);
  178. break;
  179. case 6:
  180. filename = sysctl_exec_name2 (state, error_callback, data);
  181. break;
  182. case 7:
  183. filename = macho_get_executable_path (state, error_callback, data);
  184. break;
  185. default:
  186. abort ();
  187. }
  188. if (filename == NULL)
  189. continue;
  190. descriptor = backtrace_open (filename, error_callback, data,
  191. &does_not_exist);
  192. if (descriptor < 0 && !does_not_exist)
  193. {
  194. called_error_callback = 1;
  195. break;
  196. }
  197. if (descriptor >= 0)
  198. break;
  199. }
  200. if (descriptor < 0)
  201. {
  202. if (!called_error_callback)
  203. {
  204. if (state->filename != NULL)
  205. error_callback (data, state->filename, ENOENT);
  206. else
  207. error_callback (data,
  208. "libbacktrace could not find executable to open",
  209. 0);
  210. }
  211. failed = 1;
  212. }
  213. if (!failed)
  214. {
  215. if (!backtrace_initialize (state, filename, descriptor, error_callback,
  216. data, &fileline_fn))
  217. failed = 1;
  218. }
  219. if (failed)
  220. {
  221. if (!state->threaded)
  222. state->fileline_initialization_failed = 1;
  223. else
  224. backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);
  225. return 0;
  226. }
  227. if (!state->threaded)
  228. state->fileline_fn = fileline_fn;
  229. else
  230. {
  231. backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);
  232. /* Note that if two threads initialize at once, one of the data
  233. sets may be leaked. */
  234. }
  235. return 1;
  236. }
  237. /* Given a PC, find the file name, line number, and function name. */
  238. int
  239. backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
  240. backtrace_full_callback callback,
  241. backtrace_error_callback error_callback, void *data)
  242. {
  243. if (!fileline_initialize (state, error_callback, data))
  244. return 0;
  245. if (state->fileline_initialization_failed)
  246. return 0;
  247. return state->fileline_fn (state, pc, callback, error_callback, data);
  248. }
  249. /* Given a PC, find the symbol for it, and its value. */
  250. int
  251. backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
  252. backtrace_syminfo_callback callback,
  253. backtrace_error_callback error_callback, void *data)
  254. {
  255. if (!fileline_initialize (state, error_callback, data))
  256. return 0;
  257. if (state->fileline_initialization_failed)
  258. return 0;
  259. state->syminfo_fn (state, pc, callback, error_callback, data);
  260. return 1;
  261. }
  262. /* A backtrace_syminfo_callback that can call into a
  263. backtrace_full_callback, used when we have a symbol table but no
  264. debug info. */
  265. void
  266. backtrace_syminfo_to_full_callback (void *data, uintptr_t pc,
  267. const char *symname,
  268. uintptr_t symval ATTRIBUTE_UNUSED,
  269. uintptr_t symsize ATTRIBUTE_UNUSED)
  270. {
  271. struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;
  272. bdata->ret = bdata->full_callback (bdata->full_data, pc, NULL, 0, symname);
  273. }
  274. /* An error callback that corresponds to
  275. backtrace_syminfo_to_full_callback. */
  276. void
  277. backtrace_syminfo_to_full_error_callback (void *data, const char *msg,
  278. int errnum)
  279. {
  280. struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;
  281. bdata->full_error_callback (bdata->full_data, msg, errnum);
  282. }