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