async.exp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2019-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. standard_testfile
  15. if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
  16. return -1
  17. }
  18. # At this point GDB will be busy handling the breakpoint hits and
  19. # re-resuming the program. Even if GDB internally switches thread
  20. # context, the user should not notice it. The following part of the
  21. # testcase ensures that.
  22. # Switch to thread EXPECTED_THR, and then confirm that the thread
  23. # stays selected.
  24. proc test_current_thread {expected_thr} {
  25. global decimal
  26. global gdb_prompt
  27. global binfile
  28. clean_restart $binfile
  29. if {![runto "all_started"]} {
  30. return
  31. }
  32. # Set a breakpoint that continuously fires but doeesn't cause a stop.
  33. gdb_breakpoint [concat [gdb_get_line_number "set breakpoint here"] " if 0"]
  34. gdb_test "thread $expected_thr" "Switching to thread $expected_thr .*" \
  35. "switch to thread $expected_thr"
  36. # Continue the program in the background.
  37. set test "continue&"
  38. gdb_test_multiple "continue&" $test {
  39. -re "Continuing\\.\r\n$gdb_prompt " {
  40. pass $test
  41. }
  42. }
  43. set test "current thread is $expected_thr"
  44. set fails 0
  45. for {set i 0} {$i < 10} {incr i} {
  46. after 200
  47. set cur_thread 0
  48. gdb_test_multiple "thread" $test {
  49. -re "Current thread is ($decimal) .*$gdb_prompt " {
  50. set cur_thread $expect_out(1,string)
  51. }
  52. }
  53. if {$cur_thread != $expected_thr} {
  54. incr fails
  55. }
  56. }
  57. gdb_assert {$fails == 0} $test
  58. # Explicitly interrupt the target, because in all-stop/remote,
  59. # that's all we can do when the target is running. If we don't do
  60. # this, we'd time out trying to kill the target, while bringing
  61. # down gdb & gdbserver.
  62. set test "interrupt"
  63. gdb_test_multiple $test $test {
  64. -re "^interrupt\r\n$gdb_prompt " {
  65. gdb_test_multiple "" $test {
  66. -re "Thread .* received signal SIGINT, Interrupt\\." {
  67. pass $test
  68. }
  69. }
  70. }
  71. }
  72. }
  73. # Try once with each thread as current, to avoid missing a bug just
  74. # because some part of GDB manages to switch to the right thread by
  75. # chance.
  76. for {set thr 1} {$thr <= 3} {incr thr} {
  77. with_test_prefix "thread $thr" {
  78. test_current_thread $thr
  79. }
  80. }