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