*** empty log message ***
[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 if (readbuf != NULL)
1229 res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
1230 reg_len, 0);
1231 else
1232 /* FIXME drow/2006-08-09: If we're going to preserve const
1233 correctness dcache_xfer_memory should take readbuf and
1234 writebuf. */
1235 res = dcache_xfer_memory (ops, target_dcache, memaddr,
1236 (void *) writebuf,
1237 reg_len, 1);
1238 if (res <= 0)
1239 return -1;
1240 else
1241 {
1242 if (readbuf && !show_memory_breakpoints)
1243 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1244 return res;
1245 }
1246 }
1247
1248 /* If none of those methods found the memory we wanted, fall back
1249 to a target partial transfer. Normally a single call to
1250 to_xfer_partial is enough; if it doesn't recognize an object
1251 it will call the to_xfer_partial of the next target down.
1252 But for memory this won't do. Memory is the only target
1253 object which can be read from more than one valid target.
1254 A core file, for instance, could have some of memory but
1255 delegate other bits to the target below it. So, we must
1256 manually try all targets. */
1257
1258 do
1259 {
1260 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1261 readbuf, writebuf, memaddr, reg_len);
1262 if (res > 0)
1263 break;
1264
1265 /* We want to continue past core files to executables, but not
1266 past a running target's memory. */
1267 if (ops->to_has_all_memory (ops))
1268 break;
1269
1270 ops = ops->beneath;
1271 }
1272 while (ops != NULL);
1273
1274 if (readbuf && !show_memory_breakpoints)
1275 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1276
1277 /* If we still haven't got anything, return the last error. We
1278 give up. */
1279 return res;
1280 }
1281
1282 static void
1283 restore_show_memory_breakpoints (void *arg)
1284 {
1285 show_memory_breakpoints = (uintptr_t) arg;
1286 }
1287
1288 struct cleanup *
1289 make_show_memory_breakpoints_cleanup (int show)
1290 {
1291 int current = show_memory_breakpoints;
1292 show_memory_breakpoints = show;
1293
1294 return make_cleanup (restore_show_memory_breakpoints,
1295 (void *) (uintptr_t) current);
1296 }
1297
1298 static LONGEST
1299 target_xfer_partial (struct target_ops *ops,
1300 enum target_object object, const char *annex,
1301 void *readbuf, const void *writebuf,
1302 ULONGEST offset, LONGEST len)
1303 {
1304 LONGEST retval;
1305
1306 gdb_assert (ops->to_xfer_partial != NULL);
1307
1308 /* If this is a memory transfer, let the memory-specific code
1309 have a look at it instead. Memory transfers are more
1310 complicated. */
1311 if (object == TARGET_OBJECT_MEMORY)
1312 retval = memory_xfer_partial (ops, readbuf, writebuf, offset, len);
1313 else
1314 {
1315 enum target_object raw_object = object;
1316
1317 /* If this is a raw memory transfer, request the normal
1318 memory object from other layers. */
1319 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1320 raw_object = TARGET_OBJECT_MEMORY;
1321
1322 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1323 writebuf, offset, len);
1324 }
1325
1326 if (targetdebug)
1327 {
1328 const unsigned char *myaddr = NULL;
1329
1330 fprintf_unfiltered (gdb_stdlog,
1331 "%s:target_xfer_partial (%d, %s, %s, %s, %s, %s) = %s",
1332 ops->to_shortname,
1333 (int) object,
1334 (annex ? annex : "(null)"),
1335 host_address_to_string (readbuf),
1336 host_address_to_string (writebuf),
1337 core_addr_to_string_nz (offset),
1338 plongest (len), plongest (retval));
1339
1340 if (readbuf)
1341 myaddr = readbuf;
1342 if (writebuf)
1343 myaddr = writebuf;
1344 if (retval > 0 && myaddr != NULL)
1345 {
1346 int i;
1347
1348 fputs_unfiltered (", bytes =", gdb_stdlog);
1349 for (i = 0; i < retval; i++)
1350 {
1351 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
1352 {
1353 if (targetdebug < 2 && i > 0)
1354 {
1355 fprintf_unfiltered (gdb_stdlog, " ...");
1356 break;
1357 }
1358 fprintf_unfiltered (gdb_stdlog, "\n");
1359 }
1360
1361 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1362 }
1363 }
1364
1365 fputc_unfiltered ('\n', gdb_stdlog);
1366 }
1367 return retval;
1368 }
1369
1370 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1371 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1372 if any error occurs.
1373
1374 If an error occurs, no guarantee is made about the contents of the data at
1375 MYADDR. In particular, the caller should not depend upon partial reads
1376 filling the buffer with good data. There is no way for the caller to know
1377 how much good data might have been transfered anyway. Callers that can
1378 deal with partial reads should call target_read (which will retry until
1379 it makes no progress, and then return how much was transferred). */
1380
1381 int
1382 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1383 {
1384 /* Dispatch to the topmost target, not the flattened current_target.
1385 Memory accesses check target->to_has_(all_)memory, and the
1386 flattened target doesn't inherit those. */
1387 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1388 myaddr, memaddr, len) == len)
1389 return 0;
1390 else
1391 return EIO;
1392 }
1393
1394 int
1395 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1396 {
1397 /* Dispatch to the topmost target, not the flattened current_target.
1398 Memory accesses check target->to_has_(all_)memory, and the
1399 flattened target doesn't inherit those. */
1400 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
1401 myaddr, memaddr, len) == len)
1402 return 0;
1403 else
1404 return EIO;
1405 }
1406
1407 /* Fetch the target's memory map. */
1408
1409 VEC(mem_region_s) *
1410 target_memory_map (void)
1411 {
1412 VEC(mem_region_s) *result;
1413 struct mem_region *last_one, *this_one;
1414 int ix;
1415 struct target_ops *t;
1416
1417 if (targetdebug)
1418 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1419
1420 for (t = current_target.beneath; t != NULL; t = t->beneath)
1421 if (t->to_memory_map != NULL)
1422 break;
1423
1424 if (t == NULL)
1425 return NULL;
1426
1427 result = t->to_memory_map (t);
1428 if (result == NULL)
1429 return NULL;
1430
1431 qsort (VEC_address (mem_region_s, result),
1432 VEC_length (mem_region_s, result),
1433 sizeof (struct mem_region), mem_region_cmp);
1434
1435 /* Check that regions do not overlap. Simultaneously assign
1436 a numbering for the "mem" commands to use to refer to
1437 each region. */
1438 last_one = NULL;
1439 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1440 {
1441 this_one->number = ix;
1442
1443 if (last_one && last_one->hi > this_one->lo)
1444 {
1445 warning (_("Overlapping regions in memory map: ignoring"));
1446 VEC_free (mem_region_s, result);
1447 return NULL;
1448 }
1449 last_one = this_one;
1450 }
1451
1452 return result;
1453 }
1454
1455 void
1456 target_flash_erase (ULONGEST address, LONGEST length)
1457 {
1458 struct target_ops *t;
1459
1460 for (t = current_target.beneath; t != NULL; t = t->beneath)
1461 if (t->to_flash_erase != NULL)
1462 {
1463 if (targetdebug)
1464 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1465 hex_string (address), phex (length, 0));
1466 t->to_flash_erase (t, address, length);
1467 return;
1468 }
1469
1470 tcomplain ();
1471 }
1472
1473 void
1474 target_flash_done (void)
1475 {
1476 struct target_ops *t;
1477
1478 for (t = current_target.beneath; t != NULL; t = t->beneath)
1479 if (t->to_flash_done != NULL)
1480 {
1481 if (targetdebug)
1482 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1483 t->to_flash_done (t);
1484 return;
1485 }
1486
1487 tcomplain ();
1488 }
1489
1490 static void
1491 show_trust_readonly (struct ui_file *file, int from_tty,
1492 struct cmd_list_element *c, const char *value)
1493 {
1494 fprintf_filtered (file, _("\
1495 Mode for reading from readonly sections is %s.\n"),
1496 value);
1497 }
1498
1499 /* More generic transfers. */
1500
1501 static LONGEST
1502 default_xfer_partial (struct target_ops *ops, enum target_object object,
1503 const char *annex, gdb_byte *readbuf,
1504 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1505 {
1506 if (object == TARGET_OBJECT_MEMORY
1507 && ops->deprecated_xfer_memory != NULL)
1508 /* If available, fall back to the target's
1509 "deprecated_xfer_memory" method. */
1510 {
1511 int xfered = -1;
1512 errno = 0;
1513 if (writebuf != NULL)
1514 {
1515 void *buffer = xmalloc (len);
1516 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1517 memcpy (buffer, writebuf, len);
1518 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1519 1/*write*/, NULL, ops);
1520 do_cleanups (cleanup);
1521 }
1522 if (readbuf != NULL)
1523 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1524 0/*read*/, NULL, ops);
1525 if (xfered > 0)
1526 return xfered;
1527 else if (xfered == 0 && errno == 0)
1528 /* "deprecated_xfer_memory" uses 0, cross checked against
1529 ERRNO as one indication of an error. */
1530 return 0;
1531 else
1532 return -1;
1533 }
1534 else if (ops->beneath != NULL)
1535 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1536 readbuf, writebuf, offset, len);
1537 else
1538 return -1;
1539 }
1540
1541 /* The xfer_partial handler for the topmost target. Unlike the default,
1542 it does not need to handle memory specially; it just passes all
1543 requests down the stack. */
1544
1545 static LONGEST
1546 current_xfer_partial (struct target_ops *ops, enum target_object object,
1547 const char *annex, gdb_byte *readbuf,
1548 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1549 {
1550 if (ops->beneath != NULL)
1551 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1552 readbuf, writebuf, offset, len);
1553 else
1554 return -1;
1555 }
1556
1557 /* Target vector read/write partial wrapper functions.
1558
1559 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1560 (inbuf, outbuf)", instead of separate read/write methods, make life
1561 easier. */
1562
1563 static LONGEST
1564 target_read_partial (struct target_ops *ops,
1565 enum target_object object,
1566 const char *annex, gdb_byte *buf,
1567 ULONGEST offset, LONGEST len)
1568 {
1569 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1570 }
1571
1572 static LONGEST
1573 target_write_partial (struct target_ops *ops,
1574 enum target_object object,
1575 const char *annex, const gdb_byte *buf,
1576 ULONGEST offset, LONGEST len)
1577 {
1578 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1579 }
1580
1581 /* Wrappers to perform the full transfer. */
1582 LONGEST
1583 target_read (struct target_ops *ops,
1584 enum target_object object,
1585 const char *annex, gdb_byte *buf,
1586 ULONGEST offset, LONGEST len)
1587 {
1588 LONGEST xfered = 0;
1589 while (xfered < len)
1590 {
1591 LONGEST xfer = target_read_partial (ops, object, annex,
1592 (gdb_byte *) buf + xfered,
1593 offset + xfered, len - xfered);
1594 /* Call an observer, notifying them of the xfer progress? */
1595 if (xfer == 0)
1596 return xfered;
1597 if (xfer < 0)
1598 return -1;
1599 xfered += xfer;
1600 QUIT;
1601 }
1602 return len;
1603 }
1604
1605 LONGEST
1606 target_read_until_error (struct target_ops *ops,
1607 enum target_object object,
1608 const char *annex, gdb_byte *buf,
1609 ULONGEST offset, LONGEST len)
1610 {
1611 LONGEST xfered = 0;
1612 while (xfered < len)
1613 {
1614 LONGEST xfer = target_read_partial (ops, object, annex,
1615 (gdb_byte *) buf + xfered,
1616 offset + xfered, len - xfered);
1617 /* Call an observer, notifying them of the xfer progress? */
1618 if (xfer == 0)
1619 return xfered;
1620 if (xfer < 0)
1621 {
1622 /* We've got an error. Try to read in smaller blocks. */
1623 ULONGEST start = offset + xfered;
1624 ULONGEST remaining = len - xfered;
1625 ULONGEST half;
1626
1627 /* If an attempt was made to read a random memory address,
1628 it's likely that the very first byte is not accessible.
1629 Try reading the first byte, to avoid doing log N tries
1630 below. */
1631 xfer = target_read_partial (ops, object, annex,
1632 (gdb_byte *) buf + xfered, start, 1);
1633 if (xfer <= 0)
1634 return xfered;
1635 start += 1;
1636 remaining -= 1;
1637 half = remaining/2;
1638
1639 while (half > 0)
1640 {
1641 xfer = target_read_partial (ops, object, annex,
1642 (gdb_byte *) buf + xfered,
1643 start, half);
1644 if (xfer == 0)
1645 return xfered;
1646 if (xfer < 0)
1647 {
1648 remaining = half;
1649 }
1650 else
1651 {
1652 /* We have successfully read the first half. So, the
1653 error must be in the second half. Adjust start and
1654 remaining to point at the second half. */
1655 xfered += xfer;
1656 start += xfer;
1657 remaining -= xfer;
1658 }
1659 half = remaining/2;
1660 }
1661
1662 return xfered;
1663 }
1664 xfered += xfer;
1665 QUIT;
1666 }
1667 return len;
1668 }
1669
1670
1671 /* An alternative to target_write with progress callbacks. */
1672
1673 LONGEST
1674 target_write_with_progress (struct target_ops *ops,
1675 enum target_object object,
1676 const char *annex, const gdb_byte *buf,
1677 ULONGEST offset, LONGEST len,
1678 void (*progress) (ULONGEST, void *), void *baton)
1679 {
1680 LONGEST xfered = 0;
1681
1682 /* Give the progress callback a chance to set up. */
1683 if (progress)
1684 (*progress) (0, baton);
1685
1686 while (xfered < len)
1687 {
1688 LONGEST xfer = target_write_partial (ops, object, annex,
1689 (gdb_byte *) buf + xfered,
1690 offset + xfered, len - xfered);
1691
1692 if (xfer == 0)
1693 return xfered;
1694 if (xfer < 0)
1695 return -1;
1696
1697 if (progress)
1698 (*progress) (xfer, baton);
1699
1700 xfered += xfer;
1701 QUIT;
1702 }
1703 return len;
1704 }
1705
1706 LONGEST
1707 target_write (struct target_ops *ops,
1708 enum target_object object,
1709 const char *annex, const gdb_byte *buf,
1710 ULONGEST offset, LONGEST len)
1711 {
1712 return target_write_with_progress (ops, object, annex, buf, offset, len,
1713 NULL, NULL);
1714 }
1715
1716 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1717 the size of the transferred data. PADDING additional bytes are
1718 available in *BUF_P. This is a helper function for
1719 target_read_alloc; see the declaration of that function for more
1720 information. */
1721
1722 static LONGEST
1723 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1724 const char *annex, gdb_byte **buf_p, int padding)
1725 {
1726 size_t buf_alloc, buf_pos;
1727 gdb_byte *buf;
1728 LONGEST n;
1729
1730 /* This function does not have a length parameter; it reads the
1731 entire OBJECT). Also, it doesn't support objects fetched partly
1732 from one target and partly from another (in a different stratum,
1733 e.g. a core file and an executable). Both reasons make it
1734 unsuitable for reading memory. */
1735 gdb_assert (object != TARGET_OBJECT_MEMORY);
1736
1737 /* Start by reading up to 4K at a time. The target will throttle
1738 this number down if necessary. */
1739 buf_alloc = 4096;
1740 buf = xmalloc (buf_alloc);
1741 buf_pos = 0;
1742 while (1)
1743 {
1744 n = target_read_partial (ops, object, annex, &buf[buf_pos],
1745 buf_pos, buf_alloc - buf_pos - padding);
1746 if (n < 0)
1747 {
1748 /* An error occurred. */
1749 xfree (buf);
1750 return -1;
1751 }
1752 else if (n == 0)
1753 {
1754 /* Read all there was. */
1755 if (buf_pos == 0)
1756 xfree (buf);
1757 else
1758 *buf_p = buf;
1759 return buf_pos;
1760 }
1761
1762 buf_pos += n;
1763
1764 /* If the buffer is filling up, expand it. */
1765 if (buf_alloc < buf_pos * 2)
1766 {
1767 buf_alloc *= 2;
1768 buf = xrealloc (buf, buf_alloc);
1769 }
1770
1771 QUIT;
1772 }
1773 }
1774
1775 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1776 the size of the transferred data. See the declaration in "target.h"
1777 function for more information about the return value. */
1778
1779 LONGEST
1780 target_read_alloc (struct target_ops *ops, enum target_object object,
1781 const char *annex, gdb_byte **buf_p)
1782 {
1783 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1784 }
1785
1786 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1787 returned as a string, allocated using xmalloc. If an error occurs
1788 or the transfer is unsupported, NULL is returned. Empty objects
1789 are returned as allocated but empty strings. A warning is issued
1790 if the result contains any embedded NUL bytes. */
1791
1792 char *
1793 target_read_stralloc (struct target_ops *ops, enum target_object object,
1794 const char *annex)
1795 {
1796 gdb_byte *buffer;
1797 LONGEST transferred;
1798
1799 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1800
1801 if (transferred < 0)
1802 return NULL;
1803
1804 if (transferred == 0)
1805 return xstrdup ("");
1806
1807 buffer[transferred] = 0;
1808 if (strlen (buffer) < transferred)
1809 warning (_("target object %d, annex %s, "
1810 "contained unexpected null characters"),
1811 (int) object, annex ? annex : "(none)");
1812
1813 return (char *) buffer;
1814 }
1815
1816 /* Memory transfer methods. */
1817
1818 void
1819 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
1820 LONGEST len)
1821 {
1822 /* This method is used to read from an alternate, non-current
1823 target. This read must bypass the overlay support (as symbols
1824 don't match this target), and GDB's internal cache (wrong cache
1825 for this target). */
1826 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
1827 != len)
1828 memory_error (EIO, addr);
1829 }
1830
1831 ULONGEST
1832 get_target_memory_unsigned (struct target_ops *ops,
1833 CORE_ADDR addr, int len, enum bfd_endian byte_order)
1834 {
1835 gdb_byte buf[sizeof (ULONGEST)];
1836
1837 gdb_assert (len <= sizeof (buf));
1838 get_target_memory (ops, addr, buf, len);
1839 return extract_unsigned_integer (buf, len, byte_order);
1840 }
1841
1842 static void
1843 target_info (char *args, int from_tty)
1844 {
1845 struct target_ops *t;
1846 int has_all_mem = 0;
1847
1848 if (symfile_objfile != NULL)
1849 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
1850
1851 for (t = target_stack; t != NULL; t = t->beneath)
1852 {
1853 if (!(*t->to_has_memory) (t))
1854 continue;
1855
1856 if ((int) (t->to_stratum) <= (int) dummy_stratum)
1857 continue;
1858 if (has_all_mem)
1859 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
1860 printf_unfiltered ("%s:\n", t->to_longname);
1861 (t->to_files_info) (t);
1862 has_all_mem = (*t->to_has_all_memory) (t);
1863 }
1864 }
1865
1866 /* This function is called before any new inferior is created, e.g.
1867 by running a program, attaching, or connecting to a target.
1868 It cleans up any state from previous invocations which might
1869 change between runs. This is a subset of what target_preopen
1870 resets (things which might change between targets). */
1871
1872 void
1873 target_pre_inferior (int from_tty)
1874 {
1875 /* Clear out solib state. Otherwise the solib state of the previous
1876 inferior might have survived and is entirely wrong for the new
1877 target. This has been observed on GNU/Linux using glibc 2.3. How
1878 to reproduce:
1879
1880 bash$ ./foo&
1881 [1] 4711
1882 bash$ ./foo&
1883 [1] 4712
1884 bash$ gdb ./foo
1885 [...]
1886 (gdb) attach 4711
1887 (gdb) detach
1888 (gdb) attach 4712
1889 Cannot access memory at address 0xdeadbeef
1890 */
1891
1892 /* In some OSs, the shared library list is the same/global/shared
1893 across inferiors. If code is shared between processes, so are
1894 memory regions and features. */
1895 if (!gdbarch_has_global_solist (target_gdbarch))
1896 {
1897 no_shared_libraries (NULL, from_tty);
1898
1899 invalidate_target_mem_regions ();
1900
1901 target_clear_description ();
1902 }
1903 }
1904
1905 /* Callback for iterate_over_inferiors. Gets rid of the given
1906 inferior. */
1907
1908 static int
1909 dispose_inferior (struct inferior *inf, void *args)
1910 {
1911 struct thread_info *thread;
1912
1913 thread = any_thread_of_process (inf->pid);
1914 if (thread)
1915 {
1916 switch_to_thread (thread->ptid);
1917
1918 /* Core inferiors actually should be detached, not killed. */
1919 if (target_has_execution)
1920 target_kill ();
1921 else
1922 target_detach (NULL, 0);
1923 }
1924
1925 return 0;
1926 }
1927
1928 /* This is to be called by the open routine before it does
1929 anything. */
1930
1931 void
1932 target_preopen (int from_tty)
1933 {
1934 dont_repeat ();
1935
1936 if (have_inferiors ())
1937 {
1938 if (!from_tty
1939 || !have_live_inferiors ()
1940 || query (_("A program is being debugged already. Kill it? ")))
1941 iterate_over_inferiors (dispose_inferior, NULL);
1942 else
1943 error (_("Program not killed."));
1944 }
1945
1946 /* Calling target_kill may remove the target from the stack. But if
1947 it doesn't (which seems like a win for UDI), remove it now. */
1948 /* Leave the exec target, though. The user may be switching from a
1949 live process to a core of the same program. */
1950 pop_all_targets_above (file_stratum, 0);
1951
1952 target_pre_inferior (from_tty);
1953 }
1954
1955 /* Detach a target after doing deferred register stores. */
1956
1957 void
1958 target_detach (char *args, int from_tty)
1959 {
1960 struct target_ops* t;
1961
1962 if (gdbarch_has_global_breakpoints (target_gdbarch))
1963 /* Don't remove global breakpoints here. They're removed on
1964 disconnection from the target. */
1965 ;
1966 else
1967 /* If we're in breakpoints-always-inserted mode, have to remove
1968 them before detaching. */
1969 remove_breakpoints ();
1970
1971 for (t = current_target.beneath; t != NULL; t = t->beneath)
1972 {
1973 if (t->to_detach != NULL)
1974 {
1975 t->to_detach (t, args, from_tty);
1976 if (targetdebug)
1977 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
1978 args, from_tty);
1979 return;
1980 }
1981 }
1982
1983 internal_error (__FILE__, __LINE__, "could not find a target to detach");
1984 }
1985
1986 void
1987 target_disconnect (char *args, int from_tty)
1988 {
1989 struct target_ops *t;
1990
1991 /* If we're in breakpoints-always-inserted mode or if breakpoints
1992 are global across processes, we have to remove them before
1993 disconnecting. */
1994 remove_breakpoints ();
1995
1996 for (t = current_target.beneath; t != NULL; t = t->beneath)
1997 if (t->to_disconnect != NULL)
1998 {
1999 if (targetdebug)
2000 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2001 args, from_tty);
2002 t->to_disconnect (t, args, from_tty);
2003 return;
2004 }
2005
2006 tcomplain ();
2007 }
2008
2009 ptid_t
2010 target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
2011 {
2012 struct target_ops *t;
2013
2014 for (t = current_target.beneath; t != NULL; t = t->beneath)
2015 {
2016 if (t->to_wait != NULL)
2017 {
2018 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
2019
2020 if (targetdebug)
2021 {
2022 char *status_string;
2023
2024 status_string = target_waitstatus_to_string (status);
2025 fprintf_unfiltered (gdb_stdlog,
2026 "target_wait (%d, status) = %d, %s\n",
2027 PIDGET (ptid), PIDGET (retval),
2028 status_string);
2029 xfree (status_string);
2030 }
2031
2032 return retval;
2033 }
2034 }
2035
2036 noprocess ();
2037 }
2038
2039 char *
2040 target_pid_to_str (ptid_t ptid)
2041 {
2042 struct target_ops *t;
2043
2044 for (t = current_target.beneath; t != NULL; t = t->beneath)
2045 {
2046 if (t->to_pid_to_str != NULL)
2047 return (*t->to_pid_to_str) (t, ptid);
2048 }
2049
2050 return normal_pid_to_str (ptid);
2051 }
2052
2053 void
2054 target_resume (ptid_t ptid, int step, enum target_signal signal)
2055 {
2056 struct target_ops *t;
2057
2058 dcache_invalidate (target_dcache);
2059
2060 for (t = current_target.beneath; t != NULL; t = t->beneath)
2061 {
2062 if (t->to_resume != NULL)
2063 {
2064 t->to_resume (t, ptid, step, signal);
2065 if (targetdebug)
2066 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2067 PIDGET (ptid),
2068 step ? "step" : "continue",
2069 target_signal_to_name (signal));
2070
2071 set_executing (ptid, 1);
2072 set_running (ptid, 1);
2073 clear_inline_frame_state (ptid);
2074 return;
2075 }
2076 }
2077
2078 noprocess ();
2079 }
2080 /* Look through the list of possible targets for a target that can
2081 follow forks. */
2082
2083 int
2084 target_follow_fork (int follow_child)
2085 {
2086 struct target_ops *t;
2087
2088 for (t = current_target.beneath; t != NULL; t = t->beneath)
2089 {
2090 if (t->to_follow_fork != NULL)
2091 {
2092 int retval = t->to_follow_fork (t, follow_child);
2093 if (targetdebug)
2094 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2095 follow_child, retval);
2096 return retval;
2097 }
2098 }
2099
2100 /* Some target returned a fork event, but did not know how to follow it. */
2101 internal_error (__FILE__, __LINE__,
2102 "could not find a target to follow fork");
2103 }
2104
2105 void
2106 target_mourn_inferior (void)
2107 {
2108 struct target_ops *t;
2109 for (t = current_target.beneath; t != NULL; t = t->beneath)
2110 {
2111 if (t->to_mourn_inferior != NULL)
2112 {
2113 t->to_mourn_inferior (t);
2114 if (targetdebug)
2115 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2116
2117 /* We no longer need to keep handles on any of the object files.
2118 Make sure to release them to avoid unnecessarily locking any
2119 of them while we're not actually debugging. */
2120 bfd_cache_close_all ();
2121
2122 return;
2123 }
2124 }
2125
2126 internal_error (__FILE__, __LINE__,
2127 "could not find a target to follow mourn inferiour");
2128 }
2129
2130 /* Look for a target which can describe architectural features, starting
2131 from TARGET. If we find one, return its description. */
2132
2133 const struct target_desc *
2134 target_read_description (struct target_ops *target)
2135 {
2136 struct target_ops *t;
2137
2138 for (t = target; t != NULL; t = t->beneath)
2139 if (t->to_read_description != NULL)
2140 {
2141 const struct target_desc *tdesc;
2142
2143 tdesc = t->to_read_description (t);
2144 if (tdesc)
2145 return tdesc;
2146 }
2147
2148 return NULL;
2149 }
2150
2151 /* The default implementation of to_search_memory.
2152 This implements a basic search of memory, reading target memory and
2153 performing the search here (as opposed to performing the search in on the
2154 target side with, for example, gdbserver). */
2155
2156 int
2157 simple_search_memory (struct target_ops *ops,
2158 CORE_ADDR start_addr, ULONGEST search_space_len,
2159 const gdb_byte *pattern, ULONGEST pattern_len,
2160 CORE_ADDR *found_addrp)
2161 {
2162 /* NOTE: also defined in find.c testcase. */
2163 #define SEARCH_CHUNK_SIZE 16000
2164 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2165 /* Buffer to hold memory contents for searching. */
2166 gdb_byte *search_buf;
2167 unsigned search_buf_size;
2168 struct cleanup *old_cleanups;
2169
2170 search_buf_size = chunk_size + pattern_len - 1;
2171
2172 /* No point in trying to allocate a buffer larger than the search space. */
2173 if (search_space_len < search_buf_size)
2174 search_buf_size = search_space_len;
2175
2176 search_buf = malloc (search_buf_size);
2177 if (search_buf == NULL)
2178 error (_("Unable to allocate memory to perform the search."));
2179 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2180
2181 /* Prime the search buffer. */
2182
2183 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2184 search_buf, start_addr, search_buf_size) != search_buf_size)
2185 {
2186 warning (_("Unable to access target memory at %s, halting search."),
2187 hex_string (start_addr));
2188 do_cleanups (old_cleanups);
2189 return -1;
2190 }
2191
2192 /* Perform the search.
2193
2194 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2195 When we've scanned N bytes we copy the trailing bytes to the start and
2196 read in another N bytes. */
2197
2198 while (search_space_len >= pattern_len)
2199 {
2200 gdb_byte *found_ptr;
2201 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2202
2203 found_ptr = memmem (search_buf, nr_search_bytes,
2204 pattern, pattern_len);
2205
2206 if (found_ptr != NULL)
2207 {
2208 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
2209 *found_addrp = found_addr;
2210 do_cleanups (old_cleanups);
2211 return 1;
2212 }
2213
2214 /* Not found in this chunk, skip to next chunk. */
2215
2216 /* Don't let search_space_len wrap here, it's unsigned. */
2217 if (search_space_len >= chunk_size)
2218 search_space_len -= chunk_size;
2219 else
2220 search_space_len = 0;
2221
2222 if (search_space_len >= pattern_len)
2223 {
2224 unsigned keep_len = search_buf_size - chunk_size;
2225 CORE_ADDR read_addr = start_addr + keep_len;
2226 int nr_to_read;
2227
2228 /* Copy the trailing part of the previous iteration to the front
2229 of the buffer for the next iteration. */
2230 gdb_assert (keep_len == pattern_len - 1);
2231 memcpy (search_buf, search_buf + chunk_size, keep_len);
2232
2233 nr_to_read = min (search_space_len - keep_len, chunk_size);
2234
2235 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2236 search_buf + keep_len, read_addr,
2237 nr_to_read) != nr_to_read)
2238 {
2239 warning (_("Unable to access target memory at %s, halting search."),
2240 hex_string (read_addr));
2241 do_cleanups (old_cleanups);
2242 return -1;
2243 }
2244
2245 start_addr += chunk_size;
2246 }
2247 }
2248
2249 /* Not found. */
2250
2251 do_cleanups (old_cleanups);
2252 return 0;
2253 }
2254
2255 /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2256 sequence of bytes in PATTERN with length PATTERN_LEN.
2257
2258 The result is 1 if found, 0 if not found, and -1 if there was an error
2259 requiring halting of the search (e.g. memory read error).
2260 If the pattern is found the address is recorded in FOUND_ADDRP. */
2261
2262 int
2263 target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2264 const gdb_byte *pattern, ULONGEST pattern_len,
2265 CORE_ADDR *found_addrp)
2266 {
2267 struct target_ops *t;
2268 int found;
2269
2270 /* We don't use INHERIT to set current_target.to_search_memory,
2271 so we have to scan the target stack and handle targetdebug
2272 ourselves. */
2273
2274 if (targetdebug)
2275 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2276 hex_string (start_addr));
2277
2278 for (t = current_target.beneath; t != NULL; t = t->beneath)
2279 if (t->to_search_memory != NULL)
2280 break;
2281
2282 if (t != NULL)
2283 {
2284 found = t->to_search_memory (t, start_addr, search_space_len,
2285 pattern, pattern_len, found_addrp);
2286 }
2287 else
2288 {
2289 /* If a special version of to_search_memory isn't available, use the
2290 simple version. */
2291 found = simple_search_memory (current_target.beneath,
2292 start_addr, search_space_len,
2293 pattern, pattern_len, found_addrp);
2294 }
2295
2296 if (targetdebug)
2297 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2298
2299 return found;
2300 }
2301
2302 /* Look through the currently pushed targets. If none of them will
2303 be able to restart the currently running process, issue an error
2304 message. */
2305
2306 void
2307 target_require_runnable (void)
2308 {
2309 struct target_ops *t;
2310
2311 for (t = target_stack; t != NULL; t = t->beneath)
2312 {
2313 /* If this target knows how to create a new program, then
2314 assume we will still be able to after killing the current
2315 one. Either killing and mourning will not pop T, or else
2316 find_default_run_target will find it again. */
2317 if (t->to_create_inferior != NULL)
2318 return;
2319
2320 /* Do not worry about thread_stratum targets that can not
2321 create inferiors. Assume they will be pushed again if
2322 necessary, and continue to the process_stratum. */
2323 if (t->to_stratum == thread_stratum
2324 || t->to_stratum == arch_stratum)
2325 continue;
2326
2327 error (_("\
2328 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
2329 t->to_shortname);
2330 }
2331
2332 /* This function is only called if the target is running. In that
2333 case there should have been a process_stratum target and it
2334 should either know how to create inferiors, or not... */
2335 internal_error (__FILE__, __LINE__, "No targets found");
2336 }
2337
2338 /* Look through the list of possible targets for a target that can
2339 execute a run or attach command without any other data. This is
2340 used to locate the default process stratum.
2341
2342 If DO_MESG is not NULL, the result is always valid (error() is
2343 called for errors); else, return NULL on error. */
2344
2345 static struct target_ops *
2346 find_default_run_target (char *do_mesg)
2347 {
2348 struct target_ops **t;
2349 struct target_ops *runable = NULL;
2350 int count;
2351
2352 count = 0;
2353
2354 for (t = target_structs; t < target_structs + target_struct_size;
2355 ++t)
2356 {
2357 if ((*t)->to_can_run && target_can_run (*t))
2358 {
2359 runable = *t;
2360 ++count;
2361 }
2362 }
2363
2364 if (count != 1)
2365 {
2366 if (do_mesg)
2367 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2368 else
2369 return NULL;
2370 }
2371
2372 return runable;
2373 }
2374
2375 void
2376 find_default_attach (struct target_ops *ops, char *args, int from_tty)
2377 {
2378 struct target_ops *t;
2379
2380 t = find_default_run_target ("attach");
2381 (t->to_attach) (t, args, from_tty);
2382 return;
2383 }
2384
2385 void
2386 find_default_create_inferior (struct target_ops *ops,
2387 char *exec_file, char *allargs, char **env,
2388 int from_tty)
2389 {
2390 struct target_ops *t;
2391
2392 t = find_default_run_target ("run");
2393 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
2394 return;
2395 }
2396
2397 static int
2398 find_default_can_async_p (void)
2399 {
2400 struct target_ops *t;
2401
2402 /* This may be called before the target is pushed on the stack;
2403 look for the default process stratum. If there's none, gdb isn't
2404 configured with a native debugger, and target remote isn't
2405 connected yet. */
2406 t = find_default_run_target (NULL);
2407 if (t && t->to_can_async_p)
2408 return (t->to_can_async_p) ();
2409 return 0;
2410 }
2411
2412 static int
2413 find_default_is_async_p (void)
2414 {
2415 struct target_ops *t;
2416
2417 /* This may be called before the target is pushed on the stack;
2418 look for the default process stratum. If there's none, gdb isn't
2419 configured with a native debugger, and target remote isn't
2420 connected yet. */
2421 t = find_default_run_target (NULL);
2422 if (t && t->to_is_async_p)
2423 return (t->to_is_async_p) ();
2424 return 0;
2425 }
2426
2427 static int
2428 find_default_supports_non_stop (void)
2429 {
2430 struct target_ops *t;
2431
2432 t = find_default_run_target (NULL);
2433 if (t && t->to_supports_non_stop)
2434 return (t->to_supports_non_stop) ();
2435 return 0;
2436 }
2437
2438 int
2439 target_supports_non_stop (void)
2440 {
2441 struct target_ops *t;
2442 for (t = &current_target; t != NULL; t = t->beneath)
2443 if (t->to_supports_non_stop)
2444 return t->to_supports_non_stop ();
2445
2446 return 0;
2447 }
2448
2449
2450 char *
2451 target_get_osdata (const char *type)
2452 {
2453 char *document;
2454 struct target_ops *t;
2455
2456 /* If we're already connected to something that can get us OS
2457 related data, use it. Otherwise, try using the native
2458 target. */
2459 if (current_target.to_stratum >= process_stratum)
2460 t = current_target.beneath;
2461 else
2462 t = find_default_run_target ("get OS data");
2463
2464 if (!t)
2465 return NULL;
2466
2467 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
2468 }
2469
2470 static int
2471 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2472 {
2473 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
2474 }
2475
2476 static int
2477 default_watchpoint_addr_within_range (struct target_ops *target,
2478 CORE_ADDR addr,
2479 CORE_ADDR start, int length)
2480 {
2481 return addr >= start && addr < start + length;
2482 }
2483
2484 static struct gdbarch *
2485 default_thread_architecture (struct target_ops *ops, ptid_t ptid)
2486 {
2487 return target_gdbarch;
2488 }
2489
2490 static int
2491 return_zero (void)
2492 {
2493 return 0;
2494 }
2495
2496 static int
2497 return_one (void)
2498 {
2499 return 1;
2500 }
2501
2502 static int
2503 return_minus_one (void)
2504 {
2505 return -1;
2506 }
2507
2508 /* Find a single runnable target in the stack and return it. If for
2509 some reason there is more than one, return NULL. */
2510
2511 struct target_ops *
2512 find_run_target (void)
2513 {
2514 struct target_ops **t;
2515 struct target_ops *runable = NULL;
2516 int count;
2517
2518 count = 0;
2519
2520 for (t = target_structs; t < target_structs + target_struct_size; ++t)
2521 {
2522 if ((*t)->to_can_run && target_can_run (*t))
2523 {
2524 runable = *t;
2525 ++count;
2526 }
2527 }
2528
2529 return (count == 1 ? runable : NULL);
2530 }
2531
2532 /* Find a single core_stratum target in the list of targets and return it.
2533 If for some reason there is more than one, return NULL. */
2534
2535 struct target_ops *
2536 find_core_target (void)
2537 {
2538 struct target_ops **t;
2539 struct target_ops *runable = NULL;
2540 int count;
2541
2542 count = 0;
2543
2544 for (t = target_structs; t < target_structs + target_struct_size;
2545 ++t)
2546 {
2547 if ((*t)->to_stratum == core_stratum)
2548 {
2549 runable = *t;
2550 ++count;
2551 }
2552 }
2553
2554 return (count == 1 ? runable : NULL);
2555 }
2556
2557 /*
2558 * Find the next target down the stack from the specified target.
2559 */
2560
2561 struct target_ops *
2562 find_target_beneath (struct target_ops *t)
2563 {
2564 return t->beneath;
2565 }
2566
2567 \f
2568 /* The inferior process has died. Long live the inferior! */
2569
2570 void
2571 generic_mourn_inferior (void)
2572 {
2573 ptid_t ptid;
2574
2575 ptid = inferior_ptid;
2576 inferior_ptid = null_ptid;
2577
2578 if (!ptid_equal (ptid, null_ptid))
2579 {
2580 int pid = ptid_get_pid (ptid);
2581 delete_inferior (pid);
2582 }
2583
2584 breakpoint_init_inferior (inf_exited);
2585 registers_changed ();
2586
2587 reopen_exec_file ();
2588 reinit_frame_cache ();
2589
2590 if (deprecated_detach_hook)
2591 deprecated_detach_hook ();
2592 }
2593 \f
2594 /* Helper function for child_wait and the derivatives of child_wait.
2595 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2596 translation of that in OURSTATUS. */
2597 void
2598 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
2599 {
2600 if (WIFEXITED (hoststatus))
2601 {
2602 ourstatus->kind = TARGET_WAITKIND_EXITED;
2603 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2604 }
2605 else if (!WIFSTOPPED (hoststatus))
2606 {
2607 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2608 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2609 }
2610 else
2611 {
2612 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2613 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2614 }
2615 }
2616 \f
2617 /* Convert a normal process ID to a string. Returns the string in a
2618 static buffer. */
2619
2620 char *
2621 normal_pid_to_str (ptid_t ptid)
2622 {
2623 static char buf[32];
2624
2625 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
2626 return buf;
2627 }
2628
2629 static char *
2630 dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
2631 {
2632 return normal_pid_to_str (ptid);
2633 }
2634
2635 /* Error-catcher for target_find_memory_regions */
2636 static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2637 {
2638 error (_("No target."));
2639 return 0;
2640 }
2641
2642 /* Error-catcher for target_make_corefile_notes */
2643 static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2644 {
2645 error (_("No target."));
2646 return NULL;
2647 }
2648
2649 /* Set up the handful of non-empty slots needed by the dummy target
2650 vector. */
2651
2652 static void
2653 init_dummy_target (void)
2654 {
2655 dummy_target.to_shortname = "None";
2656 dummy_target.to_longname = "None";
2657 dummy_target.to_doc = "";
2658 dummy_target.to_attach = find_default_attach;
2659 dummy_target.to_detach =
2660 (void (*)(struct target_ops *, char *, int))target_ignore;
2661 dummy_target.to_create_inferior = find_default_create_inferior;
2662 dummy_target.to_can_async_p = find_default_can_async_p;
2663 dummy_target.to_is_async_p = find_default_is_async_p;
2664 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
2665 dummy_target.to_pid_to_str = dummy_pid_to_str;
2666 dummy_target.to_stratum = dummy_stratum;
2667 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2668 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
2669 dummy_target.to_xfer_partial = default_xfer_partial;
2670 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
2671 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
2672 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
2673 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
2674 dummy_target.to_has_execution = (int (*) (struct target_ops *)) return_zero;
2675 dummy_target.to_magic = OPS_MAGIC;
2676 }
2677 \f
2678 static void
2679 debug_to_open (char *args, int from_tty)
2680 {
2681 debug_target.to_open (args, from_tty);
2682
2683 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
2684 }
2685
2686 void
2687 target_close (struct target_ops *targ, int quitting)
2688 {
2689 if (targ->to_xclose != NULL)
2690 targ->to_xclose (targ, quitting);
2691 else if (targ->to_close != NULL)
2692 targ->to_close (quitting);
2693
2694 if (targetdebug)
2695 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
2696 }
2697
2698 void
2699 target_attach (char *args, int from_tty)
2700 {
2701 struct target_ops *t;
2702 for (t = current_target.beneath; t != NULL; t = t->beneath)
2703 {
2704 if (t->to_attach != NULL)
2705 {
2706 t->to_attach (t, args, from_tty);
2707 if (targetdebug)
2708 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
2709 args, from_tty);
2710 return;
2711 }
2712 }
2713
2714 internal_error (__FILE__, __LINE__,
2715 "could not find a target to attach");
2716 }
2717
2718 int
2719 target_thread_alive (ptid_t ptid)
2720 {
2721 struct target_ops *t;
2722 for (t = current_target.beneath; t != NULL; t = t->beneath)
2723 {
2724 if (t->to_thread_alive != NULL)
2725 {
2726 int retval;
2727
2728 retval = t->to_thread_alive (t, ptid);
2729 if (targetdebug)
2730 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2731 PIDGET (ptid), retval);
2732
2733 return retval;
2734 }
2735 }
2736
2737 return 0;
2738 }
2739
2740 void
2741 target_find_new_threads (void)
2742 {
2743 struct target_ops *t;
2744 for (t = current_target.beneath; t != NULL; t = t->beneath)
2745 {
2746 if (t->to_find_new_threads != NULL)
2747 {
2748 t->to_find_new_threads (t);
2749 if (targetdebug)
2750 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
2751
2752 return;
2753 }
2754 }
2755 }
2756
2757 static void
2758 debug_to_post_attach (int pid)
2759 {
2760 debug_target.to_post_attach (pid);
2761
2762 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
2763 }
2764
2765 /* Return a pretty printed form of target_waitstatus.
2766 Space for the result is malloc'd, caller must free. */
2767
2768 char *
2769 target_waitstatus_to_string (const struct target_waitstatus *ws)
2770 {
2771 const char *kind_str = "status->kind = ";
2772
2773 switch (ws->kind)
2774 {
2775 case TARGET_WAITKIND_EXITED:
2776 return xstrprintf ("%sexited, status = %d",
2777 kind_str, ws->value.integer);
2778 case TARGET_WAITKIND_STOPPED:
2779 return xstrprintf ("%sstopped, signal = %s",
2780 kind_str, target_signal_to_name (ws->value.sig));
2781 case TARGET_WAITKIND_SIGNALLED:
2782 return xstrprintf ("%ssignalled, signal = %s",
2783 kind_str, target_signal_to_name (ws->value.sig));
2784 case TARGET_WAITKIND_LOADED:
2785 return xstrprintf ("%sloaded", kind_str);
2786 case TARGET_WAITKIND_FORKED:
2787 return xstrprintf ("%sforked", kind_str);
2788 case TARGET_WAITKIND_VFORKED:
2789 return xstrprintf ("%svforked", kind_str);
2790 case TARGET_WAITKIND_EXECD:
2791 return xstrprintf ("%sexecd", kind_str);
2792 case TARGET_WAITKIND_SYSCALL_ENTRY:
2793 return xstrprintf ("%ssyscall-entry", kind_str);
2794 case TARGET_WAITKIND_SYSCALL_RETURN:
2795 return xstrprintf ("%ssyscall-return", kind_str);
2796 case TARGET_WAITKIND_SPURIOUS:
2797 return xstrprintf ("%sspurious", kind_str);
2798 case TARGET_WAITKIND_IGNORE:
2799 return xstrprintf ("%signore", kind_str);
2800 case TARGET_WAITKIND_NO_HISTORY:
2801 return xstrprintf ("%sno-history", kind_str);
2802 default:
2803 return xstrprintf ("%sunknown???", kind_str);
2804 }
2805 }
2806
2807 static void
2808 debug_print_register (const char * func,
2809 struct regcache *regcache, int regno)
2810 {
2811 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2812 fprintf_unfiltered (gdb_stdlog, "%s ", func);
2813 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
2814 && gdbarch_register_name (gdbarch, regno) != NULL
2815 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2816 fprintf_unfiltered (gdb_stdlog, "(%s)",
2817 gdbarch_register_name (gdbarch, regno));
2818 else
2819 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
2820 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
2821 {
2822 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2823 int i, size = register_size (gdbarch, regno);
2824 unsigned char buf[MAX_REGISTER_SIZE];
2825 regcache_raw_collect (regcache, regno, buf);
2826 fprintf_unfiltered (gdb_stdlog, " = ");
2827 for (i = 0; i < size; i++)
2828 {
2829 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2830 }
2831 if (size <= sizeof (LONGEST))
2832 {
2833 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
2834 fprintf_unfiltered (gdb_stdlog, " %s %s",
2835 core_addr_to_string_nz (val), plongest (val));
2836 }
2837 }
2838 fprintf_unfiltered (gdb_stdlog, "\n");
2839 }
2840
2841 void
2842 target_fetch_registers (struct regcache *regcache, int regno)
2843 {
2844 struct target_ops *t;
2845 for (t = current_target.beneath; t != NULL; t = t->beneath)
2846 {
2847 if (t->to_fetch_registers != NULL)
2848 {
2849 t->to_fetch_registers (t, regcache, regno);
2850 if (targetdebug)
2851 debug_print_register ("target_fetch_registers", regcache, regno);
2852 return;
2853 }
2854 }
2855 }
2856
2857 void
2858 target_store_registers (struct regcache *regcache, int regno)
2859 {
2860
2861 struct target_ops *t;
2862 for (t = current_target.beneath; t != NULL; t = t->beneath)
2863 {
2864 if (t->to_store_registers != NULL)
2865 {
2866 t->to_store_registers (t, regcache, regno);
2867 if (targetdebug)
2868 {
2869 debug_print_register ("target_store_registers", regcache, regno);
2870 }
2871 return;
2872 }
2873 }
2874
2875 noprocess ();
2876 }
2877
2878 static void
2879 debug_to_prepare_to_store (struct regcache *regcache)
2880 {
2881 debug_target.to_prepare_to_store (regcache);
2882
2883 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
2884 }
2885
2886 static int
2887 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
2888 int write, struct mem_attrib *attrib,
2889 struct target_ops *target)
2890 {
2891 int retval;
2892
2893 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
2894 attrib, target);
2895
2896 fprintf_unfiltered (gdb_stdlog,
2897 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
2898 paddress (target_gdbarch, memaddr), len,
2899 write ? "write" : "read", retval);
2900
2901 if (retval > 0)
2902 {
2903 int i;
2904
2905 fputs_unfiltered (", bytes =", gdb_stdlog);
2906 for (i = 0; i < retval; i++)
2907 {
2908 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
2909 {
2910 if (targetdebug < 2 && i > 0)
2911 {
2912 fprintf_unfiltered (gdb_stdlog, " ...");
2913 break;
2914 }
2915 fprintf_unfiltered (gdb_stdlog, "\n");
2916 }
2917
2918 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
2919 }
2920 }
2921
2922 fputc_unfiltered ('\n', gdb_stdlog);
2923
2924 return retval;
2925 }
2926
2927 static void
2928 debug_to_files_info (struct target_ops *target)
2929 {
2930 debug_target.to_files_info (target);
2931
2932 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
2933 }
2934
2935 static int
2936 debug_to_insert_breakpoint (struct gdbarch *gdbarch,
2937 struct bp_target_info *bp_tgt)
2938 {
2939 int retval;
2940
2941 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
2942
2943 fprintf_unfiltered (gdb_stdlog,
2944 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2945 (unsigned long) bp_tgt->placed_address,
2946 (unsigned long) retval);
2947 return retval;
2948 }
2949
2950 static int
2951 debug_to_remove_breakpoint (struct gdbarch *gdbarch,
2952 struct bp_target_info *bp_tgt)
2953 {
2954 int retval;
2955
2956 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
2957
2958 fprintf_unfiltered (gdb_stdlog,
2959 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2960 (unsigned long) bp_tgt->placed_address,
2961 (unsigned long) retval);
2962 return retval;
2963 }
2964
2965 static int
2966 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
2967 {
2968 int retval;
2969
2970 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
2971
2972 fprintf_unfiltered (gdb_stdlog,
2973 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2974 (unsigned long) type,
2975 (unsigned long) cnt,
2976 (unsigned long) from_tty,
2977 (unsigned long) retval);
2978 return retval;
2979 }
2980
2981 static int
2982 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2983 {
2984 CORE_ADDR retval;
2985
2986 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
2987
2988 fprintf_unfiltered (gdb_stdlog,
2989 "target_region_ok_for_hw_watchpoint (%ld, %ld) = 0x%lx\n",
2990 (unsigned long) addr,
2991 (unsigned long) len,
2992 (unsigned long) retval);
2993 return retval;
2994 }
2995
2996 static int
2997 debug_to_stopped_by_watchpoint (void)
2998 {
2999 int retval;
3000
3001 retval = debug_target.to_stopped_by_watchpoint ();
3002
3003 fprintf_unfiltered (gdb_stdlog,
3004 "target_stopped_by_watchpoint () = %ld\n",
3005 (unsigned long) retval);
3006 return retval;
3007 }
3008
3009 static int
3010 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
3011 {
3012 int retval;
3013
3014 retval = debug_target.to_stopped_data_address (target, addr);
3015
3016 fprintf_unfiltered (gdb_stdlog,
3017 "target_stopped_data_address ([0x%lx]) = %ld\n",
3018 (unsigned long)*addr,
3019 (unsigned long)retval);
3020 return retval;
3021 }
3022
3023 static int
3024 debug_to_watchpoint_addr_within_range (struct target_ops *target,
3025 CORE_ADDR addr,
3026 CORE_ADDR start, int length)
3027 {
3028 int retval;
3029
3030 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3031 start, length);
3032
3033 fprintf_filtered (gdb_stdlog,
3034 "target_watchpoint_addr_within_range (0x%lx, 0x%lx, %d) = %d\n",
3035 (unsigned long) addr, (unsigned long) start, length,
3036 retval);
3037 return retval;
3038 }
3039
3040 static int
3041 debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3042 struct bp_target_info *bp_tgt)
3043 {
3044 int retval;
3045
3046 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
3047
3048 fprintf_unfiltered (gdb_stdlog,
3049 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
3050 (unsigned long) bp_tgt->placed_address,
3051 (unsigned long) retval);
3052 return retval;
3053 }
3054
3055 static int
3056 debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3057 struct bp_target_info *bp_tgt)
3058 {
3059 int retval;
3060
3061 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
3062
3063 fprintf_unfiltered (gdb_stdlog,
3064 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
3065 (unsigned long) bp_tgt->placed_address,
3066 (unsigned long) retval);
3067 return retval;
3068 }
3069
3070 static int
3071 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
3072 {
3073 int retval;
3074
3075 retval = debug_target.to_insert_watchpoint (addr, len, type);
3076
3077 fprintf_unfiltered (gdb_stdlog,
3078 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
3079 (unsigned long) addr, len, type, (unsigned long) retval);
3080 return retval;
3081 }
3082
3083 static int
3084 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
3085 {
3086 int retval;
3087
3088 retval = debug_target.to_remove_watchpoint (addr, len, type);
3089
3090 fprintf_unfiltered (gdb_stdlog,
3091 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
3092 (unsigned long) addr, len, type, (unsigned long) retval);
3093 return retval;
3094 }
3095
3096 static void
3097 debug_to_terminal_init (void)
3098 {
3099 debug_target.to_terminal_init ();
3100
3101 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
3102 }
3103
3104 static void
3105 debug_to_terminal_inferior (void)
3106 {
3107 debug_target.to_terminal_inferior ();
3108
3109 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
3110 }
3111
3112 static void
3113 debug_to_terminal_ours_for_output (void)
3114 {
3115 debug_target.to_terminal_ours_for_output ();
3116
3117 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
3118 }
3119
3120 static void
3121 debug_to_terminal_ours (void)
3122 {
3123 debug_target.to_terminal_ours ();
3124
3125 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
3126 }
3127
3128 static void
3129 debug_to_terminal_save_ours (void)
3130 {
3131 debug_target.to_terminal_save_ours ();
3132
3133 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3134 }
3135
3136 static void
3137 debug_to_terminal_info (char *arg, int from_tty)
3138 {
3139 debug_target.to_terminal_info (arg, from_tty);
3140
3141 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
3142 from_tty);
3143 }
3144
3145 static void
3146 debug_to_load (char *args, int from_tty)
3147 {
3148 debug_target.to_load (args, from_tty);
3149
3150 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
3151 }
3152
3153 static int
3154 debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
3155 {
3156 int retval;
3157
3158 retval = debug_target.to_lookup_symbol (name, addrp);
3159
3160 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
3161
3162 return retval;
3163 }
3164
3165 static void
3166 debug_to_post_startup_inferior (ptid_t ptid)
3167 {
3168 debug_target.to_post_startup_inferior (ptid);
3169
3170 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
3171 PIDGET (ptid));
3172 }
3173
3174 static void
3175 debug_to_acknowledge_created_inferior (int pid)
3176 {
3177 debug_target.to_acknowledge_created_inferior (pid);
3178
3179 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
3180 pid);
3181 }
3182
3183 static void
3184 debug_to_insert_fork_catchpoint (int pid)
3185 {
3186 debug_target.to_insert_fork_catchpoint (pid);
3187
3188 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
3189 pid);
3190 }
3191
3192 static int
3193 debug_to_remove_fork_catchpoint (int pid)
3194 {
3195 int retval;
3196
3197 retval = debug_target.to_remove_fork_catchpoint (pid);
3198
3199 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
3200 pid, retval);
3201
3202 return retval;
3203 }
3204
3205 static void
3206 debug_to_insert_vfork_catchpoint (int pid)
3207 {
3208 debug_target.to_insert_vfork_catchpoint (pid);
3209
3210 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
3211 pid);
3212 }
3213
3214 static int
3215 debug_to_remove_vfork_catchpoint (int pid)
3216 {
3217 int retval;
3218
3219 retval = debug_target.to_remove_vfork_catchpoint (pid);
3220
3221 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
3222 pid, retval);
3223
3224 return retval;
3225 }
3226
3227 static void
3228 debug_to_insert_exec_catchpoint (int pid)
3229 {
3230 debug_target.to_insert_exec_catchpoint (pid);
3231
3232 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
3233 pid);
3234 }
3235
3236 static int
3237 debug_to_remove_exec_catchpoint (int pid)
3238 {
3239 int retval;
3240
3241 retval = debug_target.to_remove_exec_catchpoint (pid);
3242
3243 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
3244 pid, retval);
3245
3246 return retval;
3247 }
3248
3249 static int
3250 debug_to_has_exited (int pid, int wait_status, int *exit_status)
3251 {
3252 int has_exited;
3253
3254 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
3255
3256 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
3257 pid, wait_status, *exit_status, has_exited);
3258
3259 return has_exited;
3260 }
3261
3262 static int
3263 debug_to_can_run (void)
3264 {
3265 int retval;
3266
3267 retval = debug_target.to_can_run ();
3268
3269 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
3270
3271 return retval;
3272 }
3273
3274 static void
3275 debug_to_notice_signals (ptid_t ptid)
3276 {
3277 debug_target.to_notice_signals (ptid);
3278
3279 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
3280 PIDGET (ptid));
3281 }
3282
3283 static struct gdbarch *
3284 debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
3285 {
3286 struct gdbarch *retval;
3287
3288 retval = debug_target.to_thread_architecture (ops, ptid);
3289
3290 fprintf_unfiltered (gdb_stdlog, "target_thread_architecture (%s) = %p [%s]\n",
3291 target_pid_to_str (ptid), retval,
3292 gdbarch_bfd_arch_info (retval)->printable_name);
3293 return retval;
3294 }
3295
3296 static void
3297 debug_to_stop (ptid_t ptid)
3298 {
3299 debug_target.to_stop (ptid);
3300
3301 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
3302 target_pid_to_str (ptid));
3303 }
3304
3305 static void
3306 debug_to_rcmd (char *command,
3307 struct ui_file *outbuf)
3308 {
3309 debug_target.to_rcmd (command, outbuf);
3310 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
3311 }
3312
3313 static char *
3314 debug_to_pid_to_exec_file (int pid)
3315 {
3316 char *exec_file;
3317
3318 exec_file = debug_target.to_pid_to_exec_file (pid);
3319
3320 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
3321 pid, exec_file);
3322
3323 return exec_file;
3324 }
3325
3326 static void
3327 setup_target_debug (void)
3328 {
3329 memcpy (&debug_target, &current_target, sizeof debug_target);
3330
3331 current_target.to_open = debug_to_open;
3332 current_target.to_post_attach = debug_to_post_attach;
3333 current_target.to_prepare_to_store = debug_to_prepare_to_store;
3334 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
3335 current_target.to_files_info = debug_to_files_info;
3336 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
3337 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
3338 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
3339 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
3340 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
3341 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
3342 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
3343 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
3344 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3345 current_target.to_watchpoint_addr_within_range = debug_to_watchpoint_addr_within_range;
3346 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
3347 current_target.to_terminal_init = debug_to_terminal_init;
3348 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3349 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
3350 current_target.to_terminal_ours = debug_to_terminal_ours;
3351 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
3352 current_target.to_terminal_info = debug_to_terminal_info;
3353 current_target.to_load = debug_to_load;
3354 current_target.to_lookup_symbol = debug_to_lookup_symbol;
3355 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
3356 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
3357 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
3358 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
3359 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
3360 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
3361 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
3362 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
3363 current_target.to_has_exited = debug_to_has_exited;
3364 current_target.to_can_run = debug_to_can_run;
3365 current_target.to_notice_signals = debug_to_notice_signals;
3366 current_target.to_stop = debug_to_stop;
3367 current_target.to_rcmd = debug_to_rcmd;
3368 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
3369 current_target.to_thread_architecture = debug_to_thread_architecture;
3370 }
3371 \f
3372
3373 static char targ_desc[] =
3374 "Names of targets and files being debugged.\n\
3375 Shows the entire stack of targets currently in use (including the exec-file,\n\
3376 core-file, and process, if any), as well as the symbol file name.";
3377
3378 static void
3379 do_monitor_command (char *cmd,
3380 int from_tty)
3381 {
3382 if ((current_target.to_rcmd
3383 == (void (*) (char *, struct ui_file *)) tcomplain)
3384 || (current_target.to_rcmd == debug_to_rcmd
3385 && (debug_target.to_rcmd
3386 == (void (*) (char *, struct ui_file *)) tcomplain)))
3387 error (_("\"monitor\" command not supported by this target."));
3388 target_rcmd (cmd, gdb_stdtarg);
3389 }
3390
3391 /* Print the name of each layers of our target stack. */
3392
3393 static void
3394 maintenance_print_target_stack (char *cmd, int from_tty)
3395 {
3396 struct target_ops *t;
3397
3398 printf_filtered (_("The current target stack is:\n"));
3399
3400 for (t = target_stack; t != NULL; t = t->beneath)
3401 {
3402 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
3403 }
3404 }
3405
3406 /* Controls if async mode is permitted. */
3407 int target_async_permitted = 0;
3408
3409 /* The set command writes to this variable. If the inferior is
3410 executing, linux_nat_async_permitted is *not* updated. */
3411 static int target_async_permitted_1 = 0;
3412
3413 static void
3414 set_maintenance_target_async_permitted (char *args, int from_tty,
3415 struct cmd_list_element *c)
3416 {
3417 if (have_live_inferiors ())
3418 {
3419 target_async_permitted_1 = target_async_permitted;
3420 error (_("Cannot change this setting while the inferior is running."));
3421 }
3422
3423 target_async_permitted = target_async_permitted_1;
3424 }
3425
3426 static void
3427 show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
3428 struct cmd_list_element *c,
3429 const char *value)
3430 {
3431 fprintf_filtered (file, _("\
3432 Controlling the inferior in asynchronous mode is %s.\n"), value);
3433 }
3434
3435 void
3436 initialize_targets (void)
3437 {
3438 init_dummy_target ();
3439 push_target (&dummy_target);
3440
3441 add_info ("target", target_info, targ_desc);
3442 add_info ("files", target_info, targ_desc);
3443
3444 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
3445 Set target debugging."), _("\
3446 Show target debugging."), _("\
3447 When non-zero, target debugging is enabled. Higher numbers are more\n\
3448 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
3449 command."),
3450 NULL,
3451 show_targetdebug,
3452 &setdebuglist, &showdebuglist);
3453
3454 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
3455 &trust_readonly, _("\
3456 Set mode for reading from readonly sections."), _("\
3457 Show mode for reading from readonly sections."), _("\
3458 When this mode is on, memory reads from readonly sections (such as .text)\n\
3459 will be read from the object file instead of from the target. This will\n\
3460 result in significant performance improvement for remote targets."),
3461 NULL,
3462 show_trust_readonly,
3463 &setlist, &showlist);
3464
3465 add_com ("monitor", class_obscure, do_monitor_command,
3466 _("Send a command to the remote monitor (remote targets only)."));
3467
3468 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
3469 _("Print the name of each layer of the internal target stack."),
3470 &maintenanceprintlist);
3471
3472 add_setshow_boolean_cmd ("target-async", no_class,
3473 &target_async_permitted_1, _("\
3474 Set whether gdb controls the inferior in asynchronous mode."), _("\
3475 Show whether gdb controls the inferior in asynchronous mode."), _("\
3476 Tells gdb whether to control the inferior in asynchronous mode."),
3477 set_maintenance_target_async_permitted,
3478 show_maintenance_target_async_permitted,
3479 &setlist,
3480 &showlist);
3481
3482 target_dcache = dcache_init ();
3483 }