1# Copyright 2009-2020 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] } {
32    return 0
33}
34
35# This test is currently not supported for stub targets, because it uses the
36# start command (through gdb_start_cmd).  In theory, it could be changed to
37# use something else (kill + gdb_run_cmd with a manual breakpoint at main).
38# However, when we try that with the native-gdbserver board, we see that the
39# test fails and gdb outputs this upon connection:
40#
41#   warning: Unable to display "a_global": No symbol "a_global" in current context.
42#   warning: Unable to display "b_global": No symbol "b_global" in current context.
43#   warning: Unable to display "c_global": No symbol "c_global" in current context.
44#
45# This is because the initial stop is done before the shared libraries are
46# loaded.
47
48if { [use_gdb_stub] } {
49    return 0
50}
51
52# Library file.
53set libname "solib-display-lib"
54set srcfile_lib ${srcdir}/${subdir}/${libname}.c
55set binfile_lib [standard_output_file ${libname}.so]
56set lib_flags {}
57# Binary file.
58set testfile "solib-display-main"
59set srcfile ${srcdir}/${subdir}/${testfile}.c
60set executable ${testfile}
61set binfile [standard_output_file ${executable}]
62set bin_flags [list debug shlib=${binfile_lib}]
63
64if [get_compiler_info] {
65    return -1
66}
67
68# SEP must be last for the possible `unsupported' error path.
69foreach libsepdebug {NO IN SEP} { with_test_prefix "$libsepdebug" {
70
71    set sep_lib_flags $lib_flags
72    if {$libsepdebug != "NO"} {
73	lappend sep_lib_flags {debug}
74    }
75    if { [gdb_compile_shlib ${srcfile_lib} ${binfile_lib} $sep_lib_flags] != ""
76	 || [gdb_compile ${srcfile} ${binfile} executable $bin_flags] != "" } {
77      untested "failed to compile"
78      return -1
79    }
80
81    if {$libsepdebug == "SEP"} {
82	if {[gdb_gnu_strip_debug $binfile_lib] != 0} {
83	    unsupported "could not split debug of $binfile_lib."
84	    return
85	} else {
86	    pass "split solib"
87	}
88    }
89
90    clean_restart $executable
91
92    if ![runto_main] then {
93      fail "can't run to main"
94      return 0
95    }
96
97    gdb_test "display (int) a_global" "1: \\(int\\) a_global = 41"
98    gdb_test "display (int) b_global" "2: \\(int\\) b_global = 42"
99    gdb_test "display (int) c_global" "3: \\(int\\) c_global = 43"
100
101    if { [gdb_start_cmd] < 0 } {
102	fail "can't run to main (2)"
103	continue
104    }
105
106    gdb_test "" [multi_line \
107		     "1: \\(int\\) a_global = 41" \
108		     "2: \\(int\\) b_global = 42"  \
109		     "3: \\(int\\) c_global = 43" \
110		    ] "after rerun"
111
112    # Now rebuild the library without b_global
113    if { [gdb_compile_shlib ${srcfile_lib} ${binfile_lib} \
114	      "$sep_lib_flags additional_flags=-DNO_B_GLOBAL"] != ""} {
115	fail "can't rebuild $binfile_lib"
116    }
117
118    if {$libsepdebug == "SEP"} {
119	set test "split solib second time"
120	if {[gdb_gnu_strip_debug $binfile_lib] != 0} {
121	    fail $test
122	    continue
123	} else {
124	    pass $test
125	}
126    }
127
128    if { [gdb_start_cmd] < 0 } {
129	fail "can't run to main (3)"
130	continue
131    }
132
133
134    gdb_test "" [multi_line \
135		     "1: \\(int\\) a_global = 41" \
136		     "warning: .*b_global.*"  \
137		     "3: \\(int\\) c_global = 43" \
138		    ] "after rerun (2)"
139
140    # Now verify that displays which are not in the shared library
141    # are not cleared permaturely.
142
143    gdb_test "break [gdb_get_line_number "break here" ${testfile}.c]" \
144	    ".*Breakpoint.* at .*"
145
146    gdb_test "continue"
147    gdb_test "display main_global" "4: main_global = 44"
148    gdb_test "display a_local" "5: a_local = 45"
149    gdb_test "display a_static" "6: a_static = 46"
150
151    if { [gdb_start_cmd] < 0 } {
152	fail "can't run to main (4)"
153	continue
154    }
155
156    gdb_test "" "6: a_static = 46\\r\\n4: main_global = 44\\r\\n.*"
157    gdb_test "break [gdb_get_line_number "break here" ${testfile}.c]" \
158	    ".*Breakpoint.* at .*"
159    gdb_test "continue" [multi_line \
160			     "4: main_global = 44" \
161			     "5: a_local = 45" \
162			     "6: a_static = 46" \
163			    ]
164}}
165