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