* thread.c (print_thread_info): Don't insist
[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
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 "gdb_stdint.h"
43
44 static void target_info (char *, int);
45
46 static void maybe_kill_then_attach (char *, int);
47
48 static void kill_or_be_killed (int);
49
50 static void default_terminal_info (char *, int);
51
52 static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
53
54 static int nosymbol (char *, CORE_ADDR *);
55
56 static void tcomplain (void) ATTR_NORETURN;
57
58 static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
59
60 static int return_zero (void);
61
62 static int return_one (void);
63
64 static int return_minus_one (void);
65
66 void target_ignore (void);
67
68 static void target_command (char *, int);
69
70 static struct target_ops *find_default_run_target (char *);
71
72 static void nosupport_runtime (void);
73
74 static LONGEST default_xfer_partial (struct target_ops *ops,
75 enum target_object object,
76 const char *annex, gdb_byte *readbuf,
77 const gdb_byte *writebuf,
78 ULONGEST offset, LONGEST len);
79
80 static LONGEST current_xfer_partial (struct target_ops *ops,
81 enum target_object object,
82 const char *annex, gdb_byte *readbuf,
83 const gdb_byte *writebuf,
84 ULONGEST offset, LONGEST len);
85
86 static LONGEST target_xfer_partial (struct target_ops *ops,
87 enum target_object object,
88 const char *annex,
89 void *readbuf, const void *writebuf,
90 ULONGEST offset, LONGEST len);
91
92 static void init_dummy_target (void);
93
94 static struct target_ops debug_target;
95
96 static void debug_to_open (char *, int);
97
98 static void debug_to_close (int);
99
100 static void debug_to_attach (char *, int);
101
102 static void debug_to_detach (char *, int);
103
104 static void debug_to_resume (ptid_t, int, enum target_signal);
105
106 static ptid_t debug_to_wait (ptid_t, struct target_waitstatus *);
107
108 static void debug_to_fetch_registers (struct regcache *, int);
109
110 static void debug_to_store_registers (struct regcache *, int);
111
112 static void debug_to_prepare_to_store (struct regcache *);
113
114 static void debug_to_files_info (struct target_ops *);
115
116 static int debug_to_insert_breakpoint (struct bp_target_info *);
117
118 static int debug_to_remove_breakpoint (struct bp_target_info *);
119
120 static int debug_to_can_use_hw_breakpoint (int, int, int);
121
122 static int debug_to_insert_hw_breakpoint (struct bp_target_info *);
123
124 static int debug_to_remove_hw_breakpoint (struct bp_target_info *);
125
126 static int debug_to_insert_watchpoint (CORE_ADDR, int, int);
127
128 static int debug_to_remove_watchpoint (CORE_ADDR, int, int);
129
130 static int debug_to_stopped_by_watchpoint (void);
131
132 static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
133
134 static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
135
136 static void debug_to_terminal_init (void);
137
138 static void debug_to_terminal_inferior (void);
139
140 static void debug_to_terminal_ours_for_output (void);
141
142 static void debug_to_terminal_save_ours (void);
143
144 static void debug_to_terminal_ours (void);
145
146 static void debug_to_terminal_info (char *, int);
147
148 static void debug_to_kill (void);
149
150 static void debug_to_load (char *, int);
151
152 static int debug_to_lookup_symbol (char *, CORE_ADDR *);
153
154 static void debug_to_mourn_inferior (void);
155
156 static int debug_to_can_run (void);
157
158 static void debug_to_notice_signals (ptid_t);
159
160 static int debug_to_thread_alive (ptid_t);
161
162 static void debug_to_stop (void);
163
164 /* NOTE: cagney/2004-09-29: Many targets reference this variable in
165 wierd and mysterious ways. Putting the variable here lets those
166 wierd and mysterious ways keep building while they are being
167 converted to the inferior inheritance structure. */
168 struct target_ops deprecated_child_ops;
169
170 /* Pointer to array of target architecture structures; the size of the
171 array; the current index into the array; the allocated size of the
172 array. */
173 struct target_ops **target_structs;
174 unsigned target_struct_size;
175 unsigned target_struct_index;
176 unsigned target_struct_allocsize;
177 #define DEFAULT_ALLOCSIZE 10
178
179 /* The initial current target, so that there is always a semi-valid
180 current target. */
181
182 static struct target_ops dummy_target;
183
184 /* Top of target stack. */
185
186 static struct target_ops *target_stack;
187
188 /* The target structure we are currently using to talk to a process
189 or file or whatever "inferior" we have. */
190
191 struct target_ops current_target;
192
193 /* Command list for target. */
194
195 static struct cmd_list_element *targetlist = NULL;
196
197 /* Nonzero if we are debugging an attached outside process
198 rather than an inferior. */
199
200 int attach_flag;
201
202 /* Nonzero if we should trust readonly sections from the
203 executable when reading memory. */
204
205 static int trust_readonly = 0;
206
207 /* Nonzero if we should show true memory content including
208 memory breakpoint inserted by gdb. */
209
210 static int show_memory_breakpoints = 0;
211
212 /* Non-zero if we want to see trace of target level stuff. */
213
214 static int targetdebug = 0;
215 static void
216 show_targetdebug (struct ui_file *file, int from_tty,
217 struct cmd_list_element *c, const char *value)
218 {
219 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
220 }
221
222 static void setup_target_debug (void);
223
224 DCACHE *target_dcache;
225
226 /* The user just typed 'target' without the name of a target. */
227
228 static void
229 target_command (char *arg, int from_tty)
230 {
231 fputs_filtered ("Argument required (target name). Try `help target'\n",
232 gdb_stdout);
233 }
234
235 /* Add a possible target architecture to the list. */
236
237 void
238 add_target (struct target_ops *t)
239 {
240 /* Provide default values for all "must have" methods. */
241 if (t->to_xfer_partial == NULL)
242 t->to_xfer_partial = default_xfer_partial;
243
244 if (!target_structs)
245 {
246 target_struct_allocsize = DEFAULT_ALLOCSIZE;
247 target_structs = (struct target_ops **) xmalloc
248 (target_struct_allocsize * sizeof (*target_structs));
249 }
250 if (target_struct_size >= target_struct_allocsize)
251 {
252 target_struct_allocsize *= 2;
253 target_structs = (struct target_ops **)
254 xrealloc ((char *) target_structs,
255 target_struct_allocsize * sizeof (*target_structs));
256 }
257 target_structs[target_struct_size++] = t;
258
259 if (targetlist == NULL)
260 add_prefix_cmd ("target", class_run, target_command, _("\
261 Connect to a target machine or process.\n\
262 The first argument is the type or protocol of the target machine.\n\
263 Remaining arguments are interpreted by the target protocol. For more\n\
264 information on the arguments for a particular protocol, type\n\
265 `help target ' followed by the protocol name."),
266 &targetlist, "target ", 0, &cmdlist);
267 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
268 }
269
270 /* Stub functions */
271
272 void
273 target_ignore (void)
274 {
275 }
276
277 void
278 target_load (char *arg, int from_tty)
279 {
280 dcache_invalidate (target_dcache);
281 (*current_target.to_load) (arg, from_tty);
282 }
283
284 static int
285 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
286 struct target_ops *t)
287 {
288 errno = EIO; /* Can't read/write this location */
289 return 0; /* No bytes handled */
290 }
291
292 static void
293 tcomplain (void)
294 {
295 error (_("You can't do that when your target is `%s'"),
296 current_target.to_shortname);
297 }
298
299 void
300 noprocess (void)
301 {
302 error (_("You can't do that without a process to debug."));
303 }
304
305 static int
306 nosymbol (char *name, CORE_ADDR *addrp)
307 {
308 return 1; /* Symbol does not exist in target env */
309 }
310
311 static void
312 nosupport_runtime (void)
313 {
314 if (ptid_equal (inferior_ptid, null_ptid))
315 noprocess ();
316 else
317 error (_("No run-time support for this"));
318 }
319
320
321 static void
322 default_terminal_info (char *args, int from_tty)
323 {
324 printf_unfiltered (_("No saved terminal information.\n"));
325 }
326
327 /* This is the default target_create_inferior and target_attach function.
328 If the current target is executing, it asks whether to kill it off.
329 If this function returns without calling error(), it has killed off
330 the target, and the operation should be attempted. */
331
332 static void
333 kill_or_be_killed (int from_tty)
334 {
335 if (target_has_execution)
336 {
337 printf_unfiltered (_("You are already running a program:\n"));
338 target_files_info ();
339 if (query ("Kill it? "))
340 {
341 target_kill ();
342 if (target_has_execution)
343 error (_("Killing the program did not help."));
344 return;
345 }
346 else
347 {
348 error (_("Program not killed."));
349 }
350 }
351 tcomplain ();
352 }
353
354 static void
355 maybe_kill_then_attach (char *args, int from_tty)
356 {
357 kill_or_be_killed (from_tty);
358 target_attach (args, from_tty);
359 }
360
361 static void
362 maybe_kill_then_create_inferior (char *exec, char *args, char **env,
363 int from_tty)
364 {
365 kill_or_be_killed (0);
366 target_create_inferior (exec, args, env, from_tty);
367 }
368
369 /* Go through the target stack from top to bottom, copying over zero
370 entries in current_target, then filling in still empty entries. In
371 effect, we are doing class inheritance through the pushed target
372 vectors.
373
374 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
375 is currently implemented, is that it discards any knowledge of
376 which target an inherited method originally belonged to.
377 Consequently, new new target methods should instead explicitly and
378 locally search the target stack for the target that can handle the
379 request. */
380
381 static void
382 update_current_target (void)
383 {
384 struct target_ops *t;
385
386 /* First, reset current's contents. */
387 memset (&current_target, 0, sizeof (current_target));
388
389 #define INHERIT(FIELD, TARGET) \
390 if (!current_target.FIELD) \
391 current_target.FIELD = (TARGET)->FIELD
392
393 for (t = target_stack; t; t = t->beneath)
394 {
395 INHERIT (to_shortname, t);
396 INHERIT (to_longname, t);
397 INHERIT (to_doc, t);
398 INHERIT (to_open, t);
399 INHERIT (to_close, t);
400 INHERIT (to_attach, t);
401 INHERIT (to_post_attach, t);
402 INHERIT (to_detach, t);
403 /* Do not inherit to_disconnect. */
404 INHERIT (to_resume, t);
405 INHERIT (to_wait, t);
406 INHERIT (to_fetch_registers, t);
407 INHERIT (to_store_registers, t);
408 INHERIT (to_prepare_to_store, t);
409 INHERIT (deprecated_xfer_memory, t);
410 INHERIT (to_files_info, t);
411 INHERIT (to_insert_breakpoint, t);
412 INHERIT (to_remove_breakpoint, t);
413 INHERIT (to_can_use_hw_breakpoint, t);
414 INHERIT (to_insert_hw_breakpoint, t);
415 INHERIT (to_remove_hw_breakpoint, t);
416 INHERIT (to_insert_watchpoint, t);
417 INHERIT (to_remove_watchpoint, t);
418 INHERIT (to_stopped_data_address, t);
419 INHERIT (to_stopped_by_watchpoint, t);
420 INHERIT (to_have_steppable_watchpoint, t);
421 INHERIT (to_have_continuable_watchpoint, t);
422 INHERIT (to_region_ok_for_hw_watchpoint, t);
423 INHERIT (to_terminal_init, t);
424 INHERIT (to_terminal_inferior, t);
425 INHERIT (to_terminal_ours_for_output, t);
426 INHERIT (to_terminal_ours, t);
427 INHERIT (to_terminal_save_ours, t);
428 INHERIT (to_terminal_info, t);
429 INHERIT (to_kill, t);
430 INHERIT (to_load, t);
431 INHERIT (to_lookup_symbol, t);
432 INHERIT (to_create_inferior, t);
433 INHERIT (to_post_startup_inferior, t);
434 INHERIT (to_acknowledge_created_inferior, t);
435 INHERIT (to_insert_fork_catchpoint, t);
436 INHERIT (to_remove_fork_catchpoint, t);
437 INHERIT (to_insert_vfork_catchpoint, t);
438 INHERIT (to_remove_vfork_catchpoint, t);
439 /* Do not inherit to_follow_fork. */
440 INHERIT (to_insert_exec_catchpoint, t);
441 INHERIT (to_remove_exec_catchpoint, t);
442 INHERIT (to_has_exited, t);
443 INHERIT (to_mourn_inferior, t);
444 INHERIT (to_can_run, t);
445 INHERIT (to_notice_signals, t);
446 INHERIT (to_thread_alive, t);
447 INHERIT (to_find_new_threads, t);
448 INHERIT (to_pid_to_str, t);
449 INHERIT (to_extra_thread_info, t);
450 INHERIT (to_stop, t);
451 /* Do not inherit to_xfer_partial. */
452 INHERIT (to_rcmd, t);
453 INHERIT (to_pid_to_exec_file, t);
454 INHERIT (to_log_command, t);
455 INHERIT (to_stratum, t);
456 INHERIT (to_has_all_memory, t);
457 INHERIT (to_has_memory, t);
458 INHERIT (to_has_stack, t);
459 INHERIT (to_has_registers, t);
460 INHERIT (to_has_execution, t);
461 INHERIT (to_has_thread_control, t);
462 INHERIT (to_sections, t);
463 INHERIT (to_sections_end, t);
464 INHERIT (to_can_async_p, t);
465 INHERIT (to_is_async_p, t);
466 INHERIT (to_async, t);
467 INHERIT (to_async_mask_value, t);
468 INHERIT (to_find_memory_regions, t);
469 INHERIT (to_make_corefile_notes, t);
470 INHERIT (to_get_thread_local_address, t);
471 /* Do not inherit to_read_description. */
472 INHERIT (to_magic, t);
473 /* Do not inherit to_memory_map. */
474 /* Do not inherit to_flash_erase. */
475 /* Do not inherit to_flash_done. */
476 }
477 #undef INHERIT
478
479 /* Clean up a target struct so it no longer has any zero pointers in
480 it. Some entries are defaulted to a method that print an error,
481 others are hard-wired to a standard recursive default. */
482
483 #define de_fault(field, value) \
484 if (!current_target.field) \
485 current_target.field = value
486
487 de_fault (to_open,
488 (void (*) (char *, int))
489 tcomplain);
490 de_fault (to_close,
491 (void (*) (int))
492 target_ignore);
493 de_fault (to_attach,
494 maybe_kill_then_attach);
495 de_fault (to_post_attach,
496 (void (*) (int))
497 target_ignore);
498 de_fault (to_detach,
499 (void (*) (char *, int))
500 target_ignore);
501 de_fault (to_resume,
502 (void (*) (ptid_t, int, enum target_signal))
503 noprocess);
504 de_fault (to_wait,
505 (ptid_t (*) (ptid_t, struct target_waitstatus *))
506 noprocess);
507 de_fault (to_fetch_registers,
508 (void (*) (struct regcache *, int))
509 target_ignore);
510 de_fault (to_store_registers,
511 (void (*) (struct regcache *, int))
512 noprocess);
513 de_fault (to_prepare_to_store,
514 (void (*) (struct regcache *))
515 noprocess);
516 de_fault (deprecated_xfer_memory,
517 (int (*) (CORE_ADDR, gdb_byte *, int, int, struct mem_attrib *, struct target_ops *))
518 nomemory);
519 de_fault (to_files_info,
520 (void (*) (struct target_ops *))
521 target_ignore);
522 de_fault (to_insert_breakpoint,
523 memory_insert_breakpoint);
524 de_fault (to_remove_breakpoint,
525 memory_remove_breakpoint);
526 de_fault (to_can_use_hw_breakpoint,
527 (int (*) (int, int, int))
528 return_zero);
529 de_fault (to_insert_hw_breakpoint,
530 (int (*) (struct bp_target_info *))
531 return_minus_one);
532 de_fault (to_remove_hw_breakpoint,
533 (int (*) (struct bp_target_info *))
534 return_minus_one);
535 de_fault (to_insert_watchpoint,
536 (int (*) (CORE_ADDR, int, int))
537 return_minus_one);
538 de_fault (to_remove_watchpoint,
539 (int (*) (CORE_ADDR, int, int))
540 return_minus_one);
541 de_fault (to_stopped_by_watchpoint,
542 (int (*) (void))
543 return_zero);
544 de_fault (to_stopped_data_address,
545 (int (*) (struct target_ops *, CORE_ADDR *))
546 return_zero);
547 de_fault (to_region_ok_for_hw_watchpoint,
548 default_region_ok_for_hw_watchpoint);
549 de_fault (to_terminal_init,
550 (void (*) (void))
551 target_ignore);
552 de_fault (to_terminal_inferior,
553 (void (*) (void))
554 target_ignore);
555 de_fault (to_terminal_ours_for_output,
556 (void (*) (void))
557 target_ignore);
558 de_fault (to_terminal_ours,
559 (void (*) (void))
560 target_ignore);
561 de_fault (to_terminal_save_ours,
562 (void (*) (void))
563 target_ignore);
564 de_fault (to_terminal_info,
565 default_terminal_info);
566 de_fault (to_kill,
567 (void (*) (void))
568 noprocess);
569 de_fault (to_load,
570 (void (*) (char *, int))
571 tcomplain);
572 de_fault (to_lookup_symbol,
573 (int (*) (char *, CORE_ADDR *))
574 nosymbol);
575 de_fault (to_create_inferior,
576 maybe_kill_then_create_inferior);
577 de_fault (to_post_startup_inferior,
578 (void (*) (ptid_t))
579 target_ignore);
580 de_fault (to_acknowledge_created_inferior,
581 (void (*) (int))
582 target_ignore);
583 de_fault (to_insert_fork_catchpoint,
584 (void (*) (int))
585 tcomplain);
586 de_fault (to_remove_fork_catchpoint,
587 (int (*) (int))
588 tcomplain);
589 de_fault (to_insert_vfork_catchpoint,
590 (void (*) (int))
591 tcomplain);
592 de_fault (to_remove_vfork_catchpoint,
593 (int (*) (int))
594 tcomplain);
595 de_fault (to_insert_exec_catchpoint,
596 (void (*) (int))
597 tcomplain);
598 de_fault (to_remove_exec_catchpoint,
599 (int (*) (int))
600 tcomplain);
601 de_fault (to_has_exited,
602 (int (*) (int, int, int *))
603 return_zero);
604 de_fault (to_mourn_inferior,
605 (void (*) (void))
606 noprocess);
607 de_fault (to_can_run,
608 return_zero);
609 de_fault (to_notice_signals,
610 (void (*) (ptid_t))
611 target_ignore);
612 de_fault (to_thread_alive,
613 (int (*) (ptid_t))
614 return_zero);
615 de_fault (to_find_new_threads,
616 (void (*) (void))
617 target_ignore);
618 de_fault (to_extra_thread_info,
619 (char *(*) (struct thread_info *))
620 return_zero);
621 de_fault (to_stop,
622 (void (*) (void))
623 target_ignore);
624 current_target.to_xfer_partial = current_xfer_partial;
625 de_fault (to_rcmd,
626 (void (*) (char *, struct ui_file *))
627 tcomplain);
628 de_fault (to_pid_to_exec_file,
629 (char *(*) (int))
630 return_zero);
631 de_fault (to_can_async_p,
632 (int (*) (void))
633 return_zero);
634 de_fault (to_is_async_p,
635 (int (*) (void))
636 return_zero);
637 de_fault (to_async,
638 (void (*) (void (*) (enum inferior_event_type, void*), void*))
639 tcomplain);
640 current_target.to_read_description = NULL;
641 #undef de_fault
642
643 /* Finally, position the target-stack beneath the squashed
644 "current_target". That way code looking for a non-inherited
645 target method can quickly and simply find it. */
646 current_target.beneath = target_stack;
647
648 if (targetdebug)
649 setup_target_debug ();
650 }
651
652 /* Mark OPS as a running target. This reverses the effect
653 of target_mark_exited. */
654
655 void
656 target_mark_running (struct target_ops *ops)
657 {
658 struct target_ops *t;
659
660 for (t = target_stack; t != NULL; t = t->beneath)
661 if (t == ops)
662 break;
663 if (t == NULL)
664 internal_error (__FILE__, __LINE__,
665 "Attempted to mark unpushed target \"%s\" as running",
666 ops->to_shortname);
667
668 ops->to_has_execution = 1;
669 ops->to_has_all_memory = 1;
670 ops->to_has_memory = 1;
671 ops->to_has_stack = 1;
672 ops->to_has_registers = 1;
673
674 update_current_target ();
675 }
676
677 /* Mark OPS as a non-running target. This reverses the effect
678 of target_mark_running. */
679
680 void
681 target_mark_exited (struct target_ops *ops)
682 {
683 struct target_ops *t;
684
685 for (t = target_stack; t != NULL; t = t->beneath)
686 if (t == ops)
687 break;
688 if (t == NULL)
689 internal_error (__FILE__, __LINE__,
690 "Attempted to mark unpushed target \"%s\" as running",
691 ops->to_shortname);
692
693 ops->to_has_execution = 0;
694 ops->to_has_all_memory = 0;
695 ops->to_has_memory = 0;
696 ops->to_has_stack = 0;
697 ops->to_has_registers = 0;
698
699 update_current_target ();
700 }
701
702 /* Push a new target type into the stack of the existing target accessors,
703 possibly superseding some of the existing accessors.
704
705 Result is zero if the pushed target ended up on top of the stack,
706 nonzero if at least one target is on top of it.
707
708 Rather than allow an empty stack, we always have the dummy target at
709 the bottom stratum, so we can call the function vectors without
710 checking them. */
711
712 int
713 push_target (struct target_ops *t)
714 {
715 struct target_ops **cur;
716
717 /* Check magic number. If wrong, it probably means someone changed
718 the struct definition, but not all the places that initialize one. */
719 if (t->to_magic != OPS_MAGIC)
720 {
721 fprintf_unfiltered (gdb_stderr,
722 "Magic number of %s target struct wrong\n",
723 t->to_shortname);
724 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
725 }
726
727 /* Find the proper stratum to install this target in. */
728 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
729 {
730 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
731 break;
732 }
733
734 /* If there's already targets at this stratum, remove them. */
735 /* FIXME: cagney/2003-10-15: I think this should be popping all
736 targets to CUR, and not just those at this stratum level. */
737 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
738 {
739 /* There's already something at this stratum level. Close it,
740 and un-hook it from the stack. */
741 struct target_ops *tmp = (*cur);
742 (*cur) = (*cur)->beneath;
743 tmp->beneath = NULL;
744 target_close (tmp, 0);
745 }
746
747 /* We have removed all targets in our stratum, now add the new one. */
748 t->beneath = (*cur);
749 (*cur) = t;
750
751 update_current_target ();
752
753 /* Not on top? */
754 return (t != target_stack);
755 }
756
757 /* Remove a target_ops vector from the stack, wherever it may be.
758 Return how many times it was removed (0 or 1). */
759
760 int
761 unpush_target (struct target_ops *t)
762 {
763 struct target_ops **cur;
764 struct target_ops *tmp;
765
766 /* Look for the specified target. Note that we assume that a target
767 can only occur once in the target stack. */
768
769 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
770 {
771 if ((*cur) == t)
772 break;
773 }
774
775 if ((*cur) == NULL)
776 return 0; /* Didn't find target_ops, quit now */
777
778 /* NOTE: cagney/2003-12-06: In '94 the close call was made
779 unconditional by moving it to before the above check that the
780 target was in the target stack (something about "Change the way
781 pushing and popping of targets work to support target overlays
782 and inheritance"). This doesn't make much sense - only open
783 targets should be closed. */
784 target_close (t, 0);
785
786 /* Unchain the target */
787 tmp = (*cur);
788 (*cur) = (*cur)->beneath;
789 tmp->beneath = NULL;
790
791 update_current_target ();
792
793 return 1;
794 }
795
796 void
797 pop_target (void)
798 {
799 target_close (&current_target, 0); /* Let it clean up */
800 if (unpush_target (target_stack) == 1)
801 return;
802
803 fprintf_unfiltered (gdb_stderr,
804 "pop_target couldn't find target %s\n",
805 current_target.to_shortname);
806 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
807 }
808
809 /* Using the objfile specified in OBJFILE, find the address for the
810 current thread's thread-local storage with offset OFFSET. */
811 CORE_ADDR
812 target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
813 {
814 volatile CORE_ADDR addr = 0;
815
816 if (target_get_thread_local_address_p ()
817 && gdbarch_fetch_tls_load_module_address_p (current_gdbarch))
818 {
819 ptid_t ptid = inferior_ptid;
820 volatile struct gdb_exception ex;
821
822 TRY_CATCH (ex, RETURN_MASK_ALL)
823 {
824 CORE_ADDR lm_addr;
825
826 /* Fetch the load module address for this objfile. */
827 lm_addr = gdbarch_fetch_tls_load_module_address (current_gdbarch,
828 objfile);
829 /* If it's 0, throw the appropriate exception. */
830 if (lm_addr == 0)
831 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
832 _("TLS load module not found"));
833
834 addr = target_get_thread_local_address (ptid, lm_addr, offset);
835 }
836 /* If an error occurred, print TLS related messages here. Otherwise,
837 throw the error to some higher catcher. */
838 if (ex.reason < 0)
839 {
840 int objfile_is_library = (objfile->flags & OBJF_SHARED);
841
842 switch (ex.error)
843 {
844 case TLS_NO_LIBRARY_SUPPORT_ERROR:
845 error (_("Cannot find thread-local variables in this thread library."));
846 break;
847 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
848 if (objfile_is_library)
849 error (_("Cannot find shared library `%s' in dynamic"
850 " linker's load module list"), objfile->name);
851 else
852 error (_("Cannot find executable file `%s' in dynamic"
853 " linker's load module list"), objfile->name);
854 break;
855 case TLS_NOT_ALLOCATED_YET_ERROR:
856 if (objfile_is_library)
857 error (_("The inferior has not yet allocated storage for"
858 " thread-local variables in\n"
859 "the shared library `%s'\n"
860 "for %s"),
861 objfile->name, target_pid_to_str (ptid));
862 else
863 error (_("The inferior has not yet allocated storage for"
864 " thread-local variables in\n"
865 "the executable `%s'\n"
866 "for %s"),
867 objfile->name, target_pid_to_str (ptid));
868 break;
869 case TLS_GENERIC_ERROR:
870 if (objfile_is_library)
871 error (_("Cannot find thread-local storage for %s, "
872 "shared library %s:\n%s"),
873 target_pid_to_str (ptid),
874 objfile->name, ex.message);
875 else
876 error (_("Cannot find thread-local storage for %s, "
877 "executable file %s:\n%s"),
878 target_pid_to_str (ptid),
879 objfile->name, ex.message);
880 break;
881 default:
882 throw_exception (ex);
883 break;
884 }
885 }
886 }
887 /* It wouldn't be wrong here to try a gdbarch method, too; finding
888 TLS is an ABI-specific thing. But we don't do that yet. */
889 else
890 error (_("Cannot find thread-local variables on this target"));
891
892 return addr;
893 }
894
895 #undef MIN
896 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
897
898 /* target_read_string -- read a null terminated string, up to LEN bytes,
899 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
900 Set *STRING to a pointer to malloc'd memory containing the data; the caller
901 is responsible for freeing it. Return the number of bytes successfully
902 read. */
903
904 int
905 target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
906 {
907 int tlen, origlen, offset, i;
908 gdb_byte buf[4];
909 int errcode = 0;
910 char *buffer;
911 int buffer_allocated;
912 char *bufptr;
913 unsigned int nbytes_read = 0;
914
915 gdb_assert (string);
916
917 /* Small for testing. */
918 buffer_allocated = 4;
919 buffer = xmalloc (buffer_allocated);
920 bufptr = buffer;
921
922 origlen = len;
923
924 while (len > 0)
925 {
926 tlen = MIN (len, 4 - (memaddr & 3));
927 offset = memaddr & 3;
928
929 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
930 if (errcode != 0)
931 {
932 /* The transfer request might have crossed the boundary to an
933 unallocated region of memory. Retry the transfer, requesting
934 a single byte. */
935 tlen = 1;
936 offset = 0;
937 errcode = target_read_memory (memaddr, buf, 1);
938 if (errcode != 0)
939 goto done;
940 }
941
942 if (bufptr - buffer + tlen > buffer_allocated)
943 {
944 unsigned int bytes;
945 bytes = bufptr - buffer;
946 buffer_allocated *= 2;
947 buffer = xrealloc (buffer, buffer_allocated);
948 bufptr = buffer + bytes;
949 }
950
951 for (i = 0; i < tlen; i++)
952 {
953 *bufptr++ = buf[i + offset];
954 if (buf[i + offset] == '\000')
955 {
956 nbytes_read += i + 1;
957 goto done;
958 }
959 }
960
961 memaddr += tlen;
962 len -= tlen;
963 nbytes_read += tlen;
964 }
965 done:
966 *string = buffer;
967 if (errnop != NULL)
968 *errnop = errcode;
969 return nbytes_read;
970 }
971
972 /* Find a section containing ADDR. */
973 struct section_table *
974 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
975 {
976 struct section_table *secp;
977 for (secp = target->to_sections;
978 secp < target->to_sections_end;
979 secp++)
980 {
981 if (addr >= secp->addr && addr < secp->endaddr)
982 return secp;
983 }
984 return NULL;
985 }
986
987 /* Perform a partial memory transfer. The arguments and return
988 value are just as for target_xfer_partial. */
989
990 static LONGEST
991 memory_xfer_partial (struct target_ops *ops, void *readbuf, const void *writebuf,
992 ULONGEST memaddr, LONGEST len)
993 {
994 LONGEST res;
995 int reg_len;
996 struct mem_region *region;
997
998 /* Zero length requests are ok and require no work. */
999 if (len == 0)
1000 return 0;
1001
1002 /* Try the executable file, if "trust-readonly-sections" is set. */
1003 if (readbuf != NULL && trust_readonly)
1004 {
1005 struct section_table *secp;
1006
1007 secp = target_section_by_addr (ops, memaddr);
1008 if (secp != NULL
1009 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1010 & SEC_READONLY))
1011 return xfer_memory (memaddr, readbuf, len, 0, NULL, ops);
1012 }
1013
1014 /* Likewise for accesses to unmapped overlay sections. */
1015 if (readbuf != NULL && overlay_debugging)
1016 {
1017 asection *section = find_pc_overlay (memaddr);
1018 if (pc_in_unmapped_range (memaddr, section))
1019 return xfer_memory (memaddr, readbuf, len, 0, NULL, ops);
1020 }
1021
1022 /* Try GDB's internal data cache. */
1023 region = lookup_mem_region (memaddr);
1024 /* region->hi == 0 means there's no upper bound. */
1025 if (memaddr + len < region->hi || region->hi == 0)
1026 reg_len = len;
1027 else
1028 reg_len = region->hi - memaddr;
1029
1030 switch (region->attrib.mode)
1031 {
1032 case MEM_RO:
1033 if (writebuf != NULL)
1034 return -1;
1035 break;
1036
1037 case MEM_WO:
1038 if (readbuf != NULL)
1039 return -1;
1040 break;
1041
1042 case MEM_FLASH:
1043 /* We only support writing to flash during "load" for now. */
1044 if (writebuf != NULL)
1045 error (_("Writing to flash memory forbidden in this context"));
1046 break;
1047
1048 case MEM_NONE:
1049 return -1;
1050 }
1051
1052 if (region->attrib.cache)
1053 {
1054 /* FIXME drow/2006-08-09: This call discards OPS, so the raw
1055 memory request will start back at current_target. */
1056 if (readbuf != NULL)
1057 res = dcache_xfer_memory (target_dcache, memaddr, readbuf,
1058 reg_len, 0);
1059 else
1060 /* FIXME drow/2006-08-09: If we're going to preserve const
1061 correctness dcache_xfer_memory should take readbuf and
1062 writebuf. */
1063 res = dcache_xfer_memory (target_dcache, memaddr,
1064 (void *) writebuf,
1065 reg_len, 1);
1066 if (res <= 0)
1067 return -1;
1068 else
1069 {
1070 if (readbuf && !show_memory_breakpoints)
1071 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1072 return res;
1073 }
1074 }
1075
1076 /* If none of those methods found the memory we wanted, fall back
1077 to a target partial transfer. Normally a single call to
1078 to_xfer_partial is enough; if it doesn't recognize an object
1079 it will call the to_xfer_partial of the next target down.
1080 But for memory this won't do. Memory is the only target
1081 object which can be read from more than one valid target.
1082 A core file, for instance, could have some of memory but
1083 delegate other bits to the target below it. So, we must
1084 manually try all targets. */
1085
1086 do
1087 {
1088 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
1089 readbuf, writebuf, memaddr, reg_len);
1090 if (res > 0)
1091 break;
1092
1093 /* We want to continue past core files to executables, but not
1094 past a running target's memory. */
1095 if (ops->to_has_all_memory)
1096 break;
1097
1098 ops = ops->beneath;
1099 }
1100 while (ops != NULL);
1101
1102 if (readbuf && !show_memory_breakpoints)
1103 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1104
1105 /* If we still haven't got anything, return the last error. We
1106 give up. */
1107 return res;
1108 }
1109
1110 static void
1111 restore_show_memory_breakpoints (void *arg)
1112 {
1113 show_memory_breakpoints = (uintptr_t) arg;
1114 }
1115
1116 struct cleanup *
1117 make_show_memory_breakpoints_cleanup (int show)
1118 {
1119 int current = show_memory_breakpoints;
1120 show_memory_breakpoints = show;
1121
1122 return make_cleanup (restore_show_memory_breakpoints,
1123 (void *) (uintptr_t) current);
1124 }
1125
1126 static LONGEST
1127 target_xfer_partial (struct target_ops *ops,
1128 enum target_object object, const char *annex,
1129 void *readbuf, const void *writebuf,
1130 ULONGEST offset, LONGEST len)
1131 {
1132 LONGEST retval;
1133
1134 gdb_assert (ops->to_xfer_partial != NULL);
1135
1136 /* If this is a memory transfer, let the memory-specific code
1137 have a look at it instead. Memory transfers are more
1138 complicated. */
1139 if (object == TARGET_OBJECT_MEMORY)
1140 retval = memory_xfer_partial (ops, readbuf, writebuf, offset, len);
1141 else
1142 {
1143 enum target_object raw_object = object;
1144
1145 /* If this is a raw memory transfer, request the normal
1146 memory object from other layers. */
1147 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1148 raw_object = TARGET_OBJECT_MEMORY;
1149
1150 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1151 writebuf, offset, len);
1152 }
1153
1154 if (targetdebug)
1155 {
1156 const unsigned char *myaddr = NULL;
1157
1158 fprintf_unfiltered (gdb_stdlog,
1159 "%s:target_xfer_partial (%d, %s, 0x%lx, 0x%lx, 0x%s, %s) = %s",
1160 ops->to_shortname,
1161 (int) object,
1162 (annex ? annex : "(null)"),
1163 (long) readbuf, (long) writebuf,
1164 paddr_nz (offset), paddr_d (len), paddr_d (retval));
1165
1166 if (readbuf)
1167 myaddr = readbuf;
1168 if (writebuf)
1169 myaddr = writebuf;
1170 if (retval > 0 && myaddr != NULL)
1171 {
1172 int i;
1173
1174 fputs_unfiltered (", bytes =", gdb_stdlog);
1175 for (i = 0; i < retval; i++)
1176 {
1177 if ((((long) &(myaddr[i])) & 0xf) == 0)
1178 {
1179 if (targetdebug < 2 && i > 0)
1180 {
1181 fprintf_unfiltered (gdb_stdlog, " ...");
1182 break;
1183 }
1184 fprintf_unfiltered (gdb_stdlog, "\n");
1185 }
1186
1187 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1188 }
1189 }
1190
1191 fputc_unfiltered ('\n', gdb_stdlog);
1192 }
1193 return retval;
1194 }
1195
1196 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
1197 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1198 if any error occurs.
1199
1200 If an error occurs, no guarantee is made about the contents of the data at
1201 MYADDR. In particular, the caller should not depend upon partial reads
1202 filling the buffer with good data. There is no way for the caller to know
1203 how much good data might have been transfered anyway. Callers that can
1204 deal with partial reads should call target_read (which will retry until
1205 it makes no progress, and then return how much was transferred). */
1206
1207 int
1208 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1209 {
1210 if (target_read (&current_target, TARGET_OBJECT_MEMORY, NULL,
1211 myaddr, memaddr, len) == len)
1212 return 0;
1213 else
1214 return EIO;
1215 }
1216
1217 int
1218 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
1219 {
1220 if (target_write (&current_target, TARGET_OBJECT_MEMORY, NULL,
1221 myaddr, memaddr, len) == len)
1222 return 0;
1223 else
1224 return EIO;
1225 }
1226
1227 /* Fetch the target's memory map. */
1228
1229 VEC(mem_region_s) *
1230 target_memory_map (void)
1231 {
1232 VEC(mem_region_s) *result;
1233 struct mem_region *last_one, *this_one;
1234 int ix;
1235 struct target_ops *t;
1236
1237 if (targetdebug)
1238 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1239
1240 for (t = current_target.beneath; t != NULL; t = t->beneath)
1241 if (t->to_memory_map != NULL)
1242 break;
1243
1244 if (t == NULL)
1245 return NULL;
1246
1247 result = t->to_memory_map (t);
1248 if (result == NULL)
1249 return NULL;
1250
1251 qsort (VEC_address (mem_region_s, result),
1252 VEC_length (mem_region_s, result),
1253 sizeof (struct mem_region), mem_region_cmp);
1254
1255 /* Check that regions do not overlap. Simultaneously assign
1256 a numbering for the "mem" commands to use to refer to
1257 each region. */
1258 last_one = NULL;
1259 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1260 {
1261 this_one->number = ix;
1262
1263 if (last_one && last_one->hi > this_one->lo)
1264 {
1265 warning (_("Overlapping regions in memory map: ignoring"));
1266 VEC_free (mem_region_s, result);
1267 return NULL;
1268 }
1269 last_one = this_one;
1270 }
1271
1272 return result;
1273 }
1274
1275 void
1276 target_flash_erase (ULONGEST address, LONGEST length)
1277 {
1278 struct target_ops *t;
1279
1280 for (t = current_target.beneath; t != NULL; t = t->beneath)
1281 if (t->to_flash_erase != NULL)
1282 {
1283 if (targetdebug)
1284 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1285 paddr (address), phex (length, 0));
1286 t->to_flash_erase (t, address, length);
1287 return;
1288 }
1289
1290 tcomplain ();
1291 }
1292
1293 void
1294 target_flash_done (void)
1295 {
1296 struct target_ops *t;
1297
1298 for (t = current_target.beneath; t != NULL; t = t->beneath)
1299 if (t->to_flash_done != NULL)
1300 {
1301 if (targetdebug)
1302 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1303 t->to_flash_done (t);
1304 return;
1305 }
1306
1307 tcomplain ();
1308 }
1309
1310 #ifndef target_stopped_data_address_p
1311 int
1312 target_stopped_data_address_p (struct target_ops *target)
1313 {
1314 if (target->to_stopped_data_address
1315 == (int (*) (struct target_ops *, CORE_ADDR *)) return_zero)
1316 return 0;
1317 if (target->to_stopped_data_address == debug_to_stopped_data_address
1318 && (debug_target.to_stopped_data_address
1319 == (int (*) (struct target_ops *, CORE_ADDR *)) return_zero))
1320 return 0;
1321 return 1;
1322 }
1323 #endif
1324
1325 static void
1326 show_trust_readonly (struct ui_file *file, int from_tty,
1327 struct cmd_list_element *c, const char *value)
1328 {
1329 fprintf_filtered (file, _("\
1330 Mode for reading from readonly sections is %s.\n"),
1331 value);
1332 }
1333
1334 /* More generic transfers. */
1335
1336 static LONGEST
1337 default_xfer_partial (struct target_ops *ops, enum target_object object,
1338 const char *annex, gdb_byte *readbuf,
1339 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1340 {
1341 if (object == TARGET_OBJECT_MEMORY
1342 && ops->deprecated_xfer_memory != NULL)
1343 /* If available, fall back to the target's
1344 "deprecated_xfer_memory" method. */
1345 {
1346 int xfered = -1;
1347 errno = 0;
1348 if (writebuf != NULL)
1349 {
1350 void *buffer = xmalloc (len);
1351 struct cleanup *cleanup = make_cleanup (xfree, buffer);
1352 memcpy (buffer, writebuf, len);
1353 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1354 1/*write*/, NULL, ops);
1355 do_cleanups (cleanup);
1356 }
1357 if (readbuf != NULL)
1358 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1359 0/*read*/, NULL, ops);
1360 if (xfered > 0)
1361 return xfered;
1362 else if (xfered == 0 && errno == 0)
1363 /* "deprecated_xfer_memory" uses 0, cross checked against
1364 ERRNO as one indication of an error. */
1365 return 0;
1366 else
1367 return -1;
1368 }
1369 else if (ops->beneath != NULL)
1370 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1371 readbuf, writebuf, offset, len);
1372 else
1373 return -1;
1374 }
1375
1376 /* The xfer_partial handler for the topmost target. Unlike the default,
1377 it does not need to handle memory specially; it just passes all
1378 requests down the stack. */
1379
1380 static LONGEST
1381 current_xfer_partial (struct target_ops *ops, enum target_object object,
1382 const char *annex, gdb_byte *readbuf,
1383 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1384 {
1385 if (ops->beneath != NULL)
1386 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1387 readbuf, writebuf, offset, len);
1388 else
1389 return -1;
1390 }
1391
1392 /* Target vector read/write partial wrapper functions.
1393
1394 NOTE: cagney/2003-10-21: I wonder if having "to_xfer_partial
1395 (inbuf, outbuf)", instead of separate read/write methods, make life
1396 easier. */
1397
1398 static LONGEST
1399 target_read_partial (struct target_ops *ops,
1400 enum target_object object,
1401 const char *annex, gdb_byte *buf,
1402 ULONGEST offset, LONGEST len)
1403 {
1404 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1405 }
1406
1407 static LONGEST
1408 target_write_partial (struct target_ops *ops,
1409 enum target_object object,
1410 const char *annex, const gdb_byte *buf,
1411 ULONGEST offset, LONGEST len)
1412 {
1413 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1414 }
1415
1416 /* Wrappers to perform the full transfer. */
1417 LONGEST
1418 target_read (struct target_ops *ops,
1419 enum target_object object,
1420 const char *annex, gdb_byte *buf,
1421 ULONGEST offset, LONGEST len)
1422 {
1423 LONGEST xfered = 0;
1424 while (xfered < len)
1425 {
1426 LONGEST xfer = target_read_partial (ops, object, annex,
1427 (gdb_byte *) buf + xfered,
1428 offset + xfered, len - xfered);
1429 /* Call an observer, notifying them of the xfer progress? */
1430 if (xfer == 0)
1431 return xfered;
1432 if (xfer < 0)
1433 return -1;
1434 xfered += xfer;
1435 QUIT;
1436 }
1437 return len;
1438 }
1439
1440 /* An alternative to target_write with progress callbacks. */
1441
1442 LONGEST
1443 target_write_with_progress (struct target_ops *ops,
1444 enum target_object object,
1445 const char *annex, const gdb_byte *buf,
1446 ULONGEST offset, LONGEST len,
1447 void (*progress) (ULONGEST, void *), void *baton)
1448 {
1449 LONGEST xfered = 0;
1450
1451 /* Give the progress callback a chance to set up. */
1452 if (progress)
1453 (*progress) (0, baton);
1454
1455 while (xfered < len)
1456 {
1457 LONGEST xfer = target_write_partial (ops, object, annex,
1458 (gdb_byte *) buf + xfered,
1459 offset + xfered, len - xfered);
1460
1461 if (xfer == 0)
1462 return xfered;
1463 if (xfer < 0)
1464 return -1;
1465
1466 if (progress)
1467 (*progress) (xfer, baton);
1468
1469 xfered += xfer;
1470 QUIT;
1471 }
1472 return len;
1473 }
1474
1475 LONGEST
1476 target_write (struct target_ops *ops,
1477 enum target_object object,
1478 const char *annex, const gdb_byte *buf,
1479 ULONGEST offset, LONGEST len)
1480 {
1481 return target_write_with_progress (ops, object, annex, buf, offset, len,
1482 NULL, NULL);
1483 }
1484
1485 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1486 the size of the transferred data. PADDING additional bytes are
1487 available in *BUF_P. This is a helper function for
1488 target_read_alloc; see the declaration of that function for more
1489 information. */
1490
1491 static LONGEST
1492 target_read_alloc_1 (struct target_ops *ops, enum target_object object,
1493 const char *annex, gdb_byte **buf_p, int padding)
1494 {
1495 size_t buf_alloc, buf_pos;
1496 gdb_byte *buf;
1497 LONGEST n;
1498
1499 /* This function does not have a length parameter; it reads the
1500 entire OBJECT). Also, it doesn't support objects fetched partly
1501 from one target and partly from another (in a different stratum,
1502 e.g. a core file and an executable). Both reasons make it
1503 unsuitable for reading memory. */
1504 gdb_assert (object != TARGET_OBJECT_MEMORY);
1505
1506 /* Start by reading up to 4K at a time. The target will throttle
1507 this number down if necessary. */
1508 buf_alloc = 4096;
1509 buf = xmalloc (buf_alloc);
1510 buf_pos = 0;
1511 while (1)
1512 {
1513 n = target_read_partial (ops, object, annex, &buf[buf_pos],
1514 buf_pos, buf_alloc - buf_pos - padding);
1515 if (n < 0)
1516 {
1517 /* An error occurred. */
1518 xfree (buf);
1519 return -1;
1520 }
1521 else if (n == 0)
1522 {
1523 /* Read all there was. */
1524 if (buf_pos == 0)
1525 xfree (buf);
1526 else
1527 *buf_p = buf;
1528 return buf_pos;
1529 }
1530
1531 buf_pos += n;
1532
1533 /* If the buffer is filling up, expand it. */
1534 if (buf_alloc < buf_pos * 2)
1535 {
1536 buf_alloc *= 2;
1537 buf = xrealloc (buf, buf_alloc);
1538 }
1539
1540 QUIT;
1541 }
1542 }
1543
1544 /* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
1545 the size of the transferred data. See the declaration in "target.h"
1546 function for more information about the return value. */
1547
1548 LONGEST
1549 target_read_alloc (struct target_ops *ops, enum target_object object,
1550 const char *annex, gdb_byte **buf_p)
1551 {
1552 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
1553 }
1554
1555 /* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
1556 returned as a string, allocated using xmalloc. If an error occurs
1557 or the transfer is unsupported, NULL is returned. Empty objects
1558 are returned as allocated but empty strings. A warning is issued
1559 if the result contains any embedded NUL bytes. */
1560
1561 char *
1562 target_read_stralloc (struct target_ops *ops, enum target_object object,
1563 const char *annex)
1564 {
1565 gdb_byte *buffer;
1566 LONGEST transferred;
1567
1568 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
1569
1570 if (transferred < 0)
1571 return NULL;
1572
1573 if (transferred == 0)
1574 return xstrdup ("");
1575
1576 buffer[transferred] = 0;
1577 if (strlen (buffer) < transferred)
1578 warning (_("target object %d, annex %s, "
1579 "contained unexpected null characters"),
1580 (int) object, annex ? annex : "(none)");
1581
1582 return (char *) buffer;
1583 }
1584
1585 /* Memory transfer methods. */
1586
1587 void
1588 get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
1589 LONGEST len)
1590 {
1591 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL, buf, addr, len)
1592 != len)
1593 memory_error (EIO, addr);
1594 }
1595
1596 ULONGEST
1597 get_target_memory_unsigned (struct target_ops *ops,
1598 CORE_ADDR addr, int len)
1599 {
1600 gdb_byte buf[sizeof (ULONGEST)];
1601
1602 gdb_assert (len <= sizeof (buf));
1603 get_target_memory (ops, addr, buf, len);
1604 return extract_unsigned_integer (buf, len);
1605 }
1606
1607 static void
1608 target_info (char *args, int from_tty)
1609 {
1610 struct target_ops *t;
1611 int has_all_mem = 0;
1612
1613 if (symfile_objfile != NULL)
1614 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
1615
1616 for (t = target_stack; t != NULL; t = t->beneath)
1617 {
1618 if (!t->to_has_memory)
1619 continue;
1620
1621 if ((int) (t->to_stratum) <= (int) dummy_stratum)
1622 continue;
1623 if (has_all_mem)
1624 printf_unfiltered (_("\tWhile running this, GDB does not access memory from...\n"));
1625 printf_unfiltered ("%s:\n", t->to_longname);
1626 (t->to_files_info) (t);
1627 has_all_mem = t->to_has_all_memory;
1628 }
1629 }
1630
1631 /* This function is called before any new inferior is created, e.g.
1632 by running a program, attaching, or connecting to a target.
1633 It cleans up any state from previous invocations which might
1634 change between runs. This is a subset of what target_preopen
1635 resets (things which might change between targets). */
1636
1637 void
1638 target_pre_inferior (int from_tty)
1639 {
1640 invalidate_target_mem_regions ();
1641
1642 target_clear_description ();
1643 }
1644
1645 /* This is to be called by the open routine before it does
1646 anything. */
1647
1648 void
1649 target_preopen (int from_tty)
1650 {
1651 dont_repeat ();
1652
1653 if (target_has_execution)
1654 {
1655 if (!from_tty
1656 || query (_("A program is being debugged already. Kill it? ")))
1657 target_kill ();
1658 else
1659 error (_("Program not killed."));
1660 }
1661
1662 /* Calling target_kill may remove the target from the stack. But if
1663 it doesn't (which seems like a win for UDI), remove it now. */
1664
1665 if (target_has_execution)
1666 pop_target ();
1667
1668 target_pre_inferior (from_tty);
1669 }
1670
1671 /* Detach a target after doing deferred register stores. */
1672
1673 void
1674 target_detach (char *args, int from_tty)
1675 {
1676 (current_target.to_detach) (args, from_tty);
1677 }
1678
1679 void
1680 target_disconnect (char *args, int from_tty)
1681 {
1682 struct target_ops *t;
1683
1684 for (t = current_target.beneath; t != NULL; t = t->beneath)
1685 if (t->to_disconnect != NULL)
1686 {
1687 if (targetdebug)
1688 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
1689 args, from_tty);
1690 t->to_disconnect (t, args, from_tty);
1691 return;
1692 }
1693
1694 tcomplain ();
1695 }
1696
1697 int
1698 target_async_mask (int mask)
1699 {
1700 int saved_async_masked_status = target_async_mask_value;
1701 target_async_mask_value = mask;
1702 return saved_async_masked_status;
1703 }
1704
1705 /* Look through the list of possible targets for a target that can
1706 follow forks. */
1707
1708 int
1709 target_follow_fork (int follow_child)
1710 {
1711 struct target_ops *t;
1712
1713 for (t = current_target.beneath; t != NULL; t = t->beneath)
1714 {
1715 if (t->to_follow_fork != NULL)
1716 {
1717 int retval = t->to_follow_fork (t, follow_child);
1718 if (targetdebug)
1719 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
1720 follow_child, retval);
1721 return retval;
1722 }
1723 }
1724
1725 /* Some target returned a fork event, but did not know how to follow it. */
1726 internal_error (__FILE__, __LINE__,
1727 "could not find a target to follow fork");
1728 }
1729
1730 /* Look for a target which can describe architectural features, starting
1731 from TARGET. If we find one, return its description. */
1732
1733 const struct target_desc *
1734 target_read_description (struct target_ops *target)
1735 {
1736 struct target_ops *t;
1737
1738 for (t = target; t != NULL; t = t->beneath)
1739 if (t->to_read_description != NULL)
1740 {
1741 const struct target_desc *tdesc;
1742
1743 tdesc = t->to_read_description (t);
1744 if (tdesc)
1745 return tdesc;
1746 }
1747
1748 return NULL;
1749 }
1750
1751 /* Look through the currently pushed targets. If none of them will
1752 be able to restart the currently running process, issue an error
1753 message. */
1754
1755 void
1756 target_require_runnable (void)
1757 {
1758 struct target_ops *t;
1759
1760 for (t = target_stack; t != NULL; t = t->beneath)
1761 {
1762 /* If this target knows how to create a new program, then
1763 assume we will still be able to after killing the current
1764 one. Either killing and mourning will not pop T, or else
1765 find_default_run_target will find it again. */
1766 if (t->to_create_inferior != NULL)
1767 return;
1768
1769 /* Do not worry about thread_stratum targets that can not
1770 create inferiors. Assume they will be pushed again if
1771 necessary, and continue to the process_stratum. */
1772 if (t->to_stratum == thread_stratum)
1773 continue;
1774
1775 error (_("\
1776 The \"%s\" target does not support \"run\". Try \"help target\" or \"continue\"."),
1777 t->to_shortname);
1778 }
1779
1780 /* This function is only called if the target is running. In that
1781 case there should have been a process_stratum target and it
1782 should either know how to create inferiors, or not... */
1783 internal_error (__FILE__, __LINE__, "No targets found");
1784 }
1785
1786 /* Look through the list of possible targets for a target that can
1787 execute a run or attach command without any other data. This is
1788 used to locate the default process stratum.
1789
1790 Result is always valid (error() is called for errors). */
1791
1792 static struct target_ops *
1793 find_default_run_target (char *do_mesg)
1794 {
1795 struct target_ops **t;
1796 struct target_ops *runable = NULL;
1797 int count;
1798
1799 count = 0;
1800
1801 for (t = target_structs; t < target_structs + target_struct_size;
1802 ++t)
1803 {
1804 if ((*t)->to_can_run && target_can_run (*t))
1805 {
1806 runable = *t;
1807 ++count;
1808 }
1809 }
1810
1811 if (count != 1)
1812 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
1813
1814 return runable;
1815 }
1816
1817 void
1818 find_default_attach (char *args, int from_tty)
1819 {
1820 struct target_ops *t;
1821
1822 t = find_default_run_target ("attach");
1823 (t->to_attach) (args, from_tty);
1824 return;
1825 }
1826
1827 void
1828 find_default_create_inferior (char *exec_file, char *allargs, char **env,
1829 int from_tty)
1830 {
1831 struct target_ops *t;
1832
1833 t = find_default_run_target ("run");
1834 (t->to_create_inferior) (exec_file, allargs, env, from_tty);
1835 return;
1836 }
1837
1838 static int
1839 default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
1840 {
1841 return (len <= TYPE_LENGTH (builtin_type_void_data_ptr));
1842 }
1843
1844 static int
1845 return_zero (void)
1846 {
1847 return 0;
1848 }
1849
1850 static int
1851 return_one (void)
1852 {
1853 return 1;
1854 }
1855
1856 static int
1857 return_minus_one (void)
1858 {
1859 return -1;
1860 }
1861
1862 /*
1863 * Resize the to_sections pointer. Also make sure that anyone that
1864 * was holding on to an old value of it gets updated.
1865 * Returns the old size.
1866 */
1867
1868 int
1869 target_resize_to_sections (struct target_ops *target, int num_added)
1870 {
1871 struct target_ops **t;
1872 struct section_table *old_value;
1873 int old_count;
1874
1875 old_value = target->to_sections;
1876
1877 if (target->to_sections)
1878 {
1879 old_count = target->to_sections_end - target->to_sections;
1880 target->to_sections = (struct section_table *)
1881 xrealloc ((char *) target->to_sections,
1882 (sizeof (struct section_table)) * (num_added + old_count));
1883 }
1884 else
1885 {
1886 old_count = 0;
1887 target->to_sections = (struct section_table *)
1888 xmalloc ((sizeof (struct section_table)) * num_added);
1889 }
1890 target->to_sections_end = target->to_sections + (num_added + old_count);
1891
1892 /* Check to see if anyone else was pointing to this structure.
1893 If old_value was null, then no one was. */
1894
1895 if (old_value)
1896 {
1897 for (t = target_structs; t < target_structs + target_struct_size;
1898 ++t)
1899 {
1900 if ((*t)->to_sections == old_value)
1901 {
1902 (*t)->to_sections = target->to_sections;
1903 (*t)->to_sections_end = target->to_sections_end;
1904 }
1905 }
1906 /* There is a flattened view of the target stack in current_target,
1907 so its to_sections pointer might also need updating. */
1908 if (current_target.to_sections == old_value)
1909 {
1910 current_target.to_sections = target->to_sections;
1911 current_target.to_sections_end = target->to_sections_end;
1912 }
1913 }
1914
1915 return old_count;
1916
1917 }
1918
1919 /* Remove all target sections taken from ABFD.
1920
1921 Scan the current target stack for targets whose section tables
1922 refer to sections from BFD, and remove those sections. We use this
1923 when we notice that the inferior has unloaded a shared object, for
1924 example. */
1925 void
1926 remove_target_sections (bfd *abfd)
1927 {
1928 struct target_ops **t;
1929
1930 for (t = target_structs; t < target_structs + target_struct_size; t++)
1931 {
1932 struct section_table *src, *dest;
1933
1934 dest = (*t)->to_sections;
1935 for (src = (*t)->to_sections; src < (*t)->to_sections_end; src++)
1936 if (src->bfd != abfd)
1937 {
1938 /* Keep this section. */
1939 if (dest < src) *dest = *src;
1940 dest++;
1941 }
1942
1943 /* If we've dropped any sections, resize the section table. */
1944 if (dest < src)
1945 target_resize_to_sections (*t, dest - src);
1946 }
1947 }
1948
1949
1950
1951
1952 /* Find a single runnable target in the stack and return it. If for
1953 some reason there is more than one, return NULL. */
1954
1955 struct target_ops *
1956 find_run_target (void)
1957 {
1958 struct target_ops **t;
1959 struct target_ops *runable = NULL;
1960 int count;
1961
1962 count = 0;
1963
1964 for (t = target_structs; t < target_structs + target_struct_size; ++t)
1965 {
1966 if ((*t)->to_can_run && target_can_run (*t))
1967 {
1968 runable = *t;
1969 ++count;
1970 }
1971 }
1972
1973 return (count == 1 ? runable : NULL);
1974 }
1975
1976 /* Find a single core_stratum target in the list of targets and return it.
1977 If for some reason there is more than one, return NULL. */
1978
1979 struct target_ops *
1980 find_core_target (void)
1981 {
1982 struct target_ops **t;
1983 struct target_ops *runable = NULL;
1984 int count;
1985
1986 count = 0;
1987
1988 for (t = target_structs; t < target_structs + target_struct_size;
1989 ++t)
1990 {
1991 if ((*t)->to_stratum == core_stratum)
1992 {
1993 runable = *t;
1994 ++count;
1995 }
1996 }
1997
1998 return (count == 1 ? runable : NULL);
1999 }
2000
2001 /*
2002 * Find the next target down the stack from the specified target.
2003 */
2004
2005 struct target_ops *
2006 find_target_beneath (struct target_ops *t)
2007 {
2008 return t->beneath;
2009 }
2010
2011 \f
2012 /* The inferior process has died. Long live the inferior! */
2013
2014 void
2015 generic_mourn_inferior (void)
2016 {
2017 extern int show_breakpoint_hit_counts;
2018
2019 inferior_ptid = null_ptid;
2020 attach_flag = 0;
2021 breakpoint_init_inferior (inf_exited);
2022 registers_changed ();
2023
2024 reopen_exec_file ();
2025 reinit_frame_cache ();
2026
2027 /* It is confusing to the user for ignore counts to stick around
2028 from previous runs of the inferior. So clear them. */
2029 /* However, it is more confusing for the ignore counts to disappear when
2030 using hit counts. So don't clear them if we're counting hits. */
2031 if (!show_breakpoint_hit_counts)
2032 breakpoint_clear_ignore_counts ();
2033
2034 if (deprecated_detach_hook)
2035 deprecated_detach_hook ();
2036 }
2037 \f
2038 /* Helper function for child_wait and the derivatives of child_wait.
2039 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
2040 translation of that in OURSTATUS. */
2041 void
2042 store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
2043 {
2044 if (WIFEXITED (hoststatus))
2045 {
2046 ourstatus->kind = TARGET_WAITKIND_EXITED;
2047 ourstatus->value.integer = WEXITSTATUS (hoststatus);
2048 }
2049 else if (!WIFSTOPPED (hoststatus))
2050 {
2051 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2052 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
2053 }
2054 else
2055 {
2056 ourstatus->kind = TARGET_WAITKIND_STOPPED;
2057 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
2058 }
2059 }
2060 \f
2061 /* Returns zero to leave the inferior alone, one to interrupt it. */
2062 int (*target_activity_function) (void);
2063 int target_activity_fd;
2064 \f
2065 /* Convert a normal process ID to a string. Returns the string in a
2066 static buffer. */
2067
2068 char *
2069 normal_pid_to_str (ptid_t ptid)
2070 {
2071 static char buf[32];
2072
2073 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
2074 return buf;
2075 }
2076
2077 /* Error-catcher for target_find_memory_regions */
2078 static int dummy_find_memory_regions (int (*ignore1) (), void *ignore2)
2079 {
2080 error (_("No target."));
2081 return 0;
2082 }
2083
2084 /* Error-catcher for target_make_corefile_notes */
2085 static char * dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
2086 {
2087 error (_("No target."));
2088 return NULL;
2089 }
2090
2091 /* Set up the handful of non-empty slots needed by the dummy target
2092 vector. */
2093
2094 static void
2095 init_dummy_target (void)
2096 {
2097 dummy_target.to_shortname = "None";
2098 dummy_target.to_longname = "None";
2099 dummy_target.to_doc = "";
2100 dummy_target.to_attach = find_default_attach;
2101 dummy_target.to_create_inferior = find_default_create_inferior;
2102 dummy_target.to_pid_to_str = normal_pid_to_str;
2103 dummy_target.to_stratum = dummy_stratum;
2104 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
2105 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
2106 dummy_target.to_xfer_partial = default_xfer_partial;
2107 dummy_target.to_magic = OPS_MAGIC;
2108 }
2109 \f
2110 static void
2111 debug_to_open (char *args, int from_tty)
2112 {
2113 debug_target.to_open (args, from_tty);
2114
2115 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
2116 }
2117
2118 static void
2119 debug_to_close (int quitting)
2120 {
2121 target_close (&debug_target, quitting);
2122 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
2123 }
2124
2125 void
2126 target_close (struct target_ops *targ, int quitting)
2127 {
2128 if (targ->to_xclose != NULL)
2129 targ->to_xclose (targ, quitting);
2130 else if (targ->to_close != NULL)
2131 targ->to_close (quitting);
2132 }
2133
2134 static void
2135 debug_to_attach (char *args, int from_tty)
2136 {
2137 debug_target.to_attach (args, from_tty);
2138
2139 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n", args, from_tty);
2140 }
2141
2142
2143 static void
2144 debug_to_post_attach (int pid)
2145 {
2146 debug_target.to_post_attach (pid);
2147
2148 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
2149 }
2150
2151 static void
2152 debug_to_detach (char *args, int from_tty)
2153 {
2154 debug_target.to_detach (args, from_tty);
2155
2156 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n", args, from_tty);
2157 }
2158
2159 static void
2160 debug_to_resume (ptid_t ptid, int step, enum target_signal siggnal)
2161 {
2162 debug_target.to_resume (ptid, step, siggnal);
2163
2164 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n", PIDGET (ptid),
2165 step ? "step" : "continue",
2166 target_signal_to_name (siggnal));
2167 }
2168
2169 static ptid_t
2170 debug_to_wait (ptid_t ptid, struct target_waitstatus *status)
2171 {
2172 ptid_t retval;
2173
2174 retval = debug_target.to_wait (ptid, status);
2175
2176 fprintf_unfiltered (gdb_stdlog,
2177 "target_wait (%d, status) = %d, ", PIDGET (ptid),
2178 PIDGET (retval));
2179 fprintf_unfiltered (gdb_stdlog, "status->kind = ");
2180 switch (status->kind)
2181 {
2182 case TARGET_WAITKIND_EXITED:
2183 fprintf_unfiltered (gdb_stdlog, "exited, status = %d\n",
2184 status->value.integer);
2185 break;
2186 case TARGET_WAITKIND_STOPPED:
2187 fprintf_unfiltered (gdb_stdlog, "stopped, signal = %s\n",
2188 target_signal_to_name (status->value.sig));
2189 break;
2190 case TARGET_WAITKIND_SIGNALLED:
2191 fprintf_unfiltered (gdb_stdlog, "signalled, signal = %s\n",
2192 target_signal_to_name (status->value.sig));
2193 break;
2194 case TARGET_WAITKIND_LOADED:
2195 fprintf_unfiltered (gdb_stdlog, "loaded\n");
2196 break;
2197 case TARGET_WAITKIND_FORKED:
2198 fprintf_unfiltered (gdb_stdlog, "forked\n");
2199 break;
2200 case TARGET_WAITKIND_VFORKED:
2201 fprintf_unfiltered (gdb_stdlog, "vforked\n");
2202 break;
2203 case TARGET_WAITKIND_EXECD:
2204 fprintf_unfiltered (gdb_stdlog, "execd\n");
2205 break;
2206 case TARGET_WAITKIND_SPURIOUS:
2207 fprintf_unfiltered (gdb_stdlog, "spurious\n");
2208 break;
2209 default:
2210 fprintf_unfiltered (gdb_stdlog, "unknown???\n");
2211 break;
2212 }
2213
2214 return retval;
2215 }
2216
2217 static void
2218 debug_print_register (const char * func,
2219 struct regcache *regcache, int regno)
2220 {
2221 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2222 fprintf_unfiltered (gdb_stdlog, "%s ", func);
2223 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
2224 + gdbarch_num_pseudo_regs (gdbarch)
2225 && gdbarch_register_name (gdbarch, regno) != NULL
2226 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
2227 fprintf_unfiltered (gdb_stdlog, "(%s)",
2228 gdbarch_register_name (gdbarch, regno));
2229 else
2230 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
2231 if (regno >= 0)
2232 {
2233 int i, size = register_size (gdbarch, regno);
2234 unsigned char buf[MAX_REGISTER_SIZE];
2235 regcache_cooked_read (regcache, regno, buf);
2236 fprintf_unfiltered (gdb_stdlog, " = ");
2237 for (i = 0; i < size; i++)
2238 {
2239 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
2240 }
2241 if (size <= sizeof (LONGEST))
2242 {
2243 ULONGEST val = extract_unsigned_integer (buf, size);
2244 fprintf_unfiltered (gdb_stdlog, " 0x%s %s",
2245 paddr_nz (val), paddr_d (val));
2246 }
2247 }
2248 fprintf_unfiltered (gdb_stdlog, "\n");
2249 }
2250
2251 static void
2252 debug_to_fetch_registers (struct regcache *regcache, int regno)
2253 {
2254 debug_target.to_fetch_registers (regcache, regno);
2255 debug_print_register ("target_fetch_registers", regcache, regno);
2256 }
2257
2258 static void
2259 debug_to_store_registers (struct regcache *regcache, int regno)
2260 {
2261 debug_target.to_store_registers (regcache, regno);
2262 debug_print_register ("target_store_registers", regcache, regno);
2263 fprintf_unfiltered (gdb_stdlog, "\n");
2264 }
2265
2266 static void
2267 debug_to_prepare_to_store (struct regcache *regcache)
2268 {
2269 debug_target.to_prepare_to_store (regcache);
2270
2271 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
2272 }
2273
2274 static int
2275 deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
2276 int write, struct mem_attrib *attrib,
2277 struct target_ops *target)
2278 {
2279 int retval;
2280
2281 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
2282 attrib, target);
2283
2284 fprintf_unfiltered (gdb_stdlog,
2285 "target_xfer_memory (0x%x, xxx, %d, %s, xxx) = %d",
2286 (unsigned int) memaddr, /* possable truncate long long */
2287 len, write ? "write" : "read", retval);
2288
2289 if (retval > 0)
2290 {
2291 int i;
2292
2293 fputs_unfiltered (", bytes =", gdb_stdlog);
2294 for (i = 0; i < retval; i++)
2295 {
2296 if ((((long) &(myaddr[i])) & 0xf) == 0)
2297 {
2298 if (targetdebug < 2 && i > 0)
2299 {
2300 fprintf_unfiltered (gdb_stdlog, " ...");
2301 break;
2302 }
2303 fprintf_unfiltered (gdb_stdlog, "\n");
2304 }
2305
2306 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
2307 }
2308 }
2309
2310 fputc_unfiltered ('\n', gdb_stdlog);
2311
2312 return retval;
2313 }
2314
2315 static void
2316 debug_to_files_info (struct target_ops *target)
2317 {
2318 debug_target.to_files_info (target);
2319
2320 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
2321 }
2322
2323 static int
2324 debug_to_insert_breakpoint (struct bp_target_info *bp_tgt)
2325 {
2326 int retval;
2327
2328 retval = debug_target.to_insert_breakpoint (bp_tgt);
2329
2330 fprintf_unfiltered (gdb_stdlog,
2331 "target_insert_breakpoint (0x%lx, xxx) = %ld\n",
2332 (unsigned long) bp_tgt->placed_address,
2333 (unsigned long) retval);
2334 return retval;
2335 }
2336
2337 static int
2338 debug_to_remove_breakpoint (struct bp_target_info *bp_tgt)
2339 {
2340 int retval;
2341
2342 retval = debug_target.to_remove_breakpoint (bp_tgt);
2343
2344 fprintf_unfiltered (gdb_stdlog,
2345 "target_remove_breakpoint (0x%lx, xxx) = %ld\n",
2346 (unsigned long) bp_tgt->placed_address,
2347 (unsigned long) retval);
2348 return retval;
2349 }
2350
2351 static int
2352 debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
2353 {
2354 int retval;
2355
2356 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
2357
2358 fprintf_unfiltered (gdb_stdlog,
2359 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
2360 (unsigned long) type,
2361 (unsigned long) cnt,
2362 (unsigned long) from_tty,
2363 (unsigned long) retval);
2364 return retval;
2365 }
2366
2367 static int
2368 debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
2369 {
2370 CORE_ADDR retval;
2371
2372 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
2373
2374 fprintf_unfiltered (gdb_stdlog,
2375 "TARGET_REGION_OK_FOR_HW_WATCHPOINT (%ld, %ld) = 0x%lx\n",
2376 (unsigned long) addr,
2377 (unsigned long) len,
2378 (unsigned long) retval);
2379 return retval;
2380 }
2381
2382 static int
2383 debug_to_stopped_by_watchpoint (void)
2384 {
2385 int retval;
2386
2387 retval = debug_target.to_stopped_by_watchpoint ();
2388
2389 fprintf_unfiltered (gdb_stdlog,
2390 "STOPPED_BY_WATCHPOINT () = %ld\n",
2391 (unsigned long) retval);
2392 return retval;
2393 }
2394
2395 static int
2396 debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
2397 {
2398 int retval;
2399
2400 retval = debug_target.to_stopped_data_address (target, addr);
2401
2402 fprintf_unfiltered (gdb_stdlog,
2403 "target_stopped_data_address ([0x%lx]) = %ld\n",
2404 (unsigned long)*addr,
2405 (unsigned long)retval);
2406 return retval;
2407 }
2408
2409 static int
2410 debug_to_insert_hw_breakpoint (struct bp_target_info *bp_tgt)
2411 {
2412 int retval;
2413
2414 retval = debug_target.to_insert_hw_breakpoint (bp_tgt);
2415
2416 fprintf_unfiltered (gdb_stdlog,
2417 "target_insert_hw_breakpoint (0x%lx, xxx) = %ld\n",
2418 (unsigned long) bp_tgt->placed_address,
2419 (unsigned long) retval);
2420 return retval;
2421 }
2422
2423 static int
2424 debug_to_remove_hw_breakpoint (struct bp_target_info *bp_tgt)
2425 {
2426 int retval;
2427
2428 retval = debug_target.to_remove_hw_breakpoint (bp_tgt);
2429
2430 fprintf_unfiltered (gdb_stdlog,
2431 "target_remove_hw_breakpoint (0x%lx, xxx) = %ld\n",
2432 (unsigned long) bp_tgt->placed_address,
2433 (unsigned long) retval);
2434 return retval;
2435 }
2436
2437 static int
2438 debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type)
2439 {
2440 int retval;
2441
2442 retval = debug_target.to_insert_watchpoint (addr, len, type);
2443
2444 fprintf_unfiltered (gdb_stdlog,
2445 "target_insert_watchpoint (0x%lx, %d, %d) = %ld\n",
2446 (unsigned long) addr, len, type, (unsigned long) retval);
2447 return retval;
2448 }
2449
2450 static int
2451 debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type)
2452 {
2453 int retval;
2454
2455 retval = debug_target.to_remove_watchpoint (addr, len, type);
2456
2457 fprintf_unfiltered (gdb_stdlog,
2458 "target_remove_watchpoint (0x%lx, %d, %d) = %ld\n",
2459 (unsigned long) addr, len, type, (unsigned long) retval);
2460 return retval;
2461 }
2462
2463 static void
2464 debug_to_terminal_init (void)
2465 {
2466 debug_target.to_terminal_init ();
2467
2468 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
2469 }
2470
2471 static void
2472 debug_to_terminal_inferior (void)
2473 {
2474 debug_target.to_terminal_inferior ();
2475
2476 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
2477 }
2478
2479 static void
2480 debug_to_terminal_ours_for_output (void)
2481 {
2482 debug_target.to_terminal_ours_for_output ();
2483
2484 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
2485 }
2486
2487 static void
2488 debug_to_terminal_ours (void)
2489 {
2490 debug_target.to_terminal_ours ();
2491
2492 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
2493 }
2494
2495 static void
2496 debug_to_terminal_save_ours (void)
2497 {
2498 debug_target.to_terminal_save_ours ();
2499
2500 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
2501 }
2502
2503 static void
2504 debug_to_terminal_info (char *arg, int from_tty)
2505 {
2506 debug_target.to_terminal_info (arg, from_tty);
2507
2508 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
2509 from_tty);
2510 }
2511
2512 static void
2513 debug_to_kill (void)
2514 {
2515 debug_target.to_kill ();
2516
2517 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
2518 }
2519
2520 static void
2521 debug_to_load (char *args, int from_tty)
2522 {
2523 debug_target.to_load (args, from_tty);
2524
2525 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
2526 }
2527
2528 static int
2529 debug_to_lookup_symbol (char *name, CORE_ADDR *addrp)
2530 {
2531 int retval;
2532
2533 retval = debug_target.to_lookup_symbol (name, addrp);
2534
2535 fprintf_unfiltered (gdb_stdlog, "target_lookup_symbol (%s, xxx)\n", name);
2536
2537 return retval;
2538 }
2539
2540 static void
2541 debug_to_create_inferior (char *exec_file, char *args, char **env,
2542 int from_tty)
2543 {
2544 debug_target.to_create_inferior (exec_file, args, env, from_tty);
2545
2546 fprintf_unfiltered (gdb_stdlog, "target_create_inferior (%s, %s, xxx, %d)\n",
2547 exec_file, args, from_tty);
2548 }
2549
2550 static void
2551 debug_to_post_startup_inferior (ptid_t ptid)
2552 {
2553 debug_target.to_post_startup_inferior (ptid);
2554
2555 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
2556 PIDGET (ptid));
2557 }
2558
2559 static void
2560 debug_to_acknowledge_created_inferior (int pid)
2561 {
2562 debug_target.to_acknowledge_created_inferior (pid);
2563
2564 fprintf_unfiltered (gdb_stdlog, "target_acknowledge_created_inferior (%d)\n",
2565 pid);
2566 }
2567
2568 static void
2569 debug_to_insert_fork_catchpoint (int pid)
2570 {
2571 debug_target.to_insert_fork_catchpoint (pid);
2572
2573 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d)\n",
2574 pid);
2575 }
2576
2577 static int
2578 debug_to_remove_fork_catchpoint (int pid)
2579 {
2580 int retval;
2581
2582 retval = debug_target.to_remove_fork_catchpoint (pid);
2583
2584 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
2585 pid, retval);
2586
2587 return retval;
2588 }
2589
2590 static void
2591 debug_to_insert_vfork_catchpoint (int pid)
2592 {
2593 debug_target.to_insert_vfork_catchpoint (pid);
2594
2595 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d)\n",
2596 pid);
2597 }
2598
2599 static int
2600 debug_to_remove_vfork_catchpoint (int pid)
2601 {
2602 int retval;
2603
2604 retval = debug_target.to_remove_vfork_catchpoint (pid);
2605
2606 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
2607 pid, retval);
2608
2609 return retval;
2610 }
2611
2612 static void
2613 debug_to_insert_exec_catchpoint (int pid)
2614 {
2615 debug_target.to_insert_exec_catchpoint (pid);
2616
2617 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d)\n",
2618 pid);
2619 }
2620
2621 static int
2622 debug_to_remove_exec_catchpoint (int pid)
2623 {
2624 int retval;
2625
2626 retval = debug_target.to_remove_exec_catchpoint (pid);
2627
2628 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
2629 pid, retval);
2630
2631 return retval;
2632 }
2633
2634 static int
2635 debug_to_has_exited (int pid, int wait_status, int *exit_status)
2636 {
2637 int has_exited;
2638
2639 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
2640
2641 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
2642 pid, wait_status, *exit_status, has_exited);
2643
2644 return has_exited;
2645 }
2646
2647 static void
2648 debug_to_mourn_inferior (void)
2649 {
2650 debug_target.to_mourn_inferior ();
2651
2652 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
2653 }
2654
2655 static int
2656 debug_to_can_run (void)
2657 {
2658 int retval;
2659
2660 retval = debug_target.to_can_run ();
2661
2662 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
2663
2664 return retval;
2665 }
2666
2667 static void
2668 debug_to_notice_signals (ptid_t ptid)
2669 {
2670 debug_target.to_notice_signals (ptid);
2671
2672 fprintf_unfiltered (gdb_stdlog, "target_notice_signals (%d)\n",
2673 PIDGET (ptid));
2674 }
2675
2676 static int
2677 debug_to_thread_alive (ptid_t ptid)
2678 {
2679 int retval;
2680
2681 retval = debug_target.to_thread_alive (ptid);
2682
2683 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
2684 PIDGET (ptid), retval);
2685
2686 return retval;
2687 }
2688
2689 static void
2690 debug_to_find_new_threads (void)
2691 {
2692 debug_target.to_find_new_threads ();
2693
2694 fputs_unfiltered ("target_find_new_threads ()\n", gdb_stdlog);
2695 }
2696
2697 static void
2698 debug_to_stop (void)
2699 {
2700 debug_target.to_stop ();
2701
2702 fprintf_unfiltered (gdb_stdlog, "target_stop ()\n");
2703 }
2704
2705 static void
2706 debug_to_rcmd (char *command,
2707 struct ui_file *outbuf)
2708 {
2709 debug_target.to_rcmd (command, outbuf);
2710 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
2711 }
2712
2713 static char *
2714 debug_to_pid_to_exec_file (int pid)
2715 {
2716 char *exec_file;
2717
2718 exec_file = debug_target.to_pid_to_exec_file (pid);
2719
2720 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
2721 pid, exec_file);
2722
2723 return exec_file;
2724 }
2725
2726 static void
2727 setup_target_debug (void)
2728 {
2729 memcpy (&debug_target, &current_target, sizeof debug_target);
2730
2731 current_target.to_open = debug_to_open;
2732 current_target.to_close = debug_to_close;
2733 current_target.to_attach = debug_to_attach;
2734 current_target.to_post_attach = debug_to_post_attach;
2735 current_target.to_detach = debug_to_detach;
2736 current_target.to_resume = debug_to_resume;
2737 current_target.to_wait = debug_to_wait;
2738 current_target.to_fetch_registers = debug_to_fetch_registers;
2739 current_target.to_store_registers = debug_to_store_registers;
2740 current_target.to_prepare_to_store = debug_to_prepare_to_store;
2741 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
2742 current_target.to_files_info = debug_to_files_info;
2743 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
2744 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
2745 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
2746 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
2747 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
2748 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
2749 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
2750 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
2751 current_target.to_stopped_data_address = debug_to_stopped_data_address;
2752 current_target.to_region_ok_for_hw_watchpoint = debug_to_region_ok_for_hw_watchpoint;
2753 current_target.to_terminal_init = debug_to_terminal_init;
2754 current_target.to_terminal_inferior = debug_to_terminal_inferior;
2755 current_target.to_terminal_ours_for_output = debug_to_terminal_ours_for_output;
2756 current_target.to_terminal_ours = debug_to_terminal_ours;
2757 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
2758 current_target.to_terminal_info = debug_to_terminal_info;
2759 current_target.to_kill = debug_to_kill;
2760 current_target.to_load = debug_to_load;
2761 current_target.to_lookup_symbol = debug_to_lookup_symbol;
2762 current_target.to_create_inferior = debug_to_create_inferior;
2763 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
2764 current_target.to_acknowledge_created_inferior = debug_to_acknowledge_created_inferior;
2765 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
2766 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
2767 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
2768 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
2769 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
2770 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
2771 current_target.to_has_exited = debug_to_has_exited;
2772 current_target.to_mourn_inferior = debug_to_mourn_inferior;
2773 current_target.to_can_run = debug_to_can_run;
2774 current_target.to_notice_signals = debug_to_notice_signals;
2775 current_target.to_thread_alive = debug_to_thread_alive;
2776 current_target.to_find_new_threads = debug_to_find_new_threads;
2777 current_target.to_stop = debug_to_stop;
2778 current_target.to_rcmd = debug_to_rcmd;
2779 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
2780 }
2781 \f
2782
2783 static char targ_desc[] =
2784 "Names of targets and files being debugged.\n\
2785 Shows the entire stack of targets currently in use (including the exec-file,\n\
2786 core-file, and process, if any), as well as the symbol file name.";
2787
2788 static void
2789 do_monitor_command (char *cmd,
2790 int from_tty)
2791 {
2792 if ((current_target.to_rcmd
2793 == (void (*) (char *, struct ui_file *)) tcomplain)
2794 || (current_target.to_rcmd == debug_to_rcmd
2795 && (debug_target.to_rcmd
2796 == (void (*) (char *, struct ui_file *)) tcomplain)))
2797 error (_("\"monitor\" command not supported by this target."));
2798 target_rcmd (cmd, gdb_stdtarg);
2799 }
2800
2801 /* Print the name of each layers of our target stack. */
2802
2803 static void
2804 maintenance_print_target_stack (char *cmd, int from_tty)
2805 {
2806 struct target_ops *t;
2807
2808 printf_filtered (_("The current target stack is:\n"));
2809
2810 for (t = target_stack; t != NULL; t = t->beneath)
2811 {
2812 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
2813 }
2814 }
2815
2816 void
2817 initialize_targets (void)
2818 {
2819 init_dummy_target ();
2820 push_target (&dummy_target);
2821
2822 add_info ("target", target_info, targ_desc);
2823 add_info ("files", target_info, targ_desc);
2824
2825 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
2826 Set target debugging."), _("\
2827 Show target debugging."), _("\
2828 When non-zero, target debugging is enabled. Higher numbers are more\n\
2829 verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
2830 command."),
2831 NULL,
2832 show_targetdebug,
2833 &setdebuglist, &showdebuglist);
2834
2835 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
2836 &trust_readonly, _("\
2837 Set mode for reading from readonly sections."), _("\
2838 Show mode for reading from readonly sections."), _("\
2839 When this mode is on, memory reads from readonly sections (such as .text)\n\
2840 will be read from the object file instead of from the target. This will\n\
2841 result in significant performance improvement for remote targets."),
2842 NULL,
2843 show_trust_readonly,
2844 &setlist, &showlist);
2845
2846 add_com ("monitor", class_obscure, do_monitor_command,
2847 _("Send a command to the remote monitor (remote targets only)."));
2848
2849 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
2850 _("Print the name of each layer of the internal target stack."),
2851 &maintenanceprintlist);
2852
2853 target_dcache = dcache_init ();
2854 }
This page took 0.088452 seconds and 4 git commands to generate.