aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/dlmopen-ns-ids.exp
blob: 3ddc07e7773851c0fefeb1da8abc0a1327c1e37a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# This testcase is part of GDB, the GNU debugger.
#
# Copyright 2025 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/>.
#
#
# Test several things related to handling linker namespaces:
# * That the user-facing namespace ID is consistent;

require allow_dlmopen_tests

standard_testfile -main.c -lib.c

set srcfile_lib $srcfile2
set so_name dlmopen-lib.so
set binfile_lib [standard_output_file $so_name]

if { [build_executable "build shlib" $binfile_lib $srcfile_lib \
	  [list debug shlib]] == -1 } {
    return
}

if { [build_executable "failed to build" $testfile $srcfile \
	  [list additional_flags=-DDSO_NAME=\"$binfile_lib\" \
	       shlib_load debug]] } {
    return
}

# Run the command "info sharedlibrary" and get the first namespace
# for the so
proc get_first_so_ns {} {
    set ns -1
    set lib_regexp [string_to_regexp ${::binfile_lib}]
    gdb_test_multiple "info sharedlibrary $::so_name" "get SO namespace" -lbl {
	-re "\r\nFrom\\s+To\\s+\(NS\\s+\)?Syms\\s+Read\\s+Shared Object Library(?=\r\n)" {
	    exp_continue
	}
	-re "\r\n$::hex\\s+$::hex\\s+\\\[\\\[($::decimal)\\\]\\\]\\s+\[^\r\n]+${lib_regexp}(?=\r\n)" {
	    if {$ns == -1} {
		set ns $expect_out(1,string)
	    }
	    exp_continue
	}
	-re -wrap "" {
	}
    }
    return $ns
}

# Run the tests relating to the command "info sharedlibrary", to
# verify that the namespace ID is consistent.
proc test_info_shared {} {
    clean_restart $::binfile

    if { ![runto_main] } {
	return
    }

    # First test that we don't print a namespace column at the start.
    gdb_test "info sharedlibrary" \
	"From\\s+To\\s+Syms\\s+Read\\s+Shared Object Library.*" \
	"before loading anything"

    gdb_breakpoint [gdb_get_line_number "TAG: first dlclose"]
    gdb_continue_to_breakpoint "TAG: first dlclose"

    # Next, test that we *do* print a namespace column after loading SOs.
    gdb_test "info sharedlibrary" \
	"From\\s+To\\s+NS\\s+Syms\\s+Read\\s+Shared Object Library.*" \
	"after loading everything"

    gdb_assert {[get_first_so_ns] == 1} "before closing any library"

    gdb_test "next" ".*second dlclose.*" "close first library"
    gdb_assert {[get_first_so_ns] == 2} "after closing one library"

    gdb_test "next" ".*third dlclose.*" "close second library"
    gdb_assert {[get_first_so_ns] == 3} "before closing two libraries"

    gdb_breakpoint [gdb_get_line_number "TAG: fourth dlclose"]
    gdb_continue_to_breakpoint "TAG: fourth dlclose"
    # As of writing this test, glibc's LMID is just an index on an array of
    # namespaces.  After closing a namespace, requesting a new one will
    # return the index of the lowest-closed namespace, so this will likely
    # be namespace 1, and because of glibc's reuse of the r_debug object,
    # GDB should be able to assign the same number.
    gdb_assert {[get_first_so_ns] == [get_integer_valueof "lmid" "-1"]} \
	"reopen a namespace"

    gdb_test "next" ".*return 0.*" "final namespace inactive"
    gdb_test "info sharedlibrary" \
	"From\\s+To\\s+Syms\\s+Read\\s+Shared Object Library.*" \
	"after unloading everything"
}

test_info_shared