]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/testsuite/gdb.python/py-send-packet.py
gas pending_bundle_size assert
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-send-packet.py
... / ...
CommitLineData
1# Copyright (C) 2021-2025 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
16import xml.etree.ElementTree as ET
17
18import gdb
19
20
21# Make use of gdb.RemoteTargetConnection.send_packet to fetch the
22# thread list from the remote target.
23#
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.
27#
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.
31def get_thread_list_str():
32 start_pos = 0
33 thread_desc = ""
34 conn = gdb.selected_inferior().connection
35 if not isinstance(conn, gdb.RemoteTargetConnection):
36 raise gdb.GdbError("connection is the wrong type")
37 while True:
38 str = conn.send_packet("qXfer:threads:read::%d,200" % start_pos).decode("ascii")
39 start_pos += 200
40 c = str[0]
41 str = str[1:]
42 thread_desc += str
43 if c == "l":
44 break
45 return thread_desc
46
47
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.
52def run_send_packet_test():
53 # Find the IDs of all current threads.
54 all_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
59
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)
63
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:
67 id = thr.get("id")
68 if not id in all_threads:
69 raise "found unexpected thread in remote thread list"
70 else:
71 all_threads[id] = True
72
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"
77
78 # Test complete.
79 print("Send packet test passed")
80
81
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
84# can be compared.
85def bytes_to_string(byte_array):
86 res = ""
87 for b in byte_array:
88 b = int(b)
89 if b >= 32 and b <= 126:
90 res = res + ("%c" % b)
91 else:
92 res = res + ("\\x%02x" % b)
93 return res
94
95
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.
100def 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")
110
111
112# Check that the value of 'global_var' is EXPECTED_VAL.
113def 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))
118
119
120# Return a bytes object representing an 'X' packet header with
121# address ADDR.
122def xpacket_header(addr):
123 return ("X%x,4:" % addr).encode("ascii")
124
125
126# Set the 'X' packet to the remote target to set a global variable.
127# Checks that we can send byte values.
128def 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)
139
140 # This first attempt will not work as we're passing a Unicode string
141 # containing non-ascii characters.
142 saw_error = False
143 try:
144 res = conn.send_packet("X%x,4:\xff\xff\xff\xff" % addr)
145 except UnicodeError:
146 saw_error = True
147 except:
148 assert False
149
150 assert saw_error
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)
155
156 print("set global_var test passed")
157
158
159# Just to indicate the file was sourced correctly.
160print("Sourcing complete.")