1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * General utility functions used in the RTS.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
21 /* HACK: On Mac OS X 10.4 (at least), time.h doesn't declare ctime_r with
22 * _POSIX_C_SOURCE. If this is the case, we declare it ourselves.
24 #if HAVE_CTIME_R && !HAVE_DECL_CTIME_R
25 extern char *ctime_r(const time_t *, char *);
32 #ifdef HAVE_GETTIMEOFDAY
45 #if defined(THREADED_RTS) && defined(openbsd_HOST_OS) && defined(HAVE_PTHREAD_H)
54 /* -----------------------------------------------------------------------------
55 Result-checking malloc wrappers.
56 -------------------------------------------------------------------------- */
59 stgMallocBytes (int n
, char *msg
)
65 if ((space
= (char *) malloc(n2
)) == NULL
) {
66 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
67 MallocFailHook((W_
) n
, msg
); /*msg*/
68 stg_exit(EXIT_INTERNAL_ERROR
);
74 stgReallocBytes (void *p
, int n
, char *msg
)
80 if ((space
= (char *) realloc(p
, (size_t) n2
)) == NULL
) {
81 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
82 MallocFailHook((W_
) n
, msg
); /*msg*/
83 stg_exit(EXIT_INTERNAL_ERROR
);
89 stgCallocBytes (int n
, int m
, char *msg
)
93 if ((space
= (char *) calloc((size_t) n
, (size_t) m
)) == NULL
) {
94 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
95 MallocFailHook((W_
) n
*m
, msg
); /*msg*/
96 stg_exit(EXIT_INTERNAL_ERROR
);
101 /* To simplify changing the underlying allocator used
102 * by stgMallocBytes(), provide stgFree() as well.
110 /* -----------------------------------------------------------------------------
113 Not sure if this belongs here.
114 -------------------------------------------------------------------------- */
117 stackOverflow(StgTSO
* tso
)
119 StackOverflowHook(tso
->tot_stack_size
* sizeof(W_
));
121 #if defined(TICKY_TICKY)
122 if (RtsFlags
.TickyFlags
.showTickyStats
) PrintTickyInfo();
131 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
132 OutOfHeapHook(0/*unknown request size*/,
133 (W_
)RtsFlags
.GcFlags
.maxHeapSize
* BLOCK_SIZE
);
135 heap_overflow
= rtsTrue
;
139 /* -----------------------------------------------------------------------------
140 Get the current time as a string. Used in profiling reports.
141 -------------------------------------------------------------------------- */
146 static time_t now
= 0;
147 static char nowstr
[26];
152 ctime_r(&now
, nowstr
);
154 strcpy(nowstr
, ctime(&now
));
156 memmove(nowstr
+16,nowstr
+19,7);
157 nowstr
[21] = '\0'; // removes the \n
162 /* -----------------------------------------------------------------------------
163 Print large numbers, with punctuation.
164 -------------------------------------------------------------------------- */
167 showStgWord64(StgWord64 x
, char *s
, rtsBool with_commas
)
170 if (x
< (StgWord64
)1e3
)
171 sprintf(s
, "%" FMT_Word64
, (StgWord64
)x
);
172 else if (x
< (StgWord64
)1e6
)
173 sprintf(s
, "%" FMT_Word64
",%03" FMT_Word64
,
174 (StgWord64
)(x
/ 1000),
175 (StgWord64
)(x
% 1000));
176 else if (x
< (StgWord64
)1e9
)
177 sprintf(s
, "%" FMT_Word64
180 (StgWord64
)(x
/ 1e6
),
181 (StgWord64
)((x
/ 1000) % 1000),
182 (StgWord64
)(x
% 1000));
183 else if (x
< (StgWord64
)1e12
)
184 sprintf(s
, "%" FMT_Word64
188 (StgWord64
)(x
/ (StgWord64
)1e9
),
189 (StgWord64
)((x
/ (StgWord64
)1e6
) % 1000),
190 (StgWord64
)((x
/ (StgWord64
)1e3
) % 1000),
191 (StgWord64
)(x
% 1000));
192 else if (x
< (StgWord64
)1e15
)
193 sprintf(s
, "%" FMT_Word64
198 (StgWord64
)(x
/ (StgWord64
)1e12
),
199 (StgWord64
)((x
/ (StgWord64
)1e9
) % 1000),
200 (StgWord64
)((x
/ (StgWord64
)1e6
) % 1000),
201 (StgWord64
)((x
/ (StgWord64
)1e3
) % 1000),
202 (StgWord64
)(x
% 1000));
203 else if (x
< (StgWord64
)1e18
)
204 sprintf(s
, "%" FMT_Word64
210 (StgWord64
)(x
/ (StgWord64
)1e15
),
211 (StgWord64
)((x
/ (StgWord64
)1e12
) % 1000),
212 (StgWord64
)((x
/ (StgWord64
)1e9
) % 1000),
213 (StgWord64
)((x
/ (StgWord64
)1e6
) % 1000),
214 (StgWord64
)((x
/ (StgWord64
)1e3
) % 1000),
215 (StgWord64
)(x
% 1000));
217 sprintf(s
, "%" FMT_Word64
224 (StgWord64
)(x
/ (StgWord64
)1e18
),
225 (StgWord64
)((x
/ (StgWord64
)1e15
) % 1000),
226 (StgWord64
)((x
/ (StgWord64
)1e12
) % 1000),
227 (StgWord64
)((x
/ (StgWord64
)1e9
) % 1000),
228 (StgWord64
)((x
/ (StgWord64
)1e6
) % 1000),
229 (StgWord64
)((x
/ (StgWord64
)1e3
) % 1000),
230 (StgWord64
)(x
% 1000));
233 sprintf(s
, "%" FMT_Word64
, x
);
239 // Can be used as a breakpoint to set on every heap check failure.
242 heapCheckFail( void )
248 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
249 * pthreads (and possibly others). When linking with -lpthreads, we
250 * have to use pthread_kill to send blockable signals. So use that
251 * when we have a threaded rts. So System.Posix.Signals will call
252 * genericRaise(), rather than raise(3).
254 int genericRaise(int sig
) {
255 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(darwin_HOST_OS))
256 return pthread_kill(pthread_self(), sig
);
262 static void mkRtsInfoPair(char *key
, char *val
) {
263 /* XXX should check for "s, \s etc in key and val */
264 printf(" ,(\"%s\", \"%s\")\n", key
, val
);
267 /* This little bit of magic allows us to say TOSTRING(SYM) and get
269 #define TOSTRING2(x) #x
270 #define TOSTRING(x) TOSTRING2(x)
272 void printRtsInfo(void) {
273 /* The first entry is just a hack to make it easy to get the
275 printf(" [(\"GHC RTS\", \"YES\")\n");
276 mkRtsInfoPair("GHC version", ProjectVersion
);
277 mkRtsInfoPair("RTS way", RtsWay
);
278 mkRtsInfoPair("Build platform", BuildPlatform
);
279 mkRtsInfoPair("Build architecture", BuildArch
);
280 mkRtsInfoPair("Build OS", BuildOS
);
281 mkRtsInfoPair("Build vendor", BuildVendor
);
282 mkRtsInfoPair("Host platform", HostPlatform
);
283 mkRtsInfoPair("Host architecture", HostArch
);
284 mkRtsInfoPair("Host OS", HostOS
);
285 mkRtsInfoPair("Host vendor", HostVendor
);
286 mkRtsInfoPair("Target platform", TargetPlatform
);
287 mkRtsInfoPair("Target architecture", TargetArch
);
288 mkRtsInfoPair("Target OS", TargetOS
);
289 mkRtsInfoPair("Target vendor", TargetVendor
);
290 mkRtsInfoPair("Word size", TOSTRING(WORD_SIZE_IN_BITS
));
291 mkRtsInfoPair("Compiler unregisterised", GhcUnregisterised
);
292 mkRtsInfoPair("Tables next to code", GhcEnableTablesNextToCode
);
296 // Provides a way for Haskell programs to tell whether they're being
297 // profiled or not. GHCi uses it (see #2197).
298 int rts_isProfiled(void)
307 // Provides a way for Haskell programs to tell whether they're
308 // dynamically-linked or not.
309 int rts_isDynamic(void)
318 // Used for detecting a non-empty FPU stack on x86 (see #4914)
319 void checkFPUStack(void)
322 static unsigned char buf
[108];
323 asm("FSAVE %0":"=m" (buf
));
325 if(buf
[8]!=255 || buf
[9]!=255) {
326 errorBelch("NONEMPTY FPU Stack, TAG = %x %x\n",buf
[8],buf
[9]);