1 /* Select target systems and architectures at runtime for GDB.
2 Copyright (C) 1990 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB.
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)
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.
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. */
32 extern int memory_insert_breakpoint(), memory_remove_breakpoint();
33 extern void host_convert_to_virtual(), host_convert_from_virtual();
34 extern void add_syms_addr_command();
36 static void cleanup_target ();
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
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
47 /* The initial current target, so that there is always a semi-valid
50 struct target_ops dummy_target = {"None", "None", "",
51 0, 0, 0, 0, /* open, close, attach, detach */
52 0, 0, /* resume, wait */
53 0, 0, 0, 0, 0, /* registers */
56 0, 0, 0, 0, 0, /* terminal */
57 0, 0, /* kill, load */
58 add_syms_addr_command, /* add_syms */
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 */
66 /* The target structure we are currently using to talk to a process
67 or file or whatever "inferior" we have. */
69 struct target_ops *current_target;
71 /* The stack of target structures that have been pushed. */
73 struct target_ops **current_target_stack;
75 /* Command list for target. */
77 static struct cmd_list_element *targetlist = NULL;
79 /* The user just typed 'target' without the name of a target. */
83 target_command (arg, from_tty)
87 fputs_filtered ("Argument required (target name).\n", stdout);
90 /* Add a possible target architecture to the list. */
96 if (t->to_magic != OPS_MAGIC)
98 fprintf(stderr, "Magic number of %s target struct wrong\n",
105 target_struct_allocsize = DEFAULT_ALLOCSIZE;
106 target_structs = (struct target_ops **) xmalloc
107 (target_struct_allocsize * sizeof (*target_structs));
109 if (target_struct_size >= target_struct_allocsize)
111 target_struct_allocsize *= 2;
112 target_structs = (struct target_ops **) xrealloc (target_structs,
113 target_struct_allocsize * sizeof (*target_structs));
115 target_structs[target_struct_size++] = t;
118 if (targetlist == NULL)
119 add_prefix_cmd ("target", class_run, target_command,
120 "Connect to a target machine or process.\n\
121 The first argument is the type or protocol of the target machine.\n\
122 Remaining arguments are interpreted by the target protocol. For more\n\
123 information on the arguments for a particular protocol, type\n\
124 `help target ' followed by the protocol name.",
125 &targetlist, "target ", 0, &cmdlist);
126 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
138 nomemory (memaddr, myaddr, len, write)
144 return 0; /* No bytes handled */
150 error ("You can't do that when your target is `%s'",
151 current_target->to_shortname);
157 error ("You can't do that without a process to debug");
162 nosymbol (name, addrp)
166 return 1; /* Symbol does not exist in target env */
171 default_terminal_info (args, from_tty)
175 printf("No saved terminal information.\n");
179 /* With strata, this function is no longer needed. FIXME. */
180 /* This is the default target_create_inferior function. It looks up
181 the stack for some target that cares to create inferiors, then
182 calls it -- or complains if not found. */
185 upstack_create_inferior (exec, args, env)
190 struct target_ops *t;
192 for (t = current_target;
196 if (t->to_create_inferior != upstack_create_inferior)
198 t->to_create_inferior (exec, args, env);
207 /* This is the default target_create_inferior and target_attach function.
208 If the current target is executing, it asks whether to kill it off.
209 If this function returns without calling error(), it has killed off
210 the target, and the operation should be attempted. */
213 kill_or_be_killed (from_tty)
216 /* FIXME: What is savecur for? Why isn't it used? */
217 struct target_ops *savecur;
219 if (target_has_execution)
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.");
230 error ("Program not killed.");
237 maybe_kill_then_attach (args, from_tty)
241 kill_or_be_killed (from_tty);
242 target_attach (args, from_tty);
246 maybe_kill_then_create_inferior (exec, args, env)
251 kill_or_be_killed (0);
252 target_create_inferior (exec, args, env);
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. */
260 struct target_ops *t;
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)
267 fprintf(stderr, "Magic number of %s target struct wrong\n",
272 #define de_fault(field, value) \
273 if (!t->field) t->field = value
275 /* FIELD DEFAULT VALUE */
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);
314 /* Push a new target type into the stack of the existing target accessors,
315 possibly superseding some of the existing accessors.
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.
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
326 struct target_ops *t;
328 struct target_ops *st, *prev;
330 for (prev = 0, st = current_target;
332 prev = st, st = st->to_next) {
333 if ((int)(t->to_stratum) >= (int)(st->to_stratum))
337 while (t->to_stratum == st->to_stratum) {
338 /* There's already something on this stratum. Close it off. */
341 prev->to_next = st->to_next; /* Unchain old target_ops */
343 current_target = st->to_next; /* Unchain first on list */
347 /* We have removed all targets in our stratum, now add ourself. */
354 cleanup_target (current_target);
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). */
363 struct target_ops *t;
365 struct target_ops *u, *v;
368 for (u = current_target, v = 0;
370 v = u, u = u->to_next)
374 pop_target(); /* unchain top copy */
376 (t->to_close)(0); /* Let it clean up */
377 v->to_next = t->to_next; /* unchain middle copy */
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);
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.
401 Result is 0 or errno value. */
404 target_read_memory (memaddr, myaddr, len)
409 return target_xfer_memory (memaddr, myaddr, len, 0);
413 target_write_memory (memaddr, myaddr, len)
418 return target_xfer_memory (memaddr, myaddr, len, 1);
422 target_xfer_memory (memaddr, myaddr, len, write)
430 struct target_ops *t;
432 /* The quick case is that the top target does it all. */
433 res = current_target->to_xfer_memory(memaddr, myaddr, len, write);
439 /* If res <= 0 then we call it again in the loop. Ah well. */
443 curlen = len; /* Want to do it all */
444 for (t = current_target;
446 t = t->to_has_all_memory? 0: t->to_next)
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 */
455 /* If this address is for nonexistent memory,
456 read zeros if reading, or do nothing if writing. Return error. */
466 return 0; /* We managed to cover it all somehow. */
472 target_info (args, from_tty)
476 struct target_ops *t;
480 printf ("Symbols from \"%s\".\n", symfile);
482 #ifdef FILES_INFO_HOOK
483 if (FILES_INFO_HOOK ())
487 for (t = current_target;
491 if ((int)(t->to_stratum) <= (int)dummy_stratum)
494 printf("\tWhile running this, gdb does not access memory from...\n");
495 printf("%s:\n", t->to_longname);
496 (t->to_files_info)();
497 has_all_mem = t->to_has_all_memory;
501 /* This is to be called by the open routine before it does
505 target_preopen (from_tty)
510 if (target_has_execution)
512 if (query ("A program is being debugged already. Kill it? "))
513 target_kill ((char *)0, from_tty);
515 error ("Program not killed.");
519 static char targ_desc[] =
520 "Names of targets and files being debugged.\n\
521 Shows the entire stack of targets currently in use (including the exec-file,\n\
522 core-file, and process, if any), as well as the symbol file name.";
525 _initialize_targets ()
527 current_target = &dummy_target;
528 cleanup_target (current_target);
530 add_info ("target", target_info, targ_desc);
531 add_info ("files", target_info, targ_desc);