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