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