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