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