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