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