]>
Commit | Line | Data |
---|---|---|
4ede3a83 DE |
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 | #include "libiberty.h" | |
30 | #include "../libiberty/alloca-conf.h" | |
31 | #include "sim-options.h" | |
32 | #include "sim-io.h" | |
33 | ||
34 | /* This is defined in sim-config.h. */ | |
35 | #ifndef MAX_NR_PROCESSORS | |
36 | #define MAX_NR_PROCESSORS 1 | |
37 | #endif | |
38 | ||
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. */ | |
42 | ||
43 | SIM_RC | |
44 | sim_add_option_table (sd, table) | |
45 | SIM_DESC sd; | |
46 | const OPTION *table; | |
47 | { | |
48 | struct option_list *ol = ((struct option_list *) | |
49 | xmalloc (sizeof (struct option_list))); | |
50 | ||
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); | |
55 | ol->options = table; | |
56 | STATE_OPTIONS (sd) = ol; | |
57 | ||
58 | return SIM_RC_OK; | |
59 | } | |
60 | ||
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. */ | |
65 | ||
66 | static DECLARE_OPTION_HANDLER (standard_option_handler); | |
67 | ||
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 | |
71 | options. */ | |
72 | ||
73 | #define OPTION_DEBUG_INSN (OPTION_START + 0) | |
74 | #define OPTION_DEBUG_FILE (OPTION_START + 1) | |
75 | #define OPTION_TRACE_INSN (OPTION_START + 2) | |
76 | #define OPTION_TRACE_DECODE (OPTION_START + 3) | |
77 | #define OPTION_TRACE_EXTRACT (OPTION_START + 4) | |
78 | #define OPTION_TRACE_LINENUM (OPTION_START + 5) | |
79 | #define OPTION_TRACE_MEMORY (OPTION_START + 6) | |
80 | #define OPTION_TRACE_MODEL (OPTION_START + 7) | |
81 | #define OPTION_TRACE_ALU (OPTION_START + 8) | |
82 | #define OPTION_TRACE_FILE (OPTION_START + 9) | |
83 | #define OPTION_PROFILE_INSN (OPTION_START + 10) | |
84 | #define OPTION_PROFILE_MEMORY (OPTION_START + 11) | |
85 | #define OPTION_PROFILE_MODEL (OPTION_START + 12) | |
86 | #define OPTION_PROFILE_SIMCACHE (OPTION_START + 13) | |
87 | #define OPTION_PROFILE_FILE (OPTION_START + 14) | |
88 | ||
89 | static const OPTION standard_options[] = | |
90 | { | |
91 | { {"verbose", no_argument, NULL, 'v'}, | |
92 | 'v', NULL, "Verbose output", | |
93 | standard_option_handler }, | |
94 | ||
95 | { {"debug", no_argument, NULL, 'D'}, | |
96 | 'D', NULL, "Print debugging messages", | |
97 | standard_option_handler }, | |
98 | { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN}, | |
99 | '\0', NULL, "Print instruction debugging messages", | |
100 | standard_option_handler }, | |
101 | { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE}, | |
102 | '\0', "FILE NAME", "Specify debugging output file", | |
103 | standard_option_handler }, | |
104 | ||
105 | { {"trace", no_argument, NULL, 't'}, | |
106 | 't', NULL, "Perform tracing", | |
107 | standard_option_handler }, | |
108 | { {"trace-insn", no_argument, NULL, OPTION_TRACE_INSN}, | |
109 | '\0', NULL, "Perform instruction tracing", | |
110 | standard_option_handler }, | |
111 | { {"trace-decode", no_argument, NULL, OPTION_TRACE_DECODE}, | |
112 | '\0', NULL, "Perform instruction decoding tracing", | |
113 | standard_option_handler }, | |
114 | { {"trace-extract", no_argument, NULL, OPTION_TRACE_EXTRACT}, | |
115 | '\0', NULL, "Perform instruction extraction tracing", | |
116 | standard_option_handler }, | |
117 | { {"trace-linenum", no_argument, NULL, OPTION_TRACE_LINENUM}, | |
118 | '\0', NULL, "Perform line number tracing", | |
119 | standard_option_handler }, | |
120 | { {"trace-memory", no_argument, NULL, OPTION_TRACE_MEMORY}, | |
121 | '\0', NULL, "Perform memory tracing", | |
122 | standard_option_handler }, | |
123 | { {"trace-model", no_argument, NULL, OPTION_TRACE_MODEL}, | |
124 | '\0', NULL, "Perform model tracing", | |
125 | standard_option_handler }, | |
126 | { {"trace-alu", no_argument, NULL, OPTION_TRACE_ALU}, | |
127 | '\0', NULL, "Perform ALU tracing", | |
128 | standard_option_handler }, | |
129 | { {"trace-file", required_argument, NULL, OPTION_TRACE_FILE}, | |
130 | '\0', "FILE NAME", "Specify tracing output file", | |
131 | standard_option_handler }, | |
132 | ||
133 | #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */ | |
134 | { {"h8300h", no_argument, NULL, 'h'}, | |
135 | 'h', NULL, "Indicate the CPU is h8/300h or h8/300s", | |
136 | standard_option_handler }, | |
137 | #endif | |
138 | ||
139 | #ifdef SIM_HAVE_SIMCACHE | |
140 | { {"simcache-size", required_argument, NULL, 'c'}, | |
141 | 'c', "SIM CACHE SIZE", "Specify size of simulator instruction cache", | |
142 | standard_option_handler }, | |
143 | #endif | |
144 | ||
145 | #ifdef SIM_HAVE_FLATMEM | |
146 | { {"mem-size", required_argument, NULL, 'm'}, | |
147 | 'm', "MEMORY SIZE", "Specify memory size", | |
148 | standard_option_handler }, | |
149 | #endif | |
150 | ||
151 | #ifdef SIM_HAVE_MAX_INSNS | |
152 | { {"max-insns", required_argument, NULL, 'M'}, | |
153 | 'M', "MAX INSNS", "Specify maximum instructions to execute", | |
154 | standard_option_handler }, | |
155 | #endif | |
156 | ||
157 | #ifdef SIM_HAVE_PROFILE | |
158 | { {"profile", no_argument, NULL, 'p'}, | |
159 | 'p', NULL, "Perform profiling", | |
160 | standard_option_handler }, | |
161 | { {"profile-insn", no_argument, NULL, OPTION_PROFILE_INSN}, | |
162 | '\0', NULL, "Perform instruction profiling", | |
163 | standard_option_handler }, | |
164 | { {"profile-memory", no_argument, NULL, OPTION_PROFILE_MEMORY}, | |
165 | '\0', NULL, "Perform memory profiling", | |
166 | standard_option_handler }, | |
167 | { {"profile-model", no_argument, NULL, OPTION_PROFILE_MODEL}, | |
168 | '\0', NULL, "Perform model profiling", | |
169 | standard_option_handler }, | |
170 | { {"profile-simcache", no_argument, NULL, OPTION_PROFILE_SIMCACHE}, | |
171 | '\0', NULL, "Perform simulator execution cache profiling", | |
172 | standard_option_handler }, | |
173 | { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE}, | |
174 | '\0', "FILE NAME", "Specify profile output file", | |
175 | standard_option_handler }, | |
176 | #ifdef SIM_HAVE_PROFILE_PC | |
177 | { {"profile-pc-frequency", required_argument, NULL, 'F'}, | |
178 | 'F', "PC PROFILE FREQUENCY", "Turn on PC profiling at specified frequency", | |
179 | standard_option_handler }, | |
180 | { {"profile-pc-size", required_argument, NULL, 'S'}, | |
181 | 'S', "PC PROFILE SIZE", "Specify PC profiling size", | |
182 | standard_option_handler }, | |
183 | #endif | |
184 | #endif /* SIM_HAVE_PROFILE */ | |
185 | ||
186 | { {"help", no_argument, NULL, 'H'}, | |
187 | 'H', NULL, "Print help information", | |
188 | standard_option_handler }, | |
189 | ||
190 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL } | |
191 | }; | |
192 | ||
193 | static SIM_RC | |
194 | standard_option_handler (sd, opt, arg) | |
195 | SIM_DESC sd; | |
196 | int opt; | |
197 | char *arg; | |
198 | { | |
199 | int i,n; | |
200 | unsigned long ul; | |
201 | ||
202 | switch (opt) | |
203 | { | |
204 | case 'v' : | |
205 | STATE_VERBOSE_P (sd) = 1; | |
206 | break; | |
207 | ||
208 | case 'D' : | |
209 | if (! WITH_DEBUG) | |
210 | sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n"); | |
211 | else | |
212 | { | |
213 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
214 | for (i = 0; i < MAX_DEBUG_VALUES; ++i) | |
215 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1; | |
216 | } | |
217 | break; | |
218 | ||
219 | case OPTION_DEBUG_INSN : | |
220 | if (! WITH_DEBUG) | |
221 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n"); | |
222 | else | |
223 | { | |
224 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
225 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1; | |
226 | } | |
227 | break; | |
228 | ||
229 | case OPTION_DEBUG_FILE : | |
230 | if (! WITH_DEBUG) | |
231 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n"); | |
232 | else | |
233 | { | |
234 | FILE *f = fopen (arg, "w"); | |
235 | ||
236 | if (f == NULL) | |
237 | { | |
238 | sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg); | |
239 | return SIM_RC_FAIL; | |
240 | } | |
241 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
242 | CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f; | |
243 | } | |
244 | break; | |
245 | ||
246 | case 't' : | |
247 | if (! WITH_TRACE) | |
248 | sim_io_eprintf (sd, "Tracing not compiled in, `-t' ignored\n"); | |
249 | else | |
250 | { | |
251 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
252 | for (i = 0; i < MAX_TRACE_VALUES; ++i) | |
253 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[i] = 1; | |
254 | } | |
255 | break; | |
256 | ||
257 | case OPTION_TRACE_INSN : | |
258 | if (! (WITH_TRACE & (1 << TRACE_INSN_IDX))) | |
8786d426 | 259 | sim_io_eprintf (sd, "Instruction tracing not compiled in, `--trace-insn' ignored\n"); |
4ede3a83 DE |
260 | else |
261 | { | |
262 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
263 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_INSN_IDX] = 1; | |
264 | } | |
265 | break; | |
266 | ||
267 | case OPTION_TRACE_DECODE : | |
268 | if (! (WITH_TRACE & (1 << TRACE_DECODE_IDX))) | |
8786d426 | 269 | sim_io_eprintf (sd, "Decode tracing not compiled in, `--trace-decode' ignored\n"); |
4ede3a83 DE |
270 | else |
271 | { | |
272 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
273 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_DECODE_IDX] = 1; | |
274 | } | |
275 | break; | |
276 | ||
277 | case OPTION_TRACE_EXTRACT : | |
278 | if (! (WITH_TRACE & (1 << TRACE_EXTRACT_IDX))) | |
8786d426 | 279 | sim_io_eprintf (sd, "Extract tracing not compiled in, `--trace-extract' ignored\n"); |
4ede3a83 DE |
280 | else |
281 | { | |
282 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
283 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_EXTRACT_IDX] = 1; | |
284 | } | |
285 | break; | |
286 | ||
287 | case OPTION_TRACE_LINENUM : | |
288 | if (! (WITH_TRACE & (1 << TRACE_LINENUM_IDX))) | |
8786d426 | 289 | sim_io_eprintf (sd, "Line number tracing not compiled in, `--trace-linenum' ignored\n"); |
4ede3a83 DE |
290 | else |
291 | { | |
292 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
293 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_LINENUM_IDX] = 1; | |
294 | } | |
295 | break; | |
296 | ||
297 | case OPTION_TRACE_MEMORY : | |
298 | if (! (WITH_TRACE & (1 << TRACE_MEMORY_IDX))) | |
8786d426 | 299 | sim_io_eprintf (sd, "Memory tracing not compiled in, `--trace-memory' ignored\n"); |
4ede3a83 DE |
300 | else |
301 | { | |
302 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
303 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_MEMORY_IDX] = 1; | |
304 | } | |
305 | break; | |
306 | ||
307 | case OPTION_TRACE_MODEL : | |
308 | if (! (WITH_TRACE & (1 << TRACE_MODEL_IDX))) | |
8786d426 | 309 | sim_io_eprintf (sd, "Model tracing not compiled in, `--trace-model' ignored\n"); |
4ede3a83 DE |
310 | else |
311 | { | |
312 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
313 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_MODEL_IDX] = 1; | |
314 | } | |
315 | break; | |
316 | ||
317 | case OPTION_TRACE_ALU : | |
318 | if (! (WITH_TRACE & (1 << TRACE_ALU_IDX))) | |
8786d426 | 319 | sim_io_eprintf (sd, "ALU tracing not compiled in, `--trace-alu' ignored\n"); |
4ede3a83 DE |
320 | else |
321 | { | |
322 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
323 | CPU_TRACE_FLAGS (STATE_CPU (sd, n))[TRACE_ALU_IDX] = 1; | |
324 | } | |
325 | break; | |
326 | ||
327 | case OPTION_TRACE_FILE : | |
328 | if (! WITH_TRACE) | |
329 | sim_io_eprintf (sd, "Tracing not compiled in, `--trace-file' ignored\n"); | |
330 | else | |
331 | { | |
332 | FILE *f = fopen (arg, "w"); | |
333 | ||
334 | if (f == NULL) | |
335 | { | |
336 | sim_io_eprintf (sd, "Unable to open trace output file `%s'\n", arg); | |
337 | return SIM_RC_FAIL; | |
338 | } | |
339 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
340 | CPU_TRACE_FILE (STATE_CPU (sd, n)) = f; | |
341 | } | |
342 | break; | |
343 | ||
344 | #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */ | |
345 | case 'h' : | |
346 | set_h8300h (1); | |
347 | break; | |
348 | #endif | |
349 | ||
350 | #ifdef SIM_HAVE_SIMCACHE | |
351 | case 'c': | |
352 | n = strtol (arg, NULL, 0); | |
353 | if (n <= 0) | |
354 | { | |
355 | sim_io_eprintf (sd, "Invalid simulator cache size: %d", n); | |
356 | return SIM_RC_FAIL; | |
357 | } | |
358 | STATE_SIMCACHE_SIZE (sd) = n; | |
359 | break; | |
360 | #endif | |
361 | ||
362 | #ifdef SIM_HAVE_FLATMEM | |
363 | case 'm': | |
364 | 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 | break; | |
373 | #endif | |
374 | ||
375 | #ifdef SIM_HAVE_MAX_INSNS | |
376 | case 'M' : | |
377 | ul = strtoul (arg, NULL, 0); | |
378 | if (! isdigit (arg[0])) | |
379 | { | |
380 | sim_io_eprintf (sd, "Invalid maximum instruction count: `%s'", arg); | |
381 | return SIM_RC_FAIL; | |
382 | } | |
383 | STATE_MAX_INSNS (sd) = ul; | |
384 | break; | |
385 | #endif | |
386 | ||
387 | #ifdef SIM_HAVE_PROFILE | |
388 | case 'p' : | |
389 | if (! WITH_PROFILE) | |
390 | sim_io_eprintf (sd, "Profile not compiled in, -p option ignored\n"); | |
391 | else | |
392 | { | |
393 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
394 | for (i = 0; i < MAX_PROFILE_VALUES; ++i) | |
395 | CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[i] = 1; | |
396 | } | |
397 | break; | |
398 | ||
399 | case OPTION_PROFILE_INSN : | |
400 | if (! (WITH_PROFILE & (1 << PROFILE_INSN_IDX))) | |
8786d426 | 401 | sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n"); |
4ede3a83 DE |
402 | else |
403 | { | |
404 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
405 | CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_INSN_IDX] = 1; | |
406 | } | |
407 | break; | |
408 | ||
409 | case OPTION_PROFILE_MEMORY : | |
410 | if (! (WITH_PROFILE & (1 << PROFILE_MEMORY_IDX))) | |
8786d426 | 411 | sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n"); |
4ede3a83 DE |
412 | else |
413 | { | |
414 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
415 | CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_MEMORY_IDX] = 1; | |
416 | } | |
417 | break; | |
418 | ||
419 | case OPTION_PROFILE_MODEL : | |
420 | if (! (WITH_PROFILE & (1 << PROFILE_MODEL_IDX))) | |
8786d426 | 421 | sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n"); |
4ede3a83 DE |
422 | else |
423 | { | |
424 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
425 | CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_MODEL_IDX] = 1; | |
426 | } | |
427 | break; | |
428 | ||
429 | case OPTION_PROFILE_SIMCACHE : | |
430 | if (! (WITH_PROFILE & (1 << PROFILE_SIMCACHE_IDX))) | |
8786d426 | 431 | sim_io_eprintf (sd, "Simulator cache profiling not compiled in, `--profile-simcache' ignored\n"); |
4ede3a83 DE |
432 | else |
433 | { | |
434 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
435 | CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_SIMCACHE_IDX] = 1; | |
436 | } | |
437 | break; | |
438 | ||
439 | case OPTION_PROFILE_FILE : | |
440 | /* FIXME: Might want this to apply to pc profiling only, | |
441 | or have two profile file options. */ | |
442 | if (! WITH_PROFILE) | |
443 | sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n"); | |
444 | else | |
445 | { | |
446 | FILE *f = fopen (arg, "w"); | |
447 | ||
448 | if (f == NULL) | |
449 | { | |
450 | sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg); | |
451 | return SIM_RC_FAIL; | |
452 | } | |
453 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
454 | CPU_PROFILE_FILE (STATE_CPU (sd, n)) = f; | |
455 | } | |
456 | break; | |
457 | ||
458 | #ifdef SIM_HAVE_PROFILE_PC | |
459 | case 'F' : | |
460 | STATE_PROFILE_PC_FREQUENCY (sd) = atoi (arg); | |
461 | /* FIXME: Validate arg. */ | |
462 | break; | |
463 | case 'S' : | |
464 | STATE_PROFILE_PC_SIZE (sd) = atoi (arg); | |
465 | /* FIXME: Validate arg. */ | |
466 | break; | |
467 | #endif | |
468 | #endif /* SIM_HAVE_PROFILE */ | |
469 | ||
470 | case 'H': | |
471 | sim_print_help (sd); | |
472 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
473 | exit (0); | |
474 | /* FIXME: 'twould be nice to do something similar if gdb. */ | |
475 | break; | |
476 | } | |
477 | ||
478 | return SIM_RC_OK; | |
479 | } | |
480 | ||
481 | /* Initialize common parts before argument processing. | |
482 | Called by sim_open. */ | |
483 | ||
484 | SIM_RC | |
485 | sim_pre_argv_init (sd, myname) | |
486 | SIM_DESC sd; | |
487 | const char *myname; | |
488 | { | |
489 | STATE_MY_NAME (sd) = myname + strlen (myname); | |
490 | while (STATE_MY_NAME (sd) > myname && STATE_MY_NAME (sd)[-1] != '/') | |
491 | --STATE_MY_NAME (sd); | |
492 | ||
493 | if (sim_add_option_table (sd, standard_options) != SIM_RC_OK) | |
494 | return SIM_RC_FAIL; | |
495 | ||
496 | return SIM_RC_OK; | |
497 | } | |
498 | ||
499 | /* Return non-zero if arg is a duplicate argument. | |
500 | If ARG is NULL, initialize. */ | |
501 | ||
502 | #define ARG_HASH_SIZE 97 | |
503 | #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE) | |
504 | ||
505 | static int | |
506 | dup_arg_p (arg) | |
507 | char *arg; | |
508 | { | |
509 | int hash; | |
510 | static char **arg_table = NULL; | |
511 | ||
512 | if (arg == NULL) | |
513 | { | |
514 | if (arg_table == NULL) | |
515 | arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *)); | |
516 | memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *)); | |
517 | return 0; | |
518 | } | |
519 | ||
520 | hash = ARG_HASH (arg); | |
521 | while (arg_table[hash] != NULL) | |
522 | { | |
523 | if (strcmp (arg, arg_table[hash]) == 0) | |
524 | return 1; | |
525 | /* We assume there won't be more than ARG_HASH_SIZE arguments so we | |
526 | don't check if the table is full. */ | |
527 | if (++hash == ARG_HASH_SIZE) | |
528 | hash = 0; | |
529 | } | |
530 | arg_table[hash] = arg; | |
531 | return 0; | |
532 | } | |
533 | ||
534 | /* Called by sim_open to parse the arguments. */ | |
535 | ||
536 | SIM_RC | |
537 | sim_parse_args (sd, argv) | |
538 | SIM_DESC sd; | |
539 | char **argv; | |
540 | { | |
541 | int i, argc, num_opts; | |
542 | char *p, *short_options; | |
543 | /* The `val' option struct entry is dynamically assigned for options that | |
544 | only come in the long form. ORIG_VAL is used to get the original value | |
545 | back. */ | |
8786d426 | 546 | unsigned char *orig_val; |
4ede3a83 DE |
547 | struct option *lp, *long_options; |
548 | const struct option_list *ol; | |
549 | const OPTION *opt; | |
550 | OPTION_HANDLER **handlers; | |
551 | ||
552 | /* Count the number of arguments. */ | |
553 | for (argc = 0; argv[argc] != NULL; ++argc) | |
554 | continue; | |
555 | ||
556 | /* Count the number of options. */ | |
557 | num_opts = 0; | |
558 | for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) | |
559 | for (opt = ol->options; opt->opt.name != NULL; ++opt) | |
560 | ++num_opts; | |
561 | ||
562 | /* Initialize duplicate argument checker. */ | |
563 | (void) dup_arg_p (NULL); | |
564 | ||
565 | /* Build the option table for getopt. */ | |
566 | long_options = (struct option *) alloca ((num_opts + 1) * sizeof (struct option)); | |
567 | lp = long_options; | |
568 | short_options = (char *) alloca (num_opts * 3 + 1); | |
569 | p = short_options; | |
570 | #if 0 /* ??? necessary anymore? */ | |
571 | /* Set '+' as first char so argument permutation isn't done. This is done | |
572 | to workaround a problem with invoking getopt_long in run.c.: optind gets | |
573 | decremented when the program name is reached. */ | |
574 | *p++ = '+'; | |
575 | #endif | |
576 | handlers = (OPTION_HANDLER **) alloca (256 * sizeof (OPTION_HANDLER *)); | |
577 | memset (handlers, 0, 256 * sizeof (OPTION_HANDLER *)); | |
8786d426 | 578 | orig_val = (unsigned char *) alloca (256); |
4ede3a83 DE |
579 | for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) |
580 | for (opt = ol->options; opt->opt.name != NULL; ++opt) | |
581 | { | |
582 | if (dup_arg_p (opt->opt.name)) | |
583 | continue; | |
584 | if (opt->shortopt != 0) | |
585 | { | |
586 | *p++ = opt->shortopt; | |
587 | if (opt->opt.has_arg == required_argument) | |
588 | *p++ = ':'; | |
589 | else if (opt->opt.has_arg == optional_argument) | |
590 | { *p++ = ':'; *p++ = ':'; } | |
591 | } | |
592 | *lp = opt->opt; | |
593 | /* Dynamically assign `val' numbers for long options that don't have | |
594 | a short option equivalent. */ | |
595 | if (OPTION_LONG_ONLY_P (opt->opt.val)) | |
596 | lp->val = i++; | |
597 | handlers[(unsigned char) lp->val] = opt->handler; | |
598 | orig_val[(unsigned char) lp->val] = opt->opt.val; | |
599 | ++lp; | |
600 | } | |
601 | *p = 0; | |
602 | lp->name = NULL; | |
603 | ||
604 | /* Ensure getopt is initialized. */ | |
605 | optind = 0; | |
606 | while (1) | |
607 | { | |
608 | int longind, optc; | |
609 | ||
610 | optc = getopt_long (argc, argv, short_options, long_options, &longind); | |
611 | if (optc == -1) | |
612 | { | |
613 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
614 | STATE_PROG_ARGV (sd) = argv + optind; | |
615 | break; | |
616 | } | |
617 | if (optc == '?') | |
618 | return SIM_RC_FAIL; | |
619 | ||
620 | if ((*handlers[optc]) (sd, orig_val[optc], optarg) == SIM_RC_FAIL) | |
621 | return SIM_RC_FAIL; | |
622 | } | |
623 | ||
624 | return SIM_RC_OK; | |
625 | } | |
626 | ||
627 | /* Print help messages for the options. */ | |
628 | ||
629 | void | |
630 | sim_print_help (sd) | |
631 | SIM_DESC sd; | |
632 | { | |
633 | const struct option_list *ol; | |
634 | const OPTION *opt; | |
635 | ||
636 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
637 | sim_io_printf (sd, "Usage: %s [options] program [program args]\n", | |
638 | STATE_MY_NAME (sd)); | |
639 | ||
640 | /* Initialize duplicate argument checker. */ | |
641 | (void) dup_arg_p (NULL); | |
642 | ||
643 | sim_io_printf (sd, "Options:\n"); | |
644 | for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) | |
645 | for (opt = ol->options; opt->opt.name != NULL; ++opt) | |
646 | { | |
647 | int comma, len; | |
648 | const OPTION *o; | |
649 | ||
650 | if (dup_arg_p (opt->opt.name)) | |
651 | continue; | |
652 | ||
653 | if (opt->doc == NULL) | |
654 | continue; | |
655 | ||
656 | sim_io_printf (sd, " "); | |
657 | ||
658 | comma = 0; | |
659 | len = 2; | |
660 | ||
661 | o = opt; | |
662 | do | |
663 | { | |
664 | if (o->shortopt != '\0') | |
665 | { | |
666 | sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt); | |
667 | len += (comma ? 2 : 0) + 2; | |
668 | if (o->arg != NULL) | |
669 | { | |
670 | if (o->opt.has_arg != optional_argument) | |
671 | { | |
672 | sim_io_printf (sd, " "); | |
673 | ++len; | |
674 | } | |
675 | sim_io_printf (sd, "%s", o->arg); | |
676 | len += strlen (o->arg); | |
677 | } | |
678 | comma = 1; | |
679 | } | |
680 | ++o; | |
681 | } | |
682 | while (o->opt.name != NULL && o->doc == NULL); | |
683 | ||
684 | o = opt; | |
685 | do | |
686 | { | |
687 | if (o->opt.name != NULL) | |
688 | { | |
689 | sim_io_printf (sd, "%s--%s", | |
690 | comma ? ", " : "", | |
691 | o->opt.name); | |
692 | len += ((comma ? 2 : 0) | |
693 | + 2 | |
694 | + strlen (o->opt.name)); | |
695 | if (o->arg != NULL) | |
696 | { | |
697 | sim_io_printf (sd, " %s", o->arg); | |
698 | len += 1 + strlen (o->arg); | |
699 | } | |
700 | comma = 1; | |
701 | } | |
702 | ++o; | |
703 | } | |
704 | while (o->opt.name != NULL && o->doc == NULL); | |
705 | ||
706 | if (len >= 30) | |
707 | { | |
708 | sim_io_printf (sd, "\n"); | |
709 | len = 0; | |
710 | } | |
711 | ||
712 | for (; len < 30; len++) | |
713 | sim_io_printf (sd, " "); | |
714 | ||
715 | sim_io_printf (sd, "%s\n", opt->doc); | |
716 | } | |
717 | ||
718 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
719 | { | |
720 | sim_io_printf (sd, "\n"); | |
721 | sim_io_printf (sd, "program args Arguments to pass to simulated program.\n"); | |
722 | sim_io_printf (sd, " Note: Very few simulators support this.\n"); | |
723 | } | |
724 | } |