2009-08-14 Pedro Alves <pedro@codesourcery.com>
[binutils-gdb.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
6
7 Contributed by Cygnus Support.
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23
24 #include "defs.h"
25 #include <errno.h>
26 #include "gdb_string.h"
27 #include "target.h"
28 #include "gdbcmd.h"
29 #include "symtab.h"
30 #include "inferior.h"
31 #include "bfd.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "gdb_wait.h"
35 #include "dcache.h"
36 #include <signal.h>
37 #include "regcache.h"
38 #include "gdb_assert.h"
39 #include "gdbcore.h"
40 #include "exceptions.h"
41 #include "target-descriptions.h"
42 #include "gdbthread.h"
43 #include "solib.h"
44 #include "exec.h"
45 #include "inline-frame.h"
46
47 static void target_info (char *, int);
48
49 static void kill_or_be_killed (int);
50
51 static void default_terminal_info (char *, int);
52
53 static int default_watchpoint_addr_within_range (struct target_ops *,
54 CORE_ADDR, CORE_ADDR, int);
55
56 static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
57
58 static int nosymbol (char *, CORE_ADDR *);
59
60 static void tcomplain (void) ATTR_NORETURN;
61
62 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
63
64 static int return_zero (void);
65
66 static int return_one (void);
67
68 static int return_minus_one (void);
69
70 void target_ignore (void);
71
72 static void target_command (char *, int);
73
74 static struct target_ops *find_default_run_target (char *);
75
76 static void nosupport_runtime (void);
77
78 static LONGEST default_xfer_partial (struct target_ops *ops,
79 enum target_object object,
80 const char *annex, gdb_byte *readbuf,
81 const gdb_byte *writebuf,
82 ULONGEST offset, LONGEST len);
83
84 static LONGEST current_xfer_partial (struct target_ops *ops,
85 enum target_object object,
86 const char *annex, gdb_byte *readbuf,
87 const gdb_byte *writebuf,
88 ULONGEST offset, LONGEST len);
89
90 static LONGEST target_xfer_partial (struct target_ops *ops,
91 enum target_object object,
92 const char *annex,
93 void *readbuf, const void *writebuf,
94 ULONGEST offset, LONGEST len);
95
96 static struct gdbarch *default_thread_architecture (struct target_ops *ops,
97 ptid_t ptid);
98
99 static void init_dummy_target (void);
100
101 static struct target_ops debug_target;
102
103 static void debug_to_open (char *, int);
104
105 static void debug_to_prepare_to_store (struct regcache *);
106
107 static void debug_to_files_info (struct target_ops *);
108
109 static int debug_to_insert_breakpoint (struct gdbarch *,
110 struct bp_target_info *);
111
112 static int debug_to_remove_breakpoint (struct gdbarch *,
113 struct bp_target_info *);
114
115 static int debug_to_can_use_hw_breakpoint (int, int, int);
116
117 static int debug_to_insert_hw_breakpoint (struct gdbarch *,
118 struct bp_target_info *);
119
120 static int debug_to_remove_hw_breakpoint (struct gdbarch *,
121 struct bp_target_info *);
122
123 static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
124
125 static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
126
127 static int debug_to_stopped_by_watchpoint (void);
128
129 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
130
131 static int debug_to_watchpoint_addr_within_range (struct target_ops *,
132 CORE_ADDR, CORE_ADDR, int);
133
134 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
135
136 static void debug_to_terminal_init (void);
137
138 static void debug_to_terminal_inferior (void);
139
140 static void debug_to_terminal_ours_for_output (void);
141
142 static void debug_to_terminal_save_ours (void);
143
144 static void debug_to_terminal_ours (void);
145
146 static void debug_to_terminal_info (char *, int);
147
148 static void debug_to_load (char *, int);
149
150 static int debug_to_lookup_symbol (char *, CORE_ADDR *);
151
152 static int debug_to_can_run (void);
153
154 static void debug_to_notice_signals (ptid_t);
155
156 static void debug_to_stop (ptid_t);
157
158 /* NOTE: cagney/2004-09-29: Many targets reference this variable in
159 wierd and mysterious ways. Putting the variable here lets those
160 wierd and mysterious ways keep building while they are being
161 converted to the inferior inheritance structure. */
162 struct target_ops deprecated_child_ops;
163
164 /* Pointer to array of target architecture structures; the size of the
165 array; the current index into the array; the allocated size of the
166 array. */
167 struct target_ops **target_structs;
168 unsigned target_struct_size;
169 unsigned target_struct_index;
170 unsigned target_struct_allocsize;
171 #define DEFAULT_ALLOCSIZE 10
172
173 /* The initial current target, so that there is always a semi-valid
174 current target. */
175
176 static struct target_ops dummy_target;
177
178 /* Top of target stack. */
179
180 static struct target_ops *target_stack;
181
182 /* The target structure we are currently using to talk to a process
183 or file or whatever "inferior" we have. */
184
185 struct target_ops current_target;
186
187 /* Command list for target. */
188
189 static struct cmd_list_element *targetlist = NULL;
190
191 /* Nonzero if we should trust readonly sections from the
192 executable when reading memory. */
193
194 static int trust_readonly = 0;
195
196 /* Nonzero if we should show true memory content including
197 memory breakpoint inserted by gdb. */
198
199 static int show_memory_breakpoints = 0;
200
201 /* Non-zero if we want to see trace of target level stuff. */
202
203 static int targetdebug = 0;
204 static void
205 show_targetdebug (struct ui_file *file, int from_tty,
206 struct cmd_list_element *c, const char *value)
207 {
208 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
209 }
210
211 static void setup_target_debug (void);
212
213 DCACHE *target_dcache;
214
215 /* The user just typed 'target' without the name of a target. */
216
217 static void
218 target_command (char *arg, int from_tty)
219 {
220 fputs_filtered ("Argument required (target name). Try `help target'\n",
221 gdb_stdout);
222 }
223
224 /* Default target_has_* methods for process_stratum targets. */
225
226 int
227 default_child_has_all_memory (struct target_ops *ops)
228 {
229 /* If no inferior selected, then we can't read memory here. */
230 if (ptid_equal (inferior_ptid, null_ptid))
231 return 0;
232
233 return 1;
234 }
235
236 int
237 default_child_has_memory (struct target_ops *ops)
238 {
239 /* If no inferior selected, then we can't read memory here. */
240 if (ptid_equal (inferior_ptid, null_ptid))
241 return 0;
242
243 return 1;
244 }
245
246 int
247 default_child_has_stack (struct target_ops *ops)
248 {
249 /* If no inferior selected, there's no stack. */
250 if (ptid_equal (inferior_ptid, null_ptid))
251 return 0;
252
253 return 1;
254 }
255
256 int
257 default_child_has_registers (struct target_ops *ops)
258 {
259 /* Can't read registers from no inferior. */
260 if (ptid_equal (inferior_ptid, null_ptid))
261 return 0;
262
263 return 1;
264 }
265
266 int
267 default_child_has_execution (struct target_ops *ops)
268 {
269 /* If there's no thread selected, then we can't make it run through
270 hoops. */
271 if (ptid_equal (inferior_ptid, null_ptid))
272 return 0;
273
274 return 1;
275 }
276
277
278 int
279 target_has_all_memory_1 (void)
280 {
281 struct target_ops *t;
282
283 for (t = current_target.beneath; t != NULL; t = t->beneath)
284 if (t->to_has_all_memory (t))
285 return 1;
286
287 return 0;
288 }
289
290 int
291 target_has_memory_1 (void)
292 {
293 struct target_ops *t;
294
295 for (t = current_target.beneath; t != NULL; t = t->beneath)
296 if (t->to_has_memory (t))
297 return 1;
298
299 return 0;
300 }
301
302 int
303 target_has_stack_1 (void)
304 {
305 struct target_ops *t;
306
307 for (t = current_target.beneath; t != NULL; t = t->beneath)
308 if (t->to_has_stack (t))
309 return 1;
310
311 return 0;
312 }
313
314 int
315 target_has_registers_1 (void)
316 {
317 struct target_ops *t;
318
319 for (t = current_target.beneath; t != NULL; t = t->beneath)
320 if (t->to_has_registers (t))
321 return 1;
322
323 return 0;
324 }
325
326 int
327 target_has_execution_1 (void)
328 {
329 struct target_ops *t;
330
331 for (t = current_target.beneath; t != NULL; t = t->beneath)
332 if (t->to_has_execution (t))
333 return 1;
334
335 return 0;
336 }
337
338 /* Add a possible target architecture to the list. */
339
340 void
341 add_target (struct target_ops *t)
342 {
343 /* Provide default values for all "must have" methods. */
344 if (t->to_xfer_partial == NULL)
345 t->to_xfer_partial = default_xfer_partial;
346
347 if (t->to_has_all_memory == NULL)
348 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
349
350 if (t->to_has_memory == NULL)
351 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
352
353 if (t->to_has_stack == NULL)
354 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
355
356 if (t->to_has_registers == NULL)
357 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
358
359 if (t->to_has_execution == NULL)
360 t->to_has_execution = (int (*) (struct target_ops *)) return_zero;
361
362 if (!target_structs)
363 {
364 target_struct_allocsize = DEFAULT_ALLOCSIZE;
365 target_structs = (struct target_ops **) xmalloc
366 (target_struct_allocsize * sizeof (*target_structs));
367 }
368 if (target_struct_size >= target_struct_allocsize)
369 {
370 target_struct_allocsize *= 2;
371 target_structs = (struct target_ops **)
372 xrealloc ((char *) target_structs,
373 target_struct_allocsize * sizeof (*target_structs));
374 }
375 target_structs[target_struct_size++] = t;
376
377 if (targetlist == NULL)
378 add_prefix_cmd ("target", class_run, target_command, _("\
379 Connect to a target machine or process.\n\
380 The first argument is the type or protocol of the target machine.\n\
381 Remaining arguments are interpreted by the target protocol. For more\n\
382 information on the arguments for a particular protocol, type\n\
383 `help target ' followed by the protocol name."),
384 &targetlist, "target ", 0, &cmdlist);
385 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
386 }
387
388 /* Stub functions */
389
390 void
391 target_ignore (void)
392 {
393 }
394
395 void
396 target_kill (void)
397 {
398 struct target_ops *t;
399
400 for (t = current_target.beneath; t != NULL; t = t->beneath)
401 if (t->to_kill != NULL)
402 {
403 if (targetdebug)
404 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
405
406 t->to_kill (t);
407 return;
408 }
409
410 noprocess ();
411 }
412
413 void
414 target_load (char *arg, int from_tty)
415 {
416 dcache_invalidate (target_dcache);
417 (*current_target.to_load) (arg, from_tty);
418 }
419
420 void
421 target_create_inferior (char *exec_file, char *args,
422 char **env, int from_tty)
423 {
424 struct target_ops *t;
425 for (t = current_target.beneath; t != NULL; t = t->beneath)
426 {
427 if (t->to_create_inferior != NULL)
428 {
429 t->to_create_inferior (t, exec_file, args, env, from_tty);
430 if (targetdebug)
431 fprintf_unfiltered (gdb_stdlog,
432 "target_create_inferior (%s, %s, xxx, %d)\n",
433 exec_file, args, from_tty);
434 return;
435 }
436 }
437
438 internal_error (__FILE__, __LINE__,
439 "could not find a target to create inferior");
440 }
441
442 void
443 target_terminal_inferior (void)
444 {
445 /* A background resume (``run&'') should leave GDB in control of the
446 terminal. */
447 if (target_is_async_p () && !sync_execution)
448 return;
449
450 /* If GDB is resuming the inferior in the foreground, install
451 inferior's terminal modes. */
452 (*current_target.to_terminal_inferior) ();
453 }
454
455 static int
456 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
457 struct target_ops *t)
458 {
459 errno = EIO; /* Can't read/write this location */
460 return 0; /* No bytes handled */
461 }
462
463 static void
464 tcomplain (void)
465 {
466 error (_("You can't do that when your target is `%s'"),
467 current_target.to_shortname);
468 }
469
470 void
471 noprocess (void)
472 {
473 error (_("You can't do that without a process to debug."));
474 }
475
476 static int
477 nosymbol (char *name, CORE_ADDR *addrp)
478 {
479 return 1; /* Symbol does not exist in target env */
480 }
481
482 static void
483 nosupport_runtime (void)
484 {
485 if (ptid_equal (inferior_ptid, null_ptid))
486 noprocess ();
487 else
488 error (_("No run-time support for this"));
489 }
490
491
492 static void
493 default_terminal_info (char *args, int from_tty)
494 {
495 printf_unfiltered (_("No saved terminal information.\n"));
496 }
497
498 /* This is the default target_create_inferior and target_attach function.
499 If the current target is executing, it asks whether to kill it off.
500 If this function returns without calling error(), it has killed off
501 the target, and the operation should be attempted. */
502
503 static void
504 kill_or_be_killed (int from_tty)
505 {
506 if (target_has_execution)
507 {
508 printf_unfiltered (_("You are already running a program:\n"));
509 target_files_info ();
510 if (query (_("Kill it? ")))
511 {
512 target_kill ();
513 if (target_has_execution)
514 error (_("Killing the program did not help."));
515 return;
516 }
517 else
518 {
519 error (_("Program not killed."));
520 }
521 }
522 tcomplain ();
523 }
524
525 /* A default implementation for the to_get_ada_task_ptid target method.
526
527 This function builds the PTID by using both LWP and TID as part of
528 the PTID lwp and tid elements. The pid used is the pid of the
529 inferior_ptid. */
530
531 static ptid_t
532 default_get_ada_task_ptid (long lwp, long tid)
533 {
534 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
535 }
536
537 /* Go through the target stack from top to bottom, copying over zero
538 entries in current_target, then filling in still empty entries. In
539 effect, we are doing class inheritance through the pushed target
540 vectors.
541
542 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
543 is currently implemented, is that it discards any knowledge of
544 which target an inherited method originally belonged to.
545 Consequently, new new target methods should instead explicitly and
546 locally search the target stack for the target that can handle the
547 request. */
548
549 static void
550 update_current_target (void)
551 {
552 struct target_ops *t;
553
554 /* First, reset current's contents. */
555 memset (&current_target, 0, sizeof (current_target));
556
557 #define INHERIT(FIELD, TARGET) \
558 if (!current_target.FIELD) \
559 current_target.FIELD = (TARGET)->FIELD
560
561 for (t = target_stack; t; t = t->beneath)
562 {
563 INHERIT (to_shortname, t);
564 INHERIT (to_longname, t);
565 INHERIT (to_doc, t);
566 /* Do not inherit to_open. */
567 /* Do not inherit to_close. */
568 /* Do not inherit to_attach. */
569 INHERIT (to_post_attach, t);
570 INHERIT (to_attach_no_wait, t);
571 /* Do not inherit to_detach. */
572 /* Do not inherit to_disconnect. */
573 /* Do not inherit to_resume. */
574 /* Do not inherit to_wait. */
575 /* Do not inherit to_fetch_registers. */
576 /* Do not inherit to_store_registers. */
577 INHERIT (to_prepare_to_store, t);
578 INHERIT (deprecated_xfer_memory, t);
579 INHERIT (to_files_info, t);
580 INHERIT (to_insert_breakpoint, t);
581 INHERIT (to_remove_breakpoint, t);
582 INHERIT (to_can_use_hw_breakpoint, t);
583 INHERIT (to_insert_hw_breakpoint, t);
584 INHERIT (to_remove_hw_breakpoint, t);
585 INHERIT (to_insert_watchpoint, t);
586 INHERIT (to_remove_watchpoint, t);
587 INHERIT (to_stopped_data_address, t);
588 INHERIT (to_have_steppable_watchpoint, t);
589 INHERIT (to_have_continuable_watchpoint, t);
590 INHERIT (to_stopped_by_watchpoint, t);
591 INHERIT (to_watchpoint_addr_within_range, t);
592 INHERIT (to_region_ok_for_hw_watchpoint, t);
593 INHERIT (to_terminal_init, t);
594 INHERIT (to_terminal_inferior, t);
595 INHERIT (to_terminal_ours_for_output, t);
596 INHERIT (to_terminal_ours, t);
597 INHERIT (to_terminal_save_ours, t);
598 INHERIT (to_terminal_info, t);
599 /* Do not inherit to_kill. */
600 INHERIT (to_load, t);
601 INHERIT (to_lookup_symbol, t);
602 /* Do no inherit to_create_inferior. */
603 INHERIT (to_post_startup_inferior, t);
604 INHERIT (to_acknowledge_created_inferior, t);
605 INHERIT (to_insert_fork_catchpoint, t);
606 INHERIT (to_remove_fork_catchpoint, t);
607 INHERIT (to_insert_vfork_catchpoint, t);
608 INHERIT (to_remove_vfork_catchpoint, t);
609 /* Do not inherit to_follow_fork. */
610 INHERIT (to_insert_exec_catchpoint, t);
611 INHERIT (to_remove_exec_catchpoint, t);
612 INHERIT (to_has_exited, t);
613 /* Do not inherit to_mourn_inferiour. */
614 INHERIT (to_can_run, t);
615 INHERIT (to_notice_signals, t);
616 /* Do not inherit to_thread_alive. */
617 /* Do not inherit to_find_new_threads. */
618 /* Do not inherit to_pid_to_str. */
619 INHERIT (to_extra_thread_info, t);
620 INHERIT (to_stop, t);
621 /* Do not inherit to_xfer_partial. */
622 INHERIT (to_rcmd, t);
623 INHERIT (to_pid_to_exec_file, t);
624 INHERIT (to_log_command, t);
625 INHERIT (to_stratum, t);
626 /* Do not inherit to_has_all_memory */
627 /* Do not inherit to_has_memory */
628 /* Do not inherit to_has_stack */
629 /* Do not inherit to_has_registers */
630 /* Do not inherit to_has_execution */
631 INHERIT (to_has_thread_control, t);
632 INHERIT (to_can_async_p, t);
633 INHERIT (to_is_async_p, t);
634 INHERIT (to_async, t);
635 INHERIT (to_async_mask, t);
636 INHERIT (to_find_memory_regions, t);
637 INHERIT (to_make_corefile_notes, t);
638 /* Do not inherit to_get_thread_local_address. */
639 INHERIT (to_can_execute_reverse, t);
640 INHERIT (to_thread_architecture, t);
641 /* Do not inherit to_read_description. */
642 INHERIT (to_get_ada_task_ptid, t);
643 /* Do not inherit to_search_memory. */
644 INHERIT (to_supports_multi_process, t);
645 INHERIT (to_magic, t);
646 /* Do not inherit to_memory_map. */
647 /* Do not inherit to_flash_erase. */
648 /* Do not inherit to_flash_done. */
649 }
650 #undef INHERIT
651
652 /* Clean up a target struct so it no longer has any zero pointers in
653 it. Some entries are defaulted to a method that print an error,
654 others are hard-wired to a standard recursive default. */
655
656 #define de_fault(field, value) \
657 if (!current_target.field) \
658 current_target.field = value
659
660 de_fault (to_open,
661 (void (*) (char *, int))
662 tcomplain);
663 de_fault (to_close,
664 (void (*) (int))
665 target_ignore);
666 de_fault (to_post_attach,
667 (void (*) (int))
668 target_ignore);
669 de_fault (to_prepare_to_store,
670 (void (*) (struct regcache *))
671 noprocess);
672 de_fault (deprecated_xfer_memory,
673 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
674 nomemory);
675 de_fault (to_files_info,
676 (void (*) (struct target_ops *))
677 target_ignore);
678 de_fault (to_insert_breakpoint,
679 memory_insert_breakpoint);
680 de_fault (to_remove_breakpoint,
681 memory_remove_breakpoint);
682 de_fault (to_can_use_hw_breakpoint,
683 (int (*) (int, int, int))
684 return_zero);
685 de_fault (to_insert_hw_breakpoint,
686 (int (*) (struct gdbarch *, struct bp_target_info *))
687 return_minus_one);
688 de_fault (to_remove_hw_breakpoint,
689 (int (*) (struct gdbarch *, struct bp_target_info *))
690 return_minus_one);
691 de_fault (to_insert_watchpoint,
692 (int (*) (CORE_ADDR, int, int))
693 return_minus_one);
694 de_fault (to_remove_watchpoint,
695 (int (*) (CORE_ADDR, int, int))
696 return_minus_one);
697 de_fault (to_stopped_by_watchpoint,
698 (int (*) (void))
699 return_zero);
700 de_fault (to_stopped_data_address,
701 (int (*) (struct target_ops *, CORE_ADDR *))
702 return_zero);
703 de_fault (to_watchpoint_addr_within_range,
704 default_watchpoint_addr_within_range);
705 de_fault (to_region_ok_for_hw_watchpoint,
706 default_region_ok_for_hw_watchpoint);
707 de_fault (to_terminal_init,
708 (void (*) (void))
709 target_ignore);
710 de_fault (to_terminal_inferior,
711 (void (*) (void))
712 target_ignore);
713 de_fault (to_terminal_ours_for_output,
714 (void (*) (void))
715 target_ignore);
716 de_fault (to_terminal_ours,
717 (void (*) (void))
718 target_ignore);
719 de_fault (to_terminal_save_ours,
720 (void (*) (void))
721 target_ignore);
722 de_fault (to_terminal_info,
723 default_terminal_info);
724 de_fault (to_load,
725 (void (*) (char *, int))
726 tcomplain);
727 de_fault (to_lookup_symbol,
728 (int (*) (char *, CORE_ADDR *))
729 nosymbol);
730 de_fault (to_post_startup_inferior,
731 (void (*) (ptid_t))
732 target_ignore);
733 de_fault (to_acknowledge_created_inferior,
734 (void (*) (int))
735 target_ignore);
736 de_fault (to_insert_fork_catchpoint,
737 (void (*) (int))
738 tcomplain);
739 de_fault (to_remove_fork_catchpoint,
740 (int (*) (int))
741 tcomplain);
742 de_fault (to_insert_vfork_catchpoint,
743 (void (*) (int))
744 tcomplain);
745 de_fault (to_remove_vfork_catchpoint,
746 (int (*) (int))
747 tcomplain);
748 de_fault (to_insert_exec_catchpoint,
749 (void (*) (int))
750 tcomplain);
751 de_fault (to_remove_exec_catchpoint,
752 (int (*) (int))
753 tcomplain);
754 de_fault (to_has_exited,
755 (int (*) (int, int, int *))
756 return_zero);
757 de_fault (to_can_run,
758 return_zero);
759 de_fault (to_notice_signals,
760 (void (*) (ptid_t))
761 target_ignore);
762 de_fault (to_extra_thread_info,
763 (char *(*) (struct thread_info *))
764 return_zero);
765 de_fault (to_stop,
766 (void (*) (ptid_t))
767 target_ignore);
768 current_target.to_xfer_partial = current_xfer_partial;
769 de_fault (to_rcmd,
770 (void (*) (char *, struct ui_file *))
771 tcomplain);
772 de_fault (to_pid_to_exec_file,
773 (char *(*) (int))
774 return_zero);
775 de_fault (to_async,
776 (void (*) (void (*) (enum inferior_event_type, void*), void*))
777 tcomplain);
778 de_fault (to_async_mask,
779 (int (*) (int))
780 return_one);
781 de_fault (to_thread_architecture,
782 default_thread_architecture);
783 current_target.to_read_description = NULL;
784 de_fault (to_get_ada_task_ptid,
785 (ptid_t (*) (long, long))
786 default_get_ada_task_ptid);
787 de_fault (to_supports_multi_process,
788 (int (*) (void))
789 return_zero);
790 #undef de_fault
791
792 /* Finally, position the target-stack beneath the squashed
793 "current_target". That way code looking for a non-inherited
794 target method can quickly and simply find it. */
795 current_target.beneath = target_stack;
796
797 if (targetdebug)
798 setup_target_debug ();
799 }
800
801 /* Push a new target type into the stack of the existing target accessors,
802 possibly superseding some of the existing accessors.
803
804 Result is zero if the pushed target ended up on top of the stack,
805 nonzero if at least one target is on top of it.
806
807 Rather than allow an empty stack, we always have the dummy target at
808 the bottom stratum, so we can call the function vectors without
809 checking them. */
810
811 int
812 push_target (struct target_ops *t)
813 {
814 struct target_ops **cur;
815
816 /* Check magic number. If wrong, it probably means someone changed
817 the struct definition, but not all the places that initialize one. */
818 if (t->to_magic != OPS_MAGIC)
819 {
820 fprintf_unfiltered (gdb_stderr,
821 "Magic number of %s target struct wrong\n",
822 t->to_shortname);
823 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
824 }
825
826 /* Find the proper stratum to install this target in. */
827 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
828 {
829 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
830 break;
831 }
832
833 /* If there's already targets at this stratum, remove them. */
834 /* FIXME: cagney/2003-10-15: I think this should be popping all
835 targets to CUR, and not just those at this stratum level. */
836 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
837 {
838 /* There's already something at this stratum level. Close it,
839 and un-hook it from the stack. */
840 struct target_ops *tmp = (*cur);
841 (*cur) = (*cur)->beneath;
842 tmp->beneath = NULL;
843 target_close (tmp, 0);
844 }
845
846 /* We have removed all targets in our stratum, now add the new one. */
847 t->beneath = (*cur);
848 (*cur) = t;
849
850 update_current_target ();
851
852 /* Not on top? */
853 return (t != target_stack);
854 }
855
856 /* Remove a target_ops vector from the stack, wherever it may be.
857 Return how many times it was removed (0 or 1). */
858
859 int
860 unpush_target (struct target_ops *t)
861 {
862 struct target_ops **cur;
863 struct target_ops *tmp;
864
865 if (t->to_stratum == dummy_stratum)
866 internal_error (__FILE__, __LINE__,
867 "Attempt to unpush the dummy target");
868
869 /* Look for the specified target. Note that we assume that a target
870 can only occur once in the target stack. */
871
872 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
873 {
874 if ((*cur) == t)
875 break;
876 }
877
878 if ((*cur) == NULL)
879 return 0; /* Didn't find target_ops, quit now */
880
881 /* NOTE: cagney/2003-12-06: In '94 the close call was made
882 unconditional by moving it to before the above check that the
883 target was in the target stack (something about "Change the way
884 pushing and popping of targets work to support target overlays
885 and inheritance"). This doesn't make much sense - only open
886 targets should be closed. */
887 target_close (t, 0);
888
889 /* Unchain the target */
890 tmp = (*cur);
891 (*cur) = (*cur)->beneath;
892 tmp->beneath = NULL;
893
894 update_current_target ();
895
896 return 1;
897 }
898
899 void
900 pop_target (void)
901 {
902 target_close (target_stack, 0); /* Let it clean up */
903 if (unpush_target (target_stack) == 1)
904 return;
905
906 fprintf_unfiltered (gdb_stderr,
907 "pop_target couldn't find target %s\n",
908 current_target.to_shortname);
909 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
910 }
911
912 void
913 pop_all_targets_above (enum strata above_stratum, int quitting)
914 {
915 while ((int) (current_target.to_stratum) > (int) above_stratum)
916 {
917 target_close (target_stack, quitting);
918 if (!unpush_target (target_stack))
919 {
920 fprintf_unfiltered (gdb_stderr,
921 "pop_all_targets couldn't find target %s\n",
922 target_stack->to_shortname);
923 internal_error (__FILE__, __LINE__,
924 _("failed internal consistency check"));
925 break;
926 }
927 }
928 }
929
930 void
931 pop_all_targets (int quitting)
932 {
933 pop_all_targets_above (dummy_stratum, quitting);
934 }
935
936 /* Using the objfile specified in OBJFILE, find the address for the
937 current thread's thread-local storage with offset OFFSET. */
938 CORE_ADDR
939 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
940 {
941 volatile CORE_ADDR addr = 0;
942 struct target_ops *target;
943
944 for (target = current_target.beneath;
945 target != NULL;
946 target = target->beneath)
947 {
948 if (target->to_get_thread_local_address != NULL)
949 break;
950 }
951
952 if (target != NULL
953 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
954 {
955 ptid_t ptid = inferior_ptid;
956 volatile struct gdb_exception ex;
957
958 TRY_CATCH (ex, RETURN_MASK_ALL)
959 {
960 CORE_ADDR lm_addr;
961
962 /* Fetch the load module address for this objfile. */
963 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
964 objfile);
965 /* If it's 0, throw the appropriate exception. */
966 if (lm_addr == 0)
967 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
968 _("TLS load module not found"));
969
970 addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset);
971 }
972 /* If an error occurred, print TLS related messages here. Otherwise,
973 throw the error to some higher catcher. */
974 if (ex.reason < 0)
975 {
976 int objfile_is_library = (objfile->flags & OBJF_SHARED);
977
978 switch (ex.error)
979 {
980 case TLS_NO_LIBRARY_SUPPORT_ERROR:
981 error (_("Cannot find thread-local variables in this thread library."));
982 break;
983 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
984 if (objfile_is_library)
985 error (_("Cannot find shared library `%s' in dynamic"
986 " linker's load module list"), objfile->name);
987 else
988 error (_("Cannot find executable file `%s' in dynamic"
989 " linker's load module list"), objfile->name);
990 break;
991 case TLS_NOT_ALLOCATED_YET_ERROR:
992 if (objfile_is_library)
993 error (_("The inferior has not yet allocated storage for"
994 " thread-local variables in\n"
995 "the shared library `%s'\n"
996 "for %s"),
997 objfile->name, target_pid_to_str (ptid));
998 else
999 error (_("The inferior has not yet allocated storage for"
1000 " thread-local variables in\n"
1001 "the executable `%s'\n"
1002 "for %s"),
1003 objfile->name, target_pid_to_str (ptid));
1004 break;
1005 case TLS_GENERIC_ERROR:
1006 if (objfile_is_library)
1007 error (_("Cannot find thread-local storage for %s, "
1008 "shared library %s:\n%s"),
1009 target_pid_to_str (ptid),
1010 objfile->name, ex.message);
1011 else
1012 error (_("Cannot find thread-local storage for %s, "
1013 "executable file %s:\n%s"),
1014 target_pid_to_str (ptid),
1015 objfile->name, ex.message);
1016 break;
1017 default:
1018 throw_exception (ex);
1019 break;
1020 }
1021 }
1022 }
1023 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1024 TLS is an ABI-specific thing. But we don't do that yet. */
1025 else
1026 error (_("Cannot find thread-local variables on this target"));
1027
1028 return addr;
1029 }
1030
1031 #undef MIN
1032 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1033
1034 /* target_read_string -- read a null terminated string, up to LEN bytes,
1035 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1036 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1037 is responsible for freeing it. Return the number of bytes successfully
1038 read. */
1039
1040 int
1041 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
1042 {
1043 int tlen, origlen, offset, i;
1044 gdb_byte buf[4];
1045 int errcode = 0;
1046 char *buffer;
1047 int buffer_allocated;
1048 char *bufptr;
1049 unsigned int nbytes_read = 0;
1050
1051 gdb_assert (string);
1052
1053 /* Small for testing. */
1054 buffer_allocated = 4;
1055 buffer = xmalloc (buffer_allocated);
1056 bufptr = buffer;
1057
1058 origlen = len;
1059
1060 while (len > 0)
1061 {
1062 tlen = MIN (len, 4 - (memaddr & 3));
1063 offset = memaddr & 3;
1064
1065 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
1066 if (errcode != 0)
1067 {
1068 /* The transfer request might have crossed the boundary to an
1069 unallocated region of memory. Retry the transfer, requesting
1070 a single byte. */
1071 tlen = 1;
1072 offset = 0;
1073 errcode = target_read_memory (memaddr, buf, 1);
1074 if (errcode != 0)
1075 goto done;
1076 }
1077
1078 if (bufptr - buffer + tlen > buffer_allocated)
1079 {
1080 unsigned int bytes;
1081 bytes = bufptr - buffer;
1082 buffer_allocated *= 2;
1083 buffer = xrealloc (buffer, buffer_allocated);
1084 bufptr = buffer + bytes;
1085 }
1086
1087 for (i = 0; i < tlen; i++)
1088 {
1089 *bufptr++ = buf[i + offset];
1090 if (buf[i + offset] == '\000')
1091 {
1092 nbytes_read += i + 1;
1093 goto done;
1094 }
1095 }
1096
1097 memaddr += tlen;
1098 len -= tlen;
1099 nbytes_read += tlen;
1100 }
1101 done:
1102 *string = buffer;
1103 if (errnop != NULL)
1104 *errnop = errcode;
1105 return nbytes_read;
1106 }
1107
1108 struct target_section_table *
1109 target_get_section_table (struct target_ops *target)
1110 {
1111 struct target_ops *t;
1112
1113 if (targetdebug)
1114 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1115
1116 for (t = target; t != NULL; t = t->beneath)
1117 if (t->to_get_section_table != NULL)
1118 return (*t->to_get_section_table) (t);
1119
1120 return NULL;
1121 }
1122
1123 /* Find a section containing ADDR. */
1124
1125 struct target_section *
1126 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1127 {
1128 struct target_section_table *table = target_get_section_table (target);
1129 struct target_section *secp;
1130
1131 if (table == NULL)
1132 return NULL;
1133
1134 for (secp = table->sections; secp < table->sections_end; secp++)
1135 {
1136 if (addr >= secp->addr && addr < secp->endaddr)
1137 return secp;
1138 }
1139 return NULL;
1140 }
1141
1142 /* Perform a partial memory transfer. The arguments and return
1143 value are just as for target_xfer_partial. */
1144
1145 static LONGEST
1146 memory_xfer_partial (struct target_ops *ops, void *readbuf, const void *writebuf,
1147 ULONGEST memaddr, LONGEST len)
1148 {
1149 LONGEST res;
1150 int reg_len;
1151 struct mem_region *region;
1152
1153 /* Zero length requests are ok and require no work. */
1154 if (len == 0)
1155 return 0;
1156
1157 /* For accesses to unmapped overlay sections, read directly from
1158 files. Must do this first, as MEMADDR may need adjustment. */
1159 if (readbuf != NULL && overlay_debugging)
1160 {
1161 struct obj_section *section = find_pc_overlay (memaddr);
1162 if (pc_in_unmapped_range (memaddr, section))
1163 {
1164 struct target_section_table *table
1165 = target_get_section_table (ops);
1166 const char *section_name = section->the_bfd_section->name;
1167 memaddr = overlay_mapped_address (memaddr, section);
1168 return section_table_xfer_memory_partial (readbuf, writebuf,
1169 memaddr, len,
1170 table->sections,
1171 table->sections_end,
1172 section_name);
1173 }
1174 }
1175
1176 /* Try the executable files, if "trust-readonly-sections" is set. */
1177 if (readbuf != NULL && trust_readonly)
1178 {
1179 struct target_section *secp;
1180 struct target_section_table *table;
1181
1182 secp = target_section_by_addr (ops, memaddr);
1183 if (secp != NULL
1184 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1185 & SEC_READONLY))
1186 {
1187 table = target_get_section_table (ops);
1188 return section_table_xfer_memory_partial (readbuf, writebuf,
1189 memaddr, len,
1190 table->sections,
1191 table->sections_end,
1192 NULL);
1193 }
1194 }
1195
1196 /* Try GDB's internal data cache. */
1197 region = lookup_mem_region (memaddr);
1198 /* region->hi == 0 means there's no upper bound. */
1199 if (memaddr + len < region->hi || region->hi == 0)
1200 reg_len = len;
1201 else
1202 reg_len = region->hi - memaddr;
1203
1204 switch (region->attrib.mode)
1205 {
1206 case MEM_RO:
1207 if (writebuf != NULL)
1208 return -1;
1209 break;
1210
1211 case MEM_WO:
1212 if (readbuf != NULL)
1213 return -1;
1214 break;
1215
1216 case MEM_FLASH:
1217 /* We only support writing to flash during "load" for now. */
1218 if (writebuf != NULL)
1219 error (_("Writing to flash memory forbidden in this context"));
1220 break;
1221
1222 case MEM_NONE:
1223 return -1;
1224 }
1225
1226 if (region->attrib.cache)
1227 {
1228 /* FIXME drow/2006-08-09: This call discards OPS, so the raw
1229 memory request will start back at current_target. */
1230 if (readbuf != NULL)
1231 res = dcache_xfer_memory (target_dcache, memaddr, readbuf,
1232 reg_len, 0);
1233 else
1234 /* FIXME drow/2006-08-09: If we're going to preserve const
1235 correctness dcache_xfer_memory should take readbuf and
1236 writebuf. */
1237 res = dcache_xfer_memory (target_dcache, memaddr,
1238 (void *) writebuf,
1239 reg_len, 1);
1240 if (res <= 0)
1241 return -1;
1242 else
1243 {
1244 if (readbuf && !show_memory_breakpoints)
1245 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1246 return res;
1247 }
1248 }
1249
1250 /* If none of those methods found the memory we wanted, fall back
1251 to a target partial transfer. Normally a single call to
1252 to_xfer_partial is enough; if it doesn't recognize an object
1253 it will call the to_xfer_partial of the next target down.
1254 But for memory this won't do. Memory is the only target
1255 object which can be read from more than one valid target.
1256 A core file, for instance, could have some of memory but
1257 delegate other bits to the target below it. So, we must
1258 manually try all targets. */
1259
1260 do
1261 {
1262 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1263 readbuf, writebuf, memaddr, reg_len);
1264 if (res > 0)
1265 break;
1266
1267 /* We want to continue past core files to executables, but not
1268 past a running target's memory. */
1269 if (ops->to_has_all_memory (ops))
1270 break;
1271
1272 ops = ops->beneath;
1273 }
1274 while (ops != NULL);
1275
1276 if (readbuf && !show_memory_breakpoints)
1277 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1278
1279 /* If we still haven't got anything, return the last error. We
1280 give up. */
1281 return res;
1282 }
1283
1284 static void
1285 restore_show_memory_breakpoints (void *arg)
1286 {
1287 show_memory_breakpoints = (uintptr_t) arg;
1288 }
1289
1290 struct cleanup *
1291 make_show_memory_breakpoints_cleanup (int show)
1292 {
1293 int current = show_memory_breakpoints;
1294 show_memory_breakpoints = show;
1295
1296 return make_cleanup (restore_show_memory_breakpoints,
1297 (void *) (uintptr_t) current);
1298 }
1299
1300 static LONGEST
1301 target_xfer_partial (struct target_ops *ops,
1302 enum target_object object, const char *annex,
1303 void *readbuf, const void *writebuf,
1304 ULONGEST offset, LONGEST len)
1305 {
1306 LONGEST retval;
1307
1308 gdb_assert (ops->to_xfer_partial != NULL);
1309
1310 /* If this is a memory transfer, let the memory-specific code
1311 have a look at it instead. Memory transfers are more
1312 complicated. */
1313 if (object == TARGET_OBJECT_MEMORY)
1314 retval = memory_xfer_partial (ops, readbuf, writebuf, offset, len);
1315 else
1316 {
1317 enum target_object raw_object = object;
1318
1319 /* If this is a raw memory transfer, request the normal
1320 memory object from other layers. */
1321 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1322 raw_object = TARGET_OBJECT_MEMORY;
1323
1324 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1325 writebuf, offset, len);
1326 }
1327
1328 if (targetdebug)
1329 {
1330 const unsigned char *myaddr = NULL;
1331
1332 fprintf_unfiltered (gdb_stdlog,
1333 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
1334 ops->to_shortname,
1335 (int) object,
1336 (annex ? annex : "(null)"),
1337 host_address_to_string (readbuf),
1338 host_address_to_string (writebuf),
1339 core_addr_to_string_nz (offset),
1340 plongest (len), plongest (retval));
1341
1342 if (readbuf)
1343 myaddr = readbuf;
1344 if (writebuf)
1345 myaddr = writebuf;
1346 if (retval > 0 && myaddr != NULL)
1347 {
1348 int i;
1349
1350 fputs_unfiltered (", bytes =", gdb_stdlog);
1351 for (i = 0; i < retval; i++)
1352 {
1353 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1354 {
1355 if (targetdebug < 2 && i > 0)
1356 {
1357 fprintf_unfiltered (gdb_stdlog, " ...");
1358 break;
1359 }
1360 fprintf_unfiltered (gdb_stdlog, "\n");
1361 }
1362
1363 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1364 }
1365 }
1366
1367 fputc_unfiltered ('\n', gdb_stdlog);
1368 }
1369 return retval;
1370 }
1371
1372 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1373 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1374 if any error occurs.
1375
1376 If an error occurs, no guarantee is made about the contents of the data at
1377 MYADDR. In particular, the caller should not depend upon partial reads
1378 filling the buffer with good data. There is no way for the caller to know
1379 how much good data might have been transfered anyway. Callers that can
1380 deal with partial reads should call target_read (which will retry until
1381 it makes no progress, and then return how much was transferred). */
1382
1383 int
1384 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1385 {
1386 /* Dispatch to the topmost target, not the flattened current_target.
1387 Memory accesses check target->to_has_(all_)memory, and the
1388 flattened target doesn't inherit those. */
1389 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1390 myaddr, memaddr, len) == len)
1391 return 0;
1392 else
1393 return EIO;
1394 }
1395
1396 int
1397 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1398 {
1399 /* Dispatch to the topmost target, not the flattened current_target.
1400 Memory accesses check target->to_has_(all_)memory, and the
1401 flattened target doesn't inherit those. */
1402 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1403 myaddr, memaddr, len) == len)
1404 return 0;
1405 else
1406 return EIO;
1407 }
1408
1409 /* Fetch the target's memory map. */
1410
1411 VEC(mem_region_s) *
1412 target_memory_map (void)
1413 {
1414 VEC(mem_region_s) *result;
1415 struct mem_region *last_one, *this_one;
1416 int ix;
1417 struct target_ops *t;
1418
1419 if (targetdebug)
1420 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1421
1422 for (t = current_target.beneath; t != NULL; t = t->beneath)
1423 if (t->to_memory_map != NULL)
1424 break;
1425
1426 if (t == NULL)
1427 return NULL;
1428
1429 result = t->to_memory_map (t);
1430 if (result == NULL)
1431 return NULL;
1432
1433 qsort (VEC_address (mem_region_s, result),
1434 VEC_length (mem_region_s, result),
1435 sizeof (struct mem_region), mem_region_cmp);
1436
1437 /* Check that regions do not overlap. Simultaneously assign
1438 a numbering for the "mem" commands to use to refer to
1439 each region. */
1440 last_one = NULL;
1441 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1442 {
1443 this_one->number = ix;
1444
1445 if (last_one && last_one->hi > this_one->lo)
1446 {
1447 warning (_("Overlapping regions in memory map: ignoring"));
1448 VEC_free (mem_region_s, result);
1449 return NULL;
1450 }
1451 last_one = this_one;
1452 }
1453
1454 return result;
1455 }
1456
1457 void
1458 target_flash_erase (ULONGEST address, LONGEST length)
1459 {
1460 struct target_ops *t;
1461
1462 for (t = current_target.beneath; t != NULL; t = t->beneath)
1463 if (t->to_flash_erase != NULL)
1464 {
1465 if (targetdebug)
1466 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1467 hex_string (address), phex (length, 0));
1468 t->to_flash_erase (t, address, length);
1469 return;
1470 }
1471
1472 tcomplain ();
1473 }
1474
1475 void
1476 target_flash_done (void)
1477 {
1478 struct target_ops *t;
1479
1480 for (t = current_target.beneath; t != NULL; t = t->beneath)
1481 if (t->to_flash_done != NULL)
1482 {
1483 if (targetdebug)
1484 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1485 t->to_flash_done (t);
1486 return;
1487 }
1488
1489 tcomplain ();
1490 }
1491
1492 static void
1493 show_trust_readonly (struct ui_file *file, int from_tty,
1494 struct cmd_list_element *c, const char *value)
1495 {
1496 fprintf_filtered (file, _("\
1497 Mode for reading from readonly sections is %s.\n"),
1498 value);
1499 }
1500
1501 /* More generic transfers. */
1502
1503 static LONGEST
1504 default_xfer_partial (struct target_ops *ops, enum target_object object,
1505 const char *annex, gdb_byte *readbuf,
1506 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1507 {
1508 if (object == TARGET_OBJECT_MEMORY
1509 && ops->deprecated_xfer_memory != NULL)
1510 /* If available, fall back to the target's
1511 "deprecated_xfer_memory" method. */
1512 {
1513 int xfered = -1;
1514 errno = 0;
1515 if (writebuf != NULL)
1516 {
1517 void *buffer = xmalloc (len);
1518 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1519 memcpy (buffer, writebuf, len);
1520 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1521 1/*write*/, NULL, ops);
1522 do_cleanups (cleanup);
1523 }
1524 if (readbuf != NULL)
1525 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1526 0/*read*/, NULL, ops);
1527 if (xfered > 0)
1528 return xfered;
1529 else if (xfered == 0 && errno == 0)
1530 /* "deprecated_xfer_memory" uses 0, cross checked against
1531 ERRNO as one indication of an error. */
1532 return 0;
1533 else
1534 return -1;
1535 }
1536 else if (ops->beneath != NULL)
1537 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1538 readbuf, writebuf, offset, len);
1539 else
1540 return -1;
1541 }
1542
1543 /* The xfer_partial handler for the topmost target. Unlike the default,
1544 it does not need to handle memory specially; it just passes all
1545 requests down the stack. */
1546
1547 static LONGEST
1548 current_xfer_partial (struct target_ops *ops, enum target_object object,
1549 const char *annex, gdb_byte *readbuf,
1550 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1551 {
1552 if (ops->beneath != NULL)
1553 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1554 readbuf, writebuf, offset, len);
1555 else
1556 return -1;
1557 }
1558
1559 /* Target vector read/write partial wrapper functions.
1560
1561 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1562 (inbuf, outbuf)", instead of separate read/write methods, make life
1563 easier. */
1564
1565 static LONGEST
1566 target_read_partial (struct target_ops *ops,
1567 enum target_object object,
1568 const char *annex, gdb_byte *buf,
1569 ULONGEST offset, LONGEST len)
1570 {
1571 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1572 }
1573
1574 static LONGEST
1575 target_write_partial (struct target_ops *ops,
1576 enum target_object object,
1577 const char *annex, const gdb_byte *buf,
1578 ULONGEST offset, LONGEST len)
1579 {
1580 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1581 }
1582
1583 /* Wrappers to perform the full transfer. */
1584 LONGEST
1585 target_read (struct target_ops *ops,
1586 enum target_object object,
1587 const char *annex, gdb_byte *buf,
1588 ULONGEST offset, LONGEST len)
1589 {
1590 LONGEST xfered = 0;
1591 while (xfered < len)
1592 {
1593 LONGEST xfer = target_read_partial (ops, object, annex,
1594 (gdb_byte *) buf + xfered,
1595 offset + xfered, len - xfered);
1596 /* Call an observer, notifying them of the xfer progress? */
1597 if (xfer == 0)
1598 return xfered;
1599 if (xfer < 0)
1600 return -1;
1601 xfered += xfer;
1602 QUIT;
1603 }
1604 return len;
1605 }
1606
1607 LONGEST
1608 target_read_until_error (struct target_ops *ops,
1609 enum target_object object,
1610 const char *annex, gdb_byte *buf,
1611 ULONGEST offset, LONGEST len)
1612 {
1613 LONGEST xfered = 0;
1614 while (xfered < len)
1615 {
1616 LONGEST xfer = target_read_partial (ops, object, annex,
1617 (gdb_byte *) buf + xfered,
1618 offset + xfered, len - xfered);
1619 /* Call an observer, notifying them of the xfer progress? */
1620 if (xfer == 0)
1621 return xfered;
1622 if (xfer < 0)
1623 {
1624 /* We've got an error. Try to read in smaller blocks. */
1625 ULONGEST start = offset + xfered;
1626 ULONGEST remaining = len - xfered;
1627 ULONGEST half;
1628
1629 /* If an attempt was made to read a random memory address,
1630 it's likely that the very first byte is not accessible.
1631 Try reading the first byte, to avoid doing log N tries
1632 below. */
1633 xfer = target_read_partial (ops, object, annex,
1634 (gdb_byte *) buf + xfered, start, 1);
1635 if (xfer <= 0)
1636 return xfered;
1637 start += 1;
1638 remaining -= 1;
1639 half = remaining/2;
1640
1641 while (half > 0)
1642 {
1643 xfer = target_read_partial (ops, object, annex,
1644 (gdb_byte *) buf + xfered,
1645 start, half);
1646 if (xfer == 0)
1647 return xfered;
1648 if (xfer < 0)
1649 {
1650 remaining = half;
1651 }
1652 else
1653 {
1654 /* We have successfully read the first half. So, the
1655 error must be in the second half. Adjust start and
1656 remaining to point at the second half. */
1657 xfered += xfer;
1658 start += xfer;
1659 remaining -= xfer;
1660 }
1661 half = remaining/2;
1662 }
1663
1664 return xfered;
1665 }
1666 xfered += xfer;
1667 QUIT;
1668 }
1669 return len;
1670 }
1671
1672
1673 /* An alternative to target_write with progress callbacks. */
1674
1675 LONGEST
1676 target_write_with_progress (struct target_ops *ops,
1677 enum target_object object,
1678 const char *annex, const gdb_byte *buf,
1679 ULONGEST offset, LONGEST len,
1680 void (*progress) (ULONGEST, void *), void *baton)
1681 {
1682 LONGEST xfered = 0;
1683
1684 /* Give the progress callback a chance to set up. */
1685 if (progress)
1686 (*progress) (0, baton);
1687
1688 while (xfered < len)
1689 {
1690 LONGEST xfer = target_write_partial (ops, object, annex,
1691 (gdb_byte *) buf + xfered,
1692 offset + xfered, len - xfered);
1693
1694 if (xfer == 0)
1695 return xfered;
1696 if (xfer < 0)
1697 return -1;
1698
1699 if (progress)
1700 (*progress) (xfer, baton);
1701
1702 xfered += xfer;
1703 QUIT;
1704 }
1705 return len;
1706 }
1707
1708 LONGEST
1709 target_write (struct target_ops *ops,
1710 enum target_object object,
1711 const char *annex, const gdb_byte *buf,
1712 ULONGEST offset, LONGEST len)
1713 {
1714 return target_write_with_progress (ops, object, annex, buf, offset, len,
1715 NULL, NULL);
1716 }
1717
1718 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1719 the size of the transferred data. PADDING additional bytes are
1720 available in *BUF_P. This is a helper function for
1721 target_read_alloc; see the declaration of that function for more
1722 information. */
1723
1724 static LONGEST
1725 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1726 const char *annex, gdb_byte **buf_p, int padding)
1727 {
1728 size_t buf_alloc, buf_pos;
1729 gdb_byte *buf;
1730 LONGEST n;
1731
1732 /* This function does not have a length parameter; it reads the
1733 entire OBJECT). Also, it doesn't support objects fetched partly
1734 from one target and partly from another (in a different stratum,
1735 e.g. a core file and an executable). Both reasons make it
1736 unsuitable for reading memory. */
1737 gdb_assert (object != TARGET_OBJECT_MEMORY);
1738
1739 /* Start by reading up to 4K at a time. The target will throttle
1740 this number down if necessary. */
1741 buf_alloc = 4096;
1742 buf = xmalloc (buf_alloc);
1743 buf_pos = 0;
1744 while (1)
1745 {
1746 n = target_read_partial (ops, object, annex, &buf[buf_pos],
1747 buf_pos, buf_alloc - buf_pos - padding);
1748 if (n < 0)
1749 {
1750 /* An error occurred. */
1751 xfree (buf);
1752 return -1;
1753 }
1754 else if (n == 0)
1755 {
1756 /* Read all there was. */
1757 if (buf_pos == 0)
1758 xfree (buf);
1759 else
1760 *buf_p = buf;
1761 return buf_pos;
1762 }
1763
1764 buf_pos += n;
1765
1766 /* If the buffer is filling up, expand it. */
1767 if (buf_alloc < buf_pos * 2)
1768 {
1769 buf_alloc *= 2;
1770 buf = xrealloc (buf, buf_alloc);
1771 }
1772
1773 QUIT;
1774 }
1775 }
1776
1777 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1778 the size of the transferred data. See the declaration in "target.h"
1779 function for more information about the return value. */
1780
1781 LONGEST
1782 target_read_alloc (struct target_ops *ops, enum target_object object,
1783 const char *annex, gdb_byte **buf_p)
1784 {
1785 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1786 }
1787
1788 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1789 returned as a string, allocated using xmalloc. If an error occurs
1790 or the transfer is unsupported, NULL is returned. Empty objects
1791 are returned as allocated but empty strings. A warning is issued
1792 if the result contains any embedded NUL bytes. */
1793
1794 char *
1795 target_read_stralloc (struct target_ops *ops, enum target_object object,
1796 const char *annex)
1797 {
1798 gdb_byte *buffer;
1799 LONGEST transferred;
1800
1801 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1802
1803 if (transferred < 0)
1804 return NULL;
1805
1806 if (transferred == 0)
1807 return xstrdup ("");
1808
1809 buffer[transferred] = 0;
1810 if (strlen (buffer) < transferred)
1811 warning (_("target object %d, annex %s, "
1812 "contained unexpected null characters"),
1813 (int) object, annex ? annex : "(none)");
1814
1815 return (char *) buffer;
1816 }
1817
1818 /* Memory transfer methods. */
1819
1820 void
1821 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
1822 LONGEST len)
1823 {
1824 /* This method is used to read from an alternate, non-current
1825 target. This read must bypass the overlay support (as symbols
1826 don't match this target), and GDB's internal cache (wrong cache
1827 for this target). */
1828 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
1829 != len)
1830 memory_error (EIO, addr);
1831 }
1832
1833 ULONGEST
1834 get_target_memory_unsigned (struct target_ops *ops,
1835 CORE_ADDR addr, int len, enum bfd_endian byte_order)
1836 {
1837 gdb_byte buf[sizeof (ULONGEST)];
1838
1839 gdb_assert (len <= sizeof (buf));
1840 get_target_memory (ops, addr, buf, len);
1841 return extract_unsigned_integer (buf, len, byte_order);
1842 }
1843
1844 static void
1845 target_info (char *args, int from_tty)
1846 {
1847 struct target_ops *t;
1848 int has_all_mem = 0;
1849
1850 if (symfile_objfile != NULL)
1851 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
1852
1853 for (t = target_stack; t != NULL; t = t->beneath)
1854 {
1855 if (!(*t->to_has_memory) (t))
1856 continue;
1857
1858 if ((int) (t->to_stratum) <= (int) dummy_stratum)
1859 continue;
1860 if (has_all_mem)
1861 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
1862 printf_unfiltered ("%s:\n", t->to_longname);
1863 (t->to_files_info) (t);
1864 has_all_mem = (*t->to_has_all_memory) (t);
1865 }
1866 }
1867
1868 /* This function is called before any new inferior is created, e.g.
1869 by running a program, attaching, or connecting to a target.
1870 It cleans up any state from previous invocations which might
1871 change between runs. This is a subset of what target_preopen
1872 resets (things which might change between targets). */
1873
1874 void
1875 target_pre_inferior (int from_tty)
1876 {
1877 /* Clear out solib state. Otherwise the solib state of the previous
1878 inferior might have survived and is entirely wrong for the new
1879 target. This has been observed on GNU/Linux using glibc 2.3. How
1880 to reproduce:
1881
1882 bash$ ./foo&
1883 [1] 4711
1884 bash$ ./foo&
1885 [1] 4712
1886 bash$ gdb ./foo
1887 [...]
1888 (gdb) attach 4711
1889 (gdb) detach
1890 (gdb) attach 4712
1891 Cannot access memory at address 0xdeadbeef
1892 */
1893
1894 /* In some OSs, the shared library list is the same/global/shared
1895 across inferiors. If code is shared between processes, so are
1896 memory regions and features. */
1897 if (!gdbarch_has_global_solist (target_gdbarch))
1898 {
1899 no_shared_libraries (NULL, from_tty);
1900
1901 invalidate_target_mem_regions ();
1902
1903 target_clear_description ();
1904 }
1905 }
1906
1907 /* Callback for iterate_over_inferiors. Gets rid of the given
1908 inferior. */
1909
1910 static int
1911 dispose_inferior (struct inferior *inf, void *args)
1912 {
1913 struct thread_info *thread;
1914
1915 thread = any_thread_of_process (inf->pid);
1916 if (thread)
1917 {
1918 switch_to_thread (thread->ptid);
1919
1920 /* Core inferiors actually should be detached, not killed. */
1921 if (target_has_execution)
1922 target_kill ();
1923 else
1924 target_detach (NULL, 0);
1925 }
1926
1927 return 0;
1928 }
1929
1930 /* This is to be called by the open routine before it does
1931 anything. */
1932
1933 void
1934 target_preopen (int from_tty)
1935 {
1936 dont_repeat ();
1937
1938 if (have_inferiors ())
1939 {
1940 if (!from_tty
1941 || !have_live_inferiors ()
1942 || query (_("A program is being debugged already. Kill it? ")))
1943 iterate_over_inferiors (dispose_inferior, NULL);
1944 else
1945 error (_("Program not killed."));
1946 }
1947
1948 /* Calling target_kill may remove the target from the stack. But if
1949 it doesn't (which seems like a win for UDI), remove it now. */
1950 /* Leave the exec target, though. The user may be switching from a
1951 live process to a core of the same program. */
1952 pop_all_targets_above (file_stratum, 0);
1953
1954 target_pre_inferior (from_tty);
1955 }
1956
1957 /* Detach a target after doing deferred register stores. */
1958
1959 void
1960 target_detach (char *args, int from_tty)
1961 {
1962 struct target_ops* t;
1963
1964 if (gdbarch_has_global_breakpoints (target_gdbarch))
1965 /* Don't remove global breakpoints here. They're removed on
1966 disconnection from the target. */
1967 ;
1968 else
1969 /* If we're in breakpoints-always-inserted mode, have to remove
1970 them before detaching. */
1971 remove_breakpoints ();
1972
1973 for (t = current_target.beneath; t != NULL; t = t->beneath)
1974 {
1975 if (t->to_detach != NULL)
1976 {
1977 t->to_detach (t, args, from_tty);
1978 if (targetdebug)
1979 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
1980 args, from_tty);
1981 return;
1982 }
1983 }
1984
1985 internal_error (__FILE__, __LINE__, "could not find a target to detach");
1986 }
1987
1988 void
1989 target_disconnect (char *args, int from_tty)
1990 {
1991 struct target_ops *t;
1992
1993 /* If we're in breakpoints-always-inserted mode or if breakpoints
1994 are global across processes, we have to remove them before
1995 disconnecting. */
1996 remove_breakpoints ();
1997
1998 for (t = current_target.beneath; t != NULL; t = t->beneath)
1999 if (t->to_disconnect != NULL)
2000 {
2001 if (targetdebug)
2002 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2003 args, from_tty);
2004 t->to_disconnect (t, args, from_tty);
2005 return;
2006 }
2007
2008 tcomplain ();
2009 }
2010
2011 ptid_t
2012 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
2013 {
2014 struct target_ops *t;
2015
2016 for (t = current_target.beneath; t != NULL; t = t->beneath)
2017 {
2018 if (t->to_wait != NULL)
2019 {
2020 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
2021
2022 if (targetdebug)
2023 {
2024 char *status_string;
2025
2026 status_string = target_waitstatus_to_string (status);
2027 fprintf_unfiltered (gdb_stdlog,
2028 "target_wait (%d, status) = %d, %s\n",
2029 PIDGET (ptid), PIDGET (retval),
2030 status_string);
2031 xfree (status_string);
2032 }
2033
2034 return retval;
2035 }
2036 }
2037
2038 noprocess ();
2039 }
2040
2041 char *
2042 target_pid_to_str (ptid_t ptid)
2043 {
2044 struct target_ops *t;
2045
2046 for (t = current_target.beneath; t != NULL; t = t->beneath)
2047 {
2048 if (t->to_pid_to_str != NULL)
2049 return (*t->to_pid_to_str) (t, ptid);
2050 }
2051
2052 return normal_pid_to_str (ptid);
2053 }
2054
2055 void
2056 target_resume (ptid_t ptid, int step, enum target_signal signal)
2057 {
2058 struct target_ops *t;
2059
2060 dcache_invalidate (target_dcache);
2061
2062 for (t = current_target.beneath; t != NULL; t = t->beneath)
2063 {
2064 if (t->to_resume != NULL)
2065 {
2066 t->to_resume (t, ptid, step, signal);
2067 if (targetdebug)
2068 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2069 PIDGET (ptid),
2070 step ? "step" : "continue",
2071 target_signal_to_name (signal));
2072
2073 set_executing (ptid, 1);
2074 set_running (ptid, 1);
2075 clear_inline_frame_state (ptid);
2076 return;
2077 }
2078 }
2079
2080 noprocess ();
2081 }
2082 /* Look through the list of possible targets for a target that can
2083 follow forks. */
2084
2085 int
2086 target_follow_fork (int follow_child)
2087 {
2088 struct target_ops *t;
2089
2090 for (t = current_target.beneath; t != NULL; t = t->beneath)
2091 {
2092 if (t->to_follow_fork != NULL)
2093 {
2094 int retval = t->to_follow_fork (t, follow_child);
2095 if (targetdebug)
2096 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2097 follow_child, retval);
2098 return retval;
2099 }
2100 }
2101
2102 /* Some target returned a fork event, but did not know how to follow it. */
2103 internal_error (__FILE__, __LINE__,
2104 "could not find a target to follow fork");
2105 }
2106
2107 void
2108 target_mourn_inferior (void)
2109 {
2110 struct target_ops *t;
2111 for (t = current_target.beneath; t != NULL; t = t->beneath)
2112 {
2113 if (t->to_mourn_inferior != NULL)
2114 {
2115 t->to_mourn_inferior (t);
2116 if (targetdebug)
2117 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2118
2119 /* We no longer need to keep handles on any of the object files.
2120 Make sure to release them to avoid unnecessarily locking any
2121 of them while we're not actually debugging. */
2122 bfd_cache_close_all ();
2123
2124 return;
2125 }
2126 }
2127
2128 internal_error (__FILE__, __LINE__,
2129 "could not find a target to follow mourn inferiour");
2130 }
2131
2132 /* Look for a target which can describe architectural features, starting
2133 from TARGET. If we find one, return its description. */
2134
2135 const struct target_desc *
2136 target_read_description (struct target_ops *target)
2137 {
2138 struct target_ops *t;
2139
2140 for (t = target; t != NULL; t = t->beneath)
2141 if (t->to_read_description != NULL)
2142 {
2143 const struct target_desc *tdesc;
2144
2145 tdesc = t->to_read_description (t);
2146 if (tdesc)
2147 return tdesc;
2148 }
2149
2150 return NULL;
2151 }
2152
2153 /* The default implementation of to_search_memory.
2154 This implements a basic search of memory, reading target memory and
2155 performing the search here (as opposed to performing the search in on the
2156 target side with, for example, gdbserver). */
2157
2158 int
2159 simple_search_memory (struct target_ops *ops,
2160 CORE_ADDR start_addr, ULONGEST search_space_len,
2161 const gdb_byte *pattern, ULONGEST pattern_len,
2162 CORE_ADDR *found_addrp)
2163 {
2164 /* NOTE: also defined in find.c testcase. */
2165 #define SEARCH_CHUNK_SIZE 16000
2166 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2167 /* Buffer to hold memory contents for searching. */
2168 gdb_byte *search_buf;
2169 unsigned search_buf_size;
2170 struct cleanup *old_cleanups;
2171
2172 search_buf_size = chunk_size + pattern_len - 1;
2173
2174 /* No point in trying to allocate a buffer larger than the search space. */
2175 if (search_space_len < search_buf_size)
2176 search_buf_size = search_space_len;
2177
2178 search_buf = malloc (search_buf_size);
2179 if (search_buf == NULL)
2180 error (_("Unable to allocate memory to perform the search."));
2181 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2182
2183 /* Prime the search buffer. */
2184
2185 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2186 search_buf, start_addr, search_buf_size) != search_buf_size)
2187 {
2188 warning (_("Unable to access target memory at %s, halting search."),
2189 hex_string (start_addr));
2190 do_cleanups (old_cleanups);
2191 return -1;
2192 }
2193
2194 /* Perform the search.
2195
2196 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2197 When we've scanned N bytes we copy the trailing bytes to the start and
2198 read in another N bytes. */
2199
2200 while (search_space_len >= pattern_len)
2201 {
2202 gdb_byte *found_ptr;
2203 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2204
2205 found_ptr = memmem (search_buf, nr_search_bytes,
2206 pattern, pattern_len);
2207
2208 if (found_ptr != NULL)
2209 {
2210 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2211 *found_addrp = found_addr;
2212 do_cleanups (old_cleanups);
2213 return 1;
2214 }
2215
2216 /* Not found in this chunk, skip to next chunk. */
2217
2218 /* Don't let search_space_len wrap here, it's unsigned. */
2219 if (search_space_len >= chunk_size)
2220 search_space_len -= chunk_size;
2221 else
2222 search_space_len = 0;
2223
2224 if (search_space_len >= pattern_len)
2225 {
2226 unsigned keep_len = search_buf_size - chunk_size;
2227 CORE_ADDR read_addr = start_addr + keep_len;
2228 int nr_to_read;
2229
2230 /* Copy the trailing part of the previous iteration to the front
2231 of the buffer for the next iteration. */
2232 gdb_assert (keep_len == pattern_len - 1);
2233 memcpy (search_buf, search_buf + chunk_size, keep_len);
2234
2235 nr_to_read = min (search_space_len - keep_len, chunk_size);
2236
2237 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2238 search_buf + keep_len, read_addr,
2239 nr_to_read) != nr_to_read)
2240 {
2241 warning (_("Unable to access target memory at %s, halting search."),
2242 hex_string (read_addr));
2243 do_cleanups (old_cleanups);
2244 return -1;
2245 }
2246
2247 start_addr += chunk_size;
2248 }
2249 }
2250
2251 /* Not found. */
2252
2253 do_cleanups (old_cleanups);
2254 return 0;
2255 }
2256
2257 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2258 sequence of bytes in PATTERN with length PATTERN_LEN.
2259
2260 The result is 1 if found, 0 if not found, and -1 if there was an error
2261 requiring halting of the search (e.g. memory read error).
2262 If the pattern is found the address is recorded in FOUND_ADDRP. */
2263
2264 int
2265 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2266 const gdb_byte *pattern, ULONGEST pattern_len,
2267 CORE_ADDR *found_addrp)
2268 {
2269 struct target_ops *t;
2270 int found;
2271
2272 /* We don't use INHERIT to set current_target.to_search_memory,
2273 so we have to scan the target stack and handle targetdebug
2274 ourselves. */
2275
2276 if (targetdebug)
2277 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2278 hex_string (start_addr));
2279
2280 for (t = current_target.beneath; t != NULL; t = t->beneath)
2281 if (t->to_search_memory != NULL)
2282 break;
2283
2284 if (t != NULL)
2285 {
2286 found = t->to_search_memory (t, start_addr, search_space_len,
2287 pattern, pattern_len, found_addrp);
2288 }
2289 else
2290 {
2291 /* If a special version of to_search_memory isn't available, use the
2292 simple version. */
2293 found = simple_search_memory (current_target.beneath,
2294 start_addr, search_space_len,
2295 pattern, pattern_len, found_addrp);
2296 }
2297
2298 if (targetdebug)
2299 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2300
2301 return found;
2302 }
2303
2304 /* Look through the currently pushed targets. If none of them will
2305 be able to restart the currently running process, issue an error
2306 message. */
2307
2308 void
2309 target_require_runnable (void)
2310 {
2311 struct target_ops *t;
2312
2313 for (t = target_stack; t != NULL; t = t->beneath)
2314 {
2315 /* If this target knows how to create a new program, then
2316 assume we will still be able to after killing the current
2317 one. Either killing and mourning will not pop T, or else
2318 find_default_run_target will find it again. */
2319 if (t->to_create_inferior != NULL)
2320 return;
2321
2322 /* Do not worry about thread_stratum targets that can not
2323 create inferiors. Assume they will be pushed again if
2324 necessary, and continue to the process_stratum. */
2325 if (t->to_stratum == thread_stratum
2326 || t->to_stratum == arch_stratum)
2327 continue;
2328
2329 error (_("\
2330 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2331 t->to_shortname);
2332 }
2333
2334 /* This function is only called if the target is running. In that
2335 case there should have been a process_stratum target and it
2336 should either know how to create inferiors, or not... */
2337 internal_error (__FILE__, __LINE__, "No targets found");
2338 }
2339
2340 /* Look through the list of possible targets for a target that can
2341 execute a run or attach command without any other data. This is
2342 used to locate the default process stratum.
2343
2344 If DO_MESG is not NULL, the result is always valid (error() is
2345 called for errors); else, return NULL on error. */
2346
2347 static struct target_ops *
2348 find_default_run_target (char *do_mesg)
2349 {
2350 struct target_ops **t;
2351 struct target_ops *runable = NULL;
2352 int count;
2353
2354 count = 0;
2355
2356 for (t = target_structs; t < target_structs + target_struct_size;
2357 ++t)
2358 {
2359 if ((*t)->to_can_run && target_can_run (*t))
2360 {
2361 runable = *t;
2362 ++count;
2363 }
2364 }
2365
2366 if (count != 1)
2367 {
2368 if (do_mesg)
2369 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2370 else
2371 return NULL;
2372 }
2373
2374 return runable;
2375 }
2376
2377 void
2378 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2379 {
2380 struct target_ops *t;
2381
2382 t = find_default_run_target ("attach");
2383 (t->to_attach) (t, args, from_tty);
2384 return;
2385 }
2386
2387 void
2388 find_default_create_inferior (struct target_ops *ops,
2389 char *exec_file, char *allargs, char **env,
2390 int from_tty)
2391 {
2392 struct target_ops *t;
2393
2394 t = find_default_run_target ("run");
2395 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2396 return;
2397 }
2398
2399 static int
2400 find_default_can_async_p (void)
2401 {
2402 struct target_ops *t;
2403
2404 /* This may be called before the target is pushed on the stack;
2405 look for the default process stratum. If there's none, gdb isn't
2406 configured with a native debugger, and target remote isn't
2407 connected yet. */
2408 t = find_default_run_target (NULL);
2409 if (t && t->to_can_async_p)
2410 return (t->to_can_async_p) ();
2411 return 0;
2412 }
2413
2414 static int
2415 find_default_is_async_p (void)
2416 {
2417 struct target_ops *t;
2418
2419 /* This may be called before the target is pushed on the stack;
2420 look for the default process stratum. If there's none, gdb isn't
2421 configured with a native debugger, and target remote isn't
2422 connected yet. */
2423 t = find_default_run_target (NULL);
2424 if (t && t->to_is_async_p)
2425 return (t->to_is_async_p) ();
2426 return 0;
2427 }
2428
2429 static int
2430 find_default_supports_non_stop (void)
2431 {
2432 struct target_ops *t;
2433
2434 t = find_default_run_target (NULL);
2435 if (t && t->to_supports_non_stop)
2436 return (t->to_supports_non_stop) ();
2437 return 0;
2438 }
2439
2440 int
2441 target_supports_non_stop (void)
2442 {
2443 struct target_ops *t;
2444 for (t = &current_target; t != NULL; t = t->beneath)
2445 if (t->to_supports_non_stop)
2446 return t->to_supports_non_stop ();
2447
2448 return 0;
2449 }
2450
2451
2452 char *
2453 target_get_osdata (const char *type)
2454 {
2455 char *document;
2456 struct target_ops *t;
2457
2458 /* If we're already connected to something that can get us OS
2459 related data, use it. Otherwise, try using the native
2460 target. */
2461 if (current_target.to_stratum >= process_stratum)
2462 t = current_target.beneath;
2463 else
2464 t = find_default_run_target ("get OS data");
2465
2466 if (!t)
2467 return NULL;
2468
2469 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
2470 }
2471
2472 static int
2473 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2474 {
2475 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
2476 }
2477
2478 static int
2479 default_watchpoint_addr_within_range (struct target_ops *target,
2480 CORE_ADDR addr,
2481 CORE_ADDR start, int length)
2482 {
2483 return addr >= start && addr < start + length;
2484 }
2485
2486 static struct gdbarch *
2487 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2488 {
2489 return target_gdbarch;
2490 }
2491
2492 static int
2493 return_zero (void)
2494 {
2495 return 0;
2496 }
2497
2498 static int
2499 return_one (void)
2500 {
2501 return 1;
2502 }
2503
2504 static int
2505 return_minus_one (void)
2506 {
2507 return -1;
2508 }
2509
2510 /* Find a single runnable target in the stack and return it. If for
2511 some reason there is more than one, return NULL. */
2512
2513 struct target_ops *
2514 find_run_target (void)
2515 {
2516 struct target_ops **t;
2517 struct target_ops *runable = NULL;
2518 int count;
2519
2520 count = 0;
2521
2522 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2523 {
2524 if ((*t)->to_can_run && target_can_run (*t))
2525 {
2526 runable = *t;
2527 ++count;
2528 }
2529 }
2530
2531 return (count == 1 ? runable : NULL);
2532 }
2533
2534 /* Find a single core_stratum target in the list of targets and return it.
2535 If for some reason there is more than one, return NULL. */
2536
2537 struct target_ops *
2538 find_core_target (void)
2539 {
2540 struct target_ops **t;
2541 struct target_ops *runable = NULL;
2542 int count;
2543
2544 count = 0;
2545
2546 for (t = target_structs; t < target_structs + target_struct_size;
2547 ++t)
2548 {
2549 if ((*t)->to_stratum == core_stratum)
2550 {
2551 runable = *t;
2552 ++count;
2553 }
2554 }
2555
2556 return (count == 1 ? runable : NULL);
2557 }
2558
2559 /*
2560 * Find the next target down the stack from the specified target.
2561 */
2562
2563 struct target_ops *
2564 find_target_beneath (struct target_ops *t)
2565 {
2566 return t->beneath;
2567 }
2568
2569 \f
2570 /* The inferior process has died. Long live the inferior! */
2571
2572 void
2573 generic_mourn_inferior (void)
2574 {
2575 ptid_t ptid;
2576
2577 ptid = inferior_ptid;
2578 inferior_ptid = null_ptid;
2579
2580 if (!ptid_equal (ptid, null_ptid))
2581 {
2582 int pid = ptid_get_pid (ptid);
2583 delete_inferior (pid);
2584 }
2585
2586 breakpoint_init_inferior (inf_exited);
2587 registers_changed ();
2588
2589 reopen_exec_file ();
2590 reinit_frame_cache ();
2591
2592 if (deprecated_detach_hook)
2593 deprecated_detach_hook ();
2594 }
2595 \f
2596 /* Helper function for child_wait and the derivatives of child_wait.
2597 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2598 translation of that in OURSTATUS. */
2599 void
2600 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
2601 {
2602 if (WIFEXITED (hoststatus))
2603 {
2604 ourstatus->kind = TARGET_WAITKIND_EXITED;
2605 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2606 }
2607 else if (!WIFSTOPPED (hoststatus))
2608 {
2609 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2610 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2611 }
2612 else
2613 {
2614 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2615 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2616 }
2617 }
2618 \f
2619 /* Convert a normal process ID to a string. Returns the string in a
2620 static buffer. */
2621
2622 char *
2623 normal_pid_to_str (ptid_t ptid)
2624 {
2625 static char buf[32];
2626
2627 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
2628 return buf;
2629 }
2630
2631 static char *
2632 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2633 {
2634 return normal_pid_to_str (ptid);
2635 }
2636
2637 /* Error-catcher for target_find_memory_regions */
2638 static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2639 {
2640 error (_("No target."));
2641 return 0;
2642 }
2643
2644 /* Error-catcher for target_make_corefile_notes */
2645 static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2646 {
2647 error (_("No target."));
2648 return NULL;
2649 }
2650
2651 /* Set up the handful of non-empty slots needed by the dummy target
2652 vector. */
2653
2654 static void
2655 init_dummy_target (void)
2656 {
2657 dummy_target.to_shortname = "None";
2658 dummy_target.to_longname = "None";
2659 dummy_target.to_doc = "";
2660 dummy_target.to_attach = find_default_attach;
2661 dummy_target.to_detach =
2662 (void (*)(struct target_ops *, char *, int))target_ignore;
2663 dummy_target.to_create_inferior = find_default_create_inferior;
2664 dummy_target.to_can_async_p = find_default_can_async_p;
2665 dummy_target.to_is_async_p = find_default_is_async_p;
2666 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
2667 dummy_target.to_pid_to_str = dummy_pid_to_str;
2668 dummy_target.to_stratum = dummy_stratum;
2669 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2670 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
2671 dummy_target.to_xfer_partial = default_xfer_partial;
2672 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2673 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2674 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2675 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2676 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
2677 dummy_target.to_magic = OPS_MAGIC;
2678 }
2679 \f
2680 static void
2681 debug_to_open (char *args, int from_tty)
2682 {
2683 debug_target.to_open (args, from_tty);
2684
2685 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
2686 }
2687
2688 void
2689 target_close (struct target_ops *targ, int quitting)
2690 {
2691 if (targ->to_xclose != NULL)
2692 targ->to_xclose (targ, quitting);
2693 else if (targ->to_close != NULL)
2694 targ->to_close (quitting);
2695
2696 if (targetdebug)
2697 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
2698 }
2699
2700 void
2701 target_attach (char *args, int from_tty)
2702 {
2703 struct target_ops *t;
2704 for (t = current_target.beneath; t != NULL; t = t->beneath)
2705 {
2706 if (t->to_attach != NULL)
2707 {
2708 t->to_attach (t, args, from_tty);
2709 if (targetdebug)
2710 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2711 args, from_tty);
2712 return;
2713 }
2714 }
2715
2716 internal_error (__FILE__, __LINE__,
2717 "could not find a target to attach");
2718 }
2719
2720 int
2721 target_thread_alive (ptid_t ptid)
2722 {
2723 struct target_ops *t;
2724 for (t = current_target.beneath; t != NULL; t = t->beneath)
2725 {
2726 if (t->to_thread_alive != NULL)
2727 {
2728 int retval;
2729
2730 retval = t->to_thread_alive (t, ptid);
2731 if (targetdebug)
2732 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2733 PIDGET (ptid), retval);
2734
2735 return retval;
2736 }
2737 }
2738
2739 return 0;
2740 }
2741
2742 void
2743 target_find_new_threads (void)
2744 {
2745 struct target_ops *t;
2746 for (t = current_target.beneath; t != NULL; t = t->beneath)
2747 {
2748 if (t->to_find_new_threads != NULL)
2749 {
2750 t->to_find_new_threads (t);
2751 if (targetdebug)
2752 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2753
2754 return;
2755 }
2756 }
2757 }
2758
2759 static void
2760 debug_to_post_attach (int pid)
2761 {
2762 debug_target.to_post_attach (pid);
2763
2764 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
2765 }
2766
2767 /* Return a pretty printed form of target_waitstatus.
2768 Space for the result is malloc'd, caller must free. */
2769
2770 char *
2771 target_waitstatus_to_string (const struct target_waitstatus *ws)
2772 {
2773 const char *kind_str = "status->kind = ";
2774
2775 switch (ws->kind)
2776 {
2777 case TARGET_WAITKIND_EXITED:
2778 return xstrprintf ("%sexited, status = %d",
2779 kind_str, ws->value.integer);
2780 case TARGET_WAITKIND_STOPPED:
2781 return xstrprintf ("%sstopped, signal = %s",
2782 kind_str, target_signal_to_name (ws->value.sig));
2783 case TARGET_WAITKIND_SIGNALLED:
2784 return xstrprintf ("%ssignalled, signal = %s",
2785 kind_str, target_signal_to_name (ws->value.sig));
2786 case TARGET_WAITKIND_LOADED:
2787 return xstrprintf ("%sloaded", kind_str);
2788 case TARGET_WAITKIND_FORKED:
2789 return xstrprintf ("%sforked", kind_str);
2790 case TARGET_WAITKIND_VFORKED:
2791 return xstrprintf ("%svforked", kind_str);
2792 case TARGET_WAITKIND_EXECD:
2793 return xstrprintf ("%sexecd", kind_str);
2794 case TARGET_WAITKIND_SYSCALL_ENTRY:
2795 return xstrprintf ("%ssyscall-entry", kind_str);
2796 case TARGET_WAITKIND_SYSCALL_RETURN:
2797 return xstrprintf ("%ssyscall-return", kind_str);
2798 case TARGET_WAITKIND_SPURIOUS:
2799 return xstrprintf ("%sspurious", kind_str);
2800 case TARGET_WAITKIND_IGNORE:
2801 return xstrprintf ("%signore", kind_str);
2802 case TARGET_WAITKIND_NO_HISTORY:
2803 return xstrprintf ("%sno-history", kind_str);
2804 default:
2805 return xstrprintf ("%sunknown???", kind_str);
2806 }
2807 }
2808
2809 static void
2810 debug_print_register (const char * func,
2811 struct regcache *regcache, int regno)
2812 {
2813 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2814 fprintf_unfiltered (gdb_stdlog, "%s ", func);
2815 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
2816 && gdbarch_register_name (gdbarch, regno) != NULL
2817 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2818 fprintf_unfiltered (gdb_stdlog, "(%s)",
2819 gdbarch_register_name (gdbarch, regno));
2820 else
2821 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
2822 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
2823 {
2824 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2825 int i, size = register_size (gdbarch, regno);
2826 unsigned char buf[MAX_REGISTER_SIZE];
2827 regcache_raw_collect (regcache, regno, buf);
2828 fprintf_unfiltered (gdb_stdlog, " = ");
2829 for (i = 0; i < size; i++)
2830 {
2831 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2832 }
2833 if (size <= sizeof (LONGEST))
2834 {
2835 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
2836 fprintf_unfiltered (gdb_stdlog, " %s %s",
2837 core_addr_to_string_nz (val), plongest (val));
2838 }
2839 }
2840 fprintf_unfiltered (gdb_stdlog, "\n");
2841 }
2842
2843 void
2844 target_fetch_registers (struct regcache *regcache, int regno)
2845 {
2846 struct target_ops *t;
2847 for (t = current_target.beneath; t != NULL; t = t->beneath)
2848 {
2849 if (t->to_fetch_registers != NULL)
2850 {
2851 t->to_fetch_registers (t, regcache, regno);
2852 if (targetdebug)
2853 debug_print_register ("target_fetch_registers", regcache, regno);
2854 return;
2855 }
2856 }
2857 }
2858
2859 void
2860 target_store_registers (struct regcache *regcache, int regno)
2861 {
2862
2863 struct target_ops *t;
2864 for (t = current_target.beneath; t != NULL; t = t->beneath)
2865 {
2866 if (t->to_store_registers != NULL)
2867 {
2868 t->to_store_registers (t, regcache, regno);
2869 if (targetdebug)
2870 {
2871 debug_print_register ("target_store_registers", regcache, regno);
2872 }
2873 return;
2874 }
2875 }
2876
2877 noprocess ();
2878 }
2879
2880 static void
2881 debug_to_prepare_to_store (struct regcache *regcache)
2882 {
2883 debug_target.to_prepare_to_store (regcache);
2884
2885 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
2886 }
2887
2888 static int
2889 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
2890 int write, struct mem_attrib *attrib,
2891 struct target_ops *target)
2892 {
2893 int retval;
2894
2895 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
2896 attrib, target);
2897
2898 fprintf_unfiltered (gdb_stdlog,
2899 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
2900 paddress (target_gdbarch, memaddr), len,
2901 write ? "write" : "read", retval);
2902
2903 if (retval > 0)
2904 {
2905 int i;
2906
2907 fputs_unfiltered (", bytes =", gdb_stdlog);
2908 for (i = 0; i < retval; i++)
2909 {
2910 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
2911 {
2912 if (targetdebug < 2 && i > 0)
2913 {
2914 fprintf_unfiltered (gdb_stdlog, " ...");
2915 break;
2916 }
2917 fprintf_unfiltered (gdb_stdlog, "\n");
2918 }
2919
2920 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
2921 }
2922 }
2923
2924 fputc_unfiltered ('\n', gdb_stdlog);
2925
2926 return retval;
2927 }
2928
2929 static void
2930 debug_to_files_info (struct target_ops *target)
2931 {
2932 debug_target.to_files_info (target);
2933
2934 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
2935 }
2936
2937 static int
2938 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
2939 struct bp_target_info *bp_tgt)
2940 {
2941 int retval;
2942
2943 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
2944
2945 fprintf_unfiltered (gdb_stdlog,
2946 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2947 (unsigned long) bp_tgt->placed_address,
2948 (unsigned long) retval);
2949 return retval;
2950 }
2951
2952 static int
2953 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
2954 struct bp_target_info *bp_tgt)
2955 {
2956 int retval;
2957
2958 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
2959
2960 fprintf_unfiltered (gdb_stdlog,
2961 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2962 (unsigned long) bp_tgt->placed_address,
2963 (unsigned long) retval);
2964 return retval;
2965 }
2966
2967 static int
2968 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
2969 {
2970 int retval;
2971
2972 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
2973
2974 fprintf_unfiltered (gdb_stdlog,
2975 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2976 (unsigned long) type,
2977 (unsigned long) cnt,
2978 (unsigned long) from_tty,
2979 (unsigned long) retval);
2980 return retval;
2981 }
2982
2983 static int
2984 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2985 {
2986 CORE_ADDR retval;
2987
2988 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
2989
2990 fprintf_unfiltered (gdb_stdlog,
2991 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
2992 (unsigned long) addr,
2993 (unsigned long) len,
2994 (unsigned long) retval);
2995 return retval;
2996 }
2997
2998 static int
2999 debug_to_stopped_by_watchpoint (void)
3000 {
3001 int retval;
3002
3003 retval = debug_target.to_stopped_by_watchpoint ();
3004
3005 fprintf_unfiltered (gdb_stdlog,
3006 "target_stopped_by_watchpoint () = %ld\n",
3007 (unsigned long) retval);
3008 return retval;
3009 }
3010
3011 static int
3012 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
3013 {
3014 int retval;
3015
3016 retval = debug_target.to_stopped_data_address (target, addr);
3017
3018 fprintf_unfiltered (gdb_stdlog,
3019 "target_stopped_data_address ([0x%lx]) = %ld\n",
3020 (unsigned long)*addr,
3021 (unsigned long)retval);
3022 return retval;
3023 }
3024
3025 static int
3026 debug_to_watchpoint_addr_within_range (struct target_ops *target,
3027 CORE_ADDR addr,
3028 CORE_ADDR start, int length)
3029 {
3030 int retval;
3031
3032 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3033 start, length);
3034
3035 fprintf_filtered (gdb_stdlog,
3036 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3037 (unsigned long) addr, (unsigned long) start, length,
3038 retval);
3039 return retval;
3040 }
3041
3042 static int
3043 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3044 struct bp_target_info *bp_tgt)
3045 {
3046 int retval;
3047
3048 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
3049
3050 fprintf_unfiltered (gdb_stdlog,
3051 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
3052 (unsigned long) bp_tgt->placed_address,
3053 (unsigned long) retval);
3054 return retval;
3055 }
3056
3057 static int
3058 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3059 struct bp_target_info *bp_tgt)
3060 {
3061 int retval;
3062
3063 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
3064
3065 fprintf_unfiltered (gdb_stdlog,
3066 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
3067 (unsigned long) bp_tgt->placed_address,
3068 (unsigned long) retval);
3069 return retval;
3070 }
3071
3072 static int
3073 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3074 {
3075 int retval;
3076
3077 retval = debug_target.to_insert_watchpoint (addr, len, type);
3078
3079 fprintf_unfiltered (gdb_stdlog,
3080 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3081 (unsigned long) addr, len, type, (unsigned long) retval);
3082 return retval;
3083 }
3084
3085 static int
3086 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3087 {
3088 int retval;
3089
3090 retval = debug_target.to_remove_watchpoint (addr, len, type);
3091
3092 fprintf_unfiltered (gdb_stdlog,
3093 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
3094 (unsigned long) addr, len, type, (unsigned long) retval);
3095 return retval;
3096 }
3097
3098 static void
3099 debug_to_terminal_init (void)
3100 {
3101 debug_target.to_terminal_init ();
3102
3103 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3104 }
3105
3106 static void
3107 debug_to_terminal_inferior (void)
3108 {
3109 debug_target.to_terminal_inferior ();
3110
3111 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3112 }
3113
3114 static void
3115 debug_to_terminal_ours_for_output (void)
3116 {
3117 debug_target.to_terminal_ours_for_output ();
3118
3119 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3120 }
3121
3122 static void
3123 debug_to_terminal_ours (void)
3124 {
3125 debug_target.to_terminal_ours ();
3126
3127 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3128 }
3129
3130 static void
3131 debug_to_terminal_save_ours (void)
3132 {
3133 debug_target.to_terminal_save_ours ();
3134
3135 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3136 }
3137
3138 static void
3139 debug_to_terminal_info (char *arg, int from_tty)
3140 {
3141 debug_target.to_terminal_info (arg, from_tty);
3142
3143 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3144 from_tty);
3145 }
3146
3147 static void
3148 debug_to_load (char *args, int from_tty)
3149 {
3150 debug_target.to_load (args, from_tty);
3151
3152 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3153 }
3154
3155 static int
3156 debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
3157 {
3158 int retval;
3159
3160 retval = debug_target.to_lookup_symbol (name, addrp);
3161
3162 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
3163
3164 return retval;
3165 }
3166
3167 static void
3168 debug_to_post_startup_inferior (ptid_t ptid)
3169 {
3170 debug_target.to_post_startup_inferior (ptid);
3171
3172 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3173 PIDGET (ptid));
3174 }
3175
3176 static void
3177 debug_to_acknowledge_created_inferior (int pid)
3178 {
3179 debug_target.to_acknowledge_created_inferior (pid);
3180
3181 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
3182 pid);
3183 }
3184
3185 static void
3186 debug_to_insert_fork_catchpoint (int pid)
3187 {
3188 debug_target.to_insert_fork_catchpoint (pid);
3189
3190 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3191 pid);
3192 }
3193
3194 static int
3195 debug_to_remove_fork_catchpoint (int pid)
3196 {
3197 int retval;
3198
3199 retval = debug_target.to_remove_fork_catchpoint (pid);
3200
3201 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3202 pid, retval);
3203
3204 return retval;
3205 }
3206
3207 static void
3208 debug_to_insert_vfork_catchpoint (int pid)
3209 {
3210 debug_target.to_insert_vfork_catchpoint (pid);
3211
3212 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3213 pid);
3214 }
3215
3216 static int
3217 debug_to_remove_vfork_catchpoint (int pid)
3218 {
3219 int retval;
3220
3221 retval = debug_target.to_remove_vfork_catchpoint (pid);
3222
3223 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
3224 pid, retval);
3225
3226 return retval;
3227 }
3228
3229 static void
3230 debug_to_insert_exec_catchpoint (int pid)
3231 {
3232 debug_target.to_insert_exec_catchpoint (pid);
3233
3234 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3235 pid);
3236 }
3237
3238 static int
3239 debug_to_remove_exec_catchpoint (int pid)
3240 {
3241 int retval;
3242
3243 retval = debug_target.to_remove_exec_catchpoint (pid);
3244
3245 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
3246 pid, retval);
3247
3248 return retval;
3249 }
3250
3251 static int
3252 debug_to_has_exited (int pid, int wait_status, int *exit_status)
3253 {
3254 int has_exited;
3255
3256 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3257
3258 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
3259 pid, wait_status, *exit_status, has_exited);
3260
3261 return has_exited;
3262 }
3263
3264 static int
3265 debug_to_can_run (void)
3266 {
3267 int retval;
3268
3269 retval = debug_target.to_can_run ();
3270
3271 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
3272
3273 return retval;
3274 }
3275
3276 static void
3277 debug_to_notice_signals (ptid_t ptid)
3278 {
3279 debug_target.to_notice_signals (ptid);
3280
3281 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3282 PIDGET (ptid));
3283 }
3284
3285 static struct gdbarch *
3286 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3287 {
3288 struct gdbarch *retval;
3289
3290 retval = debug_target.to_thread_architecture (ops, ptid);
3291
3292 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %p [%s]\n",
3293 target_pid_to_str (ptid), retval,
3294 gdbarch_bfd_arch_info (retval)->printable_name);
3295 return retval;
3296 }
3297
3298 static void
3299 debug_to_stop (ptid_t ptid)
3300 {
3301 debug_target.to_stop (ptid);
3302
3303 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3304 target_pid_to_str (ptid));
3305 }
3306
3307 static void
3308 debug_to_rcmd (char *command,
3309 struct ui_file *outbuf)
3310 {
3311 debug_target.to_rcmd (command, outbuf);
3312 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3313 }
3314
3315 static char *
3316 debug_to_pid_to_exec_file (int pid)
3317 {
3318 char *exec_file;
3319
3320 exec_file = debug_target.to_pid_to_exec_file (pid);
3321
3322 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
3323 pid, exec_file);
3324
3325 return exec_file;
3326 }
3327
3328 static void
3329 setup_target_debug (void)
3330 {
3331 memcpy (&debug_target, &current_target, sizeof debug_target);
3332
3333 current_target.to_open = debug_to_open;
3334 current_target.to_post_attach = debug_to_post_attach;
3335 current_target.to_prepare_to_store = debug_to_prepare_to_store;
3336 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
3337 current_target.to_files_info = debug_to_files_info;
3338 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3339 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
3340 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3341 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3342 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3343 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3344 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3345 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3346 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3347 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
3348 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
3349 current_target.to_terminal_init = debug_to_terminal_init;
3350 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3351 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3352 current_target.to_terminal_ours = debug_to_terminal_ours;
3353 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
3354 current_target.to_terminal_info = debug_to_terminal_info;
3355 current_target.to_load = debug_to_load;
3356 current_target.to_lookup_symbol = debug_to_lookup_symbol;
3357 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3358 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
3359 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3360 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3361 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3362 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
3363 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3364 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
3365 current_target.to_has_exited = debug_to_has_exited;
3366 current_target.to_can_run = debug_to_can_run;
3367 current_target.to_notice_signals = debug_to_notice_signals;
3368 current_target.to_stop = debug_to_stop;
3369 current_target.to_rcmd = debug_to_rcmd;
3370 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
3371 current_target.to_thread_architecture = debug_to_thread_architecture;
3372 }
3373 \f
3374
3375 static char targ_desc[] =
3376 "Names of targets and files being debugged.\n\
3377 Shows the entire stack of targets currently in use (including the exec-file,\n\
3378 core-file, and process, if any), as well as the symbol file name.";
3379
3380 static void
3381 do_monitor_command (char *cmd,
3382 int from_tty)
3383 {
3384 if ((current_target.to_rcmd
3385 == (void (*) (char *, struct ui_file *)) tcomplain)
3386 || (current_target.to_rcmd == debug_to_rcmd
3387 && (debug_target.to_rcmd
3388 == (void (*) (char *, struct ui_file *)) tcomplain)))
3389 error (_("\"monitor\" command not supported by this target."));
3390 target_rcmd (cmd, gdb_stdtarg);
3391 }
3392
3393 /* Print the name of each layers of our target stack. */
3394
3395 static void
3396 maintenance_print_target_stack (char *cmd, int from_tty)
3397 {
3398 struct target_ops *t;
3399
3400 printf_filtered (_("The current target stack is:\n"));
3401
3402 for (t = target_stack; t != NULL; t = t->beneath)
3403 {
3404 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3405 }
3406 }
3407
3408 /* Controls if async mode is permitted. */
3409 int target_async_permitted = 0;
3410
3411 /* The set command writes to this variable. If the inferior is
3412 executing, linux_nat_async_permitted is *not* updated. */
3413 static int target_async_permitted_1 = 0;
3414
3415 static void
3416 set_maintenance_target_async_permitted (char *args, int from_tty,
3417 struct cmd_list_element *c)
3418 {
3419 if (have_live_inferiors ())
3420 {
3421 target_async_permitted_1 = target_async_permitted;
3422 error (_("Cannot change this setting while the inferior is running."));
3423 }
3424
3425 target_async_permitted = target_async_permitted_1;
3426 }
3427
3428 static void
3429 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3430 struct cmd_list_element *c,
3431 const char *value)
3432 {
3433 fprintf_filtered (file, _("\
3434 Controlling the inferior in asynchronous mode is %s.\n"), value);
3435 }
3436
3437 void
3438 initialize_targets (void)
3439 {
3440 init_dummy_target ();
3441 push_target (&dummy_target);
3442
3443 add_info ("target", target_info, targ_desc);
3444 add_info ("files", target_info, targ_desc);
3445
3446 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3447 Set target debugging."), _("\
3448 Show target debugging."), _("\
3449 When non-zero, target debugging is enabled. Higher numbers are more\n\
3450 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
3451 command."),
3452 NULL,
3453 show_targetdebug,
3454 &setdebuglist, &showdebuglist);
3455
3456 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
3457 &trust_readonly, _("\
3458 Set mode for reading from readonly sections."), _("\
3459 Show mode for reading from readonly sections."), _("\
3460 When this mode is on, memory reads from readonly sections (such as .text)\n\
3461 will be read from the object file instead of from the target. This will\n\
3462 result in significant performance improvement for remote targets."),
3463 NULL,
3464 show_trust_readonly,
3465 &setlist, &showlist);
3466
3467 add_com ("monitor", class_obscure, do_monitor_command,
3468 _("Send a command to the remote monitor (remote targets only)."));
3469
3470 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3471 _("Print the name of each layer of the internal target stack."),
3472 &maintenanceprintlist);
3473
3474 add_setshow_boolean_cmd ("target-async", no_class,
3475 &target_async_permitted_1, _("\
3476 Set whether gdb controls the inferior in asynchronous mode."), _("\
3477 Show whether gdb controls the inferior in asynchronous mode."), _("\
3478 Tells gdb whether to control the inferior in asynchronous mode."),
3479 set_maintenance_target_async_permitted,
3480 show_maintenance_target_async_permitted,
3481 &setlist,
3482 &showlist);
3483
3484 target_dcache = dcache_init ();
3485 }