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