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