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