step-over-exec.exp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright 2020-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 stepping over an exec syscall instruction in a multi-threaded program.
  15. standard_testfile .c -execd.c
  16. set syscalls_src $srcdir/lib/my-syscalls.S
  17. # EXECR_THREAD is "leader" or "other", and decides which thread does the exec.
  18. #
  19. # If DIFFERENT_TEXT_SEGMENTS is true, the exec'er and exec'd program are
  20. # compiled with different, explicit text segment addresses. This makes it so
  21. # the address of the displaced stepping buffer in the old executable is likely
  22. # not accessible in the new executable. This might catch cases where GDB tries
  23. # (wrongfully) to restore the bytes saved from the old executable in the new
  24. # executable.
  25. #
  26. # DISPLACED_STEPPING is "auto" or "off" and controls the value of "set
  27. # displaced-stepping".
  28. proc do_test { execr_thread different_text_segments displaced_stepping } {
  29. global srcdir subdir srcfile srcfile2 binfile
  30. global syscalls_src
  31. global decimal hex
  32. set execr_srcs [list $srcdir/$subdir/$srcfile $syscalls_src]
  33. set execd_srcs [list $srcdir/$subdir/$srcfile2]
  34. # Generate unique filenames for each case.
  35. set execr_binfile $binfile-execr-thread-$execr_thread-diff-text-segs-$different_text_segments
  36. set execd_binfile $execr_binfile-execd
  37. set execr_opts [list debug]
  38. set execd_opts [list debug]
  39. if { $different_text_segments } {
  40. lappend execr_opts "ldflags=-Wl,-Ttext-segment=0x600000"
  41. lappend execd_opts "ldflags=-Wl,-Ttext-segment=0x800000"
  42. }
  43. if { $execr_thread == "leader" } {
  44. lappend execr_opts "additional_flags=-DLEADER_DOES_EXEC"
  45. } elseif { $execr_thread == "other" } {
  46. lappend execr_opts "additional_flags=-DOTHER_DOES_EXEC"
  47. } else {
  48. error "Invalid execr_thread value: $execr_thread."
  49. }
  50. # Compile execr binary (the one that does the exec).
  51. if {[gdb_compile_pthreads $execr_srcs $execr_binfile executable $execr_opts] != "" } {
  52. return -1
  53. }
  54. # Compile the second binary (the one that gets exec'd).
  55. if {[gdb_compile $execd_srcs $execd_binfile executable $execd_opts] != "" } {
  56. return -1
  57. }
  58. clean_restart ${execr_binfile}
  59. gdb_test_no_output "set displaced-stepping $displaced_stepping"
  60. if ![runto_main] {
  61. return
  62. }
  63. # Leave breakpoint main inserted, we expect to hit it after exec.
  64. # This breakpoint will be stepped by whatever thread does the exec.
  65. gdb_test "break my_execve_syscall if 0" "Breakpoint $decimal at $hex.*"
  66. # Continue across exec to main.
  67. if { [target_is_gdbserver] } {
  68. setup_kfail gdb/27020 "*-*-*"
  69. }
  70. set failed [gdb_test "continue" \
  71. "process $decimal is executing new program: .* hit Breakpoint $decimal, main .*" \
  72. "continue across exec"]
  73. if { $failed } {
  74. return
  75. }
  76. # Just to confirm we are indeed in the execd program.
  77. gdb_test "print a_variable_in_execd" " = 1212"
  78. # Continue execution to make sure we can step over the breakpoint on main.
  79. # It would be nice to use gdb_continue_to_end to ensure the program can
  80. # exit properly, but it hangs due to PR gdb/26995.
  81. gdb_breakpoint foo
  82. gdb_test "continue" "Breakpoint $decimal, foo .*" \
  83. "continue to foo"
  84. }
  85. foreach_with_prefix displaced_stepping {auto off} {
  86. foreach_with_prefix different_text_segments {true false} {
  87. foreach_with_prefix execr_thread {leader other} {
  88. do_test $execr_thread $different_text_segments $displaced_stepping
  89. }
  90. }
  91. }