backtrace.c 22 KB

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