PageRenderTime 70ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/libgfortran/runtime/error.c

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