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