error.c 8.6 KB

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