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