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