backtrace.c 16 KB

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