]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-objfile.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-objfile.exp
CommitLineData
213516ef 1# Copyright (C) 2011-2023 Free Software Foundation, Inc.
29703da4
PM
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# This file is part of the GDB testsuite. It tests the program space
17# support in Python.
18
29703da4
PM
19load_lib gdb-python.exp
20
b4a58790 21standard_testfile
29703da4 22
5b362f04 23if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
29703da4
PM
24 return -1
25}
26
27# Skip all tests if Python scripting is not enabled.
28if { [skip_python_tests] } { continue }
29
b0e16ca5 30if {![runto_main]} {
29703da4
PM
31 return 0
32}
33
6dddd6a5
DE
34set python_error_text "Error while executing Python code\\."
35
29703da4
PM
36gdb_py_test_silent_cmd "python sym = gdb.lookup_symbol(\"some_var\")" \
37 "Find a symbol in objfile" 1
38gdb_py_test_silent_cmd "python objfile = sym\[0\].symtab.objfile" \
39 "Get backing object file" 1
40
6dddd6a5 41gdb_test "python print (objfile.filename)" "${testfile}" \
14ea52ee 42 "Get objfile file name"
7c50a931 43
3a8b707a
DE
44gdb_test "python print (objfile.username)" "${testfile}" \
45 "Get objfile user name"
46
1256af7d 47gdb_test "python print (objfile)" \
74c2c1f4 48 "<gdb.Objfile filename=.*${testfile}.*>"
1256af7d 49
0f6ed0e0
TT
50gdb_test_no_output "python dir(objfile)"
51
6dddd6a5 52gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").filename)" \
d60a9221 53 "${testfile}" "print lookup_objfile filename"
6dddd6a5
DE
54gdb_test "python print (gdb.lookup_objfile (\"junk\"))" \
55 "Objfile not found\\.\r\n${python_error_text}"
56
c620ed88
CB
57gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"global_var\").name)" \
58 "global_var" "lookup_global_symbol finds a valid symbol"
59gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"static_var\") is None)" \
60 "True" "lookup_global_symbol does not find static symbol"
61gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"stdout\"))" \
62 "None" "lookup_global_symbol doesn't find symbol in other objfile"
63
64gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"static_var\").name)" \
65 "static_var" "lookup_static_symbol finds a valid symbol"
66gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"global_var\") is None)" \
67 "True" "lookup_static_symbol does not find global symbol"
25ec8924
CB
68gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"nonexistent\"))" \
69 "None" "lookup_static_symbol can handle nonexistent symbol"
c620ed88 70
7c50a931
DE
71set binfile_build_id [get_build_id $binfile]
72if [string compare $binfile_build_id ""] {
73 verbose -log "binfile_build_id = $binfile_build_id"
74 gdb_test "python print (objfile.build_id)" "$binfile_build_id" \
75 "Get objfile build id"
6dddd6a5 76 gdb_test "python print (gdb.lookup_objfile (\"$binfile_build_id\", by_build_id=True).filename)" \
d60a9221 77 "${testfile}" "print lookup_objfile filename by build-id"
7c50a931
DE
78} else {
79 unsupported "build-id is not supported by the compiler"
80}
81
6dddd6a5
DE
82# Other lookup_objfile_by_build_id tests we can do, even if compiler doesn't
83# support them.
84gdb_test "python print (gdb.lookup_objfile (\"foo\", by_build_id=True))" \
d60a9221
SDJ
85 "Not a valid build id\\.\r\n${python_error_text}" \
86 "print invalid file lookup_objfile by build-id"
6dddd6a5 87gdb_test "python print (gdb.lookup_objfile (\"1234abcdef\", by_build_id=True))" \
d60a9221
SDJ
88 "Objfile not found\\.\r\n${python_error_text}" \
89 "print invalid file lookup_objfile by build-id 2"
6dddd6a5 90
d096d8c1
DE
91gdb_test "python print (objfile.progspace)" "<gdb\.Progspace object at .*>" \
92 "Get objfile program space"
9325cb04 93gdb_test "python print (objfile.is_valid())" "True" \
29703da4 94 "Get objfile validity"
f432d5ef
SM
95
96gdb_unload "unload 1"
97
9325cb04 98gdb_test "python print (objfile.is_valid())" "False" \
29703da4 99 "Get objfile validity after unload"
02be9a71
DE
100
101gdb_py_test_silent_cmd "python objfile.random_attribute = 42" \
102 "Set random attribute in objfile" 1
103gdb_test "python print (objfile.random_attribute)" "42" \
104 "Verify set of random attribute in objfile"
86e4ed39 105
3a8b707a
DE
106# Verify invalid objfile handling.
107
f432d5ef
SM
108if { [gdb_unload "unload 2"] != 0 } {
109 return
3a8b707a
DE
110}
111
40d2f8d6 112gdb_test "python print(objfile.filename)" "None" \
3a8b707a 113 "objfile.filename after objfile is unloaded"
40d2f8d6 114gdb_test "python print(objfile.username)" "None" \
3a8b707a
DE
115 "objfile.username after objfile is unloaded"
116
86e4ed39
DE
117# Now build another copy of the testcase, this time without debug info.
118
5b362f04 119if { [prepare_for_testing "failed to prepare" ${testfile}2 ${srcfile} {nodebug ldflags=-Wl,--strip-debug}] } {
86e4ed39
DE
120 return -1
121}
122
123if ![runto_main] {
86e4ed39
DE
124 return 0
125}
126
dac804df 127gdb_py_test_silent_cmd "python objfile = gdb.objfiles()\[0\]" \
86e4ed39
DE
128 "Get no-debug objfile file" 1
129
a0be3e44
DE
130gdb_test "python print (objfile.owner)" "None" \
131 "Test owner of real objfile."
132
86e4ed39
DE
133gdb_test "p main" "= {<text variable, no debug info>} $hex <main>" \
134 "print main without debug info"
135
136gdb_py_test_silent_cmd "python objfile.add_separate_debug_file(\"${binfile}\")" \
137 "Add separate debug file file" 1
138
139gdb_py_test_silent_cmd "python sep_objfile = gdb.objfiles()\[0\]" \
140 "Get separate debug info objfile" 1
141
a0be3e44
DE
142gdb_test "python print (sep_objfile.owner.filename)" "${testfile}2" \
143 "Test owner of separate debug file"
144
3a8b707a
DE
145gdb_test "python print (sep_objfile.owner.username)" "${testfile}2" \
146 "Test user-name of owner of separate debug file"
147
86e4ed39
DE
148gdb_test "p main" "= {int \\(\\)} $hex <main>" \
149 "print main with debug info"
6dddd6a5
DE
150
151# Separate debug files are not findable.
152if { [get_python_valueof "sep_objfile.build_id" "None"] != "None" } {
153 gdb_test "python print (gdb.lookup_objfile (sep_objfile.build_id, by_build_id=True))" \
d60a9221
SDJ
154 "Objfile not found\\.\r\n${python_error_text}" \
155 "print lookup_objfile of separate debug file"
6dddd6a5 156}
e02c96a7
DE
157
158# An objfile that was a symlink to a differently named file is still
159# findable with its original name.
74c2c1f4
SL
160# On Windows we don't have proper symlinks, so skip this.
161if ![ishost *-*-mingw*] {
162 set symlink_binary [standard_output_file "symlink-binary"]
163 remote_exec host "rm -f ${symlink_binary}"
164 remote_exec host "ln -sf ${testfile} ${symlink_binary}"
165 if [remote_file host exists "${symlink_binary}"] {
166 clean_restart "${symlink_binary}"
167 gdb_test "python print (gdb.lookup_objfile (\"${symlink_binary}\").filename)" \
168 "${testfile}" "gdb.lookup_objfile of symlinked binary"
169 }
e02c96a7 170}
1256af7d
SM
171
172# Test printing an Objfile object that is no longer valid.
173gdb_py_test_silent_cmd "python objfile = gdb.objfiles()\[0\]" \
174 "get first objfile" 1
175gdb_file_cmd ${binfile}
74c2c1f4 176gdb_test "python print(objfile)" "<gdb.Objfile \\\(invalid\\\)>"