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