1# Copyright 2009-2013 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#
15# Contributed by Paul Pluzhnikov <ppluzhnikov@google.com>
16#
17
18# This test case verifies that if a display is active on a variable
19# which belongs in a shared library, and that shared library is
20# reloaded (e.g. due to re-execution of the program), GDB will continue
21# to display it (gdb-6.8 crashed under this scenario).
22
23# Also test that a display of variable which is currently present in
24# a shared library, but disappears before re-run, doesn't cause GDB
25# difficulties, and that it continues to display other variables.
26
27# Finally, test that displays which refer to main executable
28# (and thus aren't affected by shared library unloading) are not
29# disabled prematurely.
30
31if { [skip_shlib_tests] || [is_remote target] } {
32    return 0
33}
34
35# Library file.
36set libname "solib-display-lib"
37set srcfile_lib ${srcdir}/${subdir}/${libname}.c
38set binfile_lib ${objdir}/${subdir}/${libname}.so
39set lib_flags {}
40# Binary file.
41set testfile "solib-display-main"
42set srcfile ${srcdir}/${subdir}/${testfile}.c
43set executable ${testfile}
44set binfile ${objdir}/${subdir}/${executable}
45set bin_flags [list debug shlib=${binfile_lib}]
46
47if [get_compiler_info] {
48    return -1
49}
50
51# SEP must be last for the possible `unsupported' error path.
52foreach libsepdebug {NO IN SEP} { with_test_prefix "$libsepdebug" {
53
54    set sep_lib_flags $lib_flags
55    if {$libsepdebug != "NO"} {
56	lappend sep_lib_flags {debug}
57    }
58    if { [gdb_compile_shlib ${srcfile_lib} ${binfile_lib} $sep_lib_flags] != ""
59	 || [gdb_compile ${srcfile} ${binfile} executable $bin_flags] != "" } {
60      untested "Could not compile $binfile_lib or $binfile."
61      return -1
62    }
63
64    if {$libsepdebug == "SEP"} {
65	if {[gdb_gnu_strip_debug $binfile_lib] != 0} {
66	    unsupported "Could not split debug of $binfile_lib."
67	    return
68	} else {
69	    pass "split solib"
70	}
71    }
72
73    clean_restart $executable
74
75    if ![runto_main] then {
76      fail "Can't run to main"
77      return 0
78    }
79
80    gdb_test "display a_global" "1: a_global = 41"
81    gdb_test "display b_global" "2: b_global = 42"
82    gdb_test "display c_global" "3: c_global = 43"
83
84    if { [gdb_start_cmd] < 0 } {
85	fail "Can't run to main (2)"
86	continue
87    }
88
89    gdb_test "" "3: c_global = 43\\r\\n2: b_global = 42\\r\\n1: a_global = 41" "after rerun"
90
91    # Now rebuild the library without b_global
92    if { [gdb_compile_shlib ${srcfile_lib} ${binfile_lib} \
93	      "$sep_lib_flags additional_flags=-DNO_B_GLOBAL"] != ""} {
94	fail "Can't rebuild $binfile_lib"
95    }
96
97    if {$libsepdebug == "SEP"} {
98	set test "split solib second time"
99	if {[gdb_gnu_strip_debug $binfile_lib] != 0} {
100	    fail $test
101	    continue
102	} else {
103	    pass $test
104	}
105    }
106
107    if { [gdb_start_cmd] < 0 } {
108	fail "Can't run to main (3)"
109	continue
110    }
111
112    gdb_test "" "3: c_global = 43\\r\\nwarning: .*b_global.*\\r\\n1: a_global = 41" "after rerun (2)"
113
114    # Now verify that displays which are not in the shared library
115    # are not cleared permaturely.
116
117    gdb_test "break [gdb_get_line_number "break here" ${testfile}.c]" \
118	    ".*Breakpoint.* at .*"
119
120    gdb_test "continue"
121    gdb_test "display main_global" "4: main_global = 44"
122    gdb_test "display a_local" "5: a_local = 45"
123    gdb_test "display a_static" "6: a_static = 46"
124
125    if { [gdb_start_cmd] < 0 } {
126	fail "Can't run to main (4)"
127	continue
128    }
129
130    gdb_test "" "6: a_static = 46\\r\\n4: main_global = 44\\r\\n.*"
131    gdb_test "break [gdb_get_line_number "break here" ${testfile}.c]" \
132	    ".*Breakpoint.* at .*"
133    gdb_test "continue" "6: a_static = 46\\r\\n5: a_local = 45\\r\\n4: main_global = 44\\r\\n.*"
134}}
135