예제 #1
0
def to_sbvalue(value, target):
    value = Value.unwrap(value)

    if isinstance(value, lldb.SBValue):
        return value
    elif value is None:
        ty = target.GetBasicType(lldb.eBasicTypeVoid)
        return target.CreateValueFromData('result', lldb.SBData(), ty)
    elif isinstance(value, bool):
        value = c_int(value)
        asbytes = memoryview(value).tobytes() # type: ignore
        data = lldb.SBData()
        data.SetData(dummy_sberror, asbytes, target.GetByteOrder(), target.GetAddressByteSize()) # borrows from asbytes
        ty = target.GetBasicType(lldb.eBasicTypeBool)
        return target.CreateValueFromData('result', data, ty)
    elif isinstance(value, int):
        value = c_int64(value)
        asbytes = memoryview(value).tobytes() # type: ignore
        data = lldb.SBData()
        data.SetData(dummy_sberror, asbytes, target.GetByteOrder(), target.GetAddressByteSize()) # borrows from asbytes
        ty = target.GetBasicType(lldb.eBasicTypeLongLong)
        return target.CreateValueFromData('result', data, ty)
    elif isinstance(value, float):
        value = c_double(value)
        asbytes = memoryview(value).tobytes() # type: ignore
        data = lldb.SBData()
        data.SetData(dummy_sberror, asbytes, target.GetByteOrder(), target.GetAddressByteSize()) # borrows from asbytes
        ty = target.GetBasicType(lldb.eBasicTypeDouble)
        return target.CreateValueFromData('result', data, ty)
    else: # Fall back to string representation
        value = str(value)
        data = lldb.SBData.CreateDataFromCString(target.GetByteOrder(), target.GetAddressByteSize(), value)
        sbtype_arr = target.GetBasicType(lldb.eBasicTypeChar).GetArrayType(len(value))
        return target.CreateValueFromData('result', data, sbtype_arr)
예제 #2
0
def to_sbvalue(value, target):
    if isinstance(value, lldb.SBValue):
        return value
    elif isinstance(value, bool):
        value = c_int(value)
        asbytes = memoryview(value).tobytes()
        data = lldb.SBData()
        data.SetData(sberror, asbytes, target.GetByteOrder(),
                     target.GetAddressByteSize())  # borrows from asbytes
        ty = target.GetBasicType(lldb.eBasicTypeBool)
        return target.CreateValueFromData('result', data, ty)
    elif isinstance(value, int):
        value = c_int64(value)
        asbytes = memoryview(value).tobytes()
        data = lldb.SBData()
        data.SetData(sberror, asbytes, target.GetByteOrder(),
                     target.GetAddressByteSize())  # borrows from asbytes
        ty = target.GetBasicType(lldb.eBasicTypeLongLong)
        return target.CreateValueFromData('result', data, ty)
    else:
        value = str(value)
        data = lldb.SBData.CreateDataFromCString(target.GetByteOrder(),
                                                 target.GetAddressByteSize(),
                                                 value)
        sbtype_arr = target.GetBasicType(lldb.eBasicTypeChar).GetArrayType(
            len(value))
        return target.CreateValueFromData('result', data, sbtype_arr)
예제 #3
0
def fuzz_obj(obj):
    obj.GetError()
    obj.GetID()
    obj.GetName()
    obj.GetTypeName()
    obj.GetByteSize()
    obj.IsInScope()
    obj.GetFormat()
    obj.SetFormat(lldb.eFormatBoolean)
    obj.GetValue()
    obj.GetValueType()
    obj.GetValueDidChange()
    obj.GetSummary()
    obj.GetObjectDescription()
    obj.GetLocation()
    obj.SetValueFromCString("my_new_value")
    obj.GetChildAtIndex(1)
    obj.GetChildAtIndex(2, lldb.eNoDynamicValues, False)
    obj.GetIndexOfChildWithName("my_first_child")
    obj.GetChildMemberWithName("my_first_child")
    obj.GetChildMemberWithName("my_first_child", lldb.eNoDynamicValues)
    obj.GetNumChildren()
    obj.GetOpaqueType()
    obj.Dereference()
    obj.TypeIsPointerType()
    stream = lldb.SBStream()
    obj.GetDescription(stream)
    obj.GetExpressionPath(stream)
    obj.GetExpressionPath(stream, True)
    error = lldb.SBError()
    obj.Watch(True, True, False, error)
    obj.WatchPointee(True, False, True, error)
    for child_val in obj:
        s = str(child_val)
    error = lldb.SBError()
    obj.GetValueAsSigned(error, 0)
    obj.GetValueAsUnsigned(error, 0)
    obj.GetValueAsSigned(0)
    obj.GetValueAsUnsigned(0)
    obj.GetDynamicValue(lldb.eNoDynamicValues)
    obj.GetStaticValue()
    obj.IsDynamic()
    invalid_type = lldb.SBType()
    obj.CreateChildAtOffset("a", 12, invalid_type)
    obj.Cast(invalid_type)
    obj.CreateValueFromExpression("pt->x", "pt->x")
    obj.CreateValueFromAddress("x", 0x123, invalid_type)
    invalid_data = lldb.SBData()
    obj.CreateValueFromData("x", invalid_data, invalid_type)
    obj.GetValueForExpressionPath("[0]")
    obj.AddressOf()
    obj.GetLoadAddress()
    obj.GetAddress()
    obj.GetPointeeData(0, 1)
    obj.GetData()
    obj.GetTarget()
    obj.GetProcess()
    obj.GetThread()
    obj.GetFrame()
    obj.GetType()
예제 #4
0
 def test_byte_order_and_address_byte_size_with_ownership(self):
     """Test the SBData::SetDataWithOwnership() to ensure the byte order
     and address byte size are obeyed even when source date is released"""
     addr_data = b'\x11\x22\x33\x44\x55\x66\x77\x88'
     error = lldb.SBError()
     data = lldb.SBData()
     data.SetDataWithOwnership(error, addr_data, lldb.eByteOrderBig, 8)
     del addr_data
     addr = data.GetAddress(error, 0)
     self.assertEqual(addr, 0x1122334455667788)
    def read_memory_at_address(self, addr: int, size: int) -> lldb.SBData:
        data = lldb.SBData()
        error = lldb.SBError()
        bytes_read = self.corefile_process.ReadMemory(addr, size, error)

        if error.Fail():
            return data

        data.SetData(error, bytes_read, self.corefile_target.GetByteOrder(),
                     self.corefile_target.GetAddressByteSize())

        return data
예제 #6
0
 def get_child_at_index(self, index):
     assert hasattr(self._pp, 'children')
     if self._get_display_hint() == 'map':
         self._get_children(2 * (index + 1))
         if index < (len(self._children) / 2):
             key = self._children[index * 2][1]
             val = self._children[index * 2 + 1][1]
             key_str = key.sbvalue().GetSummary()
             if not key_str:
                 key_str = key.sbvalue().GetValue()
             if not key_str:
                 key_str = str(key)
             if isinstance(val, gdb.Value):
                 return val.sbvalue().CreateChildAtOffset(
                     '[%s]' % key_str, 0,
                     val.sbvalue().GetType())
             else:
                 data = lldb.SBData()
                 data.SetDataFromUInt64Array([int(val)])
                 return self._sbvalue.CreateValueFromData(
                     '[%s]' % key_str, data,
                     lldb.debugger.GetSelectedTarget().FindFirstType('int'))
     else:
         self._get_children(index + 1)
         if index < len(self._children):
             c = self._children[index]
             if not isinstance(c[1], gdb.Value):
                 data = lldb.SBData()
                 data.SetDataFromUInt64Array([int(c[1])])
                 return self._sbvalue.CreateValueFromData(
                     c[0], data,
                     lldb.debugger.GetSelectedTarget().FindFirstType('int'))
             else:
                 return c[1].sbvalue().CreateChildAtOffset(
                     c[0], 0, c[1].sbvalue().GetType())
             return sbvalue
     raise IndexError('Child not present at given index.')
예제 #7
0
 def test_byte_order_and_address_byte_size(self):
     """Test the SBData::SetData() to ensure the byte order and address
     byte size are obeyed"""
     addr_data = b'\x11\x22\x33\x44\x55\x66\x77\x88'
     error = lldb.SBError()
     data = lldb.SBData()
     data.SetData(error, addr_data, lldb.eByteOrderBig, 4)
     addr = data.GetAddress(error, 0)
     self.assertEqual(addr, 0x11223344)
     data.SetData(error, addr_data, lldb.eByteOrderBig, 8)
     addr = data.GetAddress(error, 0)
     self.assertEqual(addr, 0x1122334455667788)
     data.SetData(error, addr_data, lldb.eByteOrderLittle, 4)
     addr = data.GetAddress(error, 0)
     self.assertEqual(addr, 0x44332211)
     data.SetData(error, addr_data, lldb.eByteOrderLittle, 8)
     addr = data.GetAddress(error, 0)
     self.assertEqual(addr, 0x8877665544332211)
예제 #8
0
    def read_memory_at_address(self, addr: int, size: int) -> lldb.SBData:
        data = lldb.SBData()

        with open(self.stack_memory_dump, 'rb') as f:
            stack_mem = f.read(-1)
            if not stack_mem:
                return data

            mem_region = self.get_memory_region_containing_address(addr)

            if not mem_region or addr + size > mem_region.GetRegionEnd():
                return data

            offset = addr - mem_region.GetRegionBase()
            shrunk_stack_mem = stack_mem[offset:offset + size]

            error = lldb.SBError()
            data.SetData(error, shrunk_stack_mem, self.target.GetByteOrder(),
                         self.target.GetAddressByteSize())
        return data
예제 #9
0
 def read_memory_at_address(self, addr: int, size: int) -> lldb.SBData:
     # NOTE: CrashLogs don't contain any memory.
     return lldb.SBData()
예제 #10
0
    def test_with_run_command(self):
        """Test the SBData APIs."""
        self.build()
        self.runCmd("file " + self.getBuildArtifact("a.out"),
                    CURRENT_EXECUTABLE_SET)

        lldbutil.run_break_set_by_file_and_line(self,
                                                "main.cpp",
                                                self.line,
                                                num_expected_locations=1,
                                                loc_exact=True)

        self.runCmd("run", RUN_SUCCEEDED)

        # The stop reason of the thread should be breakpoint.
        self.expect("thread list",
                    STOPPED_DUE_TO_BREAKPOINT,
                    substrs=['stopped', 'stop reason = breakpoint'])

        target = self.dbg.GetSelectedTarget()

        process = target.GetProcess()

        thread = lldbutil.get_stopped_thread(process,
                                             lldb.eStopReasonBreakpoint)
        self.assertIsNotNone(thread)

        frame = thread.GetSelectedFrame()
        foobar = frame.FindVariable('foobar')
        self.assertTrue(foobar.IsValid())
        data = foobar.GetPointeeData(0, 2)
        offset = 0
        error = lldb.SBError()

        self.assert_data(data.GetUnsignedInt32, offset, 1)
        offset += 4
        low = data.GetSignedInt16(error, offset)
        self.assertTrue(error.Success())
        offset += 2
        high = data.GetSignedInt16(error, offset)
        self.assertTrue(error.Success())
        offset += 2
        self.assertTrue((low == 9 and high == 0) or (low == 0 and high == 9),
                        'foo[0].b == 9')
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[0].c == 3.14')
        self.assertTrue(error.Success())
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 8)
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 5)
        offset += 4

        self.runCmd("n")

        offset = 16

        self.assert_data(data.GetUnsignedInt32, offset, 5)

        data = foobar.GetPointeeData(1, 1)

        offset = 0

        self.assert_data(data.GetSignedInt32, offset, 8)
        offset += 4
        self.assert_data(data.GetSignedInt32, offset, 7)
        offset += 8
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 0,
            'do not read beyond end')
        self.assertTrue(not error.Success())
        error.Clear()  # clear the error for the next test

        star_foobar = foobar.Dereference()
        self.assertTrue(star_foobar.IsValid())

        data = star_foobar.GetData()

        offset = 0
        self.assert_data(data.GetUnsignedInt32, offset, 1)
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 9)

        foobar_addr = star_foobar.GetLoadAddress()
        foobar_addr += 12

        # http://llvm.org/bugs/show_bug.cgi?id=11579
        # lldb::SBValue::CreateValueFromAddress does not verify SBType::GetPointerType succeeds
        # This should not crash LLDB.
        nothing = foobar.CreateValueFromAddress(
            "nothing", foobar_addr,
            star_foobar.GetType().GetBasicType(lldb.eBasicTypeInvalid))

        new_foobar = foobar.CreateValueFromAddress("f00", foobar_addr,
                                                   star_foobar.GetType())
        self.assertTrue(new_foobar.IsValid())
        data = new_foobar.GetData()

        self.assertEqual(data.uint32[0], 8, 'then foo[1].a == 8')
        self.assertEqual(data.uint32[1], 7, 'then foo[1].b == 7')
        # exploiting that sizeof(uint32) == sizeof(float)
        self.assertTrue(fabs(data.float[2] - 3.14) < 1, 'foo[1].c == 3.14')

        self.runCmd("n")

        offset = 0
        self.assert_data(data.GetUnsignedInt32, offset, 8)
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 7)
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[1].c == 3.14')
        self.assertTrue(error.Success())

        data = new_foobar.GetData()

        offset = 0
        self.assert_data(data.GetUnsignedInt32, offset, 8)
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 7)
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 6.28) < 1, 'foo[1].c == 6.28')
        self.assertTrue(error.Success())

        self.runCmd("n")

        barfoo = frame.FindVariable('barfoo')

        data = barfoo.GetData()
        offset = 0
        self.assert_data(data.GetUnsignedInt32, offset, 1)
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 2)
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 3) < 1, 'barfoo[0].c == 3')
        self.assertTrue(error.Success())
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 4)
        offset += 4
        self.assert_data(data.GetUnsignedInt32, offset, 5)
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 6) < 1, 'barfoo[1].c == 6')
        self.assertTrue(error.Success())

        new_object = barfoo.CreateValueFromData(
            "new_object", data,
            barfoo.GetType().GetBasicType(lldb.eBasicTypeInt))
        self.assertEqual(new_object.GetValue(), "1", 'new_object == 1')

        if data.GetByteOrder() == lldb.eByteOrderBig:
            data.SetData(error, '\0\0\0A', data.GetByteOrder(),
                         data.GetAddressByteSize())
        else:
            data.SetData(error, 'A\0\0\0', data.GetByteOrder(),
                         data.GetAddressByteSize())
        self.assertTrue(error.Success())

        data2 = lldb.SBData()
        data2.SetData(error, 'BCD', data.GetByteOrder(),
                      data.GetAddressByteSize())
        self.assertTrue(error.Success())

        data.Append(data2)

        # this breaks on EBCDIC
        offset = 0
        self.assert_data(data.GetUnsignedInt32, offset, 65)
        offset += 4
        self.assert_data(data.GetUnsignedInt8, offset, 66)
        offset += 1
        self.assert_data(data.GetUnsignedInt8, offset, 67)
        offset += 1
        self.assert_data(data.GetUnsignedInt8, offset, 68)
        offset += 1

        # check the new API calls introduced per LLVM llvm.org/prenhancement request
        # 11619 (Allow creating SBData values from arrays or primitives in
        # Python)

        hello_str = "hello!"
        data2 = lldb.SBData.CreateDataFromCString(process.GetByteOrder(),
                                                  process.GetAddressByteSize(),
                                                  hello_str)
        self.assertEqual(len(data2.uint8), len(hello_str))
        self.assertEqual(data2.uint8[0], 104, 'h == 104')
        self.assertEqual(data2.uint8[1], 101, 'e == 101')
        self.assertEqual(data2.uint8[2], 108, 'l == 108')
        self.assert_data(data2.GetUnsignedInt8, 3, 108)  # l
        self.assertEqual(data2.uint8[4], 111, 'o == 111')
        self.assert_data(data2.GetUnsignedInt8, 5, 33)  # !

        uint_lists = [[1, 2, 3, 4, 5], [int(i) for i in [1, 2, 3, 4, 5]]]
        int_lists = [[2, -2], [int(i) for i in [2, -2]]]

        for l in uint_lists:
            data2 = lldb.SBData.CreateDataFromUInt64Array(
                process.GetByteOrder(), process.GetAddressByteSize(), l)
            self.assert_data(data2.GetUnsignedInt64, 0, 1)
            self.assert_data(data2.GetUnsignedInt64, 8, 2)
            self.assert_data(data2.GetUnsignedInt64, 16, 3)
            self.assert_data(data2.GetUnsignedInt64, 24, 4)
            self.assert_data(data2.GetUnsignedInt64, 32, 5)

            self.assertTrue(data2.uint64s == [1, 2, 3, 4, 5],
                            'read_data_helper failure: data2 == [1,2,3,4,5]')

        for l in int_lists:
            data2 = lldb.SBData.CreateDataFromSInt32Array(
                process.GetByteOrder(), process.GetAddressByteSize(), l)
            self.assertTrue(data2.sint32[0:2] == [2, -2],
                            'signed32 data2 = [2,-2]')

        data2.Append(
            lldb.SBData.CreateDataFromSInt64Array(process.GetByteOrder(),
                                                  process.GetAddressByteSize(),
                                                  int_lists[0]))
        self.assert_data(data2.GetSignedInt32, 0, 2)
        self.assert_data(data2.GetSignedInt32, 4, -2)
        self.assertTrue(data2.sint64[1:3] == [2, -2],
                        'signed64 data2 = [2,-2]')

        for l in int_lists:
            data2 = lldb.SBData.CreateDataFromSInt64Array(
                process.GetByteOrder(), process.GetAddressByteSize(), l)
            self.assert_data(data2.GetSignedInt64, 0, 2)
            self.assert_data(data2.GetSignedInt64, 8, -2)
            self.assertTrue(data2.sint64[0:2] == [2, -2],
                            'signed64 data2 = [2,-2]')

        for l in uint_lists:
            data2 = lldb.SBData.CreateDataFromUInt32Array(
                process.GetByteOrder(), process.GetAddressByteSize(), l)
            self.assert_data(data2.GetUnsignedInt32, 0, 1)
            self.assert_data(data2.GetUnsignedInt32, 4, 2)
            self.assert_data(data2.GetUnsignedInt32, 8, 3)
            self.assert_data(data2.GetUnsignedInt32, 12, 4)
            self.assert_data(data2.GetUnsignedInt32, 16, 5)

        bool_list = [True, True, False, False, True, False]

        data2 = lldb.SBData.CreateDataFromSInt32Array(
            process.GetByteOrder(), process.GetAddressByteSize(), bool_list)
        self.assertTrue(data2.sint32[0:6] == [1, 1, 0, 0, 1, 0],
                        'signed32 data2 = [1, 1, 0, 0, 1, 0]')

        data2 = lldb.SBData.CreateDataFromUInt32Array(
            process.GetByteOrder(), process.GetAddressByteSize(), bool_list)
        self.assertTrue(data2.uint32[0:6] == [1, 1, 0, 0, 1, 0],
                        'unsigned32 data2 = [1, 1, 0, 0, 1, 0]')

        data2 = lldb.SBData.CreateDataFromSInt64Array(
            process.GetByteOrder(), process.GetAddressByteSize(), bool_list)
        self.assertTrue(data2.sint64[0:6] == [1, 1, 0, 0, 1, 0],
                        'signed64 data2 = [1, 1, 0, 0, 1, 0]')

        data2 = lldb.SBData.CreateDataFromUInt64Array(
            process.GetByteOrder(), process.GetAddressByteSize(), bool_list)
        self.assertTrue(data2.uint64[0:6] == [1, 1, 0, 0, 1, 0],
                        'signed64 data2 = [1, 1, 0, 0, 1, 0]')

        data2 = lldb.SBData.CreateDataFromDoubleArray(
            process.GetByteOrder(), process.GetAddressByteSize(),
            [3.14, 6.28, 2.71])
        self.assertTrue(
            fabs(data2.GetDouble(error, 0) - 3.14) < 0.5,
            'double data2[0] = 3.14')
        self.assertTrue(error.Success())
        self.assertTrue(
            fabs(data2.GetDouble(error, 8) - 6.28) < 0.5,
            'double data2[1] = 6.28')
        self.assertTrue(error.Success())
        self.assertTrue(
            fabs(data2.GetDouble(error, 16) - 2.71) < 0.5,
            'double data2[2] = 2.71')
        self.assertTrue(error.Success())

        data2 = lldb.SBData()

        data2.SetDataFromCString(hello_str)
        self.assertEqual(len(data2.uint8), len(hello_str))
        self.assert_data(data2.GetUnsignedInt8, 0, 104)
        self.assert_data(data2.GetUnsignedInt8, 1, 101)
        self.assert_data(data2.GetUnsignedInt8, 2, 108)
        self.assert_data(data2.GetUnsignedInt8, 3, 108)
        self.assert_data(data2.GetUnsignedInt8, 4, 111)
        self.assert_data(data2.GetUnsignedInt8, 5, 33)

        data2.SetDataFromUInt64Array([1, 2, 3, 4, 5])
        self.assert_data(data2.GetUnsignedInt64, 0, 1)
        self.assert_data(data2.GetUnsignedInt64, 8, 2)
        self.assert_data(data2.GetUnsignedInt64, 16, 3)
        self.assert_data(data2.GetUnsignedInt64, 24, 4)
        self.assert_data(data2.GetUnsignedInt64, 32, 5)

        self.assertEqual(data2.uint64[0], 1,
                         'read_data_helper failure: set data2[0] = 1')
        self.assertEqual(data2.uint64[1], 2,
                         'read_data_helper failure: set data2[1] = 2')
        self.assertEqual(data2.uint64[2], 3,
                         'read_data_helper failure: set data2[2] = 3')
        self.assertEqual(data2.uint64[3], 4,
                         'read_data_helper failure: set data2[3] = 4')
        self.assertEqual(data2.uint64[4], 5,
                         'read_data_helper failure: set data2[4] = 5')

        self.assertTrue(data2.uint64[0:2] == [1, 2],
                        'read_data_helper failure: set data2[0:2] = [1,2]')

        data2.SetDataFromSInt32Array([2, -2])
        self.assert_data(data2.GetSignedInt32, 0, 2)
        self.assert_data(data2.GetSignedInt32, 4, -2)

        data2.SetDataFromSInt64Array([2, -2])
        self.assert_data(data2.GetSignedInt64, 0, 2)
        self.assert_data(data2.GetSignedInt64, 8, -2)

        data2.SetDataFromUInt32Array([1, 2, 3, 4, 5])
        self.assert_data(data2.GetUnsignedInt32, 0, 1)
        self.assert_data(data2.GetUnsignedInt32, 4, 2)
        self.assert_data(data2.GetUnsignedInt32, 8, 3)
        self.assert_data(data2.GetUnsignedInt32, 12, 4)
        self.assert_data(data2.GetUnsignedInt32, 16, 5)

        self.assertEqual(data2.uint32[0], 1,
                         'read_data_helper failure: set 32-bit data2[0] = 1')
        self.assertEqual(data2.uint32[1], 2,
                         'read_data_helper failure: set 32-bit data2[1] = 2')
        self.assertEqual(data2.uint32[2], 3,
                         'read_data_helper failure: set 32-bit data2[2] = 3')
        self.assertEqual(data2.uint32[3], 4,
                         'read_data_helper failure: set 32-bit data2[3] = 4')
        self.assertEqual(data2.uint32[4], 5,
                         'read_data_helper failure: set 32-bit data2[4] = 5')

        data2.SetDataFromDoubleArray([3.14, 6.28, 2.71])
        self.assertTrue(
            fabs(data2.GetDouble(error, 0) - 3.14) < 0.5,
            'set double data2[0] = 3.14')
        self.assertTrue(
            fabs(data2.GetDouble(error, 8) - 6.28) < 0.5,
            'set double data2[1] = 6.28')
        self.assertTrue(
            fabs(data2.GetDouble(error, 16) - 2.71) < 0.5,
            'set double data2[2] = 2.71')

        self.assertTrue(
            fabs(data2.double[0] - 3.14) < 0.5,
            'read_data_helper failure: set double data2[0] = 3.14')
        self.assertTrue(
            fabs(data2.double[1] - 6.28) < 0.5,
            'read_data_helper failure: set double data2[1] = 6.28')
        self.assertTrue(
            fabs(data2.double[2] - 2.71) < 0.5,
            'read_data_helper failure: set double data2[2] = 2.71')
예제 #11
0
 def read_memory_at_address(self, addr: int, size: int) -> lldb.SBData:
     data = lldb.SBData().CreateDataFromCString(
         self.target.GetByteOrder(), self.target.GetCodeByteSize(),
         "Hello, world!")
     return data
예제 #12
0
 def _binary_op(self, other, op, reverse=False):
     sbtype, type_class = self._stripped_sbtype()
     if type_class == lldb.eTypeClassPointer:
         if not (op == OP_ADD or op == OP_SUB) or reverse:
             raise TypeError(
                 'Invalid binary operation on/with pointer value.')
     if isinstance(other, int) or isinstance(other, long):
         other_val = other
         other_sbtype = get_builtin_sbtype('long')
         other_type_class = lldb.eTypeClassBuiltin
     elif isinstance(other, float):
         other_val = other
         other_sbtype = get_builtin_sbtype('double')
         other_type_class = lldb.eTypeClassBuiltin
     elif isinstance(other, Value):
         other_sbtype, other_type_class = other._stripped_sbtype()
         if (other_type_class == lldb.eTypeClassPointer
                 and not (type_class == lldb.eTypeClassPointer
                          and op == OP_SUB)):
             raise TypeError(
                 'Invalid binary operation on/with pointer value.')
         other_val = other._as_number()
     else:
         raise TypeError('Cannot perform binary operation with/on value '
                         'of type "%s".' % str(type(other)))
     if op == OP_BITWISE_AND:
         res = self._as_number() & other_val
     elif op == OP_BITWISE_OR:
         res = self._as_number() | other_val
     elif op == OP_BITWISE_XOR:
         res = self._as_number() ^ other_val
     elif op == OP_ADD:
         if type_class == lldb.eTypeClassPointer:
             addr = self._sbvalue_object.GetValueAsUnsigned()
             new_addr = (addr +
                         other_val * sbtype.GetPointeeType().GetByteSize())
             new_sbvalue = self._sbvalue_object.CreateValueFromAddress(
                 '', new_addr, sbtype.GetPointeeType())
             return Value(new_sbvalue.AddressOf().Cast(
                 self._sbvalue_object.GetType()))
         else:
             res = self._as_number() + other_val
     elif op == OP_SUB:
         if reverse:
             res = other_val - self._as_number()
         else:
             if type_class == lldb.eTypeClassPointer:
                 if other_type_class == lldb.eTypeClassPointer:
                     if sbtype != other_sbtype:
                         raise TypeError('Arithmetic operation on '
                                         'incompatible pointer types.')
                     diff = self._as_number() - other_val
                     return diff / sbtype.GetPointeeType().GetByteSize()
                 else:
                     return self._binary_op(-other_val, OP_ADD)
             else:
                 res = self._as_number() - other_val
     elif op == OP_LSHIFT:
         if reverse:
             return other_val << self._as_number()
         else:
             res = self._as_number() << other_val
     elif op == OP_RSHIFT:
         if reverse:
             return other_val >> self._as_number()
         else:
             res = self._as_number() >> other_val
     else:
         raise RuntimeError('Unsupported or incorrect binary operation.')
     data = lldb.SBData()
     data.SetDataFromUInt64Array([res])
     return Value(
         self._sbvalue_object.CreateValueFromData(
             '', data, self._sbvalue_object.GetType()))
예제 #13
0
    def data_api(self):
        """Test the SBData APIs."""
        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)

        lldbutil.run_break_set_by_file_and_line(self,
                                                "main.cpp",
                                                self.line,
                                                num_expected_locations=1,
                                                loc_exact=True)

        self.runCmd("run", RUN_SUCCEEDED)

        # The stop reason of the thread should be breakpoint.
        self.expect("thread list",
                    STOPPED_DUE_TO_BREAKPOINT,
                    substrs=['stopped', 'stop reason = breakpoint'])

        target = self.dbg.GetSelectedTarget()

        process = target.GetProcess()

        thread = process.GetThreadAtIndex(0)

        frame = thread.GetSelectedFrame()
        if self.TraceOn():
            print frame
        foobar = frame.FindVariable('foobar')
        self.assertTrue(foobar.IsValid())
        if self.TraceOn():
            print foobar

        data = foobar.GetPointeeData(0, 2)

        if self.TraceOn():
            print data

        offset = 0
        error = lldb.SBError()

        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 1, 'foo[0].a == 1')
        offset += 4
        low = data.GetSignedInt16(error, offset)
        offset += 2
        high = data.GetSignedInt16(error, offset)
        offset += 2
        self.assertTrue((low == 9 and high == 0) or (low == 0 and high == 9),
                        'foo[0].b == 9')
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[0].c == 3.14')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 8, 'foo[1].a == 8')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 5, 'foo[1].b == 5')
        offset += 4

        self.runCmd("n")

        offset = 16

        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 5, 'saved foo[1].b == 5')

        data = foobar.GetPointeeData(1, 1)

        offset = 0

        self.assertTrue(
            data.GetSignedInt32(error, offset) == 8, 'new foo[1].a == 8')
        offset += 4
        self.assertTrue(
            data.GetSignedInt32(error, offset) == 7, 'new foo[1].a == 7')
        offset += 8
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 0,
            'do not read beyond end')

        star_foobar = foobar.Dereference()
        self.assertTrue(star_foobar.IsValid())

        data = star_foobar.GetData()

        if self.TraceOn():
            print data

        offset = 0
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 1, 'foo[0].a == 1')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 9, 'foo[0].b == 9')

        foobar_addr = star_foobar.GetLoadAddress()
        foobar_addr += 12

        # http://llvm.org/bugs/show_bug.cgi?id=11579
        # lldb::SBValue::CreateValueFromAddress does not verify SBType::GetPointerType succeeds
        # This should not crash LLDB.
        nothing = foobar.CreateValueFromAddress(
            "nothing", foobar_addr,
            star_foobar.GetType().GetBasicType(lldb.eBasicTypeInvalid))

        new_foobar = foobar.CreateValueFromAddress("f00", foobar_addr,
                                                   star_foobar.GetType())
        self.assertTrue(new_foobar.IsValid())
        if self.TraceOn():
            print new_foobar

        data = new_foobar.GetData()

        if self.TraceOn():
            print data

        self.assertTrue(data.uint32[0] == 8, 'then foo[1].a == 8')
        self.assertTrue(data.uint32[1] == 7, 'then foo[1].b == 7')
        self.assertTrue(fabs(data.float[2] - 3.14) < 1, 'foo[1].c == 3.14'
                        )  # exploiting that sizeof(uint32) == sizeof(float)

        self.runCmd("n")

        offset = 0
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 8, 'then foo[1].a == 8')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 7, 'then foo[1].b == 7')
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[1].c == 3.14')

        data = new_foobar.GetData()

        if self.TraceOn():
            print data

        offset = 0
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 8, 'finally foo[1].a == 8')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 7, 'finally foo[1].b == 7')
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 6.28) < 1, 'foo[1].c == 6.28')

        self.runCmd("n")

        barfoo = frame.FindVariable('barfoo')

        data = barfoo.GetData()

        if self.TraceOn():
            print barfoo

        if self.TraceOn():
            print data

        offset = 0
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 1, 'barfoo[0].a = 1')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 2, 'barfoo[0].b == 2')
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 3) < 1, 'barfoo[0].c == 3')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 4, 'barfoo[1].a = 4')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 5, 'barfoo[1].b == 5')
        offset += 4
        self.assertTrue(
            fabs(data.GetFloat(error, offset) - 6) < 1, 'barfoo[1].c == 6')

        new_object = barfoo.CreateValueFromData(
            "new_object", data,
            barfoo.GetType().GetBasicType(lldb.eBasicTypeInt))

        if self.TraceOn():
            print new_object

        self.assertTrue(new_object.GetLoadAddress() == 0xFFFFFFFFFFFFFFFF,
                        'GetLoadAddress() == invalid')
        self.assertTrue(new_object.AddressOf().IsValid() == False,
                        'AddressOf() == invalid')
        self.assertTrue(new_object.GetAddress().IsValid() == False,
                        'GetAddress() == invalid')

        self.assertTrue(new_object.GetValue() == "1", 'new_object == 1')

        data.SetData(error, 'A\0\0\0', data.GetByteOrder(),
                     data.GetAddressByteSize())

        data2 = lldb.SBData()
        data2.SetData(error, 'BCD', data.GetByteOrder(),
                      data.GetAddressByteSize())

        data.Append(data2)

        if self.TraceOn():
            print data

        # this breaks on EBCDIC
        offset = 0
        self.assertTrue(
            data.GetUnsignedInt32(error, offset) == 65, 'made-up data == 65')
        offset += 4
        self.assertTrue(
            data.GetUnsignedInt8(error, offset) == 66, 'made-up data == 66')
        offset += 1
        self.assertTrue(
            data.GetUnsignedInt8(error, offset) == 67, 'made-up data == 67')
        offset += 1
        self.assertTrue(
            data.GetUnsignedInt8(error, offset) == 68, 'made-up data == 68')
        offset += 1

        # check the new API calls introduced per LLVM bugzilla enhancement request
        # 11619 (Allow creating SBData values from arrays or primitives in Python)

        data2 = lldb.SBData.CreateDataFromCString(process.GetByteOrder(),
                                                  process.GetAddressByteSize(),
                                                  'hello!')
        self.assertTrue(data2.uint8[0] == 104, 'h == 104')
        self.assertTrue(data2.uint8[1] == 101, 'e == 101')
        self.assertTrue(data2.uint8[2] == 108, 'l == 108')
        self.assertTrue(data2.GetUnsignedInt8(error, 3) == 108, 'l == 108')
        self.assertTrue(data2.uint8[4] == 111, 'o == 111')
        self.assertTrue(data2.GetUnsignedInt8(error, 5) == 33, '! == 33')
        self.assertTrue(data2.uint8[6] == 0, 'binary 0 terminator')

        data2 = lldb.SBData.CreateDataFromUInt64Array(
            process.GetByteOrder(), process.GetAddressByteSize(),
            [1, 2, 3, 4, 5])
        self.assertTrue(data2.GetUnsignedInt64(error, 0) == 1, 'data2[0] = 1')
        self.assertTrue(data2.GetUnsignedInt64(error, 8) == 2, 'data2[1] = 2')
        self.assertTrue(data2.GetUnsignedInt64(error, 16) == 3, 'data2[2] = 3')
        self.assertTrue(data2.GetUnsignedInt64(error, 24) == 4, 'data2[3] = 4')
        self.assertTrue(data2.GetUnsignedInt64(error, 32) == 5, 'data2[4] = 5')

        self.assertTrue(data2.uint64s == [1, 2, 3, 4, 5],
                        'read_data_helper failure: data2 == [1,2,3,4,5]')

        data2 = lldb.SBData.CreateDataFromSInt32Array(
            process.GetByteOrder(), process.GetAddressByteSize(), [2, -2])
        self.assertTrue(data2.sint32[0:2] == [2, -2],
                        'signed32 data2 = [2,-2]')

        data2.Append(
            lldb.SBData.CreateDataFromSInt64Array(process.GetByteOrder(),
                                                  process.GetAddressByteSize(),
                                                  [2, -2]))
        self.assertTrue(
            data2.GetSignedInt32(error, 0) == 2, 'signed32 data2[0] = 2')
        self.assertTrue(
            data2.GetSignedInt32(error, 4) == -2, 'signed32 data2[1] = -2')
        self.assertTrue(data2.sint64[1:3] == [2, -2],
                        'signed64 data2 = [2,-2]')

        data2 = lldb.SBData.CreateDataFromUInt32Array(
            process.GetByteOrder(), process.GetAddressByteSize(),
            [1, 2, 3, 4, 5])
        self.assertTrue(
            data2.GetUnsignedInt32(error, 0) == 1, '32-bit data2[0] = 1')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 4) == 2, '32-bit data2[1] = 2')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 8) == 3, '32-bit data2[2] = 3')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 12) == 4, '32-bit data2[3] = 4')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 16) == 5, '32-bit data2[4] = 5')

        data2 = lldb.SBData.CreateDataFromDoubleArray(
            process.GetByteOrder(), process.GetAddressByteSize(),
            [3.14, 6.28, 2.71])
        self.assertTrue(
            fabs(data2.GetDouble(error, 0) - 3.14) < 0.5,
            'double data2[0] = 3.14')
        self.assertTrue(
            fabs(data2.GetDouble(error, 8) - 6.28) < 0.5,
            'double data2[1] = 6.28')
        self.assertTrue(
            fabs(data2.GetDouble(error, 16) - 2.71) < 0.5,
            'double data2[2] = 2.71')

        data2 = lldb.SBData()

        data2.SetDataFromCString('hello!')
        self.assertTrue(data2.GetUnsignedInt8(error, 0) == 104, 'set h == 104')
        self.assertTrue(data2.GetUnsignedInt8(error, 1) == 101, 'set e == 101')
        self.assertTrue(data2.GetUnsignedInt8(error, 2) == 108, 'set l == 108')
        self.assertTrue(data2.GetUnsignedInt8(error, 3) == 108, 'set l == 108')
        self.assertTrue(data2.GetUnsignedInt8(error, 4) == 111, 'set o == 111')
        self.assertTrue(data2.GetUnsignedInt8(error, 5) == 33, 'set ! == 33')
        self.assertTrue(
            data2.GetUnsignedInt8(error, 6) == 0, 'set binary 0 terminator')

        data2.SetDataFromUInt64Array([1, 2, 3, 4, 5])
        self.assertTrue(
            data2.GetUnsignedInt64(error, 0) == 1, 'set data2[0] = 1')
        self.assertTrue(
            data2.GetUnsignedInt64(error, 8) == 2, 'set data2[1] = 2')
        self.assertTrue(
            data2.GetUnsignedInt64(error, 16) == 3, 'set data2[2] = 3')
        self.assertTrue(
            data2.GetUnsignedInt64(error, 24) == 4, 'set data2[3] = 4')
        self.assertTrue(
            data2.GetUnsignedInt64(error, 32) == 5, 'set data2[4] = 5')

        self.assertTrue(data2.uint64[0] == 1,
                        'read_data_helper failure: set data2[0] = 1')
        self.assertTrue(data2.uint64[1] == 2,
                        'read_data_helper failure: set data2[1] = 2')
        self.assertTrue(data2.uint64[2] == 3,
                        'read_data_helper failure: set data2[2] = 3')
        self.assertTrue(data2.uint64[3] == 4,
                        'read_data_helper failure: set data2[3] = 4')
        self.assertTrue(data2.uint64[4] == 5,
                        'read_data_helper failure: set data2[4] = 5')

        self.assertTrue(data2.uint64[0:2] == [1, 2],
                        'read_data_helper failure: set data2[0:2] = [1,2]')

        data2.SetDataFromSInt32Array([2, -2])
        self.assertTrue(
            data2.GetSignedInt32(error, 0) == 2, 'set signed32 data2[0] = 2')
        self.assertTrue(
            data2.GetSignedInt32(error, 4) == -2, 'set signed32 data2[1] = -2')

        data2.SetDataFromSInt64Array([2, -2])
        self.assertTrue(
            data2.GetSignedInt32(error, 0) == 2, 'set signed64 data2[0] = 2')
        self.assertTrue(
            data2.GetSignedInt32(error, 8) == -2, 'set signed64 data2[1] = -2')

        data2.SetDataFromUInt32Array([1, 2, 3, 4, 5])
        self.assertTrue(
            data2.GetUnsignedInt32(error, 0) == 1, 'set 32-bit data2[0] = 1')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 4) == 2, 'set 32-bit data2[1] = 2')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 8) == 3, 'set 32-bit data2[2] = 3')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 12) == 4, 'set 32-bit data2[3] = 4')
        self.assertTrue(
            data2.GetUnsignedInt32(error, 16) == 5, 'set 32-bit data2[4] = 5')

        self.assertTrue(data2.uint32[0] == 1,
                        'read_data_helper failure: set 32-bit data2[0] = 1')
        self.assertTrue(data2.uint32[1] == 2,
                        'read_data_helper failure: set 32-bit data2[1] = 2')
        self.assertTrue(data2.uint32[2] == 3,
                        'read_data_helper failure: set 32-bit data2[2] = 3')
        self.assertTrue(data2.uint32[3] == 4,
                        'read_data_helper failure: set 32-bit data2[3] = 4')
        self.assertTrue(data2.uint32[4] == 5,
                        'read_data_helper failure: set 32-bit data2[4] = 5')

        data2.SetDataFromDoubleArray([3.14, 6.28, 2.71])
        self.assertTrue(
            fabs(data2.GetDouble(error, 0) - 3.14) < 0.5,
            'set double data2[0] = 3.14')
        self.assertTrue(
            fabs(data2.GetDouble(error, 8) - 6.28) < 0.5,
            'set double data2[1] = 6.28')
        self.assertTrue(
            fabs(data2.GetDouble(error, 16) - 2.71) < 0.5,
            'set double data2[2] = 2.71')

        self.assertTrue(
            fabs(data2.double[0] - 3.14) < 0.5,
            'read_data_helper failure: set double data2[0] = 3.14')
        self.assertTrue(
            fabs(data2.double[1] - 6.28) < 0.5,
            'read_data_helper failure: set double data2[1] = 6.28')
        self.assertTrue(
            fabs(data2.double[2] - 2.71) < 0.5,
            'read_data_helper failure: set double data2[2] = 2.71')
예제 #14
0
    def test(self):
        """
        Test lldb's parsing of SEGGER J-Link v6.54 register
        definition for a Cortex M-4 dev board, and the fact
        that the J-Link only supports g/G for reading/writing
        register AND the J-Link v6.54 doesn't provide anything
        but the general purpose registers."""
        class MyResponder(MockGDBServerResponder):
            def qXferRead(self, obj, annex, offset, length):
                if annex == "target.xml":
                    return """<?xml version="1.0"?>
<!-- Copyright (C) 2008 Free Software Foundation, Inc.

     Copying and distribution of this file, with or without modification,
     are permitted in any medium without royalty provided the copyright
     notice and this notice are preserved.  -->

<!DOCTYPE feature SYSTEM "gdb-target.dtd">
<target version="1.0">
  <architecture>arm</architecture>
  <feature name="org.gnu.gdb.arm.m-profile">
    <reg name="r0" bitsize="32" regnum="0" type="uint32" group="general"/>
    <reg name="r1" bitsize="32" regnum="1" type="uint32" group="general"/>
    <reg name="r2" bitsize="32" regnum="2" type="uint32" group="general"/>
    <reg name="r3" bitsize="32" regnum="3" type="uint32" group="general"/>
    <reg name="r4" bitsize="32" regnum="4" type="uint32" group="general"/>
    <reg name="r5" bitsize="32" regnum="5" type="uint32" group="general"/>
    <reg name="r6" bitsize="32" regnum="6" type="uint32" group="general"/>
    <reg name="r7" bitsize="32" regnum="7" type="uint32" group="general"/>
    <reg name="r8" bitsize="32" regnum="8" type="uint32" group="general"/>
    <reg name="r9" bitsize="32" regnum="9" type="uint32" group="general"/>
    <reg name="r10" bitsize="32" regnum="10" type="uint32" group="general"/>
    <reg name="r11" bitsize="32" regnum="11" type="uint32" group="general"/>
    <reg name="r12" bitsize="32" regnum="12" type="uint32" group="general"/>
    <reg name="sp" bitsize="32" regnum="13" type="data_ptr" group="general"/>
    <reg name="lr" bitsize="32" regnum="14" type="uint32" group="general"/>
    <reg name="pc" bitsize="32" regnum="15" type="code_ptr" group="general"/>
    <reg name="xpsr" bitsize="32" regnum="25" type="uint32" group="general"/>
  </feature>
  <feature name="org.gnu.gdb.arm.m-system">
    <reg name="msp" bitsize="32" regnum="26" type="uint32" group="general"/>
    <reg name="psp" bitsize="32" regnum="27" type="uint32" group="general"/>
    <reg name="primask" bitsize="32" regnum="28" type="uint32" group="general"/>
    <reg name="basepri" bitsize="32" regnum="29" type="uint32" group="general"/>
    <reg name="faultmask" bitsize="32" regnum="30" type="uint32" group="general"/>
    <reg name="control" bitsize="32" regnum="31" type="uint32" group="general"/>
  </feature>
  <feature name="org.gnu.gdb.arm.m-float">
    <reg name="fpscr" bitsize="32" regnum="32" type="uint32" group="float"/>
    <reg name="s0" bitsize="32" regnum="33" type="float" group="float"/>
    <reg name="s1" bitsize="32" regnum="34" type="float" group="float"/>
    <reg name="s2" bitsize="32" regnum="35" type="float" group="float"/>
    <reg name="s3" bitsize="32" regnum="36" type="float" group="float"/>
    <reg name="s4" bitsize="32" regnum="37" type="float" group="float"/>
    <reg name="s5" bitsize="32" regnum="38" type="float" group="float"/>
    <reg name="s6" bitsize="32" regnum="39" type="float" group="float"/>
    <reg name="s7" bitsize="32" regnum="40" type="float" group="float"/>
    <reg name="s8" bitsize="32" regnum="41" type="float" group="float"/>
    <reg name="s9" bitsize="32" regnum="42" type="float" group="float"/>
    <reg name="s10" bitsize="32" regnum="43" type="float" group="float"/>
    <reg name="s11" bitsize="32" regnum="44" type="float" group="float"/>
    <reg name="s12" bitsize="32" regnum="45" type="float" group="float"/>
    <reg name="s13" bitsize="32" regnum="46" type="float" group="float"/>
    <reg name="s14" bitsize="32" regnum="47" type="float" group="float"/>
    <reg name="s15" bitsize="32" regnum="48" type="float" group="float"/>
    <reg name="s16" bitsize="32" regnum="49" type="float" group="float"/>
    <reg name="s17" bitsize="32" regnum="50" type="float" group="float"/>
    <reg name="s18" bitsize="32" regnum="51" type="float" group="float"/>
    <reg name="s19" bitsize="32" regnum="52" type="float" group="float"/>
    <reg name="s20" bitsize="32" regnum="53" type="float" group="float"/>
    <reg name="s21" bitsize="32" regnum="54" type="float" group="float"/>
    <reg name="s22" bitsize="32" regnum="55" type="float" group="float"/>
    <reg name="s23" bitsize="32" regnum="56" type="float" group="float"/>
    <reg name="s24" bitsize="32" regnum="57" type="float" group="float"/>
    <reg name="s25" bitsize="32" regnum="58" type="float" group="float"/>
    <reg name="s26" bitsize="32" regnum="59" type="float" group="float"/>
    <reg name="s27" bitsize="32" regnum="60" type="float" group="float"/>
    <reg name="s28" bitsize="32" regnum="61" type="float" group="float"/>
    <reg name="s29" bitsize="32" regnum="62" type="float" group="float"/>
    <reg name="s30" bitsize="32" regnum="63" type="float" group="float"/>
    <reg name="s31" bitsize="32" regnum="64" type="float" group="float"/>
    <reg name="d0" bitsize="64" regnum="65" type="ieee_double" group="float"/>
    <reg name="d1" bitsize="64" regnum="66" type="ieee_double" group="float"/>
    <reg name="d2" bitsize="64" regnum="67" type="ieee_double" group="float"/>
    <reg name="d3" bitsize="64" regnum="68" type="ieee_double" group="float"/>
    <reg name="d4" bitsize="64" regnum="69" type="ieee_double" group="float"/>
    <reg name="d5" bitsize="64" regnum="70" type="ieee_double" group="float"/>
    <reg name="d6" bitsize="64" regnum="71" type="ieee_double" group="float"/>
    <reg name="d7" bitsize="64" regnum="72" type="ieee_double" group="float"/>
    <reg name="d8" bitsize="64" regnum="73" type="ieee_double" group="float"/>
    <reg name="d9" bitsize="64" regnum="74" type="ieee_double" group="float"/>
    <reg name="d10" bitsize="64" regnum="75" type="ieee_double" group="float"/>
    <reg name="d11" bitsize="64" regnum="76" type="ieee_double" group="float"/>
    <reg name="d12" bitsize="64" regnum="77" type="ieee_double" group="float"/>
    <reg name="d13" bitsize="64" regnum="78" type="ieee_double" group="float"/>
    <reg name="d14" bitsize="64" regnum="79" type="ieee_double" group="float"/>
    <reg name="d15" bitsize="64" regnum="80" type="ieee_double" group="float"/>
  </feature>
</target>""", False
                else:
                    return None, False

            def readRegister(self, regnum):
                return "E01"

            # Initial r1 bytes, in little-endian order
            r1_bytes = "01000000"

            ## readRegisters only provides reg values up through xpsr (0x61000000)
            ## it doesn't send up any of the exception registers or floating point
            ## registers that the above register xml describes.
            def readRegisters(self):
                return "00000000" + self.r1_bytes + "010000000100000001000000000000008c080020a872012000000000a0790120000000008065012041ad0008a0720120692a00089e26000800000061"

            ## the J-Link accepts a register write packet with just the GPRs
            ## defined.
            def writeRegisters(self, registers_hex):
                # Check that lldb returns the full 704 hex-byte register context,
                # or the 136 hex-byte general purpose register reg ctx.
                if len(registers_hex) != 704 and len(register_hex) != 136:
                    return "E06"
                if registers_hex.startswith(
                        "0000000044332211010000000100000001000000000000008c080020a872012000000000a0790120000000008065012041ad0008a0720120692a00089e26000800000061"
                ):
                    self.r1_bytes = "44332211"
                    return "OK"
                else:
                    return "E07"

            def haltReason(self):
                return "S05"

            def qfThreadInfo(self):
                return "mdead"

            def qC(self):
                return ""

            def qSupported(self, client_supported):
                return "PacketSize=4000;qXfer:memory-map:read-;QStartNoAckMode+;hwbreak+;qXfer:features:read+"

            def QThreadSuffixSupported(self):
                return "OK"

            def QListThreadsInStopReply(self):
                return "OK"

        self.server.responder = MyResponder()
        if self.TraceOn():
            self.runCmd("log enable gdb-remote packets")
            self.addTearDownHook(
                lambda: self.runCmd("log disable gdb-remote packets"))

        self.dbg.SetDefaultArchitecture("armv7em")
        target = self.dbg.CreateTargetWithFileAndArch(None, None)

        process = self.connect(target)

        if self.TraceOn():
            interp = self.dbg.GetCommandInterpreter()
            result = lldb.SBCommandReturnObject()
            interp.HandleCommand("target list", result)
            print(result.GetOutput())

        r1_valobj = process.GetThreadAtIndex(0).GetFrameAtIndex(
            0).FindRegister("r1")
        self.assertEqual(r1_valobj.GetValueAsUnsigned(), 1)

        pc_valobj = process.GetThreadAtIndex(0).GetFrameAtIndex(
            0).FindRegister("pc")
        self.assertEqual(pc_valobj.GetValueAsUnsigned(), 0x0800269e)

        xpsr_valobj = process.GetThreadAtIndex(0).GetFrameAtIndex(
            0).FindRegister("xpsr")
        self.assertEqual(xpsr_valobj.GetValueAsUnsigned(), 0x61000000)

        msp_valobj = process.GetThreadAtIndex(0).GetFrameAtIndex(
            0).FindRegister("msp")
        err = msp_valobj.GetError()
        self.assertTrue(err.Fail(),
                        "lldb should not be able to fetch the msp register")

        # Reproducers don't support SetData (yet) because it takes a void*.
        if not configuration.is_reproducer():
            val = b'\x11\x22\x33\x44'
            error = lldb.SBError()
            data = lldb.SBData()
            data.SetData(error, val, lldb.eByteOrderBig, 4)
            self.assertEqual(r1_valobj.SetData(data, error), True)
            self.assertTrue(error.Success())

            r1_valobj = process.GetThreadAtIndex(0).GetFrameAtIndex(
                0).FindRegister("r1")
            self.assertEqual(r1_valobj.GetValueAsUnsigned(), 0x11223344)
예제 #15
0
    def data_api(self):
        """Test the SBData APIs."""
        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
        
        self.expect("breakpoint set -f main.cpp -l %d" % self.line,
                    BREAKPOINT_CREATED,
                    startstr = "Breakpoint created: 1: file ='main.cpp', line = %d, locations = 1" %
                    self.line)
        
        self.runCmd("run", RUN_SUCCEEDED)
        
        # The stop reason of the thread should be breakpoint.
        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
                    substrs = ['stopped',
                               'stop reason = breakpoint'])
        
        target = self.dbg.GetSelectedTarget()
        
        process = target.GetProcess()
        
        thread = process.GetThreadAtIndex(0)

        frame = thread.GetSelectedFrame()

        foobar = frame.FindVariable('foobar')

        if self.TraceOn():
            print foobar

        data = foobar.GetPointeeData(0, 2)

        if self.TraceOn():
            print data

        offset = 0
        error = lldb.SBError()

        self.assertTrue(data.GetUnsignedInt32(error, offset) == 1, 'foo[0].a == 1')
        offset += 4
        low = data.GetSignedInt16(error, offset)
        offset += 2
        high = data.GetSignedInt16(error, offset)
        offset += 2
        self.assertTrue ((low == 9 and high == 0) or (low == 0 and high == 9), 'foo[0].b == 9')
        self.assertTrue( fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[0].c == 3.14')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 8, 'foo[1].a == 8')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 5, 'foo[1].b == 5')
        offset += 4

        self.runCmd("n")

        offset = 16

        self.assertTrue(data.GetUnsignedInt32(error, offset) == 5, 'saved foo[1].b == 5')

        data = foobar.GetPointeeData(1, 1)

        offset = 0

        self.assertTrue(data.GetSignedInt32(error, offset) == 8, 'new foo[1].a == 8')
        offset += 4
        self.assertTrue(data.GetSignedInt32(error, offset) == 7, 'new foo[1].a == 7')
        offset += 8
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 0, 'do not read beyond end')

        star_foobar = foobar.Dereference()
        
        data = star_foobar.GetData()

        if self.TraceOn():
            print data
        
        offset = 0
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 1, 'foo[0].a == 1')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 9, 'foo[0].b == 9')

        foobar_addr = star_foobar.GetLoadAddress()
        foobar_addr += 12

        # http://llvm.org/bugs/show_bug.cgi?id=11579
        # lldb::SBValue::CreateValueFromAddress does not verify SBType::GetPointerType succeeds
        # This should not crash LLDB.
        nothing = foobar.CreateValueFromAddress("nothing", foobar_addr, star_foobar.GetType().GetBasicType(lldb.eBasicTypeInvalid))

        new_foobar = foobar.CreateValueFromAddress("f00", foobar_addr, star_foobar.GetType())

        if self.TraceOn():
            print new_foobar
        
        data = new_foobar.GetData()

        if self.TraceOn():
            print data

        offset = 0
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 8, 'then foo[1].a == 8')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 7, 'then foo[1].b == 7')
        offset += 4
        self.assertTrue(fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[1].c == 3.14')

        self.runCmd("n")

        offset = 0
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 8, 'then foo[1].a == 8')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 7, 'then foo[1].b == 7')
        offset += 4
        self.assertTrue(fabs(data.GetFloat(error, offset) - 3.14) < 1, 'foo[1].c == 3.14')

        data = new_foobar.GetData()

        if self.TraceOn():
            print data

        offset = 0
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 8, 'finally foo[1].a == 8')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 7, 'finally foo[1].b == 7')
        offset += 4
        self.assertTrue(fabs(data.GetFloat(error, offset) - 6.28) < 1, 'foo[1].c == 6.28')

        self.runCmd("n")

        barfoo = frame.FindVariable('barfoo')

        data = barfoo.GetData()

        if self.TraceOn():
            print barfoo

        if self.TraceOn():
            print data

        offset = 0
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 1, 'barfoo[0].a = 1')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 2, 'barfoo[0].b == 2')
        offset += 4
        self.assertTrue(fabs(data.GetFloat(error, offset) - 3) < 1, 'barfoo[0].c == 3')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 4, 'barfoo[1].a = 4')
        offset += 4
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 5, 'barfoo[1].b == 5')
        offset += 4
        self.assertTrue(fabs(data.GetFloat(error, offset) - 6) < 1, 'barfoo[1].c == 6')

        new_object = barfoo.CreateValueFromData("new_object",data,barfoo.GetType().GetBasicType(lldb.eBasicTypeInt))

        if self.TraceOn():
            print new_object
        
        self.assertTrue(new_object.GetLoadAddress() == 0xFFFFFFFFFFFFFFFF, 'GetLoadAddress() == invalid')
        self.assertTrue(new_object.AddressOf().IsValid() == False, 'AddressOf() == invalid')
        self.assertTrue(new_object.GetAddress().IsValid() == False, 'GetAddress() == invalid')

        self.assertTrue(new_object.GetValue() == "1", 'new_object == 1')

        data.SetData(error, 'A\0\0\0', data.GetByteOrder(), data.GetAddressByteSize())
        
        data2 = lldb.SBData()
        data2.SetData(error, 'BCD', data.GetByteOrder(), data.GetAddressByteSize())

        data.Append(data2)
        
        if self.TraceOn():
            print data

        # this breaks on EBCDIC
        offset = 0
        self.assertTrue(data.GetUnsignedInt32(error, offset) == 65, 'made-up data == 65')
        offset += 4
        self.assertTrue(data.GetUnsignedInt8(error, offset) == 66, 'made-up data == 66')
        offset += 1
        self.assertTrue(data.GetUnsignedInt8(error, offset) == 67, 'made-up data == 67')
        offset += 1
        self.assertTrue(data.GetUnsignedInt8(error, offset) == 68, 'made-up data == 68')
        offset += 1