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