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