Update/correct copyright notices.
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
b6ba6518
KB
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001 Free Software Foundation, Inc.
c906108c
SS
4 Contributed by Cygnus Support.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include <errno.h>
25#include <ctype.h>
26#include "gdb_string.h"
27#include "target.h"
28#include "gdbcmd.h"
29#include "symtab.h"
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
33#include "objfiles.h"
03f2053f 34#include "gdb_wait.h"
4930751a 35#include "dcache.h"
c906108c 36#include <signal.h>
4e052eda 37#include "regcache.h"
c906108c
SS
38
39extern int errno;
40
a14ed312 41static void target_info (char *, int);
c906108c 42
a14ed312 43static void cleanup_target (struct target_ops *);
c906108c 44
a14ed312 45static void maybe_kill_then_create_inferior (char *, char *, char **);
c906108c 46
a14ed312 47static void default_clone_and_follow_inferior (int, int *);
c906108c 48
a14ed312 49static void maybe_kill_then_attach (char *, int);
c906108c 50
a14ed312 51static void kill_or_be_killed (int);
c906108c 52
a14ed312 53static void default_terminal_info (char *, int);
c906108c 54
a14ed312 55static int nosymbol (char *, CORE_ADDR *);
c906108c 56
a14ed312 57static void tcomplain (void);
c906108c 58
a14ed312 59static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 60
a14ed312 61static int return_zero (void);
c906108c 62
a14ed312 63static int return_one (void);
c906108c 64
a14ed312 65void target_ignore (void);
c906108c 66
a14ed312 67static void target_command (char *, int);
c906108c 68
a14ed312 69static struct target_ops *find_default_run_target (char *);
c906108c 70
a14ed312 71static void update_current_target (void);
c906108c 72
a14ed312 73static void nosupport_runtime (void);
392a587b 74
a14ed312 75static void normal_target_post_startup_inferior (int pid);
392a587b 76
917317f4
JM
77/* Transfer LEN bytes between target address MEMADDR and GDB address
78 MYADDR. Returns 0 for success, errno code for failure (which
79 includes partial transfers -- if you want a more useful response to
80 partial transfers, try either target_read_memory_partial or
81 target_write_memory_partial). */
c906108c
SS
82
83static int
a14ed312 84target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write);
c906108c 85
a14ed312 86static void init_dummy_target (void);
c906108c 87
a14ed312 88static void debug_to_open (char *, int);
c906108c 89
a14ed312 90static void debug_to_close (int);
c906108c 91
a14ed312 92static void debug_to_attach (char *, int);
c906108c 93
a14ed312 94static void debug_to_detach (char *, int);
c906108c 95
a14ed312 96static void debug_to_resume (int, int, enum target_signal);
c906108c 97
a14ed312 98static int debug_to_wait (int, struct target_waitstatus *);
c906108c 99
a14ed312 100static void debug_to_fetch_registers (int);
c906108c 101
a14ed312 102static void debug_to_store_registers (int);
c906108c 103
a14ed312 104static void debug_to_prepare_to_store (void);
c906108c
SS
105
106static int
29e57380
C
107debug_to_xfer_memory (CORE_ADDR, char *, int, int, struct mem_attrib *,
108 struct target_ops *);
c906108c 109
a14ed312 110static void debug_to_files_info (struct target_ops *);
c906108c 111
a14ed312 112static int debug_to_insert_breakpoint (CORE_ADDR, char *);
c906108c 113
a14ed312 114static int debug_to_remove_breakpoint (CORE_ADDR, char *);
c906108c 115
a14ed312 116static void debug_to_terminal_init (void);
c906108c 117
a14ed312 118static void debug_to_terminal_inferior (void);
c906108c 119
a14ed312 120static void debug_to_terminal_ours_for_output (void);
c906108c 121
a14ed312 122static void debug_to_terminal_ours (void);
c906108c 123
a14ed312 124static void debug_to_terminal_info (char *, int);
c906108c 125
a14ed312 126static void debug_to_kill (void);
c906108c 127
a14ed312 128static void debug_to_load (char *, int);
c906108c 129
a14ed312 130static int debug_to_lookup_symbol (char *, CORE_ADDR *);
c906108c 131
a14ed312 132static void debug_to_create_inferior (char *, char *, char **);
c906108c 133
a14ed312 134static void debug_to_mourn_inferior (void);
c906108c 135
a14ed312 136static int debug_to_can_run (void);
c906108c 137
a14ed312 138static void debug_to_notice_signals (int);
c906108c 139
a14ed312 140static int debug_to_thread_alive (int);
c906108c 141
a14ed312 142static void debug_to_stop (void);
c906108c 143
a14ed312 144static int debug_to_query (int /*char */ , char *, char *, int *);
c906108c
SS
145
146/* Pointer to array of target architecture structures; the size of the
147 array; the current index into the array; the allocated size of the
148 array. */
149struct target_ops **target_structs;
150unsigned target_struct_size;
151unsigned target_struct_index;
152unsigned target_struct_allocsize;
153#define DEFAULT_ALLOCSIZE 10
154
155/* The initial current target, so that there is always a semi-valid
156 current target. */
157
158static struct target_ops dummy_target;
159
160/* Top of target stack. */
161
162struct target_stack_item *target_stack;
163
164/* The target structure we are currently using to talk to a process
165 or file or whatever "inferior" we have. */
166
167struct target_ops current_target;
168
169/* Command list for target. */
170
171static struct cmd_list_element *targetlist = NULL;
172
173/* Nonzero if we are debugging an attached outside process
174 rather than an inferior. */
175
176int attach_flag;
177
c906108c
SS
178/* Non-zero if we want to see trace of target level stuff. */
179
180static int targetdebug = 0;
181
a14ed312 182static void setup_target_debug (void);
c906108c 183
4930751a
C
184DCACHE *target_dcache;
185
c906108c
SS
186/* The user just typed 'target' without the name of a target. */
187
188/* ARGSUSED */
189static void
fba45db2 190target_command (char *arg, int from_tty)
c906108c
SS
191{
192 fputs_filtered ("Argument required (target name). Try `help target'\n",
193 gdb_stdout);
194}
195
196/* Add a possible target architecture to the list. */
197
198void
fba45db2 199add_target (struct target_ops *t)
c906108c
SS
200{
201 if (!target_structs)
202 {
203 target_struct_allocsize = DEFAULT_ALLOCSIZE;
204 target_structs = (struct target_ops **) xmalloc
205 (target_struct_allocsize * sizeof (*target_structs));
206 }
207 if (target_struct_size >= target_struct_allocsize)
208 {
209 target_struct_allocsize *= 2;
210 target_structs = (struct target_ops **)
c5aa993b
JM
211 xrealloc ((char *) target_structs,
212 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
213 }
214 target_structs[target_struct_size++] = t;
c5aa993b 215/* cleanup_target (t); */
c906108c
SS
216
217 if (targetlist == NULL)
218 add_prefix_cmd ("target", class_run, target_command,
219 "Connect to a target machine or process.\n\
220The first argument is the type or protocol of the target machine.\n\
221Remaining arguments are interpreted by the target protocol. For more\n\
222information on the arguments for a particular protocol, type\n\
223`help target ' followed by the protocol name.",
224 &targetlist, "target ", 0, &cmdlist);
225 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
226}
227
228/* Stub functions */
229
230void
fba45db2 231target_ignore (void)
c906108c
SS
232{
233}
234
11cf8741
JM
235void
236target_load (char *arg, int from_tty)
237{
4930751a 238 dcache_invalidate (target_dcache);
11cf8741
JM
239 (*current_target.to_load) (arg, from_tty);
240}
241
c906108c
SS
242/* ARGSUSED */
243static int
fba45db2
KB
244nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
245 struct target_ops *t)
c906108c 246{
c5aa993b
JM
247 errno = EIO; /* Can't read/write this location */
248 return 0; /* No bytes handled */
c906108c
SS
249}
250
251static void
fba45db2 252tcomplain (void)
c906108c
SS
253{
254 error ("You can't do that when your target is `%s'",
255 current_target.to_shortname);
256}
257
258void
fba45db2 259noprocess (void)
c906108c
SS
260{
261 error ("You can't do that without a process to debug.");
262}
263
264/* ARGSUSED */
265static int
fba45db2 266nosymbol (char *name, CORE_ADDR *addrp)
c906108c 267{
c5aa993b 268 return 1; /* Symbol does not exist in target env */
c906108c
SS
269}
270
271/* ARGSUSED */
392a587b 272static void
fba45db2 273nosupport_runtime (void)
c906108c
SS
274{
275 if (!inferior_pid)
276 noprocess ();
277 else
278 error ("No run-time support for this");
279}
280
281
282/* ARGSUSED */
283static void
fba45db2 284default_terminal_info (char *args, int from_tty)
c906108c 285{
c5aa993b 286 printf_unfiltered ("No saved terminal information.\n");
c906108c
SS
287}
288
289/* This is the default target_create_inferior and target_attach function.
290 If the current target is executing, it asks whether to kill it off.
291 If this function returns without calling error(), it has killed off
292 the target, and the operation should be attempted. */
293
294static void
fba45db2 295kill_or_be_killed (int from_tty)
c906108c
SS
296{
297 if (target_has_execution)
298 {
299 printf_unfiltered ("You are already running a program:\n");
300 target_files_info ();
c5aa993b
JM
301 if (query ("Kill it? "))
302 {
303 target_kill ();
304 if (target_has_execution)
305 error ("Killing the program did not help.");
306 return;
307 }
308 else
309 {
310 error ("Program not killed.");
311 }
c906108c 312 }
c5aa993b 313 tcomplain ();
c906108c
SS
314}
315
316static void
fba45db2 317maybe_kill_then_attach (char *args, int from_tty)
c906108c
SS
318{
319 kill_or_be_killed (from_tty);
320 target_attach (args, from_tty);
321}
322
323static void
fba45db2 324maybe_kill_then_create_inferior (char *exec, char *args, char **env)
c906108c
SS
325{
326 kill_or_be_killed (0);
327 target_create_inferior (exec, args, env);
328}
329
330static void
fba45db2 331default_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
332{
333 target_clone_and_follow_inferior (child_pid, followed_child);
334}
335
336/* Clean up a target struct so it no longer has any zero pointers in it.
337 We default entries, at least to stubs that print error messages. */
338
339static void
fba45db2 340cleanup_target (struct target_ops *t)
c906108c
SS
341{
342
343#define de_fault(field, value) \
0d06e24b
JM
344 if (!t->field) \
345 t->field = value
346
347 de_fault (to_open,
348 (void (*) (char *, int))
349 tcomplain);
350 de_fault (to_close,
351 (void (*) (int))
352 target_ignore);
353 de_fault (to_attach,
354 maybe_kill_then_attach);
355 de_fault (to_post_attach,
356 (void (*) (int))
357 target_ignore);
358 de_fault (to_require_attach,
359 maybe_kill_then_attach);
360 de_fault (to_detach,
361 (void (*) (char *, int))
362 target_ignore);
363 de_fault (to_require_detach,
364 (void (*) (int, char *, int))
365 target_ignore);
366 de_fault (to_resume,
367 (void (*) (int, int, enum target_signal))
368 noprocess);
369 de_fault (to_wait,
370 (int (*) (int, struct target_waitstatus *))
371 noprocess);
372 de_fault (to_post_wait,
373 (void (*) (int, int))
374 target_ignore);
375 de_fault (to_fetch_registers,
376 (void (*) (int))
377 target_ignore);
378 de_fault (to_store_registers,
379 (void (*) (int))
380 noprocess);
381 de_fault (to_prepare_to_store,
382 (void (*) (void))
383 noprocess);
384 de_fault (to_xfer_memory,
29e57380 385 (int (*) (CORE_ADDR, char *, int, int, struct mem_attrib *, struct target_ops *))
0d06e24b
JM
386 nomemory);
387 de_fault (to_files_info,
388 (void (*) (struct target_ops *))
389 target_ignore);
390 de_fault (to_insert_breakpoint,
391 memory_insert_breakpoint);
392 de_fault (to_remove_breakpoint,
393 memory_remove_breakpoint);
394 de_fault (to_terminal_init,
395 (void (*) (void))
396 target_ignore);
397 de_fault (to_terminal_inferior,
398 (void (*) (void))
399 target_ignore);
400 de_fault (to_terminal_ours_for_output,
401 (void (*) (void))
402 target_ignore);
403 de_fault (to_terminal_ours,
404 (void (*) (void))
405 target_ignore);
406 de_fault (to_terminal_info,
407 default_terminal_info);
408 de_fault (to_kill,
409 (void (*) (void))
410 noprocess);
411 de_fault (to_load,
412 (void (*) (char *, int))
413 tcomplain);
414 de_fault (to_lookup_symbol,
415 (int (*) (char *, CORE_ADDR *))
416 nosymbol);
417 de_fault (to_create_inferior,
418 maybe_kill_then_create_inferior);
419 de_fault (to_post_startup_inferior,
420 (void (*) (int))
421 target_ignore);
422 de_fault (to_acknowledge_created_inferior,
423 (void (*) (int))
424 target_ignore);
425 de_fault (to_clone_and_follow_inferior,
426 default_clone_and_follow_inferior);
427 de_fault (to_post_follow_inferior_by_clone,
428 (void (*) (void))
429 target_ignore);
430 de_fault (to_insert_fork_catchpoint,
431 (int (*) (int))
432 tcomplain);
433 de_fault (to_remove_fork_catchpoint,
434 (int (*) (int))
435 tcomplain);
436 de_fault (to_insert_vfork_catchpoint,
437 (int (*) (int))
438 tcomplain);
439 de_fault (to_remove_vfork_catchpoint,
440 (int (*) (int))
441 tcomplain);
442 de_fault (to_has_forked,
443 (int (*) (int, int *))
444 return_zero);
445 de_fault (to_has_vforked,
446 (int (*) (int, int *))
447 return_zero);
448 de_fault (to_can_follow_vfork_prior_to_exec,
449 (int (*) (void))
450 return_zero);
451 de_fault (to_post_follow_vfork,
452 (void (*) (int, int, int, int))
453 target_ignore);
454 de_fault (to_insert_exec_catchpoint,
455 (int (*) (int))
456 tcomplain);
457 de_fault (to_remove_exec_catchpoint,
458 (int (*) (int))
459 tcomplain);
460 de_fault (to_has_execd,
461 (int (*) (int, char **))
462 return_zero);
463 de_fault (to_reported_exec_events_per_exec_call,
464 (int (*) (void))
465 return_one);
466 de_fault (to_has_syscall_event,
467 (int (*) (int, enum target_waitkind *, int *))
468 return_zero);
469 de_fault (to_has_exited,
470 (int (*) (int, int, int *))
471 return_zero);
472 de_fault (to_mourn_inferior,
473 (void (*) (void))
474 noprocess);
475 de_fault (to_can_run,
476 return_zero);
477 de_fault (to_notice_signals,
478 (void (*) (int))
479 target_ignore);
480 de_fault (to_thread_alive,
481 (int (*) (int))
482 return_zero);
483 de_fault (to_find_new_threads,
484 (void (*) (void))
485 target_ignore);
486 de_fault (to_extra_thread_info,
487 (char *(*) (struct thread_info *))
488 return_zero);
489 de_fault (to_stop,
490 (void (*) (void))
491 target_ignore);
492 de_fault (to_query,
493 (int (*) (int, char *, char *, int *))
494 return_zero);
495 de_fault (to_rcmd,
d9fcf2fb 496 (void (*) (char *, struct ui_file *))
0d06e24b
JM
497 tcomplain);
498 de_fault (to_enable_exception_callback,
499 (struct symtab_and_line * (*) (enum exception_event_kind, int))
500 nosupport_runtime);
501 de_fault (to_get_current_exception_event,
502 (struct exception_event_record * (*) (void))
503 nosupport_runtime);
504 de_fault (to_pid_to_exec_file,
505 (char *(*) (int))
506 return_zero);
507 de_fault (to_core_file_to_sym_file,
508 (char *(*) (char *))
509 return_zero);
510 de_fault (to_can_async_p,
511 (int (*) (void))
512 return_zero);
513 de_fault (to_is_async_p,
514 (int (*) (void))
515 return_zero);
516 de_fault (to_async,
517 (void (*) (void (*) (enum inferior_event_type, void*), void*))
518 tcomplain);
c906108c
SS
519#undef de_fault
520}
521
522/* Go through the target stack from top to bottom, copying over zero entries in
523 current_target. In effect, we are doing class inheritance through the
524 pushed target vectors. */
525
526static void
fba45db2 527update_current_target (void)
c906108c
SS
528{
529 struct target_stack_item *item;
530 struct target_ops *t;
531
532 /* First, reset current_target */
533 memset (&current_target, 0, sizeof current_target);
534
535 for (item = target_stack; item; item = item->next)
536 {
537 t = item->target_ops;
538
539#define INHERIT(FIELD, TARGET) \
540 if (!current_target.FIELD) \
541 current_target.FIELD = TARGET->FIELD
542
543 INHERIT (to_shortname, t);
544 INHERIT (to_longname, t);
545 INHERIT (to_doc, t);
546 INHERIT (to_open, t);
547 INHERIT (to_close, t);
548 INHERIT (to_attach, t);
549 INHERIT (to_post_attach, t);
550 INHERIT (to_require_attach, t);
551 INHERIT (to_detach, t);
552 INHERIT (to_require_detach, t);
553 INHERIT (to_resume, t);
554 INHERIT (to_wait, t);
555 INHERIT (to_post_wait, t);
556 INHERIT (to_fetch_registers, t);
557 INHERIT (to_store_registers, t);
558 INHERIT (to_prepare_to_store, t);
559 INHERIT (to_xfer_memory, t);
560 INHERIT (to_files_info, t);
561 INHERIT (to_insert_breakpoint, t);
562 INHERIT (to_remove_breakpoint, t);
563 INHERIT (to_terminal_init, t);
564 INHERIT (to_terminal_inferior, t);
565 INHERIT (to_terminal_ours_for_output, t);
566 INHERIT (to_terminal_ours, t);
567 INHERIT (to_terminal_info, t);
568 INHERIT (to_kill, t);
569 INHERIT (to_load, t);
570 INHERIT (to_lookup_symbol, t);
571 INHERIT (to_create_inferior, t);
572 INHERIT (to_post_startup_inferior, t);
573 INHERIT (to_acknowledge_created_inferior, t);
574 INHERIT (to_clone_and_follow_inferior, t);
575 INHERIT (to_post_follow_inferior_by_clone, t);
576 INHERIT (to_insert_fork_catchpoint, t);
577 INHERIT (to_remove_fork_catchpoint, t);
578 INHERIT (to_insert_vfork_catchpoint, t);
579 INHERIT (to_remove_vfork_catchpoint, t);
580 INHERIT (to_has_forked, t);
581 INHERIT (to_has_vforked, t);
582 INHERIT (to_can_follow_vfork_prior_to_exec, t);
583 INHERIT (to_post_follow_vfork, t);
584 INHERIT (to_insert_exec_catchpoint, t);
585 INHERIT (to_remove_exec_catchpoint, t);
586 INHERIT (to_has_execd, t);
587 INHERIT (to_reported_exec_events_per_exec_call, t);
588 INHERIT (to_has_syscall_event, t);
589 INHERIT (to_has_exited, t);
590 INHERIT (to_mourn_inferior, t);
591 INHERIT (to_can_run, t);
592 INHERIT (to_notice_signals, t);
593 INHERIT (to_thread_alive, t);
b83266a0 594 INHERIT (to_find_new_threads, t);
ed9a39eb 595 INHERIT (to_pid_to_str, t);
0d06e24b 596 INHERIT (to_extra_thread_info, t);
c906108c
SS
597 INHERIT (to_stop, t);
598 INHERIT (to_query, t);
96baa820 599 INHERIT (to_rcmd, t);
c906108c
SS
600 INHERIT (to_enable_exception_callback, t);
601 INHERIT (to_get_current_exception_event, t);
602 INHERIT (to_pid_to_exec_file, t);
603 INHERIT (to_core_file_to_sym_file, t);
604 INHERIT (to_stratum, t);
605 INHERIT (DONT_USE, t);
606 INHERIT (to_has_all_memory, t);
607 INHERIT (to_has_memory, t);
608 INHERIT (to_has_stack, t);
609 INHERIT (to_has_registers, t);
610 INHERIT (to_has_execution, t);
611 INHERIT (to_has_thread_control, t);
612 INHERIT (to_sections, t);
613 INHERIT (to_sections_end, t);
6426a772
JM
614 INHERIT (to_can_async_p, t);
615 INHERIT (to_is_async_p, t);
616 INHERIT (to_async, t);
ed9a39eb 617 INHERIT (to_async_mask_value, t);
c906108c
SS
618 INHERIT (to_magic, t);
619
620#undef INHERIT
621 }
622}
623
624/* Push a new target type into the stack of the existing target accessors,
625 possibly superseding some of the existing accessors.
626
627 Result is zero if the pushed target ended up on top of the stack,
628 nonzero if at least one target is on top of it.
629
630 Rather than allow an empty stack, we always have the dummy target at
631 the bottom stratum, so we can call the function vectors without
632 checking them. */
633
634int
fba45db2 635push_target (struct target_ops *t)
c906108c
SS
636{
637 struct target_stack_item *cur, *prev, *tmp;
638
639 /* Check magic number. If wrong, it probably means someone changed
640 the struct definition, but not all the places that initialize one. */
641 if (t->to_magic != OPS_MAGIC)
642 {
c5aa993b
JM
643 fprintf_unfiltered (gdb_stderr,
644 "Magic number of %s target struct wrong\n",
645 t->to_shortname);
e1e9e218 646 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
647 }
648
649 /* Find the proper stratum to install this target in. */
650
651 for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
652 {
c5aa993b 653 if ((int) (t->to_stratum) >= (int) (cur->target_ops->to_stratum))
c906108c
SS
654 break;
655 }
656
657 /* If there's already targets at this stratum, remove them. */
658
659 if (cur)
660 while (t->to_stratum == cur->target_ops->to_stratum)
661 {
662 /* There's already something on this stratum. Close it off. */
663 if (cur->target_ops->to_close)
664 (cur->target_ops->to_close) (0);
665 if (prev)
c5aa993b 666 prev->next = cur->next; /* Unchain old target_ops */
c906108c 667 else
c5aa993b 668 target_stack = cur->next; /* Unchain first on list */
c906108c 669 tmp = cur->next;
b8c9b27d 670 xfree (cur);
c906108c
SS
671 cur = tmp;
672 }
673
674 /* We have removed all targets in our stratum, now add the new one. */
675
676 tmp = (struct target_stack_item *)
677 xmalloc (sizeof (struct target_stack_item));
678 tmp->next = cur;
679 tmp->target_ops = t;
680
681 if (prev)
682 prev->next = tmp;
683 else
684 target_stack = tmp;
685
686 update_current_target ();
687
c5aa993b 688 cleanup_target (&current_target); /* Fill in the gaps */
c906108c 689
c906108c
SS
690 if (targetdebug)
691 setup_target_debug ();
c906108c
SS
692
693 return prev != 0;
694}
695
696/* Remove a target_ops vector from the stack, wherever it may be.
697 Return how many times it was removed (0 or 1). */
698
699int
fba45db2 700unpush_target (struct target_ops *t)
c906108c
SS
701{
702 struct target_stack_item *cur, *prev;
703
704 if (t->to_close)
705 t->to_close (0); /* Let it clean up */
706
707 /* Look for the specified target. Note that we assume that a target
708 can only occur once in the target stack. */
709
710 for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
711 if (cur->target_ops == t)
712 break;
713
714 if (!cur)
715 return 0; /* Didn't find target_ops, quit now */
716
717 /* Unchain the target */
718
719 if (!prev)
720 target_stack = cur->next;
721 else
722 prev->next = cur->next;
723
b8c9b27d 724 xfree (cur); /* Release the target_stack_item */
c906108c
SS
725
726 update_current_target ();
727 cleanup_target (&current_target);
728
729 return 1;
730}
731
732void
fba45db2 733pop_target (void)
c906108c 734{
c5aa993b 735 (current_target.to_close) (0); /* Let it clean up */
c906108c
SS
736 if (unpush_target (target_stack->target_ops) == 1)
737 return;
738
c5aa993b
JM
739 fprintf_unfiltered (gdb_stderr,
740 "pop_target couldn't find target %s\n",
741 current_target.to_shortname);
e1e9e218 742 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
743}
744
745#undef MIN
746#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
747
748/* target_read_string -- read a null terminated string, up to LEN bytes,
749 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
750 Set *STRING to a pointer to malloc'd memory containing the data; the caller
751 is responsible for freeing it. Return the number of bytes successfully
752 read. */
753
754int
fba45db2 755target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c
SS
756{
757 int tlen, origlen, offset, i;
758 char buf[4];
759 int errcode = 0;
760 char *buffer;
761 int buffer_allocated;
762 char *bufptr;
763 unsigned int nbytes_read = 0;
764
765 /* Small for testing. */
766 buffer_allocated = 4;
767 buffer = xmalloc (buffer_allocated);
768 bufptr = buffer;
769
770 origlen = len;
771
772 while (len > 0)
773 {
774 tlen = MIN (len, 4 - (memaddr & 3));
775 offset = memaddr & 3;
776
d4b2399a 777 errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
c906108c
SS
778 if (errcode != 0)
779 {
780 /* The transfer request might have crossed the boundary to an
781 unallocated region of memory. Retry the transfer, requesting
782 a single byte. */
783 tlen = 1;
784 offset = 0;
d4b2399a 785 errcode = target_xfer_memory (memaddr, buf, 1, 0);
c906108c
SS
786 if (errcode != 0)
787 goto done;
788 }
789
790 if (bufptr - buffer + tlen > buffer_allocated)
791 {
792 unsigned int bytes;
793 bytes = bufptr - buffer;
794 buffer_allocated *= 2;
795 buffer = xrealloc (buffer, buffer_allocated);
796 bufptr = buffer + bytes;
797 }
798
799 for (i = 0; i < tlen; i++)
800 {
801 *bufptr++ = buf[i + offset];
802 if (buf[i + offset] == '\000')
803 {
804 nbytes_read += i + 1;
805 goto done;
806 }
807 }
808
809 memaddr += tlen;
810 len -= tlen;
811 nbytes_read += tlen;
812 }
c5aa993b 813done:
c906108c
SS
814 if (errnop != NULL)
815 *errnop = errcode;
816 if (string != NULL)
817 *string = buffer;
818 return nbytes_read;
819}
820
821/* Read LEN bytes of target memory at address MEMADDR, placing the results in
822 GDB's memory at MYADDR. Returns either 0 for success or an errno value
823 if any error occurs.
824
825 If an error occurs, no guarantee is made about the contents of the data at
826 MYADDR. In particular, the caller should not depend upon partial reads
827 filling the buffer with good data. There is no way for the caller to know
828 how much good data might have been transfered anyway. Callers that can
829 deal with partial reads should call target_read_memory_partial. */
830
831int
fba45db2 832target_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 833{
d4b2399a 834 return target_xfer_memory (memaddr, myaddr, len, 0);
c906108c
SS
835}
836
c906108c 837int
fba45db2 838target_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 839{
d4b2399a 840 return target_xfer_memory (memaddr, myaddr, len, 1);
c906108c 841}
c5aa993b 842
67e0617e
C
843/* Move memory to or from the targets. The top target gets priority;
844 if it cannot handle it, it is offered to the next one down, etc.
c906108c 845
67e0617e 846 Result is -1 on error, or the number of bytes transfered. */
c906108c 847
4930751a 848int
29e57380
C
849do_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
850 struct mem_attrib *attrib)
c906108c 851{
c906108c 852 int res;
4930751a 853 int done = 0;
c906108c
SS
854 struct target_ops *t;
855 struct target_stack_item *item;
856
857 /* Zero length requests are ok and require no work. */
858 if (len == 0)
859 return 0;
860
c906108c
SS
861 /* to_xfer_memory is not guaranteed to set errno, even when it returns
862 0. */
863 errno = 0;
864
67e0617e 865 /* The quick case is that the top target can handle the transfer. */
c906108c 866 res = current_target.to_xfer_memory
29e57380 867 (memaddr, myaddr, len, write, attrib, &current_target);
c906108c 868
67e0617e
C
869 /* If res <= 0 then we call it again in the loop. Ah well. */
870 if (res <= 0)
c906108c 871 {
c906108c
SS
872 for (item = target_stack; item; item = item->next)
873 {
874 t = item->target_ops;
875 if (!t->to_has_memory)
876 continue;
877
29e57380 878 res = t->to_xfer_memory (memaddr, myaddr, len, write, attrib, t);
c906108c
SS
879 if (res > 0)
880 break; /* Handled all or part of xfer */
881 if (t->to_has_all_memory)
882 break;
883 }
884
4930751a 885 if (res <= 0)
67e0617e 886 return -1;
4930751a 887 }
67e0617e
C
888
889 return res;
4930751a
C
890}
891
67e0617e
C
892
893/* Perform a memory transfer. Iterate until the entire region has
894 been transfered.
895
896 Result is 0 or errno value. */
897
4930751a
C
898static int
899target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write)
900{
901 int res;
29e57380
C
902 int reg_len;
903 struct mem_region *region;
4930751a
C
904
905 /* Zero length requests are ok and require no work. */
906 if (len == 0)
907 {
908 return 0;
909 }
910
911 while (len > 0)
912 {
29e57380
C
913 region = lookup_mem_region(memaddr);
914 if (memaddr + len < region->hi)
915 reg_len = len;
916 else
917 reg_len = region->hi - memaddr;
918
919 switch (region->attrib.mode)
c906108c 920 {
29e57380
C
921 case MEM_RO:
922 if (write)
923 return EIO;
924 break;
925
926 case MEM_WO:
c906108c 927 if (!write)
c906108c 928 return EIO;
29e57380 929 break;
c906108c 930 }
4930751a 931
29e57380
C
932 while (reg_len > 0)
933 {
934 if (region->attrib.cache)
935 res = dcache_xfer_memory(target_dcache, memaddr, myaddr,
936 reg_len, write);
937 else
938 res = do_xfer_memory(memaddr, myaddr, reg_len, write,
939 &region->attrib);
940
941 if (res <= 0)
942 {
943 /* If this address is for nonexistent memory, read zeros
944 if reading, or do nothing if writing. Return
945 error. */
946 if (!write)
947 memset (myaddr, 0, len);
948 if (errno == 0)
949 return EIO;
950 else
951 return errno;
952 }
953
954 memaddr += res;
955 myaddr += res;
956 len -= res;
957 reg_len -= res;
958 }
c906108c 959 }
4930751a 960
c906108c
SS
961 return 0; /* We managed to cover it all somehow. */
962}
963
964
67e0617e
C
965/* Perform a partial memory transfer.
966
967 Result is -1 on error, or the number of bytes transfered. */
917317f4
JM
968
969static int
4930751a 970target_xfer_memory_partial (CORE_ADDR memaddr, char *myaddr, int len,
917317f4
JM
971 int write_p, int *err)
972{
973 int res;
29e57380
C
974 int reg_len;
975 struct mem_region *region;
917317f4
JM
976
977 /* Zero length requests are ok and require no work. */
978 if (len == 0)
979 {
980 *err = 0;
981 return 0;
982 }
983
29e57380
C
984 region = lookup_mem_region(memaddr);
985 if (memaddr + len < region->hi)
986 reg_len = len;
987 else
988 reg_len = region->hi - memaddr;
989
990 switch (region->attrib.mode)
991 {
992 case MEM_RO:
993 if (write_p)
994 {
995 *err = EIO;
873406a6 996 return -1;
29e57380
C
997 }
998 break;
999
1000 case MEM_WO:
1001 if (write_p)
1002 {
1003 *err = EIO;
873406a6 1004 return -1;
29e57380
C
1005 }
1006 break;
1007 }
1008
1009 if (region->attrib.cache)
1010 res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
1011 reg_len, write_p);
1012 else
1013 res = do_xfer_memory (memaddr, myaddr, reg_len, write_p,
1014 &region->attrib);
1015
4930751a 1016 if (res <= 0)
917317f4 1017 {
4930751a
C
1018 if (errno != 0)
1019 *err = errno;
1020 else
1021 *err = EIO;
917317f4 1022
4930751a 1023 return -1;
917317f4
JM
1024 }
1025
4930751a 1026 *err = 0;
67e0617e 1027 return res;
917317f4
JM
1028}
1029
1030int
1031target_read_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
1032{
1033 return target_xfer_memory_partial (memaddr, buf, len, 0, err);
1034}
1035
1036int
1037target_write_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
1038{
1039 return target_xfer_memory_partial (memaddr, buf, len, 1, err);
1040}
1041
c906108c
SS
1042/* ARGSUSED */
1043static void
fba45db2 1044target_info (char *args, int from_tty)
c906108c
SS
1045{
1046 struct target_ops *t;
1047 struct target_stack_item *item;
1048 int has_all_mem = 0;
c5aa993b 1049
c906108c
SS
1050 if (symfile_objfile != NULL)
1051 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
1052
1053#ifdef FILES_INFO_HOOK
1054 if (FILES_INFO_HOOK ())
1055 return;
1056#endif
1057
1058 for (item = target_stack; item; item = item->next)
1059 {
1060 t = item->target_ops;
1061
1062 if (!t->to_has_memory)
1063 continue;
1064
c5aa993b 1065 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
1066 continue;
1067 if (has_all_mem)
c5aa993b
JM
1068 printf_unfiltered ("\tWhile running this, GDB does not access memory from...\n");
1069 printf_unfiltered ("%s:\n", t->to_longname);
1070 (t->to_files_info) (t);
c906108c
SS
1071 has_all_mem = t->to_has_all_memory;
1072 }
1073}
1074
1075/* This is to be called by the open routine before it does
1076 anything. */
1077
1078void
fba45db2 1079target_preopen (int from_tty)
c906108c 1080{
c5aa993b 1081 dont_repeat ();
c906108c
SS
1082
1083 if (target_has_execution)
c5aa993b 1084 {
adf40b2e
JM
1085 if (!from_tty
1086 || query ("A program is being debugged already. Kill it? "))
c5aa993b 1087 target_kill ();
c906108c 1088 else
c5aa993b 1089 error ("Program not killed.");
c906108c
SS
1090 }
1091
1092 /* Calling target_kill may remove the target from the stack. But if
1093 it doesn't (which seems like a win for UDI), remove it now. */
1094
1095 if (target_has_execution)
1096 pop_target ();
1097}
1098
1099/* Detach a target after doing deferred register stores. */
1100
1101void
fba45db2 1102target_detach (char *args, int from_tty)
c906108c
SS
1103{
1104 /* Handle any optimized stores to the inferior. */
1105#ifdef DO_DEFERRED_STORES
1106 DO_DEFERRED_STORES;
1107#endif
1108 (current_target.to_detach) (args, from_tty);
1109}
1110
1111void
fba45db2 1112target_link (char *modname, CORE_ADDR *t_reloc)
c906108c 1113{
c5aa993b 1114 if (STREQ (current_target.to_shortname, "rombug"))
c906108c
SS
1115 {
1116 (current_target.to_lookup_symbol) (modname, t_reloc);
1117 if (*t_reloc == 0)
c5aa993b 1118 error ("Unable to link to %s and get relocation in rombug", modname);
c906108c
SS
1119 }
1120 else
2acceee2 1121 *t_reloc = (CORE_ADDR) -1;
c906108c
SS
1122}
1123
ed9a39eb
JM
1124int
1125target_async_mask (int mask)
1126{
1127 int saved_async_masked_status = target_async_mask_value;
1128 target_async_mask_value = mask;
1129 return saved_async_masked_status;
1130}
1131
c906108c
SS
1132/* Look through the list of possible targets for a target that can
1133 execute a run or attach command without any other data. This is
1134 used to locate the default process stratum.
1135
1136 Result is always valid (error() is called for errors). */
1137
1138static struct target_ops *
fba45db2 1139find_default_run_target (char *do_mesg)
c906108c
SS
1140{
1141 struct target_ops **t;
1142 struct target_ops *runable = NULL;
1143 int count;
1144
1145 count = 0;
1146
1147 for (t = target_structs; t < target_structs + target_struct_size;
1148 ++t)
1149 {
c5aa993b 1150 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
1151 {
1152 runable = *t;
1153 ++count;
1154 }
1155 }
1156
1157 if (count != 1)
1158 error ("Don't know how to %s. Try \"help target\".", do_mesg);
1159
1160 return runable;
1161}
1162
1163void
fba45db2 1164find_default_attach (char *args, int from_tty)
c906108c
SS
1165{
1166 struct target_ops *t;
1167
c5aa993b 1168 t = find_default_run_target ("attach");
c906108c
SS
1169 (t->to_attach) (args, from_tty);
1170 return;
1171}
1172
1173void
fba45db2 1174find_default_require_attach (char *args, int from_tty)
c906108c
SS
1175{
1176 struct target_ops *t;
1177
c5aa993b 1178 t = find_default_run_target ("require_attach");
c906108c
SS
1179 (t->to_require_attach) (args, from_tty);
1180 return;
1181}
1182
1183void
fba45db2 1184find_default_require_detach (int pid, char *args, int from_tty)
c906108c
SS
1185{
1186 struct target_ops *t;
1187
c5aa993b 1188 t = find_default_run_target ("require_detach");
c906108c
SS
1189 (t->to_require_detach) (pid, args, from_tty);
1190 return;
1191}
1192
1193void
fba45db2 1194find_default_create_inferior (char *exec_file, char *allargs, char **env)
c906108c
SS
1195{
1196 struct target_ops *t;
1197
c5aa993b 1198 t = find_default_run_target ("run");
c906108c
SS
1199 (t->to_create_inferior) (exec_file, allargs, env);
1200 return;
1201}
1202
1203void
fba45db2 1204find_default_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
1205{
1206 struct target_ops *t;
1207
c5aa993b 1208 t = find_default_run_target ("run");
c906108c
SS
1209 (t->to_clone_and_follow_inferior) (child_pid, followed_child);
1210 return;
1211}
1212
1213static int
fba45db2 1214return_zero (void)
c906108c
SS
1215{
1216 return 0;
1217}
1218
1219static int
fba45db2 1220return_one (void)
c906108c
SS
1221{
1222 return 1;
1223}
1224
6426a772
JM
1225/*
1226 * Resize the to_sections pointer. Also make sure that anyone that
1227 * was holding on to an old value of it gets updated.
1228 * Returns the old size.
1229 */
1230
1231int
1232target_resize_to_sections (struct target_ops *target, int num_added)
1233{
1234 struct target_ops **t;
1235 struct section_table *old_value;
1236 int old_count;
1237
1238 old_value = target->to_sections;
1239
1240 if (target->to_sections)
1241 {
1242 old_count = target->to_sections_end - target->to_sections;
1243 target->to_sections = (struct section_table *)
1244 xrealloc ((char *) target->to_sections,
1245 (sizeof (struct section_table)) * (num_added + old_count));
1246 }
1247 else
1248 {
1249 old_count = 0;
1250 target->to_sections = (struct section_table *)
1251 xmalloc ((sizeof (struct section_table)) * num_added);
1252 }
1253 target->to_sections_end = target->to_sections + (num_added + old_count);
1254
1255 /* Check to see if anyone else was pointing to this structure.
1256 If old_value was null, then no one was. */
1257
1258 if (old_value)
1259 {
1260 for (t = target_structs; t < target_structs + target_struct_size;
1261 ++t)
1262 {
1263 if ((*t)->to_sections == old_value)
1264 {
1265 (*t)->to_sections = target->to_sections;
1266 (*t)->to_sections_end = target->to_sections_end;
1267 }
1268 }
1269 }
1270
1271 return old_count;
1272
1273}
1274
07cd4b97
JB
1275/* Remove all target sections taken from ABFD.
1276
1277 Scan the current target stack for targets whose section tables
1278 refer to sections from BFD, and remove those sections. We use this
1279 when we notice that the inferior has unloaded a shared object, for
1280 example. */
1281void
1282remove_target_sections (bfd *abfd)
1283{
1284 struct target_ops **t;
1285
1286 for (t = target_structs; t < target_structs + target_struct_size; t++)
1287 {
1288 struct section_table *src, *dest;
1289
1290 dest = (*t)->to_sections;
1291 for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
1292 if (src->bfd != abfd)
1293 {
1294 /* Keep this section. */
1295 if (dest < src) *dest = *src;
1296 dest++;
1297 }
1298
1299 /* If we've dropped any sections, resize the section table. */
1300 if (dest < src)
1301 target_resize_to_sections (*t, dest - src);
1302 }
1303}
1304
1305
1306
1307
7a292a7a
SS
1308/* Find a single runnable target in the stack and return it. If for
1309 some reason there is more than one, return NULL. */
1310
1311struct target_ops *
fba45db2 1312find_run_target (void)
7a292a7a
SS
1313{
1314 struct target_ops **t;
1315 struct target_ops *runable = NULL;
1316 int count;
c5aa993b 1317
7a292a7a 1318 count = 0;
c5aa993b 1319
7a292a7a
SS
1320 for (t = target_structs; t < target_structs + target_struct_size; ++t)
1321 {
c5aa993b 1322 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
1323 {
1324 runable = *t;
1325 ++count;
1326 }
1327 }
c5aa993b 1328
7a292a7a
SS
1329 return (count == 1 ? runable : NULL);
1330}
1331
ed9a39eb
JM
1332/* Find a single core_stratum target in the list of targets and return it.
1333 If for some reason there is more than one, return NULL. */
1334
c906108c 1335struct target_ops *
fba45db2 1336find_core_target (void)
c906108c
SS
1337{
1338 struct target_ops **t;
1339 struct target_ops *runable = NULL;
1340 int count;
c5aa993b 1341
c906108c 1342 count = 0;
c5aa993b 1343
c906108c
SS
1344 for (t = target_structs; t < target_structs + target_struct_size;
1345 ++t)
1346 {
1347 if ((*t)->to_stratum == core_stratum)
1348 {
1349 runable = *t;
1350 ++count;
1351 }
1352 }
c5aa993b
JM
1353
1354 return (count == 1 ? runable : NULL);
c906108c 1355}
ed9a39eb
JM
1356
1357/*
1358 * Find the next target down the stack from the specified target.
1359 */
1360
1361struct target_ops *
fba45db2 1362find_target_beneath (struct target_ops *t)
ed9a39eb
JM
1363{
1364 struct target_stack_item *cur;
1365
1366 for (cur = target_stack; cur; cur = cur->next)
1367 if (cur->target_ops == t)
1368 break;
1369
1370 if (cur == NULL || cur->next == NULL)
1371 return NULL;
1372 else
1373 return cur->next->target_ops;
1374}
1375
c906108c
SS
1376\f
1377/* The inferior process has died. Long live the inferior! */
1378
1379void
fba45db2 1380generic_mourn_inferior (void)
c906108c
SS
1381{
1382 extern int show_breakpoint_hit_counts;
1383
1384 inferior_pid = 0;
1385 attach_flag = 0;
1386 breakpoint_init_inferior (inf_exited);
1387 registers_changed ();
1388
1389#ifdef CLEAR_DEFERRED_STORES
1390 /* Delete any pending stores to the inferior... */
1391 CLEAR_DEFERRED_STORES;
1392#endif
1393
1394 reopen_exec_file ();
1395 reinit_frame_cache ();
1396
1397 /* It is confusing to the user for ignore counts to stick around
1398 from previous runs of the inferior. So clear them. */
1399 /* However, it is more confusing for the ignore counts to disappear when
1400 using hit counts. So don't clear them if we're counting hits. */
1401 if (!show_breakpoint_hit_counts)
1402 breakpoint_clear_ignore_counts ();
c5b739b5
FN
1403
1404 if (detach_hook)
1405 detach_hook ();
c906108c
SS
1406}
1407\f
1408/* This table must match in order and size the signals in enum target_signal
1409 in target.h. */
9846de1b 1410/* *INDENT-OFF* */
c906108c
SS
1411static struct {
1412 char *name;
1413 char *string;
1414 } signals [] =
1415{
1416 {"0", "Signal 0"},
1417 {"SIGHUP", "Hangup"},
1418 {"SIGINT", "Interrupt"},
1419 {"SIGQUIT", "Quit"},
1420 {"SIGILL", "Illegal instruction"},
1421 {"SIGTRAP", "Trace/breakpoint trap"},
1422 {"SIGABRT", "Aborted"},
1423 {"SIGEMT", "Emulation trap"},
1424 {"SIGFPE", "Arithmetic exception"},
1425 {"SIGKILL", "Killed"},
1426 {"SIGBUS", "Bus error"},
1427 {"SIGSEGV", "Segmentation fault"},
1428 {"SIGSYS", "Bad system call"},
1429 {"SIGPIPE", "Broken pipe"},
1430 {"SIGALRM", "Alarm clock"},
1431 {"SIGTERM", "Terminated"},
1432 {"SIGURG", "Urgent I/O condition"},
1433 {"SIGSTOP", "Stopped (signal)"},
1434 {"SIGTSTP", "Stopped (user)"},
1435 {"SIGCONT", "Continued"},
1436 {"SIGCHLD", "Child status changed"},
1437 {"SIGTTIN", "Stopped (tty input)"},
1438 {"SIGTTOU", "Stopped (tty output)"},
1439 {"SIGIO", "I/O possible"},
1440 {"SIGXCPU", "CPU time limit exceeded"},
1441 {"SIGXFSZ", "File size limit exceeded"},
1442 {"SIGVTALRM", "Virtual timer expired"},
1443 {"SIGPROF", "Profiling timer expired"},
1444 {"SIGWINCH", "Window size changed"},
1445 {"SIGLOST", "Resource lost"},
1446 {"SIGUSR1", "User defined signal 1"},
1447 {"SIGUSR2", "User defined signal 2"},
1448 {"SIGPWR", "Power fail/restart"},
1449 {"SIGPOLL", "Pollable event occurred"},
1450 {"SIGWIND", "SIGWIND"},
1451 {"SIGPHONE", "SIGPHONE"},
1452 {"SIGWAITING", "Process's LWPs are blocked"},
1453 {"SIGLWP", "Signal LWP"},
1454 {"SIGDANGER", "Swap space dangerously low"},
1455 {"SIGGRANT", "Monitor mode granted"},
1456 {"SIGRETRACT", "Need to relinquish monitor mode"},
1457 {"SIGMSG", "Monitor mode data available"},
1458 {"SIGSOUND", "Sound completed"},
1459 {"SIGSAK", "Secure attention"},
1460 {"SIGPRIO", "SIGPRIO"},
1461 {"SIG33", "Real-time event 33"},
1462 {"SIG34", "Real-time event 34"},
1463 {"SIG35", "Real-time event 35"},
1464 {"SIG36", "Real-time event 36"},
1465 {"SIG37", "Real-time event 37"},
1466 {"SIG38", "Real-time event 38"},
1467 {"SIG39", "Real-time event 39"},
1468 {"SIG40", "Real-time event 40"},
1469 {"SIG41", "Real-time event 41"},
1470 {"SIG42", "Real-time event 42"},
1471 {"SIG43", "Real-time event 43"},
1472 {"SIG44", "Real-time event 44"},
1473 {"SIG45", "Real-time event 45"},
1474 {"SIG46", "Real-time event 46"},
1475 {"SIG47", "Real-time event 47"},
1476 {"SIG48", "Real-time event 48"},
1477 {"SIG49", "Real-time event 49"},
1478 {"SIG50", "Real-time event 50"},
1479 {"SIG51", "Real-time event 51"},
1480 {"SIG52", "Real-time event 52"},
1481 {"SIG53", "Real-time event 53"},
1482 {"SIG54", "Real-time event 54"},
1483 {"SIG55", "Real-time event 55"},
1484 {"SIG56", "Real-time event 56"},
1485 {"SIG57", "Real-time event 57"},
1486 {"SIG58", "Real-time event 58"},
1487 {"SIG59", "Real-time event 59"},
1488 {"SIG60", "Real-time event 60"},
1489 {"SIG61", "Real-time event 61"},
1490 {"SIG62", "Real-time event 62"},
1491 {"SIG63", "Real-time event 63"},
cd0fc7c3 1492 {"SIGCANCEL", "LWP internal signal"},
d4f3574e 1493 {"SIG32", "Real-time event 32"},
d57fc352 1494 {"SIG64", "Real-time event 64"},
c906108c
SS
1495
1496#if defined(MACH) || defined(__MACH__)
1497 /* Mach exceptions */
1498 {"EXC_BAD_ACCESS", "Could not access memory"},
1499 {"EXC_BAD_INSTRUCTION", "Illegal instruction/operand"},
1500 {"EXC_ARITHMETIC", "Arithmetic exception"},
1501 {"EXC_EMULATION", "Emulation instruction"},
1502 {"EXC_SOFTWARE", "Software generated exception"},
1503 {"EXC_BREAKPOINT", "Breakpoint"},
1504#endif
7a292a7a
SS
1505 {"SIGINFO", "Information request"},
1506
c906108c
SS
1507 {NULL, "Unknown signal"},
1508 {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
1509
1510 /* Last entry, used to check whether the table is the right size. */
1511 {NULL, "TARGET_SIGNAL_MAGIC"}
1512};
9846de1b 1513/* *INDENT-ON* */
c906108c 1514
c5aa993b
JM
1515
1516
c906108c
SS
1517/* Return the string for a signal. */
1518char *
fba45db2 1519target_signal_to_string (enum target_signal sig)
c906108c 1520{
7a292a7a
SS
1521 if ((sig >= TARGET_SIGNAL_FIRST) && (sig <= TARGET_SIGNAL_LAST))
1522 return signals[sig].string;
1523 else
1524 return signals[TARGET_SIGNAL_UNKNOWN].string;
c906108c
SS
1525}
1526
1527/* Return the name for a signal. */
1528char *
fba45db2 1529target_signal_to_name (enum target_signal sig)
c906108c
SS
1530{
1531 if (sig == TARGET_SIGNAL_UNKNOWN)
1532 /* I think the code which prints this will always print it along with
1533 the string, so no need to be verbose. */
1534 return "?";
1535 return signals[sig].name;
1536}
1537
1538/* Given a name, return its signal. */
1539enum target_signal
fba45db2 1540target_signal_from_name (char *name)
c906108c
SS
1541{
1542 enum target_signal sig;
1543
1544 /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
1545 for TARGET_SIGNAL_SIGCHLD. SIGIOT, on the other hand, is more
1546 questionable; seems like by now people should call it SIGABRT
1547 instead. */
1548
1549 /* This ugly cast brought to you by the native VAX compiler. */
1550 for (sig = TARGET_SIGNAL_HUP;
1551 signals[sig].name != NULL;
c5aa993b 1552 sig = (enum target_signal) ((int) sig + 1))
c906108c
SS
1553 if (STREQ (name, signals[sig].name))
1554 return sig;
1555 return TARGET_SIGNAL_UNKNOWN;
1556}
1557\f
1558/* The following functions are to help certain targets deal
1559 with the signal/waitstatus stuff. They could just as well be in
1560 a file called native-utils.c or unixwaitstatus-utils.c or whatever. */
1561
1562/* Convert host signal to our signals. */
1563enum target_signal
fba45db2 1564target_signal_from_host (int hostsig)
c906108c
SS
1565{
1566 /* A switch statement would make sense but would require special kludges
1567 to deal with the cases where more than one signal has the same number. */
1568
c5aa993b
JM
1569 if (hostsig == 0)
1570 return TARGET_SIGNAL_0;
c906108c
SS
1571
1572#if defined (SIGHUP)
c5aa993b
JM
1573 if (hostsig == SIGHUP)
1574 return TARGET_SIGNAL_HUP;
c906108c
SS
1575#endif
1576#if defined (SIGINT)
c5aa993b
JM
1577 if (hostsig == SIGINT)
1578 return TARGET_SIGNAL_INT;
c906108c
SS
1579#endif
1580#if defined (SIGQUIT)
c5aa993b
JM
1581 if (hostsig == SIGQUIT)
1582 return TARGET_SIGNAL_QUIT;
c906108c
SS
1583#endif
1584#if defined (SIGILL)
c5aa993b
JM
1585 if (hostsig == SIGILL)
1586 return TARGET_SIGNAL_ILL;
c906108c
SS
1587#endif
1588#if defined (SIGTRAP)
c5aa993b
JM
1589 if (hostsig == SIGTRAP)
1590 return TARGET_SIGNAL_TRAP;
c906108c
SS
1591#endif
1592#if defined (SIGABRT)
c5aa993b
JM
1593 if (hostsig == SIGABRT)
1594 return TARGET_SIGNAL_ABRT;
c906108c
SS
1595#endif
1596#if defined (SIGEMT)
c5aa993b
JM
1597 if (hostsig == SIGEMT)
1598 return TARGET_SIGNAL_EMT;
c906108c
SS
1599#endif
1600#if defined (SIGFPE)
c5aa993b
JM
1601 if (hostsig == SIGFPE)
1602 return TARGET_SIGNAL_FPE;
c906108c
SS
1603#endif
1604#if defined (SIGKILL)
c5aa993b
JM
1605 if (hostsig == SIGKILL)
1606 return TARGET_SIGNAL_KILL;
c906108c
SS
1607#endif
1608#if defined (SIGBUS)
c5aa993b
JM
1609 if (hostsig == SIGBUS)
1610 return TARGET_SIGNAL_BUS;
c906108c
SS
1611#endif
1612#if defined (SIGSEGV)
c5aa993b
JM
1613 if (hostsig == SIGSEGV)
1614 return TARGET_SIGNAL_SEGV;
c906108c
SS
1615#endif
1616#if defined (SIGSYS)
c5aa993b
JM
1617 if (hostsig == SIGSYS)
1618 return TARGET_SIGNAL_SYS;
c906108c
SS
1619#endif
1620#if defined (SIGPIPE)
c5aa993b
JM
1621 if (hostsig == SIGPIPE)
1622 return TARGET_SIGNAL_PIPE;
c906108c
SS
1623#endif
1624#if defined (SIGALRM)
c5aa993b
JM
1625 if (hostsig == SIGALRM)
1626 return TARGET_SIGNAL_ALRM;
c906108c
SS
1627#endif
1628#if defined (SIGTERM)
c5aa993b
JM
1629 if (hostsig == SIGTERM)
1630 return TARGET_SIGNAL_TERM;
c906108c
SS
1631#endif
1632#if defined (SIGUSR1)
c5aa993b
JM
1633 if (hostsig == SIGUSR1)
1634 return TARGET_SIGNAL_USR1;
c906108c
SS
1635#endif
1636#if defined (SIGUSR2)
c5aa993b
JM
1637 if (hostsig == SIGUSR2)
1638 return TARGET_SIGNAL_USR2;
c906108c
SS
1639#endif
1640#if defined (SIGCLD)
c5aa993b
JM
1641 if (hostsig == SIGCLD)
1642 return TARGET_SIGNAL_CHLD;
c906108c
SS
1643#endif
1644#if defined (SIGCHLD)
c5aa993b
JM
1645 if (hostsig == SIGCHLD)
1646 return TARGET_SIGNAL_CHLD;
c906108c
SS
1647#endif
1648#if defined (SIGPWR)
c5aa993b
JM
1649 if (hostsig == SIGPWR)
1650 return TARGET_SIGNAL_PWR;
c906108c
SS
1651#endif
1652#if defined (SIGWINCH)
c5aa993b
JM
1653 if (hostsig == SIGWINCH)
1654 return TARGET_SIGNAL_WINCH;
c906108c
SS
1655#endif
1656#if defined (SIGURG)
c5aa993b
JM
1657 if (hostsig == SIGURG)
1658 return TARGET_SIGNAL_URG;
c906108c
SS
1659#endif
1660#if defined (SIGIO)
c5aa993b
JM
1661 if (hostsig == SIGIO)
1662 return TARGET_SIGNAL_IO;
c906108c
SS
1663#endif
1664#if defined (SIGPOLL)
c5aa993b
JM
1665 if (hostsig == SIGPOLL)
1666 return TARGET_SIGNAL_POLL;
c906108c
SS
1667#endif
1668#if defined (SIGSTOP)
c5aa993b
JM
1669 if (hostsig == SIGSTOP)
1670 return TARGET_SIGNAL_STOP;
c906108c
SS
1671#endif
1672#if defined (SIGTSTP)
c5aa993b
JM
1673 if (hostsig == SIGTSTP)
1674 return TARGET_SIGNAL_TSTP;
c906108c
SS
1675#endif
1676#if defined (SIGCONT)
c5aa993b
JM
1677 if (hostsig == SIGCONT)
1678 return TARGET_SIGNAL_CONT;
c906108c
SS
1679#endif
1680#if defined (SIGTTIN)
c5aa993b
JM
1681 if (hostsig == SIGTTIN)
1682 return TARGET_SIGNAL_TTIN;
c906108c
SS
1683#endif
1684#if defined (SIGTTOU)
c5aa993b
JM
1685 if (hostsig == SIGTTOU)
1686 return TARGET_SIGNAL_TTOU;
c906108c
SS
1687#endif
1688#if defined (SIGVTALRM)
c5aa993b
JM
1689 if (hostsig == SIGVTALRM)
1690 return TARGET_SIGNAL_VTALRM;
c906108c
SS
1691#endif
1692#if defined (SIGPROF)
c5aa993b
JM
1693 if (hostsig == SIGPROF)
1694 return TARGET_SIGNAL_PROF;
c906108c
SS
1695#endif
1696#if defined (SIGXCPU)
c5aa993b
JM
1697 if (hostsig == SIGXCPU)
1698 return TARGET_SIGNAL_XCPU;
c906108c
SS
1699#endif
1700#if defined (SIGXFSZ)
c5aa993b
JM
1701 if (hostsig == SIGXFSZ)
1702 return TARGET_SIGNAL_XFSZ;
c906108c
SS
1703#endif
1704#if defined (SIGWIND)
c5aa993b
JM
1705 if (hostsig == SIGWIND)
1706 return TARGET_SIGNAL_WIND;
c906108c
SS
1707#endif
1708#if defined (SIGPHONE)
c5aa993b
JM
1709 if (hostsig == SIGPHONE)
1710 return TARGET_SIGNAL_PHONE;
c906108c
SS
1711#endif
1712#if defined (SIGLOST)
c5aa993b
JM
1713 if (hostsig == SIGLOST)
1714 return TARGET_SIGNAL_LOST;
c906108c
SS
1715#endif
1716#if defined (SIGWAITING)
c5aa993b
JM
1717 if (hostsig == SIGWAITING)
1718 return TARGET_SIGNAL_WAITING;
c906108c 1719#endif
cd0fc7c3 1720#if defined (SIGCANCEL)
c5aa993b
JM
1721 if (hostsig == SIGCANCEL)
1722 return TARGET_SIGNAL_CANCEL;
cd0fc7c3 1723#endif
c906108c 1724#if defined (SIGLWP)
c5aa993b
JM
1725 if (hostsig == SIGLWP)
1726 return TARGET_SIGNAL_LWP;
c906108c
SS
1727#endif
1728#if defined (SIGDANGER)
c5aa993b
JM
1729 if (hostsig == SIGDANGER)
1730 return TARGET_SIGNAL_DANGER;
c906108c
SS
1731#endif
1732#if defined (SIGGRANT)
c5aa993b
JM
1733 if (hostsig == SIGGRANT)
1734 return TARGET_SIGNAL_GRANT;
c906108c
SS
1735#endif
1736#if defined (SIGRETRACT)
c5aa993b
JM
1737 if (hostsig == SIGRETRACT)
1738 return TARGET_SIGNAL_RETRACT;
c906108c
SS
1739#endif
1740#if defined (SIGMSG)
c5aa993b
JM
1741 if (hostsig == SIGMSG)
1742 return TARGET_SIGNAL_MSG;
c906108c
SS
1743#endif
1744#if defined (SIGSOUND)
c5aa993b
JM
1745 if (hostsig == SIGSOUND)
1746 return TARGET_SIGNAL_SOUND;
c906108c
SS
1747#endif
1748#if defined (SIGSAK)
c5aa993b
JM
1749 if (hostsig == SIGSAK)
1750 return TARGET_SIGNAL_SAK;
c906108c
SS
1751#endif
1752#if defined (SIGPRIO)
c5aa993b
JM
1753 if (hostsig == SIGPRIO)
1754 return TARGET_SIGNAL_PRIO;
c906108c
SS
1755#endif
1756
1757 /* Mach exceptions. Assumes that the values for EXC_ are positive! */
1758#if defined (EXC_BAD_ACCESS) && defined (_NSIG)
c5aa993b
JM
1759 if (hostsig == _NSIG + EXC_BAD_ACCESS)
1760 return TARGET_EXC_BAD_ACCESS;
c906108c
SS
1761#endif
1762#if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
c5aa993b
JM
1763 if (hostsig == _NSIG + EXC_BAD_INSTRUCTION)
1764 return TARGET_EXC_BAD_INSTRUCTION;
c906108c
SS
1765#endif
1766#if defined (EXC_ARITHMETIC) && defined (_NSIG)
c5aa993b
JM
1767 if (hostsig == _NSIG + EXC_ARITHMETIC)
1768 return TARGET_EXC_ARITHMETIC;
c906108c
SS
1769#endif
1770#if defined (EXC_EMULATION) && defined (_NSIG)
c5aa993b
JM
1771 if (hostsig == _NSIG + EXC_EMULATION)
1772 return TARGET_EXC_EMULATION;
c906108c
SS
1773#endif
1774#if defined (EXC_SOFTWARE) && defined (_NSIG)
c5aa993b
JM
1775 if (hostsig == _NSIG + EXC_SOFTWARE)
1776 return TARGET_EXC_SOFTWARE;
c906108c
SS
1777#endif
1778#if defined (EXC_BREAKPOINT) && defined (_NSIG)
c5aa993b
JM
1779 if (hostsig == _NSIG + EXC_BREAKPOINT)
1780 return TARGET_EXC_BREAKPOINT;
c906108c
SS
1781#endif
1782
7a292a7a 1783#if defined (SIGINFO)
c5aa993b
JM
1784 if (hostsig == SIGINFO)
1785 return TARGET_SIGNAL_INFO;
7a292a7a
SS
1786#endif
1787
c906108c
SS
1788#if defined (REALTIME_LO)
1789 if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
d4f3574e
SS
1790 {
1791 /* This block of TARGET_SIGNAL_REALTIME value is in order. */
1792 if (33 <= hostsig && hostsig <= 63)
1793 return (enum target_signal)
1794 (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
1795 else if (hostsig == 32)
1796 return TARGET_SIGNAL_REALTIME_32;
1797 else
1798 error ("GDB bug: target.c (target_signal_from_host): unrecognized real-time signal");
1799 }
c906108c 1800#endif
5ef22741
PS
1801
1802#if defined (SIGRTMIN)
1803 if (hostsig >= SIGRTMIN && hostsig <= SIGRTMAX)
1804 {
1805 /* This block of TARGET_SIGNAL_REALTIME value is in order. */
1806 if (33 <= hostsig && hostsig <= 63)
1807 return (enum target_signal)
1808 (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
d57fc352
PS
1809 else if (hostsig == 64)
1810 return TARGET_SIGNAL_REALTIME_64;
5ef22741
PS
1811 else
1812 error ("GDB bug: target.c (target_signal_from_host): unrecognized real-time signal");
1813 }
1814#endif
c906108c
SS
1815 return TARGET_SIGNAL_UNKNOWN;
1816}
1817
c2d11a7d
JM
1818/* Convert a OURSIG (an enum target_signal) to the form used by the
1819 target operating system (refered to as the ``host'') or zero if the
1820 equivalent host signal is not available. Set/clear OURSIG_OK
1821 accordingly. */
1822
1823static int
1824do_target_signal_to_host (enum target_signal oursig,
1825 int *oursig_ok)
c906108c 1826{
c2d11a7d 1827 *oursig_ok = 1;
c906108c
SS
1828 switch (oursig)
1829 {
c5aa993b
JM
1830 case TARGET_SIGNAL_0:
1831 return 0;
c906108c
SS
1832
1833#if defined (SIGHUP)
c5aa993b
JM
1834 case TARGET_SIGNAL_HUP:
1835 return SIGHUP;
c906108c
SS
1836#endif
1837#if defined (SIGINT)
c5aa993b
JM
1838 case TARGET_SIGNAL_INT:
1839 return SIGINT;
c906108c
SS
1840#endif
1841#if defined (SIGQUIT)
c5aa993b
JM
1842 case TARGET_SIGNAL_QUIT:
1843 return SIGQUIT;
c906108c
SS
1844#endif
1845#if defined (SIGILL)
c5aa993b
JM
1846 case TARGET_SIGNAL_ILL:
1847 return SIGILL;
c906108c
SS
1848#endif
1849#if defined (SIGTRAP)
c5aa993b
JM
1850 case TARGET_SIGNAL_TRAP:
1851 return SIGTRAP;
c906108c
SS
1852#endif
1853#if defined (SIGABRT)
c5aa993b
JM
1854 case TARGET_SIGNAL_ABRT:
1855 return SIGABRT;
c906108c
SS
1856#endif
1857#if defined (SIGEMT)
c5aa993b
JM
1858 case TARGET_SIGNAL_EMT:
1859 return SIGEMT;
c906108c
SS
1860#endif
1861#if defined (SIGFPE)
c5aa993b
JM
1862 case TARGET_SIGNAL_FPE:
1863 return SIGFPE;
c906108c
SS
1864#endif
1865#if defined (SIGKILL)
c5aa993b
JM
1866 case TARGET_SIGNAL_KILL:
1867 return SIGKILL;
c906108c
SS
1868#endif
1869#if defined (SIGBUS)
c5aa993b
JM
1870 case TARGET_SIGNAL_BUS:
1871 return SIGBUS;
c906108c
SS
1872#endif
1873#if defined (SIGSEGV)
c5aa993b
JM
1874 case TARGET_SIGNAL_SEGV:
1875 return SIGSEGV;
c906108c
SS
1876#endif
1877#if defined (SIGSYS)
c5aa993b
JM
1878 case TARGET_SIGNAL_SYS:
1879 return SIGSYS;
c906108c
SS
1880#endif
1881#if defined (SIGPIPE)
c5aa993b
JM
1882 case TARGET_SIGNAL_PIPE:
1883 return SIGPIPE;
c906108c
SS
1884#endif
1885#if defined (SIGALRM)
c5aa993b
JM
1886 case TARGET_SIGNAL_ALRM:
1887 return SIGALRM;
c906108c
SS
1888#endif
1889#if defined (SIGTERM)
c5aa993b
JM
1890 case TARGET_SIGNAL_TERM:
1891 return SIGTERM;
c906108c
SS
1892#endif
1893#if defined (SIGUSR1)
c5aa993b
JM
1894 case TARGET_SIGNAL_USR1:
1895 return SIGUSR1;
c906108c
SS
1896#endif
1897#if defined (SIGUSR2)
c5aa993b
JM
1898 case TARGET_SIGNAL_USR2:
1899 return SIGUSR2;
c906108c
SS
1900#endif
1901#if defined (SIGCHLD) || defined (SIGCLD)
c5aa993b 1902 case TARGET_SIGNAL_CHLD:
c906108c
SS
1903#if defined (SIGCHLD)
1904 return SIGCHLD;
1905#else
1906 return SIGCLD;
1907#endif
1908#endif /* SIGCLD or SIGCHLD */
1909#if defined (SIGPWR)
c5aa993b
JM
1910 case TARGET_SIGNAL_PWR:
1911 return SIGPWR;
c906108c
SS
1912#endif
1913#if defined (SIGWINCH)
c5aa993b
JM
1914 case TARGET_SIGNAL_WINCH:
1915 return SIGWINCH;
c906108c
SS
1916#endif
1917#if defined (SIGURG)
c5aa993b
JM
1918 case TARGET_SIGNAL_URG:
1919 return SIGURG;
c906108c
SS
1920#endif
1921#if defined (SIGIO)
c5aa993b
JM
1922 case TARGET_SIGNAL_IO:
1923 return SIGIO;
c906108c
SS
1924#endif
1925#if defined (SIGPOLL)
c5aa993b
JM
1926 case TARGET_SIGNAL_POLL:
1927 return SIGPOLL;
c906108c
SS
1928#endif
1929#if defined (SIGSTOP)
c5aa993b
JM
1930 case TARGET_SIGNAL_STOP:
1931 return SIGSTOP;
c906108c
SS
1932#endif
1933#if defined (SIGTSTP)
c5aa993b
JM
1934 case TARGET_SIGNAL_TSTP:
1935 return SIGTSTP;
c906108c
SS
1936#endif
1937#if defined (SIGCONT)
c5aa993b
JM
1938 case TARGET_SIGNAL_CONT:
1939 return SIGCONT;
c906108c
SS
1940#endif
1941#if defined (SIGTTIN)
c5aa993b
JM
1942 case TARGET_SIGNAL_TTIN:
1943 return SIGTTIN;
c906108c
SS
1944#endif
1945#if defined (SIGTTOU)
c5aa993b
JM
1946 case TARGET_SIGNAL_TTOU:
1947 return SIGTTOU;
c906108c
SS
1948#endif
1949#if defined (SIGVTALRM)
c5aa993b
JM
1950 case TARGET_SIGNAL_VTALRM:
1951 return SIGVTALRM;
c906108c
SS
1952#endif
1953#if defined (SIGPROF)
c5aa993b
JM
1954 case TARGET_SIGNAL_PROF:
1955 return SIGPROF;
c906108c
SS
1956#endif
1957#if defined (SIGXCPU)
c5aa993b
JM
1958 case TARGET_SIGNAL_XCPU:
1959 return SIGXCPU;
c906108c
SS
1960#endif
1961#if defined (SIGXFSZ)
c5aa993b
JM
1962 case TARGET_SIGNAL_XFSZ:
1963 return SIGXFSZ;
c906108c
SS
1964#endif
1965#if defined (SIGWIND)
c5aa993b
JM
1966 case TARGET_SIGNAL_WIND:
1967 return SIGWIND;
c906108c
SS
1968#endif
1969#if defined (SIGPHONE)
c5aa993b
JM
1970 case TARGET_SIGNAL_PHONE:
1971 return SIGPHONE;
c906108c
SS
1972#endif
1973#if defined (SIGLOST)
c5aa993b
JM
1974 case TARGET_SIGNAL_LOST:
1975 return SIGLOST;
c906108c
SS
1976#endif
1977#if defined (SIGWAITING)
c5aa993b
JM
1978 case TARGET_SIGNAL_WAITING:
1979 return SIGWAITING;
c906108c 1980#endif
cd0fc7c3 1981#if defined (SIGCANCEL)
c5aa993b
JM
1982 case TARGET_SIGNAL_CANCEL:
1983 return SIGCANCEL;
cd0fc7c3 1984#endif
c906108c 1985#if defined (SIGLWP)
c5aa993b
JM
1986 case TARGET_SIGNAL_LWP:
1987 return SIGLWP;
c906108c
SS
1988#endif
1989#if defined (SIGDANGER)
c5aa993b
JM
1990 case TARGET_SIGNAL_DANGER:
1991 return SIGDANGER;
c906108c
SS
1992#endif
1993#if defined (SIGGRANT)
c5aa993b
JM
1994 case TARGET_SIGNAL_GRANT:
1995 return SIGGRANT;
c906108c
SS
1996#endif
1997#if defined (SIGRETRACT)
c5aa993b
JM
1998 case TARGET_SIGNAL_RETRACT:
1999 return SIGRETRACT;
c906108c
SS
2000#endif
2001#if defined (SIGMSG)
c5aa993b
JM
2002 case TARGET_SIGNAL_MSG:
2003 return SIGMSG;
c906108c
SS
2004#endif
2005#if defined (SIGSOUND)
c5aa993b
JM
2006 case TARGET_SIGNAL_SOUND:
2007 return SIGSOUND;
c906108c
SS
2008#endif
2009#if defined (SIGSAK)
c5aa993b
JM
2010 case TARGET_SIGNAL_SAK:
2011 return SIGSAK;
c906108c
SS
2012#endif
2013#if defined (SIGPRIO)
c5aa993b
JM
2014 case TARGET_SIGNAL_PRIO:
2015 return SIGPRIO;
c906108c
SS
2016#endif
2017
2018 /* Mach exceptions. Assumes that the values for EXC_ are positive! */
2019#if defined (EXC_BAD_ACCESS) && defined (_NSIG)
c5aa993b
JM
2020 case TARGET_EXC_BAD_ACCESS:
2021 return _NSIG + EXC_BAD_ACCESS;
c906108c
SS
2022#endif
2023#if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
c5aa993b
JM
2024 case TARGET_EXC_BAD_INSTRUCTION:
2025 return _NSIG + EXC_BAD_INSTRUCTION;
c906108c
SS
2026#endif
2027#if defined (EXC_ARITHMETIC) && defined (_NSIG)
c5aa993b
JM
2028 case TARGET_EXC_ARITHMETIC:
2029 return _NSIG + EXC_ARITHMETIC;
c906108c
SS
2030#endif
2031#if defined (EXC_EMULATION) && defined (_NSIG)
c5aa993b
JM
2032 case TARGET_EXC_EMULATION:
2033 return _NSIG + EXC_EMULATION;
c906108c
SS
2034#endif
2035#if defined (EXC_SOFTWARE) && defined (_NSIG)
c5aa993b
JM
2036 case TARGET_EXC_SOFTWARE:
2037 return _NSIG + EXC_SOFTWARE;
c906108c
SS
2038#endif
2039#if defined (EXC_BREAKPOINT) && defined (_NSIG)
c5aa993b
JM
2040 case TARGET_EXC_BREAKPOINT:
2041 return _NSIG + EXC_BREAKPOINT;
c906108c
SS
2042#endif
2043
7a292a7a 2044#if defined (SIGINFO)
c5aa993b
JM
2045 case TARGET_SIGNAL_INFO:
2046 return SIGINFO;
7a292a7a
SS
2047#endif
2048
c906108c
SS
2049 default:
2050#if defined (REALTIME_LO)
2051 if (oursig >= TARGET_SIGNAL_REALTIME_33
2052 && oursig <= TARGET_SIGNAL_REALTIME_63)
2053 {
e95a43ad
MK
2054 /* This block of signals is continuous, and
2055 TARGET_SIGNAL_REALTIME_33 is 33 by definition. */
c906108c 2056 int retsig =
e95a43ad
MK
2057 (int) oursig - (int) TARGET_SIGNAL_REALTIME_33 + 33;
2058 if (retsig >= REALTIME_LO && retsig < REALTIME_HI)
c906108c
SS
2059 return retsig;
2060 }
e95a43ad
MK
2061#if (REALTIME_LO < 33)
2062 else if (oursig == TARGET_SIGNAL_REALTIME_32)
2063 {
2064 /* TARGET_SIGNAL_REALTIME_32 isn't contiguous with
2065 TARGET_SIGNAL_REALTIME_33. It is 32 by definition. */
2066 return 32;
2067 }
2068#endif
5ef22741
PS
2069#endif
2070
2071#if defined (SIGRTMIN)
2072 if (oursig >= TARGET_SIGNAL_REALTIME_33
2073 && oursig <= TARGET_SIGNAL_REALTIME_63)
2074 {
2075 /* This block of signals is continuous, and
2076 TARGET_SIGNAL_REALTIME_33 is 33 by definition. */
2077 int retsig =
2078 (int) oursig - (int) TARGET_SIGNAL_REALTIME_33 + 33;
2079 if (retsig >= SIGRTMIN && retsig <= SIGRTMAX)
2080 return retsig;
2081 }
d57fc352
PS
2082 else if (oursig == TARGET_SIGNAL_REALTIME_64)
2083 return 64;
c906108c 2084#endif
c2d11a7d
JM
2085 *oursig_ok = 0;
2086 return 0;
2087 }
2088}
2089
2090int
2091target_signal_to_host_p (enum target_signal oursig)
2092{
2093 int oursig_ok;
2094 do_target_signal_to_host (oursig, &oursig_ok);
2095 return oursig_ok;
2096}
2097
2098int
2099target_signal_to_host (enum target_signal oursig)
2100{
2101 int oursig_ok;
2102 int targ_signo = do_target_signal_to_host (oursig, &oursig_ok);
2103 if (!oursig_ok)
2104 {
c906108c 2105 /* The user might be trying to do "signal SIGSAK" where this system
c5aa993b 2106 doesn't have SIGSAK. */
c906108c
SS
2107 warning ("Signal %s does not exist on this system.\n",
2108 target_signal_to_name (oursig));
2109 return 0;
2110 }
c2d11a7d
JM
2111 else
2112 return targ_signo;
c906108c
SS
2113}
2114
2115/* Helper function for child_wait and the Lynx derivatives of child_wait.
2116 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2117 translation of that in OURSTATUS. */
2118void
fba45db2 2119store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c
SS
2120{
2121#ifdef CHILD_SPECIAL_WAITSTATUS
2122 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
2123 if it wants to deal with hoststatus. */
2124 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
2125 return;
2126#endif
2127
2128 if (WIFEXITED (hoststatus))
2129 {
2130 ourstatus->kind = TARGET_WAITKIND_EXITED;
2131 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2132 }
2133 else if (!WIFSTOPPED (hoststatus))
2134 {
2135 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2136 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2137 }
2138 else
2139 {
2140 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2141 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2142 }
2143}
2144\f
2145/* In some circumstances we allow a command to specify a numeric
2146 signal. The idea is to keep these circumstances limited so that
2147 users (and scripts) develop portable habits. For comparison,
2148 POSIX.2 `kill' requires that 1,2,3,6,9,14, and 15 work (and using a
7e73cedf 2149 numeric signal at all is obsolescent. We are slightly more
c906108c
SS
2150 lenient and allow 1-15 which should match host signal numbers on
2151 most systems. Use of symbolic signal names is strongly encouraged. */
2152
2153enum target_signal
fba45db2 2154target_signal_from_command (int num)
c906108c
SS
2155{
2156 if (num >= 1 && num <= 15)
c5aa993b 2157 return (enum target_signal) num;
c906108c
SS
2158 error ("Only signals 1-15 are valid as numeric signals.\n\
2159Use \"info signals\" for a list of symbolic signals.");
2160}
2161\f
2162/* Returns zero to leave the inferior alone, one to interrupt it. */
507f3c78 2163int (*target_activity_function) (void);
c906108c
SS
2164int target_activity_fd;
2165\f
2166/* Convert a normal process ID to a string. Returns the string in a static
2167 buffer. */
2168
2169char *
fba45db2 2170normal_pid_to_str (int pid)
c906108c
SS
2171{
2172 static char buf[30];
2173
2174 if (STREQ (current_target.to_shortname, "remote"))
104c1213 2175 sprintf (buf, "thread %d", pid);
c906108c 2176 else
104c1213 2177 sprintf (buf, "process %d", pid);
c906108c
SS
2178
2179 return buf;
2180}
2181
2182/* Some targets (such as ttrace-based HPUX) don't allow us to request
2183 notification of inferior events such as fork and vork immediately
2184 after the inferior is created. (This because of how gdb gets an
2185 inferior created via invoking a shell to do it. In such a scenario,
2186 if the shell init file has commands in it, the shell will fork and
2187 exec for each of those commands, and we will see each such fork
2188 event. Very bad.)
c5aa993b 2189
c906108c
SS
2190 This function is used by all targets that allow us to request
2191 notification of forks, etc at inferior creation time; e.g., in
2192 target_acknowledge_forked_child.
c5aa993b 2193 */
392a587b 2194static void
fba45db2 2195normal_target_post_startup_inferior (int pid)
c906108c
SS
2196{
2197 /* This space intentionally left blank. */
2198}
2199
2200/* Set up the handful of non-empty slots needed by the dummy target
2201 vector. */
2202
2203static void
fba45db2 2204init_dummy_target (void)
c906108c
SS
2205{
2206 dummy_target.to_shortname = "None";
2207 dummy_target.to_longname = "None";
2208 dummy_target.to_doc = "";
2209 dummy_target.to_attach = find_default_attach;
2210 dummy_target.to_require_attach = find_default_require_attach;
2211 dummy_target.to_require_detach = find_default_require_detach;
2212 dummy_target.to_create_inferior = find_default_create_inferior;
2213 dummy_target.to_clone_and_follow_inferior = find_default_clone_and_follow_inferior;
ed9a39eb 2214 dummy_target.to_pid_to_str = normal_pid_to_str;
c906108c
SS
2215 dummy_target.to_stratum = dummy_stratum;
2216 dummy_target.to_magic = OPS_MAGIC;
2217}
c906108c 2218\f
c5aa993b 2219
c906108c
SS
2220static struct target_ops debug_target;
2221
2222static void
fba45db2 2223debug_to_open (char *args, int from_tty)
c906108c
SS
2224{
2225 debug_target.to_open (args, from_tty);
2226
96baa820 2227 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
2228}
2229
2230static void
fba45db2 2231debug_to_close (int quitting)
c906108c
SS
2232{
2233 debug_target.to_close (quitting);
2234
96baa820 2235 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
c906108c
SS
2236}
2237
2238static void
fba45db2 2239debug_to_attach (char *args, int from_tty)
c906108c
SS
2240{
2241 debug_target.to_attach (args, from_tty);
2242
96baa820 2243 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n", args, from_tty);
c906108c
SS
2244}
2245
2246
2247static void
fba45db2 2248debug_to_post_attach (int pid)
c906108c
SS
2249{
2250 debug_target.to_post_attach (pid);
2251
96baa820 2252 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
2253}
2254
2255static void
fba45db2 2256debug_to_require_attach (char *args, int from_tty)
c906108c
SS
2257{
2258 debug_target.to_require_attach (args, from_tty);
2259
96baa820 2260 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2261 "target_require_attach (%s, %d)\n", args, from_tty);
2262}
2263
2264static void
fba45db2 2265debug_to_detach (char *args, int from_tty)
c906108c
SS
2266{
2267 debug_target.to_detach (args, from_tty);
2268
96baa820 2269 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n", args, from_tty);
c906108c
SS
2270}
2271
2272static void
fba45db2 2273debug_to_require_detach (int pid, char *args, int from_tty)
c906108c
SS
2274{
2275 debug_target.to_require_detach (pid, args, from_tty);
2276
96baa820 2277 fprintf_unfiltered (gdb_stdlog,
c5aa993b 2278 "target_require_detach (%d, %s, %d)\n", pid, args, from_tty);
c906108c
SS
2279}
2280
2281static void
fba45db2 2282debug_to_resume (int pid, int step, enum target_signal siggnal)
c906108c
SS
2283{
2284 debug_target.to_resume (pid, step, siggnal);
2285
96baa820 2286 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", pid,
c906108c
SS
2287 step ? "step" : "continue",
2288 target_signal_to_name (siggnal));
2289}
2290
2291static int
fba45db2 2292debug_to_wait (int pid, struct target_waitstatus *status)
c906108c
SS
2293{
2294 int retval;
2295
2296 retval = debug_target.to_wait (pid, status);
2297
96baa820 2298 fprintf_unfiltered (gdb_stdlog,
c906108c 2299 "target_wait (%d, status) = %d, ", pid, retval);
96baa820 2300 fprintf_unfiltered (gdb_stdlog, "status->kind = ");
c906108c
SS
2301 switch (status->kind)
2302 {
2303 case TARGET_WAITKIND_EXITED:
96baa820 2304 fprintf_unfiltered (gdb_stdlog, "exited, status = %d\n",
c906108c
SS
2305 status->value.integer);
2306 break;
2307 case TARGET_WAITKIND_STOPPED:
96baa820 2308 fprintf_unfiltered (gdb_stdlog, "stopped, signal = %s\n",
c906108c
SS
2309 target_signal_to_name (status->value.sig));
2310 break;
2311 case TARGET_WAITKIND_SIGNALLED:
96baa820 2312 fprintf_unfiltered (gdb_stdlog, "signalled, signal = %s\n",
c906108c
SS
2313 target_signal_to_name (status->value.sig));
2314 break;
2315 case TARGET_WAITKIND_LOADED:
96baa820 2316 fprintf_unfiltered (gdb_stdlog, "loaded\n");
c906108c
SS
2317 break;
2318 case TARGET_WAITKIND_FORKED:
96baa820 2319 fprintf_unfiltered (gdb_stdlog, "forked\n");
c906108c
SS
2320 break;
2321 case TARGET_WAITKIND_VFORKED:
96baa820 2322 fprintf_unfiltered (gdb_stdlog, "vforked\n");
c906108c
SS
2323 break;
2324 case TARGET_WAITKIND_EXECD:
96baa820 2325 fprintf_unfiltered (gdb_stdlog, "execd\n");
c906108c
SS
2326 break;
2327 case TARGET_WAITKIND_SPURIOUS:
96baa820 2328 fprintf_unfiltered (gdb_stdlog, "spurious\n");
c906108c
SS
2329 break;
2330 default:
96baa820 2331 fprintf_unfiltered (gdb_stdlog, "unknown???\n");
c906108c
SS
2332 break;
2333 }
2334
2335 return retval;
2336}
2337
2338static void
fba45db2 2339debug_to_post_wait (int pid, int status)
c906108c
SS
2340{
2341 debug_target.to_post_wait (pid, status);
2342
96baa820 2343 fprintf_unfiltered (gdb_stdlog, "target_post_wait (%d, %d)\n",
c906108c
SS
2344 pid, status);
2345}
2346
2347static void
fba45db2 2348debug_to_fetch_registers (int regno)
c906108c
SS
2349{
2350 debug_target.to_fetch_registers (regno);
2351
96baa820 2352 fprintf_unfiltered (gdb_stdlog, "target_fetch_registers (%s)",
c906108c
SS
2353 regno != -1 ? REGISTER_NAME (regno) : "-1");
2354 if (regno != -1)
104c1213 2355 fprintf_unfiltered (gdb_stdlog, " = 0x%lx %ld",
c906108c 2356 (unsigned long) read_register (regno),
104c1213 2357 (unsigned long) read_register (regno));
96baa820 2358 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
2359}
2360
2361static void
fba45db2 2362debug_to_store_registers (int regno)
c906108c
SS
2363{
2364 debug_target.to_store_registers (regno);
2365
2366 if (regno >= 0 && regno < NUM_REGS)
104c1213 2367 fprintf_unfiltered (gdb_stdlog, "target_store_registers (%s) = 0x%lx %ld\n",
c906108c
SS
2368 REGISTER_NAME (regno),
2369 (unsigned long) read_register (regno),
2370 (unsigned long) read_register (regno));
2371 else
96baa820 2372 fprintf_unfiltered (gdb_stdlog, "target_store_registers (%d)\n", regno);
c906108c
SS
2373}
2374
2375static void
fba45db2 2376debug_to_prepare_to_store (void)
c906108c
SS
2377{
2378 debug_target.to_prepare_to_store ();
2379
96baa820 2380 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
2381}
2382
2383static int
fba45db2 2384debug_to_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
29e57380 2385 struct mem_attrib *attrib,
fba45db2 2386 struct target_ops *target)
c906108c
SS
2387{
2388 int retval;
2389
29e57380
C
2390 retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write,
2391 attrib, target);
c906108c 2392
96baa820 2393 fprintf_unfiltered (gdb_stdlog,
c906108c 2394 "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
c5aa993b 2395 (unsigned int) memaddr, /* possable truncate long long */
c906108c
SS
2396 len, write ? "write" : "read", retval);
2397
c5aa993b 2398
c906108c
SS
2399
2400 if (retval > 0)
2401 {
2402 int i;
2403
96baa820 2404 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
2405 for (i = 0; i < retval; i++)
2406 {
2407 if ((((long) &(myaddr[i])) & 0xf) == 0)
96baa820
JM
2408 fprintf_unfiltered (gdb_stdlog, "\n");
2409 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
2410 }
2411 }
2412
96baa820 2413 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
2414
2415 return retval;
2416}
2417
2418static void
fba45db2 2419debug_to_files_info (struct target_ops *target)
c906108c
SS
2420{
2421 debug_target.to_files_info (target);
2422
96baa820 2423 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
2424}
2425
2426static int
fba45db2 2427debug_to_insert_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
2428{
2429 int retval;
2430
2431 retval = debug_target.to_insert_breakpoint (addr, save);
2432
96baa820 2433 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
2434 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2435 (unsigned long) addr,
2436 (unsigned long) retval);
c906108c
SS
2437 return retval;
2438}
2439
2440static int
fba45db2 2441debug_to_remove_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
2442{
2443 int retval;
2444
2445 retval = debug_target.to_remove_breakpoint (addr, save);
2446
96baa820 2447 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
2448 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2449 (unsigned long) addr,
2450 (unsigned long) retval);
c906108c
SS
2451 return retval;
2452}
2453
2454static void
fba45db2 2455debug_to_terminal_init (void)
c906108c
SS
2456{
2457 debug_target.to_terminal_init ();
2458
96baa820 2459 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
2460}
2461
2462static void
fba45db2 2463debug_to_terminal_inferior (void)
c906108c
SS
2464{
2465 debug_target.to_terminal_inferior ();
2466
96baa820 2467 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
2468}
2469
2470static void
fba45db2 2471debug_to_terminal_ours_for_output (void)
c906108c
SS
2472{
2473 debug_target.to_terminal_ours_for_output ();
2474
96baa820 2475 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
2476}
2477
2478static void
fba45db2 2479debug_to_terminal_ours (void)
c906108c
SS
2480{
2481 debug_target.to_terminal_ours ();
2482
96baa820 2483 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
2484}
2485
2486static void
fba45db2 2487debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
2488{
2489 debug_target.to_terminal_info (arg, from_tty);
2490
96baa820 2491 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
2492 from_tty);
2493}
2494
2495static void
fba45db2 2496debug_to_kill (void)
c906108c
SS
2497{
2498 debug_target.to_kill ();
2499
96baa820 2500 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
c906108c
SS
2501}
2502
2503static void
fba45db2 2504debug_to_load (char *args, int from_tty)
c906108c
SS
2505{
2506 debug_target.to_load (args, from_tty);
2507
96baa820 2508 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
2509}
2510
2511static int
fba45db2 2512debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
2513{
2514 int retval;
2515
2516 retval = debug_target.to_lookup_symbol (name, addrp);
2517
96baa820 2518 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
2519
2520 return retval;
2521}
2522
2523static void
fba45db2 2524debug_to_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
2525{
2526 debug_target.to_create_inferior (exec_file, args, env);
2527
96baa820 2528 fprintf_unfiltered (gdb_stdlog, "target_create_inferior (%s, %s, xxx)\n",
c906108c
SS
2529 exec_file, args);
2530}
2531
2532static void
fba45db2 2533debug_to_post_startup_inferior (int pid)
c906108c
SS
2534{
2535 debug_target.to_post_startup_inferior (pid);
2536
96baa820 2537 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
c906108c
SS
2538 pid);
2539}
2540
2541static void
fba45db2 2542debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
2543{
2544 debug_target.to_acknowledge_created_inferior (pid);
2545
96baa820 2546 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
2547 pid);
2548}
2549
2550static void
fba45db2 2551debug_to_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
2552{
2553 debug_target.to_clone_and_follow_inferior (child_pid, followed_child);
2554
96baa820 2555 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2556 "target_clone_and_follow_inferior (%d, %d)\n",
2557 child_pid, *followed_child);
2558}
2559
2560static void
fba45db2 2561debug_to_post_follow_inferior_by_clone (void)
c906108c
SS
2562{
2563 debug_target.to_post_follow_inferior_by_clone ();
2564
96baa820 2565 fprintf_unfiltered (gdb_stdlog, "target_post_follow_inferior_by_clone ()\n");
c906108c
SS
2566}
2567
2568static int
fba45db2 2569debug_to_insert_fork_catchpoint (int pid)
c906108c 2570{
c5aa993b 2571 int retval;
c906108c
SS
2572
2573 retval = debug_target.to_insert_fork_catchpoint (pid);
2574
96baa820 2575 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
c5aa993b 2576 pid, retval);
c906108c
SS
2577
2578 return retval;
2579}
2580
2581static int
fba45db2 2582debug_to_remove_fork_catchpoint (int pid)
c906108c 2583{
c5aa993b 2584 int retval;
c906108c
SS
2585
2586 retval = debug_target.to_remove_fork_catchpoint (pid);
2587
96baa820 2588 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 2589 pid, retval);
c906108c
SS
2590
2591 return retval;
2592}
2593
2594static int
fba45db2 2595debug_to_insert_vfork_catchpoint (int pid)
c906108c 2596{
c5aa993b 2597 int retval;
c906108c
SS
2598
2599 retval = debug_target.to_insert_vfork_catchpoint (pid);
2600
96baa820 2601 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)= %d\n",
c5aa993b 2602 pid, retval);
c906108c
SS
2603
2604 return retval;
2605}
2606
2607static int
fba45db2 2608debug_to_remove_vfork_catchpoint (int pid)
c906108c 2609{
c5aa993b 2610 int retval;
c906108c
SS
2611
2612 retval = debug_target.to_remove_vfork_catchpoint (pid);
2613
96baa820 2614 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 2615 pid, retval);
c906108c
SS
2616
2617 return retval;
2618}
2619
2620static int
fba45db2 2621debug_to_has_forked (int pid, int *child_pid)
c906108c 2622{
c5aa993b 2623 int has_forked;
c906108c
SS
2624
2625 has_forked = debug_target.to_has_forked (pid, child_pid);
2626
96baa820 2627 fprintf_unfiltered (gdb_stdlog, "target_has_forked (%d, %d) = %d\n",
c5aa993b 2628 pid, *child_pid, has_forked);
c906108c
SS
2629
2630 return has_forked;
2631}
2632
2633static int
fba45db2 2634debug_to_has_vforked (int pid, int *child_pid)
c906108c 2635{
c5aa993b 2636 int has_vforked;
c906108c
SS
2637
2638 has_vforked = debug_target.to_has_vforked (pid, child_pid);
2639
96baa820 2640 fprintf_unfiltered (gdb_stdlog, "target_has_vforked (%d, %d) = %d\n",
c5aa993b 2641 pid, *child_pid, has_vforked);
c906108c
SS
2642
2643 return has_vforked;
2644}
2645
2646static int
fba45db2 2647debug_to_can_follow_vfork_prior_to_exec (void)
c906108c 2648{
c5aa993b 2649 int can_immediately_follow_vfork;
c906108c
SS
2650
2651 can_immediately_follow_vfork = debug_target.to_can_follow_vfork_prior_to_exec ();
2652
96baa820 2653 fprintf_unfiltered (gdb_stdlog, "target_can_follow_vfork_prior_to_exec () = %d\n",
c5aa993b 2654 can_immediately_follow_vfork);
c906108c
SS
2655
2656 return can_immediately_follow_vfork;
2657}
2658
2659static void
fba45db2
KB
2660debug_to_post_follow_vfork (int parent_pid, int followed_parent, int child_pid,
2661 int followed_child)
c906108c
SS
2662{
2663 debug_target.to_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child);
2664
96baa820 2665 fprintf_unfiltered (gdb_stdlog,
c906108c 2666 "target_post_follow_vfork (%d, %d, %d, %d)\n",
c5aa993b 2667 parent_pid, followed_parent, child_pid, followed_child);
c906108c
SS
2668}
2669
2670static int
fba45db2 2671debug_to_insert_exec_catchpoint (int pid)
c906108c 2672{
c5aa993b 2673 int retval;
c906108c
SS
2674
2675 retval = debug_target.to_insert_exec_catchpoint (pid);
2676
96baa820 2677 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
c5aa993b 2678 pid, retval);
c906108c
SS
2679
2680 return retval;
2681}
2682
2683static int
fba45db2 2684debug_to_remove_exec_catchpoint (int pid)
c906108c 2685{
c5aa993b 2686 int retval;
c906108c
SS
2687
2688 retval = debug_target.to_remove_exec_catchpoint (pid);
2689
96baa820 2690 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 2691 pid, retval);
c906108c
SS
2692
2693 return retval;
2694}
2695
2696static int
fba45db2 2697debug_to_has_execd (int pid, char **execd_pathname)
c906108c 2698{
c5aa993b 2699 int has_execd;
c906108c
SS
2700
2701 has_execd = debug_target.to_has_execd (pid, execd_pathname);
2702
96baa820 2703 fprintf_unfiltered (gdb_stdlog, "target_has_execd (%d, %s) = %d\n",
c5aa993b 2704 pid, (*execd_pathname ? *execd_pathname : "<NULL>"),
7a292a7a 2705 has_execd);
c906108c
SS
2706
2707 return has_execd;
2708}
2709
2710static int
fba45db2 2711debug_to_reported_exec_events_per_exec_call (void)
c906108c 2712{
c5aa993b 2713 int reported_exec_events;
c906108c
SS
2714
2715 reported_exec_events = debug_target.to_reported_exec_events_per_exec_call ();
2716
96baa820 2717 fprintf_unfiltered (gdb_stdlog,
c906108c 2718 "target_reported_exec_events_per_exec_call () = %d\n",
c5aa993b 2719 reported_exec_events);
c906108c
SS
2720
2721 return reported_exec_events;
2722}
2723
2724static int
fba45db2
KB
2725debug_to_has_syscall_event (int pid, enum target_waitkind *kind,
2726 int *syscall_id)
c906108c 2727{
c5aa993b
JM
2728 int has_syscall_event;
2729 char *kind_spelling = "??";
c906108c
SS
2730
2731 has_syscall_event = debug_target.to_has_syscall_event (pid, kind, syscall_id);
2732 if (has_syscall_event)
2733 {
2734 switch (*kind)
c5aa993b
JM
2735 {
2736 case TARGET_WAITKIND_SYSCALL_ENTRY:
2737 kind_spelling = "SYSCALL_ENTRY";
2738 break;
2739 case TARGET_WAITKIND_SYSCALL_RETURN:
2740 kind_spelling = "SYSCALL_RETURN";
2741 break;
2742 default:
2743 break;
2744 }
c906108c
SS
2745 }
2746
96baa820 2747 fprintf_unfiltered (gdb_stdlog,
c906108c 2748 "target_has_syscall_event (%d, %s, %d) = %d\n",
c5aa993b 2749 pid, kind_spelling, *syscall_id, has_syscall_event);
c906108c
SS
2750
2751 return has_syscall_event;
2752}
2753
2754static int
fba45db2 2755debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 2756{
c5aa993b 2757 int has_exited;
c906108c
SS
2758
2759 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
2760
96baa820 2761 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 2762 pid, wait_status, *exit_status, has_exited);
c906108c
SS
2763
2764 return has_exited;
2765}
2766
2767static void
fba45db2 2768debug_to_mourn_inferior (void)
c906108c
SS
2769{
2770 debug_target.to_mourn_inferior ();
2771
96baa820 2772 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
c906108c
SS
2773}
2774
2775static int
fba45db2 2776debug_to_can_run (void)
c906108c
SS
2777{
2778 int retval;
2779
2780 retval = debug_target.to_can_run ();
2781
96baa820 2782 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
2783
2784 return retval;
2785}
2786
2787static void
fba45db2 2788debug_to_notice_signals (int pid)
c906108c
SS
2789{
2790 debug_target.to_notice_signals (pid);
2791
96baa820 2792 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n", pid);
c906108c
SS
2793}
2794
2795static int
fba45db2 2796debug_to_thread_alive (int pid)
c906108c
SS
2797{
2798 int retval;
2799
2800 retval = debug_target.to_thread_alive (pid);
2801
96baa820 2802 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
c906108c
SS
2803 pid, retval);
2804
2805 return retval;
2806}
2807
0d06e24b 2808static void
fba45db2 2809debug_to_find_new_threads (void)
0d06e24b
JM
2810{
2811 debug_target.to_find_new_threads ();
2812
2813 fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
2814}
2815
c906108c 2816static void
fba45db2 2817debug_to_stop (void)
c906108c
SS
2818{
2819 debug_target.to_stop ();
2820
96baa820 2821 fprintf_unfiltered (gdb_stdlog, "target_stop ()\n");
c906108c
SS
2822}
2823
2824static int
fba45db2 2825debug_to_query (int type, char *req, char *resp, int *siz)
c906108c
SS
2826{
2827 int retval;
2828
2829 retval = debug_target.to_query (type, req, resp, siz);
2830
96baa820 2831 fprintf_unfiltered (gdb_stdlog, "target_query (%c, %s, %s, %d) = %d\n", type, req, resp, *siz, retval);
c906108c
SS
2832
2833 return retval;
2834}
2835
96baa820
JM
2836static void
2837debug_to_rcmd (char *command,
d9fcf2fb 2838 struct ui_file *outbuf)
96baa820
JM
2839{
2840 debug_target.to_rcmd (command, outbuf);
2841 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
2842}
2843
c906108c 2844static struct symtab_and_line *
fba45db2 2845debug_to_enable_exception_callback (enum exception_event_kind kind, int enable)
c906108c 2846{
7a292a7a
SS
2847 struct symtab_and_line *result;
2848 result = debug_target.to_enable_exception_callback (kind, enable);
96baa820 2849 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2850 "target get_exception_callback_sal (%d, %d)\n",
2851 kind, enable);
7a292a7a 2852 return result;
c906108c
SS
2853}
2854
2855static struct exception_event_record *
fba45db2 2856debug_to_get_current_exception_event (void)
c906108c 2857{
7a292a7a 2858 struct exception_event_record *result;
c5aa993b 2859 result = debug_target.to_get_current_exception_event ();
96baa820 2860 fprintf_unfiltered (gdb_stdlog, "target get_current_exception_event ()\n");
7a292a7a 2861 return result;
c906108c
SS
2862}
2863
2864static char *
fba45db2 2865debug_to_pid_to_exec_file (int pid)
c906108c 2866{
c5aa993b 2867 char *exec_file;
c906108c
SS
2868
2869 exec_file = debug_target.to_pid_to_exec_file (pid);
2870
96baa820 2871 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 2872 pid, exec_file);
c906108c
SS
2873
2874 return exec_file;
2875}
2876
2877static char *
fba45db2 2878debug_to_core_file_to_sym_file (char *core)
c906108c 2879{
c5aa993b 2880 char *sym_file;
c906108c
SS
2881
2882 sym_file = debug_target.to_core_file_to_sym_file (core);
2883
96baa820 2884 fprintf_unfiltered (gdb_stdlog, "target_core_file_to_sym_file (%s) = %s\n",
c5aa993b 2885 core, sym_file);
c906108c
SS
2886
2887 return sym_file;
2888}
2889
2890static void
fba45db2 2891setup_target_debug (void)
c906108c
SS
2892{
2893 memcpy (&debug_target, &current_target, sizeof debug_target);
2894
2895 current_target.to_open = debug_to_open;
2896 current_target.to_close = debug_to_close;
2897 current_target.to_attach = debug_to_attach;
2898 current_target.to_post_attach = debug_to_post_attach;
2899 current_target.to_require_attach = debug_to_require_attach;
2900 current_target.to_detach = debug_to_detach;
2901 current_target.to_require_detach = debug_to_require_detach;
2902 current_target.to_resume = debug_to_resume;
2903 current_target.to_wait = debug_to_wait;
2904 current_target.to_post_wait = debug_to_post_wait;
2905 current_target.to_fetch_registers = debug_to_fetch_registers;
2906 current_target.to_store_registers = debug_to_store_registers;
2907 current_target.to_prepare_to_store = debug_to_prepare_to_store;
2908 current_target.to_xfer_memory = debug_to_xfer_memory;
2909 current_target.to_files_info = debug_to_files_info;
2910 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2911 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
2912 current_target.to_terminal_init = debug_to_terminal_init;
2913 current_target.to_terminal_inferior = debug_to_terminal_inferior;
2914 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2915 current_target.to_terminal_ours = debug_to_terminal_ours;
2916 current_target.to_terminal_info = debug_to_terminal_info;
2917 current_target.to_kill = debug_to_kill;
2918 current_target.to_load = debug_to_load;
2919 current_target.to_lookup_symbol = debug_to_lookup_symbol;
2920 current_target.to_create_inferior = debug_to_create_inferior;
2921 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
2922 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
2923 current_target.to_clone_and_follow_inferior = debug_to_clone_and_follow_inferior;
2924 current_target.to_post_follow_inferior_by_clone = debug_to_post_follow_inferior_by_clone;
2925 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
2926 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
2927 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
2928 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
2929 current_target.to_has_forked = debug_to_has_forked;
2930 current_target.to_has_vforked = debug_to_has_vforked;
2931 current_target.to_can_follow_vfork_prior_to_exec = debug_to_can_follow_vfork_prior_to_exec;
2932 current_target.to_post_follow_vfork = debug_to_post_follow_vfork;
2933 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
2934 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
2935 current_target.to_has_execd = debug_to_has_execd;
2936 current_target.to_reported_exec_events_per_exec_call = debug_to_reported_exec_events_per_exec_call;
2937 current_target.to_has_syscall_event = debug_to_has_syscall_event;
2938 current_target.to_has_exited = debug_to_has_exited;
2939 current_target.to_mourn_inferior = debug_to_mourn_inferior;
2940 current_target.to_can_run = debug_to_can_run;
2941 current_target.to_notice_signals = debug_to_notice_signals;
2942 current_target.to_thread_alive = debug_to_thread_alive;
0d06e24b 2943 current_target.to_find_new_threads = debug_to_find_new_threads;
c906108c
SS
2944 current_target.to_stop = debug_to_stop;
2945 current_target.to_query = debug_to_query;
96baa820 2946 current_target.to_rcmd = debug_to_rcmd;
c906108c
SS
2947 current_target.to_enable_exception_callback = debug_to_enable_exception_callback;
2948 current_target.to_get_current_exception_event = debug_to_get_current_exception_event;
2949 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
2950 current_target.to_core_file_to_sym_file = debug_to_core_file_to_sym_file;
2951
2952}
c906108c 2953\f
c5aa993b
JM
2954
2955static char targ_desc[] =
2956"Names of targets and files being debugged.\n\
c906108c
SS
2957Shows the entire stack of targets currently in use (including the exec-file,\n\
2958core-file, and process, if any), as well as the symbol file name.";
2959
96baa820
JM
2960static void
2961do_monitor_command (char *cmd,
2962 int from_tty)
2963{
2b5fe715
AC
2964 if ((current_target.to_rcmd
2965 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 2966 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
2967 && (debug_target.to_rcmd
2968 == (void (*) (char *, struct ui_file *)) tcomplain)))
96baa820
JM
2969 {
2970 error ("\"monitor\" command not supported by this target.\n");
2971 }
2972 target_rcmd (cmd, gdb_stdtarg);
2973}
2974
c906108c 2975void
fba45db2 2976initialize_targets (void)
c906108c
SS
2977{
2978 init_dummy_target ();
2979 push_target (&dummy_target);
2980
2981 add_info ("target", target_info, targ_desc);
2982 add_info ("files", target_info, targ_desc);
2983
c906108c 2984 add_show_from_set (
5d161b24 2985 add_set_cmd ("target", class_maintenance, var_zinteger,
c5aa993b
JM
2986 (char *) &targetdebug,
2987 "Set target debugging.\n\
5d161b24
DB
2988When non-zero, target debugging is enabled.", &setdebuglist),
2989 &showdebuglist);
c906108c 2990
96baa820
JM
2991
2992 add_com ("monitor", class_obscure, do_monitor_command,
2993 "Send a command to the remote monitor (remote targets only).");
2994
4930751a
C
2995 target_dcache = dcache_init();
2996
c906108c 2997 if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
e1e9e218 2998 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c 2999}
This page took 0.238004 seconds and 4 git commands to generate.