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