1 """Test the SBData APIs."""
6 from lldbsuite
.test
.decorators
import *
7 from lldbsuite
.test
.lldbtest
import *
8 from lldbsuite
.test
import lldbutil
11 class SBDataAPICase(TestBase
):
12 NO_DEBUG_INFO_TESTCASE
= True
15 # Call super's setUp().
17 # Find the line number to break on inside main.cpp.
18 self
.line
= line_number("main.cpp", "// set breakpoint here")
20 def test_byte_order_and_address_byte_size(self
):
21 """Test the SBData::SetData() to ensure the byte order and address
22 byte size are obeyed"""
23 addr_data
= b
"\x11\x22\x33\x44\x55\x66\x77\x88"
24 error
= lldb
.SBError()
26 data
.SetData(error
, addr_data
, lldb
.eByteOrderBig
, 4)
27 addr
= data
.GetAddress(error
, 0)
28 self
.assertEqual(addr
, 0x11223344)
29 data
.SetData(error
, addr_data
, lldb
.eByteOrderBig
, 8)
30 addr
= data
.GetAddress(error
, 0)
31 self
.assertEqual(addr
, 0x1122334455667788)
32 data
.SetData(error
, addr_data
, lldb
.eByteOrderLittle
, 4)
33 addr
= data
.GetAddress(error
, 0)
34 self
.assertEqual(addr
, 0x44332211)
35 data
.SetData(error
, addr_data
, lldb
.eByteOrderLittle
, 8)
36 addr
= data
.GetAddress(error
, 0)
37 self
.assertEqual(addr
, 0x8877665544332211)
39 def test_byte_order_and_address_byte_size_with_ownership(self
):
40 """Test the SBData::SetDataWithOwnership() to ensure the byte order
41 and address byte size are obeyed even when source date is released"""
42 addr_data
= b
"\x11\x22\x33\x44\x55\x66\x77\x88"
43 error
= lldb
.SBError()
45 data
.SetDataWithOwnership(error
, addr_data
, lldb
.eByteOrderBig
, 8)
47 addr
= data
.GetAddress(error
, 0)
48 self
.assertEqual(addr
, 0x1122334455667788)
50 def test_with_run_command(self
):
51 """Test the SBData APIs."""
53 self
.runCmd("file " + self
.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET
)
55 lldbutil
.run_break_set_by_file_and_line(
56 self
, "main.cpp", self
.line
, num_expected_locations
=1, loc_exact
=True
59 self
.runCmd("run", RUN_SUCCEEDED
)
61 # The stop reason of the thread should be breakpoint.
64 STOPPED_DUE_TO_BREAKPOINT
,
65 substrs
=["stopped", "stop reason = breakpoint"],
68 target
= self
.dbg
.GetSelectedTarget()
70 process
= target
.GetProcess()
72 thread
= lldbutil
.get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
73 self
.assertIsNotNone(thread
)
75 frame
= thread
.GetSelectedFrame()
76 foobar
= frame
.FindVariable("foobar")
77 self
.assertTrue(foobar
.IsValid())
78 data
= foobar
.GetPointeeData(0, 2)
80 error
= lldb
.SBError()
82 self
.assert_data(data
.GetUnsignedInt32
, offset
, 1)
84 low
= data
.GetSignedInt16(error
, offset
)
85 self
.assertSuccess(error
)
87 high
= data
.GetSignedInt16(error
, offset
)
88 self
.assertSuccess(error
)
91 (low
== 9 and high
== 0) or (low
== 0 and high
== 9), "foo[0].b == 9"
94 fabs(data
.GetFloat(error
, offset
) - 3.14) < 1, "foo[0].c == 3.14"
96 self
.assertSuccess(error
)
98 self
.assert_data(data
.GetUnsignedInt32
, offset
, 8)
100 self
.assert_data(data
.GetUnsignedInt32
, offset
, 5)
107 self
.assert_data(data
.GetUnsignedInt32
, offset
, 5)
109 data
= foobar
.GetPointeeData(1, 1)
113 self
.assert_data(data
.GetSignedInt32
, offset
, 8)
115 self
.assert_data(data
.GetSignedInt32
, offset
, 7)
118 data
.GetUnsignedInt32(error
, offset
), 0, "do not read beyond end"
120 self
.assertTrue(not error
.Success())
121 error
.Clear() # clear the error for the next test
123 star_foobar
= foobar
.Dereference()
124 self
.assertTrue(star_foobar
.IsValid())
126 data
= star_foobar
.GetData()
129 self
.assert_data(data
.GetUnsignedInt32
, offset
, 1)
131 self
.assert_data(data
.GetUnsignedInt32
, offset
, 9)
133 foobar_addr
= star_foobar
.GetLoadAddress()
136 # http://llvm.org/bugs/show_bug.cgi?id=11579
137 # lldb::SBValue::CreateValueFromAddress does not verify SBType::GetPointerType succeeds
138 # This should not crash LLDB.
139 nothing
= foobar
.CreateValueFromAddress(
142 star_foobar
.GetType().GetBasicType(lldb
.eBasicTypeInvalid
),
145 new_foobar
= foobar
.CreateValueFromAddress(
146 "f00", foobar_addr
, star_foobar
.GetType()
148 self
.assertTrue(new_foobar
.IsValid())
149 data
= new_foobar
.GetData()
151 self
.assertEqual(data
.uint32
[0], 8, "then foo[1].a == 8")
152 self
.assertEqual(data
.uint32
[1], 7, "then foo[1].b == 7")
153 # exploiting that sizeof(uint32) == sizeof(float)
154 self
.assertTrue(fabs(data
.float[2] - 3.14) < 1, "foo[1].c == 3.14")
159 self
.assert_data(data
.GetUnsignedInt32
, offset
, 8)
161 self
.assert_data(data
.GetUnsignedInt32
, offset
, 7)
164 fabs(data
.GetFloat(error
, offset
) - 3.14) < 1, "foo[1].c == 3.14"
166 self
.assertSuccess(error
)
168 data
= new_foobar
.GetData()
171 self
.assert_data(data
.GetUnsignedInt32
, offset
, 8)
173 self
.assert_data(data
.GetUnsignedInt32
, offset
, 7)
176 fabs(data
.GetFloat(error
, offset
) - 6.28) < 1, "foo[1].c == 6.28"
178 self
.assertSuccess(error
)
182 barfoo
= frame
.FindVariable("barfoo")
184 data
= barfoo
.GetData()
186 self
.assert_data(data
.GetUnsignedInt32
, offset
, 1)
188 self
.assert_data(data
.GetUnsignedInt32
, offset
, 2)
190 self
.assertTrue(fabs(data
.GetFloat(error
, offset
) - 3) < 1, "barfoo[0].c == 3")
191 self
.assertSuccess(error
)
193 self
.assert_data(data
.GetUnsignedInt32
, offset
, 4)
195 self
.assert_data(data
.GetUnsignedInt32
, offset
, 5)
197 self
.assertTrue(fabs(data
.GetFloat(error
, offset
) - 6) < 1, "barfoo[1].c == 6")
198 self
.assertSuccess(error
)
200 new_object
= barfoo
.CreateValueFromData(
201 "new_object", data
, barfoo
.GetType().GetBasicType(lldb
.eBasicTypeInt
)
203 self
.assertEqual(new_object
.GetValue(), "1", "new_object == 1")
205 if data
.GetByteOrder() == lldb
.eByteOrderBig
:
207 error
, "\0\0\0A", data
.GetByteOrder(), data
.GetAddressByteSize()
211 error
, "A\0\0\0", data
.GetByteOrder(), data
.GetAddressByteSize()
213 self
.assertSuccess(error
)
215 data2
= lldb
.SBData()
216 data2
.SetData(error
, "BCD", data
.GetByteOrder(), data
.GetAddressByteSize())
217 self
.assertSuccess(error
)
221 # this breaks on EBCDIC
223 self
.assert_data(data
.GetUnsignedInt32
, offset
, 65)
225 self
.assert_data(data
.GetUnsignedInt8
, offset
, 66)
227 self
.assert_data(data
.GetUnsignedInt8
, offset
, 67)
229 self
.assert_data(data
.GetUnsignedInt8
, offset
, 68)
232 # check the new API calls introduced per LLVM llvm.org/prenhancement request
233 # 11619 (Allow creating SBData values from arrays or primitives in
237 data2
= lldb
.SBData
.CreateDataFromCString(
238 process
.GetByteOrder(), process
.GetAddressByteSize(), hello_str
240 self
.assertEqual(len(data2
.uint8
), len(hello_str
))
241 self
.assertEqual(data2
.uint8
[0], 104, "h == 104")
242 self
.assertEqual(data2
.uint8
[1], 101, "e == 101")
243 self
.assertEqual(data2
.uint8
[2], 108, "l == 108")
244 self
.assert_data(data2
.GetUnsignedInt8
, 3, 108) # l
245 self
.assertEqual(data2
.uint8
[4], 111, "o == 111")
246 self
.assert_data(data2
.GetUnsignedInt8
, 5, 33) # !
248 uint_lists
= [[1, 2, 3, 4, 5], [int(i
) for i
in [1, 2, 3, 4, 5]]]
249 int_lists
= [[2, -2], [int(i
) for i
in [2, -2]]]
252 data2
= lldb
.SBData
.CreateDataFromUInt64Array(
253 process
.GetByteOrder(), process
.GetAddressByteSize(), l
255 self
.assert_data(data2
.GetUnsignedInt64
, 0, 1)
256 self
.assert_data(data2
.GetUnsignedInt64
, 8, 2)
257 self
.assert_data(data2
.GetUnsignedInt64
, 16, 3)
258 self
.assert_data(data2
.GetUnsignedInt64
, 24, 4)
259 self
.assert_data(data2
.GetUnsignedInt64
, 32, 5)
264 "read_data_helper failure: data2 == [1,2,3,4,5]",
268 data2
= lldb
.SBData
.CreateDataFromSInt32Array(
269 process
.GetByteOrder(), process
.GetAddressByteSize(), l
271 self
.assertEqual(data2
.sint32
[0:2], [2, -2], "signed32 data2 = [2,-2]")
274 lldb
.SBData
.CreateDataFromSInt64Array(
275 process
.GetByteOrder(), process
.GetAddressByteSize(), int_lists
[0]
278 self
.assert_data(data2
.GetSignedInt32
, 0, 2)
279 self
.assert_data(data2
.GetSignedInt32
, 4, -2)
280 self
.assertEqual(data2
.sint64
[1:3], [2, -2], "signed64 data2 = [2,-2]")
283 data2
= lldb
.SBData
.CreateDataFromSInt64Array(
284 process
.GetByteOrder(), process
.GetAddressByteSize(), l
286 self
.assert_data(data2
.GetSignedInt64
, 0, 2)
287 self
.assert_data(data2
.GetSignedInt64
, 8, -2)
288 self
.assertEqual(data2
.sint64
[0:2], [2, -2], "signed64 data2 = [2,-2]")
291 data2
= lldb
.SBData
.CreateDataFromUInt32Array(
292 process
.GetByteOrder(), process
.GetAddressByteSize(), l
294 self
.assert_data(data2
.GetUnsignedInt32
, 0, 1)
295 self
.assert_data(data2
.GetUnsignedInt32
, 4, 2)
296 self
.assert_data(data2
.GetUnsignedInt32
, 8, 3)
297 self
.assert_data(data2
.GetUnsignedInt32
, 12, 4)
298 self
.assert_data(data2
.GetUnsignedInt32
, 16, 5)
300 bool_list
= [True, True, False, False, True, False]
302 data2
= lldb
.SBData
.CreateDataFromSInt32Array(
303 process
.GetByteOrder(), process
.GetAddressByteSize(), bool_list
306 data2
.sint32
[0:6], [1, 1, 0, 0, 1, 0], "signed32 data2 = [1, 1, 0, 0, 1, 0]"
309 data2
= lldb
.SBData
.CreateDataFromUInt32Array(
310 process
.GetByteOrder(), process
.GetAddressByteSize(), bool_list
315 "unsigned32 data2 = [1, 1, 0, 0, 1, 0]",
318 data2
= lldb
.SBData
.CreateDataFromSInt64Array(
319 process
.GetByteOrder(), process
.GetAddressByteSize(), bool_list
322 data2
.sint64
[0:6], [1, 1, 0, 0, 1, 0], "signed64 data2 = [1, 1, 0, 0, 1, 0]"
325 data2
= lldb
.SBData
.CreateDataFromUInt64Array(
326 process
.GetByteOrder(), process
.GetAddressByteSize(), bool_list
329 data2
.uint64
[0:6], [1, 1, 0, 0, 1, 0], "signed64 data2 = [1, 1, 0, 0, 1, 0]"
332 data2
= lldb
.SBData
.CreateDataFromDoubleArray(
333 process
.GetByteOrder(), process
.GetAddressByteSize(), [3.14, 6.28, 2.71]
336 fabs(data2
.GetDouble(error
, 0) - 3.14) < 0.5, "double data2[0] = 3.14"
338 self
.assertSuccess(error
)
340 fabs(data2
.GetDouble(error
, 8) - 6.28) < 0.5, "double data2[1] = 6.28"
342 self
.assertSuccess(error
)
344 fabs(data2
.GetDouble(error
, 16) - 2.71) < 0.5, "double data2[2] = 2.71"
346 self
.assertSuccess(error
)
348 data2
= lldb
.SBData()
350 data2
.SetDataFromCString(hello_str
)
351 self
.assertEqual(len(data2
.uint8
), len(hello_str
))
352 self
.assert_data(data2
.GetUnsignedInt8
, 0, 104)
353 self
.assert_data(data2
.GetUnsignedInt8
, 1, 101)
354 self
.assert_data(data2
.GetUnsignedInt8
, 2, 108)
355 self
.assert_data(data2
.GetUnsignedInt8
, 3, 108)
356 self
.assert_data(data2
.GetUnsignedInt8
, 4, 111)
357 self
.assert_data(data2
.GetUnsignedInt8
, 5, 33)
359 data2
.SetDataFromUInt64Array([1, 2, 3, 4, 5, 2**63])
360 self
.assert_data(data2
.GetUnsignedInt64
, 0, 1)
361 self
.assert_data(data2
.GetUnsignedInt64
, 8, 2)
362 self
.assert_data(data2
.GetUnsignedInt64
, 16, 3)
363 self
.assert_data(data2
.GetUnsignedInt64
, 24, 4)
364 self
.assert_data(data2
.GetUnsignedInt64
, 32, 5)
365 self
.assert_data(data2
.GetUnsignedInt64
, 40, 2**63)
368 data2
.uint64
[0], 1, "read_data_helper failure: set data2[0] = 1"
371 data2
.uint64
[1], 2, "read_data_helper failure: set data2[1] = 2"
374 data2
.uint64
[2], 3, "read_data_helper failure: set data2[2] = 3"
377 data2
.uint64
[3], 4, "read_data_helper failure: set data2[3] = 4"
380 data2
.uint64
[4], 5, "read_data_helper failure: set data2[4] = 5"
384 data2
.uint64
[0:2] == [1, 2],
385 "read_data_helper failure: set data2[0:2] = [1,2]",
388 data2
.SetDataFromSInt32Array([2, -2])
389 self
.assert_data(data2
.GetSignedInt32
, 0, 2)
390 self
.assert_data(data2
.GetSignedInt32
, 4, -2)
392 data2
.SetDataFromSInt64Array([2, -2])
393 self
.assert_data(data2
.GetSignedInt64
, 0, 2)
394 self
.assert_data(data2
.GetSignedInt64
, 8, -2)
396 data2
.SetDataFromUInt32Array([1, 2, 3, 4, 5])
397 self
.assert_data(data2
.GetUnsignedInt32
, 0, 1)
398 self
.assert_data(data2
.GetUnsignedInt32
, 4, 2)
399 self
.assert_data(data2
.GetUnsignedInt32
, 8, 3)
400 self
.assert_data(data2
.GetUnsignedInt32
, 12, 4)
401 self
.assert_data(data2
.GetUnsignedInt32
, 16, 5)
404 data2
.uint32
[0], 1, "read_data_helper failure: set 32-bit data2[0] = 1"
407 data2
.uint32
[1], 2, "read_data_helper failure: set 32-bit data2[1] = 2"
410 data2
.uint32
[2], 3, "read_data_helper failure: set 32-bit data2[2] = 3"
413 data2
.uint32
[3], 4, "read_data_helper failure: set 32-bit data2[3] = 4"
416 data2
.uint32
[4], 5, "read_data_helper failure: set 32-bit data2[4] = 5"
419 data2
.SetDataFromDoubleArray([3.14, 6.28, 2.71])
421 fabs(data2
.GetDouble(error
, 0) - 3.14) < 0.5, "set double data2[0] = 3.14"
424 fabs(data2
.GetDouble(error
, 8) - 6.28) < 0.5, "set double data2[1] = 6.28"
427 fabs(data2
.GetDouble(error
, 16) - 2.71) < 0.5, "set double data2[2] = 2.71"
431 fabs(data2
.double
[0] - 3.14) < 0.5,
432 "read_data_helper failure: set double data2[0] = 3.14",
435 fabs(data2
.double
[1] - 6.28) < 0.5,
436 "read_data_helper failure: set double data2[1] = 6.28",
439 fabs(data2
.double
[2] - 2.71) < 0.5,
440 "read_data_helper failure: set double data2[2] = 2.71",
443 def assert_data(self
, func
, arg
, expected
):
444 """Asserts func(SBError error, arg) == expected."""
445 error
= lldb
.SBError()
446 result
= func(error
, arg
)
447 if not error
.Success():
448 stream
= lldb
.SBStream()
449 error
.GetDescription(stream
)
452 "%s(error, %s) did not succeed: %s"
453 % (func
.__name
__, arg
, stream
.GetData()),
457 "%s(error, %s) == %s != %s" % (func
.__name
__, arg
, result
, expected
),