* gdb.base/opaque.exp: use runto instead of messing around with
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
bd5635a1 1/* Select target systems and architectures at runtime for GDB.
4ad0021e 2 Copyright 1990, 1992, 1993, 1994 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"
4ad0021e
JK
31#include "wait.h"
32#include <signal.h>
bd5635a1 33
e17960fb
JG
34extern int errno;
35
7919c3ed
JG
36static void
37target_info PARAMS ((char *, int));
38
39static void
40cleanup_target PARAMS ((struct target_ops *));
41
42static void
43maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
44
45static void
46maybe_kill_then_attach PARAMS ((char *, int));
47
48static void
49kill_or_be_killed PARAMS ((int));
50
51static void
52default_terminal_info PARAMS ((char *, int));
53
54static int
55nosymbol PARAMS ((char *, CORE_ADDR *));
56
7919c3ed
JG
57static void
58tcomplain PARAMS ((void));
59
60static int
61nomemory PARAMS ((CORE_ADDR, char *, int, int));
62
597dc86b
SG
63static int
64return_zero PARAMS ((void));
65
7919c3ed
JG
66static void
67ignore PARAMS ((void));
597dc86b 68
7919c3ed
JG
69static void
70target_command PARAMS ((char *, int));
bd5635a1 71
597dc86b
SG
72static struct target_ops *
73find_default_run_target PARAMS ((char *));
74
bd5635a1
RP
75/* Pointer to array of target architecture structures; the size of the
76 array; the current index into the array; the allocated size of the
77 array. */
78struct target_ops **target_structs;
79unsigned target_struct_size;
80unsigned target_struct_index;
81unsigned target_struct_allocsize;
82#define DEFAULT_ALLOCSIZE 10
83
84/* The initial current target, so that there is always a semi-valid
85 current target. */
86
f2fc6e7a 87struct target_ops dummy_target = {"None", "None", "",
597dc86b
SG
88 0, 0, /* open, close */
89 find_default_attach, 0, /* attach, detach */
bd5635a1 90 0, 0, /* resume, wait */
dcc8abce 91 0, 0, 0, /* registers */
bd5635a1
RP
92 0, 0, /* memory */
93 0, 0, /* bkpts */
94 0, 0, 0, 0, 0, /* terminal */
fc47a10d 95 0, 0, /* kill, load */
e17960fb 96 0, /* lookup_symbol */
597dc86b
SG
97 find_default_create_inferior, /* create_inferior */
98 0, /* mourn_inferior */
99 0, /* can_run */
49781499 100 0, /* notice_signals */
bd5635a1
RP
101 dummy_stratum, 0, /* stratum, next */
102 0, 0, 0, 0, 0, /* all mem, mem, stack, regs, exec */
e17960fb 103 0, 0, /* section pointers */
bd5635a1
RP
104 OPS_MAGIC,
105};
106
cad1498f 107/* Top of target stack. */
bd5635a1 108
cad1498f 109struct target_stack_item *target_stack;
bd5635a1 110
cad1498f
SG
111/* The target structure we are currently using to talk to a process
112 or file or whatever "inferior" we have. */
bd5635a1 113
cad1498f 114struct target_ops current_target;
bd5635a1 115
f2fc6e7a
JK
116/* Command list for target. */
117
118static struct cmd_list_element *targetlist = NULL;
119
16d2cc80
SS
120/* Nonzero if we are debugging an attached outside process
121 rather than an inferior. */
122
123int attach_flag;
124
f2fc6e7a
JK
125/* The user just typed 'target' without the name of a target. */
126
e1ce8aa5 127/* ARGSUSED */
f2fc6e7a
JK
128static void
129target_command (arg, from_tty)
130 char *arg;
131 int from_tty;
132{
597dc86b 133 fputs_filtered ("Argument required (target name). Try `help target'\n",
67ac9759 134 gdb_stdout);
f2fc6e7a 135}
bd5635a1
RP
136
137/* Add a possible target architecture to the list. */
138
139void
140add_target (t)
141 struct target_ops *t;
142{
bd5635a1
RP
143 if (!target_structs)
144 {
145 target_struct_allocsize = DEFAULT_ALLOCSIZE;
146 target_structs = (struct target_ops **) xmalloc
147 (target_struct_allocsize * sizeof (*target_structs));
148 }
149 if (target_struct_size >= target_struct_allocsize)
150 {
151 target_struct_allocsize *= 2;
7919c3ed
JG
152 target_structs = (struct target_ops **)
153 xrealloc ((char *) target_structs,
154 target_struct_allocsize * sizeof (*target_structs));
bd5635a1
RP
155 }
156 target_structs[target_struct_size++] = t;
157 cleanup_target (t);
f2fc6e7a
JK
158
159 if (targetlist == NULL)
160 add_prefix_cmd ("target", class_run, target_command,
161 "Connect to a target machine or process.\n\
162The first argument is the type or protocol of the target machine.\n\
163Remaining arguments are interpreted by the target protocol. For more\n\
164information on the arguments for a particular protocol, type\n\
165`help target ' followed by the protocol name.",
166 &targetlist, "target ", 0, &cmdlist);
167 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
bd5635a1
RP
168}
169
170/* Stub functions */
171
172static void
173ignore ()
174{
175}
176
177/* ARGSUSED */
178static int
179nomemory (memaddr, myaddr, len, write)
180 CORE_ADDR memaddr;
181 char *myaddr;
182 int len;
183 int write;
184{
51b57ded 185 errno = EIO; /* Can't read/write this location */
bd5635a1
RP
186 return 0; /* No bytes handled */
187}
188
189static void
190tcomplain ()
191{
192 error ("You can't do that when your target is `%s'",
cad1498f 193 current_target.to_shortname);
bd5635a1
RP
194}
195
80d68b1d 196void
bd5635a1
RP
197noprocess ()
198{
199 error ("You can't do that without a process to debug");
200}
201
e1ce8aa5 202/* ARGSUSED */
bd5635a1
RP
203static int
204nosymbol (name, addrp)
205 char *name;
206 CORE_ADDR *addrp;
207{
208 return 1; /* Symbol does not exist in target env */
209}
210
e1ce8aa5 211/* ARGSUSED */
bd5635a1
RP
212static void
213default_terminal_info (args, from_tty)
214 char *args;
215 int from_tty;
216{
67ac9759 217 printf_unfiltered("No saved terminal information.\n");
bd5635a1
RP
218}
219
bd5635a1
RP
220/* This is the default target_create_inferior and target_attach function.
221 If the current target is executing, it asks whether to kill it off.
222 If this function returns without calling error(), it has killed off
223 the target, and the operation should be attempted. */
224
225static void
226kill_or_be_killed (from_tty)
227 int from_tty;
228{
bd5635a1
RP
229 if (target_has_execution)
230 {
67ac9759 231 printf_unfiltered ("You are already running a program:\n");
bd5635a1
RP
232 target_files_info ();
233 if (query ("Kill it? ")) {
e17960fb 234 target_kill ();
bd5635a1
RP
235 if (target_has_execution)
236 error ("Killing the program did not help.");
237 return;
238 } else {
239 error ("Program not killed.");
240 }
241 }
242 tcomplain();
243}
244
245static void
246maybe_kill_then_attach (args, from_tty)
247 char *args;
248 int from_tty;
249{
250 kill_or_be_killed (from_tty);
251 target_attach (args, from_tty);
252}
253
254static void
255maybe_kill_then_create_inferior (exec, args, env)
256 char *exec;
257 char *args;
258 char **env;
259{
260 kill_or_be_killed (0);
261 target_create_inferior (exec, args, env);
262}
263
264/* Clean up a target struct so it no longer has any zero pointers in it.
265 We default entries, at least to stubs that print error messages. */
266
267static void
268cleanup_target (t)
269 struct target_ops *t;
270{
271
bd5635a1
RP
272#define de_fault(field, value) \
273 if (!t->field) t->field = value
274
275 /* FIELD DEFAULT VALUE */
276
7919c3ed 277 de_fault (to_open, (void (*)())tcomplain);
bd5635a1
RP
278 de_fault (to_close, (void (*)())ignore);
279 de_fault (to_attach, maybe_kill_then_attach);
280 de_fault (to_detach, (void (*)())ignore);
281 de_fault (to_resume, (void (*)())noprocess);
7919c3ed
JG
282 de_fault (to_wait, (int (*)())noprocess);
283 de_fault (to_fetch_registers, (void (*)())ignore);
e17960fb 284 de_fault (to_store_registers, (void (*)())noprocess);
bd5635a1 285 de_fault (to_prepare_to_store, (void (*)())noprocess);
7919c3ed
JG
286 de_fault (to_xfer_memory, (int (*)())nomemory);
287 de_fault (to_files_info, (void (*)())ignore);
bd5635a1
RP
288 de_fault (to_insert_breakpoint, memory_insert_breakpoint);
289 de_fault (to_remove_breakpoint, memory_remove_breakpoint);
290 de_fault (to_terminal_init, ignore);
291 de_fault (to_terminal_inferior, ignore);
292 de_fault (to_terminal_ours_for_output,ignore);
293 de_fault (to_terminal_ours, ignore);
294 de_fault (to_terminal_info, default_terminal_info);
295 de_fault (to_kill, (void (*)())noprocess);
7919c3ed 296 de_fault (to_load, (void (*)())tcomplain);
bd5635a1
RP
297 de_fault (to_lookup_symbol, nosymbol);
298 de_fault (to_create_inferior, maybe_kill_then_create_inferior);
299 de_fault (to_mourn_inferior, (void (*)())noprocess);
597dc86b 300 de_fault (to_can_run, return_zero);
49781499 301 de_fault (to_notice_signals, (void (*)())ignore);
bd5635a1
RP
302
303#undef de_fault
304}
305
cad1498f
SG
306/* Go through the target stack from top to bottom, copying over zero entries in
307 current_target. In effect, we are doing class inheritance through the
308 pushed target vectors. */
309
310static void
311update_current_target ()
312{
313 struct target_stack_item *item;
314 struct target_ops *t;
315
316 /* First, reset current_target */
317 memset (&current_target, 0, sizeof current_target);
318
319 for (item = target_stack; item; item = item->next)
320 {
321 t = item->target_ops;
322
323#define INHERIT(FIELD, TARGET) \
324 if (!current_target.FIELD) \
325 current_target.FIELD = TARGET->FIELD
326
327 INHERIT (to_shortname, t);
328 INHERIT (to_longname, t);
329 INHERIT (to_doc, t);
330 INHERIT (to_open, t);
331 INHERIT (to_close, t);
332 INHERIT (to_attach, t);
333 INHERIT (to_detach, t);
334 INHERIT (to_resume, t);
335 INHERIT (to_wait, t);
336 INHERIT (to_fetch_registers, t);
337 INHERIT (to_store_registers, t);
338 INHERIT (to_prepare_to_store, t);
339 INHERIT (to_xfer_memory, t);
340 INHERIT (to_files_info, t);
341 INHERIT (to_insert_breakpoint, t);
342 INHERIT (to_remove_breakpoint, t);
343 INHERIT (to_terminal_init, t);
344 INHERIT (to_terminal_inferior, t);
345 INHERIT (to_terminal_ours_for_output, t);
346 INHERIT (to_terminal_ours, t);
347 INHERIT (to_terminal_info, t);
348 INHERIT (to_kill, t);
349 INHERIT (to_load, t);
350 INHERIT (to_lookup_symbol, t);
351 INHERIT (to_create_inferior, t);
352 INHERIT (to_mourn_inferior, t);
353 INHERIT (to_can_run, t);
354 INHERIT (to_notice_signals, t);
355 INHERIT (to_stratum, t);
356 INHERIT (DONT_USE, t);
357 INHERIT (to_has_all_memory, t);
358 INHERIT (to_has_memory, t);
359 INHERIT (to_has_stack, t);
360 INHERIT (to_has_registers, t);
361 INHERIT (to_has_execution, t);
362 INHERIT (to_sections, t);
363 INHERIT (to_sections_end, t);
364 INHERIT (to_magic, t);
365
366#undef INHERIT
367 }
368}
369
bd5635a1
RP
370/* Push a new target type into the stack of the existing target accessors,
371 possibly superseding some of the existing accessors.
372
373 Result is zero if the pushed target ended up on top of the stack,
374 nonzero if at least one target is on top of it.
375
376 Rather than allow an empty stack, we always have the dummy target at
377 the bottom stratum, so we can call the function vectors without
378 checking them. */
379
380int
381push_target (t)
382 struct target_ops *t;
383{
cad1498f
SG
384 struct target_stack_item *cur, *prev, *tmp;
385
386 /* Check magic number. If wrong, it probably means someone changed
387 the struct definition, but not all the places that initialize one. */
388 if (t->to_magic != OPS_MAGIC)
389 {
390 fprintf_unfiltered(gdb_stderr,
391 "Magic number of %s target struct wrong\n",
392 t->to_shortname);
393 abort();
394 }
395
396 /* Find the proper stratum to install this target in. */
397
398 for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
399 {
400 if ((int)(t->to_stratum) >= (int)(cur->target_ops->to_stratum))
401 break;
402 }
403
404 /* If there's already targets at this stratum, remove them. */
405
406 if (cur)
407 while (t->to_stratum == cur->target_ops->to_stratum)
408 {
409 /* There's already something on this stratum. Close it off. */
410 (cur->target_ops->to_close) (0);
411 if (prev)
412 prev->next = cur->next; /* Unchain old target_ops */
413 else
414 target_stack = cur->next; /* Unchain first on list */
415 tmp = cur->next;
416 free (cur);
417 cur = tmp;
418 }
419
420 /* We have removed all targets in our stratum, now add the new one. */
421
422 tmp = xmalloc (sizeof (struct target_stack_item));
423 tmp->next = cur;
424 tmp->target_ops = t;
bd5635a1 425
bd5635a1 426 if (prev)
cad1498f 427 prev->next = tmp;
bd5635a1 428 else
cad1498f
SG
429 target_stack = tmp;
430
431 update_current_target ();
bd5635a1 432
cad1498f 433 cleanup_target (&current_target); /* Fill in the gaps */
bd5635a1
RP
434 return prev != 0;
435}
436
437/* Remove a target_ops vector from the stack, wherever it may be.
cad1498f 438 Return how many times it was removed (0 or 1). */
bd5635a1
RP
439
440int
441unpush_target (t)
442 struct target_ops *t;
443{
cad1498f 444 struct target_stack_item *cur, *prev;
bd5635a1 445
67e947de
SG
446 t->to_close (0); /* Let it clean up */
447
cad1498f
SG
448 /* Look for the specified target. Note that we assume that a target
449 can only occur once in the target stack. */
450
451 for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
452 if (cur->target_ops == t)
453 break;
454
455 if (!cur)
456 return 0; /* Didn't find target_ops, quit now */
457
458 /* Unchain the target */
459
460 if (!prev)
67e947de 461 target_stack = cur->next;
cad1498f
SG
462 else
463 prev->next = cur->next;
464
465 free (cur); /* Release the target_stack_item */
466
67e947de
SG
467 update_current_target ();
468 cleanup_target (&current_target);
cad1498f
SG
469
470 return 1;
bd5635a1
RP
471}
472
473void
474pop_target ()
475{
cad1498f
SG
476 (current_target.to_close)(0); /* Let it clean up */
477 if (unpush_target (target_stack->target_ops) == 1)
478 return;
479
480 fprintf_unfiltered(gdb_stderr,
481 "pop_target couldn't find target %s\n",
482 current_target.to_shortname);
483 abort();
bd5635a1
RP
484}
485
49781499 486#undef MIN
e17960fb
JG
487#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
488
4ad0021e
JK
489/* target_read_string -- read a null terminated string, up to LEN bytes,
490 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
491 Set *STRING to a pointer to malloc'd memory containing the data; the caller
492 is responsible for freeing it. Return the number of bytes successfully
493 read. */
e17960fb
JG
494
495int
4ad0021e 496target_read_string (memaddr, string, len, errnop)
e17960fb 497 CORE_ADDR memaddr;
4ad0021e 498 char **string;
e17960fb 499 int len;
4ad0021e 500 int *errnop;
e17960fb
JG
501{
502 int tlen, origlen, offset, i;
503 char buf[4];
4ad0021e
JK
504 int errcode = 0;
505 char *buffer;
506 int buffer_allocated;
507 char *bufptr;
508 unsigned int nbytes_read = 0;
509
510 /* Small for testing. */
511 buffer_allocated = 4;
512 buffer = xmalloc (buffer_allocated);
513 bufptr = buffer;
e17960fb
JG
514
515 origlen = len;
516
517 while (len > 0)
518 {
519 tlen = MIN (len, 4 - (memaddr & 3));
520 offset = memaddr & 3;
521
4ad0021e
JK
522 errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
523 if (errcode != 0)
524 goto done;
525
526 if (bufptr - buffer + tlen > buffer_allocated)
527 {
528 unsigned int bytes;
529 bytes = bufptr - buffer;
530 buffer_allocated *= 2;
531 buffer = xrealloc (buffer, buffer_allocated);
532 bufptr = buffer + bytes;
533 }
e17960fb
JG
534
535 for (i = 0; i < tlen; i++)
536 {
4ad0021e 537 *bufptr++ = buf[i + offset];
e17960fb 538 if (buf[i + offset] == '\000')
4ad0021e
JK
539 {
540 nbytes_read += i + 1;
541 goto done;
542 }
e17960fb
JG
543 }
544
545 memaddr += tlen;
546 len -= tlen;
4ad0021e 547 nbytes_read += tlen;
e17960fb 548 }
4ad0021e
JK
549 done:
550 if (errnop != NULL)
551 *errnop = errcode;
552 if (string != NULL)
553 *string = buffer;
554 return nbytes_read;
e17960fb
JG
555}
556
49781499
JK
557/* Read LEN bytes of target memory at address MEMADDR, placing the results in
558 GDB's memory at MYADDR. Returns either 0 for success or an errno value
559 if any error occurs.
bd5635a1 560
49781499
JK
561 If an error occurs, no guarantee is made about the contents of the data at
562 MYADDR. In particular, the caller should not depend upon partial reads
563 filling the buffer with good data. There is no way for the caller to know
564 how much good data might have been transfered anyway. Callers that can
565 deal with partial reads should call target_read_memory_partial. */
bd5635a1
RP
566
567int
568target_read_memory (memaddr, myaddr, len)
569 CORE_ADDR memaddr;
570 char *myaddr;
571 int len;
572{
573 return target_xfer_memory (memaddr, myaddr, len, 0);
574}
575
49781499
JK
576/* Read LEN bytes of target memory at address MEMADDR, placing the results
577 in GDB's memory at MYADDR. Returns a count of the bytes actually read,
578 and optionally an errno value in the location pointed to by ERRNOPTR
579 if ERRNOPTR is non-null. */
580
581int
582target_read_memory_partial (memaddr, myaddr, len, errnoptr)
583 CORE_ADDR memaddr;
584 char *myaddr;
585 int len;
586 int *errnoptr;
587{
588 int nread; /* Number of bytes actually read. */
589 int errcode; /* Error from last read. */
590
591 /* First try a complete read. */
592 errcode = target_xfer_memory (memaddr, myaddr, len, 0);
593 if (errcode == 0)
594 {
595 /* Got it all. */
596 nread = len;
597 }
598 else
599 {
600 /* Loop, reading one byte at a time until we get as much as we can. */
601 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
602 {
603 errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0);
604 }
605 /* If an error, the last read was unsuccessful, so adjust count. */
606 if (errcode != 0)
607 {
608 nread--;
609 }
610 }
611 if (errnoptr != NULL)
612 {
613 *errnoptr = errcode;
614 }
615 return (nread);
616}
617
bd5635a1
RP
618int
619target_write_memory (memaddr, myaddr, len)
620 CORE_ADDR memaddr;
621 char *myaddr;
622 int len;
623{
624 return target_xfer_memory (memaddr, myaddr, len, 1);
625}
626
49781499
JK
627/* Move memory to or from the targets. Iterate until all of it has
628 been moved, if necessary. The top target gets priority; anything
629 it doesn't want, is offered to the next one down, etc. Note the
630 business with curlen: if an early target says "no, but I have a
631 boundary overlapping this xfer" then we shorten what we offer to
632 the subsequent targets so the early guy will get a chance at the
633 tail before the subsequent ones do.
634
635 Result is 0 or errno value. */
636
bd5635a1
RP
637int
638target_xfer_memory (memaddr, myaddr, len, write)
639 CORE_ADDR memaddr;
640 char *myaddr;
641 int len;
642 int write;
643{
644 int curlen;
645 int res;
646 struct target_ops *t;
cad1498f 647 struct target_stack_item *item;
49781499
JK
648
649 /* to_xfer_memory is not guaranteed to set errno, even when it returns
650 0. */
651 errno = 0;
652
bd5635a1 653 /* The quick case is that the top target does it all. */
cad1498f
SG
654 res = current_target.to_xfer_memory
655 (memaddr, myaddr, len, write, &current_target);
bd5635a1
RP
656 if (res == len)
657 return 0;
658
659 if (res > 0)
660 goto bump;
661 /* If res <= 0 then we call it again in the loop. Ah well. */
662
663 for (; len > 0;)
664 {
665 curlen = len; /* Want to do it all */
cad1498f 666 for (item = target_stack; item; item = item->next)
bd5635a1 667 {
cad1498f
SG
668 t = item->target_ops;
669
670 res = t->to_xfer_memory (memaddr, myaddr, curlen, write, t);
671 if (res > 0)
672 break; /* Handled all or part of xfer */
673 if (t->to_has_all_memory)
674 break;
bd5635a1 675 }
cad1498f 676
bd5635a1
RP
677 if (res <= 0)
678 {
679 /* If this address is for nonexistent memory,
680 read zeros if reading, or do nothing if writing. Return error. */
681 if (!write)
a8e033f2 682 memset (myaddr, 0, len);
e17960fb
JG
683 if (errno == 0)
684 return EIO;
685 else
686 return errno;
bd5635a1
RP
687 }
688bump:
689 memaddr += res;
690 myaddr += res;
691 len -= res;
692 }
693 return 0; /* We managed to cover it all somehow. */
694}
695
696
e1ce8aa5 697/* ARGSUSED */
bd5635a1
RP
698static void
699target_info (args, from_tty)
700 char *args;
701 int from_tty;
702{
703 struct target_ops *t;
cad1498f 704 struct target_stack_item *item;
bd5635a1
RP
705 int has_all_mem = 0;
706
80d68b1d 707 if (symfile_objfile != NULL)
67ac9759 708 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
bd5635a1
RP
709
710#ifdef FILES_INFO_HOOK
711 if (FILES_INFO_HOOK ())
712 return;
713#endif
714
cad1498f 715 for (item = target_stack; item; item = item->next)
bd5635a1 716 {
cad1498f
SG
717 t = item->target_ops;
718
bd5635a1
RP
719 if ((int)(t->to_stratum) <= (int)dummy_stratum)
720 continue;
721 if (has_all_mem)
cad1498f 722 printf_unfiltered("\tWhile running this, GDB does not access memory from...\n");
67ac9759 723 printf_unfiltered("%s:\n", t->to_longname);
e17960fb 724 (t->to_files_info)(t);
bd5635a1
RP
725 has_all_mem = t->to_has_all_memory;
726 }
727}
728
f2fc6e7a
JK
729/* This is to be called by the open routine before it does
730 anything. */
bd5635a1 731
f2fc6e7a
JK
732void
733target_preopen (from_tty)
bd5635a1
RP
734 int from_tty;
735{
bd5635a1
RP
736 dont_repeat();
737
bd5635a1
RP
738 if (target_has_execution)
739 {
740 if (query ("A program is being debugged already. Kill it? "))
e17960fb 741 target_kill ();
bd5635a1
RP
742 else
743 error ("Program not killed.");
744 }
4ad0021e
JK
745
746 /* Calling target_kill may remove the target from the stack. But if
747 it doesn't (which seems like a win for UDI), remove it now. */
748
749 if (target_has_execution)
750 pop_target ();
bd5635a1
RP
751}
752
49781499
JK
753/* Detach a target after doing deferred register stores. */
754
755void
756target_detach (args, from_tty)
757 char *args;
758 int from_tty;
759{
760 /* Handle any optimized stores to the inferior. */
761#ifdef DO_DEFERRED_STORES
762 DO_DEFERRED_STORES;
763#endif
cad1498f 764 (current_target.to_detach) (args, from_tty);
49781499
JK
765}
766
1340861c
KH
767void
768target_link (modname, t_reloc)
769 char *modname;
770 CORE_ADDR *t_reloc;
771{
cad1498f 772 if (STREQ(current_target.to_shortname, "rombug"))
1340861c 773 {
cad1498f 774 (current_target.to_lookup_symbol) (modname, t_reloc);
16d2cc80
SS
775 if (*t_reloc == 0)
776 error("Unable to link to %s and get relocation in rombug", modname);
1340861c
KH
777 }
778 else
779 *t_reloc = (CORE_ADDR)-1;
780}
781
597dc86b
SG
782/* Look through the list of possible targets for a target that can
783 execute a run or attach command without any other data. This is
784 used to locate the default process stratum.
785
786 Result is always valid (error() is called for errors). */
787
788static struct target_ops *
789find_default_run_target (do_mesg)
790 char *do_mesg;
791{
792 struct target_ops **t;
49781499 793 struct target_ops *runable = NULL;
597dc86b
SG
794 int count;
795
796 count = 0;
797
798 for (t = target_structs; t < target_structs + target_struct_size;
799 ++t)
800 {
801 if (target_can_run(*t))
802 {
803 runable = *t;
804 ++count;
805 }
806 }
807
808 if (count != 1)
809 error ("Don't know how to %s. Try \"help target\".", do_mesg);
810
811 return runable;
812}
813
814void
815find_default_attach (args, from_tty)
816 char *args;
817 int from_tty;
818{
819 struct target_ops *t;
820
821 t = find_default_run_target("attach");
822 (t->to_attach) (args, from_tty);
823 return;
824}
825
826void
827find_default_create_inferior (exec_file, allargs, env)
828 char *exec_file;
829 char *allargs;
830 char **env;
831{
832 struct target_ops *t;
833
834 t = find_default_run_target("run");
835 (t->to_create_inferior) (exec_file, allargs, env);
836 return;
837}
838
839static int
840return_zero ()
841{
842 return 0;
843}
844
49781499
JK
845struct target_ops *
846find_core_target ()
847{
848 struct target_ops **t;
849 struct target_ops *runable = NULL;
850 int count;
851
852 count = 0;
853
854 for (t = target_structs; t < target_structs + target_struct_size;
855 ++t)
856 {
857 if ((*t)->to_stratum == core_stratum)
858 {
859 runable = *t;
860 ++count;
861 }
862 }
863
864 return(count == 1 ? runable : NULL);
865}
67ac9759 866\f
16d2cc80
SS
867/* The inferior process has died. Long live the inferior! */
868
869void
870generic_mourn_inferior ()
871{
cad1498f
SG
872 extern int show_breakpoint_hit_counts;
873
16d2cc80
SS
874 inferior_pid = 0;
875 attach_flag = 0;
876 breakpoint_init_inferior ();
877 registers_changed ();
878
879#ifdef CLEAR_DEFERRED_STORES
880 /* Delete any pending stores to the inferior... */
881 CLEAR_DEFERRED_STORES;
882#endif
883
884 reopen_exec_file ();
885 reinit_frame_cache ();
886
887 /* It is confusing to the user for ignore counts to stick around
888 from previous runs of the inferior. So clear them. */
cad1498f
SG
889 /* However, it is more confusing for the ignore counts to disappear when
890 using hit counts. So don't clear them if we're counting hits. */
891 if (!show_breakpoint_hit_counts)
892 breakpoint_clear_ignore_counts ();
16d2cc80
SS
893}
894\f
67ac9759
JK
895/* This table must match in order and size the signals in enum target_signal
896 in target.h. */
897static struct {
898 char *name;
899 char *string;
900 } signals [] =
901{
902 {"0", "Signal 0"},
903 {"SIGHUP", "Hangup"},
904 {"SIGINT", "Interrupt"},
905 {"SIGQUIT", "Quit"},
906 {"SIGILL", "Illegal instruction"},
907 {"SIGTRAP", "Trace/breakpoint trap"},
908 {"SIGABRT", "Aborted"},
909 {"SIGEMT", "Emulation trap"},
910 {"SIGFPE", "Arithmetic exception"},
911 {"SIGKILL", "Killed"},
912 {"SIGBUS", "Bus error"},
913 {"SIGSEGV", "Segmentation fault"},
914 {"SIGSYS", "Bad system call"},
915 {"SIGPIPE", "Broken pipe"},
916 {"SIGALRM", "Alarm clock"},
917 {"SIGTERM", "Terminated"},
918 {"SIGURG", "Urgent I/O condition"},
919 {"SIGSTOP", "Stopped (signal)"},
920 {"SIGTSTP", "Stopped (user)"},
921 {"SIGCONT", "Continued"},
922 {"SIGCHLD", "Child status changed"},
923 {"SIGTTIN", "Stopped (tty input)"},
924 {"SIGTTOU", "Stopped (tty output)"},
925 {"SIGIO", "I/O possible"},
926 {"SIGXCPU", "CPU time limit exceeded"},
927 {"SIGXFSZ", "File size limit exceeded"},
928 {"SIGVTALRM", "Virtual timer expired"},
929 {"SIGPROF", "Profiling timer expired"},
930 {"SIGWINCH", "Window size changed"},
931 {"SIGLOST", "Resource lost"},
932 {"SIGUSR1", "User defined signal 1"},
933 {"SIGUSR2", "User defined signal 2"},
934 {"SIGPWR", "Power fail/restart"},
935 {"SIGPOLL", "Pollable event occurred"},
936 {"SIGWIND", "SIGWIND"},
937 {"SIGPHONE", "SIGPHONE"},
938 {"SIGWAITING", "Process's LWPs are blocked"},
939 {"SIGLWP", "Signal LWP"},
940 {"SIGDANGER", "Swap space dangerously low"},
941 {"SIGGRANT", "Monitor mode granted"},
942 {"SIGRETRACT", "Need to relinguish monitor mode"},
943 {"SIGMSG", "Monitor mode data available"},
944 {"SIGSOUND", "Sound completed"},
945 {"SIGSAK", "Secure attention"},
946 {NULL, "Unknown signal"},
4ad0021e
JK
947 {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
948
67ac9759
JK
949 /* Last entry, used to check whether the table is the right size. */
950 {NULL, "TARGET_SIGNAL_MAGIC"}
951};
952
953/* Return the string for a signal. */
954char *
955target_signal_to_string (sig)
956 enum target_signal sig;
957{
958 return signals[sig].string;
959}
960
961/* Return the name for a signal. */
962char *
963target_signal_to_name (sig)
964 enum target_signal sig;
965{
966 if (sig == TARGET_SIGNAL_UNKNOWN)
967 /* I think the code which prints this will always print it along with
968 the string, so no need to be verbose. */
969 return "?";
970 return signals[sig].name;
971}
972
973/* Given a name, return its signal. */
974enum target_signal
975target_signal_from_name (name)
976 char *name;
977{
978 enum target_signal sig;
979
980 /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
981 for TARGET_SIGNAL_SIGCHLD. SIGIOT, on the other hand, is more
982 questionable; seems like by now people should call it SIGABRT
983 instead. */
984
4ad0021e
JK
985 /* This ugly cast brought to you by the native VAX compiler. */
986 for (sig = TARGET_SIGNAL_HUP;
987 signals[sig].name != NULL;
988 sig = (enum target_signal)((int)sig + 1))
67ac9759
JK
989 if (STREQ (name, signals[sig].name))
990 return sig;
991 return TARGET_SIGNAL_UNKNOWN;
992}
4ad0021e
JK
993\f
994/* The following functions are to help certain targets deal
995 with the signal/waitstatus stuff. They could just as well be in
996 a file called native-utils.c or unixwaitstatus-utils.c or whatever. */
997
998/* Convert host signal to our signals. */
999enum target_signal
1000target_signal_from_host (hostsig)
1001 int hostsig;
1002{
1003 /* A switch statement would make sense but would require special kludges
1004 to deal with the cases where more than one signal has the same number. */
1005
1006 if (hostsig == 0) return TARGET_SIGNAL_0;
1007
1008#if defined (SIGHUP)
1009 if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1010#endif
1011#if defined (SIGINT)
1012 if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1013#endif
1014#if defined (SIGQUIT)
1015 if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1016#endif
1017#if defined (SIGILL)
1018 if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1019#endif
1020#if defined (SIGTRAP)
1021 if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1022#endif
1023#if defined (SIGABRT)
1024 if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1025#endif
1026#if defined (SIGEMT)
1027 if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1028#endif
1029#if defined (SIGFPE)
1030 if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1031#endif
1032#if defined (SIGKILL)
1033 if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1034#endif
1035#if defined (SIGBUS)
1036 if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1037#endif
1038#if defined (SIGSEGV)
1039 if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1040#endif
1041#if defined (SIGSYS)
1042 if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1043#endif
1044#if defined (SIGPIPE)
1045 if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1046#endif
1047#if defined (SIGALRM)
1048 if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1049#endif
1050#if defined (SIGTERM)
1051 if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1052#endif
1053#if defined (SIGUSR1)
1054 if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1055#endif
1056#if defined (SIGUSR2)
1057 if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1058#endif
1059#if defined (SIGCLD)
1060 if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1061#endif
1062#if defined (SIGCHLD)
1063 if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1064#endif
1065#if defined (SIGPWR)
1066 if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1067#endif
1068#if defined (SIGWINCH)
1069 if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1070#endif
1071#if defined (SIGURG)
1072 if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1073#endif
1074#if defined (SIGIO)
1075 if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1076#endif
1077#if defined (SIGPOLL)
1078 if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1079#endif
1080#if defined (SIGSTOP)
1081 if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1082#endif
1083#if defined (SIGTSTP)
1084 if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1085#endif
1086#if defined (SIGCONT)
1087 if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1088#endif
1089#if defined (SIGTTIN)
1090 if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1091#endif
1092#if defined (SIGTTOU)
1093 if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1094#endif
1095#if defined (SIGVTALRM)
1096 if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1097#endif
1098#if defined (SIGPROF)
1099 if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1100#endif
1101#if defined (SIGXCPU)
1102 if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1103#endif
1104#if defined (SIGXFSZ)
1105 if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1106#endif
1107#if defined (SIGWIND)
1108 if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1109#endif
1110#if defined (SIGPHONE)
1111 if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1112#endif
1113#if defined (SIGLOST)
1114 if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1115#endif
1116#if defined (SIGWAITING)
1117 if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1118#endif
1119#if defined (SIGLWP)
1120 if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1121#endif
1122#if defined (SIGDANGER)
1123 if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1124#endif
1125#if defined (SIGGRANT)
1126 if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1127#endif
1128#if defined (SIGRETRACT)
1129 if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1130#endif
1131#if defined (SIGMSG)
1132 if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1133#endif
1134#if defined (SIGSOUND)
1135 if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1136#endif
1137#if defined (SIGSAK)
1138 if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
1139#endif
1140 return TARGET_SIGNAL_UNKNOWN;
1141}
1142
1143int
1144target_signal_to_host (oursig)
1145 enum target_signal oursig;
1146{
1147 switch (oursig)
1148 {
1149 case TARGET_SIGNAL_0: return 0;
1150
1151#if defined (SIGHUP)
1152 case TARGET_SIGNAL_HUP: return SIGHUP;
1153#endif
1154#if defined (SIGINT)
1155 case TARGET_SIGNAL_INT: return SIGINT;
1156#endif
1157#if defined (SIGQUIT)
1158 case TARGET_SIGNAL_QUIT: return SIGQUIT;
1159#endif
1160#if defined (SIGILL)
1161 case TARGET_SIGNAL_ILL: return SIGILL;
1162#endif
1163#if defined (SIGTRAP)
1164 case TARGET_SIGNAL_TRAP: return SIGTRAP;
1165#endif
1166#if defined (SIGABRT)
1167 case TARGET_SIGNAL_ABRT: return SIGABRT;
1168#endif
1169#if defined (SIGEMT)
1170 case TARGET_SIGNAL_EMT: return SIGEMT;
1171#endif
1172#if defined (SIGFPE)
1173 case TARGET_SIGNAL_FPE: return SIGFPE;
1174#endif
1175#if defined (SIGKILL)
1176 case TARGET_SIGNAL_KILL: return SIGKILL;
1177#endif
1178#if defined (SIGBUS)
1179 case TARGET_SIGNAL_BUS: return SIGBUS;
1180#endif
1181#if defined (SIGSEGV)
1182 case TARGET_SIGNAL_SEGV: return SIGSEGV;
1183#endif
1184#if defined (SIGSYS)
1185 case TARGET_SIGNAL_SYS: return SIGSYS;
1186#endif
1187#if defined (SIGPIPE)
1188 case TARGET_SIGNAL_PIPE: return SIGPIPE;
1189#endif
1190#if defined (SIGALRM)
1191 case TARGET_SIGNAL_ALRM: return SIGALRM;
1192#endif
1193#if defined (SIGTERM)
1194 case TARGET_SIGNAL_TERM: return SIGTERM;
1195#endif
1196#if defined (SIGUSR1)
1197 case TARGET_SIGNAL_USR1: return SIGUSR1;
1198#endif
1199#if defined (SIGUSR2)
1200 case TARGET_SIGNAL_USR2: return SIGUSR2;
1201#endif
1202#if defined (SIGCHLD) || defined (SIGCLD)
1203 case TARGET_SIGNAL_CHLD:
1204#if defined (SIGCHLD)
1205 return SIGCHLD;
1206#else
1207 return SIGCLD;
1208#endif
1209#endif /* SIGCLD or SIGCHLD */
1210#if defined (SIGPWR)
1211 case TARGET_SIGNAL_PWR: return SIGPWR;
1212#endif
1213#if defined (SIGWINCH)
1214 case TARGET_SIGNAL_WINCH: return SIGWINCH;
1215#endif
1216#if defined (SIGURG)
1217 case TARGET_SIGNAL_URG: return SIGURG;
1218#endif
1219#if defined (SIGIO)
1220 case TARGET_SIGNAL_IO: return SIGIO;
1221#endif
1222#if defined (SIGPOLL)
1223 case TARGET_SIGNAL_POLL: return SIGPOLL;
1224#endif
1225#if defined (SIGSTOP)
1226 case TARGET_SIGNAL_STOP: return SIGSTOP;
1227#endif
1228#if defined (SIGTSTP)
1229 case TARGET_SIGNAL_TSTP: return SIGTSTP;
1230#endif
1231#if defined (SIGCONT)
1232 case TARGET_SIGNAL_CONT: return SIGCONT;
1233#endif
1234#if defined (SIGTTIN)
1235 case TARGET_SIGNAL_TTIN: return SIGTTIN;
1236#endif
1237#if defined (SIGTTOU)
1238 case TARGET_SIGNAL_TTOU: return SIGTTOU;
1239#endif
1240#if defined (SIGVTALRM)
1241 case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1242#endif
1243#if defined (SIGPROF)
1244 case TARGET_SIGNAL_PROF: return SIGPROF;
1245#endif
1246#if defined (SIGXCPU)
1247 case TARGET_SIGNAL_XCPU: return SIGXCPU;
1248#endif
1249#if defined (SIGXFSZ)
1250 case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1251#endif
1252#if defined (SIGWIND)
1253 case TARGET_SIGNAL_WIND: return SIGWIND;
1254#endif
1255#if defined (SIGPHONE)
1256 case TARGET_SIGNAL_PHONE: return SIGPHONE;
1257#endif
1258#if defined (SIGLOST)
1259 case TARGET_SIGNAL_LOST: return SIGLOST;
1260#endif
1261#if defined (SIGWAITING)
1262 case TARGET_SIGNAL_WAITING: return SIGWAITING;
1263#endif
1264#if defined (SIGLWP)
1265 case TARGET_SIGNAL_LWP: return SIGLWP;
1266#endif
1267#if defined (SIGDANGER)
1268 case TARGET_SIGNAL_DANGER: return SIGDANGER;
1269#endif
1270#if defined (SIGGRANT)
1271 case TARGET_SIGNAL_GRANT: return SIGGRANT;
1272#endif
1273#if defined (SIGRETRACT)
1274 case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1275#endif
1276#if defined (SIGMSG)
1277 case TARGET_SIGNAL_MSG: return SIGMSG;
1278#endif
1279#if defined (SIGSOUND)
1280 case TARGET_SIGNAL_SOUND: return SIGSOUND;
1281#endif
1282#if defined (SIGSAK)
1283 case TARGET_SIGNAL_SAK: return SIGSAK;
1284#endif
1285 default:
1286 /* The user might be trying to do "signal SIGSAK" where this system
1287 doesn't have SIGSAK. */
1288 warning ("Signal %s does not exist on this system.\n",
1289 target_signal_to_name (oursig));
1290 return 0;
1291 }
1292}
1293
1294/* Helper function for child_wait and the Lynx derivatives of child_wait.
1295 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1296 translation of that in OURSTATUS. */
1297void
1298store_waitstatus (ourstatus, hoststatus)
1299 struct target_waitstatus *ourstatus;
1300 int hoststatus;
1301{
1302#ifdef CHILD_SPECIAL_WAITSTATUS
1303 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1304 if it wants to deal with hoststatus. */
1305 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1306 return;
1307#endif
1308
1309 if (WIFEXITED (hoststatus))
1310 {
1311 ourstatus->kind = TARGET_WAITKIND_EXITED;
1312 ourstatus->value.integer = WEXITSTATUS (hoststatus);
1313 }
1314 else if (!WIFSTOPPED (hoststatus))
1315 {
1316 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1317 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1318 }
1319 else
1320 {
1321 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1322 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1323 }
1324}
1325
cad1498f
SG
1326\f
1327/* Returns zero to leave the inferior alone, one to interrupt it. */
1328int (*target_activity_function) PARAMS ((void));
1329int target_activity_fd;
67ac9759 1330\f
49781499
JK
1331/* Convert a normal process ID to a string. Returns the string in a static
1332 buffer. */
1333
1334char *
1335normal_pid_to_str (pid)
1336 int pid;
1337{
1338 static char buf[30];
1339
1340 sprintf (buf, "process %d", pid);
1341
1342 return buf;
1343}
67ac9759 1344\f
bd5635a1
RP
1345static char targ_desc[] =
1346 "Names of targets and files being debugged.\n\
1347Shows the entire stack of targets currently in use (including the exec-file,\n\
1348core-file, and process, if any), as well as the symbol file name.";
1349
1350void
1351_initialize_targets ()
1352{
cad1498f 1353 push_target (&dummy_target);
bd5635a1 1354
bd5635a1
RP
1355 add_info ("target", target_info, targ_desc);
1356 add_info ("files", target_info, targ_desc);
67ac9759
JK
1357
1358 if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
1359 abort ();
bd5635a1 1360}
This page took 0.241084 seconds and 4 git commands to generate.