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