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