clone-attach-detach.exp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # This testcase is part of GDB, the GNU debugger.
  2. # Copyright 2016-2022 Free Software Foundation, Inc.
  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 3 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, see <http://www.gnu.org/licenses/>.
  15. # Test attach / detach from a process that uses raw clone. We use raw
  16. # clone as proxy for when libthread_db is not available.
  17. # This only works on targets with the Linux kernel.
  18. if ![istarget *-*-linux*] {
  19. return
  20. }
  21. if {![can_spawn_for_attach]} {
  22. return 0
  23. }
  24. standard_testfile
  25. if [prepare_for_testing "failed to prepare" $testfile $srcfile {debug}] {
  26. return -1
  27. }
  28. set test_spawn_id [spawn_wait_for_attach $binfile]
  29. set testpid [spawn_id_get_pid $test_spawn_id]
  30. # Native/gdbserver.
  31. set thread_re "(LWP $decimal|Thread )"
  32. # Try attach / detach a few times, in case GDB ends up with stale
  33. # state after detaching.
  34. set attempts 3
  35. for {set attempt 1} {$attempt <= $attempts} {incr attempt} {
  36. with_test_prefix "fg attach $attempt" {
  37. gdb_test "attach $testpid" \
  38. "Attaching to program.*process $testpid.*" \
  39. "attach"
  40. gdb_test "info threads" \
  41. "1.*${thread_re}.*\r\n.*2.*${thread_re}.*" \
  42. "info threads shows two LWPs"
  43. gdb_test "detach" "Detaching from .*, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]"
  44. }
  45. }
  46. # Same, but use async/background attach.
  47. # If debugging with target remote, check whether the all-stop variant
  48. # of the RSP is being used. If so, we can't run the background tests.
  49. if {[target_info exists gdb_protocol]
  50. && ([target_info gdb_protocol] == "remote"
  51. || [target_info gdb_protocol] == "extended-remote")} {
  52. set test "maint show target-non-stop"
  53. gdb_test_multiple "maint show target-non-stop" $test {
  54. -re "(is|currently) on.*$gdb_prompt $" {
  55. }
  56. -re "(is|currently) off.*$gdb_prompt $" {
  57. unsupported "bg attach: can't issue info threads while target is running"
  58. return 0
  59. }
  60. }
  61. }
  62. set attempts 3
  63. for {set attempt 1} {$attempt <= $attempts} {incr attempt} {
  64. with_test_prefix "bg attach $attempt" {
  65. gdb_test "attach $testpid &" \
  66. "Attaching to program.*process $testpid.*" \
  67. "attach"
  68. gdb_test "info threads" \
  69. "1.*${thread_re}.*\\(running\\)\r\n.*2.*${thread_re}.*\\(running\\)" \
  70. "info threads shows two LWPs"
  71. gdb_test "detach" "Detaching from .*, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]"
  72. }
  73. }
  74. kill_wait_spawned_process $test_spawn_id