| 1 |
/* Inferior process information for the remote server for GDB. |
| 2 |
Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
Contributed by MontaVista Software. |
| 5 |
|
| 6 |
This file is part of GDB. |
| 7 |
|
| 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. |
| 12 |
|
| 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. |
| 17 |
|
| 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/>. */ |
| 20 |
|
| 21 |
#include "gdbsupport/common-gdbthread.h" |
| 22 |
#include "gdbsupport/common-inferior.h" |
| 23 |
#include "gdbsupport/owning_intrusive_list.h" |
| 24 |
#include "gdbthread.h" |
| 25 |
#include "dll.h" |
| 26 |
|
| 27 |
owning_intrusive_list<process_info> all_processes; |
| 28 |
|
| 29 |
/* The current process. */ |
| 30 |
static process_info *current_process_; |
| 31 |
|
| 32 |
/* The current thread. This is either a thread of CURRENT_PROCESS, or |
| 33 |
NULL. */ |
| 34 |
thread_info *current_thread; |
| 35 |
|
| 36 |
/* The current working directory used to start the inferior. |
| 37 |
|
| 38 |
Empty if not specified. */ |
| 39 |
static std::string current_inferior_cwd; |
| 40 |
|
| 41 |
thread_info * |
| 42 |
process_info::add_thread (ptid_t id, void *target_data) |
| 43 |
{ |
| 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; |
| 46 |
|
| 47 |
/* A thread with this ptid should not exist in the map yet. */ |
| 48 |
gdb_assert (inserted); |
| 49 |
|
| 50 |
if (current_thread == NULL) |
| 51 |
switch_to_thread (&new_thread); |
| 52 |
|
| 53 |
return &new_thread; |
| 54 |
} |
| 55 |
|
| 56 |
/* See gdbthread.h. */ |
| 57 |
|
| 58 |
thread_info * |
| 59 |
get_first_thread (void) |
| 60 |
{ |
| 61 |
return find_thread ([] (thread_info *thread) |
| 62 |
{ |
| 63 |
return true; |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
thread_info * |
| 68 |
find_thread_ptid (ptid_t ptid) |
| 69 |
{ |
| 70 |
process_info *process = find_process_pid (ptid.pid ()); |
| 71 |
if (process == nullptr) |
| 72 |
return nullptr; |
| 73 |
|
| 74 |
return process->find_thread (ptid); |
| 75 |
} |
| 76 |
|
| 77 |
/* Find a thread associated with the given PROCESS, or NULL if no |
| 78 |
such thread exists. */ |
| 79 |
|
| 80 |
static thread_info * |
| 81 |
find_thread_process (const struct process_info *const process) |
| 82 |
{ |
| 83 |
return find_any_thread_of_pid (process->pid); |
| 84 |
} |
| 85 |
|
| 86 |
/* See gdbthread.h. */ |
| 87 |
|
| 88 |
thread_info * |
| 89 |
find_any_thread_of_pid (int pid) |
| 90 |
{ |
| 91 |
return find_thread (pid, [] (thread_info *thread) { |
| 92 |
return true; |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
process_info::remove_thread (thread_info *thread) |
| 98 |
{ |
| 99 |
if (thread->btrace != NULL) |
| 100 |
target_disable_btrace (thread->btrace); |
| 101 |
|
| 102 |
discard_queued_stop_replies (thread->id); |
| 103 |
|
| 104 |
if (current_thread == thread) |
| 105 |
switch_to_thread (nullptr); |
| 106 |
|
| 107 |
/* We should not try to remove a thread that was not added. */ |
| 108 |
gdb_assert (thread->process () == this); |
| 109 |
int num_erased = m_ptid_thread_map.erase (thread->id); |
| 110 |
gdb_assert (num_erased > 0); |
| 111 |
|
| 112 |
m_thread_list.erase (m_thread_list.iterator_to (*thread)); |
| 113 |
} |
| 114 |
|
| 115 |
struct process_info * |
| 116 |
add_process (int pid, int attached) |
| 117 |
{ |
| 118 |
return &all_processes.emplace_back (pid, attached); |
| 119 |
} |
| 120 |
|
| 121 |
/* Remove a process from the common process list and free the memory |
| 122 |
allocated for it. |
| 123 |
The caller is responsible for freeing private data first. */ |
| 124 |
|
| 125 |
void |
| 126 |
remove_process (struct process_info *process) |
| 127 |
{ |
| 128 |
clear_symbol_cache (&process->symbol_cache); |
| 129 |
free_all_breakpoints (process); |
| 130 |
gdb_assert (find_thread_process (process) == NULL); |
| 131 |
if (current_process () == process) |
| 132 |
switch_to_process (nullptr); |
| 133 |
|
| 134 |
all_processes.erase (all_processes.iterator_to (*process)); |
| 135 |
} |
| 136 |
|
| 137 |
process_info * |
| 138 |
find_process_pid (int pid) |
| 139 |
{ |
| 140 |
return find_process ([&] (process_info *process) { |
| 141 |
return process->pid == pid; |
| 142 |
}); |
| 143 |
} |
| 144 |
|
| 145 |
/* Get the first process in the process list, or NULL if the list is empty. */ |
| 146 |
|
| 147 |
process_info * |
| 148 |
get_first_process (void) |
| 149 |
{ |
| 150 |
if (!all_processes.empty ()) |
| 151 |
return &all_processes.front (); |
| 152 |
else |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
/* Return non-zero if there are any inferiors that we have created |
| 157 |
(as opposed to attached-to). */ |
| 158 |
|
| 159 |
int |
| 160 |
have_started_inferiors_p (void) |
| 161 |
{ |
| 162 |
return find_process ([] (process_info *process) { |
| 163 |
return !process->attached; |
| 164 |
}) != NULL; |
| 165 |
} |
| 166 |
|
| 167 |
/* Return non-zero if there are any inferiors that we have attached to. */ |
| 168 |
|
| 169 |
int |
| 170 |
have_attached_inferiors_p (void) |
| 171 |
{ |
| 172 |
return find_process ([] (process_info *process) { |
| 173 |
return process->attached; |
| 174 |
}) != NULL; |
| 175 |
} |
| 176 |
|
| 177 |
struct process_info * |
| 178 |
current_process (void) |
| 179 |
{ |
| 180 |
return current_process_; |
| 181 |
} |
| 182 |
|
| 183 |
/* See inferiors.h. */ |
| 184 |
|
| 185 |
void |
| 186 |
for_each_process (gdb::function_view<void (process_info *)> func) |
| 187 |
{ |
| 188 |
owning_intrusive_list<process_info>::iterator next, cur |
| 189 |
= all_processes.begin (); |
| 190 |
|
| 191 |
while (cur != all_processes.end ()) |
| 192 |
{ |
| 193 |
next = cur; |
| 194 |
next++; |
| 195 |
func (&*cur); |
| 196 |
cur = next; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/* See inferiors.h. */ |
| 201 |
|
| 202 |
process_info * |
| 203 |
find_process (gdb::function_view<bool (process_info *)> func) |
| 204 |
{ |
| 205 |
for (process_info &process : all_processes) |
| 206 |
if (func (&process)) |
| 207 |
return &process; |
| 208 |
|
| 209 |
return NULL; |
| 210 |
} |
| 211 |
|
| 212 |
/* See inferiors.h. */ |
| 213 |
|
| 214 |
thread_info * |
| 215 |
process_info::find_thread (ptid_t ptid) |
| 216 |
{ |
| 217 |
if (auto it = m_ptid_thread_map.find (ptid); |
| 218 |
it != m_ptid_thread_map.end ()) |
| 219 |
return it->second; |
| 220 |
|
| 221 |
return nullptr; |
| 222 |
} |
| 223 |
|
| 224 |
/* See inferiors.h. */ |
| 225 |
|
| 226 |
thread_info * |
| 227 |
process_info::find_thread (gdb::function_view<bool (thread_info *)> func) |
| 228 |
{ |
| 229 |
for (thread_info &thread : m_thread_list) |
| 230 |
if (func (&thread)) |
| 231 |
return &thread; |
| 232 |
|
| 233 |
return nullptr; |
| 234 |
} |
| 235 |
|
| 236 |
/* See gdbthread.h. */ |
| 237 |
|
| 238 |
thread_info * |
| 239 |
find_thread (gdb::function_view<bool (thread_info *)> func) |
| 240 |
{ |
| 241 |
for (process_info &process : all_processes) |
| 242 |
if (thread_info *thread = process.find_thread (func); |
| 243 |
thread != nullptr) |
| 244 |
return thread; |
| 245 |
|
| 246 |
return NULL; |
| 247 |
} |
| 248 |
|
| 249 |
/* See gdbthread.h. */ |
| 250 |
|
| 251 |
thread_info * |
| 252 |
find_thread (int pid, gdb::function_view<bool (thread_info *)> func) |
| 253 |
{ |
| 254 |
process_info *process = find_process_pid (pid); |
| 255 |
if (process == nullptr) |
| 256 |
return nullptr; |
| 257 |
|
| 258 |
return process->find_thread (func); |
| 259 |
} |
| 260 |
|
| 261 |
/* See gdbthread.h. */ |
| 262 |
|
| 263 |
thread_info * |
| 264 |
find_thread (ptid_t filter, gdb::function_view<bool (thread_info *)> func) |
| 265 |
{ |
| 266 |
if (filter == minus_one_ptid) |
| 267 |
return find_thread (func); |
| 268 |
|
| 269 |
process_info *process = find_process_pid (filter.pid ()); |
| 270 |
if (process == nullptr) |
| 271 |
return nullptr; |
| 272 |
|
| 273 |
if (filter.is_pid ()) |
| 274 |
return process->find_thread (func); |
| 275 |
|
| 276 |
if (thread_info *thread = process->find_thread (filter); |
| 277 |
thread != nullptr && func (thread)) |
| 278 |
return thread; |
| 279 |
|
| 280 |
return nullptr; |
| 281 |
} |
| 282 |
|
| 283 |
/* See gdbthread.h. */ |
| 284 |
|
| 285 |
void |
| 286 |
for_each_thread (gdb::function_view<void (thread_info *)> func) |
| 287 |
{ |
| 288 |
for_each_process ([&] (process_info *proc) |
| 289 |
{ |
| 290 |
proc->for_each_thread (func); |
| 291 |
}); |
| 292 |
} |
| 293 |
|
| 294 |
/* See inferiors.h. */ |
| 295 |
|
| 296 |
void |
| 297 |
process_info::for_each_thread (gdb::function_view<void (thread_info *)> func) |
| 298 |
{ |
| 299 |
owning_intrusive_list<thread_info>::iterator next, cur |
| 300 |
= m_thread_list.begin (); |
| 301 |
|
| 302 |
while (cur != m_thread_list.end ()) |
| 303 |
{ |
| 304 |
/* FUNC may alter the current iterator. */ |
| 305 |
next = cur; |
| 306 |
next++; |
| 307 |
func (&*cur); |
| 308 |
cur = next; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* See gdbthread.h. */ |
| 313 |
|
| 314 |
void |
| 315 |
for_each_thread (ptid_t ptid, gdb::function_view<void (thread_info *)> func) |
| 316 |
{ |
| 317 |
if (ptid == minus_one_ptid) |
| 318 |
for_each_thread (func); |
| 319 |
else if (ptid.is_pid ()) |
| 320 |
{ |
| 321 |
process_info *process = find_process_pid (ptid.pid ()); |
| 322 |
|
| 323 |
if (process != nullptr) |
| 324 |
process->for_each_thread (func); |
| 325 |
} |
| 326 |
else |
| 327 |
find_thread (ptid, [func] (thread_info *thread) |
| 328 |
{ |
| 329 |
func (thread); |
| 330 |
return false; |
| 331 |
}); |
| 332 |
} |
| 333 |
|
| 334 |
/* See gdbthread.h. */ |
| 335 |
|
| 336 |
thread_info * |
| 337 |
find_thread_in_random (ptid_t ptid, |
| 338 |
gdb::function_view<bool (thread_info *)> func) |
| 339 |
{ |
| 340 |
int count = 0; |
| 341 |
int random_selector; |
| 342 |
|
| 343 |
/* First count how many interesting entries we have. */ |
| 344 |
for_each_thread (ptid, [&] (thread_info *thread) |
| 345 |
{ |
| 346 |
if (func (thread)) |
| 347 |
count++; |
| 348 |
}); |
| 349 |
|
| 350 |
if (count == 0) |
| 351 |
return nullptr; |
| 352 |
|
| 353 |
/* Now randomly pick an entry out of those. */ |
| 354 |
random_selector = (int) |
| 355 |
((count * (double) rand ()) / (RAND_MAX + 1.0)); |
| 356 |
|
| 357 |
thread_info *thread = find_thread (ptid, [&] (thread_info *thr_arg) |
| 358 |
{ |
| 359 |
return func (thr_arg) && (random_selector-- == 0); |
| 360 |
}); |
| 361 |
|
| 362 |
gdb_assert (thread != NULL); |
| 363 |
|
| 364 |
return thread; |
| 365 |
} |
| 366 |
|
| 367 |
/* See gdbthread.h. */ |
| 368 |
|
| 369 |
thread_info * |
| 370 |
find_thread_in_random (gdb::function_view<bool (thread_info *)> func) |
| 371 |
{ |
| 372 |
return find_thread_in_random (minus_one_ptid, func); |
| 373 |
} |
| 374 |
|
| 375 |
/* See gdbsupport/common-gdbthread.h. */ |
| 376 |
|
| 377 |
void |
| 378 |
switch_to_thread (process_stratum_target *ops, ptid_t ptid) |
| 379 |
{ |
| 380 |
gdb_assert (ptid != minus_one_ptid); |
| 381 |
switch_to_thread (find_thread_ptid (ptid)); |
| 382 |
} |
| 383 |
|
| 384 |
/* See gdbthread.h. */ |
| 385 |
|
| 386 |
void |
| 387 |
switch_to_thread (thread_info *thread) |
| 388 |
{ |
| 389 |
if (thread != nullptr) |
| 390 |
current_process_ = thread->process (); |
| 391 |
else |
| 392 |
current_process_ = nullptr; |
| 393 |
current_thread = thread; |
| 394 |
} |
| 395 |
|
| 396 |
/* See inferiors.h. */ |
| 397 |
|
| 398 |
void |
| 399 |
switch_to_process (process_info *proc) |
| 400 |
{ |
| 401 |
current_process_ = proc; |
| 402 |
current_thread = nullptr; |
| 403 |
} |
| 404 |
|
| 405 |
/* See gdbsupport/common-inferior.h. */ |
| 406 |
|
| 407 |
const std::string & |
| 408 |
get_inferior_cwd () |
| 409 |
{ |
| 410 |
return current_inferior_cwd; |
| 411 |
} |
| 412 |
|
| 413 |
/* See inferiors.h. */ |
| 414 |
|
| 415 |
void |
| 416 |
set_inferior_cwd (std::string cwd) |
| 417 |
{ |
| 418 |
current_inferior_cwd = std::move (cwd); |
| 419 |
} |
| 420 |
|
| 421 |
scoped_restore_current_thread::scoped_restore_current_thread () |
| 422 |
{ |
| 423 |
m_process = current_process_; |
| 424 |
m_thread = current_thread; |
| 425 |
} |
| 426 |
|
| 427 |
scoped_restore_current_thread::~scoped_restore_current_thread () |
| 428 |
{ |
| 429 |
if (m_dont_restore) |
| 430 |
return; |
| 431 |
|
| 432 |
if (m_thread != nullptr) |
| 433 |
switch_to_thread (m_thread); |
| 434 |
else |
| 435 |
switch_to_process (m_process); |
| 436 |
} |