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