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