process-dies-while-detaching.exp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. # Copyright 2016-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. # This test spawns a few threads that immediately exit the whole
  15. # process. On targets where the debugger needs to detach from each
  16. # thread individually (such as on the Linux kernel), the debugger must
  17. # handle the case of the process exiting while the detach is ongoing.
  18. #
  19. # Similarly, the process can also be killed from outside the debugger
  20. # (e.g., with SIGKILL), _before_ the user requests a detach. The
  21. # debugger must likewise detach gracefully.
  22. #
  23. # The testcase actually builds two variants of the test program:
  24. # single-process, and multi-process. In the multi-process variant,
  25. # the test program forks, and it's the fork child that spawns threads
  26. # that exit just while the process is being detached from. The fork
  27. # parent waits for its child to exit, so if GDB fails to detach from
  28. # the child correctly, the parent hangs. Because continuing the
  29. # parent can mask failure to detach from the child correctly (e.g.,
  30. # due to waitpid(-1,...) calls deep in the target layers managing to
  31. # reap the child), we try immediately detaching from the parent too,
  32. # and observing whether the parent exits via standard output.
  33. #
  34. # Normally, if testing with "target remote" against gdbserver, then
  35. # after detaching from all attached processes, gdbserver exits.
  36. # However, when gdbserver detaches from a process that is its own
  37. # direct child, gdbserver does not exit immediately. Instead it
  38. # "joins" (waits for) the child, only exiting when the child itself
  39. # exits too. Thus, on Linux, if gdbserver fails to detach from the
  40. # zombie child's threads correctly (or rather, reap them), it'll hang,
  41. # because the leader thread will only return an exit status after all
  42. # threads are reaped. We test that as well.
  43. standard_testfile
  44. # Test that GDBserver exits.
  45. proc test_server_exit {} {
  46. global server_spawn_id
  47. set test "server exits"
  48. gdb_expect {
  49. -i $server_spawn_id
  50. eof {
  51. pass $test
  52. wait -i $server_spawn_id
  53. unset server_spawn_id
  54. }
  55. timeout {
  56. fail "$test (timeout)"
  57. }
  58. }
  59. }
  60. # If RESULT is not zero, make the caller return.
  61. proc return_if_fail { result } {
  62. if {$result != 0} {
  63. return -code return
  64. }
  65. }
  66. # Detach from a process, and ensure that it exits after detaching.
  67. # This relies on inferior I/O. INF_OUTPUT_RE is the pattern that
  68. # matches the expected inferior output.
  69. proc detach_and_expect_exit {inf_output_re test} {
  70. global decimal
  71. global gdb_spawn_id
  72. global inferior_spawn_id
  73. global gdb_prompt
  74. return_if_fail [gdb_test_multiple "detach" $test {
  75. -re "Detaching from .*, process $decimal" {
  76. }
  77. }]
  78. # Use an indirect spawn id list, and remove inferior spawn id from
  79. # the expected output as soon as it matches, so that if
  80. # $inf_inferior_spawn_id is $server_spawn_id and we're testing in
  81. # "target remote" mode, the eof caused by gdbserver exiting is
  82. # left for the caller to handle.
  83. global daee_spawn_id_list
  84. set daee_spawn_id_list "$inferior_spawn_id $gdb_spawn_id"
  85. set saw_prompt 0
  86. set saw_inf_exit 0
  87. while { !$saw_prompt || ! $saw_inf_exit } {
  88. # We don't know what order the interesting things will arrive in.
  89. # Using a pattern of the form 'x|y|z' instead of -re x ... -re y
  90. # ... -re z ensures that expect always chooses the match that
  91. # occurs leftmost in the input, and not the pattern appearing
  92. # first in the script that occurs anywhere in the input, so that
  93. # we don't skip anything.
  94. return_if_fail [gdb_test_multiple "" $test {
  95. -i daee_spawn_id_list
  96. -re "($inf_output_re)|($gdb_prompt )" {
  97. if {[info exists expect_out(1,string)]} {
  98. verbose -log "saw inferior exit"
  99. set saw_inf_exit 1
  100. set daee_spawn_id_list "$gdb_spawn_id"
  101. } elseif {[info exists expect_out(2,string)]} {
  102. verbose -log "saw prompt"
  103. set saw_prompt 1
  104. set daee_spawn_id_list "$inferior_spawn_id"
  105. }
  106. array unset expect_out
  107. }
  108. }]
  109. }
  110. pass $test
  111. }
  112. # Run to _exit in the child.
  113. proc continue_to_exit_bp {} {
  114. gdb_breakpoint "_exit" temporary
  115. gdb_continue_to_breakpoint "_exit" ".*_exit.*"
  116. }
  117. # If testing single-process, simply detach from the process.
  118. #
  119. # If testing multi-process, first detach from the child, then detach
  120. # from the parent and confirm that the parent exits, thus ensuring
  121. # we've detached from the child successfully, as the parent hangs in
  122. # its waitpid call otherwise.
  123. #
  124. # If connected with "target remote", make sure gdbserver exits.
  125. #
  126. # CMD indicates what to do with the parent after detaching the child.
  127. # Can be either "detach" to detach, or "continue", to continue to
  128. # exit.
  129. #
  130. # CHILD_EXIT indicates how is the child expected to exit. Can be
  131. # either "normal" for normal exit, or "signal" for killed with signal
  132. # SIGKILL.
  133. #
  134. proc do_detach {multi_process cmd child_exit} {
  135. global decimal
  136. global server_spawn_id
  137. if {$child_exit == "normal"} {
  138. set continue_re "exited normally.*"
  139. set inf_output_re "exited, status=0"
  140. } elseif {$child_exit == "signal"} {
  141. if {$multi_process} {
  142. set continue_re "exited with code 02.*"
  143. } else {
  144. set continue_re "terminated with signal SIGKILL.*"
  145. }
  146. set inf_output_re "signaled, sig=9"
  147. } else {
  148. error "unhandled \$child_exit: $child_exit"
  149. }
  150. set is_remote [expr {[target_info exists gdb_protocol]
  151. && [target_info gdb_protocol] == "remote"}]
  152. if {$multi_process} {
  153. gdb_test "detach" "Detaching from .*, process $decimal\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
  154. "detach child"
  155. gdb_test "inferior 1" "\[Switching to inferior $decimal\].*" \
  156. "switch to parent"
  157. if {$cmd == "detach"} {
  158. # Make sure that detach works and that the parent process
  159. # exits cleanly.
  160. detach_and_expect_exit $inf_output_re "detach parent"
  161. } elseif {$cmd == "continue"} {
  162. # Make sure that continuing works and that the parent process
  163. # exits cleanly.
  164. gdb_test "continue" $continue_re
  165. } else {
  166. perror "unhandled command: $cmd"
  167. }
  168. } else {
  169. if $is_remote {
  170. set extra "\r\nEnding remote debugging\."
  171. } else {
  172. set extra ""
  173. }
  174. if {$cmd == "detach"} {
  175. gdb_test "detach" "Detaching from .*, process ${decimal}\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]$extra"
  176. } elseif {$cmd == "continue"} {
  177. gdb_test "continue" $continue_re
  178. } else {
  179. perror "unhandled command: $cmd"
  180. }
  181. }
  182. # When connected in "target remote" mode, the server should exit
  183. # when there are no processes left to debug.
  184. if { $is_remote && [info exists server_spawn_id]} {
  185. test_server_exit
  186. }
  187. }
  188. # Test detaching from a process that dies just while GDB is detaching.
  189. proc test_detach {multi_process cmd} {
  190. with_test_prefix "detach" {
  191. global binfile
  192. clean_restart ${binfile}
  193. if ![runto_main] {
  194. return -1
  195. }
  196. if {$multi_process} {
  197. gdb_test_no_output "set detach-on-fork off"
  198. gdb_test_no_output "set follow-fork-mode child"
  199. }
  200. # Run to _exit in the child.
  201. continue_to_exit_bp
  202. do_detach $multi_process $cmd "normal"
  203. }
  204. }
  205. # Same as test_detach, except set a watchpoint before detaching.
  206. proc test_detach_watch {wp multi_process cmd} {
  207. if { $wp == "hw" && [skip_hw_watchpoint_tests] } {
  208. unsupported "hw watchpoint"
  209. return
  210. }
  211. with_test_prefix "watchpoint:$wp" {
  212. global binfile decimal
  213. clean_restart ${binfile}
  214. if ![runto_main] {
  215. return -1
  216. }
  217. if {$multi_process} {
  218. gdb_test_no_output "set detach-on-fork off"
  219. gdb_test_no_output "set follow-fork-mode child"
  220. gdb_breakpoint "child_function" temporary
  221. gdb_continue_to_breakpoint "child_function" ".*"
  222. }
  223. if { $wp == "hw" } {
  224. # Set a watchpoint in the child.
  225. gdb_test "watch globalvar" ".* watchpoint $decimal: globalvar"
  226. # Continue to the _exit breakpoint. This arms the watchpoint
  227. # registers in all threads. Detaching will thus need to clear
  228. # them out, and handle the case of the thread disappearing
  229. # while doing that (on targets that need to detach from each
  230. # thread individually).
  231. continue_to_exit_bp
  232. } else {
  233. # Force software watchpoints.
  234. gdb_test_no_output "set can-use-hw-watchpoints 0"
  235. # As above, but flip order, other wise things take too long.
  236. continue_to_exit_bp
  237. gdb_test "watch globalvar" "Watchpoint $decimal: globalvar"
  238. if { $multi_process == 0 && $cmd == "continue" } {
  239. setup_kfail "gdb/28375" "*-*-*"
  240. }
  241. }
  242. do_detach $multi_process $cmd "normal"
  243. }
  244. }
  245. # Test detaching from a process that dies _before_ GDB starts
  246. # detaching.
  247. proc test_detach_killed_outside {multi_process cmd} {
  248. with_test_prefix "killed outside" {
  249. global binfile
  250. clean_restart ${binfile}
  251. if ![runto_main] {
  252. return -1
  253. }
  254. gdb_test_no_output "set breakpoint always-inserted on"
  255. if {$multi_process} {
  256. gdb_test_no_output "set detach-on-fork off"
  257. gdb_test_no_output "set follow-fork-mode child"
  258. }
  259. # Run to _exit in the child.
  260. continue_to_exit_bp
  261. set childpid [get_integer_valueof "mypid" -1]
  262. if { $childpid == -1 } {
  263. untested "failed to extract child pid"
  264. return -1
  265. }
  266. remote_exec target "kill -9 ${childpid}"
  267. # Give it some time to die.
  268. sleep 2
  269. do_detach $multi_process $cmd "signal"
  270. }
  271. }
  272. # The test proper. MULTI_PROCESS is true if testing the multi-process
  273. # variant.
  274. proc do_test {multi_process cmd} {
  275. global testfile srcfile binfile
  276. if {$multi_process && $cmd == "detach"
  277. && [target_info exists gdb,noinferiorio]} {
  278. # This requires inferior I/O to tell whether both the parent
  279. # and child exit successfully.
  280. return
  281. }
  282. set binfile [standard_output_file ${testfile}-$multi_process-$cmd]
  283. set options {debug pthreads}
  284. if {$multi_process} {
  285. lappend options "additional_flags=-DMULTIPROCESS"
  286. }
  287. if {[build_executable "failed to build" \
  288. $testfile-$multi_process-$cmd $srcfile $options] == -1} {
  289. return -1
  290. }
  291. test_detach $multi_process $cmd
  292. foreach wp {"sw" "hw"} {
  293. test_detach_watch $wp $multi_process $cmd
  294. }
  295. test_detach_killed_outside $multi_process $cmd
  296. }
  297. foreach multi_process {0 1} {
  298. set mode [expr {$multi_process ? "multi-process" : "single-process"}]
  299. foreach cmd {"detach" "continue"} {
  300. with_test_prefix "$mode: $cmd" {
  301. do_test $multi_process $cmd
  302. }
  303. }
  304. }