1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h> /* for mode_t */
25 #include "target/target.h"
26 #include "target/resume.h"
27 #include "target/wait.h"
28 #include "target/waitstatus.h"
29 #include "mem-break.h"
30 #include "btrace-common.h"
36 /* This structure describes how to resume a particular thread (or all
37 threads) based on the client's request. If thread is -1, then this
38 entry applies to all threads. These are passed around as an
45 /* How to "resume". */
46 enum resume_kind kind;
48 /* If non-zero, send this signal when we resume, or to stop the
49 thread. If stopping a thread, and this is 0, the target should
50 stop the thread however it best decides to (e.g., SIGSTOP on
51 linux; SuspendThread on win32). This is a host signal value (not
55 /* Range to single step within. Valid only iff KIND is resume_step.
57 Single-step once, and then continuing stepping as long as the
58 thread stops in this range. (If the range is empty
59 [STEP_RANGE_START == STEP_RANGE_END], then this is a single-step
61 CORE_ADDR step_range_start; /* Inclusive */
62 CORE_ADDR step_range_end; /* Exclusive */
67 /* Start a new process.
69 PROGRAM is a path to the program to execute.
70 ARGS is a standard NULL-terminated array of arguments,
71 to be passed to the inferior as ``argv''.
73 Returns the new PID on success, -1 on failure. Registers the new
74 process with the process list. */
76 int (*create_inferior) (char *program, char **args);
78 /* Do additional setup after a new process is created, including
79 exec-wrapper completion. */
80 void (*post_create_inferior) (void);
82 /* Attach to a running process.
84 PID is the process ID to attach to, specified by the user
87 Returns -1 if attaching is unsupported, 0 on success, and calls
90 int (*attach) (unsigned long pid);
92 /* Kill inferior PID. Return -1 on failure, and 0 on success. */
94 int (*kill) (int pid);
96 /* Detach from inferior PID. Return -1 on failure, and 0 on
99 int (*detach) (int pid);
101 /* The inferior process has died. Do what is right. */
103 void (*mourn) (struct process_info *proc);
105 /* Wait for inferior PID to exit. */
106 void (*join) (int pid);
108 /* Return 1 iff the thread with process ID PID is alive. */
110 int (*thread_alive) (ptid_t pid);
112 /* Resume the inferior process. */
114 void (*resume) (struct thread_resume *resume_info, size_t n);
116 /* Wait for the inferior process or thread to change state. Store
117 status through argument pointer STATUS.
119 PTID = -1 to wait for any pid to do something, PTID(pid,0,0) to
120 wait for any thread of process pid to do something. Return ptid
121 of child, or -1 in case of error; store status through argument
122 pointer STATUS. OPTIONS is a bit set of options defined as
123 TARGET_W* above. If options contains TARGET_WNOHANG and there's
124 no child stop to report, return is
125 null_ptid/TARGET_WAITKIND_IGNORE. */
127 ptid_t (*wait) (ptid_t ptid, struct target_waitstatus *status, int options);
129 /* Fetch registers from the inferior process.
131 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
133 void (*fetch_registers) (struct regcache *regcache, int regno);
135 /* Store registers to the inferior process.
137 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
139 void (*store_registers) (struct regcache *regcache, int regno);
141 /* Prepare to read or write memory from the inferior process.
142 Targets use this to do what is necessary to get the state of the
143 inferior such that it is possible to access memory.
145 This should generally only be called from client facing routines,
146 such as gdb_read_memory/gdb_write_memory, or the GDB breakpoint
149 Like `read_memory' and `write_memory' below, returns 0 on success
150 and errno on failure. */
152 int (*prepare_to_access_memory) (void);
154 /* Undo the effects of prepare_to_access_memory. */
156 void (*done_accessing_memory) (void);
158 /* Read memory from the inferior process. This should generally be
159 called through read_inferior_memory, which handles breakpoint shadowing.
161 Read LEN bytes at MEMADDR into a buffer at MYADDR.
163 Returns 0 on success and errno on failure. */
165 int (*read_memory) (CORE_ADDR memaddr, unsigned char *myaddr, int len);
167 /* Write memory to the inferior process. This should generally be
168 called through write_inferior_memory, which handles breakpoint shadowing.
170 Write LEN bytes from the buffer at MYADDR to MEMADDR.
172 Returns 0 on success and errno on failure. */
174 int (*write_memory) (CORE_ADDR memaddr, const unsigned char *myaddr,
177 /* Query GDB for the values of any symbols we're interested in.
178 This function is called whenever we receive a "qSymbols::"
179 query, which corresponds to every time more symbols (might)
180 become available. NULL if we aren't interested in any
183 void (*look_up_symbols) (void);
185 /* Send an interrupt request to the inferior process,
186 however is appropriate. */
188 void (*request_interrupt) (void);
190 /* Read auxiliary vector data from the inferior process.
192 Read LEN bytes at OFFSET into a buffer at MYADDR. */
194 int (*read_auxv) (CORE_ADDR offset, unsigned char *myaddr,
197 /* Returns true if GDB Z breakpoint type TYPE is supported, false
198 otherwise. The type is coded as follows:
199 '0' - software-breakpoint
200 '1' - hardware-breakpoint
201 '2' - write watchpoint
202 '3' - read watchpoint
203 '4' - access watchpoint
205 int (*supports_z_point_type) (char z_type);
207 /* Insert and remove a break or watchpoint.
208 Returns 0 on success, -1 on failure and 1 on unsupported. */
210 int (*insert_point) (enum raw_bkpt_type type, CORE_ADDR addr,
211 int size, struct raw_breakpoint *bp);
212 int (*remove_point) (enum raw_bkpt_type type, CORE_ADDR addr,
213 int size, struct raw_breakpoint *bp);
215 /* Returns 1 if the target stopped because it executed a software
216 breakpoint instruction, 0 otherwise. */
217 int (*stopped_by_sw_breakpoint) (void);
219 /* Returns true if the target knows whether a trap was caused by a
220 SW breakpoint triggering. */
221 int (*supports_stopped_by_sw_breakpoint) (void);
223 /* Returns 1 if the target stopped for a hardware breakpoint. */
224 int (*stopped_by_hw_breakpoint) (void);
226 /* Returns true if the target knows whether a trap was caused by a
227 HW breakpoint triggering. */
228 int (*supports_stopped_by_hw_breakpoint) (void);
230 /* Returns true if the target can do hardware single step. */
231 int (*supports_hardware_single_step) (void);
233 /* Returns 1 if target was stopped due to a watchpoint hit, 0 otherwise. */
235 int (*stopped_by_watchpoint) (void);
237 /* Returns the address associated with the watchpoint that hit, if any;
238 returns 0 otherwise. */
240 CORE_ADDR (*stopped_data_address) (void);
242 /* Reports the text, data offsets of the executable. This is
243 needed for uclinux where the executable is relocated during load
246 int (*read_offsets) (CORE_ADDR *text, CORE_ADDR *data);
248 /* Fetch the address associated with a specific thread local storage
249 area, determined by the specified THREAD, OFFSET, and LOAD_MODULE.
250 Stores it in *ADDRESS and returns zero on success; otherwise returns
251 an error code. A return value of -1 means this system does not
252 support the operation. */
254 int (*get_tls_address) (struct thread_info *thread, CORE_ADDR offset,
255 CORE_ADDR load_module, CORE_ADDR *address);
257 /* Read/Write from/to spufs using qXfer packets. */
258 int (*qxfer_spu) (const char *annex, unsigned char *readbuf,
259 unsigned const char *writebuf, CORE_ADDR offset, int len);
261 /* Fill BUF with an hostio error packet representing the last hostio
263 void (*hostio_last_error) (char *buf);
265 /* Read/Write OS data using qXfer packets. */
266 int (*qxfer_osdata) (const char *annex, unsigned char *readbuf,
267 unsigned const char *writebuf, CORE_ADDR offset,
270 /* Read/Write extra signal info. */
271 int (*qxfer_siginfo) (const char *annex, unsigned char *readbuf,
272 unsigned const char *writebuf,
273 CORE_ADDR offset, int len);
275 int (*supports_non_stop) (void);
277 /* Enables async target events. Returns the previous enable
279 int (*async) (int enable);
281 /* Switch to non-stop (1) or all-stop (0) mode. Return 0 on
282 success, -1 otherwise. */
283 int (*start_non_stop) (int);
285 /* Returns true if the target supports multi-process debugging. */
286 int (*supports_multi_process) (void);
288 /* Returns true if fork events are supported. */
289 int (*supports_fork_events) (void);
291 /* Returns true if vfork events are supported. */
292 int (*supports_vfork_events) (void);
294 /* Returns true if exec events are supported. */
295 int (*supports_exec_events) (void);
297 /* Allows target to re-initialize connection-specific settings. */
298 void (*handle_new_gdb_connection) (void);
300 /* If not NULL, target-specific routine to process monitor command.
301 Returns 1 if handled, or 0 to perform default processing. */
302 int (*handle_monitor_command) (char *);
304 /* Returns the core given a thread, or -1 if not known. */
305 int (*core_of_thread) (ptid_t);
307 /* Read loadmaps. Read LEN bytes at OFFSET into a buffer at MYADDR. */
308 int (*read_loadmap) (const char *annex, CORE_ADDR offset,
309 unsigned char *myaddr, unsigned int len);
311 /* Target specific qSupported support. FEATURES is an array of
312 features with COUNT elements. */
313 void (*process_qsupported) (char **features, int count);
315 /* Return 1 if the target supports tracepoints, 0 (or leave the
316 callback NULL) otherwise. */
317 int (*supports_tracepoints) (void);
319 /* Read PC from REGCACHE. */
320 CORE_ADDR (*read_pc) (struct regcache *regcache);
322 /* Write PC to REGCACHE. */
323 void (*write_pc) (struct regcache *regcache, CORE_ADDR pc);
325 /* Return true if THREAD is known to be stopped now. */
326 int (*thread_stopped) (struct thread_info *thread);
328 /* Read Thread Information Block address. */
329 int (*get_tib_address) (ptid_t ptid, CORE_ADDR *address);
331 /* Pause all threads. If FREEZE, arrange for any resume attempt to
332 be ignored until an unpause_all call unfreezes threads again.
333 There can be nested calls to pause_all, so a freeze counter
334 should be maintained. */
335 void (*pause_all) (int freeze);
337 /* Unpause all threads. Threads that hadn't been resumed by the
338 client should be left stopped. Basically a pause/unpause call
339 pair should not end up resuming threads that were stopped before
341 void (*unpause_all) (int unfreeze);
343 /* Stabilize all threads. That is, force them out of jump pads. */
344 void (*stabilize_threads) (void);
346 /* Install a fast tracepoint jump pad. TPOINT is the address of the
347 tracepoint internal object as used by the IPA agent. TPADDR is
348 the address of tracepoint. COLLECTOR is address of the function
349 the jump pad redirects to. LOCKADDR is the address of the jump
350 pad lock object. ORIG_SIZE is the size in bytes of the
351 instruction at TPADDR. JUMP_ENTRY points to the address of the
352 jump pad entry, and on return holds the address past the end of
353 the created jump pad. If a trampoline is created by the function,
354 then TRAMPOLINE and TRAMPOLINE_SIZE return the address and size of
355 the trampoline, else they remain unchanged. JJUMP_PAD_INSN is a
356 buffer containing a copy of the instruction at TPADDR.
357 ADJUST_INSN_ADDR and ADJUST_INSN_ADDR_END are output parameters that
358 return the address range where the instruction at TPADDR was relocated
359 to. If an error occurs, the ERR may be used to pass on an error
361 int (*install_fast_tracepoint_jump_pad) (CORE_ADDR tpoint, CORE_ADDR tpaddr,
365 CORE_ADDR *jump_entry,
366 CORE_ADDR *trampoline,
367 ULONGEST *trampoline_size,
368 unsigned char *jjump_pad_insn,
369 ULONGEST *jjump_pad_insn_size,
370 CORE_ADDR *adjusted_insn_addr,
371 CORE_ADDR *adjusted_insn_addr_end,
374 /* Return the bytecode operations vector for the current inferior.
375 Returns NULL if bytecode compilation is not supported. */
376 struct emit_ops *(*emit_ops) (void);
378 /* Returns true if the target supports disabling randomization. */
379 int (*supports_disable_randomization) (void);
381 /* Return the minimum length of an instruction that can be safely overwritten
382 for use as a fast tracepoint. */
383 int (*get_min_fast_tracepoint_insn_len) (void);
385 /* Read solib info on SVR4 platforms. */
386 int (*qxfer_libraries_svr4) (const char *annex, unsigned char *readbuf,
387 unsigned const char *writebuf,
388 CORE_ADDR offset, int len);
390 /* Return true if target supports debugging agent. */
391 int (*supports_agent) (void);
393 /* Check whether the target supports branch tracing. */
394 int (*supports_btrace) (struct target_ops *, enum btrace_format);
396 /* Enable branch tracing for PTID based on CONF and allocate a branch trace
397 target information struct for reading and for disabling branch trace. */
398 struct btrace_target_info *(*enable_btrace)
399 (ptid_t ptid, const struct btrace_config *conf);
401 /* Disable branch tracing.
402 Returns zero on success, non-zero otherwise. */
403 int (*disable_btrace) (struct btrace_target_info *tinfo);
405 /* Read branch trace data into buffer.
406 Return 0 on success; print an error message into BUFFER and return -1,
408 int (*read_btrace) (struct btrace_target_info *, struct buffer *,
409 enum btrace_read_type type);
411 /* Read the branch trace configuration into BUFFER.
412 Return 0 on success; print an error message into BUFFER and return -1
414 int (*read_btrace_conf) (const struct btrace_target_info *, struct buffer *);
416 /* Return true if target supports range stepping. */
417 int (*supports_range_stepping) (void);
419 /* Return the full absolute name of the executable file that was
420 run to create the process PID. If the executable file cannot
421 be determined, NULL is returned. Otherwise, a pointer to a
422 character string containing the pathname is returned. This
423 string should be copied into a buffer by the client if the string
424 will not be immediately used, or if it must persist. */
425 char *(*pid_to_exec_file) (int pid);
427 /* Multiple-filesystem-aware open. Like open(2), but operating in
428 the filesystem as it appears to process PID. Systems where all
429 processes share a common filesystem should set this to NULL.
430 If NULL, the caller should fall back to open(2). */
431 int (*multifs_open) (int pid, const char *filename,
432 int flags, mode_t mode);
434 /* Multiple-filesystem-aware unlink. Like unlink(2), but operates
435 in the filesystem as it appears to process PID. Systems where
436 all processes share a common filesystem should set this to NULL.
437 If NULL, the caller should fall back to unlink(2). */
438 int (*multifs_unlink) (int pid, const char *filename);
440 /* Multiple-filesystem-aware readlink. Like readlink(2), but
441 operating in the filesystem as it appears to process PID.
442 Systems where all processes share a common filesystem should
443 set this to NULL. If NULL, the caller should fall back to
445 ssize_t (*multifs_readlink) (int pid, const char *filename,
446 char *buf, size_t bufsiz);
448 /* Return the breakpoint kind for this target based on PC. The PCPTR is
449 adjusted to the real memory location in case a flag (e.g., the Thumb bit on
450 ARM) was present in the PC. */
451 int (*breakpoint_kind_from_pc) (CORE_ADDR *pcptr);
453 /* Return the software breakpoint from KIND. KIND can have target
454 specific meaning like the Z0 kind parameter.
455 SIZE is set to the software breakpoint's length in memory. */
456 const gdb_byte *(*sw_breakpoint_from_kind) (int kind, int *size);
458 /* Return the thread's name, or NULL if the target is unable to determine it.
459 The returned value must not be freed by the caller. */
460 const char *(*thread_name) (ptid_t thread);
462 /* Return the breakpoint kind for this target based on the current
463 processor state (e.g. the current instruction mode on ARM) and the
464 PC. The PCPTR is adjusted to the real memory location in case a flag
465 (e.g., the Thumb bit on ARM) is present in the PC. */
466 int (*breakpoint_kind_from_current_state) (CORE_ADDR *pcptr);
468 /* Returns true if the target can software single step. */
469 int (*supports_software_single_step) (void);
472 extern struct target_ops *the_target;
474 void set_target_ops (struct target_ops *);
476 #define create_inferior(program, args) \
477 (*the_target->create_inferior) (program, args)
479 #define target_post_create_inferior() \
482 if (the_target->post_create_inferior != NULL) \
483 (*the_target->post_create_inferior) (); \
486 #define myattach(pid) \
487 (*the_target->attach) (pid)
489 int kill_inferior (int);
491 #define target_supports_fork_events() \
492 (the_target->supports_fork_events ? \
493 (*the_target->supports_fork_events) () : 0)
495 #define target_supports_vfork_events() \
496 (the_target->supports_vfork_events ? \
497 (*the_target->supports_vfork_events) () : 0)
499 #define target_supports_exec_events() \
500 (the_target->supports_exec_events ? \
501 (*the_target->supports_exec_events) () : 0)
503 #define target_handle_new_gdb_connection() \
506 if (the_target->handle_new_gdb_connection != NULL) \
507 (*the_target->handle_new_gdb_connection) (); \
510 #define detach_inferior(pid) \
511 (*the_target->detach) (pid)
513 #define mourn_inferior(PROC) \
514 (*the_target->mourn) (PROC)
516 #define mythread_alive(pid) \
517 (*the_target->thread_alive) (pid)
519 #define fetch_inferior_registers(regcache, regno) \
520 (*the_target->fetch_registers) (regcache, regno)
522 #define store_inferior_registers(regcache, regno) \
523 (*the_target->store_registers) (regcache, regno)
525 #define join_inferior(pid) \
526 (*the_target->join) (pid)
528 #define target_supports_non_stop() \
529 (the_target->supports_non_stop ? (*the_target->supports_non_stop ) () : 0)
531 #define target_async(enable) \
532 (the_target->async ? (*the_target->async) (enable) : 0)
534 #define target_supports_multi_process() \
535 (the_target->supports_multi_process ? \
536 (*the_target->supports_multi_process) () : 0)
538 #define target_process_qsupported(features, count) \
541 if (the_target->process_qsupported) \
542 the_target->process_qsupported (features, count); \
545 #define target_supports_tracepoints() \
546 (the_target->supports_tracepoints \
547 ? (*the_target->supports_tracepoints) () : 0)
549 #define target_supports_fast_tracepoints() \
550 (the_target->install_fast_tracepoint_jump_pad != NULL)
552 #define target_get_min_fast_tracepoint_insn_len() \
553 (the_target->get_min_fast_tracepoint_insn_len \
554 ? (*the_target->get_min_fast_tracepoint_insn_len) () : 0)
556 #define thread_stopped(thread) \
557 (*the_target->thread_stopped) (thread)
559 #define pause_all(freeze) \
562 if (the_target->pause_all) \
563 (*the_target->pause_all) (freeze); \
566 #define unpause_all(unfreeze) \
569 if (the_target->unpause_all) \
570 (*the_target->unpause_all) (unfreeze); \
573 #define stabilize_threads() \
576 if (the_target->stabilize_threads) \
577 (*the_target->stabilize_threads) (); \
580 #define install_fast_tracepoint_jump_pad(tpoint, tpaddr, \
581 collector, lockaddr, \
584 trampoline, trampoline_size, \
586 jjump_pad_insn_size, \
587 adjusted_insn_addr, \
588 adjusted_insn_addr_end, \
590 (*the_target->install_fast_tracepoint_jump_pad) (tpoint, tpaddr, \
591 collector,lockaddr, \
592 orig_size, jump_entry, \
596 jjump_pad_insn_size, \
597 adjusted_insn_addr, \
598 adjusted_insn_addr_end, \
601 #define target_emit_ops() \
602 (the_target->emit_ops ? (*the_target->emit_ops) () : NULL)
604 #define target_supports_disable_randomization() \
605 (the_target->supports_disable_randomization ? \
606 (*the_target->supports_disable_randomization) () : 0)
608 #define target_supports_agent() \
609 (the_target->supports_agent ? \
610 (*the_target->supports_agent) () : 0)
612 #define target_supports_btrace(format) \
613 (the_target->supports_btrace \
614 ? (*the_target->supports_btrace) (the_target, format) : 0)
616 #define target_enable_btrace(ptid, conf) \
617 (*the_target->enable_btrace) (ptid, conf)
619 #define target_disable_btrace(tinfo) \
620 (*the_target->disable_btrace) (tinfo)
622 #define target_read_btrace(tinfo, buffer, type) \
623 (*the_target->read_btrace) (tinfo, buffer, type)
625 #define target_read_btrace_conf(tinfo, buffer) \
626 (*the_target->read_btrace_conf) (tinfo, buffer)
628 #define target_supports_range_stepping() \
629 (the_target->supports_range_stepping ? \
630 (*the_target->supports_range_stepping) () : 0)
632 #define target_supports_stopped_by_sw_breakpoint() \
633 (the_target->supports_stopped_by_sw_breakpoint ? \
634 (*the_target->supports_stopped_by_sw_breakpoint) () : 0)
636 #define target_stopped_by_sw_breakpoint() \
637 (the_target->stopped_by_sw_breakpoint ? \
638 (*the_target->stopped_by_sw_breakpoint) () : 0)
640 #define target_supports_stopped_by_hw_breakpoint() \
641 (the_target->supports_stopped_by_hw_breakpoint ? \
642 (*the_target->supports_stopped_by_hw_breakpoint) () : 0)
644 #define target_supports_hardware_single_step() \
645 (the_target->supports_hardware_single_step ? \
646 (*the_target->supports_hardware_single_step) () : 0)
648 #define target_stopped_by_hw_breakpoint() \
649 (the_target->stopped_by_hw_breakpoint ? \
650 (*the_target->stopped_by_hw_breakpoint) () : 0)
652 #define target_breakpoint_kind_from_pc(pcptr) \
653 (the_target->breakpoint_kind_from_pc \
654 ? (*the_target->breakpoint_kind_from_pc) (pcptr) \
655 : default_breakpoint_kind_from_pc (pcptr))
657 #define target_breakpoint_kind_from_current_state(pcptr) \
658 (the_target->breakpoint_kind_from_current_state \
659 ? (*the_target->breakpoint_kind_from_current_state) (pcptr) \
660 : target_breakpoint_kind_from_pc (pcptr))
662 #define target_supports_software_single_step() \
663 (the_target->supports_software_single_step ? \
664 (*the_target->supports_software_single_step) () : 0)
666 /* Start non-stop mode, returns 0 on success, -1 on failure. */
668 int start_non_stop (int nonstop);
670 ptid_t mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
673 /* Prepare to read or write memory from the inferior process. See the
674 corresponding target_ops methods for more details. */
676 int prepare_to_access_memory (void);
677 void done_accessing_memory (void);
679 #define target_core_of_thread(ptid) \
680 (the_target->core_of_thread ? (*the_target->core_of_thread) (ptid) \
683 #define target_thread_name(ptid) \
684 (the_target->thread_name ? (*the_target->thread_name) (ptid) \
687 int read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len);
689 int write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
692 int set_desired_thread (int id);
694 const char *target_pid_to_str (ptid_t);
696 int target_can_do_hardware_single_step (void);
698 int default_breakpoint_kind_from_pc (CORE_ADDR *pcptr);
700 #endif /* TARGET_H */