This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] $thread convenience variable


This little patch adds a periodically-requested convenience variable whose value is the current thread. It's handy in scripts and conditionals, since the GDB-assigned thread id is unpredictable.

Although the original request was for "$thread", it might be better to make it "$_thread" instead, so as not to interfere with possible use in existing scripts. But I'll only change it if I get enough votes in favor. :-)

Stan

2010-06-13 Stan Shebs <stan@codesourcery.com>

   * thread.c (thread_id_make_value): Make a value representing the
   current thread.
   (_initialize_thread): Create $thread.

   * gdb.texinfo (Debugging Programs with Multiple Threads): Describe
   $thread.

* gdb.threads/thread-specific.exp: Add tests of $thread.

Index: thread.c
===================================================================
RCS file: /cvs/src/src/gdb/thread.c,v
retrieving revision 1.120
diff -p -r1.120 thread.c
*** thread.c	17 May 2010 01:15:20 -0000	1.120
--- thread.c	14 Jun 2010 01:20:14 -0000
*************** update_thread_list (void)
*** 1262,1267 ****
--- 1262,1279 ----
    target_find_new_threads ();
  }
  
+ /* Return a new value for the selected thread's id.  Return a value of 0 if
+    no thread is selected, or no threads exist.  */
+ 
+ static struct value *
+ thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var)
+ {
+   struct thread_info *tp = find_thread_ptid (inferior_ptid);
+ 
+   return value_from_longest (builtin_type (gdbarch)->builtin_int,
+ 			     (tp ? tp->num : 0));
+ }
+ 
  /* Commands with a prefix of `thread'.  */
  struct cmd_list_element *thread_cmd_list = NULL;
  
*************** Show printing of thread events (such as 
*** 1295,1298 ****
--- 1307,1312 ----
           NULL,
           show_print_thread_events,
           &setprintlist, &showprintlist);
+ 
+   create_internalvar_type_lazy ("thread", thread_id_make_value);
  }
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.722
diff -p -r1.722 gdb.texinfo
*** doc/gdb.texinfo	12 Jun 2010 00:05:21 -0000	1.722
--- doc/gdb.texinfo	14 Jun 2010 01:20:14 -0000
*************** As with the @samp{[New @dots{}]} message
*** 2788,2793 ****
--- 2788,2800 ----
  @samp{Switching to} depends on your system's conventions for identifying
  threads.
  
+ @vindex $thread@r{, convenience variable}
+ The debugger convenience variable @samp{$thread} contains the number
+ of the current thread.  You may find this useful in writing breakpoint
+ conditional expressions, command scripts, and so forth.  See
+ @ref{Convenience Vars,, Convenience Variables}, for general
+ information on convenience variables.
+ 
  @kindex thread apply
  @cindex apply command to several threads
  @item thread apply [@var{threadno}] [@var{all}] @var{command}
Index: testsuite/gdb.threads/thread-specific.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.threads/thread-specific.exp,v
retrieving revision 1.12
diff -p -r1.12 thread-specific.exp
*** testsuite/gdb.threads/thread-specific.exp	2 Jun 2010 21:53:28 -0000	1.12
--- testsuite/gdb.threads/thread-specific.exp	14 Jun 2010 01:20:14 -0000
*************** gdb_load ${binfile}
*** 75,80 ****
--- 75,82 ----
  gdb_test_no_output "set print sevenbit-strings"
  gdb_test_no_output "set width 0"
  
+ gdb_test {print $thread} ".* = 0" "thread var when not running"
+ 
  runto_main
  
  gdb_breakpoint [gdb_get_line_number "thread-specific.exp: last thread start"]
*************** if {[llength $threads] == 0} {
*** 88,93 ****
--- 90,97 ----
    return 1
  }
  
+ gdb_test {print $thread} ".* = [lindex $threads 0]" "thread var in main"
+ 
  gdb_test_multiple "break $line thread [lindex $threads 0]" \
    "breakpoint $line main thread" {
      -re "Breakpoint (\[0-9\]*) at.* file .*$srcfile, line.*$gdb_prompt $" {
*************** gdb_test_multiple "continue" "continue t
*** 104,112 ****
  	-re "Breakpoint $main_breakpoint, .* at .*\r\n$gdb_prompt $" {
  	    fail "continue to thread-specific breakpoint (wrong breakpoint)"
  	}
! 	-re "Breakpoint .* at .*\r\n$gdb_prompt $" {
  	    pass "continue to thread-specific breakpoint"
  	}
  }
  
  return 0
--- 108,126 ----
  	-re "Breakpoint $main_breakpoint, .* at .*\r\n$gdb_prompt $" {
  	    fail "continue to thread-specific breakpoint (wrong breakpoint)"
  	}
! 	-re "Breakpoint (\[0-9\]*), .* at .*\r\n$gdb_prompt $" {
! 	    set this_breakpoint $expect_out(1,string)
  	    pass "continue to thread-specific breakpoint"
  	}
  }
  
+ gdb_test_multiple "info breakpoint $this_breakpoint" "info on bp" {
+     -re ".*stop only in thread (\[0-9\]*).*$gdb_prompt $" {
+ 	set this_thread $expect_out(1,string)
+ 	pass "found breakpoint for thread number"
+     }
+ }
+ 
+ gdb_test {print $thread} ".* = $this_thread" "thread var at break"
+ 
  return 0

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]