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();
35 static void cleanup_target ();
37 /* Pointer to array of target architecture structures; the size of the
38 array; the current index into the array; the allocated size of the
40 struct target_ops
**target_structs
;
41 unsigned target_struct_size
;
42 unsigned target_struct_index
;
43 unsigned target_struct_allocsize
;
44 #define DEFAULT_ALLOCSIZE 10
46 /* The initial current target, so that there is always a semi-valid
49 struct target_ops dummy_target
= {"None", "None",
50 0, 0, 0, 0, /* open, close, attach, detach */
51 0, 0, /* resume, wait */
52 0, 0, 0, 0, 0, /* registers */
55 0, 0, 0, 0, 0, /* terminal */
56 0, 0, 0, /* kill, load, add_syms */
57 0, 0, /* call_function, lookup_symbol */
58 0, 0, /* create_inferior, mourn_inferior */
59 dummy_stratum
, 0, /* stratum, next */
60 0, 0, 0, 0, 0, /* all mem, mem, stack, regs, exec */
64 /* The target structure we are currently using to talk to a process
65 or file or whatever "inferior" we have. */
67 struct target_ops
*current_target
;
69 /* The stack of target structures that have been pushed. */
71 struct target_ops
**current_target_stack
;
74 /* Add a possible target architecture to the list. */
80 if (t
->to_magic
!= OPS_MAGIC
)
82 fprintf(stderr
, "Magic number of %s target struct wrong\n",
89 target_struct_allocsize
= DEFAULT_ALLOCSIZE
;
90 target_structs
= (struct target_ops
**) xmalloc
91 (target_struct_allocsize
* sizeof (*target_structs
));
93 if (target_struct_size
>= target_struct_allocsize
)
95 target_struct_allocsize
*= 2;
96 target_structs
= (struct target_ops
**) xrealloc (target_structs
,
97 target_struct_allocsize
* sizeof (*target_structs
));
99 target_structs
[target_struct_size
++] = t
;
112 nomemory (memaddr
, myaddr
, len
, write
)
118 return 0; /* No bytes handled */
124 error ("You can't do that when your target is `%s'",
125 current_target
->to_shortname
);
131 error ("You can't do that without a process to debug");
135 nosymbol (name
, addrp
)
139 return 1; /* Symbol does not exist in target env */
143 default_terminal_info (args
, from_tty
)
147 printf("No saved terminal information.\n");
151 /* With strata, this function is no longer needed. FIXME. */
152 /* This is the default target_create_inferior function. It looks up
153 the stack for some target that cares to create inferiors, then
154 calls it -- or complains if not found. */
157 upstack_create_inferior (exec
, args
, env
)
162 struct target_ops
*t
;
164 for (t
= current_target
;
168 if (t
->to_create_inferior
!= upstack_create_inferior
)
170 t
->to_create_inferior (exec
, args
, env
);
179 /* This is the default target_create_inferior and target_attach function.
180 If the current target is executing, it asks whether to kill it off.
181 If this function returns without calling error(), it has killed off
182 the target, and the operation should be attempted. */
185 kill_or_be_killed (from_tty
)
188 struct target_ops
*savecur
;
190 if (target_has_execution
)
192 printf ("You are already running a program:\n");
193 target_files_info ();
194 if (query ("Kill it? ")) {
195 savecur
= current_target
;
196 target_kill (0, from_tty
);
197 if (target_has_execution
)
198 error ("Killing the program did not help.");
201 error ("Program not killed.");
208 maybe_kill_then_attach (args
, from_tty
)
212 kill_or_be_killed (from_tty
);
213 target_attach (args
, from_tty
);
217 maybe_kill_then_create_inferior (exec
, args
, env
)
222 kill_or_be_killed (0);
223 target_create_inferior (exec
, args
, env
);
226 /* Clean up a target struct so it no longer has any zero pointers in it.
227 We default entries, at least to stubs that print error messages. */
231 struct target_ops
*t
;
234 /* Check magic number. If wrong, it probably means someone changed
235 the struct definition, but not all the places that initialize one. */
236 if (t
->to_magic
!= OPS_MAGIC
)
238 fprintf(stderr
, "Magic number of %s target struct wrong\n",
243 #define de_fault(field, value) \
244 if (!t->field) t->field = value
246 /* FIELD DEFAULT VALUE */
248 de_fault (to_open
, tcomplain
);
249 de_fault (to_close
, (void (*)())ignore
);
250 de_fault (to_attach
, maybe_kill_then_attach
);
251 de_fault (to_detach
, (void (*)())ignore
);
252 de_fault (to_resume
, (void (*)())noprocess
);
253 de_fault (to_wait
, noprocess
);
254 de_fault (to_fetch_registers
, noprocess
);
255 de_fault (to_store_registers
, noprocess
);
256 de_fault (to_prepare_to_store
, (void (*)())noprocess
);
257 de_fault (to_convert_to_virtual
, host_convert_to_virtual
);
258 de_fault (to_convert_from_virtual
, host_convert_from_virtual
);
259 de_fault (to_xfer_memory
, nomemory
);
260 de_fault (to_files_info
, ignore
);
261 de_fault (to_insert_breakpoint
, memory_insert_breakpoint
);
262 de_fault (to_remove_breakpoint
, memory_remove_breakpoint
);
263 de_fault (to_terminal_init
, ignore
);
264 de_fault (to_terminal_inferior
, ignore
);
265 de_fault (to_terminal_ours_for_output
,ignore
);
266 de_fault (to_terminal_ours
, ignore
);
267 de_fault (to_terminal_info
, default_terminal_info
);
268 de_fault (to_kill
, (void (*)())noprocess
);
269 de_fault (to_load
, tcomplain
);
270 de_fault (to_add_syms
, tcomplain
);
271 de_fault (to_call_function
, (struct value
*(*)())noprocess
);
272 de_fault (to_lookup_symbol
, nosymbol
);
273 de_fault (to_create_inferior
, maybe_kill_then_create_inferior
);
274 de_fault (to_mourn_inferior
, (void (*)())noprocess
);
275 de_fault (to_next
, 0);
276 de_fault (to_has_all_memory
, 0);
277 de_fault (to_has_memory
, 0);
278 de_fault (to_has_stack
, 0);
279 de_fault (to_has_registers
, 0);
280 de_fault (to_has_execution
, 0);
285 /* Push a new target type into the stack of the existing target accessors,
286 possibly superseding some of the existing accessors.
288 Result is zero if the pushed target ended up on top of the stack,
289 nonzero if at least one target is on top of it.
291 Rather than allow an empty stack, we always have the dummy target at
292 the bottom stratum, so we can call the function vectors without
297 struct target_ops
*t
;
299 struct target_ops
*st
, *prev
;
301 for (prev
= 0, st
= current_target
;
303 prev
= st
, st
= st
->to_next
) {
304 if ((int)(t
->to_stratum
) >= (int)(st
->to_stratum
))
308 while (t
->to_stratum
== st
->to_stratum
) {
309 /* There's already something on this stratum. Close it off. */
312 prev
->to_next
= st
->to_next
; /* Unchain old target_ops */
314 current_target
= st
->to_next
; /* Unchain first on list */
318 /* We have removed all targets in our stratum, now add ourself. */
325 cleanup_target (current_target
);
329 /* Remove a target_ops vector from the stack, wherever it may be.
330 Return how many times it was removed (0 or 1 unless bug). */
334 struct target_ops
*t
;
336 struct target_ops
*u
, *v
;
339 for (u
= current_target
, v
= 0;
341 v
= u
, u
= u
->to_next
)
345 pop_target(); /* unchain top copy */
347 (t
->to_close
)(0); /* Let it clean up */
348 v
->to_next
= t
->to_next
; /* unchain middle copy */
358 (current_target
->to_close
)(0); /* Let it clean up */
359 current_target
= current_target
->to_next
;
360 if (!current_target
) /* At bottom, push dummy. */
361 push_target (&dummy_target
);
364 /* Print things about the whole set of targets and about the
365 current target stack. */
371 printf("Possible targets:\n\n");
372 for (i
= 0; i
< target_struct_size
; i
++)
373 printf ("%-15s %s\n",
374 target_structs
[i
]->to_shortname
,
375 target_structs
[i
]->to_longname
);
378 /* Move memory to or from the targets. Iterate until all of it has
379 been moved, if necessary. The top target gets priority; anything
380 it doesn't want, is offered to the next one down, etc. Note the
381 business with curlen: if an early target says "no, but I have a
382 boundary overlapping this xfer" then we shorten what we offer to
383 the subsequent targets so the early guy will get a chance at the
384 tail before the subsequent ones do.
386 Result is 0 or errno value. */
389 target_read_memory (memaddr
, myaddr
, len
)
394 return target_xfer_memory (memaddr
, myaddr
, len
, 0);
398 target_write_memory (memaddr
, myaddr
, len
)
403 return target_xfer_memory (memaddr
, myaddr
, len
, 1);
407 target_xfer_memory (memaddr
, myaddr
, len
, write
)
415 struct target_ops
*t
;
417 /* The quick case is that the top target does it all. */
418 res
= current_target
->to_xfer_memory(memaddr
, myaddr
, len
, write
);
424 /* If res <= 0 then we call it again in the loop. Ah well. */
428 curlen
= len
; /* Want to do it all */
429 for (t
= current_target
;
431 t
= t
->to_has_all_memory
? 0: t
->to_next
)
433 res
= t
->to_xfer_memory(memaddr
, myaddr
, curlen
, write
);
434 if (res
> 0) break; /* Handled all or part of xfer */
435 if (res
== 0) continue; /* Handled none */
436 curlen
= -res
; /* Could handle once we get past res bytes */
440 /* If this address is for nonexistent memory,
441 read zeros if reading, or do nothing if writing. Return error. */
451 return 0; /* We managed to cover it all somehow. */
456 target_info (args
, from_tty
)
460 struct target_ops
*t
;
464 printf ("Symbols from \"%s\".\n", symfile
);
466 #ifdef FILES_INFO_HOOK
467 if (FILES_INFO_HOOK ())
471 for (t
= current_target
;
475 if ((int)(t
->to_stratum
) <= (int)dummy_stratum
)
478 printf("\tWhile running this, gdb does not access memory from...\n");
479 printf("%s:\n", t
->to_longname
);
480 (t
->to_files_info
)();
481 has_all_mem
= t
->to_has_all_memory
;
485 /* The target command selects a target and calls its open routine.
486 The open routine takes the rest of the parameters from the command,
487 and (if successful) pushes a new target onto the stack. */
490 target_command (args
, from_tty
)
501 "Argument required (target name). `info targets' lists possible targets");
503 if (target_has_execution
)
505 if (query ("A program is being debugged already. Kill it? "))
506 target_kill ((char *)0, from_tty
);
508 error ("Program not killed.");
511 /* Skip to first space, or end of args */
512 for (rest
= args
; *rest
&& !isspace(*rest
); rest
++) ;
514 rest
= 0; /* Only one word in args */
517 *rest
= '\0'; /* Terminate first word, scan for next */
518 for (rest
++; isspace (*rest
); rest
++) ;
519 if (*rest
== '\0') /* Only one word w/trailing blanks */
523 /* Search target list for a match */
525 for (i
= 0; i
< target_struct_size
; i
++)
527 if (!strcmp (args
, target_structs
[i
]->to_shortname
))
530 error ("No such target. `info targets' will list all targets");
533 (*target_structs
[i
]->to_open
) (rest
, from_tty
);
536 static char targ_desc
[] =
537 "Names of targets and files being debugged.\n\
538 Shows the entire stack of targets currently in use (including the exec-file,\n\
539 core-file, and process, if any), as well as the symbol file name.";
542 _initialize_targets ()
544 current_target
= &dummy_target
;
545 cleanup_target (current_target
);
547 add_info ("targets", targets_info
,
548 "Names of all possible targets.\n\
549 A target is typically a protocol for talking to debugging facilities;\n\
550 for example, `child' for Unix child processes, or `vxworks' for a\n\
551 TCP/IP link to a VxWorks system.");
553 add_info ("target", target_info
, targ_desc
);
554 add_info ("files", target_info
, targ_desc
);
556 add_com ("target", class_run
, target_command
,
557 "Connect to a target machine or process.\n\
558 The first argument is the type or protocol of the target machine. Remaining\n\
559 arguments are interpreted by the target protocol, but typically include\n\
560 things like device names or host names to connect with, process numbers,\n\
561 baud rates, etc. You can list all possible targets with the `info targets'\n\