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