fileline.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #ifdef HAVE_WINDOWS_H
  41. #ifndef WIN32_MEAN_AND_LEAN
  42. #define WIN32_MEAN_AND_LEAN
  43. #endif
  44. #ifndef NOMINMAX
  45. #define NOMINMAX
  46. #endif
  47. #include <windows.h>
  48. #endif
  49. #include "backtrace.h"
  50. #include "internal.h"
  51. #ifndef HAVE_GETEXECNAME
  52. #define getexecname() NULL
  53. #endif
  54. #if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC)
  55. #define sysctl_exec_name1(state, error_callback, data) NULL
  56. #define sysctl_exec_name2(state, error_callback, data) NULL
  57. #else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
  58. static char *
  59. sysctl_exec_name (struct backtrace_state *state,
  60. int mib0, int mib1, int mib2, int mib3,
  61. backtrace_error_callback error_callback, void *data)
  62. {
  63. int mib[4];
  64. size_t len;
  65. char *name;
  66. size_t rlen;
  67. mib[0] = mib0;
  68. mib[1] = mib1;
  69. mib[2] = mib2;
  70. mib[3] = mib3;
  71. if (sysctl (mib, 4, NULL, &len, NULL, 0) < 0)
  72. return NULL;
  73. name = (char *) backtrace_alloc (state, len, error_callback, data);
  74. if (name == NULL)
  75. return NULL;
  76. rlen = len;
  77. if (sysctl (mib, 4, name, &rlen, NULL, 0) < 0)
  78. {
  79. backtrace_free (state, name, len, error_callback, data);
  80. return NULL;
  81. }
  82. return name;
  83. }
  84. #ifdef HAVE_KERN_PROC_ARGS
  85. static char *
  86. sysctl_exec_name1 (struct backtrace_state *state,
  87. backtrace_error_callback error_callback, void *data)
  88. {
  89. /* This variant is used on NetBSD. */
  90. return sysctl_exec_name (state, CTL_KERN, KERN_PROC_ARGS, -1,
  91. KERN_PROC_PATHNAME, error_callback, data);
  92. }
  93. #else
  94. #define sysctl_exec_name1(state, error_callback, data) NULL
  95. #endif
  96. #ifdef HAVE_KERN_PROC
  97. static char *
  98. sysctl_exec_name2 (struct backtrace_state *state,
  99. backtrace_error_callback error_callback, void *data)
  100. {
  101. /* This variant is used on FreeBSD. */
  102. return sysctl_exec_name (state, CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,
  103. error_callback, data);
  104. }
  105. #else
  106. #define sysctl_exec_name2(state, error_callback, data) NULL
  107. #endif
  108. #endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
  109. #ifdef HAVE_MACH_O_DYLD_H
  110. static char *
  111. macho_get_executable_path (struct backtrace_state *state,
  112. backtrace_error_callback error_callback, void *data)
  113. {
  114. uint32_t len;
  115. char *name;
  116. len = 0;
  117. if (_NSGetExecutablePath (NULL, &len) == 0)
  118. return NULL;
  119. name = (char *) backtrace_alloc (state, len, error_callback, data);
  120. if (name == NULL)
  121. return NULL;
  122. if (_NSGetExecutablePath (name, &len) != 0)
  123. {
  124. backtrace_free (state, name, len, error_callback, data);
  125. return NULL;
  126. }
  127. return name;
  128. }
  129. #else /* !defined (HAVE_MACH_O_DYLD_H) */
  130. #define macho_get_executable_path(state, error_callback, data) NULL
  131. #endif /* !defined (HAVE_MACH_O_DYLD_H) */
  132. #if HAVE_DECL__PGMPTR
  133. #define windows_executable_filename() _pgmptr
  134. #else /* !HAVE_DECL__PGMPTR */
  135. #define windows_executable_filename() NULL
  136. #endif /* !HAVE_DECL__PGMPTR */
  137. #ifdef HAVE_WINDOWS_H
  138. #define FILENAME_BUF_SIZE (MAX_PATH)
  139. static char *
  140. windows_get_executable_path (char *buf, backtrace_error_callback error_callback,
  141. void *data)
  142. {
  143. size_t got;
  144. int error;
  145. got = GetModuleFileNameA (NULL, buf, FILENAME_BUF_SIZE - 1);
  146. error = GetLastError ();
  147. if (got == 0
  148. || (got == FILENAME_BUF_SIZE - 1 && error == ERROR_INSUFFICIENT_BUFFER))
  149. {
  150. error_callback (data,
  151. "could not get the filename of the current executable",
  152. error);
  153. return NULL;
  154. }
  155. return buf;
  156. }
  157. #else /* !defined (HAVE_WINDOWS_H) */
  158. #define windows_get_executable_path(buf, error_callback, data) NULL
  159. #define FILENAME_BUF_SIZE 64
  160. #endif /* !defined (HAVE_WINDOWS_H) */
  161. /* Initialize the fileline information from the executable. Returns 1
  162. on success, 0 on failure. */
  163. static int
  164. fileline_initialize (struct backtrace_state *state,
  165. backtrace_error_callback error_callback, void *data)
  166. {
  167. int failed;
  168. fileline fileline_fn;
  169. int pass;
  170. int called_error_callback;
  171. int descriptor;
  172. const char *filename;
  173. char buf[FILENAME_BUF_SIZE];
  174. if (!state->threaded)
  175. failed = state->fileline_initialization_failed;
  176. else
  177. failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);
  178. if (failed)
  179. {
  180. error_callback (data, "failed to read executable information", -1);
  181. return 0;
  182. }
  183. if (!state->threaded)
  184. fileline_fn = state->fileline_fn;
  185. else
  186. fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
  187. if (fileline_fn != NULL)
  188. return 1;
  189. /* We have not initialized the information. Do it now. */
  190. descriptor = -1;
  191. called_error_callback = 0;
  192. for (pass = 0; pass < 10; ++pass)
  193. {
  194. int does_not_exist;
  195. switch (pass)
  196. {
  197. case 0:
  198. filename = state->filename;
  199. break;
  200. case 1:
  201. filename = getexecname ();
  202. break;
  203. case 2:
  204. /* Test this before /proc/self/exe, as the latter exists but points
  205. to the wine binary (and thus doesn't work). */
  206. filename = windows_executable_filename ();
  207. break;
  208. case 3:
  209. filename = "/proc/self/exe";
  210. break;
  211. case 4:
  212. filename = "/proc/curproc/file";
  213. break;
  214. case 5:
  215. snprintf (buf, sizeof (buf), "/proc/%ld/object/a.out",
  216. (long) getpid ());
  217. filename = buf;
  218. break;
  219. case 6:
  220. filename = sysctl_exec_name1 (state, error_callback, data);
  221. break;
  222. case 7:
  223. filename = sysctl_exec_name2 (state, error_callback, data);
  224. break;
  225. case 8:
  226. filename = macho_get_executable_path (state, error_callback, data);
  227. break;
  228. case 9:
  229. filename = windows_get_executable_path (buf, error_callback, data);
  230. break;
  231. default:
  232. abort ();
  233. }
  234. if (filename == NULL)
  235. continue;
  236. descriptor = backtrace_open (filename, error_callback, data,
  237. &does_not_exist);
  238. if (descriptor < 0 && !does_not_exist)
  239. {
  240. called_error_callback = 1;
  241. break;
  242. }
  243. if (descriptor >= 0)
  244. break;
  245. }
  246. if (descriptor < 0)
  247. {
  248. if (!called_error_callback)
  249. {
  250. if (state->filename != NULL)
  251. error_callback (data, state->filename, ENOENT);
  252. else
  253. error_callback (data,
  254. "libbacktrace could not find executable to open",
  255. 0);
  256. }
  257. failed = 1;
  258. }
  259. if (!failed)
  260. {
  261. if (!backtrace_initialize (state, filename, descriptor, error_callback,
  262. data, &fileline_fn))
  263. failed = 1;
  264. }
  265. if (failed)
  266. {
  267. if (!state->threaded)
  268. state->fileline_initialization_failed = 1;
  269. else
  270. backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);
  271. return 0;
  272. }
  273. if (!state->threaded)
  274. state->fileline_fn = fileline_fn;
  275. else
  276. {
  277. backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);
  278. /* Note that if two threads initialize at once, one of the data
  279. sets may be leaked. */
  280. }
  281. return 1;
  282. }
  283. /* Given a PC, find the file name, line number, and function name. */
  284. int
  285. backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
  286. backtrace_full_callback callback,
  287. backtrace_error_callback error_callback, void *data)
  288. {
  289. if (!fileline_initialize (state, error_callback, data))
  290. return 0;
  291. if (state->fileline_initialization_failed)
  292. return 0;
  293. return state->fileline_fn (state, pc, callback, error_callback, data);
  294. }
  295. /* Given a PC, find the symbol for it, and its value. */
  296. int
  297. backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
  298. backtrace_syminfo_callback callback,
  299. backtrace_error_callback error_callback, void *data)
  300. {
  301. if (!fileline_initialize (state, error_callback, data))
  302. return 0;
  303. if (state->fileline_initialization_failed)
  304. return 0;
  305. state->syminfo_fn (state, pc, callback, error_callback, data);
  306. return 1;
  307. }
  308. /* A backtrace_syminfo_callback that can call into a
  309. backtrace_full_callback, used when we have a symbol table but no
  310. debug info. */
  311. void
  312. backtrace_syminfo_to_full_callback (void *data, uintptr_t pc,
  313. const char *symname,
  314. uintptr_t symval ATTRIBUTE_UNUSED,
  315. uintptr_t symsize ATTRIBUTE_UNUSED)
  316. {
  317. struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;
  318. bdata->ret = bdata->full_callback (bdata->full_data, pc, NULL, 0, symname);
  319. }
  320. /* An error callback that corresponds to
  321. backtrace_syminfo_to_full_callback. */
  322. void
  323. backtrace_syminfo_to_full_error_callback (void *data, const char *msg,
  324. int errnum)
  325. {
  326. struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;
  327. bdata->full_error_callback (bdata->full_data, msg, errnum);
  328. }