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