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 "sim-options.h"
36 #include "sim-assert.h"
40 /* Add a set of options to the simulator.
41 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
42 This is intended to be called by modules in their `install' handler. */
45 sim_add_option_table (sd
, table
)
49 struct option_list
*ol
= ((struct option_list
*)
50 xmalloc (sizeof (struct option_list
)));
52 /* Note: The list is constructed in the reverse order we're called so
53 later calls will override earlier ones (in case that ever happens).
54 This is the intended behaviour. */
55 ol
->next
= STATE_OPTIONS (sd
);
57 STATE_OPTIONS (sd
) = ol
;
62 /* Standard option table.
63 Modules may specify additional ones.
64 The caller of sim_parse_args may also specify additional options
65 by calling sim_add_option_table first. */
67 static DECLARE_OPTION_HANDLER (standard_option_handler
);
69 /* FIXME: We shouldn't print in --help output options that aren't usable.
70 Some fine tuning will be necessary. One can either move less general
71 options to another table or use a HAVE_FOO macro to ifdef out unavailable
74 /* ??? One might want to conditionally compile out the entries that
75 aren't enabled. There's a distinction, however, between options a
76 simulator can't support and options that haven't been configured in.
77 Certainly options a simulator can't support shouldn't appear in the
78 output of --help. Whether the same thing applies to options that haven't
79 been configured in or not isn't something I can get worked up over.
80 [Note that conditionally compiling them out might simply involve moving
81 the option to another table.]
82 If you decide to conditionally compile them out as well, delete this
83 comment and add a comment saying that that is the rule. */
86 OPTION_DEBUG_INSN
= OPTION_START
,
91 OPTION_ARCHITECTURE_INFO
,
95 #if defined (SIM_HAVE_BIENDIAN)
99 #ifdef SIM_HAVE_FLATMEM
103 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
108 static const OPTION standard_options
[] =
110 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
111 'v', NULL
, "Verbose output",
112 standard_option_handler
},
114 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
115 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
116 'E', "big|little", "Set endianness",
117 standard_option_handler
},
120 #ifdef SIM_HAVE_ENVIRONMENT
121 /* This option isn't supported unless all choices are supported in keeping
122 with the goal of not printing in --help output things the simulator can't
123 do [as opposed to things that just haven't been configured in]. */
124 { {"environment", required_argument
, NULL
, OPTION_ENVIRONMENT
},
125 '\0', "user|virtual|operating", "Set running environment",
126 standard_option_handler
},
129 { {"alignment", required_argument
, NULL
, OPTION_ALIGNMENT
},
130 '\0', "strict|nonstrict|forced", "Set memory access alignment",
131 standard_option_handler
},
133 { {"debug", no_argument
, NULL
, OPTION_DEBUG
},
134 'D', NULL
, "Print debugging messages",
135 standard_option_handler
},
136 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
137 '\0', NULL
, "Print instruction debugging messages",
138 standard_option_handler
},
139 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
140 '\0', "FILE NAME", "Specify debugging output file",
141 standard_option_handler
},
143 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
144 { {"h8300h", no_argument
, NULL
, OPTION_H8300
},
145 'h', NULL
, "Indicate the CPU is h8/300h or h8/300s",
146 standard_option_handler
},
149 #ifdef SIM_HAVE_FLATMEM
150 { {"mem-size", required_argument
, NULL
, OPTION_MEM_SIZE
},
151 'm', "MEMORY SIZE", "Specify memory size",
152 standard_option_handler
},
155 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
156 '\0', "COMMAND", ""/*undocumented*/,
157 standard_option_handler
},
159 { {"help", no_argument
, NULL
, OPTION_HELP
},
160 'H', NULL
, "Print help information",
161 standard_option_handler
},
163 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
164 '\0', "MACHINE", "Specify the architecture to use",
165 standard_option_handler
},
166 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
167 '\0', NULL
, "List supported architectures",
168 standard_option_handler
},
169 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
171 standard_option_handler
},
173 { {"target", required_argument
, NULL
, OPTION_TARGET
},
174 '\0', "BFDNAME", "Specify the object-code format for the object files",
175 standard_option_handler
},
177 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
181 standard_option_handler (sd
, opt
, arg
, is_command
)
189 switch ((STANDARD_OPTIONS
) opt
)
192 STATE_VERBOSE_P (sd
) = 1;
195 #ifdef SIM_HAVE_BIENDIAN
197 if (strcmp (arg
, "big") == 0)
199 if (WITH_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
)
201 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
204 /* FIXME:wip: Need to set something in STATE_CONFIG. */
205 current_target_byte_order
= BIG_ENDIAN
;
207 else if (strcmp (arg
, "little") == 0)
209 if (WITH_TARGET_BYTE_ORDER
== BIG_ENDIAN
)
211 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
214 /* FIXME:wip: Need to set something in STATE_CONFIG. */
215 current_target_byte_order
= LITTLE_ENDIAN
;
219 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
225 case OPTION_ENVIRONMENT
:
226 if (strcmp (arg
, "user") == 0)
227 current_environment
= USER_ENVIRONMENT
;
228 else if (strcmp (arg
, "virtual") == 0)
229 current_environment
= VIRTUAL_ENVIRONMENT
;
230 else if (strcmp (arg
, "operating") == 0)
231 current_environment
= OPERATING_ENVIRONMENT
;
234 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
237 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
238 && WITH_ENVIRONMENT
!= current_environment
)
241 switch (WITH_ENVIRONMENT
)
243 case USER_ENVIRONMENT
: type
= "user"; break;
244 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
245 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
247 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
253 case OPTION_ALIGNMENT
:
254 if (strcmp (arg
, "strict") == 0)
256 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
258 current_alignment
= STRICT_ALIGNMENT
;
262 else if (strcmp (arg
, "nonstrict") == 0)
264 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
266 current_alignment
= NONSTRICT_ALIGNMENT
;
270 else if (strcmp (arg
, "forced") == 0)
272 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
274 current_alignment
= FORCED_ALIGNMENT
;
280 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
283 switch (WITH_ALIGNMENT
)
285 case STRICT_ALIGNMENT
:
286 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
288 case NONSTRICT_ALIGNMENT
:
289 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
291 case FORCED_ALIGNMENT
:
292 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
299 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
302 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
303 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
304 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
308 case OPTION_DEBUG_INSN
:
310 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
313 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
314 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
318 case OPTION_DEBUG_FILE
:
320 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
323 FILE *f
= fopen (arg
, "w");
327 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
330 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
331 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
335 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
341 #ifdef SIM_HAVE_FLATMEM
342 case OPTION_MEM_SIZE
:
344 unsigned long ul
= strtol (arg
, NULL
, 0);
345 /* 16384: some minimal amount */
346 if (! isdigit (arg
[0]) || ul
< 16384)
348 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
351 STATE_MEM_SIZE (sd
) = ul
;
356 case OPTION_DO_COMMAND
:
357 sim_do_command (sd
, arg
);
360 case OPTION_ARCHITECTURE
:
362 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
365 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
368 STATE_ARCHITECTURE (sd
) = ap
;
372 case OPTION_ARCHITECTURE_INFO
:
374 const char **list
= bfd_arch_list();
378 sim_io_printf (sd
, "Valid architectures:");
379 for (lp
= list
; *lp
!= NULL
; lp
++)
380 sim_io_printf (sd
, " %s", *lp
);
381 sim_io_printf (sd
, "\n");
388 STATE_TARGET (sd
) = xstrdup (arg
);
393 sim_print_help (sd
, is_command
);
394 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
396 /* FIXME: 'twould be nice to do something similar if gdb. */
403 /* Add the standard option list to the simulator. */
406 standard_install (SIM_DESC sd
)
408 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
409 if (sim_add_option_table (sd
, standard_options
) != SIM_RC_OK
)
414 /* Return non-zero if arg is a duplicate argument.
415 If ARG is NULL, initialize. */
417 #define ARG_HASH_SIZE 97
418 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
425 static char **arg_table
= NULL
;
429 if (arg_table
== NULL
)
430 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
431 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
435 hash
= ARG_HASH (arg
);
436 while (arg_table
[hash
] != NULL
)
438 if (strcmp (arg
, arg_table
[hash
]) == 0)
440 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
441 don't check if the table is full. */
442 if (++hash
== ARG_HASH_SIZE
)
445 arg_table
[hash
] = arg
;
449 /* Called by sim_open to parse the arguments. */
452 sim_parse_args (sd
, argv
)
456 int i
, argc
, num_opts
;
457 char *p
, *short_options
;
458 /* The `val' option struct entry is dynamically assigned for options that
459 only come in the long form. ORIG_VAL is used to get the original value
462 struct option
*lp
, *long_options
;
463 const struct option_list
*ol
;
465 OPTION_HANDLER
**handlers
;
467 /* Count the number of arguments. */
468 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
471 /* Count the number of options. */
473 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
474 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
477 /* Initialize duplicate argument checker. */
478 (void) dup_arg_p (NULL
);
480 /* Build the option table for getopt. */
481 long_options
= NZALLOC (struct option
, num_opts
+ 1);
483 short_options
= NZALLOC (char, num_opts
* 3 + 1);
485 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
486 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
487 /* Set '+' as first char so argument permutation isn't done. This
488 is done to stop getopt_long returning options that appear after
489 the target program. Such options should be passed unchanged into
490 the program image. */
492 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
493 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
495 if (dup_arg_p (opt
->opt
.name
))
497 if (opt
->shortopt
!= 0)
499 *p
++ = opt
->shortopt
;
500 if (opt
->opt
.has_arg
== required_argument
)
502 else if (opt
->opt
.has_arg
== optional_argument
)
503 { *p
++ = ':'; *p
++ = ':'; }
504 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
505 if (opt
->opt
.val
!= 0)
506 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
508 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
510 if (opt
->opt
.name
!= NULL
)
513 /* Dynamically assign `val' numbers for long options. */
515 handlers
[lp
->val
] = opt
->handler
;
516 orig_val
[lp
->val
] = opt
->opt
.val
;
523 /* Ensure getopt is initialized. */
529 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
532 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
533 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
539 if ((*handlers
[optc
]) (sd
, orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
546 /* Print help messages for the options. */
549 sim_print_help (sd
, is_command
)
553 const struct option_list
*ol
;
556 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
557 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
560 /* Initialize duplicate argument checker. */
561 (void) dup_arg_p (NULL
);
563 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
564 sim_io_printf (sd
, "Options:\n");
566 sim_io_printf (sd
, "Commands:\n");
568 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
569 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
571 const int indent
= 30;
575 if (dup_arg_p (opt
->opt
.name
))
578 if (opt
->doc
== NULL
)
581 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
584 sim_io_printf (sd
, " ");
589 /* list any short options (aliases) for the current OPT */
595 if (o
->shortopt
!= '\0')
597 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
598 len
+= (comma
? 2 : 0) + 2;
601 if (o
->opt
.has_arg
== optional_argument
)
603 sim_io_printf (sd
, "[%s]", o
->arg
);
604 len
+= 1 + strlen (o
->arg
) + 1;
608 sim_io_printf (sd
, " %s", o
->arg
);
609 len
+= 1 + strlen (o
->arg
);
616 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
619 /* list any long options (aliases) for the current OPT */
624 if (o
->doc_name
!= NULL
)
630 sim_io_printf (sd
, "%s%s%s",
632 is_command
? "" : "--",
634 len
+= ((comma
? 2 : 0)
635 + (is_command
? 0 : 2)
639 if (o
->opt
.has_arg
== optional_argument
)
641 sim_io_printf (sd
, " [%s]", o
->arg
);
642 len
+= 2 + strlen (o
->arg
) + 1;
646 sim_io_printf (sd
, " %s", o
->arg
);
647 len
+= 1 + strlen (o
->arg
);
654 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
658 sim_io_printf (sd
, "\n%*s", indent
, "");
661 sim_io_printf (sd
, "%*s", indent
- len
, "");
663 /* print the description, word wrap long lines */
665 const char *chp
= opt
->doc
;
666 unsigned doc_width
= 80 - indent
;
667 while (strlen (chp
) >= doc_width
) /* some slack */
669 const char *end
= chp
+ doc_width
- 1;
670 while (end
> chp
&& !isspace (*end
))
673 end
= chp
+ doc_width
- 1;
674 sim_io_printf (sd
, "%.*s\n%*s", end
- chp
, chp
, indent
, "");
676 while (isspace (*chp
) && *chp
!= '\0')
679 sim_io_printf (sd
, "%s\n", chp
);
683 sim_io_printf (sd
, "\n");
684 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
685 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
686 sim_io_printf (sd
, " may not be applicable\n");
688 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
690 sim_io_printf (sd
, "\n");
691 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
692 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
700 sim_args_command (sd
, cmd
)
704 /* something to do? */
706 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
710 /* user specified -<opt> ... form? */
711 char **argv
= buildargv (cmd
);
712 SIM_RC rc
= sim_parse_args (sd
, argv
);
718 /* user specified <opt> form? */
719 const struct option_list
*ol
;
721 char **argv
= buildargv (cmd
);
722 /* most recent option match */
723 const OPTION
*matching_opt
= NULL
;
724 int matching_argi
= -1;
725 if (argv
[0] != NULL
)
726 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
727 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
730 const char *name
= opt
->opt
.name
;
733 while (strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
735 name
= &name
[strlen (argv
[argi
])];
738 /* leading match ...<a-b-c>-d-e-f - continue search */
739 name
++; /* skip `-' */
743 else if (name
[0] == '\0')
745 /* exact match ...<a-b-c-d-e-f> - better than before? */
746 if (argi
> matching_argi
)
748 matching_argi
= argi
;
757 if (matching_opt
!= NULL
)
759 switch (matching_opt
->opt
.has_arg
)
762 if (argv
[matching_argi
+ 1] == NULL
)
763 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
764 NULL
, 1/*is_command*/);
766 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
767 matching_opt
->opt
.name
);
769 case optional_argument
:
770 if (argv
[matching_argi
+ 1] == NULL
)
771 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
772 NULL
, 1/*is_command*/);
773 else if (argv
[matching_argi
+ 2] == NULL
)
774 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
775 argv
[matching_argi
+ 1], 1/*is_command*/);
777 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
778 matching_opt
->opt
.name
);
780 case required_argument
:
781 if (argv
[matching_argi
+ 1] == NULL
)
782 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
783 matching_opt
->opt
.name
);
784 else if (argv
[matching_argi
+ 2] == NULL
)
785 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
786 argv
[matching_argi
+ 1], 1/*is_command*/);
788 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
789 matching_opt
->opt
.name
);
795 /* didn't find anything that remotly matched */