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