vfork-multi-thread.exp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 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 that a multi-threaded program doing a vfork doesn't miss breakpoints.
  15. #
  16. # When a program vforks, its address space is shared with the parent. When we
  17. # detach a vfork child, we must keep breakpoints out of that shared address space
  18. # until the child either exits or execs, so that the child does not hit a
  19. # breakpoint while out of GDB's control. During that time, threads from
  20. # the parent must be held stopped, otherwise they could miss breakpoints.
  21. #
  22. # The thread that did the vfork is suspended by the kernel, so it's not a
  23. # concern. The other threads need to be manually stopped by GDB and resumed
  24. # once the vfork critical region is done.
  25. #
  26. # This test spawns one thread that calls vfork. Meanwhile, the main thread
  27. # crosses a breakpoint. A buggy GDB would let the main thread run while
  28. # breakpoints are removed, so the main thread would miss the breakpoint and run
  29. # until exit.
  30. standard_testfile
  31. if { [build_executable "failed to prepare" ${testfile} ${srcfile} {debug pthreads}] } {
  32. return
  33. }
  34. set any "\[^\r\n\]*"
  35. # A bunch of util procedures to continue an inferior to an expected point.
  36. proc continue_to_parent_breakpoint {} {
  37. gdb_test "continue" \
  38. "hit Breakpoint .* should_break_here .*" \
  39. "continue parent to breakpoint"
  40. }
  41. proc continue_to_parent_end {} {
  42. gdb_test "continue" "Inferior 1.*exited with code 06.*" \
  43. "continue parent to end"
  44. }
  45. # Run the test with the given GDB settings.
  46. proc do_test { target-non-stop non-stop follow-fork-mode detach-on-fork schedule-multiple } {
  47. save_vars { ::GDBFLAGS } {
  48. append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\""
  49. append ::GDBFLAGS " -ex \"set non-stop ${non-stop}\""
  50. clean_restart ${::binfile}
  51. }
  52. gdb_test_no_output "set follow-fork-mode ${follow-fork-mode}"
  53. gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
  54. gdb_test_no_output "set schedule-multiple ${schedule-multiple}"
  55. # The message about thread 2 of inferior 1 exiting happens at a somewhat
  56. # unpredictable moment, it's simpler to silence it than to try to match it.
  57. gdb_test_no_output "set print thread-events off"
  58. if { ![runto_main] } {
  59. return
  60. }
  61. # The main thread is expected to hit this breakpoint.
  62. gdb_test "break should_break_here" "Breakpoint $::decimal at .*"
  63. continue_to_parent_breakpoint
  64. continue_to_parent_end
  65. }
  66. # We only test with follow-fork-mode=parent and detach-on-fork=on at the
  67. # moment, but the loops below are written to make it easy to add other values
  68. # on these axes in the future.
  69. foreach_with_prefix target-non-stop {auto on off} {
  70. foreach_with_prefix non-stop {off on} {
  71. foreach_with_prefix follow-fork-mode {parent} {
  72. foreach_with_prefix detach-on-fork {on} {
  73. foreach_with_prefix schedule-multiple {off on} {
  74. do_test ${target-non-stop} ${non-stop} ${follow-fork-mode} ${detach-on-fork} ${schedule-multiple}
  75. }
  76. }
  77. }
  78. }
  79. }