OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.1/] [gdb/] [target.c] - Blame information for rev 294

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.