throw.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <unistdio.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/eval.h"
  26. #include "libguile/eq.h"
  27. #include "libguile/control.h"
  28. #include "libguile/deprecation.h"
  29. #include "libguile/backtrace.h"
  30. #include "libguile/debug.h"
  31. #include "libguile/stackchk.h"
  32. #include "libguile/stacks.h"
  33. #include "libguile/fluids.h"
  34. #include "libguile/ports.h"
  35. #include "libguile/validate.h"
  36. #include "libguile/vm.h"
  37. #include "libguile/throw.h"
  38. #include "libguile/init.h"
  39. #include "libguile/strings.h"
  40. #include "libguile/private-options.h"
  41. /* Pleasantly enough, the guts of catch are defined in Scheme, in terms of
  42. prompt, abort, and the %exception-handler fluid. This file just provides
  43. shims so that it's easy to have catch functionality from C.
  44. All of these function names and prototypes carry a fair bit of historical
  45. baggage. */
  46. #define CACHE_VAR(var,name) \
  47. static SCM var = SCM_BOOL_F; \
  48. if (scm_is_false (var)) \
  49. { \
  50. var = scm_module_variable (scm_the_root_module (), \
  51. scm_from_latin1_symbol (name)); \
  52. if (scm_is_false (var)) \
  53. abort (); \
  54. }
  55. SCM
  56. scm_catch (SCM key, SCM thunk, SCM handler)
  57. {
  58. CACHE_VAR (var, "catch");
  59. return scm_call_3 (scm_variable_ref (var), key, thunk, handler);
  60. }
  61. SCM
  62. scm_catch_with_pre_unwind_handler (SCM key, SCM thunk, SCM handler,
  63. SCM pre_unwind_handler)
  64. {
  65. if (SCM_UNBNDP (pre_unwind_handler))
  66. return scm_catch (key, thunk, handler);
  67. else
  68. {
  69. CACHE_VAR (var, "catch");
  70. return scm_call_4 (scm_variable_ref (var), key, thunk, handler,
  71. pre_unwind_handler);
  72. }
  73. }
  74. SCM
  75. scm_with_throw_handler (SCM key, SCM thunk, SCM handler)
  76. {
  77. CACHE_VAR (var, "with-throw-handler");
  78. return scm_call_3 (scm_variable_ref (var), key, thunk, handler);
  79. }
  80. SCM
  81. scm_throw (SCM key, SCM args)
  82. {
  83. CACHE_VAR (var, "throw");
  84. return scm_apply_1 (scm_variable_ref (var), key, args);
  85. }
  86. /* Now some support for C bodies and catch handlers */
  87. static scm_t_bits tc16_catch_closure;
  88. enum {
  89. CATCH_CLOSURE_BODY,
  90. CATCH_CLOSURE_HANDLER
  91. };
  92. static SCM
  93. make_catch_body_closure (scm_t_catch_body body, void *body_data)
  94. {
  95. SCM ret;
  96. SCM_NEWSMOB2 (ret, tc16_catch_closure, body, body_data);
  97. SCM_SET_SMOB_FLAGS (ret, CATCH_CLOSURE_BODY);
  98. return ret;
  99. }
  100. static SCM
  101. make_catch_handler_closure (scm_t_catch_handler handler, void *handler_data)
  102. {
  103. SCM ret;
  104. SCM_NEWSMOB2 (ret, tc16_catch_closure, handler, handler_data);
  105. SCM_SET_SMOB_FLAGS (ret, CATCH_CLOSURE_HANDLER);
  106. return ret;
  107. }
  108. static SCM
  109. apply_catch_closure (SCM clo, SCM args)
  110. {
  111. void *data = (void*)SCM_SMOB_DATA_2 (clo);
  112. switch (SCM_SMOB_FLAGS (clo))
  113. {
  114. case CATCH_CLOSURE_BODY:
  115. {
  116. scm_t_catch_body body = (void*)SCM_SMOB_DATA (clo);
  117. return body (data);
  118. }
  119. case CATCH_CLOSURE_HANDLER:
  120. {
  121. scm_t_catch_handler handler = (void*)SCM_SMOB_DATA (clo);
  122. return handler (data, scm_car (args), scm_cdr (args));
  123. }
  124. default:
  125. abort ();
  126. }
  127. }
  128. /* TAG is the catch tag. Typically, this is a symbol, but this
  129. function doesn't actually care about that.
  130. BODY is a pointer to a C function which runs the body of the catch;
  131. this is the code you can throw from. We call it like this:
  132. BODY (BODY_DATA)
  133. where:
  134. BODY_DATA is just the BODY_DATA argument we received; we pass it
  135. through to BODY as its first argument. The caller can make
  136. BODY_DATA point to anything useful that BODY might need.
  137. HANDLER is a pointer to a C function to deal with a throw to TAG,
  138. should one occur. We call it like this:
  139. HANDLER (HANDLER_DATA, THROWN_TAG, THROW_ARGS)
  140. where
  141. HANDLER_DATA is the HANDLER_DATA argument we recevied; it's the
  142. same idea as BODY_DATA above.
  143. THROWN_TAG is the tag that the user threw to; usually this is
  144. TAG, but it could be something else if TAG was #t (i.e., a
  145. catch-all), or the user threw to a jmpbuf.
  146. THROW_ARGS is the list of arguments the user passed to the THROW
  147. function, after the tag.
  148. BODY_DATA is just a pointer we pass through to BODY. HANDLER_DATA
  149. is just a pointer we pass through to HANDLER. We don't actually
  150. use either of those pointers otherwise ourselves. The idea is
  151. that, if our caller wants to communicate something to BODY or
  152. HANDLER, it can pass a pointer to it as MUMBLE_DATA, which BODY and
  153. HANDLER can then use. Think of it as a way to make BODY and
  154. HANDLER closures, not just functions; MUMBLE_DATA points to the
  155. enclosed variables.
  156. Of course, it's up to the caller to make sure that any data a
  157. MUMBLE_DATA needs is protected from GC. A common way to do this is
  158. to make MUMBLE_DATA a pointer to data stored in an automatic
  159. structure variable; since the collector must scan the stack for
  160. references anyway, this assures that any references in MUMBLE_DATA
  161. will be found. */
  162. SCM
  163. scm_c_catch (SCM tag,
  164. scm_t_catch_body body, void *body_data,
  165. scm_t_catch_handler handler, void *handler_data,
  166. scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data)
  167. {
  168. SCM sbody, shandler, spre_unwind_handler;
  169. sbody = make_catch_body_closure (body, body_data);
  170. shandler = make_catch_handler_closure (handler, handler_data);
  171. if (pre_unwind_handler)
  172. spre_unwind_handler = make_catch_handler_closure (pre_unwind_handler,
  173. pre_unwind_handler_data);
  174. else
  175. spre_unwind_handler = SCM_UNDEFINED;
  176. return scm_catch_with_pre_unwind_handler (tag, sbody, shandler,
  177. spre_unwind_handler);
  178. }
  179. SCM
  180. scm_internal_catch (SCM tag,
  181. scm_t_catch_body body, void *body_data,
  182. scm_t_catch_handler handler, void *handler_data)
  183. {
  184. return scm_c_catch (tag,
  185. body, body_data,
  186. handler, handler_data,
  187. NULL, NULL);
  188. }
  189. SCM
  190. scm_c_with_throw_handler (SCM tag,
  191. scm_t_catch_body body,
  192. void *body_data,
  193. scm_t_catch_handler handler,
  194. void *handler_data,
  195. int lazy_catch_p)
  196. {
  197. SCM sbody, shandler;
  198. if (lazy_catch_p)
  199. scm_c_issue_deprecation_warning
  200. ("The LAZY_CATCH_P argument to `scm_c_with_throw_handler' is no longer.\n"
  201. "supported. Instead the handler will be invoked from within the dynamic\n"
  202. "context of the corresponding `throw'.\n"
  203. "\nTHIS COULD CHANGE YOUR PROGRAM'S BEHAVIOR.\n\n"
  204. "Please modify your program to pass 0 as the LAZY_CATCH_P argument,\n"
  205. "and adapt it (if necessary) to expect to be within the dynamic context\n"
  206. "of the throw.");
  207. sbody = make_catch_body_closure (body, body_data);
  208. shandler = make_catch_handler_closure (handler, handler_data);
  209. return scm_with_throw_handler (tag, sbody, shandler);
  210. }
  211. /* body and handler functions for use with any of the above catch variants */
  212. /* This is a body function you can pass to scm_internal_catch if you
  213. want the body to be like Scheme's `catch' --- a thunk.
  214. BODY_DATA is a pointer to a scm_body_thunk_data structure, which
  215. contains the Scheme procedure to invoke as the body, and the tag
  216. we're catching. */
  217. SCM
  218. scm_body_thunk (void *body_data)
  219. {
  220. struct scm_body_thunk_data *c = (struct scm_body_thunk_data *) body_data;
  221. return scm_call_0 (c->body_proc);
  222. }
  223. /* This is a handler function you can pass to scm_internal_catch if
  224. you want the handler to act like Scheme's catch: (throw TAG ARGS ...)
  225. applies a handler procedure to (TAG ARGS ...).
  226. If the user does a throw to this catch, this function runs a
  227. handler procedure written in Scheme. HANDLER_DATA is a pointer to
  228. an SCM variable holding the Scheme procedure object to invoke. It
  229. ought to be a pointer to an automatic variable (i.e., one living on
  230. the stack), or the procedure object should be otherwise protected
  231. from GC. */
  232. SCM
  233. scm_handle_by_proc (void *handler_data, SCM tag, SCM throw_args)
  234. {
  235. SCM *handler_proc_p = (SCM *) handler_data;
  236. return scm_apply_1 (*handler_proc_p, tag, throw_args);
  237. }
  238. /* SCM_HANDLE_BY_PROC_CATCHING_ALL is like SCM_HANDLE_BY_PROC but
  239. catches all throws that the handler might emit itself. The handler
  240. used for these `secondary' throws is SCM_HANDLE_BY_MESSAGE_NO_EXIT. */
  241. struct hbpca_data {
  242. SCM proc;
  243. SCM args;
  244. };
  245. static SCM
  246. hbpca_body (void *body_data)
  247. {
  248. struct hbpca_data *data = (struct hbpca_data *)body_data;
  249. return scm_apply_0 (data->proc, data->args);
  250. }
  251. SCM
  252. scm_handle_by_proc_catching_all (void *handler_data, SCM tag, SCM throw_args)
  253. {
  254. SCM *handler_proc_p = (SCM *) handler_data;
  255. struct hbpca_data data;
  256. data.proc = *handler_proc_p;
  257. data.args = scm_cons (tag, throw_args);
  258. return scm_internal_catch (SCM_BOOL_T,
  259. hbpca_body, &data,
  260. scm_handle_by_message_noexit, NULL);
  261. }
  262. /* Derive the an exit status from the arguments to (quit ...). */
  263. int
  264. scm_exit_status (SCM args)
  265. {
  266. if (scm_is_pair (args))
  267. {
  268. SCM cqa = SCM_CAR (args);
  269. if (scm_is_integer (cqa))
  270. return (scm_to_int (cqa));
  271. else if (scm_is_false (cqa))
  272. return EXIT_FAILURE;
  273. else
  274. return EXIT_SUCCESS;
  275. }
  276. else if (scm_is_null (args))
  277. return EXIT_SUCCESS;
  278. else
  279. /* A type error. Strictly speaking we shouldn't get here. */
  280. return EXIT_FAILURE;
  281. }
  282. static int
  283. should_print_backtrace (SCM tag, SCM stack)
  284. {
  285. return SCM_BACKTRACE_P
  286. && scm_is_true (stack)
  287. && scm_initialized_p
  288. /* It's generally not useful to print backtraces for errors reading
  289. or expanding code in these fallback catch statements. */
  290. && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
  291. && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
  292. }
  293. static void
  294. handler_message (void *handler_data, SCM tag, SCM args)
  295. {
  296. SCM p, stack, frame;
  297. p = scm_current_error_port ();
  298. /* Usually we get here via a throw to a catch-all. In that case
  299. there is the throw frame active, and the catch closure, so narrow by
  300. two frames. It is possible for a user to invoke
  301. scm_handle_by_message directly, though, so it could be this
  302. narrows too much. We'll have to see how this works out in
  303. practice. */
  304. stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
  305. frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
  306. if (should_print_backtrace (tag, stack))
  307. {
  308. scm_puts_unlocked ("Backtrace:\n", p);
  309. scm_display_backtrace_with_highlights (stack, p,
  310. SCM_BOOL_F, SCM_BOOL_F,
  311. SCM_EOL);
  312. scm_newline (p);
  313. }
  314. scm_print_exception (p, frame, tag, args);
  315. }
  316. /* This is a handler function to use if you want scheme to print a
  317. message and die. Useful for dealing with throws to uncaught keys
  318. at the top level.
  319. At boot time, we establish a catch-all that uses this as its handler.
  320. 1) If the user wants something different, they can use (catch #t
  321. ...) to do what they like.
  322. 2) Outside the context of a read-eval-print loop, there isn't
  323. anything else good to do; libguile should not assume the existence
  324. of a read-eval-print loop.
  325. 3) Given that we shouldn't do anything complex, it's much more
  326. robust to do it in C code.
  327. HANDLER_DATA, if non-zero, is assumed to be a char * pointing to a
  328. message header to print; if zero, we use "guile" instead. That
  329. text is followed by a colon, then the message described by ARGS. */
  330. /* Dirk:FIXME:: The name of the function should make clear that the
  331. * application gets terminated.
  332. */
  333. SCM
  334. scm_handle_by_message (void *handler_data, SCM tag, SCM args)
  335. {
  336. if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit"))))
  337. exit (scm_exit_status (args));
  338. handler_message (handler_data, tag, args);
  339. scm_i_pthread_exit (NULL);
  340. /* this point not reached, but suppress gcc warning about no return value
  341. in case scm_i_pthread_exit isn't marked as "noreturn" (which seemed not
  342. to be the case on cygwin for instance) */
  343. return SCM_BOOL_F;
  344. }
  345. /* This is just like scm_handle_by_message, but it doesn't exit; it
  346. just returns #f. It's useful in cases where you don't really know
  347. enough about the body to handle things in a better way, but don't
  348. want to let throws fall off the bottom of the wind list. */
  349. SCM
  350. scm_handle_by_message_noexit (void *handler_data, SCM tag, SCM args)
  351. {
  352. if (scm_is_true (scm_eq_p (tag, scm_from_latin1_symbol ("quit"))))
  353. exit (scm_exit_status (args));
  354. handler_message (handler_data, tag, args);
  355. return SCM_BOOL_F;
  356. }
  357. SCM
  358. scm_handle_by_throw (void *handler_data SCM_UNUSED, SCM tag, SCM args)
  359. {
  360. scm_ithrow (tag, args, 1);
  361. return SCM_UNSPECIFIED; /* never returns */
  362. }
  363. SCM
  364. scm_ithrow (SCM key, SCM args, int noreturn SCM_UNUSED)
  365. {
  366. return scm_throw (key, args);
  367. }
  368. /* Unfortunately we have to support catch and throw before boot-9 has, um,
  369. booted. So here are lame versions, which will get replaced with their scheme
  370. equivalents. */
  371. SCM_SYMBOL (sym_pre_init_catch_tag, "%pre-init-catch-tag");
  372. static SCM
  373. pre_init_catch (SCM tag, SCM thunk, SCM handler, SCM pre_unwind_handler)
  374. {
  375. SCM vm, prompt, res;
  376. /* Only handle catch-alls without pre-unwind handlers */
  377. if (!SCM_UNBNDP (pre_unwind_handler))
  378. abort ();
  379. if (scm_is_false (scm_eqv_p (tag, SCM_BOOL_T)))
  380. abort ();
  381. vm = scm_the_vm ();
  382. prompt = scm_c_make_prompt (sym_pre_init_catch_tag,
  383. SCM_VM_DATA (vm)->fp, SCM_VM_DATA (vm)->sp,
  384. SCM_VM_DATA (vm)->ip, 1, -1, scm_i_dynwinds ());
  385. scm_i_set_dynwinds (scm_cons (prompt, SCM_PROMPT_DYNWINDS (prompt)));
  386. if (SCM_PROMPT_SETJMP (prompt))
  387. {
  388. /* nonlocal exit */
  389. SCM args = scm_i_prompt_pop_abort_args_x (vm);
  390. /* cdr past the continuation */
  391. return scm_apply_0 (handler, scm_cdr (args));
  392. }
  393. res = scm_call_0 (thunk);
  394. scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ()));
  395. return res;
  396. }
  397. static int
  398. find_pre_init_catch (void)
  399. {
  400. SCM winds;
  401. /* Search the wind list for an appropriate prompt.
  402. "Waiter, please bring us the wind list." */
  403. for (winds = scm_i_dynwinds (); scm_is_pair (winds); winds = SCM_CDR (winds))
  404. if (SCM_PROMPT_P (SCM_CAR (winds))
  405. && scm_is_eq (SCM_PROMPT_TAG (SCM_CAR (winds)), sym_pre_init_catch_tag))
  406. return 1;
  407. return 0;
  408. }
  409. static SCM
  410. pre_init_throw (SCM k, SCM args)
  411. {
  412. if (find_pre_init_catch ())
  413. return scm_at_abort (sym_pre_init_catch_tag, scm_cons (k, args));
  414. else
  415. {
  416. static int error_printing_error = 0;
  417. static int error_printing_fallback = 0;
  418. if (error_printing_fallback)
  419. fprintf (stderr, "\nFailed to print exception.\n");
  420. else if (error_printing_error)
  421. {
  422. fprintf (stderr, "\nError while printing exception:\n");
  423. error_printing_fallback = 1;
  424. fprintf (stderr, "Key: ");
  425. scm_write (k, scm_current_error_port ());
  426. fprintf (stderr, ", args: ");
  427. scm_write (args, scm_current_error_port ());
  428. scm_newline (scm_current_error_port ());
  429. }
  430. else
  431. {
  432. fprintf (stderr, "Throw without catch before boot:\n");
  433. error_printing_error = 1;
  434. scm_handle_by_message_noexit (NULL, k, args);
  435. }
  436. fprintf (stderr, "Aborting.\n");
  437. abort ();
  438. return SCM_BOOL_F; /* not reached */
  439. }
  440. }
  441. void
  442. scm_init_throw ()
  443. {
  444. tc16_catch_closure = scm_make_smob_type ("catch-closure", 0);
  445. scm_set_smob_apply (tc16_catch_closure, apply_catch_closure, 0, 0, 1);
  446. scm_c_define ("catch", scm_c_make_gsubr ("catch", 3, 1, 0, pre_init_catch));
  447. scm_c_define ("throw", scm_c_make_gsubr ("throw", 1, 0, 1, pre_init_throw));
  448. #include "libguile/throw.x"
  449. }
  450. /*
  451. Local Variables:
  452. c-file-style: "gnu"
  453. End:
  454. */