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