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