From b903e17e961ef45c2cfa76e5b65a3f25e55398ed Mon Sep 17 00:00:00 2001 From: Doug Evans Date: Mon, 6 May 2013 22:20:18 +0000 Subject: [PATCH] * gdb.base/solib-search-lib1.c: New file. * gdb.base/solib-search-lib2.c: New file. * gdb.base/solib-search.c: New file. * gdb.base/solib-search.h: New file. * gdb.base/solib-search.exp: New file. --- gdb/testsuite/ChangeLog | 6 + gdb/testsuite/gdb.base/solib-search-lib1.c | 51 ++++++ gdb/testsuite/gdb.base/solib-search-lib2.c | 51 ++++++ gdb/testsuite/gdb.base/solib-search.c | 31 ++++ gdb/testsuite/gdb.base/solib-search.exp | 184 +++++++++++++++++++++ gdb/testsuite/gdb.base/solib-search.h | 32 ++++ 6 files changed, 355 insertions(+) create mode 100644 gdb/testsuite/gdb.base/solib-search-lib1.c create mode 100644 gdb/testsuite/gdb.base/solib-search-lib2.c create mode 100644 gdb/testsuite/gdb.base/solib-search.c create mode 100644 gdb/testsuite/gdb.base/solib-search.exp create mode 100644 gdb/testsuite/gdb.base/solib-search.h diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index b41b135ab47..ec491f11dd1 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2013-05-06 Doug Evans + * gdb.base/solib-search-lib1.c: New file. + * gdb.base/solib-search-lib2.c: New file. + * gdb.base/solib-search.c: New file. + * gdb.base/solib-search.h: New file. + * gdb.base/solib-search.exp: New file. + * lib/gdb.exp (gdb_core_cmd): New function. * gdb.arch/system-gcore.exp: Use it. * gdb.arch/vsx-regs.exp: Ditto. diff --git a/gdb/testsuite/gdb.base/solib-search-lib1.c b/gdb/testsuite/gdb.base/solib-search-lib1.c new file mode 100644 index 00000000000..38c748e4e6c --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-search-lib1.c @@ -0,0 +1,51 @@ +/* This test program is part of GDB, the GNU debugger. + + Copyright 2013 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 . + */ + +#include "solib-search.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE 1 +#endif + +const int lib1_array[ARRAY_SIZE] = { 42 }; +const int lib1_size = ARRAY_SIZE; + +void +lib1_func1 (void) +{ + lib2_func2 (); +} + +/* Make the relative address of func3 different b/w the "wrong" and "right" + versions of the library" to further ensure backtrace doesn't work with + the "wrong" version. */ + +#ifdef RIGHT + +void +lib1_spacer (void) +{ +} + +#endif + +void +lib1_func3 (void) +{ + lib2_func4 (); +} diff --git a/gdb/testsuite/gdb.base/solib-search-lib2.c b/gdb/testsuite/gdb.base/solib-search-lib2.c new file mode 100644 index 00000000000..6fc08abc1b3 --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-search-lib2.c @@ -0,0 +1,51 @@ +/* This test program is part of GDB, the GNU debugger. + + Copyright 2013 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 . + */ + +#include "solib-search.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE 1 +#endif + +const int lib2_array[ARRAY_SIZE] = { 42 }; +const int lib2_size = ARRAY_SIZE; + +void +lib2_func2 (void) +{ + lib1_func3 (); +} + +/* Make the relative address of func4 different b/w the "wrong" and "right" + versions of the library" to further ensure backtrace doesn't work with + the "wrong" version. */ + +#ifdef RIGHT + +void +lib2_spacer (void) +{ +} + +#endif + +void +lib2_func4 (void) +{ + break_here (); +} diff --git a/gdb/testsuite/gdb.base/solib-search.c b/gdb/testsuite/gdb.base/solib-search.c new file mode 100644 index 00000000000..106c6eadfbc --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-search.c @@ -0,0 +1,31 @@ +/* This test program is part of GDB, the GNU debugger. + + Copyright 2013 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 . + */ + +#include "solib-search.h" + +int +main () +{ + lib1_func1 (); + return 0; +} + +void +break_here () +{ +} diff --git a/gdb/testsuite/gdb.base/solib-search.exp b/gdb/testsuite/gdb.base/solib-search.exp new file mode 100644 index 00000000000..cb00d6fbfe5 --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-search.exp @@ -0,0 +1,184 @@ +# Copyright 2013 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 . + +# Test solib-search-path, and in the case of solib-svr4.c whether l_addr_p +# is properly reset when the path is changed. + +if {[is_remote target] || [skip_shlib_tests]} { + untested solib-search.exp + return -1 +} + +# This is required by gdb_compile_shlib. +if {[get_compiler_info]} { + warning "Could not get compiler info" + untested solib-search.exp + return 1 +} + +# Build "wrong" and "right" versions of the libraries in separate directories. +set wrong_lib_subdir "solib-search-wrong" +set right_lib_subdir "solib-search-right" + +# First library file. +set libname1 "solib-search-lib1" +set srcfile1_lib ${srcdir}/${subdir}/${libname1}.c +set wrong_binfile1_lib ${objdir}/${subdir}/${wrong_lib_subdir}/${libname1}.so +set right_binfile1_lib ${objdir}/${subdir}/${right_lib_subdir}/${libname1}.so +# Second library file. +set libname2 "solib-search-lib2" +set srcfile2_lib ${srcdir}/${subdir}/${libname2}.c +set wrong_binfile2_lib ${objdir}/${subdir}/${wrong_lib_subdir}/${libname2}.so +set right_binfile2_lib ${objdir}/${subdir}/${right_lib_subdir}/${libname2}.so +# Link with the library that lives here. +# This is so that we can replace what gdb sees with the wrong copy, +# and then tell gdb to use the right copy that lives someplace else. +set binfile1_lib ${objdir}/${subdir}/${libname1}.so +set binfile2_lib ${objdir}/${subdir}/${libname2}.so + +set lib_flags [list debug ldflags=-Wl,-Bsymbolic] +set wrong_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=1" +set right_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=8192 -DRIGHT" + +# Binary file. +set testfile "solib-search" +set srcfile ${srcdir}/${subdir}/${testfile}.c +set binfile ${objdir}/${subdir}/${testfile} +set bin_flags [list debug shlib=${binfile1_lib} shlib=${binfile2_lib}] + +remote_exec build "rm -rf ${subdir}/${wrong_lib_subdir}" +remote_exec build "rm -rf ${subdir}/${right_lib_subdir}" +remote_exec build "mkdir ${subdir}/${wrong_lib_subdir}" +remote_exec build "mkdir ${subdir}/${right_lib_subdir}" + +if { [gdb_compile_shlib ${srcfile1_lib} ${wrong_binfile1_lib} $wrong_lib_flags] != "" + || [gdb_compile_shlib ${srcfile2_lib} ${wrong_binfile2_lib} $wrong_lib_flags] != "" + || [gdb_compile_shlib ${srcfile1_lib} ${right_binfile1_lib} $right_lib_flags] != "" + || [gdb_compile_shlib ${srcfile2_lib} ${right_binfile2_lib} $right_lib_flags] != "" } { + untested "Could not compile libs." + return -1 +} + +# Build the test binary using the right copies of the libraries. +remote_exec build "ln -sf ${right_lib_subdir}/${libname1}.so ${binfile1_lib}" +remote_exec build "ln -sf ${right_lib_subdir}/${libname2}.so ${binfile2_lib}" +if { [gdb_compile ${srcfile} ${binfile} executable $bin_flags] != "" } { + untested "Could not compile $binfile." + return -1 +} + +clean_restart $testfile + +if { ![runto_main] } { + fail "Can't run to main" + return +} + +gdb_breakpoint "break_here" +gdb_continue "break_here" + +set corefile "${objdir}/${subdir}/solib-search.core" +set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"] + +if {!$core_supported} { + return -1 +} + +# Before we restart gdb, replace the libs with the "wrong" ones. +remote_exec build "ln -sf ${wrong_lib_subdir}/${libname1}.so ${binfile1_lib}" +remote_exec build "ln -sf ${wrong_lib_subdir}/${libname2}.so ${binfile2_lib}" + +clean_restart $testfile + +set core_loaded [gdb_core_cmd $corefile "re-load generated corefile"] +if { $core_loaded == -1 } { + # No use proceeding from here. + return +} + +proc test_backtrace { expect_fail } { + global gdb_prompt + + set count 0 + set total_expected 5 + + if { $expect_fail } { + set testname "backtrace (with wrong libs)" + } else { + set testname "backtrace (with right libs)" + } + # N.B. The order of the tests here is important. + # We need to count each function in the backtrace, and expect matches + # the first one it finds. + gdb_test_multiple "backtrace" $testname { + -re "\[^\r\n\]* in lib2_func4 \[^\r\n\]*" { + incr count + exp_continue + } + -re "\[^\r\n\]* in lib1_func3 \[^\r\n\]*" { + incr count + exp_continue + } + -re "\[^\r\n\]* in lib2_func2 \[^\r\n\]*" { + incr count + exp_continue + } + -re "\[^\r\n\]* in lib1_func1 \[^\r\n\]*" { + incr count + exp_continue + } + -re "\[^\r\n\]* in main \[^\r\n\]*" { + incr count + exp_continue + } + -re "\[\r\n\]$gdb_prompt $" { + pass "$testname (data collection)" + } + } + + set fail 0 + if { $expect_fail } { + # If the backtrace output is correct the test isn't sufficiently + # testing what it should. + if { $count == $total_expected } { + set fail 1 + } + } else { + if { $count != $total_expected } { + set fail 1 + } + } + if { $fail } { + fail $testname + } else { + pass $testname + } +} + +# Verify the backtrace is messed up. +test_backtrace 1 + +# Remove the copies gdb currently sees: the absolute path is encoded in +# the core file and we want gdb to find the right copies elsewhere. +remote_exec build "rm -f ${binfile1_lib}" +remote_exec build "rm -f ${binfile2_lib}" +# Set solib-search-path to use the correct copies of libraries. +gdb_test "set solib-search-path ${objdir}/${subdir}/${right_lib_subdir}" + +# Verify gdb has properly updated the location of the libraries. +test_backtrace 0 +gdb_test "p lib1_size" " = 8192" +gdb_test "p lib2_size" " = 8192" + diff --git a/gdb/testsuite/gdb.base/solib-search.h b/gdb/testsuite/gdb.base/solib-search.h new file mode 100644 index 00000000000..b75d4ff9944 --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-search.h @@ -0,0 +1,32 @@ +/* This test program is part of GDB, the GNU debugger. + + Copyright 2013 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 . + */ + +#ifndef GDB_BASE_SOLIB_SEARCH_H +#define GDB_BASE_SOLIB_SEARCH_H + +/* These functions create a call chain that traverses both libs. */ + +extern void lib1_func1 (void); +extern void lib1_func3 (void); + +extern void lib2_func2 (void); +extern void lib2_func4 (void); + +extern void break_here (void); + +#endif /* GDB_BASE_SOLIB_SEARCH_H */ -- 2.30.2