Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @brief
0007  */
0008 
0009 /*
0010  * Copyright (C) 1995 Tony Bennett <tbennett@divnc.com>
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #ifdef HAVE_CONFIG_H
0035 #include "config.h"
0036 #endif
0037 
0038 #include <rtems.h>
0039 #include <rtems/error.h>
0040 #include <rtems/assoc.h>
0041 #include <rtems/score/sysstate.h>
0042 #include <rtems/score/threadimpl.h>
0043 #include <inttypes.h>
0044 #include <stdio.h>
0045 #include <stdarg.h>
0046 #include <errno.h>
0047 #include <stdlib.h>
0048 #include <string.h>
0049 #include <unistd.h>     /* _exit() */
0050 
0051 int          rtems_panic_in_progress;
0052 
0053 int rtems_verror(
0054   rtems_error_code_t  error_flag,
0055   const char         *printf_format,
0056   va_list             arglist
0057 )
0058 {
0059   int               local_errno = 0;
0060   int               chars_written = 0;
0061   rtems_status_code status;
0062 
0063   if (error_flag & RTEMS_ERROR_PANIC) {
0064     if (rtems_panic_in_progress++)
0065       _Thread_Dispatch_disable();       /* disable task switches */
0066 
0067     /* don't aggravate things */
0068     if (rtems_panic_in_progress > 2)
0069       return 0;
0070   }
0071 
0072   (void) fflush(stdout);            /* in case stdout/stderr same */
0073 
0074   status = error_flag & ~RTEMS_ERROR_MASK;
0075   if (error_flag & RTEMS_ERROR_ERRNO)     /* include errno? */
0076     local_errno = errno;
0077 
0078   #if defined(RTEMS_MULTIPROCESSING)
0079     if (_System_state_Is_multiprocessing)
0080       fprintf(stderr, "[%" PRIu16 "] ", rtems_object_get_local_node());
0081   #endif
0082 
0083   chars_written += vfprintf(stderr, printf_format, arglist);
0084 
0085   if (status)
0086     chars_written +=
0087       fprintf(stderr, " (status: %s)", rtems_status_text(status));
0088 
0089   if (local_errno) {
0090     if ((local_errno > 0) && *strerror(local_errno))
0091       chars_written += fprintf(stderr, " (errno: %s)", strerror(local_errno));
0092     else
0093       chars_written += fprintf(stderr, " (unknown errno=%d)", local_errno);
0094   }
0095 
0096   chars_written += fprintf(stderr, "\n");
0097 
0098   (void) fflush(stderr);
0099 
0100   return chars_written;
0101 }
0102 
0103 int rtems_error(
0104   rtems_error_code_t error_flag,
0105   const char *printf_format,
0106   ...
0107 )
0108 {
0109   va_list arglist;
0110   int chars_written;
0111 
0112   va_start(arglist, printf_format);
0113   chars_written = rtems_verror(error_flag, printf_format, arglist);
0114   va_end(arglist);
0115 
0116   if (error_flag & RTEMS_ERROR_PANIC) {
0117     rtems_error(0, "fatal error, exiting");
0118     _exit(errno);
0119   }
0120   if (error_flag & RTEMS_ERROR_ABORT) {
0121     rtems_error(0, "fatal error, aborting");
0122     abort();
0123   }
0124 
0125   return chars_written;
0126 }