| 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 | #include "gdbcmd.h" |
| 33 | |
| 34 | #include <ctype.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <signal.h> |
| 37 | |
| 38 | /*#include "lynxos-core.h"*/ |
| 39 | |
| 40 | struct thread_info |
| 41 | { |
| 42 | struct thread_info *next; |
| 43 | int pid; /* Actual process id */ |
| 44 | int num; /* Convenient handle */ |
| 45 | }; |
| 46 | |
| 47 | static struct thread_info *thread_list = NULL; |
| 48 | static int highest_thread_num; |
| 49 | |
| 50 | static void thread_command PARAMS ((char * tidstr, int from_tty)); |
| 51 | |
| 52 | static void prune_threads PARAMS ((void)); |
| 53 | |
| 54 | static void thread_switch PARAMS ((int pid)); |
| 55 | |
| 56 | static struct thread_info * find_thread_id PARAMS ((int num)); |
| 57 | |
| 58 | void |
| 59 | init_thread_list () |
| 60 | { |
| 61 | struct thread_info *tp, *tpnext; |
| 62 | |
| 63 | if (!thread_list) |
| 64 | return; |
| 65 | |
| 66 | for (tp = thread_list; tp; tp = tpnext) |
| 67 | { |
| 68 | tpnext = tp->next; |
| 69 | free (tp); |
| 70 | } |
| 71 | |
| 72 | thread_list = NULL; |
| 73 | highest_thread_num = 0; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | add_thread (pid) |
| 78 | int pid; |
| 79 | { |
| 80 | struct thread_info *tp; |
| 81 | |
| 82 | tp = (struct thread_info *) xmalloc (sizeof (struct thread_info)); |
| 83 | |
| 84 | tp->pid = pid; |
| 85 | tp->num = ++highest_thread_num; |
| 86 | tp->next = thread_list; |
| 87 | thread_list = tp; |
| 88 | } |
| 89 | |
| 90 | static struct thread_info * |
| 91 | find_thread_id (num) |
| 92 | int num; |
| 93 | { |
| 94 | struct thread_info *tp; |
| 95 | |
| 96 | for (tp = thread_list; tp; tp = tp->next) |
| 97 | if (tp->num == num) |
| 98 | return tp; |
| 99 | |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | valid_thread_id (num) |
| 105 | int num; |
| 106 | { |
| 107 | struct thread_info *tp; |
| 108 | |
| 109 | for (tp = thread_list; tp; tp = tp->next) |
| 110 | if (tp->num == num) |
| 111 | return 1; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | pid_to_thread_id (pid) |
| 118 | int pid; |
| 119 | { |
| 120 | struct thread_info *tp; |
| 121 | |
| 122 | for (tp = thread_list; tp; tp = tp->next) |
| 123 | if (tp->pid == pid) |
| 124 | return tp->num; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | in_thread_list (pid) |
| 131 | int pid; |
| 132 | { |
| 133 | struct thread_info *tp; |
| 134 | |
| 135 | for (tp = thread_list; tp; tp = tp->next) |
| 136 | if (tp->pid == pid) |
| 137 | return 1; |
| 138 | |
| 139 | return 0; /* Never heard of 'im */ |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | prune_threads () |
| 144 | { |
| 145 | struct thread_info *tp, *tpprev; |
| 146 | |
| 147 | tpprev = 0; |
| 148 | |
| 149 | for (tp = thread_list; tp; tp = tp->next) |
| 150 | if (tp->pid == -1) |
| 151 | { |
| 152 | if (tpprev) |
| 153 | tpprev->next = tp->next; |
| 154 | else |
| 155 | thread_list = NULL; |
| 156 | |
| 157 | free (tp); |
| 158 | } |
| 159 | else |
| 160 | tpprev = tp; |
| 161 | } |
| 162 | |
| 163 | /* Print information about currently known threads */ |
| 164 | |
| 165 | static void |
| 166 | info_threads_command (arg, from_tty) |
| 167 | char *arg; |
| 168 | int from_tty; |
| 169 | { |
| 170 | struct thread_info *tp; |
| 171 | int current_pid = inferior_pid; |
| 172 | |
| 173 | for (tp = thread_list; tp; tp = tp->next) |
| 174 | { |
| 175 | if (target_has_execution |
| 176 | && kill (tp->pid, 0) == -1) |
| 177 | { |
| 178 | tp->pid = -1; /* Mark it as dead */ |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | if (tp->pid == current_pid) |
| 183 | printf_filtered ("* "); |
| 184 | else |
| 185 | printf_filtered (" "); |
| 186 | |
| 187 | printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid)); |
| 188 | |
| 189 | thread_switch (tp->pid); |
| 190 | print_stack_frame (selected_frame, -1, 0); |
| 191 | } |
| 192 | |
| 193 | thread_switch (current_pid); |
| 194 | prune_threads (); |
| 195 | } |
| 196 | |
| 197 | /* Switch from one thread to another. */ |
| 198 | |
| 199 | static void |
| 200 | thread_switch (pid) |
| 201 | int pid; |
| 202 | { |
| 203 | if (pid == inferior_pid) |
| 204 | return; |
| 205 | |
| 206 | inferior_pid = pid; |
| 207 | flush_cached_frames (); |
| 208 | registers_changed (); |
| 209 | stop_pc = read_pc(); |
| 210 | set_current_frame (create_new_frame (read_fp (), stop_pc)); |
| 211 | stop_frame_address = FRAME_FP (get_current_frame ()); |
| 212 | select_frame (get_current_frame (), 0); |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | restore_current_thread (pid) |
| 217 | int pid; |
| 218 | { |
| 219 | if (pid != inferior_pid) |
| 220 | thread_switch (pid); |
| 221 | } |
| 222 | |
| 223 | /* Apply a GDB command to a list of threads. List syntax is a whitespace |
| 224 | seperated list of numbers, or ranges, or the keyword `all'. Ranges consist |
| 225 | of two numbers seperated by a hyphen. Examples: |
| 226 | |
| 227 | thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4 |
| 228 | thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9 |
| 229 | thread apply all p x/i $pc Apply x/i $pc cmd to all threads |
| 230 | */ |
| 231 | |
| 232 | static void |
| 233 | thread_apply_all_command (cmd, from_tty) |
| 234 | char *cmd; |
| 235 | int from_tty; |
| 236 | { |
| 237 | struct thread_info *tp; |
| 238 | struct cleanup *old_chain; |
| 239 | |
| 240 | if (cmd == NULL || *cmd == '\000') |
| 241 | error ("Please specify a command following the thread ID list"); |
| 242 | |
| 243 | old_chain = make_cleanup (restore_current_thread, inferior_pid); |
| 244 | |
| 245 | for (tp = thread_list; tp; tp = tp->next) |
| 246 | { |
| 247 | thread_switch (tp->pid); |
| 248 | printf_filtered ("\nThread %d (%s):\n", tp->num, |
| 249 | target_pid_to_str (inferior_pid)); |
| 250 | execute_command (cmd, from_tty); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | thread_apply_command (tidlist, from_tty) |
| 256 | char *tidlist; |
| 257 | int from_tty; |
| 258 | { |
| 259 | char *cmd; |
| 260 | char *p; |
| 261 | struct cleanup *old_chain; |
| 262 | |
| 263 | if (tidlist == NULL || *tidlist == '\000') |
| 264 | error ("Please specify a thread ID list"); |
| 265 | |
| 266 | for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++); |
| 267 | |
| 268 | if (*cmd == '\000') |
| 269 | error ("Please specify a command following the thread ID list"); |
| 270 | |
| 271 | old_chain = make_cleanup (restore_current_thread, inferior_pid); |
| 272 | |
| 273 | while (tidlist < cmd) |
| 274 | { |
| 275 | struct thread_info *tp; |
| 276 | int start, end; |
| 277 | |
| 278 | start = strtol (tidlist, &p, 10); |
| 279 | if (p == tidlist) |
| 280 | error ("Error parsing %s", tidlist); |
| 281 | tidlist = p; |
| 282 | |
| 283 | while (*tidlist == ' ' || *tidlist == '\t') |
| 284 | tidlist++; |
| 285 | |
| 286 | if (*tidlist == '-') /* Got a range of IDs? */ |
| 287 | { |
| 288 | tidlist++; /* Skip the - */ |
| 289 | end = strtol (tidlist, &p, 10); |
| 290 | if (p == tidlist) |
| 291 | error ("Error parsing %s", tidlist); |
| 292 | tidlist = p; |
| 293 | |
| 294 | while (*tidlist == ' ' || *tidlist == '\t') |
| 295 | tidlist++; |
| 296 | } |
| 297 | else |
| 298 | end = start; |
| 299 | |
| 300 | for (; start <= end; start++) |
| 301 | { |
| 302 | tp = find_thread_id (start); |
| 303 | |
| 304 | if (!tp) |
| 305 | { |
| 306 | warning ("Unknown thread %d.", start); |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | thread_switch (tp->pid); |
| 311 | printf_filtered ("\nThread %d (%s):\n", tp->num, |
| 312 | target_pid_to_str (inferior_pid)); |
| 313 | execute_command (cmd, from_tty); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* Switch to the specified thread. Will dispatch off to thread_apply_command |
| 319 | if prefix of arg is `apply'. */ |
| 320 | |
| 321 | static void |
| 322 | thread_command (tidstr, from_tty) |
| 323 | char *tidstr; |
| 324 | int from_tty; |
| 325 | { |
| 326 | int num; |
| 327 | struct thread_info *tp; |
| 328 | |
| 329 | if (!tidstr) |
| 330 | error ("Please specify a thread ID. Use the \"info threads\" command to\n\ |
| 331 | see the IDs of currently known threads."); |
| 332 | |
| 333 | num = atoi (tidstr); |
| 334 | |
| 335 | tp = find_thread_id (num); |
| 336 | |
| 337 | if (!tp) |
| 338 | error ("Thread ID %d not known. Use the \"info threads\" command to\n\ |
| 339 | see the IDs of currently known threads.", num); |
| 340 | |
| 341 | thread_switch (tp->pid); |
| 342 | |
| 343 | printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid)); |
| 344 | print_stack_frame (selected_frame, selected_frame_level, 1); |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | _initialize_thread () |
| 349 | { |
| 350 | static struct cmd_list_element *thread_cmd_list = NULL; |
| 351 | static struct cmd_list_element *thread_apply_list = NULL; |
| 352 | extern struct cmd_list_element *cmdlist; |
| 353 | |
| 354 | add_info ("threads", info_threads_command, |
| 355 | "IDs of currently known threads."); |
| 356 | |
| 357 | add_prefix_cmd ("thread", class_run, thread_command, |
| 358 | "Use this command to switch between threads.\n\ |
| 359 | The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1, |
| 360 | &cmdlist); |
| 361 | |
| 362 | add_prefix_cmd ("apply", class_run, thread_apply_command, |
| 363 | "Apply a command to a list of threads.", |
| 364 | &thread_apply_list, "apply ", 1, &thread_cmd_list); |
| 365 | |
| 366 | add_cmd ("all", class_run, thread_apply_all_command, |
| 367 | "Apply a command to all threads.", |
| 368 | &thread_apply_list); |
| 369 | |
| 370 | add_com_alias ("t", "thread", class_run, 1); |
| 371 | } |