* TODO: Note abstraction layer violation where "ocd reset" command
[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
c906108c
SS
840/* Move memory to or from the targets. Iterate until all of it has
841 been moved, if necessary. The top target gets priority; anything
842 it doesn't want, is offered to the next one down, etc. Note the
843 business with curlen: if an early target says "no, but I have a
844 boundary overlapping this xfer" then we shorten what we offer to
845 the subsequent targets so the early guy will get a chance at the
846 tail before the subsequent ones do.
847
848 Result is 0 or errno value. */
849
4930751a
C
850int
851do_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write)
c906108c 852{
c906108c 853 int res;
4930751a 854 int done = 0;
c906108c
SS
855 struct target_ops *t;
856 struct target_stack_item *item;
857
858 /* Zero length requests are ok and require no work. */
859 if (len == 0)
860 return 0;
861
c906108c
SS
862 /* to_xfer_memory is not guaranteed to set errno, even when it returns
863 0. */
864 errno = 0;
865
866 /* The quick case is that the top target does it all. */
867 res = current_target.to_xfer_memory
c5aa993b 868 (memaddr, myaddr, len, write, &current_target);
c906108c 869 if (res == len)
4930751a 870 return len;
c906108c
SS
871
872 if (res > 0)
873 goto bump;
874 /* If res <= 0 then we call it again in the loop. Ah well. */
875
4930751a 876 while (len > 0)
c906108c 877 {
c906108c
SS
878 for (item = target_stack; item; item = item->next)
879 {
880 t = item->target_ops;
881 if (!t->to_has_memory)
882 continue;
883
4930751a 884 res = t->to_xfer_memory (memaddr, myaddr, len, write, t);
c906108c
SS
885 if (res > 0)
886 break; /* Handled all or part of xfer */
887 if (t->to_has_all_memory)
888 break;
889 }
890
4930751a
C
891 if (res <= 0)
892 {
893 return -1;
894 }
895 bump:
896 done += res;
897 memaddr += res;
898 myaddr += res;
899 len -= res;
900 }
901
902 return done;
903}
904
905static int
906target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write)
907{
908 int res;
909
910 /* Zero length requests are ok and require no work. */
911 if (len == 0)
912 {
913 return 0;
914 }
915
916 while (len > 0)
917 {
918 res = dcache_xfer_memory(target_dcache, memaddr, myaddr, len, write);
c906108c
SS
919 if (res <= 0)
920 {
921 /* If this address is for nonexistent memory,
922 read zeros if reading, or do nothing if writing. Return error. */
923 if (!write)
924 memset (myaddr, 0, len);
925 if (errno == 0)
926 return EIO;
927 else
928 return errno;
929 }
4930751a 930
c906108c 931 memaddr += res;
4930751a
C
932 myaddr += res;
933 len -= res;
c906108c 934 }
4930751a 935
c906108c
SS
936 return 0; /* We managed to cover it all somehow. */
937}
938
939
917317f4
JM
940/* Perform a partial memory transfer. */
941
942static int
4930751a 943target_xfer_memory_partial (CORE_ADDR memaddr, char *myaddr, int len,
917317f4
JM
944 int write_p, int *err)
945{
946 int res;
947 int err_res;
948 int len_res;
949 struct target_ops *t;
950 struct target_stack_item *item;
951
952 /* Zero length requests are ok and require no work. */
953 if (len == 0)
954 {
955 *err = 0;
956 return 0;
957 }
958
4930751a
C
959 res = dcache_xfer_memory (target_dcache, memaddr, myaddr, len, write_p);
960 if (res <= 0)
917317f4 961 {
4930751a
C
962 if (errno != 0)
963 *err = errno;
964 else
965 *err = EIO;
917317f4 966
4930751a 967 return -1;
917317f4
JM
968 }
969
4930751a
C
970 *err = 0;
971 return 0;
917317f4
JM
972}
973
974int
975target_read_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
976{
977 return target_xfer_memory_partial (memaddr, buf, len, 0, err);
978}
979
980int
981target_write_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
982{
983 return target_xfer_memory_partial (memaddr, buf, len, 1, err);
984}
985
c906108c
SS
986/* ARGSUSED */
987static void
fba45db2 988target_info (char *args, int from_tty)
c906108c
SS
989{
990 struct target_ops *t;
991 struct target_stack_item *item;
992 int has_all_mem = 0;
c5aa993b 993
c906108c
SS
994 if (symfile_objfile != NULL)
995 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
996
997#ifdef FILES_INFO_HOOK
998 if (FILES_INFO_HOOK ())
999 return;
1000#endif
1001
1002 for (item = target_stack; item; item = item->next)
1003 {
1004 t = item->target_ops;
1005
1006 if (!t->to_has_memory)
1007 continue;
1008
c5aa993b 1009 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
1010 continue;
1011 if (has_all_mem)
c5aa993b
JM
1012 printf_unfiltered ("\tWhile running this, GDB does not access memory from...\n");
1013 printf_unfiltered ("%s:\n", t->to_longname);
1014 (t->to_files_info) (t);
c906108c
SS
1015 has_all_mem = t->to_has_all_memory;
1016 }
1017}
1018
1019/* This is to be called by the open routine before it does
1020 anything. */
1021
1022void
fba45db2 1023target_preopen (int from_tty)
c906108c 1024{
c5aa993b 1025 dont_repeat ();
c906108c
SS
1026
1027 if (target_has_execution)
c5aa993b 1028 {
adf40b2e
JM
1029 if (!from_tty
1030 || query ("A program is being debugged already. Kill it? "))
c5aa993b 1031 target_kill ();
c906108c 1032 else
c5aa993b 1033 error ("Program not killed.");
c906108c
SS
1034 }
1035
1036 /* Calling target_kill may remove the target from the stack. But if
1037 it doesn't (which seems like a win for UDI), remove it now. */
1038
1039 if (target_has_execution)
1040 pop_target ();
1041}
1042
1043/* Detach a target after doing deferred register stores. */
1044
1045void
fba45db2 1046target_detach (char *args, int from_tty)
c906108c
SS
1047{
1048 /* Handle any optimized stores to the inferior. */
1049#ifdef DO_DEFERRED_STORES
1050 DO_DEFERRED_STORES;
1051#endif
1052 (current_target.to_detach) (args, from_tty);
1053}
1054
1055void
fba45db2 1056target_link (char *modname, CORE_ADDR *t_reloc)
c906108c 1057{
c5aa993b 1058 if (STREQ (current_target.to_shortname, "rombug"))
c906108c
SS
1059 {
1060 (current_target.to_lookup_symbol) (modname, t_reloc);
1061 if (*t_reloc == 0)
c5aa993b 1062 error ("Unable to link to %s and get relocation in rombug", modname);
c906108c
SS
1063 }
1064 else
2acceee2 1065 *t_reloc = (CORE_ADDR) -1;
c906108c
SS
1066}
1067
ed9a39eb
JM
1068int
1069target_async_mask (int mask)
1070{
1071 int saved_async_masked_status = target_async_mask_value;
1072 target_async_mask_value = mask;
1073 return saved_async_masked_status;
1074}
1075
c906108c
SS
1076/* Look through the list of possible targets for a target that can
1077 execute a run or attach command without any other data. This is
1078 used to locate the default process stratum.
1079
1080 Result is always valid (error() is called for errors). */
1081
1082static struct target_ops *
fba45db2 1083find_default_run_target (char *do_mesg)
c906108c
SS
1084{
1085 struct target_ops **t;
1086 struct target_ops *runable = NULL;
1087 int count;
1088
1089 count = 0;
1090
1091 for (t = target_structs; t < target_structs + target_struct_size;
1092 ++t)
1093 {
c5aa993b 1094 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
1095 {
1096 runable = *t;
1097 ++count;
1098 }
1099 }
1100
1101 if (count != 1)
1102 error ("Don't know how to %s. Try \"help target\".", do_mesg);
1103
1104 return runable;
1105}
1106
1107void
fba45db2 1108find_default_attach (char *args, int from_tty)
c906108c
SS
1109{
1110 struct target_ops *t;
1111
c5aa993b 1112 t = find_default_run_target ("attach");
c906108c
SS
1113 (t->to_attach) (args, from_tty);
1114 return;
1115}
1116
1117void
fba45db2 1118find_default_require_attach (char *args, int from_tty)
c906108c
SS
1119{
1120 struct target_ops *t;
1121
c5aa993b 1122 t = find_default_run_target ("require_attach");
c906108c
SS
1123 (t->to_require_attach) (args, from_tty);
1124 return;
1125}
1126
1127void
fba45db2 1128find_default_require_detach (int pid, char *args, int from_tty)
c906108c
SS
1129{
1130 struct target_ops *t;
1131
c5aa993b 1132 t = find_default_run_target ("require_detach");
c906108c
SS
1133 (t->to_require_detach) (pid, args, from_tty);
1134 return;
1135}
1136
1137void
fba45db2 1138find_default_create_inferior (char *exec_file, char *allargs, char **env)
c906108c
SS
1139{
1140 struct target_ops *t;
1141
c5aa993b 1142 t = find_default_run_target ("run");
c906108c
SS
1143 (t->to_create_inferior) (exec_file, allargs, env);
1144 return;
1145}
1146
1147void
fba45db2 1148find_default_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
1149{
1150 struct target_ops *t;
1151
c5aa993b 1152 t = find_default_run_target ("run");
c906108c
SS
1153 (t->to_clone_and_follow_inferior) (child_pid, followed_child);
1154 return;
1155}
1156
1157static int
fba45db2 1158return_zero (void)
c906108c
SS
1159{
1160 return 0;
1161}
1162
1163static int
fba45db2 1164return_one (void)
c906108c
SS
1165{
1166 return 1;
1167}
1168
6426a772
JM
1169/*
1170 * Resize the to_sections pointer. Also make sure that anyone that
1171 * was holding on to an old value of it gets updated.
1172 * Returns the old size.
1173 */
1174
1175int
1176target_resize_to_sections (struct target_ops *target, int num_added)
1177{
1178 struct target_ops **t;
1179 struct section_table *old_value;
1180 int old_count;
1181
1182 old_value = target->to_sections;
1183
1184 if (target->to_sections)
1185 {
1186 old_count = target->to_sections_end - target->to_sections;
1187 target->to_sections = (struct section_table *)
1188 xrealloc ((char *) target->to_sections,
1189 (sizeof (struct section_table)) * (num_added + old_count));
1190 }
1191 else
1192 {
1193 old_count = 0;
1194 target->to_sections = (struct section_table *)
1195 xmalloc ((sizeof (struct section_table)) * num_added);
1196 }
1197 target->to_sections_end = target->to_sections + (num_added + old_count);
1198
1199 /* Check to see if anyone else was pointing to this structure.
1200 If old_value was null, then no one was. */
1201
1202 if (old_value)
1203 {
1204 for (t = target_structs; t < target_structs + target_struct_size;
1205 ++t)
1206 {
1207 if ((*t)->to_sections == old_value)
1208 {
1209 (*t)->to_sections = target->to_sections;
1210 (*t)->to_sections_end = target->to_sections_end;
1211 }
1212 }
1213 }
1214
1215 return old_count;
1216
1217}
1218
07cd4b97
JB
1219/* Remove all target sections taken from ABFD.
1220
1221 Scan the current target stack for targets whose section tables
1222 refer to sections from BFD, and remove those sections. We use this
1223 when we notice that the inferior has unloaded a shared object, for
1224 example. */
1225void
1226remove_target_sections (bfd *abfd)
1227{
1228 struct target_ops **t;
1229
1230 for (t = target_structs; t < target_structs + target_struct_size; t++)
1231 {
1232 struct section_table *src, *dest;
1233
1234 dest = (*t)->to_sections;
1235 for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
1236 if (src->bfd != abfd)
1237 {
1238 /* Keep this section. */
1239 if (dest < src) *dest = *src;
1240 dest++;
1241 }
1242
1243 /* If we've dropped any sections, resize the section table. */
1244 if (dest < src)
1245 target_resize_to_sections (*t, dest - src);
1246 }
1247}
1248
1249
1250
1251
7a292a7a
SS
1252/* Find a single runnable target in the stack and return it. If for
1253 some reason there is more than one, return NULL. */
1254
1255struct target_ops *
fba45db2 1256find_run_target (void)
7a292a7a
SS
1257{
1258 struct target_ops **t;
1259 struct target_ops *runable = NULL;
1260 int count;
c5aa993b 1261
7a292a7a 1262 count = 0;
c5aa993b 1263
7a292a7a
SS
1264 for (t = target_structs; t < target_structs + target_struct_size; ++t)
1265 {
c5aa993b 1266 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
1267 {
1268 runable = *t;
1269 ++count;
1270 }
1271 }
c5aa993b 1272
7a292a7a
SS
1273 return (count == 1 ? runable : NULL);
1274}
1275
ed9a39eb
JM
1276/* Find a single core_stratum target in the list of targets and return it.
1277 If for some reason there is more than one, return NULL. */
1278
c906108c 1279struct target_ops *
fba45db2 1280find_core_target (void)
c906108c
SS
1281{
1282 struct target_ops **t;
1283 struct target_ops *runable = NULL;
1284 int count;
c5aa993b 1285
c906108c 1286 count = 0;
c5aa993b 1287
c906108c
SS
1288 for (t = target_structs; t < target_structs + target_struct_size;
1289 ++t)
1290 {
1291 if ((*t)->to_stratum == core_stratum)
1292 {
1293 runable = *t;
1294 ++count;
1295 }
1296 }
c5aa993b
JM
1297
1298 return (count == 1 ? runable : NULL);
c906108c 1299}
ed9a39eb
JM
1300
1301/*
1302 * Find the next target down the stack from the specified target.
1303 */
1304
1305struct target_ops *
fba45db2 1306find_target_beneath (struct target_ops *t)
ed9a39eb
JM
1307{
1308 struct target_stack_item *cur;
1309
1310 for (cur = target_stack; cur; cur = cur->next)
1311 if (cur->target_ops == t)
1312 break;
1313
1314 if (cur == NULL || cur->next == NULL)
1315 return NULL;
1316 else
1317 return cur->next->target_ops;
1318}
1319
c906108c
SS
1320\f
1321/* The inferior process has died. Long live the inferior! */
1322
1323void
fba45db2 1324generic_mourn_inferior (void)
c906108c
SS
1325{
1326 extern int show_breakpoint_hit_counts;
1327
1328 inferior_pid = 0;
1329 attach_flag = 0;
1330 breakpoint_init_inferior (inf_exited);
1331 registers_changed ();
1332
1333#ifdef CLEAR_DEFERRED_STORES
1334 /* Delete any pending stores to the inferior... */
1335 CLEAR_DEFERRED_STORES;
1336#endif
1337
1338 reopen_exec_file ();
1339 reinit_frame_cache ();
1340
1341 /* It is confusing to the user for ignore counts to stick around
1342 from previous runs of the inferior. So clear them. */
1343 /* However, it is more confusing for the ignore counts to disappear when
1344 using hit counts. So don't clear them if we're counting hits. */
1345 if (!show_breakpoint_hit_counts)
1346 breakpoint_clear_ignore_counts ();
c5b739b5
FN
1347
1348 if (detach_hook)
1349 detach_hook ();
c906108c
SS
1350}
1351\f
1352/* This table must match in order and size the signals in enum target_signal
1353 in target.h. */
9846de1b 1354/* *INDENT-OFF* */
c906108c
SS
1355static struct {
1356 char *name;
1357 char *string;
1358 } signals [] =
1359{
1360 {"0", "Signal 0"},
1361 {"SIGHUP", "Hangup"},
1362 {"SIGINT", "Interrupt"},
1363 {"SIGQUIT", "Quit"},
1364 {"SIGILL", "Illegal instruction"},
1365 {"SIGTRAP", "Trace/breakpoint trap"},
1366 {"SIGABRT", "Aborted"},
1367 {"SIGEMT", "Emulation trap"},
1368 {"SIGFPE", "Arithmetic exception"},
1369 {"SIGKILL", "Killed"},
1370 {"SIGBUS", "Bus error"},
1371 {"SIGSEGV", "Segmentation fault"},
1372 {"SIGSYS", "Bad system call"},
1373 {"SIGPIPE", "Broken pipe"},
1374 {"SIGALRM", "Alarm clock"},
1375 {"SIGTERM", "Terminated"},
1376 {"SIGURG", "Urgent I/O condition"},
1377 {"SIGSTOP", "Stopped (signal)"},
1378 {"SIGTSTP", "Stopped (user)"},
1379 {"SIGCONT", "Continued"},
1380 {"SIGCHLD", "Child status changed"},
1381 {"SIGTTIN", "Stopped (tty input)"},
1382 {"SIGTTOU", "Stopped (tty output)"},
1383 {"SIGIO", "I/O possible"},
1384 {"SIGXCPU", "CPU time limit exceeded"},
1385 {"SIGXFSZ", "File size limit exceeded"},
1386 {"SIGVTALRM", "Virtual timer expired"},
1387 {"SIGPROF", "Profiling timer expired"},
1388 {"SIGWINCH", "Window size changed"},
1389 {"SIGLOST", "Resource lost"},
1390 {"SIGUSR1", "User defined signal 1"},
1391 {"SIGUSR2", "User defined signal 2"},
1392 {"SIGPWR", "Power fail/restart"},
1393 {"SIGPOLL", "Pollable event occurred"},
1394 {"SIGWIND", "SIGWIND"},
1395 {"SIGPHONE", "SIGPHONE"},
1396 {"SIGWAITING", "Process's LWPs are blocked"},
1397 {"SIGLWP", "Signal LWP"},
1398 {"SIGDANGER", "Swap space dangerously low"},
1399 {"SIGGRANT", "Monitor mode granted"},
1400 {"SIGRETRACT", "Need to relinquish monitor mode"},
1401 {"SIGMSG", "Monitor mode data available"},
1402 {"SIGSOUND", "Sound completed"},
1403 {"SIGSAK", "Secure attention"},
1404 {"SIGPRIO", "SIGPRIO"},
1405 {"SIG33", "Real-time event 33"},
1406 {"SIG34", "Real-time event 34"},
1407 {"SIG35", "Real-time event 35"},
1408 {"SIG36", "Real-time event 36"},
1409 {"SIG37", "Real-time event 37"},
1410 {"SIG38", "Real-time event 38"},
1411 {"SIG39", "Real-time event 39"},
1412 {"SIG40", "Real-time event 40"},
1413 {"SIG41", "Real-time event 41"},
1414 {"SIG42", "Real-time event 42"},
1415 {"SIG43", "Real-time event 43"},
1416 {"SIG44", "Real-time event 44"},
1417 {"SIG45", "Real-time event 45"},
1418 {"SIG46", "Real-time event 46"},
1419 {"SIG47", "Real-time event 47"},
1420 {"SIG48", "Real-time event 48"},
1421 {"SIG49", "Real-time event 49"},
1422 {"SIG50", "Real-time event 50"},
1423 {"SIG51", "Real-time event 51"},
1424 {"SIG52", "Real-time event 52"},
1425 {"SIG53", "Real-time event 53"},
1426 {"SIG54", "Real-time event 54"},
1427 {"SIG55", "Real-time event 55"},
1428 {"SIG56", "Real-time event 56"},
1429 {"SIG57", "Real-time event 57"},
1430 {"SIG58", "Real-time event 58"},
1431 {"SIG59", "Real-time event 59"},
1432 {"SIG60", "Real-time event 60"},
1433 {"SIG61", "Real-time event 61"},
1434 {"SIG62", "Real-time event 62"},
1435 {"SIG63", "Real-time event 63"},
cd0fc7c3 1436 {"SIGCANCEL", "LWP internal signal"},
d4f3574e 1437 {"SIG32", "Real-time event 32"},
c906108c
SS
1438
1439#if defined(MACH) || defined(__MACH__)
1440 /* Mach exceptions */
1441 {"EXC_BAD_ACCESS", "Could not access memory"},
1442 {"EXC_BAD_INSTRUCTION", "Illegal instruction/operand"},
1443 {"EXC_ARITHMETIC", "Arithmetic exception"},
1444 {"EXC_EMULATION", "Emulation instruction"},
1445 {"EXC_SOFTWARE", "Software generated exception"},
1446 {"EXC_BREAKPOINT", "Breakpoint"},
1447#endif
7a292a7a
SS
1448 {"SIGINFO", "Information request"},
1449
c906108c
SS
1450 {NULL, "Unknown signal"},
1451 {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
1452
1453 /* Last entry, used to check whether the table is the right size. */
1454 {NULL, "TARGET_SIGNAL_MAGIC"}
1455};
9846de1b 1456/* *INDENT-ON* */
c906108c 1457
c5aa993b
JM
1458
1459
c906108c
SS
1460/* Return the string for a signal. */
1461char *
fba45db2 1462target_signal_to_string (enum target_signal sig)
c906108c 1463{
7a292a7a
SS
1464 if ((sig >= TARGET_SIGNAL_FIRST) && (sig <= TARGET_SIGNAL_LAST))
1465 return signals[sig].string;
1466 else
1467 return signals[TARGET_SIGNAL_UNKNOWN].string;
c906108c
SS
1468}
1469
1470/* Return the name for a signal. */
1471char *
fba45db2 1472target_signal_to_name (enum target_signal sig)
c906108c
SS
1473{
1474 if (sig == TARGET_SIGNAL_UNKNOWN)
1475 /* I think the code which prints this will always print it along with
1476 the string, so no need to be verbose. */
1477 return "?";
1478 return signals[sig].name;
1479}
1480
1481/* Given a name, return its signal. */
1482enum target_signal
fba45db2 1483target_signal_from_name (char *name)
c906108c
SS
1484{
1485 enum target_signal sig;
1486
1487 /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
1488 for TARGET_SIGNAL_SIGCHLD. SIGIOT, on the other hand, is more
1489 questionable; seems like by now people should call it SIGABRT
1490 instead. */
1491
1492 /* This ugly cast brought to you by the native VAX compiler. */
1493 for (sig = TARGET_SIGNAL_HUP;
1494 signals[sig].name != NULL;
c5aa993b 1495 sig = (enum target_signal) ((int) sig + 1))
c906108c
SS
1496 if (STREQ (name, signals[sig].name))
1497 return sig;
1498 return TARGET_SIGNAL_UNKNOWN;
1499}
1500\f
1501/* The following functions are to help certain targets deal
1502 with the signal/waitstatus stuff. They could just as well be in
1503 a file called native-utils.c or unixwaitstatus-utils.c or whatever. */
1504
1505/* Convert host signal to our signals. */
1506enum target_signal
fba45db2 1507target_signal_from_host (int hostsig)
c906108c
SS
1508{
1509 /* A switch statement would make sense but would require special kludges
1510 to deal with the cases where more than one signal has the same number. */
1511
c5aa993b
JM
1512 if (hostsig == 0)
1513 return TARGET_SIGNAL_0;
c906108c
SS
1514
1515#if defined (SIGHUP)
c5aa993b
JM
1516 if (hostsig == SIGHUP)
1517 return TARGET_SIGNAL_HUP;
c906108c
SS
1518#endif
1519#if defined (SIGINT)
c5aa993b
JM
1520 if (hostsig == SIGINT)
1521 return TARGET_SIGNAL_INT;
c906108c
SS
1522#endif
1523#if defined (SIGQUIT)
c5aa993b
JM
1524 if (hostsig == SIGQUIT)
1525 return TARGET_SIGNAL_QUIT;
c906108c
SS
1526#endif
1527#if defined (SIGILL)
c5aa993b
JM
1528 if (hostsig == SIGILL)
1529 return TARGET_SIGNAL_ILL;
c906108c
SS
1530#endif
1531#if defined (SIGTRAP)
c5aa993b
JM
1532 if (hostsig == SIGTRAP)
1533 return TARGET_SIGNAL_TRAP;
c906108c
SS
1534#endif
1535#if defined (SIGABRT)
c5aa993b
JM
1536 if (hostsig == SIGABRT)
1537 return TARGET_SIGNAL_ABRT;
c906108c
SS
1538#endif
1539#if defined (SIGEMT)
c5aa993b
JM
1540 if (hostsig == SIGEMT)
1541 return TARGET_SIGNAL_EMT;
c906108c
SS
1542#endif
1543#if defined (SIGFPE)
c5aa993b
JM
1544 if (hostsig == SIGFPE)
1545 return TARGET_SIGNAL_FPE;
c906108c
SS
1546#endif
1547#if defined (SIGKILL)
c5aa993b
JM
1548 if (hostsig == SIGKILL)
1549 return TARGET_SIGNAL_KILL;
c906108c
SS
1550#endif
1551#if defined (SIGBUS)
c5aa993b
JM
1552 if (hostsig == SIGBUS)
1553 return TARGET_SIGNAL_BUS;
c906108c
SS
1554#endif
1555#if defined (SIGSEGV)
c5aa993b
JM
1556 if (hostsig == SIGSEGV)
1557 return TARGET_SIGNAL_SEGV;
c906108c
SS
1558#endif
1559#if defined (SIGSYS)
c5aa993b
JM
1560 if (hostsig == SIGSYS)
1561 return TARGET_SIGNAL_SYS;
c906108c
SS
1562#endif
1563#if defined (SIGPIPE)
c5aa993b
JM
1564 if (hostsig == SIGPIPE)
1565 return TARGET_SIGNAL_PIPE;
c906108c
SS
1566#endif
1567#if defined (SIGALRM)
c5aa993b
JM
1568 if (hostsig == SIGALRM)
1569 return TARGET_SIGNAL_ALRM;
c906108c
SS
1570#endif
1571#if defined (SIGTERM)
c5aa993b
JM
1572 if (hostsig == SIGTERM)
1573 return TARGET_SIGNAL_TERM;
c906108c
SS
1574#endif
1575#if defined (SIGUSR1)
c5aa993b
JM
1576 if (hostsig == SIGUSR1)
1577 return TARGET_SIGNAL_USR1;
c906108c
SS
1578#endif
1579#if defined (SIGUSR2)
c5aa993b
JM
1580 if (hostsig == SIGUSR2)
1581 return TARGET_SIGNAL_USR2;
c906108c
SS
1582#endif
1583#if defined (SIGCLD)
c5aa993b
JM
1584 if (hostsig == SIGCLD)
1585 return TARGET_SIGNAL_CHLD;
c906108c
SS
1586#endif
1587#if defined (SIGCHLD)
c5aa993b
JM
1588 if (hostsig == SIGCHLD)
1589 return TARGET_SIGNAL_CHLD;
c906108c
SS
1590#endif
1591#if defined (SIGPWR)
c5aa993b
JM
1592 if (hostsig == SIGPWR)
1593 return TARGET_SIGNAL_PWR;
c906108c
SS
1594#endif
1595#if defined (SIGWINCH)
c5aa993b
JM
1596 if (hostsig == SIGWINCH)
1597 return TARGET_SIGNAL_WINCH;
c906108c
SS
1598#endif
1599#if defined (SIGURG)
c5aa993b
JM
1600 if (hostsig == SIGURG)
1601 return TARGET_SIGNAL_URG;
c906108c
SS
1602#endif
1603#if defined (SIGIO)
c5aa993b
JM
1604 if (hostsig == SIGIO)
1605 return TARGET_SIGNAL_IO;
c906108c
SS
1606#endif
1607#if defined (SIGPOLL)
c5aa993b
JM
1608 if (hostsig == SIGPOLL)
1609 return TARGET_SIGNAL_POLL;
c906108c
SS
1610#endif
1611#if defined (SIGSTOP)
c5aa993b
JM
1612 if (hostsig == SIGSTOP)
1613 return TARGET_SIGNAL_STOP;
c906108c
SS
1614#endif
1615#if defined (SIGTSTP)
c5aa993b
JM
1616 if (hostsig == SIGTSTP)
1617 return TARGET_SIGNAL_TSTP;
c906108c
SS
1618#endif
1619#if defined (SIGCONT)
c5aa993b
JM
1620 if (hostsig == SIGCONT)
1621 return TARGET_SIGNAL_CONT;
c906108c
SS
1622#endif
1623#if defined (SIGTTIN)
c5aa993b
JM
1624 if (hostsig == SIGTTIN)
1625 return TARGET_SIGNAL_TTIN;
c906108c
SS
1626#endif
1627#if defined (SIGTTOU)
c5aa993b
JM
1628 if (hostsig == SIGTTOU)
1629 return TARGET_SIGNAL_TTOU;
c906108c
SS
1630#endif
1631#if defined (SIGVTALRM)
c5aa993b
JM
1632 if (hostsig == SIGVTALRM)
1633 return TARGET_SIGNAL_VTALRM;
c906108c
SS
1634#endif
1635#if defined (SIGPROF)
c5aa993b
JM
1636 if (hostsig == SIGPROF)
1637 return TARGET_SIGNAL_PROF;
c906108c
SS
1638#endif
1639#if defined (SIGXCPU)
c5aa993b
JM
1640 if (hostsig == SIGXCPU)
1641 return TARGET_SIGNAL_XCPU;
c906108c
SS
1642#endif
1643#if defined (SIGXFSZ)
c5aa993b
JM
1644 if (hostsig == SIGXFSZ)
1645 return TARGET_SIGNAL_XFSZ;
c906108c
SS
1646#endif
1647#if defined (SIGWIND)
c5aa993b
JM
1648 if (hostsig == SIGWIND)
1649 return TARGET_SIGNAL_WIND;
c906108c
SS
1650#endif
1651#if defined (SIGPHONE)
c5aa993b
JM
1652 if (hostsig == SIGPHONE)
1653 return TARGET_SIGNAL_PHONE;
c906108c
SS
1654#endif
1655#if defined (SIGLOST)
c5aa993b
JM
1656 if (hostsig == SIGLOST)
1657 return TARGET_SIGNAL_LOST;
c906108c
SS
1658#endif
1659#if defined (SIGWAITING)
c5aa993b
JM
1660 if (hostsig == SIGWAITING)
1661 return TARGET_SIGNAL_WAITING;
c906108c 1662#endif
cd0fc7c3 1663#if defined (SIGCANCEL)
c5aa993b
JM
1664 if (hostsig == SIGCANCEL)
1665 return TARGET_SIGNAL_CANCEL;
cd0fc7c3 1666#endif
c906108c 1667#if defined (SIGLWP)
c5aa993b
JM
1668 if (hostsig == SIGLWP)
1669 return TARGET_SIGNAL_LWP;
c906108c
SS
1670#endif
1671#if defined (SIGDANGER)
c5aa993b
JM
1672 if (hostsig == SIGDANGER)
1673 return TARGET_SIGNAL_DANGER;
c906108c
SS
1674#endif
1675#if defined (SIGGRANT)
c5aa993b
JM
1676 if (hostsig == SIGGRANT)
1677 return TARGET_SIGNAL_GRANT;
c906108c
SS
1678#endif
1679#if defined (SIGRETRACT)
c5aa993b
JM
1680 if (hostsig == SIGRETRACT)
1681 return TARGET_SIGNAL_RETRACT;
c906108c
SS
1682#endif
1683#if defined (SIGMSG)
c5aa993b
JM
1684 if (hostsig == SIGMSG)
1685 return TARGET_SIGNAL_MSG;
c906108c
SS
1686#endif
1687#if defined (SIGSOUND)
c5aa993b
JM
1688 if (hostsig == SIGSOUND)
1689 return TARGET_SIGNAL_SOUND;
c906108c
SS
1690#endif
1691#if defined (SIGSAK)
c5aa993b
JM
1692 if (hostsig == SIGSAK)
1693 return TARGET_SIGNAL_SAK;
c906108c
SS
1694#endif
1695#if defined (SIGPRIO)
c5aa993b
JM
1696 if (hostsig == SIGPRIO)
1697 return TARGET_SIGNAL_PRIO;
c906108c
SS
1698#endif
1699
1700 /* Mach exceptions. Assumes that the values for EXC_ are positive! */
1701#if defined (EXC_BAD_ACCESS) && defined (_NSIG)
c5aa993b
JM
1702 if (hostsig == _NSIG + EXC_BAD_ACCESS)
1703 return TARGET_EXC_BAD_ACCESS;
c906108c
SS
1704#endif
1705#if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
c5aa993b
JM
1706 if (hostsig == _NSIG + EXC_BAD_INSTRUCTION)
1707 return TARGET_EXC_BAD_INSTRUCTION;
c906108c
SS
1708#endif
1709#if defined (EXC_ARITHMETIC) && defined (_NSIG)
c5aa993b
JM
1710 if (hostsig == _NSIG + EXC_ARITHMETIC)
1711 return TARGET_EXC_ARITHMETIC;
c906108c
SS
1712#endif
1713#if defined (EXC_EMULATION) && defined (_NSIG)
c5aa993b
JM
1714 if (hostsig == _NSIG + EXC_EMULATION)
1715 return TARGET_EXC_EMULATION;
c906108c
SS
1716#endif
1717#if defined (EXC_SOFTWARE) && defined (_NSIG)
c5aa993b
JM
1718 if (hostsig == _NSIG + EXC_SOFTWARE)
1719 return TARGET_EXC_SOFTWARE;
c906108c
SS
1720#endif
1721#if defined (EXC_BREAKPOINT) && defined (_NSIG)
c5aa993b
JM
1722 if (hostsig == _NSIG + EXC_BREAKPOINT)
1723 return TARGET_EXC_BREAKPOINT;
c906108c
SS
1724#endif
1725
7a292a7a 1726#if defined (SIGINFO)
c5aa993b
JM
1727 if (hostsig == SIGINFO)
1728 return TARGET_SIGNAL_INFO;
7a292a7a
SS
1729#endif
1730
c906108c
SS
1731#if defined (REALTIME_LO)
1732 if (hostsig >= REALTIME_LO && hostsig < REALTIME_HI)
d4f3574e
SS
1733 {
1734 /* This block of TARGET_SIGNAL_REALTIME value is in order. */
1735 if (33 <= hostsig && hostsig <= 63)
1736 return (enum target_signal)
1737 (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
1738 else if (hostsig == 32)
1739 return TARGET_SIGNAL_REALTIME_32;
1740 else
1741 error ("GDB bug: target.c (target_signal_from_host): unrecognized real-time signal");
1742 }
c906108c 1743#endif
5ef22741
PS
1744
1745#if defined (SIGRTMIN)
1746 if (hostsig >= SIGRTMIN && hostsig <= SIGRTMAX)
1747 {
1748 /* This block of TARGET_SIGNAL_REALTIME value is in order. */
1749 if (33 <= hostsig && hostsig <= 63)
1750 return (enum target_signal)
1751 (hostsig - 33 + (int) TARGET_SIGNAL_REALTIME_33);
1752 else
1753 error ("GDB bug: target.c (target_signal_from_host): unrecognized real-time signal");
1754 }
1755#endif
c906108c
SS
1756 return TARGET_SIGNAL_UNKNOWN;
1757}
1758
c2d11a7d
JM
1759/* Convert a OURSIG (an enum target_signal) to the form used by the
1760 target operating system (refered to as the ``host'') or zero if the
1761 equivalent host signal is not available. Set/clear OURSIG_OK
1762 accordingly. */
1763
1764static int
1765do_target_signal_to_host (enum target_signal oursig,
1766 int *oursig_ok)
c906108c 1767{
c2d11a7d 1768 *oursig_ok = 1;
c906108c
SS
1769 switch (oursig)
1770 {
c5aa993b
JM
1771 case TARGET_SIGNAL_0:
1772 return 0;
c906108c
SS
1773
1774#if defined (SIGHUP)
c5aa993b
JM
1775 case TARGET_SIGNAL_HUP:
1776 return SIGHUP;
c906108c
SS
1777#endif
1778#if defined (SIGINT)
c5aa993b
JM
1779 case TARGET_SIGNAL_INT:
1780 return SIGINT;
c906108c
SS
1781#endif
1782#if defined (SIGQUIT)
c5aa993b
JM
1783 case TARGET_SIGNAL_QUIT:
1784 return SIGQUIT;
c906108c
SS
1785#endif
1786#if defined (SIGILL)
c5aa993b
JM
1787 case TARGET_SIGNAL_ILL:
1788 return SIGILL;
c906108c
SS
1789#endif
1790#if defined (SIGTRAP)
c5aa993b
JM
1791 case TARGET_SIGNAL_TRAP:
1792 return SIGTRAP;
c906108c
SS
1793#endif
1794#if defined (SIGABRT)
c5aa993b
JM
1795 case TARGET_SIGNAL_ABRT:
1796 return SIGABRT;
c906108c
SS
1797#endif
1798#if defined (SIGEMT)
c5aa993b
JM
1799 case TARGET_SIGNAL_EMT:
1800 return SIGEMT;
c906108c
SS
1801#endif
1802#if defined (SIGFPE)
c5aa993b
JM
1803 case TARGET_SIGNAL_FPE:
1804 return SIGFPE;
c906108c
SS
1805#endif
1806#if defined (SIGKILL)
c5aa993b
JM
1807 case TARGET_SIGNAL_KILL:
1808 return SIGKILL;
c906108c
SS
1809#endif
1810#if defined (SIGBUS)
c5aa993b
JM
1811 case TARGET_SIGNAL_BUS:
1812 return SIGBUS;
c906108c
SS
1813#endif
1814#if defined (SIGSEGV)
c5aa993b
JM
1815 case TARGET_SIGNAL_SEGV:
1816 return SIGSEGV;
c906108c
SS
1817#endif
1818#if defined (SIGSYS)
c5aa993b
JM
1819 case TARGET_SIGNAL_SYS:
1820 return SIGSYS;
c906108c
SS
1821#endif
1822#if defined (SIGPIPE)
c5aa993b
JM
1823 case TARGET_SIGNAL_PIPE:
1824 return SIGPIPE;
c906108c
SS
1825#endif
1826#if defined (SIGALRM)
c5aa993b
JM
1827 case TARGET_SIGNAL_ALRM:
1828 return SIGALRM;
c906108c
SS
1829#endif
1830#if defined (SIGTERM)
c5aa993b
JM
1831 case TARGET_SIGNAL_TERM:
1832 return SIGTERM;
c906108c
SS
1833#endif
1834#if defined (SIGUSR1)
c5aa993b
JM
1835 case TARGET_SIGNAL_USR1:
1836 return SIGUSR1;
c906108c
SS
1837#endif
1838#if defined (SIGUSR2)
c5aa993b
JM
1839 case TARGET_SIGNAL_USR2:
1840 return SIGUSR2;
c906108c
SS
1841#endif
1842#if defined (SIGCHLD) || defined (SIGCLD)
c5aa993b 1843 case TARGET_SIGNAL_CHLD:
c906108c
SS
1844#if defined (SIGCHLD)
1845 return SIGCHLD;
1846#else
1847 return SIGCLD;
1848#endif
1849#endif /* SIGCLD or SIGCHLD */
1850#if defined (SIGPWR)
c5aa993b
JM
1851 case TARGET_SIGNAL_PWR:
1852 return SIGPWR;
c906108c
SS
1853#endif
1854#if defined (SIGWINCH)
c5aa993b
JM
1855 case TARGET_SIGNAL_WINCH:
1856 return SIGWINCH;
c906108c
SS
1857#endif
1858#if defined (SIGURG)
c5aa993b
JM
1859 case TARGET_SIGNAL_URG:
1860 return SIGURG;
c906108c
SS
1861#endif
1862#if defined (SIGIO)
c5aa993b
JM
1863 case TARGET_SIGNAL_IO:
1864 return SIGIO;
c906108c
SS
1865#endif
1866#if defined (SIGPOLL)
c5aa993b
JM
1867 case TARGET_SIGNAL_POLL:
1868 return SIGPOLL;
c906108c
SS
1869#endif
1870#if defined (SIGSTOP)
c5aa993b
JM
1871 case TARGET_SIGNAL_STOP:
1872 return SIGSTOP;
c906108c
SS
1873#endif
1874#if defined (SIGTSTP)
c5aa993b
JM
1875 case TARGET_SIGNAL_TSTP:
1876 return SIGTSTP;
c906108c
SS
1877#endif
1878#if defined (SIGCONT)
c5aa993b
JM
1879 case TARGET_SIGNAL_CONT:
1880 return SIGCONT;
c906108c
SS
1881#endif
1882#if defined (SIGTTIN)
c5aa993b
JM
1883 case TARGET_SIGNAL_TTIN:
1884 return SIGTTIN;
c906108c
SS
1885#endif
1886#if defined (SIGTTOU)
c5aa993b
JM
1887 case TARGET_SIGNAL_TTOU:
1888 return SIGTTOU;
c906108c
SS
1889#endif
1890#if defined (SIGVTALRM)
c5aa993b
JM
1891 case TARGET_SIGNAL_VTALRM:
1892 return SIGVTALRM;
c906108c
SS
1893#endif
1894#if defined (SIGPROF)
c5aa993b
JM
1895 case TARGET_SIGNAL_PROF:
1896 return SIGPROF;
c906108c
SS
1897#endif
1898#if defined (SIGXCPU)
c5aa993b
JM
1899 case TARGET_SIGNAL_XCPU:
1900 return SIGXCPU;
c906108c
SS
1901#endif
1902#if defined (SIGXFSZ)
c5aa993b
JM
1903 case TARGET_SIGNAL_XFSZ:
1904 return SIGXFSZ;
c906108c
SS
1905#endif
1906#if defined (SIGWIND)
c5aa993b
JM
1907 case TARGET_SIGNAL_WIND:
1908 return SIGWIND;
c906108c
SS
1909#endif
1910#if defined (SIGPHONE)
c5aa993b
JM
1911 case TARGET_SIGNAL_PHONE:
1912 return SIGPHONE;
c906108c
SS
1913#endif
1914#if defined (SIGLOST)
c5aa993b
JM
1915 case TARGET_SIGNAL_LOST:
1916 return SIGLOST;
c906108c
SS
1917#endif
1918#if defined (SIGWAITING)
c5aa993b
JM
1919 case TARGET_SIGNAL_WAITING:
1920 return SIGWAITING;
c906108c 1921#endif
cd0fc7c3 1922#if defined (SIGCANCEL)
c5aa993b
JM
1923 case TARGET_SIGNAL_CANCEL:
1924 return SIGCANCEL;
cd0fc7c3 1925#endif
c906108c 1926#if defined (SIGLWP)
c5aa993b
JM
1927 case TARGET_SIGNAL_LWP:
1928 return SIGLWP;
c906108c
SS
1929#endif
1930#if defined (SIGDANGER)
c5aa993b
JM
1931 case TARGET_SIGNAL_DANGER:
1932 return SIGDANGER;
c906108c
SS
1933#endif
1934#if defined (SIGGRANT)
c5aa993b
JM
1935 case TARGET_SIGNAL_GRANT:
1936 return SIGGRANT;
c906108c
SS
1937#endif
1938#if defined (SIGRETRACT)
c5aa993b
JM
1939 case TARGET_SIGNAL_RETRACT:
1940 return SIGRETRACT;
c906108c
SS
1941#endif
1942#if defined (SIGMSG)
c5aa993b
JM
1943 case TARGET_SIGNAL_MSG:
1944 return SIGMSG;
c906108c
SS
1945#endif
1946#if defined (SIGSOUND)
c5aa993b
JM
1947 case TARGET_SIGNAL_SOUND:
1948 return SIGSOUND;
c906108c
SS
1949#endif
1950#if defined (SIGSAK)
c5aa993b
JM
1951 case TARGET_SIGNAL_SAK:
1952 return SIGSAK;
c906108c
SS
1953#endif
1954#if defined (SIGPRIO)
c5aa993b
JM
1955 case TARGET_SIGNAL_PRIO:
1956 return SIGPRIO;
c906108c
SS
1957#endif
1958
1959 /* Mach exceptions. Assumes that the values for EXC_ are positive! */
1960#if defined (EXC_BAD_ACCESS) && defined (_NSIG)
c5aa993b
JM
1961 case TARGET_EXC_BAD_ACCESS:
1962 return _NSIG + EXC_BAD_ACCESS;
c906108c
SS
1963#endif
1964#if defined (EXC_BAD_INSTRUCTION) && defined (_NSIG)
c5aa993b
JM
1965 case TARGET_EXC_BAD_INSTRUCTION:
1966 return _NSIG + EXC_BAD_INSTRUCTION;
c906108c
SS
1967#endif
1968#if defined (EXC_ARITHMETIC) && defined (_NSIG)
c5aa993b
JM
1969 case TARGET_EXC_ARITHMETIC:
1970 return _NSIG + EXC_ARITHMETIC;
c906108c
SS
1971#endif
1972#if defined (EXC_EMULATION) && defined (_NSIG)
c5aa993b
JM
1973 case TARGET_EXC_EMULATION:
1974 return _NSIG + EXC_EMULATION;
c906108c
SS
1975#endif
1976#if defined (EXC_SOFTWARE) && defined (_NSIG)
c5aa993b
JM
1977 case TARGET_EXC_SOFTWARE:
1978 return _NSIG + EXC_SOFTWARE;
c906108c
SS
1979#endif
1980#if defined (EXC_BREAKPOINT) && defined (_NSIG)
c5aa993b
JM
1981 case TARGET_EXC_BREAKPOINT:
1982 return _NSIG + EXC_BREAKPOINT;
c906108c
SS
1983#endif
1984
7a292a7a 1985#if defined (SIGINFO)
c5aa993b
JM
1986 case TARGET_SIGNAL_INFO:
1987 return SIGINFO;
7a292a7a
SS
1988#endif
1989
c906108c
SS
1990 default:
1991#if defined (REALTIME_LO)
1992 if (oursig >= TARGET_SIGNAL_REALTIME_33
1993 && oursig <= TARGET_SIGNAL_REALTIME_63)
1994 {
e95a43ad
MK
1995 /* This block of signals is continuous, and
1996 TARGET_SIGNAL_REALTIME_33 is 33 by definition. */
c906108c 1997 int retsig =
e95a43ad
MK
1998 (int) oursig - (int) TARGET_SIGNAL_REALTIME_33 + 33;
1999 if (retsig >= REALTIME_LO && retsig < REALTIME_HI)
c906108c
SS
2000 return retsig;
2001 }
e95a43ad
MK
2002#if (REALTIME_LO < 33)
2003 else if (oursig == TARGET_SIGNAL_REALTIME_32)
2004 {
2005 /* TARGET_SIGNAL_REALTIME_32 isn't contiguous with
2006 TARGET_SIGNAL_REALTIME_33. It is 32 by definition. */
2007 return 32;
2008 }
2009#endif
5ef22741
PS
2010#endif
2011
2012#if defined (SIGRTMIN)
2013 if (oursig >= TARGET_SIGNAL_REALTIME_33
2014 && oursig <= TARGET_SIGNAL_REALTIME_63)
2015 {
2016 /* This block of signals is continuous, and
2017 TARGET_SIGNAL_REALTIME_33 is 33 by definition. */
2018 int retsig =
2019 (int) oursig - (int) TARGET_SIGNAL_REALTIME_33 + 33;
2020 if (retsig >= SIGRTMIN && retsig <= SIGRTMAX)
2021 return retsig;
2022 }
c906108c 2023#endif
c2d11a7d
JM
2024 *oursig_ok = 0;
2025 return 0;
2026 }
2027}
2028
2029int
2030target_signal_to_host_p (enum target_signal oursig)
2031{
2032 int oursig_ok;
2033 do_target_signal_to_host (oursig, &oursig_ok);
2034 return oursig_ok;
2035}
2036
2037int
2038target_signal_to_host (enum target_signal oursig)
2039{
2040 int oursig_ok;
2041 int targ_signo = do_target_signal_to_host (oursig, &oursig_ok);
2042 if (!oursig_ok)
2043 {
c906108c 2044 /* The user might be trying to do "signal SIGSAK" where this system
c5aa993b 2045 doesn't have SIGSAK. */
c906108c
SS
2046 warning ("Signal %s does not exist on this system.\n",
2047 target_signal_to_name (oursig));
2048 return 0;
2049 }
c2d11a7d
JM
2050 else
2051 return targ_signo;
c906108c
SS
2052}
2053
2054/* Helper function for child_wait and the Lynx derivatives of child_wait.
2055 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2056 translation of that in OURSTATUS. */
2057void
fba45db2 2058store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c
SS
2059{
2060#ifdef CHILD_SPECIAL_WAITSTATUS
2061 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
2062 if it wants to deal with hoststatus. */
2063 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
2064 return;
2065#endif
2066
2067 if (WIFEXITED (hoststatus))
2068 {
2069 ourstatus->kind = TARGET_WAITKIND_EXITED;
2070 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2071 }
2072 else if (!WIFSTOPPED (hoststatus))
2073 {
2074 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2075 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2076 }
2077 else
2078 {
2079 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2080 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2081 }
2082}
2083\f
2084/* In some circumstances we allow a command to specify a numeric
2085 signal. The idea is to keep these circumstances limited so that
2086 users (and scripts) develop portable habits. For comparison,
2087 POSIX.2 `kill' requires that 1,2,3,6,9,14, and 15 work (and using a
7e73cedf 2088 numeric signal at all is obsolescent. We are slightly more
c906108c
SS
2089 lenient and allow 1-15 which should match host signal numbers on
2090 most systems. Use of symbolic signal names is strongly encouraged. */
2091
2092enum target_signal
fba45db2 2093target_signal_from_command (int num)
c906108c
SS
2094{
2095 if (num >= 1 && num <= 15)
c5aa993b 2096 return (enum target_signal) num;
c906108c
SS
2097 error ("Only signals 1-15 are valid as numeric signals.\n\
2098Use \"info signals\" for a list of symbolic signals.");
2099}
2100\f
2101/* Returns zero to leave the inferior alone, one to interrupt it. */
507f3c78 2102int (*target_activity_function) (void);
c906108c
SS
2103int target_activity_fd;
2104\f
2105/* Convert a normal process ID to a string. Returns the string in a static
2106 buffer. */
2107
2108char *
fba45db2 2109normal_pid_to_str (int pid)
c906108c
SS
2110{
2111 static char buf[30];
2112
2113 if (STREQ (current_target.to_shortname, "remote"))
104c1213 2114 sprintf (buf, "thread %d", pid);
c906108c 2115 else
104c1213 2116 sprintf (buf, "process %d", pid);
c906108c
SS
2117
2118 return buf;
2119}
2120
2121/* Some targets (such as ttrace-based HPUX) don't allow us to request
2122 notification of inferior events such as fork and vork immediately
2123 after the inferior is created. (This because of how gdb gets an
2124 inferior created via invoking a shell to do it. In such a scenario,
2125 if the shell init file has commands in it, the shell will fork and
2126 exec for each of those commands, and we will see each such fork
2127 event. Very bad.)
c5aa993b 2128
c906108c
SS
2129 This function is used by all targets that allow us to request
2130 notification of forks, etc at inferior creation time; e.g., in
2131 target_acknowledge_forked_child.
c5aa993b 2132 */
392a587b 2133static void
fba45db2 2134normal_target_post_startup_inferior (int pid)
c906108c
SS
2135{
2136 /* This space intentionally left blank. */
2137}
2138
2139/* Set up the handful of non-empty slots needed by the dummy target
2140 vector. */
2141
2142static void
fba45db2 2143init_dummy_target (void)
c906108c
SS
2144{
2145 dummy_target.to_shortname = "None";
2146 dummy_target.to_longname = "None";
2147 dummy_target.to_doc = "";
2148 dummy_target.to_attach = find_default_attach;
2149 dummy_target.to_require_attach = find_default_require_attach;
2150 dummy_target.to_require_detach = find_default_require_detach;
2151 dummy_target.to_create_inferior = find_default_create_inferior;
2152 dummy_target.to_clone_and_follow_inferior = find_default_clone_and_follow_inferior;
ed9a39eb 2153 dummy_target.to_pid_to_str = normal_pid_to_str;
c906108c
SS
2154 dummy_target.to_stratum = dummy_stratum;
2155 dummy_target.to_magic = OPS_MAGIC;
2156}
c906108c 2157\f
c5aa993b 2158
c906108c
SS
2159static struct target_ops debug_target;
2160
2161static void
fba45db2 2162debug_to_open (char *args, int from_tty)
c906108c
SS
2163{
2164 debug_target.to_open (args, from_tty);
2165
96baa820 2166 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
2167}
2168
2169static void
fba45db2 2170debug_to_close (int quitting)
c906108c
SS
2171{
2172 debug_target.to_close (quitting);
2173
96baa820 2174 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
c906108c
SS
2175}
2176
2177static void
fba45db2 2178debug_to_attach (char *args, int from_tty)
c906108c
SS
2179{
2180 debug_target.to_attach (args, from_tty);
2181
96baa820 2182 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n", args, from_tty);
c906108c
SS
2183}
2184
2185
2186static void
fba45db2 2187debug_to_post_attach (int pid)
c906108c
SS
2188{
2189 debug_target.to_post_attach (pid);
2190
96baa820 2191 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
2192}
2193
2194static void
fba45db2 2195debug_to_require_attach (char *args, int from_tty)
c906108c
SS
2196{
2197 debug_target.to_require_attach (args, from_tty);
2198
96baa820 2199 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2200 "target_require_attach (%s, %d)\n", args, from_tty);
2201}
2202
2203static void
fba45db2 2204debug_to_detach (char *args, int from_tty)
c906108c
SS
2205{
2206 debug_target.to_detach (args, from_tty);
2207
96baa820 2208 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n", args, from_tty);
c906108c
SS
2209}
2210
2211static void
fba45db2 2212debug_to_require_detach (int pid, char *args, int from_tty)
c906108c
SS
2213{
2214 debug_target.to_require_detach (pid, args, from_tty);
2215
96baa820 2216 fprintf_unfiltered (gdb_stdlog,
c5aa993b 2217 "target_require_detach (%d, %s, %d)\n", pid, args, from_tty);
c906108c
SS
2218}
2219
2220static void
fba45db2 2221debug_to_resume (int pid, int step, enum target_signal siggnal)
c906108c
SS
2222{
2223 debug_target.to_resume (pid, step, siggnal);
2224
96baa820 2225 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", pid,
c906108c
SS
2226 step ? "step" : "continue",
2227 target_signal_to_name (siggnal));
2228}
2229
2230static int
fba45db2 2231debug_to_wait (int pid, struct target_waitstatus *status)
c906108c
SS
2232{
2233 int retval;
2234
2235 retval = debug_target.to_wait (pid, status);
2236
96baa820 2237 fprintf_unfiltered (gdb_stdlog,
c906108c 2238 "target_wait (%d, status) = %d, ", pid, retval);
96baa820 2239 fprintf_unfiltered (gdb_stdlog, "status->kind = ");
c906108c
SS
2240 switch (status->kind)
2241 {
2242 case TARGET_WAITKIND_EXITED:
96baa820 2243 fprintf_unfiltered (gdb_stdlog, "exited, status = %d\n",
c906108c
SS
2244 status->value.integer);
2245 break;
2246 case TARGET_WAITKIND_STOPPED:
96baa820 2247 fprintf_unfiltered (gdb_stdlog, "stopped, signal = %s\n",
c906108c
SS
2248 target_signal_to_name (status->value.sig));
2249 break;
2250 case TARGET_WAITKIND_SIGNALLED:
96baa820 2251 fprintf_unfiltered (gdb_stdlog, "signalled, signal = %s\n",
c906108c
SS
2252 target_signal_to_name (status->value.sig));
2253 break;
2254 case TARGET_WAITKIND_LOADED:
96baa820 2255 fprintf_unfiltered (gdb_stdlog, "loaded\n");
c906108c
SS
2256 break;
2257 case TARGET_WAITKIND_FORKED:
96baa820 2258 fprintf_unfiltered (gdb_stdlog, "forked\n");
c906108c
SS
2259 break;
2260 case TARGET_WAITKIND_VFORKED:
96baa820 2261 fprintf_unfiltered (gdb_stdlog, "vforked\n");
c906108c
SS
2262 break;
2263 case TARGET_WAITKIND_EXECD:
96baa820 2264 fprintf_unfiltered (gdb_stdlog, "execd\n");
c906108c
SS
2265 break;
2266 case TARGET_WAITKIND_SPURIOUS:
96baa820 2267 fprintf_unfiltered (gdb_stdlog, "spurious\n");
c906108c
SS
2268 break;
2269 default:
96baa820 2270 fprintf_unfiltered (gdb_stdlog, "unknown???\n");
c906108c
SS
2271 break;
2272 }
2273
2274 return retval;
2275}
2276
2277static void
fba45db2 2278debug_to_post_wait (int pid, int status)
c906108c
SS
2279{
2280 debug_target.to_post_wait (pid, status);
2281
96baa820 2282 fprintf_unfiltered (gdb_stdlog, "target_post_wait (%d, %d)\n",
c906108c
SS
2283 pid, status);
2284}
2285
2286static void
fba45db2 2287debug_to_fetch_registers (int regno)
c906108c
SS
2288{
2289 debug_target.to_fetch_registers (regno);
2290
96baa820 2291 fprintf_unfiltered (gdb_stdlog, "target_fetch_registers (%s)",
c906108c
SS
2292 regno != -1 ? REGISTER_NAME (regno) : "-1");
2293 if (regno != -1)
104c1213 2294 fprintf_unfiltered (gdb_stdlog, " = 0x%lx %ld",
c906108c 2295 (unsigned long) read_register (regno),
104c1213 2296 (unsigned long) read_register (regno));
96baa820 2297 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
2298}
2299
2300static void
fba45db2 2301debug_to_store_registers (int regno)
c906108c
SS
2302{
2303 debug_target.to_store_registers (regno);
2304
2305 if (regno >= 0 && regno < NUM_REGS)
104c1213 2306 fprintf_unfiltered (gdb_stdlog, "target_store_registers (%s) = 0x%lx %ld\n",
c906108c
SS
2307 REGISTER_NAME (regno),
2308 (unsigned long) read_register (regno),
2309 (unsigned long) read_register (regno));
2310 else
96baa820 2311 fprintf_unfiltered (gdb_stdlog, "target_store_registers (%d)\n", regno);
c906108c
SS
2312}
2313
2314static void
fba45db2 2315debug_to_prepare_to_store (void)
c906108c
SS
2316{
2317 debug_target.to_prepare_to_store ();
2318
96baa820 2319 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
2320}
2321
2322static int
fba45db2
KB
2323debug_to_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
2324 struct target_ops *target)
c906108c
SS
2325{
2326 int retval;
2327
2328 retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write, target);
2329
96baa820 2330 fprintf_unfiltered (gdb_stdlog,
c906108c 2331 "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
c5aa993b 2332 (unsigned int) memaddr, /* possable truncate long long */
c906108c
SS
2333 len, write ? "write" : "read", retval);
2334
c5aa993b 2335
c906108c
SS
2336
2337 if (retval > 0)
2338 {
2339 int i;
2340
96baa820 2341 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
2342 for (i = 0; i < retval; i++)
2343 {
2344 if ((((long) &(myaddr[i])) & 0xf) == 0)
96baa820
JM
2345 fprintf_unfiltered (gdb_stdlog, "\n");
2346 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
2347 }
2348 }
2349
96baa820 2350 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
2351
2352 return retval;
2353}
2354
2355static void
fba45db2 2356debug_to_files_info (struct target_ops *target)
c906108c
SS
2357{
2358 debug_target.to_files_info (target);
2359
96baa820 2360 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
2361}
2362
2363static int
fba45db2 2364debug_to_insert_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
2365{
2366 int retval;
2367
2368 retval = debug_target.to_insert_breakpoint (addr, save);
2369
96baa820 2370 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
2371 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2372 (unsigned long) addr,
2373 (unsigned long) retval);
c906108c
SS
2374 return retval;
2375}
2376
2377static int
fba45db2 2378debug_to_remove_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
2379{
2380 int retval;
2381
2382 retval = debug_target.to_remove_breakpoint (addr, save);
2383
96baa820 2384 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
2385 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2386 (unsigned long) addr,
2387 (unsigned long) retval);
c906108c
SS
2388 return retval;
2389}
2390
2391static void
fba45db2 2392debug_to_terminal_init (void)
c906108c
SS
2393{
2394 debug_target.to_terminal_init ();
2395
96baa820 2396 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
2397}
2398
2399static void
fba45db2 2400debug_to_terminal_inferior (void)
c906108c
SS
2401{
2402 debug_target.to_terminal_inferior ();
2403
96baa820 2404 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
2405}
2406
2407static void
fba45db2 2408debug_to_terminal_ours_for_output (void)
c906108c
SS
2409{
2410 debug_target.to_terminal_ours_for_output ();
2411
96baa820 2412 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
2413}
2414
2415static void
fba45db2 2416debug_to_terminal_ours (void)
c906108c
SS
2417{
2418 debug_target.to_terminal_ours ();
2419
96baa820 2420 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
2421}
2422
2423static void
fba45db2 2424debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
2425{
2426 debug_target.to_terminal_info (arg, from_tty);
2427
96baa820 2428 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
2429 from_tty);
2430}
2431
2432static void
fba45db2 2433debug_to_kill (void)
c906108c
SS
2434{
2435 debug_target.to_kill ();
2436
96baa820 2437 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
c906108c
SS
2438}
2439
2440static void
fba45db2 2441debug_to_load (char *args, int from_tty)
c906108c
SS
2442{
2443 debug_target.to_load (args, from_tty);
2444
96baa820 2445 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
2446}
2447
2448static int
fba45db2 2449debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
2450{
2451 int retval;
2452
2453 retval = debug_target.to_lookup_symbol (name, addrp);
2454
96baa820 2455 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
2456
2457 return retval;
2458}
2459
2460static void
fba45db2 2461debug_to_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
2462{
2463 debug_target.to_create_inferior (exec_file, args, env);
2464
96baa820 2465 fprintf_unfiltered (gdb_stdlog, "target_create_inferior (%s, %s, xxx)\n",
c906108c
SS
2466 exec_file, args);
2467}
2468
2469static void
fba45db2 2470debug_to_post_startup_inferior (int pid)
c906108c
SS
2471{
2472 debug_target.to_post_startup_inferior (pid);
2473
96baa820 2474 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
c906108c
SS
2475 pid);
2476}
2477
2478static void
fba45db2 2479debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
2480{
2481 debug_target.to_acknowledge_created_inferior (pid);
2482
96baa820 2483 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
2484 pid);
2485}
2486
2487static void
fba45db2 2488debug_to_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
2489{
2490 debug_target.to_clone_and_follow_inferior (child_pid, followed_child);
2491
96baa820 2492 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2493 "target_clone_and_follow_inferior (%d, %d)\n",
2494 child_pid, *followed_child);
2495}
2496
2497static void
fba45db2 2498debug_to_post_follow_inferior_by_clone (void)
c906108c
SS
2499{
2500 debug_target.to_post_follow_inferior_by_clone ();
2501
96baa820 2502 fprintf_unfiltered (gdb_stdlog, "target_post_follow_inferior_by_clone ()\n");
c906108c
SS
2503}
2504
2505static int
fba45db2 2506debug_to_insert_fork_catchpoint (int pid)
c906108c 2507{
c5aa993b 2508 int retval;
c906108c
SS
2509
2510 retval = debug_target.to_insert_fork_catchpoint (pid);
2511
96baa820 2512 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
c5aa993b 2513 pid, retval);
c906108c
SS
2514
2515 return retval;
2516}
2517
2518static int
fba45db2 2519debug_to_remove_fork_catchpoint (int pid)
c906108c 2520{
c5aa993b 2521 int retval;
c906108c
SS
2522
2523 retval = debug_target.to_remove_fork_catchpoint (pid);
2524
96baa820 2525 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 2526 pid, retval);
c906108c
SS
2527
2528 return retval;
2529}
2530
2531static int
fba45db2 2532debug_to_insert_vfork_catchpoint (int pid)
c906108c 2533{
c5aa993b 2534 int retval;
c906108c
SS
2535
2536 retval = debug_target.to_insert_vfork_catchpoint (pid);
2537
96baa820 2538 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)= %d\n",
c5aa993b 2539 pid, retval);
c906108c
SS
2540
2541 return retval;
2542}
2543
2544static int
fba45db2 2545debug_to_remove_vfork_catchpoint (int pid)
c906108c 2546{
c5aa993b 2547 int retval;
c906108c
SS
2548
2549 retval = debug_target.to_remove_vfork_catchpoint (pid);
2550
96baa820 2551 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 2552 pid, retval);
c906108c
SS
2553
2554 return retval;
2555}
2556
2557static int
fba45db2 2558debug_to_has_forked (int pid, int *child_pid)
c906108c 2559{
c5aa993b 2560 int has_forked;
c906108c
SS
2561
2562 has_forked = debug_target.to_has_forked (pid, child_pid);
2563
96baa820 2564 fprintf_unfiltered (gdb_stdlog, "target_has_forked (%d, %d) = %d\n",
c5aa993b 2565 pid, *child_pid, has_forked);
c906108c
SS
2566
2567 return has_forked;
2568}
2569
2570static int
fba45db2 2571debug_to_has_vforked (int pid, int *child_pid)
c906108c 2572{
c5aa993b 2573 int has_vforked;
c906108c
SS
2574
2575 has_vforked = debug_target.to_has_vforked (pid, child_pid);
2576
96baa820 2577 fprintf_unfiltered (gdb_stdlog, "target_has_vforked (%d, %d) = %d\n",
c5aa993b 2578 pid, *child_pid, has_vforked);
c906108c
SS
2579
2580 return has_vforked;
2581}
2582
2583static int
fba45db2 2584debug_to_can_follow_vfork_prior_to_exec (void)
c906108c 2585{
c5aa993b 2586 int can_immediately_follow_vfork;
c906108c
SS
2587
2588 can_immediately_follow_vfork = debug_target.to_can_follow_vfork_prior_to_exec ();
2589
96baa820 2590 fprintf_unfiltered (gdb_stdlog, "target_can_follow_vfork_prior_to_exec () = %d\n",
c5aa993b 2591 can_immediately_follow_vfork);
c906108c
SS
2592
2593 return can_immediately_follow_vfork;
2594}
2595
2596static void
fba45db2
KB
2597debug_to_post_follow_vfork (int parent_pid, int followed_parent, int child_pid,
2598 int followed_child)
c906108c
SS
2599{
2600 debug_target.to_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child);
2601
96baa820 2602 fprintf_unfiltered (gdb_stdlog,
c906108c 2603 "target_post_follow_vfork (%d, %d, %d, %d)\n",
c5aa993b 2604 parent_pid, followed_parent, child_pid, followed_child);
c906108c
SS
2605}
2606
2607static int
fba45db2 2608debug_to_insert_exec_catchpoint (int pid)
c906108c 2609{
c5aa993b 2610 int retval;
c906108c
SS
2611
2612 retval = debug_target.to_insert_exec_catchpoint (pid);
2613
96baa820 2614 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
c5aa993b 2615 pid, retval);
c906108c
SS
2616
2617 return retval;
2618}
2619
2620static int
fba45db2 2621debug_to_remove_exec_catchpoint (int pid)
c906108c 2622{
c5aa993b 2623 int retval;
c906108c
SS
2624
2625 retval = debug_target.to_remove_exec_catchpoint (pid);
2626
96baa820 2627 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 2628 pid, retval);
c906108c
SS
2629
2630 return retval;
2631}
2632
2633static int
fba45db2 2634debug_to_has_execd (int pid, char **execd_pathname)
c906108c 2635{
c5aa993b 2636 int has_execd;
c906108c
SS
2637
2638 has_execd = debug_target.to_has_execd (pid, execd_pathname);
2639
96baa820 2640 fprintf_unfiltered (gdb_stdlog, "target_has_execd (%d, %s) = %d\n",
c5aa993b 2641 pid, (*execd_pathname ? *execd_pathname : "<NULL>"),
7a292a7a 2642 has_execd);
c906108c
SS
2643
2644 return has_execd;
2645}
2646
2647static int
fba45db2 2648debug_to_reported_exec_events_per_exec_call (void)
c906108c 2649{
c5aa993b 2650 int reported_exec_events;
c906108c
SS
2651
2652 reported_exec_events = debug_target.to_reported_exec_events_per_exec_call ();
2653
96baa820 2654 fprintf_unfiltered (gdb_stdlog,
c906108c 2655 "target_reported_exec_events_per_exec_call () = %d\n",
c5aa993b 2656 reported_exec_events);
c906108c
SS
2657
2658 return reported_exec_events;
2659}
2660
2661static int
fba45db2
KB
2662debug_to_has_syscall_event (int pid, enum target_waitkind *kind,
2663 int *syscall_id)
c906108c 2664{
c5aa993b
JM
2665 int has_syscall_event;
2666 char *kind_spelling = "??";
c906108c
SS
2667
2668 has_syscall_event = debug_target.to_has_syscall_event (pid, kind, syscall_id);
2669 if (has_syscall_event)
2670 {
2671 switch (*kind)
c5aa993b
JM
2672 {
2673 case TARGET_WAITKIND_SYSCALL_ENTRY:
2674 kind_spelling = "SYSCALL_ENTRY";
2675 break;
2676 case TARGET_WAITKIND_SYSCALL_RETURN:
2677 kind_spelling = "SYSCALL_RETURN";
2678 break;
2679 default:
2680 break;
2681 }
c906108c
SS
2682 }
2683
96baa820 2684 fprintf_unfiltered (gdb_stdlog,
c906108c 2685 "target_has_syscall_event (%d, %s, %d) = %d\n",
c5aa993b 2686 pid, kind_spelling, *syscall_id, has_syscall_event);
c906108c
SS
2687
2688 return has_syscall_event;
2689}
2690
2691static int
fba45db2 2692debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 2693{
c5aa993b 2694 int has_exited;
c906108c
SS
2695
2696 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
2697
96baa820 2698 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 2699 pid, wait_status, *exit_status, has_exited);
c906108c
SS
2700
2701 return has_exited;
2702}
2703
2704static void
fba45db2 2705debug_to_mourn_inferior (void)
c906108c
SS
2706{
2707 debug_target.to_mourn_inferior ();
2708
96baa820 2709 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
c906108c
SS
2710}
2711
2712static int
fba45db2 2713debug_to_can_run (void)
c906108c
SS
2714{
2715 int retval;
2716
2717 retval = debug_target.to_can_run ();
2718
96baa820 2719 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
2720
2721 return retval;
2722}
2723
2724static void
fba45db2 2725debug_to_notice_signals (int pid)
c906108c
SS
2726{
2727 debug_target.to_notice_signals (pid);
2728
96baa820 2729 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n", pid);
c906108c
SS
2730}
2731
2732static int
fba45db2 2733debug_to_thread_alive (int pid)
c906108c
SS
2734{
2735 int retval;
2736
2737 retval = debug_target.to_thread_alive (pid);
2738
96baa820 2739 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
c906108c
SS
2740 pid, retval);
2741
2742 return retval;
2743}
2744
0d06e24b 2745static void
fba45db2 2746debug_to_find_new_threads (void)
0d06e24b
JM
2747{
2748 debug_target.to_find_new_threads ();
2749
2750 fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
2751}
2752
c906108c 2753static void
fba45db2 2754debug_to_stop (void)
c906108c
SS
2755{
2756 debug_target.to_stop ();
2757
96baa820 2758 fprintf_unfiltered (gdb_stdlog, "target_stop ()\n");
c906108c
SS
2759}
2760
2761static int
fba45db2 2762debug_to_query (int type, char *req, char *resp, int *siz)
c906108c
SS
2763{
2764 int retval;
2765
2766 retval = debug_target.to_query (type, req, resp, siz);
2767
96baa820 2768 fprintf_unfiltered (gdb_stdlog, "target_query (%c, %s, %s, %d) = %d\n", type, req, resp, *siz, retval);
c906108c
SS
2769
2770 return retval;
2771}
2772
96baa820
JM
2773static void
2774debug_to_rcmd (char *command,
d9fcf2fb 2775 struct ui_file *outbuf)
96baa820
JM
2776{
2777 debug_target.to_rcmd (command, outbuf);
2778 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
2779}
2780
c906108c 2781static struct symtab_and_line *
fba45db2 2782debug_to_enable_exception_callback (enum exception_event_kind kind, int enable)
c906108c 2783{
7a292a7a
SS
2784 struct symtab_and_line *result;
2785 result = debug_target.to_enable_exception_callback (kind, enable);
96baa820 2786 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2787 "target get_exception_callback_sal (%d, %d)\n",
2788 kind, enable);
7a292a7a 2789 return result;
c906108c
SS
2790}
2791
2792static struct exception_event_record *
fba45db2 2793debug_to_get_current_exception_event (void)
c906108c 2794{
7a292a7a 2795 struct exception_event_record *result;
c5aa993b 2796 result = debug_target.to_get_current_exception_event ();
96baa820 2797 fprintf_unfiltered (gdb_stdlog, "target get_current_exception_event ()\n");
7a292a7a 2798 return result;
c906108c
SS
2799}
2800
2801static char *
fba45db2 2802debug_to_pid_to_exec_file (int pid)
c906108c 2803{
c5aa993b 2804 char *exec_file;
c906108c
SS
2805
2806 exec_file = debug_target.to_pid_to_exec_file (pid);
2807
96baa820 2808 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 2809 pid, exec_file);
c906108c
SS
2810
2811 return exec_file;
2812}
2813
2814static char *
fba45db2 2815debug_to_core_file_to_sym_file (char *core)
c906108c 2816{
c5aa993b 2817 char *sym_file;
c906108c
SS
2818
2819 sym_file = debug_target.to_core_file_to_sym_file (core);
2820
96baa820 2821 fprintf_unfiltered (gdb_stdlog, "target_core_file_to_sym_file (%s) = %s\n",
c5aa993b 2822 core, sym_file);
c906108c
SS
2823
2824 return sym_file;
2825}
2826
2827static void
fba45db2 2828setup_target_debug (void)
c906108c
SS
2829{
2830 memcpy (&debug_target, &current_target, sizeof debug_target);
2831
2832 current_target.to_open = debug_to_open;
2833 current_target.to_close = debug_to_close;
2834 current_target.to_attach = debug_to_attach;
2835 current_target.to_post_attach = debug_to_post_attach;
2836 current_target.to_require_attach = debug_to_require_attach;
2837 current_target.to_detach = debug_to_detach;
2838 current_target.to_require_detach = debug_to_require_detach;
2839 current_target.to_resume = debug_to_resume;
2840 current_target.to_wait = debug_to_wait;
2841 current_target.to_post_wait = debug_to_post_wait;
2842 current_target.to_fetch_registers = debug_to_fetch_registers;
2843 current_target.to_store_registers = debug_to_store_registers;
2844 current_target.to_prepare_to_store = debug_to_prepare_to_store;
2845 current_target.to_xfer_memory = debug_to_xfer_memory;
2846 current_target.to_files_info = debug_to_files_info;
2847 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2848 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
2849 current_target.to_terminal_init = debug_to_terminal_init;
2850 current_target.to_terminal_inferior = debug_to_terminal_inferior;
2851 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2852 current_target.to_terminal_ours = debug_to_terminal_ours;
2853 current_target.to_terminal_info = debug_to_terminal_info;
2854 current_target.to_kill = debug_to_kill;
2855 current_target.to_load = debug_to_load;
2856 current_target.to_lookup_symbol = debug_to_lookup_symbol;
2857 current_target.to_create_inferior = debug_to_create_inferior;
2858 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
2859 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
2860 current_target.to_clone_and_follow_inferior = debug_to_clone_and_follow_inferior;
2861 current_target.to_post_follow_inferior_by_clone = debug_to_post_follow_inferior_by_clone;
2862 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
2863 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
2864 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
2865 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
2866 current_target.to_has_forked = debug_to_has_forked;
2867 current_target.to_has_vforked = debug_to_has_vforked;
2868 current_target.to_can_follow_vfork_prior_to_exec = debug_to_can_follow_vfork_prior_to_exec;
2869 current_target.to_post_follow_vfork = debug_to_post_follow_vfork;
2870 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
2871 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
2872 current_target.to_has_execd = debug_to_has_execd;
2873 current_target.to_reported_exec_events_per_exec_call = debug_to_reported_exec_events_per_exec_call;
2874 current_target.to_has_syscall_event = debug_to_has_syscall_event;
2875 current_target.to_has_exited = debug_to_has_exited;
2876 current_target.to_mourn_inferior = debug_to_mourn_inferior;
2877 current_target.to_can_run = debug_to_can_run;
2878 current_target.to_notice_signals = debug_to_notice_signals;
2879 current_target.to_thread_alive = debug_to_thread_alive;
0d06e24b 2880 current_target.to_find_new_threads = debug_to_find_new_threads;
c906108c
SS
2881 current_target.to_stop = debug_to_stop;
2882 current_target.to_query = debug_to_query;
96baa820 2883 current_target.to_rcmd = debug_to_rcmd;
c906108c
SS
2884 current_target.to_enable_exception_callback = debug_to_enable_exception_callback;
2885 current_target.to_get_current_exception_event = debug_to_get_current_exception_event;
2886 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
2887 current_target.to_core_file_to_sym_file = debug_to_core_file_to_sym_file;
2888
2889}
c906108c 2890\f
c5aa993b
JM
2891
2892static char targ_desc[] =
2893"Names of targets and files being debugged.\n\
c906108c
SS
2894Shows the entire stack of targets currently in use (including the exec-file,\n\
2895core-file, and process, if any), as well as the symbol file name.";
2896
96baa820
JM
2897static void
2898do_monitor_command (char *cmd,
2899 int from_tty)
2900{
2b5fe715
AC
2901 if ((current_target.to_rcmd
2902 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 2903 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
2904 && (debug_target.to_rcmd
2905 == (void (*) (char *, struct ui_file *)) tcomplain)))
96baa820
JM
2906 {
2907 error ("\"monitor\" command not supported by this target.\n");
2908 }
2909 target_rcmd (cmd, gdb_stdtarg);
2910}
2911
c906108c 2912void
fba45db2 2913initialize_targets (void)
c906108c
SS
2914{
2915 init_dummy_target ();
2916 push_target (&dummy_target);
2917
2918 add_info ("target", target_info, targ_desc);
2919 add_info ("files", target_info, targ_desc);
2920
c906108c 2921 add_show_from_set (
5d161b24 2922 add_set_cmd ("target", class_maintenance, var_zinteger,
c5aa993b
JM
2923 (char *) &targetdebug,
2924 "Set target debugging.\n\
5d161b24
DB
2925When non-zero, target debugging is enabled.", &setdebuglist),
2926 &showdebuglist);
c906108c 2927
96baa820
JM
2928
2929 add_com ("monitor", class_obscure, do_monitor_command,
2930 "Send a command to the remote monitor (remote targets only).");
2931
4930751a
C
2932 target_dcache = dcache_init();
2933
c906108c
SS
2934 if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
2935 abort ();
2936}
This page took 0.223228 seconds and 4 git commands to generate.