]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-objfile.exp
61b9942de79bbde3682e9f790be0a284cd1ca13a
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-objfile.exp
1 # Copyright (C) 2011-2023 Free Software Foundation, Inc.
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
19 load_lib gdb-python.exp
20
21 require allow_python_tests
22
23 standard_testfile
24
25 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
26 return -1
27 }
28
29 if {![runto_main]} {
30 return 0
31 }
32
33 set python_error_text "Error while executing Python code\\."
34
35 gdb_py_test_silent_cmd "python sym = gdb.lookup_symbol(\"some_var\")" \
36 "Find a symbol in objfile" 1
37 gdb_py_test_silent_cmd "python objfile = sym\[0\].symtab.objfile" \
38 "Get backing object file" 1
39
40 gdb_test "python print (objfile.filename)" "${testfile}" \
41 "Get objfile file name"
42
43 gdb_test "python print (objfile.username)" "${testfile}" \
44 "Get objfile user name"
45
46 gdb_test "python print (objfile)" \
47 "<gdb.Objfile filename=.*${testfile}.*>"
48
49 gdb_test_no_output "python dir(objfile)"
50
51 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").filename)" \
52 "${testfile}" "print lookup_objfile filename"
53 gdb_test "python print (gdb.lookup_objfile (\"junk\"))" \
54 "Objfile not found\\.\r\n${python_error_text}"
55
56 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"global_var\").name)" \
57 "global_var" "lookup_global_symbol finds a valid symbol"
58 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"static_var\") is None)" \
59 "True" "lookup_global_symbol does not find static symbol"
60 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"stdout\"))" \
61 "None" "lookup_global_symbol doesn't find symbol in other objfile"
62
63 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"static_var\").name)" \
64 "static_var" "lookup_static_symbol finds a valid symbol"
65 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"global_var\") is None)" \
66 "True" "lookup_static_symbol does not find global symbol"
67 gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"nonexistent\"))" \
68 "None" "lookup_static_symbol can handle nonexistent symbol"
69
70 set binfile_build_id [get_build_id $binfile]
71 if [string compare $binfile_build_id ""] {
72 verbose -log "binfile_build_id = $binfile_build_id"
73 gdb_test "python print (objfile.build_id)" "$binfile_build_id" \
74 "Get objfile build id"
75 gdb_test "python print (gdb.lookup_objfile (\"$binfile_build_id\", by_build_id=True).filename)" \
76 "${testfile}" "print lookup_objfile filename by build-id"
77 } else {
78 unsupported "build-id is not supported by the compiler"
79 }
80
81 # Other lookup_objfile_by_build_id tests we can do, even if compiler doesn't
82 # support them.
83 gdb_test "python print (gdb.lookup_objfile (\"foo\", by_build_id=True))" \
84 "Not a valid build id\\.\r\n${python_error_text}" \
85 "print invalid file lookup_objfile by build-id"
86 gdb_test "python print (gdb.lookup_objfile (\"1234abcdef\", by_build_id=True))" \
87 "Objfile not found\\.\r\n${python_error_text}" \
88 "print invalid file lookup_objfile by build-id 2"
89
90 gdb_test "python print (objfile.progspace)" "<gdb\.Progspace object at .*>" \
91 "Get objfile program space"
92 gdb_test "python print (objfile.is_valid())" "True" \
93 "Get objfile validity"
94
95 gdb_unload "unload 1"
96
97 gdb_test "python print (objfile.is_valid())" "False" \
98 "Get objfile validity after unload"
99
100 gdb_py_test_silent_cmd "python objfile.random_attribute = 42" \
101 "Set random attribute in objfile" 1
102 gdb_test "python print (objfile.random_attribute)" "42" \
103 "Verify set of random attribute in objfile"
104
105 # Verify invalid objfile handling.
106
107 if { [gdb_unload "unload 2"] != 0 } {
108 return
109 }
110
111 gdb_test "python print(objfile.filename)" "None" \
112 "objfile.filename after objfile is unloaded"
113 gdb_test "python print(objfile.username)" "None" \
114 "objfile.username after objfile is unloaded"
115
116 # Now build another copy of the testcase, this time without debug info.
117
118 if { [prepare_for_testing "failed to prepare" ${testfile}2 ${srcfile} {nodebug ldflags=-Wl,--strip-debug}] } {
119 return -1
120 }
121
122 if ![runto_main] {
123 return 0
124 }
125
126 gdb_py_test_silent_cmd "python objfile = gdb.objfiles()\[0\]" \
127 "Get no-debug objfile file" 1
128
129 gdb_test "python print (objfile.owner)" "None" \
130 "Test owner of real objfile."
131
132 gdb_test "p main" "= {<text variable, no debug info>} $hex <main>" \
133 "print main without debug info"
134
135 gdb_py_test_silent_cmd "python objfile.add_separate_debug_file(\"${binfile}\")" \
136 "Add separate debug file file" 1
137
138 gdb_py_test_silent_cmd "python sep_objfile = gdb.objfiles()\[0\]" \
139 "Get separate debug info objfile" 1
140
141 gdb_test "python print (sep_objfile.owner.filename)" "${testfile}2" \
142 "Test owner of separate debug file"
143
144 gdb_test "python print (sep_objfile.owner.username)" "${testfile}2" \
145 "Test user-name of owner of separate debug file"
146
147 gdb_test "p main" "= {int \\(\\)} $hex <main>" \
148 "print main with debug info"
149
150 # Separate debug files are not findable.
151 if { [get_python_valueof "sep_objfile.build_id" "None"] != "None" } {
152 gdb_test "python print (gdb.lookup_objfile (sep_objfile.build_id, by_build_id=True))" \
153 "Objfile not found\\.\r\n${python_error_text}" \
154 "print lookup_objfile of separate debug file"
155 }
156
157 # An objfile that was a symlink to a differently named file is still
158 # findable with its original name.
159 # On Windows we don't have proper symlinks, so skip this.
160 if ![ishost *-*-mingw*] {
161 set symlink_binary [standard_output_file "symlink-binary"]
162 remote_exec host "rm -f ${symlink_binary}"
163 remote_exec host "ln -sf ${testfile} ${symlink_binary}"
164 if [remote_file host exists "${symlink_binary}"] {
165 clean_restart "${symlink_binary}"
166 gdb_test "python print (gdb.lookup_objfile (\"${symlink_binary}\").filename)" \
167 "${testfile}" "gdb.lookup_objfile of symlinked binary"
168 }
169 }
170
171 # Test printing an Objfile object that is no longer valid.
172 gdb_py_test_silent_cmd "python objfile = gdb.objfiles()\[0\]" \
173 "get first objfile" 1
174 gdb_file_cmd ${binfile}
175 gdb_test "python print(objfile)" "<gdb.Objfile \\\(invalid\\\)>"