Merge /Users/benl/devel/ghc/ghc-head-devel
[ghc.git] / rts / RtsFlags.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The AQUA Project, Glasgow University, 1994-1997
4 * (c) The GHC Team, 1998-2006
5 *
6 * Functions for parsing the argument list.
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #include "PosixSource.h"
11 #include "Rts.h"
12
13 #include "RtsUtils.h"
14 #include "Profiling.h"
15 #include "RtsFlags.h"
16
17 #ifdef HAVE_CTYPE_H
18 #include <ctype.h>
19 #endif
20
21 #include <string.h>
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30
31 // Flag Structure
32 RTS_FLAGS RtsFlags;
33
34 /*
35 * Split argument lists
36 */
37 int prog_argc = 0; /* an "int" so as to match normal "argc" */
38 char **prog_argv = NULL;
39 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
40 char **full_prog_argv = NULL;
41 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
42 int rts_argc = 0; /* ditto */
43 char **rts_argv = NULL;
44 #if defined(mingw32_HOST_OS)
45 // On Windows, we want to use GetCommandLineW rather than argc/argv,
46 // but we need to mutate the command line arguments for withProgName and
47 // friends. The System.Environment module achieves that using this bit of
48 // shared state:
49 int win32_prog_argc = 0;
50 wchar_t **win32_prog_argv = NULL;
51 #endif
52
53 /*
54 * constants, used later
55 */
56 #define RTS 1
57 #define PGM 0
58
59 /* -----------------------------------------------------------------------------
60 Static function decls
61 -------------------------------------------------------------------------- */
62
63 static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum enabled);
64
65 static void normaliseRtsOpts (void);
66
67 static void initStatsFile (FILE *f);
68
69 static int openStatsFile (char *filename, const char *FILENAME_FMT,
70 FILE **file_ret);
71
72 static StgWord64 decodeSize (const char *flag, nat offset,
73 StgWord64 min, StgWord64 max);
74
75 static void bad_option (const char *s);
76
77 #ifdef TRACING
78 static void read_trace_flags(char *arg);
79 #endif
80
81 static void errorUsage (void) GNU_ATTRIBUTE(__noreturn__);
82
83 static char * copyArg (char *arg);
84 static char ** copyArgv (int argc, char *argv[]);
85 static void freeArgv (int argc, char *argv[]);
86
87 /* -----------------------------------------------------------------------------
88 * Command-line option parsing routines.
89 * ---------------------------------------------------------------------------*/
90
91 void initRtsFlagsDefaults(void)
92 {
93 RtsFlags.GcFlags.statsFile = NULL;
94 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
95
96 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
97 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
98 RtsFlags.GcFlags.stkChunkSize = (32 * 1024) / sizeof(W_);
99 RtsFlags.GcFlags.stkChunkBufferSize = (1 * 1024) / sizeof(W_);
100
101 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
102 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
103 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
104 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
105 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
106 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
107 RtsFlags.GcFlags.oldGenFactor = 2;
108 RtsFlags.GcFlags.generations = 2;
109 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
110 RtsFlags.GcFlags.compact = rtsFalse;
111 RtsFlags.GcFlags.compactThreshold = 30.0;
112 RtsFlags.GcFlags.sweep = rtsFalse;
113 #ifdef RTS_GTK_FRONTPANEL
114 RtsFlags.GcFlags.frontpanel = rtsFalse;
115 #endif
116 RtsFlags.GcFlags.idleGCDelayTime = 300; /* millisecs */
117
118 #if osf3_HOST_OS
119 /* ToDo: Perhaps by adjusting this value we can make linking without
120 * -static work (i.e., not generate a core-dumping executable)? */
121 # if SIZEOF_VOID_P == 8
122 RtsFlags.GcFlags.heapBase = 0x180000000L;
123 # else
124 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
125 # endif
126 #else
127 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
128 #endif
129
130 #ifdef DEBUG
131 RtsFlags.DebugFlags.scheduler = rtsFalse;
132 RtsFlags.DebugFlags.interpreter = rtsFalse;
133 RtsFlags.DebugFlags.weak = rtsFalse;
134 RtsFlags.DebugFlags.gccafs = rtsFalse;
135 RtsFlags.DebugFlags.gc = rtsFalse;
136 RtsFlags.DebugFlags.block_alloc = rtsFalse;
137 RtsFlags.DebugFlags.sanity = rtsFalse;
138 RtsFlags.DebugFlags.stable = rtsFalse;
139 RtsFlags.DebugFlags.stm = rtsFalse;
140 RtsFlags.DebugFlags.prof = rtsFalse;
141 RtsFlags.DebugFlags.apply = rtsFalse;
142 RtsFlags.DebugFlags.linker = rtsFalse;
143 RtsFlags.DebugFlags.squeeze = rtsFalse;
144 RtsFlags.DebugFlags.hpc = rtsFalse;
145 RtsFlags.DebugFlags.sparks = rtsFalse;
146 #endif
147
148 #if defined(PROFILING)
149 RtsFlags.CcFlags.doCostCentres = 0;
150 #endif /* PROFILING */
151
152 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
153 RtsFlags.ProfFlags.profileInterval = 100;
154
155 #ifdef PROFILING
156 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
157 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
158 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
159 RtsFlags.ProfFlags.ccsLength = 25;
160 RtsFlags.ProfFlags.modSelector = NULL;
161 RtsFlags.ProfFlags.descrSelector = NULL;
162 RtsFlags.ProfFlags.typeSelector = NULL;
163 RtsFlags.ProfFlags.ccSelector = NULL;
164 RtsFlags.ProfFlags.ccsSelector = NULL;
165 RtsFlags.ProfFlags.retainerSelector = NULL;
166 RtsFlags.ProfFlags.bioSelector = NULL;
167 #endif
168
169 #ifdef TRACING
170 RtsFlags.TraceFlags.tracing = TRACE_NONE;
171 RtsFlags.TraceFlags.timestamp = rtsFalse;
172 RtsFlags.TraceFlags.scheduler = rtsFalse;
173 RtsFlags.TraceFlags.gc = rtsFalse;
174 RtsFlags.TraceFlags.sparks_sampled= rtsFalse;
175 RtsFlags.TraceFlags.sparks_full = rtsFalse;
176 RtsFlags.TraceFlags.user = rtsFalse;
177 #endif
178
179 RtsFlags.MiscFlags.tickInterval = 20; /* In milliseconds */
180 RtsFlags.ConcFlags.ctxtSwitchTime = 20; /* In milliseconds */
181
182 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
183 RtsFlags.MiscFlags.machineReadable = rtsFalse;
184 RtsFlags.MiscFlags.linkerMemBase = 0;
185
186 #ifdef THREADED_RTS
187 RtsFlags.ParFlags.nNodes = 1;
188 RtsFlags.ParFlags.migrate = rtsTrue;
189 RtsFlags.ParFlags.parGcEnabled = 1;
190 RtsFlags.ParFlags.parGcGen = 0;
191 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
192 RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
193 RtsFlags.ParFlags.setAffinity = 0;
194 #endif
195
196 #if defined(THREADED_RTS)
197 RtsFlags.ParFlags.maxLocalSparks = 4096;
198 #endif /* THREADED_RTS */
199
200 #ifdef TICKY_TICKY
201 RtsFlags.TickyFlags.showTickyStats = rtsFalse;
202 RtsFlags.TickyFlags.tickyFile = NULL;
203 #endif
204
205 #ifdef USE_PAPI
206 /* By default no special measurements taken */
207 RtsFlags.PapiFlags.eventType = 0;
208 RtsFlags.PapiFlags.numUserEvents = 0;
209 #endif
210 }
211
212 static const char *
213 usage_text[] = {
214 "",
215 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
216 "",
217 " +RTS Indicates run time system options follow",
218 " -RTS Indicates program arguments follow",
219 " --RTS Indicates that ALL subsequent arguments will be given to the",
220 " program (including any of these RTS flags)",
221 "",
222 "The following run time system options are available:",
223 "",
224 " -? Prints this message and exits; the program is not executed",
225 " --info Print information about the RTS used by this program",
226 "",
227 " -K<size> Sets the maximum stack size (default 8M) Egs: -K32k -K512k",
228 " -ki<size> Sets the initial thread stack size (default 1k) Egs: -ki4k -ki2m",
229 " -kc<size> Sets the stack chunk size (default 32k)",
230 " -kb<size> Sets the stack chunk buffer size (default 1k)",
231 "",
232 " -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
233 " -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
234 " -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
235 " -m<n> Minimum % of heap which must be available (default 3%)",
236 " -G<n> Number of generations (default: 2)",
237 " -c<n> Use in-place compaction instead of copying in the oldest generation",
238 " when live data is at least <n>% of the maximum heap size set with",
239 " -M (default: 30%)",
240 " -c Use in-place compaction for all oldest generation collections",
241 " (the default is to use copying)",
242 " -w Use mark-region for the oldest generation (experimental)",
243 #if defined(THREADED_RTS)
244 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
245 #endif
246 "",
247 " -T Collect GC statistics (useful for in-program statistics access)"
248 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
249 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
250 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
251 #ifdef RTS_GTK_FRONTPANEL
252 " -f Display front panel (requires X11 & GTK+)",
253 #endif
254 "",
255 "",
256 " -Z Don't squeeze out update frames on stack overflow",
257 " -B Sound the bell at the start of each garbage collection",
258 #if defined(PROFILING)
259 "",
260 " -p Time/allocation profile (output file <program>.prof)",
261 " -P More detailed Time/Allocation profile",
262 " -Pa Give information about *all* cost centres",
263
264 # if defined(PROFILING)
265 "",
266 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
267 " break-down: c = cost centre stack (default)",
268 " m = module",
269 " d = closure description",
270 " y = type description",
271 " r = retainer",
272 " b = biography (LAG,DRAG,VOID,USE)",
273 " A subset of closures may be selected thusly:",
274 " -hc<cc>,... specific cost centre(s) (top of stack only)",
275 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
276 " -hm<mod>... all cost centres from the specified modules(s)",
277 " -hd<des>,... closures with specified closure descriptions",
278 " -hy<typ>... closures with specified type descriptions",
279 " -hr<cc>... closures with specified retainers",
280 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
281 "",
282 " -R<size> Set the maximum retainer set size (default: 8)",
283 "",
284 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
285 " (default: 25)",
286 "",
287 " -xt Include threads (TSOs) in a heap profile",
288 "",
289 " -xc Show current cost centre stack on raising an exception",
290 # endif
291 #endif /* PROFILING or PAR */
292
293 #ifdef TRACING
294 "",
295 " -l[flags] Log events in binary format to the file <program>.eventlog",
296 # ifdef DEBUG
297 " -v[flags] Log events to stderr",
298 # endif
299 " where [flags] can contain:",
300 " s scheduler events",
301 " g GC events",
302 " p par spark events (sampled)",
303 " f par spark events (full detail)",
304 " u user events (emitted from Haskell code)",
305 " a all event classes above",
306 # ifdef DEBUG
307 " t add time stamps (only useful with -v)",
308 # endif
309 " -x disable an event class, for any flag above",
310 " the initial enabled event classes are 'sgpu'",
311 #endif
312
313 #if !defined(PROFILING)
314 "",
315 " -hT Heap residency profile (output file <program>.hp)",
316 #endif
317 " -i<sec> Time between heap samples (seconds, default: 0.1)",
318 "",
319 #if defined(TICKY_TICKY)
320 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
321 "",
322 #endif
323 " -C<secs> Context-switch interval in seconds.",
324 " 0 or no argument means switch as often as possible.",
325 " Default: 0.02 sec; resolution is set by -V below.",
326 " -V<secs> Master tick interval in seconds (0 == disable timer).",
327 " This sets the resolution for -C and the profile timer -i.",
328 " Default: 0.02 sec.",
329 "",
330 #if defined(DEBUG)
331 " -Ds DEBUG: scheduler",
332 " -Di DEBUG: interpreter",
333 " -Dw DEBUG: weak",
334 " -DG DEBUG: gccafs",
335 " -Dg DEBUG: gc",
336 " -Db DEBUG: block",
337 " -DS DEBUG: sanity",
338 " -Dt DEBUG: stable",
339 " -Dp DEBUG: prof",
340 " -Da DEBUG: apply",
341 " -Dl DEBUG: linker",
342 " -Dm DEBUG: stm",
343 " -Dz DEBUG: stack squeezing",
344 " -Dc DEBUG: program coverage",
345 " -Dr DEBUG: sparks",
346 "",
347 " NOTE: DEBUG events are sent to stderr by default; add -l to create a",
348 " binary event log file instead.",
349 "",
350 #endif /* DEBUG */
351 #if defined(THREADED_RTS) && !defined(NOSMP)
352 " -N<n> Use <n> processors (default: 1)",
353 " -N Determine the number of processors to use automatically",
354 " -qg[<n>] Use parallel GC only for generations >= <n>",
355 " (default: 0, -qg alone turns off parallel GC)",
356 " -qb[<n>] Use load-balancing in the parallel GC only for generations >= <n>",
357 " (default: 1, -qb alone turns off load-balancing)",
358 " -qa Use the OS to set thread affinity (experimental)",
359 " -qm Don't automatically migrate threads between CPUs",
360 #endif
361 " --install-signal-handlers=<yes|no>",
362 " Install signal handlers (default: yes)",
363 #if defined(THREADED_RTS)
364 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
365 #endif
366 #if defined(x86_64_HOST_ARCH)
367 " -xm Base address to mmap memory in the GHCi linker",
368 " (hex; must be <80000000)",
369 #endif
370 #if defined(USE_PAPI)
371 " -aX CPU performance counter measurements using PAPI",
372 " (use with the -s<file> option). X is one of:",
373 "",
374 /* " y - cycles", */
375 " 1 - level 1 cache misses",
376 " 2 - level 2 cache misses",
377 " b - branch mispredictions",
378 " s - stalled cycles",
379 " e - cache miss and branch misprediction events",
380 " +PAPI_EVENT - collect papi preset event PAPI_EVENT",
381 " #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
382 #endif
383 "",
384 "RTS options may also be specified using the GHCRTS environment variable.",
385 "",
386 "Other RTS options may be available for programs compiled a different way.",
387 "The GHC User's Guide has full details.",
388 "",
389 0
390 };
391
392 STATIC_INLINE rtsBool
393 strequal(const char *a, const char * b)
394 {
395 return(strcmp(a, b) == 0);
396 }
397
398 static void splitRtsFlags(const char *s)
399 {
400 const char *c1, *c2;
401 char *t;
402
403 c1 = s;
404 do {
405 while (isspace(*c1)) { c1++; };
406 c2 = c1;
407 while (!isspace(*c2) && *c2 != '\0') { c2++; };
408
409 if (c1 == c2) { break; }
410
411 t = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
412 strncpy(t, c1, c2-c1);
413 t[c2-c1] = '\0';
414 rts_argv[rts_argc++] = t;
415
416 c1 = c2;
417 } while (*c1 != '\0');
418 }
419
420 /* -----------------------------------------------------------------------------
421 Parse the command line arguments, collecting options for the RTS.
422
423 On return:
424 - argv[] is *modified*, any RTS options have been stripped out
425 - *argc contains the new count of arguments in argv[]
426
427 - rts_argv[] (global) contains a copy of the collected RTS args
428 - rts_argc (global) contains the count of args in rts_argv
429
430 - prog_argv[] (global) contains a copy of the non-RTS args (== argv)
431 - prog_argc (global) contains the count of args in prog_argv
432
433 - prog_name (global) contains the basename of prog_argv[0]
434
435 -------------------------------------------------------------------------- */
436
437 void setupRtsFlags (int *argc, char *argv[],
438 RtsOptsEnabledEnum rtsOptsEnabled,
439 const char *ghc_rts_opts)
440 {
441 nat mode;
442 nat total_arg;
443 nat arg, rts_argc0;
444
445 setProgName (argv);
446 total_arg = *argc;
447 arg = 1;
448
449 *argc = 1;
450 rts_argc = 0;
451
452 rts_argv = stgCallocBytes(total_arg + 1, sizeof (char *), "setupRtsFlags");
453
454 rts_argc0 = rts_argc;
455
456 // process arguments from the ghc_rts_opts global variable first.
457 // (arguments from the GHCRTS environment variable and the command
458 // line override these).
459 {
460 if (ghc_rts_opts != NULL) {
461 splitRtsFlags(ghc_rts_opts);
462 // opts from ghc_rts_opts are always enabled:
463 procRtsOpts(rts_argc0, RtsOptsAll);
464 rts_argc0 = rts_argc;
465 }
466 }
467
468 // process arguments from the GHCRTS environment variable next
469 // (arguments from the command line override these).
470 {
471 char *ghc_rts = getenv("GHCRTS");
472
473 if (ghc_rts != NULL) {
474 if (rtsOptsEnabled == RtsOptsNone) {
475 errorBelch("Warning: Ignoring GHCRTS variable as RTS options are disabled.\n Link with -rtsopts to enable them.");
476 // We don't actually exit, just warn
477 } else {
478 splitRtsFlags(ghc_rts);
479 procRtsOpts(rts_argc0, rtsOptsEnabled);
480 rts_argc0 = rts_argc;
481 }
482 }
483 }
484
485 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
486 // argv[0] must be PGM argument -- leave in argv
487
488 for (mode = PGM; arg < total_arg; arg++) {
489 // The '--RTS' argument disables all future +RTS ... -RTS processing.
490 if (strequal("--RTS", argv[arg])) {
491 arg++;
492 break;
493 }
494 // The '--' argument is passed through to the program, but
495 // disables all further +RTS ... -RTS processing.
496 else if (strequal("--", argv[arg])) {
497 break;
498 }
499 else if (strequal("+RTS", argv[arg])) {
500 mode = RTS;
501 }
502 else if (strequal("-RTS", argv[arg])) {
503 mode = PGM;
504 }
505 else if (mode == RTS) {
506 rts_argv[rts_argc++] = copyArg(argv[arg]);
507 }
508 else {
509 argv[(*argc)++] = argv[arg];
510 }
511 }
512 // process remaining program arguments
513 for (; arg < total_arg; arg++) {
514 argv[(*argc)++] = argv[arg];
515 }
516 argv[*argc] = (char *) 0;
517 rts_argv[rts_argc] = (char *) 0;
518
519 procRtsOpts(rts_argc0, rtsOptsEnabled);
520
521 normaliseRtsOpts();
522
523 setProgArgv(*argc, argv);
524
525 if (RtsFlags.GcFlags.statsFile != NULL) {
526 initStatsFile (RtsFlags.GcFlags.statsFile);
527 }
528 if (RtsFlags.TickyFlags.tickyFile != NULL) {
529 initStatsFile (RtsFlags.GcFlags.statsFile);
530 }
531 }
532
533 /* -----------------------------------------------------------------------------
534 * procRtsOpts: Process rts_argv between rts_argc0 and rts_argc.
535 * -------------------------------------------------------------------------- */
536
537 static void checkSuid(RtsOptsEnabledEnum enabled)
538 {
539 if (enabled == RtsOptsSafeOnly) {
540 #if defined(HAVE_UNISTD_H) && defined(HAVE_SYS_TYPES_H) && !defined(mingw32_HOST_OS)
541 /* This doesn't cover linux/posix capabilities like CAP_DAC_OVERRIDE,
542 we'd have to link with -lcap for that. */
543 if ((getuid() != geteuid()) || (getgid() != getegid())) {
544 errorBelch("RTS options are disabled for setuid binaries. Link with -rtsopts to enable them.");
545 stg_exit(EXIT_FAILURE);
546 }
547 #endif
548 }
549 }
550
551 static void checkUnsafe(RtsOptsEnabledEnum enabled)
552 {
553 if (enabled == RtsOptsSafeOnly) {
554 errorBelch("Most RTS options are disabled. Link with -rtsopts to enable them.");
555 stg_exit(EXIT_FAILURE);
556 }
557 }
558
559 static void procRtsOpts (int rts_argc0, RtsOptsEnabledEnum rtsOptsEnabled)
560 {
561 rtsBool error = rtsFalse;
562 int arg;
563
564 if (!(rts_argc0 < rts_argc)) return;
565
566 if (rtsOptsEnabled == RtsOptsNone) {
567 errorBelch("RTS options are disabled. Link with -rtsopts to enable them.");
568 stg_exit(EXIT_FAILURE);
569 }
570
571 checkSuid(rtsOptsEnabled);
572
573 // Process RTS (rts_argv) part: mainly to determine statsfile
574 for (arg = rts_argc0; arg < rts_argc; arg++) {
575
576 /* We handle RtsOptsSafeOnly mode by declaring each option as
577 either OPTION_SAFE or OPTION_UNSAFE. To make sure we cover
578 every branch we use an option_checked flag which is reset
579 at the start each iteration and checked at the end. */
580 rtsBool option_checked = rtsFalse;
581
582 #define OPTION_SAFE option_checked = rtsTrue;
583 #define OPTION_UNSAFE checkUnsafe(rtsOptsEnabled); option_checked = rtsTrue;
584
585 if (rts_argv[arg][0] != '-') {
586 fflush(stdout);
587 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
588 error = rtsTrue;
589
590 } else {
591
592 switch(rts_argv[arg][1]) {
593
594 /* process: general args, then PROFILING-only ones, then
595 CONCURRENT-only, TICKY-only (same order as defined in
596 RtsFlags.lh); within those groups, mostly in
597 case-insensitive alphabetical order. Final group is
598 x*, which allows for more options.
599 */
600
601 #ifdef TICKY_TICKY
602 # define TICKY_BUILD_ONLY(x) x
603 #else
604 # define TICKY_BUILD_ONLY(x) \
605 errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
606 error = rtsTrue;
607 #endif
608
609 #ifdef PROFILING
610 # define PROFILING_BUILD_ONLY(x) x
611 #else
612 # define PROFILING_BUILD_ONLY(x) \
613 errorBelch("the flag %s requires the program to be built with -prof", rts_argv[arg]); \
614 error = rtsTrue;
615 #endif
616
617 #ifdef TRACING
618 # define TRACING_BUILD_ONLY(x) x
619 #else
620 # define TRACING_BUILD_ONLY(x) \
621 errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
622 error = rtsTrue;
623 #endif
624
625 #ifdef THREADED_RTS
626 # define THREADED_BUILD_ONLY(x) x
627 #else
628 # define THREADED_BUILD_ONLY(x) \
629 errorBelch("the flag %s requires the program to be built with -threaded", rts_argv[arg]); \
630 error = rtsTrue;
631 #endif
632
633 #ifdef DEBUG
634 # define DEBUG_BUILD_ONLY(x) x
635 #else
636 # define DEBUG_BUILD_ONLY(x) \
637 errorBelch("the flag %s requires the program to be built with -debug", rts_argv[arg]); \
638 error = rtsTrue;
639 #endif
640
641 /* =========== GENERAL ========================== */
642 case '?':
643 OPTION_SAFE;
644 error = rtsTrue;
645 break;
646
647 /* This isn't going to allow us to keep related options
648 together as we add more --* flags. We really need a
649 proper options parser. */
650 case '-':
651 if (strequal("install-signal-handlers=yes",
652 &rts_argv[arg][2])) {
653 OPTION_UNSAFE;
654 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
655 }
656 else if (strequal("install-signal-handlers=no",
657 &rts_argv[arg][2])) {
658 OPTION_UNSAFE;
659 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
660 }
661 else if (strequal("machine-readable",
662 &rts_argv[arg][2])) {
663 OPTION_UNSAFE;
664 RtsFlags.MiscFlags.machineReadable = rtsTrue;
665 }
666 else if (strequal("info",
667 &rts_argv[arg][2])) {
668 OPTION_SAFE;
669 printRtsInfo();
670 stg_exit(0);
671 }
672 else {
673 OPTION_SAFE;
674 errorBelch("unknown RTS option: %s",rts_argv[arg]);
675 error = rtsTrue;
676 }
677 break;
678 case 'A':
679 OPTION_UNSAFE;
680 RtsFlags.GcFlags.minAllocAreaSize
681 = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
682 / BLOCK_SIZE;
683 break;
684
685 #ifdef USE_PAPI
686 case 'a':
687 OPTION_UNSAFE;
688 switch(rts_argv[arg][2]) {
689 case '1':
690 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
691 break;
692 case '2':
693 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
694 break;
695 case 'b':
696 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
697 break;
698 case 's':
699 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
700 break;
701 case 'e':
702 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
703 break;
704 case '+':
705 case '#':
706 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
707 errorBelch("maximum number of PAPI events reached");
708 stg_exit(EXIT_FAILURE);
709 }
710 nat eventNum = RtsFlags.PapiFlags.numUserEvents++;
711 char kind = rts_argv[arg][2];
712 nat eventKind = kind == '+' ? PAPI_PRESET_EVENT_KIND : PAPI_NATIVE_EVENT_KIND;
713
714 RtsFlags.PapiFlags.userEvents[eventNum] = rts_argv[arg] + 3;
715 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
716 RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
717 break;
718 default:
719 bad_option( rts_argv[arg] );
720 }
721 break;
722 #endif
723
724 case 'B':
725 OPTION_UNSAFE;
726 RtsFlags.GcFlags.ringBell = rtsTrue;
727 break;
728
729 case 'c':
730 OPTION_UNSAFE;
731 if (rts_argv[arg][2] != '\0') {
732 RtsFlags.GcFlags.compactThreshold =
733 atof(rts_argv[arg]+2);
734 } else {
735 RtsFlags.GcFlags.compact = rtsTrue;
736 }
737 break;
738
739 case 'w':
740 OPTION_UNSAFE;
741 RtsFlags.GcFlags.sweep = rtsTrue;
742 break;
743
744 case 'F':
745 OPTION_UNSAFE;
746 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
747
748 if (RtsFlags.GcFlags.oldGenFactor < 0)
749 bad_option( rts_argv[arg] );
750 break;
751
752 case 'D':
753 OPTION_SAFE;
754 DEBUG_BUILD_ONLY(
755 {
756 char *c;
757
758 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
759 switch (*c) {
760 case 's':
761 RtsFlags.DebugFlags.scheduler = rtsTrue;
762 break;
763 case 'i':
764 RtsFlags.DebugFlags.interpreter = rtsTrue;
765 break;
766 case 'w':
767 RtsFlags.DebugFlags.weak = rtsTrue;
768 break;
769 case 'G':
770 RtsFlags.DebugFlags.gccafs = rtsTrue;
771 break;
772 case 'g':
773 RtsFlags.DebugFlags.gc = rtsTrue;
774 break;
775 case 'b':
776 RtsFlags.DebugFlags.block_alloc = rtsTrue;
777 break;
778 case 'S':
779 RtsFlags.DebugFlags.sanity = rtsTrue;
780 break;
781 case 't':
782 RtsFlags.DebugFlags.stable = rtsTrue;
783 break;
784 case 'p':
785 RtsFlags.DebugFlags.prof = rtsTrue;
786 break;
787 case 'l':
788 RtsFlags.DebugFlags.linker = rtsTrue;
789 break;
790 case 'a':
791 RtsFlags.DebugFlags.apply = rtsTrue;
792 break;
793 case 'm':
794 RtsFlags.DebugFlags.stm = rtsTrue;
795 break;
796 case 'z':
797 RtsFlags.DebugFlags.squeeze = rtsTrue;
798 break;
799 case 'c':
800 RtsFlags.DebugFlags.hpc = rtsTrue;
801 break;
802 case 'r':
803 RtsFlags.DebugFlags.sparks = rtsTrue;
804 break;
805 default:
806 bad_option( rts_argv[arg] );
807 }
808 }
809 // -Dx also turns on -v. Use -l to direct trace
810 // events to the .eventlog file instead.
811 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
812 })
813 break;
814
815 case 'K':
816 OPTION_UNSAFE;
817 RtsFlags.GcFlags.maxStkSize =
818 decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
819 break;
820
821 case 'k':
822 OPTION_UNSAFE;
823 switch(rts_argv[arg][2]) {
824 case 'c':
825 RtsFlags.GcFlags.stkChunkSize =
826 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
827 break;
828 case 'b':
829 RtsFlags.GcFlags.stkChunkBufferSize =
830 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
831 break;
832 case 'i':
833 RtsFlags.GcFlags.initialStkSize =
834 decodeSize(rts_argv[arg], 3, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
835 break;
836 default:
837 RtsFlags.GcFlags.initialStkSize =
838 decodeSize(rts_argv[arg], 2, sizeof(W_), HS_WORD_MAX) / sizeof(W_);
839 break;
840 }
841 break;
842
843 case 'M':
844 OPTION_UNSAFE;
845 RtsFlags.GcFlags.maxHeapSize =
846 decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE;
847 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
848 break;
849
850 case 'm':
851 OPTION_UNSAFE;
852 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
853
854 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
855 RtsFlags.GcFlags.pcFreeHeap > 100)
856 bad_option( rts_argv[arg] );
857 break;
858
859 case 'G':
860 OPTION_UNSAFE;
861 RtsFlags.GcFlags.generations =
862 decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
863 break;
864
865 case 'H':
866 OPTION_UNSAFE;
867 if (rts_argv[arg][2] == '\0') {
868 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsTrue;
869 } else {
870 RtsFlags.GcFlags.heapSizeSuggestion =
871 (nat)(decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE);
872 }
873 break;
874
875 #ifdef RTS_GTK_FRONTPANEL
876 case 'f':
877 OPTION_UNSAFE;
878 RtsFlags.GcFlags.frontpanel = rtsTrue;
879 break;
880 #endif
881
882 case 'I': /* idle GC delay */
883 OPTION_UNSAFE;
884 if (rts_argv[arg][2] == '\0') {
885 /* use default */
886 } else {
887 I_ cst; /* tmp */
888
889 /* Convert to millisecs */
890 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
891 RtsFlags.GcFlags.idleGCDelayTime = cst;
892 }
893 break;
894
895 case 'T':
896 OPTION_SAFE;
897 RtsFlags.GcFlags.giveStats = COLLECT_GC_STATS;
898 break; /* Don't initialize statistics file. */
899
900 case 'S':
901 OPTION_SAFE; /* but see below */
902 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
903 goto stats;
904
905 case 's':
906 OPTION_SAFE; /* but see below */
907 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
908 goto stats;
909
910 case 't':
911 OPTION_SAFE; /* but see below */
912 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
913 goto stats;
914
915 stats:
916 {
917 int r;
918 if (rts_argv[arg][2] != '\0') {
919 OPTION_UNSAFE;
920 }
921 r = openStatsFile(rts_argv[arg]+2, NULL,
922 &RtsFlags.GcFlags.statsFile);
923 if (r == -1) { error = rtsTrue; }
924 }
925 break;
926
927 case 'Z':
928 OPTION_UNSAFE;
929 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
930 break;
931
932 /* =========== PROFILING ========================== */
933
934 case 'P': /* detailed cost centre profiling (time/alloc) */
935 case 'p': /* cost centre profiling (time/alloc) */
936 OPTION_SAFE;
937 PROFILING_BUILD_ONLY(
938 switch (rts_argv[arg][2]) {
939 case 'a':
940 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
941 break;
942 default:
943 if (rts_argv[arg][1] == 'P') {
944 RtsFlags.CcFlags.doCostCentres =
945 COST_CENTRES_VERBOSE;
946 } else {
947 RtsFlags.CcFlags.doCostCentres =
948 COST_CENTRES_SUMMARY;
949 }
950 break;
951 }
952 ) break;
953
954 case 'R':
955 OPTION_SAFE;
956 PROFILING_BUILD_ONLY(
957 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
958 ) break;
959 case 'L':
960 OPTION_SAFE;
961 PROFILING_BUILD_ONLY(
962 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
963 if(RtsFlags.ProfFlags.ccsLength <= 0) {
964 bad_option(rts_argv[arg]);
965 }
966 ) break;
967 case 'h': /* serial heap profile */
968 #if !defined(PROFILING)
969 OPTION_UNSAFE;
970 switch (rts_argv[arg][2]) {
971 case '\0':
972 case 'T':
973 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
974 break;
975 default:
976 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
977 error = rtsTrue;
978 }
979 #else
980 OPTION_SAFE;
981 PROFILING_BUILD_ONLY(
982 switch (rts_argv[arg][2]) {
983 case '\0':
984 case 'C':
985 case 'c':
986 case 'M':
987 case 'm':
988 case 'D':
989 case 'd':
990 case 'Y':
991 case 'y':
992 case 'R':
993 case 'r':
994 case 'B':
995 case 'b':
996 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
997 {
998 char *left = strchr(rts_argv[arg], '{');
999 char *right = strrchr(rts_argv[arg], '}');
1000
1001 // curly braces are optional, for
1002 // backwards compat.
1003 if (left)
1004 left = left+1;
1005 else
1006 left = rts_argv[arg] + 3;
1007
1008 if (!right)
1009 right = rts_argv[arg] + strlen(rts_argv[arg]);
1010
1011 *right = '\0';
1012
1013 switch (rts_argv[arg][2]) {
1014 case 'c': // cost centre label select
1015 RtsFlags.ProfFlags.ccSelector = left;
1016 break;
1017 case 'C':
1018 RtsFlags.ProfFlags.ccsSelector = left;
1019 break;
1020 case 'M':
1021 case 'm': // cost centre module select
1022 RtsFlags.ProfFlags.modSelector = left;
1023 break;
1024 case 'D':
1025 case 'd': // closure descr select
1026 RtsFlags.ProfFlags.descrSelector = left;
1027 break;
1028 case 'Y':
1029 case 'y': // closure type select
1030 RtsFlags.ProfFlags.typeSelector = left;
1031 break;
1032 case 'R':
1033 case 'r': // retainer select
1034 RtsFlags.ProfFlags.retainerSelector = left;
1035 break;
1036 case 'B':
1037 case 'b': // biography select
1038 RtsFlags.ProfFlags.bioSelector = left;
1039 break;
1040 }
1041 }
1042 break;
1043 }
1044
1045 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
1046 errorBelch("multiple heap profile options");
1047 error = rtsTrue;
1048 break;
1049 }
1050
1051 switch (rts_argv[arg][2]) {
1052 case '\0':
1053 case 'C':
1054 case 'c':
1055 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
1056 break;
1057 case 'M':
1058 case 'm':
1059 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
1060 break;
1061 case 'D':
1062 case 'd':
1063 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
1064 break;
1065 case 'Y':
1066 case 'y':
1067 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
1068 break;
1069 case 'R':
1070 case 'r':
1071 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
1072 break;
1073 case 'B':
1074 case 'b':
1075 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
1076 break;
1077 }
1078 break;
1079
1080 default:
1081 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
1082 error = rtsTrue;
1083 }
1084 )
1085 #endif /* PROFILING */
1086 break;
1087
1088 case 'i': /* heap sample interval */
1089 OPTION_UNSAFE;
1090 if (rts_argv[arg][2] == '\0') {
1091 /* use default */
1092 } else {
1093 I_ cst; /* tmp */
1094
1095 /* Convert to milliseconds */
1096 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1097 RtsFlags.ProfFlags.profileInterval = cst;
1098 }
1099 break;
1100
1101 /* =========== CONCURRENT ========================= */
1102 case 'C': /* context switch interval */
1103 OPTION_UNSAFE;
1104 if (rts_argv[arg][2] == '\0')
1105 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1106 else {
1107 I_ cst; /* tmp */
1108
1109 /* Convert to milliseconds */
1110 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1111 RtsFlags.ConcFlags.ctxtSwitchTime = cst;
1112 }
1113 break;
1114
1115 case 'V': /* master tick interval */
1116 OPTION_UNSAFE;
1117 if (rts_argv[arg][2] == '\0') {
1118 // turns off ticks completely
1119 RtsFlags.MiscFlags.tickInterval = 0;
1120 } else {
1121 I_ cst; /* tmp */
1122
1123 /* Convert to milliseconds */
1124 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1125 RtsFlags.MiscFlags.tickInterval = cst;
1126 }
1127 break;
1128
1129 #if !defined(NOSMP)
1130 case 'N':
1131 OPTION_SAFE;
1132 THREADED_BUILD_ONLY(
1133 if (rts_argv[arg][2] == '\0') {
1134 #if defined(PROFILING)
1135 RtsFlags.ParFlags.nNodes = 1;
1136 #else
1137 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
1138 #endif
1139 } else {
1140 int nNodes;
1141 OPTION_SAFE; /* but see extra checks below... */
1142 nNodes = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1143 if (nNodes <= 0) {
1144 errorBelch("bad value for -N");
1145 error = rtsTrue;
1146 }
1147 if (rtsOptsEnabled == RtsOptsSafeOnly &&
1148 nNodes > (int)getNumberOfProcessors()) {
1149 errorBelch("Using large values for -N is not allowed by default. Link with -rtsopts to allow full control.");
1150 stg_exit(EXIT_FAILURE);
1151 }
1152 #if defined(PROFILING)
1153 if (nNodes > 1) {
1154 errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
1155 error = rtsTrue;
1156 }
1157 #endif
1158 RtsFlags.ParFlags.nNodes = (nat)nNodes;
1159 }
1160 ) break;
1161
1162 case 'g':
1163 OPTION_UNSAFE;
1164 THREADED_BUILD_ONLY(
1165 switch (rts_argv[arg][2]) {
1166 case '1':
1167 // backwards compat only
1168 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1169 break;
1170 default:
1171 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1172 error = rtsTrue;
1173 break;
1174 }
1175 ) break;
1176
1177 case 'q':
1178 OPTION_UNSAFE;
1179 THREADED_BUILD_ONLY(
1180 switch (rts_argv[arg][2]) {
1181 case '\0':
1182 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1183 error = rtsTrue;
1184 break;
1185 case 'g':
1186 if (rts_argv[arg][3] == '\0') {
1187 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1188 } else {
1189 RtsFlags.ParFlags.parGcEnabled = rtsTrue;
1190 RtsFlags.ParFlags.parGcGen
1191 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1192 }
1193 break;
1194 case 'b':
1195 if (rts_argv[arg][3] == '\0') {
1196 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsFalse;
1197 }
1198 else {
1199 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
1200 RtsFlags.ParFlags.parGcLoadBalancingGen
1201 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1202 }
1203 break;
1204 case 'a':
1205 RtsFlags.ParFlags.setAffinity = rtsTrue;
1206 break;
1207 case 'm':
1208 RtsFlags.ParFlags.migrate = rtsFalse;
1209 break;
1210 case 'w':
1211 // -qw was removed; accepted for backwards compat
1212 break;
1213 default:
1214 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1215 error = rtsTrue;
1216 break;
1217 }
1218 ) break;
1219 #endif
1220 /* =========== PARALLEL =========================== */
1221 case 'e':
1222 OPTION_UNSAFE;
1223 THREADED_BUILD_ONLY(
1224 if (rts_argv[arg][2] != '\0') {
1225 RtsFlags.ParFlags.maxLocalSparks
1226 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1227 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1228 errorBelch("bad value for -e");
1229 error = rtsTrue;
1230 }
1231 }
1232 ) break;
1233
1234 /* =========== TICKY ============================== */
1235
1236 case 'r': /* Basic profiling stats */
1237 OPTION_SAFE;
1238 TICKY_BUILD_ONLY(
1239
1240 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1241
1242 {
1243 int r;
1244 if (rts_argv[arg][2] != '\0') {
1245 OPTION_UNSAFE;
1246 }
1247 r = openStatsFile(rts_argv[arg]+2,
1248 TICKY_FILENAME_FMT,
1249 &RtsFlags.TickyFlags.tickyFile);
1250 if (r == -1) { error = rtsTrue; }
1251 }
1252 ) break;
1253
1254 /* =========== TRACING ---------=================== */
1255
1256 case 'l':
1257 OPTION_SAFE;
1258 TRACING_BUILD_ONLY(
1259 RtsFlags.TraceFlags.tracing = TRACE_EVENTLOG;
1260 read_trace_flags(&rts_argv[arg][2]);
1261 );
1262 break;
1263
1264 case 'v':
1265 OPTION_SAFE;
1266 DEBUG_BUILD_ONLY(
1267 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
1268 read_trace_flags(&rts_argv[arg][2]);
1269 );
1270 break;
1271
1272 /* =========== EXTENDED OPTIONS =================== */
1273
1274 case 'x': /* Extend the argument space */
1275 switch(rts_argv[arg][2]) {
1276 case '\0':
1277 OPTION_SAFE;
1278 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1279 error = rtsTrue;
1280 break;
1281
1282 case 'b': /* heapBase in hex; undocumented */
1283 OPTION_UNSAFE;
1284 if (rts_argv[arg][3] != '\0') {
1285 RtsFlags.GcFlags.heapBase
1286 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1287 } else {
1288 errorBelch("-xb: requires argument");
1289 error = rtsTrue;
1290 }
1291 break;
1292
1293 #if defined(x86_64_HOST_ARCH)
1294 case 'm': /* linkerMemBase */
1295 OPTION_UNSAFE;
1296 if (rts_argv[arg][3] != '\0') {
1297 RtsFlags.MiscFlags.linkerMemBase
1298 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1299 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1300 errorBelch("-xm: value must be <80000000");
1301 error = rtsTrue;
1302 }
1303 } else {
1304 RtsFlags.MiscFlags.linkerMemBase = 0;
1305 }
1306 break;
1307 #endif
1308
1309 case 'c': /* Debugging tool: show current cost centre on an exception */
1310 OPTION_SAFE;
1311 PROFILING_BUILD_ONLY(
1312 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1313 );
1314 break;
1315
1316 case 't': /* Include memory used by TSOs in a heap profile */
1317 OPTION_SAFE;
1318 PROFILING_BUILD_ONLY(
1319 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1320 );
1321 break;
1322
1323 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1324
1325 default:
1326 OPTION_SAFE;
1327 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1328 error = rtsTrue;
1329 break;
1330 }
1331 break; /* defensive programming */
1332
1333 /* =========== OH DEAR ============================ */
1334 default:
1335 OPTION_SAFE;
1336 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1337 error = rtsTrue;
1338 break;
1339 }
1340
1341 if (!option_checked) {
1342 /* Naughty! Someone didn't use OPTION_UNSAFE / OPTION_SAFE for
1343 an option above */
1344 errorBelch("Internal error in the RTS options parser");
1345 stg_exit(EXIT_FAILURE);
1346 }
1347 }
1348 }
1349
1350 if (error) errorUsage();
1351 }
1352
1353 /* -----------------------------------------------------------------------------
1354 * normaliseRtsOpts: Set some derived values, and make sure things are
1355 * within sensible ranges.
1356 * -------------------------------------------------------------------------- */
1357
1358 static void normaliseRtsOpts (void)
1359 {
1360 if (RtsFlags.MiscFlags.tickInterval < 0) {
1361 RtsFlags.MiscFlags.tickInterval = 50;
1362 }
1363
1364 // If the master timer is disabled, turn off the other timers.
1365 if (RtsFlags.MiscFlags.tickInterval == 0) {
1366 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1367 RtsFlags.GcFlags.idleGCDelayTime = 0;
1368 RtsFlags.ProfFlags.profileInterval = 0;
1369 }
1370
1371 // Determine what tick interval we should use for the RTS timer
1372 // by taking the shortest of the various intervals that we need to
1373 // monitor.
1374 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1375 RtsFlags.MiscFlags.tickInterval =
1376 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1377 RtsFlags.MiscFlags.tickInterval);
1378 }
1379
1380 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1381 RtsFlags.MiscFlags.tickInterval =
1382 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1383 RtsFlags.MiscFlags.tickInterval);
1384 }
1385
1386 if (RtsFlags.ProfFlags.profileInterval > 0) {
1387 RtsFlags.MiscFlags.tickInterval =
1388 stg_min(RtsFlags.ProfFlags.profileInterval,
1389 RtsFlags.MiscFlags.tickInterval);
1390 }
1391
1392 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1393 RtsFlags.ConcFlags.ctxtSwitchTicks =
1394 RtsFlags.ConcFlags.ctxtSwitchTime /
1395 RtsFlags.MiscFlags.tickInterval;
1396 } else {
1397 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1398 }
1399
1400 if (RtsFlags.ProfFlags.profileInterval > 0) {
1401 RtsFlags.ProfFlags.profileIntervalTicks =
1402 RtsFlags.ProfFlags.profileInterval /
1403 RtsFlags.MiscFlags.tickInterval;
1404 } else {
1405 RtsFlags.ProfFlags.profileIntervalTicks = 0;
1406 }
1407
1408 if (RtsFlags.GcFlags.stkChunkBufferSize >
1409 RtsFlags.GcFlags.stkChunkSize / 2) {
1410 errorBelch("stack chunk buffer size (-kb) must be less than 50%% of the stack chunk size (-kc)");
1411 errorUsage();
1412 }
1413 }
1414
1415 static void errorUsage (void)
1416 {
1417 const char **p;
1418
1419 fflush(stdout);
1420 for (p = usage_text; *p; p++)
1421 errorBelch("%s", *p);
1422 stg_exit(EXIT_FAILURE);
1423 }
1424
1425 static void
1426 stats_fprintf(FILE *f, char *s, ...)
1427 {
1428 va_list ap;
1429 va_start(ap,s);
1430 if (f == NULL) {
1431 vdebugBelch(s, ap);
1432 } else {
1433 vfprintf(f, s, ap);
1434 }
1435 va_end(ap);
1436 }
1437
1438 /* -----------------------------------------------------------------------------
1439 * openStatsFile: open a file in which to put some runtime stats
1440 * -------------------------------------------------------------------------- */
1441
1442 static int // return -1 on error
1443 openStatsFile (char *filename, // filename, or NULL
1444 const char *filename_fmt, // if filename == NULL, use
1445 // this fmt with sprintf to
1446 // generate the filename. %s
1447 // expands to the program name.
1448 FILE **file_ret) // return the FILE*
1449 {
1450 FILE *f = NULL;
1451
1452 if (strequal(filename, "stderr")
1453 || (filename_fmt == NULL && *filename == '\0')) {
1454 f = NULL; /* NULL means use debugBelch */
1455 } else {
1456 if (*filename != '\0') { /* stats file specified */
1457 f = fopen(filename,"w");
1458 } else {
1459 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1460 sprintf(stats_filename, filename_fmt, prog_name);
1461 f = fopen(stats_filename,"w");
1462 }
1463 if (f == NULL) {
1464 errorBelch("Can't open stats file %s\n", filename);
1465 return -1;
1466 }
1467 }
1468 *file_ret = f;
1469
1470 return 0;
1471 }
1472
1473 /* -----------------------------------------------------------------------------
1474 * initStatsFile: write a line to the file containing the program name
1475 * and the arguments it was invoked with.
1476 -------------------------------------------------------------------------- */
1477
1478 static void initStatsFile (FILE *f)
1479 {
1480 /* Write prog_argv and rts_argv into start of stats file */
1481 int count;
1482 for (count = 0; count < prog_argc; count++) {
1483 stats_fprintf(f, "%s ", prog_argv[count]);
1484 }
1485 stats_fprintf(f, "+RTS ");
1486 for (count = 0; count < rts_argc; count++)
1487 stats_fprintf(f, "%s ", rts_argv[count]);
1488 stats_fprintf(f, "\n");
1489 }
1490
1491 /* -----------------------------------------------------------------------------
1492 * decodeSize: parse a string containing a size, like 300K or 1.2M
1493 -------------------------------------------------------------------------- */
1494
1495 static StgWord64
1496 decodeSize(const char *flag, nat offset, StgWord64 min, StgWord64 max)
1497 {
1498 char c;
1499 const char *s;
1500 StgDouble m;
1501 StgWord64 val;
1502
1503 s = flag + offset;
1504
1505 if (!*s)
1506 {
1507 m = 0;
1508 }
1509 else
1510 {
1511 m = atof(s);
1512 c = s[strlen(s)-1];
1513
1514 if (c == 'g' || c == 'G')
1515 m *= 1024*1024*1024;
1516 else if (c == 'm' || c == 'M')
1517 m *= 1024*1024;
1518 else if (c == 'k' || c == 'K')
1519 m *= 1024;
1520 else if (c == 'w' || c == 'W')
1521 m *= sizeof(W_);
1522 }
1523
1524 val = (StgWord64)m;
1525
1526 if (m < 0 || val < min || val > max) {
1527 // printf doesn't like 64-bit format specs on Windows
1528 // apparently, so fall back to unsigned long.
1529 errorBelch("error in RTS option %s: size outside allowed range (%lu - %lu)", flag, (lnat)min, (lnat)max);
1530 stg_exit(EXIT_FAILURE);
1531 }
1532
1533 return val;
1534 }
1535
1536 #if defined(TRACING)
1537 static void read_trace_flags(char *arg)
1538 {
1539 char *c;
1540 rtsBool enabled = rtsTrue;
1541 /* Syntax for tracing flags currently looks like:
1542 *
1543 * -l To turn on eventlog tracing with default trace classes
1544 * -lx Turn on class 'x' (for some class listed below)
1545 * -l-x Turn off class 'x'
1546 * -la Turn on all classes
1547 * -l-a Turn off all classes
1548 *
1549 * This lets users say things like:
1550 * -la-p "all but sparks"
1551 * -l-ap "only sparks"
1552 */
1553
1554 /* Start by turning on the default tracing flags.
1555 *
1556 * Currently this is all the trace classes, except full-detail sparks.
1557 * Similarly, in future we might default to slightly less verbose
1558 * scheduler or GC tracing.
1559 */
1560 RtsFlags.TraceFlags.scheduler = rtsTrue;
1561 RtsFlags.TraceFlags.gc = rtsTrue;
1562 RtsFlags.TraceFlags.sparks_sampled = rtsTrue;
1563 RtsFlags.TraceFlags.user = rtsTrue;
1564
1565 for (c = arg; *c != '\0'; c++) {
1566 switch(*c) {
1567 case '\0':
1568 break;
1569 case '-':
1570 enabled = rtsFalse;
1571 break;
1572 case 'a':
1573 RtsFlags.TraceFlags.scheduler = enabled;
1574 RtsFlags.TraceFlags.gc = enabled;
1575 RtsFlags.TraceFlags.sparks_sampled = enabled;
1576 RtsFlags.TraceFlags.sparks_full = enabled;
1577 RtsFlags.TraceFlags.user = enabled;
1578 enabled = rtsTrue;
1579 break;
1580
1581 case 's':
1582 RtsFlags.TraceFlags.scheduler = enabled;
1583 enabled = rtsTrue;
1584 break;
1585 case 'p':
1586 RtsFlags.TraceFlags.sparks_sampled = enabled;
1587 enabled = rtsTrue;
1588 break;
1589 case 'f':
1590 RtsFlags.TraceFlags.sparks_full = enabled;
1591 enabled = rtsTrue;
1592 break;
1593 case 't':
1594 RtsFlags.TraceFlags.timestamp = enabled;
1595 enabled = rtsTrue;
1596 break;
1597 case 'g':
1598 RtsFlags.TraceFlags.gc = enabled;
1599 enabled = rtsTrue;
1600 break;
1601 case 'u':
1602 RtsFlags.TraceFlags.user = enabled;
1603 enabled = rtsTrue;
1604 break;
1605 default:
1606 errorBelch("unknown trace option: %c",*c);
1607 break;
1608 }
1609 }
1610 }
1611 #endif
1612
1613 static void GNU_ATTRIBUTE(__noreturn__)
1614 bad_option(const char *s)
1615 {
1616 errorBelch("bad RTS option: %s", s);
1617 stg_exit(EXIT_FAILURE);
1618 }
1619
1620 /* ----------------------------------------------------------------------------
1621 Copying and freeing argc/argv
1622 ------------------------------------------------------------------------- */
1623
1624 static char * copyArg(char *arg)
1625 {
1626 char *new_arg = stgMallocBytes(strlen(arg) + 1, "copyArg");
1627 strcpy(new_arg, arg);
1628 return new_arg;
1629 }
1630
1631 static char ** copyArgv(int argc, char *argv[])
1632 {
1633 int i;
1634 char **new_argv;
1635
1636 new_argv = stgCallocBytes(argc + 1, sizeof (char *), "copyArgv 1");
1637 for (i = 0; i < argc; i++) {
1638 new_argv[i] = copyArg(argv[i]);
1639 }
1640 new_argv[argc] = NULL;
1641 return new_argv;
1642 }
1643
1644 static void freeArgv(int argc, char *argv[])
1645 {
1646 int i;
1647 if (argv != NULL) {
1648 for (i = 0; i < argc; i++) {
1649 stgFree(argv[i]);
1650 }
1651 stgFree(argv);
1652 }
1653 }
1654
1655 /* -----------------------------------------------------------------------------
1656 Getting/Setting the program's arguments.
1657
1658 These are used by System.Environment, and parts of the RTS.
1659 -------------------------------------------------------------------------- */
1660
1661 void
1662 setProgName(char *argv[])
1663 {
1664 /* Remove directory from argv[0] -- default files in current directory */
1665 #if !defined(mingw32_HOST_OS)
1666 char *last_slash;
1667 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1668 prog_name = last_slash+1;
1669 } else {
1670 prog_name = argv[0];
1671 }
1672 #else
1673 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1674 while ( last_slash > argv[0] ) {
1675 if ( *last_slash == '/' || *last_slash == '\\' ) {
1676 prog_name = last_slash+1;
1677 return;
1678 }
1679 last_slash--;
1680 }
1681 prog_name = argv[0];
1682 #endif
1683 }
1684
1685 void
1686 getProgArgv(int *argc, char **argv[])
1687 {
1688 if (argc) { *argc = prog_argc; }
1689 if (argv) { *argv = prog_argv; }
1690 }
1691
1692 void
1693 setProgArgv(int argc, char *argv[])
1694 {
1695 prog_argc = argc;
1696 prog_argv = copyArgv(argc,argv);
1697 setProgName(prog_argv);
1698 }
1699
1700 static void
1701 freeProgArgv(void)
1702 {
1703 freeArgv(prog_argc,prog_argv);
1704 prog_argc = 0;
1705 prog_argv = NULL;
1706 }
1707
1708 /* ----------------------------------------------------------------------------
1709 The full argv - a copy of the original program's argc/argv
1710 ------------------------------------------------------------------------- */
1711
1712 void
1713 setFullProgArgv(int argc, char *argv[])
1714 {
1715 full_prog_argc = argc;
1716 full_prog_argv = copyArgv(argc,argv);
1717 }
1718
1719 /* These functions record and recall the full arguments, including the
1720 +RTS ... -RTS options. The reason for adding them was so that the
1721 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1722 void
1723 getFullProgArgv(int *argc, char **argv[])
1724 {
1725 if (argc) { *argc = full_prog_argc; }
1726 if (argv) { *argv = full_prog_argv; }
1727 }
1728
1729 void
1730 freeFullProgArgv (void)
1731 {
1732 freeArgv(full_prog_argc, full_prog_argv);
1733 full_prog_argc = 0;
1734 full_prog_argv = NULL;
1735 }
1736
1737 /* ----------------------------------------------------------------------------
1738 The Win32 argv
1739 ------------------------------------------------------------------------- */
1740
1741 #if defined(mingw32_HOST_OS)
1742 void freeWin32ProgArgv (void);
1743
1744 void
1745 freeWin32ProgArgv (void)
1746 {
1747 int i;
1748
1749 if (win32_prog_argv != NULL) {
1750 for (i = 0; i < win32_prog_argc; i++) {
1751 stgFree(win32_prog_argv[i]);
1752 }
1753 stgFree(win32_prog_argv);
1754 }
1755
1756 win32_prog_argc = 0;
1757 win32_prog_argv = NULL;
1758 }
1759
1760 void
1761 getWin32ProgArgv(int *argc, wchar_t **argv[])
1762 {
1763 *argc = win32_prog_argc;
1764 *argv = win32_prog_argv;
1765 }
1766
1767 void
1768 setWin32ProgArgv(int argc, wchar_t *argv[])
1769 {
1770 int i;
1771
1772 freeWin32ProgArgv();
1773
1774 win32_prog_argc = argc;
1775 if (argv == NULL) {
1776 win32_prog_argv = NULL;
1777 return;
1778 }
1779
1780 win32_prog_argv = stgCallocBytes(argc + 1, sizeof (wchar_t *),
1781 "setWin32ProgArgv 1");
1782 for (i = 0; i < argc; i++) {
1783 win32_prog_argv[i] = stgMallocBytes((wcslen(argv[i]) + 1) * sizeof(wchar_t),
1784 "setWin32ProgArgv 2");
1785 wcscpy(win32_prog_argv[i], argv[i]);
1786 }
1787 win32_prog_argv[argc] = NULL;
1788 }
1789 #endif
1790
1791 /* ----------------------------------------------------------------------------
1792 The RTS argv
1793 ------------------------------------------------------------------------- */
1794
1795 static void
1796 freeRtsArgv(void)
1797 {
1798 freeArgv(rts_argc,rts_argv);
1799 rts_argc = 0;
1800 rts_argv = NULL;
1801 }
1802
1803 /* ----------------------------------------------------------------------------
1804 All argvs
1805 ------------------------------------------------------------------------- */
1806
1807 void freeRtsArgs(void)
1808 {
1809 #if defined(mingw32_HOST_OS)
1810 freeWin32ProgArgv();
1811 #endif
1812 freeFullProgArgv();
1813 freeProgArgv();
1814 freeRtsArgv();
1815 }