backtrace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* Printing of backtraces and error messages
  2. * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009,
  3. * 2010, 2011, 2014 Free Software Foundation
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "libguile/_scm.h"
  26. #include <unistd.h>
  27. #ifdef HAVE_IO_H
  28. #include <io.h>
  29. #endif
  30. #include "libguile/deprecation.h"
  31. #include "libguile/stacks.h"
  32. #include "libguile/srcprop.h"
  33. #include "libguile/struct.h"
  34. #include "libguile/strports.h"
  35. #include "libguile/throw.h"
  36. #include "libguile/fluids.h"
  37. #include "libguile/ports.h"
  38. #include "libguile/strings.h"
  39. #include "libguile/dynwind.h"
  40. #include "libguile/frames.h"
  41. #include "libguile/validate.h"
  42. #include "libguile/backtrace.h"
  43. #include "libguile/filesys.h"
  44. #include "libguile/private-options.h"
  45. /* {Error reporting and backtraces}
  46. *
  47. * Note that these functions shouldn't generate errors themselves.
  48. */
  49. static SCM
  50. boot_print_exception (SCM port, SCM frame, SCM key, SCM args)
  51. #define FUNC_NAME "boot-print-exception"
  52. {
  53. scm_puts_unlocked ("Throw to key ", port);
  54. scm_write (key, port);
  55. scm_puts_unlocked (" with args ", port);
  56. scm_write (args, port);
  57. return SCM_UNSPECIFIED;
  58. }
  59. #undef FUNC_NAME
  60. static SCM print_exception_var;
  61. static void
  62. init_print_exception_var (void)
  63. {
  64. print_exception_var
  65. = scm_module_variable (scm_the_root_module (),
  66. scm_from_latin1_symbol ("print-exception"));
  67. }
  68. SCM
  69. scm_print_exception (SCM port, SCM frame, SCM key, SCM args)
  70. #define FUNC_NAME "print-exception"
  71. {
  72. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  73. scm_i_pthread_once (&once, init_print_exception_var);
  74. SCM_VALIDATE_OPOUTPORT (1, port);
  75. if (scm_is_true (frame))
  76. SCM_VALIDATE_FRAME (2, frame);
  77. SCM_VALIDATE_SYMBOL (3, key);
  78. SCM_VALIDATE_LIST (4, args);
  79. return scm_call_4 (scm_variable_ref (print_exception_var),
  80. port, frame, key, args);
  81. }
  82. #undef FUNC_NAME
  83. /* Print parameters for error messages. */
  84. #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
  85. #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
  86. /* Print parameters for failing expressions in error messages.
  87. * (See also `print_params' below for backtrace print parameters.)
  88. */
  89. #define DISPLAY_EXPRESSION_MAX_LEVEL 2
  90. #define DISPLAY_EXPRESSION_MAX_LENGTH 3
  91. #undef SCM_ASSERT
  92. #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
  93. if (!(_cond)) \
  94. return SCM_BOOL_F;
  95. void
  96. scm_display_error_message (SCM message, SCM args, SCM port)
  97. {
  98. scm_print_exception (port, SCM_BOOL_F, scm_misc_error_key,
  99. scm_list_3 (SCM_BOOL_F, message, args));
  100. }
  101. /* The function scm_i_display_error prints out a detailed error message. This
  102. * function will be called directly within libguile to signal error messages.
  103. * No parameter checks will be performed by scm_i_display_error. Thus, User
  104. * code should rather use the function scm_display_error.
  105. */
  106. void
  107. scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest)
  108. {
  109. scm_print_exception (port, frame, scm_misc_error_key,
  110. scm_list_3 (subr, message, args));
  111. }
  112. SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
  113. (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest),
  114. "Display an error message to the output port @var{port}.\n"
  115. "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
  116. "the name of the procedure in which the error occurred and\n"
  117. "@var{message} is the actual error message, which may contain\n"
  118. "formatting instructions. These will format the arguments in\n"
  119. "the list @var{args} accordingly. @var{rest} is currently\n"
  120. "ignored.")
  121. #define FUNC_NAME s_scm_display_error
  122. {
  123. SCM_VALIDATE_OUTPUT_PORT (2, port);
  124. #if SCM_ENABLE_DEPRECATED
  125. if (SCM_STACKP (frame))
  126. {
  127. scm_c_issue_deprecation_warning
  128. ("Passing a stack as the first argument to `scm_display_error' is "
  129. "deprecated. Pass a frame instead.");
  130. if (SCM_STACK_LENGTH (frame))
  131. frame = scm_stack_ref (frame, SCM_INUM0);
  132. else
  133. frame = SCM_BOOL_F;
  134. }
  135. #endif
  136. scm_i_display_error (frame, port, subr, message, args, rest);
  137. return SCM_UNSPECIFIED;
  138. }
  139. #undef FUNC_NAME
  140. typedef struct {
  141. int level;
  142. int length;
  143. } print_params_t;
  144. static int n_print_params = 9;
  145. static print_params_t default_print_params[] = {
  146. { 4, 9 }, { 4, 3 },
  147. { 3, 4 }, { 3, 3 },
  148. { 2, 4 }, { 2, 3 },
  149. { 1, 4 }, { 1, 3 }, { 1, 2 }
  150. };
  151. static print_params_t *print_params = default_print_params;
  152. #ifdef GUILE_DEBUG
  153. SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
  154. (SCM params),
  155. "Set the print parameters to the values from @var{params}.\n"
  156. "@var{params} must be a list of two-element lists which must\n"
  157. "hold two integer values.")
  158. #define FUNC_NAME s_scm_set_print_params_x
  159. {
  160. int i;
  161. int n;
  162. SCM ls;
  163. print_params_t *new_params;
  164. SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
  165. for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
  166. SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
  167. && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX)
  168. && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX),
  169. params,
  170. SCM_ARG2,
  171. s_scm_set_print_params_x);
  172. new_params = scm_malloc (n * sizeof (print_params_t));
  173. if (print_params != default_print_params)
  174. free (print_params);
  175. print_params = new_params;
  176. for (i = 0; i < n; ++i)
  177. {
  178. print_params[i].level = scm_to_int (SCM_CAAR (params));
  179. print_params[i].length = scm_to_int (SCM_CADAR (params));
  180. params = SCM_CDR (params);
  181. }
  182. n_print_params = n;
  183. return SCM_UNSPECIFIED;
  184. }
  185. #undef FUNC_NAME
  186. #endif
  187. static void
  188. indent (int n, SCM port)
  189. {
  190. int i;
  191. for (i = 0; i < n; ++i)
  192. scm_putc_unlocked (' ', port);
  193. }
  194. static void
  195. display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
  196. {
  197. int i = 0, n;
  198. scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (sport);
  199. do
  200. {
  201. pstate->length = print_params[i].length;
  202. ptob->seek (sport, 0, SEEK_SET);
  203. if (scm_is_pair (exp))
  204. {
  205. pstate->level = print_params[i].level - 1;
  206. scm_iprlist (hdr, exp, tlr[0], sport, pstate);
  207. scm_puts_unlocked (&tlr[1], sport);
  208. }
  209. else
  210. {
  211. pstate->level = print_params[i].level;
  212. scm_iprin1 (exp, sport, pstate);
  213. }
  214. ptob->flush (sport);
  215. n = ptob->seek (sport, 0, SEEK_CUR);
  216. ++i;
  217. }
  218. while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
  219. ptob->truncate (sport, n);
  220. scm_display (scm_strport_to_string (sport), port);
  221. }
  222. static void
  223. display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
  224. {
  225. display_frame_expr ("[", scm_frame_call_representation (frame), "]",
  226. indentation,
  227. sport,
  228. port,
  229. pstate);
  230. }
  231. SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
  232. (SCM frame, SCM port, SCM indent),
  233. "Display a procedure application @var{frame} to the output port\n"
  234. "@var{port}. @var{indent} specifies the indentation of the\n"
  235. "output.")
  236. #define FUNC_NAME s_scm_display_application
  237. {
  238. SCM_VALIDATE_FRAME (1, frame);
  239. if (SCM_UNBNDP (port))
  240. port = scm_current_output_port ();
  241. else
  242. SCM_VALIDATE_OPOUTPORT (2, port);
  243. if (SCM_UNBNDP (indent))
  244. indent = SCM_INUM0;
  245. /* Display an application. */
  246. {
  247. SCM sport, print_state;
  248. scm_print_state *pstate;
  249. /* Create a string port used for adaptation of printing parameters. */
  250. sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  251. SCM_OPN | SCM_WRTNG,
  252. FUNC_NAME);
  253. /* Create a print state for printing of frames. */
  254. print_state = scm_make_print_state ();
  255. pstate = SCM_PRINT_STATE (print_state);
  256. pstate->writingp = 1;
  257. pstate->fancyp = 1;
  258. display_application (frame, scm_to_int (indent), sport, port, pstate);
  259. return SCM_BOOL_T;
  260. }
  261. }
  262. #undef FUNC_NAME
  263. SCM_SYMBOL (sym_base, "base");
  264. static void
  265. display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
  266. {
  267. SCM source = scm_frame_source (frame);
  268. *file = *line = SCM_BOOL_F;
  269. if (scm_is_pair (source)
  270. && scm_is_pair (scm_cdr (source))
  271. && scm_is_pair (scm_cddr (source))
  272. && !scm_is_pair (scm_cdddr (source)))
  273. {
  274. /* (addr . (filename . (line . column))), from vm compilation */
  275. *file = scm_cadr (source);
  276. *line = scm_caddr (source);
  277. }
  278. }
  279. static void
  280. display_backtrace_file (frame, last_file, port, pstate)
  281. SCM frame;
  282. SCM *last_file;
  283. SCM port;
  284. scm_print_state *pstate;
  285. {
  286. SCM file, line;
  287. display_backtrace_get_file_line (frame, &file, &line);
  288. if (scm_is_true (scm_equal_p (file, *last_file)))
  289. return;
  290. *last_file = file;
  291. scm_puts_unlocked ("In ", port);
  292. if (scm_is_false (file))
  293. if (scm_is_false (line))
  294. scm_puts_unlocked ("unknown file", port);
  295. else
  296. scm_puts_unlocked ("current input", port);
  297. else
  298. {
  299. pstate->writingp = 0;
  300. scm_iprin1 (file, port, pstate);
  301. pstate->writingp = 1;
  302. }
  303. scm_puts_unlocked (":\n", port);
  304. }
  305. static void
  306. display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
  307. {
  308. SCM file, line;
  309. display_backtrace_get_file_line (frame, &file, &line);
  310. if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
  311. {
  312. if (scm_is_false (file))
  313. {
  314. if (scm_is_false (line))
  315. scm_putc_unlocked ('?', port);
  316. else
  317. scm_puts_unlocked ("<stdin>", port);
  318. }
  319. else
  320. {
  321. pstate -> writingp = 0;
  322. #ifdef HAVE_POSIX
  323. scm_iprin1 ((scm_is_string (file)?
  324. scm_basename (file, SCM_UNDEFINED) : file),
  325. port, pstate);
  326. #else
  327. scm_iprin1 (file, port, pstate);
  328. #endif
  329. pstate -> writingp = 1;
  330. }
  331. scm_putc_unlocked (':', port);
  332. }
  333. else if (scm_is_true (line))
  334. {
  335. int i, j=0;
  336. for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
  337. ;
  338. indent (4-j, port);
  339. }
  340. if (scm_is_false (line))
  341. scm_puts_unlocked (" ?", port);
  342. else
  343. scm_intprint (scm_to_int (line) + 1, 10, port);
  344. scm_puts_unlocked (": ", port);
  345. }
  346. static void
  347. display_frame (SCM frame, int n, int nfield, int indentation,
  348. SCM sport, SCM port, scm_print_state *pstate)
  349. {
  350. int i, j;
  351. /* display file name and line number */
  352. if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
  353. display_backtrace_file_and_line (frame, port, pstate);
  354. /* Check size of frame number. */
  355. for (i = 0, j = n; j > 0; ++i) j /= 10;
  356. /* Number indentation. */
  357. indent (nfield - (i ? i : 1), port);
  358. /* Frame number. */
  359. scm_iprin1 (scm_from_int (n), port, pstate);
  360. /* Indentation. */
  361. indent (indentation, port);
  362. /* Display an application. */
  363. display_application (frame, nfield + 1 + indentation, sport, port, pstate);
  364. scm_putc_unlocked ('\n', port);
  365. }
  366. struct display_backtrace_args {
  367. SCM stack;
  368. SCM port;
  369. SCM first;
  370. SCM depth;
  371. SCM highlight_objects;
  372. };
  373. static SCM
  374. display_backtrace_body (struct display_backtrace_args *a)
  375. #define FUNC_NAME "display_backtrace_body"
  376. {
  377. int n_frames, beg, end, n, i, j;
  378. int nfield, indentation;
  379. SCM frame, sport, print_state;
  380. SCM last_file;
  381. scm_print_state *pstate;
  382. a->port = SCM_COERCE_OUTPORT (a->port);
  383. /* Argument checking and extraction. */
  384. SCM_VALIDATE_STACK (1, a->stack);
  385. SCM_VALIDATE_OPOUTPORT (2, a->port);
  386. n_frames = scm_to_int (scm_stack_length (a->stack));
  387. n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
  388. if (SCM_BACKWARDS_P)
  389. {
  390. beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
  391. end = beg + n - 1;
  392. if (end >= n_frames)
  393. end = n_frames - 1;
  394. n = end - beg + 1;
  395. }
  396. else
  397. {
  398. if (scm_is_integer (a->first))
  399. {
  400. beg = scm_to_int (a->first);
  401. end = beg - n + 1;
  402. if (end < 0)
  403. end = 0;
  404. }
  405. else
  406. {
  407. beg = n - 1;
  408. end = 0;
  409. if (beg >= n_frames)
  410. beg = n_frames - 1;
  411. }
  412. n = beg - end + 1;
  413. }
  414. SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
  415. SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
  416. /* Create a string port used for adaptation of printing parameters. */
  417. sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  418. SCM_OPN | SCM_WRTNG,
  419. FUNC_NAME);
  420. /* Create a print state for printing of frames. */
  421. print_state = scm_make_print_state ();
  422. pstate = SCM_PRINT_STATE (print_state);
  423. pstate->writingp = 1;
  424. pstate->fancyp = 1;
  425. pstate->highlight_objects = a->highlight_objects;
  426. /* Determine size of frame number field. */
  427. j = end;
  428. for (i = 0; j > 0; ++i) j /= 10;
  429. nfield = i ? i : 1;
  430. /* Print frames. */
  431. indentation = 1;
  432. last_file = SCM_UNDEFINED;
  433. if (SCM_BACKWARDS_P)
  434. end++;
  435. else
  436. end--;
  437. for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i)
  438. {
  439. frame = scm_stack_ref (a->stack, scm_from_int (i));
  440. if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
  441. display_backtrace_file (frame, &last_file, a->port, pstate);
  442. display_frame (frame, i, nfield, indentation, sport, a->port, pstate);
  443. }
  444. scm_remember_upto_here_1 (print_state);
  445. return SCM_UNSPECIFIED;
  446. }
  447. #undef FUNC_NAME
  448. static SCM
  449. error_during_backtrace (void *data, SCM tag, SCM throw_args)
  450. {
  451. SCM port = SCM_PACK_POINTER (data);
  452. scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port);
  453. scm_print_exception (port, SCM_BOOL_F, tag, throw_args);
  454. return SCM_UNSPECIFIED;
  455. }
  456. SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
  457. (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
  458. "Display a backtrace to the output port @var{port}. @var{stack}\n"
  459. "is the stack to take the backtrace from, @var{first} specifies\n"
  460. "where in the stack to start and @var{depth} how many frames\n"
  461. "to display. @var{first} and @var{depth} can be @code{#f},\n"
  462. "which means that default values will be used.\n"
  463. "If @var{highlights} is given it should be a list; the elements\n"
  464. "of this list will be highlighted wherever they appear in the\n"
  465. "backtrace.")
  466. #define FUNC_NAME s_scm_display_backtrace_with_highlights
  467. {
  468. struct display_backtrace_args a;
  469. a.stack = stack;
  470. a.port = port;
  471. a.first = first;
  472. a.depth = depth;
  473. if (SCM_UNBNDP (highlights))
  474. a.highlight_objects = SCM_EOL;
  475. else
  476. a.highlight_objects = highlights;
  477. scm_internal_catch (SCM_BOOL_T,
  478. (scm_t_catch_body) display_backtrace_body, &a,
  479. (scm_t_catch_handler) error_during_backtrace, SCM_UNPACK_POINTER (port));
  480. return SCM_UNSPECIFIED;
  481. }
  482. #undef FUNC_NAME
  483. SCM
  484. scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
  485. {
  486. return scm_display_backtrace_with_highlights (stack, port, first, depth,
  487. SCM_EOL);
  488. }
  489. SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
  490. SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
  491. (SCM highlights),
  492. "Display a backtrace of the current stack to the current\n"
  493. "output port. If @var{highlights} is given, it should be\n"
  494. "a list; the elements of this list will be highlighted\n"
  495. "wherever they appear in the backtrace.")
  496. #define FUNC_NAME s_scm_backtrace_with_highlights
  497. {
  498. SCM port = scm_current_output_port ();
  499. SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  500. if (SCM_UNBNDP (highlights))
  501. highlights = SCM_EOL;
  502. scm_newline (port);
  503. scm_puts_unlocked ("Backtrace:\n", port);
  504. scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
  505. highlights);
  506. scm_newline (port);
  507. return SCM_UNSPECIFIED;
  508. }
  509. #undef FUNC_NAME
  510. SCM
  511. scm_backtrace (void)
  512. {
  513. return scm_backtrace_with_highlights (SCM_EOL);
  514. }
  515. void
  516. scm_init_backtrace ()
  517. {
  518. scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception);
  519. #include "libguile/backtrace.x"
  520. }
  521. /*
  522. Local Variables:
  523. c-file-style: "gnu"
  524. End:
  525. */