Linker: some extra debugging / logging
[ghc.git] / rts / RtsUtils.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2004
4 *
5 * General utility functions used in the RTS.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "RtsAPI.h"
12
13 #include "RtsUtils.h"
14 #include "Ticky.h"
15 #include "Schedule.h"
16 #include "RtsFlags.h"
17
18 #ifdef HAVE_TIME_H
19 #include <time.h>
20 #endif
21
22 /* HACK: On Mac OS X 10.4 (at least), time.h doesn't declare ctime_r with
23 * _POSIX_C_SOURCE. If this is the case, we declare it ourselves.
24 */
25 #if HAVE_CTIME_R && !HAVE_DECL_CTIME_R
26 extern char *ctime_r(const time_t *, char *);
27 #endif
28
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32
33 #ifdef HAVE_GETTIMEOFDAY
34 #include <sys/time.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41
42 #ifdef HAVE_SIGNAL_H
43 #include <signal.h>
44 #endif
45
46 #if defined(THREADED_RTS) && defined(openbsd_HOST_OS) && defined(HAVE_PTHREAD_H)
47 #include <pthread.h>
48 #endif
49
50
51 #if defined(_WIN32)
52 #include <windows.h>
53 #endif
54
55 /* -----------------------------------------------------------------------------
56 Result-checking malloc wrappers.
57 -------------------------------------------------------------------------- */
58
59 void *
60 stgMallocBytes (size_t n, char *msg)
61 {
62 void *space;
63
64 if ((space = malloc(n)) == NULL) {
65 /* Quoting POSIX.1-2008 (which says more or less the same as ISO C99):
66 *
67 * "Upon successful completion with size not equal to 0, malloc() shall
68 * return a pointer to the allocated space. If size is 0, either a null
69 * pointer or a unique pointer that can be successfully passed to free()
70 * shall be returned. Otherwise, it shall return a null pointer and set
71 * errno to indicate the error."
72 *
73 * Consequently, a NULL pointer being returned by `malloc()` for a 0-size
74 * allocation is *not* to be considered an error.
75 */
76 if (n == 0) return NULL;
77
78 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
79 rtsConfig.mallocFailHook((W_) n, msg); /*msg*/
80 stg_exit(EXIT_INTERNAL_ERROR);
81 }
82 return space;
83 }
84
85 void *
86 stgReallocBytes (void *p, size_t n, char *msg)
87 {
88 void *space;
89
90 if ((space = realloc(p, n)) == NULL) {
91 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
92 rtsConfig.mallocFailHook((W_) n, msg); /*msg*/
93 stg_exit(EXIT_INTERNAL_ERROR);
94 }
95 return space;
96 }
97
98 void *
99 stgCallocBytes (size_t n, size_t m, char *msg)
100 {
101 void *space;
102
103 if ((space = calloc(n, m)) == NULL) {
104 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
105 rtsConfig.mallocFailHook((W_) n*m, msg); /*msg*/
106 stg_exit(EXIT_INTERNAL_ERROR);
107 }
108 return space;
109 }
110
111 /* borrowed from the MUSL libc project */
112 char *stgStrndup(const char *s, size_t n)
113 {
114 size_t l = strnlen(s, n);
115 char *d = stgMallocBytes(l+1, "stgStrndup");
116 if (!d) return NULL;
117 memcpy(d, s, l);
118 d[l] = 0;
119 return d;
120 }
121
122
123 /* To simplify changing the underlying allocator used
124 * by stgMallocBytes(), provide stgFree() as well.
125 */
126 void
127 stgFree(void* p)
128 {
129 free(p);
130 }
131
132 /* -----------------------------------------------------------------------------
133 Stack overflow
134
135 Not sure if this belongs here.
136 -------------------------------------------------------------------------- */
137
138 void
139 stackOverflow(StgTSO* tso)
140 {
141 rtsConfig.stackOverflowHook(tso->tot_stack_size * sizeof(W_));
142
143 #if defined(TICKY_TICKY)
144 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
145 #endif
146 }
147
148 void
149 heapOverflow(void)
150 {
151 if (!heap_overflow)
152 {
153 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
154 rtsConfig.outOfHeapHook(0/*unknown request size*/,
155 (W_)RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
156
157 heap_overflow = rtsTrue;
158 }
159 }
160
161 /* -----------------------------------------------------------------------------
162 Get the current time as a string. Used in profiling reports.
163 -------------------------------------------------------------------------- */
164
165 char *
166 time_str(void)
167 {
168 static time_t now = 0;
169 static char nowstr[26];
170
171 if (now == 0) {
172 time(&now);
173 #if HAVE_CTIME_R
174 ctime_r(&now, nowstr);
175 #else
176 strcpy(nowstr, ctime(&now));
177 #endif
178 memmove(nowstr+16,nowstr+19,7);
179 nowstr[21] = '\0'; // removes the \n
180 }
181 return nowstr;
182 }
183
184 /* -----------------------------------------------------------------------------
185 Print large numbers, with punctuation.
186 -------------------------------------------------------------------------- */
187
188 char *
189 showStgWord64(StgWord64 x, char *s, rtsBool with_commas)
190 {
191 if (with_commas) {
192 if (x < (StgWord64)1e3)
193 sprintf(s, "%" FMT_Word64, (StgWord64)x);
194 else if (x < (StgWord64)1e6)
195 sprintf(s, "%" FMT_Word64 ",%03" FMT_Word64,
196 (StgWord64)(x / 1000),
197 (StgWord64)(x % 1000));
198 else if (x < (StgWord64)1e9)
199 sprintf(s, "%" FMT_Word64
200 ",%03" FMT_Word64
201 ",%03" FMT_Word64,
202 (StgWord64)(x / 1e6),
203 (StgWord64)((x / 1000) % 1000),
204 (StgWord64)(x % 1000));
205 else if (x < (StgWord64)1e12)
206 sprintf(s, "%" FMT_Word64
207 ",%03" FMT_Word64
208 ",%03" FMT_Word64
209 ",%03" FMT_Word64,
210 (StgWord64)(x / (StgWord64)1e9),
211 (StgWord64)((x / (StgWord64)1e6) % 1000),
212 (StgWord64)((x / (StgWord64)1e3) % 1000),
213 (StgWord64)(x % 1000));
214 else if (x < (StgWord64)1e15)
215 sprintf(s, "%" FMT_Word64
216 ",%03" FMT_Word64
217 ",%03" FMT_Word64
218 ",%03" FMT_Word64
219 ",%03" FMT_Word64,
220 (StgWord64)(x / (StgWord64)1e12),
221 (StgWord64)((x / (StgWord64)1e9) % 1000),
222 (StgWord64)((x / (StgWord64)1e6) % 1000),
223 (StgWord64)((x / (StgWord64)1e3) % 1000),
224 (StgWord64)(x % 1000));
225 else if (x < (StgWord64)1e18)
226 sprintf(s, "%" FMT_Word64
227 ",%03" FMT_Word64
228 ",%03" FMT_Word64
229 ",%03" FMT_Word64
230 ",%03" FMT_Word64
231 ",%03" FMT_Word64,
232 (StgWord64)(x / (StgWord64)1e15),
233 (StgWord64)((x / (StgWord64)1e12) % 1000),
234 (StgWord64)((x / (StgWord64)1e9) % 1000),
235 (StgWord64)((x / (StgWord64)1e6) % 1000),
236 (StgWord64)((x / (StgWord64)1e3) % 1000),
237 (StgWord64)(x % 1000));
238 else
239 sprintf(s, "%" FMT_Word64
240 ",%03" FMT_Word64
241 ",%03" FMT_Word64
242 ",%03" FMT_Word64
243 ",%03" FMT_Word64
244 ",%03" FMT_Word64
245 ",%03" FMT_Word64,
246 (StgWord64)(x / (StgWord64)1e18),
247 (StgWord64)((x / (StgWord64)1e15) % 1000),
248 (StgWord64)((x / (StgWord64)1e12) % 1000),
249 (StgWord64)((x / (StgWord64)1e9) % 1000),
250 (StgWord64)((x / (StgWord64)1e6) % 1000),
251 (StgWord64)((x / (StgWord64)1e3) % 1000),
252 (StgWord64)(x % 1000));
253 }
254 else {
255 sprintf(s, "%" FMT_Word64, x);
256 }
257 return s;
258 }
259
260
261 // Can be used as a breakpoint to set on every heap check failure.
262 #ifdef DEBUG
263 void
264 heapCheckFail( void )
265 {
266 }
267 #endif
268
269 /*
270 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
271 * pthreads (and possibly others). When linking with -lpthreads, we
272 * have to use pthread_kill to send blockable signals. So use that
273 * when we have a threaded rts. So System.Posix.Signals will call
274 * genericRaise(), rather than raise(3).
275 */
276 int genericRaise(int sig) {
277 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(darwin_HOST_OS))
278 return pthread_kill(pthread_self(), sig);
279 #else
280 return raise(sig);
281 #endif
282 }
283
284 static void mkRtsInfoPair(char *key, char *val) {
285 /* XXX should check for "s, \s etc in key and val */
286 printf(" ,(\"%s\", \"%s\")\n", key, val);
287 }
288
289 /* This little bit of magic allows us to say TOSTRING(SYM) and get
290 * "5" if SYM is 5 */
291 #define TOSTRING2(x) #x
292 #define TOSTRING(x) TOSTRING2(x)
293
294 void printRtsInfo(void) {
295 /* The first entry is just a hack to make it easy to get the
296 * commas right */
297 printf(" [(\"GHC RTS\", \"YES\")\n");
298 mkRtsInfoPair("GHC version", ProjectVersion);
299 mkRtsInfoPair("RTS way", RtsWay);
300 mkRtsInfoPair("Build platform", BuildPlatform);
301 mkRtsInfoPair("Build architecture", BuildArch);
302 mkRtsInfoPair("Build OS", BuildOS);
303 mkRtsInfoPair("Build vendor", BuildVendor);
304 mkRtsInfoPair("Host platform", HostPlatform);
305 mkRtsInfoPair("Host architecture", HostArch);
306 mkRtsInfoPair("Host OS", HostOS);
307 mkRtsInfoPair("Host vendor", HostVendor);
308 mkRtsInfoPair("Target platform", TargetPlatform);
309 mkRtsInfoPair("Target architecture", TargetArch);
310 mkRtsInfoPair("Target OS", TargetOS);
311 mkRtsInfoPair("Target vendor", TargetVendor);
312 mkRtsInfoPair("Word size", TOSTRING(WORD_SIZE_IN_BITS));
313 mkRtsInfoPair("Compiler unregisterised", GhcUnregisterised);
314 mkRtsInfoPair("Tables next to code", GhcEnableTablesNextToCode);
315 printf(" ]\n");
316 }
317
318 // Provides a way for Haskell programs to tell whether they're being
319 // profiled or not. GHCi uses it (see #2197).
320 int rts_isProfiled(void)
321 {
322 #ifdef PROFILING
323 return 1;
324 #else
325 return 0;
326 #endif
327 }
328
329 // Provides a way for Haskell programs to tell whether they're
330 // dynamically-linked or not.
331 int rts_isDynamic(void)
332 {
333 #ifdef DYNAMIC
334 return 1;
335 #else
336 return 0;
337 #endif
338 }
339
340 // Used for detecting a non-empty FPU stack on x86 (see #4914)
341 void checkFPUStack(void)
342 {
343 #ifdef i386_HOST_ARCH
344 static unsigned char buf[108];
345 asm("FSAVE %0":"=m" (buf));
346
347 if(buf[8]!=255 || buf[9]!=255) {
348 errorBelch("NONEMPTY FPU Stack, TAG = %x %x\n",buf[8],buf[9]);
349 abort();
350 }
351 #endif
352 }
353