| 1 | /* Multi-process control for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "inferior.h" |
| 22 | #include "target.h" |
| 23 | #include "command.h" |
| 24 | #include "gdbcmd.h" |
| 25 | #include "gdbthread.h" |
| 26 | #include "ui-out.h" |
| 27 | #include "observer.h" |
| 28 | |
| 29 | void _initialize_inferiors (void); |
| 30 | |
| 31 | static struct inferior *inferior_list = NULL; |
| 32 | static int highest_inferior_num; |
| 33 | |
| 34 | /* Print notices on inferior events (attach, detach, etc.), set with |
| 35 | `set print inferior-events'. */ |
| 36 | static int print_inferior_events = 0; |
| 37 | |
| 38 | struct inferior* |
| 39 | current_inferior (void) |
| 40 | { |
| 41 | struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid)); |
| 42 | gdb_assert (inf); |
| 43 | return inf; |
| 44 | } |
| 45 | |
| 46 | static void |
| 47 | free_inferior (struct inferior *inf) |
| 48 | { |
| 49 | discard_all_inferior_continuations (inf); |
| 50 | xfree (inf->private); |
| 51 | xfree (inf); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | init_inferior_list (void) |
| 56 | { |
| 57 | struct inferior *inf, *infnext; |
| 58 | |
| 59 | highest_inferior_num = 0; |
| 60 | if (!inferior_list) |
| 61 | return; |
| 62 | |
| 63 | for (inf = inferior_list; inf; inf = infnext) |
| 64 | { |
| 65 | infnext = inf->next; |
| 66 | free_inferior (inf); |
| 67 | } |
| 68 | |
| 69 | inferior_list = NULL; |
| 70 | } |
| 71 | |
| 72 | struct inferior * |
| 73 | add_inferior_silent (int pid) |
| 74 | { |
| 75 | struct inferior *inf; |
| 76 | |
| 77 | inf = xmalloc (sizeof (*inf)); |
| 78 | memset (inf, 0, sizeof (*inf)); |
| 79 | inf->pid = pid; |
| 80 | |
| 81 | inf->stop_soon = NO_STOP_QUIETLY; |
| 82 | |
| 83 | inf->num = ++highest_inferior_num; |
| 84 | inf->next = inferior_list; |
| 85 | inferior_list = inf; |
| 86 | |
| 87 | observer_notify_new_inferior (pid); |
| 88 | |
| 89 | return inf; |
| 90 | } |
| 91 | |
| 92 | struct inferior * |
| 93 | add_inferior (int pid) |
| 94 | { |
| 95 | struct inferior *inf = add_inferior_silent (pid); |
| 96 | |
| 97 | if (print_inferior_events) |
| 98 | printf_unfiltered (_("[New inferior %d]\n"), pid); |
| 99 | |
| 100 | return inf; |
| 101 | } |
| 102 | |
| 103 | struct delete_thread_of_inferior_arg |
| 104 | { |
| 105 | int pid; |
| 106 | int silent; |
| 107 | }; |
| 108 | |
| 109 | static int |
| 110 | delete_thread_of_inferior (struct thread_info *tp, void *data) |
| 111 | { |
| 112 | struct delete_thread_of_inferior_arg *arg = data; |
| 113 | |
| 114 | if (ptid_get_pid (tp->ptid) == arg->pid) |
| 115 | { |
| 116 | if (arg->silent) |
| 117 | delete_thread_silent (tp->ptid); |
| 118 | else |
| 119 | delete_thread (tp->ptid); |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* If SILENT then be quiet -- don't announce a inferior death, or the |
| 126 | exit of its threads. */ |
| 127 | static void |
| 128 | delete_inferior_1 (int pid, int silent) |
| 129 | { |
| 130 | struct inferior *inf, *infprev; |
| 131 | struct delete_thread_of_inferior_arg arg = { pid, silent }; |
| 132 | |
| 133 | infprev = NULL; |
| 134 | |
| 135 | for (inf = inferior_list; inf; infprev = inf, inf = inf->next) |
| 136 | if (inf->pid == pid) |
| 137 | break; |
| 138 | |
| 139 | if (!inf) |
| 140 | return; |
| 141 | |
| 142 | if (infprev) |
| 143 | infprev->next = inf->next; |
| 144 | else |
| 145 | inferior_list = inf->next; |
| 146 | |
| 147 | free_inferior (inf); |
| 148 | |
| 149 | arg.pid = pid; |
| 150 | arg.silent = silent; |
| 151 | |
| 152 | iterate_over_threads (delete_thread_of_inferior, &arg); |
| 153 | |
| 154 | observer_notify_inferior_exit (pid); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | delete_inferior (int pid) |
| 159 | { |
| 160 | delete_inferior_1 (pid, 0); |
| 161 | |
| 162 | if (print_inferior_events) |
| 163 | printf_unfiltered (_("[Inferior %d exited]\n"), pid); |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | delete_inferior_silent (int pid) |
| 168 | { |
| 169 | delete_inferior_1 (pid, 1); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | detach_inferior (int pid) |
| 174 | { |
| 175 | delete_inferior_1 (pid, 1); |
| 176 | |
| 177 | if (print_inferior_events) |
| 178 | printf_unfiltered (_("[Inferior %d detached]\n"), pid); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | discard_all_inferiors (void) |
| 183 | { |
| 184 | struct inferior *inf, *infnext; |
| 185 | |
| 186 | for (inf = inferior_list; inf; inf = infnext) |
| 187 | { |
| 188 | infnext = inf->next; |
| 189 | delete_inferior_silent (inf->pid); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static struct inferior * |
| 194 | find_inferior_id (int num) |
| 195 | { |
| 196 | struct inferior *inf; |
| 197 | |
| 198 | for (inf = inferior_list; inf; inf = inf->next) |
| 199 | if (inf->num == num) |
| 200 | return inf; |
| 201 | |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | struct inferior * |
| 206 | find_inferior_pid (int pid) |
| 207 | { |
| 208 | struct inferior *inf; |
| 209 | |
| 210 | for (inf = inferior_list; inf; inf = inf->next) |
| 211 | if (inf->pid == pid) |
| 212 | return inf; |
| 213 | |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | struct inferior * |
| 218 | iterate_over_inferiors (int (*callback) (struct inferior *, void *), |
| 219 | void *data) |
| 220 | { |
| 221 | struct inferior *inf, *infnext; |
| 222 | |
| 223 | for (inf = inferior_list; inf; inf = infnext) |
| 224 | { |
| 225 | infnext = inf->next; |
| 226 | if ((*callback) (inf, data)) |
| 227 | return inf; |
| 228 | } |
| 229 | |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | int |
| 234 | valid_gdb_inferior_id (int num) |
| 235 | { |
| 236 | struct inferior *inf; |
| 237 | |
| 238 | for (inf = inferior_list; inf; inf = inf->next) |
| 239 | if (inf->num == num) |
| 240 | return 1; |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | pid_to_gdb_inferior_id (int pid) |
| 247 | { |
| 248 | struct inferior *inf; |
| 249 | |
| 250 | for (inf = inferior_list; inf; inf = inf->next) |
| 251 | if (inf->pid == pid) |
| 252 | return inf->num; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int |
| 258 | gdb_inferior_id_to_pid (int num) |
| 259 | { |
| 260 | struct inferior *inferior = find_inferior_id (num); |
| 261 | if (inferior) |
| 262 | return inferior->pid; |
| 263 | else |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | int |
| 268 | in_inferior_list (int pid) |
| 269 | { |
| 270 | struct inferior *inf; |
| 271 | |
| 272 | for (inf = inferior_list; inf; inf = inf->next) |
| 273 | if (inf->pid == pid) |
| 274 | return 1; |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int |
| 280 | have_inferiors (void) |
| 281 | { |
| 282 | return inferior_list != NULL; |
| 283 | } |
| 284 | |
| 285 | /* Prints the list of inferiors and their details on UIOUT. This is a |
| 286 | version of 'info_inferior_command' suitable for use from MI. |
| 287 | |
| 288 | If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that |
| 289 | should be printed. Otherwise, all inferiors are printed. */ |
| 290 | void |
| 291 | print_inferior (struct ui_out *uiout, int requested_inferior) |
| 292 | { |
| 293 | struct inferior *inf; |
| 294 | struct cleanup *old_chain; |
| 295 | |
| 296 | old_chain = make_cleanup_ui_out_list_begin_end (uiout, "inferiors"); |
| 297 | |
| 298 | for (inf = inferior_list; inf; inf = inf->next) |
| 299 | { |
| 300 | struct cleanup *chain2; |
| 301 | |
| 302 | if (requested_inferior != -1 && inf->num != requested_inferior) |
| 303 | continue; |
| 304 | |
| 305 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
| 306 | |
| 307 | if (inf->pid == ptid_get_pid (inferior_ptid)) |
| 308 | ui_out_text (uiout, "* "); |
| 309 | else |
| 310 | ui_out_text (uiout, " "); |
| 311 | |
| 312 | ui_out_field_int (uiout, "id", inf->num); |
| 313 | ui_out_text (uiout, " "); |
| 314 | ui_out_field_int (uiout, "target-id", inf->pid); |
| 315 | |
| 316 | ui_out_text (uiout, "\n"); |
| 317 | do_cleanups (chain2); |
| 318 | } |
| 319 | |
| 320 | do_cleanups (old_chain); |
| 321 | } |
| 322 | |
| 323 | /* Print information about currently known inferiors. */ |
| 324 | |
| 325 | static void |
| 326 | info_inferiors_command (char *arg, int from_tty) |
| 327 | { |
| 328 | print_inferior (uiout, -1); |
| 329 | } |
| 330 | |
| 331 | /* Print notices when new inferiors are created and die. */ |
| 332 | static void |
| 333 | show_print_inferior_events (struct ui_file *file, int from_tty, |
| 334 | struct cmd_list_element *c, const char *value) |
| 335 | { |
| 336 | fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value); |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | _initialize_inferiors (void) |
| 341 | { |
| 342 | add_info ("inferiors", info_inferiors_command, |
| 343 | _("IDs of currently known inferiors.")); |
| 344 | |
| 345 | add_setshow_boolean_cmd ("inferior-events", no_class, |
| 346 | &print_inferior_events, _("\ |
| 347 | Set printing of inferior events (e.g., inferior start and exit)."), _("\ |
| 348 | Show printing of inferior events (e.g., inferior start and exit)."), NULL, |
| 349 | NULL, |
| 350 | show_print_inferior_events, |
| 351 | &setprintlist, &showprintlist); |
| 352 | } |