Re: [RFC] New threadnum command for breakpoints

Frédéric Riss
2006-07-31T20:07:07Z

[-- Attachment #1: Type: text/plain, Size: 1507 bytes --]

Le lundi 31 juillet 2006 Ã  16:00 +0200, Frederic RISS a Ã©crit :
> I'll follow up soon with a patch doing this.

Here's a code patch for discussion and a tentative doco patch. I've
chosen to initialize the variable early in handle_inferior_event, so
that it's set whatever happens. I thought that if we document it, it'd
rather be always set and not only for breakpoint condition evaluation.

I'm still wondering about 2 things:

1. Should it change on a user requested thread switch? That would be a
simple patch to thread.c:switch_to_thread. But maybe documenting it as
'the id of the last stopped thread' wouldn't be a bad idea. I don't
think GDB provides a way for the user to get this information (I mean
once he has selected another thread). This way 'thread $_gdb_thread'
would always bring you back to the stopped thread.

2. Should we add support for read-only convenience variables? Writing in
$_gdb_thread makes no sense... but it causes no harm either. We already
have $bpnum in the same situation. I see some ways to support this:
- Add lval_internalvar_readonly
- Add a read_only flag to struct internalvar and check it at
assignment time. Internal vars are already special-cased there,
so this should be easy. Would be my solution of choice.
- Don't create a real convenience variable, but add OP_THREADID
to expression.h and generate it in write_dollar_var. Also add
some API to infrun.c to export the stopped threadid.

Fred.

[-- Attachment #2: gdb_thread.patch --]
[-- Type: text/x-patch, Size: 1513 bytes --]

2006-07-31 Frederic Riss <frederic.riss@st.com>

* infrun.c (handle_inferior_event): Set the $_gdb_thread convenience
variable. Don't call breakpoint_thread_match anymore. Suggested by
Vladimir Prus <vladimir@codesourcery.com>

Index: infrun.c
===================================================================
RCS file: /cvs/src/src/gdb/infrun.c,v
retrieving revision 1.213
diff -u -p -r1.213 infrun.c
--- infrun.c 22 Jul 2006 14:48:03 -0000 1.213
+++ infrun.c 31 Jul 2006 19:26:33 -0000
@@ -1250,6 +1250,10 @@ handle_inferior_event (struct execution_

int sw_single_step_trap_p = 0;
int stopped_by_watchpoint = -1; /* Mark as unknown. */
+ static struct internalvar *gdb_thread_var = NULL;
+
+ if (gdb_thread_var == NULL)
+ gdb_thread_var = lookup_internalvar("_gdb_thread");

/* Cache the last pid/waitstatus. */
target_last_wait_ptid = ecs->ptid;
@@ -1307,6 +1311,10 @@ handle_inferior_event (struct execution_
ui_out_text (uiout, "]\n");
}

+ set_internalvar (gdb_thread_var,
+ value_from_longest (builtin_type_int,
+ pid_to_thread_id (ecs->ptid)));
+
switch (ecs->ws.kind)
{
case TARGET_WAITKIND_LOADED:
@@ -1589,8 +1597,6 @@ handle_inferior_event (struct execution_
if (breakpoints_inserted && breakpoint_here_p (stop_pc))
{
ecs->random_signal = 0;
- if (!breakpoint_thread_match (stop_pc, ecs->ptid))
- thread_hop_needed = 1;
}
else if (SOFTWARE_SINGLE_STEP_P () && singlestep_breakpoints_inserted_p)
{

[-- Attachment #3: gdb_thread_doc.patch --]
[-- Type: text/x-patch, Size: 1591 bytes --]

2006-07-31 Frederic Riss <frederic.riss@st.com>

* gdb.texinfo (Stopping and starting multi-thread programs): Mention
the $_gdb_thread convenience variable.

Index: gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.345
diff -u -p -r1.345 gdb.texinfo
--- gdb.texinfo 21 Jul 2006 14:46:55 -0000 1.345
+++ gdb.texinfo 31 Jul 2006 19:22:50 -0000
@@ -4208,8 +4208,10 @@ breakpoints on all threads, or on a part
@cindex breakpoints and threads
@cindex thread breakpoints
@kindex break @dots{} thread @var{threadno}
+@vindex $_gdb_thread@r{, convenience variable}
@item break @var{linespec} thread @var{threadno}
@itemx break @var{linespec} thread @var{threadno} if @dots{}
+@itemx break @var{linespec} if $_gdb_thread == @var{threadno} @r{[} && @dots@r{]}
@var{linespec} specifies source lines; there are several ways of
writing them, but the effect is always to specify some source line.

@@ -4231,6 +4233,15 @@ breakpoint condition, like this:
(@value{GDBP}) break frik.c:13 thread 28 if bartab > lim
@end smallexample

+Alternatively, you can use the @samp{$_gdb_thread} convenience variable in
+the breakpoint's condition. The @samp{$_gdb_thread} variable is set by
+@value{GDBN} to the thread identifier of the stopped thread. This can be used
+for example to make a breakpoint trigger in multiple threads, like this:
+
+@smallexample
+(@value{GDBP}) break frik.c:13 if $_gdb_thread == 8 ||Â  $_gdb_thread == 9
+@end smallexample
+
@end table

@cindex stopped threads