backtrace.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /* Printing of backtraces and error messages
  2. * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006 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 <assert.h>
  25. #include "libguile/_scm.h"
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #ifdef HAVE_IO_H
  30. #include <io.h>
  31. #endif
  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/validate.h"
  42. #include "libguile/lang.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. /* Print parameters for error messages. */
  51. #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
  52. #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
  53. /* Print parameters for failing expressions in error messages.
  54. * (See also `print_params' below for backtrace print parameters.)
  55. */
  56. #define DISPLAY_EXPRESSION_MAX_LEVEL 2
  57. #define DISPLAY_EXPRESSION_MAX_LENGTH 3
  58. #undef SCM_ASSERT
  59. #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
  60. if (!(_cond)) \
  61. return SCM_BOOL_F;
  62. SCM scm_the_last_stack_fluid_var;
  63. static void
  64. display_header (SCM source, SCM port)
  65. {
  66. if (SCM_MEMOIZEDP (source))
  67. {
  68. SCM fname = scm_source_property (source, scm_sym_filename);
  69. SCM line = scm_source_property (source, scm_sym_line);
  70. SCM col = scm_source_property (source, scm_sym_column);
  71. /* Dirk:FIXME:: Maybe we should store the _port_ rather than the
  72. * filename with the source properties? Then we could in case of
  73. * non-file ports give at least some more details than just
  74. * "<unnamed port>". */
  75. if (scm_is_true (fname))
  76. scm_prin1 (fname, port, 0);
  77. else
  78. scm_puts ("<unnamed port>", port);
  79. if (scm_is_true (line) && scm_is_true (col))
  80. {
  81. scm_putc (':', port);
  82. scm_intprint (scm_to_long (line) + 1, 10, port);
  83. scm_putc (':', port);
  84. scm_intprint (scm_to_long (col) + 1, 10, port);
  85. }
  86. }
  87. else
  88. scm_puts ("ERROR", port);
  89. scm_puts (": ", port);
  90. }
  91. struct display_error_message_data {
  92. SCM message;
  93. SCM args;
  94. SCM port;
  95. scm_print_state *pstate;
  96. int old_fancyp;
  97. int old_level;
  98. int old_length;
  99. };
  100. static SCM
  101. display_error_message (struct display_error_message_data *d)
  102. {
  103. if (scm_is_string (d->message) && scm_is_true (scm_list_p (d->args)))
  104. scm_simple_format (d->port, d->message, d->args);
  105. else
  106. scm_display (d->message, d->port);
  107. scm_newline (d->port);
  108. return SCM_UNSPECIFIED;
  109. }
  110. static void
  111. before_display_error_message (struct display_error_message_data *d)
  112. {
  113. scm_print_state *pstate = d->pstate;
  114. d->old_fancyp = pstate->fancyp;
  115. d->old_level = pstate->level;
  116. d->old_length = pstate->length;
  117. pstate->fancyp = 1;
  118. pstate->level = DISPLAY_ERROR_MESSAGE_MAX_LEVEL;
  119. pstate->length = DISPLAY_ERROR_MESSAGE_MAX_LENGTH;
  120. }
  121. static void
  122. after_display_error_message (struct display_error_message_data *d)
  123. {
  124. scm_print_state *pstate = d->pstate;
  125. pstate->fancyp = d->old_fancyp;
  126. pstate->level = d->old_level;
  127. pstate->length = d->old_length;
  128. }
  129. void
  130. scm_display_error_message (SCM message, SCM args, SCM port)
  131. {
  132. struct display_error_message_data d;
  133. SCM print_state;
  134. scm_print_state *pstate;
  135. port = scm_i_port_with_print_state (port, SCM_UNDEFINED);
  136. print_state = SCM_PORT_WITH_PS_PS (port);
  137. pstate = SCM_PRINT_STATE (print_state);
  138. d.message = message;
  139. d.args = args;
  140. d.port = port;
  141. d.pstate = pstate;
  142. scm_internal_dynamic_wind ((scm_t_guard) before_display_error_message,
  143. (scm_t_inner) display_error_message,
  144. (scm_t_guard) after_display_error_message,
  145. &d,
  146. &d);
  147. }
  148. static void
  149. display_expression (SCM frame, SCM pname, SCM source, SCM port)
  150. {
  151. SCM print_state = scm_make_print_state ();
  152. scm_print_state *pstate = SCM_PRINT_STATE (print_state);
  153. pstate->writingp = 0;
  154. pstate->fancyp = 1;
  155. pstate->level = DISPLAY_EXPRESSION_MAX_LEVEL;
  156. pstate->length = DISPLAY_EXPRESSION_MAX_LENGTH;
  157. if (scm_is_symbol (pname) || scm_is_string (pname))
  158. {
  159. if (SCM_FRAMEP (frame)
  160. && SCM_FRAME_EVAL_ARGS_P (frame))
  161. scm_puts ("While evaluating arguments to ", port);
  162. else
  163. scm_puts ("In procedure ", port);
  164. scm_iprin1 (pname, port, pstate);
  165. if (SCM_MEMOIZEDP (source))
  166. {
  167. scm_puts (" in expression ", port);
  168. pstate->writingp = 1;
  169. scm_iprin1 (scm_i_unmemoize_expr (source), port, pstate);
  170. }
  171. }
  172. else if (SCM_MEMOIZEDP (source))
  173. {
  174. scm_puts ("In expression ", port);
  175. pstate->writingp = 1;
  176. scm_iprin1 (scm_i_unmemoize_expr (source), port, pstate);
  177. }
  178. scm_puts (":\n", port);
  179. scm_free_print_state (print_state);
  180. }
  181. struct display_error_args {
  182. SCM stack;
  183. SCM port;
  184. SCM subr;
  185. SCM message;
  186. SCM args;
  187. SCM rest;
  188. };
  189. static SCM
  190. display_error_body (struct display_error_args *a)
  191. {
  192. SCM current_frame = SCM_BOOL_F;
  193. SCM source = SCM_BOOL_F;
  194. SCM prev_frame = SCM_BOOL_F;
  195. SCM pname = a->subr;
  196. if (scm_debug_mode_p
  197. && SCM_STACKP (a->stack)
  198. && SCM_STACK_LENGTH (a->stack) > 0)
  199. {
  200. current_frame = scm_stack_ref (a->stack, SCM_INUM0);
  201. source = SCM_FRAME_SOURCE (current_frame);
  202. prev_frame = SCM_FRAME_PREV (current_frame);
  203. if (!SCM_MEMOIZEDP (source) && scm_is_true (prev_frame))
  204. source = SCM_FRAME_SOURCE (prev_frame);
  205. if (!scm_is_symbol (pname)
  206. && !scm_is_string (pname)
  207. && SCM_FRAME_PROC_P (current_frame)
  208. && scm_is_true (scm_procedure_p (SCM_FRAME_PROC (current_frame))))
  209. pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
  210. }
  211. if (scm_is_symbol (pname) || scm_is_string (pname) || SCM_MEMOIZEDP (source))
  212. {
  213. display_header (source, a->port);
  214. display_expression (current_frame, pname, source, a->port);
  215. }
  216. display_header (source, a->port);
  217. scm_display_error_message (a->message, a->args, a->port);
  218. return SCM_UNSPECIFIED;
  219. }
  220. struct display_error_handler_data {
  221. char *mode;
  222. SCM port;
  223. };
  224. /* This is the exception handler for error reporting routines.
  225. Note that it is very important that this handler *doesn't* try to
  226. print more than the error tag, since the error very probably is
  227. caused by an erroneous print call-back routine. If we would
  228. try to print all objects, we would enter an infinite loop. */
  229. static SCM
  230. display_error_handler (struct display_error_handler_data *data,
  231. SCM tag, SCM args SCM_UNUSED)
  232. {
  233. SCM print_state = scm_make_print_state ();
  234. scm_puts ("\nException during displaying of ", data->port);
  235. scm_puts (data->mode, data->port);
  236. scm_puts (": ", data->port);
  237. scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
  238. scm_putc ('\n', data->port);
  239. return SCM_UNSPECIFIED;
  240. }
  241. /* The function scm_i_display_error prints out a detailed error message. This
  242. * function will be called directly within libguile to signal error messages.
  243. * No parameter checks will be performed by scm_i_display_error. Thus, User
  244. * code should rather use the function scm_display_error.
  245. */
  246. void
  247. scm_i_display_error (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest)
  248. {
  249. struct display_error_args a;
  250. struct display_error_handler_data data;
  251. a.stack = stack;
  252. a.port = port;
  253. a.subr = subr;
  254. a.message = message;
  255. a.args = args;
  256. a.rest = rest;
  257. data.mode = "error";
  258. data.port = port;
  259. scm_internal_catch (SCM_BOOL_T,
  260. (scm_t_catch_body) display_error_body, &a,
  261. (scm_t_catch_handler) display_error_handler, &data);
  262. }
  263. SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
  264. (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
  265. "Display an error message to the output port @var{port}.\n"
  266. "@var{stack} is the saved stack for the error, @var{subr} is\n"
  267. "the name of the procedure in which the error occurred and\n"
  268. "@var{message} is the actual error message, which may contain\n"
  269. "formatting instructions. These will format the arguments in\n"
  270. "the list @var{args} accordingly. @var{rest} is currently\n"
  271. "ignored.")
  272. #define FUNC_NAME s_scm_display_error
  273. {
  274. SCM_VALIDATE_OUTPUT_PORT (2, port);
  275. scm_i_display_error (stack, port, subr, message, args, rest);
  276. return SCM_UNSPECIFIED;
  277. }
  278. #undef FUNC_NAME
  279. typedef struct {
  280. int level;
  281. int length;
  282. } print_params_t;
  283. static int n_print_params = 9;
  284. static print_params_t default_print_params[] = {
  285. { 4, 9 }, { 4, 3 },
  286. { 3, 4 }, { 3, 3 },
  287. { 2, 4 }, { 2, 3 },
  288. { 1, 4 }, { 1, 3 }, { 1, 2 }
  289. };
  290. static print_params_t *print_params = default_print_params;
  291. #ifdef GUILE_DEBUG
  292. SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
  293. (SCM params),
  294. "Set the print parameters to the values from @var{params}.\n"
  295. "@var{params} must be a list of two-element lists which must\n"
  296. "hold two integer values.")
  297. #define FUNC_NAME s_scm_set_print_params_x
  298. {
  299. int i;
  300. int n;
  301. SCM ls;
  302. print_params_t *new_params;
  303. SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
  304. for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
  305. SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
  306. && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX)
  307. && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX),
  308. params,
  309. SCM_ARG2,
  310. s_scm_set_print_params_x);
  311. new_params = scm_malloc (n * sizeof (print_params_t));
  312. if (print_params != default_print_params)
  313. free (print_params);
  314. print_params = new_params;
  315. for (i = 0; i < n; ++i)
  316. {
  317. print_params[i].level = scm_to_int (SCM_CAAR (params));
  318. print_params[i].length = scm_to_int (SCM_CADAR (params));
  319. params = SCM_CDR (params);
  320. }
  321. n_print_params = n;
  322. return SCM_UNSPECIFIED;
  323. }
  324. #undef FUNC_NAME
  325. #endif
  326. static void
  327. indent (int n, SCM port)
  328. {
  329. int i;
  330. for (i = 0; i < n; ++i)
  331. scm_putc (' ', port);
  332. }
  333. static void
  334. display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
  335. {
  336. int i = 0, n;
  337. scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
  338. do
  339. {
  340. pstate->length = print_params[i].length;
  341. ptob->seek (sport, 0, SEEK_SET);
  342. if (scm_is_pair (exp))
  343. {
  344. pstate->level = print_params[i].level - 1;
  345. scm_iprlist (hdr, exp, tlr[0], sport, pstate);
  346. scm_puts (&tlr[1], sport);
  347. }
  348. else
  349. {
  350. pstate->level = print_params[i].level;
  351. scm_iprin1 (exp, sport, pstate);
  352. }
  353. ptob->flush (sport);
  354. n = ptob->seek (sport, 0, SEEK_CUR);
  355. ++i;
  356. }
  357. while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
  358. ptob->truncate (sport, n);
  359. scm_display (scm_strport_to_string (sport), port);
  360. }
  361. static void
  362. display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
  363. {
  364. SCM proc = SCM_FRAME_PROC (frame);
  365. SCM name = (scm_is_true (scm_procedure_p (proc))
  366. ? scm_procedure_name (proc)
  367. : SCM_BOOL_F);
  368. display_frame_expr ("[",
  369. scm_cons (scm_is_true (name) ? name : proc,
  370. SCM_FRAME_ARGS (frame)),
  371. SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
  372. indentation,
  373. sport,
  374. port,
  375. pstate);
  376. }
  377. SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
  378. (SCM frame, SCM port, SCM indent),
  379. "Display a procedure application @var{frame} to the output port\n"
  380. "@var{port}. @var{indent} specifies the indentation of the\n"
  381. "output.")
  382. #define FUNC_NAME s_scm_display_application
  383. {
  384. SCM_VALIDATE_FRAME (1, frame);
  385. if (SCM_UNBNDP (port))
  386. port = scm_current_output_port ();
  387. else
  388. SCM_VALIDATE_OPOUTPORT (2, port);
  389. if (SCM_UNBNDP (indent))
  390. indent = SCM_INUM0;
  391. if (SCM_FRAME_PROC_P (frame))
  392. /* Display an application. */
  393. {
  394. SCM sport, print_state;
  395. scm_print_state *pstate;
  396. /* Create a string port used for adaptation of printing parameters. */
  397. sport = scm_mkstrport (SCM_INUM0,
  398. scm_make_string (scm_from_int (240),
  399. SCM_UNDEFINED),
  400. SCM_OPN | SCM_WRTNG,
  401. FUNC_NAME);
  402. /* Create a print state for printing of frames. */
  403. print_state = scm_make_print_state ();
  404. pstate = SCM_PRINT_STATE (print_state);
  405. pstate->writingp = 1;
  406. pstate->fancyp = 1;
  407. display_application (frame, scm_to_int (indent), sport, port, pstate);
  408. return SCM_BOOL_T;
  409. }
  410. else
  411. return SCM_BOOL_F;
  412. }
  413. #undef FUNC_NAME
  414. SCM_SYMBOL (sym_base, "base");
  415. static void
  416. display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
  417. {
  418. SCM source = SCM_FRAME_SOURCE (frame);
  419. *file = *line = SCM_BOOL_F;
  420. if (SCM_MEMOIZEDP (source))
  421. {
  422. *file = scm_source_property (source, scm_sym_filename);
  423. *line = scm_source_property (source, scm_sym_line);
  424. }
  425. else if (scm_is_pair (source)
  426. && scm_is_pair (scm_cdr (source))
  427. && scm_is_pair (scm_cddr (source))
  428. && !scm_is_pair (scm_cdddr (source)))
  429. {
  430. /* (addr . (filename . (line . column))), from vm compilation */
  431. *file = scm_cadr (source);
  432. *line = scm_caddr (source);
  433. }
  434. }
  435. static void
  436. display_backtrace_file (frame, last_file, port, pstate)
  437. SCM frame;
  438. SCM *last_file;
  439. SCM port;
  440. scm_print_state *pstate;
  441. {
  442. SCM file, line;
  443. display_backtrace_get_file_line (frame, &file, &line);
  444. if (scm_is_eq (file, *last_file))
  445. return;
  446. *last_file = file;
  447. scm_puts ("In ", port);
  448. if (scm_is_false (file))
  449. if (scm_is_false (line))
  450. scm_puts ("unknown file", port);
  451. else
  452. scm_puts ("current input", port);
  453. else
  454. {
  455. pstate->writingp = 0;
  456. scm_iprin1 (file, port, pstate);
  457. pstate->writingp = 1;
  458. }
  459. scm_puts (":\n", port);
  460. }
  461. static void
  462. display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
  463. {
  464. SCM file, line;
  465. display_backtrace_get_file_line (frame, &file, &line);
  466. if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
  467. {
  468. if (scm_is_false (file))
  469. {
  470. if (scm_is_false (line))
  471. scm_putc ('?', port);
  472. else
  473. scm_puts ("<stdin>", port);
  474. }
  475. else
  476. {
  477. pstate -> writingp = 0;
  478. #ifdef HAVE_POSIX
  479. scm_iprin1 ((scm_is_string (file)?
  480. scm_basename (file, SCM_UNDEFINED) : file),
  481. port, pstate);
  482. #else
  483. scm_iprin1 (file, port, pstate);
  484. #endif
  485. pstate -> writingp = 1;
  486. }
  487. scm_putc (':', port);
  488. }
  489. else if (scm_is_true (line))
  490. {
  491. int i, j=0;
  492. for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
  493. ;
  494. indent (4-j, port);
  495. }
  496. if (scm_is_false (line))
  497. scm_puts (" ?", port);
  498. else
  499. scm_intprint (scm_to_int (line) + 1, 10, port);
  500. scm_puts (": ", port);
  501. }
  502. static void
  503. display_frame (SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate)
  504. {
  505. int n, i, j;
  506. /* Announce missing frames? */
  507. if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
  508. {
  509. indent (nfield + 1 + indentation, port);
  510. scm_puts ("...\n", port);
  511. }
  512. /* display file name and line number */
  513. if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
  514. display_backtrace_file_and_line (frame, port, pstate);
  515. /* Check size of frame number. */
  516. n = SCM_FRAME_NUMBER (frame);
  517. for (i = 0, j = n; j > 0; ++i) j /= 10;
  518. /* Number indentation. */
  519. indent (nfield - (i ? i : 1), port);
  520. /* Frame number. */
  521. scm_iprin1 (scm_from_int (n), port, pstate);
  522. /* Real frame marker */
  523. scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
  524. /* Indentation. */
  525. indent (indentation, port);
  526. if (SCM_FRAME_PROC_P (frame))
  527. /* Display an application. */
  528. display_application (frame, nfield + 1 + indentation, sport, port, pstate);
  529. else
  530. /* Display a special form. */
  531. {
  532. SCM source = SCM_FRAME_SOURCE (frame);
  533. SCM copy = (scm_is_pair (source)
  534. ? scm_source_property (source, scm_sym_copy)
  535. : SCM_BOOL_F);
  536. SCM umcopy = (SCM_MEMOIZEDP (source)
  537. ? scm_i_unmemoize_expr (source)
  538. : SCM_BOOL_F);
  539. display_frame_expr ("(",
  540. scm_is_pair (copy) ? copy : umcopy,
  541. ")",
  542. nfield + 1 + indentation,
  543. sport,
  544. port,
  545. pstate);
  546. }
  547. scm_putc ('\n', port);
  548. /* Announce missing frames? */
  549. if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
  550. {
  551. indent (nfield + 1 + indentation, port);
  552. scm_puts ("...\n", port);
  553. }
  554. }
  555. struct display_backtrace_args {
  556. SCM stack;
  557. SCM port;
  558. SCM first;
  559. SCM depth;
  560. SCM highlight_objects;
  561. };
  562. static SCM
  563. display_backtrace_body (struct display_backtrace_args *a)
  564. #define FUNC_NAME "display_backtrace_body"
  565. {
  566. int n_frames, beg, end, n, i, j;
  567. int nfield, indent_p, indentation;
  568. SCM frame, sport, print_state;
  569. SCM last_file;
  570. scm_print_state *pstate;
  571. a->port = SCM_COERCE_OUTPORT (a->port);
  572. /* Argument checking and extraction. */
  573. SCM_VALIDATE_STACK (1, a->stack);
  574. SCM_VALIDATE_OPOUTPORT (2, a->port);
  575. n_frames = scm_to_int (scm_stack_length (a->stack));
  576. n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
  577. if (SCM_BACKWARDS_P)
  578. {
  579. beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
  580. end = beg + n - 1;
  581. if (end >= n_frames)
  582. end = n_frames - 1;
  583. n = end - beg + 1;
  584. }
  585. else
  586. {
  587. if (scm_is_integer (a->first))
  588. {
  589. beg = scm_to_int (a->first);
  590. end = beg - n + 1;
  591. if (end < 0)
  592. end = 0;
  593. }
  594. else
  595. {
  596. beg = n - 1;
  597. end = 0;
  598. if (beg >= n_frames)
  599. beg = n_frames - 1;
  600. }
  601. n = beg - end + 1;
  602. }
  603. SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
  604. SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
  605. /* Create a string port used for adaptation of printing parameters. */
  606. sport = scm_mkstrport (SCM_INUM0,
  607. scm_make_string (scm_from_int (240), SCM_UNDEFINED),
  608. SCM_OPN | SCM_WRTNG,
  609. FUNC_NAME);
  610. /* Create a print state for printing of frames. */
  611. print_state = scm_make_print_state ();
  612. pstate = SCM_PRINT_STATE (print_state);
  613. pstate->writingp = 1;
  614. pstate->fancyp = 1;
  615. pstate->highlight_objects = a->highlight_objects;
  616. /* First find out if it's reasonable to do indentation. */
  617. if (SCM_BACKWARDS_P)
  618. indent_p = 0;
  619. else
  620. {
  621. unsigned int j;
  622. indent_p = 1;
  623. frame = scm_stack_ref (a->stack, scm_from_int (beg));
  624. for (i = 0, j = 0; i < n; ++i)
  625. {
  626. if (SCM_FRAME_REAL_P (frame))
  627. ++j;
  628. if (j > SCM_BACKTRACE_INDENT)
  629. {
  630. indent_p = 0;
  631. break;
  632. }
  633. frame = (SCM_BACKWARDS_P
  634. ? SCM_FRAME_PREV (frame)
  635. : SCM_FRAME_NEXT (frame));
  636. }
  637. }
  638. /* Determine size of frame number field. */
  639. j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, scm_from_int (end)));
  640. for (i = 0; j > 0; ++i) j /= 10;
  641. nfield = i ? i : 1;
  642. /* Print frames. */
  643. frame = scm_stack_ref (a->stack, scm_from_int (beg));
  644. indentation = 1;
  645. last_file = SCM_UNDEFINED;
  646. for (i = 0; i < n; ++i)
  647. {
  648. if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
  649. display_backtrace_file (frame, &last_file, a->port, pstate);
  650. display_frame (frame, nfield, indentation, sport, a->port, pstate);
  651. if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
  652. ++indentation;
  653. frame = (SCM_BACKWARDS_P ?
  654. SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame));
  655. }
  656. scm_remember_upto_here_1 (print_state);
  657. return SCM_UNSPECIFIED;
  658. }
  659. #undef FUNC_NAME
  660. SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
  661. (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
  662. "Display a backtrace to the output port @var{port}. @var{stack}\n"
  663. "is the stack to take the backtrace from, @var{first} specifies\n"
  664. "where in the stack to start and @var{depth} how many frames\n"
  665. "to display. @var{first} and @var{depth} can be @code{#f},\n"
  666. "which means that default values will be used.\n"
  667. "If @var{highlights} is given it should be a list; the elements\n"
  668. "of this list will be highlighted wherever they appear in the\n"
  669. "backtrace.")
  670. #define FUNC_NAME s_scm_display_backtrace_with_highlights
  671. {
  672. struct display_backtrace_args a;
  673. struct display_error_handler_data data;
  674. a.stack = stack;
  675. a.port = port;
  676. a.first = first;
  677. a.depth = depth;
  678. if (SCM_UNBNDP (highlights))
  679. a.highlight_objects = SCM_EOL;
  680. else
  681. a.highlight_objects = highlights;
  682. data.mode = "backtrace";
  683. data.port = port;
  684. scm_internal_catch (SCM_BOOL_T,
  685. (scm_t_catch_body) display_backtrace_body, &a,
  686. (scm_t_catch_handler) display_error_handler, &data);
  687. return SCM_UNSPECIFIED;
  688. }
  689. #undef FUNC_NAME
  690. SCM
  691. scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
  692. {
  693. return scm_display_backtrace_with_highlights (stack, port, first, depth,
  694. SCM_EOL);
  695. }
  696. SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
  697. SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
  698. (SCM highlights),
  699. "Display a backtrace of the stack saved by the last error\n"
  700. "to the current output port. If @var{highlights} is given\n"
  701. "it should be a list; the elements of this list will be\n"
  702. "highlighted wherever they appear in the backtrace.")
  703. #define FUNC_NAME s_scm_backtrace_with_highlights
  704. {
  705. SCM port = scm_current_output_port ();
  706. SCM the_last_stack =
  707. scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var));
  708. if (SCM_UNBNDP (highlights))
  709. highlights = SCM_EOL;
  710. if (scm_is_true (the_last_stack))
  711. {
  712. scm_newline (port);
  713. scm_puts ("Backtrace:\n", port);
  714. scm_display_backtrace_with_highlights (the_last_stack,
  715. port,
  716. SCM_BOOL_F,
  717. SCM_BOOL_F,
  718. highlights);
  719. scm_newline (port);
  720. if (scm_is_false (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var))
  721. && !SCM_BACKTRACE_P)
  722. {
  723. scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
  724. "a backtrace\n"
  725. "automatically if an error occurs in the future.\n",
  726. port);
  727. SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
  728. }
  729. }
  730. else
  731. {
  732. scm_puts ("No backtrace available.\n", port);
  733. }
  734. return SCM_UNSPECIFIED;
  735. }
  736. #undef FUNC_NAME
  737. SCM
  738. scm_backtrace (void)
  739. {
  740. return scm_backtrace_with_highlights (SCM_EOL);
  741. }
  742. void
  743. scm_init_backtrace ()
  744. {
  745. SCM f = scm_make_fluid ();
  746. scm_the_last_stack_fluid_var = scm_c_define ("the-last-stack", f);
  747. #include "libguile/backtrace.x"
  748. }
  749. /*
  750. Local Variables:
  751. c-file-style: "gnu"
  752. End:
  753. */