* stabs.texinfo (Negative Type Numbers): FORTRAN LOGICAL fix.
[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"
51b57ded 30#include "objfiles.h"
bd5635a1 31
e17960fb
JG
32extern int errno;
33
7919c3ed
JG
34static void
35target_info PARAMS ((char *, int));
36
37static void
38cleanup_target PARAMS ((struct target_ops *));
39
40static void
41maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
42
43static void
44maybe_kill_then_attach PARAMS ((char *, int));
45
46static void
47kill_or_be_killed PARAMS ((int));
48
49static void
50default_terminal_info PARAMS ((char *, int));
51
52static int
53nosymbol PARAMS ((char *, CORE_ADDR *));
54
7919c3ed
JG
55static void
56tcomplain PARAMS ((void));
57
58static int
59nomemory PARAMS ((CORE_ADDR, char *, int, int));
60
597dc86b
SG
61static int
62return_zero PARAMS ((void));
63
7919c3ed
JG
64static void
65ignore PARAMS ((void));
597dc86b 66
7919c3ed
JG
67static void
68target_command PARAMS ((char *, int));
bd5635a1 69
597dc86b
SG
70static struct target_ops *
71find_default_run_target PARAMS ((char *));
72
bd5635a1
RP
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
75 array. */
76struct target_ops **target_structs;
77unsigned target_struct_size;
78unsigned target_struct_index;
79unsigned target_struct_allocsize;
80#define DEFAULT_ALLOCSIZE 10
81
82/* The initial current target, so that there is always a semi-valid
83 current target. */
84
f2fc6e7a 85struct target_ops dummy_target = {"None", "None", "",
597dc86b
SG
86 0, 0, /* open, close */
87 find_default_attach, 0, /* attach, detach */
bd5635a1 88 0, 0, /* resume, wait */
dcc8abce 89 0, 0, 0, /* registers */
bd5635a1
RP
90 0, 0, /* memory */
91 0, 0, /* bkpts */
92 0, 0, 0, 0, 0, /* terminal */
fc47a10d 93 0, 0, /* kill, load */
e17960fb 94 0, /* lookup_symbol */
597dc86b
SG
95 find_default_create_inferior, /* create_inferior */
96 0, /* mourn_inferior */
97 0, /* can_run */
bd5635a1
RP
98 dummy_stratum, 0, /* stratum, next */
99 0, 0, 0, 0, 0, /* all mem, mem, stack, regs, exec */
e17960fb 100 0, 0, /* section pointers */
bd5635a1
RP
101 OPS_MAGIC,
102};
103
104/* The target structure we are currently using to talk to a process
105 or file or whatever "inferior" we have. */
106
107struct target_ops *current_target;
108
109/* The stack of target structures that have been pushed. */
110
111struct target_ops **current_target_stack;
112
f2fc6e7a
JK
113/* Command list for target. */
114
115static struct cmd_list_element *targetlist = NULL;
116
f2fc6e7a
JK
117/* The user just typed 'target' without the name of a target. */
118
e1ce8aa5 119/* ARGSUSED */
f2fc6e7a
JK
120static void
121target_command (arg, from_tty)
122 char *arg;
123 int from_tty;
124{
597dc86b
SG
125 fputs_filtered ("Argument required (target name). Try `help target'\n",
126 stdout);
f2fc6e7a 127}
bd5635a1
RP
128
129/* Add a possible target architecture to the list. */
130
131void
132add_target (t)
133 struct target_ops *t;
134{
135 if (t->to_magic != OPS_MAGIC)
136 {
597dc86b 137 fprintf(stderr, "Magic number of %s target struct wrong\n",
bd5635a1
RP
138 t->to_shortname);
139 abort();
140 }
141
142 if (!target_structs)
143 {
144 target_struct_allocsize = DEFAULT_ALLOCSIZE;
145 target_structs = (struct target_ops **) xmalloc
146 (target_struct_allocsize * sizeof (*target_structs));
147 }
148 if (target_struct_size >= target_struct_allocsize)
149 {
150 target_struct_allocsize *= 2;
7919c3ed
JG
151 target_structs = (struct target_ops **)
152 xrealloc ((char *) target_structs,
153 target_struct_allocsize * sizeof (*target_structs));
bd5635a1
RP
154 }
155 target_structs[target_struct_size++] = t;
156 cleanup_target (t);
f2fc6e7a
JK
157
158 if (targetlist == NULL)
159 add_prefix_cmd ("target", class_run, target_command,
160 "Connect to a target machine or process.\n\
161The first argument is the type or protocol of the target machine.\n\
162Remaining arguments are interpreted by the target protocol. For more\n\
163information 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);
bd5635a1
RP
167}
168
169/* Stub functions */
170
171static void
172ignore ()
173{
174}
175
176/* ARGSUSED */
177static int
178nomemory (memaddr, myaddr, len, write)
179 CORE_ADDR memaddr;
180 char *myaddr;
181 int len;
182 int write;
183{
51b57ded 184 errno = EIO; /* Can't read/write this location */
bd5635a1
RP
185 return 0; /* No bytes handled */
186}
187
188static void
189tcomplain ()
190{
191 error ("You can't do that when your target is `%s'",
192 current_target->to_shortname);
193}
194
80d68b1d 195void
bd5635a1
RP
196noprocess ()
197{
198 error ("You can't do that without a process to debug");
199}
200
e1ce8aa5 201/* ARGSUSED */
bd5635a1
RP
202static int
203nosymbol (name, addrp)
204 char *name;
205 CORE_ADDR *addrp;
206{
207 return 1; /* Symbol does not exist in target env */
208}
209
e1ce8aa5 210/* ARGSUSED */
bd5635a1
RP
211static void
212default_terminal_info (args, from_tty)
213 char *args;
214 int from_tty;
215{
597dc86b 216 printf("No saved terminal information.\n");
bd5635a1
RP
217}
218
219#if 0
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. */
224
225static void
226upstack_create_inferior (exec, args, env)
227 char *exec;
228 char *args;
229 char **env;
230{
231 struct target_ops *t;
232
233 for (t = current_target;
234 t;
235 t = t->to_next)
236 {
237 if (t->to_create_inferior != upstack_create_inferior)
238 {
239 t->to_create_inferior (exec, args, env);
240 return;
241 }
242
243 }
244 tcomplain();
245}
246#endif
247
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. */
252
253static void
254kill_or_be_killed (from_tty)
255 int from_tty;
256{
bd5635a1
RP
257 if (target_has_execution)
258 {
597dc86b 259 printf ("You are already running a program:\n");
bd5635a1
RP
260 target_files_info ();
261 if (query ("Kill it? ")) {
e17960fb 262 target_kill ();
bd5635a1
RP
263 if (target_has_execution)
264 error ("Killing the program did not help.");
265 return;
266 } else {
267 error ("Program not killed.");
268 }
269 }
270 tcomplain();
271}
272
273static void
274maybe_kill_then_attach (args, from_tty)
275 char *args;
276 int from_tty;
277{
278 kill_or_be_killed (from_tty);
279 target_attach (args, from_tty);
280}
281
282static void
283maybe_kill_then_create_inferior (exec, args, env)
284 char *exec;
285 char *args;
286 char **env;
287{
288 kill_or_be_killed (0);
289 target_create_inferior (exec, args, env);
290}
291
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. */
294
295static void
296cleanup_target (t)
297 struct target_ops *t;
298{
299
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)
303 {
597dc86b 304 fprintf(stderr, "Magic number of %s target struct wrong\n",
bd5635a1
RP
305 t->to_shortname);
306 abort();
307 }
308
309#define de_fault(field, value) \
310 if (!t->field) t->field = value
311
312 /* FIELD DEFAULT VALUE */
313
7919c3ed 314 de_fault (to_open, (void (*)())tcomplain);
bd5635a1
RP
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);
7919c3ed
JG
319 de_fault (to_wait, (int (*)())noprocess);
320 de_fault (to_fetch_registers, (void (*)())ignore);
e17960fb 321 de_fault (to_store_registers, (void (*)())noprocess);
bd5635a1 322 de_fault (to_prepare_to_store, (void (*)())noprocess);
7919c3ed
JG
323 de_fault (to_xfer_memory, (int (*)())nomemory);
324 de_fault (to_files_info, (void (*)())ignore);
bd5635a1
RP
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);
7919c3ed 333 de_fault (to_load, (void (*)())tcomplain);
bd5635a1
RP
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);
597dc86b 337 de_fault (to_can_run, return_zero);
bd5635a1
RP
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);
344
345#undef de_fault
346}
347
348/* Push a new target type into the stack of the existing target accessors,
349 possibly superseding some of the existing accessors.
350
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.
353
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
356 checking them. */
357
358int
359push_target (t)
360 struct target_ops *t;
361{
362 struct target_ops *st, *prev;
363
364 for (prev = 0, st = current_target;
365 st;
366 prev = st, st = st->to_next) {
367 if ((int)(t->to_stratum) >= (int)(st->to_stratum))
368 break;
369 }
370
371 while (t->to_stratum == st->to_stratum) {
372 /* There's already something on this stratum. Close it off. */
373 (st->to_close) (0);
374 if (prev)
375 prev->to_next = st->to_next; /* Unchain old target_ops */
376 else
377 current_target = st->to_next; /* Unchain first on list */
378 st = st->to_next;
379 }
380
381 /* We have removed all targets in our stratum, now add ourself. */
382 t->to_next = st;
383 if (prev)
384 prev->to_next = t;
385 else
386 current_target = t;
387
388 cleanup_target (current_target);
389 return prev != 0;
390}
391
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). */
394
395int
396unpush_target (t)
397 struct target_ops *t;
398{
399 struct target_ops *u, *v;
400 int result = 0;
401
402 for (u = current_target, v = 0;
403 u;
404 v = u, u = u->to_next)
405 if (u == t)
406 {
407 if (v == 0)
408 pop_target(); /* unchain top copy */
409 else {
410 (t->to_close)(0); /* Let it clean up */
411 v->to_next = t->to_next; /* unchain middle copy */
412 }
413 result++;
414 }
415 return result;
416}
417
418void
419pop_target ()
420{
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);
425}
426
e17960fb
JG
427#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
428
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_
431 memory.
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.
435*/
436
437int
438target_read_string (memaddr, myaddr, len)
439 CORE_ADDR memaddr;
440 char *myaddr;
441 int len;
442{
443 int tlen, origlen, offset, i;
444 char buf[4];
445
446 origlen = len;
447
448 while (len > 0)
449 {
450 tlen = MIN (len, 4 - (memaddr & 3));
451 offset = memaddr & 3;
452
453 if (target_xfer_memory (memaddr & ~3, buf, 4, 0))
454 return origlen - len;
455
456 for (i = 0; i < tlen; i++)
457 {
458 *myaddr++ = buf[i + offset];
459 if (buf[i + offset] == '\000')
460 return (origlen - len) + i + 1;
461 }
462
463 memaddr += tlen;
464 len -= tlen;
465 }
466 return origlen;
467}
468
bd5635a1
RP
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.
476
477 Result is 0 or errno value. */
478
479int
480target_read_memory (memaddr, myaddr, len)
481 CORE_ADDR memaddr;
482 char *myaddr;
483 int len;
484{
485 return target_xfer_memory (memaddr, myaddr, len, 0);
486}
487
488int
489target_write_memory (memaddr, myaddr, len)
490 CORE_ADDR memaddr;
491 char *myaddr;
492 int len;
493{
494 return target_xfer_memory (memaddr, myaddr, len, 1);
495}
496
497int
498target_xfer_memory (memaddr, myaddr, len, write)
499 CORE_ADDR memaddr;
500 char *myaddr;
501 int len;
502 int write;
503{
504 int curlen;
505 int res;
506 struct target_ops *t;
507
508 /* The quick case is that the top target does it all. */
e17960fb
JG
509 res = current_target->to_xfer_memory
510 (memaddr, myaddr, len, write, current_target);
bd5635a1
RP
511 if (res == len)
512 return 0;
513
514 if (res > 0)
515 goto bump;
516 /* If res <= 0 then we call it again in the loop. Ah well. */
517
518 for (; len > 0;)
519 {
520 curlen = len; /* Want to do it all */
521 for (t = current_target;
522 t;
523 t = t->to_has_all_memory? 0: t->to_next)
524 {
e17960fb 525 res = t->to_xfer_memory(memaddr, myaddr, curlen, write, t);
bd5635a1
RP
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 */
529 }
530 if (res <= 0)
531 {
532 /* If this address is for nonexistent memory,
533 read zeros if reading, or do nothing if writing. Return error. */
534 if (!write)
a8e033f2 535 memset (myaddr, 0, len);
e17960fb
JG
536 if (errno == 0)
537 return EIO;
538 else
539 return errno;
bd5635a1
RP
540 }
541bump:
542 memaddr += res;
543 myaddr += res;
544 len -= res;
545 }
546 return 0; /* We managed to cover it all somehow. */
547}
548
549
e1ce8aa5 550/* ARGSUSED */
bd5635a1
RP
551static void
552target_info (args, from_tty)
553 char *args;
554 int from_tty;
555{
556 struct target_ops *t;
557 int has_all_mem = 0;
558
80d68b1d 559 if (symfile_objfile != NULL)
597dc86b 560 printf ("Symbols from \"%s\".\n", symfile_objfile->name);
bd5635a1
RP
561
562#ifdef FILES_INFO_HOOK
563 if (FILES_INFO_HOOK ())
564 return;
565#endif
566
567 for (t = current_target;
568 t;
569 t = t->to_next)
570 {
571 if ((int)(t->to_stratum) <= (int)dummy_stratum)
572 continue;
573 if (has_all_mem)
597dc86b
SG
574 printf("\tWhile running this, gdb does not access memory from...\n");
575 printf("%s:\n", t->to_longname);
e17960fb 576 (t->to_files_info)(t);
bd5635a1
RP
577 has_all_mem = t->to_has_all_memory;
578 }
579}
580
f2fc6e7a
JK
581/* This is to be called by the open routine before it does
582 anything. */
bd5635a1 583
f2fc6e7a
JK
584void
585target_preopen (from_tty)
bd5635a1
RP
586 int from_tty;
587{
bd5635a1
RP
588 dont_repeat();
589
bd5635a1
RP
590 if (target_has_execution)
591 {
592 if (query ("A program is being debugged already. Kill it? "))
e17960fb 593 target_kill ();
bd5635a1
RP
594 else
595 error ("Program not killed.");
596 }
bd5635a1
RP
597}
598
597dc86b
SG
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.
602
603 Result is always valid (error() is called for errors). */
604
605static struct target_ops *
606find_default_run_target (do_mesg)
607 char *do_mesg;
608{
609 struct target_ops **t;
610 struct target_ops *runable;
611 int count;
612
613 count = 0;
614
615 for (t = target_structs; t < target_structs + target_struct_size;
616 ++t)
617 {
618 if (target_can_run(*t))
619 {
620 runable = *t;
621 ++count;
622 }
623 }
624
625 if (count != 1)
626 error ("Don't know how to %s. Try \"help target\".", do_mesg);
627
628 return runable;
629}
630
631void
632find_default_attach (args, from_tty)
633 char *args;
634 int from_tty;
635{
636 struct target_ops *t;
637
638 t = find_default_run_target("attach");
639 (t->to_attach) (args, from_tty);
640 return;
641}
642
643void
644find_default_create_inferior (exec_file, allargs, env)
645 char *exec_file;
646 char *allargs;
647 char **env;
648{
649 struct target_ops *t;
650
651 t = find_default_run_target("run");
652 (t->to_create_inferior) (exec_file, allargs, env);
653 return;
654}
655
656static int
657return_zero ()
658{
659 return 0;
660}
661
bd5635a1
RP
662static char targ_desc[] =
663 "Names of targets and files being debugged.\n\
664Shows the entire stack of targets currently in use (including the exec-file,\n\
665core-file, and process, if any), as well as the symbol file name.";
666
667void
668_initialize_targets ()
669{
670 current_target = &dummy_target;
671 cleanup_target (current_target);
672
bd5635a1
RP
673 add_info ("target", target_info, targ_desc);
674 add_info ("files", target_info, targ_desc);
bd5635a1 675}
This page took 0.144387 seconds and 4 git commands to generate.