* doc/as.texinfo: Reorder chapter of machine dependent options so
[deliverable/binutils-gdb.git] / gdb / thread.c
CommitLineData
da0baf42
SG
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
7This file is part of GDB.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
6c9638b4 21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25286543
SG
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"
fdfa3315 30#include "gdbthread.h"
46c28185 31#include "command.h"
89ce0c8f 32#include "gdbcmd.h"
25286543 33
89ce0c8f 34#include <ctype.h>
25286543
SG
35#include <sys/types.h>
36#include <signal.h>
37
38/*#include "lynxos-core.h"*/
39
40struct thread_info
41{
42 struct thread_info *next;
43 int pid; /* Actual process id */
44 int num; /* Convenient handle */
88a294b1
JL
45 CORE_ADDR prev_pc; /* State from wait_for_inferior */
46 CORE_ADDR prev_func_start;
47 char *prev_func_name;
48 struct breakpoint *step_resume_breakpoint;
49 struct breakpoint *through_sigtramp_breakpoint;
50 CORE_ADDR step_range_start;
51 CORE_ADDR step_range_end;
52 CORE_ADDR step_frame_address;
53 int trap_expected;
54 int handling_longjmp;
55 int another_trap;
25286543
SG
56};
57
58static struct thread_info *thread_list = NULL;
59static int highest_thread_num;
60
25286543
SG
61static void thread_command PARAMS ((char * tidstr, int from_tty));
62
63static void prune_threads PARAMS ((void));
64
65static void thread_switch PARAMS ((int pid));
66
de43d7d0
SG
67static struct thread_info * find_thread_id PARAMS ((int num));
68
25286543
SG
69void
70init_thread_list ()
71{
72 struct thread_info *tp, *tpnext;
73
74 if (!thread_list)
75 return;
76
77 for (tp = thread_list; tp; tp = tpnext)
78 {
79 tpnext = tp->next;
80 free (tp);
81 }
82
83 thread_list = NULL;
84 highest_thread_num = 0;
85}
86
87void
88add_thread (pid)
89 int pid;
90{
91 struct thread_info *tp;
92
3082244d 93 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
25286543
SG
94
95 tp->pid = pid;
96 tp->num = ++highest_thread_num;
88a294b1
JL
97 tp->prev_pc = 0;
98 tp->prev_func_start = 0;
99 tp->prev_func_name = NULL;
100 tp->step_range_start = 0;
101 tp->step_range_end = 0;
102 tp->step_frame_address =0;
103 tp->step_resume_breakpoint = 0;
104 tp->through_sigtramp_breakpoint = 0;
105 tp->handling_longjmp = 0;
106 tp->trap_expected = 0;
107 tp->another_trap = 0;
25286543
SG
108 tp->next = thread_list;
109 thread_list = tp;
110}
111
112static struct thread_info *
113find_thread_id (num)
114 int num;
115{
116 struct thread_info *tp;
117
118 for (tp = thread_list; tp; tp = tp->next)
119 if (tp->num == num)
120 return tp;
121
122 return NULL;
123}
124
de43d7d0
SG
125int
126valid_thread_id (num)
127 int num;
128{
129 struct thread_info *tp;
130
131 for (tp = thread_list; tp; tp = tp->next)
132 if (tp->num == num)
133 return 1;
134
135 return 0;
136}
137
138int
139pid_to_thread_id (pid)
140 int pid;
141{
142 struct thread_info *tp;
143
144 for (tp = thread_list; tp; tp = tp->next)
145 if (tp->pid == pid)
146 return tp->num;
147
148 return 0;
149}
150
25286543
SG
151int
152in_thread_list (pid)
153 int pid;
154{
155 struct thread_info *tp;
156
157 for (tp = thread_list; tp; tp = tp->next)
158 if (tp->pid == pid)
159 return 1;
160
161 return 0; /* Never heard of 'im */
162}
163
88a294b1
JL
164/* Load infrun state for the thread PID. */
165
166void load_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
167 trap_expected, step_resume_breakpoint,
168 through_sigtramp_breakpoint, step_range_start,
169 step_range_end, step_frame_address,
170 handling_longjmp, another_trap)
171 int pid;
172 CORE_ADDR *prev_pc;
173 CORE_ADDR *prev_func_start;
174 char **prev_func_name;
175 int *trap_expected;
176 struct breakpoint **step_resume_breakpoint;
177 struct breakpoint **through_sigtramp_breakpoint;
178 CORE_ADDR *step_range_start;
179 CORE_ADDR *step_range_end;
180 CORE_ADDR *step_frame_address;
181 int *handling_longjmp;
182 int *another_trap;
183{
184 struct thread_info *tp;
185
186 /* If we can't find the thread, then we're debugging a single threaded
187 process. No need to do anything in that case. */
188 tp = find_thread_id (pid_to_thread_id (pid));
189 if (tp == NULL)
190 return;
191
192 *prev_pc = tp->prev_pc;
193 *prev_func_start = tp->prev_func_start;
194 *prev_func_name = tp->prev_func_name;
195 *step_resume_breakpoint = tp->step_resume_breakpoint;
196 *step_range_start = tp->step_range_start;
197 *step_range_end = tp->step_range_end;
198 *step_frame_address = tp->step_frame_address;
199 *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
200 *handling_longjmp = tp->handling_longjmp;
201 *trap_expected = tp->trap_expected;
202 *another_trap = tp->another_trap;
203}
204
205/* Save infrun state for the thread PID. */
206
207void save_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
208 trap_expected, step_resume_breakpoint,
209 through_sigtramp_breakpoint, step_range_start,
210 step_range_end, step_frame_address,
211 handling_longjmp, another_trap)
212 int pid;
213 CORE_ADDR prev_pc;
214 CORE_ADDR prev_func_start;
215 char *prev_func_name;
216 int trap_expected;
217 struct breakpoint *step_resume_breakpoint;
218 struct breakpoint *through_sigtramp_breakpoint;
219 CORE_ADDR step_range_start;
220 CORE_ADDR step_range_end;
221 CORE_ADDR step_frame_address;
222 int handling_longjmp;
223 int another_trap;
224{
225 struct thread_info *tp;
226
227 /* If we can't find the thread, then we're debugging a single-threaded
228 process. Nothing to do in that case. */
229 tp = find_thread_id (pid_to_thread_id (pid));
230 if (tp == NULL)
231 return;
232
233 tp->prev_pc = prev_pc;
234 tp->prev_func_start = prev_func_start;
235 tp->prev_func_name = prev_func_name;
236 tp->step_resume_breakpoint = step_resume_breakpoint;
237 tp->step_range_start = step_range_start;
238 tp->step_range_end = step_range_end;
239 tp->step_frame_address = step_frame_address;
240 tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
241 tp->handling_longjmp = handling_longjmp;
242 tp->trap_expected = trap_expected;
243 tp->another_trap = another_trap;
244}
245
25286543
SG
246static void
247prune_threads ()
248{
249 struct thread_info *tp, *tpprev;
250
251 tpprev = 0;
252
253 for (tp = thread_list; tp; tp = tp->next)
254 if (tp->pid == -1)
255 {
256 if (tpprev)
257 tpprev->next = tp->next;
258 else
259 thread_list = NULL;
260
261 free (tp);
262 }
263 else
264 tpprev = tp;
265}
266
267/* Print information about currently known threads */
268
269static void
270info_threads_command (arg, from_tty)
271 char *arg;
272 int from_tty;
273{
274 struct thread_info *tp;
275 int current_pid = inferior_pid;
276
4cc1b3f7
JK
277 /* Avoid coredumps which would happen if we tried to access a NULL
278 selected_frame. */
279 if (!target_has_stack) error ("No stack.");
280
25286543
SG
281 for (tp = thread_list; tp; tp = tp->next)
282 {
43fc25c8 283 if (! target_thread_alive (tp->pid))
25286543 284 {
46c28185 285 tp->pid = -1; /* Mark it as dead */
25286543
SG
286 continue;
287 }
288
289 if (tp->pid == current_pid)
290 printf_filtered ("* ");
291 else
292 printf_filtered (" ");
293
294 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
295
296 thread_switch (tp->pid);
297 print_stack_frame (selected_frame, -1, 0);
298 }
299
300 thread_switch (current_pid);
301 prune_threads ();
302}
303
304/* Switch from one thread to another. */
305
3082244d 306static void
25286543
SG
307thread_switch (pid)
308 int pid;
309{
310 if (pid == inferior_pid)
311 return;
312
313 inferior_pid = pid;
25286543
SG
314 flush_cached_frames ();
315 registers_changed ();
316 stop_pc = read_pc();
25286543
SG
317 select_frame (get_current_frame (), 0);
318}
319
5090e82c
SG
320static void
321restore_current_thread (pid)
322 int pid;
323{
324 if (pid != inferior_pid)
325 thread_switch (pid);
326}
327
328/* Apply a GDB command to a list of threads. List syntax is a whitespace
329 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
330 of two numbers seperated by a hyphen. Examples:
331
332 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
333 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
334 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
335*/
336
337static void
338thread_apply_all_command (cmd, from_tty)
339 char *cmd;
340 int from_tty;
341{
342 struct thread_info *tp;
343 struct cleanup *old_chain;
344
345 if (cmd == NULL || *cmd == '\000')
346 error ("Please specify a command following the thread ID list");
347
348 old_chain = make_cleanup (restore_current_thread, inferior_pid);
349
350 for (tp = thread_list; tp; tp = tp->next)
351 {
352 thread_switch (tp->pid);
353 printf_filtered ("\nThread %d (%s):\n", tp->num,
354 target_pid_to_str (inferior_pid));
355 execute_command (cmd, from_tty);
356 }
357}
358
359static void
360thread_apply_command (tidlist, from_tty)
361 char *tidlist;
362 int from_tty;
363{
364 char *cmd;
365 char *p;
366 struct cleanup *old_chain;
367
368 if (tidlist == NULL || *tidlist == '\000')
369 error ("Please specify a thread ID list");
370
371 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
372
373 if (*cmd == '\000')
374 error ("Please specify a command following the thread ID list");
375
376 old_chain = make_cleanup (restore_current_thread, inferior_pid);
377
378 while (tidlist < cmd)
379 {
380 struct thread_info *tp;
381 int start, end;
382
383 start = strtol (tidlist, &p, 10);
384 if (p == tidlist)
385 error ("Error parsing %s", tidlist);
386 tidlist = p;
387
388 while (*tidlist == ' ' || *tidlist == '\t')
389 tidlist++;
390
391 if (*tidlist == '-') /* Got a range of IDs? */
392 {
393 tidlist++; /* Skip the - */
394 end = strtol (tidlist, &p, 10);
395 if (p == tidlist)
396 error ("Error parsing %s", tidlist);
397 tidlist = p;
398
399 while (*tidlist == ' ' || *tidlist == '\t')
400 tidlist++;
401 }
402 else
403 end = start;
404
405 for (; start <= end; start++)
406 {
407 tp = find_thread_id (start);
408
409 if (!tp)
410 {
411 warning ("Unknown thread %d.", start);
412 continue;
413 }
414
415 thread_switch (tp->pid);
416 printf_filtered ("\nThread %d (%s):\n", tp->num,
417 target_pid_to_str (inferior_pid));
418 execute_command (cmd, from_tty);
419 }
420 }
421}
422
423/* Switch to the specified thread. Will dispatch off to thread_apply_command
424 if prefix of arg is `apply'. */
425
25286543
SG
426static void
427thread_command (tidstr, from_tty)
428 char *tidstr;
429 int from_tty;
430{
431 int num;
432 struct thread_info *tp;
433
434 if (!tidstr)
435 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
436see the IDs of currently known threads.");
437
25286543
SG
438 num = atoi (tidstr);
439
440 tp = find_thread_id (num);
441
442 if (!tp)
443 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
444see the IDs of currently known threads.", num);
445
446 thread_switch (tp->pid);
447
448 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
449 print_stack_frame (selected_frame, selected_frame_level, 1);
450}
451
452void
453_initialize_thread ()
454{
5090e82c
SG
455 static struct cmd_list_element *thread_cmd_list = NULL;
456 static struct cmd_list_element *thread_apply_list = NULL;
457 extern struct cmd_list_element *cmdlist;
458
25286543
SG
459 add_info ("threads", info_threads_command,
460 "IDs of currently known threads.");
5090e82c
SG
461
462 add_prefix_cmd ("thread", class_run, thread_command,
463 "Use this command to switch between threads.\n\
464The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
465 &cmdlist);
466
467 add_prefix_cmd ("apply", class_run, thread_apply_command,
468 "Apply a command to a list of threads.",
469 &thread_apply_list, "apply ", 1, &thread_cmd_list);
470
471 add_cmd ("all", class_run, thread_apply_all_command,
472 "Apply a command to all threads.",
473 &thread_apply_list);
474
475 add_com_alias ("t", "thread", class_run, 1);
25286543 476}
This page took 0.267265 seconds and 4 git commands to generate.