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