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