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. */
33 #include "libiberty.h"
34 #include "../libiberty/alloca-conf.h"
35 #include "sim-options.h"
37 #include "sim-assert.h"
41 /* Add a set of options to the simulator.
42 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
43 This is intended to be called by modules in their `install' handler. */
46 sim_add_option_table (sd, table)
50 struct option_list *ol = ((struct option_list *)
51 xmalloc (sizeof (struct option_list)));
53 /* Note: The list is constructed in the reverse order we're called so
54 later calls will override earlier ones (in case that ever happens).
55 This is the intended behaviour. */
56 ol->next = STATE_OPTIONS (sd);
58 STATE_OPTIONS (sd) = ol;
63 /* Standard option table.
64 Modules may specify additional ones.
65 The caller of sim_parse_args may also specify additional options
66 by calling sim_add_option_table first. */
68 static DECLARE_OPTION_HANDLER (standard_option_handler);
70 /* FIXME: We shouldn't print in --help output options that aren't usable.
71 Some fine tuning will be necessary. One can either move less general
72 options to another table or use a HAVE_FOO macro to ifdef out unavailable
75 /* ??? One might want to conditionally compile out the entries that
76 aren't enabled. There's a distinction, however, between options a
77 simulator can't support and options that haven't been configured in.
78 Certainly options a simulator can't support shouldn't appear in the
79 output of --help. Whether the same thing applies to options that haven't
80 been configured in or not isn't something I can get worked up over.
81 [Note that conditionally compiling them out might simply involve moving
82 the option to another table.]
83 If you decide to conditionally compile them out as well, delete this
84 comment and add a comment saying that that is the rule. */
86 #define OPTION_DEBUG_INSN (OPTION_START + 0)
87 #define OPTION_DEBUG_FILE (OPTION_START + 1)
88 #define OPTION_DO_COMMAND (OPTION_START + 2)
89 #define OPTION_ARCHITECTURE (OPTION_START + 3)
90 #define OPTION_TARGET (OPTION_START + 4)
91 #define OPTION_ARCHITECTURE_INFO (OPTION_START + 5)
92 #define OPTION_ALIGNMENT (OPTION_START + 6)
94 static const OPTION standard_options[] =
96 { {"verbose", no_argument, NULL, 'v'},
97 'v', NULL, "Verbose output",
98 standard_option_handler },
100 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
101 { {"endian", required_argument, NULL, 'E'},
102 'E', "big|little", "Set endianness",
103 standard_option_handler },
106 { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
107 '\0', "strict|nonstrict|forced", "Set memory access alignment",
108 standard_option_handler },
110 { {"debug", no_argument, NULL, 'D'},
111 'D', NULL, "Print debugging messages",
112 standard_option_handler },
113 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
114 '\0', NULL, "Print instruction debugging messages",
115 standard_option_handler },
116 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
117 '\0', "FILE NAME", "Specify debugging output file",
118 standard_option_handler },
120 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
121 { {"h8300h", no_argument, NULL, 'h'},
122 'h', NULL, "Indicate the CPU is h8/300h or h8/300s",
123 standard_option_handler },
126 #ifdef SIM_HAVE_FLATMEM
127 { {"mem-size", required_argument, NULL, 'm'},
128 'm', "MEMORY SIZE", "Specify memory size",
129 standard_option_handler },
132 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
133 '\0', "COMMAND", ""/*undocumented*/,
134 standard_option_handler },
136 { {"help", no_argument, NULL, 'H'},
137 'H', NULL, "Print help information",
138 standard_option_handler },
140 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
141 '\0', "MACHINE", "Specify the architecture to use",
142 standard_option_handler },
143 { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
144 '\0', NULL, "List supported architectures",
145 standard_option_handler },
146 { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
148 standard_option_handler },
150 { {"target", required_argument, NULL, OPTION_TARGET},
151 '\0', "BFDNAME", "Specify the object-code format for the object files",
152 standard_option_handler },
154 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
158 standard_option_handler (sd, opt, arg, is_command)
169 STATE_VERBOSE_P (sd) = 1;
172 #ifdef SIM_HAVE_BIENDIAN
174 if (strcmp (arg, "big") == 0)
176 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
178 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
181 /* FIXME:wip: Need to set something in STATE_CONFIG. */
182 current_target_byte_order = BIG_ENDIAN;
184 else if (strcmp (arg, "little") == 0)
186 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
188 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
191 /* FIXME:wip: Need to set something in STATE_CONFIG. */
192 current_target_byte_order = LITTLE_ENDIAN;
196 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
202 case OPTION_ALIGNMENT:
203 if (strcmp (arg, "strict") == 0)
205 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
207 current_alignment = STRICT_ALIGNMENT;
211 else if (strcmp (arg, "nonstrict") == 0)
213 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
215 current_alignment = NONSTRICT_ALIGNMENT;
219 else if (strcmp (arg, "forced") == 0)
221 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
223 current_alignment = FORCED_ALIGNMENT;
229 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
232 switch (WITH_ALIGNMENT)
234 case STRICT_ALIGNMENT:
235 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
237 case NONSTRICT_ALIGNMENT:
238 sim_io_eprintf (sd, "Simulator compiled for nonsitrct alignment only.\n");
240 case FORCED_ALIGNMENT:
241 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
248 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
251 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
252 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
253 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
257 case OPTION_DEBUG_INSN :
259 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
262 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
263 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
267 case OPTION_DEBUG_FILE :
269 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
272 FILE *f = fopen (arg, "w");
276 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
279 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
280 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
284 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
290 #ifdef SIM_HAVE_FLATMEM
293 unsigned long ul = strtol (arg, NULL, 0);
294 /* 16384: some minimal amount */
295 if (! isdigit (arg[0]) || ul < 16384)
297 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
300 STATE_MEM_SIZE (sd) = ul;
305 case OPTION_DO_COMMAND:
306 sim_do_command (sd, arg);
309 case OPTION_ARCHITECTURE:
311 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
314 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
317 STATE_ARCHITECTURE (sd) = ap;
321 case OPTION_ARCHITECTURE_INFO:
323 const char **list = bfd_arch_list();
327 sim_io_printf (sd, "Valid architectures:");
328 for (lp = list; *lp != NULL; lp++)
329 sim_io_printf (sd, " %s", *lp);
330 sim_io_printf (sd, "\n");
337 STATE_TARGET (sd) = xstrdup (arg);
342 sim_print_help (sd, is_command);
343 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
345 /* FIXME: 'twould be nice to do something similar if gdb. */
352 /* Add the standard option list to the simulator. */
355 standard_install (SIM_DESC sd)
357 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
358 if (sim_add_option_table (sd, standard_options) != SIM_RC_OK)
363 /* Return non-zero if arg is a duplicate argument.
364 If ARG is NULL, initialize. */
366 #define ARG_HASH_SIZE 97
367 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
374 static char **arg_table = NULL;
378 if (arg_table == NULL)
379 arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
380 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
384 hash = ARG_HASH (arg);
385 while (arg_table[hash] != NULL)
387 if (strcmp (arg, arg_table[hash]) == 0)
389 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
390 don't check if the table is full. */
391 if (++hash == ARG_HASH_SIZE)
394 arg_table[hash] = arg;
398 /* Called by sim_open to parse the arguments. */
401 sim_parse_args (sd, argv)
405 int i, argc, num_opts;
406 char *p, *short_options;
407 /* The `val' option struct entry is dynamically assigned for options that
408 only come in the long form. ORIG_VAL is used to get the original value
410 unsigned char *orig_val;
411 struct option *lp, *long_options;
412 const struct option_list *ol;
414 OPTION_HANDLER **handlers;
416 /* Count the number of arguments. */
417 for (argc = 0; argv[argc] != NULL; ++argc)
420 /* Count the number of options. */
422 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
423 for (opt = ol->options; opt->opt.name != NULL; ++opt)
426 /* Initialize duplicate argument checker. */
427 (void) dup_arg_p (NULL);
429 /* Build the option table for getopt. */
430 long_options = (struct option *) alloca ((num_opts + 1) * sizeof (struct option));
432 short_options = (char *) alloca (num_opts * 3 + 1);
434 #if 0 /* ??? necessary anymore? */
435 /* Set '+' as first char so argument permutation isn't done. This is done
436 to workaround a problem with invoking getopt_long in run.c.: optind gets
437 decremented when the program name is reached. */
440 handlers = (OPTION_HANDLER **) alloca (256 * sizeof (OPTION_HANDLER *));
441 memset (handlers, 0, 256 * sizeof (OPTION_HANDLER *));
442 orig_val = (unsigned char *) alloca (256);
443 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
444 for (opt = ol->options; opt->opt.name != NULL; ++opt)
446 if (dup_arg_p (opt->opt.name))
448 if (opt->shortopt != 0)
450 *p++ = opt->shortopt;
451 if (opt->opt.has_arg == required_argument)
453 else if (opt->opt.has_arg == optional_argument)
454 { *p++ = ':'; *p++ = ':'; }
457 /* Dynamically assign `val' numbers for long options that don't have
458 a short option equivalent. */
459 if (OPTION_LONG_ONLY_P (opt->opt.val))
461 handlers[(unsigned char) lp->val] = opt->handler;
462 orig_val[(unsigned char) lp->val] = opt->opt.val;
468 /* Ensure getopt is initialized. */
474 optc = getopt_long (argc, argv, short_options, long_options, &longind);
477 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
478 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
484 if ((*handlers[optc]) (sd, orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
491 /* Print help messages for the options. */
494 sim_print_help (sd, is_command)
498 const struct option_list *ol;
501 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
502 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
505 /* Initialize duplicate argument checker. */
506 (void) dup_arg_p (NULL);
508 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
509 sim_io_printf (sd, "Options:\n");
511 sim_io_printf (sd, "Commands:\n");
513 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
514 for (opt = ol->options; opt->opt.name != NULL; ++opt)
516 const int indent = 30;
520 if (dup_arg_p (opt->opt.name))
523 if (opt->doc == NULL)
526 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
529 sim_io_printf (sd, " ");
539 if (o->shortopt != '\0')
541 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
542 len += (comma ? 2 : 0) + 2;
545 if (o->opt.has_arg == optional_argument)
547 sim_io_printf (sd, "[%s]", o->arg);
548 len += 1 + strlen (o->arg) + 1;
552 sim_io_printf (sd, " %s", o->arg);
553 len += 1 + strlen (o->arg);
560 while (o->opt.name != NULL && o->doc == NULL);
567 if (o->doc_name != NULL)
573 sim_io_printf (sd, "%s%s%s",
575 is_command ? "" : "--",
577 len += ((comma ? 2 : 0)
578 + (is_command ? 0 : 2)
582 if (o->opt.has_arg == optional_argument)
584 sim_io_printf (sd, " [%s]", o->arg);
585 len += 2 + strlen (o->arg) + 1;
589 sim_io_printf (sd, " %s", o->arg);
590 len += 1 + strlen (o->arg);
597 while (o->opt.name != NULL && o->doc == NULL);
601 sim_io_printf (sd, "\n%*s", indent, "");
604 sim_io_printf (sd, "%*s", indent - len, "");
607 const char *chp = opt->doc;
608 unsigned doc_width = 80 - indent;
609 while (strlen (chp) >= doc_width) /* some slack */
611 const char *end = chp + doc_width - 1;
612 while (end > chp && !isspace (*end))
615 end = chp + doc_width - 1;
616 sim_io_printf (sd, "%.*s\n%*s", end - chp, chp, indent, "");
618 while (isspace (*chp) && *chp != '\0')
621 sim_io_printf (sd, "%s\n", chp);
625 sim_io_printf (sd, "\n");
626 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
627 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
628 sim_io_printf (sd, " may not be applicable\n");
630 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
632 sim_io_printf (sd, "\n");
633 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
634 sim_io_printf (sd, " Note: Very few simulators support this.\n");
642 sim_args_command (sd, cmd)
646 /* something to do? */
648 return SIM_RC_OK; /* FIXME - perhaphs help would be better */
652 /* user specified -<opt> ... form? */
653 char **argv = buildargv (cmd);
654 SIM_RC rc = sim_parse_args (sd, argv);
660 /* user specified <opt> form? */
661 const struct option_list *ol;
663 char **argv = buildargv (cmd);
664 /* most recent option match */
665 const OPTION *matching_opt = NULL;
666 int matching_argi = -1;
667 if (argv [0] != NULL)
668 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
669 for (opt = ol->options; opt->opt.name != NULL; ++opt)
672 const char *name = opt->opt.name;
673 while (strncmp (name, argv [argi], strlen (argv [argi])) == 0)
675 name = &name [strlen (argv[argi])];
678 /* leading match ...<a-b-c>-d-e-f - continue search */
679 name ++; /* skip `-' */
683 else if (name [0] == '\0')
685 /* exact match ...<a-b-c-d-e-f> - better than before? */
686 if (argi > matching_argi)
688 matching_argi = argi;
697 if (matching_opt != NULL)
699 switch (matching_opt->opt.has_arg)
702 if (argv [matching_argi + 1] == NULL)
703 matching_opt->handler (sd, matching_opt->opt.val,
704 NULL, 1/*is_command*/);
706 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
707 matching_opt->opt.name);
709 case optional_argument:
710 if (argv [matching_argi + 1] == NULL)
711 matching_opt->handler (sd, matching_opt->opt.val,
712 NULL, 1/*is_command*/);
713 else if (argv [matching_argi + 2] == NULL)
714 matching_opt->handler (sd, matching_opt->opt.val,
715 argv [matching_argi + 1], 1/*is_command*/);
717 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
718 matching_opt->opt.name);
720 case required_argument:
721 if (argv [matching_argi + 1] == NULL)
722 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
723 matching_opt->opt.name);
724 else if (argv [matching_argi + 2] == NULL)
725 matching_opt->handler (sd, matching_opt->opt.val,
726 argv [matching_argi + 1], 1/*is_command*/);
728 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
729 matching_opt->opt.name);
735 /* didn't find anything that remotly matched */