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