compile_options.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Handling of compile-time options that influence the library.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include <signal.h>
  21. /* Useful compile-time options will be stored in here. */
  22. compile_options_t compile_options;
  23. #ifndef LIBGFOR_MINIMAL
  24. static volatile sig_atomic_t fatal_error_in_progress = 0;
  25. /* Helper function for backtrace_handler to write information about the
  26. received signal to stderr before actually giving the backtrace. */
  27. static void
  28. show_signal (int signum)
  29. {
  30. const char * name = NULL, * desc = NULL;
  31. switch (signum)
  32. {
  33. #if defined(SIGQUIT)
  34. case SIGQUIT:
  35. name = "SIGQUIT";
  36. desc = "Terminal quit signal";
  37. break;
  38. #endif
  39. /* The following 4 signals are defined by C89. */
  40. case SIGILL:
  41. name = "SIGILL";
  42. desc = "Illegal instruction";
  43. break;
  44. case SIGABRT:
  45. name = "SIGABRT";
  46. desc = "Process abort signal";
  47. break;
  48. case SIGFPE:
  49. name = "SIGFPE";
  50. desc = "Floating-point exception - erroneous arithmetic operation";
  51. break;
  52. case SIGSEGV:
  53. name = "SIGSEGV";
  54. desc = "Segmentation fault - invalid memory reference";
  55. break;
  56. #if defined(SIGBUS)
  57. case SIGBUS:
  58. name = "SIGBUS";
  59. desc = "Access to an undefined portion of a memory object";
  60. break;
  61. #endif
  62. #if defined(SIGSYS)
  63. case SIGSYS:
  64. name = "SIGSYS";
  65. desc = "Bad system call";
  66. break;
  67. #endif
  68. #if defined(SIGTRAP)
  69. case SIGTRAP:
  70. name = "SIGTRAP";
  71. desc = "Trace/breakpoint trap";
  72. break;
  73. #endif
  74. #if defined(SIGXCPU)
  75. case SIGXCPU:
  76. name = "SIGXCPU";
  77. desc = "CPU time limit exceeded";
  78. break;
  79. #endif
  80. #if defined(SIGXFSZ)
  81. case SIGXFSZ:
  82. name = "SIGXFSZ";
  83. desc = "File size limit exceeded";
  84. break;
  85. #endif
  86. }
  87. if (name)
  88. st_printf ("\nProgram received signal %s: %s.\n", name, desc);
  89. else
  90. st_printf ("\nProgram received signal %d.\n", signum);
  91. }
  92. /* A signal handler to allow us to output a backtrace. */
  93. void
  94. backtrace_handler (int signum)
  95. {
  96. /* Since this handler is established for more than one kind of signal,
  97. it might still get invoked recursively by delivery of some other kind
  98. of signal. Use a static variable to keep track of that. */
  99. if (fatal_error_in_progress)
  100. raise (signum);
  101. fatal_error_in_progress = 1;
  102. show_signal (signum);
  103. estr_write ("\nBacktrace for this error:\n");
  104. show_backtrace (true);
  105. /* Now reraise the signal. We reactivate the signal's
  106. default handling, which is to terminate the process.
  107. We could just call exit or abort,
  108. but reraising the signal sets the return status
  109. from the process correctly. */
  110. signal (signum, SIG_DFL);
  111. raise (signum);
  112. }
  113. #endif
  114. /* Set the usual compile-time options. */
  115. extern void set_options (int , int []);
  116. export_proto(set_options);
  117. void
  118. set_options (int num, int options[])
  119. {
  120. if (num >= 1)
  121. compile_options.warn_std = options[0];
  122. if (num >= 2)
  123. compile_options.allow_std = options[1];
  124. if (num >= 3)
  125. compile_options.pedantic = options[2];
  126. if (num >= 4)
  127. compile_options.backtrace = options[3];
  128. if (num >= 5)
  129. compile_options.sign_zero = options[4];
  130. if (num >= 6)
  131. compile_options.bounds_check = options[5];
  132. if (num >= 7)
  133. compile_options.fpe_summary = options[6];
  134. #ifndef LIBGFOR_MINIMAL
  135. /* If backtrace is required, we set signal handlers on the POSIX
  136. 2001 signals with core action. */
  137. if (compile_options.backtrace)
  138. {
  139. #if defined(SIGQUIT)
  140. signal (SIGQUIT, backtrace_handler);
  141. #endif
  142. /* The following 4 signals are defined by C89. */
  143. signal (SIGILL, backtrace_handler);
  144. signal (SIGABRT, backtrace_handler);
  145. signal (SIGFPE, backtrace_handler);
  146. signal (SIGSEGV, backtrace_handler);
  147. #if defined(SIGBUS)
  148. signal (SIGBUS, backtrace_handler);
  149. #endif
  150. #if defined(SIGSYS)
  151. signal (SIGSYS, backtrace_handler);
  152. #endif
  153. #if defined(SIGTRAP)
  154. signal (SIGTRAP, backtrace_handler);
  155. #endif
  156. #if defined(SIGXCPU)
  157. signal (SIGXCPU, backtrace_handler);
  158. #endif
  159. #if defined(SIGXFSZ)
  160. signal (SIGXFSZ, backtrace_handler);
  161. #endif
  162. }
  163. #endif
  164. }
  165. /* Default values for the compile-time options. Keep in sync with
  166. gcc/fortran/options.c (gfc_init_options). */
  167. void
  168. init_compile_options (void)
  169. {
  170. compile_options.warn_std = GFC_STD_F95_DEL | GFC_STD_LEGACY;
  171. compile_options.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
  172. | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
  173. | GFC_STD_F2008_OBS | GFC_STD_GNU | GFC_STD_LEGACY;
  174. compile_options.pedantic = 0;
  175. compile_options.backtrace = 0;
  176. compile_options.sign_zero = 1;
  177. compile_options.fpe_summary = 0;
  178. }
  179. /* Function called by the front-end to tell us the
  180. default for unformatted data conversion. */
  181. extern void set_convert (int);
  182. export_proto (set_convert);
  183. void
  184. set_convert (int conv)
  185. {
  186. compile_options.convert = conv;
  187. }
  188. extern void set_record_marker (int);
  189. export_proto (set_record_marker);
  190. void
  191. set_record_marker (int val)
  192. {
  193. switch(val)
  194. {
  195. case 4:
  196. compile_options.record_marker = sizeof (GFC_INTEGER_4);
  197. break;
  198. case 8:
  199. compile_options.record_marker = sizeof (GFC_INTEGER_8);
  200. break;
  201. default:
  202. runtime_error ("Invalid value for record marker");
  203. break;
  204. }
  205. }
  206. extern void set_max_subrecord_length (int);
  207. export_proto (set_max_subrecord_length);
  208. void set_max_subrecord_length(int val)
  209. {
  210. if (val > GFC_MAX_SUBRECORD_LENGTH || val < 1)
  211. {
  212. runtime_error ("Invalid value for maximum subrecord length");
  213. return;
  214. }
  215. compile_options.max_subrecord_length = val;
  216. }