* pr-8894.exp, pr-8894.ch, pr-8894-grt.ch: New test case.
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
bd5635a1 1/* Select target systems and architectures at runtime for GDB.
b8ea4fec 2 Copyright 1990, 1992, 1993, 1994, 1995 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>
2b576293 24#include "gdb_string.h"
bd5635a1
RP
25#include "target.h"
26#include "gdbcmd.h"
27#include "symtab.h"
28#include "inferior.h"
29#include "bfd.h"
30#include "symfile.h"
51b57ded 31#include "objfiles.h"
4ad0021e
JK
32#include "wait.h"
33#include <signal.h>
bd5635a1 34
e17960fb
JG
35extern int errno;
36
7919c3ed
JG
37static void
38target_info PARAMS ((char *, int));
39
40static void
41cleanup_target PARAMS ((struct target_ops *));
42
43static void
44maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
45
46static void
47maybe_kill_then_attach PARAMS ((char *, int));
48
49static void
50kill_or_be_killed PARAMS ((int));
51
52static void
53default_terminal_info PARAMS ((char *, int));
54
55static int
56nosymbol PARAMS ((char *, CORE_ADDR *));
57
7919c3ed
JG
58static void
59tcomplain PARAMS ((void));
60
61static int
4fc5969d 62nomemory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
7919c3ed 63
597dc86b
SG
64static int
65return_zero PARAMS ((void));
66
7919c3ed
JG
67static void
68ignore PARAMS ((void));
597dc86b 69
7919c3ed
JG
70static void
71target_command PARAMS ((char *, int));
bd5635a1 72
597dc86b
SG
73static struct target_ops *
74find_default_run_target PARAMS ((char *));
75
bd5635a1
RP
76/* Pointer to array of target architecture structures; the size of the
77 array; the current index into the array; the allocated size of the
78 array. */
79struct target_ops **target_structs;
80unsigned target_struct_size;
81unsigned target_struct_index;
82unsigned target_struct_allocsize;
83#define DEFAULT_ALLOCSIZE 10
84
85/* The initial current target, so that there is always a semi-valid
86 current target. */
87
43fc25c8
JL
88struct target_ops dummy_target = {
89 "None", /* to_shortname */
90 "None", /* to_longname */
91 "", /* to_doc */
92 0, /* to_open */
93 0, /* to_close */
94 find_default_attach, /* to_attach */
95 0, /* to_detach */
96 0, /* to_resume */
97 0, /* to_wait */
98 0, /* to_fetch_registers */
99 0, /* to_store_registers */
100 0, /* to_prepare_to_store */
101 0, /* to_xfer_memory */
102 0, /* to_files_info */
103 0, /* to_insert_breakpoint */
104 0, /* to_remove_breakpoint */
105 0, /* to_terminal_init */
106 0, /* to_terminal_inferior */
107 0, /* to_terminal_ours_for_output */
108 0, /* to_terminal_ours */
109 0, /* to_terminal_info */
110 0, /* to_kill */
111 0, /* to_load */
112 0, /* to_lookup_symbol */
113 find_default_create_inferior, /* to_create_inferior */
114 0, /* to_mourn_inferior */
115 0, /* to_can_run */
116 0, /* to_notice_signals */
117 0, /* to_thread_alive */
118 0, /* to_stop */
119 dummy_stratum, /* to_stratum */
120 0, /* to_next */
121 0, /* to_next */
122 0, /* to_has_all_memory */
123 0, /* to_has_memory */
124 0, /* to_has_registers */
125 0, /* to_has_execution */
126 0, /* to_sections */
127 0, /* to_sections_end */
128 OPS_MAGIC, /* to_magic */
bd5635a1
RP
129};
130
cad1498f 131/* Top of target stack. */
bd5635a1 132
cad1498f 133struct target_stack_item *target_stack;
bd5635a1 134
cad1498f
SG
135/* The target structure we are currently using to talk to a process
136 or file or whatever "inferior" we have. */
bd5635a1 137
cad1498f 138struct target_ops current_target;
bd5635a1 139
f2fc6e7a
JK
140/* Command list for target. */
141
142static struct cmd_list_element *targetlist = NULL;
143
16d2cc80
SS
144/* Nonzero if we are debugging an attached outside process
145 rather than an inferior. */
146
147int attach_flag;
148
43fc25c8
JL
149#ifdef MAINTENANCE_CMDS
150/* Non-zero if we want to see trace of target level stuff. */
151
152static int targetdebug = 0;
153
154static void setup_target_debug PARAMS ((void));
155
156#endif
157
f2fc6e7a
JK
158/* The user just typed 'target' without the name of a target. */
159
e1ce8aa5 160/* ARGSUSED */
f2fc6e7a
JK
161static void
162target_command (arg, from_tty)
163 char *arg;
164 int from_tty;
165{
597dc86b 166 fputs_filtered ("Argument required (target name). Try `help target'\n",
67ac9759 167 gdb_stdout);
f2fc6e7a 168}
bd5635a1
RP
169
170/* Add a possible target architecture to the list. */
171
172void
173add_target (t)
174 struct target_ops *t;
175{
bd5635a1
RP
176 if (!target_structs)
177 {
178 target_struct_allocsize = DEFAULT_ALLOCSIZE;
179 target_structs = (struct target_ops **) xmalloc
180 (target_struct_allocsize * sizeof (*target_structs));
181 }
182 if (target_struct_size >= target_struct_allocsize)
183 {
184 target_struct_allocsize *= 2;
7919c3ed
JG
185 target_structs = (struct target_ops **)
186 xrealloc ((char *) target_structs,
187 target_struct_allocsize * sizeof (*target_structs));
bd5635a1
RP
188 }
189 target_structs[target_struct_size++] = t;
a3ecbe7c 190/* cleanup_target (t);*/
f2fc6e7a
JK
191
192 if (targetlist == NULL)
193 add_prefix_cmd ("target", class_run, target_command,
194 "Connect to a target machine or process.\n\
195The first argument is the type or protocol of the target machine.\n\
196Remaining arguments are interpreted by the target protocol. For more\n\
197information on the arguments for a particular protocol, type\n\
198`help target ' followed by the protocol name.",
199 &targetlist, "target ", 0, &cmdlist);
200 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
bd5635a1
RP
201}
202
203/* Stub functions */
204
205static void
206ignore ()
207{
208}
209
210/* ARGSUSED */
211static int
4fc5969d 212nomemory (memaddr, myaddr, len, write, t)
bd5635a1
RP
213 CORE_ADDR memaddr;
214 char *myaddr;
215 int len;
216 int write;
4fc5969d 217 struct target_ops *t;
bd5635a1 218{
51b57ded 219 errno = EIO; /* Can't read/write this location */
bd5635a1
RP
220 return 0; /* No bytes handled */
221}
222
223static void
224tcomplain ()
225{
226 error ("You can't do that when your target is `%s'",
cad1498f 227 current_target.to_shortname);
bd5635a1
RP
228}
229
80d68b1d 230void
bd5635a1
RP
231noprocess ()
232{
233 error ("You can't do that without a process to debug");
234}
235
e1ce8aa5 236/* ARGSUSED */
bd5635a1
RP
237static int
238nosymbol (name, addrp)
239 char *name;
240 CORE_ADDR *addrp;
241{
242 return 1; /* Symbol does not exist in target env */
243}
244
e1ce8aa5 245/* ARGSUSED */
bd5635a1
RP
246static void
247default_terminal_info (args, from_tty)
248 char *args;
249 int from_tty;
250{
67ac9759 251 printf_unfiltered("No saved terminal information.\n");
bd5635a1
RP
252}
253
bd5635a1
RP
254/* This is the default target_create_inferior and target_attach function.
255 If the current target is executing, it asks whether to kill it off.
256 If this function returns without calling error(), it has killed off
257 the target, and the operation should be attempted. */
258
259static void
260kill_or_be_killed (from_tty)
261 int from_tty;
262{
bd5635a1
RP
263 if (target_has_execution)
264 {
67ac9759 265 printf_unfiltered ("You are already running a program:\n");
bd5635a1
RP
266 target_files_info ();
267 if (query ("Kill it? ")) {
e17960fb 268 target_kill ();
bd5635a1
RP
269 if (target_has_execution)
270 error ("Killing the program did not help.");
271 return;
272 } else {
273 error ("Program not killed.");
274 }
275 }
276 tcomplain();
277}
278
279static void
280maybe_kill_then_attach (args, from_tty)
281 char *args;
282 int from_tty;
283{
284 kill_or_be_killed (from_tty);
285 target_attach (args, from_tty);
286}
287
288static void
289maybe_kill_then_create_inferior (exec, args, env)
290 char *exec;
291 char *args;
292 char **env;
293{
294 kill_or_be_killed (0);
295 target_create_inferior (exec, args, env);
296}
297
298/* Clean up a target struct so it no longer has any zero pointers in it.
299 We default entries, at least to stubs that print error messages. */
300
301static void
302cleanup_target (t)
303 struct target_ops *t;
304{
305
bd5635a1
RP
306#define de_fault(field, value) \
307 if (!t->field) t->field = value
308
309 /* FIELD DEFAULT VALUE */
310
7919c3ed 311 de_fault (to_open, (void (*)())tcomplain);
bd5635a1
RP
312 de_fault (to_close, (void (*)())ignore);
313 de_fault (to_attach, maybe_kill_then_attach);
314 de_fault (to_detach, (void (*)())ignore);
315 de_fault (to_resume, (void (*)())noprocess);
7919c3ed
JG
316 de_fault (to_wait, (int (*)())noprocess);
317 de_fault (to_fetch_registers, (void (*)())ignore);
e17960fb 318 de_fault (to_store_registers, (void (*)())noprocess);
bd5635a1 319 de_fault (to_prepare_to_store, (void (*)())noprocess);
7919c3ed
JG
320 de_fault (to_xfer_memory, (int (*)())nomemory);
321 de_fault (to_files_info, (void (*)())ignore);
bd5635a1
RP
322 de_fault (to_insert_breakpoint, memory_insert_breakpoint);
323 de_fault (to_remove_breakpoint, memory_remove_breakpoint);
324 de_fault (to_terminal_init, ignore);
325 de_fault (to_terminal_inferior, ignore);
326 de_fault (to_terminal_ours_for_output,ignore);
327 de_fault (to_terminal_ours, ignore);
328 de_fault (to_terminal_info, default_terminal_info);
329 de_fault (to_kill, (void (*)())noprocess);
7919c3ed 330 de_fault (to_load, (void (*)())tcomplain);
bd5635a1
RP
331 de_fault (to_lookup_symbol, nosymbol);
332 de_fault (to_create_inferior, maybe_kill_then_create_inferior);
333 de_fault (to_mourn_inferior, (void (*)())noprocess);
597dc86b 334 de_fault (to_can_run, return_zero);
49781499 335 de_fault (to_notice_signals, (void (*)())ignore);
2b576293 336 de_fault (to_thread_alive, (int (*)())ignore);
43fc25c8 337 de_fault (to_stop, (void (*)())ignore);
bd5635a1
RP
338
339#undef de_fault
340}
341
cad1498f
SG
342/* Go through the target stack from top to bottom, copying over zero entries in
343 current_target. In effect, we are doing class inheritance through the
344 pushed target vectors. */
345
346static void
347update_current_target ()
348{
349 struct target_stack_item *item;
350 struct target_ops *t;
351
352 /* First, reset current_target */
353 memset (&current_target, 0, sizeof current_target);
354
355 for (item = target_stack; item; item = item->next)
356 {
357 t = item->target_ops;
358
359#define INHERIT(FIELD, TARGET) \
360 if (!current_target.FIELD) \
361 current_target.FIELD = TARGET->FIELD
362
363 INHERIT (to_shortname, t);
364 INHERIT (to_longname, t);
365 INHERIT (to_doc, t);
366 INHERIT (to_open, t);
367 INHERIT (to_close, t);
368 INHERIT (to_attach, t);
369 INHERIT (to_detach, t);
370 INHERIT (to_resume, t);
371 INHERIT (to_wait, t);
372 INHERIT (to_fetch_registers, t);
373 INHERIT (to_store_registers, t);
374 INHERIT (to_prepare_to_store, t);
375 INHERIT (to_xfer_memory, t);
376 INHERIT (to_files_info, t);
377 INHERIT (to_insert_breakpoint, t);
378 INHERIT (to_remove_breakpoint, t);
379 INHERIT (to_terminal_init, t);
380 INHERIT (to_terminal_inferior, t);
381 INHERIT (to_terminal_ours_for_output, t);
382 INHERIT (to_terminal_ours, t);
383 INHERIT (to_terminal_info, t);
384 INHERIT (to_kill, t);
385 INHERIT (to_load, t);
386 INHERIT (to_lookup_symbol, t);
387 INHERIT (to_create_inferior, t);
388 INHERIT (to_mourn_inferior, t);
389 INHERIT (to_can_run, t);
390 INHERIT (to_notice_signals, t);
43fc25c8
JL
391 INHERIT (to_thread_alive, t);
392 INHERIT (to_stop, t);
cad1498f
SG
393 INHERIT (to_stratum, t);
394 INHERIT (DONT_USE, t);
395 INHERIT (to_has_all_memory, t);
396 INHERIT (to_has_memory, t);
397 INHERIT (to_has_stack, t);
398 INHERIT (to_has_registers, t);
399 INHERIT (to_has_execution, t);
400 INHERIT (to_sections, t);
401 INHERIT (to_sections_end, t);
402 INHERIT (to_magic, t);
403
404#undef INHERIT
405 }
406}
407
bd5635a1
RP
408/* Push a new target type into the stack of the existing target accessors,
409 possibly superseding some of the existing accessors.
410
411 Result is zero if the pushed target ended up on top of the stack,
412 nonzero if at least one target is on top of it.
413
414 Rather than allow an empty stack, we always have the dummy target at
415 the bottom stratum, so we can call the function vectors without
416 checking them. */
417
418int
419push_target (t)
420 struct target_ops *t;
421{
cad1498f
SG
422 struct target_stack_item *cur, *prev, *tmp;
423
424 /* Check magic number. If wrong, it probably means someone changed
425 the struct definition, but not all the places that initialize one. */
426 if (t->to_magic != OPS_MAGIC)
427 {
428 fprintf_unfiltered(gdb_stderr,
429 "Magic number of %s target struct wrong\n",
430 t->to_shortname);
431 abort();
432 }
433
434 /* Find the proper stratum to install this target in. */
435
436 for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
437 {
438 if ((int)(t->to_stratum) >= (int)(cur->target_ops->to_stratum))
439 break;
440 }
441
442 /* If there's already targets at this stratum, remove them. */
443
444 if (cur)
445 while (t->to_stratum == cur->target_ops->to_stratum)
446 {
447 /* There's already something on this stratum. Close it off. */
448 (cur->target_ops->to_close) (0);
449 if (prev)
450 prev->next = cur->next; /* Unchain old target_ops */
451 else
452 target_stack = cur->next; /* Unchain first on list */
453 tmp = cur->next;
454 free (cur);
455 cur = tmp;
456 }
457
458 /* We have removed all targets in our stratum, now add the new one. */
459
f6d5d006
JK
460 tmp = (struct target_stack_item *)
461 xmalloc (sizeof (struct target_stack_item));
cad1498f
SG
462 tmp->next = cur;
463 tmp->target_ops = t;
bd5635a1 464
bd5635a1 465 if (prev)
cad1498f 466 prev->next = tmp;
bd5635a1 467 else
cad1498f
SG
468 target_stack = tmp;
469
470 update_current_target ();
bd5635a1 471
cad1498f 472 cleanup_target (&current_target); /* Fill in the gaps */
43fc25c8
JL
473
474#ifdef MAINTENANCE_CMDS
475 if (targetdebug)
476 setup_target_debug ();
477#endif
478
bd5635a1
RP
479 return prev != 0;
480}
481
482/* Remove a target_ops vector from the stack, wherever it may be.
cad1498f 483 Return how many times it was removed (0 or 1). */
bd5635a1
RP
484
485int
486unpush_target (t)
487 struct target_ops *t;
488{
cad1498f 489 struct target_stack_item *cur, *prev;
bd5635a1 490
a3ecbe7c
SG
491 if (t->to_close)
492 t->to_close (0); /* Let it clean up */
67e947de 493
cad1498f
SG
494 /* Look for the specified target. Note that we assume that a target
495 can only occur once in the target stack. */
496
497 for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
498 if (cur->target_ops == t)
499 break;
500
501 if (!cur)
502 return 0; /* Didn't find target_ops, quit now */
503
504 /* Unchain the target */
505
506 if (!prev)
67e947de 507 target_stack = cur->next;
cad1498f
SG
508 else
509 prev->next = cur->next;
510
511 free (cur); /* Release the target_stack_item */
512
67e947de
SG
513 update_current_target ();
514 cleanup_target (&current_target);
cad1498f
SG
515
516 return 1;
bd5635a1
RP
517}
518
519void
520pop_target ()
521{
cad1498f
SG
522 (current_target.to_close)(0); /* Let it clean up */
523 if (unpush_target (target_stack->target_ops) == 1)
524 return;
525
526 fprintf_unfiltered(gdb_stderr,
527 "pop_target couldn't find target %s\n",
528 current_target.to_shortname);
529 abort();
bd5635a1
RP
530}
531
49781499 532#undef MIN
e17960fb
JG
533#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
534
4ad0021e
JK
535/* target_read_string -- read a null terminated string, up to LEN bytes,
536 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
537 Set *STRING to a pointer to malloc'd memory containing the data; the caller
538 is responsible for freeing it. Return the number of bytes successfully
539 read. */
e17960fb
JG
540
541int
4ad0021e 542target_read_string (memaddr, string, len, errnop)
e17960fb 543 CORE_ADDR memaddr;
4ad0021e 544 char **string;
e17960fb 545 int len;
4ad0021e 546 int *errnop;
e17960fb
JG
547{
548 int tlen, origlen, offset, i;
549 char buf[4];
4ad0021e
JK
550 int errcode = 0;
551 char *buffer;
552 int buffer_allocated;
553 char *bufptr;
554 unsigned int nbytes_read = 0;
555
556 /* Small for testing. */
557 buffer_allocated = 4;
558 buffer = xmalloc (buffer_allocated);
559 bufptr = buffer;
e17960fb
JG
560
561 origlen = len;
562
563 while (len > 0)
564 {
565 tlen = MIN (len, 4 - (memaddr & 3));
566 offset = memaddr & 3;
567
4ad0021e
JK
568 errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
569 if (errcode != 0)
570 goto done;
571
572 if (bufptr - buffer + tlen > buffer_allocated)
573 {
574 unsigned int bytes;
575 bytes = bufptr - buffer;
576 buffer_allocated *= 2;
577 buffer = xrealloc (buffer, buffer_allocated);
578 bufptr = buffer + bytes;
579 }
e17960fb
JG
580
581 for (i = 0; i < tlen; i++)
582 {
4ad0021e 583 *bufptr++ = buf[i + offset];
e17960fb 584 if (buf[i + offset] == '\000')
4ad0021e
JK
585 {
586 nbytes_read += i + 1;
587 goto done;
588 }
e17960fb
JG
589 }
590
591 memaddr += tlen;
592 len -= tlen;
4ad0021e 593 nbytes_read += tlen;
e17960fb 594 }
4ad0021e
JK
595 done:
596 if (errnop != NULL)
597 *errnop = errcode;
598 if (string != NULL)
599 *string = buffer;
600 return nbytes_read;
e17960fb
JG
601}
602
49781499
JK
603/* Read LEN bytes of target memory at address MEMADDR, placing the results in
604 GDB's memory at MYADDR. Returns either 0 for success or an errno value
605 if any error occurs.
bd5635a1 606
49781499
JK
607 If an error occurs, no guarantee is made about the contents of the data at
608 MYADDR. In particular, the caller should not depend upon partial reads
609 filling the buffer with good data. There is no way for the caller to know
610 how much good data might have been transfered anyway. Callers that can
611 deal with partial reads should call target_read_memory_partial. */
bd5635a1
RP
612
613int
614target_read_memory (memaddr, myaddr, len)
615 CORE_ADDR memaddr;
616 char *myaddr;
617 int len;
618{
619 return target_xfer_memory (memaddr, myaddr, len, 0);
620}
621
49781499
JK
622/* Read LEN bytes of target memory at address MEMADDR, placing the results
623 in GDB's memory at MYADDR. Returns a count of the bytes actually read,
624 and optionally an errno value in the location pointed to by ERRNOPTR
625 if ERRNOPTR is non-null. */
626
627int
628target_read_memory_partial (memaddr, myaddr, len, errnoptr)
629 CORE_ADDR memaddr;
630 char *myaddr;
631 int len;
632 int *errnoptr;
633{
634 int nread; /* Number of bytes actually read. */
635 int errcode; /* Error from last read. */
636
637 /* First try a complete read. */
638 errcode = target_xfer_memory (memaddr, myaddr, len, 0);
639 if (errcode == 0)
640 {
641 /* Got it all. */
642 nread = len;
643 }
644 else
645 {
646 /* Loop, reading one byte at a time until we get as much as we can. */
647 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
648 {
649 errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0);
650 }
651 /* If an error, the last read was unsuccessful, so adjust count. */
652 if (errcode != 0)
653 {
654 nread--;
655 }
656 }
657 if (errnoptr != NULL)
658 {
659 *errnoptr = errcode;
660 }
661 return (nread);
662}
663
bd5635a1
RP
664int
665target_write_memory (memaddr, myaddr, len)
666 CORE_ADDR memaddr;
667 char *myaddr;
668 int len;
669{
670 return target_xfer_memory (memaddr, myaddr, len, 1);
671}
672
49781499
JK
673/* Move memory to or from the targets. Iterate until all of it has
674 been moved, if necessary. The top target gets priority; anything
675 it doesn't want, is offered to the next one down, etc. Note the
676 business with curlen: if an early target says "no, but I have a
677 boundary overlapping this xfer" then we shorten what we offer to
678 the subsequent targets so the early guy will get a chance at the
679 tail before the subsequent ones do.
680
681 Result is 0 or errno value. */
682
bd5635a1
RP
683int
684target_xfer_memory (memaddr, myaddr, len, write)
685 CORE_ADDR memaddr;
686 char *myaddr;
687 int len;
688 int write;
689{
690 int curlen;
691 int res;
692 struct target_ops *t;
cad1498f 693 struct target_stack_item *item;
49781499
JK
694
695 /* to_xfer_memory is not guaranteed to set errno, even when it returns
696 0. */
697 errno = 0;
698
bd5635a1 699 /* The quick case is that the top target does it all. */
cad1498f
SG
700 res = current_target.to_xfer_memory
701 (memaddr, myaddr, len, write, &current_target);
bd5635a1
RP
702 if (res == len)
703 return 0;
704
705 if (res > 0)
706 goto bump;
707 /* If res <= 0 then we call it again in the loop. Ah well. */
708
709 for (; len > 0;)
710 {
711 curlen = len; /* Want to do it all */
cad1498f 712 for (item = target_stack; item; item = item->next)
bd5635a1 713 {
cad1498f 714 t = item->target_ops;
a3ecbe7c
SG
715 if (!t->to_has_memory)
716 continue;
cad1498f
SG
717
718 res = t->to_xfer_memory (memaddr, myaddr, curlen, write, t);
719 if (res > 0)
720 break; /* Handled all or part of xfer */
721 if (t->to_has_all_memory)
722 break;
bd5635a1 723 }
cad1498f 724
bd5635a1
RP
725 if (res <= 0)
726 {
727 /* If this address is for nonexistent memory,
728 read zeros if reading, or do nothing if writing. Return error. */
729 if (!write)
a8e033f2 730 memset (myaddr, 0, len);
e17960fb
JG
731 if (errno == 0)
732 return EIO;
733 else
734 return errno;
bd5635a1
RP
735 }
736bump:
737 memaddr += res;
738 myaddr += res;
739 len -= res;
740 }
741 return 0; /* We managed to cover it all somehow. */
742}
743
744
e1ce8aa5 745/* ARGSUSED */
bd5635a1
RP
746static void
747target_info (args, from_tty)
748 char *args;
749 int from_tty;
750{
751 struct target_ops *t;
cad1498f 752 struct target_stack_item *item;
bd5635a1
RP
753 int has_all_mem = 0;
754
80d68b1d 755 if (symfile_objfile != NULL)
67ac9759 756 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
bd5635a1
RP
757
758#ifdef FILES_INFO_HOOK
759 if (FILES_INFO_HOOK ())
760 return;
761#endif
762
cad1498f 763 for (item = target_stack; item; item = item->next)
bd5635a1 764 {
cad1498f
SG
765 t = item->target_ops;
766
a3ecbe7c
SG
767 if (!t->to_has_memory)
768 continue;
769
bd5635a1
RP
770 if ((int)(t->to_stratum) <= (int)dummy_stratum)
771 continue;
772 if (has_all_mem)
cad1498f 773 printf_unfiltered("\tWhile running this, GDB does not access memory from...\n");
67ac9759 774 printf_unfiltered("%s:\n", t->to_longname);
e17960fb 775 (t->to_files_info)(t);
bd5635a1
RP
776 has_all_mem = t->to_has_all_memory;
777 }
778}
779
f2fc6e7a
JK
780/* This is to be called by the open routine before it does
781 anything. */
bd5635a1 782
f2fc6e7a
JK
783void
784target_preopen (from_tty)
bd5635a1
RP
785 int from_tty;
786{
bd5635a1
RP
787 dont_repeat();
788
bd5635a1
RP
789 if (target_has_execution)
790 {
791 if (query ("A program is being debugged already. Kill it? "))
e17960fb 792 target_kill ();
bd5635a1
RP
793 else
794 error ("Program not killed.");
795 }
4ad0021e
JK
796
797 /* Calling target_kill may remove the target from the stack. But if
798 it doesn't (which seems like a win for UDI), remove it now. */
799
800 if (target_has_execution)
801 pop_target ();
bd5635a1
RP
802}
803
49781499
JK
804/* Detach a target after doing deferred register stores. */
805
806void
807target_detach (args, from_tty)
808 char *args;
809 int from_tty;
810{
811 /* Handle any optimized stores to the inferior. */
812#ifdef DO_DEFERRED_STORES
813 DO_DEFERRED_STORES;
814#endif
cad1498f 815 (current_target.to_detach) (args, from_tty);
49781499
JK
816}
817
1340861c
KH
818void
819target_link (modname, t_reloc)
820 char *modname;
821 CORE_ADDR *t_reloc;
822{
cad1498f 823 if (STREQ(current_target.to_shortname, "rombug"))
1340861c 824 {
cad1498f 825 (current_target.to_lookup_symbol) (modname, t_reloc);
16d2cc80
SS
826 if (*t_reloc == 0)
827 error("Unable to link to %s and get relocation in rombug", modname);
1340861c
KH
828 }
829 else
830 *t_reloc = (CORE_ADDR)-1;
831}
832
597dc86b
SG
833/* Look through the list of possible targets for a target that can
834 execute a run or attach command without any other data. This is
835 used to locate the default process stratum.
836
837 Result is always valid (error() is called for errors). */
838
839static struct target_ops *
840find_default_run_target (do_mesg)
841 char *do_mesg;
842{
843 struct target_ops **t;
49781499 844 struct target_ops *runable = NULL;
597dc86b
SG
845 int count;
846
847 count = 0;
848
849 for (t = target_structs; t < target_structs + target_struct_size;
850 ++t)
851 {
43fc25c8 852 if ((*t)->to_can_run && target_can_run(*t))
597dc86b
SG
853 {
854 runable = *t;
855 ++count;
856 }
857 }
858
859 if (count != 1)
860 error ("Don't know how to %s. Try \"help target\".", do_mesg);
861
862 return runable;
863}
864
865void
866find_default_attach (args, from_tty)
867 char *args;
868 int from_tty;
869{
870 struct target_ops *t;
871
872 t = find_default_run_target("attach");
873 (t->to_attach) (args, from_tty);
874 return;
875}
876
877void
878find_default_create_inferior (exec_file, allargs, env)
879 char *exec_file;
880 char *allargs;
881 char **env;
882{
883 struct target_ops *t;
884
885 t = find_default_run_target("run");
886 (t->to_create_inferior) (exec_file, allargs, env);
887 return;
888}
889
890static int
891return_zero ()
892{
893 return 0;
894}
895
49781499
JK
896struct target_ops *
897find_core_target ()
898{
899 struct target_ops **t;
900 struct target_ops *runable = NULL;
901 int count;
902
903 count = 0;
904
905 for (t = target_structs; t < target_structs + target_struct_size;
906 ++t)
907 {
908 if ((*t)->to_stratum == core_stratum)
909 {
910 runable = *t;
911 ++count;
912 }
913 }
914
915 return(count == 1 ? runable : NULL);
916}
67ac9759 917\f
16d2cc80
SS
918/* The inferior process has died. Long live the inferior! */
919
920void
921generic_mourn_inferior ()
922{
cad1498f
SG
923 extern int show_breakpoint_hit_counts;
924
16d2cc80
SS
925 inferior_pid = 0;
926 attach_flag = 0;
927 breakpoint_init_inferior ();
928 registers_changed ();
929
930#ifdef CLEAR_DEFERRED_STORES
931 /* Delete any pending stores to the inferior... */
932 CLEAR_DEFERRED_STORES;
933#endif
934
935 reopen_exec_file ();
936 reinit_frame_cache ();
937
938 /* It is confusing to the user for ignore counts to stick around
939 from previous runs of the inferior. So clear them. */
cad1498f
SG
940 /* However, it is more confusing for the ignore counts to disappear when
941 using hit counts. So don't clear them if we're counting hits. */
942 if (!show_breakpoint_hit_counts)
943 breakpoint_clear_ignore_counts ();
16d2cc80
SS
944}
945\f
67ac9759
JK
946/* This table must match in order and size the signals in enum target_signal
947 in target.h. */
948static struct {
949 char *name;
950 char *string;
951 } signals [] =
952{
953 {"0", "Signal 0"},
954 {"SIGHUP", "Hangup"},
955 {"SIGINT", "Interrupt"},
956 {"SIGQUIT", "Quit"},
957 {"SIGILL", "Illegal instruction"},
958 {"SIGTRAP", "Trace/breakpoint trap"},
959 {"SIGABRT", "Aborted"},
960 {"SIGEMT", "Emulation trap"},
961 {"SIGFPE", "Arithmetic exception"},
962 {"SIGKILL", "Killed"},
963 {"SIGBUS", "Bus error"},
964 {"SIGSEGV", "Segmentation fault"},
965 {"SIGSYS", "Bad system call"},
966 {"SIGPIPE", "Broken pipe"},
967 {"SIGALRM", "Alarm clock"},
968 {"SIGTERM", "Terminated"},
969 {"SIGURG", "Urgent I/O condition"},
970 {"SIGSTOP", "Stopped (signal)"},
971 {"SIGTSTP", "Stopped (user)"},
972 {"SIGCONT", "Continued"},
973 {"SIGCHLD", "Child status changed"},
974 {"SIGTTIN", "Stopped (tty input)"},
975 {"SIGTTOU", "Stopped (tty output)"},
976 {"SIGIO", "I/O possible"},
977 {"SIGXCPU", "CPU time limit exceeded"},
978 {"SIGXFSZ", "File size limit exceeded"},
979 {"SIGVTALRM", "Virtual timer expired"},
980 {"SIGPROF", "Profiling timer expired"},
981 {"SIGWINCH", "Window size changed"},
982 {"SIGLOST", "Resource lost"},
983 {"SIGUSR1", "User defined signal 1"},
984 {"SIGUSR2", "User defined signal 2"},
985 {"SIGPWR", "Power fail/restart"},
986 {"SIGPOLL", "Pollable event occurred"},
987 {"SIGWIND", "SIGWIND"},
988 {"SIGPHONE", "SIGPHONE"},
989 {"SIGWAITING", "Process's LWPs are blocked"},
990 {"SIGLWP", "Signal LWP"},
991 {"SIGDANGER", "Swap space dangerously low"},
992 {"SIGGRANT", "Monitor mode granted"},
993 {"SIGRETRACT", "Need to relinguish monitor mode"},
994 {"SIGMSG", "Monitor mode data available"},
995 {"SIGSOUND", "Sound completed"},
996 {"SIGSAK", "Secure attention"},
43fc25c8
JL
997 {"SIGPRIO", "SIGPRIO"},
998 {"SIG33", "Real-time event 33"},
999 {"SIG34", "Real-time event 34"},
1000 {"SIG35", "Real-time event 35"},
1001 {"SIG36", "Real-time event 36"},
1002 {"SIG37", "Real-time event 37"},
1003 {"SIG38", "Real-time event 38"},
1004 {"SIG39", "Real-time event 39"},
1005 {"SIG40", "Real-time event 40"},
1006 {"SIG41", "Real-time event 41"},
1007 {"SIG42", "Real-time event 42"},
1008 {"SIG43", "Real-time event 43"},
1009 {"SIG44", "Real-time event 44"},
1010 {"SIG45", "Real-time event 45"},
1011 {"SIG46", "Real-time event 46"},
1012 {"SIG47", "Real-time event 47"},
1013 {"SIG48", "Real-time event 48"},
1014 {"SIG49", "Real-time event 49"},
1015 {"SIG50", "Real-time event 50"},
1016 {"SIG51", "Real-time event 51"},
1017 {"SIG52", "Real-time event 52"},
1018 {"SIG53", "Real-time event 53"},
1019 {"SIG54", "Real-time event 54"},
1020 {"SIG55", "Real-time event 55"},
1021 {"SIG56", "Real-time event 56"},
1022 {"SIG57", "Real-time event 57"},
1023 {"SIG58", "Real-time event 58"},
1024 {"SIG59", "Real-time event 59"},
1025 {"SIG60", "Real-time event 60"},
1026 {"SIG61", "Real-time event 61"},
1027 {"SIG62", "Real-time event 62"},
1028 {"SIG63", "Real-time event 63"},
1029
67ac9759 1030 {NULL, "Unknown signal"},
4ad0021e
JK
1031 {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
1032
67ac9759
JK
1033 /* Last entry, used to check whether the table is the right size. */
1034 {NULL, "TARGET_SIGNAL_MAGIC"}
1035};
1036
1037/* Return the string for a signal. */
1038char *
1039target_signal_to_string (sig)
1040 enum target_signal sig;
1041{
1042 return signals[sig].string;
1043}
1044
1045/* Return the name for a signal. */
1046char *
1047target_signal_to_name (sig)
1048 enum target_signal sig;
1049{
1050 if (sig == TARGET_SIGNAL_UNKNOWN)
1051 /* I think the code which prints this will always print it along with
1052 the string, so no need to be verbose. */
1053 return "?";
1054 return signals[sig].name;
1055}
1056
1057/* Given a name, return its signal. */
1058enum target_signal
1059target_signal_from_name (name)
1060 char *name;
1061{
1062 enum target_signal sig;
1063
1064 /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
1065 for TARGET_SIGNAL_SIGCHLD. SIGIOT, on the other hand, is more
1066 questionable; seems like by now people should call it SIGABRT
1067 instead. */
1068
4ad0021e
JK
1069 /* This ugly cast brought to you by the native VAX compiler. */
1070 for (sig = TARGET_SIGNAL_HUP;
1071 signals[sig].name != NULL;
1072 sig = (enum target_signal)((int)sig + 1))
67ac9759
JK
1073 if (STREQ (name, signals[sig].name))
1074 return sig;
1075 return TARGET_SIGNAL_UNKNOWN;
1076}
4ad0021e
JK
1077\f
1078/* The following functions are to help certain targets deal
1079 with the signal/waitstatus stuff. They could just as well be in
1080 a file called native-utils.c or unixwaitstatus-utils.c or whatever. */
1081
1082/* Convert host signal to our signals. */
1083enum target_signal
1084target_signal_from_host (hostsig)
1085 int hostsig;
1086{
1087 /* A switch statement would make sense but would require special kludges
1088 to deal with the cases where more than one signal has the same number. */
1089
1090 if (hostsig == 0) return TARGET_SIGNAL_0;
1091
1092#if defined (SIGHUP)
1093 if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1094#endif
1095#if defined (SIGINT)
1096 if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1097#endif
1098#if defined (SIGQUIT)
1099 if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1100#endif
1101#if defined (SIGILL)
1102 if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1103#endif
1104#if defined (SIGTRAP)
1105 if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1106#endif
1107#if defined (SIGABRT)
1108 if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1109#endif
1110#if defined (SIGEMT)
1111 if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1112#endif
1113#if defined (SIGFPE)
1114 if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1115#endif
1116#if defined (SIGKILL)
1117 if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1118#endif
1119#if defined (SIGBUS)
1120 if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1121#endif
1122#if defined (SIGSEGV)
1123 if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1124#endif
1125#if defined (SIGSYS)
1126 if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1127#endif
1128#if defined (SIGPIPE)
1129 if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1130#endif
1131#if defined (SIGALRM)
1132 if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1133#endif
1134#if defined (SIGTERM)
1135 if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1136#endif
1137#if defined (SIGUSR1)
1138 if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1139#endif
1140#if defined (SIGUSR2)
1141 if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1142#endif
1143#if defined (SIGCLD)
1144 if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1145#endif
1146#if defined (SIGCHLD)
1147 if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1148#endif
1149#if defined (SIGPWR)
1150 if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1151#endif
1152#if defined (SIGWINCH)
1153 if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1154#endif
1155#if defined (SIGURG)
1156 if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1157#endif
1158#if defined (SIGIO)
1159 if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1160#endif
1161#if defined (SIGPOLL)
1162 if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1163#endif
1164#if defined (SIGSTOP)
1165 if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1166#endif
1167#if defined (SIGTSTP)
1168 if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1169#endif
1170#if defined (SIGCONT)
1171 if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1172#endif
1173#if defined (SIGTTIN)
1174 if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1175#endif
1176#if defined (SIGTTOU)
1177 if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1178#endif
1179#if defined (SIGVTALRM)
1180 if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1181#endif
1182#if defined (SIGPROF)
1183 if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1184#endif
1185#if defined (SIGXCPU)
1186 if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1187#endif
1188#if defined (SIGXFSZ)
1189 if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1190#endif
1191#if defined (SIGWIND)
1192 if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1193#endif
1194#if defined (SIGPHONE)
1195 if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1196#endif
1197#if defined (SIGLOST)
1198 if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1199#endif
1200#if defined (SIGWAITING)
1201 if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1202#endif
1203#if defined (SIGLWP)
1204 if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1205#endif
1206#if defined (SIGDANGER)
1207 if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1208#endif
1209#if defined (SIGGRANT)
1210 if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1211#endif
1212#if defined (SIGRETRACT)
1213 if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1214#endif
1215#if defined (SIGMSG)
1216 if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1217#endif
1218#if defined (SIGSOUND)
1219 if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1220#endif
1221#if defined (SIGSAK)
1222 if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
43fc25c8
JL
1223#endif
1224#if defined (SIGPRIO)
1225 if (hostsig == SIGPRIO) return TARGET_SIGNAL_PRIO;
1226#endif
1227#if defined (REALTIME_LO)
1228 if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
1229 return (enum target_signal)
1230 (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
4ad0021e
JK
1231#endif
1232 return TARGET_SIGNAL_UNKNOWN;
1233}
1234
1235int
1236target_signal_to_host (oursig)
1237 enum target_signal oursig;
1238{
1239 switch (oursig)
1240 {
1241 case TARGET_SIGNAL_0: return 0;
1242
1243#if defined (SIGHUP)
1244 case TARGET_SIGNAL_HUP: return SIGHUP;
1245#endif
1246#if defined (SIGINT)
1247 case TARGET_SIGNAL_INT: return SIGINT;
1248#endif
1249#if defined (SIGQUIT)
1250 case TARGET_SIGNAL_QUIT: return SIGQUIT;
1251#endif
1252#if defined (SIGILL)
1253 case TARGET_SIGNAL_ILL: return SIGILL;
1254#endif
1255#if defined (SIGTRAP)
1256 case TARGET_SIGNAL_TRAP: return SIGTRAP;
1257#endif
1258#if defined (SIGABRT)
1259 case TARGET_SIGNAL_ABRT: return SIGABRT;
1260#endif
1261#if defined (SIGEMT)
1262 case TARGET_SIGNAL_EMT: return SIGEMT;
1263#endif
1264#if defined (SIGFPE)
1265 case TARGET_SIGNAL_FPE: return SIGFPE;
1266#endif
1267#if defined (SIGKILL)
1268 case TARGET_SIGNAL_KILL: return SIGKILL;
1269#endif
1270#if defined (SIGBUS)
1271 case TARGET_SIGNAL_BUS: return SIGBUS;
1272#endif
1273#if defined (SIGSEGV)
1274 case TARGET_SIGNAL_SEGV: return SIGSEGV;
1275#endif
1276#if defined (SIGSYS)
1277 case TARGET_SIGNAL_SYS: return SIGSYS;
1278#endif
1279#if defined (SIGPIPE)
1280 case TARGET_SIGNAL_PIPE: return SIGPIPE;
1281#endif
1282#if defined (SIGALRM)
1283 case TARGET_SIGNAL_ALRM: return SIGALRM;
1284#endif
1285#if defined (SIGTERM)
1286 case TARGET_SIGNAL_TERM: return SIGTERM;
1287#endif
1288#if defined (SIGUSR1)
1289 case TARGET_SIGNAL_USR1: return SIGUSR1;
1290#endif
1291#if defined (SIGUSR2)
1292 case TARGET_SIGNAL_USR2: return SIGUSR2;
1293#endif
1294#if defined (SIGCHLD) || defined (SIGCLD)
1295 case TARGET_SIGNAL_CHLD:
1296#if defined (SIGCHLD)
1297 return SIGCHLD;
1298#else
1299 return SIGCLD;
1300#endif
1301#endif /* SIGCLD or SIGCHLD */
1302#if defined (SIGPWR)
1303 case TARGET_SIGNAL_PWR: return SIGPWR;
1304#endif
1305#if defined (SIGWINCH)
1306 case TARGET_SIGNAL_WINCH: return SIGWINCH;
1307#endif
1308#if defined (SIGURG)
1309 case TARGET_SIGNAL_URG: return SIGURG;
1310#endif
1311#if defined (SIGIO)
1312 case TARGET_SIGNAL_IO: return SIGIO;
1313#endif
1314#if defined (SIGPOLL)
1315 case TARGET_SIGNAL_POLL: return SIGPOLL;
1316#endif
1317#if defined (SIGSTOP)
1318 case TARGET_SIGNAL_STOP: return SIGSTOP;
1319#endif
1320#if defined (SIGTSTP)
1321 case TARGET_SIGNAL_TSTP: return SIGTSTP;
1322#endif
1323#if defined (SIGCONT)
1324 case TARGET_SIGNAL_CONT: return SIGCONT;
1325#endif
1326#if defined (SIGTTIN)
1327 case TARGET_SIGNAL_TTIN: return SIGTTIN;
1328#endif
1329#if defined (SIGTTOU)
1330 case TARGET_SIGNAL_TTOU: return SIGTTOU;
1331#endif
1332#if defined (SIGVTALRM)
1333 case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1334#endif
1335#if defined (SIGPROF)
1336 case TARGET_SIGNAL_PROF: return SIGPROF;
1337#endif
1338#if defined (SIGXCPU)
1339 case TARGET_SIGNAL_XCPU: return SIGXCPU;
1340#endif
1341#if defined (SIGXFSZ)
1342 case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1343#endif
1344#if defined (SIGWIND)
1345 case TARGET_SIGNAL_WIND: return SIGWIND;
1346#endif
1347#if defined (SIGPHONE)
1348 case TARGET_SIGNAL_PHONE: return SIGPHONE;
1349#endif
1350#if defined (SIGLOST)
1351 case TARGET_SIGNAL_LOST: return SIGLOST;
1352#endif
1353#if defined (SIGWAITING)
1354 case TARGET_SIGNAL_WAITING: return SIGWAITING;
1355#endif
1356#if defined (SIGLWP)
1357 case TARGET_SIGNAL_LWP: return SIGLWP;
1358#endif
1359#if defined (SIGDANGER)
1360 case TARGET_SIGNAL_DANGER: return SIGDANGER;
1361#endif
1362#if defined (SIGGRANT)
1363 case TARGET_SIGNAL_GRANT: return SIGGRANT;
1364#endif
1365#if defined (SIGRETRACT)
1366 case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1367#endif
1368#if defined (SIGMSG)
1369 case TARGET_SIGNAL_MSG: return SIGMSG;
1370#endif
1371#if defined (SIGSOUND)
1372 case TARGET_SIGNAL_SOUND: return SIGSOUND;
1373#endif
1374#if defined (SIGSAK)
1375 case TARGET_SIGNAL_SAK: return SIGSAK;
43fc25c8
JL
1376#endif
1377#if defined (SIGPRIO)
1378 case TARGET_SIGNAL_PRIO: return SIGPRIO;
4ad0021e
JK
1379#endif
1380 default:
43fc25c8
JL
1381#if defined (REALTIME_LO)
1382 if (oursig >= TARGET_SIGNAL_REALTIME_33
1383 && oursig <= TARGET_SIGNAL_REALTIME_63)
1384 {
1385 int retsig =
1386 (int)oursig - (int)TARGET_SIGNAL_REALTIME_33 + REALTIME_LO;
1387 if (retsig < REALTIME_HI)
1388 return retsig;
1389 }
1390#endif
4ad0021e
JK
1391 /* The user might be trying to do "signal SIGSAK" where this system
1392 doesn't have SIGSAK. */
1393 warning ("Signal %s does not exist on this system.\n",
1394 target_signal_to_name (oursig));
1395 return 0;
1396 }
1397}
1398
1399/* Helper function for child_wait and the Lynx derivatives of child_wait.
1400 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1401 translation of that in OURSTATUS. */
1402void
1403store_waitstatus (ourstatus, hoststatus)
1404 struct target_waitstatus *ourstatus;
1405 int hoststatus;
1406{
1407#ifdef CHILD_SPECIAL_WAITSTATUS
1408 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1409 if it wants to deal with hoststatus. */
1410 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1411 return;
1412#endif
1413
1414 if (WIFEXITED (hoststatus))
1415 {
1416 ourstatus->kind = TARGET_WAITKIND_EXITED;
1417 ourstatus->value.integer = WEXITSTATUS (hoststatus);
1418 }
1419 else if (!WIFSTOPPED (hoststatus))
1420 {
1421 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1422 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1423 }
1424 else
1425 {
1426 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1427 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1428 }
1429}
43fc25c8
JL
1430\f
1431/* In some circumstances we allow a command to specify a numeric
1432 signal. The idea is to keep these circumstances limited so that
1433 users (and scripts) develop portable habits. For comparison,
1434 POSIX.2 `kill' requires that 1,2,3,6,9,14, and 15 work (and using a
1435 numeric signal at all is obscelescent. We are slightly more
1436 lenient and allow 1-15 which should match host signal numbers on
1437 most systems. Use of symbolic signal names is strongly encouraged. */
4ad0021e 1438
43fc25c8
JL
1439enum target_signal
1440target_signal_from_command (num)
1441 int num;
1442{
1443 if (num >= 1 && num <= 15)
1444 return (enum target_signal)num;
1445 error ("Only signals 1-15 are valid as numeric signals.\n\
1446Use \"info signals\" for a list of symbolic signals.");
1447}
cad1498f
SG
1448\f
1449/* Returns zero to leave the inferior alone, one to interrupt it. */
1450int (*target_activity_function) PARAMS ((void));
1451int target_activity_fd;
67ac9759 1452\f
49781499
JK
1453/* Convert a normal process ID to a string. Returns the string in a static
1454 buffer. */
1455
1456char *
1457normal_pid_to_str (pid)
1458 int pid;
1459{
1460 static char buf[30];
1461
43fc25c8
JL
1462 if (STREQ (current_target.to_shortname, "remote"))
1463 sprintf (buf, "thread %d", pid);
1464 else
1465 sprintf (buf, "process %d", pid);
49781499
JK
1466
1467 return buf;
1468}
67ac9759 1469\f
43fc25c8
JL
1470#ifdef MAINTENANCE_CMDS
1471static struct target_ops debug_target;
1472
1473static void
1474debug_to_open (args, from_tty)
1475 char *args;
1476 int from_tty;
1477{
1478 debug_target.to_open (args, from_tty);
1479
1480 fprintf_unfiltered (stderr, "target_open (%s, %d)\n", args, from_tty);
1481}
1482
1483static void
1484debug_to_close (quitting)
1485 int quitting;
1486{
1487 debug_target.to_close (quitting);
1488
1489 fprintf_unfiltered (stderr, "target_close (%d)\n", quitting);
1490}
1491
1492static void
1493debug_to_attach (args, from_tty)
1494 char *args;
1495 int from_tty;
1496{
1497 debug_target.to_attach (args, from_tty);
1498
1499 fprintf_unfiltered (stderr, "target_attach (%s, %d)\n", args, from_tty);
1500}
1501
1502static void
1503debug_to_detach (args, from_tty)
1504 char *args;
1505 int from_tty;
1506{
1507 debug_target.to_detach (args, from_tty);
1508
1509 fprintf_unfiltered (stderr, "target_detach (%s, %d)\n", args, from_tty);
1510}
1511
1512static void
1513debug_to_resume (pid, step, siggnal)
1514 int pid;
1515 int step;
1516 enum target_signal siggnal;
1517{
1518 debug_target.to_resume (pid, step, siggnal);
1519
1520 fprintf_unfiltered (stderr, "target_resume (%d, %s, %s)\n", pid,
1521 step ? "step" : "continue",
1522 target_signal_to_name (siggnal));
1523}
1524
1525static int
1526debug_to_wait (pid, status)
1527 int pid;
1528 struct target_waitstatus *status;
1529{
1530 int retval;
1531
1532 retval = debug_target.to_wait (pid, status);
1533
1534 fprintf_unfiltered (stderr, "target_wait (%d, status) = %d, ", pid, retval);
1535 fprintf_unfiltered (stderr, "status->kind = ");
1536 switch (status->kind)
1537 {
1538 case TARGET_WAITKIND_EXITED:
1539 fprintf_unfiltered (stderr, "exited, status = %d\n", status->value.integer);
1540 break;
1541 case TARGET_WAITKIND_STOPPED:
1542 fprintf_unfiltered (stderr, "stopped, signal = %s\n",
1543 target_signal_to_name (status->value.sig));
1544 break;
1545 case TARGET_WAITKIND_SIGNALLED:
1546 fprintf_unfiltered (stderr, "signalled, signal = %s\n",
1547 target_signal_to_name (status->value.sig));
1548 break;
1549 case TARGET_WAITKIND_LOADED:
1550 fprintf_unfiltered (stderr, "loaded\n");
1551 break;
1552 case TARGET_WAITKIND_SPURIOUS:
1553 fprintf_unfiltered (stderr, "spurious\n");
1554 break;
1555 default:
1556 fprintf_unfiltered (stderr, "unknown???\n");
1557 break;
1558 }
1559
1560 return retval;
1561}
1562
1563static void
1564debug_to_fetch_registers (regno)
1565 int regno;
1566{
1567 debug_target.to_fetch_registers (regno);
1568
1569 fprintf_unfiltered (stderr, "target_fetch_registers (%s)",
1570 regno != -1 ? reg_names[regno] : "-1");
1571 if (regno != -1)
1572 fprintf_unfiltered (stderr, " = 0x%x %d", read_register (regno),
1573 read_register (regno));
1574 fprintf_unfiltered (stderr, "\n");
1575}
1576
1577static void
1578debug_to_store_registers (regno)
1579 int regno;
1580{
1581 debug_target.to_store_registers (regno);
1582
1583 if (regno >= 0 && regno < NUM_REGS)
1584 fprintf_unfiltered (stderr, "target_store_registers (%s) = 0x%x %d\n",
1585 reg_names[regno], read_register (regno),
1586 read_register (regno));
1587 else
1588 fprintf_unfiltered (stderr, "target_store_registers (%d)\n", regno);
1589}
1590
1591static void
1592debug_to_prepare_to_store ()
1593{
1594 debug_target.to_prepare_to_store ();
1595
1596 fprintf_unfiltered (stderr, "target_prepare_to_store ()\n");
1597}
1598
1599static int
1600debug_to_xfer_memory (memaddr, myaddr, len, write, target)
1601 CORE_ADDR memaddr;
1602 char *myaddr;
1603 int len;
1604 int write;
1605 struct target_ops *target;
1606{
1607 int retval;
1608
1609 retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write, target);
1610
1611 fprintf_unfiltered (stderr, "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
1612 memaddr, len, write ? "write" : "read", retval);
1613
1614 if (retval > 0)
1615 {
1616 int i;
1617
1618 fputs_unfiltered (", bytes =", gdb_stderr);
1619 for (i = 0; i < retval; i++)
1620 fprintf_unfiltered (stderr, " %02x", myaddr[i] & 0xff);
1621 }
1622
1623 fputc_unfiltered ('\n', gdb_stderr);
1624
1625 return retval;
1626}
1627
1628static void
1629debug_to_files_info (target)
1630 struct target_ops *target;
1631{
1632 debug_target.to_files_info (target);
1633
1634 fprintf_unfiltered (stderr, "target_files_info (xxx)\n");
1635}
1636
1637static int
1638debug_to_insert_breakpoint (addr, save)
1639 CORE_ADDR addr;
1640 char *save;
1641{
1642 int retval;
1643
1644 retval = debug_target.to_insert_breakpoint (addr, save);
1645
1646 fprintf_unfiltered (stderr, "target_insert_breakpoint (0x%x, xxx) = %d\n",
1647 addr, retval);
1648 return retval;
1649}
1650
1651static int
1652debug_to_remove_breakpoint (addr, save)
1653 CORE_ADDR addr;
1654 char *save;
1655{
1656 int retval;
1657
1658 retval = debug_target.to_remove_breakpoint (addr, save);
1659
1660 fprintf_unfiltered (stderr, "target_remove_breakpoint (0x%x, xxx) = %d\n",
1661 addr, retval);
1662 return retval;
1663}
1664
1665static void
1666debug_to_terminal_init ()
1667{
1668 debug_target.to_terminal_init ();
1669
1670 fprintf_unfiltered (stderr, "target_terminal_init ()\n");
1671}
1672
1673static void
1674debug_to_terminal_inferior ()
1675{
1676 debug_target.to_terminal_inferior ();
1677
1678 fprintf_unfiltered (stderr, "target_terminal_inferior ()\n");
1679}
1680
1681static void
1682debug_to_terminal_ours_for_output ()
1683{
1684 debug_target.to_terminal_ours_for_output ();
1685
1686 fprintf_unfiltered (stderr, "target_terminal_ours_for_output ()\n");
1687}
1688
1689static void
1690debug_to_terminal_ours ()
1691{
1692 debug_target.to_terminal_ours ();
1693
1694 fprintf_unfiltered (stderr, "target_terminal_ours ()\n");
1695}
1696
1697static void
1698debug_to_terminal_info (arg, from_tty)
1699 char *arg;
1700 int from_tty;
1701{
1702 debug_target.to_terminal_info (arg, from_tty);
1703
1704 fprintf_unfiltered (stderr, "target_terminal_info (%s, %d)\n", arg,
1705 from_tty);
1706}
1707
1708static void
1709debug_to_kill ()
1710{
1711 debug_target.to_kill ();
1712
1713 fprintf_unfiltered (stderr, "target_kill ()\n");
1714}
1715
1716static void
1717debug_to_load (args, from_tty)
1718 char *args;
1719 int from_tty;
1720{
1721 debug_target.to_load (args, from_tty);
1722
1723 fprintf_unfiltered (stderr, "target_load (%s, %d)\n", args, from_tty);
1724}
1725
1726static int
1727debug_to_lookup_symbol (name, addrp)
1728 char *name;
1729 CORE_ADDR *addrp;
1730{
1731 int retval;
1732
1733 retval = debug_target.to_lookup_symbol (name, addrp);
1734
1735 fprintf_unfiltered (stderr, "target_lookup_symbol (%s, xxx)\n", name);
1736
1737 return retval;
1738}
1739
1740static void
1741debug_to_create_inferior (exec_file, args, env)
1742 char *exec_file;
1743 char *args;
1744 char **env;
1745{
1746 debug_target.to_create_inferior (exec_file, args, env);
1747
1748 fprintf_unfiltered (stderr, "target_create_inferior (%s, %s, xxx)\n",
1749 exec_file, args);
1750}
1751
1752static void
1753debug_to_mourn_inferior ()
1754{
1755 debug_target.to_mourn_inferior ();
1756
1757 fprintf_unfiltered (stderr, "target_mourn_inferior ()\n");
1758}
1759
1760static int
1761debug_to_can_run ()
1762{
1763 int retval;
1764
1765 retval = debug_target.to_can_run ();
1766
1767 fprintf_unfiltered (stderr, "target_can_run () = %d\n", retval);
1768
1769 return retval;
1770}
1771
1772static void
1773debug_to_notice_signals (pid)
1774 int pid;
1775{
1776 debug_target.to_notice_signals (pid);
1777
1778 fprintf_unfiltered (stderr, "target_notice_signals (%d)\n", pid);
1779}
1780
2b576293 1781static int
43fc25c8
JL
1782debug_to_thread_alive (pid)
1783 int pid;
1784{
1785 debug_target.to_thread_alive (pid);
1786
1787 fprintf_unfiltered (stderr, "target_thread_alive (%d)\n", pid);
2b576293 1788 return (0);
43fc25c8
JL
1789}
1790
1791static void
1792debug_to_stop ()
1793{
1794 debug_target.to_stop ();
1795
1796 fprintf_unfiltered (stderr, "target_stop ()\n");
1797}
1798
1799static void
1800setup_target_debug ()
1801{
1802 memcpy (&debug_target, &current_target, sizeof debug_target);
1803
1804 current_target.to_open = debug_to_open;
1805 current_target.to_close = debug_to_close;
1806 current_target.to_attach = debug_to_attach;
1807 current_target.to_detach = debug_to_detach;
1808 current_target.to_resume = debug_to_resume;
1809 current_target.to_wait = debug_to_wait;
1810 current_target.to_fetch_registers = debug_to_fetch_registers;
1811 current_target.to_store_registers = debug_to_store_registers;
1812 current_target.to_prepare_to_store = debug_to_prepare_to_store;
1813 current_target.to_xfer_memory = debug_to_xfer_memory;
1814 current_target.to_files_info = debug_to_files_info;
1815 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
1816 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
1817 current_target.to_terminal_init = debug_to_terminal_init;
1818 current_target.to_terminal_inferior = debug_to_terminal_inferior;
1819 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
1820 current_target.to_terminal_ours = debug_to_terminal_ours;
1821 current_target.to_terminal_info = debug_to_terminal_info;
1822 current_target.to_kill = debug_to_kill;
1823 current_target.to_load = debug_to_load;
1824 current_target.to_lookup_symbol = debug_to_lookup_symbol;
1825 current_target.to_create_inferior = debug_to_create_inferior;
1826 current_target.to_mourn_inferior = debug_to_mourn_inferior;
1827 current_target.to_can_run = debug_to_can_run;
1828 current_target.to_notice_signals = debug_to_notice_signals;
1829 current_target.to_thread_alive = debug_to_thread_alive;
1830 current_target.to_stop = debug_to_stop;
1831}
1832#endif /* MAINTENANCE_CMDS */
1833\f
bd5635a1
RP
1834static char targ_desc[] =
1835 "Names of targets and files being debugged.\n\
1836Shows the entire stack of targets currently in use (including the exec-file,\n\
1837core-file, and process, if any), as well as the symbol file name.";
1838
1839void
4fc5969d 1840initialize_targets ()
bd5635a1 1841{
cad1498f 1842 push_target (&dummy_target);
bd5635a1 1843
bd5635a1
RP
1844 add_info ("target", target_info, targ_desc);
1845 add_info ("files", target_info, targ_desc);
67ac9759 1846
43fc25c8
JL
1847#ifdef MAINTENANCE_CMDS
1848 add_show_from_set (
1849 add_set_cmd ("targetdebug", class_maintenance, var_zinteger,
1850 (char *)&targetdebug,
1851 "Set target debugging.\n\
1852When non-zero, target debugging is enabled.", &setlist),
1853 &showlist);
1854#endif
1855
67ac9759
JK
1856 if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
1857 abort ();
bd5635a1 1858}
This page took 0.533841 seconds and 4 git commands to generate.