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