RtsMessages.c 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 1998-2004
 *
 * General utility functions used in the RTS.
 *
 * ---------------------------------------------------------------------------*/

#include "PosixSource.h"
#include "Rts.h"

12 13
#include "eventlog/EventLog.h"

14
#include <stdio.h>
Simon Marlow's avatar
Simon Marlow committed
15 16
#include <string.h>
#include <errno.h>
17

18 19 20 21
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif

22 23 24 25 26 27 28 29 30 31
/* -----------------------------------------------------------------------------
   General message generation functions

   All messages should go through here.  We can't guarantee that
   stdout/stderr will be available - e.g. in a Windows program there
   is no console for generating messages, so they have to either go to
   to the debug console, or pop up message boxes.
   -------------------------------------------------------------------------- */

// Default to the stdio implementation of these hooks.
32 33 34
RtsMsgFunction *fatalInternalErrorFn = rtsFatalInternalErrorFn;
RtsMsgFunction *debugMsgFn           = rtsDebugMsgFn;
RtsMsgFunction *errorMsgFn           = rtsErrorMsgFn;
35
RtsMsgFunction *sysErrorMsgFn        = rtsSysErrorMsgFn;
36 37

void
38
barf(const char*s, ...)
39 40 41 42 43 44 45 46 47
{
  va_list ap;
  va_start(ap,s);
  (*fatalInternalErrorFn)(s,ap);
  stg_exit(EXIT_INTERNAL_ERROR); // just in case fatalInternalErrorFn() returns
  va_end(ap);
}

void
48
vbarf(const char*s, va_list ap)
49 50 51 52 53
{
  (*fatalInternalErrorFn)(s,ap);
  stg_exit(EXIT_INTERNAL_ERROR); // just in case fatalInternalErrorFn() returns
}

54
void 
55
_assertFail(const char*filename, unsigned int linenum)
56
{
57
    barf("ASSERTION FAILED: file %s, line %u\n", filename, linenum);
58 59
}

60
void
61
errorBelch(const char*s, ...)
62 63 64 65 66 67 68 69
{
  va_list ap;
  va_start(ap,s);
  (*errorMsgFn)(s,ap);
  va_end(ap);
}

void
70
verrorBelch(const char*s, va_list ap)
71 72 73 74
{
  (*errorMsgFn)(s,ap);
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
void
sysErrorBelch(const char*s, ...)
{
  va_list ap;
  va_start(ap,s);
  (*sysErrorMsgFn)(s,ap);
  va_end(ap);
}

void
vsysErrorBelch(const char*s, va_list ap)
{
  (*sysErrorMsgFn)(s,ap);
}

90
void
91
debugBelch(const char*s, ...)
92 93 94 95 96 97 98 99
{
  va_list ap;
  va_start(ap,s);
  (*debugMsgFn)(s,ap);
  va_end(ap);
}

void
100
vdebugBelch(const char*s, va_list ap)
101 102 103 104 105 106 107 108
{
  (*debugMsgFn)(s,ap);
}

/* -----------------------------------------------------------------------------
   stdio versions of the message functions
   -------------------------------------------------------------------------- */

109 110
#define BUFSIZE 512

Simon Marlow's avatar
Simon Marlow committed
111
#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
112
static int
113
isGUIApp(void)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
{
  PIMAGE_DOS_HEADER pDOSHeader;
  PIMAGE_NT_HEADERS pPEHeader;

  pDOSHeader = (PIMAGE_DOS_HEADER) GetModuleHandleA(NULL);
  if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE)
    return 0;

  pPEHeader = (PIMAGE_NT_HEADERS) ((char *)pDOSHeader + pDOSHeader->e_lfanew);
  if (pPEHeader->Signature != IMAGE_NT_SIGNATURE)
    return 0;

  return (pPEHeader->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
}
#endif

130 131 132
#define xstr(s) str(s)
#define str(s) #s

Simon Marlow's avatar
Simon Marlow committed
133
void GNU_ATTRIBUTE(__noreturn__)
134
rtsFatalInternalErrorFn(const char *s, va_list ap)
135
{
Simon Marlow's avatar
Simon Marlow committed
136
#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
137 138 139 140
  if (isGUIApp())
  {
     char title[BUFSIZE], message[BUFSIZE];

141 142
     snprintf(title,   BUFSIZE, "%s: internal error", prog_name);
     vsnprintf(message, BUFSIZE, s, ap);
143

144 145 146 147 148
     MessageBox(NULL /* hWnd */,
	        message,
	        title,
	        MB_OK | MB_ICONERROR | MB_TASKMODAL
	       );
149 150 151 152 153 154 155 156 157 158 159 160
  }
  else
#endif
  {
     /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
     if (prog_argv != NULL && prog_name != NULL) {
       fprintf(stderr, "%s: internal error: ", prog_name);
     } else {
       fprintf(stderr, "internal error: ");
     }
     vfprintf(stderr, s, ap);
     fprintf(stderr, "\n");
161 162
     fprintf(stderr, "    (GHC version %s for %s)\n", ProjectVersion, xstr(HostPlatform_TYPE));
     fprintf(stderr, "    Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug\n");
163
     fflush(stderr);
164
  }
165

166 167 168 169
#ifdef TRACING
  if (RtsFlags.TraceFlags.tracing == TRACE_EVENTLOG) endEventLogging();
#endif

170 171
  abort();
  // stg_exit(EXIT_INTERNAL_ERROR);
172 173 174
}

void
175
rtsErrorMsgFn(const char *s, va_list ap)
176
{
Simon Marlow's avatar
Simon Marlow committed
177
#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  if (isGUIApp())
  {
     char buf[BUFSIZE];
     int r;

	 r = vsnprintf(buf, BUFSIZE, s, ap);
	 if (r > 0 && r < BUFSIZE) {
		MessageBox(NULL /* hWnd */,
              buf,
              prog_name,
              MB_OK | MB_ICONERROR | MB_TASKMODAL
              );
     }
  }
  else
#endif
  {
     /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
196
     if (prog_name != NULL) {
197 198 199 200 201
       fprintf(stderr, "%s: ", prog_name);
     }
     vfprintf(stderr, s, ap);
     fprintf(stderr, "\n");
  }
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
void
rtsSysErrorMsgFn(const char *s, va_list ap)
{
    char *syserr;

#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
    FormatMessage( 
	FORMAT_MESSAGE_ALLOCATE_BUFFER | 
	FORMAT_MESSAGE_FROM_SYSTEM | 
	FORMAT_MESSAGE_IGNORE_INSERTS,
	NULL,
	GetLastError(),
	MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
	(LPTSTR) &syserr,
	0,
	NULL );

    if (isGUIApp())
    {
	char buf[BUFSIZE];
	int r;
	
	r = vsnprintf(buf, BUFSIZE, s, ap);
	if (r > 0 && r < BUFSIZE) {
	    r = vsnprintf(buf+r, BUFSIZE-r, ": %s", syserr);
	    MessageBox(NULL /* hWnd */,
		       buf,
		       prog_name,
		       MB_OK | MB_ICONERROR | MB_TASKMODAL
		);
	}
    }
    else
#else
    syserr = strerror(errno);
    // ToDo: use strerror_r() if available
#endif
    {
	/* don't fflush(stdout); WORKAROUND bug in Linux glibc */
	if (prog_argv != NULL && prog_name != NULL) {
	    fprintf(stderr, "%s: ", prog_name);
	}
	vfprintf(stderr, s, ap);
	if (syserr) {
248 249 250 251
#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
            // Win32 error messages have a terminating \n
	    fprintf(stderr, ": %s", syserr);
#else
252
	    fprintf(stderr, ": %s\n", syserr);
253
#endif
254 255 256 257 258 259 260 261 262 263
	} else {
	    fprintf(stderr, "\n");
	}
    }

#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
    if (syserr) LocalFree(syserr);
#endif
}

264
void
265
rtsDebugMsgFn(const char *s, va_list ap)
266
{
Simon Marlow's avatar
Simon Marlow committed
267
#if defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  if (isGUIApp())
  {
     char buf[BUFSIZE];
	 int r;

	 r = vsnprintf(buf, BUFSIZE, s, ap);
	 if (r > 0 && r < BUFSIZE) {
       OutputDebugString(buf);
     }
  }
  else
#endif
  {
     /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
     vfprintf(stderr, s, ap);
     fflush(stderr);
  }
285
}