error.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2004, 2006, 2010,
  2. * 2012, 2013 Free Software Foundation, Inc.
  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 License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * 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
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include "libguile/_scm.h"
  26. #include "libguile/dynwind.h"
  27. #include "libguile/pairs.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/throw.h"
  30. #include "libguile/validate.h"
  31. #include "libguile/error.h"
  32. #ifdef HAVE_STRING_H
  33. #include <string.h>
  34. #endif
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. /* For Windows... */
  39. #ifdef HAVE_IO_H
  40. #include <io.h>
  41. #endif
  42. /* {Errors and Exceptional Conditions}
  43. */
  44. /* Scheme interface to scm_error_scm. */
  45. void
  46. scm_error (SCM key, const char *subr, const char *message, SCM args, SCM rest)
  47. {
  48. scm_error_scm
  49. (key,
  50. (subr == NULL) ? SCM_BOOL_F : scm_from_locale_string (subr),
  51. (message == NULL) ? SCM_BOOL_F : scm_from_locale_string (message),
  52. args, rest);
  53. }
  54. /* All errors should pass through here. */
  55. SCM_DEFINE (scm_error_scm, "scm-error", 5, 0, 0,
  56. (SCM key, SCM subr, SCM message, SCM args, SCM data),
  57. "Raise an error with key @var{key}. @var{subr} can be a string\n"
  58. "naming the procedure associated with the error, or @code{#f}.\n"
  59. "@var{message} is the error message string, possibly containing\n"
  60. "@code{~S} and @code{~A} escapes. When an error is reported,\n"
  61. "these are replaced by formatting the corresponding members of\n"
  62. "@var{args}: @code{~A} (was @code{%s} in older versions of\n"
  63. "Guile) formats using @code{display} and @code{~S} (was\n"
  64. "@code{%S}) formats using @code{write}. @var{data} is a list or\n"
  65. "@code{#f} depending on @var{key}: if @var{key} is\n"
  66. "@code{system-error} then it should be a list containing the\n"
  67. "Unix @code{errno} value; If @var{key} is @code{signal} then it\n"
  68. "should be a list containing the Unix signal number; If\n"
  69. "@var{key} is @code{out-of-range}, @code{wrong-type-arg},\n"
  70. "or @code{keyword-argument-error}, "
  71. "it is a list containing the bad value; otherwise\n"
  72. "it will usually be @code{#f}.")
  73. #define FUNC_NAME s_scm_error_scm
  74. {
  75. if (scm_gc_running_p)
  76. {
  77. /* The error occured during GC --- abort */
  78. fprintf (stderr, "Guile: error during GC.\n"),
  79. abort ();
  80. }
  81. scm_ithrow (key, scm_list_4 (subr, message, args, data), 1);
  82. /* No return, but just in case: */
  83. fprintf (stderr, "Guile scm_ithrow returned!\n");
  84. exit (EXIT_FAILURE);
  85. }
  86. #undef FUNC_NAME
  87. /* strerror may not be thread safe, for instance in glibc (version 2.3.2) an
  88. error number not among the known values results in a string like "Unknown
  89. error 9999" formed in a static buffer, which will be overwritten by a
  90. similar call in another thread. A test program running two threads with
  91. different unknown error numbers can trip this fairly quickly.
  92. Some systems don't do what glibc does, instead just giving a single
  93. "Unknown error" for unrecognised numbers. It doesn't seem worth trying
  94. to tell if that's the case, a mutex is reasonably fast, and strerror
  95. isn't needed very often.
  96. strerror_r (when available) could be used, it might be a touch faster
  97. than a frame and a mutex, though there's probably not much
  98. difference. */
  99. SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0,
  100. (SCM err),
  101. "Return the Unix error message corresponding to @var{err}, which\n"
  102. "must be an integer value.")
  103. #define FUNC_NAME s_scm_strerror
  104. {
  105. SCM ret;
  106. scm_dynwind_begin (0);
  107. scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
  108. ret = scm_from_locale_string (strerror (scm_to_int (err)));
  109. scm_dynwind_end ();
  110. return ret;
  111. }
  112. #undef FUNC_NAME
  113. SCM_GLOBAL_SYMBOL (scm_system_error_key, "system-error");
  114. void
  115. scm_syserror (const char *subr)
  116. {
  117. SCM err = scm_from_int (errno);
  118. /* It could be that we're getting here because the syscall was
  119. interrupted by a signal. In that case a signal handler might have
  120. been queued to run. The signal handler probably throws an
  121. exception.
  122. If we don't try to run the signal handler now, it will run later,
  123. which would result in two exceptions being thrown: this syserror,
  124. and then at some later time the exception thrown by the async
  125. signal handler.
  126. The problem is that we don't know if handling the signal caused an
  127. async to be queued. By this time scmsigs.c:take_signal will have
  128. written a byte on the fd, but we don't know if the signal-handling
  129. thread has read it off and queued an async.
  130. Ideally we need some API like scm_i_ensure_signals_delivered() to
  131. catch up signal delivery. Barring that, we just cross our digits
  132. and pray; it could be that we handle the signal in time, and just
  133. throw once, or it could be that we miss the deadline and throw
  134. twice.
  135. */
  136. #ifdef EINTR
  137. if (scm_to_int (err) == EINTR)
  138. SCM_ASYNC_TICK;
  139. #endif
  140. scm_error (scm_system_error_key,
  141. subr,
  142. "~A",
  143. scm_cons (scm_strerror (err), SCM_EOL),
  144. scm_cons (err, SCM_EOL));
  145. }
  146. void
  147. scm_syserror_msg (const char *subr, const char *message, SCM args, int eno)
  148. {
  149. /* See above note about the EINTR signal handling race. */
  150. #ifdef EINTR
  151. if (eno == EINTR)
  152. SCM_ASYNC_TICK;
  153. #endif
  154. scm_error (scm_system_error_key,
  155. subr,
  156. message,
  157. args,
  158. scm_cons (scm_from_int (eno), SCM_EOL));
  159. }
  160. SCM_GLOBAL_SYMBOL (scm_num_overflow_key, "numerical-overflow");
  161. void
  162. scm_num_overflow (const char *subr)
  163. {
  164. scm_error (scm_num_overflow_key,
  165. subr,
  166. "Numerical overflow",
  167. SCM_BOOL_F,
  168. SCM_BOOL_F);
  169. }
  170. SCM_GLOBAL_SYMBOL (scm_out_of_range_key, "out-of-range");
  171. void
  172. scm_out_of_range (const char *subr, SCM bad_value)
  173. {
  174. scm_error (scm_out_of_range_key,
  175. subr,
  176. "Value out of range: ~S",
  177. scm_list_1 (bad_value),
  178. scm_list_1 (bad_value));
  179. }
  180. void
  181. scm_out_of_range_pos (const char *subr, SCM bad_value, SCM pos)
  182. {
  183. scm_error (scm_out_of_range_key,
  184. subr,
  185. "Argument ~A out of range: ~S",
  186. scm_list_2 (pos, bad_value),
  187. scm_list_1 (bad_value));
  188. }
  189. SCM_GLOBAL_SYMBOL (scm_args_number_key, "wrong-number-of-args");
  190. void
  191. scm_wrong_num_args (SCM proc)
  192. {
  193. scm_error (scm_args_number_key,
  194. NULL,
  195. "Wrong number of arguments to ~A",
  196. scm_list_1 (proc),
  197. SCM_BOOL_F);
  198. }
  199. void
  200. scm_error_num_args_subr (const char *subr)
  201. {
  202. scm_error (scm_args_number_key,
  203. NULL,
  204. "Wrong number of arguments to ~A",
  205. scm_list_1 (scm_from_locale_string (subr)),
  206. SCM_BOOL_F);
  207. }
  208. SCM_GLOBAL_SYMBOL (scm_arg_type_key, "wrong-type-arg");
  209. void
  210. scm_wrong_type_arg (const char *subr, int pos, SCM bad_value)
  211. {
  212. scm_error (scm_arg_type_key,
  213. subr,
  214. (pos == 0) ? "Wrong type: ~S"
  215. : "Wrong type argument in position ~A: ~S",
  216. (pos == 0) ? scm_list_1 (bad_value)
  217. : scm_list_2 (scm_from_int (pos), bad_value),
  218. scm_list_1 (bad_value));
  219. }
  220. void
  221. scm_i_wrong_type_arg_symbol (SCM symbol, int pos, SCM bad_value)
  222. {
  223. scm_error_scm (scm_arg_type_key,
  224. scm_symbol_to_string (symbol),
  225. (pos == 0) ? scm_from_locale_string ("Wrong type: ~S")
  226. : scm_from_locale_string ("Wrong type argument in position ~A: ~S"),
  227. (pos == 0) ? scm_list_1 (bad_value)
  228. : scm_list_2 (scm_from_int (pos), bad_value),
  229. scm_list_1 (bad_value));
  230. scm_remember_upto_here_2 (symbol, bad_value);
  231. }
  232. void
  233. scm_wrong_type_arg_msg (const char *subr, int pos, SCM bad_value, const char *szMessage)
  234. {
  235. SCM msg = scm_from_locale_string (szMessage);
  236. if (pos == 0)
  237. {
  238. scm_error (scm_arg_type_key,
  239. subr, "Wrong type (expecting ~A): ~S",
  240. scm_list_2 (msg, bad_value),
  241. scm_list_1 (bad_value));
  242. }
  243. else
  244. {
  245. scm_error (scm_arg_type_key,
  246. subr,
  247. "Wrong type argument in position ~A (expecting ~A): ~S",
  248. scm_list_3 (scm_from_int (pos), msg, bad_value),
  249. scm_list_1 (bad_value));
  250. }
  251. }
  252. SCM_GLOBAL_SYMBOL (scm_memory_alloc_key, "memory-allocation-error");
  253. void
  254. scm_memory_error (const char *subr)
  255. {
  256. fprintf (stderr, "FATAL: memory error in %s\n", subr);
  257. abort ();
  258. }
  259. SCM_GLOBAL_SYMBOL (scm_misc_error_key, "misc-error");
  260. void
  261. scm_misc_error (const char *subr, const char *message, SCM args)
  262. {
  263. scm_error (scm_misc_error_key, subr, message, args, SCM_BOOL_F);
  264. }
  265. void
  266. scm_init_error ()
  267. {
  268. #include "libguile/cpp-E.c"
  269. #include "libguile/error.x"
  270. }
  271. /*
  272. Local Variables:
  273. c-file-style: "gnu"
  274. End:
  275. */