backtrace.c 21 KB

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