summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/jit-bfd-name.exp
blob: 80f06269524e51d397697d53f46f607dfbd040a9 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright 2022-2023 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/>.

# Check the BFD filename (as used in the symfile name) that is
# automatically generated for in-memory BFD files, as used by the JIT
# system within GDB.
#
# Additionally, check that GDB cau use 'dump binary memory' to write
# out the in-memory JIT files.

require allow_shlib_tests

load_lib jit-elf-helpers.exp

# The main code that loads and registers JIT objects.
set main_basename "jit-elf-main"
set main_srcfile ${srcdir}/${subdir}/${main_basename}.c
set main_binfile [standard_output_file ${main_basename}]

# The shared library that gets loaded as JIT objects.
set jit_solib_basename jit-elf-solib
set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c

# Compile two shared libraries to use as JIT objects.
set jit_solibs_target [compile_and_download_n_jit_so \
			   $jit_solib_basename $jit_solib_srcfile 2 \
			   {debug}]
if { $jit_solibs_target == -1 } {
    return
}

# Compile the main code (which loads the JIT objects).
if { [compile_jit_main ${main_srcfile} ${main_binfile} {}] != 0 } {
    return
}

clean_restart $::main_binfile
if { ![runto_main] } {
    return
}

# Poke desired values directly into inferior instead of using "set
# args" because "set args" does not work under gdbserver.
set count [expr [llength $jit_solibs_target] + 1]
gdb_test_no_output "set var argc=$count" "forging argc"
gdb_test_no_output "set var argv=fake_argv" "forging argv"
for {set i 1} {$i < $count} {incr i} {
    set jit_solib_target [lindex $jit_solibs_target [expr $i-1]]
    gdb_test_no_output "set var argv\[$i\]=\"${jit_solib_target}\"" \
	"forging argv\[$i\]"
}

# Run until the JIT libraries are loaded.
gdb_breakpoint [gdb_get_line_number "break here 1" $::main_srcfile]
gdb_continue_to_breakpoint "break here 1"

# Confirm that the two expected functions are available.
gdb_test "info function ^jit_function" \
    [multi_line \
	 "File \[^\r\n\]+jit-elf-solib.c:" \
	 "${decimal}:\\s+int jit_function_0001\\(\\);" \
	 "${decimal}:\\s+int jit_function_0002\\(\\);"]

# Capture the addresses of each JIT symfile.
set symfile_addrs {}
set symfile_lengths {}
gdb_test_multiple "maint info jit" "" {
    -re "^maint info jit\r\n" {
	exp_continue
    }
    -re "^jit_code_entry address\\s+symfile address\\s+symfile size\\s*\r\n" {
	exp_continue
    }
    -re "^${hex}\\s+(${hex})\\s+(${decimal})\\s*\r\n" {
	lappend symfile_addrs $expect_out(1,string)
	lappend symfile_lengths $expect_out(2,string)
	exp_continue
    }
    -re "^$gdb_prompt $" {
    }
}

# Now check the 'maint info symtabs' output to ensure that each
# symfile is mentioned, and that the names are as expected.
set bfd_name_addrs {}
gdb_test_multiple "maint info symtabs" "" {
    -re "^maint info symtabs\r\n" {
	exp_continue
    }
    -re "^\\\}\\s*\r\n" {
	exp_continue
    }
    -re "^\\\{ objfile <in-memory@($hex)>\\s+\[^\r\n\]+\r\n" {
	lappend bfd_name_addrs $expect_out(1,string)
	exp_continue
    }
    -re "^\\\{ objfile (\\S+)\\s+\[^\r\n\]+\r\n" {
	exp_continue
    }
    -re "^\\s+\[^\r\n\]+\r\n" {
	exp_continue
    }
    -re "^$gdb_prompt $" {
    }
}

# Now dump each JIT solib using the 'dump binary memory' command.
set count 0
foreach addr $symfile_addrs len $symfile_lengths {
    incr count
    set output [standard_output_file "dump-elf-solib.${count}.so"]
    set end [expr $addr + $len]
    gdb_test_no_output "dump binary memory $output $addr $end" \
	"dump jit solib $count"

    gdb_assert { [cmp_binary_files $output [standard_output_file "jit-elf-solib.${count}.so"]] == 0} \
	"check dump of jit solib $count is as expected"
}


# Check that each of the expected jit symfile addresses was mentioned
# in an in-memory BFD filename.
set count 1
foreach addr $symfile_addrs {
    # Drop any loading zeros from the symfile address.
    set addr [format 0x%x $addr]

    # Check there was a BFD with the expected address in its name.
    gdb_assert { [expr [lsearch -exact $bfd_name_addrs $addr] != -1] } \
	"check for in-memory bfd $count"
    incr count
}

# Continue until the JIT libraries are unloaded.
gdb_breakpoint [gdb_get_line_number "break here 2" $::main_srcfile]
gdb_continue_to_breakpoint "break here 2"

# All jit librares must have been unregistered.
gdb_test "info function jit_function" \
    "All functions matching regular expression \"jit_function\":"