| 1 | /* Simulator option handling. |
| 2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support. |
| 4 | |
| 5 | This file is part of GDB, the GNU debugger. |
| 6 | |
| 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) |
| 10 | any later version. |
| 11 | |
| 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. |
| 16 | |
| 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. */ |
| 20 | |
| 21 | #include "sim-main.h" |
| 22 | #ifdef HAVE_STRING_H |
| 23 | #include <string.h> |
| 24 | #else |
| 25 | #ifdef HAVE_STRINGS_H |
| 26 | #include <strings.h> |
| 27 | #endif |
| 28 | #endif |
| 29 | #ifdef HAVE_STDLIB_H |
| 30 | #include <stdlib.h> |
| 31 | #endif |
| 32 | #include <ctype.h> |
| 33 | #include "libiberty.h" |
| 34 | #include "sim-options.h" |
| 35 | #include "sim-io.h" |
| 36 | #include "sim-assert.h" |
| 37 | |
| 38 | #include "bfd.h" |
| 39 | |
| 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. */ |
| 43 | |
| 44 | SIM_RC |
| 45 | sim_add_option_table (SIM_DESC sd, sim_cpu *cpu, const OPTION *table) |
| 46 | { |
| 47 | struct option_list *ol = ((struct option_list *) |
| 48 | xmalloc (sizeof (struct option_list))); |
| 49 | |
| 50 | /* Note: The list is constructed in the reverse order we're called so |
| 51 | later calls will override earlier ones (in case that ever happens). |
| 52 | This is the intended behaviour. */ |
| 53 | |
| 54 | if (cpu) |
| 55 | { |
| 56 | ol->next = CPU_OPTIONS (cpu); |
| 57 | ol->options = table; |
| 58 | CPU_OPTIONS (cpu) = ol; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | ol->next = STATE_OPTIONS (sd); |
| 63 | ol->options = table; |
| 64 | STATE_OPTIONS (sd) = ol; |
| 65 | } |
| 66 | |
| 67 | return SIM_RC_OK; |
| 68 | } |
| 69 | |
| 70 | /* Standard option table. |
| 71 | Modules may specify additional ones. |
| 72 | The caller of sim_parse_args may also specify additional options |
| 73 | by calling sim_add_option_table first. */ |
| 74 | |
| 75 | static DECLARE_OPTION_HANDLER (standard_option_handler); |
| 76 | |
| 77 | /* FIXME: We shouldn't print in --help output options that aren't usable. |
| 78 | Some fine tuning will be necessary. One can either move less general |
| 79 | options to another table or use a HAVE_FOO macro to ifdef out unavailable |
| 80 | options. */ |
| 81 | |
| 82 | /* ??? One might want to conditionally compile out the entries that |
| 83 | aren't enabled. There's a distinction, however, between options a |
| 84 | simulator can't support and options that haven't been configured in. |
| 85 | Certainly options a simulator can't support shouldn't appear in the |
| 86 | output of --help. Whether the same thing applies to options that haven't |
| 87 | been configured in or not isn't something I can get worked up over. |
| 88 | [Note that conditionally compiling them out might simply involve moving |
| 89 | the option to another table.] |
| 90 | If you decide to conditionally compile them out as well, delete this |
| 91 | comment and add a comment saying that that is the rule. */ |
| 92 | |
| 93 | typedef enum { |
| 94 | OPTION_DEBUG_INSN = OPTION_START, |
| 95 | OPTION_DEBUG_FILE, |
| 96 | OPTION_DO_COMMAND, |
| 97 | OPTION_ARCHITECTURE, |
| 98 | OPTION_TARGET, |
| 99 | OPTION_ARCHITECTURE_INFO, |
| 100 | OPTION_ENVIRONMENT, |
| 101 | OPTION_ALIGNMENT, |
| 102 | OPTION_VERBOSE, |
| 103 | #if defined (SIM_HAVE_BIENDIAN) |
| 104 | OPTION_ENDIAN, |
| 105 | #endif |
| 106 | OPTION_DEBUG, |
| 107 | #ifdef SIM_HAVE_FLATMEM |
| 108 | OPTION_MEM_SIZE, |
| 109 | #endif |
| 110 | OPTION_HELP, |
| 111 | #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */ |
| 112 | OPTION_H8300, |
| 113 | #endif |
| 114 | OPTION_LOAD_LMA, |
| 115 | OPTION_LOAD_VMA, |
| 116 | } STANDARD_OPTIONS; |
| 117 | |
| 118 | static const OPTION standard_options[] = |
| 119 | { |
| 120 | { {"verbose", no_argument, NULL, OPTION_VERBOSE}, |
| 121 | 'v', NULL, "Verbose output", |
| 122 | standard_option_handler }, |
| 123 | |
| 124 | #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */ |
| 125 | { {"endian", required_argument, NULL, OPTION_ENDIAN}, |
| 126 | 'E', "big|little", "Set endianness", |
| 127 | standard_option_handler }, |
| 128 | #endif |
| 129 | |
| 130 | #ifdef SIM_HAVE_ENVIRONMENT |
| 131 | /* This option isn't supported unless all choices are supported in keeping |
| 132 | with the goal of not printing in --help output things the simulator can't |
| 133 | do [as opposed to things that just haven't been configured in]. */ |
| 134 | { {"environment", required_argument, NULL, OPTION_ENVIRONMENT}, |
| 135 | '\0', "user|virtual|operating", "Set running environment", |
| 136 | standard_option_handler }, |
| 137 | #endif |
| 138 | |
| 139 | { {"alignment", required_argument, NULL, OPTION_ALIGNMENT}, |
| 140 | '\0', "strict|nonstrict|forced", "Set memory access alignment", |
| 141 | standard_option_handler }, |
| 142 | |
| 143 | { {"debug", no_argument, NULL, OPTION_DEBUG}, |
| 144 | 'D', NULL, "Print debugging messages", |
| 145 | standard_option_handler }, |
| 146 | { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN}, |
| 147 | '\0', NULL, "Print instruction debugging messages", |
| 148 | standard_option_handler }, |
| 149 | { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE}, |
| 150 | '\0', "FILE NAME", "Specify debugging output file", |
| 151 | standard_option_handler }, |
| 152 | |
| 153 | #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */ |
| 154 | { {"h8300h", no_argument, NULL, OPTION_H8300}, |
| 155 | 'h', NULL, "Indicate the CPU is h8/300h or h8/300s", |
| 156 | standard_option_handler }, |
| 157 | #endif |
| 158 | |
| 159 | #ifdef SIM_HAVE_FLATMEM |
| 160 | { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE}, |
| 161 | 'm', "MEMORY SIZE", "Specify memory size", |
| 162 | standard_option_handler }, |
| 163 | #endif |
| 164 | |
| 165 | { {"do-command", required_argument, NULL, OPTION_DO_COMMAND}, |
| 166 | '\0', "COMMAND", ""/*undocumented*/, |
| 167 | standard_option_handler }, |
| 168 | |
| 169 | { {"help", no_argument, NULL, OPTION_HELP}, |
| 170 | 'H', NULL, "Print help information", |
| 171 | standard_option_handler }, |
| 172 | |
| 173 | { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE}, |
| 174 | '\0', "MACHINE", "Specify the architecture to use", |
| 175 | standard_option_handler }, |
| 176 | { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO}, |
| 177 | '\0', NULL, "List supported architectures", |
| 178 | standard_option_handler }, |
| 179 | { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO}, |
| 180 | '\0', NULL, NULL, |
| 181 | standard_option_handler }, |
| 182 | |
| 183 | { {"target", required_argument, NULL, OPTION_TARGET}, |
| 184 | '\0', "BFDNAME", "Specify the object-code format for the object files", |
| 185 | standard_option_handler }, |
| 186 | |
| 187 | #ifdef SIM_HANDLES_LMA |
| 188 | { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA}, |
| 189 | '\0', NULL, |
| 190 | #if SIM_HANDLES_LMA |
| 191 | "Use VMA or LMA addresses when loading image (default LMA)", |
| 192 | #else |
| 193 | "Use VMA or LMA addresses when loading image (default VMA)", |
| 194 | #endif |
| 195 | standard_option_handler, "load-{lma,vma}" }, |
| 196 | { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA}, |
| 197 | '\0', NULL, "", standard_option_handler, "" }, |
| 198 | #endif |
| 199 | |
| 200 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL } |
| 201 | }; |
| 202 | |
| 203 | static SIM_RC |
| 204 | standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, |
| 205 | char *arg, int is_command) |
| 206 | { |
| 207 | int i,n; |
| 208 | |
| 209 | switch ((STANDARD_OPTIONS) opt) |
| 210 | { |
| 211 | case OPTION_VERBOSE: |
| 212 | STATE_VERBOSE_P (sd) = 1; |
| 213 | break; |
| 214 | |
| 215 | #ifdef SIM_HAVE_BIENDIAN |
| 216 | case OPTION_ENDIAN: |
| 217 | if (strcmp (arg, "big") == 0) |
| 218 | { |
| 219 | if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN) |
| 220 | { |
| 221 | sim_io_eprintf (sd, "Simulator compiled for little endian only.\n"); |
| 222 | return SIM_RC_FAIL; |
| 223 | } |
| 224 | /* FIXME:wip: Need to set something in STATE_CONFIG. */ |
| 225 | current_target_byte_order = BIG_ENDIAN; |
| 226 | } |
| 227 | else if (strcmp (arg, "little") == 0) |
| 228 | { |
| 229 | if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN) |
| 230 | { |
| 231 | sim_io_eprintf (sd, "Simulator compiled for big endian only.\n"); |
| 232 | return SIM_RC_FAIL; |
| 233 | } |
| 234 | /* FIXME:wip: Need to set something in STATE_CONFIG. */ |
| 235 | current_target_byte_order = LITTLE_ENDIAN; |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg); |
| 240 | return SIM_RC_FAIL; |
| 241 | } |
| 242 | break; |
| 243 | #endif |
| 244 | |
| 245 | case OPTION_ENVIRONMENT: |
| 246 | if (strcmp (arg, "user") == 0) |
| 247 | STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT; |
| 248 | else if (strcmp (arg, "virtual") == 0) |
| 249 | STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT; |
| 250 | else if (strcmp (arg, "operating") == 0) |
| 251 | STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT; |
| 252 | else |
| 253 | { |
| 254 | sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg); |
| 255 | return SIM_RC_FAIL; |
| 256 | } |
| 257 | if (WITH_ENVIRONMENT != ALL_ENVIRONMENT |
| 258 | && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd)) |
| 259 | { |
| 260 | char *type; |
| 261 | switch (WITH_ENVIRONMENT) |
| 262 | { |
| 263 | case USER_ENVIRONMENT: type = "user"; break; |
| 264 | case VIRTUAL_ENVIRONMENT: type = "virtual"; break; |
| 265 | case OPERATING_ENVIRONMENT: type = "operating"; break; |
| 266 | } |
| 267 | sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n", |
| 268 | type); |
| 269 | return SIM_RC_FAIL; |
| 270 | } |
| 271 | break; |
| 272 | |
| 273 | case OPTION_ALIGNMENT: |
| 274 | if (strcmp (arg, "strict") == 0) |
| 275 | { |
| 276 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT) |
| 277 | { |
| 278 | current_alignment = STRICT_ALIGNMENT; |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | else if (strcmp (arg, "nonstrict") == 0) |
| 283 | { |
| 284 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT) |
| 285 | { |
| 286 | current_alignment = NONSTRICT_ALIGNMENT; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | else if (strcmp (arg, "forced") == 0) |
| 291 | { |
| 292 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT) |
| 293 | { |
| 294 | current_alignment = FORCED_ALIGNMENT; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg); |
| 301 | return SIM_RC_FAIL; |
| 302 | } |
| 303 | switch (WITH_ALIGNMENT) |
| 304 | { |
| 305 | case STRICT_ALIGNMENT: |
| 306 | sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n"); |
| 307 | break; |
| 308 | case NONSTRICT_ALIGNMENT: |
| 309 | sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n"); |
| 310 | break; |
| 311 | case FORCED_ALIGNMENT: |
| 312 | sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n"); |
| 313 | break; |
| 314 | } |
| 315 | return SIM_RC_FAIL; |
| 316 | |
| 317 | case OPTION_DEBUG: |
| 318 | if (! WITH_DEBUG) |
| 319 | sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n"); |
| 320 | else |
| 321 | { |
| 322 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) |
| 323 | for (i = 0; i < MAX_DEBUG_VALUES; ++i) |
| 324 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1; |
| 325 | } |
| 326 | break; |
| 327 | |
| 328 | case OPTION_DEBUG_INSN : |
| 329 | if (! WITH_DEBUG) |
| 330 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n"); |
| 331 | else |
| 332 | { |
| 333 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) |
| 334 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1; |
| 335 | } |
| 336 | break; |
| 337 | |
| 338 | case OPTION_DEBUG_FILE : |
| 339 | if (! WITH_DEBUG) |
| 340 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n"); |
| 341 | else |
| 342 | { |
| 343 | FILE *f = fopen (arg, "w"); |
| 344 | |
| 345 | if (f == NULL) |
| 346 | { |
| 347 | sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg); |
| 348 | return SIM_RC_FAIL; |
| 349 | } |
| 350 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) |
| 351 | CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f; |
| 352 | } |
| 353 | break; |
| 354 | |
| 355 | #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */ |
| 356 | case OPTION_H8300: |
| 357 | set_h8300h (1); |
| 358 | break; |
| 359 | #endif |
| 360 | |
| 361 | #ifdef SIM_HAVE_FLATMEM |
| 362 | case OPTION_MEM_SIZE: |
| 363 | { |
| 364 | unsigned long ul = strtol (arg, NULL, 0); |
| 365 | /* 16384: some minimal amount */ |
| 366 | if (! isdigit (arg[0]) || ul < 16384) |
| 367 | { |
| 368 | sim_io_eprintf (sd, "Invalid memory size `%s'", arg); |
| 369 | return SIM_RC_FAIL; |
| 370 | } |
| 371 | STATE_MEM_SIZE (sd) = ul; |
| 372 | } |
| 373 | break; |
| 374 | #endif |
| 375 | |
| 376 | case OPTION_DO_COMMAND: |
| 377 | sim_do_command (sd, arg); |
| 378 | break; |
| 379 | |
| 380 | case OPTION_ARCHITECTURE: |
| 381 | { |
| 382 | const struct bfd_arch_info *ap = bfd_scan_arch (arg); |
| 383 | if (ap == NULL) |
| 384 | { |
| 385 | sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg); |
| 386 | return SIM_RC_FAIL; |
| 387 | } |
| 388 | STATE_ARCHITECTURE (sd) = ap; |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | case OPTION_ARCHITECTURE_INFO: |
| 393 | { |
| 394 | const char **list = bfd_arch_list(); |
| 395 | const char **lp; |
| 396 | if (list == NULL) |
| 397 | abort (); |
| 398 | sim_io_printf (sd, "Possible architectures:"); |
| 399 | for (lp = list; *lp != NULL; lp++) |
| 400 | sim_io_printf (sd, " %s", *lp); |
| 401 | sim_io_printf (sd, "\n"); |
| 402 | free (list); |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | case OPTION_TARGET: |
| 407 | { |
| 408 | STATE_TARGET (sd) = xstrdup (arg); |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | case OPTION_LOAD_LMA: |
| 413 | { |
| 414 | STATE_LOAD_AT_LMA_P (sd) = 1; |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | case OPTION_LOAD_VMA: |
| 419 | { |
| 420 | STATE_LOAD_AT_LMA_P (sd) = 0; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | case OPTION_HELP: |
| 425 | sim_print_help (sd, is_command); |
| 426 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) |
| 427 | exit (0); |
| 428 | /* FIXME: 'twould be nice to do something similar if gdb. */ |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | return SIM_RC_OK; |
| 433 | } |
| 434 | |
| 435 | /* Add the standard option list to the simulator. */ |
| 436 | |
| 437 | SIM_RC |
| 438 | standard_install (SIM_DESC sd) |
| 439 | { |
| 440 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); |
| 441 | if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK) |
| 442 | return SIM_RC_FAIL; |
| 443 | #ifdef SIM_HANDLES_LMA |
| 444 | STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA; |
| 445 | #endif |
| 446 | return SIM_RC_OK; |
| 447 | } |
| 448 | |
| 449 | /* Return non-zero if arg is a duplicate argument. |
| 450 | If ARG is NULL, initialize. */ |
| 451 | |
| 452 | #define ARG_HASH_SIZE 97 |
| 453 | #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE) |
| 454 | |
| 455 | static int |
| 456 | dup_arg_p (arg) |
| 457 | char *arg; |
| 458 | { |
| 459 | int hash; |
| 460 | static char **arg_table = NULL; |
| 461 | |
| 462 | if (arg == NULL) |
| 463 | { |
| 464 | if (arg_table == NULL) |
| 465 | arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *)); |
| 466 | memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *)); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | hash = ARG_HASH (arg); |
| 471 | while (arg_table[hash] != NULL) |
| 472 | { |
| 473 | if (strcmp (arg, arg_table[hash]) == 0) |
| 474 | return 1; |
| 475 | /* We assume there won't be more than ARG_HASH_SIZE arguments so we |
| 476 | don't check if the table is full. */ |
| 477 | if (++hash == ARG_HASH_SIZE) |
| 478 | hash = 0; |
| 479 | } |
| 480 | arg_table[hash] = arg; |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* Called by sim_open to parse the arguments. */ |
| 485 | |
| 486 | SIM_RC |
| 487 | sim_parse_args (sd, argv) |
| 488 | SIM_DESC sd; |
| 489 | char **argv; |
| 490 | { |
| 491 | int c, i, argc, num_opts; |
| 492 | char *p, *short_options; |
| 493 | /* The `val' option struct entry is dynamically assigned for options that |
| 494 | only come in the long form. ORIG_VAL is used to get the original value |
| 495 | back. */ |
| 496 | int *orig_val; |
| 497 | struct option *lp, *long_options; |
| 498 | const struct option_list *ol; |
| 499 | const OPTION *opt; |
| 500 | OPTION_HANDLER **handlers; |
| 501 | sim_cpu **opt_cpu; |
| 502 | |
| 503 | /* Count the number of arguments. */ |
| 504 | for (argc = 0; argv[argc] != NULL; ++argc) |
| 505 | continue; |
| 506 | |
| 507 | /* Count the number of options. */ |
| 508 | num_opts = 0; |
| 509 | for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) |
| 510 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) |
| 511 | ++num_opts; |
| 512 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) |
| 513 | for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next) |
| 514 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) |
| 515 | ++num_opts; |
| 516 | |
| 517 | /* Initialize duplicate argument checker. */ |
| 518 | (void) dup_arg_p (NULL); |
| 519 | |
| 520 | /* Build the option table for getopt. */ |
| 521 | |
| 522 | long_options = NZALLOC (struct option, num_opts + 1); |
| 523 | lp = long_options; |
| 524 | short_options = NZALLOC (char, num_opts * 3 + 1); |
| 525 | p = short_options; |
| 526 | handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts); |
| 527 | orig_val = NZALLOC (int, OPTION_START + num_opts); |
| 528 | opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts); |
| 529 | |
| 530 | /* Set '+' as first char so argument permutation isn't done. This |
| 531 | is done to stop getopt_long returning options that appear after |
| 532 | the target program. Such options should be passed unchanged into |
| 533 | the program image. */ |
| 534 | *p++ = '+'; |
| 535 | |
| 536 | for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) |
| 537 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) |
| 538 | { |
| 539 | if (dup_arg_p (opt->opt.name)) |
| 540 | continue; |
| 541 | if (opt->shortopt != 0) |
| 542 | { |
| 543 | *p++ = opt->shortopt; |
| 544 | if (opt->opt.has_arg == required_argument) |
| 545 | *p++ = ':'; |
| 546 | else if (opt->opt.has_arg == optional_argument) |
| 547 | { *p++ = ':'; *p++ = ':'; } |
| 548 | handlers[(unsigned char) opt->shortopt] = opt->handler; |
| 549 | if (opt->opt.val != 0) |
| 550 | orig_val[(unsigned char) opt->shortopt] = opt->opt.val; |
| 551 | else |
| 552 | orig_val[(unsigned char) opt->shortopt] = opt->shortopt; |
| 553 | } |
| 554 | if (opt->opt.name != NULL) |
| 555 | { |
| 556 | *lp = opt->opt; |
| 557 | /* Dynamically assign `val' numbers for long options. */ |
| 558 | lp->val = i++; |
| 559 | handlers[lp->val] = opt->handler; |
| 560 | orig_val[lp->val] = opt->opt.val; |
| 561 | opt_cpu[lp->val] = NULL; |
| 562 | ++lp; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | for (c = 0; c < MAX_NR_PROCESSORS; ++c) |
| 567 | { |
| 568 | sim_cpu *cpu = STATE_CPU (sd, c); |
| 569 | for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next) |
| 570 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) |
| 571 | { |
| 572 | #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down |
| 573 | on the need for dup_arg_p checking. Maybe in the future it'll be |
| 574 | needed so this is just commented out, and not deleted. */ |
| 575 | if (dup_arg_p (opt->opt.name)) |
| 576 | continue; |
| 577 | #endif |
| 578 | /* Don't allow short versions of cpu specific options for now. */ |
| 579 | if (opt->shortopt != 0) |
| 580 | { |
| 581 | sim_io_eprintf (sd, "internal error, short cpu specific option"); |
| 582 | return SIM_RC_FAIL; |
| 583 | } |
| 584 | if (opt->opt.name != NULL) |
| 585 | { |
| 586 | char *name; |
| 587 | *lp = opt->opt; |
| 588 | /* Prepend --<cpuname>- to the option. */ |
| 589 | asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name); |
| 590 | lp->name = name; |
| 591 | /* Dynamically assign `val' numbers for long options. */ |
| 592 | lp->val = i++; |
| 593 | handlers[lp->val] = opt->handler; |
| 594 | orig_val[lp->val] = opt->opt.val; |
| 595 | opt_cpu[lp->val] = cpu; |
| 596 | ++lp; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* Terminate the short and long option lists. */ |
| 602 | *p = 0; |
| 603 | lp->name = NULL; |
| 604 | |
| 605 | /* Ensure getopt is initialized. */ |
| 606 | optind = 0; |
| 607 | |
| 608 | while (1) |
| 609 | { |
| 610 | int longind, optc; |
| 611 | |
| 612 | optc = getopt_long (argc, argv, short_options, long_options, &longind); |
| 613 | if (optc == -1) |
| 614 | { |
| 615 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) |
| 616 | STATE_PROG_ARGV (sd) = dupargv (argv + optind); |
| 617 | break; |
| 618 | } |
| 619 | if (optc == '?') |
| 620 | return SIM_RC_FAIL; |
| 621 | |
| 622 | if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL) |
| 623 | return SIM_RC_FAIL; |
| 624 | } |
| 625 | |
| 626 | return SIM_RC_OK; |
| 627 | } |
| 628 | |
| 629 | /* Utility of sim_print_help to print a list of option tables. */ |
| 630 | |
| 631 | static void |
| 632 | print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command) |
| 633 | { |
| 634 | const OPTION *opt; |
| 635 | |
| 636 | for ( ; ol != NULL; ol = ol->next) |
| 637 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) |
| 638 | { |
| 639 | const int indent = 30; |
| 640 | int comma, len; |
| 641 | const OPTION *o; |
| 642 | |
| 643 | if (dup_arg_p (opt->opt.name)) |
| 644 | continue; |
| 645 | |
| 646 | if (opt->doc == NULL) |
| 647 | continue; |
| 648 | |
| 649 | if (opt->doc_name != NULL && opt->doc_name [0] == '\0') |
| 650 | continue; |
| 651 | |
| 652 | sim_io_printf (sd, " "); |
| 653 | |
| 654 | comma = 0; |
| 655 | len = 2; |
| 656 | |
| 657 | /* list any short options (aliases) for the current OPT */ |
| 658 | if (!is_command) |
| 659 | { |
| 660 | o = opt; |
| 661 | do |
| 662 | { |
| 663 | if (o->shortopt != '\0') |
| 664 | { |
| 665 | sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt); |
| 666 | len += (comma ? 2 : 0) + 2; |
| 667 | if (o->arg != NULL) |
| 668 | { |
| 669 | if (o->opt.has_arg == optional_argument) |
| 670 | { |
| 671 | sim_io_printf (sd, "[%s]", o->arg); |
| 672 | len += 1 + strlen (o->arg) + 1; |
| 673 | } |
| 674 | else |
| 675 | { |
| 676 | sim_io_printf (sd, " %s", o->arg); |
| 677 | len += 1 + strlen (o->arg); |
| 678 | } |
| 679 | } |
| 680 | comma = 1; |
| 681 | } |
| 682 | ++o; |
| 683 | } |
| 684 | while (OPTION_VALID_P (o) && o->doc == NULL); |
| 685 | } |
| 686 | |
| 687 | /* list any long options (aliases) for the current OPT */ |
| 688 | o = opt; |
| 689 | do |
| 690 | { |
| 691 | const char *name; |
| 692 | const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL; |
| 693 | if (o->doc_name != NULL) |
| 694 | name = o->doc_name; |
| 695 | else |
| 696 | name = o->opt.name; |
| 697 | if (name != NULL) |
| 698 | { |
| 699 | sim_io_printf (sd, "%s%s%s%s%s", |
| 700 | comma ? ", " : "", |
| 701 | is_command ? "" : "--", |
| 702 | cpu ? cpu_prefix : "", |
| 703 | cpu ? "-" : "", |
| 704 | name); |
| 705 | len += ((comma ? 2 : 0) |
| 706 | + (is_command ? 0 : 2) |
| 707 | + strlen (name)); |
| 708 | if (o->arg != NULL) |
| 709 | { |
| 710 | if (o->opt.has_arg == optional_argument) |
| 711 | { |
| 712 | sim_io_printf (sd, " [%s]", o->arg); |
| 713 | len += 2 + strlen (o->arg) + 1; |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | sim_io_printf (sd, " %s", o->arg); |
| 718 | len += 1 + strlen (o->arg); |
| 719 | } |
| 720 | } |
| 721 | comma = 1; |
| 722 | } |
| 723 | ++o; |
| 724 | } |
| 725 | while (OPTION_VALID_P (o) && o->doc == NULL); |
| 726 | |
| 727 | if (len >= indent) |
| 728 | { |
| 729 | sim_io_printf (sd, "\n%*s", indent, ""); |
| 730 | } |
| 731 | else |
| 732 | sim_io_printf (sd, "%*s", indent - len, ""); |
| 733 | |
| 734 | /* print the description, word wrap long lines */ |
| 735 | { |
| 736 | const char *chp = opt->doc; |
| 737 | unsigned doc_width = 80 - indent; |
| 738 | while (strlen (chp) >= doc_width) /* some slack */ |
| 739 | { |
| 740 | const char *end = chp + doc_width - 1; |
| 741 | while (end > chp && !isspace (*end)) |
| 742 | end --; |
| 743 | if (end == chp) |
| 744 | end = chp + doc_width - 1; |
| 745 | sim_io_printf (sd, "%.*s\n%*s", end - chp, chp, indent, ""); |
| 746 | chp = end; |
| 747 | while (isspace (*chp) && *chp != '\0') |
| 748 | chp++; |
| 749 | } |
| 750 | sim_io_printf (sd, "%s\n", chp); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* Print help messages for the options. */ |
| 756 | |
| 757 | void |
| 758 | sim_print_help (sd, is_command) |
| 759 | SIM_DESC sd; |
| 760 | int is_command; |
| 761 | { |
| 762 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) |
| 763 | sim_io_printf (sd, "Usage: %s [options] program [program args]\n", |
| 764 | STATE_MY_NAME (sd)); |
| 765 | |
| 766 | /* Initialize duplicate argument checker. */ |
| 767 | (void) dup_arg_p (NULL); |
| 768 | |
| 769 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) |
| 770 | sim_io_printf (sd, "Options:\n"); |
| 771 | else |
| 772 | sim_io_printf (sd, "Commands:\n"); |
| 773 | |
| 774 | print_help (sd, NULL, STATE_OPTIONS (sd), is_command); |
| 775 | sim_io_printf (sd, "\n"); |
| 776 | |
| 777 | /* Print cpu-specific options. */ |
| 778 | { |
| 779 | int i; |
| 780 | |
| 781 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) |
| 782 | { |
| 783 | sim_cpu *cpu = STATE_CPU (sd, i); |
| 784 | if (CPU_OPTIONS (cpu) == NULL) |
| 785 | continue; |
| 786 | sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu)); |
| 787 | print_help (sd, cpu, CPU_OPTIONS (cpu), is_command); |
| 788 | sim_io_printf (sd, "\n"); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n", |
| 793 | STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command"); |
| 794 | sim_io_printf (sd, " may not be applicable\n"); |
| 795 | |
| 796 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) |
| 797 | { |
| 798 | sim_io_printf (sd, "\n"); |
| 799 | sim_io_printf (sd, "program args Arguments to pass to simulated program.\n"); |
| 800 | sim_io_printf (sd, " Note: Very few simulators support this.\n"); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* Utility of sim_args_command to find the closest match for a command. |
| 805 | Commands that have "-" in them can be specified as separate words. |
| 806 | e.g. sim memory-region 0x800000,0x4000 |
| 807 | or sim memory region 0x800000,0x4000 |
| 808 | If CPU is non-null, use its option table list, otherwise use the main one. |
| 809 | *PARGI is where to start looking in ARGV. It is updated to point past |
| 810 | the found option. */ |
| 811 | |
| 812 | static const OPTION * |
| 813 | find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi) |
| 814 | { |
| 815 | const struct option_list *ol; |
| 816 | const OPTION *opt; |
| 817 | /* most recent option match */ |
| 818 | const OPTION *matching_opt = NULL; |
| 819 | int matching_argi = -1; |
| 820 | |
| 821 | if (cpu) |
| 822 | ol = CPU_OPTIONS (cpu); |
| 823 | else |
| 824 | ol = STATE_OPTIONS (sd); |
| 825 | |
| 826 | /* Skip passed elements specified by *PARGI. */ |
| 827 | argv += *pargi; |
| 828 | |
| 829 | for ( ; ol != NULL; ol = ol->next) |
| 830 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) |
| 831 | { |
| 832 | int argi = 0; |
| 833 | const char *name = opt->opt.name; |
| 834 | if (name == NULL) |
| 835 | continue; |
| 836 | while (argv [argi] != NULL |
| 837 | && strncmp (name, argv [argi], strlen (argv [argi])) == 0) |
| 838 | { |
| 839 | name = &name [strlen (argv[argi])]; |
| 840 | if (name [0] == '-') |
| 841 | { |
| 842 | /* leading match ...<a-b-c>-d-e-f - continue search */ |
| 843 | name ++; /* skip `-' */ |
| 844 | argi ++; |
| 845 | continue; |
| 846 | } |
| 847 | else if (name [0] == '\0') |
| 848 | { |
| 849 | /* exact match ...<a-b-c-d-e-f> - better than before? */ |
| 850 | if (argi > matching_argi) |
| 851 | { |
| 852 | matching_argi = argi; |
| 853 | matching_opt = opt; |
| 854 | } |
| 855 | break; |
| 856 | } |
| 857 | else |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | *pargi = matching_argi; |
| 863 | return matching_opt; |
| 864 | } |
| 865 | |
| 866 | SIM_RC |
| 867 | sim_args_command (SIM_DESC sd, char *cmd) |
| 868 | { |
| 869 | /* something to do? */ |
| 870 | if (cmd == NULL) |
| 871 | return SIM_RC_OK; /* FIXME - perhaphs help would be better */ |
| 872 | |
| 873 | if (cmd [0] == '-') |
| 874 | { |
| 875 | /* user specified -<opt> ... form? */ |
| 876 | char **argv = buildargv (cmd); |
| 877 | SIM_RC rc = sim_parse_args (sd, argv); |
| 878 | freeargv (argv); |
| 879 | return rc; |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | char **argv = buildargv (cmd); |
| 884 | const OPTION *matching_opt = NULL; |
| 885 | int matching_argi; |
| 886 | sim_cpu *cpu; |
| 887 | |
| 888 | if (argv [0] == NULL) |
| 889 | return SIM_RC_OK; /* FIXME - perhaphs help would be better */ |
| 890 | |
| 891 | /* First check for a cpu selector. */ |
| 892 | { |
| 893 | char *cpu_name = xstrdup (argv[0]); |
| 894 | char *hyphen = strchr (cpu_name, '-'); |
| 895 | if (hyphen) |
| 896 | *hyphen = 0; |
| 897 | cpu = sim_cpu_lookup (sd, cpu_name); |
| 898 | if (cpu) |
| 899 | { |
| 900 | /* If <cpuname>-<command>, point argv[0] at <command>. */ |
| 901 | if (hyphen) |
| 902 | { |
| 903 | matching_argi = 0; |
| 904 | argv[0] += hyphen - cpu_name + 1; |
| 905 | } |
| 906 | else |
| 907 | matching_argi = 1; |
| 908 | matching_opt = find_match (sd, cpu, argv, &matching_argi); |
| 909 | /* If hyphen found restore argv[0]. */ |
| 910 | if (hyphen) |
| 911 | argv[0] -= hyphen - cpu_name + 1; |
| 912 | } |
| 913 | free (cpu_name); |
| 914 | } |
| 915 | |
| 916 | /* If that failed, try the main table. */ |
| 917 | if (matching_opt == NULL) |
| 918 | { |
| 919 | matching_argi = 0; |
| 920 | matching_opt = find_match (sd, NULL, argv, &matching_argi); |
| 921 | } |
| 922 | |
| 923 | if (matching_opt != NULL) |
| 924 | { |
| 925 | switch (matching_opt->opt.has_arg) |
| 926 | { |
| 927 | case no_argument: |
| 928 | if (argv [matching_argi + 1] == NULL) |
| 929 | matching_opt->handler (sd, cpu, matching_opt->opt.val, |
| 930 | NULL, 1/*is_command*/); |
| 931 | else |
| 932 | sim_io_eprintf (sd, "Command `%s' takes no arguments\n", |
| 933 | matching_opt->opt.name); |
| 934 | break; |
| 935 | case optional_argument: |
| 936 | if (argv [matching_argi + 1] == NULL) |
| 937 | matching_opt->handler (sd, cpu, matching_opt->opt.val, |
| 938 | NULL, 1/*is_command*/); |
| 939 | else if (argv [matching_argi + 2] == NULL) |
| 940 | matching_opt->handler (sd, cpu, matching_opt->opt.val, |
| 941 | argv [matching_argi + 1], 1/*is_command*/); |
| 942 | else |
| 943 | sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n", |
| 944 | matching_opt->opt.name); |
| 945 | break; |
| 946 | case required_argument: |
| 947 | if (argv [matching_argi + 1] == NULL) |
| 948 | sim_io_eprintf (sd, "Command `%s' requires an argument\n", |
| 949 | matching_opt->opt.name); |
| 950 | else if (argv [matching_argi + 2] == NULL) |
| 951 | matching_opt->handler (sd, cpu, matching_opt->opt.val, |
| 952 | argv [matching_argi + 1], 1/*is_command*/); |
| 953 | else |
| 954 | sim_io_eprintf (sd, "Command `%s' requires only one argument\n", |
| 955 | matching_opt->opt.name); |
| 956 | } |
| 957 | freeargv (argv); |
| 958 | return SIM_RC_OK; |
| 959 | } |
| 960 | |
| 961 | freeargv (argv); |
| 962 | } |
| 963 | |
| 964 | /* didn't find anything that remotly matched */ |
| 965 | return SIM_RC_FAIL; |
| 966 | } |