1 /* -----------------------------------------------------------------------------
3 * (c) The AQUA Project, Glasgow University, 1994-1997
4 * (c) The GHC Team, 1998-2006
6 * Functions for parsing the argument list.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
15 #include "Profiling.h"
27 * Split argument lists
29 int prog_argc = 0; /* an "int" so as to match normal "argc" */
30 char **prog_argv = NULL;
31 int full_prog_argc = 0; /* an "int" so as to match normal "argc" */
32 char **full_prog_argv = NULL;
33 char *prog_name = NULL; /* 'basename' of prog_argv[0] */
34 int rts_argc = 0; /* ditto */
35 char *rts_argv[MAX_RTS_ARGS];
38 * constants, used later
43 /* -----------------------------------------------------------------------------
45 -------------------------------------------------------------------------- */
47 static int /* return NULL on error */
50 int argc, char *argv[],
51 int rts_argc, char *rts_argv[],
52 const char *FILENAME_FMT,
55 static StgWord64 decodeSize(const char *flag, nat offset, StgWord64 min, StgWord64 max);
56 static void bad_option(const char *s);
58 static void read_trace_flags(char *arg);
61 /* -----------------------------------------------------------------------------
62 * Command-line option parsing routines.
63 * ---------------------------------------------------------------------------*/
65 void initRtsFlagsDefaults(void)
67 RtsFlags.GcFlags.statsFile = NULL;
68 RtsFlags.GcFlags.giveStats = NO_GC_STATS;
70 RtsFlags.GcFlags.maxStkSize = (8 * 1024 * 1024) / sizeof(W_);
71 RtsFlags.GcFlags.initialStkSize = 1024 / sizeof(W_);
73 RtsFlags.GcFlags.minAllocAreaSize = (512 * 1024) / BLOCK_SIZE;
74 RtsFlags.GcFlags.minOldGenSize = (1024 * 1024) / BLOCK_SIZE;
75 RtsFlags.GcFlags.maxHeapSize = 0; /* off by default */
76 RtsFlags.GcFlags.heapSizeSuggestion = 0; /* none */
77 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsFalse;
78 RtsFlags.GcFlags.pcFreeHeap = 3; /* 3% */
79 RtsFlags.GcFlags.oldGenFactor = 2;
80 RtsFlags.GcFlags.generations = 2;
81 RtsFlags.GcFlags.squeezeUpdFrames = rtsTrue;
82 RtsFlags.GcFlags.compact = rtsFalse;
83 RtsFlags.GcFlags.compactThreshold = 30.0;
84 RtsFlags.GcFlags.sweep = rtsFalse;
85 #ifdef RTS_GTK_FRONTPANEL
86 RtsFlags.GcFlags.frontpanel = rtsFalse;
88 RtsFlags.GcFlags.idleGCDelayTime = 300; /* millisecs */
91 /* ToDo: Perhaps by adjusting this value we can make linking without
92 * -static work (i.e., not generate a core-dumping executable)? */
93 # if SIZEOF_VOID_P == 8
94 RtsFlags.GcFlags.heapBase = 0x180000000L;
96 # error I have no idea where to begin the heap on a non-64-bit osf3 machine.
99 RtsFlags.GcFlags.heapBase = 0; /* means don't care */
103 RtsFlags.DebugFlags.scheduler = rtsFalse;
104 RtsFlags.DebugFlags.interpreter = rtsFalse;
105 RtsFlags.DebugFlags.weak = rtsFalse;
106 RtsFlags.DebugFlags.gccafs = rtsFalse;
107 RtsFlags.DebugFlags.gc = rtsFalse;
108 RtsFlags.DebugFlags.block_alloc = rtsFalse;
109 RtsFlags.DebugFlags.sanity = rtsFalse;
110 RtsFlags.DebugFlags.stable = rtsFalse;
111 RtsFlags.DebugFlags.stm = rtsFalse;
112 RtsFlags.DebugFlags.prof = rtsFalse;
113 RtsFlags.DebugFlags.apply = rtsFalse;
114 RtsFlags.DebugFlags.linker = rtsFalse;
115 RtsFlags.DebugFlags.squeeze = rtsFalse;
116 RtsFlags.DebugFlags.hpc = rtsFalse;
117 RtsFlags.DebugFlags.sparks = rtsFalse;
120 #if defined(PROFILING)
121 RtsFlags.CcFlags.doCostCentres = 0;
122 #endif /* PROFILING */
124 RtsFlags.ProfFlags.doHeapProfile = rtsFalse;
125 RtsFlags.ProfFlags.profileInterval = 100;
128 RtsFlags.ProfFlags.includeTSOs = rtsFalse;
129 RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
130 RtsFlags.ProfFlags.maxRetainerSetSize = 8;
131 RtsFlags.ProfFlags.ccsLength = 25;
132 RtsFlags.ProfFlags.modSelector = NULL;
133 RtsFlags.ProfFlags.descrSelector = NULL;
134 RtsFlags.ProfFlags.typeSelector = NULL;
135 RtsFlags.ProfFlags.ccSelector = NULL;
136 RtsFlags.ProfFlags.ccsSelector = NULL;
137 RtsFlags.ProfFlags.retainerSelector = NULL;
138 RtsFlags.ProfFlags.bioSelector = NULL;
142 RtsFlags.TraceFlags.tracing = TRACE_NONE;
143 RtsFlags.TraceFlags.timestamp = rtsFalse;
144 RtsFlags.TraceFlags.scheduler = rtsFalse;
147 RtsFlags.MiscFlags.tickInterval = 20; /* In milliseconds */
148 RtsFlags.ConcFlags.ctxtSwitchTime = 20; /* In milliseconds */
150 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
151 RtsFlags.MiscFlags.machineReadable = rtsFalse;
152 RtsFlags.MiscFlags.linkerMemBase = 0;
155 RtsFlags.ParFlags.nNodes = 1;
156 RtsFlags.ParFlags.migrate = rtsTrue;
157 RtsFlags.ParFlags.wakeupMigrate = rtsFalse;
158 RtsFlags.ParFlags.parGcEnabled = 1;
159 RtsFlags.ParFlags.parGcGen = 0;
160 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
161 RtsFlags.ParFlags.parGcLoadBalancingGen = 1;
162 RtsFlags.ParFlags.setAffinity = 0;
165 #if defined(THREADED_RTS)
166 RtsFlags.ParFlags.maxLocalSparks = 4096;
167 #endif /* THREADED_RTS */
170 RtsFlags.TickyFlags.showTickyStats = rtsFalse;
171 RtsFlags.TickyFlags.tickyFile = NULL;
175 /* By default no special measurements taken */
176 RtsFlags.PapiFlags.eventType = 0;
177 RtsFlags.PapiFlags.numUserEvents = 0;
184 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
186 " +RTS Indicates run time system options follow",
187 " -RTS Indicates program arguments follow",
188 " --RTS Indicates that ALL subsequent arguments will be given to the",
189 " program (including any of these RTS flags)",
191 "The following run time system options are available:",
193 " -? Prints this message and exits; the program is not executed",
194 " --info Print information about the RTS used by this program",
196 " -K<size> Sets the maximum stack size (default 8M) Egs: -K32k -K512k",
197 " -k<size> Sets the initial thread stack size (default 1k) Egs: -k4k -k2m",
199 " -A<size> Sets the minimum allocation area size (default 512k) Egs: -A1m -A10k",
200 " -M<size> Sets the maximum heap size (default unlimited) Egs: -M256k -M1G",
201 " -H<size> Sets the minimum heap size (default 0M) Egs: -H24m -H1G",
202 " -m<n> Minimum % of heap which must be available (default 3%)",
203 " -G<n> Number of generations (default: 2)",
204 " -c<n> Use in-place compaction instead of copying in the oldest generation",
205 " when live data is at least <n>% of the maximum heap size set with",
206 " -M (default: 30%)",
207 " -c Use in-place compaction for all oldest generation collections",
208 " (the default is to use copying)",
209 " -w Use mark-region for the oldest generation (experimental)",
210 #if defined(THREADED_RTS)
211 " -I<sec> Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
214 " -t[<file>] One-line GC statistics (if <file> omitted, uses stderr)",
215 " -s[<file>] Summary GC statistics (if <file> omitted, uses stderr)",
216 " -S[<file>] Detailed GC statistics (if <file> omitted, uses stderr)",
217 #ifdef RTS_GTK_FRONTPANEL
218 " -f Display front panel (requires X11 & GTK+)",
222 " -Z Don't squeeze out update frames on stack overflow",
223 " -B Sound the bell at the start of each garbage collection",
224 #if defined(PROFILING)
226 " -px Time/allocation profile (XML) (output file <program>.prof)",
227 " -p Time/allocation profile (output file <program>.prof)",
228 " -P More detailed Time/Allocation profile",
229 " -Pa Give information about *all* cost centres",
231 # if defined(PROFILING)
233 " -hx Heap residency profile (XML) (output file <program>.prof)",
234 " -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
235 " break-down: c = cost centre stack (default)",
237 " d = closure description",
238 " y = type description",
240 " b = biography (LAG,DRAG,VOID,USE)",
241 " A subset of closures may be selected thusly:",
242 " -hc<cc>,... specific cost centre(s) (top of stack only)",
243 " -hC<cc>,... specific cost centre(s) (anywhere in stack)",
244 " -hm<mod>... all cost centres from the specified modules(s)",
245 " -hd<des>,... closures with specified closure descriptions",
246 " -hy<typ>... closures with specified type descriptions",
247 " -hr<cc>... closures with specified retainers",
248 " -hb<bio>... closures with specified biographies (lag,drag,void,use)",
250 " -R<size> Set the maximum retainer set size (default: 8)",
252 " -L<chars> Maximum length of a cost-centre stack in a heap profile",
255 " -xt Include threads (TSOs) in a heap profile",
257 " -xc Show current cost centre stack on raising an exception",
259 #endif /* PROFILING or PAR */
263 " -l[flags] Log events in binary format to the file <program>.eventlog",
265 " -v[flags] Log events to stderr",
267 " where [flags] can contain:",
268 " s scheduler events",
270 " t add time stamps (only useful with -v)",
274 #if !defined(PROFILING)
276 " -hT Heap residency profile (output file <program>.hp)",
278 " -i<sec> Time between heap samples (seconds, default: 0.1)",
280 #if defined(TICKY_TICKY)
281 " -r<file> Produce ticky-ticky statistics (with -rstderr for stderr)",
284 " -C<secs> Context-switch interval in seconds.",
285 " 0 or no argument means switch as often as possible.",
286 " Default: 0.02 sec; resolution is set by -V below.",
287 " -V<secs> Master tick interval in seconds (0 == disable timer).",
288 " This sets the resolution for -C and the profile timer -i.",
289 " Default: 0.02 sec.",
292 " -Ds DEBUG: scheduler",
293 " -Di DEBUG: interpreter",
295 " -DG DEBUG: gccafs",
298 " -DS DEBUG: sanity",
299 " -Dt DEBUG: stable",
301 " -De DEBUG: event logging",
303 " -Dl DEBUG: linker",
305 " -Dz DEBUG: stack squezing",
306 " -Dc DEBUG: program coverage",
307 " -Dr DEBUG: sparks",
309 " NOTE: DEBUG events are sent to stderr by default; add -l to create a",
310 " binary event log file instead.",
313 #if defined(THREADED_RTS) && !defined(NOSMP)
314 " -N<n> Use <n> processors (default: 1)",
315 " -N Determine the number of processors to use automatically",
316 " -qg[<n>] Use parallel GC only for generations >= <n>",
317 " (default: 0, -qg alone turns off parallel GC)",
318 " -qb[<n>] Use load-balancing in the parallel GC only for generations >= <n>",
319 " (default: 1, -qb alone turns off load-balancing)",
320 " -qa Use the OS to set thread affinity (experimental)",
321 " -qm Don't automatically migrate threads between CPUs",
322 " -qw Migrate a thread to the current CPU when it is woken up",
324 " --install-signal-handlers=<yes|no>",
325 " Install signal handlers (default: yes)",
326 #if defined(THREADED_RTS)
327 " -e<n> Maximum number of outstanding local sparks (default: 4096)",
329 #if defined(x86_64_HOST_ARCH)
330 " -xm Base address to mmap memory in the GHCi linker",
331 " (hex; must be <80000000)",
333 #if defined(USE_PAPI)
334 " -aX CPU performance counter measurements using PAPI",
335 " (use with the -s<file> option). X is one of:",
338 " 1 - level 1 cache misses",
339 " 2 - level 2 cache misses",
340 " b - branch mispredictions",
341 " s - stalled cycles",
342 " e - cache miss and branch misprediction events",
343 " +PAPI_EVENT - collect papi preset event PAPI_EVENT",
344 " #NATIVE_EVENT - collect native event NATIVE_EVENT (in hex)",
347 "RTS options may also be specified using the GHCRTS environment variable.",
349 "Other RTS options may be available for programs compiled a different way.",
350 "The GHC User's Guide has full details.",
355 STATIC_INLINE rtsBool
356 strequal(const char *a, const char * b)
358 return(strcmp(a, b) == 0);
362 splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
368 while (isspace(*c1)) { c1++; };
370 while (!isspace(*c2) && *c2 != '\0') { c2++; };
372 if (c1 == c2) { break; }
374 if (*rts_argc < MAX_RTS_ARGS-1) {
375 s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
376 strncpy(s, c1, c2-c1);
378 rts_argv[(*rts_argc)++] = s;
380 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
384 } while (*c1 != '\0');
388 setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
390 rtsBool error = rtsFalse;
401 // process arguments from the ghc_rts_opts global variable first.
402 // (arguments from the GHCRTS environment variable and the command
403 // line override these).
405 if (ghc_rts_opts != NULL) {
406 splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
410 // process arguments from the GHCRTS environment variable next
411 // (arguments from the command line override these).
413 char *ghc_rts = getenv("GHCRTS");
415 if (ghc_rts != NULL) {
416 if (rtsOptsEnabled != rtsOptsNone) {
417 splitRtsFlags(ghc_rts, rts_argc, rts_argv);
420 errorBelch("Warning: Ignoring GHCRTS variable as RTS options are disabled.\n Link with -rtsopts to enable them.");
421 // We don't actually exit, just warn
426 // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
427 // argv[0] must be PGM argument -- leave in argv
429 for (mode = PGM; arg < total_arg; arg++) {
430 // The '--RTS' argument disables all future +RTS ... -RTS processing.
431 if (strequal("--RTS", argv[arg])) {
435 // The '--' argument is passed through to the program, but
436 // disables all further +RTS ... -RTS processing.
437 else if (strequal("--", argv[arg])) {
440 else if (strequal("+RTS", argv[arg])) {
441 if (rtsOptsEnabled != rtsOptsNone) {
445 errorBelch("RTS options are disabled. Link with -rtsopts to enable them.");
446 stg_exit(EXIT_FAILURE);
449 else if (strequal("-RTS", argv[arg])) {
452 else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
453 if ((rtsOptsEnabled == rtsOptsAll) ||
454 strequal(argv[arg], "--info")) {
455 rts_argv[(*rts_argc)++] = argv[arg];
458 errorBelch("Most RTS options are disabled. Link with -rtsopts to enable them.");
459 stg_exit(EXIT_FAILURE);
462 else if (mode == PGM) {
463 argv[(*argc)++] = argv[arg];
466 barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
469 // process remaining program arguments
470 for (; arg < total_arg; arg++) {
471 argv[(*argc)++] = argv[arg];
473 argv[*argc] = (char *) 0;
474 rts_argv[*rts_argc] = (char *) 0;
476 // Process RTS (rts_argv) part: mainly to determine statsfile
477 for (arg = 0; arg < *rts_argc; arg++) {
478 if (rts_argv[arg][0] != '-') {
480 errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
484 switch(rts_argv[arg][1]) {
486 /* process: general args, then PROFILING-only ones, then
487 CONCURRENT-only, TICKY-only (same order as defined in
488 RtsFlags.lh); within those groups, mostly in
489 case-insensitive alphabetical order. Final group is
490 x*, which allows for more options.
494 # define TICKY_BUILD_ONLY(x) x
496 # define TICKY_BUILD_ONLY(x) \
497 errorBelch("the flag %s requires the program to be built with -ticky", rts_argv[arg]); \
502 # define PROFILING_BUILD_ONLY(x) x
504 # define PROFILING_BUILD_ONLY(x) \
505 errorBelch("the flag %s requires the program to be built with -prof", rts_argv[arg]); \
510 # define TRACING_BUILD_ONLY(x) x
512 # define TRACING_BUILD_ONLY(x) \
513 errorBelch("the flag %s requires the program to be built with -eventlog or -debug", rts_argv[arg]); \
518 # define THREADED_BUILD_ONLY(x) x
520 # define THREADED_BUILD_ONLY(x) \
521 errorBelch("the flag %s requires the program to be built with -threaded", rts_argv[arg]); \
526 # define DEBUG_BUILD_ONLY(x) x
528 # define DEBUG_BUILD_ONLY(x) \
529 errorBelch("the flag %s requires the program to be built with -debug", rts_argv[arg]); \
533 /* =========== GENERAL ========================== */
538 /* This isn't going to allow us to keep related options
539 together as we add more --* flags. We really need a
540 proper options parser. */
542 if (strequal("install-signal-handlers=yes",
543 &rts_argv[arg][2])) {
544 RtsFlags.MiscFlags.install_signal_handlers = rtsTrue;
546 else if (strequal("install-signal-handlers=no",
547 &rts_argv[arg][2])) {
548 RtsFlags.MiscFlags.install_signal_handlers = rtsFalse;
550 else if (strequal("machine-readable",
551 &rts_argv[arg][2])) {
552 RtsFlags.MiscFlags.machineReadable = rtsTrue;
554 else if (strequal("info",
555 &rts_argv[arg][2])) {
560 errorBelch("unknown RTS option: %s",rts_argv[arg]);
565 RtsFlags.GcFlags.minAllocAreaSize
566 = decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_INT_MAX)
572 switch(rts_argv[arg][2]) {
574 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L1;
577 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CACHE_L2;
580 RtsFlags.PapiFlags.eventType = PAPI_FLAG_BRANCH;
583 RtsFlags.PapiFlags.eventType = PAPI_FLAG_STALLS;
586 RtsFlags.PapiFlags.eventType = PAPI_FLAG_CB_EVENTS;
590 if (RtsFlags.PapiFlags.numUserEvents >= MAX_PAPI_USER_EVENTS) {
591 errorBelch("maximum number of PAPI events reached");
592 stg_exit(EXIT_FAILURE);
594 nat eventNum = RtsFlags.PapiFlags.numUserEvents++;
595 char kind = rts_argv[arg][2];
596 nat eventKind = kind == '+' ? PAPI_PRESET_EVENT_KIND : PAPI_NATIVE_EVENT_KIND;
598 RtsFlags.PapiFlags.userEvents[eventNum] = rts_argv[arg] + 3;
599 RtsFlags.PapiFlags.eventType = PAPI_USER_EVENTS;
600 RtsFlags.PapiFlags.userEventsKind[eventNum] = eventKind;
603 bad_option( rts_argv[arg] );
609 RtsFlags.GcFlags.ringBell = rtsTrue;
613 if (rts_argv[arg][2] != '\0') {
614 RtsFlags.GcFlags.compactThreshold =
615 atof(rts_argv[arg]+2);
617 RtsFlags.GcFlags.compact = rtsTrue;
622 RtsFlags.GcFlags.sweep = rtsTrue;
626 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
628 if (RtsFlags.GcFlags.oldGenFactor < 0)
629 bad_option( rts_argv[arg] );
637 for (c = rts_argv[arg] + 2; *c != '\0'; c++) {
640 RtsFlags.DebugFlags.scheduler = rtsTrue;
643 RtsFlags.DebugFlags.interpreter = rtsTrue;
646 RtsFlags.DebugFlags.weak = rtsTrue;
649 RtsFlags.DebugFlags.gccafs = rtsTrue;
652 RtsFlags.DebugFlags.gc = rtsTrue;
655 RtsFlags.DebugFlags.block_alloc = rtsTrue;
658 RtsFlags.DebugFlags.sanity = rtsTrue;
661 RtsFlags.DebugFlags.stable = rtsTrue;
664 RtsFlags.DebugFlags.prof = rtsTrue;
667 RtsFlags.DebugFlags.linker = rtsTrue;
670 RtsFlags.DebugFlags.apply = rtsTrue;
673 RtsFlags.DebugFlags.stm = rtsTrue;
676 RtsFlags.DebugFlags.squeeze = rtsTrue;
679 RtsFlags.DebugFlags.hpc = rtsTrue;
682 RtsFlags.DebugFlags.sparks = rtsTrue;
685 bad_option( rts_argv[arg] );
688 // -Dx also turns on -v. Use -l to direct trace
689 // events to the .eventlog file instead.
690 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
695 RtsFlags.GcFlags.maxStkSize =
696 decodeSize(rts_argv[arg], 2, 1, HS_WORD_MAX) / sizeof(W_);
700 RtsFlags.GcFlags.initialStkSize =
701 decodeSize(rts_argv[arg], 2, 1, HS_WORD_MAX) / sizeof(W_);
705 RtsFlags.GcFlags.maxHeapSize =
706 decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE;
707 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
711 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
713 if (RtsFlags.GcFlags.pcFreeHeap < 0 ||
714 RtsFlags.GcFlags.pcFreeHeap > 100)
715 bad_option( rts_argv[arg] );
719 RtsFlags.GcFlags.generations =
720 decodeSize(rts_argv[arg], 2, 1, HS_INT_MAX);
724 if (rts_argv[arg][2] == '\0') {
725 RtsFlags.GcFlags.heapSizeSuggestionAuto = rtsTrue;
727 RtsFlags.GcFlags.heapSizeSuggestion =
728 (nat)(decodeSize(rts_argv[arg], 2, BLOCK_SIZE, HS_WORD_MAX) / BLOCK_SIZE);
732 #ifdef RTS_GTK_FRONTPANEL
734 RtsFlags.GcFlags.frontpanel = rtsTrue;
738 case 'I': /* idle GC delay */
739 if (rts_argv[arg][2] == '\0') {
744 /* Convert to millisecs */
745 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
746 RtsFlags.GcFlags.idleGCDelayTime = cst;
751 RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
755 RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
759 RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
765 r = open_stats_file(arg, *argc, argv,
766 *rts_argc, rts_argv, NULL,
767 &RtsFlags.GcFlags.statsFile);
768 if (r == -1) { error = rtsTrue; }
773 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
776 /* =========== PROFILING ========================== */
778 case 'P': /* detailed cost centre profiling (time/alloc) */
779 case 'p': /* cost centre profiling (time/alloc) */
780 PROFILING_BUILD_ONLY(
781 switch (rts_argv[arg][2]) {
783 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
786 RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
789 if (rts_argv[arg][1] == 'P') {
790 RtsFlags.CcFlags.doCostCentres =
791 COST_CENTRES_VERBOSE;
793 RtsFlags.CcFlags.doCostCentres =
794 COST_CENTRES_SUMMARY;
801 PROFILING_BUILD_ONLY(
802 RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
805 PROFILING_BUILD_ONLY(
806 RtsFlags.ProfFlags.ccsLength = atof(rts_argv[arg]+2);
807 if(RtsFlags.ProfFlags.ccsLength <= 0) {
808 bad_option(rts_argv[arg]);
811 case 'h': /* serial heap profile */
812 #if !defined(PROFILING)
813 switch (rts_argv[arg][2]) {
816 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
819 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
823 PROFILING_BUILD_ONLY(
824 switch (rts_argv[arg][2]) {
838 if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
840 char *left = strchr(rts_argv[arg], '{');
841 char *right = strrchr(rts_argv[arg], '}');
843 // curly braces are optional, for
848 left = rts_argv[arg] + 3;
851 right = rts_argv[arg] + strlen(rts_argv[arg]);
855 switch (rts_argv[arg][2]) {
856 case 'c': // cost centre label select
857 RtsFlags.ProfFlags.ccSelector = left;
860 RtsFlags.ProfFlags.ccsSelector = left;
863 case 'm': // cost centre module select
864 RtsFlags.ProfFlags.modSelector = left;
867 case 'd': // closure descr select
868 RtsFlags.ProfFlags.descrSelector = left;
871 case 'y': // closure type select
872 RtsFlags.ProfFlags.typeSelector = left;
875 case 'r': // retainer select
876 RtsFlags.ProfFlags.retainerSelector = left;
879 case 'b': // biography select
880 RtsFlags.ProfFlags.bioSelector = left;
887 if (RtsFlags.ProfFlags.doHeapProfile != 0) {
888 errorBelch("multiple heap profile options");
893 switch (rts_argv[arg][2]) {
897 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
901 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
905 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
909 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
913 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
917 RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
923 errorBelch("invalid heap profile option: %s",rts_argv[arg]);
927 #endif /* PROFILING */
930 case 'i': /* heap sample interval */
931 if (rts_argv[arg][2] == '\0') {
936 /* Convert to milliseconds */
937 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
938 RtsFlags.ProfFlags.profileInterval = cst;
942 /* =========== CONCURRENT ========================= */
943 case 'C': /* context switch interval */
944 if (rts_argv[arg][2] == '\0')
945 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
949 /* Convert to milliseconds */
950 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
951 RtsFlags.ConcFlags.ctxtSwitchTime = cst;
955 case 'V': /* master tick interval */
956 if (rts_argv[arg][2] == '\0') {
957 // turns off ticks completely
958 RtsFlags.MiscFlags.tickInterval = 0;
962 /* Convert to milliseconds */
963 cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
964 RtsFlags.MiscFlags.tickInterval = cst;
971 if (rts_argv[arg][2] == '\0') {
972 #if defined(PROFILING)
973 RtsFlags.ParFlags.nNodes = 1;
975 RtsFlags.ParFlags.nNodes = getNumberOfProcessors();
978 RtsFlags.ParFlags.nNodes
979 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
980 if (RtsFlags.ParFlags.nNodes <= 0) {
981 errorBelch("bad value for -N");
984 #if defined(PROFILING)
985 if (RtsFlags.ParFlags.nNodes > 1) {
986 errorBelch("bad option %s: only -N1 is supported with profiling", rts_argv[arg]);
995 switch (rts_argv[arg][2]) {
997 // backwards compat only
998 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1001 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1008 THREADED_BUILD_ONLY(
1009 switch (rts_argv[arg][2]) {
1011 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1015 if (rts_argv[arg][3] == '\0') {
1016 RtsFlags.ParFlags.parGcEnabled = rtsFalse;
1018 RtsFlags.ParFlags.parGcEnabled = rtsTrue;
1019 RtsFlags.ParFlags.parGcGen
1020 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1024 if (rts_argv[arg][3] == '\0') {
1025 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsFalse;
1028 RtsFlags.ParFlags.parGcLoadBalancingEnabled = rtsTrue;
1029 RtsFlags.ParFlags.parGcLoadBalancingGen
1030 = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1034 RtsFlags.ParFlags.setAffinity = rtsTrue;
1037 RtsFlags.ParFlags.migrate = rtsFalse;
1040 RtsFlags.ParFlags.wakeupMigrate = rtsTrue;
1043 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1049 /* =========== PARALLEL =========================== */
1051 THREADED_BUILD_ONLY(
1052 if (rts_argv[arg][2] != '\0') {
1053 RtsFlags.ParFlags.maxLocalSparks
1054 = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1055 if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1056 errorBelch("bad value for -e");
1062 /* =========== TICKY ============================== */
1064 case 'r': /* Basic profiling stats */
1067 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1071 r = open_stats_file(arg, *argc, argv,
1072 *rts_argc, rts_argv, TICKY_FILENAME_FMT,
1073 &RtsFlags.TickyFlags.tickyFile);
1074 if (r == -1) { error = rtsTrue; }
1078 /* =========== TRACING ---------=================== */
1082 RtsFlags.TraceFlags.tracing = TRACE_EVENTLOG;
1083 read_trace_flags(&rts_argv[arg][2]);
1089 RtsFlags.TraceFlags.tracing = TRACE_STDERR;
1090 read_trace_flags(&rts_argv[arg][2]);
1094 /* =========== EXTENDED OPTIONS =================== */
1096 case 'x': /* Extend the argument space */
1097 switch(rts_argv[arg][2]) {
1099 errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1103 case 'b': /* heapBase in hex; undocumented */
1104 if (rts_argv[arg][3] != '\0') {
1105 RtsFlags.GcFlags.heapBase
1106 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1108 errorBelch("-xb: requires argument");
1113 #if defined(x86_64_HOST_ARCH)
1114 case 'm': /* linkerMemBase */
1115 if (rts_argv[arg][3] != '\0') {
1116 RtsFlags.MiscFlags.linkerMemBase
1117 = strtol(rts_argv[arg]+3, (char **) NULL, 16);
1118 if (RtsFlags.MiscFlags.linkerMemBase > 0x80000000) {
1119 errorBelch("-xm: value must be <80000000");
1123 RtsFlags.MiscFlags.linkerMemBase = 0;
1128 case 'c': /* Debugging tool: show current cost centre on an exception */
1129 PROFILING_BUILD_ONLY(
1130 RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1134 case 't': /* Include memory used by TSOs in a heap profile */
1135 PROFILING_BUILD_ONLY(
1136 RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1140 /* The option prefix '-xx' is reserved for future extension. KSW 1999-11. */
1143 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1147 break; /* defensive programming */
1149 /* =========== OH DEAR ============================ */
1151 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1158 if (RtsFlags.MiscFlags.tickInterval < 0) {
1159 RtsFlags.MiscFlags.tickInterval = 50;
1162 // If the master timer is disabled, turn off the other timers.
1163 if (RtsFlags.MiscFlags.tickInterval == 0) {
1164 RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1165 RtsFlags.GcFlags.idleGCDelayTime = 0;
1166 RtsFlags.ProfFlags.profileInterval = 0;
1169 // Determine what tick interval we should use for the RTS timer
1170 // by taking the shortest of the various intervals that we need to
1172 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1173 RtsFlags.MiscFlags.tickInterval =
1174 stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1175 RtsFlags.MiscFlags.tickInterval);
1178 if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1179 RtsFlags.MiscFlags.tickInterval =
1180 stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1181 RtsFlags.MiscFlags.tickInterval);
1184 if (RtsFlags.ProfFlags.profileInterval > 0) {
1185 RtsFlags.MiscFlags.tickInterval =
1186 stg_min(RtsFlags.ProfFlags.profileInterval,
1187 RtsFlags.MiscFlags.tickInterval);
1190 if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1191 RtsFlags.ConcFlags.ctxtSwitchTicks =
1192 RtsFlags.ConcFlags.ctxtSwitchTime /
1193 RtsFlags.MiscFlags.tickInterval;
1195 RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1198 if (RtsFlags.ProfFlags.profileInterval > 0) {
1199 RtsFlags.ProfFlags.profileIntervalTicks =
1200 RtsFlags.ProfFlags.profileInterval /
1201 RtsFlags.MiscFlags.tickInterval;
1203 RtsFlags.ProfFlags.profileIntervalTicks = 0;
1210 for (p = usage_text; *p; p++)
1211 errorBelch("%s", *p);
1212 stg_exit(EXIT_FAILURE);
1218 stats_fprintf(FILE *f, char *s, ...)
1230 static int /* return -1 on error */
1233 int argc, char *argv[],
1234 int rts_argc, char *rts_argv[],
1235 const char *FILENAME_FMT,
1240 if (strequal(rts_argv[arg]+2, "stderr")
1241 || (FILENAME_FMT == NULL && rts_argv[arg][2] == '\0')) {
1242 f = NULL; /* NULL means use debugBelch */
1244 if (rts_argv[arg][2] != '\0') { /* stats file specified */
1245 f = fopen(rts_argv[arg]+2,"w");
1247 char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
1248 sprintf(stats_filename, FILENAME_FMT, argv[0]);
1249 f = fopen(stats_filename,"w");
1252 errorBelch("Can't open stats file %s\n", rts_argv[arg]+2);
1259 /* Write argv and rtsv into start of stats file */
1261 for(count = 0; count < argc; count++) {
1262 stats_fprintf(f, "%s ", argv[count]);
1264 stats_fprintf(f, "+RTS ");
1265 for(count = 0; count < rts_argc; count++)
1266 stats_fprintf(f, "%s ", rts_argv[count]);
1267 stats_fprintf(f, "\n");
1275 decodeSize(const char *flag, nat offset, StgWord64 min, StgWord64 max)
1293 if (c == 'g' || c == 'G')
1294 m *= 1024*1024*1024;
1295 else if (c == 'm' || c == 'M')
1297 else if (c == 'k' || c == 'K')
1299 else if (c == 'w' || c == 'W')
1305 if (m < 0 || val < min || val > max) {
1306 // printf doesn't like 64-bit format specs on Windows
1307 // apparently, so fall back to unsigned long.
1308 errorBelch("error in RTS option %s: size outside allowed range (%lu - %lu)", flag, (lnat)min, (lnat)max);
1309 stg_exit(EXIT_FAILURE);
1315 #if defined(TRACING)
1316 static void read_trace_flags(char *arg)
1320 for (c = arg; *c != '\0'; c++) {
1325 RtsFlags.TraceFlags.scheduler = rtsTrue;
1328 RtsFlags.TraceFlags.timestamp = rtsTrue;
1331 // ignored for backwards-compat
1334 errorBelch("unknown trace option: %c",*c);
1341 static void GNU_ATTRIBUTE(__noreturn__)
1342 bad_option(const char *s)
1344 errorBelch("bad RTS option: %s", s);
1345 stg_exit(EXIT_FAILURE);
1348 /* -----------------------------------------------------------------------------
1349 Getting/Setting the program's arguments.
1351 These are used by System.Environment, and parts of the RTS.
1352 -------------------------------------------------------------------------- */
1355 setProgName(char *argv[])
1357 /* Remove directory from argv[0] -- default files in current directory */
1358 #if !defined(mingw32_HOST_OS)
1360 if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
1361 prog_name = last_slash+1;
1363 prog_name = argv[0];
1366 char* last_slash = argv[0] + (strlen(argv[0]) - 1);
1367 while ( last_slash > argv[0] ) {
1368 if ( *last_slash == '/' || *last_slash == '\\' ) {
1369 prog_name = last_slash+1;
1374 prog_name = argv[0];
1379 getProgArgv(int *argc, char **argv[])
1381 if (argc) { *argc = prog_argc; }
1382 if (argv) { *argv = prog_argv; }
1386 setProgArgv(int argc, char *argv[])
1388 /* Usually this is done by startupHaskell, so we don't need to call this.
1389 However, sometimes Hugs wants to change the arguments which Haskell
1390 getArgs >>= ... will be fed. So you can do that by calling here
1391 _after_ calling startupHaskell.
1395 setProgName(prog_argv);
1398 /* These functions record and recall the full arguments, including the
1399 +RTS ... -RTS options. The reason for adding them was so that the
1400 ghc-inplace program can pass /all/ the arguments on to the real ghc. */
1402 getFullProgArgv(int *argc, char **argv[])
1404 if (argc) { *argc = full_prog_argc; }
1405 if (argv) { *argv = full_prog_argv; }
1409 setFullProgArgv(int argc, char *argv[])
1412 full_prog_argc = argc;
1413 full_prog_argv = stgCallocBytes(argc + 1, sizeof (char *),
1414 "setFullProgArgv 1");
1415 for (i = 0; i < argc; i++) {
1416 full_prog_argv[i] = stgMallocBytes(strlen(argv[i]) + 1,
1417 "setFullProgArgv 2");
1418 strcpy(full_prog_argv[i], argv[i]);
1420 full_prog_argv[argc] = NULL;
1424 freeFullProgArgv (void)
1428 if (full_prog_argv != NULL) {
1429 for (i = 0; i < full_prog_argc; i++) {
1430 stgFree(full_prog_argv[i]);
1432 stgFree(full_prog_argv);
1436 full_prog_argv = NULL;