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