1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2017 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/>. */
22 #include "gdbthread.h"
25 std::list
<process_info
*> all_processes
;
26 std::list
<thread_info
*> all_threads
;
28 struct thread_info
*current_thread
;
30 /* The current working directory used to start the inferior. */
31 static const char *current_inferior_cwd
= NULL
;
34 for_each_inferior (std::list
<thread_info
*> *thread_list
,
35 void (*action
) (thread_info
*))
37 gdb_assert (thread_list
== &all_threads
);
39 for_each_thread ([&] (thread_info
*thread
) {
45 for_each_inferior_with_data (std::list
<thread_info
*> *thread_list
,
46 void (*action
) (thread_info
*, void *),
49 gdb_assert (thread_list
== &all_threads
);
51 for_each_thread ([&] (thread_info
*thread
) {
52 action (thread
, data
);
57 add_thread (ptid_t thread_id
, void *target_data
)
59 struct thread_info
*new_thread
= XCNEW (struct thread_info
);
61 new_thread
->id
= thread_id
;
62 new_thread
->last_resume_kind
= resume_continue
;
63 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
65 all_threads
.push_back (new_thread
);
67 if (current_thread
== NULL
)
68 current_thread
= new_thread
;
70 new_thread
->target_data
= target_data
;
75 /* See gdbthread.h. */
78 get_first_thread (void)
80 if (!all_threads
.empty ())
81 return all_threads
.front ();
87 find_thread_ptid (ptid_t ptid
)
89 return find_thread ([&] (thread_info
*thread
) {
90 return thread
->id
== ptid
;
94 /* Find a thread associated with the given PROCESS, or NULL if no
95 such thread exists. */
97 static struct thread_info
*
98 find_thread_process (const struct process_info
*const process
)
100 return find_any_thread_of_pid (process
->pid
);
103 /* See gdbthread.h. */
106 find_any_thread_of_pid (int pid
)
108 return find_thread (pid
, [] (thread_info
*thread
) {
114 free_one_thread (thread_info
*thread
)
116 free_register_cache (thread_regcache_data (thread
));
121 remove_thread (struct thread_info
*thread
)
123 if (thread
->btrace
!= NULL
)
124 target_disable_btrace (thread
->btrace
);
126 discard_queued_stop_replies (ptid_of (thread
));
127 all_threads
.remove (thread
);
128 free_one_thread (thread
);
129 if (current_thread
== thread
)
130 current_thread
= NULL
;
134 thread_target_data (struct thread_info
*thread
)
136 return thread
->target_data
;
140 thread_regcache_data (struct thread_info
*thread
)
142 return thread
->regcache_data
;
146 set_thread_regcache_data (struct thread_info
*thread
, struct regcache
*data
)
148 thread
->regcache_data
= data
;
152 clear_inferiors (void)
154 for_each_inferior (&all_threads
, free_one_thread
);
155 all_threads
.clear ();
159 current_thread
= NULL
;
162 struct process_info
*
163 add_process (int pid
, int attached
)
165 process_info
*process
= new process_info (pid
, attached
);
167 all_processes
.push_back (process
);
172 /* Remove a process from the common process list and free the memory
174 The caller is responsible for freeing private data first. */
177 remove_process (struct process_info
*process
)
179 clear_symbol_cache (&process
->symbol_cache
);
180 free_all_breakpoints (process
);
181 gdb_assert (find_thread_process (process
) == NULL
);
182 all_processes
.remove (process
);
187 find_process_pid (int pid
)
189 return find_process ([&] (process_info
*process
) {
190 return process
->pid
== pid
;
194 /* Get the first process in the process list, or NULL if the list is empty. */
197 get_first_process (void)
199 if (!all_processes
.empty ())
200 return all_processes
.front ();
205 /* Return non-zero if there are any inferiors that we have created
206 (as opposed to attached-to). */
209 have_started_inferiors_p (void)
211 return find_process ([] (process_info
*process
) {
212 return !process
->attached
;
216 /* Return non-zero if there are any inferiors that we have attached to. */
219 have_attached_inferiors_p (void)
221 return find_process ([] (process_info
*process
) {
222 return process
->attached
;
226 struct process_info
*
227 get_thread_process (const struct thread_info
*thread
)
229 return find_process_pid (thread
->id
.pid ());
232 struct process_info
*
233 current_process (void)
235 gdb_assert (current_thread
!= NULL
);
236 return get_thread_process (current_thread
);
240 do_restore_current_thread_cleanup (void *arg
)
242 current_thread
= (struct thread_info
*) arg
;
246 make_cleanup_restore_current_thread (void)
248 return make_cleanup (do_restore_current_thread_cleanup
, current_thread
);
251 /* See common/common-gdbthread.h. */
254 switch_to_thread (ptid_t ptid
)
256 gdb_assert (ptid
!= minus_one_ptid
);
257 current_thread
= find_thread_ptid (ptid
);
260 /* See common/common-inferior.h. */
265 return current_inferior_cwd
;
268 /* See common/common-inferior.h. */
271 set_inferior_cwd (const char *cwd
)
273 xfree ((void *) current_inferior_cwd
);
275 current_inferior_cwd
= xstrdup (cwd
);
277 current_inferior_cwd
= NULL
;