1 /* Select target systems and architectures at runtime for GDB.
2 Copyright 1990, 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB.
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 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
35 target_info
PARAMS ((char *, int));
38 cleanup_target
PARAMS ((struct target_ops
*));
41 maybe_kill_then_create_inferior
PARAMS ((char *, char *, char **));
44 maybe_kill_then_attach
PARAMS ((char *, int));
47 kill_or_be_killed
PARAMS ((int));
50 default_terminal_info
PARAMS ((char *, int));
53 nosymbol
PARAMS ((char *, CORE_ADDR
*));
56 tcomplain
PARAMS ((void));
59 nomemory
PARAMS ((CORE_ADDR
, char *, int, int));
62 return_zero
PARAMS ((void));
65 ignore
PARAMS ((void));
68 target_command
PARAMS ((char *, int));
70 static struct target_ops
*
71 find_default_run_target
PARAMS ((char *));
73 /* Pointer to array of target architecture structures; the size of the
74 array; the current index into the array; the allocated size of the
76 struct target_ops
**target_structs
;
77 unsigned target_struct_size
;
78 unsigned target_struct_index
;
79 unsigned target_struct_allocsize
;
80 #define DEFAULT_ALLOCSIZE 10
82 /* The initial current target, so that there is always a semi-valid
85 struct target_ops dummy_target
= {"None", "None", "",
86 0, 0, /* open, close */
87 find_default_attach
, 0, /* attach, detach */
88 0, 0, /* resume, wait */
89 0, 0, 0, /* registers */
92 0, 0, 0, 0, 0, /* terminal */
93 0, 0, /* kill, load */
94 0, /* lookup_symbol */
95 find_default_create_inferior
, /* create_inferior */
96 0, /* mourn_inferior */
98 dummy_stratum
, 0, /* stratum, next */
99 0, 0, 0, 0, 0, /* all mem, mem, stack, regs, exec */
100 0, 0, /* section pointers */
104 /* The target structure we are currently using to talk to a process
105 or file or whatever "inferior" we have. */
107 struct target_ops
*current_target
;
109 /* The stack of target structures that have been pushed. */
111 struct target_ops
**current_target_stack
;
113 /* Command list for target. */
115 static struct cmd_list_element
*targetlist
= NULL
;
117 /* The user just typed 'target' without the name of a target. */
121 target_command (arg
, from_tty
)
125 fputs_filtered ("Argument required (target name). Try `help target'\n",
129 /* Add a possible target architecture to the list. */
133 struct target_ops
*t
;
135 if (t
->to_magic
!= OPS_MAGIC
)
137 fprintf(stderr
, "Magic number of %s target struct wrong\n",
144 target_struct_allocsize
= DEFAULT_ALLOCSIZE
;
145 target_structs
= (struct target_ops
**) xmalloc
146 (target_struct_allocsize
* sizeof (*target_structs
));
148 if (target_struct_size
>= target_struct_allocsize
)
150 target_struct_allocsize
*= 2;
151 target_structs
= (struct target_ops
**)
152 xrealloc ((char *) target_structs
,
153 target_struct_allocsize
* sizeof (*target_structs
));
155 target_structs
[target_struct_size
++] = t
;
158 if (targetlist
== NULL
)
159 add_prefix_cmd ("target", class_run
, target_command
,
160 "Connect to a target machine or process.\n\
161 The first argument is the type or protocol of the target machine.\n\
162 Remaining arguments are interpreted by the target protocol. For more\n\
163 information on the arguments for a particular protocol, type\n\
164 `help target ' followed by the protocol name.",
165 &targetlist
, "target ", 0, &cmdlist
);
166 add_cmd (t
->to_shortname
, no_class
, t
->to_open
, t
->to_doc
, &targetlist
);
178 nomemory (memaddr
, myaddr
, len
, write
)
184 errno
= EIO
; /* Can't read/write this location */
185 return 0; /* No bytes handled */
191 error ("You can't do that when your target is `%s'",
192 current_target
->to_shortname
);
198 error ("You can't do that without a process to debug");
203 nosymbol (name
, addrp
)
207 return 1; /* Symbol does not exist in target env */
212 default_terminal_info (args
, from_tty
)
216 printf("No saved terminal information.\n");
220 /* With strata, this function is no longer needed. FIXME. */
221 /* This is the default target_create_inferior function. It looks up
222 the stack for some target that cares to create inferiors, then
223 calls it -- or complains if not found. */
226 upstack_create_inferior (exec
, args
, env
)
231 struct target_ops
*t
;
233 for (t
= current_target
;
237 if (t
->to_create_inferior
!= upstack_create_inferior
)
239 t
->to_create_inferior (exec
, args
, env
);
248 /* This is the default target_create_inferior and target_attach function.
249 If the current target is executing, it asks whether to kill it off.
250 If this function returns without calling error(), it has killed off
251 the target, and the operation should be attempted. */
254 kill_or_be_killed (from_tty
)
257 if (target_has_execution
)
259 printf ("You are already running a program:\n");
260 target_files_info ();
261 if (query ("Kill it? ")) {
263 if (target_has_execution
)
264 error ("Killing the program did not help.");
267 error ("Program not killed.");
274 maybe_kill_then_attach (args
, from_tty
)
278 kill_or_be_killed (from_tty
);
279 target_attach (args
, from_tty
);
283 maybe_kill_then_create_inferior (exec
, args
, env
)
288 kill_or_be_killed (0);
289 target_create_inferior (exec
, args
, env
);
292 /* Clean up a target struct so it no longer has any zero pointers in it.
293 We default entries, at least to stubs that print error messages. */
297 struct target_ops
*t
;
300 /* Check magic number. If wrong, it probably means someone changed
301 the struct definition, but not all the places that initialize one. */
302 if (t
->to_magic
!= OPS_MAGIC
)
304 fprintf(stderr
, "Magic number of %s target struct wrong\n",
309 #define de_fault(field, value) \
310 if (!t->field) t->field = value
312 /* FIELD DEFAULT VALUE */
314 de_fault (to_open
, (void (*)())tcomplain
);
315 de_fault (to_close
, (void (*)())ignore
);
316 de_fault (to_attach
, maybe_kill_then_attach
);
317 de_fault (to_detach
, (void (*)())ignore
);
318 de_fault (to_resume
, (void (*)())noprocess
);
319 de_fault (to_wait
, (int (*)())noprocess
);
320 de_fault (to_fetch_registers
, (void (*)())ignore
);
321 de_fault (to_store_registers
, (void (*)())noprocess
);
322 de_fault (to_prepare_to_store
, (void (*)())noprocess
);
323 de_fault (to_xfer_memory
, (int (*)())nomemory
);
324 de_fault (to_files_info
, (void (*)())ignore
);
325 de_fault (to_insert_breakpoint
, memory_insert_breakpoint
);
326 de_fault (to_remove_breakpoint
, memory_remove_breakpoint
);
327 de_fault (to_terminal_init
, ignore
);
328 de_fault (to_terminal_inferior
, ignore
);
329 de_fault (to_terminal_ours_for_output
,ignore
);
330 de_fault (to_terminal_ours
, ignore
);
331 de_fault (to_terminal_info
, default_terminal_info
);
332 de_fault (to_kill
, (void (*)())noprocess
);
333 de_fault (to_load
, (void (*)())tcomplain
);
334 de_fault (to_lookup_symbol
, nosymbol
);
335 de_fault (to_create_inferior
, maybe_kill_then_create_inferior
);
336 de_fault (to_mourn_inferior
, (void (*)())noprocess
);
337 de_fault (to_can_run
, return_zero
);
338 de_fault (to_next
, 0);
339 de_fault (to_has_all_memory
, 0);
340 de_fault (to_has_memory
, 0);
341 de_fault (to_has_stack
, 0);
342 de_fault (to_has_registers
, 0);
343 de_fault (to_has_execution
, 0);
348 /* Push a new target type into the stack of the existing target accessors,
349 possibly superseding some of the existing accessors.
351 Result is zero if the pushed target ended up on top of the stack,
352 nonzero if at least one target is on top of it.
354 Rather than allow an empty stack, we always have the dummy target at
355 the bottom stratum, so we can call the function vectors without
360 struct target_ops
*t
;
362 struct target_ops
*st
, *prev
;
364 for (prev
= 0, st
= current_target
;
366 prev
= st
, st
= st
->to_next
) {
367 if ((int)(t
->to_stratum
) >= (int)(st
->to_stratum
))
371 while (t
->to_stratum
== st
->to_stratum
) {
372 /* There's already something on this stratum. Close it off. */
375 prev
->to_next
= st
->to_next
; /* Unchain old target_ops */
377 current_target
= st
->to_next
; /* Unchain first on list */
381 /* We have removed all targets in our stratum, now add ourself. */
388 cleanup_target (current_target
);
392 /* Remove a target_ops vector from the stack, wherever it may be.
393 Return how many times it was removed (0 or 1 unless bug). */
397 struct target_ops
*t
;
399 struct target_ops
*u
, *v
;
402 for (u
= current_target
, v
= 0;
404 v
= u
, u
= u
->to_next
)
408 pop_target(); /* unchain top copy */
410 (t
->to_close
)(0); /* Let it clean up */
411 v
->to_next
= t
->to_next
; /* unchain middle copy */
421 (current_target
->to_close
)(0); /* Let it clean up */
422 current_target
= current_target
->to_next
;
423 if (!current_target
) /* At bottom, push dummy. */
424 push_target (&dummy_target
);
427 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
429 /* target_read_string -- read a null terminated string from MEMADDR in target.
430 The read may also be terminated early by getting an error from target_xfer_
432 LEN is the size of the buffer pointed to by MYADDR. Note that a terminating
433 null will only be written if there is sufficient room. The return value is
434 is the number of bytes (including the null) actually transferred.
438 target_read_string (memaddr
, myaddr
, len
)
443 int tlen
, origlen
, offset
, i
;
450 tlen
= MIN (len
, 4 - (memaddr
& 3));
451 offset
= memaddr
& 3;
453 if (target_xfer_memory (memaddr
& ~3, buf
, 4, 0))
454 return origlen
- len
;
456 for (i
= 0; i
< tlen
; i
++)
458 *myaddr
++ = buf
[i
+ offset
];
459 if (buf
[i
+ offset
] == '\000')
460 return (origlen
- len
) + i
+ 1;
469 /* Move memory to or from the targets. Iterate until all of it has
470 been moved, if necessary. The top target gets priority; anything
471 it doesn't want, is offered to the next one down, etc. Note the
472 business with curlen: if an early target says "no, but I have a
473 boundary overlapping this xfer" then we shorten what we offer to
474 the subsequent targets so the early guy will get a chance at the
475 tail before the subsequent ones do.
477 Result is 0 or errno value. */
480 target_read_memory (memaddr
, myaddr
, len
)
485 return target_xfer_memory (memaddr
, myaddr
, len
, 0);
489 target_write_memory (memaddr
, myaddr
, len
)
494 return target_xfer_memory (memaddr
, myaddr
, len
, 1);
498 target_xfer_memory (memaddr
, myaddr
, len
, write
)
506 struct target_ops
*t
;
508 /* The quick case is that the top target does it all. */
509 res
= current_target
->to_xfer_memory
510 (memaddr
, myaddr
, len
, write
, current_target
);
516 /* If res <= 0 then we call it again in the loop. Ah well. */
520 curlen
= len
; /* Want to do it all */
521 for (t
= current_target
;
523 t
= t
->to_has_all_memory
? 0: t
->to_next
)
525 res
= t
->to_xfer_memory(memaddr
, myaddr
, curlen
, write
, t
);
526 if (res
> 0) break; /* Handled all or part of xfer */
527 if (res
== 0) continue; /* Handled none */
528 curlen
= -res
; /* Could handle once we get past res bytes */
532 /* If this address is for nonexistent memory,
533 read zeros if reading, or do nothing if writing. Return error. */
535 memset (myaddr
, 0, len
);
546 return 0; /* We managed to cover it all somehow. */
552 target_info (args
, from_tty
)
556 struct target_ops
*t
;
559 if (symfile_objfile
!= NULL
)
560 printf ("Symbols from \"%s\".\n", symfile_objfile
->name
);
562 #ifdef FILES_INFO_HOOK
563 if (FILES_INFO_HOOK ())
567 for (t
= current_target
;
571 if ((int)(t
->to_stratum
) <= (int)dummy_stratum
)
574 printf("\tWhile running this, gdb does not access memory from...\n");
575 printf("%s:\n", t
->to_longname
);
576 (t
->to_files_info
)(t
);
577 has_all_mem
= t
->to_has_all_memory
;
581 /* This is to be called by the open routine before it does
585 target_preopen (from_tty
)
590 if (target_has_execution
)
592 if (query ("A program is being debugged already. Kill it? "))
595 error ("Program not killed.");
599 /* Look through the list of possible targets for a target that can
600 execute a run or attach command without any other data. This is
601 used to locate the default process stratum.
603 Result is always valid (error() is called for errors). */
605 static struct target_ops
*
606 find_default_run_target (do_mesg
)
609 struct target_ops
**t
;
610 struct target_ops
*runable
;
615 for (t
= target_structs
; t
< target_structs
+ target_struct_size
;
618 if (target_can_run(*t
))
626 error ("Don't know how to %s. Try \"help target\".", do_mesg
);
632 find_default_attach (args
, from_tty
)
636 struct target_ops
*t
;
638 t
= find_default_run_target("attach");
639 (t
->to_attach
) (args
, from_tty
);
644 find_default_create_inferior (exec_file
, allargs
, env
)
649 struct target_ops
*t
;
651 t
= find_default_run_target("run");
652 (t
->to_create_inferior
) (exec_file
, allargs
, env
);
662 static char targ_desc
[] =
663 "Names of targets and files being debugged.\n\
664 Shows the entire stack of targets currently in use (including the exec-file,\n\
665 core-file, and process, if any), as well as the symbol file name.";
668 _initialize_targets ()
670 current_target
= &dummy_target
;
671 cleanup_target (current_target
);
673 add_info ("target", target_info
, targ_desc
);
674 add_info ("files", target_info
, targ_desc
);