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