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. */
34 extern int memory_insert_breakpoint(), memory_remove_breakpoint();
35 extern void host_convert_to_virtual(), host_convert_from_virtual();
37 static void cleanup_target ();
39 /* Pointer to array of target architecture structures; the size of the
40 array; the current index into the array; the allocated size of the
42 struct target_ops
**target_structs
;
43 unsigned target_struct_size
;
44 unsigned target_struct_index
;
45 unsigned target_struct_allocsize
;
46 #define DEFAULT_ALLOCSIZE 10
48 /* The initial current target, so that there is always a semi-valid
51 struct target_ops dummy_target
= {"None", "None", "",
52 0, 0, 0, 0, /* open, close, attach, detach */
53 0, 0, /* resume, wait */
54 0, 0, 0, 0, 0, /* registers */
57 0, 0, 0, 0, 0, /* terminal */
58 0, 0, /* kill, load */
59 0, /* 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 0, 0, /* section pointers */
67 /* The target structure we are currently using to talk to a process
68 or file or whatever "inferior" we have. */
70 struct target_ops
*current_target
;
72 /* The stack of target structures that have been pushed. */
74 struct target_ops
**current_target_stack
;
76 /* Command list for target. */
78 static struct cmd_list_element
*targetlist
= NULL
;
80 /* The user just typed 'target' without the name of a target. */
84 target_command (arg
, from_tty
)
88 fputs_filtered ("Argument required (target name).\n", stdout
);
91 /* Add a possible target architecture to the list. */
97 if (t
->to_magic
!= OPS_MAGIC
)
99 fprintf(stderr
, "Magic number of %s target struct wrong\n",
106 target_struct_allocsize
= DEFAULT_ALLOCSIZE
;
107 target_structs
= (struct target_ops
**) xmalloc
108 (target_struct_allocsize
* sizeof (*target_structs
));
110 if (target_struct_size
>= target_struct_allocsize
)
112 target_struct_allocsize
*= 2;
113 target_structs
= (struct target_ops
**) xrealloc (target_structs
,
114 target_struct_allocsize
* sizeof (*target_structs
));
116 target_structs
[target_struct_size
++] = t
;
119 if (targetlist
== NULL
)
120 add_prefix_cmd ("target", class_run
, target_command
,
121 "Connect to a target machine or process.\n\
122 The first argument is the type or protocol of the target machine.\n\
123 Remaining arguments are interpreted by the target protocol. For more\n\
124 information on the arguments for a particular protocol, type\n\
125 `help target ' followed by the protocol name.",
126 &targetlist
, "target ", 0, &cmdlist
);
127 add_cmd (t
->to_shortname
, no_class
, t
->to_open
, t
->to_doc
, &targetlist
);
139 nomemory (memaddr
, myaddr
, len
, write
)
145 return 0; /* No bytes handled */
151 error ("You can't do that when your target is `%s'",
152 current_target
->to_shortname
);
158 error ("You can't do that without a process to debug");
163 nosymbol (name
, addrp
)
167 return 1; /* Symbol does not exist in target env */
172 default_terminal_info (args
, from_tty
)
176 printf("No saved terminal information.\n");
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. */
186 upstack_create_inferior (exec
, args
, env
)
191 struct target_ops
*t
;
193 for (t
= current_target
;
197 if (t
->to_create_inferior
!= upstack_create_inferior
)
199 t
->to_create_inferior (exec
, args
, env
);
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. */
214 kill_or_be_killed (from_tty
)
217 if (target_has_execution
)
219 printf ("You are already running a program:\n");
220 target_files_info ();
221 if (query ("Kill it? ")) {
223 if (target_has_execution
)
224 error ("Killing the program did not help.");
227 error ("Program not killed.");
234 maybe_kill_then_attach (args
, from_tty
)
238 kill_or_be_killed (from_tty
);
239 target_attach (args
, from_tty
);
243 maybe_kill_then_create_inferior (exec
, args
, env
)
248 kill_or_be_killed (0);
249 target_create_inferior (exec
, args
, env
);
252 /* Clean up a target struct so it no longer has any zero pointers in it.
253 We default entries, at least to stubs that print error messages. */
257 struct target_ops
*t
;
260 /* Check magic number. If wrong, it probably means someone changed
261 the struct definition, but not all the places that initialize one. */
262 if (t
->to_magic
!= OPS_MAGIC
)
264 fprintf(stderr
, "Magic number of %s target struct wrong\n",
269 #define de_fault(field, value) \
270 if (!t->field) t->field = value
272 /* FIELD DEFAULT VALUE */
274 de_fault (to_open
, tcomplain
);
275 de_fault (to_close
, (void (*)())ignore
);
276 de_fault (to_attach
, maybe_kill_then_attach
);
277 de_fault (to_detach
, (void (*)())ignore
);
278 de_fault (to_resume
, (void (*)())noprocess
);
279 de_fault (to_wait
, noprocess
);
280 de_fault (to_fetch_registers
, ignore
);
281 de_fault (to_store_registers
, (void (*)())noprocess
);
282 de_fault (to_prepare_to_store
, (void (*)())noprocess
);
283 de_fault (to_convert_to_virtual
, host_convert_to_virtual
);
284 de_fault (to_convert_from_virtual
, host_convert_from_virtual
);
285 de_fault (to_xfer_memory
, nomemory
);
286 de_fault (to_files_info
, ignore
);
287 de_fault (to_insert_breakpoint
, memory_insert_breakpoint
);
288 de_fault (to_remove_breakpoint
, memory_remove_breakpoint
);
289 de_fault (to_terminal_init
, ignore
);
290 de_fault (to_terminal_inferior
, ignore
);
291 de_fault (to_terminal_ours_for_output
,ignore
);
292 de_fault (to_terminal_ours
, ignore
);
293 de_fault (to_terminal_info
, default_terminal_info
);
294 de_fault (to_kill
, (void (*)())noprocess
);
295 de_fault (to_load
, tcomplain
);
296 de_fault (to_lookup_symbol
, nosymbol
);
297 de_fault (to_create_inferior
, maybe_kill_then_create_inferior
);
298 de_fault (to_mourn_inferior
, (void (*)())noprocess
);
299 de_fault (to_next
, 0);
300 de_fault (to_has_all_memory
, 0);
301 de_fault (to_has_memory
, 0);
302 de_fault (to_has_stack
, 0);
303 de_fault (to_has_registers
, 0);
304 de_fault (to_has_execution
, 0);
309 /* Push a new target type into the stack of the existing target accessors,
310 possibly superseding some of the existing accessors.
312 Result is zero if the pushed target ended up on top of the stack,
313 nonzero if at least one target is on top of it.
315 Rather than allow an empty stack, we always have the dummy target at
316 the bottom stratum, so we can call the function vectors without
321 struct target_ops
*t
;
323 struct target_ops
*st
, *prev
;
325 for (prev
= 0, st
= current_target
;
327 prev
= st
, st
= st
->to_next
) {
328 if ((int)(t
->to_stratum
) >= (int)(st
->to_stratum
))
332 while (t
->to_stratum
== st
->to_stratum
) {
333 /* There's already something on this stratum. Close it off. */
336 prev
->to_next
= st
->to_next
; /* Unchain old target_ops */
338 current_target
= st
->to_next
; /* Unchain first on list */
342 /* We have removed all targets in our stratum, now add ourself. */
349 cleanup_target (current_target
);
353 /* Remove a target_ops vector from the stack, wherever it may be.
354 Return how many times it was removed (0 or 1 unless bug). */
358 struct target_ops
*t
;
360 struct target_ops
*u
, *v
;
363 for (u
= current_target
, v
= 0;
365 v
= u
, u
= u
->to_next
)
369 pop_target(); /* unchain top copy */
371 (t
->to_close
)(0); /* Let it clean up */
372 v
->to_next
= t
->to_next
; /* unchain middle copy */
382 (current_target
->to_close
)(0); /* Let it clean up */
383 current_target
= current_target
->to_next
;
384 if (!current_target
) /* At bottom, push dummy. */
385 push_target (&dummy_target
);
388 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
390 /* target_read_string -- read a null terminated string from MEMADDR in target.
391 The read may also be terminated early by getting an error from target_xfer_
393 LEN is the size of the buffer pointed to by MYADDR. Note that a terminating
394 null will only be written if there is sufficient room. The return value is
395 is the number of bytes (including the null) actually transferred.
399 target_read_string (memaddr
, myaddr
, len
)
404 int tlen
, origlen
, offset
, i
;
411 tlen
= MIN (len
, 4 - (memaddr
& 3));
412 offset
= memaddr
& 3;
414 if (target_xfer_memory (memaddr
& ~3, buf
, 4, 0))
415 return origlen
- len
;
417 for (i
= 0; i
< tlen
; i
++)
419 *myaddr
++ = buf
[i
+ offset
];
420 if (buf
[i
+ offset
] == '\000')
421 return (origlen
- len
) + i
+ 1;
430 /* Move memory to or from the targets. Iterate until all of it has
431 been moved, if necessary. The top target gets priority; anything
432 it doesn't want, is offered to the next one down, etc. Note the
433 business with curlen: if an early target says "no, but I have a
434 boundary overlapping this xfer" then we shorten what we offer to
435 the subsequent targets so the early guy will get a chance at the
436 tail before the subsequent ones do.
438 Result is 0 or errno value. */
441 target_read_memory (memaddr
, myaddr
, len
)
446 return target_xfer_memory (memaddr
, myaddr
, len
, 0);
450 target_write_memory (memaddr
, myaddr
, len
)
455 return target_xfer_memory (memaddr
, myaddr
, len
, 1);
459 target_xfer_memory (memaddr
, myaddr
, len
, write
)
467 struct target_ops
*t
;
469 /* The quick case is that the top target does it all. */
470 res
= current_target
->to_xfer_memory
471 (memaddr
, myaddr
, len
, write
, current_target
);
477 /* If res <= 0 then we call it again in the loop. Ah well. */
481 curlen
= len
; /* Want to do it all */
482 for (t
= current_target
;
484 t
= t
->to_has_all_memory
? 0: t
->to_next
)
486 res
= t
->to_xfer_memory(memaddr
, myaddr
, curlen
, write
, t
);
487 if (res
> 0) break; /* Handled all or part of xfer */
488 if (res
== 0) continue; /* Handled none */
489 curlen
= -res
; /* Could handle once we get past res bytes */
493 /* If this address is for nonexistent memory,
494 read zeros if reading, or do nothing if writing. Return error. */
507 return 0; /* We managed to cover it all somehow. */
513 target_info (args
, from_tty
)
517 struct target_ops
*t
;
520 if (symfile_objfile
!= 0)
521 printf ("Symbols from \"%s\".\n", symfile_objfile
->name
);
523 #ifdef FILES_INFO_HOOK
524 if (FILES_INFO_HOOK ())
528 for (t
= current_target
;
532 if ((int)(t
->to_stratum
) <= (int)dummy_stratum
)
535 printf("\tWhile running this, gdb does not access memory from...\n");
536 printf("%s:\n", t
->to_longname
);
537 (t
->to_files_info
)(t
);
538 has_all_mem
= t
->to_has_all_memory
;
542 /* This is to be called by the open routine before it does
546 target_preopen (from_tty
)
551 if (target_has_execution
)
553 if (query ("A program is being debugged already. Kill it? "))
556 error ("Program not killed.");
560 static char targ_desc
[] =
561 "Names of targets and files being debugged.\n\
562 Shows the entire stack of targets currently in use (including the exec-file,\n\
563 core-file, and process, if any), as well as the symbol file name.";
566 _initialize_targets ()
568 current_target
= &dummy_target
;
569 cleanup_target (current_target
);
571 add_info ("target", target_info
, targ_desc
);
572 add_info ("files", target_info
, targ_desc
);