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