1 /* Simulator option handling.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "libiberty.h"
30 #include "../libiberty/alloca-conf.h"
31 #include "sim-options.h"
34 /* This is defined in sim-config.h. */
35 #ifndef MAX_NR_PROCESSORS
36 #define MAX_NR_PROCESSORS 1
39 /* Add a set of options to the simulator.
40 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
41 This is intended to be called by modules in their `install' handler. */
44 sim_add_option_table (sd, table)
48 struct option_list *ol = ((struct option_list *)
49 xmalloc (sizeof (struct option_list)));
51 /* Note: The list is constructed in the reverse order we're called so
52 later calls will override earlier ones (in case that ever happens).
53 This is the intended behaviour. */
54 ol->next = STATE_OPTIONS (sd);
56 STATE_OPTIONS (sd) = ol;
61 /* Standard option table.
62 Modules may specify additional ones.
63 The caller of sim_parse_args may also specify additional options
64 by calling sim_add_option_table first. */
66 static DECLARE_OPTION_HANDLER (standard_option_handler);
68 /* FIXME: We shouldn't print in --help output options that aren't usable.
69 Some fine tuning will be necessary. One can either move less general
70 options to another table or use a HAVE_FOO macro to ifdef out unavailable
73 /* ??? One might want to conditionally compile out the entries that
74 aren't enabled. There's a distinction, however, between options a
75 simulator can't support and options that haven't been configured in.
76 Certainly options a simulator can't support shouldn't appear in the
77 output of --help. Whether the same thing applies to options that haven't
78 been configured in or not isn't something I can get worked up over.
79 [Note that conditionally compiling them out might simply involve moving
80 the option to another table.]
81 If you decide to conditionally compile them out as well, delete this
82 comment and add a comment saying that that is the rule. */
84 #define OPTION_DEBUG_INSN (OPTION_START + 0)
85 #define OPTION_DEBUG_FILE (OPTION_START + 1)
86 #define OPTION_TRACE_INSN (OPTION_START + 2)
87 #define OPTION_TRACE_DECODE (OPTION_START + 3)
88 #define OPTION_TRACE_EXTRACT (OPTION_START + 4)
89 #define OPTION_TRACE_LINENUM (OPTION_START + 5)
90 #define OPTION_TRACE_MEMORY (OPTION_START + 6)
91 #define OPTION_TRACE_MODEL (OPTION_START + 7)
92 #define OPTION_TRACE_ALU (OPTION_START + 8)
93 #define OPTION_TRACE_FILE (OPTION_START + 9)
94 #define OPTION_PROFILE_INSN (OPTION_START + 10)
95 #define OPTION_PROFILE_MEMORY (OPTION_START + 11)
96 #define OPTION_PROFILE_MODEL (OPTION_START + 12)
97 #define OPTION_PROFILE_SIMCACHE (OPTION_START + 13)
98 #define OPTION_PROFILE_FILE (OPTION_START + 14)
100 static const OPTION standard_options[] =
102 { {"verbose", no_argument, NULL, 'v'},
103 'v', NULL, "Verbose output",
104 standard_option_handler },
106 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
107 { {"endian", required_argument, NULL, 'E'},
108 'E', "big|little", "Set endianness",
109 standard_option_handler },
112 { {"debug", no_argument, NULL, 'D'},
113 'D', NULL, "Print debugging messages",
114 standard_option_handler },
115 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
116 '\0', NULL, "Print instruction debugging messages",
117 standard_option_handler },
118 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
119 '\0', "FILE NAME", "Specify debugging output file",
120 standard_option_handler },
122 { {"trace", no_argument, NULL, 't'},
123 't', NULL, "Perform tracing",
124 standard_option_handler },
125 { {"trace-insn", no_argument, NULL, OPTION_TRACE_INSN},
126 '\0', NULL, "Perform instruction tracing",
127 standard_option_handler },
128 { {"trace-decode", no_argument, NULL, OPTION_TRACE_DECODE},
129 '\0', NULL, "Perform instruction decoding tracing",
130 standard_option_handler },
131 { {"trace-extract", no_argument, NULL, OPTION_TRACE_EXTRACT},
132 '\0', NULL, "Perform instruction extraction tracing",
133 standard_option_handler },
134 { {"trace-linenum", no_argument, NULL, OPTION_TRACE_LINENUM},
135 '\0', NULL, "Perform line number tracing",
136 standard_option_handler },
137 { {"trace-memory", no_argument, NULL, OPTION_TRACE_MEMORY},
138 '\0', NULL, "Perform memory tracing",
139 standard_option_handler },
140 { {"trace-model", no_argument, NULL, OPTION_TRACE_MODEL},
141 '\0', NULL, "Perform model tracing",
142 standard_option_handler },
143 { {"trace-alu", no_argument, NULL, OPTION_TRACE_ALU},
144 '\0', NULL, "Perform ALU tracing",
145 standard_option_handler },
146 { {"trace-file", required_argument, NULL, OPTION_TRACE_FILE},
147 '\0', "FILE NAME", "Specify tracing output file",
148 standard_option_handler },
150 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
151 { {"h8300h", no_argument, NULL, 'h'},
152 'h', NULL, "Indicate the CPU is h8/300h or h8/300s",
153 standard_option_handler },
156 #ifdef SIM_HAVE_SIMCACHE
157 { {"simcache-size", required_argument, NULL, 'c'},
158 'c', "SIM CACHE SIZE", "Specify size of simulator execution cache",
159 standard_option_handler },
162 #ifdef SIM_HAVE_FLATMEM
163 { {"mem-size", required_argument, NULL, 'm'},
164 'm', "MEMORY SIZE", "Specify memory size",
165 standard_option_handler },
168 #ifdef SIM_HAVE_MAX_INSNS
169 { {"max-insns", required_argument, NULL, 'M'},
170 'M', "MAX INSNS", "Specify maximum number of instructions to execute",
171 standard_option_handler },
174 #ifdef SIM_HAVE_PROFILE
175 { {"profile", no_argument, NULL, 'p'},
176 'p', NULL, "Perform profiling",
177 standard_option_handler },
178 { {"profile-insn", no_argument, NULL, OPTION_PROFILE_INSN},
179 '\0', NULL, "Perform instruction profiling",
180 standard_option_handler },
181 { {"profile-memory", no_argument, NULL, OPTION_PROFILE_MEMORY},
182 '\0', NULL, "Perform memory profiling",
183 standard_option_handler },
184 { {"profile-model", no_argument, NULL, OPTION_PROFILE_MODEL},
185 '\0', NULL, "Perform model profiling",
186 standard_option_handler },
187 { {"profile-simcache", no_argument, NULL, OPTION_PROFILE_SIMCACHE},
188 '\0', NULL, "Perform simulator execution cache profiling",
189 standard_option_handler },
190 { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
191 '\0', "FILE NAME", "Specify profile output file",
192 standard_option_handler },
193 #ifdef SIM_HAVE_PROFILE_PC
194 { {"profile-pc-frequency", required_argument, NULL, 'F'},
195 'F', "PC PROFILE FREQUENCY", "Turn on PC profiling at specified frequency",
196 standard_option_handler },
197 { {"profile-pc-size", required_argument, NULL, 'S'},
198 'S', "PC PROFILE SIZE", "Specify PC profiling size",
199 standard_option_handler },
201 #endif /* SIM_HAVE_PROFILE */
203 { {"help", no_argument, NULL, 'H'},
204 'H', NULL, "Print help information",
205 standard_option_handler },
207 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
211 standard_option_handler (sd, opt, arg)
222 STATE_VERBOSE_P (sd) = 1;
225 #ifdef SIM_HAVE_BIENDIAN
227 if (strcmp (arg, "big") == 0)
229 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
231 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
234 /* FIXME:wip: Need to set something in STATE_CONFIG. */
236 else if (strcmp (arg, "little") == 0)
238 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
240 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
243 /* FIXME:wip: Need to set something in STATE_CONFIG. */
247 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
255 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
258 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
259 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
260 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
264 case OPTION_DEBUG_INSN :
266 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
269 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
270 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
274 case OPTION_DEBUG_FILE :
276 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
279 FILE *f = fopen (arg, "w");
283 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
286 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
287 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
293 sim_io_eprintf (sd, "Tracing not compiled in, `-t' ignored\n");
296 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
297 for (i = 0; i < MAX_TRACE_VALUES; ++i)
298 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[i] = 1;
302 case OPTION_TRACE_INSN :
303 if (! (WITH_TRACE & (1 << TRACE_INSN_IDX)))
304 sim_io_eprintf (sd, "Instruction tracing not compiled in, `--trace-insn' ignored\n");
307 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
308 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_INSN_IDX] = 1;
312 case OPTION_TRACE_DECODE :
313 if (! (WITH_TRACE & (1 << TRACE_DECODE_IDX)))
314 sim_io_eprintf (sd, "Decode tracing not compiled in, `--trace-decode' ignored\n");
317 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
318 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_DECODE_IDX] = 1;
322 case OPTION_TRACE_EXTRACT :
323 if (! (WITH_TRACE & (1 << TRACE_EXTRACT_IDX)))
324 sim_io_eprintf (sd, "Extract tracing not compiled in, `--trace-extract' ignored\n");
327 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
328 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_EXTRACT_IDX] = 1;
332 case OPTION_TRACE_LINENUM :
333 if (! (WITH_TRACE & (1 << TRACE_LINENUM_IDX)))
334 sim_io_eprintf (sd, "Line number tracing not compiled in, `--trace-linenum' ignored\n");
337 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
338 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_LINENUM_IDX] = 1;
342 case OPTION_TRACE_MEMORY :
343 if (! (WITH_TRACE & (1 << TRACE_MEMORY_IDX)))
344 sim_io_eprintf (sd, "Memory tracing not compiled in, `--trace-memory' ignored\n");
347 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
348 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_MEMORY_IDX] = 1;
352 case OPTION_TRACE_MODEL :
353 if (! (WITH_TRACE & (1 << TRACE_MODEL_IDX)))
354 sim_io_eprintf (sd, "Model tracing not compiled in, `--trace-model' ignored\n");
357 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
358 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_MODEL_IDX] = 1;
362 case OPTION_TRACE_ALU :
363 if (! (WITH_TRACE & (1 << TRACE_ALU_IDX)))
364 sim_io_eprintf (sd, "ALU tracing not compiled in, `--trace-alu' ignored\n");
367 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
368 CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_ALU_IDX] = 1;
372 case OPTION_TRACE_FILE :
374 sim_io_eprintf (sd, "Tracing not compiled in, `--trace-file' ignored\n");
377 FILE *f = fopen (arg, "w");
381 sim_io_eprintf (sd, "Unable to open trace output file `%s'\n", arg);
384 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
385 CPU_TRACE_FILE (STATE_CPU (sd, n)) = f;
389 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
395 #ifdef SIM_HAVE_SIMCACHE
397 n = strtol (arg, NULL, 0);
400 sim_io_eprintf (sd, "Invalid simulator cache size: %d", n);
403 STATE_SIMCACHE_SIZE (sd) = n;
407 #ifdef SIM_HAVE_FLATMEM
409 ul = strtol (arg, NULL, 0);
410 /* 16384: some minimal amount */
411 if (! isdigit (arg[0]) || ul < 16384)
413 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
416 STATE_MEM_SIZE (sd) = ul;
420 #ifdef SIM_HAVE_MAX_INSNS
422 ul = strtoul (arg, NULL, 0);
423 if (! isdigit (arg[0]))
425 sim_io_eprintf (sd, "Invalid maximum instruction count: `%s'", arg);
428 STATE_MAX_INSNS (sd) = ul;
432 #ifdef SIM_HAVE_PROFILE
435 sim_io_eprintf (sd, "Profile not compiled in, -p option ignored\n");
438 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
439 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
440 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[i] = 1;
444 case OPTION_PROFILE_INSN :
445 if (! (WITH_PROFILE & (1 << PROFILE_INSN_IDX)))
446 sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
449 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
450 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_INSN_IDX] = 1;
454 case OPTION_PROFILE_MEMORY :
455 if (! (WITH_PROFILE & (1 << PROFILE_MEMORY_IDX)))
456 sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n");
459 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
460 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_MEMORY_IDX] = 1;
464 case OPTION_PROFILE_MODEL :
465 if (! (WITH_PROFILE & (1 << PROFILE_MODEL_IDX)))
466 sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n");
469 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
470 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_MODEL_IDX] = 1;
474 case OPTION_PROFILE_SIMCACHE :
475 if (! (WITH_PROFILE & (1 << PROFILE_SIMCACHE_IDX)))
476 sim_io_eprintf (sd, "Simulator cache profiling not compiled in, `--profile-simcache' ignored\n");
479 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
480 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_SIMCACHE_IDX] = 1;
484 case OPTION_PROFILE_FILE :
485 /* FIXME: Might want this to apply to pc profiling only,
486 or have two profile file options. */
488 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n");
491 FILE *f = fopen (arg, "w");
495 sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
498 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
499 CPU_PROFILE_FILE (STATE_CPU (sd, n)) = f;
503 #ifdef SIM_HAVE_PROFILE_PC
505 STATE_PROFILE_PC_FREQUENCY (sd) = atoi (arg);
506 /* FIXME: Validate arg. */
509 STATE_PROFILE_PC_SIZE (sd) = atoi (arg);
510 /* FIXME: Validate arg. */
513 #endif /* SIM_HAVE_PROFILE */
517 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
519 /* FIXME: 'twould be nice to do something similar if gdb. */
526 /* Initialize common parts before argument processing.
527 Called by sim_open. */
530 sim_pre_argv_init (sd, myname)
534 STATE_MY_NAME (sd) = myname + strlen (myname);
535 while (STATE_MY_NAME (sd) > myname && STATE_MY_NAME (sd)[-1] != '/')
536 --STATE_MY_NAME (sd);
538 if (sim_add_option_table (sd, standard_options) != SIM_RC_OK)
544 /* Return non-zero if arg is a duplicate argument.
545 If ARG is NULL, initialize. */
547 #define ARG_HASH_SIZE 97
548 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
555 static char **arg_table = NULL;
559 if (arg_table == NULL)
560 arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
561 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
565 hash = ARG_HASH (arg);
566 while (arg_table[hash] != NULL)
568 if (strcmp (arg, arg_table[hash]) == 0)
570 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
571 don't check if the table is full. */
572 if (++hash == ARG_HASH_SIZE)
575 arg_table[hash] = arg;
579 /* Called by sim_open to parse the arguments. */
582 sim_parse_args (sd, argv)
586 int i, argc, num_opts;
587 char *p, *short_options;
588 /* The `val' option struct entry is dynamically assigned for options that
589 only come in the long form. ORIG_VAL is used to get the original value
591 unsigned char *orig_val;
592 struct option *lp, *long_options;
593 const struct option_list *ol;
595 OPTION_HANDLER **handlers;
597 /* Count the number of arguments. */
598 for (argc = 0; argv[argc] != NULL; ++argc)
601 /* Count the number of options. */
603 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
604 for (opt = ol->options; opt->opt.name != NULL; ++opt)
607 /* Initialize duplicate argument checker. */
608 (void) dup_arg_p (NULL);
610 /* Build the option table for getopt. */
611 long_options = (struct option *) alloca ((num_opts + 1) * sizeof (struct option));
613 short_options = (char *) alloca (num_opts * 3 + 1);
615 #if 0 /* ??? necessary anymore? */
616 /* Set '+' as first char so argument permutation isn't done. This is done
617 to workaround a problem with invoking getopt_long in run.c.: optind gets
618 decremented when the program name is reached. */
621 handlers = (OPTION_HANDLER **) alloca (256 * sizeof (OPTION_HANDLER *));
622 memset (handlers, 0, 256 * sizeof (OPTION_HANDLER *));
623 orig_val = (unsigned char *) alloca (256);
624 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
625 for (opt = ol->options; opt->opt.name != NULL; ++opt)
627 if (dup_arg_p (opt->opt.name))
629 if (opt->shortopt != 0)
631 *p++ = opt->shortopt;
632 if (opt->opt.has_arg == required_argument)
634 else if (opt->opt.has_arg == optional_argument)
635 { *p++ = ':'; *p++ = ':'; }
638 /* Dynamically assign `val' numbers for long options that don't have
639 a short option equivalent. */
640 if (OPTION_LONG_ONLY_P (opt->opt.val))
642 handlers[(unsigned char) lp->val] = opt->handler;
643 orig_val[(unsigned char) lp->val] = opt->opt.val;
649 /* Ensure getopt is initialized. */
655 optc = getopt_long (argc, argv, short_options, long_options, &longind);
658 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
659 STATE_PROG_ARGV (sd) = argv + optind;
665 if ((*handlers[optc]) (sd, orig_val[optc], optarg) == SIM_RC_FAIL)
672 /* Print help messages for the options. */
678 const struct option_list *ol;
681 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
682 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
685 /* Initialize duplicate argument checker. */
686 (void) dup_arg_p (NULL);
688 sim_io_printf (sd, "Options:\n");
689 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
690 for (opt = ol->options; opt->opt.name != NULL; ++opt)
695 if (dup_arg_p (opt->opt.name))
698 if (opt->doc == NULL)
701 sim_io_printf (sd, " ");
709 if (o->shortopt != '\0')
711 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
712 len += (comma ? 2 : 0) + 2;
715 if (o->opt.has_arg != optional_argument)
717 sim_io_printf (sd, " ");
720 sim_io_printf (sd, "%s", o->arg);
721 len += strlen (o->arg);
727 while (o->opt.name != NULL && o->doc == NULL);
732 if (o->opt.name != NULL)
734 sim_io_printf (sd, "%s--%s",
737 len += ((comma ? 2 : 0)
739 + strlen (o->opt.name));
742 sim_io_printf (sd, " %s", o->arg);
743 len += 1 + strlen (o->arg);
749 while (o->opt.name != NULL && o->doc == NULL);
753 sim_io_printf (sd, "\n");
757 for (; len < 30; len++)
758 sim_io_printf (sd, " ");
760 sim_io_printf (sd, "%s\n", opt->doc);
763 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
765 sim_io_printf (sd, "\n");
766 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
767 sim_io_printf (sd, " Note: Very few simulators support this.\n");