attach-non-stop.exp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright 2021-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. # Test attaching to a multi-threaded process, in all combinations of:
  15. #
  16. # - set non-stop on/off
  17. # - maint target non-stop off/on
  18. # - "attach" vs "attach &"
  19. if {![can_spawn_for_attach]} {
  20. return 0
  21. }
  22. standard_testfile
  23. # The test proper. See description above.
  24. proc test {target_non_stop non_stop cmd} {
  25. global binfile srcfile
  26. global gdb_prompt
  27. global decimal
  28. global GDBFLAGS
  29. # Number of threads started by the program.
  30. set n_threads 10
  31. save_vars { GDBFLAGS } {
  32. append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
  33. append GDBFLAGS " -ex \"set non-stop $non_stop\""
  34. clean_restart $binfile
  35. }
  36. set test_spawn_id [spawn_wait_for_attach $binfile]
  37. set testpid [spawn_id_get_pid $test_spawn_id]
  38. set attached 0
  39. set test "attach"
  40. set any "\[^\r\n\]*"
  41. if {$cmd == "attach"} {
  42. gdb_test_multiple "attach $testpid" $test {
  43. -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
  44. pass $test
  45. set attached 1
  46. }
  47. }
  48. if {!$attached} {
  49. kill_wait_spawned_process $test_spawn_id
  50. return
  51. }
  52. if {$non_stop} {
  53. # In non-stop, we will see one stop per thread after
  54. # the prompt.
  55. set stops 0
  56. set test "seen all stops"
  57. for {set thread 1} { $thread <= $n_threads } { incr thread } {
  58. gdb_test_multiple "" $test {
  59. -re "Thread $::decimal ${any} stopped" {
  60. incr stops
  61. }
  62. }
  63. }
  64. # If we haven't seen all stops, then the gdb_test_multiple
  65. # in the loop above will have already issued a FAIL.
  66. if {$stops == $n_threads} {
  67. pass $test
  68. }
  69. }
  70. gdb_test_multiple "info threads" "" {
  71. -re "\\(running\\).*$gdb_prompt $" {
  72. fail $gdb_test_name
  73. }
  74. -re "$gdb_prompt $" {
  75. pass $gdb_test_name
  76. }
  77. }
  78. } else {
  79. gdb_test_multiple "attach $testpid &" $test {
  80. -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
  81. pass $test
  82. set attached 1
  83. }
  84. }
  85. if {!$attached} {
  86. kill_wait_spawned_process $test_spawn_id
  87. return
  88. }
  89. set running_count 0
  90. gdb_test_multiple "info threads" "all threads running" {
  91. -re "\\(running\\)" {
  92. incr running_count
  93. exp_continue
  94. }
  95. -re "Cannot execute this command while the target is running.*$gdb_prompt $" {
  96. # Testing against a remote server that doesn't do
  97. # non-stop mode. Explicitly interrupt. This doesn't
  98. # test the same code paths in GDB, but it's still
  99. # something.
  100. gdb_test_multiple "interrupt" "" {
  101. -re "$gdb_prompt " {
  102. gdb_test_multiple "" $gdb_test_name {
  103. -re "received signal SIGINT, Interrupt" {
  104. pass $gdb_test_name
  105. }
  106. }
  107. }
  108. }
  109. }
  110. -re "$gdb_prompt $" {
  111. gdb_assert {$running_count == ($n_threads + 1)} $gdb_test_name
  112. }
  113. }
  114. }
  115. gdb_test "detach" "Detaching from.*"
  116. kill_wait_spawned_process $test_spawn_id
  117. }
  118. if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
  119. return -1
  120. }
  121. foreach_with_prefix target-non-stop {"off" "on"} {
  122. foreach_with_prefix non-stop {"off" "on"} {
  123. foreach_with_prefix cmd {"attach" "attach&"} {
  124. test ${target-non-stop} ${non-stop} ${cmd}
  125. }
  126. }
  127. }