[PATCH v3 3/7] Centralize thread ID printing

Pedro Alves
2016-01-06T13:03:55Z

Add a new function to print a thread ID, in the style of paddress,
plongest, etc. and adjust all CLI-reachable paths to use it.

This gives us a single place to tweak to print inferior-qualified
thread IDs later:

- [Switching to thread 1 (Thread 0x7ffff7fc2740 (LWP 8155))]
+ [Switching to thread 1.1 (Thread 0x7ffff7fc2740 (LWP 8155))]

etc., though for now, this has no user-visible change.

No regressions on x86_64 Fedora 20.

gdb/ChangeLog:
2016-01-06 Pedro Alves <palves@redhat.com>

* breakpoint.c (remove_threaded_breakpoints)
(print_one_breakpoint_location): Use print_thread_id.
* btrace.c (btrace_enable, btrace_disable, btrace_teardown)
(btrace_fetch, btrace_clear): Use print_thread_id.
* common/print-utils.c (CELLSIZE): Delete.
(get_cell): Rename to ...
(get_print_cell): ... this and made extern. Adjust call callers.
Adjust to use PRINT_CELL_SIZE.
* common/print-utils.h (get_print_cell): Declare.
(PRINT_CELL_SIZE): New.
* gdbthread.h (print_thread_id): Declare.
* infcmd.c (signal_command): Use print_thread_id.
* inferior.c (print_inferior): Use print_thread_id.
* infrun.c (handle_signal_stop)
(insert_exception_resume_breakpoint)
(insert_exception_resume_from_probe)
(print_signal_received_reason): Use print_thread_id.
* record-btrace.c (record_btrace_info)
(record_btrace_resume_thread, record_btrace_cancel_resume)
(record_btrace_step_thread, record_btrace_wait): Use
print_thread_id.
* thread.c (thread_apply_all_command): Use print_thread_id.
(print_thread_id): New function.
(thread_apply_command): Use print_thread_id.
(thread_command, thread_find_command, do_captured_thread_select):
Use print_thread_id.
---
gdb/breakpoint.c | 8 ++++--
gdb/btrace.c | 15 ++++++----
gdb/common/print-utils.c | 71 ++++++++++++++++++++++++------------------------
gdb/common/print-utils.h | 8 ++++++
gdb/gdbthread.h | 4 +++
gdb/infcmd.c | 8 +++---
gdb/inferior.c | 4 +--
gdb/infrun.c | 2 +-
gdb/record-btrace.c | 14 ++++++----
gdb/thread.c | 45 ++++++++++++++++++++----------
10 files changed, 108 insertions(+), 71 deletions(-)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index d72021e..b2a3c47 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -3249,8 +3249,8 @@ remove_threaded_breakpoints (struct thread_info *tp, int silent)
b->disposition = disp_del_at_next_stop;

printf_filtered (_("\
-Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
- b->number, tp->num);
+Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
+ b->number, print_thread_id (tp));

/* Hide it from the user. */
b->number = 0;
@@ -6514,9 +6514,11 @@ print_one_breakpoint_location (struct breakpoint *b,

if (!part_of_multiple && b->thread != -1)
{
+ struct thread_info *thr = find_thread_id (b->thread);
+
/* FIXME should make an annotation for this. */
ui_out_text (uiout, "\tstop only in thread ");
- ui_out_field_int (uiout, "thread", b->thread);
+ ui_out_field_string (uiout, "thread", print_thread_id (thr));
ui_out_text (uiout, "\n");
}

diff --git a/gdb/btrace.c b/gdb/btrace.c
index 4c88ddd..dfcc3c8 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -1044,7 +1044,8 @@ btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
if (!target_supports_btrace (conf->format))
error (_("Target does not support branch tracing."));

- DEBUG ("enable thread %d (%s)", tp->num, target_pid_to_str (tp->ptid));
+ DEBUG ("enable thread %s (%s)", print_thread_id (tp),
+ target_pid_to_str (tp->ptid));

tp->btrace.target = target_enable_btrace (tp->ptid, conf);

@@ -1076,7 +1077,8 @@ btrace_disable (struct thread_info *tp)
if (btp->target == NULL)
return;

- DEBUG ("disable thread %d (%s)", tp->num, target_pid_to_str (tp->ptid));
+ DEBUG ("disable thread %s (%s)", print_thread_id (tp),
+ target_pid_to_str (tp->ptid));

target_disable_btrace (btp->target);
btp->target = NULL;
@@ -1095,7 +1097,8 @@ btrace_teardown (struct thread_info *tp)
if (btp->target == NULL)
return;

- DEBUG ("teardown thread %d (%s)", tp->num, target_pid_to_str (tp->ptid));
+ DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
+ target_pid_to_str (tp->ptid));

target_teardown_btrace (btp->target);
btp->target = NULL;
@@ -1269,7 +1272,8 @@ btrace_fetch (struct thread_info *tp)
struct cleanup *cleanup;
int errcode;

- DEBUG ("fetch thread %d (%s)", tp->num, target_pid_to_str (tp->ptid));
+ DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
+ target_pid_to_str (tp->ptid));

btinfo = &tp->btrace;
tinfo = btinfo->target;
@@ -1341,7 +1345,8 @@ btrace_clear (struct thread_info *tp)
struct btrace_thread_info *btinfo;
struct btrace_function *it, *trash;

- DEBUG ("clear thread %d (%s)", tp->num, target_pid_to_str (tp->ptid));
+ DEBUG ("clear thread %s (%s)", print_thread_id (tp),
+ target_pid_to_str (tp->ptid));

/* Make sure btrace frames that may hold a pointer into the branch
trace data are destroyed. */
diff --git a/gdb/common/print-utils.c b/gdb/common/print-utils.c
index 5bb83b5..1a21404 100644
--- a/gdb/common/print-utils.c
+++ b/gdb/common/print-utils.c
@@ -21,15 +21,15 @@
#include "print-utils.h"
/* Temporary storage using circular buffer. */

+/* Number of cells in the circular buffer. */
#define NUMCELLS 16
-#define CELLSIZE 50

/* Return the next entry in the circular buffer. */

-static char *
-get_cell (void)
+char *
+get_print_cell (void)
{
- static char buf[NUMCELLS][CELLSIZE];
+ static char buf[NUMCELLS][PRINT_CELL_SIZE];
static int cell = 0;

if (++cell >= NUMCELLS)
@@ -43,7 +43,7 @@ decimal2str (char *sign, ULONGEST addr, int width)
/* Steal code from valprint.c:print_decimal(). Should this worry
about the real size of addr as the above does? */
unsigned long temp[3];
- char *str = get_cell ();
+ char *str = get_print_cell ();
int i = 0;

do
@@ -62,14 +62,14 @@ decimal2str (char *sign, ULONGEST addr, int width)
switch (i)
{
case 1:
- xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
+ xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu", sign, width, temp[0]);
break;
case 2:
- xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
+ xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu%09lu", sign, width,
temp[1], temp[0]);
break;
case 3:
- xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
+ xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu%09lu%09lu", sign, width,
temp[2], temp[1], temp[0]);
break;
default:
@@ -84,7 +84,7 @@ static char *
octal2str (ULONGEST addr, int width)
{
unsigned long temp[3];
- char *str = get_cell ();
+ char *str = get_print_cell ();
int i = 0;

do
@@ -104,15 +104,15 @@ octal2str (ULONGEST addr, int width)
{
case 1:
if (temp[0] == 0)
- xsnprintf (str, CELLSIZE, "%*o", width, 0);
+ xsnprintf (str, PRINT_CELL_SIZE, "%*o", width, 0);
else
- xsnprintf (str, CELLSIZE, "0%0*lo", width, temp[0]);
+ xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo", width, temp[0]);
break;
case 2:
- xsnprintf (str, CELLSIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
+ xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
break;
case 3:
- xsnprintf (str, CELLSIZE, "0%0*lo%010lo%010lo", width,
+ xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo%010lo%010lo", width,
temp[2], temp[1], temp[0]);
break;
default:
@@ -155,18 +155,18 @@ phex (ULONGEST l, int sizeof_l)
switch (sizeof_l)
{
case 8:
- str = get_cell ();
- xsnprintf (str, CELLSIZE, "%08lx%08lx",
+ str = get_print_cell ();
+ xsnprintf (str, PRINT_CELL_SIZE, "%08lx%08lx",
(unsigned long) (l >> thirty_two),
(unsigned long) (l & 0xffffffff));
break;
case 4:
- str = get_cell ();
- xsnprintf (str, CELLSIZE, "%08lx", (unsigned long) l);
+ str = get_print_cell ();
+ xsnprintf (str, PRINT_CELL_SIZE, "%08lx", (unsigned long) l);
break;
case 2:
- str = get_cell ();
- xsnprintf (str, CELLSIZE, "%04x", (unsigned short) (l & 0xffff));
+ str = get_print_cell ();
+ xsnprintf (str, PRINT_CELL_SIZE, "%04x", (unsigned short) (l & 0xffff));
break;
default:
str = phex (l, sizeof (l));
@@ -189,22 +189,22 @@ phex_nz (ULONGEST l, int sizeof_l)
{
unsigned long high = (unsigned long) (l >> thirty_two);

- str = get_cell ();
+ str = get_print_cell ();
if (high == 0)
- xsnprintf (str, CELLSIZE, "%lx",
+ xsnprintf (str, PRINT_CELL_SIZE, "%lx",
(unsigned long) (l & 0xffffffff));
else
- xsnprintf (str, CELLSIZE, "%lx%08lx", high,
+ xsnprintf (str, PRINT_CELL_SIZE, "%lx%08lx", high,
(unsigned long) (l & 0xffffffff));
break;
}
case 4:
- str = get_cell ();
- xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
+ str = get_print_cell ();
+ xsnprintf (str, PRINT_CELL_SIZE, "%lx", (unsigned long) l);
break;
case 2:
- str = get_cell ();
- xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
+ str = get_print_cell ();
+ xsnprintf (str, PRINT_CELL_SIZE, "%x", (unsigned short) (l & 0xffff));
break;
default:
str = phex_nz (l, sizeof (l));
@@ -219,9 +219,9 @@ phex_nz (ULONGEST l, int sizeof_l)
char *
hex_string (LONGEST num)
{
- char *result = get_cell ();
+ char *result = get_print_cell ();

- xsnprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num)));
+ xsnprintf (result, PRINT_CELL_SIZE, "0x%s", phex_nz (num, sizeof (num)));
return result;
}

@@ -230,14 +230,14 @@ hex_string (LONGEST num)
char *
hex_string_custom (LONGEST num, int width)
{
- char *result = get_cell ();
- char *result_end = result + CELLSIZE - 1;
+ char *result = get_print_cell ();
+ char *result_end = result + PRINT_CELL_SIZE - 1;
const char *hex = phex_nz (num, sizeof (num));
int hex_len = strlen (hex);

if (hex_len > width)
width = hex_len;
- if (width + 2 >= CELLSIZE)
+ if (width + 2 >= PRINT_CELL_SIZE)
internal_error (__FILE__, __LINE__, _("\
hex_string_custom: insufficient space to store result"));

@@ -294,7 +294,7 @@ int_string (LONGEST val, int radix, int is_signed, int width,
const char *
core_addr_to_string (const CORE_ADDR addr)
{
- char *str = get_cell ();
+ char *str = get_print_cell ();

strcpy (str, "0x");
strcat (str, phex (addr, sizeof (addr)));
@@ -306,7 +306,7 @@ core_addr_to_string (const CORE_ADDR addr)
const char *
core_addr_to_string_nz (const CORE_ADDR addr)
{
- char *str = get_cell ();
+ char *str = get_print_cell ();

strcpy (str, "0x");
strcat (str, phex_nz (addr, sizeof (addr)));
@@ -318,8 +318,9 @@ core_addr_to_string_nz (const CORE_ADDR addr)
const char *
host_address_to_string_1 (const void *addr)
{
- char *str = get_cell ();
+ char *str = get_print_cell ();

- xsnprintf (str, CELLSIZE, "0x%s", phex_nz ((uintptr_t) addr, sizeof (addr)));
+ xsnprintf (str, PRINT_CELL_SIZE, "0x%s",
+ phex_nz ((uintptr_t) addr, sizeof (addr)));
return str;
}
diff --git a/gdb/common/print-utils.h b/gdb/common/print-utils.h
index 706877b..8790d0b 100644
--- a/gdb/common/print-utils.h
+++ b/gdb/common/print-utils.h
@@ -20,6 +20,10 @@
#ifndef COMMON_CELLS_H
#define COMMON_CELLS_H

+/* How many characters (including the terminating null byte) fit in a
+ cell. */
+#define PRINT_CELL_SIZE 50
+
/* %d for LONGEST. The result is stored in a circular static buffer,
NUMCELLS deep. */

@@ -71,4 +75,8 @@ extern const char *host_address_to_string_1 (const void *addr);
#define host_address_to_string(ADDR) \
host_address_to_string_1 ((const void *) (ADDR))

+/* Return the next entry in the circular print buffer. */
+
+extern char *get_print_cell (void);
+
#endif /* COMMON_CELLS_H */
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index b75184b..e1e264a 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -361,6 +361,10 @@ extern ptid_t thread_id_to_pid (int);
into the integer thread id (GDB's homegrown id, not the system's). */
extern int pid_to_thread_id (ptid_t ptid);

+/* Return a string version of THR's thread ID. The result is stored
+ in a circular static buffer, NUMCELLS deep. */
+const char *print_thread_id (struct thread_info *thr);
+
/* Boolean test for an already-known pid (which may be overloaded with
extra thread information). */
extern int in_thread_list (ptid_t ptid);
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 8a71283..d46e8a7 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -1316,8 +1316,8 @@ signal_command (char *signum_exp, int from_tty)
{
if (!must_confirm)
printf_unfiltered (_("Note:\n"));
- printf_unfiltered (_(" Thread %d previously stopped with signal %s, %s.\n"),
- tp->num,
+ printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
+ print_thread_id (tp),
gdb_signal_to_name (tp->suspend.stop_signal),
gdb_signal_to_string (tp->suspend.stop_signal));
must_confirm = 1;
@@ -1325,10 +1325,10 @@ signal_command (char *signum_exp, int from_tty)
}

if (must_confirm
- && !query (_("Continuing thread %d (the current thread) with specified signal will\n"
+ && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
"still deliver the signals noted above to their respective threads.\n"
"Continue anyway? "),
- inferior_thread ()->num))
+ print_thread_id (inferior_thread ())))
error (_("Not confirmed."));
}

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 8f9ac33..45b3141 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -748,8 +748,8 @@ inferior_command (char *args, int from_tty)
switch_to_thread (tp->ptid);
}

- printf_filtered (_("[Switching to thread %d (%s)] "),
- pid_to_thread_id (inferior_ptid),
+ printf_filtered (_("[Switching to thread %s (%s)] "),
+ print_thread_id (inferior_thread ()),
target_pid_to_str (inferior_ptid));
}
else
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 0c61d26..001e7b4 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -7905,7 +7905,7 @@ print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
ui_out_text (uiout, "\n[");
ui_out_field_string (uiout, "thread-name",
target_pid_to_str (t->ptid));
- ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
+ ui_out_field_fmt (uiout, "thread-id", "] #%s", print_thread_id (t));
ui_out_text (uiout, " stopped");
}
else
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index a0b8836..8c33010 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -438,8 +438,8 @@ record_btrace_info (struct target_ops *self)
}

printf_unfiltered (_("Recorded %u instructions in %u functions (%u gaps) "
- "for thread %d (%s).\n"), insns, calls, gaps,
- tp->num, target_pid_to_str (tp->ptid));
+ "for thread %s (%s).\n"), insns, calls, gaps,
+ print_thread_id (tp), target_pid_to_str (tp->ptid));

if (btrace_is_replaying (tp))
printf_unfiltered (_("Replay in progress. At instruction %u.\n"),
@@ -1864,7 +1864,7 @@ record_btrace_resume_thread (struct thread_info *tp,
{
struct btrace_thread_info *btinfo;

- DEBUG ("resuming thread %d (%s): %x (%s)", tp->num,
+ DEBUG ("resuming thread %s (%s): %x (%s)", print_thread_id (tp),
target_pid_to_str (tp->ptid), flag, btrace_thread_flag_to_str (flag));

btinfo = &tp->btrace;
@@ -2131,7 +2131,8 @@ record_btrace_cancel_resume (struct thread_info *tp)
if (flags == 0)
return;

- DEBUG ("cancel resume thread %d (%s): %x (%s)", tp->num,
+ DEBUG ("cancel resume thread %s (%s): %x (%s)",
+ print_thread_id (tp),
target_pid_to_str (tp->ptid), flags,
btrace_thread_flag_to_str (flags));

@@ -2354,7 +2355,7 @@ record_btrace_step_thread (struct thread_info *tp)
flags = btinfo->flags & (BTHR_MOVE | BTHR_STOP);
btinfo->flags &= ~(BTHR_MOVE | BTHR_STOP);

- DEBUG ("stepping thread %d (%s): %x (%s)", tp->num,
+ DEBUG ("stepping thread %s (%s): %x (%s)", print_thread_id (tp),
target_pid_to_str (tp->ptid), flags,
btrace_thread_flag_to_str (flags));

@@ -2563,7 +2564,8 @@ record_btrace_wait (struct target_ops *ops, ptid_t ptid,
/* We moved the replay position but did not update registers. */
registers_changed_ptid (eventing->ptid);

- DEBUG ("wait ended by thread %d (%s): %s", eventing->num,
+ DEBUG ("wait ended by thread %s (%s): %s",
+ print_thread_id (eventing),
target_pid_to_str (eventing->ptid),
target_waitstatus_to_string (status));

diff --git a/gdb/thread.c b/gdb/thread.c
index 4c2259f..87861d5 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1588,6 +1588,17 @@ make_cleanup_restore_current_thread (void)
restore_current_thread_cleanup_dtor);
}

+/* See gdbthread.h. */
+
+const char *
+print_thread_id (struct thread_info *thr)
+{
+ char *s = get_print_cell ();
+
+ xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->num);
+ return s;
+}
+
/* If non-zero tp_array_compar should sort in ascending order, otherwise in
descending order. */

@@ -1678,8 +1689,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);

@@ -1691,6 +1702,8 @@ 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)
{
@@ -1757,13 +1770,15 @@ thread_command (char *tidstr, int from_tty)

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
@@ -1812,32 +1827,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++;
}
}
@@ -1877,7 +1892,7 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr)
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_field_string (uiout, "new-thread-id", print_thread_id (tp));
ui_out_text (uiout, " (");
ui_out_text (uiout, target_pid_to_str (inferior_ptid));
ui_out_text (uiout, ")]");
--
1.9.3