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