1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2024 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/>. */
21 #include "gdbsupport/common-gdbthread.h"
22 #include "gdbsupport/common-inferior.h"
23 #include "gdbsupport/owning_intrusive_list.h"
24 #include "gdbthread.h"
27 owning_intrusive_list
<process_info
> all_processes
;
29 /* The current process. */
30 static process_info
*current_process_
;
32 /* The current thread. This is either a thread of CURRENT_PROCESS, or
34 struct thread_info
*current_thread
;
36 /* The current working directory used to start the inferior.
38 Empty if not specified. */
39 static std::string current_inferior_cwd
;
42 process_info::add_thread (ptid_t id
, void *target_data
)
44 auto &new_thread
= m_thread_list
.emplace_back (id
, this, target_data
);
45 bool inserted
= m_ptid_thread_map
.insert ({ id
, &new_thread
}).second
;
47 /* A thread with this ptid should not exist in the map yet. */
48 gdb_assert (inserted
);
50 if (current_thread
== NULL
)
51 switch_to_thread (&new_thread
);
56 /* See gdbthread.h. */
59 get_first_thread (void)
61 return find_thread ([] (thread_info
*thread
)
68 find_thread_ptid (ptid_t ptid
)
70 process_info
*process
= find_process_pid (ptid
.pid ());
71 if (process
== nullptr)
74 auto &thread_map
= process
->thread_map ();
76 if (auto it
= thread_map
.find (ptid
);
77 it
!= thread_map
.end ())
83 /* Find a thread associated with the given PROCESS, or NULL if no
84 such thread exists. */
86 static struct thread_info
*
87 find_thread_process (const struct process_info
*const process
)
89 return find_any_thread_of_pid (process
->pid
);
92 /* See gdbthread.h. */
95 find_any_thread_of_pid (int pid
)
97 return find_thread (pid
, [] (thread_info
*thread
) {
103 process_info::remove_thread (thread_info
*thread
)
105 if (thread
->btrace
!= NULL
)
106 target_disable_btrace (thread
->btrace
);
108 discard_queued_stop_replies (thread
->id
);
110 if (current_thread
== thread
)
111 switch_to_thread (nullptr);
113 /* We should not try to remove a thread that was not added. */
114 gdb_assert (thread
->process () == this);
115 int num_erased
= m_ptid_thread_map
.erase (thread
->id
);
116 gdb_assert (num_erased
> 0);
118 m_thread_list
.erase (m_thread_list
.iterator_to (*thread
));
122 thread_target_data (struct thread_info
*thread
)
124 return thread
->target_data
;
128 thread_regcache_data (struct thread_info
*thread
)
130 return thread
->regcache_data
;
134 set_thread_regcache_data (struct thread_info
*thread
, struct regcache
*data
)
136 thread
->regcache_data
= data
;
140 clear_inferiors (void)
142 for_each_process ([] (process_info
*process
)
144 process
->thread_list ().clear ();
145 process
->thread_map ().clear ();
150 switch_to_thread (nullptr);
151 current_process_
= nullptr;
154 struct process_info
*
155 add_process (int pid
, int attached
)
157 return &all_processes
.emplace_back (pid
, attached
);
160 /* Remove a process from the common process list and free the memory
162 The caller is responsible for freeing private data first. */
165 remove_process (struct process_info
*process
)
167 clear_symbol_cache (&process
->symbol_cache
);
168 free_all_breakpoints (process
);
169 gdb_assert (find_thread_process (process
) == NULL
);
170 if (current_process () == process
)
171 switch_to_process (nullptr);
173 all_processes
.erase (all_processes
.iterator_to (*process
));
177 find_process_pid (int pid
)
179 return find_process ([&] (process_info
*process
) {
180 return process
->pid
== pid
;
184 /* Get the first process in the process list, or NULL if the list is empty. */
187 get_first_process (void)
189 if (!all_processes
.empty ())
190 return &all_processes
.front ();
195 /* Return non-zero if there are any inferiors that we have created
196 (as opposed to attached-to). */
199 have_started_inferiors_p (void)
201 return find_process ([] (process_info
*process
) {
202 return !process
->attached
;
206 /* Return non-zero if there are any inferiors that we have attached to. */
209 have_attached_inferiors_p (void)
211 return find_process ([] (process_info
*process
) {
212 return process
->attached
;
216 struct process_info
*
217 current_process (void)
219 return current_process_
;
222 /* See inferiors.h. */
225 for_each_process (gdb::function_view
<void (process_info
*)> func
)
227 owning_intrusive_list
<process_info
>::iterator next
, cur
228 = all_processes
.begin ();
230 while (cur
!= all_processes
.end ())
239 /* See inferiors.h. */
242 find_process (gdb::function_view
<bool (process_info
*)> func
)
244 for (process_info
&process
: all_processes
)
251 /* See inferiors.h. */
254 process_info::find_thread (gdb::function_view
<bool (thread_info
*)> func
)
256 for (thread_info
&thread
: m_thread_list
)
263 /* See gdbthread.h. */
266 find_thread (gdb::function_view
<bool (thread_info
*)> func
)
268 for (process_info
&process
: all_processes
)
269 if (thread_info
*thread
= process
.find_thread (func
);
276 /* See gdbthread.h. */
279 find_thread (int pid
, gdb::function_view
<bool (thread_info
*)> func
)
281 process_info
*process
= find_process_pid (pid
);
282 if (process
== nullptr)
285 return process
->find_thread (func
);
288 /* See gdbthread.h. */
291 find_thread (ptid_t filter
, gdb::function_view
<bool (thread_info
*)> func
)
293 if (filter
== minus_one_ptid
)
294 return find_thread (func
);
296 process_info
*process
= find_process_pid (filter
.pid ());
297 if (process
== nullptr)
300 if (filter
.is_pid ())
301 return process
->find_thread (func
);
303 auto &thread_map
= process
->thread_map ();
305 if (auto it
= thread_map
.find (filter
);
306 it
!= thread_map
.end () && func (it
->second
))
312 /* See gdbthread.h. */
315 for_each_thread (gdb::function_view
<void (thread_info
*)> func
)
317 for_each_process ([&] (process_info
*proc
)
319 proc
->for_each_thread (func
);
323 /* See inferiors.h. */
326 process_info::for_each_thread (gdb::function_view
<void (thread_info
*)> func
)
328 owning_intrusive_list
<thread_info
>::iterator next
, cur
329 = m_thread_list
.begin ();
331 while (cur
!= m_thread_list
.end ())
333 /* FUNC may alter the current iterator. */
341 /* See gdbthread.h. */
344 for_each_thread (ptid_t ptid
, gdb::function_view
<void (thread_info
*)> func
)
346 if (ptid
== minus_one_ptid
)
347 for_each_thread (func
);
348 else if (ptid
.is_pid ())
350 process_info
*process
= find_process_pid (ptid
.pid ());
352 if (process
!= nullptr)
353 process
->for_each_thread (func
);
356 find_thread (ptid
, [func
] (thread_info
*thread
)
363 /* See gdbthread.h. */
366 find_thread_in_random (ptid_t ptid
,
367 gdb::function_view
<bool (thread_info
*)> func
)
372 /* First count how many interesting entries we have. */
373 for_each_thread (ptid
, [&] (thread_info
*thread
)
382 /* Now randomly pick an entry out of those. */
383 random_selector
= (int)
384 ((count
* (double) rand ()) / (RAND_MAX
+ 1.0));
386 thread_info
*thread
= find_thread (ptid
, [&] (thread_info
*thr_arg
)
388 return func (thr_arg
) && (random_selector
-- == 0);
391 gdb_assert (thread
!= NULL
);
396 /* See gdbthread.h. */
399 find_thread_in_random (gdb::function_view
<bool (thread_info
*)> func
)
401 return find_thread_in_random (minus_one_ptid
, func
);
404 /* See gdbsupport/common-gdbthread.h. */
407 switch_to_thread (process_stratum_target
*ops
, ptid_t ptid
)
409 gdb_assert (ptid
!= minus_one_ptid
);
410 switch_to_thread (find_thread_ptid (ptid
));
413 /* See gdbthread.h. */
416 switch_to_thread (thread_info
*thread
)
418 if (thread
!= nullptr)
419 current_process_
= thread
->process ();
421 current_process_
= nullptr;
422 current_thread
= thread
;
425 /* See inferiors.h. */
428 switch_to_process (process_info
*proc
)
430 current_process_
= proc
;
431 current_thread
= nullptr;
434 /* See gdbsupport/common-inferior.h. */
439 return current_inferior_cwd
;
442 /* See inferiors.h. */
445 set_inferior_cwd (std::string cwd
)
447 current_inferior_cwd
= std::move (cwd
);
450 scoped_restore_current_thread::scoped_restore_current_thread ()
452 m_process
= current_process_
;
453 m_thread
= current_thread
;
456 scoped_restore_current_thread::~scoped_restore_current_thread ()
461 if (m_thread
!= nullptr)
462 switch_to_thread (m_thread
);
464 switch_to_process (m_process
);