Debian Codegraph @

gdbserver/gdbthread.h

raw
1 /* Multi-thread control defs for remote server for GDB.
2 Copyright (C) 1993-2025 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #ifndef GDBSERVER_GDBTHREAD_H
20 #define GDBSERVER_GDBTHREAD_H
21
22 #include "gdbsupport/function-view.h"
23 #include "gdbsupport/intrusive_list.h"
24 #include <memory>
25
26 struct btrace_target_info;
27 struct regcache;
28
29 struct thread_info : public intrusive_list_node<thread_info>
30 {
31 thread_info (ptid_t id, process_info *process, void *target_data)
32 : id (id), m_process (process), m_target_data (target_data)
33 {}
34
35 /* Return the process owning this thread. */
36 process_info *process () const
37 { return m_process; }
38
39 struct regcache *regcache ()
40 { return m_regcache.get (); }
41
42 void set_regcache (std::unique_ptr<struct regcache> regcache)
43 { m_regcache = std::move (regcache); }
44
45 void *target_data ()
46 { return m_target_data; }
47
48 /* The id of this thread. */
49 ptid_t id;
50
51 /* The last resume GDB requested on this thread. */
52 enum resume_kind last_resume_kind = resume_continue;
53
54 /* The last wait status reported for this thread. */
55 struct target_waitstatus last_status;
56
57 /* True if LAST_STATUS hasn't been reported to GDB yet. */
58 int status_pending_p = 0;
59
60 /* Given `while-stepping', a thread may be collecting data for more
61 than one tracepoint simultaneously. E.g.:
62
63 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
64 ff0002 INSN2
65 ff0003 INSN3 <-- TP2, collect $regs
66 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
67 ff0005 INSN5
68
69 Notice that when instruction INSN5 is reached, the while-stepping
70 actions of both TP1 and TP3 are still being collected, and that TP2
71 had been collected meanwhile. The whole range of ff0001-ff0005
72 should be single-stepped, due to at least TP1's while-stepping
73 action covering the whole range.
74
75 On the other hand, the same tracepoint with a while-stepping action
76 may be hit by more than one thread simultaneously, hence we can't
77 keep the current step count in the tracepoint itself.
78
79 This is the head of the list of the states of `while-stepping'
80 tracepoint actions this thread is now collecting; NULL if empty.
81 Each item in the list holds the current step of the while-stepping
82 action. */
83 struct wstep_state *while_stepping = nullptr;
84
85 /* Branch trace target information for this thread. */
86 struct btrace_target_info *btrace = nullptr;
87
88 /* Thread options GDB requested with QThreadOptions. */
89 gdb_thread_options thread_options = 0;
90
91 private:
92 process_info *m_process;
93 std::unique_ptr<struct regcache> m_regcache = nullptr;
94 void *m_target_data;
95 };
96
97 /* Return a pointer to the first thread, or NULL if there isn't one. */
98
99 thread_info *get_first_thread (void);
100
101 thread_info *find_thread_ptid (ptid_t ptid);
102
103 /* Find any thread of the PID process. Returns NULL if none is
104 found. */
105 thread_info *find_any_thread_of_pid (int pid);
106
107 /* Find the first thread for which FUNC returns true. Return NULL if no thread
108 satisfying FUNC is found. */
109
110 thread_info *find_thread (gdb::function_view<bool (thread_info *)> func);
111
112 /* Like the above, but only consider threads with pid PID. */
113
114 thread_info *find_thread (int pid,
115 gdb::function_view<bool (thread_info *)> func);
116
117 /* Find the first thread that matches FILTER for which FUNC returns true.
118 Return NULL if no thread satisfying these conditions is found. */
119
120 thread_info *find_thread (ptid_t filter,
121 gdb::function_view<bool (thread_info *)> func);
122
123 /* Invoke FUNC for each thread. */
124
125 void for_each_thread (gdb::function_view<void (thread_info *)> func);
126
127 /* Like the above, but only consider threads matching PTID. */
128
129 void for_each_thread
130 (ptid_t ptid, gdb::function_view<void (thread_info *)> func);
131
132 /* Find a random thread that matches PTID and for which FUNC (THREAD)
133 returns true. If no entry is found then return nullptr. */
134
135 thread_info *find_thread_in_random
136 (gdb::function_view<bool (thread_info *)> func);
137
138 /* Like the above, but only consider threads matching PTID. */
139
140 thread_info *find_thread_in_random
141 (ptid_t ptid, gdb::function_view<bool (thread_info *)> func);
142
143 /* Switch the current thread. */
144
145 void switch_to_thread (thread_info *thread);
146
147 /* Save/restore current thread. */
148
149 class scoped_restore_current_thread
150 {
151 public:
152 scoped_restore_current_thread ();
153 ~scoped_restore_current_thread ();
154
155 DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread);
156
157 /* Cancel restoring on scope exit. */
158 void dont_restore () { m_dont_restore = true; }
159
160 private:
161 bool m_dont_restore = false;
162 process_info *m_process;
163 thread_info *m_thread;
164 };
165
166 #endif /* GDBSERVER_GDBTHREAD_H */