]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-send-packet.py
0a0b3591711e6c9db9e32c1a9774900e86f1808c
1 # Copyright (C) 2021-2025 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 import xml
.etree
.ElementTree
as ET
21 # Make use of gdb.RemoteTargetConnection.send_packet to fetch the
22 # thread list from the remote target.
24 # Sending existing serial protocol packets like this is not a good
25 # idea, there should be better ways to get this information using an
26 # official API, this is just being used as a test case.
28 # Really, the send_packet API would be used to send target
29 # specific packets to the target, but these are, by definition, target
30 # specific, so hard to test in a general testsuite.
31 def get_thread_list_str():
34 conn
= gdb
.selected_inferior().connection
35 if not isinstance(conn
, gdb
.RemoteTargetConnection
):
36 raise gdb
.GdbError("connection is the wrong type")
38 str = conn
.send_packet("qXfer:threads:read::%d,200" % start_pos
).decode("ascii")
48 # Use gdb.RemoteTargetConnection.send_packet to manually fetch the
49 # thread list, then extract the thread list using the gdb.Inferior and
50 # gdb.InferiorThread API. Compare the two results to ensure we
51 # managed to successfully read the thread list from the remote.
52 def run_send_packet_test():
53 # Find the IDs of all current threads.
55 for inf
in gdb
.inferiors():
56 for thr
in inf
.threads():
57 id = "p%x.%x" % (thr
.ptid
[0], thr
.ptid
[1])
58 all_threads
[id] = False
60 # Now fetch the thread list from the remote, and parse the XML.
61 str = get_thread_list_str()
62 threads_xml
= ET
.fromstring(str)
64 # Look over all threads in the XML list and check we expected to
65 # find them, mark the ones we do find.
66 for thr
in threads_xml
:
68 if not id in all_threads
:
69 raise "found unexpected thread in remote thread list"
71 all_threads
[id] = True
73 # Check that all the threads were found in the XML list.
74 for id in all_threads
:
75 if not all_threads
[id]:
76 raise "thread missingt from remote thread list"
79 print("Send packet test passed")
82 # Convert a bytes object to a string. This follows the same rules as
83 # the 'maint packet' command so that the output from the two sources
85 def bytes_to_string(byte_array
):
89 if b
>= 32 and b
<= 126:
90 res
= res
+ ("%c" % b
)
92 res
= res
+ ("\\x%02x" % b
)
96 # A very simple test for sending the packet that reads the auxv data.
97 # We convert the result to a string and expect to find some
98 # hex-encoded bytes in the output. This test will only work on
99 # targets that actually supply auxv data.
100 def run_auxv_send_packet_test(expected_result
):
101 inf
= gdb
.selected_inferior()
102 conn
= inf
.connection
103 assert isinstance(conn
, gdb
.RemoteTargetConnection
)
104 res
= conn
.send_packet("qXfer:auxv:read::0,1000")
105 assert isinstance(res
, bytes
)
106 string
= bytes_to_string(res
)
107 assert string
.count("\\x") > 0
108 assert string
== expected_result
109 print("auxv send packet test passed")
112 # Check that the value of 'global_var' is EXPECTED_VAL.
113 def check_global_var(expected_val
):
114 val
= int(gdb
.parse_and_eval("global_var"))
115 val
= val
& 0xFFFFFFFF
116 if val
!= expected_val
:
117 raise gdb
.GdbError("global_var is 0x%x, expected 0x%x" % (val
, expected_val
))
120 # Return a bytes object representing an 'X' packet header with
122 def xpacket_header(addr
):
123 return ("X%x,4:" % addr
).encode("ascii")
126 # Set the 'X' packet to the remote target to set a global variable.
127 # Checks that we can send byte values.
128 def run_set_global_var_test():
129 inf
= gdb
.selected_inferior()
130 conn
= inf
.connection
131 assert isinstance(conn
, gdb
.RemoteTargetConnection
)
132 addr
= gdb
.parse_and_eval("&global_var")
133 res
= conn
.send_packet("X%x,4:\x01\x01\x01\x01" % addr
)
134 assert isinstance(res
, bytes
)
135 check_global_var(0x01010101)
136 res
= conn
.send_packet(xpacket_header(addr
) + b
"\x02\x02\x02\x02")
137 assert isinstance(res
, bytes
)
138 check_global_var(0x02020202)
140 # This first attempt will not work as we're passing a Unicode string
141 # containing non-ascii characters.
144 res
= conn
.send_packet("X%x,4:\xff\xff\xff\xff" % addr
)
151 check_global_var(0x02020202)
152 # Now we pass a bytes object, which will work.
153 res
= conn
.send_packet(xpacket_header(addr
) + b
"\xff\xff\xff\xff")
154 check_global_var(0xFFFFFFFF)
156 print("set global_var test passed")
159 # Just to indicate the file was sourced correctly.
160 print("Sourcing complete.")