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