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