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