*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
b6ba6518 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
be4d1333
MS
3 2000, 2001, 2002
4 Free Software Foundation, Inc.
c906108c
SS
5 Contributed by Cygnus Support.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
c906108c
SS
23
24#include "defs.h"
25#include <errno.h>
c906108c
SS
26#include "gdb_string.h"
27#include "target.h"
28#include "gdbcmd.h"
29#include "symtab.h"
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
33#include "objfiles.h"
03f2053f 34#include "gdb_wait.h"
4930751a 35#include "dcache.h"
c906108c 36#include <signal.h>
4e052eda 37#include "regcache.h"
c906108c
SS
38
39extern int errno;
40
a14ed312 41static void target_info (char *, int);
c906108c 42
a14ed312 43static void cleanup_target (struct target_ops *);
c906108c 44
a14ed312 45static void maybe_kill_then_create_inferior (char *, char *, char **);
c906108c 46
a14ed312 47static void default_clone_and_follow_inferior (int, int *);
c906108c 48
a14ed312 49static void maybe_kill_then_attach (char *, int);
c906108c 50
a14ed312 51static void kill_or_be_killed (int);
c906108c 52
a14ed312 53static void default_terminal_info (char *, int);
c906108c 54
ccaa32c7
GS
55static int default_region_size_ok_for_hw_watchpoint (int);
56
a14ed312 57static int nosymbol (char *, CORE_ADDR *);
c906108c 58
a14ed312 59static void tcomplain (void);
c906108c 60
a14ed312 61static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 62
a14ed312 63static int return_zero (void);
c906108c 64
a14ed312 65static int return_one (void);
c906108c 66
ccaa32c7
GS
67static int return_minus_one (void);
68
a14ed312 69void target_ignore (void);
c906108c 70
a14ed312 71static void target_command (char *, int);
c906108c 72
a14ed312 73static struct target_ops *find_default_run_target (char *);
c906108c 74
a14ed312 75static void update_current_target (void);
c906108c 76
a14ed312 77static void nosupport_runtime (void);
392a587b 78
39f77062 79static void normal_target_post_startup_inferior (ptid_t ptid);
392a587b 80
917317f4
JM
81/* Transfer LEN bytes between target address MEMADDR and GDB address
82 MYADDR. Returns 0 for success, errno code for failure (which
83 includes partial transfers -- if you want a more useful response to
84 partial transfers, try either target_read_memory_partial or
85 target_write_memory_partial). */
c906108c
SS
86
87static int
a14ed312 88target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write);
c906108c 89
a14ed312 90static void init_dummy_target (void);
c906108c 91
a14ed312 92static void debug_to_open (char *, int);
c906108c 93
a14ed312 94static void debug_to_close (int);
c906108c 95
a14ed312 96static void debug_to_attach (char *, int);
c906108c 97
a14ed312 98static void debug_to_detach (char *, int);
c906108c 99
39f77062 100static void debug_to_resume (ptid_t, int, enum target_signal);
c906108c 101
39f77062 102static ptid_t debug_to_wait (ptid_t, struct target_waitstatus *);
c906108c 103
a14ed312 104static void debug_to_fetch_registers (int);
c906108c 105
a14ed312 106static void debug_to_store_registers (int);
c906108c 107
a14ed312 108static void debug_to_prepare_to_store (void);
c906108c 109
5ae5f592
AC
110static int debug_to_xfer_memory (CORE_ADDR, char *, int, int,
111 struct mem_attrib *, struct target_ops *);
c906108c 112
a14ed312 113static void debug_to_files_info (struct target_ops *);
c906108c 114
a14ed312 115static int debug_to_insert_breakpoint (CORE_ADDR, char *);
c906108c 116
a14ed312 117static int debug_to_remove_breakpoint (CORE_ADDR, char *);
c906108c 118
ccaa32c7
GS
119static int debug_to_can_use_hw_breakpoint (int, int, int);
120
121static int debug_to_insert_hw_breakpoint (CORE_ADDR, char *);
122
123static int debug_to_remove_hw_breakpoint (CORE_ADDR, char *);
124
125static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
126
127static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
128
129static int debug_to_stopped_by_watchpoint (void);
130
131static CORE_ADDR debug_to_stopped_data_address (void);
132
133static int debug_to_region_size_ok_for_hw_watchpoint (int);
134
a14ed312 135static void debug_to_terminal_init (void);
c906108c 136
a14ed312 137static void debug_to_terminal_inferior (void);
c906108c 138
a14ed312 139static void debug_to_terminal_ours_for_output (void);
c906108c 140
a790ad35
SC
141static void debug_to_terminal_save_ours (void);
142
a14ed312 143static void debug_to_terminal_ours (void);
c906108c 144
a14ed312 145static void debug_to_terminal_info (char *, int);
c906108c 146
a14ed312 147static void debug_to_kill (void);
c906108c 148
a14ed312 149static void debug_to_load (char *, int);
c906108c 150
a14ed312 151static int debug_to_lookup_symbol (char *, CORE_ADDR *);
c906108c 152
a14ed312 153static void debug_to_create_inferior (char *, char *, char **);
c906108c 154
a14ed312 155static void debug_to_mourn_inferior (void);
c906108c 156
a14ed312 157static int debug_to_can_run (void);
c906108c 158
39f77062 159static void debug_to_notice_signals (ptid_t);
c906108c 160
39f77062 161static int debug_to_thread_alive (ptid_t);
c906108c 162
a14ed312 163static void debug_to_stop (void);
c906108c 164
a14ed312 165static int debug_to_query (int /*char */ , char *, char *, int *);
c906108c
SS
166
167/* Pointer to array of target architecture structures; the size of the
168 array; the current index into the array; the allocated size of the
169 array. */
170struct target_ops **target_structs;
171unsigned target_struct_size;
172unsigned target_struct_index;
173unsigned target_struct_allocsize;
174#define DEFAULT_ALLOCSIZE 10
175
176/* The initial current target, so that there is always a semi-valid
177 current target. */
178
179static struct target_ops dummy_target;
180
181/* Top of target stack. */
182
183struct target_stack_item *target_stack;
184
185/* The target structure we are currently using to talk to a process
186 or file or whatever "inferior" we have. */
187
188struct target_ops current_target;
189
190/* Command list for target. */
191
192static struct cmd_list_element *targetlist = NULL;
193
194/* Nonzero if we are debugging an attached outside process
195 rather than an inferior. */
196
197int attach_flag;
198
c906108c
SS
199/* Non-zero if we want to see trace of target level stuff. */
200
201static int targetdebug = 0;
202
a14ed312 203static void setup_target_debug (void);
c906108c 204
4930751a
C
205DCACHE *target_dcache;
206
c906108c
SS
207/* The user just typed 'target' without the name of a target. */
208
209/* ARGSUSED */
210static void
fba45db2 211target_command (char *arg, int from_tty)
c906108c
SS
212{
213 fputs_filtered ("Argument required (target name). Try `help target'\n",
214 gdb_stdout);
215}
216
217/* Add a possible target architecture to the list. */
218
219void
fba45db2 220add_target (struct target_ops *t)
c906108c
SS
221{
222 if (!target_structs)
223 {
224 target_struct_allocsize = DEFAULT_ALLOCSIZE;
225 target_structs = (struct target_ops **) xmalloc
226 (target_struct_allocsize * sizeof (*target_structs));
227 }
228 if (target_struct_size >= target_struct_allocsize)
229 {
230 target_struct_allocsize *= 2;
231 target_structs = (struct target_ops **)
c5aa993b
JM
232 xrealloc ((char *) target_structs,
233 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
234 }
235 target_structs[target_struct_size++] = t;
c5aa993b 236/* cleanup_target (t); */
c906108c
SS
237
238 if (targetlist == NULL)
239 add_prefix_cmd ("target", class_run, target_command,
240 "Connect to a target machine or process.\n\
241The first argument is the type or protocol of the target machine.\n\
242Remaining arguments are interpreted by the target protocol. For more\n\
243information on the arguments for a particular protocol, type\n\
244`help target ' followed by the protocol name.",
245 &targetlist, "target ", 0, &cmdlist);
246 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
247}
248
249/* Stub functions */
250
251void
fba45db2 252target_ignore (void)
c906108c
SS
253{
254}
255
11cf8741
JM
256void
257target_load (char *arg, int from_tty)
258{
4930751a 259 dcache_invalidate (target_dcache);
11cf8741
JM
260 (*current_target.to_load) (arg, from_tty);
261}
262
c906108c
SS
263/* ARGSUSED */
264static int
fba45db2
KB
265nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
266 struct target_ops *t)
c906108c 267{
c5aa993b
JM
268 errno = EIO; /* Can't read/write this location */
269 return 0; /* No bytes handled */
c906108c
SS
270}
271
272static void
fba45db2 273tcomplain (void)
c906108c
SS
274{
275 error ("You can't do that when your target is `%s'",
276 current_target.to_shortname);
277}
278
279void
fba45db2 280noprocess (void)
c906108c
SS
281{
282 error ("You can't do that without a process to debug.");
283}
284
285/* ARGSUSED */
286static int
fba45db2 287nosymbol (char *name, CORE_ADDR *addrp)
c906108c 288{
c5aa993b 289 return 1; /* Symbol does not exist in target env */
c906108c
SS
290}
291
292/* ARGSUSED */
392a587b 293static void
fba45db2 294nosupport_runtime (void)
c906108c 295{
39f77062 296 if (ptid_equal (inferior_ptid, null_ptid))
c906108c
SS
297 noprocess ();
298 else
299 error ("No run-time support for this");
300}
301
302
303/* ARGSUSED */
304static void
fba45db2 305default_terminal_info (char *args, int from_tty)
c906108c 306{
c5aa993b 307 printf_unfiltered ("No saved terminal information.\n");
c906108c
SS
308}
309
310/* This is the default target_create_inferior and target_attach function.
311 If the current target is executing, it asks whether to kill it off.
312 If this function returns without calling error(), it has killed off
313 the target, and the operation should be attempted. */
314
315static void
fba45db2 316kill_or_be_killed (int from_tty)
c906108c
SS
317{
318 if (target_has_execution)
319 {
320 printf_unfiltered ("You are already running a program:\n");
321 target_files_info ();
c5aa993b
JM
322 if (query ("Kill it? "))
323 {
324 target_kill ();
325 if (target_has_execution)
326 error ("Killing the program did not help.");
327 return;
328 }
329 else
330 {
331 error ("Program not killed.");
332 }
c906108c 333 }
c5aa993b 334 tcomplain ();
c906108c
SS
335}
336
337static void
fba45db2 338maybe_kill_then_attach (char *args, int from_tty)
c906108c
SS
339{
340 kill_or_be_killed (from_tty);
341 target_attach (args, from_tty);
342}
343
344static void
fba45db2 345maybe_kill_then_create_inferior (char *exec, char *args, char **env)
c906108c
SS
346{
347 kill_or_be_killed (0);
348 target_create_inferior (exec, args, env);
349}
350
351static void
fba45db2 352default_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
353{
354 target_clone_and_follow_inferior (child_pid, followed_child);
355}
356
357/* Clean up a target struct so it no longer has any zero pointers in it.
358 We default entries, at least to stubs that print error messages. */
359
360static void
fba45db2 361cleanup_target (struct target_ops *t)
c906108c
SS
362{
363
364#define de_fault(field, value) \
0d06e24b
JM
365 if (!t->field) \
366 t->field = value
367
368 de_fault (to_open,
369 (void (*) (char *, int))
370 tcomplain);
371 de_fault (to_close,
372 (void (*) (int))
373 target_ignore);
374 de_fault (to_attach,
375 maybe_kill_then_attach);
376 de_fault (to_post_attach,
377 (void (*) (int))
378 target_ignore);
379 de_fault (to_require_attach,
380 maybe_kill_then_attach);
381 de_fault (to_detach,
382 (void (*) (char *, int))
383 target_ignore);
384 de_fault (to_require_detach,
385 (void (*) (int, char *, int))
386 target_ignore);
387 de_fault (to_resume,
39f77062 388 (void (*) (ptid_t, int, enum target_signal))
0d06e24b
JM
389 noprocess);
390 de_fault (to_wait,
39f77062 391 (ptid_t (*) (ptid_t, struct target_waitstatus *))
0d06e24b
JM
392 noprocess);
393 de_fault (to_post_wait,
39f77062 394 (void (*) (ptid_t, int))
0d06e24b
JM
395 target_ignore);
396 de_fault (to_fetch_registers,
397 (void (*) (int))
398 target_ignore);
399 de_fault (to_store_registers,
400 (void (*) (int))
401 noprocess);
402 de_fault (to_prepare_to_store,
403 (void (*) (void))
404 noprocess);
405 de_fault (to_xfer_memory,
29e57380 406 (int (*) (CORE_ADDR, char *, int, int, struct mem_attrib *, struct target_ops *))
0d06e24b
JM
407 nomemory);
408 de_fault (to_files_info,
409 (void (*) (struct target_ops *))
410 target_ignore);
411 de_fault (to_insert_breakpoint,
412 memory_insert_breakpoint);
413 de_fault (to_remove_breakpoint,
414 memory_remove_breakpoint);
ccaa32c7
GS
415 de_fault (to_can_use_hw_breakpoint,
416 (int (*) (int, int, int))
417 return_zero);
418 de_fault (to_insert_hw_breakpoint,
419 (int (*) (CORE_ADDR, char *))
420 return_minus_one);
421 de_fault (to_remove_hw_breakpoint,
422 (int (*) (CORE_ADDR, char *))
423 return_minus_one);
424 de_fault (to_insert_watchpoint,
425 (int (*) (CORE_ADDR, int, int))
426 return_minus_one);
427 de_fault (to_remove_watchpoint,
428 (int (*) (CORE_ADDR, int, int))
429 return_minus_one);
430 de_fault (to_stopped_by_watchpoint,
431 (int (*) (void))
432 return_zero);
433 de_fault (to_stopped_data_address,
434 (CORE_ADDR (*) (void))
435 return_zero);
436 de_fault (to_region_size_ok_for_hw_watchpoint,
437 default_region_size_ok_for_hw_watchpoint);
0d06e24b
JM
438 de_fault (to_terminal_init,
439 (void (*) (void))
440 target_ignore);
441 de_fault (to_terminal_inferior,
442 (void (*) (void))
443 target_ignore);
444 de_fault (to_terminal_ours_for_output,
445 (void (*) (void))
446 target_ignore);
447 de_fault (to_terminal_ours,
448 (void (*) (void))
449 target_ignore);
a790ad35
SC
450 de_fault (to_terminal_save_ours,
451 (void (*) (void))
452 target_ignore);
0d06e24b
JM
453 de_fault (to_terminal_info,
454 default_terminal_info);
455 de_fault (to_kill,
456 (void (*) (void))
457 noprocess);
458 de_fault (to_load,
459 (void (*) (char *, int))
460 tcomplain);
461 de_fault (to_lookup_symbol,
462 (int (*) (char *, CORE_ADDR *))
463 nosymbol);
464 de_fault (to_create_inferior,
465 maybe_kill_then_create_inferior);
466 de_fault (to_post_startup_inferior,
39f77062 467 (void (*) (ptid_t))
0d06e24b
JM
468 target_ignore);
469 de_fault (to_acknowledge_created_inferior,
470 (void (*) (int))
471 target_ignore);
472 de_fault (to_clone_and_follow_inferior,
473 default_clone_and_follow_inferior);
474 de_fault (to_post_follow_inferior_by_clone,
475 (void (*) (void))
476 target_ignore);
477 de_fault (to_insert_fork_catchpoint,
478 (int (*) (int))
479 tcomplain);
480 de_fault (to_remove_fork_catchpoint,
481 (int (*) (int))
482 tcomplain);
483 de_fault (to_insert_vfork_catchpoint,
484 (int (*) (int))
485 tcomplain);
486 de_fault (to_remove_vfork_catchpoint,
487 (int (*) (int))
488 tcomplain);
489 de_fault (to_has_forked,
490 (int (*) (int, int *))
491 return_zero);
492 de_fault (to_has_vforked,
493 (int (*) (int, int *))
494 return_zero);
495 de_fault (to_can_follow_vfork_prior_to_exec,
496 (int (*) (void))
497 return_zero);
498 de_fault (to_post_follow_vfork,
499 (void (*) (int, int, int, int))
500 target_ignore);
501 de_fault (to_insert_exec_catchpoint,
502 (int (*) (int))
503 tcomplain);
504 de_fault (to_remove_exec_catchpoint,
505 (int (*) (int))
506 tcomplain);
507 de_fault (to_has_execd,
508 (int (*) (int, char **))
509 return_zero);
510 de_fault (to_reported_exec_events_per_exec_call,
511 (int (*) (void))
512 return_one);
513 de_fault (to_has_syscall_event,
514 (int (*) (int, enum target_waitkind *, int *))
515 return_zero);
516 de_fault (to_has_exited,
517 (int (*) (int, int, int *))
518 return_zero);
519 de_fault (to_mourn_inferior,
520 (void (*) (void))
521 noprocess);
522 de_fault (to_can_run,
523 return_zero);
524 de_fault (to_notice_signals,
39f77062 525 (void (*) (ptid_t))
0d06e24b
JM
526 target_ignore);
527 de_fault (to_thread_alive,
39f77062 528 (int (*) (ptid_t))
0d06e24b
JM
529 return_zero);
530 de_fault (to_find_new_threads,
531 (void (*) (void))
532 target_ignore);
533 de_fault (to_extra_thread_info,
534 (char *(*) (struct thread_info *))
535 return_zero);
536 de_fault (to_stop,
537 (void (*) (void))
538 target_ignore);
0d06e24b 539 de_fault (to_rcmd,
d9fcf2fb 540 (void (*) (char *, struct ui_file *))
0d06e24b
JM
541 tcomplain);
542 de_fault (to_enable_exception_callback,
543 (struct symtab_and_line * (*) (enum exception_event_kind, int))
544 nosupport_runtime);
545 de_fault (to_get_current_exception_event,
546 (struct exception_event_record * (*) (void))
547 nosupport_runtime);
548 de_fault (to_pid_to_exec_file,
549 (char *(*) (int))
550 return_zero);
0d06e24b
JM
551 de_fault (to_can_async_p,
552 (int (*) (void))
553 return_zero);
554 de_fault (to_is_async_p,
555 (int (*) (void))
556 return_zero);
557 de_fault (to_async,
558 (void (*) (void (*) (enum inferior_event_type, void*), void*))
559 tcomplain);
c906108c
SS
560#undef de_fault
561}
562
563/* Go through the target stack from top to bottom, copying over zero entries in
564 current_target. In effect, we are doing class inheritance through the
565 pushed target vectors. */
566
567static void
fba45db2 568update_current_target (void)
c906108c
SS
569{
570 struct target_stack_item *item;
571 struct target_ops *t;
572
573 /* First, reset current_target */
574 memset (&current_target, 0, sizeof current_target);
575
576 for (item = target_stack; item; item = item->next)
577 {
578 t = item->target_ops;
579
580#define INHERIT(FIELD, TARGET) \
581 if (!current_target.FIELD) \
582 current_target.FIELD = TARGET->FIELD
583
584 INHERIT (to_shortname, t);
585 INHERIT (to_longname, t);
586 INHERIT (to_doc, t);
587 INHERIT (to_open, t);
588 INHERIT (to_close, t);
589 INHERIT (to_attach, t);
590 INHERIT (to_post_attach, t);
591 INHERIT (to_require_attach, t);
592 INHERIT (to_detach, t);
593 INHERIT (to_require_detach, t);
594 INHERIT (to_resume, t);
595 INHERIT (to_wait, t);
596 INHERIT (to_post_wait, t);
597 INHERIT (to_fetch_registers, t);
598 INHERIT (to_store_registers, t);
599 INHERIT (to_prepare_to_store, t);
600 INHERIT (to_xfer_memory, t);
601 INHERIT (to_files_info, t);
602 INHERIT (to_insert_breakpoint, t);
603 INHERIT (to_remove_breakpoint, t);
ccaa32c7
GS
604 INHERIT (to_can_use_hw_breakpoint, t);
605 INHERIT (to_insert_hw_breakpoint, t);
606 INHERIT (to_remove_hw_breakpoint, t);
607 INHERIT (to_insert_watchpoint, t);
608 INHERIT (to_remove_watchpoint, t);
609 INHERIT (to_stopped_data_address, t);
610 INHERIT (to_stopped_by_watchpoint, t);
611 INHERIT (to_region_size_ok_for_hw_watchpoint, t);
c906108c
SS
612 INHERIT (to_terminal_init, t);
613 INHERIT (to_terminal_inferior, t);
614 INHERIT (to_terminal_ours_for_output, t);
615 INHERIT (to_terminal_ours, t);
a790ad35 616 INHERIT (to_terminal_save_ours, t);
c906108c
SS
617 INHERIT (to_terminal_info, t);
618 INHERIT (to_kill, t);
619 INHERIT (to_load, t);
620 INHERIT (to_lookup_symbol, t);
621 INHERIT (to_create_inferior, t);
622 INHERIT (to_post_startup_inferior, t);
623 INHERIT (to_acknowledge_created_inferior, t);
624 INHERIT (to_clone_and_follow_inferior, t);
625 INHERIT (to_post_follow_inferior_by_clone, t);
626 INHERIT (to_insert_fork_catchpoint, t);
627 INHERIT (to_remove_fork_catchpoint, t);
628 INHERIT (to_insert_vfork_catchpoint, t);
629 INHERIT (to_remove_vfork_catchpoint, t);
630 INHERIT (to_has_forked, t);
631 INHERIT (to_has_vforked, t);
632 INHERIT (to_can_follow_vfork_prior_to_exec, t);
633 INHERIT (to_post_follow_vfork, t);
634 INHERIT (to_insert_exec_catchpoint, t);
635 INHERIT (to_remove_exec_catchpoint, t);
636 INHERIT (to_has_execd, t);
637 INHERIT (to_reported_exec_events_per_exec_call, t);
638 INHERIT (to_has_syscall_event, t);
639 INHERIT (to_has_exited, t);
640 INHERIT (to_mourn_inferior, t);
641 INHERIT (to_can_run, t);
642 INHERIT (to_notice_signals, t);
643 INHERIT (to_thread_alive, t);
b83266a0 644 INHERIT (to_find_new_threads, t);
ed9a39eb 645 INHERIT (to_pid_to_str, t);
0d06e24b 646 INHERIT (to_extra_thread_info, t);
c906108c
SS
647 INHERIT (to_stop, t);
648 INHERIT (to_query, t);
96baa820 649 INHERIT (to_rcmd, t);
c906108c
SS
650 INHERIT (to_enable_exception_callback, t);
651 INHERIT (to_get_current_exception_event, t);
652 INHERIT (to_pid_to_exec_file, t);
c906108c
SS
653 INHERIT (to_stratum, t);
654 INHERIT (DONT_USE, t);
655 INHERIT (to_has_all_memory, t);
656 INHERIT (to_has_memory, t);
657 INHERIT (to_has_stack, t);
658 INHERIT (to_has_registers, t);
659 INHERIT (to_has_execution, t);
660 INHERIT (to_has_thread_control, t);
661 INHERIT (to_sections, t);
662 INHERIT (to_sections_end, t);
6426a772
JM
663 INHERIT (to_can_async_p, t);
664 INHERIT (to_is_async_p, t);
665 INHERIT (to_async, t);
ed9a39eb 666 INHERIT (to_async_mask_value, t);
be4d1333
MS
667 INHERIT (to_find_memory_regions, t);
668 INHERIT (to_make_corefile_notes, t);
c906108c
SS
669 INHERIT (to_magic, t);
670
671#undef INHERIT
672 }
673}
674
675/* Push a new target type into the stack of the existing target accessors,
676 possibly superseding some of the existing accessors.
677
678 Result is zero if the pushed target ended up on top of the stack,
679 nonzero if at least one target is on top of it.
680
681 Rather than allow an empty stack, we always have the dummy target at
682 the bottom stratum, so we can call the function vectors without
683 checking them. */
684
685int
fba45db2 686push_target (struct target_ops *t)
c906108c
SS
687{
688 struct target_stack_item *cur, *prev, *tmp;
689
690 /* Check magic number. If wrong, it probably means someone changed
691 the struct definition, but not all the places that initialize one. */
692 if (t->to_magic != OPS_MAGIC)
693 {
c5aa993b
JM
694 fprintf_unfiltered (gdb_stderr,
695 "Magic number of %s target struct wrong\n",
696 t->to_shortname);
e1e9e218 697 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
698 }
699
700 /* Find the proper stratum to install this target in. */
701
702 for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
703 {
c5aa993b 704 if ((int) (t->to_stratum) >= (int) (cur->target_ops->to_stratum))
c906108c
SS
705 break;
706 }
707
708 /* If there's already targets at this stratum, remove them. */
709
710 if (cur)
711 while (t->to_stratum == cur->target_ops->to_stratum)
712 {
713 /* There's already something on this stratum. Close it off. */
714 if (cur->target_ops->to_close)
715 (cur->target_ops->to_close) (0);
716 if (prev)
c5aa993b 717 prev->next = cur->next; /* Unchain old target_ops */
c906108c 718 else
c5aa993b 719 target_stack = cur->next; /* Unchain first on list */
c906108c 720 tmp = cur->next;
b8c9b27d 721 xfree (cur);
c906108c
SS
722 cur = tmp;
723 }
724
725 /* We have removed all targets in our stratum, now add the new one. */
726
727 tmp = (struct target_stack_item *)
728 xmalloc (sizeof (struct target_stack_item));
729 tmp->next = cur;
730 tmp->target_ops = t;
731
732 if (prev)
733 prev->next = tmp;
734 else
735 target_stack = tmp;
736
737 update_current_target ();
738
c5aa993b 739 cleanup_target (&current_target); /* Fill in the gaps */
c906108c 740
c906108c
SS
741 if (targetdebug)
742 setup_target_debug ();
c906108c
SS
743
744 return prev != 0;
745}
746
747/* Remove a target_ops vector from the stack, wherever it may be.
748 Return how many times it was removed (0 or 1). */
749
750int
fba45db2 751unpush_target (struct target_ops *t)
c906108c
SS
752{
753 struct target_stack_item *cur, *prev;
754
755 if (t->to_close)
756 t->to_close (0); /* Let it clean up */
757
758 /* Look for the specified target. Note that we assume that a target
759 can only occur once in the target stack. */
760
761 for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
762 if (cur->target_ops == t)
763 break;
764
765 if (!cur)
766 return 0; /* Didn't find target_ops, quit now */
767
768 /* Unchain the target */
769
770 if (!prev)
771 target_stack = cur->next;
772 else
773 prev->next = cur->next;
774
b8c9b27d 775 xfree (cur); /* Release the target_stack_item */
c906108c
SS
776
777 update_current_target ();
778 cleanup_target (&current_target);
779
780 return 1;
781}
782
783void
fba45db2 784pop_target (void)
c906108c 785{
c5aa993b 786 (current_target.to_close) (0); /* Let it clean up */
c906108c
SS
787 if (unpush_target (target_stack->target_ops) == 1)
788 return;
789
c5aa993b
JM
790 fprintf_unfiltered (gdb_stderr,
791 "pop_target couldn't find target %s\n",
792 current_target.to_shortname);
e1e9e218 793 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
794}
795
796#undef MIN
797#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
798
799/* target_read_string -- read a null terminated string, up to LEN bytes,
800 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
801 Set *STRING to a pointer to malloc'd memory containing the data; the caller
802 is responsible for freeing it. Return the number of bytes successfully
803 read. */
804
805int
fba45db2 806target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c
SS
807{
808 int tlen, origlen, offset, i;
809 char buf[4];
810 int errcode = 0;
811 char *buffer;
812 int buffer_allocated;
813 char *bufptr;
814 unsigned int nbytes_read = 0;
815
816 /* Small for testing. */
817 buffer_allocated = 4;
818 buffer = xmalloc (buffer_allocated);
819 bufptr = buffer;
820
821 origlen = len;
822
823 while (len > 0)
824 {
825 tlen = MIN (len, 4 - (memaddr & 3));
826 offset = memaddr & 3;
827
d4b2399a 828 errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
c906108c
SS
829 if (errcode != 0)
830 {
831 /* The transfer request might have crossed the boundary to an
832 unallocated region of memory. Retry the transfer, requesting
833 a single byte. */
834 tlen = 1;
835 offset = 0;
d4b2399a 836 errcode = target_xfer_memory (memaddr, buf, 1, 0);
c906108c
SS
837 if (errcode != 0)
838 goto done;
839 }
840
841 if (bufptr - buffer + tlen > buffer_allocated)
842 {
843 unsigned int bytes;
844 bytes = bufptr - buffer;
845 buffer_allocated *= 2;
846 buffer = xrealloc (buffer, buffer_allocated);
847 bufptr = buffer + bytes;
848 }
849
850 for (i = 0; i < tlen; i++)
851 {
852 *bufptr++ = buf[i + offset];
853 if (buf[i + offset] == '\000')
854 {
855 nbytes_read += i + 1;
856 goto done;
857 }
858 }
859
860 memaddr += tlen;
861 len -= tlen;
862 nbytes_read += tlen;
863 }
c5aa993b 864done:
c906108c
SS
865 if (errnop != NULL)
866 *errnop = errcode;
867 if (string != NULL)
868 *string = buffer;
869 return nbytes_read;
870}
871
872/* Read LEN bytes of target memory at address MEMADDR, placing the results in
873 GDB's memory at MYADDR. Returns either 0 for success or an errno value
874 if any error occurs.
875
876 If an error occurs, no guarantee is made about the contents of the data at
877 MYADDR. In particular, the caller should not depend upon partial reads
878 filling the buffer with good data. There is no way for the caller to know
879 how much good data might have been transfered anyway. Callers that can
880 deal with partial reads should call target_read_memory_partial. */
881
882int
fba45db2 883target_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 884{
d4b2399a 885 return target_xfer_memory (memaddr, myaddr, len, 0);
c906108c
SS
886}
887
c906108c 888int
fba45db2 889target_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 890{
d4b2399a 891 return target_xfer_memory (memaddr, myaddr, len, 1);
c906108c 892}
c5aa993b 893
3a11626d
MS
894static int trust_readonly = 0;
895
67e0617e
C
896/* Move memory to or from the targets. The top target gets priority;
897 if it cannot handle it, it is offered to the next one down, etc.
c906108c 898
67e0617e 899 Result is -1 on error, or the number of bytes transfered. */
c906108c 900
4930751a 901int
29e57380
C
902do_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
903 struct mem_attrib *attrib)
c906108c 904{
c906108c 905 int res;
4930751a 906 int done = 0;
c906108c
SS
907 struct target_ops *t;
908 struct target_stack_item *item;
909
910 /* Zero length requests are ok and require no work. */
911 if (len == 0)
912 return 0;
913
c906108c
SS
914 /* to_xfer_memory is not guaranteed to set errno, even when it returns
915 0. */
916 errno = 0;
917
3a11626d
MS
918 if (!write && trust_readonly)
919 {
2ceb85d0
BE
920 /* User-settable option, "trust-readonly-sections". If true,
921 then memory from any SEC_READONLY bfd section may be read
922 directly from the bfd file. */
3a11626d
MS
923
924 struct section_table *secp;
925
926 for (secp = current_target.to_sections;
927 secp < current_target.to_sections_end;
928 secp++)
929 {
405f26e6
MS
930 if (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
931 & SEC_READONLY)
932 if (memaddr >= secp->addr && memaddr < secp->endaddr)
933 return xfer_memory (memaddr, myaddr, len, 0,
934 attrib, &current_target);
3a11626d
MS
935 }
936 }
937
67e0617e 938 /* The quick case is that the top target can handle the transfer. */
c906108c 939 res = current_target.to_xfer_memory
29e57380 940 (memaddr, myaddr, len, write, attrib, &current_target);
c906108c 941
67e0617e
C
942 /* If res <= 0 then we call it again in the loop. Ah well. */
943 if (res <= 0)
c906108c 944 {
c906108c
SS
945 for (item = target_stack; item; item = item->next)
946 {
947 t = item->target_ops;
948 if (!t->to_has_memory)
949 continue;
950
29e57380 951 res = t->to_xfer_memory (memaddr, myaddr, len, write, attrib, t);
c906108c
SS
952 if (res > 0)
953 break; /* Handled all or part of xfer */
954 if (t->to_has_all_memory)
955 break;
956 }
957
4930751a 958 if (res <= 0)
67e0617e 959 return -1;
4930751a 960 }
67e0617e
C
961
962 return res;
4930751a
C
963}
964
67e0617e
C
965
966/* Perform a memory transfer. Iterate until the entire region has
967 been transfered.
968
969 Result is 0 or errno value. */
970
4930751a
C
971static int
972target_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write)
973{
974 int res;
29e57380
C
975 int reg_len;
976 struct mem_region *region;
4930751a
C
977
978 /* Zero length requests are ok and require no work. */
979 if (len == 0)
980 {
981 return 0;
982 }
983
984 while (len > 0)
985 {
29e57380
C
986 region = lookup_mem_region(memaddr);
987 if (memaddr + len < region->hi)
988 reg_len = len;
989 else
990 reg_len = region->hi - memaddr;
991
992 switch (region->attrib.mode)
c906108c 993 {
29e57380
C
994 case MEM_RO:
995 if (write)
996 return EIO;
997 break;
998
999 case MEM_WO:
c906108c 1000 if (!write)
c906108c 1001 return EIO;
29e57380 1002 break;
c906108c 1003 }
4930751a 1004
29e57380
C
1005 while (reg_len > 0)
1006 {
1007 if (region->attrib.cache)
8add0441 1008 res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
29e57380
C
1009 reg_len, write);
1010 else
8add0441 1011 res = do_xfer_memory (memaddr, myaddr, reg_len, write,
29e57380
C
1012 &region->attrib);
1013
1014 if (res <= 0)
1015 {
1016 /* If this address is for nonexistent memory, read zeros
1017 if reading, or do nothing if writing. Return
1018 error. */
1019 if (!write)
1020 memset (myaddr, 0, len);
1021 if (errno == 0)
1022 return EIO;
1023 else
1024 return errno;
1025 }
1026
1027 memaddr += res;
1028 myaddr += res;
1029 len -= res;
1030 reg_len -= res;
1031 }
c906108c 1032 }
4930751a 1033
c906108c
SS
1034 return 0; /* We managed to cover it all somehow. */
1035}
1036
1037
67e0617e
C
1038/* Perform a partial memory transfer.
1039
1040 Result is -1 on error, or the number of bytes transfered. */
917317f4
JM
1041
1042static int
4930751a 1043target_xfer_memory_partial (CORE_ADDR memaddr, char *myaddr, int len,
917317f4
JM
1044 int write_p, int *err)
1045{
1046 int res;
29e57380
C
1047 int reg_len;
1048 struct mem_region *region;
917317f4
JM
1049
1050 /* Zero length requests are ok and require no work. */
1051 if (len == 0)
1052 {
1053 *err = 0;
1054 return 0;
1055 }
1056
29e57380
C
1057 region = lookup_mem_region(memaddr);
1058 if (memaddr + len < region->hi)
1059 reg_len = len;
1060 else
1061 reg_len = region->hi - memaddr;
1062
1063 switch (region->attrib.mode)
1064 {
1065 case MEM_RO:
1066 if (write_p)
1067 {
1068 *err = EIO;
873406a6 1069 return -1;
29e57380
C
1070 }
1071 break;
1072
1073 case MEM_WO:
1074 if (write_p)
1075 {
1076 *err = EIO;
873406a6 1077 return -1;
29e57380
C
1078 }
1079 break;
1080 }
1081
1082 if (region->attrib.cache)
1083 res = dcache_xfer_memory (target_dcache, memaddr, myaddr,
1084 reg_len, write_p);
1085 else
1086 res = do_xfer_memory (memaddr, myaddr, reg_len, write_p,
1087 &region->attrib);
1088
4930751a 1089 if (res <= 0)
917317f4 1090 {
4930751a
C
1091 if (errno != 0)
1092 *err = errno;
1093 else
1094 *err = EIO;
917317f4 1095
4930751a 1096 return -1;
917317f4
JM
1097 }
1098
4930751a 1099 *err = 0;
67e0617e 1100 return res;
917317f4
JM
1101}
1102
1103int
1104target_read_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
1105{
1106 return target_xfer_memory_partial (memaddr, buf, len, 0, err);
1107}
1108
1109int
1110target_write_memory_partial (CORE_ADDR memaddr, char *buf, int len, int *err)
1111{
1112 return target_xfer_memory_partial (memaddr, buf, len, 1, err);
1113}
1114
c906108c
SS
1115/* ARGSUSED */
1116static void
fba45db2 1117target_info (char *args, int from_tty)
c906108c
SS
1118{
1119 struct target_ops *t;
1120 struct target_stack_item *item;
1121 int has_all_mem = 0;
c5aa993b 1122
c906108c
SS
1123 if (symfile_objfile != NULL)
1124 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
1125
1126#ifdef FILES_INFO_HOOK
1127 if (FILES_INFO_HOOK ())
1128 return;
1129#endif
1130
1131 for (item = target_stack; item; item = item->next)
1132 {
1133 t = item->target_ops;
1134
1135 if (!t->to_has_memory)
1136 continue;
1137
c5aa993b 1138 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
1139 continue;
1140 if (has_all_mem)
c5aa993b
JM
1141 printf_unfiltered ("\tWhile running this, GDB does not access memory from...\n");
1142 printf_unfiltered ("%s:\n", t->to_longname);
1143 (t->to_files_info) (t);
c906108c
SS
1144 has_all_mem = t->to_has_all_memory;
1145 }
1146}
1147
1148/* This is to be called by the open routine before it does
1149 anything. */
1150
1151void
fba45db2 1152target_preopen (int from_tty)
c906108c 1153{
c5aa993b 1154 dont_repeat ();
c906108c
SS
1155
1156 if (target_has_execution)
c5aa993b 1157 {
adf40b2e
JM
1158 if (!from_tty
1159 || query ("A program is being debugged already. Kill it? "))
c5aa993b 1160 target_kill ();
c906108c 1161 else
c5aa993b 1162 error ("Program not killed.");
c906108c
SS
1163 }
1164
1165 /* Calling target_kill may remove the target from the stack. But if
1166 it doesn't (which seems like a win for UDI), remove it now. */
1167
1168 if (target_has_execution)
1169 pop_target ();
1170}
1171
1172/* Detach a target after doing deferred register stores. */
1173
1174void
fba45db2 1175target_detach (char *args, int from_tty)
c906108c
SS
1176{
1177 /* Handle any optimized stores to the inferior. */
1178#ifdef DO_DEFERRED_STORES
1179 DO_DEFERRED_STORES;
1180#endif
1181 (current_target.to_detach) (args, from_tty);
1182}
1183
1184void
fba45db2 1185target_link (char *modname, CORE_ADDR *t_reloc)
c906108c 1186{
c5aa993b 1187 if (STREQ (current_target.to_shortname, "rombug"))
c906108c
SS
1188 {
1189 (current_target.to_lookup_symbol) (modname, t_reloc);
1190 if (*t_reloc == 0)
c5aa993b 1191 error ("Unable to link to %s and get relocation in rombug", modname);
c906108c
SS
1192 }
1193 else
2acceee2 1194 *t_reloc = (CORE_ADDR) -1;
c906108c
SS
1195}
1196
ed9a39eb
JM
1197int
1198target_async_mask (int mask)
1199{
1200 int saved_async_masked_status = target_async_mask_value;
1201 target_async_mask_value = mask;
1202 return saved_async_masked_status;
1203}
1204
c906108c
SS
1205/* Look through the list of possible targets for a target that can
1206 execute a run or attach command without any other data. This is
1207 used to locate the default process stratum.
1208
1209 Result is always valid (error() is called for errors). */
1210
1211static struct target_ops *
fba45db2 1212find_default_run_target (char *do_mesg)
c906108c
SS
1213{
1214 struct target_ops **t;
1215 struct target_ops *runable = NULL;
1216 int count;
1217
1218 count = 0;
1219
1220 for (t = target_structs; t < target_structs + target_struct_size;
1221 ++t)
1222 {
c5aa993b 1223 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
1224 {
1225 runable = *t;
1226 ++count;
1227 }
1228 }
1229
1230 if (count != 1)
1231 error ("Don't know how to %s. Try \"help target\".", do_mesg);
1232
1233 return runable;
1234}
1235
1236void
fba45db2 1237find_default_attach (char *args, int from_tty)
c906108c
SS
1238{
1239 struct target_ops *t;
1240
c5aa993b 1241 t = find_default_run_target ("attach");
c906108c
SS
1242 (t->to_attach) (args, from_tty);
1243 return;
1244}
1245
1246void
fba45db2 1247find_default_require_attach (char *args, int from_tty)
c906108c
SS
1248{
1249 struct target_ops *t;
1250
c5aa993b 1251 t = find_default_run_target ("require_attach");
c906108c
SS
1252 (t->to_require_attach) (args, from_tty);
1253 return;
1254}
1255
1256void
fba45db2 1257find_default_require_detach (int pid, char *args, int from_tty)
c906108c
SS
1258{
1259 struct target_ops *t;
1260
c5aa993b 1261 t = find_default_run_target ("require_detach");
c906108c
SS
1262 (t->to_require_detach) (pid, args, from_tty);
1263 return;
1264}
1265
1266void
fba45db2 1267find_default_create_inferior (char *exec_file, char *allargs, char **env)
c906108c
SS
1268{
1269 struct target_ops *t;
1270
c5aa993b 1271 t = find_default_run_target ("run");
c906108c
SS
1272 (t->to_create_inferior) (exec_file, allargs, env);
1273 return;
1274}
1275
1276void
fba45db2 1277find_default_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
1278{
1279 struct target_ops *t;
1280
c5aa993b 1281 t = find_default_run_target ("run");
c906108c
SS
1282 (t->to_clone_and_follow_inferior) (child_pid, followed_child);
1283 return;
1284}
1285
ccaa32c7
GS
1286static int
1287default_region_size_ok_for_hw_watchpoint (int byte_count)
1288{
1289 return (byte_count <= REGISTER_SIZE);
1290}
1291
c906108c 1292static int
fba45db2 1293return_zero (void)
c906108c
SS
1294{
1295 return 0;
1296}
1297
1298static int
fba45db2 1299return_one (void)
c906108c
SS
1300{
1301 return 1;
1302}
1303
ccaa32c7
GS
1304static int
1305return_minus_one (void)
1306{
1307 return -1;
1308}
1309
6426a772
JM
1310/*
1311 * Resize the to_sections pointer. Also make sure that anyone that
1312 * was holding on to an old value of it gets updated.
1313 * Returns the old size.
1314 */
1315
1316int
1317target_resize_to_sections (struct target_ops *target, int num_added)
1318{
1319 struct target_ops **t;
1320 struct section_table *old_value;
1321 int old_count;
1322
1323 old_value = target->to_sections;
1324
1325 if (target->to_sections)
1326 {
1327 old_count = target->to_sections_end - target->to_sections;
1328 target->to_sections = (struct section_table *)
1329 xrealloc ((char *) target->to_sections,
1330 (sizeof (struct section_table)) * (num_added + old_count));
1331 }
1332 else
1333 {
1334 old_count = 0;
1335 target->to_sections = (struct section_table *)
1336 xmalloc ((sizeof (struct section_table)) * num_added);
1337 }
1338 target->to_sections_end = target->to_sections + (num_added + old_count);
1339
1340 /* Check to see if anyone else was pointing to this structure.
1341 If old_value was null, then no one was. */
1342
1343 if (old_value)
1344 {
1345 for (t = target_structs; t < target_structs + target_struct_size;
1346 ++t)
1347 {
1348 if ((*t)->to_sections == old_value)
1349 {
1350 (*t)->to_sections = target->to_sections;
1351 (*t)->to_sections_end = target->to_sections_end;
1352 }
1353 }
1354 }
1355
1356 return old_count;
1357
1358}
1359
07cd4b97
JB
1360/* Remove all target sections taken from ABFD.
1361
1362 Scan the current target stack for targets whose section tables
1363 refer to sections from BFD, and remove those sections. We use this
1364 when we notice that the inferior has unloaded a shared object, for
1365 example. */
1366void
1367remove_target_sections (bfd *abfd)
1368{
1369 struct target_ops **t;
1370
1371 for (t = target_structs; t < target_structs + target_struct_size; t++)
1372 {
1373 struct section_table *src, *dest;
1374
1375 dest = (*t)->to_sections;
1376 for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
1377 if (src->bfd != abfd)
1378 {
1379 /* Keep this section. */
1380 if (dest < src) *dest = *src;
1381 dest++;
1382 }
1383
1384 /* If we've dropped any sections, resize the section table. */
1385 if (dest < src)
1386 target_resize_to_sections (*t, dest - src);
1387 }
1388}
1389
1390
1391
1392
7a292a7a
SS
1393/* Find a single runnable target in the stack and return it. If for
1394 some reason there is more than one, return NULL. */
1395
1396struct target_ops *
fba45db2 1397find_run_target (void)
7a292a7a
SS
1398{
1399 struct target_ops **t;
1400 struct target_ops *runable = NULL;
1401 int count;
c5aa993b 1402
7a292a7a 1403 count = 0;
c5aa993b 1404
7a292a7a
SS
1405 for (t = target_structs; t < target_structs + target_struct_size; ++t)
1406 {
c5aa993b 1407 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
1408 {
1409 runable = *t;
1410 ++count;
1411 }
1412 }
c5aa993b 1413
7a292a7a
SS
1414 return (count == 1 ? runable : NULL);
1415}
1416
ed9a39eb
JM
1417/* Find a single core_stratum target in the list of targets and return it.
1418 If for some reason there is more than one, return NULL. */
1419
c906108c 1420struct target_ops *
fba45db2 1421find_core_target (void)
c906108c
SS
1422{
1423 struct target_ops **t;
1424 struct target_ops *runable = NULL;
1425 int count;
c5aa993b 1426
c906108c 1427 count = 0;
c5aa993b 1428
c906108c
SS
1429 for (t = target_structs; t < target_structs + target_struct_size;
1430 ++t)
1431 {
1432 if ((*t)->to_stratum == core_stratum)
1433 {
1434 runable = *t;
1435 ++count;
1436 }
1437 }
c5aa993b
JM
1438
1439 return (count == 1 ? runable : NULL);
c906108c 1440}
ed9a39eb
JM
1441
1442/*
1443 * Find the next target down the stack from the specified target.
1444 */
1445
1446struct target_ops *
fba45db2 1447find_target_beneath (struct target_ops *t)
ed9a39eb
JM
1448{
1449 struct target_stack_item *cur;
1450
1451 for (cur = target_stack; cur; cur = cur->next)
1452 if (cur->target_ops == t)
1453 break;
1454
1455 if (cur == NULL || cur->next == NULL)
1456 return NULL;
1457 else
1458 return cur->next->target_ops;
1459}
1460
c906108c
SS
1461\f
1462/* The inferior process has died. Long live the inferior! */
1463
1464void
fba45db2 1465generic_mourn_inferior (void)
c906108c
SS
1466{
1467 extern int show_breakpoint_hit_counts;
1468
39f77062 1469 inferior_ptid = null_ptid;
c906108c
SS
1470 attach_flag = 0;
1471 breakpoint_init_inferior (inf_exited);
1472 registers_changed ();
1473
1474#ifdef CLEAR_DEFERRED_STORES
1475 /* Delete any pending stores to the inferior... */
1476 CLEAR_DEFERRED_STORES;
1477#endif
1478
1479 reopen_exec_file ();
1480 reinit_frame_cache ();
1481
1482 /* It is confusing to the user for ignore counts to stick around
1483 from previous runs of the inferior. So clear them. */
1484 /* However, it is more confusing for the ignore counts to disappear when
1485 using hit counts. So don't clear them if we're counting hits. */
1486 if (!show_breakpoint_hit_counts)
1487 breakpoint_clear_ignore_counts ();
c5b739b5
FN
1488
1489 if (detach_hook)
1490 detach_hook ();
c906108c
SS
1491}
1492\f
c906108c
SS
1493/* Helper function for child_wait and the Lynx derivatives of child_wait.
1494 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1495 translation of that in OURSTATUS. */
1496void
fba45db2 1497store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c
SS
1498{
1499#ifdef CHILD_SPECIAL_WAITSTATUS
1500 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1501 if it wants to deal with hoststatus. */
1502 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1503 return;
1504#endif
1505
1506 if (WIFEXITED (hoststatus))
1507 {
1508 ourstatus->kind = TARGET_WAITKIND_EXITED;
1509 ourstatus->value.integer = WEXITSTATUS (hoststatus);
1510 }
1511 else if (!WIFSTOPPED (hoststatus))
1512 {
1513 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1514 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1515 }
1516 else
1517 {
1518 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1519 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1520 }
1521}
1522\f
c906108c 1523/* Returns zero to leave the inferior alone, one to interrupt it. */
507f3c78 1524int (*target_activity_function) (void);
c906108c
SS
1525int target_activity_fd;
1526\f
1527/* Convert a normal process ID to a string. Returns the string in a static
1528 buffer. */
1529
1530char *
39f77062 1531normal_pid_to_str (ptid_t ptid)
c906108c
SS
1532{
1533 static char buf[30];
1534
39f77062 1535 sprintf (buf, "process %d", PIDGET (ptid));
c906108c
SS
1536 return buf;
1537}
1538
1539/* Some targets (such as ttrace-based HPUX) don't allow us to request
1540 notification of inferior events such as fork and vork immediately
1541 after the inferior is created. (This because of how gdb gets an
1542 inferior created via invoking a shell to do it. In such a scenario,
1543 if the shell init file has commands in it, the shell will fork and
1544 exec for each of those commands, and we will see each such fork
1545 event. Very bad.)
c5aa993b 1546
c906108c
SS
1547 This function is used by all targets that allow us to request
1548 notification of forks, etc at inferior creation time; e.g., in
1549 target_acknowledge_forked_child.
c5aa993b 1550 */
392a587b 1551static void
39f77062 1552normal_target_post_startup_inferior (ptid_t ptid)
c906108c
SS
1553{
1554 /* This space intentionally left blank. */
1555}
1556
be4d1333
MS
1557/* Error-catcher for target_find_memory_regions */
1558/* ARGSUSED */
1559static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
1560{
1561 error ("No target.");
1562 return 0;
1563}
1564
1565/* Error-catcher for target_make_corefile_notes */
1566/* ARGSUSED */
1567static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
1568{
1569 error ("No target.");
1570 return NULL;
1571}
1572
c906108c
SS
1573/* Set up the handful of non-empty slots needed by the dummy target
1574 vector. */
1575
1576static void
fba45db2 1577init_dummy_target (void)
c906108c
SS
1578{
1579 dummy_target.to_shortname = "None";
1580 dummy_target.to_longname = "None";
1581 dummy_target.to_doc = "";
1582 dummy_target.to_attach = find_default_attach;
1583 dummy_target.to_require_attach = find_default_require_attach;
1584 dummy_target.to_require_detach = find_default_require_detach;
1585 dummy_target.to_create_inferior = find_default_create_inferior;
1586 dummy_target.to_clone_and_follow_inferior = find_default_clone_and_follow_inferior;
ed9a39eb 1587 dummy_target.to_pid_to_str = normal_pid_to_str;
c906108c 1588 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
1589 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
1590 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
c906108c
SS
1591 dummy_target.to_magic = OPS_MAGIC;
1592}
c906108c 1593\f
c5aa993b 1594
c906108c
SS
1595static struct target_ops debug_target;
1596
1597static void
fba45db2 1598debug_to_open (char *args, int from_tty)
c906108c
SS
1599{
1600 debug_target.to_open (args, from_tty);
1601
96baa820 1602 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
1603}
1604
1605static void
fba45db2 1606debug_to_close (int quitting)
c906108c
SS
1607{
1608 debug_target.to_close (quitting);
1609
96baa820 1610 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
c906108c
SS
1611}
1612
1613static void
fba45db2 1614debug_to_attach (char *args, int from_tty)
c906108c
SS
1615{
1616 debug_target.to_attach (args, from_tty);
1617
96baa820 1618 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n", args, from_tty);
c906108c
SS
1619}
1620
1621
1622static void
fba45db2 1623debug_to_post_attach (int pid)
c906108c
SS
1624{
1625 debug_target.to_post_attach (pid);
1626
96baa820 1627 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
1628}
1629
1630static void
fba45db2 1631debug_to_require_attach (char *args, int from_tty)
c906108c
SS
1632{
1633 debug_target.to_require_attach (args, from_tty);
1634
96baa820 1635 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
1636 "target_require_attach (%s, %d)\n", args, from_tty);
1637}
1638
1639static void
fba45db2 1640debug_to_detach (char *args, int from_tty)
c906108c
SS
1641{
1642 debug_target.to_detach (args, from_tty);
1643
96baa820 1644 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n", args, from_tty);
c906108c
SS
1645}
1646
1647static void
fba45db2 1648debug_to_require_detach (int pid, char *args, int from_tty)
c906108c
SS
1649{
1650 debug_target.to_require_detach (pid, args, from_tty);
1651
96baa820 1652 fprintf_unfiltered (gdb_stdlog,
c5aa993b 1653 "target_require_detach (%d, %s, %d)\n", pid, args, from_tty);
c906108c
SS
1654}
1655
1656static void
39f77062 1657debug_to_resume (ptid_t ptid, int step, enum target_signal siggnal)
c906108c 1658{
39f77062 1659 debug_target.to_resume (ptid, step, siggnal);
c906108c 1660
39f77062 1661 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", PIDGET (ptid),
c906108c
SS
1662 step ? "step" : "continue",
1663 target_signal_to_name (siggnal));
1664}
1665
39f77062
KB
1666static ptid_t
1667debug_to_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 1668{
39f77062 1669 ptid_t retval;
c906108c 1670
39f77062 1671 retval = debug_target.to_wait (ptid, status);
c906108c 1672
96baa820 1673 fprintf_unfiltered (gdb_stdlog,
39f77062
KB
1674 "target_wait (%d, status) = %d, ", PIDGET (ptid),
1675 PIDGET (retval));
96baa820 1676 fprintf_unfiltered (gdb_stdlog, "status->kind = ");
c906108c
SS
1677 switch (status->kind)
1678 {
1679 case TARGET_WAITKIND_EXITED:
96baa820 1680 fprintf_unfiltered (gdb_stdlog, "exited, status = %d\n",
c906108c
SS
1681 status->value.integer);
1682 break;
1683 case TARGET_WAITKIND_STOPPED:
96baa820 1684 fprintf_unfiltered (gdb_stdlog, "stopped, signal = %s\n",
c906108c
SS
1685 target_signal_to_name (status->value.sig));
1686 break;
1687 case TARGET_WAITKIND_SIGNALLED:
96baa820 1688 fprintf_unfiltered (gdb_stdlog, "signalled, signal = %s\n",
c906108c
SS
1689 target_signal_to_name (status->value.sig));
1690 break;
1691 case TARGET_WAITKIND_LOADED:
96baa820 1692 fprintf_unfiltered (gdb_stdlog, "loaded\n");
c906108c
SS
1693 break;
1694 case TARGET_WAITKIND_FORKED:
96baa820 1695 fprintf_unfiltered (gdb_stdlog, "forked\n");
c906108c
SS
1696 break;
1697 case TARGET_WAITKIND_VFORKED:
96baa820 1698 fprintf_unfiltered (gdb_stdlog, "vforked\n");
c906108c
SS
1699 break;
1700 case TARGET_WAITKIND_EXECD:
96baa820 1701 fprintf_unfiltered (gdb_stdlog, "execd\n");
c906108c
SS
1702 break;
1703 case TARGET_WAITKIND_SPURIOUS:
96baa820 1704 fprintf_unfiltered (gdb_stdlog, "spurious\n");
c906108c
SS
1705 break;
1706 default:
96baa820 1707 fprintf_unfiltered (gdb_stdlog, "unknown???\n");
c906108c
SS
1708 break;
1709 }
1710
1711 return retval;
1712}
1713
1714static void
39f77062 1715debug_to_post_wait (ptid_t ptid, int status)
c906108c 1716{
39f77062 1717 debug_target.to_post_wait (ptid, status);
c906108c 1718
96baa820 1719 fprintf_unfiltered (gdb_stdlog, "target_post_wait (%d, %d)\n",
39f77062 1720 PIDGET (ptid), status);
c906108c
SS
1721}
1722
bf0c5130
AC
1723static void
1724debug_print_register (const char * func, int regno)
1725{
1726 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1727 if (regno >= 0 && regno < NUM_REGS + NUM_PSEUDO_REGS
1728 && REGISTER_NAME (regno) != NULL && REGISTER_NAME (regno)[0] != '\0')
1729 fprintf_unfiltered (gdb_stdlog, "(%s)", REGISTER_NAME (regno));
1730 else
1731 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1732 if (regno >= 0)
1733 {
1734 int i;
1735 unsigned char *buf = alloca (MAX_REGISTER_RAW_SIZE);
1736 read_register_gen (regno, buf);
1737 fprintf_unfiltered (gdb_stdlog, " = ");
1738 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
1739 {
1740 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1741 }
1742 if (REGISTER_RAW_SIZE (regno) <= sizeof (LONGEST))
1743 {
1744 fprintf_unfiltered (gdb_stdlog, " 0x%s %s",
1745 paddr_nz (read_register (regno)),
1746 paddr_d (read_register (regno)));
1747 }
1748 }
1749 fprintf_unfiltered (gdb_stdlog, "\n");
1750}
1751
c906108c 1752static void
fba45db2 1753debug_to_fetch_registers (int regno)
c906108c
SS
1754{
1755 debug_target.to_fetch_registers (regno);
bf0c5130 1756 debug_print_register ("target_fetch_registers", regno);
c906108c
SS
1757}
1758
1759static void
fba45db2 1760debug_to_store_registers (int regno)
c906108c
SS
1761{
1762 debug_target.to_store_registers (regno);
bf0c5130
AC
1763 debug_print_register ("target_store_registers", regno);
1764 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
1765}
1766
1767static void
fba45db2 1768debug_to_prepare_to_store (void)
c906108c
SS
1769{
1770 debug_target.to_prepare_to_store ();
1771
96baa820 1772 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
1773}
1774
1775static int
fba45db2 1776debug_to_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
29e57380 1777 struct mem_attrib *attrib,
fba45db2 1778 struct target_ops *target)
c906108c
SS
1779{
1780 int retval;
1781
29e57380
C
1782 retval = debug_target.to_xfer_memory (memaddr, myaddr, len, write,
1783 attrib, target);
c906108c 1784
96baa820 1785 fprintf_unfiltered (gdb_stdlog,
c906108c 1786 "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
c5aa993b 1787 (unsigned int) memaddr, /* possable truncate long long */
c906108c
SS
1788 len, write ? "write" : "read", retval);
1789
c5aa993b 1790
c906108c
SS
1791
1792 if (retval > 0)
1793 {
1794 int i;
1795
96baa820 1796 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
1797 for (i = 0; i < retval; i++)
1798 {
1799 if ((((long) &(myaddr[i])) & 0xf) == 0)
96baa820
JM
1800 fprintf_unfiltered (gdb_stdlog, "\n");
1801 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
1802 }
1803 }
1804
96baa820 1805 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
1806
1807 return retval;
1808}
1809
1810static void
fba45db2 1811debug_to_files_info (struct target_ops *target)
c906108c
SS
1812{
1813 debug_target.to_files_info (target);
1814
96baa820 1815 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
1816}
1817
1818static int
fba45db2 1819debug_to_insert_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
1820{
1821 int retval;
1822
1823 retval = debug_target.to_insert_breakpoint (addr, save);
1824
96baa820 1825 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
1826 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
1827 (unsigned long) addr,
1828 (unsigned long) retval);
c906108c
SS
1829 return retval;
1830}
1831
1832static int
fba45db2 1833debug_to_remove_breakpoint (CORE_ADDR addr, char *save)
c906108c
SS
1834{
1835 int retval;
1836
1837 retval = debug_target.to_remove_breakpoint (addr, save);
1838
96baa820 1839 fprintf_unfiltered (gdb_stdlog,
104c1213
JM
1840 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
1841 (unsigned long) addr,
1842 (unsigned long) retval);
c906108c
SS
1843 return retval;
1844}
1845
ccaa32c7
GS
1846static int
1847debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
1848{
1849 int retval;
1850
1851 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
1852
1853 fprintf_unfiltered (gdb_stdlog,
1854 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
1855 (unsigned long) type,
1856 (unsigned long) cnt,
1857 (unsigned long) from_tty,
1858 (unsigned long) retval);
1859 return retval;
1860}
1861
1862static int
1863debug_to_region_size_ok_for_hw_watchpoint (int byte_count)
1864{
1865 CORE_ADDR retval;
1866
1867 retval = debug_target.to_region_size_ok_for_hw_watchpoint (byte_count);
1868
1869 fprintf_unfiltered (gdb_stdlog,
1870 "TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT (%ld) = 0x%lx\n",
1871 (unsigned long) byte_count,
1872 (unsigned long) retval);
1873 return retval;
1874}
1875
1876static int
1877debug_to_stopped_by_watchpoint (void)
1878{
1879 int retval;
1880
1881 retval = debug_target.to_stopped_by_watchpoint ();
1882
1883 fprintf_unfiltered (gdb_stdlog,
1884 "STOPPED_BY_WATCHPOINT () = %ld\n",
1885 (unsigned long) retval);
1886 return retval;
1887}
1888
1889static CORE_ADDR
1890debug_to_stopped_data_address (void)
1891{
1892 CORE_ADDR retval;
1893
1894 retval = debug_target.to_stopped_data_address ();
1895
1896 fprintf_unfiltered (gdb_stdlog,
1897 "target_stopped_data_address () = 0x%lx\n",
1898 (unsigned long) retval);
1899 return retval;
1900}
1901
1902static int
1903debug_to_insert_hw_breakpoint (CORE_ADDR addr, char *save)
1904{
1905 int retval;
1906
1907 retval = debug_target.to_insert_hw_breakpoint (addr, save);
1908
1909 fprintf_unfiltered (gdb_stdlog,
1910 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
1911 (unsigned long) addr,
1912 (unsigned long) retval);
1913 return retval;
1914}
1915
1916static int
1917debug_to_remove_hw_breakpoint (CORE_ADDR addr, char *save)
1918{
1919 int retval;
1920
1921 retval = debug_target.to_remove_hw_breakpoint (addr, save);
1922
1923 fprintf_unfiltered (gdb_stdlog,
1924 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
1925 (unsigned long) addr,
1926 (unsigned long) retval);
1927 return retval;
1928}
1929
1930static int
1931debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
1932{
1933 int retval;
1934
1935 retval = debug_target.to_insert_watchpoint (addr, len, type);
1936
1937 fprintf_unfiltered (gdb_stdlog,
1938 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
1939 (unsigned long) addr, len, type, (unsigned long) retval);
1940 return retval;
1941}
1942
1943static int
1944debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
1945{
1946 int retval;
1947
1948 retval = debug_target.to_insert_watchpoint (addr, len, type);
1949
1950 fprintf_unfiltered (gdb_stdlog,
1951 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
1952 (unsigned long) addr, len, type, (unsigned long) retval);
1953 return retval;
1954}
1955
c906108c 1956static void
fba45db2 1957debug_to_terminal_init (void)
c906108c
SS
1958{
1959 debug_target.to_terminal_init ();
1960
96baa820 1961 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
1962}
1963
1964static void
fba45db2 1965debug_to_terminal_inferior (void)
c906108c
SS
1966{
1967 debug_target.to_terminal_inferior ();
1968
96baa820 1969 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
1970}
1971
1972static void
fba45db2 1973debug_to_terminal_ours_for_output (void)
c906108c
SS
1974{
1975 debug_target.to_terminal_ours_for_output ();
1976
96baa820 1977 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
1978}
1979
1980static void
fba45db2 1981debug_to_terminal_ours (void)
c906108c
SS
1982{
1983 debug_target.to_terminal_ours ();
1984
96baa820 1985 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
1986}
1987
a790ad35
SC
1988static void
1989debug_to_terminal_save_ours (void)
1990{
1991 debug_target.to_terminal_save_ours ();
1992
1993 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
1994}
1995
c906108c 1996static void
fba45db2 1997debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
1998{
1999 debug_target.to_terminal_info (arg, from_tty);
2000
96baa820 2001 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
2002 from_tty);
2003}
2004
2005static void
fba45db2 2006debug_to_kill (void)
c906108c
SS
2007{
2008 debug_target.to_kill ();
2009
96baa820 2010 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
c906108c
SS
2011}
2012
2013static void
fba45db2 2014debug_to_load (char *args, int from_tty)
c906108c
SS
2015{
2016 debug_target.to_load (args, from_tty);
2017
96baa820 2018 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
2019}
2020
2021static int
fba45db2 2022debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
c906108c
SS
2023{
2024 int retval;
2025
2026 retval = debug_target.to_lookup_symbol (name, addrp);
2027
96baa820 2028 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
c906108c
SS
2029
2030 return retval;
2031}
2032
2033static void
fba45db2 2034debug_to_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
2035{
2036 debug_target.to_create_inferior (exec_file, args, env);
2037
96baa820 2038 fprintf_unfiltered (gdb_stdlog, "target_create_inferior (%s, %s, xxx)\n",
c906108c
SS
2039 exec_file, args);
2040}
2041
2042static void
39f77062 2043debug_to_post_startup_inferior (ptid_t ptid)
c906108c 2044{
39f77062 2045 debug_target.to_post_startup_inferior (ptid);
c906108c 2046
96baa820 2047 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 2048 PIDGET (ptid));
c906108c
SS
2049}
2050
2051static void
fba45db2 2052debug_to_acknowledge_created_inferior (int pid)
c906108c
SS
2053{
2054 debug_target.to_acknowledge_created_inferior (pid);
2055
96baa820 2056 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
c906108c
SS
2057 pid);
2058}
2059
2060static void
fba45db2 2061debug_to_clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c
SS
2062{
2063 debug_target.to_clone_and_follow_inferior (child_pid, followed_child);
2064
96baa820 2065 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2066 "target_clone_and_follow_inferior (%d, %d)\n",
2067 child_pid, *followed_child);
2068}
2069
2070static void
fba45db2 2071debug_to_post_follow_inferior_by_clone (void)
c906108c
SS
2072{
2073 debug_target.to_post_follow_inferior_by_clone ();
2074
96baa820 2075 fprintf_unfiltered (gdb_stdlog, "target_post_follow_inferior_by_clone ()\n");
c906108c
SS
2076}
2077
2078static int
fba45db2 2079debug_to_insert_fork_catchpoint (int pid)
c906108c 2080{
c5aa993b 2081 int retval;
c906108c
SS
2082
2083 retval = debug_target.to_insert_fork_catchpoint (pid);
2084
96baa820 2085 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
c5aa993b 2086 pid, retval);
c906108c
SS
2087
2088 return retval;
2089}
2090
2091static int
fba45db2 2092debug_to_remove_fork_catchpoint (int pid)
c906108c 2093{
c5aa993b 2094 int retval;
c906108c
SS
2095
2096 retval = debug_target.to_remove_fork_catchpoint (pid);
2097
96baa820 2098 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 2099 pid, retval);
c906108c
SS
2100
2101 return retval;
2102}
2103
2104static int
fba45db2 2105debug_to_insert_vfork_catchpoint (int pid)
c906108c 2106{
c5aa993b 2107 int retval;
c906108c
SS
2108
2109 retval = debug_target.to_insert_vfork_catchpoint (pid);
2110
96baa820 2111 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)= %d\n",
c5aa993b 2112 pid, retval);
c906108c
SS
2113
2114 return retval;
2115}
2116
2117static int
fba45db2 2118debug_to_remove_vfork_catchpoint (int pid)
c906108c 2119{
c5aa993b 2120 int retval;
c906108c
SS
2121
2122 retval = debug_target.to_remove_vfork_catchpoint (pid);
2123
96baa820 2124 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 2125 pid, retval);
c906108c
SS
2126
2127 return retval;
2128}
2129
2130static int
fba45db2 2131debug_to_has_forked (int pid, int *child_pid)
c906108c 2132{
c5aa993b 2133 int has_forked;
c906108c
SS
2134
2135 has_forked = debug_target.to_has_forked (pid, child_pid);
2136
96baa820 2137 fprintf_unfiltered (gdb_stdlog, "target_has_forked (%d, %d) = %d\n",
c5aa993b 2138 pid, *child_pid, has_forked);
c906108c
SS
2139
2140 return has_forked;
2141}
2142
2143static int
fba45db2 2144debug_to_has_vforked (int pid, int *child_pid)
c906108c 2145{
c5aa993b 2146 int has_vforked;
c906108c
SS
2147
2148 has_vforked = debug_target.to_has_vforked (pid, child_pid);
2149
96baa820 2150 fprintf_unfiltered (gdb_stdlog, "target_has_vforked (%d, %d) = %d\n",
c5aa993b 2151 pid, *child_pid, has_vforked);
c906108c
SS
2152
2153 return has_vforked;
2154}
2155
2156static int
fba45db2 2157debug_to_can_follow_vfork_prior_to_exec (void)
c906108c 2158{
c5aa993b 2159 int can_immediately_follow_vfork;
c906108c
SS
2160
2161 can_immediately_follow_vfork = debug_target.to_can_follow_vfork_prior_to_exec ();
2162
96baa820 2163 fprintf_unfiltered (gdb_stdlog, "target_can_follow_vfork_prior_to_exec () = %d\n",
c5aa993b 2164 can_immediately_follow_vfork);
c906108c
SS
2165
2166 return can_immediately_follow_vfork;
2167}
2168
2169static void
fba45db2
KB
2170debug_to_post_follow_vfork (int parent_pid, int followed_parent, int child_pid,
2171 int followed_child)
c906108c
SS
2172{
2173 debug_target.to_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child);
2174
96baa820 2175 fprintf_unfiltered (gdb_stdlog,
c906108c 2176 "target_post_follow_vfork (%d, %d, %d, %d)\n",
c5aa993b 2177 parent_pid, followed_parent, child_pid, followed_child);
c906108c
SS
2178}
2179
2180static int
fba45db2 2181debug_to_insert_exec_catchpoint (int pid)
c906108c 2182{
c5aa993b 2183 int retval;
c906108c
SS
2184
2185 retval = debug_target.to_insert_exec_catchpoint (pid);
2186
96baa820 2187 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
c5aa993b 2188 pid, retval);
c906108c
SS
2189
2190 return retval;
2191}
2192
2193static int
fba45db2 2194debug_to_remove_exec_catchpoint (int pid)
c906108c 2195{
c5aa993b 2196 int retval;
c906108c
SS
2197
2198 retval = debug_target.to_remove_exec_catchpoint (pid);
2199
96baa820 2200 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 2201 pid, retval);
c906108c
SS
2202
2203 return retval;
2204}
2205
2206static int
fba45db2 2207debug_to_has_execd (int pid, char **execd_pathname)
c906108c 2208{
c5aa993b 2209 int has_execd;
c906108c
SS
2210
2211 has_execd = debug_target.to_has_execd (pid, execd_pathname);
2212
96baa820 2213 fprintf_unfiltered (gdb_stdlog, "target_has_execd (%d, %s) = %d\n",
c5aa993b 2214 pid, (*execd_pathname ? *execd_pathname : "<NULL>"),
7a292a7a 2215 has_execd);
c906108c
SS
2216
2217 return has_execd;
2218}
2219
2220static int
fba45db2 2221debug_to_reported_exec_events_per_exec_call (void)
c906108c 2222{
c5aa993b 2223 int reported_exec_events;
c906108c
SS
2224
2225 reported_exec_events = debug_target.to_reported_exec_events_per_exec_call ();
2226
96baa820 2227 fprintf_unfiltered (gdb_stdlog,
c906108c 2228 "target_reported_exec_events_per_exec_call () = %d\n",
c5aa993b 2229 reported_exec_events);
c906108c
SS
2230
2231 return reported_exec_events;
2232}
2233
2234static int
fba45db2
KB
2235debug_to_has_syscall_event (int pid, enum target_waitkind *kind,
2236 int *syscall_id)
c906108c 2237{
c5aa993b
JM
2238 int has_syscall_event;
2239 char *kind_spelling = "??";
c906108c
SS
2240
2241 has_syscall_event = debug_target.to_has_syscall_event (pid, kind, syscall_id);
2242 if (has_syscall_event)
2243 {
2244 switch (*kind)
c5aa993b
JM
2245 {
2246 case TARGET_WAITKIND_SYSCALL_ENTRY:
2247 kind_spelling = "SYSCALL_ENTRY";
2248 break;
2249 case TARGET_WAITKIND_SYSCALL_RETURN:
2250 kind_spelling = "SYSCALL_RETURN";
2251 break;
2252 default:
2253 break;
2254 }
c906108c
SS
2255 }
2256
96baa820 2257 fprintf_unfiltered (gdb_stdlog,
c906108c 2258 "target_has_syscall_event (%d, %s, %d) = %d\n",
c5aa993b 2259 pid, kind_spelling, *syscall_id, has_syscall_event);
c906108c
SS
2260
2261 return has_syscall_event;
2262}
2263
2264static int
fba45db2 2265debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 2266{
c5aa993b 2267 int has_exited;
c906108c
SS
2268
2269 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
2270
96baa820 2271 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 2272 pid, wait_status, *exit_status, has_exited);
c906108c
SS
2273
2274 return has_exited;
2275}
2276
2277static void
fba45db2 2278debug_to_mourn_inferior (void)
c906108c
SS
2279{
2280 debug_target.to_mourn_inferior ();
2281
96baa820 2282 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
c906108c
SS
2283}
2284
2285static int
fba45db2 2286debug_to_can_run (void)
c906108c
SS
2287{
2288 int retval;
2289
2290 retval = debug_target.to_can_run ();
2291
96baa820 2292 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
2293
2294 return retval;
2295}
2296
2297static void
39f77062 2298debug_to_notice_signals (ptid_t ptid)
c906108c 2299{
39f77062 2300 debug_target.to_notice_signals (ptid);
c906108c 2301
39f77062
KB
2302 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
2303 PIDGET (ptid));
c906108c
SS
2304}
2305
2306static int
39f77062 2307debug_to_thread_alive (ptid_t ptid)
c906108c
SS
2308{
2309 int retval;
2310
39f77062 2311 retval = debug_target.to_thread_alive (ptid);
c906108c 2312
96baa820 2313 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
39f77062 2314 PIDGET (ptid), retval);
c906108c
SS
2315
2316 return retval;
2317}
2318
0d06e24b 2319static void
fba45db2 2320debug_to_find_new_threads (void)
0d06e24b
JM
2321{
2322 debug_target.to_find_new_threads ();
2323
2324 fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
2325}
2326
c906108c 2327static void
fba45db2 2328debug_to_stop (void)
c906108c
SS
2329{
2330 debug_target.to_stop ();
2331
96baa820 2332 fprintf_unfiltered (gdb_stdlog, "target_stop ()\n");
c906108c
SS
2333}
2334
2335static int
fba45db2 2336debug_to_query (int type, char *req, char *resp, int *siz)
c906108c
SS
2337{
2338 int retval;
2339
2340 retval = debug_target.to_query (type, req, resp, siz);
2341
96baa820 2342 fprintf_unfiltered (gdb_stdlog, "target_query (%c, %s, %s, %d) = %d\n", type, req, resp, *siz, retval);
c906108c
SS
2343
2344 return retval;
2345}
2346
96baa820
JM
2347static void
2348debug_to_rcmd (char *command,
d9fcf2fb 2349 struct ui_file *outbuf)
96baa820
JM
2350{
2351 debug_target.to_rcmd (command, outbuf);
2352 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
2353}
2354
c906108c 2355static struct symtab_and_line *
fba45db2 2356debug_to_enable_exception_callback (enum exception_event_kind kind, int enable)
c906108c 2357{
7a292a7a
SS
2358 struct symtab_and_line *result;
2359 result = debug_target.to_enable_exception_callback (kind, enable);
96baa820 2360 fprintf_unfiltered (gdb_stdlog,
c906108c
SS
2361 "target get_exception_callback_sal (%d, %d)\n",
2362 kind, enable);
7a292a7a 2363 return result;
c906108c
SS
2364}
2365
2366static struct exception_event_record *
fba45db2 2367debug_to_get_current_exception_event (void)
c906108c 2368{
7a292a7a 2369 struct exception_event_record *result;
c5aa993b 2370 result = debug_target.to_get_current_exception_event ();
96baa820 2371 fprintf_unfiltered (gdb_stdlog, "target get_current_exception_event ()\n");
7a292a7a 2372 return result;
c906108c
SS
2373}
2374
2375static char *
fba45db2 2376debug_to_pid_to_exec_file (int pid)
c906108c 2377{
c5aa993b 2378 char *exec_file;
c906108c
SS
2379
2380 exec_file = debug_target.to_pid_to_exec_file (pid);
2381
96baa820 2382 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 2383 pid, exec_file);
c906108c
SS
2384
2385 return exec_file;
2386}
2387
c906108c 2388static void
fba45db2 2389setup_target_debug (void)
c906108c
SS
2390{
2391 memcpy (&debug_target, &current_target, sizeof debug_target);
2392
2393 current_target.to_open = debug_to_open;
2394 current_target.to_close = debug_to_close;
2395 current_target.to_attach = debug_to_attach;
2396 current_target.to_post_attach = debug_to_post_attach;
2397 current_target.to_require_attach = debug_to_require_attach;
2398 current_target.to_detach = debug_to_detach;
2399 current_target.to_require_detach = debug_to_require_detach;
2400 current_target.to_resume = debug_to_resume;
2401 current_target.to_wait = debug_to_wait;
2402 current_target.to_post_wait = debug_to_post_wait;
2403 current_target.to_fetch_registers = debug_to_fetch_registers;
2404 current_target.to_store_registers = debug_to_store_registers;
2405 current_target.to_prepare_to_store = debug_to_prepare_to_store;
2406 current_target.to_xfer_memory = debug_to_xfer_memory;
2407 current_target.to_files_info = debug_to_files_info;
2408 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2409 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
2410 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
2411 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
2412 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
2413 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
2414 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
2415 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
2416 current_target.to_stopped_data_address = debug_to_stopped_data_address;
2417 current_target.to_region_size_ok_for_hw_watchpoint = debug_to_region_size_ok_for_hw_watchpoint;
c906108c
SS
2418 current_target.to_terminal_init = debug_to_terminal_init;
2419 current_target.to_terminal_inferior = debug_to_terminal_inferior;
2420 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2421 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 2422 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c
SS
2423 current_target.to_terminal_info = debug_to_terminal_info;
2424 current_target.to_kill = debug_to_kill;
2425 current_target.to_load = debug_to_load;
2426 current_target.to_lookup_symbol = debug_to_lookup_symbol;
2427 current_target.to_create_inferior = debug_to_create_inferior;
2428 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
2429 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
2430 current_target.to_clone_and_follow_inferior = debug_to_clone_and_follow_inferior;
2431 current_target.to_post_follow_inferior_by_clone = debug_to_post_follow_inferior_by_clone;
2432 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
2433 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
2434 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
2435 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
2436 current_target.to_has_forked = debug_to_has_forked;
2437 current_target.to_has_vforked = debug_to_has_vforked;
2438 current_target.to_can_follow_vfork_prior_to_exec = debug_to_can_follow_vfork_prior_to_exec;
2439 current_target.to_post_follow_vfork = debug_to_post_follow_vfork;
2440 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
2441 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
2442 current_target.to_has_execd = debug_to_has_execd;
2443 current_target.to_reported_exec_events_per_exec_call = debug_to_reported_exec_events_per_exec_call;
2444 current_target.to_has_syscall_event = debug_to_has_syscall_event;
2445 current_target.to_has_exited = debug_to_has_exited;
2446 current_target.to_mourn_inferior = debug_to_mourn_inferior;
2447 current_target.to_can_run = debug_to_can_run;
2448 current_target.to_notice_signals = debug_to_notice_signals;
2449 current_target.to_thread_alive = debug_to_thread_alive;
0d06e24b 2450 current_target.to_find_new_threads = debug_to_find_new_threads;
c906108c
SS
2451 current_target.to_stop = debug_to_stop;
2452 current_target.to_query = debug_to_query;
96baa820 2453 current_target.to_rcmd = debug_to_rcmd;
c906108c
SS
2454 current_target.to_enable_exception_callback = debug_to_enable_exception_callback;
2455 current_target.to_get_current_exception_event = debug_to_get_current_exception_event;
2456 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c906108c
SS
2457
2458}
c906108c 2459\f
c5aa993b
JM
2460
2461static char targ_desc[] =
2462"Names of targets and files being debugged.\n\
c906108c
SS
2463Shows the entire stack of targets currently in use (including the exec-file,\n\
2464core-file, and process, if any), as well as the symbol file name.";
2465
96baa820
JM
2466static void
2467do_monitor_command (char *cmd,
2468 int from_tty)
2469{
2b5fe715
AC
2470 if ((current_target.to_rcmd
2471 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 2472 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
2473 && (debug_target.to_rcmd
2474 == (void (*) (char *, struct ui_file *)) tcomplain)))
96baa820
JM
2475 {
2476 error ("\"monitor\" command not supported by this target.\n");
2477 }
2478 target_rcmd (cmd, gdb_stdtarg);
2479}
2480
c906108c 2481void
fba45db2 2482initialize_targets (void)
c906108c
SS
2483{
2484 init_dummy_target ();
2485 push_target (&dummy_target);
2486
2487 add_info ("target", target_info, targ_desc);
2488 add_info ("files", target_info, targ_desc);
2489
3a11626d
MS
2490 add_show_from_set
2491 (add_set_cmd ("target", class_maintenance, var_zinteger,
2492 (char *) &targetdebug,
2493 "Set target debugging.\n\
5d161b24 2494When non-zero, target debugging is enabled.", &setdebuglist),
3a11626d
MS
2495 &showdebuglist);
2496
e707bbc2
AC
2497 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
2498 &trust_readonly, "\
2499Set mode for reading from readonly sections.\n\
3a11626d
MS
2500When this mode is on, memory reads from readonly sections (such as .text)\n\
2501will be read from the object file instead of from the target. This will\n\
e707bbc2 2502result in significant performance improvement for remote targets.", "\
c0e624e7 2503Show mode for reading from readonly sections.\n",
e707bbc2
AC
2504 NULL, NULL,
2505 &setlist, &showlist);
96baa820
JM
2506
2507 add_com ("monitor", class_obscure, do_monitor_command,
2508 "Send a command to the remote monitor (remote targets only).");
2509
8add0441 2510 target_dcache = dcache_init ();
c906108c 2511}
This page took 0.39619 seconds and 4 git commands to generate.