throw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* Copyright 1995-1998,2000-2001,2003-2004,2006,2008,2009-2014,2017-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #include <unistdio.h>
  20. #include "backtrace.h"
  21. #include "boolean.h"
  22. #include "debug.h"
  23. #include "dynwind.h"
  24. #include "eq.h"
  25. #include "eval.h"
  26. #include "exceptions.h"
  27. #include "fluids.h"
  28. #include "gsubr.h"
  29. #include "init.h"
  30. #include "list.h"
  31. #include "modules.h"
  32. #include "numbers.h"
  33. #include "pairs.h"
  34. #include "ports.h"
  35. #include "private-options.h"
  36. #include "smob.h"
  37. #include "stackchk.h"
  38. #include "stacks.h"
  39. #include "strings.h"
  40. #include "symbols.h"
  41. #include "variable.h"
  42. #include "vm.h"
  43. #include "throw.h"
  44. static SCM throw_var;
  45. /* TAG is the catch tag. Typically, this is a symbol, but this
  46. function doesn't actually care about that.
  47. BODY is a pointer to a C function which runs the body of the catch;
  48. this is the code you can throw from. We call it like this:
  49. BODY (BODY_DATA)
  50. where:
  51. BODY_DATA is just the BODY_DATA argument we received; we pass it
  52. through to BODY as its first argument. The caller can make
  53. BODY_DATA point to anything useful that BODY might need.
  54. HANDLER is a pointer to a C function to deal with a throw to TAG,
  55. should one occur. We call it like this:
  56. HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
  57. where
  58. HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
  59. same idea as BODY_DATA above.
  60. THROWN_TAG is the tag that the user threw to; usually this is
  61. TAG, but it could be something else if TAG was #t (i.e., a
  62. catch-all), or the user threw to a jmpbuf.
  63. THROW_ARGS is the list of arguments the user passed to the THROW
  64. function, after the tag.
  65. BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
  66. is just a pointer we pass through to HANDLER. We don't actually
  67. use either of those pointers otherwise ourselves. The idea is
  68. that, if our caller wants to communicate something to BODY or
  69. HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
  70. HANDLER can then use. Think of it as a way to make BODY and
  71. HANDLER closures, not just functions; MUMBLE_DATA points to the
  72. enclosed variables.
  73. Of course, it's up to the caller to make sure that any data a
  74. MUMBLE_DATA needs is protected from GC. A common way to do this is
  75. to make MUMBLE_DATA a pointer to data stored in an automatic
  76. structure variable; since the collector must scan the stack for
  77. references anyway, this assures that any references in MUMBLE_DATA
  78. will be found. */
  79. struct scm_catch_data
  80. {
  81. SCM tag;
  82. scm_t_thunk body;
  83. void *body_data;
  84. scm_t_catch_handler handler;
  85. void *handler_data;
  86. scm_t_catch_handler pre_unwind_handler;
  87. void *pre_unwind_handler_data;
  88. SCM pre_unwind_running;
  89. };
  90. static SCM
  91. catch_post_unwind_handler (void *data, SCM exn)
  92. {
  93. struct scm_catch_data *catch_data = data;
  94. return catch_data->handler (catch_data->handler_data,
  95. scm_exception_kind (exn),
  96. scm_exception_args (exn));
  97. }
  98. static SCM
  99. catch_pre_unwind_handler (void *data, SCM exn)
  100. {
  101. struct scm_catch_data *catch_data = data;
  102. SCM kind = scm_exception_kind (exn);
  103. SCM args = scm_exception_args (exn);
  104. if ((scm_is_eq (catch_data->tag, SCM_BOOL_T)
  105. || scm_is_eq (kind, catch_data->tag))
  106. && scm_is_false (scm_fluid_ref (catch_data->pre_unwind_running))) {
  107. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  108. scm_dynwind_throw_handler ();
  109. scm_dynwind_fluid (catch_data->pre_unwind_running, SCM_BOOL_T);
  110. catch_data->pre_unwind_handler (catch_data->pre_unwind_handler_data,
  111. kind, args);
  112. scm_dynwind_end ();
  113. }
  114. return scm_raise_exception (exn);
  115. }
  116. static SCM
  117. catch_body (void *data)
  118. {
  119. struct scm_catch_data *catch_data = data;
  120. if (catch_data->pre_unwind_handler) {
  121. SCM thunk = scm_c_make_thunk (catch_data->body, catch_data->body_data);
  122. SCM handler = scm_c_make_exception_handler (catch_pre_unwind_handler, data);
  123. SCM fluid = scm_make_thread_local_fluid (SCM_BOOL_F);
  124. catch_data->pre_unwind_running = fluid;
  125. return scm_with_pre_unwind_exception_handler (handler, thunk);
  126. }
  127. return catch_data->body (catch_data->body_data);
  128. }
  129. SCM
  130. scm_c_catch (SCM tag,
  131. scm_t_thunk body, void *body_data,
  132. scm_t_catch_handler handler, void *handler_data,
  133. scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data)
  134. {
  135. struct scm_catch_data data =
  136. { tag, body, body_data, handler, handler_data, pre_unwind_handler,
  137. pre_unwind_handler_data, SCM_BOOL_F };
  138. return scm_c_with_exception_handler (tag, catch_post_unwind_handler, &data,
  139. catch_body, &data);
  140. }
  141. SCM
  142. scm_internal_catch (SCM tag,
  143. scm_t_thunk body, void *body_data,
  144. scm_t_catch_handler handler, void *handler_data)
  145. {
  146. return scm_c_catch (tag,
  147. body, body_data,
  148. handler, handler_data,
  149. NULL, NULL);
  150. }
  151. SCM
  152. scm_c_with_throw_handler (SCM tag,
  153. scm_t_thunk body,
  154. void *body_data,
  155. scm_t_catch_handler handler,
  156. void *handler_data,
  157. int lazy_catch_p)
  158. {
  159. struct scm_catch_data data =
  160. { tag, body, body_data, NULL, NULL, handler, handler_data, SCM_BOOL_F };
  161. if (lazy_catch_p)
  162. /* Non-zero lazy_catch_p arguments have been deprecated since
  163. 2010. */
  164. abort ();
  165. return catch_body (&data);
  166. }
  167. static SCM
  168. call_thunk (void* data)
  169. {
  170. return scm_call_0 (PTR2SCM (data));
  171. }
  172. static SCM
  173. call_handler (void* data, SCM a, SCM b)
  174. {
  175. return scm_call_2 (PTR2SCM (data), a, b);
  176. }
  177. SCM
  178. scm_catch (SCM key, SCM thunk, SCM handler)
  179. {
  180. return scm_c_catch (key, call_thunk, SCM2PTR (thunk),
  181. call_handler, SCM2PTR (handler), NULL, NULL);
  182. }
  183. SCM
  184. scm_catch_with_pre_unwind_handler (SCM key, SCM thunk, SCM handler,
  185. SCM pre_unwind_handler)
  186. {
  187. if (SCM_UNBNDP (pre_unwind_handler))
  188. return scm_catch (key, thunk, handler);
  189. return scm_c_catch (key, call_thunk, SCM2PTR (thunk),
  190. call_handler, SCM2PTR (handler),
  191. call_handler, SCM2PTR (pre_unwind_handler));
  192. }
  193. SCM
  194. scm_with_throw_handler (SCM key, SCM thunk, SCM handler)
  195. {
  196. return scm_c_with_throw_handler (key, call_thunk, SCM2PTR (thunk),
  197. call_handler, SCM2PTR (handler), 0);
  198. }
  199. SCM
  200. scm_throw (SCM key, SCM args)
  201. {
  202. SCM throw = scm_variable_ref (throw_var);
  203. if (scm_is_false (throw)) {
  204. static int error_printing_error = 0;
  205. if (error_printing_error++)
  206. {
  207. fprintf (stderr, "Error while printing pre-boot error: %s\n",
  208. scm_i_symbol_chars (key));
  209. }
  210. else
  211. {
  212. SCM port = scm_current_error_port ();
  213. scm_puts ("Pre-boot error; key: ", port);
  214. scm_write (key, port);
  215. scm_puts (", args: ", port);
  216. scm_write (args, port);
  217. }
  218. abort ();
  219. }
  220. scm_apply_1 (throw, key, args);
  221. /* Should not be reached. */
  222. abort ();
  223. }
  224. /* Now some support for C bodies and catch handlers */
  225. static scm_t_bits tc16_catch_handler;
  226. SCM
  227. scm_i_make_catch_handler (scm_t_catch_handler handler, void *data)
  228. {
  229. SCM_RETURN_NEWSMOB2 (tc16_catch_handler, handler, data);
  230. }
  231. static SCM
  232. apply_catch_handler (SCM clo, SCM args)
  233. {
  234. scm_t_catch_handler handler = (void*)SCM_SMOB_DATA (clo);
  235. void *data = (void*)SCM_SMOB_DATA_2 (clo);
  236. return handler (data, scm_car (args), scm_cdr (args));
  237. }
  238. /* body and handler functions for use with any of the above catch variants */
  239. /* This is a body function you can pass to scm_internal_catch if you
  240. want the body to be like Scheme's `catch' --- a thunk.
  241. BODY_DATA is a pointer to a scm_body_thunk_data structure, which
  242. contains the Scheme procedure to invoke as the body, and the tag
  243. we're catching. */
  244. SCM
  245. scm_body_thunk (void *body_data)
  246. {
  247. struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
  248. return scm_call_0 (c->body_proc);
  249. }
  250. /* This is a handler function you can pass to scm_internal_catch if
  251. you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
  252. applies a handler procedure to (TAG ARGS ...).
  253. If the user does a throw to this catch, this function runs a
  254. handler procedure written in Scheme. HANDLER_DATA is a pointer to
  255. an SCM variable holding the Scheme procedure object to invoke. It
  256. ought to be a pointer to an automatic variable (i.e., one living on
  257. the stack), or the procedure object should be otherwise protected
  258. from GC. */
  259. SCM
  260. scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
  261. {
  262. SCM *handler_proc_p = (SCM *) handler_data;
  263. return scm_apply_1 (*handler_proc_p, tag, throw_args);
  264. }
  265. /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
  266. catches all throws that the handler might emit itself. The handler
  267. used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
  268. struct hbpca_data {
  269. SCM proc;
  270. SCM args;
  271. };
  272. static SCM
  273. hbpca_body (void *body_data)
  274. {
  275. struct hbpca_data *data = (struct hbpca_data *)body_data;
  276. return scm_apply_0 (data->proc, data->args);
  277. }
  278. SCM
  279. scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
  280. {
  281. SCM *handler_proc_p = (SCM *) handler_data;
  282. struct hbpca_data data;
  283. data.proc = *handler_proc_p;
  284. data.args = scm_cons (tag, throw_args);
  285. return scm_internal_catch (SCM_BOOL_T,
  286. hbpca_body, &data,
  287. scm_handle_by_message_noexit, NULL);
  288. }
  289. static int
  290. should_print_backtrace (SCM tag, SCM stack)
  291. {
  292. return SCM_BACKTRACE_P
  293. && scm_is_true (stack)
  294. && scm_initialized_p
  295. /* It's generally not useful to print backtraces for errors reading
  296. or expanding code in these fallback catch statements. */
  297. && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
  298. && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
  299. }
  300. static void
  301. handler_message (void *handler_data, SCM tag, SCM args)
  302. {
  303. SCM p, stack, frame;
  304. p = scm_current_error_port ();
  305. /* Usually we get here via a throw to a catch-all. In that case
  306. there is the throw frame active, and the catch closure, so narrow by
  307. two frames. It is possible for a user to invoke
  308. scm_handle_by_message directly, though, so it could be this
  309. narrows too much. We'll have to see how this works out in
  310. practice. */
  311. stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
  312. frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
  313. if (should_print_backtrace (tag, stack))
  314. {
  315. scm_puts ("Backtrace:\n", p);
  316. scm_display_backtrace_with_highlights (stack, p,
  317. SCM_BOOL_F, SCM_BOOL_F,
  318. SCM_EOL);
  319. scm_newline (p);
  320. }
  321. scm_print_exception (p, frame, tag, args);
  322. }
  323. /* This is a handler function to use if you want scheme to print a
  324. message and die. Useful for dealing with throws to uncaught keys
  325. at the top level.
  326. At boot time, we establish a catch-all that uses this as its handler.
  327. 1) If the user wants something different, they can use (catch #t
  328. ...) to do what they like.
  329. 2) Outside the context of a read-eval-print loop, there isn't
  330. anything else good to do; libguile should not assume the existence
  331. of a read-eval-print loop.
  332. 3) Given that we shouldn't do anything complex, it's much more
  333. robust to do it in C code.
  334. HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
  335. message header to print; if zero, we use "guile" instead. That
  336. text is followed by a colon, then the message described by ARGS. */
  337. /* Dirk:FIXME:: The name of the function should make clear that the
  338. * application gets terminated.
  339. */
  340. SCM
  341. scm_handle_by_message (void *handler_data, SCM tag, SCM args)
  342. {
  343. if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit"))))
  344. exit (scm_exit_status (args));
  345. handler_message (handler_data, tag, args);
  346. scm_i_pthread_exit (NULL);
  347. /* this point not reached, but suppress gcc warning about no return value
  348. in case scm_i_pthread_exit isn't marked as "noreturn" (which seemed not
  349. to be the case on cygwin for instance) */
  350. return SCM_BOOL_F;
  351. }
  352. /* This is just like scm_handle_by_message, but it doesn't exit; it
  353. just returns #f. It's useful in cases where you don't really know
  354. enough about the body to handle things in a better way, but don't
  355. want to let throws fall off the bottom of the wind list. */
  356. SCM
  357. scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
  358. {
  359. if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit"))))
  360. exit (scm_exit_status (args));
  361. handler_message (handler_data, tag, args);
  362. return SCM_BOOL_F;
  363. }
  364. SCM
  365. scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args)
  366. {
  367. scm_ithrow (tag, args, 1);
  368. return SCM_UNSPECIFIED; /* never returns */
  369. }
  370. SCM
  371. scm_ithrow (SCM key, SCM args, int no_return SCM_UNUSED)
  372. {
  373. scm_throw (key, args);
  374. }
  375. void
  376. scm_init_throw ()
  377. {
  378. tc16_catch_handler = scm_make_smob_type ("catch-handler", 0);
  379. scm_set_smob_apply (tc16_catch_handler, apply_catch_handler, 0, 0, 1);
  380. throw_var = scm_c_define ("throw", SCM_BOOL_F);
  381. #include "throw.x"
  382. }