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_with_data (std::list
<thread_info
*> *thread_list
,
35 void (*action
) (thread_info
*, void *),
38 gdb_assert (thread_list
== &all_threads
);
40 for_each_thread ([&] (thread_info
*thread
) {
41 action (thread
, data
);
46 add_thread (ptid_t thread_id
, void *target_data
)
48 struct thread_info
*new_thread
= XCNEW (struct thread_info
);
50 new_thread
->id
= thread_id
;
51 new_thread
->last_resume_kind
= resume_continue
;
52 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
54 all_threads
.push_back (new_thread
);
56 if (current_thread
== NULL
)
57 current_thread
= new_thread
;
59 new_thread
->target_data
= target_data
;
64 /* See gdbthread.h. */
67 get_first_thread (void)
69 if (!all_threads
.empty ())
70 return all_threads
.front ();
76 find_thread_ptid (ptid_t ptid
)
78 return find_thread ([&] (thread_info
*thread
) {
79 return thread
->id
== ptid
;
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 free_one_thread (thread_info
*thread
)
105 free_register_cache (thread_regcache_data (thread
));
110 remove_thread (struct thread_info
*thread
)
112 if (thread
->btrace
!= NULL
)
113 target_disable_btrace (thread
->btrace
);
115 discard_queued_stop_replies (ptid_of (thread
));
116 all_threads
.remove (thread
);
117 free_one_thread (thread
);
118 if (current_thread
== thread
)
119 current_thread
= NULL
;
123 thread_target_data (struct thread_info
*thread
)
125 return thread
->target_data
;
129 thread_regcache_data (struct thread_info
*thread
)
131 return thread
->regcache_data
;
135 set_thread_regcache_data (struct thread_info
*thread
, struct regcache
*data
)
137 thread
->regcache_data
= data
;
141 clear_inferiors (void)
143 for_each_thread (free_one_thread
);
144 all_threads
.clear ();
148 current_thread
= NULL
;
151 struct process_info
*
152 add_process (int pid
, int attached
)
154 process_info
*process
= new process_info (pid
, attached
);
156 all_processes
.push_back (process
);
161 /* Remove a process from the common process list and free the memory
163 The caller is responsible for freeing private data first. */
166 remove_process (struct process_info
*process
)
168 clear_symbol_cache (&process
->symbol_cache
);
169 free_all_breakpoints (process
);
170 gdb_assert (find_thread_process (process
) == NULL
);
171 all_processes
.remove (process
);
176 find_process_pid (int pid
)
178 return find_process ([&] (process_info
*process
) {
179 return process
->pid
== pid
;
183 /* Get the first process in the process list, or NULL if the list is empty. */
186 get_first_process (void)
188 if (!all_processes
.empty ())
189 return all_processes
.front ();
194 /* Return non-zero if there are any inferiors that we have created
195 (as opposed to attached-to). */
198 have_started_inferiors_p (void)
200 return find_process ([] (process_info
*process
) {
201 return !process
->attached
;
205 /* Return non-zero if there are any inferiors that we have attached to. */
208 have_attached_inferiors_p (void)
210 return find_process ([] (process_info
*process
) {
211 return process
->attached
;
215 struct process_info
*
216 get_thread_process (const struct thread_info
*thread
)
218 return find_process_pid (thread
->id
.pid ());
221 struct process_info
*
222 current_process (void)
224 gdb_assert (current_thread
!= NULL
);
225 return get_thread_process (current_thread
);
229 do_restore_current_thread_cleanup (void *arg
)
231 current_thread
= (struct thread_info
*) arg
;
235 make_cleanup_restore_current_thread (void)
237 return make_cleanup (do_restore_current_thread_cleanup
, current_thread
);
240 /* See common/common-gdbthread.h. */
243 switch_to_thread (ptid_t ptid
)
245 gdb_assert (ptid
!= minus_one_ptid
);
246 current_thread
= find_thread_ptid (ptid
);
249 /* See common/common-inferior.h. */
254 return current_inferior_cwd
;
257 /* See common/common-inferior.h. */
260 set_inferior_cwd (const char *cwd
)
262 xfree ((void *) current_inferior_cwd
);
264 current_inferior_cwd
= xstrdup (cwd
);
266 current_inferior_cwd
= NULL
;