X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=gdb%2Fthread.c;h=e45b25750ebfcc3b8fc86fc5e2f78bcd961e445a;hb=0913ae5ad7ad465e36784ed7fcc8676f3e0e23b7;hp=29fc610e96e7dc097363d14f6a99455fa9a024a1;hpb=c0ecb95f3dc0c3e8d1545f0a37c0c3e537e1ea96;p=deliverable%2Fbinutils-gdb.git diff --git a/gdb/thread.c b/gdb/thread.c index 29fc610e96..e45b25750e 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1,6 +1,6 @@ /* Multi-process/thread control for GDB, the GNU debugger. - Copyright (C) 1986-2016 Free Software Foundation, Inc. + Copyright (C) 1986-2017 Free Software Foundation, Inc. Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA. @@ -43,6 +43,7 @@ #include "gdb_regex.h" #include "cli/cli-utils.h" #include "thread-fsm.h" +#include "tid-parse.h" /* Definition of struct thread_info exported to gdbthread.h. */ @@ -165,7 +166,7 @@ thread_cancel_execution_command (struct thread_info *thr) { if (thr->thread_fsm != NULL) { - thread_fsm_clean_up (thr->thread_fsm); + thread_fsm_clean_up (thr->thread_fsm, thr); thread_fsm_delete (thr->thread_fsm); thr->thread_fsm = NULL; } @@ -182,7 +183,7 @@ clear_thread_inferior_resources (struct thread_info *tp) delete_at_next_stop (&tp->control.exception_resume_breakpoint); delete_at_next_stop (&tp->control.single_step_breakpoints); - delete_longjmp_breakpoint_at_next_stop (tp->num); + delete_longjmp_breakpoint_at_next_stop (tp->global_num); bpstat_clear (&tp->control.stop_bpstat); @@ -226,18 +227,33 @@ init_thread_list (void) threads_executing = 0; } -/* Allocate a new thread with target id PTID and add it to the thread - list. */ +/* Allocate a new thread of inferior INF with target id PTID and add + it to the thread list. */ static struct thread_info * -new_thread (ptid_t ptid) +new_thread (struct inferior *inf, ptid_t ptid) { - struct thread_info *tp = XCNEW (struct thread_info); + struct thread_info *tp; + + gdb_assert (inf != NULL); + + tp = XCNEW (struct thread_info); tp->ptid = ptid; - tp->num = ++highest_thread_num; - tp->next = thread_list; - thread_list = tp; + tp->global_num = ++highest_thread_num; + tp->inf = inf; + tp->per_inf_num = ++inf->highest_thread_num; + + if (thread_list == NULL) + thread_list = tp; + else + { + struct thread_info *last; + + for (last = thread_list; last->next != NULL; last = last->next) + ; + last->next = tp; + } /* Nothing to follow yet. */ tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS; @@ -251,6 +267,8 @@ struct thread_info * add_thread_silent (ptid_t ptid) { struct thread_info *tp; + struct inferior *inf = find_inferior_ptid (ptid); + gdb_assert (inf != NULL); tp = find_thread_ptid (ptid); if (tp) @@ -268,7 +286,7 @@ add_thread_silent (ptid_t ptid) if (ptid_equal (inferior_ptid, ptid)) { - tp = new_thread (null_ptid); + tp = new_thread (inf, null_ptid); /* Make switch_to_thread not read from the thread. */ tp->state = THREAD_EXITED; @@ -292,7 +310,7 @@ add_thread_silent (ptid_t ptid) delete_thread (ptid); } - tp = new_thread (ptid); + tp = new_thread (inf, ptid); observer_notify_new_thread (tp); return tp; @@ -472,12 +490,24 @@ delete_thread_silent (ptid_t ptid) } struct thread_info * -find_thread_id (int num) +find_thread_global_id (int global_id) { struct thread_info *tp; for (tp = thread_list; tp; tp = tp->next) - if (tp->num == num) + if (tp->global_num == global_id) + return tp; + + return NULL; +} + +static struct thread_info * +find_thread_id (struct inferior *inf, int thr_num) +{ + struct thread_info *tp; + + for (tp = thread_list; tp; tp = tp->next) + if (tp->inf == inf && tp->per_inf_num == thr_num) return tp; return NULL; @@ -539,38 +569,38 @@ thread_count (void) } int -valid_thread_id (int num) +valid_global_thread_id (int global_id) { struct thread_info *tp; for (tp = thread_list; tp; tp = tp->next) - if (tp->num == num) + if (tp->global_num == global_id) return 1; return 0; } int -pid_to_thread_id (ptid_t ptid) +ptid_to_global_thread_id (ptid_t ptid) { struct thread_info *tp; for (tp = thread_list; tp; tp = tp->next) if (ptid_equal (tp->ptid, ptid)) - return tp->num; + return tp->global_num; return 0; } ptid_t -thread_id_to_pid (int num) +global_thread_id_to_ptid (int global_id) { - struct thread_info *thread = find_thread_id (num); + struct thread_info *thread = find_thread_global_id (global_id); if (thread) return thread->ptid; else - return pid_to_ptid (-1); + return minus_one_ptid; } int @@ -595,7 +625,7 @@ first_thread_of_process (int pid) for (tp = thread_list; tp; tp = tp->next) if (pid == -1 || ptid_get_pid (tp->ptid) == pid) - if (ret == NULL || tp->num < ret->num) + if (ret == NULL || tp->global_num < ret->global_num) ret = tp; return ret; @@ -679,17 +709,17 @@ do_captured_list_thread_ids (struct ui_out *uiout, void *arg) continue; if (ptid_equal (tp->ptid, inferior_ptid)) - current_thread = tp->num; + current_thread = tp->global_num; num++; - ui_out_field_int (uiout, "thread-id", tp->num); + uiout->field_int ("thread-id", tp->global_num); } do_cleanups (cleanup_chain); if (current_thread != -1) - ui_out_field_int (uiout, "current-thread-id", current_thread); - ui_out_field_int (uiout, "number-of-threads", num); + uiout->field_int ("current-thread-id", current_thread); + uiout->field_int ("number-of-threads", num); return GDB_RC_OK; } @@ -1089,6 +1119,28 @@ finish_thread_state_cleanup (void *arg) finish_thread_state (*ptid_p); } +/* See gdbthread.h. */ + +void +validate_registers_access (void) +{ + /* No selected thread, no registers. */ + if (ptid_equal (inferior_ptid, null_ptid)) + error (_("No thread selected.")); + + /* Don't try to read from a dead thread. */ + if (is_exited (inferior_ptid)) + error (_("The current thread has terminated")); + + /* ... or from a spinning thread. FIXME: This isn't actually fully + correct. It'll allow an user-requested access (e.g., "print $pc" + at the prompt) when a thread is not executing for some internal + reason, but is marked running from the user's perspective. E.g., + the thread is waiting for its turn in the step-over queue. */ + if (is_executing (inferior_ptid)) + error (_("Selected thread is running.")); +} + int pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread) { @@ -1096,25 +1148,61 @@ pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread) && pc < thread->control.step_range_end); } -/* Prints the list of threads and their details on UIOUT. - This is a version of 'info_threads_command' suitable for - use from MI. - If REQUESTED_THREAD is not -1, it's the GDB id of the thread - that should be printed. Otherwise, all threads are - printed. - If PID is not -1, only print threads from the process PID. - Otherwise, threads from all attached PIDs are printed. - If both REQUESTED_THREAD and PID are not -1, then the thread - is printed if it belongs to the specified process. Otherwise, - an error is raised. */ -void -print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) +/* Helper for print_thread_info. Returns true if THR should be + printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not + NULL, only print THR if its ID is included in the list. GLOBAL_IDS + is true if REQUESTED_THREADS is list of global IDs, false if a list + of per-inferior thread ids. If PID is not -1, only print THR if it + is a thread from the process PID. Otherwise, threads from all + attached PIDs are printed. If both REQUESTED_THREADS is not NULL + and PID is not -1, then the thread is printed if it belongs to the + specified process. Otherwise, an error is raised. */ + +static int +should_print_thread (const char *requested_threads, int default_inf_num, + int global_ids, int pid, struct thread_info *thr) +{ + if (requested_threads != NULL && *requested_threads != '\0') + { + int in_list; + + if (global_ids) + in_list = number_is_in_list (requested_threads, thr->global_num); + else + in_list = tid_is_in_list (requested_threads, default_inf_num, + thr->inf->num, thr->per_inf_num); + if (!in_list) + return 0; + } + + if (pid != -1 && ptid_get_pid (thr->ptid) != pid) + { + if (requested_threads != NULL && *requested_threads != '\0') + error (_("Requested thread not found in requested process")); + return 0; + } + + if (thr->state == THREAD_EXITED) + return 0; + + return 1; +} + +/* Like print_thread_info, but in addition, GLOBAL_IDS indicates + whether REQUESTED_THREADS is a list of global or per-inferior + thread ids. */ + +static void +print_thread_info_1 (struct ui_out *uiout, char *requested_threads, + int global_ids, int pid, + int show_global_ids) { struct thread_info *tp; ptid_t current_ptid; struct cleanup *old_chain; const char *extra_info, *name, *target_id; - int current_thread = -1; + struct inferior *inf; + int default_inf_num = current_inferior ()->num; update_thread_list (); current_ptid = inferior_ptid; @@ -1125,7 +1213,7 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) /* For backward compatibility, we make a list for MI. A table is preferable for the CLI, though, because it shows table headers. */ - if (ui_out_is_mi_like_p (uiout)) + if (uiout->is_mi_like_p ()) make_cleanup_ui_out_list_begin_end (uiout, "threads"); else { @@ -1133,13 +1221,8 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) for (tp = thread_list; tp; tp = tp->next) { - if (!number_is_in_list (requested_threads, tp->num)) - continue; - - if (pid != -1 && ptid_get_pid (tp->ptid) != pid) - continue; - - if (tp->state == THREAD_EXITED) + if (!should_print_thread (requested_threads, default_inf_num, + global_ids, pid, tp)) continue; ++n_threads; @@ -1148,63 +1231,62 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) if (n_threads == 0) { if (requested_threads == NULL || *requested_threads == '\0') - ui_out_message (uiout, 0, _("No threads.\n")); + uiout->message (_("No threads.\n")); else - ui_out_message (uiout, 0, _("No threads match '%s'.\n"), + uiout->message (_("No threads match '%s'.\n"), requested_threads); do_cleanups (old_chain); return; } - make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads"); + if (show_global_ids || uiout->is_mi_like_p ()) + make_cleanup_ui_out_table_begin_end (uiout, 5, n_threads, "threads"); + else + make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads"); + + uiout->table_header (1, ui_left, "current", ""); - ui_out_table_header (uiout, 1, ui_left, "current", ""); - ui_out_table_header (uiout, 4, ui_left, "id", "Id"); - ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id"); - ui_out_table_header (uiout, 1, ui_left, "frame", "Frame"); - ui_out_table_body (uiout); + if (!uiout->is_mi_like_p ()) + uiout->table_header (4, ui_left, "id-in-tg", "Id"); + if (show_global_ids || uiout->is_mi_like_p ()) + uiout->table_header (4, ui_left, "id", "GId"); + uiout->table_header (17, ui_left, "target-id", "Target Id"); + uiout->table_header (1, ui_left, "frame", "Frame"); + uiout->table_body (); } - for (tp = thread_list; tp; tp = tp->next) + ALL_THREADS_BY_INFERIOR (inf, tp) { struct cleanup *chain2; int core; - if (!number_is_in_list (requested_threads, tp->num)) - continue; - - if (pid != -1 && ptid_get_pid (tp->ptid) != pid) - { - if (requested_threads != NULL && *requested_threads != '\0') - error (_("Requested thread not found in requested process")); - continue; - } - - if (ptid_equal (tp->ptid, current_ptid)) - current_thread = tp->num; - - if (tp->state == THREAD_EXITED) + if (!should_print_thread (requested_threads, default_inf_num, + global_ids, pid, tp)) continue; chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); - if (ui_out_is_mi_like_p (uiout)) + if (uiout->is_mi_like_p ()) { /* Compatibility. */ if (ptid_equal (tp->ptid, current_ptid)) - ui_out_text (uiout, "* "); + uiout->text ("* "); else - ui_out_text (uiout, " "); + uiout->text (" "); } else { if (ptid_equal (tp->ptid, current_ptid)) - ui_out_field_string (uiout, "current", "*"); + uiout->field_string ("current", "*"); else - ui_out_field_skip (uiout, "current"); + uiout->field_skip ("current"); } - ui_out_field_int (uiout, "id", tp->num); + if (!uiout->is_mi_like_p ()) + uiout->field_string ("id-in-tg", print_thread_id (tp)); + + if (show_global_ids || uiout->is_mi_like_p ()) + uiout->field_int ("id", tp->global_num); /* For the CLI, we stuff everything into the target-id field. This is a gross hack to make the output come out looking @@ -1217,13 +1299,13 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) extra_info = target_extra_thread_info (tp); name = tp->name ? tp->name : target_thread_name (tp); - if (ui_out_is_mi_like_p (uiout)) + if (uiout->is_mi_like_p ()) { - ui_out_field_string (uiout, "target-id", target_id); + uiout->field_string ("target-id", target_id); if (extra_info) - ui_out_field_string (uiout, "details", extra_info); + uiout->field_string ("details", extra_info); if (name) - ui_out_field_string (uiout, "name", name); + uiout->field_string ("name", name); } else { @@ -1241,12 +1323,12 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) contents = xstrdup (target_id); str_cleanup = make_cleanup (xfree, contents); - ui_out_field_string (uiout, "target-id", contents); + uiout->field_string ("target-id", contents); do_cleanups (str_cleanup); } if (tp->state == THREAD_RUNNING) - ui_out_text (uiout, "(running)\n"); + uiout->text ("(running)\n"); else { /* The switch below puts us at the top of the stack (leaf @@ -1254,22 +1336,22 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) switch_to_thread (tp->ptid); print_stack_frame (get_selected_frame (NULL), /* For MI output, print frame level. */ - ui_out_is_mi_like_p (uiout), + uiout->is_mi_like_p (), LOCATION, 0); } - if (ui_out_is_mi_like_p (uiout)) + if (uiout->is_mi_like_p ()) { char *state = "stopped"; if (tp->state == THREAD_RUNNING) state = "running"; - ui_out_field_string (uiout, "state", state); + uiout->field_string ("state", state); } core = target_core_of_thread (tp->ptid); - if (ui_out_is_mi_like_p (uiout) && core != -1) - ui_out_field_int (uiout, "core", core); + if (uiout->is_mi_like_p () && core != -1) + uiout->field_int ("core", core); do_cleanups (chain2); } @@ -1280,27 +1362,35 @@ print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) if (pid == -1 && requested_threads == NULL) { - gdb_assert (current_thread != -1 - || !thread_list - || ptid_equal (inferior_ptid, null_ptid)); - if (current_thread != -1 && ui_out_is_mi_like_p (uiout)) - ui_out_field_int (uiout, "current-thread-id", current_thread); - - if (current_thread != -1 && is_exited (current_ptid)) - ui_out_message (uiout, 0, "\n\ -The current thread has terminated. See `help thread'.\n", - current_thread); - else if (thread_list - && current_thread == -1 - && ptid_equal (current_ptid, null_ptid)) - ui_out_message (uiout, 0, "\n\ + if (uiout->is_mi_like_p () + && !ptid_equal (inferior_ptid, null_ptid)) + { + int num = ptid_to_global_thread_id (inferior_ptid); + + gdb_assert (num != 0); + uiout->field_int ("current-thread-id", num); + } + + if (!ptid_equal (inferior_ptid, null_ptid) && is_exited (inferior_ptid)) + uiout->message ("\n\ +The current thread has terminated. See `help thread'.\n", + print_thread_id (inferior_thread ())); + else if (thread_list != NULL + && ptid_equal (inferior_ptid, null_ptid)) + uiout->message ("\n\ No selected thread. See `help thread'.\n"); } } -/* Print information about currently known threads +/* See gdbthread.h. */ + +void +print_thread_info (struct ui_out *uiout, char *requested_threads, int pid) +{ + print_thread_info_1 (uiout, requested_threads, 1, pid, 0); +} - Optional ARG is a thread id, or list of thread ids. +/* Implementation of the "info threads" command. Note: this has the drawback that it _really_ switches threads, which frees the frame cache. A no-side @@ -1309,7 +1399,16 @@ No selected thread. See `help thread'.\n"); static void info_threads_command (char *arg, int from_tty) { - print_thread_info (current_uiout, arg, -1); + int show_global_ids = 0; + + if (arg != NULL + && check_for_argument (&arg, "-gid", sizeof ("-gid") - 1)) + { + arg = skip_spaces (arg); + show_global_ids = 1; + } + + print_thread_info_1 (current_uiout, arg, 0, -1, show_global_ids); } /* See gdbthread.h. */ @@ -1416,7 +1515,7 @@ restore_selected_frame (struct frame_id a_frame_id, int frame_level) select_frame (get_current_frame ()); /* Warn the user. */ - if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout)) + if (frame_level > 0 && !current_uiout->is_mi_like_p ()) { warning (_("Couldn't restore frame #%d in " "current thread. Bottom (innermost) frame selected:"), @@ -1579,23 +1678,59 @@ make_cleanup_restore_current_thread (void) restore_current_thread_cleanup_dtor); } +/* See gdbthread.h. */ + +int +show_thread_that_caused_stop (void) +{ + return highest_thread_num > 1; +} + +/* See gdbthread.h. */ + +int +show_inferior_qualified_tids (void) +{ + return (inferior_list->next != NULL || inferior_list->num != 1); +} + +/* See gdbthread.h. */ + +const char * +print_thread_id (struct thread_info *thr) +{ + char *s = get_print_cell (); + + if (show_inferior_qualified_tids ()) + xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num); + else + xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num); + return s; +} + /* If non-zero tp_array_compar should sort in ascending order, otherwise in descending order. */ static int tp_array_compar_ascending; -/* Sort an array for struct thread_info pointers by their NUM, order is - determined by TP_ARRAY_COMPAR_ASCENDING. */ +/* Sort an array for struct thread_info pointers by thread ID (first + by inferior number, and then by per-inferior thread number). The + order is determined by TP_ARRAY_COMPAR_ASCENDING. */ static int tp_array_compar (const void *ap_voidp, const void *bp_voidp) { - const struct thread_info *const *ap - = (const struct thread_info * const*) ap_voidp; - const struct thread_info *const *bp - = (const struct thread_info * const*) bp_voidp; + const struct thread_info *a = *(const struct thread_info * const *) ap_voidp; + const struct thread_info *b = *(const struct thread_info * const *) bp_voidp; - return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num)) + if (a->inf->num != b->inf->num) + { + return (((a->inf->num > b->inf->num) - (a->inf->num < b->inf->num)) + * (tp_array_compar_ascending ? +1 : -1)); + } + + return (((a->per_inf_num > b->per_inf_num) + - (a->per_inf_num < b->per_inf_num)) * (tp_array_compar_ascending ? +1 : -1)); } @@ -1669,8 +1804,8 @@ thread_apply_all_command (char *cmd, int from_tty) if (thread_alive (tp_array[k])) { switch_to_thread (tp_array[k]->ptid); - printf_filtered (_("\nThread %d (%s):\n"), - tp_array[k]->num, + printf_filtered (_("\nThread %s (%s):\n"), + print_thread_id (tp_array[k]), target_pid_to_str (inferior_ptid)); execute_command (cmd, from_tty); @@ -1682,54 +1817,98 @@ thread_apply_all_command (char *cmd, int from_tty) do_cleanups (old_chain); } +/* Implementation of the "thread apply" command. */ + static void thread_apply_command (char *tidlist, int from_tty) { - char *cmd; + char *cmd = NULL; struct cleanup *old_chain; char *saved_cmd; - struct get_number_or_range_state state; + tid_range_parser parser; if (tidlist == NULL || *tidlist == '\000') error (_("Please specify a thread ID list")); - for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++); + parser.init (tidlist, current_inferior ()->num); + while (!parser.finished ()) + { + int inf_num, thr_start, thr_end; - if (*cmd == '\000') + if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end)) + { + cmd = (char *) parser.cur_tok (); + break; + } + } + + if (cmd == NULL) error (_("Please specify a command following the thread ID list")); + if (tidlist == cmd || !isalpha (cmd[0])) + invalid_thread_id_error (cmd); + /* Save a copy of the command in case it is clobbered by execute_command. */ saved_cmd = xstrdup (cmd); old_chain = make_cleanup (xfree, saved_cmd); - init_number_or_range (&state, tidlist); - while (!state.finished && state.string < cmd) + make_cleanup_restore_current_thread (); + + parser.init (tidlist, current_inferior ()->num); + while (!parser.finished () && parser.cur_tok () < cmd) { - struct thread_info *tp; - int start; + struct thread_info *tp = NULL; + struct inferior *inf; + int inf_num, thr_num; - start = get_number_or_range (&state); + parser.get_tid (&inf_num, &thr_num); + inf = find_inferior_id (inf_num); + if (inf != NULL) + tp = find_thread_id (inf, thr_num); - make_cleanup_restore_current_thread (); + if (parser.in_star_range ()) + { + if (inf == NULL) + { + warning (_("Unknown inferior %d"), inf_num); + parser.skip_range (); + continue; + } - tp = find_thread_id (start); + /* No use looking for threads past the highest thread number + the inferior ever had. */ + if (thr_num >= inf->highest_thread_num) + parser.skip_range (); - if (!tp) - warning (_("Unknown thread %d."), start); - else if (!thread_alive (tp)) - warning (_("Thread %d has terminated."), start); - else - { - switch_to_thread (tp->ptid); + /* Be quiet about unknown threads numbers. */ + if (tp == NULL) + continue; + } - printf_filtered (_("\nThread %d (%s):\n"), tp->num, - target_pid_to_str (inferior_ptid)); - execute_command (cmd, from_tty); + if (tp == NULL) + { + if (show_inferior_qualified_tids () || parser.tid_is_qualified ()) + warning (_("Unknown thread %d.%d"), inf_num, thr_num); + else + warning (_("Unknown thread %d"), thr_num); + continue; + } - /* Restore exact command used previously. */ - strcpy (cmd, saved_cmd); + if (!thread_alive (tp)) + { + warning (_("Thread %s has terminated."), print_thread_id (tp)); + continue; } + + switch_to_thread (tp->ptid); + + printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp), + target_pid_to_str (inferior_ptid)); + execute_command (cmd, from_tty); + + /* Restore exact command used previously. */ + strcpy (cmd, saved_cmd); } do_cleanups (old_chain); @@ -1741,28 +1920,51 @@ thread_apply_command (char *tidlist, int from_tty) void thread_command (char *tidstr, int from_tty) { - if (!tidstr) + if (tidstr == NULL) { if (ptid_equal (inferior_ptid, null_ptid)) error (_("No thread selected")); if (target_has_stack) { + struct thread_info *tp = inferior_thread (); + if (is_exited (inferior_ptid)) - printf_filtered (_("[Current thread is %d (%s) (exited)]\n"), - pid_to_thread_id (inferior_ptid), + printf_filtered (_("[Current thread is %s (%s) (exited)]\n"), + print_thread_id (tp), target_pid_to_str (inferior_ptid)); else - printf_filtered (_("[Current thread is %d (%s)]\n"), - pid_to_thread_id (inferior_ptid), + printf_filtered (_("[Current thread is %s (%s)]\n"), + print_thread_id (tp), target_pid_to_str (inferior_ptid)); } else error (_("No stack.")); - return; } + else + { + ptid_t previous_ptid = inferior_ptid; + enum gdb_rc result; + + result = gdb_thread_select (current_uiout, tidstr, NULL); - gdb_thread_select (current_uiout, tidstr, NULL); + /* If thread switch did not succeed don't notify or print. */ + if (result == GDB_RC_FAIL) + return; + + /* Print if the thread has not changed, otherwise an event will be sent. */ + if (ptid_equal (inferior_ptid, previous_ptid)) + { + print_selected_thread_frame (current_uiout, + USER_SELECTED_THREAD + | USER_SELECTED_FRAME); + } + else + { + observer_notify_user_selected_context_changed (USER_SELECTED_THREAD + | USER_SELECTED_FRAME); + } + } } /* Implementation of `thread name'. */ @@ -1803,32 +2005,32 @@ thread_find_command (char *arg, int from_tty) { if (tp->name != NULL && re_exec (tp->name)) { - printf_filtered (_("Thread %d has name '%s'\n"), - tp->num, tp->name); + printf_filtered (_("Thread %s has name '%s'\n"), + print_thread_id (tp), tp->name); match++; } tmp = target_thread_name (tp); if (tmp != NULL && re_exec (tmp)) { - printf_filtered (_("Thread %d has target name '%s'\n"), - tp->num, tmp); + printf_filtered (_("Thread %s has target name '%s'\n"), + print_thread_id (tp), tmp); match++; } tmp = target_pid_to_str (tp->ptid); if (tmp != NULL && re_exec (tmp)) { - printf_filtered (_("Thread %d has target id '%s'\n"), - tp->num, tmp); + printf_filtered (_("Thread %s has target id '%s'\n"), + print_thread_id (tp), tmp); match++; } tmp = target_extra_thread_info (tp); if (tmp != NULL && re_exec (tmp)) { - printf_filtered (_("Thread %d has extra info '%s'\n"), - tp->num, tmp); + printf_filtered (_("Thread %s has extra info '%s'\n"), + print_thread_id (tp), tmp); match++; } } @@ -1848,41 +2050,32 @@ show_print_thread_events (struct ui_file *file, int from_tty, } static int -do_captured_thread_select (struct ui_out *uiout, void *tidstr) +do_captured_thread_select (struct ui_out *uiout, void *tidstr_v) { - int num; + const char *tidstr = (const char *) tidstr_v; struct thread_info *tp; - num = value_as_long (parse_and_eval ((const char *) tidstr)); - - tp = find_thread_id (num); + if (uiout->is_mi_like_p ()) + { + int num = value_as_long (parse_and_eval (tidstr)); - if (!tp) - error (_("Thread ID %d not known."), num); + tp = find_thread_global_id (num); + if (tp == NULL) + error (_("Thread ID %d not known."), num); + } + else + { + tp = parse_thread_id (tidstr, NULL); + gdb_assert (tp != NULL); + } if (!thread_alive (tp)) - error (_("Thread ID %d has terminated."), num); + error (_("Thread ID %s has terminated."), tidstr); switch_to_thread (tp->ptid); annotate_thread_changed (); - ui_out_text (uiout, "[Switching to thread "); - ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid)); - ui_out_text (uiout, " ("); - ui_out_text (uiout, target_pid_to_str (inferior_ptid)); - ui_out_text (uiout, ")]"); - - /* Note that we can't reach this with an exited thread, due to the - thread_alive check above. */ - if (tp->state == THREAD_RUNNING) - ui_out_text (uiout, "(running)\n"); - else - { - ui_out_text (uiout, "\n"); - print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); - } - /* Since the current thread may have changed, see if there is any exited thread we can now delete. */ prune_threads (); @@ -1890,6 +2083,48 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr) return GDB_RC_OK; } +/* Print thread and frame switch command response. */ + +void +print_selected_thread_frame (struct ui_out *uiout, + user_selected_what selection) +{ + struct thread_info *tp = inferior_thread (); + struct inferior *inf = current_inferior (); + + if (selection & USER_SELECTED_THREAD) + { + if (uiout->is_mi_like_p ()) + { + uiout->field_int ("new-thread-id", + inferior_thread ()->global_num); + } + else + { + uiout->text ("[Switching to thread "); + uiout->field_string ("new-thread-id", print_thread_id (tp)); + uiout->text (" ("); + uiout->text (target_pid_to_str (inferior_ptid)); + uiout->text (")]"); + } + } + + if (tp->state == THREAD_RUNNING) + { + if (selection & USER_SELECTED_THREAD) + uiout->text ("(running)\n"); + } + else if (selection & USER_SELECTED_FRAME) + { + if (selection & USER_SELECTED_THREAD) + uiout->text ("\n"); + + if (has_stack_frames ()) + print_stack_frame_to_uiout (uiout, get_selected_frame (NULL), + 1, SRC_AND_LOC, 1); + } +} + enum gdb_rc gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message) { @@ -1925,17 +2160,45 @@ update_thread_list (void) update_threads_executing (); } -/* Return a new value for the selected thread's id. Return a value of 0 if - no thread is selected, or no threads exist. */ +/* Return a new value for the selected thread's id. Return a value of + 0 if no thread is selected. If GLOBAL is true, return the thread's + global number. Otherwise return the per-inferior number. */ static struct value * -thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, - void *ignore) +thread_num_make_value_helper (struct gdbarch *gdbarch, int global) { struct thread_info *tp = find_thread_ptid (inferior_ptid); + int int_val; + + if (tp == NULL) + int_val = 0; + else if (global) + int_val = tp->global_num; + else + int_val = tp->per_inf_num; + + return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val); +} + +/* Return a new value for the selected thread's per-inferior thread + number. Return a value of 0 if no thread is selected, or no + threads exist. */ + +static struct value * +thread_id_per_inf_num_make_value (struct gdbarch *gdbarch, struct internalvar *var, + void *ignore) +{ + return thread_num_make_value_helper (gdbarch, 0); +} - return value_from_longest (builtin_type (gdbarch)->builtin_int, - (tp ? tp->num : 0)); +/* Return a new value for the selected thread's global id. Return a + value of 0 if no thread is selected, or no threads exist. */ + +static struct value * +global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, + void *ignore) +{ + return thread_num_make_value_helper (gdbarch, 1); } /* Commands with a prefix of `thread'. */ @@ -1945,7 +2208,16 @@ struct cmd_list_element *thread_cmd_list = NULL; static const struct internalvar_funcs thread_funcs = { - thread_id_make_value, + thread_id_per_inf_num_make_value, + NULL, + NULL +}; + +/* Implementation of `gthread' variable. */ + +static const struct internalvar_funcs gthread_funcs = +{ + global_thread_id_make_value, NULL, NULL }; @@ -1957,9 +2229,10 @@ _initialize_thread (void) add_info ("threads", info_threads_command, _("Display currently known threads.\n\ -Usage: info threads [ID]...\n\ -Optional arguments are thread IDs with spaces between.\n\ -If no arguments, all threads are displayed.")); +Usage: info threads [-gid] [ID]...\n\ +-gid: Show global thread IDs.\n\ +If ID is given, it is a space-separated list of IDs of threads to display.\n\ +Otherwise, all threads are displayed.")); add_prefix_cmd ("thread", class_run, thread_command, _("\ Use this command to switch between threads.\n\ @@ -2002,6 +2275,7 @@ Show printing of thread events (such as thread start and exit)."), NULL, &setprintlist, &showprintlist); create_internalvar_type_lazy ("_thread", &thread_funcs, NULL); + create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL); observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed); }