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