1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2019 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 add_thread (ptid_t thread_id
, void *target_data
)
36 struct thread_info
*new_thread
= XCNEW (struct thread_info
);
38 new_thread
->id
= thread_id
;
39 new_thread
->last_resume_kind
= resume_continue
;
40 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
42 all_threads
.push_back (new_thread
);
44 if (current_thread
== NULL
)
45 current_thread
= new_thread
;
47 new_thread
->target_data
= target_data
;
52 /* See gdbthread.h. */
55 get_first_thread (void)
57 if (!all_threads
.empty ())
58 return all_threads
.front ();
64 find_thread_ptid (ptid_t ptid
)
66 return find_thread ([&] (thread_info
*thread
) {
67 return thread
->id
== ptid
;
71 /* Find a thread associated with the given PROCESS, or NULL if no
72 such thread exists. */
74 static struct thread_info
*
75 find_thread_process (const struct process_info
*const process
)
77 return find_any_thread_of_pid (process
->pid
);
80 /* See gdbthread.h. */
83 find_any_thread_of_pid (int pid
)
85 return find_thread (pid
, [] (thread_info
*thread
) {
91 free_one_thread (thread_info
*thread
)
93 free_register_cache (thread_regcache_data (thread
));
98 remove_thread (struct thread_info
*thread
)
100 if (thread
->btrace
!= NULL
)
101 target_disable_btrace (thread
->btrace
);
103 discard_queued_stop_replies (ptid_of (thread
));
104 all_threads
.remove (thread
);
105 free_one_thread (thread
);
106 if (current_thread
== thread
)
107 current_thread
= NULL
;
111 thread_target_data (struct thread_info
*thread
)
113 return thread
->target_data
;
117 thread_regcache_data (struct thread_info
*thread
)
119 return thread
->regcache_data
;
123 set_thread_regcache_data (struct thread_info
*thread
, struct regcache
*data
)
125 thread
->regcache_data
= data
;
129 clear_inferiors (void)
131 for_each_thread (free_one_thread
);
132 all_threads
.clear ();
136 current_thread
= NULL
;
139 struct process_info
*
140 add_process (int pid
, int attached
)
142 process_info
*process
= new process_info (pid
, attached
);
144 all_processes
.push_back (process
);
149 /* Remove a process from the common process list and free the memory
151 The caller is responsible for freeing private data first. */
154 remove_process (struct process_info
*process
)
156 clear_symbol_cache (&process
->symbol_cache
);
157 free_all_breakpoints (process
);
158 gdb_assert (find_thread_process (process
) == NULL
);
159 all_processes
.remove (process
);
164 find_process_pid (int pid
)
166 return find_process ([&] (process_info
*process
) {
167 return process
->pid
== pid
;
171 /* Get the first process in the process list, or NULL if the list is empty. */
174 get_first_process (void)
176 if (!all_processes
.empty ())
177 return all_processes
.front ();
182 /* Return non-zero if there are any inferiors that we have created
183 (as opposed to attached-to). */
186 have_started_inferiors_p (void)
188 return find_process ([] (process_info
*process
) {
189 return !process
->attached
;
193 /* Return non-zero if there are any inferiors that we have attached to. */
196 have_attached_inferiors_p (void)
198 return find_process ([] (process_info
*process
) {
199 return process
->attached
;
203 struct process_info
*
204 get_thread_process (const struct thread_info
*thread
)
206 return find_process_pid (thread
->id
.pid ());
209 struct process_info
*
210 current_process (void)
212 gdb_assert (current_thread
!= NULL
);
213 return get_thread_process (current_thread
);
216 /* See common/common-gdbthread.h. */
219 switch_to_thread (ptid_t ptid
)
221 gdb_assert (ptid
!= minus_one_ptid
);
222 current_thread
= find_thread_ptid (ptid
);
225 /* See common/common-inferior.h. */
230 return current_inferior_cwd
;
233 /* See common/common-inferior.h. */
236 set_inferior_cwd (const char *cwd
)
238 xfree ((void *) current_inferior_cwd
);
240 current_inferior_cwd
= xstrdup (cwd
);
242 current_inferior_cwd
= NULL
;