compile_options.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Handling of compile-time options that influence the library.
  2. Copyright (C) 2005-2015 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. 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. backtrace ();
  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. /* Helper function for set_options because we need to access the
  114. global variable options which is not seen in set_options. */
  115. static void
  116. maybe_find_addr2line (void)
  117. {
  118. if (options.backtrace == -1)
  119. find_addr2line ();
  120. }
  121. #endif
  122. /* Set the usual compile-time options. */
  123. extern void set_options (int , int []);
  124. export_proto(set_options);
  125. void
  126. set_options (int num, int options[])
  127. {
  128. if (num >= 1)
  129. compile_options.warn_std = options[0];
  130. if (num >= 2)
  131. compile_options.allow_std = options[1];
  132. if (num >= 3)
  133. compile_options.pedantic = options[2];
  134. /* options[3] is the removed -fdump-core option. It's place in the
  135. options array is retained due to ABI compatibility. Remove when
  136. bumping the library ABI. */
  137. if (num >= 5)
  138. compile_options.backtrace = options[4];
  139. if (num >= 6)
  140. compile_options.sign_zero = options[5];
  141. if (num >= 7)
  142. compile_options.bounds_check = options[6];
  143. /* options[7] is the -frange-check option, which no longer affects
  144. the library behavior; range checking is now always done when
  145. parsing integers. It's place in the options array is retained due
  146. to ABI compatibility. Remove when bumping the library ABI. */
  147. if (num >= 9)
  148. compile_options.fpe_summary = options[8];
  149. #ifndef LIBGFOR_MINIMAL
  150. /* If backtrace is required, we set signal handlers on the POSIX
  151. 2001 signals with core action. */
  152. if (compile_options.backtrace)
  153. {
  154. #if defined(SIGQUIT)
  155. signal (SIGQUIT, backtrace_handler);
  156. #endif
  157. /* The following 4 signals are defined by C89. */
  158. signal (SIGILL, backtrace_handler);
  159. signal (SIGABRT, backtrace_handler);
  160. signal (SIGFPE, backtrace_handler);
  161. signal (SIGSEGV, backtrace_handler);
  162. #if defined(SIGBUS)
  163. signal (SIGBUS, backtrace_handler);
  164. #endif
  165. #if defined(SIGSYS)
  166. signal (SIGSYS, backtrace_handler);
  167. #endif
  168. #if defined(SIGTRAP)
  169. signal (SIGTRAP, backtrace_handler);
  170. #endif
  171. #if defined(SIGXCPU)
  172. signal (SIGXCPU, backtrace_handler);
  173. #endif
  174. #if defined(SIGXFSZ)
  175. signal (SIGXFSZ, backtrace_handler);
  176. #endif
  177. maybe_find_addr2line ();
  178. }
  179. #endif
  180. }
  181. /* Default values for the compile-time options. Keep in sync with
  182. gcc/fortran/options.c (gfc_init_options). */
  183. void
  184. init_compile_options (void)
  185. {
  186. compile_options.warn_std = GFC_STD_F95_DEL | GFC_STD_LEGACY;
  187. compile_options.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
  188. | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
  189. | GFC_STD_F2008_OBS | GFC_STD_GNU | GFC_STD_LEGACY;
  190. compile_options.pedantic = 0;
  191. compile_options.backtrace = 0;
  192. compile_options.sign_zero = 1;
  193. compile_options.fpe_summary = 0;
  194. }
  195. /* Function called by the front-end to tell us the
  196. default for unformatted data conversion. */
  197. extern void set_convert (int);
  198. export_proto (set_convert);
  199. void
  200. set_convert (int conv)
  201. {
  202. compile_options.convert = conv;
  203. }
  204. extern void set_record_marker (int);
  205. export_proto (set_record_marker);
  206. void
  207. set_record_marker (int val)
  208. {
  209. switch(val)
  210. {
  211. case 4:
  212. compile_options.record_marker = sizeof (GFC_INTEGER_4);
  213. break;
  214. case 8:
  215. compile_options.record_marker = sizeof (GFC_INTEGER_8);
  216. break;
  217. default:
  218. runtime_error ("Invalid value for record marker");
  219. break;
  220. }
  221. }
  222. extern void set_max_subrecord_length (int);
  223. export_proto (set_max_subrecord_length);
  224. void set_max_subrecord_length(int val)
  225. {
  226. if (val > GFC_MAX_SUBRECORD_LENGTH || val < 1)
  227. {
  228. runtime_error ("Invalid value for maximum subrecord length");
  229. return;
  230. }
  231. compile_options.max_subrecord_length = val;
  232. }