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