2011-11-14 Stan Shebs <stan@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / target.c
CommitLineData
c906108c 1/* Select target systems and architectures at runtime for GDB.
7998dfc3 2
6aba47ca 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
7b6bb8da 4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
f6519ebc 5 Free Software Foundation, Inc.
7998dfc3 6
c906108c
SS
7 Contributed by Cygnus Support.
8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
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
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
c5aa993b 14 (at your option) any later version.
c906108c 15
c5aa993b
JM
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.
c906108c 20
c5aa993b 21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
23
24#include "defs.h"
25#include <errno.h>
c906108c
SS
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"
03f2053f 34#include "gdb_wait.h"
4930751a 35#include "dcache.h"
c906108c 36#include <signal.h>
4e052eda 37#include "regcache.h"
0088c768 38#include "gdb_assert.h"
b6591e8b 39#include "gdbcore.h"
9e35dae4 40#include "exceptions.h"
424163ea 41#include "target-descriptions.h"
e1ac3328 42#include "gdbthread.h"
b9db4ced 43#include "solib.h"
07b82ea5 44#include "exec.h"
edb3359d 45#include "inline-frame.h"
2f4d8875 46#include "tracepoint.h"
c906108c 47
a14ed312 48static void target_info (char *, int);
c906108c 49
a14ed312 50static void default_terminal_info (char *, int);
c906108c 51
5009afc5
AS
52static int default_watchpoint_addr_within_range (struct target_ops *,
53 CORE_ADDR, CORE_ADDR, int);
54
e0d24f8d
WZ
55static int default_region_ok_for_hw_watchpoint (CORE_ADDR, int);
56
c25c4a8b 57static void tcomplain (void) ATTRIBUTE_NORETURN;
c906108c 58
a14ed312 59static int nomemory (CORE_ADDR, char *, int, int, struct target_ops *);
c906108c 60
a14ed312 61static int return_zero (void);
c906108c 62
a14ed312 63static int return_one (void);
c906108c 64
ccaa32c7
GS
65static int return_minus_one (void);
66
a14ed312 67void target_ignore (void);
c906108c 68
a14ed312 69static void target_command (char *, int);
c906108c 70
a14ed312 71static struct target_ops *find_default_run_target (char *);
c906108c 72
4b8a223f 73static LONGEST default_xfer_partial (struct target_ops *ops,
0088c768 74 enum target_object object,
1b0ba102
AC
75 const char *annex, gdb_byte *readbuf,
76 const gdb_byte *writebuf,
8aa91c1e 77 ULONGEST offset, LONGEST len);
0088c768 78
cf7a04e8
DJ
79static LONGEST current_xfer_partial (struct target_ops *ops,
80 enum target_object object,
81 const char *annex, gdb_byte *readbuf,
82 const gdb_byte *writebuf,
83 ULONGEST offset, LONGEST len);
c906108c 84
cf7a04e8
DJ
85static LONGEST target_xfer_partial (struct target_ops *ops,
86 enum target_object object,
87 const char *annex,
88 void *readbuf, const void *writebuf,
89 ULONGEST offset, LONGEST len);
c906108c 90
c2250ad1
UW
91static struct gdbarch *default_thread_architecture (struct target_ops *ops,
92 ptid_t ptid);
93
a14ed312 94static void init_dummy_target (void);
c906108c 95
aa869812
AC
96static struct target_ops debug_target;
97
a14ed312 98static void debug_to_open (char *, int);
c906108c 99
316f2060 100static void debug_to_prepare_to_store (struct regcache *);
c906108c 101
a14ed312 102static void debug_to_files_info (struct target_ops *);
c906108c 103
a6d9a66e
UW
104static int debug_to_insert_breakpoint (struct gdbarch *,
105 struct bp_target_info *);
c906108c 106
a6d9a66e
UW
107static int debug_to_remove_breakpoint (struct gdbarch *,
108 struct bp_target_info *);
c906108c 109
ccaa32c7
GS
110static int debug_to_can_use_hw_breakpoint (int, int, int);
111
a6d9a66e
UW
112static int debug_to_insert_hw_breakpoint (struct gdbarch *,
113 struct bp_target_info *);
ccaa32c7 114
a6d9a66e
UW
115static int debug_to_remove_hw_breakpoint (struct gdbarch *,
116 struct bp_target_info *);
ccaa32c7 117
0cf6dd15
TJB
118static int debug_to_insert_watchpoint (CORE_ADDR, int, int,
119 struct expression *);
ccaa32c7 120
0cf6dd15
TJB
121static int debug_to_remove_watchpoint (CORE_ADDR, int, int,
122 struct expression *);
ccaa32c7
GS
123
124static int debug_to_stopped_by_watchpoint (void);
125
4aa7a7f5 126static int debug_to_stopped_data_address (struct target_ops *, CORE_ADDR *);
ccaa32c7 127
5009afc5
AS
128static int debug_to_watchpoint_addr_within_range (struct target_ops *,
129 CORE_ADDR, CORE_ADDR, int);
130
e0d24f8d
WZ
131static int debug_to_region_ok_for_hw_watchpoint (CORE_ADDR, int);
132
0cf6dd15
TJB
133static int debug_to_can_accel_watchpoint_condition (CORE_ADDR, int, int,
134 struct expression *);
135
a14ed312 136static void debug_to_terminal_init (void);
c906108c 137
a14ed312 138static void debug_to_terminal_inferior (void);
c906108c 139
a14ed312 140static void debug_to_terminal_ours_for_output (void);
c906108c 141
a790ad35
SC
142static void debug_to_terminal_save_ours (void);
143
a14ed312 144static void debug_to_terminal_ours (void);
c906108c 145
a14ed312 146static void debug_to_terminal_info (char *, int);
c906108c 147
a14ed312 148static void debug_to_load (char *, int);
c906108c 149
a14ed312 150static int debug_to_can_run (void);
c906108c 151
94cc34af 152static void debug_to_stop (ptid_t);
c906108c 153
c906108c 154/* Pointer to array of target architecture structures; the size of the
2bc416ba 155 array; the current index into the array; the allocated size of the
c906108c
SS
156 array. */
157struct target_ops **target_structs;
158unsigned target_struct_size;
159unsigned target_struct_index;
160unsigned target_struct_allocsize;
161#define DEFAULT_ALLOCSIZE 10
162
163/* The initial current target, so that there is always a semi-valid
164 current target. */
165
166static struct target_ops dummy_target;
167
168/* Top of target stack. */
169
258b763a 170static struct target_ops *target_stack;
c906108c
SS
171
172/* The target structure we are currently using to talk to a process
173 or file or whatever "inferior" we have. */
174
175struct target_ops current_target;
176
177/* Command list for target. */
178
179static struct cmd_list_element *targetlist = NULL;
180
cf7a04e8
DJ
181/* Nonzero if we should trust readonly sections from the
182 executable when reading memory. */
183
184static int trust_readonly = 0;
185
8defab1a
DJ
186/* Nonzero if we should show true memory content including
187 memory breakpoint inserted by gdb. */
188
189static int show_memory_breakpoints = 0;
190
d914c394
SS
191/* These globals control whether GDB attempts to perform these
192 operations; they are useful for targets that need to prevent
193 inadvertant disruption, such as in non-stop mode. */
194
195int may_write_registers = 1;
196
197int may_write_memory = 1;
198
199int may_insert_breakpoints = 1;
200
201int may_insert_tracepoints = 1;
202
203int may_insert_fast_tracepoints = 1;
204
205int may_stop = 1;
206
c906108c
SS
207/* Non-zero if we want to see trace of target level stuff. */
208
209static int targetdebug = 0;
920d2a44
AC
210static void
211show_targetdebug (struct ui_file *file, int from_tty,
212 struct cmd_list_element *c, const char *value)
213{
214 fprintf_filtered (file, _("Target debugging is %s.\n"), value);
215}
c906108c 216
a14ed312 217static void setup_target_debug (void);
c906108c 218
4e5d721f
DE
219/* The option sets this. */
220static int stack_cache_enabled_p_1 = 1;
221/* And set_stack_cache_enabled_p updates this.
222 The reason for the separation is so that we don't flush the cache for
223 on->on transitions. */
224static int stack_cache_enabled_p = 1;
225
226/* This is called *after* the stack-cache has been set.
227 Flush the cache for off->on and on->off transitions.
228 There's no real need to flush the cache for on->off transitions,
229 except cleanliness. */
230
231static void
232set_stack_cache_enabled_p (char *args, int from_tty,
233 struct cmd_list_element *c)
234{
235 if (stack_cache_enabled_p != stack_cache_enabled_p_1)
236 target_dcache_invalidate ();
237
238 stack_cache_enabled_p = stack_cache_enabled_p_1;
239}
240
241static void
242show_stack_cache_enabled_p (struct ui_file *file, int from_tty,
243 struct cmd_list_element *c, const char *value)
244{
245 fprintf_filtered (file, _("Cache use for stack accesses is %s.\n"), value);
246}
247
248/* Cache of memory operations, to speed up remote access. */
249static DCACHE *target_dcache;
250
251/* Invalidate the target dcache. */
252
253void
254target_dcache_invalidate (void)
255{
256 dcache_invalidate (target_dcache);
257}
4930751a 258
c906108c
SS
259/* The user just typed 'target' without the name of a target. */
260
c906108c 261static void
fba45db2 262target_command (char *arg, int from_tty)
c906108c
SS
263{
264 fputs_filtered ("Argument required (target name). Try `help target'\n",
265 gdb_stdout);
266}
267
c35b1492
PA
268/* Default target_has_* methods for process_stratum targets. */
269
270int
271default_child_has_all_memory (struct target_ops *ops)
272{
273 /* If no inferior selected, then we can't read memory here. */
274 if (ptid_equal (inferior_ptid, null_ptid))
275 return 0;
276
277 return 1;
278}
279
280int
281default_child_has_memory (struct target_ops *ops)
282{
283 /* If no inferior selected, then we can't read memory here. */
284 if (ptid_equal (inferior_ptid, null_ptid))
285 return 0;
286
287 return 1;
288}
289
290int
291default_child_has_stack (struct target_ops *ops)
292{
293 /* If no inferior selected, there's no stack. */
294 if (ptid_equal (inferior_ptid, null_ptid))
295 return 0;
296
297 return 1;
298}
299
300int
301default_child_has_registers (struct target_ops *ops)
302{
303 /* Can't read registers from no inferior. */
304 if (ptid_equal (inferior_ptid, null_ptid))
305 return 0;
306
307 return 1;
308}
309
310int
aeaec162 311default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
c35b1492
PA
312{
313 /* If there's no thread selected, then we can't make it run through
314 hoops. */
aeaec162 315 if (ptid_equal (the_ptid, null_ptid))
c35b1492
PA
316 return 0;
317
318 return 1;
319}
320
321
322int
323target_has_all_memory_1 (void)
324{
325 struct target_ops *t;
326
327 for (t = current_target.beneath; t != NULL; t = t->beneath)
328 if (t->to_has_all_memory (t))
329 return 1;
330
331 return 0;
332}
333
334int
335target_has_memory_1 (void)
336{
337 struct target_ops *t;
338
339 for (t = current_target.beneath; t != NULL; t = t->beneath)
340 if (t->to_has_memory (t))
341 return 1;
342
343 return 0;
344}
345
346int
347target_has_stack_1 (void)
348{
349 struct target_ops *t;
350
351 for (t = current_target.beneath; t != NULL; t = t->beneath)
352 if (t->to_has_stack (t))
353 return 1;
354
355 return 0;
356}
357
358int
359target_has_registers_1 (void)
360{
361 struct target_ops *t;
362
363 for (t = current_target.beneath; t != NULL; t = t->beneath)
364 if (t->to_has_registers (t))
365 return 1;
366
367 return 0;
368}
369
370int
aeaec162 371target_has_execution_1 (ptid_t the_ptid)
c35b1492
PA
372{
373 struct target_ops *t;
374
375 for (t = current_target.beneath; t != NULL; t = t->beneath)
aeaec162 376 if (t->to_has_execution (t, the_ptid))
c35b1492
PA
377 return 1;
378
379 return 0;
380}
381
aeaec162
TT
382int
383target_has_execution_current (void)
384{
385 return target_has_execution_1 (inferior_ptid);
386}
387
c906108c
SS
388/* Add a possible target architecture to the list. */
389
390void
fba45db2 391add_target (struct target_ops *t)
c906108c 392{
0088c768 393 /* Provide default values for all "must have" methods. */
0b603eba
AC
394 if (t->to_xfer_partial == NULL)
395 t->to_xfer_partial = default_xfer_partial;
0088c768 396
c35b1492
PA
397 if (t->to_has_all_memory == NULL)
398 t->to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
399
400 if (t->to_has_memory == NULL)
401 t->to_has_memory = (int (*) (struct target_ops *)) return_zero;
402
403 if (t->to_has_stack == NULL)
404 t->to_has_stack = (int (*) (struct target_ops *)) return_zero;
405
406 if (t->to_has_registers == NULL)
407 t->to_has_registers = (int (*) (struct target_ops *)) return_zero;
408
409 if (t->to_has_execution == NULL)
aeaec162 410 t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
c35b1492 411
c906108c
SS
412 if (!target_structs)
413 {
414 target_struct_allocsize = DEFAULT_ALLOCSIZE;
415 target_structs = (struct target_ops **) xmalloc
416 (target_struct_allocsize * sizeof (*target_structs));
417 }
418 if (target_struct_size >= target_struct_allocsize)
419 {
420 target_struct_allocsize *= 2;
421 target_structs = (struct target_ops **)
c5aa993b
JM
422 xrealloc ((char *) target_structs,
423 target_struct_allocsize * sizeof (*target_structs));
c906108c
SS
424 }
425 target_structs[target_struct_size++] = t;
c906108c
SS
426
427 if (targetlist == NULL)
1bedd215
AC
428 add_prefix_cmd ("target", class_run, target_command, _("\
429Connect to a target machine or process.\n\
c906108c
SS
430The first argument is the type or protocol of the target machine.\n\
431Remaining arguments are interpreted by the target protocol. For more\n\
432information on the arguments for a particular protocol, type\n\
1bedd215 433`help target ' followed by the protocol name."),
c906108c
SS
434 &targetlist, "target ", 0, &cmdlist);
435 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
436}
437
438/* Stub functions */
439
440void
fba45db2 441target_ignore (void)
c906108c
SS
442{
443}
444
7d85a9c0
JB
445void
446target_kill (void)
447{
448 struct target_ops *t;
449
450 for (t = current_target.beneath; t != NULL; t = t->beneath)
451 if (t->to_kill != NULL)
452 {
453 if (targetdebug)
454 fprintf_unfiltered (gdb_stdlog, "target_kill ()\n");
455
456 t->to_kill (t);
457 return;
458 }
459
460 noprocess ();
461}
462
11cf8741
JM
463void
464target_load (char *arg, int from_tty)
465{
4e5d721f 466 target_dcache_invalidate ();
11cf8741
JM
467 (*current_target.to_load) (arg, from_tty);
468}
469
947b8855
PA
470void
471target_create_inferior (char *exec_file, char *args,
472 char **env, int from_tty)
136d6dae
VP
473{
474 struct target_ops *t;
5d502164 475
136d6dae
VP
476 for (t = current_target.beneath; t != NULL; t = t->beneath)
477 {
478 if (t->to_create_inferior != NULL)
479 {
480 t->to_create_inferior (t, exec_file, args, env, from_tty);
947b8855
PA
481 if (targetdebug)
482 fprintf_unfiltered (gdb_stdlog,
483 "target_create_inferior (%s, %s, xxx, %d)\n",
484 exec_file, args, from_tty);
136d6dae
VP
485 return;
486 }
487 }
488
489 internal_error (__FILE__, __LINE__,
9b20d036 490 _("could not find a target to create inferior"));
136d6dae
VP
491}
492
d9d2d8b6
PA
493void
494target_terminal_inferior (void)
495{
496 /* A background resume (``run&'') should leave GDB in control of the
c378eb4e 497 terminal. Use target_can_async_p, not target_is_async_p, since at
ba7f6c64
VP
498 this point the target is not async yet. However, if sync_execution
499 is not set, we know it will become async prior to resume. */
500 if (target_can_async_p () && !sync_execution)
d9d2d8b6
PA
501 return;
502
503 /* If GDB is resuming the inferior in the foreground, install
504 inferior's terminal modes. */
505 (*current_target.to_terminal_inferior) ();
506}
136d6dae 507
c906108c 508static int
fba45db2
KB
509nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
510 struct target_ops *t)
c906108c 511{
c378eb4e
MS
512 errno = EIO; /* Can't read/write this location. */
513 return 0; /* No bytes handled. */
c906108c
SS
514}
515
516static void
fba45db2 517tcomplain (void)
c906108c 518{
8a3fe4f8 519 error (_("You can't do that when your target is `%s'"),
c906108c
SS
520 current_target.to_shortname);
521}
522
523void
fba45db2 524noprocess (void)
c906108c 525{
8a3fe4f8 526 error (_("You can't do that without a process to debug."));
c906108c
SS
527}
528
c906108c 529static void
fba45db2 530default_terminal_info (char *args, int from_tty)
c906108c 531{
a3f17187 532 printf_unfiltered (_("No saved terminal information.\n"));
c906108c
SS
533}
534
0ef643c8
JB
535/* A default implementation for the to_get_ada_task_ptid target method.
536
537 This function builds the PTID by using both LWP and TID as part of
538 the PTID lwp and tid elements. The pid used is the pid of the
539 inferior_ptid. */
540
2c0b251b 541static ptid_t
0ef643c8
JB
542default_get_ada_task_ptid (long lwp, long tid)
543{
544 return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
545}
546
32231432
PA
547static enum exec_direction_kind
548default_execution_direction (void)
549{
550 if (!target_can_execute_reverse)
551 return EXEC_FORWARD;
552 else if (!target_can_async_p ())
553 return EXEC_FORWARD;
554 else
555 gdb_assert_not_reached ("\
556to_execution_direction must be implemented for reverse async");
557}
558
7998dfc3
AC
559/* Go through the target stack from top to bottom, copying over zero
560 entries in current_target, then filling in still empty entries. In
561 effect, we are doing class inheritance through the pushed target
562 vectors.
563
564 NOTE: cagney/2003-10-17: The problem with this inheritance, as it
565 is currently implemented, is that it discards any knowledge of
566 which target an inherited method originally belonged to.
567 Consequently, new new target methods should instead explicitly and
568 locally search the target stack for the target that can handle the
569 request. */
c906108c
SS
570
571static void
7998dfc3 572update_current_target (void)
c906108c 573{
7998dfc3
AC
574 struct target_ops *t;
575
08d8bcd7 576 /* First, reset current's contents. */
7998dfc3
AC
577 memset (&current_target, 0, sizeof (current_target));
578
579#define INHERIT(FIELD, TARGET) \
580 if (!current_target.FIELD) \
581 current_target.FIELD = (TARGET)->FIELD
582
583 for (t = target_stack; t; t = t->beneath)
584 {
585 INHERIT (to_shortname, t);
586 INHERIT (to_longname, t);
587 INHERIT (to_doc, t);
b52323fa
UW
588 /* Do not inherit to_open. */
589 /* Do not inherit to_close. */
136d6dae 590 /* Do not inherit to_attach. */
7998dfc3 591 INHERIT (to_post_attach, t);
dc177b7a 592 INHERIT (to_attach_no_wait, t);
136d6dae 593 /* Do not inherit to_detach. */
597320e7 594 /* Do not inherit to_disconnect. */
28439f5e 595 /* Do not inherit to_resume. */
117de6a9 596 /* Do not inherit to_wait. */
28439f5e
PA
597 /* Do not inherit to_fetch_registers. */
598 /* Do not inherit to_store_registers. */
7998dfc3 599 INHERIT (to_prepare_to_store, t);
c8e73a31 600 INHERIT (deprecated_xfer_memory, t);
7998dfc3
AC
601 INHERIT (to_files_info, t);
602 INHERIT (to_insert_breakpoint, t);
603 INHERIT (to_remove_breakpoint, t);
604 INHERIT (to_can_use_hw_breakpoint, t);
605 INHERIT (to_insert_hw_breakpoint, t);
606 INHERIT (to_remove_hw_breakpoint, t);
f1310107 607 /* Do not inherit to_ranged_break_num_registers. */
7998dfc3
AC
608 INHERIT (to_insert_watchpoint, t);
609 INHERIT (to_remove_watchpoint, t);
9c06b0b4
TJB
610 /* Do not inherit to_insert_mask_watchpoint. */
611 /* Do not inherit to_remove_mask_watchpoint. */
7998dfc3 612 INHERIT (to_stopped_data_address, t);
74174d2e 613 INHERIT (to_have_steppable_watchpoint, t);
7998dfc3 614 INHERIT (to_have_continuable_watchpoint, t);
5009afc5
AS
615 INHERIT (to_stopped_by_watchpoint, t);
616 INHERIT (to_watchpoint_addr_within_range, t);
e0d24f8d 617 INHERIT (to_region_ok_for_hw_watchpoint, t);
0cf6dd15 618 INHERIT (to_can_accel_watchpoint_condition, t);
9c06b0b4 619 /* Do not inherit to_masked_watch_num_registers. */
7998dfc3
AC
620 INHERIT (to_terminal_init, t);
621 INHERIT (to_terminal_inferior, t);
622 INHERIT (to_terminal_ours_for_output, t);
623 INHERIT (to_terminal_ours, t);
624 INHERIT (to_terminal_save_ours, t);
625 INHERIT (to_terminal_info, t);
7d85a9c0 626 /* Do not inherit to_kill. */
7998dfc3 627 INHERIT (to_load, t);
136d6dae 628 /* Do no inherit to_create_inferior. */
7998dfc3 629 INHERIT (to_post_startup_inferior, t);
7998dfc3
AC
630 INHERIT (to_insert_fork_catchpoint, t);
631 INHERIT (to_remove_fork_catchpoint, t);
632 INHERIT (to_insert_vfork_catchpoint, t);
633 INHERIT (to_remove_vfork_catchpoint, t);
ee057212 634 /* Do not inherit to_follow_fork. */
7998dfc3
AC
635 INHERIT (to_insert_exec_catchpoint, t);
636 INHERIT (to_remove_exec_catchpoint, t);
a96d9b2e 637 INHERIT (to_set_syscall_catchpoint, t);
7998dfc3 638 INHERIT (to_has_exited, t);
82892036 639 /* Do not inherit to_mourn_inferior. */
7998dfc3 640 INHERIT (to_can_run, t);
2455069d 641 /* Do not inherit to_pass_signals. */
28439f5e
PA
642 /* Do not inherit to_thread_alive. */
643 /* Do not inherit to_find_new_threads. */
117de6a9 644 /* Do not inherit to_pid_to_str. */
7998dfc3 645 INHERIT (to_extra_thread_info, t);
4694da01 646 INHERIT (to_thread_name, t);
7998dfc3 647 INHERIT (to_stop, t);
4b8a223f 648 /* Do not inherit to_xfer_partial. */
7998dfc3 649 INHERIT (to_rcmd, t);
7998dfc3 650 INHERIT (to_pid_to_exec_file, t);
49d03eab 651 INHERIT (to_log_command, t);
7998dfc3 652 INHERIT (to_stratum, t);
c378eb4e
MS
653 /* Do not inherit to_has_all_memory. */
654 /* Do not inherit to_has_memory. */
655 /* Do not inherit to_has_stack. */
656 /* Do not inherit to_has_registers. */
657 /* Do not inherit to_has_execution. */
7998dfc3 658 INHERIT (to_has_thread_control, t);
7998dfc3
AC
659 INHERIT (to_can_async_p, t);
660 INHERIT (to_is_async_p, t);
661 INHERIT (to_async, t);
7998dfc3
AC
662 INHERIT (to_find_memory_regions, t);
663 INHERIT (to_make_corefile_notes, t);
6b04bdb7
MS
664 INHERIT (to_get_bookmark, t);
665 INHERIT (to_goto_bookmark, t);
117de6a9 666 /* Do not inherit to_get_thread_local_address. */
b2175913 667 INHERIT (to_can_execute_reverse, t);
32231432 668 INHERIT (to_execution_direction, t);
c2250ad1 669 INHERIT (to_thread_architecture, t);
424163ea 670 /* Do not inherit to_read_description. */
0ef643c8 671 INHERIT (to_get_ada_task_ptid, t);
08388c79 672 /* Do not inherit to_search_memory. */
8a305172 673 INHERIT (to_supports_multi_process, t);
d248b706 674 INHERIT (to_supports_enable_disable_tracepoint, t);
3065dfb6 675 INHERIT (to_supports_string_tracing, t);
35b1e5cc
SS
676 INHERIT (to_trace_init, t);
677 INHERIT (to_download_tracepoint, t);
1e4d1764 678 INHERIT (to_can_download_tracepoint, t);
35b1e5cc 679 INHERIT (to_download_trace_state_variable, t);
d248b706
KY
680 INHERIT (to_enable_tracepoint, t);
681 INHERIT (to_disable_tracepoint, t);
35b1e5cc
SS
682 INHERIT (to_trace_set_readonly_regions, t);
683 INHERIT (to_trace_start, t);
684 INHERIT (to_get_trace_status, t);
685 INHERIT (to_trace_stop, t);
686 INHERIT (to_trace_find, t);
687 INHERIT (to_get_trace_state_variable_value, t);
00bf0b85
SS
688 INHERIT (to_save_trace_data, t);
689 INHERIT (to_upload_tracepoints, t);
690 INHERIT (to_upload_trace_state_variables, t);
691 INHERIT (to_get_raw_trace_data, t);
405f8e94 692 INHERIT (to_get_min_fast_tracepoint_insn_len, t);
35b1e5cc 693 INHERIT (to_set_disconnected_tracing, t);
4daf5ac0 694 INHERIT (to_set_circular_trace_buffer, t);
711e434b 695 INHERIT (to_get_tib_address, t);
d914c394 696 INHERIT (to_set_permissions, t);
0fb4aa4b
PA
697 INHERIT (to_static_tracepoint_marker_at, t);
698 INHERIT (to_static_tracepoint_markers_by_strid, t);
b3b9301e 699 INHERIT (to_traceframe_info, t);
7998dfc3 700 INHERIT (to_magic, t);
fd79ecee 701 /* Do not inherit to_memory_map. */
a76d924d
DJ
702 /* Do not inherit to_flash_erase. */
703 /* Do not inherit to_flash_done. */
7998dfc3
AC
704 }
705#undef INHERIT
706
707 /* Clean up a target struct so it no longer has any zero pointers in
0088c768
AC
708 it. Some entries are defaulted to a method that print an error,
709 others are hard-wired to a standard recursive default. */
c906108c
SS
710
711#define de_fault(field, value) \
7998dfc3
AC
712 if (!current_target.field) \
713 current_target.field = value
0d06e24b 714
2bc416ba
DJ
715 de_fault (to_open,
716 (void (*) (char *, int))
0d06e24b 717 tcomplain);
2bc416ba
DJ
718 de_fault (to_close,
719 (void (*) (int))
0d06e24b 720 target_ignore);
2bc416ba
DJ
721 de_fault (to_post_attach,
722 (void (*) (int))
0d06e24b 723 target_ignore);
2bc416ba 724 de_fault (to_prepare_to_store,
316f2060 725 (void (*) (struct regcache *))
0d06e24b 726 noprocess);
2bc416ba 727 de_fault (deprecated_xfer_memory,
3e43a32a
MS
728 (int (*) (CORE_ADDR, gdb_byte *, int, int,
729 struct mem_attrib *, struct target_ops *))
0d06e24b 730 nomemory);
2bc416ba
DJ
731 de_fault (to_files_info,
732 (void (*) (struct target_ops *))
0d06e24b 733 target_ignore);
2bc416ba 734 de_fault (to_insert_breakpoint,
0d06e24b 735 memory_insert_breakpoint);
2bc416ba 736 de_fault (to_remove_breakpoint,
0d06e24b 737 memory_remove_breakpoint);
ccaa32c7
GS
738 de_fault (to_can_use_hw_breakpoint,
739 (int (*) (int, int, int))
740 return_zero);
741 de_fault (to_insert_hw_breakpoint,
a6d9a66e 742 (int (*) (struct gdbarch *, struct bp_target_info *))
ccaa32c7
GS
743 return_minus_one);
744 de_fault (to_remove_hw_breakpoint,
a6d9a66e 745 (int (*) (struct gdbarch *, struct bp_target_info *))
ccaa32c7
GS
746 return_minus_one);
747 de_fault (to_insert_watchpoint,
0cf6dd15 748 (int (*) (CORE_ADDR, int, int, struct expression *))
ccaa32c7
GS
749 return_minus_one);
750 de_fault (to_remove_watchpoint,
0cf6dd15 751 (int (*) (CORE_ADDR, int, int, struct expression *))
ccaa32c7
GS
752 return_minus_one);
753 de_fault (to_stopped_by_watchpoint,
754 (int (*) (void))
755 return_zero);
756 de_fault (to_stopped_data_address,
4aa7a7f5 757 (int (*) (struct target_ops *, CORE_ADDR *))
ccaa32c7 758 return_zero);
5009afc5
AS
759 de_fault (to_watchpoint_addr_within_range,
760 default_watchpoint_addr_within_range);
e0d24f8d
WZ
761 de_fault (to_region_ok_for_hw_watchpoint,
762 default_region_ok_for_hw_watchpoint);
0cf6dd15
TJB
763 de_fault (to_can_accel_watchpoint_condition,
764 (int (*) (CORE_ADDR, int, int, struct expression *))
765 return_zero);
2bc416ba
DJ
766 de_fault (to_terminal_init,
767 (void (*) (void))
0d06e24b 768 target_ignore);
2bc416ba
DJ
769 de_fault (to_terminal_inferior,
770 (void (*) (void))
0d06e24b 771 target_ignore);
2bc416ba
DJ
772 de_fault (to_terminal_ours_for_output,
773 (void (*) (void))
0d06e24b 774 target_ignore);
2bc416ba
DJ
775 de_fault (to_terminal_ours,
776 (void (*) (void))
0d06e24b 777 target_ignore);
2bc416ba
DJ
778 de_fault (to_terminal_save_ours,
779 (void (*) (void))
a790ad35 780 target_ignore);
2bc416ba 781 de_fault (to_terminal_info,
0d06e24b 782 default_terminal_info);
2bc416ba
DJ
783 de_fault (to_load,
784 (void (*) (char *, int))
0d06e24b 785 tcomplain);
2bc416ba
DJ
786 de_fault (to_post_startup_inferior,
787 (void (*) (ptid_t))
0d06e24b 788 target_ignore);
2bc416ba 789 de_fault (to_insert_fork_catchpoint,
77b06cd7
TJB
790 (int (*) (int))
791 return_one);
2bc416ba
DJ
792 de_fault (to_remove_fork_catchpoint,
793 (int (*) (int))
77b06cd7 794 return_one);
2bc416ba 795 de_fault (to_insert_vfork_catchpoint,
77b06cd7
TJB
796 (int (*) (int))
797 return_one);
2bc416ba
DJ
798 de_fault (to_remove_vfork_catchpoint,
799 (int (*) (int))
77b06cd7 800 return_one);
2bc416ba 801 de_fault (to_insert_exec_catchpoint,
77b06cd7
TJB
802 (int (*) (int))
803 return_one);
2bc416ba
DJ
804 de_fault (to_remove_exec_catchpoint,
805 (int (*) (int))
77b06cd7 806 return_one);
a96d9b2e
SDJ
807 de_fault (to_set_syscall_catchpoint,
808 (int (*) (int, int, int, int, int *))
77b06cd7 809 return_one);
2bc416ba
DJ
810 de_fault (to_has_exited,
811 (int (*) (int, int, int *))
0d06e24b 812 return_zero);
2bc416ba 813 de_fault (to_can_run,
0d06e24b 814 return_zero);
2bc416ba
DJ
815 de_fault (to_extra_thread_info,
816 (char *(*) (struct thread_info *))
0d06e24b 817 return_zero);
4694da01
TT
818 de_fault (to_thread_name,
819 (char *(*) (struct thread_info *))
820 return_zero);
2bc416ba 821 de_fault (to_stop,
94cc34af 822 (void (*) (ptid_t))
0d06e24b 823 target_ignore);
cf7a04e8 824 current_target.to_xfer_partial = current_xfer_partial;
2bc416ba
DJ
825 de_fault (to_rcmd,
826 (void (*) (char *, struct ui_file *))
0d06e24b 827 tcomplain);
2bc416ba
DJ
828 de_fault (to_pid_to_exec_file,
829 (char *(*) (int))
0d06e24b 830 return_zero);
2bc416ba
DJ
831 de_fault (to_async,
832 (void (*) (void (*) (enum inferior_event_type, void*), void*))
0d06e24b 833 tcomplain);
c2250ad1
UW
834 de_fault (to_thread_architecture,
835 default_thread_architecture);
424163ea 836 current_target.to_read_description = NULL;
0ef643c8
JB
837 de_fault (to_get_ada_task_ptid,
838 (ptid_t (*) (long, long))
839 default_get_ada_task_ptid);
8a305172
PA
840 de_fault (to_supports_multi_process,
841 (int (*) (void))
842 return_zero);
d248b706
KY
843 de_fault (to_supports_enable_disable_tracepoint,
844 (int (*) (void))
845 return_zero);
3065dfb6
SS
846 de_fault (to_supports_string_tracing,
847 (int (*) (void))
848 return_zero);
35b1e5cc
SS
849 de_fault (to_trace_init,
850 (void (*) (void))
851 tcomplain);
852 de_fault (to_download_tracepoint,
e8ba3115 853 (void (*) (struct bp_location *))
35b1e5cc 854 tcomplain);
1e4d1764
YQ
855 de_fault (to_can_download_tracepoint,
856 (int (*) (void))
857 return_zero);
35b1e5cc
SS
858 de_fault (to_download_trace_state_variable,
859 (void (*) (struct trace_state_variable *))
860 tcomplain);
d248b706
KY
861 de_fault (to_enable_tracepoint,
862 (void (*) (struct bp_location *))
863 tcomplain);
864 de_fault (to_disable_tracepoint,
865 (void (*) (struct bp_location *))
866 tcomplain);
35b1e5cc
SS
867 de_fault (to_trace_set_readonly_regions,
868 (void (*) (void))
869 tcomplain);
870 de_fault (to_trace_start,
871 (void (*) (void))
872 tcomplain);
873 de_fault (to_get_trace_status,
00bf0b85 874 (int (*) (struct trace_status *))
35b1e5cc
SS
875 return_minus_one);
876 de_fault (to_trace_stop,
877 (void (*) (void))
878 tcomplain);
879 de_fault (to_trace_find,
880 (int (*) (enum trace_find_type, int, ULONGEST, ULONGEST, int *))
4136fdd2 881 return_minus_one);
35b1e5cc
SS
882 de_fault (to_get_trace_state_variable_value,
883 (int (*) (int, LONGEST *))
884 return_zero);
00bf0b85 885 de_fault (to_save_trace_data,
011aacb0 886 (int (*) (const char *))
00bf0b85
SS
887 tcomplain);
888 de_fault (to_upload_tracepoints,
889 (int (*) (struct uploaded_tp **))
890 return_zero);
891 de_fault (to_upload_trace_state_variables,
892 (int (*) (struct uploaded_tsv **))
893 return_zero);
894 de_fault (to_get_raw_trace_data,
895 (LONGEST (*) (gdb_byte *, ULONGEST, LONGEST))
896 tcomplain);
405f8e94
SS
897 de_fault (to_get_min_fast_tracepoint_insn_len,
898 (int (*) (void))
899 return_minus_one);
35b1e5cc
SS
900 de_fault (to_set_disconnected_tracing,
901 (void (*) (int))
4daf5ac0
SS
902 target_ignore);
903 de_fault (to_set_circular_trace_buffer,
904 (void (*) (int))
905 target_ignore);
711e434b
PM
906 de_fault (to_get_tib_address,
907 (int (*) (ptid_t, CORE_ADDR *))
908 tcomplain);
d914c394
SS
909 de_fault (to_set_permissions,
910 (void (*) (void))
911 target_ignore);
0fb4aa4b
PA
912 de_fault (to_static_tracepoint_marker_at,
913 (int (*) (CORE_ADDR, struct static_tracepoint_marker *))
914 return_zero);
915 de_fault (to_static_tracepoint_markers_by_strid,
916 (VEC(static_tracepoint_marker_p) * (*) (const char *))
917 tcomplain);
b3b9301e
PA
918 de_fault (to_traceframe_info,
919 (struct traceframe_info * (*) (void))
920 tcomplain);
32231432
PA
921 de_fault (to_execution_direction, default_execution_direction);
922
c906108c 923#undef de_fault
c906108c 924
7998dfc3
AC
925 /* Finally, position the target-stack beneath the squashed
926 "current_target". That way code looking for a non-inherited
927 target method can quickly and simply find it. */
928 current_target.beneath = target_stack;
b4b61fdb
DJ
929
930 if (targetdebug)
931 setup_target_debug ();
c906108c
SS
932}
933
934/* Push a new target type into the stack of the existing target accessors,
935 possibly superseding some of the existing accessors.
936
c906108c
SS
937 Rather than allow an empty stack, we always have the dummy target at
938 the bottom stratum, so we can call the function vectors without
939 checking them. */
940
b26a4dcb 941void
fba45db2 942push_target (struct target_ops *t)
c906108c 943{
258b763a 944 struct target_ops **cur;
c906108c
SS
945
946 /* Check magic number. If wrong, it probably means someone changed
947 the struct definition, but not all the places that initialize one. */
948 if (t->to_magic != OPS_MAGIC)
949 {
c5aa993b
JM
950 fprintf_unfiltered (gdb_stderr,
951 "Magic number of %s target struct wrong\n",
952 t->to_shortname);
3e43a32a
MS
953 internal_error (__FILE__, __LINE__,
954 _("failed internal consistency check"));
c906108c
SS
955 }
956
258b763a
AC
957 /* Find the proper stratum to install this target in. */
958 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
c906108c 959 {
258b763a 960 if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
c906108c
SS
961 break;
962 }
963
258b763a 964 /* If there's already targets at this stratum, remove them. */
88c231eb 965 /* FIXME: cagney/2003-10-15: I think this should be popping all
258b763a
AC
966 targets to CUR, and not just those at this stratum level. */
967 while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
968 {
969 /* There's already something at this stratum level. Close it,
970 and un-hook it from the stack. */
971 struct target_ops *tmp = (*cur);
5d502164 972
258b763a
AC
973 (*cur) = (*cur)->beneath;
974 tmp->beneath = NULL;
f1c07ab0 975 target_close (tmp, 0);
258b763a 976 }
c906108c
SS
977
978 /* We have removed all targets in our stratum, now add the new one. */
258b763a
AC
979 t->beneath = (*cur);
980 (*cur) = t;
c906108c
SS
981
982 update_current_target ();
c906108c
SS
983}
984
2bc416ba 985/* Remove a target_ops vector from the stack, wherever it may be.
c906108c
SS
986 Return how many times it was removed (0 or 1). */
987
988int
fba45db2 989unpush_target (struct target_ops *t)
c906108c 990{
258b763a
AC
991 struct target_ops **cur;
992 struct target_ops *tmp;
c906108c 993
c8d104ad
PA
994 if (t->to_stratum == dummy_stratum)
995 internal_error (__FILE__, __LINE__,
9b20d036 996 _("Attempt to unpush the dummy target"));
c8d104ad 997
c906108c 998 /* Look for the specified target. Note that we assume that a target
c378eb4e 999 can only occur once in the target stack. */
c906108c 1000
258b763a
AC
1001 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1002 {
1003 if ((*cur) == t)
1004 break;
1005 }
c906108c 1006
258b763a 1007 if ((*cur) == NULL)
c378eb4e 1008 return 0; /* Didn't find target_ops, quit now. */
c906108c 1009
5269965e
AC
1010 /* NOTE: cagney/2003-12-06: In '94 the close call was made
1011 unconditional by moving it to before the above check that the
1012 target was in the target stack (something about "Change the way
1013 pushing and popping of targets work to support target overlays
1014 and inheritance"). This doesn't make much sense - only open
1015 targets should be closed. */
1016 target_close (t, 0);
1017
c378eb4e 1018 /* Unchain the target. */
258b763a
AC
1019 tmp = (*cur);
1020 (*cur) = (*cur)->beneath;
1021 tmp->beneath = NULL;
c906108c
SS
1022
1023 update_current_target ();
c906108c
SS
1024
1025 return 1;
1026}
1027
1028void
fba45db2 1029pop_target (void)
c906108c 1030{
c378eb4e 1031 target_close (target_stack, 0); /* Let it clean up. */
258b763a 1032 if (unpush_target (target_stack) == 1)
c906108c
SS
1033 return;
1034
c5aa993b
JM
1035 fprintf_unfiltered (gdb_stderr,
1036 "pop_target couldn't find target %s\n",
1037 current_target.to_shortname);
5d502164
MS
1038 internal_error (__FILE__, __LINE__,
1039 _("failed internal consistency check"));
c906108c
SS
1040}
1041
aa76d38d 1042void
87ab71f0 1043pop_all_targets_above (enum strata above_stratum, int quitting)
aa76d38d 1044{
87ab71f0 1045 while ((int) (current_target.to_stratum) > (int) above_stratum)
aa76d38d 1046 {
b52323fa 1047 target_close (target_stack, quitting);
aa76d38d
PA
1048 if (!unpush_target (target_stack))
1049 {
1050 fprintf_unfiltered (gdb_stderr,
1051 "pop_all_targets couldn't find target %s\n",
b52323fa 1052 target_stack->to_shortname);
aa76d38d
PA
1053 internal_error (__FILE__, __LINE__,
1054 _("failed internal consistency check"));
1055 break;
1056 }
1057 }
1058}
1059
87ab71f0
PA
1060void
1061pop_all_targets (int quitting)
1062{
1063 pop_all_targets_above (dummy_stratum, quitting);
1064}
1065
c0edd9ed
JK
1066/* Return 1 if T is now pushed in the target stack. Return 0 otherwise. */
1067
1068int
1069target_is_pushed (struct target_ops *t)
1070{
1071 struct target_ops **cur;
1072
1073 /* Check magic number. If wrong, it probably means someone changed
1074 the struct definition, but not all the places that initialize one. */
1075 if (t->to_magic != OPS_MAGIC)
1076 {
1077 fprintf_unfiltered (gdb_stderr,
1078 "Magic number of %s target struct wrong\n",
1079 t->to_shortname);
3e43a32a
MS
1080 internal_error (__FILE__, __LINE__,
1081 _("failed internal consistency check"));
c0edd9ed
JK
1082 }
1083
1084 for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
1085 if (*cur == t)
1086 return 1;
1087
1088 return 0;
1089}
1090
72f5cf0e 1091/* Using the objfile specified in OBJFILE, find the address for the
9e35dae4
DJ
1092 current thread's thread-local storage with offset OFFSET. */
1093CORE_ADDR
1094target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
1095{
1096 volatile CORE_ADDR addr = 0;
117de6a9
PA
1097 struct target_ops *target;
1098
1099 for (target = current_target.beneath;
1100 target != NULL;
1101 target = target->beneath)
1102 {
1103 if (target->to_get_thread_local_address != NULL)
1104 break;
1105 }
9e35dae4 1106
117de6a9 1107 if (target != NULL
1cf3db46 1108 && gdbarch_fetch_tls_load_module_address_p (target_gdbarch))
9e35dae4
DJ
1109 {
1110 ptid_t ptid = inferior_ptid;
1111 volatile struct gdb_exception ex;
1112
1113 TRY_CATCH (ex, RETURN_MASK_ALL)
1114 {
1115 CORE_ADDR lm_addr;
1116
1117 /* Fetch the load module address for this objfile. */
1cf3db46 1118 lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch,
9e35dae4
DJ
1119 objfile);
1120 /* If it's 0, throw the appropriate exception. */
1121 if (lm_addr == 0)
1122 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1123 _("TLS load module not found"));
1124
3e43a32a
MS
1125 addr = target->to_get_thread_local_address (target, ptid,
1126 lm_addr, offset);
9e35dae4
DJ
1127 }
1128 /* If an error occurred, print TLS related messages here. Otherwise,
1129 throw the error to some higher catcher. */
1130 if (ex.reason < 0)
1131 {
1132 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1133
1134 switch (ex.error)
1135 {
1136 case TLS_NO_LIBRARY_SUPPORT_ERROR:
3e43a32a
MS
1137 error (_("Cannot find thread-local variables "
1138 "in this thread library."));
9e35dae4
DJ
1139 break;
1140 case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
1141 if (objfile_is_library)
1142 error (_("Cannot find shared library `%s' in dynamic"
1143 " linker's load module list"), objfile->name);
1144 else
1145 error (_("Cannot find executable file `%s' in dynamic"
1146 " linker's load module list"), objfile->name);
1147 break;
1148 case TLS_NOT_ALLOCATED_YET_ERROR:
1149 if (objfile_is_library)
1150 error (_("The inferior has not yet allocated storage for"
1151 " thread-local variables in\n"
1152 "the shared library `%s'\n"
1153 "for %s"),
1154 objfile->name, target_pid_to_str (ptid));
1155 else
1156 error (_("The inferior has not yet allocated storage for"
1157 " thread-local variables in\n"
1158 "the executable `%s'\n"
1159 "for %s"),
1160 objfile->name, target_pid_to_str (ptid));
1161 break;
1162 case TLS_GENERIC_ERROR:
1163 if (objfile_is_library)
1164 error (_("Cannot find thread-local storage for %s, "
1165 "shared library %s:\n%s"),
1166 target_pid_to_str (ptid),
1167 objfile->name, ex.message);
1168 else
1169 error (_("Cannot find thread-local storage for %s, "
1170 "executable file %s:\n%s"),
1171 target_pid_to_str (ptid),
1172 objfile->name, ex.message);
1173 break;
1174 default:
1175 throw_exception (ex);
1176 break;
1177 }
1178 }
1179 }
1180 /* It wouldn't be wrong here to try a gdbarch method, too; finding
1181 TLS is an ABI-specific thing. But we don't do that yet. */
1182 else
1183 error (_("Cannot find thread-local variables on this target"));
1184
1185 return addr;
1186}
1187
c906108c
SS
1188#undef MIN
1189#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
1190
1191/* target_read_string -- read a null terminated string, up to LEN bytes,
1192 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
1193 Set *STRING to a pointer to malloc'd memory containing the data; the caller
1194 is responsible for freeing it. Return the number of bytes successfully
1195 read. */
1196
1197int
fba45db2 1198target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
c906108c
SS
1199{
1200 int tlen, origlen, offset, i;
1b0ba102 1201 gdb_byte buf[4];
c906108c
SS
1202 int errcode = 0;
1203 char *buffer;
1204 int buffer_allocated;
1205 char *bufptr;
1206 unsigned int nbytes_read = 0;
1207
6217bf3e
MS
1208 gdb_assert (string);
1209
c906108c
SS
1210 /* Small for testing. */
1211 buffer_allocated = 4;
1212 buffer = xmalloc (buffer_allocated);
1213 bufptr = buffer;
1214
1215 origlen = len;
1216
1217 while (len > 0)
1218 {
1219 tlen = MIN (len, 4 - (memaddr & 3));
1220 offset = memaddr & 3;
1221
1b0ba102 1222 errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
c906108c
SS
1223 if (errcode != 0)
1224 {
1225 /* The transfer request might have crossed the boundary to an
c378eb4e 1226 unallocated region of memory. Retry the transfer, requesting
c906108c
SS
1227 a single byte. */
1228 tlen = 1;
1229 offset = 0;
b8eb5af0 1230 errcode = target_read_memory (memaddr, buf, 1);
c906108c
SS
1231 if (errcode != 0)
1232 goto done;
1233 }
1234
1235 if (bufptr - buffer + tlen > buffer_allocated)
1236 {
1237 unsigned int bytes;
5d502164 1238
c906108c
SS
1239 bytes = bufptr - buffer;
1240 buffer_allocated *= 2;
1241 buffer = xrealloc (buffer, buffer_allocated);
1242 bufptr = buffer + bytes;
1243 }
1244
1245 for (i = 0; i < tlen; i++)
1246 {
1247 *bufptr++ = buf[i + offset];
1248 if (buf[i + offset] == '\000')
1249 {
1250 nbytes_read += i + 1;
1251 goto done;
1252 }
1253 }
1254
1255 memaddr += tlen;
1256 len -= tlen;
1257 nbytes_read += tlen;
1258 }
c5aa993b 1259done:
6217bf3e 1260 *string = buffer;
c906108c
SS
1261 if (errnop != NULL)
1262 *errnop = errcode;
c906108c
SS
1263 return nbytes_read;
1264}
1265
07b82ea5
PA
1266struct target_section_table *
1267target_get_section_table (struct target_ops *target)
1268{
1269 struct target_ops *t;
1270
1271 if (targetdebug)
1272 fprintf_unfiltered (gdb_stdlog, "target_get_section_table ()\n");
1273
1274 for (t = target; t != NULL; t = t->beneath)
1275 if (t->to_get_section_table != NULL)
1276 return (*t->to_get_section_table) (t);
1277
1278 return NULL;
1279}
1280
8db32d44 1281/* Find a section containing ADDR. */
07b82ea5 1282
0542c86d 1283struct target_section *
8db32d44
AC
1284target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
1285{
07b82ea5 1286 struct target_section_table *table = target_get_section_table (target);
0542c86d 1287 struct target_section *secp;
07b82ea5
PA
1288
1289 if (table == NULL)
1290 return NULL;
1291
1292 for (secp = table->sections; secp < table->sections_end; secp++)
8db32d44
AC
1293 {
1294 if (addr >= secp->addr && addr < secp->endaddr)
1295 return secp;
1296 }
1297 return NULL;
1298}
1299
e6e4e701
PA
1300/* Read memory from the live target, even if currently inspecting a
1301 traceframe. The return is the same as that of target_read. */
1302
1303static LONGEST
1304target_read_live_memory (enum target_object object,
1305 ULONGEST memaddr, gdb_byte *myaddr, LONGEST len)
1306{
1307 int ret;
1308 struct cleanup *cleanup;
1309
1310 /* Switch momentarily out of tfind mode so to access live memory.
1311 Note that this must not clear global state, such as the frame
1312 cache, which must still remain valid for the previous traceframe.
1313 We may be _building_ the frame cache at this point. */
1314 cleanup = make_cleanup_restore_traceframe_number ();
1315 set_traceframe_number (-1);
1316
1317 ret = target_read (current_target.beneath, object, NULL,
1318 myaddr, memaddr, len);
1319
1320 do_cleanups (cleanup);
1321 return ret;
1322}
1323
1324/* Using the set of read-only target sections of OPS, read live
1325 read-only memory. Note that the actual reads start from the
5657161f
PA
1326 top-most target again.
1327
1328 For interface/parameters/return description see target.h,
1329 to_xfer_partial. */
e6e4e701
PA
1330
1331static LONGEST
1332memory_xfer_live_readonly_partial (struct target_ops *ops,
1333 enum target_object object,
1334 gdb_byte *readbuf, ULONGEST memaddr,
1335 LONGEST len)
1336{
1337 struct target_section *secp;
1338 struct target_section_table *table;
1339
1340 secp = target_section_by_addr (ops, memaddr);
1341 if (secp != NULL
1342 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1343 & SEC_READONLY))
1344 {
1345 struct target_section *p;
1346 ULONGEST memend = memaddr + len;
1347
1348 table = target_get_section_table (ops);
1349
1350 for (p = table->sections; p < table->sections_end; p++)
1351 {
1352 if (memaddr >= p->addr)
1353 {
1354 if (memend <= p->endaddr)
1355 {
1356 /* Entire transfer is within this section. */
1357 return target_read_live_memory (object, memaddr,
1358 readbuf, len);
1359 }
1360 else if (memaddr >= p->endaddr)
1361 {
1362 /* This section ends before the transfer starts. */
1363 continue;
1364 }
1365 else
1366 {
1367 /* This section overlaps the transfer. Just do half. */
1368 len = p->endaddr - memaddr;
1369 return target_read_live_memory (object, memaddr,
1370 readbuf, len);
1371 }
1372 }
1373 }
1374 }
1375
1376 return 0;
1377}
1378
7f79c47e
DE
1379/* Perform a partial memory transfer.
1380 For docs see target.h, to_xfer_partial. */
cf7a04e8
DJ
1381
1382static LONGEST
4e5d721f
DE
1383memory_xfer_partial (struct target_ops *ops, enum target_object object,
1384 void *readbuf, const void *writebuf, ULONGEST memaddr,
1385 LONGEST len)
0779438d 1386{
cf7a04e8
DJ
1387 LONGEST res;
1388 int reg_len;
1389 struct mem_region *region;
4e5d721f 1390 struct inferior *inf;
cf7a04e8
DJ
1391
1392 /* Zero length requests are ok and require no work. */
1393 if (len == 0)
1394 return 0;
1395
07b82ea5
PA
1396 /* For accesses to unmapped overlay sections, read directly from
1397 files. Must do this first, as MEMADDR may need adjustment. */
1398 if (readbuf != NULL && overlay_debugging)
1399 {
1400 struct obj_section *section = find_pc_overlay (memaddr);
5d502164 1401
07b82ea5
PA
1402 if (pc_in_unmapped_range (memaddr, section))
1403 {
1404 struct target_section_table *table
1405 = target_get_section_table (ops);
1406 const char *section_name = section->the_bfd_section->name;
5d502164 1407
07b82ea5
PA
1408 memaddr = overlay_mapped_address (memaddr, section);
1409 return section_table_xfer_memory_partial (readbuf, writebuf,
1410 memaddr, len,
1411 table->sections,
1412 table->sections_end,
1413 section_name);
1414 }
1415 }
1416
1417 /* Try the executable files, if "trust-readonly-sections" is set. */
cf7a04e8
DJ
1418 if (readbuf != NULL && trust_readonly)
1419 {
0542c86d 1420 struct target_section *secp;
07b82ea5 1421 struct target_section_table *table;
cf7a04e8
DJ
1422
1423 secp = target_section_by_addr (ops, memaddr);
1424 if (secp != NULL
1425 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1426 & SEC_READONLY))
07b82ea5
PA
1427 {
1428 table = target_get_section_table (ops);
1429 return section_table_xfer_memory_partial (readbuf, writebuf,
1430 memaddr, len,
1431 table->sections,
1432 table->sections_end,
1433 NULL);
1434 }
98646950
UW
1435 }
1436
e6e4e701
PA
1437 /* If reading unavailable memory in the context of traceframes, and
1438 this address falls within a read-only section, fallback to
1439 reading from live memory. */
1440 if (readbuf != NULL && get_traceframe_number () != -1)
1441 {
1442 VEC(mem_range_s) *available;
1443
1444 /* If we fail to get the set of available memory, then the
1445 target does not support querying traceframe info, and so we
1446 attempt reading from the traceframe anyway (assuming the
1447 target implements the old QTro packet then). */
1448 if (traceframe_available_memory (&available, memaddr, len))
1449 {
1450 struct cleanup *old_chain;
1451
1452 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
1453
1454 if (VEC_empty (mem_range_s, available)
1455 || VEC_index (mem_range_s, available, 0)->start != memaddr)
1456 {
1457 /* Don't read into the traceframe's available
1458 memory. */
1459 if (!VEC_empty (mem_range_s, available))
1460 {
1461 LONGEST oldlen = len;
1462
1463 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
1464 gdb_assert (len <= oldlen);
1465 }
1466
1467 do_cleanups (old_chain);
1468
1469 /* This goes through the topmost target again. */
1470 res = memory_xfer_live_readonly_partial (ops, object,
1471 readbuf, memaddr, len);
1472 if (res > 0)
1473 return res;
1474
1475 /* No use trying further, we know some memory starting
1476 at MEMADDR isn't available. */
1477 return -1;
1478 }
1479
1480 /* Don't try to read more than how much is available, in
1481 case the target implements the deprecated QTro packet to
1482 cater for older GDBs (the target's knowledge of read-only
1483 sections may be outdated by now). */
1484 len = VEC_index (mem_range_s, available, 0)->length;
1485
1486 do_cleanups (old_chain);
1487 }
1488 }
1489
cf7a04e8
DJ
1490 /* Try GDB's internal data cache. */
1491 region = lookup_mem_region (memaddr);
4b5752d0
VP
1492 /* region->hi == 0 means there's no upper bound. */
1493 if (memaddr + len < region->hi || region->hi == 0)
cf7a04e8
DJ
1494 reg_len = len;
1495 else
1496 reg_len = region->hi - memaddr;
1497
1498 switch (region->attrib.mode)
1499 {
1500 case MEM_RO:
1501 if (writebuf != NULL)
1502 return -1;
1503 break;
1504
1505 case MEM_WO:
1506 if (readbuf != NULL)
1507 return -1;
1508 break;
a76d924d
DJ
1509
1510 case MEM_FLASH:
1511 /* We only support writing to flash during "load" for now. */
1512 if (writebuf != NULL)
1513 error (_("Writing to flash memory forbidden in this context"));
1514 break;
4b5752d0
VP
1515
1516 case MEM_NONE:
1517 return -1;
cf7a04e8
DJ
1518 }
1519
6c95b8df
PA
1520 if (!ptid_equal (inferior_ptid, null_ptid))
1521 inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
1522 else
1523 inf = NULL;
4e5d721f
DE
1524
1525 if (inf != NULL
2f4d8875
PA
1526 /* The dcache reads whole cache lines; that doesn't play well
1527 with reading from a trace buffer, because reading outside of
1528 the collected memory range fails. */
1529 && get_traceframe_number () == -1
4e5d721f
DE
1530 && (region->attrib.cache
1531 || (stack_cache_enabled_p && object == TARGET_OBJECT_STACK_MEMORY)))
cf7a04e8 1532 {
cf7a04e8 1533 if (readbuf != NULL)
25f122dc 1534 res = dcache_xfer_memory (ops, target_dcache, memaddr, readbuf,
cf7a04e8
DJ
1535 reg_len, 0);
1536 else
1537 /* FIXME drow/2006-08-09: If we're going to preserve const
1538 correctness dcache_xfer_memory should take readbuf and
1539 writebuf. */
25f122dc 1540 res = dcache_xfer_memory (ops, target_dcache, memaddr,
cf7a04e8
DJ
1541 (void *) writebuf,
1542 reg_len, 1);
1543 if (res <= 0)
1544 return -1;
1545 else
8defab1a
DJ
1546 {
1547 if (readbuf && !show_memory_breakpoints)
1548 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1549 return res;
1550 }
cf7a04e8
DJ
1551 }
1552
1553 /* If none of those methods found the memory we wanted, fall back
1554 to a target partial transfer. Normally a single call to
1555 to_xfer_partial is enough; if it doesn't recognize an object
1556 it will call the to_xfer_partial of the next target down.
1557 But for memory this won't do. Memory is the only target
1558 object which can be read from more than one valid target.
1559 A core file, for instance, could have some of memory but
1560 delegate other bits to the target below it. So, we must
1561 manually try all targets. */
1562
1563 do
1564 {
1565 res = ops->to_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
4b5752d0 1566 readbuf, writebuf, memaddr, reg_len);
cf7a04e8 1567 if (res > 0)
8defab1a 1568 break;
cf7a04e8 1569
5ad3a4ca
DJ
1570 /* We want to continue past core files to executables, but not
1571 past a running target's memory. */
c35b1492 1572 if (ops->to_has_all_memory (ops))
8defab1a 1573 break;
5ad3a4ca 1574
cf7a04e8
DJ
1575 ops = ops->beneath;
1576 }
1577 while (ops != NULL);
1578
e8c9e0a1 1579 if (res > 0 && readbuf != NULL && !show_memory_breakpoints)
8defab1a
DJ
1580 breakpoint_restore_shadows (readbuf, memaddr, reg_len);
1581
41dcd03f
DE
1582 /* Make sure the cache gets updated no matter what - if we are writing
1583 to the stack. Even if this write is not tagged as such, we still need
1584 to update the cache. */
1585
1586 if (res > 0
1587 && inf != NULL
1588 && writebuf != NULL
1589 && !region->attrib.cache
1590 && stack_cache_enabled_p
1591 && object != TARGET_OBJECT_STACK_MEMORY)
1592 {
7d4f32d3 1593 dcache_update (target_dcache, memaddr, (void *) writebuf, res);
41dcd03f
DE
1594 }
1595
cf7a04e8
DJ
1596 /* If we still haven't got anything, return the last error. We
1597 give up. */
1598 return res;
0779438d
AC
1599}
1600
8defab1a
DJ
1601static void
1602restore_show_memory_breakpoints (void *arg)
1603{
1604 show_memory_breakpoints = (uintptr_t) arg;
1605}
1606
1607struct cleanup *
1608make_show_memory_breakpoints_cleanup (int show)
1609{
1610 int current = show_memory_breakpoints;
8defab1a 1611
5d502164 1612 show_memory_breakpoints = show;
8defab1a
DJ
1613 return make_cleanup (restore_show_memory_breakpoints,
1614 (void *) (uintptr_t) current);
1615}
1616
7f79c47e
DE
1617/* For docs see target.h, to_xfer_partial. */
1618
27394598
AC
1619static LONGEST
1620target_xfer_partial (struct target_ops *ops,
1621 enum target_object object, const char *annex,
1622 void *readbuf, const void *writebuf,
1623 ULONGEST offset, LONGEST len)
1624{
1625 LONGEST retval;
1626
1627 gdb_assert (ops->to_xfer_partial != NULL);
cf7a04e8 1628
d914c394
SS
1629 if (writebuf && !may_write_memory)
1630 error (_("Writing to memory is not allowed (addr %s, len %s)"),
1631 core_addr_to_string_nz (offset), plongest (len));
1632
cf7a04e8
DJ
1633 /* If this is a memory transfer, let the memory-specific code
1634 have a look at it instead. Memory transfers are more
1635 complicated. */
4e5d721f
DE
1636 if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY)
1637 retval = memory_xfer_partial (ops, object, readbuf,
1638 writebuf, offset, len);
cf7a04e8
DJ
1639 else
1640 {
1641 enum target_object raw_object = object;
1642
1643 /* If this is a raw memory transfer, request the normal
1644 memory object from other layers. */
1645 if (raw_object == TARGET_OBJECT_RAW_MEMORY)
1646 raw_object = TARGET_OBJECT_MEMORY;
1647
1648 retval = ops->to_xfer_partial (ops, raw_object, annex, readbuf,
1649 writebuf, offset, len);
1650 }
1651
27394598
AC
1652 if (targetdebug)
1653 {
1654 const unsigned char *myaddr = NULL;
1655
1656 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
1657 "%s:target_xfer_partial "
1658 "(%d, %s, %s, %s, %s, %s) = %s",
27394598
AC
1659 ops->to_shortname,
1660 (int) object,
1661 (annex ? annex : "(null)"),
53b71562
JB
1662 host_address_to_string (readbuf),
1663 host_address_to_string (writebuf),
0b1553bc
UW
1664 core_addr_to_string_nz (offset),
1665 plongest (len), plongest (retval));
27394598
AC
1666
1667 if (readbuf)
1668 myaddr = readbuf;
1669 if (writebuf)
1670 myaddr = writebuf;
1671 if (retval > 0 && myaddr != NULL)
1672 {
1673 int i;
2bc416ba 1674
27394598
AC
1675 fputs_unfiltered (", bytes =", gdb_stdlog);
1676 for (i = 0; i < retval; i++)
1677 {
53b71562 1678 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
27394598
AC
1679 {
1680 if (targetdebug < 2 && i > 0)
1681 {
1682 fprintf_unfiltered (gdb_stdlog, " ...");
1683 break;
1684 }
1685 fprintf_unfiltered (gdb_stdlog, "\n");
1686 }
2bc416ba 1687
27394598
AC
1688 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
1689 }
1690 }
2bc416ba 1691
27394598
AC
1692 fputc_unfiltered ('\n', gdb_stdlog);
1693 }
1694 return retval;
1695}
1696
c906108c
SS
1697/* Read LEN bytes of target memory at address MEMADDR, placing the results in
1698 GDB's memory at MYADDR. Returns either 0 for success or an errno value
1699 if any error occurs.
1700
1701 If an error occurs, no guarantee is made about the contents of the data at
1702 MYADDR. In particular, the caller should not depend upon partial reads
1703 filling the buffer with good data. There is no way for the caller to know
1704 how much good data might have been transfered anyway. Callers that can
cf7a04e8 1705 deal with partial reads should call target_read (which will retry until
c378eb4e 1706 it makes no progress, and then return how much was transferred). */
c906108c
SS
1707
1708int
fc1a4b47 1709target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 1710{
c35b1492
PA
1711 /* Dispatch to the topmost target, not the flattened current_target.
1712 Memory accesses check target->to_has_(all_)memory, and the
1713 flattened target doesn't inherit those. */
1714 if (target_read (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1715 myaddr, memaddr, len) == len)
1716 return 0;
0779438d 1717 else
cf7a04e8 1718 return EIO;
c906108c
SS
1719}
1720
4e5d721f
DE
1721/* Like target_read_memory, but specify explicitly that this is a read from
1722 the target's stack. This may trigger different cache behavior. */
1723
1724int
1725target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
1726{
1727 /* Dispatch to the topmost target, not the flattened current_target.
1728 Memory accesses check target->to_has_(all_)memory, and the
1729 flattened target doesn't inherit those. */
1730
1731 if (target_read (current_target.beneath, TARGET_OBJECT_STACK_MEMORY, NULL,
1732 myaddr, memaddr, len) == len)
1733 return 0;
1734 else
1735 return EIO;
1736}
1737
7f79c47e
DE
1738/* Write LEN bytes from MYADDR to target memory at address MEMADDR.
1739 Returns either 0 for success or an errno value if any error occurs.
1740 If an error occurs, no guarantee is made about how much data got written.
1741 Callers that can deal with partial writes should call target_write. */
1742
c906108c 1743int
fc1a4b47 1744target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
c906108c 1745{
c35b1492
PA
1746 /* Dispatch to the topmost target, not the flattened current_target.
1747 Memory accesses check target->to_has_(all_)memory, and the
1748 flattened target doesn't inherit those. */
1749 if (target_write (current_target.beneath, TARGET_OBJECT_MEMORY, NULL,
cf7a04e8
DJ
1750 myaddr, memaddr, len) == len)
1751 return 0;
0779438d 1752 else
cf7a04e8 1753 return EIO;
c906108c 1754}
c5aa993b 1755
fd79ecee
DJ
1756/* Fetch the target's memory map. */
1757
1758VEC(mem_region_s) *
1759target_memory_map (void)
1760{
1761 VEC(mem_region_s) *result;
1762 struct mem_region *last_one, *this_one;
1763 int ix;
1764 struct target_ops *t;
1765
1766 if (targetdebug)
1767 fprintf_unfiltered (gdb_stdlog, "target_memory_map ()\n");
1768
1769 for (t = current_target.beneath; t != NULL; t = t->beneath)
1770 if (t->to_memory_map != NULL)
1771 break;
1772
1773 if (t == NULL)
1774 return NULL;
1775
1776 result = t->to_memory_map (t);
1777 if (result == NULL)
1778 return NULL;
1779
1780 qsort (VEC_address (mem_region_s, result),
1781 VEC_length (mem_region_s, result),
1782 sizeof (struct mem_region), mem_region_cmp);
1783
1784 /* Check that regions do not overlap. Simultaneously assign
1785 a numbering for the "mem" commands to use to refer to
1786 each region. */
1787 last_one = NULL;
1788 for (ix = 0; VEC_iterate (mem_region_s, result, ix, this_one); ix++)
1789 {
1790 this_one->number = ix;
1791
1792 if (last_one && last_one->hi > this_one->lo)
1793 {
1794 warning (_("Overlapping regions in memory map: ignoring"));
1795 VEC_free (mem_region_s, result);
1796 return NULL;
1797 }
1798 last_one = this_one;
1799 }
1800
1801 return result;
1802}
1803
a76d924d
DJ
1804void
1805target_flash_erase (ULONGEST address, LONGEST length)
1806{
1807 struct target_ops *t;
1808
1809 for (t = current_target.beneath; t != NULL; t = t->beneath)
1810 if (t->to_flash_erase != NULL)
5d502164
MS
1811 {
1812 if (targetdebug)
1813 fprintf_unfiltered (gdb_stdlog, "target_flash_erase (%s, %s)\n",
1814 hex_string (address), phex (length, 0));
1815 t->to_flash_erase (t, address, length);
1816 return;
1817 }
a76d924d
DJ
1818
1819 tcomplain ();
1820}
1821
1822void
1823target_flash_done (void)
1824{
1825 struct target_ops *t;
1826
1827 for (t = current_target.beneath; t != NULL; t = t->beneath)
1828 if (t->to_flash_done != NULL)
5d502164
MS
1829 {
1830 if (targetdebug)
1831 fprintf_unfiltered (gdb_stdlog, "target_flash_done\n");
1832 t->to_flash_done (t);
1833 return;
1834 }
a76d924d
DJ
1835
1836 tcomplain ();
1837}
1838
920d2a44
AC
1839static void
1840show_trust_readonly (struct ui_file *file, int from_tty,
1841 struct cmd_list_element *c, const char *value)
1842{
3e43a32a
MS
1843 fprintf_filtered (file,
1844 _("Mode for reading from readonly sections is %s.\n"),
920d2a44
AC
1845 value);
1846}
3a11626d 1847
1e3ff5ad
AC
1848/* More generic transfers. */
1849
0088c768 1850static LONGEST
8aa91c1e 1851default_xfer_partial (struct target_ops *ops, enum target_object object,
2bc416ba 1852 const char *annex, gdb_byte *readbuf,
1b0ba102 1853 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
0088c768
AC
1854{
1855 if (object == TARGET_OBJECT_MEMORY
c8e73a31
AC
1856 && ops->deprecated_xfer_memory != NULL)
1857 /* If available, fall back to the target's
1858 "deprecated_xfer_memory" method. */
0088c768 1859 {
4b8a223f 1860 int xfered = -1;
5d502164 1861
0088c768 1862 errno = 0;
4b8a223f
AC
1863 if (writebuf != NULL)
1864 {
1865 void *buffer = xmalloc (len);
1866 struct cleanup *cleanup = make_cleanup (xfree, buffer);
5d502164 1867
4b8a223f 1868 memcpy (buffer, writebuf, len);
c8e73a31
AC
1869 xfered = ops->deprecated_xfer_memory (offset, buffer, len,
1870 1/*write*/, NULL, ops);
4b8a223f
AC
1871 do_cleanups (cleanup);
1872 }
1873 if (readbuf != NULL)
244e85c8
MS
1874 xfered = ops->deprecated_xfer_memory (offset, readbuf, len,
1875 0/*read*/, NULL, ops);
0088c768
AC
1876 if (xfered > 0)
1877 return xfered;
1878 else if (xfered == 0 && errno == 0)
c8e73a31
AC
1879 /* "deprecated_xfer_memory" uses 0, cross checked against
1880 ERRNO as one indication of an error. */
0088c768
AC
1881 return 0;
1882 else
1883 return -1;
1884 }
1885 else if (ops->beneath != NULL)
cf7a04e8
DJ
1886 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1887 readbuf, writebuf, offset, len);
1888 else
1889 return -1;
1890}
1891
1892/* The xfer_partial handler for the topmost target. Unlike the default,
1893 it does not need to handle memory specially; it just passes all
1894 requests down the stack. */
1895
1896static LONGEST
1897current_xfer_partial (struct target_ops *ops, enum target_object object,
1898 const char *annex, gdb_byte *readbuf,
1899 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1900{
1901 if (ops->beneath != NULL)
1902 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1903 readbuf, writebuf, offset, len);
0088c768
AC
1904 else
1905 return -1;
1906}
1907
7f79c47e 1908/* Target vector read/write partial wrapper functions. */
0088c768 1909
13547ab6 1910static LONGEST
1e3ff5ad
AC
1911target_read_partial (struct target_ops *ops,
1912 enum target_object object,
1b0ba102 1913 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1914 ULONGEST offset, LONGEST len)
1915{
27394598 1916 return target_xfer_partial (ops, object, annex, buf, NULL, offset, len);
1e3ff5ad
AC
1917}
1918
13547ab6 1919static LONGEST
1e3ff5ad
AC
1920target_write_partial (struct target_ops *ops,
1921 enum target_object object,
1b0ba102 1922 const char *annex, const gdb_byte *buf,
1e3ff5ad
AC
1923 ULONGEST offset, LONGEST len)
1924{
27394598 1925 return target_xfer_partial (ops, object, annex, NULL, buf, offset, len);
1e3ff5ad
AC
1926}
1927
1928/* Wrappers to perform the full transfer. */
7f79c47e
DE
1929
1930/* For docs on target_read see target.h. */
1931
1e3ff5ad
AC
1932LONGEST
1933target_read (struct target_ops *ops,
1934 enum target_object object,
1b0ba102 1935 const char *annex, gdb_byte *buf,
1e3ff5ad
AC
1936 ULONGEST offset, LONGEST len)
1937{
1938 LONGEST xfered = 0;
5d502164 1939
1e3ff5ad
AC
1940 while (xfered < len)
1941 {
0088c768 1942 LONGEST xfer = target_read_partial (ops, object, annex,
fc1a4b47 1943 (gdb_byte *) buf + xfered,
0088c768 1944 offset + xfered, len - xfered);
5d502164 1945
1e3ff5ad 1946 /* Call an observer, notifying them of the xfer progress? */
13547ab6
DJ
1947 if (xfer == 0)
1948 return xfered;
1949 if (xfer < 0)
0088c768 1950 return -1;
1e3ff5ad
AC
1951 xfered += xfer;
1952 QUIT;
1953 }
1954 return len;
1955}
1956
f1a507a1
JB
1957/* Assuming that the entire [begin, end) range of memory cannot be
1958 read, try to read whatever subrange is possible to read.
1959
1960 The function returns, in RESULT, either zero or one memory block.
1961 If there's a readable subrange at the beginning, it is completely
1962 read and returned. Any further readable subrange will not be read.
1963 Otherwise, if there's a readable subrange at the end, it will be
1964 completely read and returned. Any readable subranges before it
1965 (obviously, not starting at the beginning), will be ignored. In
1966 other cases -- either no readable subrange, or readable subrange(s)
1967 that is neither at the beginning, or end, nothing is returned.
1968
1969 The purpose of this function is to handle a read across a boundary
1970 of accessible memory in a case when memory map is not available.
1971 The above restrictions are fine for this case, but will give
1972 incorrect results if the memory is 'patchy'. However, supporting
1973 'patchy' memory would require trying to read every single byte,
1974 and it seems unacceptable solution. Explicit memory map is
1975 recommended for this case -- and target_read_memory_robust will
1976 take care of reading multiple ranges then. */
8dedea02
VP
1977
1978static void
3e43a32a
MS
1979read_whatever_is_readable (struct target_ops *ops,
1980 ULONGEST begin, ULONGEST end,
8dedea02 1981 VEC(memory_read_result_s) **result)
d5086790 1982{
f1a507a1 1983 gdb_byte *buf = xmalloc (end - begin);
8dedea02
VP
1984 ULONGEST current_begin = begin;
1985 ULONGEST current_end = end;
1986 int forward;
1987 memory_read_result_s r;
1988
1989 /* If we previously failed to read 1 byte, nothing can be done here. */
1990 if (end - begin <= 1)
13b3fd9b
MS
1991 {
1992 xfree (buf);
1993 return;
1994 }
8dedea02
VP
1995
1996 /* Check that either first or the last byte is readable, and give up
c378eb4e 1997 if not. This heuristic is meant to permit reading accessible memory
8dedea02
VP
1998 at the boundary of accessible region. */
1999 if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2000 buf, begin, 1) == 1)
2001 {
2002 forward = 1;
2003 ++current_begin;
2004 }
2005 else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
2006 buf + (end-begin) - 1, end - 1, 1) == 1)
2007 {
2008 forward = 0;
2009 --current_end;
2010 }
2011 else
2012 {
13b3fd9b 2013 xfree (buf);
8dedea02
VP
2014 return;
2015 }
2016
2017 /* Loop invariant is that the [current_begin, current_end) was previously
2018 found to be not readable as a whole.
2019
2020 Note loop condition -- if the range has 1 byte, we can't divide the range
2021 so there's no point trying further. */
2022 while (current_end - current_begin > 1)
2023 {
2024 ULONGEST first_half_begin, first_half_end;
2025 ULONGEST second_half_begin, second_half_end;
2026 LONGEST xfer;
8dedea02 2027 ULONGEST middle = current_begin + (current_end - current_begin)/2;
f1a507a1 2028
8dedea02
VP
2029 if (forward)
2030 {
2031 first_half_begin = current_begin;
2032 first_half_end = middle;
2033 second_half_begin = middle;
2034 second_half_end = current_end;
2035 }
2036 else
2037 {
2038 first_half_begin = middle;
2039 first_half_end = current_end;
2040 second_half_begin = current_begin;
2041 second_half_end = middle;
2042 }
2043
2044 xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2045 buf + (first_half_begin - begin),
2046 first_half_begin,
2047 first_half_end - first_half_begin);
2048
2049 if (xfer == first_half_end - first_half_begin)
2050 {
c378eb4e 2051 /* This half reads up fine. So, the error must be in the
3e43a32a 2052 other half. */
8dedea02
VP
2053 current_begin = second_half_begin;
2054 current_end = second_half_end;
2055 }
2056 else
2057 {
c378eb4e
MS
2058 /* This half is not readable. Because we've tried one byte, we
2059 know some part of this half if actually redable. Go to the next
8dedea02
VP
2060 iteration to divide again and try to read.
2061
2062 We don't handle the other half, because this function only tries
2063 to read a single readable subrange. */
2064 current_begin = first_half_begin;
2065 current_end = first_half_end;
2066 }
2067 }
2068
2069 if (forward)
2070 {
2071 /* The [begin, current_begin) range has been read. */
2072 r.begin = begin;
2073 r.end = current_begin;
2074 r.data = buf;
2075 }
2076 else
2077 {
2078 /* The [current_end, end) range has been read. */
2079 LONGEST rlen = end - current_end;
f1a507a1 2080
8dedea02
VP
2081 r.data = xmalloc (rlen);
2082 memcpy (r.data, buf + current_end - begin, rlen);
2083 r.begin = current_end;
2084 r.end = end;
2085 xfree (buf);
2086 }
2087 VEC_safe_push(memory_read_result_s, (*result), &r);
2088}
2089
2090void
2091free_memory_read_result_vector (void *x)
2092{
2093 VEC(memory_read_result_s) *v = x;
2094 memory_read_result_s *current;
2095 int ix;
2096
2097 for (ix = 0; VEC_iterate (memory_read_result_s, v, ix, current); ++ix)
2098 {
2099 xfree (current->data);
2100 }
2101 VEC_free (memory_read_result_s, v);
2102}
2103
2104VEC(memory_read_result_s) *
2105read_memory_robust (struct target_ops *ops, ULONGEST offset, LONGEST len)
2106{
2107 VEC(memory_read_result_s) *result = 0;
2108
2109 LONGEST xfered = 0;
d5086790
VP
2110 while (xfered < len)
2111 {
8dedea02
VP
2112 struct mem_region *region = lookup_mem_region (offset + xfered);
2113 LONGEST rlen;
5d502164 2114
8dedea02
VP
2115 /* If there is no explicit region, a fake one should be created. */
2116 gdb_assert (region);
2117
2118 if (region->hi == 0)
2119 rlen = len - xfered;
2120 else
2121 rlen = region->hi - offset;
2122
2123 if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
d5086790 2124 {
c378eb4e 2125 /* Cannot read this region. Note that we can end up here only
8dedea02
VP
2126 if the region is explicitly marked inaccessible, or
2127 'inaccessible-by-default' is in effect. */
2128 xfered += rlen;
2129 }
2130 else
2131 {
2132 LONGEST to_read = min (len - xfered, rlen);
2133 gdb_byte *buffer = (gdb_byte *)xmalloc (to_read);
2134
2135 LONGEST xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2136 (gdb_byte *) buffer,
2137 offset + xfered, to_read);
2138 /* Call an observer, notifying them of the xfer progress? */
d5086790 2139 if (xfer <= 0)
d5086790 2140 {
c378eb4e 2141 /* Got an error reading full chunk. See if maybe we can read
8dedea02
VP
2142 some subrange. */
2143 xfree (buffer);
3e43a32a
MS
2144 read_whatever_is_readable (ops, offset + xfered,
2145 offset + xfered + to_read, &result);
8dedea02 2146 xfered += to_read;
d5086790 2147 }
8dedea02
VP
2148 else
2149 {
2150 struct memory_read_result r;
2151 r.data = buffer;
2152 r.begin = offset + xfered;
2153 r.end = r.begin + xfer;
2154 VEC_safe_push (memory_read_result_s, result, &r);
2155 xfered += xfer;
2156 }
2157 QUIT;
d5086790 2158 }
d5086790 2159 }
8dedea02 2160 return result;
d5086790
VP
2161}
2162
8dedea02 2163
cf7a04e8
DJ
2164/* An alternative to target_write with progress callbacks. */
2165
1e3ff5ad 2166LONGEST
cf7a04e8
DJ
2167target_write_with_progress (struct target_ops *ops,
2168 enum target_object object,
2169 const char *annex, const gdb_byte *buf,
2170 ULONGEST offset, LONGEST len,
2171 void (*progress) (ULONGEST, void *), void *baton)
1e3ff5ad
AC
2172{
2173 LONGEST xfered = 0;
a76d924d
DJ
2174
2175 /* Give the progress callback a chance to set up. */
2176 if (progress)
2177 (*progress) (0, baton);
2178
1e3ff5ad
AC
2179 while (xfered < len)
2180 {
2181 LONGEST xfer = target_write_partial (ops, object, annex,
fc1a4b47 2182 (gdb_byte *) buf + xfered,
1e3ff5ad 2183 offset + xfered, len - xfered);
cf7a04e8 2184
13547ab6
DJ
2185 if (xfer == 0)
2186 return xfered;
2187 if (xfer < 0)
0088c768 2188 return -1;
cf7a04e8
DJ
2189
2190 if (progress)
2191 (*progress) (xfer, baton);
2192
1e3ff5ad
AC
2193 xfered += xfer;
2194 QUIT;
2195 }
2196 return len;
2197}
2198
7f79c47e
DE
2199/* For docs on target_write see target.h. */
2200
cf7a04e8
DJ
2201LONGEST
2202target_write (struct target_ops *ops,
2203 enum target_object object,
2204 const char *annex, const gdb_byte *buf,
2205 ULONGEST offset, LONGEST len)
2206{
2207 return target_write_with_progress (ops, object, annex, buf, offset, len,
2208 NULL, NULL);
2209}
2210
159f81f3
DJ
2211/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
2212 the size of the transferred data. PADDING additional bytes are
2213 available in *BUF_P. This is a helper function for
2214 target_read_alloc; see the declaration of that function for more
2215 information. */
13547ab6 2216
159f81f3
DJ
2217static LONGEST
2218target_read_alloc_1 (struct target_ops *ops, enum target_object object,
2219 const char *annex, gdb_byte **buf_p, int padding)
13547ab6
DJ
2220{
2221 size_t buf_alloc, buf_pos;
2222 gdb_byte *buf;
2223 LONGEST n;
2224
2225 /* This function does not have a length parameter; it reads the
2226 entire OBJECT). Also, it doesn't support objects fetched partly
2227 from one target and partly from another (in a different stratum,
2228 e.g. a core file and an executable). Both reasons make it
2229 unsuitable for reading memory. */
2230 gdb_assert (object != TARGET_OBJECT_MEMORY);
2231
2232 /* Start by reading up to 4K at a time. The target will throttle
2233 this number down if necessary. */
2234 buf_alloc = 4096;
2235 buf = xmalloc (buf_alloc);
2236 buf_pos = 0;
2237 while (1)
2238 {
2239 n = target_read_partial (ops, object, annex, &buf[buf_pos],
159f81f3 2240 buf_pos, buf_alloc - buf_pos - padding);
13547ab6
DJ
2241 if (n < 0)
2242 {
2243 /* An error occurred. */
2244 xfree (buf);
2245 return -1;
2246 }
2247 else if (n == 0)
2248 {
2249 /* Read all there was. */
2250 if (buf_pos == 0)
2251 xfree (buf);
2252 else
2253 *buf_p = buf;
2254 return buf_pos;
2255 }
2256
2257 buf_pos += n;
2258
2259 /* If the buffer is filling up, expand it. */
2260 if (buf_alloc < buf_pos * 2)
2261 {
2262 buf_alloc *= 2;
2263 buf = xrealloc (buf, buf_alloc);
2264 }
2265
2266 QUIT;
2267 }
2268}
2269
159f81f3
DJ
2270/* Read OBJECT/ANNEX using OPS. Store the result in *BUF_P and return
2271 the size of the transferred data. See the declaration in "target.h"
2272 function for more information about the return value. */
2273
2274LONGEST
2275target_read_alloc (struct target_ops *ops, enum target_object object,
2276 const char *annex, gdb_byte **buf_p)
2277{
2278 return target_read_alloc_1 (ops, object, annex, buf_p, 0);
2279}
2280
2281/* Read OBJECT/ANNEX using OPS. The result is NUL-terminated and
2282 returned as a string, allocated using xmalloc. If an error occurs
2283 or the transfer is unsupported, NULL is returned. Empty objects
2284 are returned as allocated but empty strings. A warning is issued
2285 if the result contains any embedded NUL bytes. */
2286
2287char *
2288target_read_stralloc (struct target_ops *ops, enum target_object object,
2289 const char *annex)
2290{
2291 gdb_byte *buffer;
2292 LONGEST transferred;
2293
2294 transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
2295
2296 if (transferred < 0)
2297 return NULL;
2298
2299 if (transferred == 0)
2300 return xstrdup ("");
2301
2302 buffer[transferred] = 0;
2303 if (strlen (buffer) < transferred)
2304 warning (_("target object %d, annex %s, "
2305 "contained unexpected null characters"),
2306 (int) object, annex ? annex : "(none)");
2307
2308 return (char *) buffer;
2309}
2310
b6591e8b
AC
2311/* Memory transfer methods. */
2312
2313void
1b0ba102 2314get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
b6591e8b
AC
2315 LONGEST len)
2316{
07b82ea5
PA
2317 /* This method is used to read from an alternate, non-current
2318 target. This read must bypass the overlay support (as symbols
2319 don't match this target), and GDB's internal cache (wrong cache
2320 for this target). */
2321 if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
b6591e8b
AC
2322 != len)
2323 memory_error (EIO, addr);
2324}
2325
2326ULONGEST
5d502164
MS
2327get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
2328 int len, enum bfd_endian byte_order)
b6591e8b 2329{
f6519ebc 2330 gdb_byte buf[sizeof (ULONGEST)];
b6591e8b
AC
2331
2332 gdb_assert (len <= sizeof (buf));
2333 get_target_memory (ops, addr, buf, len);
e17a4113 2334 return extract_unsigned_integer (buf, len, byte_order);
b6591e8b
AC
2335}
2336
d914c394
SS
2337int
2338target_insert_breakpoint (struct gdbarch *gdbarch,
2339 struct bp_target_info *bp_tgt)
2340{
2341 if (!may_insert_breakpoints)
2342 {
2343 warning (_("May not insert breakpoints"));
2344 return 1;
2345 }
2346
2347 return (*current_target.to_insert_breakpoint) (gdbarch, bp_tgt);
2348}
2349
2350int
2351target_remove_breakpoint (struct gdbarch *gdbarch,
2352 struct bp_target_info *bp_tgt)
2353{
2354 /* This is kind of a weird case to handle, but the permission might
2355 have been changed after breakpoints were inserted - in which case
2356 we should just take the user literally and assume that any
2357 breakpoints should be left in place. */
2358 if (!may_insert_breakpoints)
2359 {
2360 warning (_("May not remove breakpoints"));
2361 return 1;
2362 }
2363
2364 return (*current_target.to_remove_breakpoint) (gdbarch, bp_tgt);
2365}
2366
c906108c 2367static void
fba45db2 2368target_info (char *args, int from_tty)
c906108c
SS
2369{
2370 struct target_ops *t;
c906108c 2371 int has_all_mem = 0;
c5aa993b 2372
c906108c 2373 if (symfile_objfile != NULL)
a3f17187 2374 printf_unfiltered (_("Symbols from \"%s\".\n"), symfile_objfile->name);
c906108c 2375
258b763a 2376 for (t = target_stack; t != NULL; t = t->beneath)
c906108c 2377 {
c35b1492 2378 if (!(*t->to_has_memory) (t))
c906108c
SS
2379 continue;
2380
c5aa993b 2381 if ((int) (t->to_stratum) <= (int) dummy_stratum)
c906108c
SS
2382 continue;
2383 if (has_all_mem)
3e43a32a
MS
2384 printf_unfiltered (_("\tWhile running this, "
2385 "GDB does not access memory from...\n"));
c5aa993b
JM
2386 printf_unfiltered ("%s:\n", t->to_longname);
2387 (t->to_files_info) (t);
c35b1492 2388 has_all_mem = (*t->to_has_all_memory) (t);
c906108c
SS
2389 }
2390}
2391
fd79ecee
DJ
2392/* This function is called before any new inferior is created, e.g.
2393 by running a program, attaching, or connecting to a target.
2394 It cleans up any state from previous invocations which might
2395 change between runs. This is a subset of what target_preopen
2396 resets (things which might change between targets). */
2397
2398void
2399target_pre_inferior (int from_tty)
2400{
c378eb4e 2401 /* Clear out solib state. Otherwise the solib state of the previous
b9db4ced 2402 inferior might have survived and is entirely wrong for the new
c378eb4e 2403 target. This has been observed on GNU/Linux using glibc 2.3. How
b9db4ced
UW
2404 to reproduce:
2405
2406 bash$ ./foo&
2407 [1] 4711
2408 bash$ ./foo&
2409 [1] 4712
2410 bash$ gdb ./foo
2411 [...]
2412 (gdb) attach 4711
2413 (gdb) detach
2414 (gdb) attach 4712
2415 Cannot access memory at address 0xdeadbeef
2416 */
b9db4ced 2417
50c71eaf
PA
2418 /* In some OSs, the shared library list is the same/global/shared
2419 across inferiors. If code is shared between processes, so are
2420 memory regions and features. */
2421 if (!gdbarch_has_global_solist (target_gdbarch))
2422 {
2423 no_shared_libraries (NULL, from_tty);
2424
2425 invalidate_target_mem_regions ();
424163ea 2426
50c71eaf
PA
2427 target_clear_description ();
2428 }
fd79ecee
DJ
2429}
2430
b8fa0bfa
PA
2431/* Callback for iterate_over_inferiors. Gets rid of the given
2432 inferior. */
2433
2434static int
2435dispose_inferior (struct inferior *inf, void *args)
2436{
2437 struct thread_info *thread;
2438
2439 thread = any_thread_of_process (inf->pid);
2440 if (thread)
2441 {
2442 switch_to_thread (thread->ptid);
2443
2444 /* Core inferiors actually should be detached, not killed. */
2445 if (target_has_execution)
2446 target_kill ();
2447 else
2448 target_detach (NULL, 0);
2449 }
2450
2451 return 0;
2452}
2453
c906108c
SS
2454/* This is to be called by the open routine before it does
2455 anything. */
2456
2457void
fba45db2 2458target_preopen (int from_tty)
c906108c 2459{
c5aa993b 2460 dont_repeat ();
c906108c 2461
b8fa0bfa 2462 if (have_inferiors ())
c5aa993b 2463 {
adf40b2e 2464 if (!from_tty
b8fa0bfa
PA
2465 || !have_live_inferiors ()
2466 || query (_("A program is being debugged already. Kill it? ")))
2467 iterate_over_inferiors (dispose_inferior, NULL);
c906108c 2468 else
8a3fe4f8 2469 error (_("Program not killed."));
c906108c
SS
2470 }
2471
2472 /* Calling target_kill may remove the target from the stack. But if
2473 it doesn't (which seems like a win for UDI), remove it now. */
87ab71f0
PA
2474 /* Leave the exec target, though. The user may be switching from a
2475 live process to a core of the same program. */
2476 pop_all_targets_above (file_stratum, 0);
fd79ecee
DJ
2477
2478 target_pre_inferior (from_tty);
c906108c
SS
2479}
2480
2481/* Detach a target after doing deferred register stores. */
2482
2483void
fba45db2 2484target_detach (char *args, int from_tty)
c906108c 2485{
136d6dae
VP
2486 struct target_ops* t;
2487
2567c7d9 2488 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2489 /* Don't remove global breakpoints here. They're removed on
2490 disconnection from the target. */
2491 ;
2492 else
2493 /* If we're in breakpoints-always-inserted mode, have to remove
2494 them before detaching. */
6c95b8df 2495 remove_breakpoints_pid (PIDGET (inferior_ptid));
74960c60 2496
24291992
PA
2497 prepare_for_detach ();
2498
136d6dae
VP
2499 for (t = current_target.beneath; t != NULL; t = t->beneath)
2500 {
2501 if (t->to_detach != NULL)
2502 {
2503 t->to_detach (t, args, from_tty);
947b8855
PA
2504 if (targetdebug)
2505 fprintf_unfiltered (gdb_stdlog, "target_detach (%s, %d)\n",
2506 args, from_tty);
136d6dae
VP
2507 return;
2508 }
2509 }
2510
9b20d036 2511 internal_error (__FILE__, __LINE__, _("could not find a target to detach"));
c906108c
SS
2512}
2513
6ad8ae5c
DJ
2514void
2515target_disconnect (char *args, int from_tty)
2516{
597320e7
DJ
2517 struct target_ops *t;
2518
50c71eaf
PA
2519 /* If we're in breakpoints-always-inserted mode or if breakpoints
2520 are global across processes, we have to remove them before
2521 disconnecting. */
74960c60
VP
2522 remove_breakpoints ();
2523
597320e7
DJ
2524 for (t = current_target.beneath; t != NULL; t = t->beneath)
2525 if (t->to_disconnect != NULL)
2526 {
2527 if (targetdebug)
2528 fprintf_unfiltered (gdb_stdlog, "target_disconnect (%s, %d)\n",
2529 args, from_tty);
2530 t->to_disconnect (t, args, from_tty);
2531 return;
2532 }
2533
2534 tcomplain ();
6ad8ae5c
DJ
2535}
2536
117de6a9 2537ptid_t
47608cb1 2538target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
117de6a9
PA
2539{
2540 struct target_ops *t;
2541
2542 for (t = current_target.beneath; t != NULL; t = t->beneath)
2543 {
2544 if (t->to_wait != NULL)
2545 {
47608cb1 2546 ptid_t retval = (*t->to_wait) (t, ptid, status, options);
117de6a9
PA
2547
2548 if (targetdebug)
2549 {
2550 char *status_string;
2551
2552 status_string = target_waitstatus_to_string (status);
2553 fprintf_unfiltered (gdb_stdlog,
2554 "target_wait (%d, status) = %d, %s\n",
2555 PIDGET (ptid), PIDGET (retval),
2556 status_string);
2557 xfree (status_string);
2558 }
2559
2560 return retval;
2561 }
2562 }
2563
2564 noprocess ();
2565}
2566
2567char *
2568target_pid_to_str (ptid_t ptid)
2569{
2570 struct target_ops *t;
2571
2572 for (t = current_target.beneath; t != NULL; t = t->beneath)
2573 {
2574 if (t->to_pid_to_str != NULL)
2575 return (*t->to_pid_to_str) (t, ptid);
2576 }
2577
2578 return normal_pid_to_str (ptid);
2579}
2580
4694da01
TT
2581char *
2582target_thread_name (struct thread_info *info)
2583{
2584 struct target_ops *t;
2585
2586 for (t = current_target.beneath; t != NULL; t = t->beneath)
2587 {
2588 if (t->to_thread_name != NULL)
2589 return (*t->to_thread_name) (info);
2590 }
2591
2592 return NULL;
2593}
2594
e1ac3328
VP
2595void
2596target_resume (ptid_t ptid, int step, enum target_signal signal)
2597{
28439f5e
PA
2598 struct target_ops *t;
2599
4e5d721f 2600 target_dcache_invalidate ();
28439f5e
PA
2601
2602 for (t = current_target.beneath; t != NULL; t = t->beneath)
2603 {
2604 if (t->to_resume != NULL)
2605 {
2606 t->to_resume (t, ptid, step, signal);
2607 if (targetdebug)
2608 fprintf_unfiltered (gdb_stdlog, "target_resume (%d, %s, %s)\n",
2609 PIDGET (ptid),
2610 step ? "step" : "continue",
2611 target_signal_to_name (signal));
2612
e66408ed 2613 registers_changed_ptid (ptid);
28439f5e
PA
2614 set_executing (ptid, 1);
2615 set_running (ptid, 1);
edb3359d 2616 clear_inline_frame_state (ptid);
28439f5e
PA
2617 return;
2618 }
2619 }
2620
2621 noprocess ();
e1ac3328 2622}
2455069d
UW
2623
2624void
2625target_pass_signals (int numsigs, unsigned char *pass_signals)
2626{
2627 struct target_ops *t;
2628
2629 for (t = current_target.beneath; t != NULL; t = t->beneath)
2630 {
2631 if (t->to_pass_signals != NULL)
2632 {
2633 if (targetdebug)
2634 {
2635 int i;
2636
2637 fprintf_unfiltered (gdb_stdlog, "target_pass_signals (%d, {",
2638 numsigs);
2639
2640 for (i = 0; i < numsigs; i++)
2641 if (pass_signals[i])
2642 fprintf_unfiltered (gdb_stdlog, " %s",
2643 target_signal_to_name (i));
2644
2645 fprintf_unfiltered (gdb_stdlog, " })\n");
2646 }
2647
2648 (*t->to_pass_signals) (numsigs, pass_signals);
2649 return;
2650 }
2651 }
2652}
2653
ee057212
DJ
2654/* Look through the list of possible targets for a target that can
2655 follow forks. */
2656
2657int
2658target_follow_fork (int follow_child)
2659{
2660 struct target_ops *t;
2661
2662 for (t = current_target.beneath; t != NULL; t = t->beneath)
2663 {
2664 if (t->to_follow_fork != NULL)
2665 {
2666 int retval = t->to_follow_fork (t, follow_child);
5d502164 2667
ee057212
DJ
2668 if (targetdebug)
2669 fprintf_unfiltered (gdb_stdlog, "target_follow_fork (%d) = %d\n",
2670 follow_child, retval);
2671 return retval;
2672 }
2673 }
2674
2675 /* Some target returned a fork event, but did not know how to follow it. */
2676 internal_error (__FILE__, __LINE__,
9b20d036 2677 _("could not find a target to follow fork"));
ee057212
DJ
2678}
2679
136d6dae
VP
2680void
2681target_mourn_inferior (void)
2682{
2683 struct target_ops *t;
5d502164 2684
136d6dae
VP
2685 for (t = current_target.beneath; t != NULL; t = t->beneath)
2686 {
2687 if (t->to_mourn_inferior != NULL)
2688 {
2689 t->to_mourn_inferior (t);
947b8855
PA
2690 if (targetdebug)
2691 fprintf_unfiltered (gdb_stdlog, "target_mourn_inferior ()\n");
efbd6e75
JB
2692
2693 /* We no longer need to keep handles on any of the object files.
2694 Make sure to release them to avoid unnecessarily locking any
2695 of them while we're not actually debugging. */
2696 bfd_cache_close_all ();
2697
136d6dae
VP
2698 return;
2699 }
2700 }
2701
2702 internal_error (__FILE__, __LINE__,
9b20d036 2703 _("could not find a target to follow mourn inferior"));
136d6dae
VP
2704}
2705
424163ea
DJ
2706/* Look for a target which can describe architectural features, starting
2707 from TARGET. If we find one, return its description. */
2708
2709const struct target_desc *
2710target_read_description (struct target_ops *target)
2711{
2712 struct target_ops *t;
2713
2714 for (t = target; t != NULL; t = t->beneath)
2715 if (t->to_read_description != NULL)
2716 {
2717 const struct target_desc *tdesc;
2718
2719 tdesc = t->to_read_description (t);
2720 if (tdesc)
2721 return tdesc;
2722 }
2723
2724 return NULL;
2725}
2726
08388c79
DE
2727/* The default implementation of to_search_memory.
2728 This implements a basic search of memory, reading target memory and
2729 performing the search here (as opposed to performing the search in on the
2730 target side with, for example, gdbserver). */
2731
2732int
2733simple_search_memory (struct target_ops *ops,
2734 CORE_ADDR start_addr, ULONGEST search_space_len,
2735 const gdb_byte *pattern, ULONGEST pattern_len,
2736 CORE_ADDR *found_addrp)
2737{
2738 /* NOTE: also defined in find.c testcase. */
2739#define SEARCH_CHUNK_SIZE 16000
2740 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
2741 /* Buffer to hold memory contents for searching. */
2742 gdb_byte *search_buf;
2743 unsigned search_buf_size;
2744 struct cleanup *old_cleanups;
2745
2746 search_buf_size = chunk_size + pattern_len - 1;
2747
2748 /* No point in trying to allocate a buffer larger than the search space. */
2749 if (search_space_len < search_buf_size)
2750 search_buf_size = search_space_len;
2751
2752 search_buf = malloc (search_buf_size);
2753 if (search_buf == NULL)
5e1471f5 2754 error (_("Unable to allocate memory to perform the search."));
08388c79
DE
2755 old_cleanups = make_cleanup (free_current_contents, &search_buf);
2756
2757 /* Prime the search buffer. */
2758
2759 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2760 search_buf, start_addr, search_buf_size) != search_buf_size)
2761 {
5e1471f5 2762 warning (_("Unable to access target memory at %s, halting search."),
08388c79
DE
2763 hex_string (start_addr));
2764 do_cleanups (old_cleanups);
2765 return -1;
2766 }
2767
2768 /* Perform the search.
2769
2770 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
2771 When we've scanned N bytes we copy the trailing bytes to the start and
2772 read in another N bytes. */
2773
2774 while (search_space_len >= pattern_len)
2775 {
2776 gdb_byte *found_ptr;
2777 unsigned nr_search_bytes = min (search_space_len, search_buf_size);
2778
2779 found_ptr = memmem (search_buf, nr_search_bytes,
2780 pattern, pattern_len);
2781
2782 if (found_ptr != NULL)
2783 {
2784 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
5d502164 2785
08388c79
DE
2786 *found_addrp = found_addr;
2787 do_cleanups (old_cleanups);
2788 return 1;
2789 }
2790
2791 /* Not found in this chunk, skip to next chunk. */
2792
2793 /* Don't let search_space_len wrap here, it's unsigned. */
2794 if (search_space_len >= chunk_size)
2795 search_space_len -= chunk_size;
2796 else
2797 search_space_len = 0;
2798
2799 if (search_space_len >= pattern_len)
2800 {
2801 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 2802 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
2803 int nr_to_read;
2804
2805 /* Copy the trailing part of the previous iteration to the front
2806 of the buffer for the next iteration. */
2807 gdb_assert (keep_len == pattern_len - 1);
2808 memcpy (search_buf, search_buf + chunk_size, keep_len);
2809
2810 nr_to_read = min (search_space_len - keep_len, chunk_size);
2811
2812 if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
2813 search_buf + keep_len, read_addr,
2814 nr_to_read) != nr_to_read)
2815 {
9b20d036
MS
2816 warning (_("Unable to access target "
2817 "memory at %s, halting search."),
08388c79
DE
2818 hex_string (read_addr));
2819 do_cleanups (old_cleanups);
2820 return -1;
2821 }
2822
2823 start_addr += chunk_size;
2824 }
2825 }
2826
2827 /* Not found. */
2828
2829 do_cleanups (old_cleanups);
2830 return 0;
2831}
2832
2833/* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
2834 sequence of bytes in PATTERN with length PATTERN_LEN.
2835
2836 The result is 1 if found, 0 if not found, and -1 if there was an error
2837 requiring halting of the search (e.g. memory read error).
2838 If the pattern is found the address is recorded in FOUND_ADDRP. */
2839
2840int
2841target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
2842 const gdb_byte *pattern, ULONGEST pattern_len,
2843 CORE_ADDR *found_addrp)
2844{
2845 struct target_ops *t;
2846 int found;
2847
2848 /* We don't use INHERIT to set current_target.to_search_memory,
2849 so we have to scan the target stack and handle targetdebug
2850 ourselves. */
2851
2852 if (targetdebug)
2853 fprintf_unfiltered (gdb_stdlog, "target_search_memory (%s, ...)\n",
2854 hex_string (start_addr));
2855
2856 for (t = current_target.beneath; t != NULL; t = t->beneath)
2857 if (t->to_search_memory != NULL)
2858 break;
2859
2860 if (t != NULL)
2861 {
2862 found = t->to_search_memory (t, start_addr, search_space_len,
2863 pattern, pattern_len, found_addrp);
2864 }
2865 else
2866 {
2867 /* If a special version of to_search_memory isn't available, use the
2868 simple version. */
c35b1492 2869 found = simple_search_memory (current_target.beneath,
08388c79
DE
2870 start_addr, search_space_len,
2871 pattern, pattern_len, found_addrp);
2872 }
2873
2874 if (targetdebug)
2875 fprintf_unfiltered (gdb_stdlog, " = %d\n", found);
2876
2877 return found;
2878}
2879
8edfe269
DJ
2880/* Look through the currently pushed targets. If none of them will
2881 be able to restart the currently running process, issue an error
2882 message. */
2883
2884void
2885target_require_runnable (void)
2886{
2887 struct target_ops *t;
2888
2889 for (t = target_stack; t != NULL; t = t->beneath)
2890 {
2891 /* If this target knows how to create a new program, then
2892 assume we will still be able to after killing the current
2893 one. Either killing and mourning will not pop T, or else
2894 find_default_run_target will find it again. */
2895 if (t->to_create_inferior != NULL)
2896 return;
2897
2898 /* Do not worry about thread_stratum targets that can not
2899 create inferiors. Assume they will be pushed again if
2900 necessary, and continue to the process_stratum. */
85e747d2
UW
2901 if (t->to_stratum == thread_stratum
2902 || t->to_stratum == arch_stratum)
8edfe269
DJ
2903 continue;
2904
3e43a32a
MS
2905 error (_("The \"%s\" target does not support \"run\". "
2906 "Try \"help target\" or \"continue\"."),
8edfe269
DJ
2907 t->to_shortname);
2908 }
2909
2910 /* This function is only called if the target is running. In that
2911 case there should have been a process_stratum target and it
c378eb4e 2912 should either know how to create inferiors, or not... */
9b20d036 2913 internal_error (__FILE__, __LINE__, _("No targets found"));
8edfe269
DJ
2914}
2915
c906108c
SS
2916/* Look through the list of possible targets for a target that can
2917 execute a run or attach command without any other data. This is
2918 used to locate the default process stratum.
2919
5f667f2d
PA
2920 If DO_MESG is not NULL, the result is always valid (error() is
2921 called for errors); else, return NULL on error. */
c906108c
SS
2922
2923static struct target_ops *
fba45db2 2924find_default_run_target (char *do_mesg)
c906108c
SS
2925{
2926 struct target_ops **t;
2927 struct target_ops *runable = NULL;
2928 int count;
2929
2930 count = 0;
2931
2932 for (t = target_structs; t < target_structs + target_struct_size;
2933 ++t)
2934 {
c5aa993b 2935 if ((*t)->to_can_run && target_can_run (*t))
c906108c
SS
2936 {
2937 runable = *t;
2938 ++count;
2939 }
2940 }
2941
2942 if (count != 1)
5f667f2d
PA
2943 {
2944 if (do_mesg)
2945 error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
2946 else
2947 return NULL;
2948 }
c906108c
SS
2949
2950 return runable;
2951}
2952
2953void
136d6dae 2954find_default_attach (struct target_ops *ops, char *args, int from_tty)
c906108c
SS
2955{
2956 struct target_ops *t;
2957
c5aa993b 2958 t = find_default_run_target ("attach");
136d6dae 2959 (t->to_attach) (t, args, from_tty);
c906108c
SS
2960 return;
2961}
2962
c906108c 2963void
136d6dae
VP
2964find_default_create_inferior (struct target_ops *ops,
2965 char *exec_file, char *allargs, char **env,
c27cda74 2966 int from_tty)
c906108c
SS
2967{
2968 struct target_ops *t;
2969
c5aa993b 2970 t = find_default_run_target ("run");
136d6dae 2971 (t->to_create_inferior) (t, exec_file, allargs, env, from_tty);
c906108c
SS
2972 return;
2973}
2974
2c0b251b 2975static int
b84876c2
PA
2976find_default_can_async_p (void)
2977{
2978 struct target_ops *t;
2979
5f667f2d
PA
2980 /* This may be called before the target is pushed on the stack;
2981 look for the default process stratum. If there's none, gdb isn't
2982 configured with a native debugger, and target remote isn't
2983 connected yet. */
2984 t = find_default_run_target (NULL);
2985 if (t && t->to_can_async_p)
b84876c2
PA
2986 return (t->to_can_async_p) ();
2987 return 0;
2988}
2989
2c0b251b 2990static int
b84876c2
PA
2991find_default_is_async_p (void)
2992{
2993 struct target_ops *t;
2994
5f667f2d
PA
2995 /* This may be called before the target is pushed on the stack;
2996 look for the default process stratum. If there's none, gdb isn't
2997 configured with a native debugger, and target remote isn't
2998 connected yet. */
2999 t = find_default_run_target (NULL);
3000 if (t && t->to_is_async_p)
b84876c2
PA
3001 return (t->to_is_async_p) ();
3002 return 0;
3003}
3004
2c0b251b 3005static int
9908b566
VP
3006find_default_supports_non_stop (void)
3007{
3008 struct target_ops *t;
3009
3010 t = find_default_run_target (NULL);
3011 if (t && t->to_supports_non_stop)
3012 return (t->to_supports_non_stop) ();
3013 return 0;
3014}
3015
3016int
2c0b251b 3017target_supports_non_stop (void)
9908b566
VP
3018{
3019 struct target_ops *t;
5d502164 3020
9908b566
VP
3021 for (t = &current_target; t != NULL; t = t->beneath)
3022 if (t->to_supports_non_stop)
3023 return t->to_supports_non_stop ();
3024
3025 return 0;
3026}
3027
03583c20
UW
3028static int
3029find_default_supports_disable_randomization (void)
3030{
3031 struct target_ops *t;
3032
3033 t = find_default_run_target (NULL);
3034 if (t && t->to_supports_disable_randomization)
3035 return (t->to_supports_disable_randomization) ();
3036 return 0;
3037}
3038
3039int
3040target_supports_disable_randomization (void)
3041{
3042 struct target_ops *t;
3043
3044 for (t = &current_target; t != NULL; t = t->beneath)
3045 if (t->to_supports_disable_randomization)
3046 return t->to_supports_disable_randomization ();
3047
3048 return 0;
3049}
9908b566 3050
07e059b5
VP
3051char *
3052target_get_osdata (const char *type)
3053{
07e059b5
VP
3054 struct target_ops *t;
3055
739ef7fb
PA
3056 /* If we're already connected to something that can get us OS
3057 related data, use it. Otherwise, try using the native
3058 target. */
3059 if (current_target.to_stratum >= process_stratum)
6d097e65 3060 t = current_target.beneath;
739ef7fb
PA
3061 else
3062 t = find_default_run_target ("get OS data");
07e059b5
VP
3063
3064 if (!t)
3065 return NULL;
3066
6d097e65 3067 return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
07e059b5
VP
3068}
3069
6c95b8df
PA
3070/* Determine the current address space of thread PTID. */
3071
3072struct address_space *
3073target_thread_address_space (ptid_t ptid)
3074{
c0694254 3075 struct address_space *aspace;
6c95b8df 3076 struct inferior *inf;
c0694254
PA
3077 struct target_ops *t;
3078
3079 for (t = current_target.beneath; t != NULL; t = t->beneath)
3080 {
3081 if (t->to_thread_address_space != NULL)
3082 {
3083 aspace = t->to_thread_address_space (t, ptid);
3084 gdb_assert (aspace);
6c95b8df 3085
c0694254
PA
3086 if (targetdebug)
3087 fprintf_unfiltered (gdb_stdlog,
3088 "target_thread_address_space (%s) = %d\n",
3089 target_pid_to_str (ptid),
3090 address_space_num (aspace));
3091 return aspace;
3092 }
3093 }
6c95b8df
PA
3094
3095 /* Fall-back to the "main" address space of the inferior. */
3096 inf = find_inferior_pid (ptid_get_pid (ptid));
3097
3098 if (inf == NULL || inf->aspace == NULL)
3e43a32a 3099 internal_error (__FILE__, __LINE__,
9b20d036
MS
3100 _("Can't determine the current "
3101 "address space of thread %s\n"),
6c95b8df
PA
3102 target_pid_to_str (ptid));
3103
3104 return inf->aspace;
3105}
3106
e0d24f8d
WZ
3107static int
3108default_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3109{
ffe5a37e 3110 return (len <= gdbarch_ptr_bit (target_gdbarch) / TARGET_CHAR_BIT);
ccaa32c7
GS
3111}
3112
5009afc5
AS
3113static int
3114default_watchpoint_addr_within_range (struct target_ops *target,
3115 CORE_ADDR addr,
3116 CORE_ADDR start, int length)
3117{
3118 return addr >= start && addr < start + length;
3119}
3120
c2250ad1
UW
3121static struct gdbarch *
3122default_thread_architecture (struct target_ops *ops, ptid_t ptid)
3123{
3124 return target_gdbarch;
3125}
3126
c906108c 3127static int
fba45db2 3128return_zero (void)
c906108c
SS
3129{
3130 return 0;
3131}
3132
3133static int
fba45db2 3134return_one (void)
c906108c
SS
3135{
3136 return 1;
3137}
3138
ccaa32c7
GS
3139static int
3140return_minus_one (void)
3141{
3142 return -1;
3143}
3144
7a292a7a
SS
3145/* Find a single runnable target in the stack and return it. If for
3146 some reason there is more than one, return NULL. */
3147
3148struct target_ops *
fba45db2 3149find_run_target (void)
7a292a7a
SS
3150{
3151 struct target_ops **t;
3152 struct target_ops *runable = NULL;
3153 int count;
c5aa993b 3154
7a292a7a 3155 count = 0;
c5aa993b 3156
7a292a7a
SS
3157 for (t = target_structs; t < target_structs + target_struct_size; ++t)
3158 {
c5aa993b 3159 if ((*t)->to_can_run && target_can_run (*t))
7a292a7a
SS
3160 {
3161 runable = *t;
3162 ++count;
3163 }
3164 }
c5aa993b 3165
7a292a7a
SS
3166 return (count == 1 ? runable : NULL);
3167}
3168
ed9a39eb
JM
3169/*
3170 * Find the next target down the stack from the specified target.
3171 */
3172
3173struct target_ops *
fba45db2 3174find_target_beneath (struct target_ops *t)
ed9a39eb 3175{
258b763a 3176 return t->beneath;
ed9a39eb
JM
3177}
3178
c906108c
SS
3179\f
3180/* The inferior process has died. Long live the inferior! */
3181
3182void
fba45db2 3183generic_mourn_inferior (void)
c906108c 3184{
7f9f62ba 3185 ptid_t ptid;
c906108c 3186
7f9f62ba 3187 ptid = inferior_ptid;
39f77062 3188 inferior_ptid = null_ptid;
7f9f62ba
PA
3189
3190 if (!ptid_equal (ptid, null_ptid))
3191 {
3192 int pid = ptid_get_pid (ptid);
6c95b8df 3193 exit_inferior (pid);
7f9f62ba
PA
3194 }
3195
c906108c
SS
3196 breakpoint_init_inferior (inf_exited);
3197 registers_changed ();
3198
c906108c
SS
3199 reopen_exec_file ();
3200 reinit_frame_cache ();
3201
9a4105ab
AC
3202 if (deprecated_detach_hook)
3203 deprecated_detach_hook ();
c906108c
SS
3204}
3205\f
8807d78b 3206/* Helper function for child_wait and the derivatives of child_wait.
c906108c
SS
3207 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
3208 translation of that in OURSTATUS. */
3209void
fba45db2 3210store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
c906108c 3211{
c906108c
SS
3212 if (WIFEXITED (hoststatus))
3213 {
3214 ourstatus->kind = TARGET_WAITKIND_EXITED;
3215 ourstatus->value.integer = WEXITSTATUS (hoststatus);
3216 }
3217 else if (!WIFSTOPPED (hoststatus))
3218 {
3219 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
3220 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
3221 }
3222 else
3223 {
3224 ourstatus->kind = TARGET_WAITKIND_STOPPED;
3225 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
3226 }
3227}
3228\f
fd0a2a6f
MK
3229/* Convert a normal process ID to a string. Returns the string in a
3230 static buffer. */
c906108c
SS
3231
3232char *
39f77062 3233normal_pid_to_str (ptid_t ptid)
c906108c 3234{
fd0a2a6f 3235 static char buf[32];
c906108c 3236
5fff8fc0 3237 xsnprintf (buf, sizeof buf, "process %d", ptid_get_pid (ptid));
c906108c
SS
3238 return buf;
3239}
3240
2c0b251b 3241static char *
117de6a9
PA
3242dummy_pid_to_str (struct target_ops *ops, ptid_t ptid)
3243{
3244 return normal_pid_to_str (ptid);
3245}
3246
9b4eba8e
HZ
3247/* Error-catcher for target_find_memory_regions. */
3248static int
b8edc417 3249dummy_find_memory_regions (find_memory_region_ftype ignore1, void *ignore2)
be4d1333 3250{
9b4eba8e 3251 error (_("Command not implemented for this target."));
be4d1333
MS
3252 return 0;
3253}
3254
9b4eba8e
HZ
3255/* Error-catcher for target_make_corefile_notes. */
3256static char *
3257dummy_make_corefile_notes (bfd *ignore1, int *ignore2)
be4d1333 3258{
9b4eba8e 3259 error (_("Command not implemented for this target."));
be4d1333
MS
3260 return NULL;
3261}
3262
6b04bdb7
MS
3263/* Error-catcher for target_get_bookmark. */
3264static gdb_byte *
3265dummy_get_bookmark (char *ignore1, int ignore2)
3266{
3267 tcomplain ();
3268 return NULL;
3269}
3270
3271/* Error-catcher for target_goto_bookmark. */
3272static void
3273dummy_goto_bookmark (gdb_byte *ignore, int from_tty)
3274{
3275 tcomplain ();
3276}
3277
c906108c
SS
3278/* Set up the handful of non-empty slots needed by the dummy target
3279 vector. */
3280
3281static void
fba45db2 3282init_dummy_target (void)
c906108c
SS
3283{
3284 dummy_target.to_shortname = "None";
3285 dummy_target.to_longname = "None";
3286 dummy_target.to_doc = "";
3287 dummy_target.to_attach = find_default_attach;
136d6dae
VP
3288 dummy_target.to_detach =
3289 (void (*)(struct target_ops *, char *, int))target_ignore;
c906108c 3290 dummy_target.to_create_inferior = find_default_create_inferior;
b84876c2
PA
3291 dummy_target.to_can_async_p = find_default_can_async_p;
3292 dummy_target.to_is_async_p = find_default_is_async_p;
9908b566 3293 dummy_target.to_supports_non_stop = find_default_supports_non_stop;
03583c20
UW
3294 dummy_target.to_supports_disable_randomization
3295 = find_default_supports_disable_randomization;
117de6a9 3296 dummy_target.to_pid_to_str = dummy_pid_to_str;
c906108c 3297 dummy_target.to_stratum = dummy_stratum;
be4d1333
MS
3298 dummy_target.to_find_memory_regions = dummy_find_memory_regions;
3299 dummy_target.to_make_corefile_notes = dummy_make_corefile_notes;
6b04bdb7
MS
3300 dummy_target.to_get_bookmark = dummy_get_bookmark;
3301 dummy_target.to_goto_bookmark = dummy_goto_bookmark;
0b603eba 3302 dummy_target.to_xfer_partial = default_xfer_partial;
c35b1492
PA
3303 dummy_target.to_has_all_memory = (int (*) (struct target_ops *)) return_zero;
3304 dummy_target.to_has_memory = (int (*) (struct target_ops *)) return_zero;
3305 dummy_target.to_has_stack = (int (*) (struct target_ops *)) return_zero;
3306 dummy_target.to_has_registers = (int (*) (struct target_ops *)) return_zero;
aeaec162
TT
3307 dummy_target.to_has_execution
3308 = (int (*) (struct target_ops *, ptid_t)) return_zero;
7155de5a
HZ
3309 dummy_target.to_stopped_by_watchpoint = return_zero;
3310 dummy_target.to_stopped_data_address =
3311 (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
c906108c
SS
3312 dummy_target.to_magic = OPS_MAGIC;
3313}
c906108c 3314\f
c906108c 3315static void
fba45db2 3316debug_to_open (char *args, int from_tty)
c906108c
SS
3317{
3318 debug_target.to_open (args, from_tty);
3319
96baa820 3320 fprintf_unfiltered (gdb_stdlog, "target_open (%s, %d)\n", args, from_tty);
c906108c
SS
3321}
3322
f1c07ab0
AC
3323void
3324target_close (struct target_ops *targ, int quitting)
3325{
3326 if (targ->to_xclose != NULL)
3327 targ->to_xclose (targ, quitting);
3328 else if (targ->to_close != NULL)
3329 targ->to_close (quitting);
947b8855
PA
3330
3331 if (targetdebug)
3332 fprintf_unfiltered (gdb_stdlog, "target_close (%d)\n", quitting);
f1c07ab0
AC
3333}
3334
136d6dae
VP
3335void
3336target_attach (char *args, int from_tty)
3337{
3338 struct target_ops *t;
5d502164 3339
136d6dae
VP
3340 for (t = current_target.beneath; t != NULL; t = t->beneath)
3341 {
3342 if (t->to_attach != NULL)
3343 {
3344 t->to_attach (t, args, from_tty);
947b8855
PA
3345 if (targetdebug)
3346 fprintf_unfiltered (gdb_stdlog, "target_attach (%s, %d)\n",
3347 args, from_tty);
136d6dae
VP
3348 return;
3349 }
3350 }
3351
3352 internal_error (__FILE__, __LINE__,
9b20d036 3353 _("could not find a target to attach"));
136d6dae
VP
3354}
3355
28439f5e
PA
3356int
3357target_thread_alive (ptid_t ptid)
c906108c 3358{
28439f5e 3359 struct target_ops *t;
5d502164 3360
28439f5e
PA
3361 for (t = current_target.beneath; t != NULL; t = t->beneath)
3362 {
3363 if (t->to_thread_alive != NULL)
3364 {
3365 int retval;
c906108c 3366
28439f5e
PA
3367 retval = t->to_thread_alive (t, ptid);
3368 if (targetdebug)
3369 fprintf_unfiltered (gdb_stdlog, "target_thread_alive (%d) = %d\n",
3370 PIDGET (ptid), retval);
3371
3372 return retval;
3373 }
3374 }
3375
3376 return 0;
3377}
3378
3379void
3380target_find_new_threads (void)
3381{
3382 struct target_ops *t;
5d502164 3383
28439f5e
PA
3384 for (t = current_target.beneath; t != NULL; t = t->beneath)
3385 {
3386 if (t->to_find_new_threads != NULL)
3387 {
3388 t->to_find_new_threads (t);
3389 if (targetdebug)
3390 fprintf_unfiltered (gdb_stdlog, "target_find_new_threads ()\n");
3391
3392 return;
3393 }
3394 }
c906108c
SS
3395}
3396
d914c394
SS
3397void
3398target_stop (ptid_t ptid)
3399{
3400 if (!may_stop)
3401 {
3402 warning (_("May not interrupt or stop the target, ignoring attempt"));
3403 return;
3404 }
3405
3406 (*current_target.to_stop) (ptid);
3407}
3408
c906108c 3409static void
28439f5e 3410debug_to_post_attach (int pid)
c906108c 3411{
28439f5e 3412 debug_target.to_post_attach (pid);
c906108c 3413
28439f5e 3414 fprintf_unfiltered (gdb_stdlog, "target_post_attach (%d)\n", pid);
c906108c
SS
3415}
3416
f00150c9
DE
3417/* Return a pretty printed form of target_waitstatus.
3418 Space for the result is malloc'd, caller must free. */
c906108c 3419
f00150c9
DE
3420char *
3421target_waitstatus_to_string (const struct target_waitstatus *ws)
3422{
3423 const char *kind_str = "status->kind = ";
c906108c 3424
f00150c9 3425 switch (ws->kind)
c906108c
SS
3426 {
3427 case TARGET_WAITKIND_EXITED:
f00150c9
DE
3428 return xstrprintf ("%sexited, status = %d",
3429 kind_str, ws->value.integer);
c906108c 3430 case TARGET_WAITKIND_STOPPED:
f00150c9
DE
3431 return xstrprintf ("%sstopped, signal = %s",
3432 kind_str, target_signal_to_name (ws->value.sig));
c906108c 3433 case TARGET_WAITKIND_SIGNALLED:
f00150c9
DE
3434 return xstrprintf ("%ssignalled, signal = %s",
3435 kind_str, target_signal_to_name (ws->value.sig));
c906108c 3436 case TARGET_WAITKIND_LOADED:
f00150c9 3437 return xstrprintf ("%sloaded", kind_str);
c906108c 3438 case TARGET_WAITKIND_FORKED:
f00150c9 3439 return xstrprintf ("%sforked", kind_str);
c906108c 3440 case TARGET_WAITKIND_VFORKED:
f00150c9 3441 return xstrprintf ("%svforked", kind_str);
c906108c 3442 case TARGET_WAITKIND_EXECD:
f00150c9
DE
3443 return xstrprintf ("%sexecd", kind_str);
3444 case TARGET_WAITKIND_SYSCALL_ENTRY:
a96d9b2e 3445 return xstrprintf ("%sentered syscall", kind_str);
f00150c9 3446 case TARGET_WAITKIND_SYSCALL_RETURN:
a96d9b2e 3447 return xstrprintf ("%sexited syscall", kind_str);
c906108c 3448 case TARGET_WAITKIND_SPURIOUS:
f00150c9
DE
3449 return xstrprintf ("%sspurious", kind_str);
3450 case TARGET_WAITKIND_IGNORE:
3451 return xstrprintf ("%signore", kind_str);
3452 case TARGET_WAITKIND_NO_HISTORY:
3453 return xstrprintf ("%sno-history", kind_str);
0c94aa73
PA
3454 case TARGET_WAITKIND_NO_RESUMED:
3455 return xstrprintf ("%sno-resumed", kind_str);
c906108c 3456 default:
f00150c9 3457 return xstrprintf ("%sunknown???", kind_str);
c906108c 3458 }
f00150c9
DE
3459}
3460
bf0c5130 3461static void
56be3814
UW
3462debug_print_register (const char * func,
3463 struct regcache *regcache, int regno)
bf0c5130 3464{
f8d29908 3465 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5d502164 3466
bf0c5130 3467 fprintf_unfiltered (gdb_stdlog, "%s ", func);
f8d29908 3468 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
f8d29908
UW
3469 && gdbarch_register_name (gdbarch, regno) != NULL
3470 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
3471 fprintf_unfiltered (gdb_stdlog, "(%s)",
3472 gdbarch_register_name (gdbarch, regno));
bf0c5130
AC
3473 else
3474 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
0ff58721 3475 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
bf0c5130 3476 {
e17a4113 3477 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f8d29908 3478 int i, size = register_size (gdbarch, regno);
d9d9c31f 3479 unsigned char buf[MAX_REGISTER_SIZE];
5d502164 3480
0ff58721 3481 regcache_raw_collect (regcache, regno, buf);
bf0c5130 3482 fprintf_unfiltered (gdb_stdlog, " = ");
81c4a259 3483 for (i = 0; i < size; i++)
bf0c5130
AC
3484 {
3485 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
3486 }
81c4a259 3487 if (size <= sizeof (LONGEST))
bf0c5130 3488 {
e17a4113 3489 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
5d502164 3490
0b1553bc
UW
3491 fprintf_unfiltered (gdb_stdlog, " %s %s",
3492 core_addr_to_string_nz (val), plongest (val));
bf0c5130
AC
3493 }
3494 }
3495 fprintf_unfiltered (gdb_stdlog, "\n");
3496}
3497
28439f5e
PA
3498void
3499target_fetch_registers (struct regcache *regcache, int regno)
c906108c 3500{
28439f5e 3501 struct target_ops *t;
5d502164 3502
28439f5e
PA
3503 for (t = current_target.beneath; t != NULL; t = t->beneath)
3504 {
3505 if (t->to_fetch_registers != NULL)
3506 {
3507 t->to_fetch_registers (t, regcache, regno);
3508 if (targetdebug)
3509 debug_print_register ("target_fetch_registers", regcache, regno);
3510 return;
3511 }
3512 }
c906108c
SS
3513}
3514
28439f5e
PA
3515void
3516target_store_registers (struct regcache *regcache, int regno)
c906108c 3517{
28439f5e 3518 struct target_ops *t;
5d502164 3519
d914c394
SS
3520 if (!may_write_registers)
3521 error (_("Writing to registers is not allowed (regno %d)"), regno);
3522
28439f5e
PA
3523 for (t = current_target.beneath; t != NULL; t = t->beneath)
3524 {
3525 if (t->to_store_registers != NULL)
3526 {
3527 t->to_store_registers (t, regcache, regno);
3528 if (targetdebug)
3529 {
3530 debug_print_register ("target_store_registers", regcache, regno);
3531 }
3532 return;
3533 }
3534 }
3535
3536 noprocess ();
c906108c
SS
3537}
3538
dc146f7c
VP
3539int
3540target_core_of_thread (ptid_t ptid)
3541{
3542 struct target_ops *t;
3543
3544 for (t = current_target.beneath; t != NULL; t = t->beneath)
3545 {
3546 if (t->to_core_of_thread != NULL)
3547 {
3548 int retval = t->to_core_of_thread (t, ptid);
5d502164 3549
dc146f7c 3550 if (targetdebug)
3e43a32a
MS
3551 fprintf_unfiltered (gdb_stdlog,
3552 "target_core_of_thread (%d) = %d\n",
dc146f7c
VP
3553 PIDGET (ptid), retval);
3554 return retval;
3555 }
3556 }
3557
3558 return -1;
3559}
3560
4a5e7a5b
PA
3561int
3562target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
3563{
3564 struct target_ops *t;
3565
3566 for (t = current_target.beneath; t != NULL; t = t->beneath)
3567 {
3568 if (t->to_verify_memory != NULL)
3569 {
3570 int retval = t->to_verify_memory (t, data, memaddr, size);
5d502164 3571
4a5e7a5b 3572 if (targetdebug)
3e43a32a
MS
3573 fprintf_unfiltered (gdb_stdlog,
3574 "target_verify_memory (%s, %s) = %d\n",
4a5e7a5b
PA
3575 paddress (target_gdbarch, memaddr),
3576 pulongest (size),
3577 retval);
3578 return retval;
3579 }
3580 }
3581
3582 tcomplain ();
3583}
3584
9c06b0b4
TJB
3585/* The documentation for this function is in its prototype declaration in
3586 target.h. */
3587
3588int
3589target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3590{
3591 struct target_ops *t;
3592
3593 for (t = current_target.beneath; t != NULL; t = t->beneath)
3594 if (t->to_insert_mask_watchpoint != NULL)
3595 {
3596 int ret;
3597
3598 ret = t->to_insert_mask_watchpoint (t, addr, mask, rw);
3599
3600 if (targetdebug)
3601 fprintf_unfiltered (gdb_stdlog, "\
3602target_insert_mask_watchpoint (%s, %s, %d) = %d\n",
3603 core_addr_to_string (addr),
3604 core_addr_to_string (mask), rw, ret);
3605
3606 return ret;
3607 }
3608
3609 return 1;
3610}
3611
3612/* The documentation for this function is in its prototype declaration in
3613 target.h. */
3614
3615int
3616target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask, int rw)
3617{
3618 struct target_ops *t;
3619
3620 for (t = current_target.beneath; t != NULL; t = t->beneath)
3621 if (t->to_remove_mask_watchpoint != NULL)
3622 {
3623 int ret;
3624
3625 ret = t->to_remove_mask_watchpoint (t, addr, mask, rw);
3626
3627 if (targetdebug)
3628 fprintf_unfiltered (gdb_stdlog, "\
3629target_remove_mask_watchpoint (%s, %s, %d) = %d\n",
3630 core_addr_to_string (addr),
3631 core_addr_to_string (mask), rw, ret);
3632
3633 return ret;
3634 }
3635
3636 return 1;
3637}
3638
3639/* The documentation for this function is in its prototype declaration
3640 in target.h. */
3641
3642int
3643target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
3644{
3645 struct target_ops *t;
3646
3647 for (t = current_target.beneath; t != NULL; t = t->beneath)
3648 if (t->to_masked_watch_num_registers != NULL)
3649 return t->to_masked_watch_num_registers (t, addr, mask);
3650
3651 return -1;
3652}
3653
f1310107
TJB
3654/* The documentation for this function is in its prototype declaration
3655 in target.h. */
3656
3657int
3658target_ranged_break_num_registers (void)
3659{
3660 struct target_ops *t;
3661
3662 for (t = current_target.beneath; t != NULL; t = t->beneath)
3663 if (t->to_ranged_break_num_registers != NULL)
3664 return t->to_ranged_break_num_registers (t);
3665
3666 return -1;
3667}
3668
c906108c 3669static void
316f2060 3670debug_to_prepare_to_store (struct regcache *regcache)
c906108c 3671{
316f2060 3672 debug_target.to_prepare_to_store (regcache);
c906108c 3673
96baa820 3674 fprintf_unfiltered (gdb_stdlog, "target_prepare_to_store ()\n");
c906108c
SS
3675}
3676
3677static int
961cb7b5 3678deprecated_debug_xfer_memory (CORE_ADDR memaddr, bfd_byte *myaddr, int len,
c8e73a31
AC
3679 int write, struct mem_attrib *attrib,
3680 struct target_ops *target)
c906108c
SS
3681{
3682 int retval;
3683
c8e73a31
AC
3684 retval = debug_target.deprecated_xfer_memory (memaddr, myaddr, len, write,
3685 attrib, target);
c906108c 3686
96baa820 3687 fprintf_unfiltered (gdb_stdlog,
53b71562 3688 "target_xfer_memory (%s, xxx, %d, %s, xxx) = %d",
5af949e3
UW
3689 paddress (target_gdbarch, memaddr), len,
3690 write ? "write" : "read", retval);
c906108c 3691
c906108c
SS
3692 if (retval > 0)
3693 {
3694 int i;
3695
96baa820 3696 fputs_unfiltered (", bytes =", gdb_stdlog);
c906108c
SS
3697 for (i = 0; i < retval; i++)
3698 {
53b71562 3699 if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
333dabeb
DJ
3700 {
3701 if (targetdebug < 2 && i > 0)
3702 {
3703 fprintf_unfiltered (gdb_stdlog, " ...");
3704 break;
3705 }
3706 fprintf_unfiltered (gdb_stdlog, "\n");
3707 }
2bc416ba 3708
96baa820 3709 fprintf_unfiltered (gdb_stdlog, " %02x", myaddr[i] & 0xff);
c906108c
SS
3710 }
3711 }
3712
96baa820 3713 fputc_unfiltered ('\n', gdb_stdlog);
c906108c
SS
3714
3715 return retval;
3716}
3717
3718static void
fba45db2 3719debug_to_files_info (struct target_ops *target)
c906108c
SS
3720{
3721 debug_target.to_files_info (target);
3722
96baa820 3723 fprintf_unfiltered (gdb_stdlog, "target_files_info (xxx)\n");
c906108c
SS
3724}
3725
3726static int
a6d9a66e
UW
3727debug_to_insert_breakpoint (struct gdbarch *gdbarch,
3728 struct bp_target_info *bp_tgt)
c906108c
SS
3729{
3730 int retval;
3731
a6d9a66e 3732 retval = debug_target.to_insert_breakpoint (gdbarch, bp_tgt);
c906108c 3733
96baa820 3734 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3735 "target_insert_breakpoint (%s, xxx) = %ld\n",
3736 core_addr_to_string (bp_tgt->placed_address),
104c1213 3737 (unsigned long) retval);
c906108c
SS
3738 return retval;
3739}
3740
3741static int
a6d9a66e
UW
3742debug_to_remove_breakpoint (struct gdbarch *gdbarch,
3743 struct bp_target_info *bp_tgt)
c906108c
SS
3744{
3745 int retval;
3746
a6d9a66e 3747 retval = debug_target.to_remove_breakpoint (gdbarch, bp_tgt);
c906108c 3748
96baa820 3749 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3750 "target_remove_breakpoint (%s, xxx) = %ld\n",
3751 core_addr_to_string (bp_tgt->placed_address),
104c1213 3752 (unsigned long) retval);
c906108c
SS
3753 return retval;
3754}
3755
ccaa32c7
GS
3756static int
3757debug_to_can_use_hw_breakpoint (int type, int cnt, int from_tty)
3758{
3759 int retval;
3760
3761 retval = debug_target.to_can_use_hw_breakpoint (type, cnt, from_tty);
3762
3763 fprintf_unfiltered (gdb_stdlog,
3764 "target_can_use_hw_breakpoint (%ld, %ld, %ld) = %ld\n",
3765 (unsigned long) type,
3766 (unsigned long) cnt,
3767 (unsigned long) from_tty,
3768 (unsigned long) retval);
3769 return retval;
3770}
3771
e0d24f8d
WZ
3772static int
3773debug_to_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
3774{
3775 CORE_ADDR retval;
3776
3777 retval = debug_target.to_region_ok_for_hw_watchpoint (addr, len);
3778
3779 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3780 "target_region_ok_for_hw_watchpoint (%s, %ld) = %s\n",
3781 core_addr_to_string (addr), (unsigned long) len,
3782 core_addr_to_string (retval));
e0d24f8d
WZ
3783 return retval;
3784}
3785
0cf6dd15
TJB
3786static int
3787debug_to_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
3788 struct expression *cond)
3789{
3790 int retval;
3791
3e43a32a
MS
3792 retval = debug_target.to_can_accel_watchpoint_condition (addr, len,
3793 rw, cond);
0cf6dd15
TJB
3794
3795 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
3796 "target_can_accel_watchpoint_condition "
3797 "(%s, %d, %d, %s) = %ld\n",
bd91e7ae
OS
3798 core_addr_to_string (addr), len, rw,
3799 host_address_to_string (cond), (unsigned long) retval);
0cf6dd15
TJB
3800 return retval;
3801}
3802
ccaa32c7
GS
3803static int
3804debug_to_stopped_by_watchpoint (void)
3805{
3806 int retval;
3807
3808 retval = debug_target.to_stopped_by_watchpoint ();
3809
3810 fprintf_unfiltered (gdb_stdlog,
d92524f1 3811 "target_stopped_by_watchpoint () = %ld\n",
ccaa32c7
GS
3812 (unsigned long) retval);
3813 return retval;
3814}
3815
4aa7a7f5
JJ
3816static int
3817debug_to_stopped_data_address (struct target_ops *target, CORE_ADDR *addr)
ccaa32c7 3818{
4aa7a7f5 3819 int retval;
ccaa32c7 3820
4aa7a7f5 3821 retval = debug_target.to_stopped_data_address (target, addr);
ccaa32c7
GS
3822
3823 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3824 "target_stopped_data_address ([%s]) = %ld\n",
3825 core_addr_to_string (*addr),
4aa7a7f5 3826 (unsigned long)retval);
ccaa32c7
GS
3827 return retval;
3828}
3829
5009afc5
AS
3830static int
3831debug_to_watchpoint_addr_within_range (struct target_ops *target,
3832 CORE_ADDR addr,
3833 CORE_ADDR start, int length)
3834{
3835 int retval;
3836
3837 retval = debug_target.to_watchpoint_addr_within_range (target, addr,
3838 start, length);
3839
3840 fprintf_filtered (gdb_stdlog,
bd91e7ae
OS
3841 "target_watchpoint_addr_within_range (%s, %s, %d) = %d\n",
3842 core_addr_to_string (addr), core_addr_to_string (start),
3843 length, retval);
5009afc5
AS
3844 return retval;
3845}
3846
ccaa32c7 3847static int
a6d9a66e
UW
3848debug_to_insert_hw_breakpoint (struct gdbarch *gdbarch,
3849 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3850{
3851 int retval;
3852
a6d9a66e 3853 retval = debug_target.to_insert_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3854
3855 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3856 "target_insert_hw_breakpoint (%s, xxx) = %ld\n",
3857 core_addr_to_string (bp_tgt->placed_address),
ccaa32c7
GS
3858 (unsigned long) retval);
3859 return retval;
3860}
3861
3862static int
a6d9a66e
UW
3863debug_to_remove_hw_breakpoint (struct gdbarch *gdbarch,
3864 struct bp_target_info *bp_tgt)
ccaa32c7
GS
3865{
3866 int retval;
3867
a6d9a66e 3868 retval = debug_target.to_remove_hw_breakpoint (gdbarch, bp_tgt);
ccaa32c7
GS
3869
3870 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3871 "target_remove_hw_breakpoint (%s, xxx) = %ld\n",
3872 core_addr_to_string (bp_tgt->placed_address),
ccaa32c7
GS
3873 (unsigned long) retval);
3874 return retval;
3875}
3876
3877static int
0cf6dd15
TJB
3878debug_to_insert_watchpoint (CORE_ADDR addr, int len, int type,
3879 struct expression *cond)
ccaa32c7
GS
3880{
3881 int retval;
3882
0cf6dd15 3883 retval = debug_target.to_insert_watchpoint (addr, len, type, cond);
ccaa32c7
GS
3884
3885 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3886 "target_insert_watchpoint (%s, %d, %d, %s) = %ld\n",
3887 core_addr_to_string (addr), len, type,
3888 host_address_to_string (cond), (unsigned long) retval);
ccaa32c7
GS
3889 return retval;
3890}
3891
3892static int
0cf6dd15
TJB
3893debug_to_remove_watchpoint (CORE_ADDR addr, int len, int type,
3894 struct expression *cond)
ccaa32c7
GS
3895{
3896 int retval;
3897
0cf6dd15 3898 retval = debug_target.to_remove_watchpoint (addr, len, type, cond);
ccaa32c7
GS
3899
3900 fprintf_unfiltered (gdb_stdlog,
bd91e7ae
OS
3901 "target_remove_watchpoint (%s, %d, %d, %s) = %ld\n",
3902 core_addr_to_string (addr), len, type,
3903 host_address_to_string (cond), (unsigned long) retval);
ccaa32c7
GS
3904 return retval;
3905}
3906
c906108c 3907static void
fba45db2 3908debug_to_terminal_init (void)
c906108c
SS
3909{
3910 debug_target.to_terminal_init ();
3911
96baa820 3912 fprintf_unfiltered (gdb_stdlog, "target_terminal_init ()\n");
c906108c
SS
3913}
3914
3915static void
fba45db2 3916debug_to_terminal_inferior (void)
c906108c
SS
3917{
3918 debug_target.to_terminal_inferior ();
3919
96baa820 3920 fprintf_unfiltered (gdb_stdlog, "target_terminal_inferior ()\n");
c906108c
SS
3921}
3922
3923static void
fba45db2 3924debug_to_terminal_ours_for_output (void)
c906108c
SS
3925{
3926 debug_target.to_terminal_ours_for_output ();
3927
96baa820 3928 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours_for_output ()\n");
c906108c
SS
3929}
3930
3931static void
fba45db2 3932debug_to_terminal_ours (void)
c906108c
SS
3933{
3934 debug_target.to_terminal_ours ();
3935
96baa820 3936 fprintf_unfiltered (gdb_stdlog, "target_terminal_ours ()\n");
c906108c
SS
3937}
3938
a790ad35
SC
3939static void
3940debug_to_terminal_save_ours (void)
3941{
3942 debug_target.to_terminal_save_ours ();
3943
3944 fprintf_unfiltered (gdb_stdlog, "target_terminal_save_ours ()\n");
3945}
3946
c906108c 3947static void
fba45db2 3948debug_to_terminal_info (char *arg, int from_tty)
c906108c
SS
3949{
3950 debug_target.to_terminal_info (arg, from_tty);
3951
96baa820 3952 fprintf_unfiltered (gdb_stdlog, "target_terminal_info (%s, %d)\n", arg,
c906108c
SS
3953 from_tty);
3954}
3955
c906108c 3956static void
fba45db2 3957debug_to_load (char *args, int from_tty)
c906108c
SS
3958{
3959 debug_target.to_load (args, from_tty);
3960
96baa820 3961 fprintf_unfiltered (gdb_stdlog, "target_load (%s, %d)\n", args, from_tty);
c906108c
SS
3962}
3963
c906108c 3964static void
39f77062 3965debug_to_post_startup_inferior (ptid_t ptid)
c906108c 3966{
39f77062 3967 debug_target.to_post_startup_inferior (ptid);
c906108c 3968
96baa820 3969 fprintf_unfiltered (gdb_stdlog, "target_post_startup_inferior (%d)\n",
39f77062 3970 PIDGET (ptid));
c906108c
SS
3971}
3972
77b06cd7 3973static int
fba45db2 3974debug_to_insert_fork_catchpoint (int pid)
c906108c 3975{
77b06cd7
TJB
3976 int retval;
3977
3978 retval = debug_target.to_insert_fork_catchpoint (pid);
3979
3980 fprintf_unfiltered (gdb_stdlog, "target_insert_fork_catchpoint (%d) = %d\n",
3981 pid, retval);
c906108c 3982
77b06cd7 3983 return retval;
c906108c
SS
3984}
3985
3986static int
fba45db2 3987debug_to_remove_fork_catchpoint (int pid)
c906108c 3988{
c5aa993b 3989 int retval;
c906108c
SS
3990
3991 retval = debug_target.to_remove_fork_catchpoint (pid);
3992
96baa820 3993 fprintf_unfiltered (gdb_stdlog, "target_remove_fork_catchpoint (%d) = %d\n",
c5aa993b 3994 pid, retval);
c906108c
SS
3995
3996 return retval;
3997}
3998
77b06cd7 3999static int
fba45db2 4000debug_to_insert_vfork_catchpoint (int pid)
c906108c 4001{
77b06cd7
TJB
4002 int retval;
4003
4004 retval = debug_target.to_insert_vfork_catchpoint (pid);
c906108c 4005
77b06cd7
TJB
4006 fprintf_unfiltered (gdb_stdlog, "target_insert_vfork_catchpoint (%d) = %d\n",
4007 pid, retval);
4008
4009 return retval;
c906108c
SS
4010}
4011
4012static int
fba45db2 4013debug_to_remove_vfork_catchpoint (int pid)
c906108c 4014{
c5aa993b 4015 int retval;
c906108c
SS
4016
4017 retval = debug_target.to_remove_vfork_catchpoint (pid);
4018
96baa820 4019 fprintf_unfiltered (gdb_stdlog, "target_remove_vfork_catchpoint (%d) = %d\n",
c5aa993b 4020 pid, retval);
c906108c
SS
4021
4022 return retval;
4023}
4024
77b06cd7 4025static int
fba45db2 4026debug_to_insert_exec_catchpoint (int pid)
c906108c 4027{
77b06cd7
TJB
4028 int retval;
4029
4030 retval = debug_target.to_insert_exec_catchpoint (pid);
c906108c 4031
77b06cd7
TJB
4032 fprintf_unfiltered (gdb_stdlog, "target_insert_exec_catchpoint (%d) = %d\n",
4033 pid, retval);
4034
4035 return retval;
c906108c
SS
4036}
4037
4038static int
fba45db2 4039debug_to_remove_exec_catchpoint (int pid)
c906108c 4040{
c5aa993b 4041 int retval;
c906108c
SS
4042
4043 retval = debug_target.to_remove_exec_catchpoint (pid);
4044
96baa820 4045 fprintf_unfiltered (gdb_stdlog, "target_remove_exec_catchpoint (%d) = %d\n",
c5aa993b 4046 pid, retval);
c906108c
SS
4047
4048 return retval;
4049}
4050
c906108c 4051static int
fba45db2 4052debug_to_has_exited (int pid, int wait_status, int *exit_status)
c906108c 4053{
c5aa993b 4054 int has_exited;
c906108c
SS
4055
4056 has_exited = debug_target.to_has_exited (pid, wait_status, exit_status);
4057
96baa820 4058 fprintf_unfiltered (gdb_stdlog, "target_has_exited (%d, %d, %d) = %d\n",
c5aa993b 4059 pid, wait_status, *exit_status, has_exited);
c906108c
SS
4060
4061 return has_exited;
4062}
4063
c906108c 4064static int
fba45db2 4065debug_to_can_run (void)
c906108c
SS
4066{
4067 int retval;
4068
4069 retval = debug_target.to_can_run ();
4070
96baa820 4071 fprintf_unfiltered (gdb_stdlog, "target_can_run () = %d\n", retval);
c906108c
SS
4072
4073 return retval;
4074}
4075
c2250ad1
UW
4076static struct gdbarch *
4077debug_to_thread_architecture (struct target_ops *ops, ptid_t ptid)
4078{
4079 struct gdbarch *retval;
4080
4081 retval = debug_target.to_thread_architecture (ops, ptid);
4082
3e43a32a
MS
4083 fprintf_unfiltered (gdb_stdlog,
4084 "target_thread_architecture (%s) = %s [%s]\n",
4085 target_pid_to_str (ptid),
4086 host_address_to_string (retval),
c2250ad1
UW
4087 gdbarch_bfd_arch_info (retval)->printable_name);
4088 return retval;
4089}
4090
c906108c 4091static void
94cc34af 4092debug_to_stop (ptid_t ptid)
c906108c 4093{
94cc34af 4094 debug_target.to_stop (ptid);
c906108c 4095
94cc34af
PA
4096 fprintf_unfiltered (gdb_stdlog, "target_stop (%s)\n",
4097 target_pid_to_str (ptid));
c906108c
SS
4098}
4099
96baa820
JM
4100static void
4101debug_to_rcmd (char *command,
d9fcf2fb 4102 struct ui_file *outbuf)
96baa820
JM
4103{
4104 debug_target.to_rcmd (command, outbuf);
4105 fprintf_unfiltered (gdb_stdlog, "target_rcmd (%s, ...)\n", command);
4106}
4107
c906108c 4108static char *
fba45db2 4109debug_to_pid_to_exec_file (int pid)
c906108c 4110{
c5aa993b 4111 char *exec_file;
c906108c
SS
4112
4113 exec_file = debug_target.to_pid_to_exec_file (pid);
4114
96baa820 4115 fprintf_unfiltered (gdb_stdlog, "target_pid_to_exec_file (%d) = %s\n",
c5aa993b 4116 pid, exec_file);
c906108c
SS
4117
4118 return exec_file;
4119}
4120
c906108c 4121static void
fba45db2 4122setup_target_debug (void)
c906108c
SS
4123{
4124 memcpy (&debug_target, &current_target, sizeof debug_target);
4125
4126 current_target.to_open = debug_to_open;
c906108c 4127 current_target.to_post_attach = debug_to_post_attach;
c906108c 4128 current_target.to_prepare_to_store = debug_to_prepare_to_store;
c8e73a31 4129 current_target.deprecated_xfer_memory = deprecated_debug_xfer_memory;
c906108c
SS
4130 current_target.to_files_info = debug_to_files_info;
4131 current_target.to_insert_breakpoint = debug_to_insert_breakpoint;
4132 current_target.to_remove_breakpoint = debug_to_remove_breakpoint;
ccaa32c7
GS
4133 current_target.to_can_use_hw_breakpoint = debug_to_can_use_hw_breakpoint;
4134 current_target.to_insert_hw_breakpoint = debug_to_insert_hw_breakpoint;
4135 current_target.to_remove_hw_breakpoint = debug_to_remove_hw_breakpoint;
4136 current_target.to_insert_watchpoint = debug_to_insert_watchpoint;
4137 current_target.to_remove_watchpoint = debug_to_remove_watchpoint;
4138 current_target.to_stopped_by_watchpoint = debug_to_stopped_by_watchpoint;
4139 current_target.to_stopped_data_address = debug_to_stopped_data_address;
3e43a32a
MS
4140 current_target.to_watchpoint_addr_within_range
4141 = debug_to_watchpoint_addr_within_range;
4142 current_target.to_region_ok_for_hw_watchpoint
4143 = debug_to_region_ok_for_hw_watchpoint;
4144 current_target.to_can_accel_watchpoint_condition
4145 = debug_to_can_accel_watchpoint_condition;
c906108c
SS
4146 current_target.to_terminal_init = debug_to_terminal_init;
4147 current_target.to_terminal_inferior = debug_to_terminal_inferior;
3e43a32a
MS
4148 current_target.to_terminal_ours_for_output
4149 = debug_to_terminal_ours_for_output;
c906108c 4150 current_target.to_terminal_ours = debug_to_terminal_ours;
a790ad35 4151 current_target.to_terminal_save_ours = debug_to_terminal_save_ours;
c906108c 4152 current_target.to_terminal_info = debug_to_terminal_info;
c906108c 4153 current_target.to_load = debug_to_load;
c906108c 4154 current_target.to_post_startup_inferior = debug_to_post_startup_inferior;
c906108c
SS
4155 current_target.to_insert_fork_catchpoint = debug_to_insert_fork_catchpoint;
4156 current_target.to_remove_fork_catchpoint = debug_to_remove_fork_catchpoint;
4157 current_target.to_insert_vfork_catchpoint = debug_to_insert_vfork_catchpoint;
4158 current_target.to_remove_vfork_catchpoint = debug_to_remove_vfork_catchpoint;
c906108c
SS
4159 current_target.to_insert_exec_catchpoint = debug_to_insert_exec_catchpoint;
4160 current_target.to_remove_exec_catchpoint = debug_to_remove_exec_catchpoint;
c906108c 4161 current_target.to_has_exited = debug_to_has_exited;
c906108c 4162 current_target.to_can_run = debug_to_can_run;
c906108c 4163 current_target.to_stop = debug_to_stop;
96baa820 4164 current_target.to_rcmd = debug_to_rcmd;
c906108c 4165 current_target.to_pid_to_exec_file = debug_to_pid_to_exec_file;
c2250ad1 4166 current_target.to_thread_architecture = debug_to_thread_architecture;
c906108c 4167}
c906108c 4168\f
c5aa993b
JM
4169
4170static char targ_desc[] =
3e43a32a
MS
4171"Names of targets and files being debugged.\nShows the entire \
4172stack of targets currently in use (including the exec-file,\n\
c906108c
SS
4173core-file, and process, if any), as well as the symbol file name.";
4174
96baa820
JM
4175static void
4176do_monitor_command (char *cmd,
4177 int from_tty)
4178{
2b5fe715
AC
4179 if ((current_target.to_rcmd
4180 == (void (*) (char *, struct ui_file *)) tcomplain)
96baa820 4181 || (current_target.to_rcmd == debug_to_rcmd
2b5fe715
AC
4182 && (debug_target.to_rcmd
4183 == (void (*) (char *, struct ui_file *)) tcomplain)))
8a3fe4f8 4184 error (_("\"monitor\" command not supported by this target."));
96baa820
JM
4185 target_rcmd (cmd, gdb_stdtarg);
4186}
4187
87680a14
JB
4188/* Print the name of each layers of our target stack. */
4189
4190static void
4191maintenance_print_target_stack (char *cmd, int from_tty)
4192{
4193 struct target_ops *t;
4194
4195 printf_filtered (_("The current target stack is:\n"));
4196
4197 for (t = target_stack; t != NULL; t = t->beneath)
4198 {
4199 printf_filtered (" - %s (%s)\n", t->to_shortname, t->to_longname);
4200 }
4201}
4202
c6ebd6cf
VP
4203/* Controls if async mode is permitted. */
4204int target_async_permitted = 0;
4205
4206/* The set command writes to this variable. If the inferior is
4207 executing, linux_nat_async_permitted is *not* updated. */
4208static int target_async_permitted_1 = 0;
4209
4210static void
4211set_maintenance_target_async_permitted (char *args, int from_tty,
4212 struct cmd_list_element *c)
4213{
c35b1492 4214 if (have_live_inferiors ())
c6ebd6cf
VP
4215 {
4216 target_async_permitted_1 = target_async_permitted;
4217 error (_("Cannot change this setting while the inferior is running."));
4218 }
4219
4220 target_async_permitted = target_async_permitted_1;
4221}
4222
4223static void
4224show_maintenance_target_async_permitted (struct ui_file *file, int from_tty,
4225 struct cmd_list_element *c,
4226 const char *value)
4227{
3e43a32a
MS
4228 fprintf_filtered (file,
4229 _("Controlling the inferior in "
4230 "asynchronous mode is %s.\n"), value);
c6ebd6cf
VP
4231}
4232
d914c394
SS
4233/* Temporary copies of permission settings. */
4234
4235static int may_write_registers_1 = 1;
4236static int may_write_memory_1 = 1;
4237static int may_insert_breakpoints_1 = 1;
4238static int may_insert_tracepoints_1 = 1;
4239static int may_insert_fast_tracepoints_1 = 1;
4240static int may_stop_1 = 1;
4241
4242/* Make the user-set values match the real values again. */
4243
4244void
4245update_target_permissions (void)
4246{
4247 may_write_registers_1 = may_write_registers;
4248 may_write_memory_1 = may_write_memory;
4249 may_insert_breakpoints_1 = may_insert_breakpoints;
4250 may_insert_tracepoints_1 = may_insert_tracepoints;
4251 may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
4252 may_stop_1 = may_stop;
4253}
4254
4255/* The one function handles (most of) the permission flags in the same
4256 way. */
4257
4258static void
4259set_target_permissions (char *args, int from_tty,
4260 struct cmd_list_element *c)
4261{
4262 if (target_has_execution)
4263 {
4264 update_target_permissions ();
4265 error (_("Cannot change this setting while the inferior is running."));
4266 }
4267
4268 /* Make the real values match the user-changed values. */
4269 may_write_registers = may_write_registers_1;
4270 may_insert_breakpoints = may_insert_breakpoints_1;
4271 may_insert_tracepoints = may_insert_tracepoints_1;
4272 may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
4273 may_stop = may_stop_1;
4274 update_observer_mode ();
4275}
4276
4277/* Set memory write permission independently of observer mode. */
4278
4279static void
4280set_write_memory_permission (char *args, int from_tty,
4281 struct cmd_list_element *c)
4282{
4283 /* Make the real values match the user-changed values. */
4284 may_write_memory = may_write_memory_1;
4285 update_observer_mode ();
4286}
4287
4288
c906108c 4289void
fba45db2 4290initialize_targets (void)
c906108c
SS
4291{
4292 init_dummy_target ();
4293 push_target (&dummy_target);
4294
4295 add_info ("target", target_info, targ_desc);
4296 add_info ("files", target_info, targ_desc);
4297
85c07804
AC
4298 add_setshow_zinteger_cmd ("target", class_maintenance, &targetdebug, _("\
4299Set target debugging."), _("\
4300Show target debugging."), _("\
333dabeb
DJ
4301When non-zero, target debugging is enabled. Higher numbers are more\n\
4302verbose. Changes do not take effect until the next \"run\" or \"target\"\n\
85c07804
AC
4303command."),
4304 NULL,
920d2a44 4305 show_targetdebug,
85c07804 4306 &setdebuglist, &showdebuglist);
3a11626d 4307
2bc416ba 4308 add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
7915a72c
AC
4309 &trust_readonly, _("\
4310Set mode for reading from readonly sections."), _("\
4311Show mode for reading from readonly sections."), _("\
3a11626d
MS
4312When this mode is on, memory reads from readonly sections (such as .text)\n\
4313will be read from the object file instead of from the target. This will\n\
7915a72c 4314result in significant performance improvement for remote targets."),
2c5b56ce 4315 NULL,
920d2a44 4316 show_trust_readonly,
e707bbc2 4317 &setlist, &showlist);
96baa820
JM
4318
4319 add_com ("monitor", class_obscure, do_monitor_command,
1bedd215 4320 _("Send a command to the remote monitor (remote targets only)."));
96baa820 4321
87680a14
JB
4322 add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
4323 _("Print the name of each layer of the internal target stack."),
4324 &maintenanceprintlist);
4325
c6ebd6cf
VP
4326 add_setshow_boolean_cmd ("target-async", no_class,
4327 &target_async_permitted_1, _("\
4328Set whether gdb controls the inferior in asynchronous mode."), _("\
4329Show whether gdb controls the inferior in asynchronous mode."), _("\
4330Tells gdb whether to control the inferior in asynchronous mode."),
4331 set_maintenance_target_async_permitted,
4332 show_maintenance_target_async_permitted,
4333 &setlist,
4334 &showlist);
4335
4e5d721f 4336 add_setshow_boolean_cmd ("stack-cache", class_support,
9cf1b572 4337 &stack_cache_enabled_p_1, _("\
4e5d721f
DE
4338Set cache use for stack access."), _("\
4339Show cache use for stack access."), _("\
4340When on, use the data cache for all stack access, regardless of any\n\
4341configured memory regions. This improves remote performance significantly.\n\
4342By default, caching for stack access is on."),
4343 set_stack_cache_enabled_p,
4344 show_stack_cache_enabled_p,
4345 &setlist, &showlist);
4346
d914c394
SS
4347 add_setshow_boolean_cmd ("may-write-registers", class_support,
4348 &may_write_registers_1, _("\
4349Set permission to write into registers."), _("\
4350Show permission to write into registers."), _("\
4351When this permission is on, GDB may write into the target's registers.\n\
4352Otherwise, any sort of write attempt will result in an error."),
4353 set_target_permissions, NULL,
4354 &setlist, &showlist);
4355
4356 add_setshow_boolean_cmd ("may-write-memory", class_support,
4357 &may_write_memory_1, _("\
4358Set permission to write into target memory."), _("\
4359Show permission to write into target memory."), _("\
4360When this permission is on, GDB may write into the target's memory.\n\
4361Otherwise, any sort of write attempt will result in an error."),
4362 set_write_memory_permission, NULL,
4363 &setlist, &showlist);
4364
4365 add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
4366 &may_insert_breakpoints_1, _("\
4367Set permission to insert breakpoints in the target."), _("\
4368Show permission to insert breakpoints in the target."), _("\
4369When this permission is on, GDB may insert breakpoints in the program.\n\
4370Otherwise, any sort of insertion attempt will result in an error."),
4371 set_target_permissions, NULL,
4372 &setlist, &showlist);
4373
4374 add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
4375 &may_insert_tracepoints_1, _("\
4376Set permission to insert tracepoints in the target."), _("\
4377Show permission to insert tracepoints in the target."), _("\
4378When this permission is on, GDB may insert tracepoints in the program.\n\
4379Otherwise, any sort of insertion attempt will result in an error."),
4380 set_target_permissions, NULL,
4381 &setlist, &showlist);
4382
4383 add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
4384 &may_insert_fast_tracepoints_1, _("\
4385Set permission to insert fast tracepoints in the target."), _("\
4386Show permission to insert fast tracepoints in the target."), _("\
4387When this permission is on, GDB may insert fast tracepoints.\n\
4388Otherwise, any sort of insertion attempt will result in an error."),
4389 set_target_permissions, NULL,
4390 &setlist, &showlist);
4391
4392 add_setshow_boolean_cmd ("may-interrupt", class_support,
4393 &may_stop_1, _("\
4394Set permission to interrupt or signal the target."), _("\
4395Show permission to interrupt or signal the target."), _("\
4396When this permission is on, GDB may interrupt/stop the target's execution.\n\
4397Otherwise, any attempt to interrupt or stop will be ignored."),
4398 set_target_permissions, NULL,
4399 &setlist, &showlist);
4400
4401
8add0441 4402 target_dcache = dcache_init ();
c906108c 4403}
This page took 1.501946 seconds and 4 git commands to generate.