linux-nat.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Native debugging support for GNU/Linux (LWP layer).
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef LINUX_NAT_H
  15. #define LINUX_NAT_H
  16. #include "nat/linux-nat.h"
  17. #include "inf-ptrace.h"
  18. #include "target.h"
  19. #include <signal.h>
  20. /* A prototype generic GNU/Linux target. A concrete instance should
  21. override it with local methods. */
  22. class linux_nat_target : public inf_ptrace_target
  23. {
  24. public:
  25. linux_nat_target ();
  26. ~linux_nat_target () override = 0;
  27. thread_control_capabilities get_thread_control_capabilities () override
  28. { return tc_schedlock; }
  29. void create_inferior (const char *, const std::string &,
  30. char **, int) override;
  31. void attach (const char *, int) override;
  32. void detach (inferior *, int) override;
  33. void resume (ptid_t, int, enum gdb_signal) override;
  34. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  35. void pass_signals (gdb::array_view<const unsigned char>) override;
  36. enum target_xfer_status xfer_partial (enum target_object object,
  37. const char *annex,
  38. gdb_byte *readbuf,
  39. const gdb_byte *writebuf,
  40. ULONGEST offset, ULONGEST len,
  41. ULONGEST *xfered_len) override;
  42. void kill () override;
  43. void mourn_inferior () override;
  44. bool thread_alive (ptid_t ptid) override;
  45. void update_thread_list () override;
  46. std::string pid_to_str (ptid_t) override;
  47. const char *thread_name (struct thread_info *) override;
  48. struct address_space *thread_address_space (ptid_t) override;
  49. bool stopped_by_watchpoint () override;
  50. bool stopped_data_address (CORE_ADDR *) override;
  51. bool stopped_by_sw_breakpoint () override;
  52. bool supports_stopped_by_sw_breakpoint () override;
  53. bool stopped_by_hw_breakpoint () override;
  54. bool supports_stopped_by_hw_breakpoint () override;
  55. void thread_events (int) override;
  56. bool can_async_p () override;
  57. bool supports_non_stop () override;
  58. bool always_non_stop_p () override;
  59. void async (int) override;
  60. void stop (ptid_t) override;
  61. bool supports_multi_process () override;
  62. bool supports_disable_randomization () override;
  63. int core_of_thread (ptid_t ptid) override;
  64. bool filesystem_is_local () override;
  65. int fileio_open (struct inferior *inf, const char *filename,
  66. int flags, int mode, int warn_if_slow,
  67. int *target_errno) override;
  68. gdb::optional<std::string>
  69. fileio_readlink (struct inferior *inf,
  70. const char *filename,
  71. int *target_errno) override;
  72. int fileio_unlink (struct inferior *inf,
  73. const char *filename,
  74. int *target_errno) override;
  75. int insert_fork_catchpoint (int) override;
  76. int remove_fork_catchpoint (int) override;
  77. int insert_vfork_catchpoint (int) override;
  78. int remove_vfork_catchpoint (int) override;
  79. int insert_exec_catchpoint (int) override;
  80. int remove_exec_catchpoint (int) override;
  81. int set_syscall_catchpoint (int pid, bool needed, int any_count,
  82. gdb::array_view<const int> syscall_counts) override;
  83. char *pid_to_exec_file (int pid) override;
  84. void post_attach (int) override;
  85. void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
  86. std::vector<static_tracepoint_marker>
  87. static_tracepoint_markers_by_strid (const char *id) override;
  88. /* Methods that are meant to overridden by the concrete
  89. arch-specific target instance. */
  90. virtual void low_resume (ptid_t ptid, int step, enum gdb_signal sig)
  91. { inf_ptrace_target::resume (ptid, step, sig); }
  92. virtual bool low_stopped_by_watchpoint ()
  93. { return false; }
  94. virtual bool low_stopped_data_address (CORE_ADDR *addr_p)
  95. { return false; }
  96. /* The method to call, if any, when a new thread is attached. */
  97. virtual void low_new_thread (struct lwp_info *)
  98. {}
  99. /* The method to call, if any, when a thread is destroyed. */
  100. virtual void low_delete_thread (struct arch_lwp_info *lp)
  101. {
  102. gdb_assert (lp == NULL);
  103. }
  104. /* The method to call, if any, when a new fork is attached. */
  105. virtual void low_new_fork (struct lwp_info *parent, pid_t child_pid)
  106. {}
  107. /* The method to call, if any, when a new clone event is detected. */
  108. virtual void low_new_clone (struct lwp_info *parent, pid_t child_lwp)
  109. {}
  110. /* The method to call, if any, when a process is no longer
  111. attached. */
  112. virtual void low_forget_process (pid_t pid)
  113. {}
  114. /* Hook to call prior to resuming a thread. */
  115. virtual void low_prepare_to_resume (struct lwp_info *)
  116. {}
  117. /* Convert a ptrace/host siginfo object, into/from the siginfo in
  118. the layout of the inferiors' architecture. Returns true if any
  119. conversion was done; false otherwise, in which case the caller
  120. does a straight memcpy. If DIRECTION is 1, then copy from INF to
  121. PTRACE. If DIRECTION is 0, copy from PTRACE to INF. */
  122. virtual bool low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf,
  123. int direction)
  124. { return false; }
  125. /* SIGTRAP-like breakpoint status events recognizer. The default
  126. recognizes SIGTRAP only. */
  127. virtual bool low_status_is_event (int status);
  128. protected:
  129. void post_startup_inferior (ptid_t) override;
  130. };
  131. /* The final/concrete instance. */
  132. extern linux_nat_target *linux_target;
  133. struct arch_lwp_info;
  134. /* Structure describing an LWP. */
  135. struct lwp_info : intrusive_list_node<lwp_info>
  136. {
  137. lwp_info (ptid_t ptid)
  138. : ptid (ptid)
  139. {}
  140. ~lwp_info ();
  141. DISABLE_COPY_AND_ASSIGN (lwp_info);
  142. /* The process id of the LWP. This is a combination of the LWP id
  143. and overall process id. */
  144. ptid_t ptid = null_ptid;
  145. /* If this flag is set, we need to set the event request flags the
  146. next time we see this LWP stop. */
  147. int must_set_ptrace_flags = 0;
  148. /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
  149. it back yet). */
  150. int signalled = 0;
  151. /* Non-zero if this LWP is stopped. */
  152. int stopped = 0;
  153. /* Non-zero if this LWP will be/has been resumed. Note that an LWP
  154. can be marked both as stopped and resumed at the same time. This
  155. happens if we try to resume an LWP that has a wait status
  156. pending. We shouldn't let the LWP run until that wait status has
  157. been processed, but we should not report that wait status if GDB
  158. didn't try to let the LWP run. */
  159. int resumed = 0;
  160. /* The last resume GDB requested on this thread. */
  161. resume_kind last_resume_kind = resume_continue;
  162. /* If non-zero, a pending wait status. */
  163. int status = 0;
  164. /* When 'stopped' is set, this is where the lwp last stopped, with
  165. decr_pc_after_break already accounted for. If the LWP is
  166. running and stepping, this is the address at which the lwp was
  167. resumed (that is, it's the previous stop PC). If the LWP is
  168. running and not stepping, this is 0. */
  169. CORE_ADDR stop_pc = 0;
  170. /* Non-zero if we were stepping this LWP. */
  171. int step = 0;
  172. /* The reason the LWP last stopped, if we need to track it
  173. (breakpoint, watchpoint, etc.). */
  174. target_stop_reason stop_reason = TARGET_STOPPED_BY_NO_REASON;
  175. /* On architectures where it is possible to know the data address of
  176. a triggered watchpoint, STOPPED_DATA_ADDRESS_P is non-zero, and
  177. STOPPED_DATA_ADDRESS contains such data address. Otherwise,
  178. STOPPED_DATA_ADDRESS_P is false, and STOPPED_DATA_ADDRESS is
  179. undefined. Only valid if STOPPED_BY_WATCHPOINT is true. */
  180. int stopped_data_address_p = 0;
  181. CORE_ADDR stopped_data_address = 0;
  182. /* Non-zero if we expect a duplicated SIGINT. */
  183. int ignore_sigint = 0;
  184. /* If WAITSTATUS->KIND != TARGET_WAITKIND_SPURIOUS, the waitstatus
  185. for this LWP's last event. This may correspond to STATUS above,
  186. or to a local variable in lin_lwp_wait. */
  187. struct target_waitstatus waitstatus;
  188. /* Signal whether we are in a SYSCALL_ENTRY or
  189. in a SYSCALL_RETURN event.
  190. Values:
  191. - TARGET_WAITKIND_SYSCALL_ENTRY
  192. - TARGET_WAITKIND_SYSCALL_RETURN */
  193. enum target_waitkind syscall_state;
  194. /* The processor core this LWP was last seen on. */
  195. int core = -1;
  196. /* Arch-specific additions. */
  197. struct arch_lwp_info *arch_private = nullptr;
  198. };
  199. /* lwp_info iterator and range types. */
  200. using lwp_info_iterator
  201. = reference_to_pointer_iterator<intrusive_list<lwp_info>::iterator>;
  202. using lwp_info_range = iterator_range<lwp_info_iterator>;
  203. using lwp_info_safe_range = basic_safe_range<lwp_info_range>;
  204. /* Get an iterable range over all lwps. */
  205. lwp_info_range all_lwps ();
  206. /* Same as the above, but safe against deletion while iterating. */
  207. lwp_info_safe_range all_lwps_safe ();
  208. /* Does the current host support PTRACE_GETREGSET? */
  209. extern enum tribool have_ptrace_getregset;
  210. /* Called from the LWP layer to inform the thread_db layer that PARENT
  211. spawned CHILD. Both LWPs are currently stopped. This function
  212. does whatever is required to have the child LWP under the
  213. thread_db's control --- e.g., enabling event reporting. Returns
  214. true on success, false if the process isn't using libpthread. */
  215. extern int thread_db_notice_clone (ptid_t parent, ptid_t child);
  216. /* Return the number of signals used by the threads library. */
  217. extern unsigned int lin_thread_get_thread_signal_num (void);
  218. /* Return the i-th signal used by the threads library. */
  219. extern int lin_thread_get_thread_signal (unsigned int i);
  220. /* Find process PID's pending signal set from /proc/pid/status. */
  221. void linux_proc_pending_signals (int pid, sigset_t *pending,
  222. sigset_t *blocked, sigset_t *ignored);
  223. /* For linux_stop_lwp see nat/linux-nat.h. */
  224. /* Stop all LWPs, synchronously. (Any events that trigger while LWPs
  225. are being stopped are left pending.) */
  226. extern void linux_stop_and_wait_all_lwps (void);
  227. /* Set resumed LWPs running again, as they were before being stopped
  228. with linux_stop_and_wait_all_lwps. (LWPS with pending events are
  229. left stopped.) */
  230. extern void linux_unstop_all_lwps (void);
  231. /* Update linux-nat internal state when changing from one fork
  232. to another. */
  233. void linux_nat_switch_fork (ptid_t new_ptid);
  234. /* Store the saved siginfo associated with PTID in *SIGINFO.
  235. Return 1 if it was retrieved successfully, 0 otherwise (*SIGINFO is
  236. uninitialized in such case). */
  237. int linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo);
  238. #endif /* LINUX_NAT_H */