Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* Select target systems and architectures at runtime for GDB. |
2 | Copyright (C) 1990 Free Software Foundation, Inc. | |
3 | Contributed by Cygnus Support. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | GDB 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 1, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GDB 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 | |
18 | along with GDB; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <errno.h> | |
23 | #include <ctype.h> | |
24 | #include "defs.h" | |
25 | #include "target.h" | |
26 | #include "gdbcmd.h" | |
27 | #include "symtab.h" | |
28 | #include "inferior.h" | |
29 | #include "bfd.h" | |
30 | #include "symfile.h" | |
31 | ||
32 | extern int memory_insert_breakpoint(), memory_remove_breakpoint(); | |
33 | extern void host_convert_to_virtual(), host_convert_from_virtual(); | |
fc47a10d | 34 | extern void add_syms_addr_command(); |
bd5635a1 RP |
35 | |
36 | static void cleanup_target (); | |
37 | ||
38 | /* Pointer to array of target architecture structures; the size of the | |
39 | array; the current index into the array; the allocated size of the | |
40 | array. */ | |
41 | struct target_ops **target_structs; | |
42 | unsigned target_struct_size; | |
43 | unsigned target_struct_index; | |
44 | unsigned target_struct_allocsize; | |
45 | #define DEFAULT_ALLOCSIZE 10 | |
46 | ||
47 | /* The initial current target, so that there is always a semi-valid | |
48 | current target. */ | |
49 | ||
f2fc6e7a | 50 | struct target_ops dummy_target = {"None", "None", "", |
bd5635a1 RP |
51 | 0, 0, 0, 0, /* open, close, attach, detach */ |
52 | 0, 0, /* resume, wait */ | |
53 | 0, 0, 0, 0, 0, /* registers */ | |
54 | 0, 0, /* memory */ | |
55 | 0, 0, /* bkpts */ | |
56 | 0, 0, 0, 0, 0, /* terminal */ | |
fc47a10d JG |
57 | 0, 0, /* kill, load */ |
58 | add_syms_addr_command, /* add_syms */ | |
bd5635a1 RP |
59 | 0, 0, /* call_function, lookup_symbol */ |
60 | 0, 0, /* create_inferior, mourn_inferior */ | |
61 | dummy_stratum, 0, /* stratum, next */ | |
62 | 0, 0, 0, 0, 0, /* all mem, mem, stack, regs, exec */ | |
63 | OPS_MAGIC, | |
64 | }; | |
65 | ||
66 | /* The target structure we are currently using to talk to a process | |
67 | or file or whatever "inferior" we have. */ | |
68 | ||
69 | struct target_ops *current_target; | |
70 | ||
71 | /* The stack of target structures that have been pushed. */ | |
72 | ||
73 | struct target_ops **current_target_stack; | |
74 | ||
f2fc6e7a JK |
75 | /* Command list for target. */ |
76 | ||
77 | static struct cmd_list_element *targetlist = NULL; | |
78 | ||
79 | /* Docstring for target (as in "help target"). */ | |
80 | ||
81 | static char *target_doc = NULL; | |
82 | ||
83 | /* The user just typed 'target' without the name of a target. */ | |
84 | ||
85 | static void | |
86 | target_command (arg, from_tty) | |
87 | char *arg; | |
88 | int from_tty; | |
89 | { | |
90 | fputs_filtered ("Argument required (target name).", stdout); | |
91 | } | |
bd5635a1 RP |
92 | |
93 | /* Add a possible target architecture to the list. */ | |
94 | ||
95 | void | |
96 | add_target (t) | |
97 | struct target_ops *t; | |
98 | { | |
99 | if (t->to_magic != OPS_MAGIC) | |
100 | { | |
101 | fprintf(stderr, "Magic number of %s target struct wrong\n", | |
102 | t->to_shortname); | |
103 | abort(); | |
104 | } | |
105 | ||
106 | if (!target_structs) | |
107 | { | |
108 | target_struct_allocsize = DEFAULT_ALLOCSIZE; | |
109 | target_structs = (struct target_ops **) xmalloc | |
110 | (target_struct_allocsize * sizeof (*target_structs)); | |
111 | } | |
112 | if (target_struct_size >= target_struct_allocsize) | |
113 | { | |
114 | target_struct_allocsize *= 2; | |
115 | target_structs = (struct target_ops **) xrealloc (target_structs, | |
116 | target_struct_allocsize * sizeof (*target_structs)); | |
117 | } | |
118 | target_structs[target_struct_size++] = t; | |
119 | cleanup_target (t); | |
f2fc6e7a JK |
120 | |
121 | if (targetlist == NULL) | |
122 | add_prefix_cmd ("target", class_run, target_command, | |
123 | "Connect to a target machine or process.\n\ | |
124 | The first argument is the type or protocol of the target machine.\n\ | |
125 | Remaining arguments are interpreted by the target protocol. For more\n\ | |
126 | information on the arguments for a particular protocol, type\n\ | |
127 | `help target ' followed by the protocol name.", | |
128 | &targetlist, "target ", 0, &cmdlist); | |
129 | add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist); | |
bd5635a1 RP |
130 | } |
131 | ||
132 | /* Stub functions */ | |
133 | ||
134 | static void | |
135 | ignore () | |
136 | { | |
137 | } | |
138 | ||
139 | /* ARGSUSED */ | |
140 | static int | |
141 | nomemory (memaddr, myaddr, len, write) | |
142 | CORE_ADDR memaddr; | |
143 | char *myaddr; | |
144 | int len; | |
145 | int write; | |
146 | { | |
147 | return 0; /* No bytes handled */ | |
148 | } | |
149 | ||
150 | static void | |
151 | tcomplain () | |
152 | { | |
153 | error ("You can't do that when your target is `%s'", | |
154 | current_target->to_shortname); | |
155 | } | |
156 | ||
157 | static int | |
158 | noprocess () | |
159 | { | |
160 | error ("You can't do that without a process to debug"); | |
161 | } | |
162 | ||
163 | static int | |
164 | nosymbol (name, addrp) | |
165 | char *name; | |
166 | CORE_ADDR *addrp; | |
167 | { | |
168 | return 1; /* Symbol does not exist in target env */ | |
169 | } | |
170 | ||
171 | static void | |
172 | default_terminal_info (args, from_tty) | |
173 | char *args; | |
174 | int from_tty; | |
175 | { | |
176 | printf("No saved terminal information.\n"); | |
177 | } | |
178 | ||
179 | #if 0 | |
180 | /* With strata, this function is no longer needed. FIXME. */ | |
181 | /* This is the default target_create_inferior function. It looks up | |
182 | the stack for some target that cares to create inferiors, then | |
183 | calls it -- or complains if not found. */ | |
184 | ||
185 | static void | |
186 | upstack_create_inferior (exec, args, env) | |
187 | char *exec; | |
188 | char *args; | |
189 | char **env; | |
190 | { | |
191 | struct target_ops *t; | |
192 | ||
193 | for (t = current_target; | |
194 | t; | |
195 | t = t->to_next) | |
196 | { | |
197 | if (t->to_create_inferior != upstack_create_inferior) | |
198 | { | |
199 | t->to_create_inferior (exec, args, env); | |
200 | return; | |
201 | } | |
202 | ||
203 | } | |
204 | tcomplain(); | |
205 | } | |
206 | #endif | |
207 | ||
208 | /* This is the default target_create_inferior and target_attach function. | |
209 | If the current target is executing, it asks whether to kill it off. | |
210 | If this function returns without calling error(), it has killed off | |
211 | the target, and the operation should be attempted. */ | |
212 | ||
213 | static void | |
214 | kill_or_be_killed (from_tty) | |
215 | int from_tty; | |
216 | { | |
217 | struct target_ops *savecur; | |
218 | ||
219 | if (target_has_execution) | |
220 | { | |
221 | printf ("You are already running a program:\n"); | |
222 | target_files_info (); | |
223 | if (query ("Kill it? ")) { | |
224 | savecur = current_target; | |
225 | target_kill (0, from_tty); | |
226 | if (target_has_execution) | |
227 | error ("Killing the program did not help."); | |
228 | return; | |
229 | } else { | |
230 | error ("Program not killed."); | |
231 | } | |
232 | } | |
233 | tcomplain(); | |
234 | } | |
235 | ||
236 | static void | |
237 | maybe_kill_then_attach (args, from_tty) | |
238 | char *args; | |
239 | int from_tty; | |
240 | { | |
241 | kill_or_be_killed (from_tty); | |
242 | target_attach (args, from_tty); | |
243 | } | |
244 | ||
245 | static void | |
246 | maybe_kill_then_create_inferior (exec, args, env) | |
247 | char *exec; | |
248 | char *args; | |
249 | char **env; | |
250 | { | |
251 | kill_or_be_killed (0); | |
252 | target_create_inferior (exec, args, env); | |
253 | } | |
254 | ||
255 | /* Clean up a target struct so it no longer has any zero pointers in it. | |
256 | We default entries, at least to stubs that print error messages. */ | |
257 | ||
258 | static void | |
259 | cleanup_target (t) | |
260 | struct target_ops *t; | |
261 | { | |
262 | ||
263 | /* Check magic number. If wrong, it probably means someone changed | |
264 | the struct definition, but not all the places that initialize one. */ | |
265 | if (t->to_magic != OPS_MAGIC) | |
266 | { | |
267 | fprintf(stderr, "Magic number of %s target struct wrong\n", | |
268 | t->to_shortname); | |
269 | abort(); | |
270 | } | |
271 | ||
272 | #define de_fault(field, value) \ | |
273 | if (!t->field) t->field = value | |
274 | ||
275 | /* FIELD DEFAULT VALUE */ | |
276 | ||
277 | de_fault (to_open, tcomplain); | |
278 | de_fault (to_close, (void (*)())ignore); | |
279 | de_fault (to_attach, maybe_kill_then_attach); | |
280 | de_fault (to_detach, (void (*)())ignore); | |
281 | de_fault (to_resume, (void (*)())noprocess); | |
282 | de_fault (to_wait, noprocess); | |
283 | de_fault (to_fetch_registers, noprocess); | |
284 | de_fault (to_store_registers, noprocess); | |
285 | de_fault (to_prepare_to_store, (void (*)())noprocess); | |
286 | de_fault (to_convert_to_virtual, host_convert_to_virtual); | |
287 | de_fault (to_convert_from_virtual, host_convert_from_virtual); | |
288 | de_fault (to_xfer_memory, nomemory); | |
289 | de_fault (to_files_info, ignore); | |
290 | de_fault (to_insert_breakpoint, memory_insert_breakpoint); | |
291 | de_fault (to_remove_breakpoint, memory_remove_breakpoint); | |
292 | de_fault (to_terminal_init, ignore); | |
293 | de_fault (to_terminal_inferior, ignore); | |
294 | de_fault (to_terminal_ours_for_output,ignore); | |
295 | de_fault (to_terminal_ours, ignore); | |
296 | de_fault (to_terminal_info, default_terminal_info); | |
297 | de_fault (to_kill, (void (*)())noprocess); | |
298 | de_fault (to_load, tcomplain); | |
299 | de_fault (to_add_syms, tcomplain); | |
300 | de_fault (to_call_function, (struct value *(*)())noprocess); | |
301 | de_fault (to_lookup_symbol, nosymbol); | |
302 | de_fault (to_create_inferior, maybe_kill_then_create_inferior); | |
303 | de_fault (to_mourn_inferior, (void (*)())noprocess); | |
304 | de_fault (to_next, 0); | |
305 | de_fault (to_has_all_memory, 0); | |
306 | de_fault (to_has_memory, 0); | |
307 | de_fault (to_has_stack, 0); | |
308 | de_fault (to_has_registers, 0); | |
309 | de_fault (to_has_execution, 0); | |
310 | ||
311 | #undef de_fault | |
312 | } | |
313 | ||
314 | /* Push a new target type into the stack of the existing target accessors, | |
315 | possibly superseding some of the existing accessors. | |
316 | ||
317 | Result is zero if the pushed target ended up on top of the stack, | |
318 | nonzero if at least one target is on top of it. | |
319 | ||
320 | Rather than allow an empty stack, we always have the dummy target at | |
321 | the bottom stratum, so we can call the function vectors without | |
322 | checking them. */ | |
323 | ||
324 | int | |
325 | push_target (t) | |
326 | struct target_ops *t; | |
327 | { | |
328 | struct target_ops *st, *prev; | |
329 | ||
330 | for (prev = 0, st = current_target; | |
331 | st; | |
332 | prev = st, st = st->to_next) { | |
333 | if ((int)(t->to_stratum) >= (int)(st->to_stratum)) | |
334 | break; | |
335 | } | |
336 | ||
337 | while (t->to_stratum == st->to_stratum) { | |
338 | /* There's already something on this stratum. Close it off. */ | |
339 | (st->to_close) (0); | |
340 | if (prev) | |
341 | prev->to_next = st->to_next; /* Unchain old target_ops */ | |
342 | else | |
343 | current_target = st->to_next; /* Unchain first on list */ | |
344 | st = st->to_next; | |
345 | } | |
346 | ||
347 | /* We have removed all targets in our stratum, now add ourself. */ | |
348 | t->to_next = st; | |
349 | if (prev) | |
350 | prev->to_next = t; | |
351 | else | |
352 | current_target = t; | |
353 | ||
354 | cleanup_target (current_target); | |
355 | return prev != 0; | |
356 | } | |
357 | ||
358 | /* Remove a target_ops vector from the stack, wherever it may be. | |
359 | Return how many times it was removed (0 or 1 unless bug). */ | |
360 | ||
361 | int | |
362 | unpush_target (t) | |
363 | struct target_ops *t; | |
364 | { | |
365 | struct target_ops *u, *v; | |
366 | int result = 0; | |
367 | ||
368 | for (u = current_target, v = 0; | |
369 | u; | |
370 | v = u, u = u->to_next) | |
371 | if (u == t) | |
372 | { | |
373 | if (v == 0) | |
374 | pop_target(); /* unchain top copy */ | |
375 | else { | |
376 | (t->to_close)(0); /* Let it clean up */ | |
377 | v->to_next = t->to_next; /* unchain middle copy */ | |
378 | } | |
379 | result++; | |
380 | } | |
381 | return result; | |
382 | } | |
383 | ||
384 | void | |
385 | pop_target () | |
386 | { | |
387 | (current_target->to_close)(0); /* Let it clean up */ | |
388 | current_target = current_target->to_next; | |
389 | if (!current_target) /* At bottom, push dummy. */ | |
390 | push_target (&dummy_target); | |
391 | } | |
392 | ||
bd5635a1 RP |
393 | /* Move memory to or from the targets. Iterate until all of it has |
394 | been moved, if necessary. The top target gets priority; anything | |
395 | it doesn't want, is offered to the next one down, etc. Note the | |
396 | business with curlen: if an early target says "no, but I have a | |
397 | boundary overlapping this xfer" then we shorten what we offer to | |
398 | the subsequent targets so the early guy will get a chance at the | |
399 | tail before the subsequent ones do. | |
400 | ||
401 | Result is 0 or errno value. */ | |
402 | ||
403 | int | |
404 | target_read_memory (memaddr, myaddr, len) | |
405 | CORE_ADDR memaddr; | |
406 | char *myaddr; | |
407 | int len; | |
408 | { | |
409 | return target_xfer_memory (memaddr, myaddr, len, 0); | |
410 | } | |
411 | ||
412 | int | |
413 | target_write_memory (memaddr, myaddr, len) | |
414 | CORE_ADDR memaddr; | |
415 | char *myaddr; | |
416 | int len; | |
417 | { | |
418 | return target_xfer_memory (memaddr, myaddr, len, 1); | |
419 | } | |
420 | ||
421 | int | |
422 | target_xfer_memory (memaddr, myaddr, len, write) | |
423 | CORE_ADDR memaddr; | |
424 | char *myaddr; | |
425 | int len; | |
426 | int write; | |
427 | { | |
428 | int curlen; | |
429 | int res; | |
430 | struct target_ops *t; | |
431 | ||
432 | /* The quick case is that the top target does it all. */ | |
433 | res = current_target->to_xfer_memory(memaddr, myaddr, len, write); | |
434 | if (res == len) | |
435 | return 0; | |
436 | ||
437 | if (res > 0) | |
438 | goto bump; | |
439 | /* If res <= 0 then we call it again in the loop. Ah well. */ | |
440 | ||
441 | for (; len > 0;) | |
442 | { | |
443 | curlen = len; /* Want to do it all */ | |
444 | for (t = current_target; | |
445 | t; | |
446 | t = t->to_has_all_memory? 0: t->to_next) | |
447 | { | |
448 | res = t->to_xfer_memory(memaddr, myaddr, curlen, write); | |
449 | if (res > 0) break; /* Handled all or part of xfer */ | |
450 | if (res == 0) continue; /* Handled none */ | |
451 | curlen = -res; /* Could handle once we get past res bytes */ | |
452 | } | |
453 | if (res <= 0) | |
454 | { | |
455 | /* If this address is for nonexistent memory, | |
456 | read zeros if reading, or do nothing if writing. Return error. */ | |
457 | if (!write) | |
458 | bzero (myaddr, len); | |
459 | return EIO; | |
460 | } | |
461 | bump: | |
462 | memaddr += res; | |
463 | myaddr += res; | |
464 | len -= res; | |
465 | } | |
466 | return 0; /* We managed to cover it all somehow. */ | |
467 | } | |
468 | ||
469 | ||
470 | static void | |
471 | target_info (args, from_tty) | |
472 | char *args; | |
473 | int from_tty; | |
474 | { | |
475 | struct target_ops *t; | |
476 | int has_all_mem = 0; | |
477 | ||
478 | if (symfile != 0) | |
479 | printf ("Symbols from \"%s\".\n", symfile); | |
480 | ||
481 | #ifdef FILES_INFO_HOOK | |
482 | if (FILES_INFO_HOOK ()) | |
483 | return; | |
484 | #endif | |
485 | ||
486 | for (t = current_target; | |
487 | t; | |
488 | t = t->to_next) | |
489 | { | |
490 | if ((int)(t->to_stratum) <= (int)dummy_stratum) | |
491 | continue; | |
492 | if (has_all_mem) | |
493 | printf("\tWhile running this, gdb does not access memory from...\n"); | |
494 | printf("%s:\n", t->to_longname); | |
495 | (t->to_files_info)(); | |
496 | has_all_mem = t->to_has_all_memory; | |
497 | } | |
498 | } | |
499 | ||
f2fc6e7a JK |
500 | /* This is to be called by the open routine before it does |
501 | anything. */ | |
bd5635a1 | 502 | |
f2fc6e7a JK |
503 | void |
504 | target_preopen (from_tty) | |
bd5635a1 RP |
505 | int from_tty; |
506 | { | |
bd5635a1 RP |
507 | dont_repeat(); |
508 | ||
bd5635a1 RP |
509 | if (target_has_execution) |
510 | { | |
511 | if (query ("A program is being debugged already. Kill it? ")) | |
512 | target_kill ((char *)0, from_tty); | |
513 | else | |
514 | error ("Program not killed."); | |
515 | } | |
bd5635a1 RP |
516 | } |
517 | ||
518 | static char targ_desc[] = | |
519 | "Names of targets and files being debugged.\n\ | |
520 | Shows the entire stack of targets currently in use (including the exec-file,\n\ | |
521 | core-file, and process, if any), as well as the symbol file name."; | |
522 | ||
523 | void | |
524 | _initialize_targets () | |
525 | { | |
526 | current_target = &dummy_target; | |
527 | cleanup_target (current_target); | |
528 | ||
bd5635a1 RP |
529 | add_info ("target", target_info, targ_desc); |
530 | add_info ("files", target_info, targ_desc); | |
bd5635a1 | 531 | } |