1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * General utility functions used in the RTS.
7 * ---------------------------------------------------------------------------*/
9 /* gettimeofday isn't POSIX */
10 /* #include "PosixSource.h" */
28 #ifdef HAVE_GETTIMEOFDAY
37 /* -----------------------------------------------------------------------------
38 General message generation functions
40 All messages should go through here. We can't guarantee that
41 stdout/stderr will be available - e.g. in a Windows program there
42 is no console for generating messages, so they have to either go to
43 to the debug console, or pop up message boxes.
44 -------------------------------------------------------------------------- */
46 RtsMsgFunction *fatalInternalMsgFn = stdioFatalInternalMsgFn;
47 RtsMsgFunction *debugMsgFn = stdioDebugMsgFn;
48 RtsMsgFunction *errorMsgFn = stdioErrorMsgFn;
55 (*fatalInternalMsgFn)(s,ap);
56 stg_exit(EXIT_INTERNAL_ERROR);
61 errorBelch(char *s, ...)
70 debugBelch(char *s, ...)
79 vdebugBelch(char *s, va_list ap)
84 /* -----------------------------------------------------------------------------
85 stdio versions of the message functions
86 -------------------------------------------------------------------------- */
89 stdioFatalInternalMsgFn(char *s, va_list ap)
91 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
92 if (prog_argv != NULL && prog_name != NULL) {
93 fprintf(stderr, "%s: internal error: ", prog_name);
95 fprintf(stderr, "internal error: ");
97 vfprintf(stderr, s, ap);
98 fprintf(stderr, "\n");
99 fprintf(stderr, " Please report this as a bug to glasgow-haskell-bugs@haskell.org,\n or http://www.sourceforge.net/projects/ghc/\n");
104 stdioErrorMsgFn(char *s, va_list ap)
106 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
107 if (prog_argv != NULL && prog_name != NULL) {
108 fprintf(stderr, "%s: ", prog_name);
110 vfprintf(stderr, s, ap);
111 fprintf(stderr, "\n");
115 stdioDebugMsgFn(char *s, va_list ap)
117 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
118 vfprintf(stderr, s, ap);
122 /* -----------------------------------------------------------------------------
123 Result-checking malloc wrappers.
124 -------------------------------------------------------------------------- */
127 stgMallocBytes (int n, char *msg)
131 if ((space = (char *) malloc((size_t) n)) == NULL) {
132 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
133 MallocFailHook((W_) n, msg); /*msg*/
134 stg_exit(EXIT_INTERNAL_ERROR);
140 stgReallocBytes (void *p, int n, char *msg)
144 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
145 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
146 MallocFailHook((W_) n, msg); /*msg*/
147 stg_exit(EXIT_INTERNAL_ERROR);
153 stgCallocBytes (int n, int m, char *msg)
157 char* p = stgMallocBytes(sz, msg);
158 for (i = 0; i < sz; i++) p[i] = 0;
162 /* To simplify changing the underlying allocator used
163 * by stgMallocBytes(), provide stgFree() as well.
172 _stgAssert (char *filename, unsigned int linenum)
175 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
180 /* -----------------------------------------------------------------------------
183 Not sure if this belongs here.
184 -------------------------------------------------------------------------- */
189 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
191 #if defined(TICKY_TICKY)
192 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
199 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
200 OutOfHeapHook(0/*unknown request size*/,
201 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
203 #if defined(TICKY_TICKY)
204 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
207 stg_exit(EXIT_HEAPOVERFLOW);
210 /* -----------------------------------------------------------------------------
213 Used in addr2Integer because the C compiler on x86 chokes on
214 strlen, trying to inline it with not enough registers available.
215 -------------------------------------------------------------------------- */
217 nat stg_strlen(char *s)
226 /* -----------------------------------------------------------------------------
227 genSym stuff, used by GHC itself for its splitting unique supply.
229 ToDo: put this somewhere sensible.
230 ------------------------------------------------------------------------- */
232 static I_ __GenSymCounter = 0;
237 return(__GenSymCounter++);
240 resetGenSymZh(void) /* it's your funeral */
243 return(__GenSymCounter);
246 /* -----------------------------------------------------------------------------
247 Get the current time as a string. Used in profiling reports.
248 -------------------------------------------------------------------------- */
250 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
254 static time_t now = 0;
255 static char nowstr[26];
259 strcpy(nowstr, ctime(&now));
260 strcpy(nowstr+16,nowstr+19);
267 /* -----------------------------------------------------------------------------
268 * Reset a file handle to blocking mode. We do this for the standard
269 * file descriptors before exiting, because the shell doesn't always
271 * -------------------------------------------------------------------------- */
273 #if !defined(mingw32_TARGET_OS)
275 resetNonBlockingFd(int fd)
279 /* clear the non-blocking flag on this file descriptor */
280 fd_flags = fcntl(fd, F_GETFL);
281 if (fd_flags & O_NONBLOCK) {
282 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
287 setNonBlockingFd(int fd)
291 /* clear the non-blocking flag on this file descriptor */
292 fd_flags = fcntl(fd, F_GETFL);
293 if (!(fd_flags & O_NONBLOCK)) {
294 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
298 /* Stub defns -- async / non-blocking IO is not done
299 * via O_NONBLOCK and select() under Win32.
301 void resetNonBlockingFd(int fd STG_UNUSED) {}
302 void setNonBlockingFd(int fd STG_UNUSED) {}
306 static ullong startTime = 0;
308 /* used in a parallel setup */
312 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
315 if (getclock(TIMEOFDAY, &tv) != 0) {
317 fprintf(stderr, "Clock failed\n");
318 stg_exit(EXIT_FAILURE);
320 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
321 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
324 if (gettimeofday(&tv, NULL) != 0) {
326 fprintf(stderr, "Clock failed\n");
327 stg_exit(EXIT_FAILURE);
329 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
332 if ((t = time(NULL)) == (time_t) -1) {
334 fprintf(stderr, "Clock failed\n");
335 stg_exit(EXIT_FAILURE);
337 return t * LL(1000) - startTime;
342 /* -----------------------------------------------------------------------------
343 Print large numbers, with punctuation.
344 -------------------------------------------------------------------------- */
347 ullong_format_string(ullong x, char *s, rtsBool with_commas)
349 if (x < (ullong)1000)
350 sprintf(s, "%lu", (lnat)x);
351 else if (x < (ullong)1000000)
352 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
353 (lnat)((x)/(ullong)1000),
354 (lnat)((x)%(ullong)1000));
355 else if (x < (ullong)1000000000)
356 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
357 (lnat)((x)/(ullong)1000000),
358 (lnat)((x)/(ullong)1000%(ullong)1000),
359 (lnat)((x)%(ullong)1000));
361 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
362 (lnat)((x)/(ullong)1000000000),
363 (lnat)((x)/(ullong)1000000%(ullong)1000),
364 (lnat)((x)/(ullong)1000%(ullong)1000),
365 (lnat)((x)%(ullong)1000));
370 // Can be used as a breakpoint to set on every heap check failure.
373 heapCheckFail( void )