Split GC.c, and move storage manager into sm/ directory
[ghc-hetmet.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 #include "RtsFlags.h"
13 #include "RtsUtils.h"
14 #include "Profiling.h"
15
16 #ifdef HAVE_CTYPE_H
17 #include <ctype.h>
18 #endif
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 // Flag Structure
24 RTS_FLAGS RtsFlags;
25
26 /*
27  * Split argument lists
28  */
29 int     prog_argc = 0;    /* an "int" so as to match normal "argc" */
30 char  **prog_argv = NULL;
31 char   *prog_name = NULL; /* 'basename' of prog_argv[0] */
32 int     rts_argc = 0;  /* ditto */
33 char   *rts_argv[MAX_RTS_ARGS];
34
35 /*
36  * constants, used later 
37  */
38 #define RTS 1
39 #define PGM 0
40
41 #if defined(GRAN)
42
43 static char *gran_debug_opts_strs[] = {
44   "DEBUG (-bDe, -bD1): event_trace; printing event trace.\n",
45   "DEBUG (-bDE, -bD2): event_stats; printing event statistics.\n",
46   "DEBUG (-bDb, -bD4): bq; check blocking queues\n",
47   "DEBUG (-bDG, -bD8): pack; routines for (un-)packing graph structures.\n",
48   "DEBUG (-bDq, -bD16): checkSparkQ; check consistency of the spark queues.\n",
49   "DEBUG (-bDf, -bD32): thunkStealing; print forwarding of fetches.\n",
50   "DEBUG (-bDr, -bD64): randomSteal; stealing sparks/threads from random PEs.\n",
51   "DEBUG (-bDF, -bD128): findWork; searching spark-pools (local & remote), thread queues for work.\n",
52   "DEBUG (-bDu, -bD256): unused; currently unused flag.\n",
53   "DEBUG (-bDS, -bD512): pri; priority sparking or scheduling.\n",
54   "DEBUG (-bD:, -bD1024): checkLight; check GranSim-Light setup.\n",
55   "DEBUG (-bDo, -bD2048): sortedQ; check whether spark/thread queues are sorted.\n",
56   "DEBUG (-bDz, -bD4096): blockOnFetch; check for blocked on fetch.\n",
57   "DEBUG (-bDP, -bD8192): packBuffer; routines handling pack buffer (GranSim internal!).\n",
58   "DEBUG (-bDt, -bD16384): blockOnFetch_sanity; check for TSO asleep on fetch.\n",
59 };
60
61 /* one character codes for the available debug options */
62 static char gran_debug_opts_flags[] = {
63   'e', 'E', 'b', 'G', 'q', 'f', 'r', 'F', 'u', 'S', ':', 'o', 'z', 'P', 't'
64 };
65
66 #elif defined(PAR)
67
68 static char *par_debug_opts_strs[] = {
69   "DEBUG (-qDv, -qD1): verbose; be generally verbose with parallel related stuff.\n",
70   "DEBUG (-qDq, -qD2): bq; print blocking queues.\n",
71   "DEBUG (-qDs, -qD4): schedule; scheduling of parallel threads.\n",
72   "DEBUG (-qDe, -qD8): free; free messages.\n",
73   "DEBUG (-qDr, -qD16): resume; resume messages.\n",
74   "DEBUG (-qDw, -qD32): weight; print weights and distrib GC stuff.\n",
75   "DEBUG (-qDF, -qD64): fetch; fetch messages.\n",
76   // "DEBUG (-qDa, -qD128): ack; ack messages.\n",
77   "DEBUG (-qDf, -qD128): fish; fish messages.\n",
78   //"DEBUG (-qDo, -qD512): forward; forwarding messages to other PEs.\n",
79   "DEBUG (-qDl, -qD256): tables; print internal LAGA etc tables.\n",
80   "DEBUG (-qDo, -qD512): packet; packets and graph structures when packing.\n",
81   "DEBUG (-qDp, -qD1024): pack; packing and unpacking graphs.\n",
82   "DEBUG (-qDz, -qD2048): paranoia; ridiculously detailed output (excellent for filling a partition).\n"
83 };
84
85 /* one character codes for the available debug options */
86 static char par_debug_opts_flags[] = {
87   'v', 'q', 's', 'e', 'r', 'w', 'F', 'f', 'l', 'o', 'p', 'z'
88 };
89
90 #endif /* PAR */
91
92 /* -----------------------------------------------------------------------------
93    Static function decls
94    -------------------------------------------------------------------------- */
95
96 static int              /* return NULL on error */
97 open_stats_file (
98     I_ arg,
99     int argc, char *argv[],
100     int rts_argc, char *rts_argv[],
101     const char *FILENAME_FMT,
102     FILE **file_ret);
103
104 static I_ decode(const char *s);
105 static void bad_option(const char *s);
106
107 #if defined(GRAN)
108 static void enable_GranSimLight(void);
109 static void process_gran_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error);
110 static void set_GranSim_debug_options(nat n);
111 static void help_GranSim_debug_options(nat n);
112 #elif defined(PAR)
113 static void process_par_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error);
114 static void set_par_debug_options(nat n);
115 static void help_par_debug_options(nat n);
116 #endif
117
118 /* -----------------------------------------------------------------------------
119  * Command-line option parsing routines.
120  * ---------------------------------------------------------------------------*/
121
122 void initRtsFlagsDefaults(void)
123 {
124     RtsFlags.GcFlags.statsFile          = NULL;
125     RtsFlags.GcFlags.giveStats          = NO_GC_STATS;
126
127     RtsFlags.GcFlags.maxStkSize         = (8 * 1024 * 1024) / sizeof(W_);
128     RtsFlags.GcFlags.initialStkSize     = 1024 / sizeof(W_);
129
130     RtsFlags.GcFlags.minAllocAreaSize   = (512 * 1024)        / BLOCK_SIZE;
131     RtsFlags.GcFlags.minOldGenSize      = (1024 * 1024)       / BLOCK_SIZE;
132     RtsFlags.GcFlags.maxHeapSize        = 0;    /* off by default */
133     RtsFlags.GcFlags.heapSizeSuggestion = 0;    /* none */
134     RtsFlags.GcFlags.pcFreeHeap         = 3;    /* 3% */
135     RtsFlags.GcFlags.oldGenFactor       = 2;
136 #if defined(PAR)
137     /* A hack currently needed for GUM -- HWL */
138     RtsFlags.GcFlags.generations        = 1;
139     RtsFlags.GcFlags.steps              = 2;
140     RtsFlags.GcFlags.squeezeUpdFrames   = rtsFalse;
141 #else
142     RtsFlags.GcFlags.generations        = 2;
143     RtsFlags.GcFlags.steps              = 2;
144     RtsFlags.GcFlags.squeezeUpdFrames   = rtsTrue;
145 #endif
146     RtsFlags.GcFlags.compact            = rtsFalse;
147     RtsFlags.GcFlags.compactThreshold   = 30.0;
148 #ifdef RTS_GTK_FRONTPANEL
149     RtsFlags.GcFlags.frontpanel         = rtsFalse;
150 #endif
151     RtsFlags.GcFlags.idleGCDelayTime    = 300; /* millisecs */
152
153 #ifdef DEBUG
154     RtsFlags.DebugFlags.scheduler       = rtsFalse;
155     RtsFlags.DebugFlags.interpreter     = rtsFalse;
156     RtsFlags.DebugFlags.weak            = rtsFalse;
157     RtsFlags.DebugFlags.gccafs          = rtsFalse;
158     RtsFlags.DebugFlags.gc              = rtsFalse;
159     RtsFlags.DebugFlags.block_alloc     = rtsFalse;
160     RtsFlags.DebugFlags.sanity          = rtsFalse;
161     RtsFlags.DebugFlags.stable          = rtsFalse;
162     RtsFlags.DebugFlags.stm             = rtsFalse;
163     RtsFlags.DebugFlags.prof            = rtsFalse;
164     RtsFlags.DebugFlags.gran            = rtsFalse;
165     RtsFlags.DebugFlags.par             = rtsFalse;
166     RtsFlags.DebugFlags.linker          = rtsFalse;
167     RtsFlags.DebugFlags.squeeze         = rtsFalse;
168 #endif
169
170 #if defined(PROFILING) || defined(PAR)
171     RtsFlags.CcFlags.doCostCentres      = 0;
172 #endif /* PROFILING or PAR */
173
174 #ifdef PROFILING
175     RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
176     RtsFlags.ProfFlags.profileInterval    = 100;
177     RtsFlags.ProfFlags.includeTSOs        = rtsFalse;
178     RtsFlags.ProfFlags.showCCSOnException = rtsFalse;
179     RtsFlags.ProfFlags.maxRetainerSetSize = 8;
180     RtsFlags.ProfFlags.modSelector        = NULL;
181     RtsFlags.ProfFlags.descrSelector      = NULL;
182     RtsFlags.ProfFlags.typeSelector       = NULL;
183     RtsFlags.ProfFlags.ccSelector         = NULL;
184     RtsFlags.ProfFlags.ccsSelector        = NULL;
185     RtsFlags.ProfFlags.retainerSelector   = NULL;
186     RtsFlags.ProfFlags.bioSelector        = NULL;
187
188 #elif defined(DEBUG)
189     RtsFlags.ProfFlags.doHeapProfile      = rtsFalse;
190 #endif
191
192     RtsFlags.MiscFlags.tickInterval     = 50;  /* In milliseconds */
193     RtsFlags.ConcFlags.ctxtSwitchTime   = 50;  /* In milliseconds */
194
195 #ifdef THREADED_RTS
196     RtsFlags.ParFlags.nNodes            = 1;
197     RtsFlags.ParFlags.migrate           = rtsTrue;
198     RtsFlags.ParFlags.wakeupMigrate     = rtsFalse;
199 #endif
200
201 #ifdef PAR
202     RtsFlags.ParFlags.ParStats.Full       = rtsFalse;
203     RtsFlags.ParFlags.ParStats.Suppressed = rtsFalse;
204     RtsFlags.ParFlags.ParStats.Binary     = rtsFalse;
205     RtsFlags.ParFlags.ParStats.Sparks     = rtsFalse;
206     RtsFlags.ParFlags.ParStats.Heap       = rtsFalse;
207     RtsFlags.ParFlags.ParStats.NewLogfile = rtsFalse;
208     RtsFlags.ParFlags.ParStats.Global     = rtsFalse;
209
210     RtsFlags.ParFlags.outputDisabled    = rtsFalse;
211 #ifdef DIST
212     RtsFlags.ParFlags.doFairScheduling  = rtsTrue;  /* fair sched by def */
213 #else
214     RtsFlags.ParFlags.doFairScheduling  = rtsFalse;  /* unfair sched by def */
215 #endif
216     RtsFlags.ParFlags.packBufferSize    = 1024;
217     RtsFlags.ParFlags.thunksToPack      = 1; /* 0 ... infinity; */
218     RtsFlags.ParFlags.globalising       = 1; /* 0 ... everything */
219     RtsFlags.ParFlags.maxThreads        = 1024;
220     RtsFlags.ParFlags.maxFishes        = MAX_FISHES;
221     RtsFlags.ParFlags.fishDelay         = FISH_DELAY;
222 #endif
223
224 #if defined(PAR) || defined(THREADED_RTS)
225     RtsFlags.ParFlags.maxLocalSparks    = 4096;
226 #endif /* PAR || THREADED_RTS */
227
228 #if defined(GRAN)
229     /* ToDo: check defaults for GranSim and GUM */
230     RtsFlags.GcFlags.maxStkSize         = (8 * 1024 * 1024) / sizeof(W_);
231     RtsFlags.GcFlags.initialStkSize     = 1024 / sizeof(W_);
232
233     RtsFlags.GranFlags.maxThreads       = 65536; // refers to mandatory threads
234     RtsFlags.GranFlags.GranSimStats.Full        = rtsFalse;
235     RtsFlags.GranFlags.GranSimStats.Suppressed  = rtsFalse;
236     RtsFlags.GranFlags.GranSimStats.Binary      = rtsFalse;
237     RtsFlags.GranFlags.GranSimStats.Sparks      = rtsFalse;
238     RtsFlags.GranFlags.GranSimStats.Heap        = rtsFalse;
239     RtsFlags.GranFlags.GranSimStats.NewLogfile  = rtsFalse;
240     RtsFlags.GranFlags.GranSimStats.Global      = rtsFalse;
241
242     RtsFlags.GranFlags.packBufferSize   = 1024;
243     RtsFlags.GranFlags.packBufferSize_internal = GRANSIM_DEFAULT_PACK_BUFFER_SIZE;
244
245     RtsFlags.GranFlags.proc         = MAX_PROC;
246     RtsFlags.GranFlags.Fishing      = rtsFalse;
247     RtsFlags.GranFlags.maxFishes   = MAX_FISHES;
248     RtsFlags.GranFlags.time_slice   = GRAN_TIME_SLICE;
249     RtsFlags.GranFlags.Light        = rtsFalse;
250
251     RtsFlags.GranFlags.Costs.latency =             LATENCY;          
252     RtsFlags.GranFlags.Costs.additional_latency =  ADDITIONAL_LATENCY; 
253     RtsFlags.GranFlags.Costs.fetchtime =           FETCHTIME; 
254     RtsFlags.GranFlags.Costs.lunblocktime =        LOCALUNBLOCKTIME; 
255     RtsFlags.GranFlags.Costs.gunblocktime =        GLOBALUNBLOCKTIME;
256     RtsFlags.GranFlags.Costs.mpacktime =           MSGPACKTIME;      
257     RtsFlags.GranFlags.Costs.munpacktime =         MSGUNPACKTIME;
258     RtsFlags.GranFlags.Costs.mtidytime =           MSGTIDYTIME;
259
260     RtsFlags.GranFlags.Costs.threadcreatetime =         THREADCREATETIME;
261     RtsFlags.GranFlags.Costs.threadqueuetime =          THREADQUEUETIME;
262     RtsFlags.GranFlags.Costs.threaddescheduletime =     THREADDESCHEDULETIME;
263     RtsFlags.GranFlags.Costs.threadscheduletime =       THREADSCHEDULETIME;
264     RtsFlags.GranFlags.Costs.threadcontextswitchtime =  THREADCONTEXTSWITCHTIME;
265
266     RtsFlags.GranFlags.Costs.arith_cost =         ARITH_COST;       
267     RtsFlags.GranFlags.Costs.branch_cost =        BRANCH_COST; 
268     RtsFlags.GranFlags.Costs.load_cost =          LOAD_COST;        
269     RtsFlags.GranFlags.Costs.store_cost =         STORE_COST; 
270     RtsFlags.GranFlags.Costs.float_cost =         FLOAT_COST;       
271
272     RtsFlags.GranFlags.Costs.heapalloc_cost =     HEAPALLOC_COST;
273
274     RtsFlags.GranFlags.Costs.pri_spark_overhead = PRI_SPARK_OVERHEAD;        
275     RtsFlags.GranFlags.Costs.pri_sched_overhead = PRI_SCHED_OVERHEAD;        
276
277     RtsFlags.GranFlags.DoFairSchedule           = rtsFalse;             
278     RtsFlags.GranFlags.DoAsyncFetch             = rtsFalse;        
279     RtsFlags.GranFlags.DoStealThreadsFirst      = rtsFalse;        
280     RtsFlags.GranFlags.DoAlwaysCreateThreads    = rtsFalse;      
281     RtsFlags.GranFlags.DoBulkFetching           = rtsFalse;             
282     RtsFlags.GranFlags.DoThreadMigration        = rtsFalse;          
283     RtsFlags.GranFlags.FetchStrategy            = 2;                     
284     RtsFlags.GranFlags.PreferSparksOfLocalNodes = rtsFalse;   
285     RtsFlags.GranFlags.DoPrioritySparking       = rtsFalse;         
286     RtsFlags.GranFlags.DoPriorityScheduling     = rtsFalse;       
287     RtsFlags.GranFlags.SparkPriority            = 0;
288     RtsFlags.GranFlags.SparkPriority2           = 0; 
289     RtsFlags.GranFlags.RandomPriorities         = rtsFalse;           
290     RtsFlags.GranFlags.InversePriorities        = rtsFalse;          
291     RtsFlags.GranFlags.IgnorePriorities         = rtsFalse;           
292     RtsFlags.GranFlags.ThunksToPack             = 0;                      
293     RtsFlags.GranFlags.RandomSteal              = rtsTrue;
294 #endif
295
296 #ifdef TICKY_TICKY
297     RtsFlags.TickyFlags.showTickyStats   = rtsFalse;
298     RtsFlags.TickyFlags.tickyFile        = NULL;
299 #endif
300
301     RtsFlags.TraceFlags.timestamp       = rtsFalse;
302     RtsFlags.TraceFlags.sched           = rtsFalse;
303 }
304
305 static const char *
306 usage_text[] = {
307 "",
308 "Usage: <prog> <args> [+RTS <rtsopts> | -RTS <args>] ... --RTS <args>",
309 "",
310 "   +RTS    Indicates run time system options follow",
311 "   -RTS    Indicates program arguments follow",
312 "  --RTS    Indicates that ALL subsequent arguments will be given to the",
313 "           program (including any of these RTS flags)",
314 "",
315 "The following run time system options are available:",
316 "",
317 "  -?       Prints this message and exits; the program is not executed",
318 "",
319 "  -K<size> Sets the maximum stack size (default 8M)  Egs: -K32k   -K512k",
320 "  -k<size> Sets the initial thread stack size (default 1k)  Egs: -k4k   -k2m",
321 "",
322 "  -A<size> Sets the minimum allocation area size (default 256k) Egs: -A1m -A10k",
323 "  -M<size> Sets the maximum heap size (default unlimited)  Egs: -M256k -M1G",
324 "  -H<size> Sets the minimum heap size (default 0M)   Egs: -H24m  -H1G",
325 "  -m<n>    Minimum % of heap which must be available (default 3%)",
326 "  -G<n>    Number of generations (default: 2)",
327 "  -T<n>    Number of steps in younger generations (default: 2)",
328 "  -c<n>    Auto-enable compaction of the oldest generation when live data is",
329 "           at least <n>% of the maximum heap size set with -M (default: 30%)",
330 "  -c       Enable compaction for all major collections",
331 #if defined(THREADED_RTS)
332 "  -I<sec>  Perform full GC after <sec> idle time (default: 0.3, 0 == off)",
333 #endif
334 "",
335 "  -t<file> One-line GC statistics  (default file: <program>.stat)",
336 "  -s<file> Summary  GC statistics  (with -Sstderr going to stderr)",
337 "  -S<file> Detailed GC statistics",
338 #ifdef RTS_GTK_FRONTPANEL
339 "  -f       Display front panel (requires X11 & GTK+)",
340 #endif
341 "",
342 "",
343 "  -Z       Don't squeeze out update frames on stack overflow",
344 "  -B       Sound the bell at the start of each garbage collection",
345 #if defined(PROFILING) || defined(PAR)
346 "",
347 "  -px      Time/allocation profile (XML)  (output file <program>.prof)",
348 "  -p       Time/allocation profile        (output file <program>.prof)",
349 "  -P       More detailed Time/Allocation profile",
350 "  -Pa      Give information about *all* cost centres",
351
352 # if defined(PROFILING)
353 "",
354 "  -hx            Heap residency profile (XML)   (output file <program>.prof)",
355 "  -h<break-down> Heap residency profile (hp2ps) (output file <program>.hp)",
356 "     break-down: c = cost centre stack (default)",
357 "                 m = module",
358 "                 d = closure description",
359 "                 y = type description",
360 "                 r = retainer",
361 "                 b = biography (LAG,DRAG,VOID,USE)",
362 "  A subset of closures may be selected thusly:",
363 "    -hc<cc>,...  specific cost centre(s) (top of stack only)",
364 "    -hC<cc>,...  specific cost centre(s) (anywhere in stack)",
365 "    -hm<mod>...  all cost centres from the specified modules(s)",
366 "    -hd<des>,... closures with specified closure descriptions",
367 "    -hy<typ>...  closures with specified type descriptions",
368 "    -hr<cc>...   closures with specified retainers",
369 "    -hb<bio>...  closures with specified biographies (lag,drag,void,use)",
370 "",
371 "  -R<size>       Set the maximum retainer set size (default: 8)",
372 "",
373 "  -i<sec>        Time between heap samples (seconds, default: 0.1)",
374 "",
375 "  -xt            Include threads (TSOs) in a heap profile",
376 "",
377 "  -xc      Show current cost centre stack on raising an exception",
378 # endif
379 #endif /* PROFILING or PAR */
380 #if !defined(PROFILING) && defined(DEBUG)
381 "",
382 "  -h<break-down> Debugging Heap residency profile",
383 "                 (output file <program>.hp)",
384 "     break-down: L = closure label (default)",
385 "                 T = closure type (constructor, thunk etc.)",
386 #endif
387 "",
388 #if defined(TICKY_TICKY)
389 "  -r<file>  Produce reduction profiling statistics (with -rstderr for stderr)",
390 "",
391 #endif
392 #if defined(PAR)
393 "  -N<n>     Use <n> PVMish processors in parallel (default: 2)",
394 /* NB: the -N<n> is implemented by the driver!! */
395 #endif
396 "  -C<secs>  Context-switch interval in seconds.",
397 "            0 or no argument means switch as often as possible.",
398 "            Default: 0.02 sec; resolution is set by -V below.",
399 "  -V<secs>  Master tick interval in seconds.",
400 "            This sets the resolution for -C and the profile timer -i.",
401 "            Default: 0.02 sec.",
402 "",
403 "  -vs       Trace scheduler events (see also -Ds with -debug)",
404 "  -vt       Time-stamp trace messages",
405 "",
406 #if defined(DEBUG)
407 "  -Ds  DEBUG: scheduler",
408 "  -Di  DEBUG: interpreter",
409 "  -Dw  DEBUG: weak",
410 "  -DG  DEBUG: gccafs",
411 "  -Dg  DEBUG: gc",
412 "  -Db  DEBUG: block",
413 "  -DS  DEBUG: sanity",
414 "  -Dt  DEBUG: stable",
415 "  -Dp  DEBUG: prof",
416 "  -Dr  DEBUG: gran",
417 "  -DP  DEBUG: par",
418 "  -Dl  DEBUG: linker",
419 "  -Dm  DEBUG: stm",
420 "  -Dz  DEBUG: stack squezing",
421 "",
422 #endif /* DEBUG */
423 #if defined(THREADED_RTS) && !defined(NOSMP)
424 "  -N<n>     Use <n> OS threads (default: 1)",
425 "  -qm       Don't automatically migrate threads between CPUs",
426 "  -qw       Migrate a thread to the current CPU when it is woken up",
427 #endif
428 #if defined(THREADED_RTS) || defined(PAR)
429 "  -e<size>  Size of spark pools (default 100)",
430 #endif
431 #if defined(PAR)
432 "  -t<num>   Set maximum number of advisory threads per PE (default 32)",
433 "  -qP       Enable activity profile (output files in ~/<program>*.gr)",
434 "  -qQ<size> Set pack-buffer size (default: 1024)",
435 "  -qd       Turn on PVM-ish debugging",
436 "  -qO       Disable output for performance measurement",
437 #endif
438 #if defined(THREADED_RTS) || defined(PAR)
439 "  -e<n>     Maximum number of outstanding local sparks (default: 4096)",
440 #endif
441 #if defined(PAR)
442 "  -d        Turn on PVM-ish debugging",
443 "  -O        Disable output for performance measurement",
444 #endif /* PAR */
445 #if defined(GRAN)  /* ToDo: fill in decent Docu here */
446 "  -b...     All GranSim options start with -b; see GranSim User's Guide for details",
447 #endif
448 "",
449 "RTS options may also be specified using the GHCRTS environment variable.",
450 "",
451 "Other RTS options may be available for programs compiled a different way.",
452 "The GHC User's Guide has full details.",
453 "",
454 0
455 };
456
457 STATIC_INLINE rtsBool
458 strequal(const char *a, const char * b)
459 {
460     return(strcmp(a, b) == 0);
461 }
462
463 static void
464 splitRtsFlags(char *s, int *rts_argc, char *rts_argv[])
465 {
466     char *c1, *c2;
467
468     c1 = s;
469     do {
470         while (isspace(*c1)) { c1++; };
471         c2 = c1;
472         while (!isspace(*c2) && *c2 != '\0') { c2++; };
473         
474         if (c1 == c2) { break; }
475         
476         if (*rts_argc < MAX_RTS_ARGS-1) {
477             s = stgMallocBytes(c2-c1+1, "RtsFlags.c:splitRtsFlags()");
478             strncpy(s, c1, c2-c1);
479             s[c2-c1] = '\0';
480             rts_argv[(*rts_argc)++] = s;
481         } else {
482             barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
483         }
484         
485         c1 = c2;
486     } while (*c1 != '\0');
487 }
488     
489 void
490 setupRtsFlags(int *argc, char *argv[], int *rts_argc, char *rts_argv[])
491 {
492     rtsBool error = rtsFalse;
493     I_ mode;
494     I_ arg, total_arg;
495
496     setProgName (argv);
497     total_arg = *argc;
498     arg = 1;
499
500     *argc = 1;
501     *rts_argc = 0;
502
503     // process arguments from the ghc_rts_opts global variable first.
504     // (arguments from the GHCRTS environment variable and the command
505     // line override these).
506     {
507         if (ghc_rts_opts != NULL) {
508             splitRtsFlags(ghc_rts_opts, rts_argc, rts_argv);
509         }
510     }
511
512     // process arguments from the GHCRTS environment variable next
513     // (arguments from the command line override these).
514     {
515         char *ghc_rts = getenv("GHCRTS");
516
517         if (ghc_rts != NULL) {
518             splitRtsFlags(ghc_rts, rts_argc, rts_argv);
519         }
520     }
521
522     // Split arguments (argv) into PGM (argv) and RTS (rts_argv) parts
523     //   argv[0] must be PGM argument -- leave in argv
524
525     for (mode = PGM; arg < total_arg; arg++) {
526         // The '--RTS' argument disables all future +RTS ... -RTS processing.
527         if (strequal("--RTS", argv[arg])) {
528             arg++;
529             break;
530         }
531         // The '--' argument is passed through to the program, but
532         // disables all further +RTS ... -RTS processing.
533         else if (strequal("--", argv[arg])) {
534             break;
535         }
536         else if (strequal("+RTS", argv[arg])) {
537             mode = RTS;
538         }
539         else if (strequal("-RTS", argv[arg])) {
540             mode = PGM;
541         }
542         else if (mode == RTS && *rts_argc < MAX_RTS_ARGS-1) {
543             rts_argv[(*rts_argc)++] = argv[arg];
544         }
545         else if (mode == PGM) {
546             argv[(*argc)++] = argv[arg];
547         }
548         else {
549           barf("too many RTS arguments (max %d)", MAX_RTS_ARGS-1);
550         }
551     }
552     // process remaining program arguments
553     for (; arg < total_arg; arg++) {
554         argv[(*argc)++] = argv[arg];
555     }
556     argv[*argc] = (char *) 0;
557     rts_argv[*rts_argc] = (char *) 0;
558
559     // Process RTS (rts_argv) part: mainly to determine statsfile
560     for (arg = 0; arg < *rts_argc; arg++) {
561         if (rts_argv[arg][0] != '-') {
562             fflush(stdout);
563             errorBelch("unexpected RTS argument: %s", rts_argv[arg]);
564             error = rtsTrue;
565
566         } else {
567             switch(rts_argv[arg][1]) {
568
569               /* process: general args, then PROFILING-only ones,
570                  then CONCURRENT-only, PARallel-only, GRAN-only,
571                  TICKY-only (same order as defined in RtsFlags.lh);
572                  within those groups, mostly in case-insensitive
573                  alphabetical order.
574                  Final group is x*, which allows for more options.
575               */
576
577 #ifdef TICKY_TICKY
578 # define TICKY_BUILD_ONLY(x) x
579 #else
580 # define TICKY_BUILD_ONLY(x) \
581 errorBelch("not built for: ticky-ticky stats"); \
582 error = rtsTrue;
583 #endif
584
585 #if defined(PROFILING) 
586 # define COST_CENTRE_USING_BUILD_ONLY(x) x
587 #else
588 # define COST_CENTRE_USING_BUILD_ONLY(x) \
589 errorBelch("not built for: -prof or -parallel"); \
590 error = rtsTrue;
591 #endif
592
593 #ifdef PROFILING
594 # define PROFILING_BUILD_ONLY(x)   x
595 #else
596 # define PROFILING_BUILD_ONLY(x) \
597 errorBelch("not built for: -prof"); \
598 error = rtsTrue;
599 #endif
600
601 #ifdef PAR
602 # define PAR_BUILD_ONLY(x)      x
603 #else
604 # define PAR_BUILD_ONLY(x) \
605 errorBelch("not built for: -parallel"); \
606 error = rtsTrue;
607 #endif
608
609 #ifdef THREADED_RTS
610 # define THREADED_BUILD_ONLY(x)      x
611 #else
612 # define THREADED_BUILD_ONLY(x) \
613 errorBelch("not built for: -smp"); \
614 error = rtsTrue;
615 #endif
616
617 #if defined(THREADED_RTS) || defined(PAR)
618 # define PAR_OR_THREADED_BUILD_ONLY(x)      x
619 #else
620 # define PAR_OR_THREADED_BUILD_ONLY(x) \
621 errorBelch("not built for: -parallel or -smp"); \
622 error = rtsTrue;
623 #endif
624
625 #ifdef GRAN
626 # define GRAN_BUILD_ONLY(x)     x
627 #else
628 # define GRAN_BUILD_ONLY(x) \
629 errorBelch("not built for: -gransim"); \
630 error = rtsTrue;
631 #endif
632
633               /* =========== GENERAL ========================== */
634               case '?':
635                 error = rtsTrue;
636                 break;
637
638               case 'A':
639                 RtsFlags.GcFlags.minAllocAreaSize
640                   = decode(rts_argv[arg]+2) / BLOCK_SIZE;
641                 if (RtsFlags.GcFlags.minAllocAreaSize <= 0) {
642                   bad_option(rts_argv[arg]);
643                 }
644                 break;
645
646               case 'B':
647                 RtsFlags.GcFlags.ringBell = rtsTrue;
648                 break;
649
650               case 'c':
651                   if (rts_argv[arg][2] != '\0') {
652                       RtsFlags.GcFlags.compactThreshold =
653                           atof(rts_argv[arg]+2);
654                   } else {
655                       RtsFlags.GcFlags.compact = rtsTrue;
656                   }
657                   break;
658
659               case 'F':
660                 RtsFlags.GcFlags.oldGenFactor = atof(rts_argv[arg]+2);
661               
662                 if (RtsFlags.GcFlags.oldGenFactor < 0)
663                   bad_option( rts_argv[arg] );
664                 break;
665               
666 #ifdef DEBUG
667               case 'D':
668               { 
669                   char *c;
670
671                   for (c  = rts_argv[arg] + 2; *c != '\0'; c++) {
672                       switch (*c) {
673                       case 's':
674                           RtsFlags.DebugFlags.scheduler = rtsTrue;
675                           break;
676                       case 'i':
677                           RtsFlags.DebugFlags.interpreter = rtsTrue;
678                           break;
679                       case 'w':
680                           RtsFlags.DebugFlags.weak = rtsTrue;
681                           break;
682                       case 'G':
683                           RtsFlags.DebugFlags.gccafs = rtsTrue;
684                           break;
685                       case 'g':
686                           RtsFlags.DebugFlags.gc = rtsTrue;
687                           break;
688                       case 'b':
689                           RtsFlags.DebugFlags.block_alloc = rtsTrue;
690                           break;
691                       case 'S':
692                           RtsFlags.DebugFlags.sanity = rtsTrue;
693                           break;
694                       case 't':
695                           RtsFlags.DebugFlags.stable = rtsTrue;
696                           break;
697                       case 'p':
698                           RtsFlags.DebugFlags.prof = rtsTrue;
699                           break;
700                       case 'r':
701                           RtsFlags.DebugFlags.gran = rtsTrue;
702                           break;
703                       case 'P':
704                           RtsFlags.DebugFlags.par = rtsTrue;
705                           break;
706                       case 'l':
707                           RtsFlags.DebugFlags.linker = rtsTrue;
708                           break;
709                       case 'a':
710                           RtsFlags.DebugFlags.apply = rtsTrue;
711                           break;
712                       case 'm':
713                           RtsFlags.DebugFlags.stm = rtsTrue;
714                           break;
715                       case 'z':
716                           RtsFlags.DebugFlags.squeeze = rtsTrue;
717                           break;
718                       default:
719                           bad_option( rts_argv[arg] );
720                       }
721                   }
722                   break;
723               }
724 #endif
725
726               case 'K':
727                 RtsFlags.GcFlags.maxStkSize = 
728                   decode(rts_argv[arg]+2) / sizeof(W_);
729
730                 if (RtsFlags.GcFlags.maxStkSize == 0) 
731                   bad_option( rts_argv[arg] );
732                 break;
733
734               case 'k':
735                 RtsFlags.GcFlags.initialStkSize = 
736                   decode(rts_argv[arg]+2) / sizeof(W_);
737
738                 if (RtsFlags.GcFlags.initialStkSize == 0) 
739                   bad_option( rts_argv[arg] );
740                 break;
741
742               case 'M':
743                 RtsFlags.GcFlags.maxHeapSize = 
744                   decode(rts_argv[arg]+2) / BLOCK_SIZE;
745                 /* user give size in *bytes* but "maxHeapSize" is in *blocks* */
746
747                 if (RtsFlags.GcFlags.maxHeapSize <= 0) {
748                   bad_option(rts_argv[arg]);
749                 }
750                 break;
751
752               case 'm':
753                 RtsFlags.GcFlags.pcFreeHeap = atof(rts_argv[arg]+2);
754
755                 if (RtsFlags.GcFlags.pcFreeHeap < 0 || 
756                     RtsFlags.GcFlags.pcFreeHeap > 100)
757                   bad_option( rts_argv[arg] );
758                 break;
759
760               case 'G':
761                 RtsFlags.GcFlags.generations = decode(rts_argv[arg]+2);
762                 if (RtsFlags.GcFlags.generations < 1) {
763                   bad_option(rts_argv[arg]);
764                 }
765                 break;
766
767               case 'T':
768                 RtsFlags.GcFlags.steps = decode(rts_argv[arg]+2);
769                 if (RtsFlags.GcFlags.steps < 1) {
770                   bad_option(rts_argv[arg]);
771                 }
772                 break;
773
774               case 'H':
775                 RtsFlags.GcFlags.heapSizeSuggestion = 
776                   decode(rts_argv[arg]+2) / BLOCK_SIZE;
777
778                 if (RtsFlags.GcFlags.heapSizeSuggestion <= 0) {
779                   bad_option(rts_argv[arg]);
780                 }
781                 break;
782
783 #ifdef RTS_GTK_FRONTPANEL
784               case 'f':
785                   RtsFlags.GcFlags.frontpanel = rtsTrue;
786                   break;
787 #endif
788
789               case 'I': /* idle GC delay */
790                 if (rts_argv[arg][2] == '\0') {
791                   /* use default */
792                 } else {
793                     I_ cst; /* tmp */
794
795                     /* Convert to millisecs */
796                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
797                     RtsFlags.GcFlags.idleGCDelayTime = cst;
798                 }
799                 break;
800
801               case 'S':
802                   RtsFlags.GcFlags.giveStats = VERBOSE_GC_STATS;
803                   goto stats;
804
805               case 's':
806                   RtsFlags.GcFlags.giveStats = SUMMARY_GC_STATS;
807                   goto stats;
808
809               case 't':
810                   RtsFlags.GcFlags.giveStats = ONELINE_GC_STATS;
811                   goto stats;
812
813             stats:
814 #ifdef PAR
815                 /* Opening all those files would almost certainly fail... */
816                 // RtsFlags.ParFlags.ParStats.Full = rtsTrue;
817                 RtsFlags.GcFlags.statsFile = NULL; /* temporary; ToDo: rm */
818 #else
819                 { 
820                     int r;
821                     r = open_stats_file(arg, *argc, argv,
822                                         *rts_argc, rts_argv, STAT_FILENAME_FMT,
823                                         &RtsFlags.GcFlags.statsFile);
824                     if (r == -1) { error = rtsTrue; }
825                 }
826 #endif
827                   break;
828
829               case 'Z':
830                 RtsFlags.GcFlags.squeezeUpdFrames = rtsFalse;
831                 break;
832
833               /* =========== PROFILING ========================== */
834
835               case 'P': /* detailed cost centre profiling (time/alloc) */
836               case 'p': /* cost centre profiling (time/alloc) */
837                 COST_CENTRE_USING_BUILD_ONLY(
838                 switch (rts_argv[arg][2]) {
839                   case 'x':
840                     RtsFlags.CcFlags.doCostCentres = COST_CENTRES_XML;
841                     break;
842                   case 'a':
843                     RtsFlags.CcFlags.doCostCentres = COST_CENTRES_ALL;
844                     break;
845                   default:
846                       if (rts_argv[arg][1] == 'P') {
847                           RtsFlags.CcFlags.doCostCentres =
848                               COST_CENTRES_VERBOSE;
849                       } else {
850                           RtsFlags.CcFlags.doCostCentres =
851                               COST_CENTRES_SUMMARY;
852                       }
853                       break;
854                 }
855                 ) break;
856
857               case 'R':
858                   PROFILING_BUILD_ONLY(
859                       RtsFlags.ProfFlags.maxRetainerSetSize = atof(rts_argv[arg]+2);
860                   ) break;
861
862               case 'h': /* serial heap profile */
863 #if !defined(PROFILING) && defined(DEBUG)
864                 switch (rts_argv[arg][2]) {
865                   case '\0':
866                   case 'L':
867                     RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_INFOPTR;
868                     break;
869                   case 'T':
870                     RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CLOSURE_TYPE;
871                     break;
872                   default:
873                     errorBelch("invalid heap profile option: %s",rts_argv[arg]);
874                     error = rtsTrue;
875                 }
876 #else
877                 PROFILING_BUILD_ONLY(
878                 switch (rts_argv[arg][2]) {
879                 case '\0':
880                 case 'C':
881                 case 'c':
882                 case 'M':
883                 case 'm':
884                 case 'D':
885                 case 'd':
886                 case 'Y':
887                 case 'y':
888                 case 'R':
889                 case 'r':
890                 case 'B':
891                 case 'b':
892                     if (rts_argv[arg][2] != '\0' && rts_argv[arg][3] != '\0') {
893                         {
894                             char *left  = strchr(rts_argv[arg], '{');
895                             char *right = strrchr(rts_argv[arg], '}');
896
897                             // curly braces are optional, for
898                             // backwards compat.
899                             if (left)
900                                 left = left+1;
901                             else
902                                 left = rts_argv[arg] + 3;
903
904                             if (!right)
905                                 right = rts_argv[arg] + strlen(rts_argv[arg]);
906
907                             *right = '\0';
908
909                             switch (rts_argv[arg][2]) {
910                             case 'c': // cost centre label select
911                                 RtsFlags.ProfFlags.ccSelector = left;
912                                 break;
913                             case 'C':
914                                 RtsFlags.ProfFlags.ccsSelector = left;
915                                 break;
916                             case 'M':
917                             case 'm': // cost centre module select
918                                 RtsFlags.ProfFlags.modSelector = left;
919                                 break;
920                             case 'D':
921                             case 'd': // closure descr select 
922                                 RtsFlags.ProfFlags.descrSelector = left;
923                                 break;
924                             case 'Y':
925                             case 'y': // closure type select
926                                 RtsFlags.ProfFlags.typeSelector = left;
927                                 break;
928                             case 'R':
929                             case 'r': // retainer select
930                                 RtsFlags.ProfFlags.retainerSelector = left;
931                                 break;
932                             case 'B':
933                             case 'b': // biography select
934                                 RtsFlags.ProfFlags.bioSelector = left;
935                                 break;
936                             }
937                         }
938                         break;
939                     }
940
941                     if (RtsFlags.ProfFlags.doHeapProfile != 0) {
942                         errorBelch("multiple heap profile options");
943                         error = rtsTrue;
944                         break;
945                     }
946
947                     switch (rts_argv[arg][2]) {
948                     case '\0':
949                     case 'C':
950                     case 'c':
951                         RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_CCS;
952                         break;
953                     case 'M':
954                     case 'm':
955                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_MOD;
956                           break;
957                     case 'D':
958                     case 'd':
959                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_DESCR;
960                           break;
961                     case 'Y':
962                     case 'y':
963                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_TYPE;
964                           break;
965                     case 'R':
966                     case 'r':
967                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_RETAINER;
968                           break;
969                     case 'B':
970                     case 'b':
971                           RtsFlags.ProfFlags.doHeapProfile = HEAP_BY_LDV;
972                           break;
973                     }
974                     break;
975                       
976                 default:
977                     errorBelch("invalid heap profile option: %s",rts_argv[arg]);
978                     error = rtsTrue;
979                 }
980                 ) 
981 #endif /* PROFILING */
982                 break;
983
984 #if defined(PROFILING) 
985               case 'i': /* heap sample interval */
986                 if (rts_argv[arg][2] == '\0') {
987                   /* use default */
988                 } else {
989                     I_ cst; /* tmp */
990
991                     /* Convert to milliseconds */
992                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
993                     RtsFlags.ProfFlags.profileInterval = cst;
994                 }
995                 break;
996 #endif
997
998               /* =========== CONCURRENT ========================= */
999               case 'C': /* context switch interval */
1000                 if (rts_argv[arg][2] == '\0')
1001                     RtsFlags.ConcFlags.ctxtSwitchTime = 0;
1002                 else {
1003                     I_ cst; /* tmp */
1004
1005                     /* Convert to milliseconds */
1006                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1007                     RtsFlags.ConcFlags.ctxtSwitchTime = cst;
1008                 }
1009                 break;
1010
1011               case 'V': /* master tick interval */
1012                 if (rts_argv[arg][2] == '\0') {
1013                     // turns off ticks completely
1014                     RtsFlags.MiscFlags.tickInterval = 0;
1015                 } else {
1016                     I_ cst; /* tmp */
1017
1018                     /* Convert to milliseconds */
1019                     cst = (I_) ((atof(rts_argv[arg]+2) * 1000));
1020                     RtsFlags.MiscFlags.tickInterval = cst;
1021                 }
1022                 break;
1023
1024 #if defined(THREADED_RTS) && !defined(NOSMP)
1025               case 'N':
1026                 THREADED_BUILD_ONLY(
1027                 if (rts_argv[arg][2] != '\0') {
1028                     RtsFlags.ParFlags.nNodes
1029                       = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1030                     if (RtsFlags.ParFlags.nNodes <= 0) {
1031                       errorBelch("bad value for -N");
1032                       error = rtsTrue;
1033                     }
1034                 }
1035                 ) break;
1036
1037               case 'q':
1038                     switch (rts_argv[arg][2]) {
1039                     case '\0':
1040                         errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1041                         error = rtsTrue;
1042                         break;
1043                     case 'm':
1044                         RtsFlags.ParFlags.migrate = rtsFalse;
1045                         break;
1046                     case 'w':
1047                         RtsFlags.ParFlags.wakeupMigrate = rtsTrue;
1048                         break;
1049                     default:
1050                         errorBelch("unknown RTS option: %s",rts_argv[arg]);
1051                         error = rtsTrue;
1052                         break;
1053                     }
1054                     break;
1055 #endif
1056               /* =========== PARALLEL =========================== */
1057               case 'e':
1058                 PAR_OR_THREADED_BUILD_ONLY(
1059                 if (rts_argv[arg][2] != '\0') {
1060                     RtsFlags.ParFlags.maxLocalSparks
1061                       = strtol(rts_argv[arg]+2, (char **) NULL, 10);
1062                     if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1063                       errorBelch("bad value for -e");
1064                       error = rtsTrue;
1065                     }
1066                 }
1067                 ) break;
1068
1069 #ifdef PAR
1070               case 'q':
1071                 PAR_BUILD_ONLY(
1072                   process_par_option(arg, rts_argc, rts_argv, &error);
1073                 ) break;
1074 #endif
1075
1076               /* =========== GRAN =============================== */
1077
1078               case 'b':
1079                 GRAN_BUILD_ONLY(
1080                   process_gran_option(arg, rts_argc, rts_argv, &error);
1081                 ) break;
1082
1083               /* =========== TICKY ============================== */
1084
1085               case 'r': /* Basic profiling stats */
1086                 TICKY_BUILD_ONLY(
1087
1088                 RtsFlags.TickyFlags.showTickyStats = rtsTrue;
1089
1090                 { 
1091                     int r;
1092                     r = open_stats_file(arg, *argc, argv,
1093                                         *rts_argc, rts_argv, TICKY_FILENAME_FMT,
1094                                         &RtsFlags.TickyFlags.tickyFile);
1095                     if (r == -1) { error = rtsTrue; }
1096                 }
1097                 ) break;
1098
1099               /* =========== TRACING ---------=================== */
1100
1101               case 'v':
1102                 switch(rts_argv[arg][2]) {
1103                 case '\0':
1104                     errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1105                     error = rtsTrue;
1106                     break;
1107                 case 't':
1108                     RtsFlags.TraceFlags.timestamp = rtsTrue;
1109                     break;
1110                 case 's':
1111                     RtsFlags.TraceFlags.sched = rtsTrue;
1112                     break;
1113                 default:
1114                     errorBelch("unknown RTS option: %s",rts_argv[arg]);
1115                     error = rtsTrue;
1116                     break;
1117                 }
1118                 break;
1119
1120               /* =========== EXTENDED OPTIONS =================== */
1121
1122               case 'x': /* Extend the argument space */
1123                 switch(rts_argv[arg][2]) {
1124                   case '\0':
1125                     errorBelch("incomplete RTS option: %s",rts_argv[arg]);
1126                     error = rtsTrue;
1127                     break;
1128
1129                   case 'c': /* Debugging tool: show current cost centre on an exception */
1130                     PROFILING_BUILD_ONLY(
1131                         RtsFlags.ProfFlags.showCCSOnException = rtsTrue;
1132                         );
1133                     break;
1134
1135                 case 't':  /* Include memory used by TSOs in a heap profile */
1136                     PROFILING_BUILD_ONLY(
1137                         RtsFlags.ProfFlags.includeTSOs = rtsTrue;
1138                         );
1139                     break;
1140
1141                   /* The option prefix '-xx' is reserved for future extension.  KSW 1999-11. */
1142
1143                   default:
1144                     errorBelch("unknown RTS option: %s",rts_argv[arg]);
1145                     error = rtsTrue;
1146                     break;
1147                 }
1148                 break;  /* defensive programming */
1149
1150               /* =========== OH DEAR ============================ */
1151               default:
1152                 errorBelch("unknown RTS option: %s",rts_argv[arg]);
1153                 error = rtsTrue;
1154                 break;
1155             }
1156         }
1157     }
1158
1159     // Determine what tick interval we should use for the RTS timer
1160     // by taking the shortest of the various intervals that we need to
1161     // monitor.
1162     if (RtsFlags.MiscFlags.tickInterval <= 0) {
1163         RtsFlags.MiscFlags.tickInterval = 50;
1164     }
1165
1166     if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1167         RtsFlags.MiscFlags.tickInterval =
1168             stg_min(RtsFlags.ConcFlags.ctxtSwitchTime,
1169                     RtsFlags.MiscFlags.tickInterval);
1170     }
1171
1172     if (RtsFlags.GcFlags.idleGCDelayTime > 0) {
1173         RtsFlags.MiscFlags.tickInterval =
1174             stg_min(RtsFlags.GcFlags.idleGCDelayTime,
1175                     RtsFlags.MiscFlags.tickInterval);
1176     }
1177
1178 #ifdef PROFILING
1179     if (RtsFlags.ProfFlags.profileInterval > 0) {
1180         RtsFlags.MiscFlags.tickInterval =
1181             stg_min(RtsFlags.ProfFlags.profileInterval,
1182                     RtsFlags.MiscFlags.tickInterval);
1183     }
1184 #endif
1185
1186     if (RtsFlags.ConcFlags.ctxtSwitchTime > 0) {
1187         RtsFlags.ConcFlags.ctxtSwitchTicks =
1188             RtsFlags.ConcFlags.ctxtSwitchTime /
1189             RtsFlags.MiscFlags.tickInterval;
1190     } else {
1191         RtsFlags.ConcFlags.ctxtSwitchTicks = 0;
1192     }
1193
1194 #ifdef PROFILING
1195     RtsFlags.ProfFlags.profileIntervalTicks =
1196         RtsFlags.ProfFlags.profileInterval / RtsFlags.MiscFlags.tickInterval;
1197 #endif
1198
1199     if (error) {
1200         const char **p;
1201
1202         fflush(stdout);
1203         for (p = usage_text; *p; p++)
1204             errorBelch("%s", *p);
1205         stg_exit(EXIT_FAILURE);
1206     }
1207 }
1208
1209 #if defined(GRAN)
1210
1211 static void
1212 enable_GranSimLight(void) {
1213
1214     debugBelch("GrAnSim Light enabled (infinite number of processors;  0 communication costs)\n");
1215     RtsFlags.GranFlags.Light=rtsTrue;
1216     RtsFlags.GranFlags.Costs.latency = 
1217         RtsFlags.GranFlags.Costs.fetchtime = 
1218         RtsFlags.GranFlags.Costs.additional_latency =
1219         RtsFlags.GranFlags.Costs.gunblocktime = 
1220         RtsFlags.GranFlags.Costs.lunblocktime =
1221         RtsFlags.GranFlags.Costs.threadcreatetime = 
1222         RtsFlags.GranFlags.Costs.threadqueuetime =
1223         RtsFlags.GranFlags.Costs.threadscheduletime = 
1224         RtsFlags.GranFlags.Costs.threaddescheduletime =
1225         RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
1226   
1227     RtsFlags.GranFlags.Costs.mpacktime = 
1228         RtsFlags.GranFlags.Costs.munpacktime = 0;
1229
1230     RtsFlags.GranFlags.DoFairSchedule = rtsTrue;
1231     RtsFlags.GranFlags.DoAsyncFetch = rtsFalse;
1232     RtsFlags.GranFlags.DoAlwaysCreateThreads = rtsTrue;
1233     /* FetchStrategy is irrelevant in GrAnSim-Light */
1234
1235     /* GrAnSim Light often creates an abundance of parallel threads,
1236        each with its own stack etc. Therefore, it's in general a good
1237        idea to use small stack chunks (use the -o<size> option to 
1238        increase it again). 
1239     */
1240     // RtsFlags.ConcFlags.stkChunkSize = 100;
1241
1242     RtsFlags.GranFlags.proc = 1; 
1243 }
1244
1245 static void
1246 process_gran_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error)
1247 {
1248     if (rts_argv[arg][1] != 'b') /* All GranSim options start with -b */
1249       return;
1250
1251     /* or a ridiculously idealised simulator */
1252     if(strcmp((rts_argv[arg]+2),"oring")==0) {
1253       RtsFlags.GranFlags.Costs.latency = 
1254         RtsFlags.GranFlags.Costs.fetchtime = 
1255         RtsFlags.GranFlags.Costs.additional_latency =
1256         RtsFlags.GranFlags.Costs.gunblocktime = 
1257         RtsFlags.GranFlags.Costs.lunblocktime =
1258         RtsFlags.GranFlags.Costs.threadcreatetime = 
1259         RtsFlags.GranFlags.Costs.threadqueuetime =
1260         RtsFlags.GranFlags.Costs.threadscheduletime = 
1261         RtsFlags.GranFlags.Costs.threaddescheduletime =
1262         RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
1263
1264       RtsFlags.GranFlags.Costs.mpacktime = 
1265         RtsFlags.GranFlags.Costs.munpacktime = 0;
1266
1267       RtsFlags.GranFlags.Costs.arith_cost = 
1268         RtsFlags.GranFlags.Costs.float_cost = 
1269         RtsFlags.GranFlags.Costs.load_cost =
1270         RtsFlags.GranFlags.Costs.store_cost = 
1271         RtsFlags.GranFlags.Costs.branch_cost = 0;
1272
1273       RtsFlags.GranFlags.Costs.heapalloc_cost = 1;
1274
1275       /* ++RtsFlags.GranFlags.DoFairSchedule; */
1276       RtsFlags.GranFlags.DoStealThreadsFirst = rtsTrue;        /* -bZ */
1277       RtsFlags.GranFlags.DoThreadMigration   = rtsTrue;        /* -bM */
1278       RtsFlags.GranFlags.GranSimStats.Full   = rtsTrue;        /* -bP */
1279       return;
1280     }
1281
1282       /* or a somewhat idealised simulator */
1283       if(strcmp((rts_argv[arg]+2),"onzo")==0) {
1284         RtsFlags.GranFlags.Costs.latency = 
1285         RtsFlags.GranFlags.Costs.fetchtime = 
1286         RtsFlags.GranFlags.Costs.additional_latency =
1287         RtsFlags.GranFlags.Costs.gunblocktime = 
1288         RtsFlags.GranFlags.Costs.lunblocktime =
1289         RtsFlags.GranFlags.Costs.threadcreatetime = 
1290         RtsFlags.GranFlags.Costs.threadqueuetime =
1291         RtsFlags.GranFlags.Costs.threadscheduletime = 
1292         RtsFlags.GranFlags.Costs.threaddescheduletime =
1293         RtsFlags.GranFlags.Costs.threadcontextswitchtime = 0;
1294
1295         RtsFlags.GranFlags.Costs.mpacktime = 
1296         RtsFlags.GranFlags.Costs.munpacktime = 0;
1297         
1298         RtsFlags.GranFlags.Costs.heapalloc_cost = 1;
1299
1300         /* RtsFlags.GranFlags.DoFairSchedule  = rtsTrue; */       /* -b-R */
1301         /* RtsFlags.GranFlags.DoStealThreadsFirst = rtsTrue; */   /* -b-T */
1302         RtsFlags.GranFlags.DoAsyncFetch = rtsTrue;         /* -bZ */
1303         RtsFlags.GranFlags.DoThreadMigration  = rtsTrue;          /* -bM */
1304         RtsFlags.GranFlags.GranSimStats.Full  = rtsTrue;          /* -bP */
1305 #  if defined(GRAN_CHECK) && defined(GRAN)
1306         RtsFlags.GranFlags.Debug.event_stats = rtsTrue; /* print event statistics   */
1307 #  endif
1308         return;
1309       }
1310
1311       /* Communication and task creation cost parameters */
1312       switch(rts_argv[arg][2]) {
1313         case '.':
1314           IgnoreYields = rtsTrue; // HWL HACK
1315           break;
1316
1317         case ':':
1318           enable_GranSimLight();       /* set flags for GrAnSim-Light mode */
1319           break;
1320
1321         case 'l':
1322           if (rts_argv[arg][3] != '\0')
1323             {
1324               RtsFlags.GranFlags.Costs.gunblocktime = 
1325               RtsFlags.GranFlags.Costs.latency = decode(rts_argv[arg]+3);
1326               RtsFlags.GranFlags.Costs.fetchtime = 2*RtsFlags.GranFlags.Costs.latency;
1327             }
1328           else
1329             RtsFlags.GranFlags.Costs.latency = LATENCY;
1330           break;
1331
1332         case 'a':
1333           if (rts_argv[arg][3] != '\0')
1334             RtsFlags.GranFlags.Costs.additional_latency = decode(rts_argv[arg]+3);
1335           else
1336             RtsFlags.GranFlags.Costs.additional_latency = ADDITIONAL_LATENCY;
1337           break;
1338
1339         case 'm':
1340           if (rts_argv[arg][3] != '\0')
1341             RtsFlags.GranFlags.Costs.mpacktime = decode(rts_argv[arg]+3);
1342           else
1343             RtsFlags.GranFlags.Costs.mpacktime = MSGPACKTIME;
1344           break;
1345
1346         case 'x':
1347           if (rts_argv[arg][3] != '\0')
1348             RtsFlags.GranFlags.Costs.mtidytime = decode(rts_argv[arg]+3);
1349           else
1350             RtsFlags.GranFlags.Costs.mtidytime = 0;
1351           break;
1352
1353         case 'r':
1354           if (rts_argv[arg][3] != '\0')
1355             RtsFlags.GranFlags.Costs.munpacktime = decode(rts_argv[arg]+3);
1356           else
1357             RtsFlags.GranFlags.Costs.munpacktime = MSGUNPACKTIME;
1358           break;
1359           
1360         case 'g':
1361           if (rts_argv[arg][3] != '\0')
1362             RtsFlags.GranFlags.Costs.fetchtime = decode(rts_argv[arg]+3);
1363           else
1364             RtsFlags.GranFlags.Costs.fetchtime = FETCHTIME;
1365           break;
1366           
1367         case 'n':
1368           if (rts_argv[arg][3] != '\0')
1369             RtsFlags.GranFlags.Costs.gunblocktime = decode(rts_argv[arg]+3);
1370           else
1371             RtsFlags.GranFlags.Costs.gunblocktime = GLOBALUNBLOCKTIME;
1372           break;
1373
1374         case 'u':
1375           if (rts_argv[arg][3] != '\0')
1376             RtsFlags.GranFlags.Costs.lunblocktime = decode(rts_argv[arg]+3);
1377           else
1378             RtsFlags.GranFlags.Costs.lunblocktime = LOCALUNBLOCKTIME;
1379           break;
1380
1381         /* Thread-related metrics */
1382         case 't':
1383           if (rts_argv[arg][3] != '\0')
1384             RtsFlags.GranFlags.Costs.threadcreatetime = decode(rts_argv[arg]+3);
1385           else
1386             RtsFlags.GranFlags.Costs.threadcreatetime = THREADCREATETIME;
1387           break;
1388           
1389         case 'q':
1390           if (rts_argv[arg][3] != '\0')
1391             RtsFlags.GranFlags.Costs.threadqueuetime = decode(rts_argv[arg]+3);
1392           else
1393             RtsFlags.GranFlags.Costs.threadqueuetime = THREADQUEUETIME;
1394           break;
1395           
1396         case 'c':
1397           if (rts_argv[arg][3] != '\0')
1398             RtsFlags.GranFlags.Costs.threadscheduletime = decode(rts_argv[arg]+3);
1399           else
1400             RtsFlags.GranFlags.Costs.threadscheduletime = THREADSCHEDULETIME;
1401           
1402           RtsFlags.GranFlags.Costs.threadcontextswitchtime = RtsFlags.GranFlags.Costs.threadscheduletime
1403             + RtsFlags.GranFlags.Costs.threaddescheduletime;
1404           break;
1405
1406         case 'd':
1407           if (rts_argv[arg][3] != '\0')
1408             RtsFlags.GranFlags.Costs.threaddescheduletime = decode(rts_argv[arg]+3);
1409           else
1410             RtsFlags.GranFlags.Costs.threaddescheduletime = THREADDESCHEDULETIME;
1411           
1412           RtsFlags.GranFlags.Costs.threadcontextswitchtime = RtsFlags.GranFlags.Costs.threadscheduletime
1413             + RtsFlags.GranFlags.Costs.threaddescheduletime;
1414           break;
1415
1416         /* Instruction Cost Metrics */
1417         case 'A':
1418           if (rts_argv[arg][3] != '\0')
1419             RtsFlags.GranFlags.Costs.arith_cost = decode(rts_argv[arg]+3);
1420           else
1421             RtsFlags.GranFlags.Costs.arith_cost = ARITH_COST;
1422           break;
1423
1424         case 'F':
1425           if (rts_argv[arg][3] != '\0')
1426             RtsFlags.GranFlags.Costs.float_cost = decode(rts_argv[arg]+3);
1427           else
1428             RtsFlags.GranFlags.Costs.float_cost = FLOAT_COST;
1429           break;
1430                       
1431         case 'B':
1432           if (rts_argv[arg][3] != '\0')
1433             RtsFlags.GranFlags.Costs.branch_cost = decode(rts_argv[arg]+3);
1434           else
1435             RtsFlags.GranFlags.Costs.branch_cost = BRANCH_COST;
1436           break;
1437
1438         case 'L':
1439           if (rts_argv[arg][3] != '\0')
1440             RtsFlags.GranFlags.Costs.load_cost = decode(rts_argv[arg]+3);
1441           else
1442             RtsFlags.GranFlags.Costs.load_cost = LOAD_COST;
1443           break;
1444           
1445         case 'S':
1446           if (rts_argv[arg][3] != '\0')
1447             RtsFlags.GranFlags.Costs.store_cost = decode(rts_argv[arg]+3);
1448           else
1449             RtsFlags.GranFlags.Costs.store_cost = STORE_COST;
1450           break;
1451
1452         case 'H':
1453           if (rts_argv[arg][3] != '\0')
1454             RtsFlags.GranFlags.Costs.heapalloc_cost = decode(rts_argv[arg]+3);
1455           else
1456             RtsFlags.GranFlags.Costs.heapalloc_cost = 0;
1457           break;
1458
1459         case 'y':
1460           RtsFlags.GranFlags.DoAsyncFetch = rtsTrue;
1461           if (rts_argv[arg][3] != '\0')
1462             RtsFlags.GranFlags.FetchStrategy = decode(rts_argv[arg]+3);
1463           else
1464             RtsFlags.GranFlags.FetchStrategy = 2;
1465           if (RtsFlags.GranFlags.FetchStrategy == 0)
1466             RtsFlags.GranFlags.DoAsyncFetch = rtsFalse;
1467           break;
1468           
1469         case 'K':   /* sort overhead (per elem in spark list) */
1470           if (rts_argv[arg][3] != '\0')
1471             RtsFlags.GranFlags.Costs.pri_spark_overhead = decode(rts_argv[arg]+3);
1472           else
1473             RtsFlags.GranFlags.Costs.pri_spark_overhead = PRI_SPARK_OVERHEAD;
1474           debugBelch("Overhead for pri spark: %d (per elem).\n",
1475                          RtsFlags.GranFlags.Costs.pri_spark_overhead);
1476           break;
1477
1478         case 'O':  /* sort overhead (per elem in spark list) */
1479           if (rts_argv[arg][3] != '\0')
1480             RtsFlags.GranFlags.Costs.pri_sched_overhead = decode(rts_argv[arg]+3);
1481           else
1482             RtsFlags.GranFlags.Costs.pri_sched_overhead = PRI_SCHED_OVERHEAD;
1483           debugBelch("Overhead for pri sched: %d (per elem).\n",
1484                        RtsFlags.GranFlags.Costs.pri_sched_overhead);
1485           break;
1486
1487         /* General Parameters */
1488         case 'p':
1489           if (rts_argv[arg][3] != '\0')
1490             {
1491               RtsFlags.GranFlags.proc = decode(rts_argv[arg]+3);
1492               if (RtsFlags.GranFlags.proc==0) {
1493                   enable_GranSimLight(); /* set flags for GrAnSim-Light mode */
1494               } else if (RtsFlags.GranFlags.proc > MAX_PROC || 
1495                          RtsFlags.GranFlags.proc < 1)
1496                 {
1497                   debugBelch("setupRtsFlags: no more than %u processors allowed\n",
1498                           MAX_PROC);
1499                   *error = rtsTrue;
1500                 }
1501             }
1502           else
1503             RtsFlags.GranFlags.proc = MAX_PROC;
1504           break;
1505
1506         case 'f':
1507           RtsFlags.GranFlags.Fishing = rtsTrue;
1508           if (rts_argv[arg][3] != '\0')
1509             RtsFlags.GranFlags.maxFishes = decode(rts_argv[arg]+3);
1510           else
1511             RtsFlags.GranFlags.maxFishes = MAX_FISHES;
1512           break;
1513           
1514         case 'w':
1515           if (rts_argv[arg][3] != '\0')
1516             RtsFlags.GranFlags.time_slice = decode(rts_argv[arg]+3);
1517           else
1518             RtsFlags.GranFlags.time_slice = GRAN_TIME_SLICE;
1519           break;
1520           
1521         case 'C':
1522           RtsFlags.GranFlags.DoAlwaysCreateThreads=rtsTrue;
1523           RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
1524           break;
1525
1526         case 'G':
1527           debugBelch("Bulk fetching enabled.\n");
1528           RtsFlags.GranFlags.DoBulkFetching=rtsTrue;
1529           break;
1530           
1531         case 'M':
1532           debugBelch("Thread migration enabled.\n");
1533           RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
1534           break;
1535
1536         case 'R':
1537           debugBelch("Fair Scheduling enabled.\n");
1538           RtsFlags.GranFlags.DoFairSchedule=rtsTrue;
1539           break;
1540           
1541         case 'I':
1542           debugBelch("Priority Scheduling enabled.\n");
1543           RtsFlags.GranFlags.DoPriorityScheduling=rtsTrue;
1544           break;
1545
1546         case 'T':
1547           RtsFlags.GranFlags.DoStealThreadsFirst=rtsTrue;
1548           RtsFlags.GranFlags.DoThreadMigration=rtsTrue;
1549           break;
1550           
1551         case 'Z':
1552           RtsFlags.GranFlags.DoAsyncFetch=rtsTrue;
1553           break;
1554           
1555 /*          case 'z': */
1556 /*        RtsFlags.GranFlags.SimplifiedFetch=rtsTrue; */
1557 /*        break; */
1558           
1559         case 'N':
1560           RtsFlags.GranFlags.PreferSparksOfLocalNodes=rtsTrue;
1561           break;
1562           
1563         case 'b':
1564           RtsFlags.GranFlags.GranSimStats.Binary=rtsTrue;
1565           break;
1566           
1567         case 'P':
1568           /* format is -bP<c> where <c> is one char describing kind of profile */
1569           RtsFlags.GranFlags.GranSimStats.Full = rtsTrue;
1570           switch(rts_argv[arg][3]) {
1571           case '\0': break; // nothing special, just an ordinary profile
1572           case '0': RtsFlags.GranFlags.GranSimStats.Suppressed = rtsTrue;
1573             break;
1574           case 'b': RtsFlags.GranFlags.GranSimStats.Binary = rtsTrue;
1575             break;
1576           case 's': RtsFlags.GranFlags.GranSimStats.Sparks = rtsTrue;
1577             break;
1578           case 'h': RtsFlags.GranFlags.GranSimStats.Heap = rtsTrue;
1579             break;
1580           case 'n': RtsFlags.GranFlags.GranSimStats.NewLogfile = rtsTrue;
1581             break;
1582           case 'g': RtsFlags.GranFlags.GranSimStats.Global = rtsTrue;
1583             break;
1584           default: barf("Unknown option -bP%c", rts_argv[arg][3]);
1585           }
1586           break;
1587
1588         case 's':
1589           RtsFlags.GranFlags.GranSimStats.Sparks=rtsTrue;
1590           break;
1591
1592         case 'h':
1593           RtsFlags.GranFlags.GranSimStats.Heap=rtsTrue;
1594           break;
1595
1596         case 'Y':   /* syntax: -bY<n>[,<n>]  n ... pos int */ 
1597           if (rts_argv[arg][3] != '\0') {
1598             char *arg0, *tmp;
1599             
1600             arg0 = rts_argv[arg]+3;
1601             if ((tmp = strstr(arg0,","))==NULL) {
1602               RtsFlags.GranFlags.SparkPriority = decode(arg0);
1603               debugBelch("SparkPriority: %u.\n",RtsFlags.GranFlags.SparkPriority);
1604             } else {
1605               *(tmp++) = '\0'; 
1606               RtsFlags.GranFlags.SparkPriority = decode(arg0);
1607               RtsFlags.GranFlags.SparkPriority2 = decode(tmp);
1608               debugBelch("SparkPriority: %u.\n",
1609                       RtsFlags.GranFlags.SparkPriority);
1610               debugBelch("SparkPriority2:%u.\n",
1611                       RtsFlags.GranFlags.SparkPriority2);
1612               if (RtsFlags.GranFlags.SparkPriority2 < 
1613                   RtsFlags.GranFlags.SparkPriority) {
1614                 debugBelch("WARNING: 2nd pri < main pri (%u<%u); 2nd pri has no effect\n",
1615                         RtsFlags.GranFlags.SparkPriority2,
1616                         RtsFlags.GranFlags.SparkPriority);
1617               }
1618             }
1619           } else {
1620             /* plain pri spark is now invoked with -bX  
1621                RtsFlags.GranFlags.DoPrioritySparking = 1;
1622                debugBelch("PrioritySparking.\n");
1623             */
1624           }
1625           break;
1626
1627         case 'Q':
1628           if (rts_argv[arg][3] != '\0') {
1629             RtsFlags.GranFlags.ThunksToPack = decode(rts_argv[arg]+3);
1630           } else {
1631             RtsFlags.GranFlags.ThunksToPack = 1;
1632           }
1633           debugBelch("Thunks To Pack in one packet: %u.\n",
1634                   RtsFlags.GranFlags.ThunksToPack);
1635           break;
1636                       
1637         case 'e':
1638           RtsFlags.GranFlags.RandomSteal = rtsFalse;
1639           debugBelch("Deterministic mode (no random stealing)\n");
1640                       break;
1641
1642           /* The following class of options contains eXperimental */
1643           /* features in connection with exploiting granularity */
1644           /* information. I.e. if -bY is chosen these options */
1645           /* tell the RTS what to do with the supplied info --HWL */
1646
1647         case 'W':
1648           if (rts_argv[arg][3] != '\0') {
1649             RtsFlags.GranFlags.packBufferSize_internal = decode(rts_argv[arg]+3);
1650           } else {
1651             RtsFlags.GranFlags.packBufferSize_internal = GRANSIM_DEFAULT_PACK_BUFFER_SIZE;
1652           }
1653           debugBelch("Size of GranSim internal pack buffer: %u.\n",
1654                   RtsFlags.GranFlags.packBufferSize_internal);
1655           break;
1656                       
1657         case 'X':
1658           switch(rts_argv[arg][3]) {
1659             
1660             case '\0':
1661               RtsFlags.GranFlags.DoPrioritySparking = 1;
1662               debugBelch("Priority Sparking with Normal Priorities.\n");
1663               RtsFlags.GranFlags.InversePriorities = rtsFalse; 
1664               RtsFlags.GranFlags.RandomPriorities = rtsFalse;
1665               RtsFlags.GranFlags.IgnorePriorities = rtsFalse;
1666               break;
1667                         
1668             case 'I':
1669               RtsFlags.GranFlags.DoPrioritySparking = 1;
1670               debugBelch("Priority Sparking with Inverse Priorities.\n");
1671               RtsFlags.GranFlags.InversePriorities++; 
1672               break;
1673               
1674             case 'R': 
1675               RtsFlags.GranFlags.DoPrioritySparking = 1;
1676               debugBelch("Priority Sparking with Random Priorities.\n");
1677               RtsFlags.GranFlags.RandomPriorities++;
1678               break;
1679               
1680             case 'N':
1681               RtsFlags.GranFlags.DoPrioritySparking = 1;
1682               debugBelch("Priority Sparking with No Priorities.\n");
1683               RtsFlags.GranFlags.IgnorePriorities++;
1684               break;
1685               
1686             default:
1687               bad_option( rts_argv[arg] );
1688               break;
1689           }
1690           break;
1691
1692         case '-':
1693           switch(rts_argv[arg][3]) {
1694             
1695             case 'C':
1696               RtsFlags.GranFlags.DoAlwaysCreateThreads=rtsFalse;
1697               RtsFlags.GranFlags.DoThreadMigration=rtsFalse;
1698               break;
1699
1700             case 'G':
1701               RtsFlags.GranFlags.DoBulkFetching=rtsFalse;
1702               break;
1703               
1704             case 'M':
1705               RtsFlags.GranFlags.DoThreadMigration=rtsFalse;
1706               break;
1707
1708             case 'R':
1709               RtsFlags.GranFlags.DoFairSchedule=rtsFalse;
1710               break;
1711
1712             case 'T':
1713               RtsFlags.GranFlags.DoStealThreadsFirst=rtsFalse;
1714               RtsFlags.GranFlags.DoThreadMigration=rtsFalse;
1715               break;
1716
1717             case 'Z':
1718               RtsFlags.GranFlags.DoAsyncFetch=rtsFalse;
1719               break;
1720               
1721             case 'N':
1722               RtsFlags.GranFlags.PreferSparksOfLocalNodes=rtsFalse;
1723                          break;
1724                          
1725             case 'P':
1726               RtsFlags.GranFlags.GranSimStats.Suppressed=rtsTrue;
1727               break;
1728
1729             case 's':
1730               RtsFlags.GranFlags.GranSimStats.Sparks=rtsFalse;
1731               break;
1732             
1733             case 'h':
1734               RtsFlags.GranFlags.GranSimStats.Heap=rtsFalse;
1735               break;
1736             
1737             case 'b':
1738               RtsFlags.GranFlags.GranSimStats.Binary=rtsFalse;
1739               break;
1740                          
1741             case 'X':
1742               RtsFlags.GranFlags.DoPrioritySparking = rtsFalse;
1743               break;
1744
1745             case 'Y':
1746               RtsFlags.GranFlags.DoPrioritySparking = rtsFalse;
1747               RtsFlags.GranFlags.SparkPriority = rtsFalse;
1748               break;
1749
1750             case 'I':
1751               RtsFlags.GranFlags.DoPriorityScheduling = rtsFalse;
1752               break;
1753
1754             case 'e':
1755               RtsFlags.GranFlags.RandomSteal = rtsFalse;
1756               break;
1757
1758             default:
1759               bad_option( rts_argv[arg] );
1760               break;
1761           }
1762           break;
1763
1764 #  if defined(GRAN_CHECK) && defined(GRAN)
1765         case 'D':
1766           switch(rts_argv[arg][3]) {
1767             case 'Q':    /* Set pack buffer size (same as 'Q' in GUM) */
1768               if (rts_argv[arg][4] != '\0') {
1769                 RtsFlags.GranFlags.packBufferSize = decode(rts_argv[arg]+4);
1770                 debugBelch("Pack buffer size: %d\n",
1771                         RtsFlags.GranFlags.packBufferSize);
1772               } else {
1773                 debugBelch("setupRtsFlags: missing size of PackBuffer (for -Q)\n");
1774                 *error = rtsTrue;
1775               }
1776               break;
1777
1778           default:
1779               if (isdigit(rts_argv[arg][3])) {/* Set all debugging options in one */
1780                 /* hack warning: interpret the flags as a binary number */
1781                 nat n = decode(rts_argv[arg]+3);
1782                 set_GranSim_debug_options(n);
1783               } else {
1784                 nat i;
1785                 for (i=0; i<=MAX_GRAN_DEBUG_OPTION; i++) 
1786                   if (rts_argv[arg][3] == gran_debug_opts_flags[i])
1787                     break;
1788                 
1789                 if (i==MAX_GRAN_DEBUG_OPTION+1) {
1790                   debugBelch("Valid GranSim debug options are:\n");
1791                   help_GranSim_debug_options(MAX_GRAN_DEBUG_MASK);
1792                   bad_option( rts_argv[arg] );
1793                 } else { // flag found; now set it
1794                   set_GranSim_debug_options(GRAN_DEBUG_MASK(i));  // 2^i
1795                 }
1796               }
1797               break;
1798               
1799 #if 0
1800             case 'e':       /* event trace; also -bD1 */
1801               debugBelch("DEBUG: event_trace; printing event trace.\n");
1802               RtsFlags.GranFlags.Debug.event_trace = rtsTrue;
1803               /* RtsFlags.GranFlags.event_trace=rtsTrue; */
1804               break;
1805               
1806             case 'E':       /* event statistics; also -bD2 */
1807               debugBelch("DEBUG: event_stats; printing event statistics.\n");
1808               RtsFlags.GranFlags.Debug.event_stats = rtsTrue;
1809               /* RtsFlags.GranFlags.Debug |= 0x20; print event statistics   */
1810               break;
1811               
1812             case 'f':       /* thunkStealing; also -bD4 */
1813               debugBelch("DEBUG: thunkStealing; printing forwarding of FETCHNODES.\n");
1814               RtsFlags.GranFlags.Debug.thunkStealing = rtsTrue;
1815               /* RtsFlags.GranFlags.Debug |= 0x2;  print fwd messages */
1816               break;
1817
1818             case 'z':       /* blockOnFetch; also -bD8 */
1819               debugBelch("DEBUG: blockOnFetch; check for blocked on fetch.\n");
1820               RtsFlags.GranFlags.Debug.blockOnFetch = rtsTrue;
1821               /* RtsFlags.GranFlags.Debug |= 0x4; debug non-reschedule-on-fetch */
1822               break;
1823               
1824             case 't':       /* blockOnFetch_sanity; also -bD16 */  
1825               debugBelch("DEBUG: blockOnFetch_sanity; check for TSO asleep on fetch.\n");
1826               RtsFlags.GranFlags.Debug.blockOnFetch_sanity = rtsTrue;
1827               /* RtsFlags.GranFlags.Debug |= 0x10; debug TSO asleep for fetch  */
1828               break;
1829
1830             case 'S':       /* priSpark; also -bD32 */
1831               debugBelch("DEBUG: priSpark; priority sparking.\n");
1832               RtsFlags.GranFlags.Debug.priSpark = rtsTrue;
1833               break;
1834
1835             case 's':       /* priSched; also -bD64 */
1836               debugBelch("DEBUG: priSched; priority scheduling.\n");
1837               RtsFlags.GranFlags.Debug.priSched = rtsTrue;
1838               break;
1839
1840             case 'F':       /* findWork; also -bD128 */
1841               debugBelch("DEBUG: findWork; searching spark-pools (local & remote), thread queues for work.\n");
1842               RtsFlags.GranFlags.Debug.findWork = rtsTrue;
1843               break;
1844               
1845             case 'g':       /* globalBlock; also -bD256 */
1846               debugBelch("DEBUG: globalBlock; blocking on remote closures (FETCHMEs etc in GUM).\n");
1847               RtsFlags.GranFlags.Debug.globalBlock = rtsTrue;
1848               break;
1849               
1850             case 'G':       /* pack; also -bD512 */
1851               debugBelch("DEBUG: pack; routines for (un-)packing graph structures.\n");
1852               RtsFlags.GranFlags.Debug.pack = rtsTrue;
1853               break;
1854               
1855             case 'P':       /* packBuffer; also -bD1024 */
1856               debugBelch("DEBUG: packBuffer; routines handling pack buffer (GranSim internal!).\n");
1857               RtsFlags.GranFlags.Debug.packBuffer = rtsTrue;
1858               break;
1859               
1860             case 'o':       /* sortedQ; also -bD2048 */
1861               debugBelch("DEBUG: sortedQ; check whether spark/thread queues are sorted.\n");
1862               RtsFlags.GranFlags.Debug.sortedQ = rtsTrue;
1863               break;
1864               
1865             case 'r':       /* randomSteal; also -bD4096 */
1866               debugBelch("DEBUG: randomSteal; stealing sparks/threads from random PEs.\n");
1867               RtsFlags.GranFlags.Debug.randomSteal = rtsTrue;
1868               break;
1869               
1870             case 'q':       /* checkSparkQ; also -bD8192 */
1871               debugBelch("DEBUG: checkSparkQ; check consistency of the spark queues.\n");
1872               RtsFlags.GranFlags.Debug.checkSparkQ = rtsTrue;
1873               break;
1874               
1875             case ':':       /* checkLight; also -bD16384 */
1876               debugBelch("DEBUG: checkLight; check GranSim-Light setup.\n");
1877               RtsFlags.GranFlags.Debug.checkLight = rtsTrue;
1878               break;
1879               
1880             case 'b':       /* bq; also -bD32768 */
1881               debugBelch("DEBUG: bq; check blocking queues\n");
1882               RtsFlags.GranFlags.Debug.bq = rtsTrue;
1883               break;
1884               
1885             case 'd':       /* all options turned on */
1886               debugBelch("DEBUG: all options turned on.\n");
1887               set_GranSim_debug_options(MAX_GRAN_DEBUG_MASK);
1888               /* RtsFlags.GranFlags.Debug |= 0x40; */
1889               break;
1890
1891 /*          case '\0': */
1892 /*            RtsFlags.GranFlags.Debug = 1; */
1893 /*            break; */
1894 #endif
1895
1896           }
1897           break;
1898 #  endif  /* GRAN_CHECK */
1899       default:
1900         bad_option( rts_argv[arg] );
1901         break;
1902       }
1903 }
1904
1905 /*
1906   Interpret n as a binary number masking GranSim debug options and set the 
1907   correxponding option. See gran_debug_opts_strs for explanations of the flags.
1908 */
1909 static void
1910 set_GranSim_debug_options(nat n) {
1911   nat i;
1912
1913   for (i=0; i<=MAX_GRAN_DEBUG_OPTION; i++) 
1914     if ((n>>i)&1) {
1915       errorBelch(gran_debug_opts_strs[i]);
1916       switch (i) {
1917         case 0: RtsFlags.GranFlags.Debug.event_trace   = rtsTrue;  break;
1918         case 1: RtsFlags.GranFlags.Debug.event_stats   = rtsTrue;  break;
1919         case 2: RtsFlags.GranFlags.Debug.bq            = rtsTrue;  break;
1920         case 3: RtsFlags.GranFlags.Debug.pack          = rtsTrue;  break;
1921         case 4: RtsFlags.GranFlags.Debug.checkSparkQ   = rtsTrue;  break;
1922         case 5: RtsFlags.GranFlags.Debug.thunkStealing = rtsTrue;  break;
1923         case 6: RtsFlags.GranFlags.Debug.randomSteal   = rtsTrue;  break;
1924         case 7: RtsFlags.GranFlags.Debug.findWork      = rtsTrue;  break;
1925         case 8: RtsFlags.GranFlags.Debug.unused        = rtsTrue;  break;
1926         case 9: RtsFlags.GranFlags.Debug.pri           = rtsTrue;  break;
1927         case 10: RtsFlags.GranFlags.Debug.checkLight   = rtsTrue;  break;
1928         case 11: RtsFlags.GranFlags.Debug.sortedQ      = rtsTrue;  break;
1929         case 12: RtsFlags.GranFlags.Debug.blockOnFetch = rtsTrue;  break;
1930         case 13: RtsFlags.GranFlags.Debug.packBuffer   = rtsTrue;  break;
1931         case 14: RtsFlags.GranFlags.Debug.blockOnFetch_sanity = rtsTrue;  break;
1932         default: barf("set_GranSim_debug_options: only %d debug options expected");
1933       } /* switch */
1934     } /* if */
1935 }
1936
1937 /*
1938   Print one line explanation for each of the GranSim debug options specified
1939   in the bitmask n.
1940 */
1941 static void
1942 help_GranSim_debug_options(nat n) {
1943   nat i;
1944
1945   for (i=0; i<=MAX_GRAN_DEBUG_OPTION; i++) 
1946     if ((n>>i)&1) 
1947       debugBelch(gran_debug_opts_strs[i]);
1948 }
1949
1950 # elif defined(PAR)
1951
1952 static void
1953 process_par_option(int arg, int *rts_argc, char *rts_argv[], rtsBool *error)
1954 {
1955
1956   if (rts_argv[arg][1] != 'q') { /* All GUM options start with -q */
1957     errorBelch("Warning: GUM option does not start with -q: %s", rts_argv[arg]);
1958     return;
1959   }
1960
1961   /* Communication and task creation cost parameters */
1962   switch(rts_argv[arg][2]) {
1963   case 'e':  /* -qe<n>  ... allow <n> local sparks */
1964     if (rts_argv[arg][3] != '\0') { /* otherwise, stick w/ the default */
1965       RtsFlags.ParFlags.maxLocalSparks
1966         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1967       
1968       if (RtsFlags.ParFlags.maxLocalSparks <= 0) {
1969         errorBelch("setupRtsFlags: bad value for -e\n");
1970         *error = rtsTrue;
1971       }
1972     }
1973     IF_PAR_DEBUG(verbose,
1974                  errorBelch("-qe<n>: max %d local sparks", 
1975                        RtsFlags.ParFlags.maxLocalSparks));
1976     break;
1977   
1978   case 't':
1979     if (rts_argv[arg][3] != '\0') {
1980       RtsFlags.ParFlags.maxThreads
1981         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
1982     } else {
1983       errorBelch("missing size for -qt\n");
1984       *error = rtsTrue;
1985     }
1986     IF_PAR_DEBUG(verbose,
1987                  errorBelch("-qt<n>: max %d threads", 
1988                        RtsFlags.ParFlags.maxThreads));
1989     break;
1990
1991   case 'f':
1992     if (rts_argv[arg][3] != '\0')
1993       RtsFlags.ParFlags.maxFishes = decode(rts_argv[arg]+3);
1994     else
1995       RtsFlags.ParFlags.maxFishes = MAX_FISHES;
1996     break;
1997     IF_PAR_DEBUG(verbose,
1998                  errorBelch("-qf<n>: max %d fishes sent out at one time", 
1999                        RtsFlags.ParFlags.maxFishes));
2000     break;
2001   
2002   case 'F':
2003     if (rts_argv[arg][3] != '\0') {
2004       RtsFlags.ParFlags.fishDelay
2005         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
2006     } else {
2007       errorBelch("missing fish delay time for -qF\n");
2008       *error = rtsTrue;
2009     }
2010     IF_PAR_DEBUG(verbose,
2011                  errorBelch("-qF<n>: fish delay time %d us", 
2012                        RtsFlags.ParFlags.fishDelay));
2013     break;
2014
2015   case 'O':
2016     RtsFlags.ParFlags.outputDisabled = rtsTrue;
2017     IF_PAR_DEBUG(verbose,
2018                  errorBelch("-qO: output disabled"));
2019     break;
2020   
2021   case 'g': /* -qg<n> ... globalisation scheme */
2022     if (rts_argv[arg][3] != '\0') {
2023       RtsFlags.ParFlags.globalising = decode(rts_argv[arg]+3);
2024     } else {
2025       errorBelch("missing identifier for globalisation scheme (for -qg)\n");
2026       *error = rtsTrue;
2027     }
2028     IF_PAR_DEBUG(verbose,
2029                  debugBelch("-qg<n>: globalisation scheme set to  %d", 
2030                        RtsFlags.ParFlags.globalising));
2031     break;
2032
2033   case 'h': /* -qh<n> ... max number of thunks (except root) in packet */
2034     if (rts_argv[arg][3] != '\0') {
2035       RtsFlags.ParFlags.thunksToPack = decode(rts_argv[arg]+3);
2036     } else {
2037       errorBelch("missing number of thunks per packet (for -qh)\n");
2038       *error = rtsTrue;
2039     }
2040     IF_PAR_DEBUG(verbose,
2041                  debugBelch("-qh<n>: thunks per packet set to %d", 
2042                        RtsFlags.ParFlags.thunksToPack));
2043     break;
2044
2045   case 'P': /* -qP for writing a log file */
2046     //RtsFlags.ParFlags.ParStats.Full = rtsFalse;
2047     /* same encoding as in GranSim after -bP */ 
2048     switch(rts_argv[arg][3]) {
2049     case '\0': RtsFlags.ParFlags.ParStats.Full = rtsTrue;
2050       break; // nothing special, just an ordinary profile
2051     case '0': RtsFlags.ParFlags.ParStats.Suppressed = rtsTrue;
2052         RtsFlags.ParFlags.ParStats.Full = rtsFalse;
2053       break;
2054     case 'b': RtsFlags.ParFlags.ParStats.Binary = rtsTrue;
2055       break;
2056     case 's': RtsFlags.ParFlags.ParStats.Sparks = rtsTrue;
2057       break;
2058       //case 'h': RtsFlags.parFlags.ParStats.Heap = rtsTrue;
2059       //  break;
2060     case 'n': RtsFlags.ParFlags.ParStats.NewLogfile = rtsTrue;
2061       break;
2062     case 'g': 
2063 # if defined(PAR_TICKY)
2064       RtsFlags.ParFlags.ParStats.Global = rtsTrue;
2065 # else 
2066       errorBelch("-qPg is only possible for a PAR_TICKY RTS, which this is not");
2067       stg_exit(EXIT_FAILURE);
2068 # endif
2069       break;
2070     default: barf("Unknown option -qP%c", rts_argv[arg][2]);
2071     }
2072     IF_PAR_DEBUG(verbose,
2073                  debugBelch("(-qP) writing to log-file (RtsFlags.ParFlags.ParStats.Full=%s)",
2074                        (RtsFlags.ParFlags.ParStats.Full ? "rtsTrue" : "rtsFalse")));
2075     break;
2076   
2077   case 'Q': /* -qQ<n> ... set pack buffer size to <n> */
2078     if (rts_argv[arg][3] != '\0') {
2079       RtsFlags.ParFlags.packBufferSize = decode(rts_argv[arg]+3);
2080     } else {
2081       errorBelch("missing size of PackBuffer (for -qQ)\n");
2082       *error = rtsTrue;
2083     }
2084     IF_PAR_DEBUG(verbose,
2085                  debugBelch("-qQ<n>: pack buffer size set to %d", 
2086                        RtsFlags.ParFlags.packBufferSize));
2087     break;
2088
2089   case 'R':
2090     RtsFlags.ParFlags.doFairScheduling = rtsTrue;
2091     IF_PAR_DEBUG(verbose,
2092                  debugBelch("-qR: fair-ish scheduling"));
2093     break;
2094   
2095 # if defined(DEBUG)  
2096   case 'w':
2097     if (rts_argv[arg][3] != '\0') {
2098       RtsFlags.ParFlags.wait
2099         = strtol(rts_argv[arg]+3, (char **) NULL, 10);
2100     } else {
2101       RtsFlags.ParFlags.wait = 1000;
2102     }
2103     IF_PAR_DEBUG(verbose,
2104                  debugBelch("-qw<n>: length of wait loop after synchr before reduction: %d", 
2105                        RtsFlags.ParFlags.wait));
2106     break;
2107
2108   case 'D':  /* -qD ... all the debugging options */
2109     if (isdigit(rts_argv[arg][3])) {/* Set all debugging options in one */
2110       /* hack warning: interpret the flags as a binary number */
2111       nat n = decode(rts_argv[arg]+3);
2112       set_par_debug_options(n);
2113     } else {
2114       nat i;
2115       for (i=0; i<=MAX_PAR_DEBUG_OPTION; i++) 
2116         if (rts_argv[arg][3] == par_debug_opts_flags[i])
2117           break;
2118         
2119       if (i==MAX_PAR_DEBUG_OPTION+1) {
2120         errorBelch("Valid GUM debug options are:\n");
2121         help_par_debug_options(MAX_PAR_DEBUG_MASK);
2122         bad_option( rts_argv[arg] );
2123       } else { // flag found; now set it
2124         set_par_debug_options(PAR_DEBUG_MASK(i));  // 2^i
2125       }
2126     }
2127     break;
2128 # endif
2129   default:
2130     errorBelch("Unknown option -q%c (%d opts in total)", 
2131           rts_argv[arg][2], *rts_argc);
2132     break;
2133   } /* switch */
2134 }
2135
2136 /*
2137   Interpret n as a binary number masking Par debug options and set the 
2138   correxponding option. See par_debug_opts_strs for explanations of the flags.
2139 */
2140 static void
2141 set_par_debug_options(nat n) {
2142   nat i;
2143
2144   for (i=0; i<=MAX_PAR_DEBUG_OPTION; i++) 
2145     if ((n>>i)&1) {
2146       debugBelch(par_debug_opts_strs[i]);
2147       switch (i) {
2148         case 0: RtsFlags.ParFlags.Debug.verbose       = rtsTrue;  break;
2149         case 1: RtsFlags.ParFlags.Debug.bq            = rtsTrue;  break;
2150         case 2: RtsFlags.ParFlags.Debug.schedule      = rtsTrue;  break;
2151         case 3: RtsFlags.ParFlags.Debug.free          = rtsTrue;  break;
2152         case 4: RtsFlags.ParFlags.Debug.resume        = rtsTrue;  break;
2153         case 5: RtsFlags.ParFlags.Debug.weight        = rtsTrue;  break;
2154         case 6: RtsFlags.ParFlags.Debug.fetch         = rtsTrue;  break;
2155           //case 7: RtsFlags.ParFlags.Debug.ack           = rtsTrue;  break;
2156         case 7: RtsFlags.ParFlags.Debug.fish          = rtsTrue;  break;
2157         case 8: RtsFlags.ParFlags.Debug.tables        = rtsTrue;  break;
2158         case 9: RtsFlags.ParFlags.Debug.packet        = rtsTrue;  break;
2159         case 10: RtsFlags.ParFlags.Debug.pack         = rtsTrue;  break;
2160         case 11: RtsFlags.ParFlags.Debug.paranoia     = rtsTrue;  break;
2161         default: barf("set_par_debug_options: only %d debug options expected",
2162                       MAX_PAR_DEBUG_OPTION);
2163       } /* switch */
2164     } /* if */
2165 }
2166
2167 /*
2168   Print one line explanation for each of the GranSim debug options specified
2169   in the bitmask n.
2170 */
2171 static void
2172 help_par_debug_options(nat n) {
2173   nat i;
2174
2175   for (i=0; i<=MAX_PAR_DEBUG_OPTION; i++) 
2176     if ((n>>i)&1) 
2177       debugBelch(par_debug_opts_strs[i]);
2178 }
2179
2180 #endif /* PAR */
2181
2182 static void
2183 stats_fprintf(FILE *f, char *s, ...)
2184 {
2185     va_list ap;
2186     va_start(ap,s);
2187     if (f == NULL) {
2188         vdebugBelch(s, ap);
2189     } else {
2190         vfprintf(f, s, ap);
2191     }
2192     va_end(ap);
2193 }
2194
2195 static int              /* return -1 on error */
2196 open_stats_file (
2197     I_ arg,
2198     int argc, char *argv[],
2199     int rts_argc, char *rts_argv[],
2200     const char *FILENAME_FMT,
2201     FILE **file_ret)
2202 {
2203     FILE *f = NULL;
2204
2205     if (strequal(rts_argv[arg]+2, "stderr")) { /* use debugBelch */
2206         f = NULL; /* NULL means use debugBelch */
2207     } else {
2208         if (rts_argv[arg][2] != '\0') {  /* stats file specified */
2209             f = fopen(rts_argv[arg]+2,"w");
2210         } else {
2211             char stats_filename[STATS_FILENAME_MAXLEN]; /* default <program>.<ext> */
2212             sprintf(stats_filename, FILENAME_FMT, argv[0]);
2213             f = fopen(stats_filename,"w");
2214         }
2215         if (f == NULL) {
2216             errorBelch("Can't open stats file %s\n", rts_argv[arg]+2);
2217             return -1;
2218         }
2219     }
2220     *file_ret = f;
2221
2222     {
2223         /* Write argv and rtsv into start of stats file */
2224         int count;
2225         for(count = 0; count < argc; count++) {
2226             stats_fprintf(f, "%s ", argv[count]);
2227         }
2228         stats_fprintf(f, "+RTS ");
2229         for(count = 0; count < rts_argc; count++)
2230             stats_fprintf(f, "%s ", rts_argv[count]);
2231         stats_fprintf(f, "\n");
2232     }
2233     return 0;
2234 }
2235
2236
2237
2238 static I_
2239 decode(const char *s)
2240 {
2241     I_ c;
2242     StgDouble m;
2243
2244     if (!*s)
2245         return 0;
2246
2247     m = atof(s);
2248     c = s[strlen(s)-1];
2249
2250     if (c == 'g' || c == 'G')
2251         m *= 1000*1000*1000;    /* UNchecked! */
2252     else if (c == 'm' || c == 'M')
2253         m *= 1000*1000;                 /* We do not use powers of 2 (1024) */
2254     else if (c == 'k' || c == 'K')      /* to avoid possible bad effects on */
2255         m *= 1000;                      /* a direct-mapped cache.           */ 
2256     else if (c == 'w' || c == 'W')
2257         m *= sizeof(W_);
2258
2259     return (I_)m;
2260 }
2261
2262 static void
2263 bad_option(const char *s)
2264 {
2265   errorBelch("bad RTS option: %s", s);
2266   stg_exit(EXIT_FAILURE);
2267 }
2268
2269 /* -----------------------------------------------------------------------------
2270    Getting/Setting the program's arguments.
2271
2272    These are used by System.Environment, and parts of the RTS.
2273    -------------------------------------------------------------------------- */
2274
2275 void
2276 setProgName(char *argv[])
2277 {
2278     /* Remove directory from argv[0] -- default files in current directory */
2279 #if !defined(mingw32_HOST_OS)
2280     char *last_slash;
2281     if ( (last_slash = (char *) strrchr(argv[0], '/')) != NULL ) {
2282         prog_name = last_slash+1;
2283    } else {
2284         prog_name = argv[0];
2285    }
2286 #else
2287     char* last_slash = argv[0] + (strlen(argv[0]) - 1);
2288     while ( last_slash > argv[0] ) {
2289         if ( *last_slash == '/' || *last_slash == '\\' ) {
2290             prog_name = last_slash+1;
2291             return;
2292         }
2293         last_slash--;
2294     }
2295     prog_name = argv[0];
2296 #endif
2297 }
2298
2299 void
2300 getProgArgv(int *argc, char **argv[])
2301 {
2302     if (argc) { *argc = prog_argc; }
2303     if (argv) { *argv = prog_argv; }
2304 }
2305
2306 void
2307 setProgArgv(int argc, char *argv[])
2308 {
2309    /* Usually this is done by startupHaskell, so we don't need to call this. 
2310       However, sometimes Hugs wants to change the arguments which Haskell
2311       getArgs >>= ... will be fed.  So you can do that by calling here
2312       _after_ calling startupHaskell.
2313    */
2314    prog_argc = argc;
2315    prog_argv = argv;
2316    setProgName(prog_argv);
2317 }