error.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  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 <assert.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <signal.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include <stdlib.h>
  28. #ifdef HAVE_SYS_TIME_H
  29. #include <sys/time.h>
  30. #endif
  31. /* <sys/time.h> has to be included before <sys/resource.h> to work
  32. around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
  33. #ifdef HAVE_SYS_RESOURCE_H
  34. #include <sys/resource.h>
  35. #endif
  36. #include <locale.h>
  37. #ifdef HAVE_XLOCALE_H
  38. #include <xlocale.h>
  39. #endif
  40. #ifdef __MINGW32__
  41. #define HAVE_GETPID 1
  42. #include <process.h>
  43. #endif
  44. /* Termination of a program: F2008 2.3.5 talks about "normal
  45. termination" and "error termination". Normal termination occurs as
  46. a result of e.g. executing the end program statement, and executing
  47. the STOP statement. It includes the effect of the C exit()
  48. function.
  49. Error termination is initiated when the ERROR STOP statement is
  50. executed, when ALLOCATE/DEALLOCATE fails without STAT= being
  51. specified, when some of the co-array synchronization statements
  52. fail without STAT= being specified, and some I/O errors if
  53. ERR/IOSTAT/END/EOR is not present, and finally EXECUTE_COMMAND_LINE
  54. failure without CMDSTAT=.
  55. 2.3.5 also explains how co-images synchronize during termination.
  56. In libgfortran we have two ways of ending a program. exit(code) is
  57. a normal exit; calling exit() also causes open units to be
  58. closed. No backtrace or core dump is needed here. When something
  59. goes wrong, we have sys_abort() which tries to print the backtrace
  60. if -fbacktrace is enabled, and then dumps core; whether a core file
  61. is generated is system dependent. When aborting, we don't flush and
  62. close open units, as program memory might be corrupted and we'd
  63. rather risk losing dirty data in the buffers rather than corrupting
  64. files on disk.
  65. */
  66. /* Error conditions. The tricky part here is printing a message when
  67. * it is the I/O subsystem that is severely wounded. Our goal is to
  68. * try and print something making the fewest assumptions possible,
  69. * then try to clean up before actually exiting.
  70. *
  71. * The following exit conditions are defined:
  72. * 0 Normal program exit.
  73. * 1 Terminated because of operating system error.
  74. * 2 Error in the runtime library
  75. * 3 Internal error in runtime library
  76. *
  77. * Other error returns are reserved for the STOP statement with a numeric code.
  78. */
  79. /* Write a null-terminated C string to standard error. This function
  80. is async-signal-safe. */
  81. ssize_t
  82. estr_write (const char *str)
  83. {
  84. return write (STDERR_FILENO, str, strlen (str));
  85. }
  86. /* st_vprintf()-- vsnprintf-like function for error output. We use a
  87. stack allocated buffer for formatting; since this function might be
  88. called from within a signal handler, printing directly to stderr
  89. with vfprintf is not safe since the stderr locking might lead to a
  90. deadlock. */
  91. #define ST_VPRINTF_SIZE 512
  92. int
  93. st_vprintf (const char *format, va_list ap)
  94. {
  95. int written;
  96. char buffer[ST_VPRINTF_SIZE];
  97. #ifdef HAVE_VSNPRINTF
  98. written = vsnprintf(buffer, ST_VPRINTF_SIZE, format, ap);
  99. #else
  100. written = vsprintf(buffer, format, ap);
  101. if (written >= ST_VPRINTF_SIZE - 1)
  102. {
  103. /* The error message was longer than our buffer. Ouch. Because
  104. we may have messed up things badly, report the error and
  105. quit. */
  106. #define ERROR_MESSAGE "Internal error: buffer overrun in st_vprintf()\n"
  107. write (STDERR_FILENO, buffer, ST_VPRINTF_SIZE - 1);
  108. write (STDERR_FILENO, ERROR_MESSAGE, strlen(ERROR_MESSAGE));
  109. sys_abort ();
  110. #undef ERROR_MESSAGE
  111. }
  112. #endif
  113. written = write (STDERR_FILENO, buffer, written);
  114. return written;
  115. }
  116. int
  117. st_printf (const char * format, ...)
  118. {
  119. int written;
  120. va_list ap;
  121. va_start (ap, format);
  122. written = st_vprintf (format, ap);
  123. va_end (ap);
  124. return written;
  125. }
  126. /* sys_abort()-- Terminate the program showing backtrace and dumping
  127. core. */
  128. void
  129. sys_abort (void)
  130. {
  131. /* If backtracing is enabled, print backtrace and disable signal
  132. handler for ABRT. */
  133. if (options.backtrace == 1
  134. || (options.backtrace == -1 && compile_options.backtrace == 1))
  135. {
  136. estr_write ("\nProgram aborted. Backtrace:\n");
  137. backtrace ();
  138. signal (SIGABRT, SIG_DFL);
  139. }
  140. abort();
  141. }
  142. /* gfc_xtoa()-- Integer to hexadecimal conversion. */
  143. const char *
  144. gfc_xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
  145. {
  146. int digit;
  147. char *p;
  148. assert (len >= GFC_XTOA_BUF_SIZE);
  149. if (n == 0)
  150. return "0";
  151. p = buffer + GFC_XTOA_BUF_SIZE - 1;
  152. *p = '\0';
  153. while (n != 0)
  154. {
  155. digit = n & 0xF;
  156. if (digit > 9)
  157. digit += 'A' - '0' - 10;
  158. *--p = '0' + digit;
  159. n >>= 4;
  160. }
  161. return p;
  162. }
  163. /* Hopefully thread-safe wrapper for a strerror() style function. */
  164. char *
  165. gf_strerror (int errnum,
  166. char * buf __attribute__((unused)),
  167. size_t buflen __attribute__((unused)))
  168. {
  169. #ifdef HAVE_STRERROR_L
  170. locale_t myloc = newlocale (LC_CTYPE_MASK | LC_MESSAGES_MASK, "",
  171. (locale_t) 0);
  172. char *p = strerror_l (errnum, myloc);
  173. freelocale (myloc);
  174. return p;
  175. #elif defined(HAVE_STRERROR_R)
  176. #ifdef HAVE_USELOCALE
  177. /* Some targets (Darwin at least) have the POSIX 2008 extended
  178. locale functions, but not strerror_l. So reset the per-thread
  179. locale here. */
  180. uselocale (LC_GLOBAL_LOCALE);
  181. #endif
  182. /* POSIX returns an "int", GNU a "char*". */
  183. return
  184. __builtin_choose_expr (__builtin_classify_type (strerror_r (0, buf, 0))
  185. == 5,
  186. /* GNU strerror_r() */
  187. strerror_r (errnum, buf, buflen),
  188. /* POSIX strerror_r () */
  189. (strerror_r (errnum, buf, buflen), buf));
  190. #elif defined(HAVE_STRERROR_R_2ARGS)
  191. strerror_r (errnum, buf);
  192. return buf;
  193. #else
  194. /* strerror () is not necessarily thread-safe, but should at least
  195. be available everywhere. */
  196. return strerror (errnum);
  197. #endif
  198. }
  199. /* show_locus()-- Print a line number and filename describing where
  200. * something went wrong */
  201. void
  202. show_locus (st_parameter_common *cmp)
  203. {
  204. char *filename;
  205. if (!options.locus || cmp == NULL || cmp->filename == NULL)
  206. return;
  207. if (cmp->unit > 0)
  208. {
  209. filename = filename_from_unit (cmp->unit);
  210. if (filename != NULL)
  211. {
  212. st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
  213. (int) cmp->line, cmp->filename, (int) cmp->unit, filename);
  214. free (filename);
  215. }
  216. else
  217. {
  218. st_printf ("At line %d of file %s (unit = %d)\n",
  219. (int) cmp->line, cmp->filename, (int) cmp->unit);
  220. }
  221. return;
  222. }
  223. st_printf ("At line %d of file %s\n", (int) cmp->line, cmp->filename);
  224. }
  225. /* recursion_check()-- It's possible for additional errors to occur
  226. * during fatal error processing. We detect this condition here and
  227. * exit with code 4 immediately. */
  228. #define MAGIC 0x20DE8101
  229. static void
  230. recursion_check (void)
  231. {
  232. static int magic = 0;
  233. /* Don't even try to print something at this point */
  234. if (magic == MAGIC)
  235. sys_abort ();
  236. magic = MAGIC;
  237. }
  238. #define STRERR_MAXSZ 256
  239. /* os_error()-- Operating system error. We get a message from the
  240. * operating system, show it and leave. Some operating system errors
  241. * are caught and processed by the library. If not, we come here. */
  242. void
  243. os_error (const char *message)
  244. {
  245. char errmsg[STRERR_MAXSZ];
  246. recursion_check ();
  247. estr_write ("Operating system error: ");
  248. estr_write (gf_strerror (errno, errmsg, STRERR_MAXSZ));
  249. estr_write ("\n");
  250. estr_write (message);
  251. estr_write ("\n");
  252. exit (1);
  253. }
  254. iexport(os_error);
  255. /* void runtime_error()-- These are errors associated with an
  256. * invalid fortran program. */
  257. void
  258. runtime_error (const char *message, ...)
  259. {
  260. va_list ap;
  261. recursion_check ();
  262. estr_write ("Fortran runtime error: ");
  263. va_start (ap, message);
  264. st_vprintf (message, ap);
  265. va_end (ap);
  266. estr_write ("\n");
  267. exit (2);
  268. }
  269. iexport(runtime_error);
  270. /* void runtime_error_at()-- These are errors associated with a
  271. * run time error generated by the front end compiler. */
  272. void
  273. runtime_error_at (const char *where, const char *message, ...)
  274. {
  275. va_list ap;
  276. recursion_check ();
  277. estr_write (where);
  278. estr_write ("\nFortran runtime error: ");
  279. va_start (ap, message);
  280. st_vprintf (message, ap);
  281. va_end (ap);
  282. estr_write ("\n");
  283. exit (2);
  284. }
  285. iexport(runtime_error_at);
  286. void
  287. runtime_warning_at (const char *where, const char *message, ...)
  288. {
  289. va_list ap;
  290. estr_write (where);
  291. estr_write ("\nFortran runtime warning: ");
  292. va_start (ap, message);
  293. st_vprintf (message, ap);
  294. va_end (ap);
  295. estr_write ("\n");
  296. }
  297. iexport(runtime_warning_at);
  298. /* void internal_error()-- These are this-can't-happen errors
  299. * that indicate something deeply wrong. */
  300. void
  301. internal_error (st_parameter_common *cmp, const char *message)
  302. {
  303. recursion_check ();
  304. show_locus (cmp);
  305. estr_write ("Internal Error: ");
  306. estr_write (message);
  307. estr_write ("\n");
  308. /* This function call is here to get the main.o object file included
  309. when linking statically. This works because error.o is supposed to
  310. be always linked in (and the function call is in internal_error
  311. because hopefully it doesn't happen too often). */
  312. stupid_function_name_for_static_linking();
  313. exit (3);
  314. }
  315. /* translate_error()-- Given an integer error code, return a string
  316. * describing the error. */
  317. const char *
  318. translate_error (int code)
  319. {
  320. const char *p;
  321. switch (code)
  322. {
  323. case LIBERROR_EOR:
  324. p = "End of record";
  325. break;
  326. case LIBERROR_END:
  327. p = "End of file";
  328. break;
  329. case LIBERROR_OK:
  330. p = "Successful return";
  331. break;
  332. case LIBERROR_OS:
  333. p = "Operating system error";
  334. break;
  335. case LIBERROR_BAD_OPTION:
  336. p = "Bad statement option";
  337. break;
  338. case LIBERROR_MISSING_OPTION:
  339. p = "Missing statement option";
  340. break;
  341. case LIBERROR_OPTION_CONFLICT:
  342. p = "Conflicting statement options";
  343. break;
  344. case LIBERROR_ALREADY_OPEN:
  345. p = "File already opened in another unit";
  346. break;
  347. case LIBERROR_BAD_UNIT:
  348. p = "Unattached unit";
  349. break;
  350. case LIBERROR_FORMAT:
  351. p = "FORMAT error";
  352. break;
  353. case LIBERROR_BAD_ACTION:
  354. p = "Incorrect ACTION specified";
  355. break;
  356. case LIBERROR_ENDFILE:
  357. p = "Read past ENDFILE record";
  358. break;
  359. case LIBERROR_BAD_US:
  360. p = "Corrupt unformatted sequential file";
  361. break;
  362. case LIBERROR_READ_VALUE:
  363. p = "Bad value during read";
  364. break;
  365. case LIBERROR_READ_OVERFLOW:
  366. p = "Numeric overflow on read";
  367. break;
  368. case LIBERROR_INTERNAL:
  369. p = "Internal error in run-time library";
  370. break;
  371. case LIBERROR_INTERNAL_UNIT:
  372. p = "Internal unit I/O error";
  373. break;
  374. case LIBERROR_DIRECT_EOR:
  375. p = "Write exceeds length of DIRECT access record";
  376. break;
  377. case LIBERROR_SHORT_RECORD:
  378. p = "I/O past end of record on unformatted file";
  379. break;
  380. case LIBERROR_CORRUPT_FILE:
  381. p = "Unformatted file structure has been corrupted";
  382. break;
  383. case LIBERROR_INQUIRE_INTERNAL_UNIT:
  384. p = "Inquire statement identifies an internal file";
  385. break;
  386. default:
  387. p = "Unknown error code";
  388. break;
  389. }
  390. return p;
  391. }
  392. /* generate_error()-- Come here when an error happens. This
  393. * subroutine is called if it is possible to continue on after the error.
  394. * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
  395. * ERR labels are present, we return, otherwise we terminate the program
  396. * after printing a message. The error code is always required but the
  397. * message parameter can be NULL, in which case a string describing
  398. * the most recent operating system error is used. */
  399. void
  400. generate_error (st_parameter_common *cmp, int family, const char *message)
  401. {
  402. char errmsg[STRERR_MAXSZ];
  403. /* If there was a previous error, don't mask it with another
  404. error message, EOF or EOR condition. */
  405. if ((cmp->flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_ERROR)
  406. return;
  407. /* Set the error status. */
  408. if ((cmp->flags & IOPARM_HAS_IOSTAT))
  409. *cmp->iostat = (family == LIBERROR_OS) ? errno : family;
  410. if (message == NULL)
  411. message =
  412. (family == LIBERROR_OS) ? gf_strerror (errno, errmsg, STRERR_MAXSZ) :
  413. translate_error (family);
  414. if (cmp->flags & IOPARM_HAS_IOMSG)
  415. cf_strcpy (cmp->iomsg, cmp->iomsg_len, message);
  416. /* Report status back to the compiler. */
  417. cmp->flags &= ~IOPARM_LIBRETURN_MASK;
  418. switch (family)
  419. {
  420. case LIBERROR_EOR:
  421. cmp->flags |= IOPARM_LIBRETURN_EOR;
  422. if ((cmp->flags & IOPARM_EOR))
  423. return;
  424. break;
  425. case LIBERROR_END:
  426. cmp->flags |= IOPARM_LIBRETURN_END;
  427. if ((cmp->flags & IOPARM_END))
  428. return;
  429. break;
  430. default:
  431. cmp->flags |= IOPARM_LIBRETURN_ERROR;
  432. if ((cmp->flags & IOPARM_ERR))
  433. return;
  434. break;
  435. }
  436. /* Return if the user supplied an iostat variable. */
  437. if ((cmp->flags & IOPARM_HAS_IOSTAT))
  438. return;
  439. /* Terminate the program */
  440. recursion_check ();
  441. show_locus (cmp);
  442. estr_write ("Fortran runtime error: ");
  443. estr_write (message);
  444. estr_write ("\n");
  445. exit (2);
  446. }
  447. iexport(generate_error);
  448. /* generate_warning()-- Similar to generate_error but just give a warning. */
  449. void
  450. generate_warning (st_parameter_common *cmp, const char *message)
  451. {
  452. if (message == NULL)
  453. message = " ";
  454. show_locus (cmp);
  455. estr_write ("Fortran runtime warning: ");
  456. estr_write (message);
  457. estr_write ("\n");
  458. }
  459. /* Whether, for a feature included in a given standard set (GFC_STD_*),
  460. we should issue an error or a warning, or be quiet. */
  461. notification
  462. notification_std (int std)
  463. {
  464. int warning;
  465. if (!compile_options.pedantic)
  466. return NOTIFICATION_SILENT;
  467. warning = compile_options.warn_std & std;
  468. if ((compile_options.allow_std & std) != 0 && !warning)
  469. return NOTIFICATION_SILENT;
  470. return warning ? NOTIFICATION_WARNING : NOTIFICATION_ERROR;
  471. }
  472. /* Possibly issue a warning/error about use of a nonstandard (or deleted)
  473. feature. An error/warning will be issued if the currently selected
  474. standard does not contain the requested bits. */
  475. bool
  476. notify_std (st_parameter_common *cmp, int std, const char * message)
  477. {
  478. int warning;
  479. if (!compile_options.pedantic)
  480. return true;
  481. warning = compile_options.warn_std & std;
  482. if ((compile_options.allow_std & std) != 0 && !warning)
  483. return true;
  484. if (!warning)
  485. {
  486. recursion_check ();
  487. show_locus (cmp);
  488. estr_write ("Fortran runtime error: ");
  489. estr_write (message);
  490. estr_write ("\n");
  491. exit (2);
  492. }
  493. else
  494. {
  495. show_locus (cmp);
  496. estr_write ("Fortran runtime warning: ");
  497. estr_write (message);
  498. estr_write ("\n");
  499. }
  500. return false;
  501. }