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