backtrace.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Printing of backtraces and error messages
  2. * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009,
  3. * 2010, 2011, 2014 Free Software Foundation
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "libguile/_scm.h"
  26. #include <unistd.h>
  27. #ifdef HAVE_IO_H
  28. #include <io.h>
  29. #endif
  30. #include "libguile/deprecation.h"
  31. #include "libguile/stacks.h"
  32. #include "libguile/srcprop.h"
  33. #include "libguile/struct.h"
  34. #include "libguile/strports.h"
  35. #include "libguile/throw.h"
  36. #include "libguile/fluids.h"
  37. #include "libguile/ports.h"
  38. #include "libguile/strings.h"
  39. #include "libguile/dynwind.h"
  40. #include "libguile/frames.h"
  41. #include "libguile/validate.h"
  42. #include "libguile/backtrace.h"
  43. #include "libguile/filesys.h"
  44. #include "libguile/private-options.h"
  45. /* {Error reporting and backtraces}
  46. *
  47. * Note that these functions shouldn't generate errors themselves.
  48. */
  49. static SCM
  50. boot_print_exception (SCM port, SCM frame, SCM key, SCM args)
  51. #define FUNC_NAME "boot-print-exception"
  52. {
  53. scm_puts ("Throw to key ", port);
  54. scm_write (key, port);
  55. scm_puts (" with args ", port);
  56. scm_write (args, port);
  57. return SCM_UNSPECIFIED;
  58. }
  59. #undef FUNC_NAME
  60. static SCM print_exception_var;
  61. static SCM print_frame_var;
  62. static SCM kw_count;
  63. static SCM print_frames_var;
  64. static SCM frame_to_stack_vector_var;
  65. static void
  66. init_print_exception_var (void)
  67. {
  68. print_exception_var
  69. = scm_module_variable (scm_the_root_module (),
  70. scm_from_latin1_symbol ("print-exception"));
  71. }
  72. static void
  73. init_print_frame_var (void)
  74. {
  75. print_frame_var =
  76. scm_c_public_variable ("system repl debug", "print-frame");
  77. }
  78. static void
  79. init_print_frames_var_and_frame_to_stack_vector_var (void)
  80. {
  81. kw_count = scm_from_latin1_keyword ("count");
  82. print_frames_var =
  83. scm_c_public_variable ("system repl debug", "print-frames");
  84. frame_to_stack_vector_var =
  85. scm_c_public_variable ("system repl debug", "frame->stack-vector");
  86. }
  87. SCM
  88. scm_print_exception (SCM port, SCM frame, SCM key, SCM args)
  89. #define FUNC_NAME "print-exception"
  90. {
  91. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  92. scm_i_pthread_once (&once, init_print_exception_var);
  93. SCM_VALIDATE_OPOUTPORT (1, port);
  94. if (scm_is_true (frame))
  95. SCM_VALIDATE_FRAME (2, frame);
  96. SCM_VALIDATE_SYMBOL (3, key);
  97. SCM_VALIDATE_LIST (4, args);
  98. return scm_call_4 (scm_variable_ref (print_exception_var),
  99. port, frame, key, args);
  100. }
  101. #undef FUNC_NAME
  102. /* Print parameters for error messages. */
  103. #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
  104. #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
  105. /* Print parameters for failing expressions in error messages.
  106. * (See also `print_params' below for backtrace print parameters.)
  107. */
  108. #define DISPLAY_EXPRESSION_MAX_LEVEL 2
  109. #define DISPLAY_EXPRESSION_MAX_LENGTH 3
  110. #undef SCM_ASSERT
  111. #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
  112. if (!(_cond)) \
  113. return SCM_BOOL_F;
  114. void
  115. scm_display_error_message (SCM message, SCM args, SCM port)
  116. {
  117. scm_print_exception (port, SCM_BOOL_F, scm_misc_error_key,
  118. scm_list_3 (SCM_BOOL_F, message, args));
  119. }
  120. /* The function scm_i_display_error prints out a detailed error message. This
  121. * function will be called directly within libguile to signal error messages.
  122. * No parameter checks will be performed by scm_i_display_error. Thus, User
  123. * code should rather use the function scm_display_error.
  124. */
  125. void
  126. scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest)
  127. {
  128. scm_print_exception (port, frame, scm_misc_error_key,
  129. scm_list_3 (subr, message, args));
  130. }
  131. SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
  132. (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest),
  133. "Display an error message to the output port @var{port}.\n"
  134. "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
  135. "the name of the procedure in which the error occurred and\n"
  136. "@var{message} is the actual error message, which may contain\n"
  137. "formatting instructions. These will format the arguments in\n"
  138. "the list @var{args} accordingly. @var{rest} is currently\n"
  139. "ignored.")
  140. #define FUNC_NAME s_scm_display_error
  141. {
  142. SCM_VALIDATE_OUTPUT_PORT (2, port);
  143. #if SCM_ENABLE_DEPRECATED
  144. if (SCM_STACKP (frame))
  145. {
  146. scm_c_issue_deprecation_warning
  147. ("Passing a stack as the first argument to `scm_display_error' is "
  148. "deprecated. Pass a frame instead.");
  149. if (SCM_STACK_LENGTH (frame))
  150. frame = scm_stack_ref (frame, SCM_INUM0);
  151. else
  152. frame = SCM_BOOL_F;
  153. }
  154. #endif
  155. scm_i_display_error (frame, port, subr, message, args, rest);
  156. return SCM_UNSPECIFIED;
  157. }
  158. #undef FUNC_NAME
  159. SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
  160. (SCM frame, SCM port, SCM indent),
  161. "Display a procedure application @var{frame} to the output port\n"
  162. "@var{port}. @var{indent} specifies the indentation of the\n"
  163. "output.")
  164. #define FUNC_NAME s_scm_display_application
  165. {
  166. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  167. scm_i_pthread_once (&once, init_print_frame_var);
  168. /* FIXME perhaps: ignoring indent. But really we should deprecate
  169. this procedure in favor of print-frame. */
  170. return scm_call_2 (scm_variable_ref (print_frame_var), frame, port);
  171. }
  172. #undef FUNC_NAME
  173. struct display_backtrace_args {
  174. SCM stack;
  175. SCM port;
  176. SCM first;
  177. SCM depth;
  178. SCM highlight_objects;
  179. };
  180. static SCM
  181. display_backtrace_body (struct display_backtrace_args *a)
  182. #define FUNC_NAME "display-backtrace"
  183. {
  184. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  185. SCM frames;
  186. scm_i_pthread_once (&once,
  187. init_print_frames_var_and_frame_to_stack_vector_var);
  188. a->port = SCM_COERCE_OUTPORT (a->port);
  189. /* Argument checking and extraction. */
  190. SCM_VALIDATE_STACK (1, a->stack);
  191. SCM_VALIDATE_OPOUTPORT (2, a->port);
  192. if (scm_is_false (a->first))
  193. a->first = SCM_INUM0;
  194. if (scm_is_false (a->depth))
  195. a->depth = scm_from_int (SCM_BACKTRACE_DEPTH);
  196. if (scm_is_false (scm_less_p (a->first, scm_stack_length (a->stack))))
  197. return SCM_UNSPECIFIED;
  198. frames = scm_call_1 (scm_variable_ref (frame_to_stack_vector_var),
  199. scm_stack_ref (a->stack, a->first));
  200. /* FIXME: highlight_objects */
  201. scm_call_4 (scm_variable_ref (print_frames_var), frames, a->port,
  202. kw_count, a->depth);
  203. return SCM_UNSPECIFIED;
  204. }
  205. #undef FUNC_NAME
  206. static SCM
  207. error_during_backtrace (void *data, SCM tag, SCM throw_args)
  208. {
  209. SCM port = SCM_PACK_POINTER (data);
  210. scm_puts ("Exception thrown while printing backtrace:\n", port);
  211. scm_print_exception (port, SCM_BOOL_F, tag, throw_args);
  212. return SCM_UNSPECIFIED;
  213. }
  214. SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
  215. (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
  216. "Display a backtrace to the output port @var{port}. @var{stack}\n"
  217. "is the stack to take the backtrace from, @var{first} specifies\n"
  218. "where in the stack to start and @var{depth} how many frames\n"
  219. "to display. @var{first} and @var{depth} can be @code{#f},\n"
  220. "which means that default values will be used.\n"
  221. "If @var{highlights} is given it should be a list; the elements\n"
  222. "of this list will be highlighted wherever they appear in the\n"
  223. "backtrace.")
  224. #define FUNC_NAME s_scm_display_backtrace_with_highlights
  225. {
  226. struct display_backtrace_args a;
  227. a.stack = stack;
  228. a.port = port;
  229. a.first = SCM_UNBNDP (first) ? SCM_BOOL_F : first;
  230. a.depth = SCM_UNBNDP (depth) ? SCM_BOOL_F : depth;
  231. a.highlight_objects = SCM_UNBNDP (highlights) ? SCM_EOL : highlights;
  232. scm_internal_catch (SCM_BOOL_T,
  233. (scm_t_catch_body) display_backtrace_body, &a,
  234. (scm_t_catch_handler) error_during_backtrace, SCM_UNPACK_POINTER (port));
  235. return SCM_UNSPECIFIED;
  236. }
  237. #undef FUNC_NAME
  238. SCM
  239. scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
  240. {
  241. return scm_display_backtrace_with_highlights (stack, port, first, depth,
  242. SCM_EOL);
  243. }
  244. SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
  245. SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
  246. (SCM highlights),
  247. "Display a backtrace of the current stack to the current\n"
  248. "output port. If @var{highlights} is given, it should be\n"
  249. "a list; the elements of this list will be highlighted\n"
  250. "wherever they appear in the backtrace.")
  251. #define FUNC_NAME s_scm_backtrace_with_highlights
  252. {
  253. SCM port = scm_current_output_port ();
  254. SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  255. if (SCM_UNBNDP (highlights))
  256. highlights = SCM_EOL;
  257. scm_newline (port);
  258. scm_puts ("Backtrace:\n", port);
  259. scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
  260. highlights);
  261. scm_newline (port);
  262. return SCM_UNSPECIFIED;
  263. }
  264. #undef FUNC_NAME
  265. SCM
  266. scm_backtrace (void)
  267. {
  268. return scm_backtrace_with_highlights (SCM_EOL);
  269. }
  270. void
  271. scm_init_backtrace ()
  272. {
  273. scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception);
  274. #include "libguile/backtrace.x"
  275. }
  276. /*
  277. Local Variables:
  278. c-file-style: "gnu"
  279. End:
  280. */