linux-dp.exp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # Copyright 1999-2022 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. # Please email any bugs, comments, and/or additions to this file to:
  15. # bug-gdb@gnu.org
  16. #### Dining Philosophers, on LinuxThreads - Jim Blandy <jimb@cygnus.com>
  17. ####
  18. #### At the moment, GDB's support for LinuxThreads is pretty
  19. #### idiosyncratic --- GDB's output doesn't look much like the output
  20. #### it produces for other thread implementations, messages appear at
  21. #### different times, etc. So these tests are specific to LinuxThreads.
  22. ####
  23. #### However, if all goes well, Linux will soon have a libthread_db
  24. #### interface, and GDB will manage it the same way it does other
  25. #### libthread_db-based systems. Then, we can adjust this file to
  26. #### work with any such system.
  27. ### Other things we ought to test:
  28. ### stepping a thread while others are running
  29. ### killing and restarting
  30. ### quitting gracefully
  31. # This only works with Linux configurations.
  32. if ![istarget *-*-linux-gnu*] then {
  33. return
  34. }
  35. standard_testfile
  36. if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != ""} {
  37. return -1
  38. }
  39. clean_restart ${binfile}
  40. gdb_test_no_output "set print sevenbit-strings"
  41. runto_main
  42. # There should be no threads initially.
  43. gdb_test "info threads" ".*" "info threads 1"
  44. # Try stepping over the thread creation function.
  45. gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
  46. set expect_manager -1
  47. for {set i 0} {$i < 5} {incr i} {
  48. gdb_continue_to_breakpoint "about to create philosopher: $i"
  49. set threads_before {}
  50. gdb_test_multiple "info threads" "info threads before: $i" {
  51. -re "info threads\r\n" {
  52. exp_continue
  53. }
  54. -re "^ *Id.*Frame *\[\r\n\]+" {
  55. exp_continue
  56. }
  57. -re "^. +(\[0-9\]+ *Thread \[-0-9a-fx.\]+) \[^\n\]*\n" {
  58. verbose -log "found thread $expect_out(1,string)" 2
  59. lappend threads_before $expect_out(1,string)
  60. exp_continue
  61. }
  62. -re "^\[^\n\]*\n" {
  63. verbose -log "skipping line" 2
  64. exp_continue -continue_timer
  65. }
  66. -re "^$gdb_prompt $" {
  67. }
  68. }
  69. set threads_created 0
  70. gdb_test_multiple "next" "create philosopher: $i" {
  71. -re "^next\r\n" {
  72. exp_continue
  73. }
  74. -re "^ *\[_!\] \[0-9\]* \[_!\]\r\n" {
  75. # Ignore program output.
  76. exp_continue -continue_timer
  77. }
  78. -re "^\\\[New \[^\]\n\]+\\\]\[^\n\]+\n" {
  79. incr threads_created
  80. exp_continue
  81. }
  82. -re "^189\[^\n\]+\n" {
  83. exp_continue
  84. }
  85. -re "^$gdb_prompt $" {
  86. }
  87. -re " received signal.*(Unknown signal|SIGUSR|Real-time event).*$gdb_prompt $" {
  88. # It would be nice if we could catch the message that GDB prints
  89. # when it first notices that the thread library doesn't support
  90. # debugging, or if we could explicitly ask GDB somehow.
  91. unsupported "this GDB does not support threads on this system."
  92. return -1
  93. }
  94. -re "$gdb_prompt $" {
  95. }
  96. }
  97. if { $threads_created == 1 } {
  98. if { $expect_manager < 0 } {
  99. set expect_manager 0
  100. }
  101. pass "create philosopher: $i"
  102. } elseif { !$i && $threads_created == 2 } {
  103. # Two threads are created the first time in LinuxThreads,
  104. # where the second is the manager thread. In NPTL, there is none.
  105. set expect_manager 1
  106. pass "create philosopher: $i"
  107. } else {
  108. fail "create philosopher: $i"
  109. }
  110. set threads_after {}
  111. gdb_test_multiple "info threads" "info threads after: $i" {
  112. -re "info threads\r\n" {
  113. exp_continue
  114. }
  115. -re "^ *Id.*Frame *\[\r\n\]+" {
  116. exp_continue
  117. }
  118. -re "^. +(\[0-9\]+ *Thread \[-0-9a-fx.\]+) \[^\n\]*\n" {
  119. set name $expect_out(1,string)
  120. for {set j 0} {$j != [llength $threads_before] } {incr j} {
  121. if {$name == [lindex $threads_before $j]} {
  122. set threads_before [lreplace $threads_before $j $j]
  123. set name ""
  124. break
  125. }
  126. }
  127. if { $name != "" } {
  128. lappend threads_after $name
  129. }
  130. exp_continue
  131. }
  132. -re "^\[^\n\]*\n" {
  133. verbose -log "skipping line" 2
  134. exp_continue -continue_timer
  135. }
  136. -re "^$gdb_prompt $" {
  137. if { [llength $threads_before] != 0 } {
  138. fail "info threads after: $i"
  139. } elseif { !$i && [llength $threads_after] == 2 } {
  140. set expect_manager 1
  141. pass "info threads after: $i"
  142. } elseif { [llength $threads_after] == 1 } {
  143. if { $expect_manager < 0 } {
  144. set expect_manager 0
  145. }
  146. pass "info threads after: $i"
  147. } else {
  148. fail "info threads after: $i"
  149. }
  150. }
  151. }
  152. }
  153. set nthreads 6
  154. # Run until there are some threads.
  155. gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
  156. gdb_continue_to_breakpoint "main thread's sleep"
  157. set info_threads_ptn ".*"
  158. for {set i 1} {$i < $nthreads} {incr i} {
  159. append info_threads_ptn "$i *Thread .*"
  160. }
  161. append info_threads_ptn "\[\r\n\]+$gdb_prompt $"
  162. set info_threads_manager_ptn "[expr $nthreads + 1] *Thread .*$info_threads_ptn"
  163. gdb_test_multiple "info threads" "info threads 2" {
  164. -re "$info_threads_manager_ptn" {
  165. # We did see a manager thread. Check that against what we expected.
  166. switch -exact -- $expect_manager {
  167. -1 {
  168. # We weren't sure whether to expect a manager thread.
  169. pass "info threads 2"
  170. }
  171. 1 {
  172. # We were expecting a manager thread.
  173. pass "info threads 2"
  174. }
  175. 0 {
  176. # We were not expecting to see the manager thread.
  177. fail "info threads 2"
  178. }
  179. }
  180. set expect_manager 1
  181. incr nthreads
  182. }
  183. -re "$info_threads_ptn" {
  184. # We did not see a manager thread. Check that against what we
  185. # expected.
  186. switch -exact -- $expect_manager {
  187. -1 {
  188. # We weren't sure whether to expect a manager thread.
  189. # Don't expect it from here on out.
  190. pass "info threads 2"
  191. }
  192. 1 {
  193. # We were expecting a manager thread, but we didn't see one.
  194. fail "info threads 2"
  195. }
  196. 0 {
  197. # We were not expecting to see the manager thread.
  198. pass "info threads 2"
  199. }
  200. }
  201. set expect_manager 0
  202. }
  203. }
  204. # Try setting a thread-specific breakpoint.
  205. gdb_breakpoint "print_philosopher thread 5"
  206. gdb_continue_to_breakpoint "thread 5's print"
  207. # When there is no debugging info available for the thread library,
  208. # the backtrace entry for philosopher's caller looks like:
  209. # #2 0x4001c548 in pthread_create () from /lib/libpthread.so.0
  210. # If you do have debug info, the output obviously depends more on the
  211. # exact library in use; under NPTL, you get:
  212. # #2 0x0012b7fc in start_thread (arg=0x21) at pthread_create.c:264
  213. gdb_test "where" "print_philosopher.*philosopher.* \(from .*libpthread\|at pthread_create\|in pthread_create|from .*libc\\.\).*" \
  214. "first thread-specific breakpoint hit"
  215. # Make sure it's catching the right thread. Try hitting the
  216. # breakpoint ten times, and make sure we don't get anyone else.
  217. set only_five 1
  218. for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
  219. gdb_continue_to_breakpoint "thread 5's print, pass: $i"
  220. gdb_test_multiple "info threads" "" {
  221. -re "\[*\] 5 *Thread .* +print_philosopher .*\r\n$gdb_prompt $" {
  222. # Okay this time.
  223. }
  224. -re ".*$gdb_prompt $" {
  225. set only_five 0
  226. }
  227. timeout {
  228. set only_five -1
  229. }
  230. }
  231. }
  232. set name "thread-specific breakpoint is thread-specific"
  233. if {$only_five == 1} { pass $name }
  234. if {$only_five == 0} { fail $name }
  235. if {$only_five == -1} { fail "$name (timeout)" }
  236. ### Select a particular thread.
  237. proc select_thread {thread} {
  238. global gdb_prompt
  239. gdb_test "thread $thread" \
  240. "\\\[Switching to thread .*\\\].*" \
  241. "selected thread: $thread"
  242. }
  243. ### Select THREAD, check for a plausible backtrace, and make sure
  244. ### we're actually selecting a different philosopher each time.
  245. ### Return true if the thread had a stack which was not only
  246. ### acceptable, but interesting. SEEN should be an array in which
  247. ### SEEN(N) exists iff we have found philosopher number N before.
  248. set main_seen 0
  249. set manager_seen 0
  250. proc check_philosopher_stack {thread seen_name} {
  251. global gdb_prompt
  252. upvar $seen_name seen
  253. global main_seen
  254. global expect_manager manager_seen
  255. set name "philosopher is distinct: $thread"
  256. set interesting 0
  257. select_thread $thread
  258. gdb_test_multiple "where" "$name" {
  259. -re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
  260. set data $expect_out(1,string)
  261. if {[info exists seen($data)]} {
  262. fail $name
  263. } else {
  264. pass $name
  265. set seen($data) yep
  266. }
  267. set interesting 1
  268. }
  269. -re ".* in __pthread_manager \\(.*$gdb_prompt $" {
  270. if {$manager_seen == 1} {
  271. fail "manager thread is distinct: $thread"
  272. } else {
  273. set manager_seen 1
  274. pass "manager thread is distinct: $thread"
  275. }
  276. set interesting 1
  277. }
  278. -re "pthread_start_thread.*\r\n$gdb_prompt $" {
  279. ## Maybe the thread hasn't started yet.
  280. pass $name
  281. }
  282. -re ".* in main \\(.*$gdb_prompt $" {
  283. if {$main_seen == 1} {
  284. fail "main is distinct: $thread"
  285. } else {
  286. set main_seen 1
  287. pass "main is distinct: $thread"
  288. }
  289. set interesting 1
  290. }
  291. -re " in \\?\\?.*\r\n$gdb_prompt $" {
  292. ## Sometimes we can't get a backtrace. I'm going to call
  293. ## this a pass, since we do verify that at least one
  294. ## thread was interesting, so we can get more consistent
  295. ## test suite totals. But in my heart, I think it should
  296. ## be an xfail.
  297. pass $name
  298. }
  299. }
  300. return $interesting
  301. }
  302. set any_interesting 0
  303. catch {unset seen}
  304. array set seen {}
  305. for {set i 1} {$i <= $nthreads} {incr i} {
  306. if [check_philosopher_stack $i seen] {
  307. set any_interesting 1
  308. }
  309. }
  310. unset seen
  311. if {$any_interesting} {
  312. pass "found an interesting thread"
  313. } else {
  314. fail "found an interesting thread"
  315. }
  316. if {$manager_seen == $expect_manager} {
  317. pass "manager thread found (not found) when expected"
  318. } else {
  319. fail "manager thread found (not found) when expected"
  320. }