]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/thread.c
* corelow.c: Add multi thread/process support for core files with
[thirdparty/binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993
3
4 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "environ.h"
28 #include "value.h"
29 #include "target.h"
30 #include "thread.h"
31 #include "command.h"
32
33 #include <sys/types.h>
34 #include <signal.h>
35
36 /*#include "lynxos-core.h"*/
37
38 struct thread_info
39 {
40 struct thread_info *next;
41 int pid; /* Actual process id */
42 int num; /* Convenient handle */
43 };
44
45 static struct thread_info *thread_list = NULL;
46 static int highest_thread_num;
47
48 static void thread_command PARAMS ((char * tidstr, int from_tty));
49
50 static void prune_threads PARAMS ((void));
51
52 static void thread_switch PARAMS ((int pid));
53
54 void
55 init_thread_list ()
56 {
57 struct thread_info *tp, *tpnext;
58
59 if (!thread_list)
60 return;
61
62 for (tp = thread_list; tp; tp = tpnext)
63 {
64 tpnext = tp->next;
65 free (tp);
66 }
67
68 thread_list = NULL;
69 highest_thread_num = 0;
70 }
71
72 void
73 add_thread (pid)
74 int pid;
75 {
76 struct thread_info *tp;
77
78 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
79
80 tp->pid = pid;
81 tp->num = ++highest_thread_num;
82 tp->next = thread_list;
83 thread_list = tp;
84 }
85
86 static struct thread_info *
87 find_thread_id (num)
88 int num;
89 {
90 struct thread_info *tp;
91
92 for (tp = thread_list; tp; tp = tp->next)
93 if (tp->num == num)
94 return tp;
95
96 return NULL;
97 }
98
99 int
100 in_thread_list (pid)
101 int pid;
102 {
103 struct thread_info *tp;
104
105 for (tp = thread_list; tp; tp = tp->next)
106 if (tp->pid == pid)
107 return 1;
108
109 return 0; /* Never heard of 'im */
110 }
111
112 static void
113 prune_threads ()
114 {
115 struct thread_info *tp, *tpprev;
116
117 tpprev = 0;
118
119 for (tp = thread_list; tp; tp = tp->next)
120 if (tp->pid == -1)
121 {
122 if (tpprev)
123 tpprev->next = tp->next;
124 else
125 thread_list = NULL;
126
127 free (tp);
128 }
129 else
130 tpprev = tp;
131 }
132
133 /* Print information about currently known threads */
134
135 static void
136 info_threads_command (arg, from_tty)
137 char *arg;
138 int from_tty;
139 {
140 struct thread_info *tp;
141 int current_pid = inferior_pid;
142
143 for (tp = thread_list; tp; tp = tp->next)
144 {
145 if (target_has_execution
146 && kill (tp->pid, 0) == -1)
147 {
148 tp->pid = -1; /* Mark it as dead */
149 continue;
150 }
151
152 if (tp->pid == current_pid)
153 printf_filtered ("* ");
154 else
155 printf_filtered (" ");
156
157 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
158
159 thread_switch (tp->pid);
160 print_stack_frame (selected_frame, -1, 0);
161 }
162
163 thread_switch (current_pid);
164 prune_threads ();
165 }
166
167 /* Switch from one thread to another. */
168
169 static void
170 thread_switch (pid)
171 int pid;
172 {
173 if (pid == inferior_pid)
174 return;
175
176 inferior_pid = pid;
177 flush_cached_frames ();
178 registers_changed ();
179 stop_pc = read_pc();
180 set_current_frame (create_new_frame (read_fp (), stop_pc));
181 stop_frame_address = FRAME_FP (get_current_frame ());
182 select_frame (get_current_frame (), 0);
183 }
184
185 static void
186 thread_command (tidstr, from_tty)
187 char *tidstr;
188 int from_tty;
189 {
190 int num;
191 struct thread_info *tp;
192
193 if (!tidstr)
194 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
195 see the IDs of currently known threads.");
196
197
198 num = atoi (tidstr);
199
200 tp = find_thread_id (num);
201
202 if (!tp)
203 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
204 see the IDs of currently known threads.", num);
205
206 thread_switch (tp->pid);
207
208 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
209 print_stack_frame (selected_frame, selected_frame_level, 1);
210 }
211
212 void
213 _initialize_thread ()
214 {
215 add_info ("threads", info_threads_command,
216 "IDs of currently known threads.");
217 add_com ("thread", class_info, thread_command,
218 "Use this command to switch between threads.\n\
219 The new thread ID must be currently known.");
220 }