1# Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
2
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 2 of the License, or
6# (at your option) any later version.
7#
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.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-gdb@prep.ai.mit.edu
19
20# This file checks for the bug gdb/669, where the console
21# command "info threads" and the MI command "-thread-list-ids"
22# return different threads in the system.
23
24# This only works with native configurations
25if {![isnative]} {
26  return
27}
28
29load_lib mi-support.exp
30set MIFLAGS "-i=mi"
31
32gdb_exit
33if {[mi_gdb_start]} {
34    continue
35}
36
37# The procs below are all stolen from mi-pthreads.exp. Any updates
38# should also be made to the procs there.
39
40proc get_mi_thread_list {name} {
41  global expect_out
42
43  # MI will return a list of thread ids:
44  #
45  # -thread-list-ids
46  # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
47  # (gdb)
48  mi_gdb_test "-thread-list-ids" \
49    {\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},number-of-threads="[0-9]+"} \
50    "-thread_list_ids ($name)"
51
52  set output {}
53  if {[info exists expect_out(buffer)]} {
54    set output $expect_out(buffer)
55  }
56  set thread_list {}
57  if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
58    fail "finding threads in MI output ($name)"
59  } else {
60    pass "finding threads in MI output ($name)"
61
62    # Make list of console threads
63    set start [expr {[string first \{ $threads] + 1}]
64    set end   [expr {[string first \} $threads] - 1}]
65    set threads [string range $threads $start $end]
66    foreach thread [split $threads ,] {
67      if {[scan $thread {thread-id="%d"} num]} {
68	lappend thread_list $num
69      }
70    }
71  }
72
73  return $thread_list
74}
75
76# Check that MI and the console know of the same threads.
77# Appends NAME to all test names.
78proc check_mi_and_console_threads {name} {
79  global expect_out
80
81  mi_gdb_test "-thread-list-ids" \
82    {\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},number-of-threads="[0-9]+"} \
83    "-thread-list-ids ($name)"
84  set mi_output {}
85  if {[info exists expect_out(buffer)]} {
86    set mi_output $expect_out(buffer)
87  }
88
89  # GDB will return a list of thread ids and some more info:
90  #
91  # (gdb)
92  # -interpreter-exec console "info threads"
93  # ~"  4 Thread 2051 (LWP 7734)  0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
94  # ~"  3 Thread 1026 (LWP 7733)   () at __libc_nanosleep:-1"
95  # ~"  2 Thread 2049 (LWP 7732)  0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
96  # ~"* 1 Thread 1024 (LWP 7731)  main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
97  # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
98  mi_gdb_test "info threads" \
99    {.*(~".*"[\r\n]*)+.*} \
100    "info threads ($name)"
101  set console_output {}
102  if {[info exists expect_out(buffer)]} {
103    set console_output $expect_out(buffer)
104  }
105
106  # Make a list of all known threads to console (gdb's thread IDs)
107  set console_thread_list {}
108  foreach line [split $console_output \n] {
109    if {[string index $line 0] == "~"} {
110      # This is a line from the console; trim off "~", " ", "*", and "\""
111      set line [string trim $line ~\ \"\*]
112      if {[scan $line "%d" id] == 1} {
113	lappend console_thread_list $id
114      }
115    }
116  }
117
118  # Now find the result string from MI
119  set mi_result ""
120  foreach line [split $mi_output \n] {
121    if {[string range $line 0 4] == "^done"} {
122      set mi_result $line
123    }
124  }
125  if {$mi_result == ""} {
126    fail "finding MI result string ($name)"
127  } else {
128    pass "finding MI result string ($name)"
129  }
130
131  # Finally, extract the thread ids and compare them to the console
132  set num_mi_threads_str ""
133  if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
134    fail "finding number of threads in MI output ($name)"
135  } else {
136    pass "finding number of threads in MI output ($name)"
137
138    # Extract the number of threads from the MI result
139    if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
140      fail "got number of threads from MI ($name)"
141    } else {
142      pass "got number of threads from MI ($name)"
143
144      # Check if MI and console have same number of threads
145      if {$num_mi_threads != [llength $console_thread_list]} {
146	fail "console and MI have same number of threads ($name)"
147      } else {
148	pass "console and MI have same number of threads ($name)"
149
150	# Get MI thread list
151	set mi_thread_list [get_mi_thread_list $name]
152
153	# Check if MI and console have the same threads
154	set fails 0
155	foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
156	  if {$ct != $mt} {
157	    incr fails
158	  }
159	}
160	if {$fails > 0} {
161	  fail "MI and console have same threads ($name)"
162
163	  # Send a list of failures to the log
164	  send_log "Console has thread ids: $console_thread_list\n"
165	  send_log "MI has thread ids: $mi_thread_list\n"
166	} else {
167	  pass "MI and console have same threads ($name)"
168	}
169      }
170    }
171  }
172}
173
174#
175# Start here
176#
177set testfile "pthreads"
178set srcfile "$testfile.c"
179set binfile "$objdir/$subdir/gdb669-$testfile"
180
181set options [list debug incdir=$objdir]
182if {[gdb_compile_pthreads "$srcdir/$subdir/$srcfile" $binfile executable $options] != "" } {
183    return -1
184}
185
186mi_gdb_reinitialize_dir $srcdir/$subdir
187mi_gdb_load $binfile
188
189mi_run_to_main
190check_mi_and_console_threads "at main"
191
192for {set i 0} {$i < 4} {incr i} {
193  mi_next "next, try $i"
194  check_mi_and_console_threads "try $i"
195}
196
197mi_gdb_exit
198
199