1 # Copyright 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Please email any bugs, comments, and/or additions to this file to:
17 # bug-gdb@prep.ai.mit.edu
19 # This file tests MI thread commands.
20 # Specifically, we are testing the MI command set and the console (in MI)
21 # command set ("interpreter-exec") and that the commands that are executed
22 # via these command pathways are properly executed. Console commands
23 # executed via MI should use MI output wrappers, MI event handlers, etc.
25 # This only works with native configurations
30 load_lib mi-support.exp
38 # The procs below dealing with parsing cli/mi output for the threadlist
39 # is duplicated in gdb669.exp. Any changes here will probably need to
40 # be made there as well.
42 proc get_mi_thread_list {name} {
45 # MI will return a list of thread ids:
48 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
50 mi_gdb_test "-thread-list-ids" \
51 {\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},number-of-threads="[0-9]+"} \
52 "-thread_list_ids ($name)"
55 if {[info exists expect_out(buffer)]} {
56 set output $expect_out(buffer)
60 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
61 fail "finding threads in MI output ($name)"
63 pass "finding threads in MI output ($name)"
65 # Make list of console threads
66 set start [expr {[string first \{ $threads] + 1}]
67 set end [expr {[string first \} $threads] - 1}]
68 set threads [string range $threads $start $end]
69 foreach thread [split $threads ,] {
70 if {[scan $thread {thread-id="%d"} num]} {
71 lappend thread_list $num
79 # Check that MI and the console know of the same threads.
80 # Appends NAME to all test names.
81 proc check_mi_and_console_threads {name} {
84 mi_gdb_test "-thread-list-ids" \
85 {\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},number-of-threads="[0-9]+"} \
86 "-thread-list-ids ($name)"
88 if {[info exists expect_out(buffer)]} {
89 set mi_output $expect_out(buffer)
92 # GDB will return a list of thread ids and some more info:
95 # -interpreter-exec console "info threads"
96 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
97 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
98 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
99 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
100 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
101 mi_gdb_test "info threads" \
102 {.*(~".*"[\r\n]*)+.*} \
103 "info threads ($name)"
104 set console_output {}
105 if {[info exists $expect_out(buffer)]} {
106 set console_output $expect_out(buffer)
109 # Make a list of all known threads to console (gdb's thread IDs)
110 set console_thread_list {}
111 foreach line [split $console_output \n] {
112 if {[string index $line 0] == "~"} {
113 # This is a line from the console; trim off "~", " ", "*", and "\""
114 set line [string trim $line ~\ \"\*]
115 if {[scan $line "%d" id] == 1} {
116 lappend console_thread_list $id
121 # Now find the result string from MI
123 foreach line [split $mi_output \n] {
124 if {[string range $line 0 4] == "^done"} {
128 if {$mi_result == ""} {
129 fail "finding MI result string ($name)"
131 pass "finding MI result string ($name)"
134 # Finally, extract the thread ids and compare them to the console
135 set num_mi_threads_str ""
136 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
137 fail "finding number of threads in MI output ($name)"
139 pass "finding number of threads in MI output ($name)"
141 # Extract the number of threads from the MI result
142 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
143 fail "got number of threads from MI ($name)"
145 pass "got number of threads from MI ($name)"
147 # Check if MI and console have same number of threads
148 if {$num_mi_threads != [llength $console_thread_list]} {
149 fail "console and MI have same number of threads ($name)"
151 pass "console and MI have same number of threads ($name)"
154 set mi_thread_list [get_mi_thread_list $name]
156 # Check if MI and console have the same threads
158 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
164 fail "MI and console have same threads ($name)"
166 # Send a list of failures to the log
167 send_log "Console has thread ids: $console_thread_list\n"
168 send_log "MI has thread ids: $mi_thread_list\n"
170 pass "MI and console have same threads ($name)"
177 # This procedure tests the various thread commands in MI.
178 proc check_mi_thread_command_set {} {
180 mi_runto done_making_threads
182 set thread_list [get_mi_thread_list "in check_mi_thread_command_set"]
184 mi_gdb_test "-thread-select" \
185 {\^error,msg="mi_cmd_thread_select: USAGE: threadnum."} \
186 "check_mi_thread_command_set: -thread-select"
188 mi_gdb_test "-thread-select 123456789" \
189 {&.*\^error,msg="Thread ID 123456789 not known\."} \
190 "check_mi_thread_command_set: -thread-select 123456789"
192 foreach thread $thread_list {
193 # line and file are optional.
194 # many of the threads are blocked in libc calls,
195 # and many people have libc's with no symbols.
196 mi_gdb_test "-thread-select $thread" \
197 "\\^done,new-thread-id=\"$thread\",frame={.*}(,line=\"(-)?\[0-9\]+\",file=\".*\")?" \
198 "check_mi_thread_command_set: -thread-select $thread"
205 set testfile "pthreads"
206 set srcfile "$testfile.c"
207 set binfile "$objdir/$subdir/mi2-$testfile"
209 set options [list debug incdir=$objdir]
210 if {[gdb_compile_pthreads "$srcdir/$subdir/$srcfile" $binfile executable $options] != "" } {
214 mi_gdb_reinitialize_dir $srcdir/$subdir
217 check_mi_thread_command_set