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