This patch addresses PR gdb/27133. Before it, the following succession of commands would cause gdb to crash: set logging redirect on set logging debugredirect on set logging on The problem eventually comes down to a use after free. The function cli_interp_base::set_logging is called with a unique_ptr argument that holds a pointer to the redirection file. In the problematic use case, no-one ever took ownership of that pointer (as far as unique_ptr is concerned), so the call to its dtor at the end of the function causes the file object to be deleted. Any later use of the pointer to the redirection file is therefore an error. This patch ensures that the unique_ptr is released when required (so it does not assume ownership anymore). The internal logic of cli_interp_base::set_logging takes care of freeing the ui_file when it is not necessary anymore using the saved_output.file_to_delete field. gdb/ChangeLog: PR gdb/27133 * cli/cli-interp.c (cli_interp_base::set_logging): Ensure the unique_ptr is released when the wrapped pointer is kept for later use. gdb/testsuite/ChangeLog: PR gdb/27133 * gdb.base/ui-redirect.exp: Add test case that ensures that redirecting both logging and debug does not cause gdb to crash.
151 lines
4.8 KiB
Plaintext
151 lines
4.8 KiB
Plaintext
# Copyright (C) 2010-2021 Free Software Foundation, Inc.
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# Do not run if gdb debug is enabled as it will interfere with log redirect.
|
|
if [gdb_debug_enabled] {
|
|
untested "debug is enabled"
|
|
return 0
|
|
}
|
|
|
|
if { [prepare_for_testing "failed to prepare" ui-redirect start.c] } {
|
|
return -1
|
|
}
|
|
|
|
if ![runto_main] {
|
|
fail "can't run to main"
|
|
return -1
|
|
}
|
|
|
|
set test "commands"
|
|
gdb_test_multiple $test $test {
|
|
-re "End with a line saying just \"end\"\\.\r\n>$" {
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
set test "print 1"
|
|
gdb_test_multiple $test $test {
|
|
-re "\r\n>$" {
|
|
pass $test
|
|
}
|
|
}
|
|
gdb_test_no_output "end"
|
|
|
|
gdb_breakpoint "foo"
|
|
gdb_breakpoint "bar"
|
|
|
|
set cmds [multi_line_input \
|
|
"break -qualified main" \
|
|
" commands" \
|
|
" print 1" \
|
|
" end" \
|
|
"break foo" \
|
|
"break bar"]
|
|
set cmds "$cmds\n"
|
|
set outdir [standard_output_file {}]
|
|
set cmds_file "$outdir/cmds.txt"
|
|
|
|
with_test_prefix "userdefined" {
|
|
set test "define userdefined"
|
|
gdb_test_multiple $test $test {
|
|
-re "End with a line saying just \"end\"\\.\r\n>$" {
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
set test "bt"
|
|
gdb_test_multiple $test $test {
|
|
-re "\r\n>$" {
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
gdb_test_no_output "end"
|
|
}
|
|
|
|
with_test_prefix "logging" {
|
|
gdb_test_no_output "set logging file /dev/null"
|
|
gdb_test "set logging on" \
|
|
"Copying output to /dev/null.*Copying debug output to /dev/null\\."
|
|
gdb_test "save breakpoints $cmds_file" "Saved to file '$cmds_file'\\." \
|
|
"save breakpoints cmds.txt"
|
|
cmp_file_string "$cmds_file" "$cmds" "cmds.txt"
|
|
gdb_test "userdefined" "#0 main ().*"
|
|
gdb_test "set logging off" "Done logging to /dev/null\\."
|
|
gdb_test "help" "List of classes of commands:.*"
|
|
}
|
|
|
|
with_test_prefix "redirect" {
|
|
gdb_test "set logging redirect on"
|
|
gdb_test "set logging on" \
|
|
"Redirecting output to /dev/null.*Copying debug output to /dev/null\\."
|
|
gdb_test_no_output "save breakpoints $cmds_file" "save breakpoints cmds.txt"
|
|
cmp_file_string "$cmds_file" "$cmds" "cmds.txt"
|
|
gdb_test_no_output "userdefined"
|
|
gdb_test "set logging off" "Done logging to /dev/null\\."
|
|
gdb_test "help" "List of classes of commands:.*"
|
|
}
|
|
|
|
with_test_prefix "redirect while already logging" {
|
|
gdb_test_no_output "set logging redirect off"
|
|
gdb_test "set logging on" \
|
|
"Copying output to /dev/null.*Copying debug output to /dev/null\\."
|
|
gdb_test "set logging redirect on" \
|
|
".*warning: Currently logging .*Turn the logging off and on to make the new setting effective.*"
|
|
gdb_test "save breakpoints $cmds_file" "Saved to file '$cmds_file'\\." \
|
|
"save breakpoints cmds.txt"
|
|
cmp_file_string "$cmds_file" "$cmds" "cmds.txt"
|
|
gdb_test "userdefined" "#0 main ().*"
|
|
gdb_test "set logging off" "Done logging to /dev/null\\."
|
|
gdb_test "help" "List of classes of commands:.*"
|
|
gdb_test_no_output "set logging redirect off"
|
|
}
|
|
|
|
with_test_prefix "debugging" {
|
|
gdb_test "set debug infrun 1"
|
|
gdb_test "set logging on" \
|
|
"Copying output to /dev/null.*Copying debug output to /dev/null\\."
|
|
|
|
set prompt "$gdb_prompt \\\[infrun\\\] fetch_inferior_event: exit\r\n$"
|
|
gdb_test_multiple "continue" "continue" -prompt $prompt {
|
|
-re "Continuing.*\\\[infrun\\\] .*\\\[infrun\\\] .*Breakpoint \[0-9\]+, foo.*$prompt$" {
|
|
pass $gdb_test_name
|
|
}
|
|
}
|
|
|
|
gdb_test "set debug infrun 0"
|
|
gdb_test "set logging off" "Done logging to /dev/null\\."
|
|
gdb_test "help" "List of classes of commands:.*"
|
|
}
|
|
|
|
with_test_prefix "redirect debugging" {
|
|
gdb_test_no_output "set logging debugredirect on"
|
|
gdb_test "set debug infrun 1"
|
|
gdb_test "set logging on" \
|
|
"Copying output to /dev/null.*Redirecting debug output to /dev/null\\."
|
|
gdb_test "continue" "Continuing.*((?!infrun).).*Breakpoint \[0-9\]+, bar.*"
|
|
gdb_test "set debug infrun 0"
|
|
gdb_test "set logging off" "Done logging to /dev/null\\."
|
|
gdb_test "help" "List of classes of commands:.*"
|
|
}
|
|
|
|
with_test_prefix "redirect logging and debuging" {
|
|
gdb_test_no_output "set logging redirect on"
|
|
gdb_test_no_output "set logging debugredirect on"
|
|
gdb_test "set logging on" \
|
|
"Redirecting output to /dev/null.*Redirecting debug output to /dev/null\\."
|
|
gdb_test "set logging off" "Done logging to /dev/null\\."
|
|
}
|