[Lldb-commits] [lldb] r126813 - in /lldb/trunk/test: lldbutil.py python_api/process/TestProcessAPI.py
Johnny Chen
johnny.chen at apple.com
Tue Mar 1 17:36:45 PST 2011
Author: johnny
Date: Tue Mar 1 19:36:45 2011
New Revision: 126813
URL: http://llvm.org/viewvc/llvm-project?rev=126813&view=rev
Log:
Add two utility functions to lldbutil.py:
o int_to_bytearray()
o bytearray_to_int()
They return/interpret the bytearray in the little endian format.
For big endian, simply perform ba.reverse() on the bytearray object.
And modify TestProcessAPI.py to take advantage of the functions.
Modified:
lldb/trunk/test/lldbutil.py
lldb/trunk/test/python_api/process/TestProcessAPI.py
Modified: lldb/trunk/test/lldbutil.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbutil.py?rev=126813&r1=126812&r2=126813&view=diff
==============================================================================
--- lldb/trunk/test/lldbutil.py (original)
+++ lldb/trunk/test/lldbutil.py Tue Mar 1 19:36:45 2011
@@ -6,6 +6,63 @@
import sys
import StringIO
+# ==========================================================
+# Integer (byte size 1, 2, 4, and 8) to bytearray conversion
+# ==========================================================
+
+def int_to_bytearray(val, bytesize):
+ """Utility function to convert an integer into a bytearray.
+
+ It returns the bytearray in the little endian format.
+ It is easy to get the big endian representation, just do
+ ba.reverse() on the returned bytearray object.
+ """
+ from struct import *
+
+ if bytesize == 1:
+ return bytearray([val])
+
+ # Little endian followed by a format character.
+ template = "<%c"
+ if bytesize == 2:
+ fmt = template % 'h'
+ elif bytesize == 4:
+ fmt = template % 'i'
+ elif bytesize == 4:
+ fmt = template % 'q'
+ else:
+ return None
+
+ packed = pack(fmt, val)
+ return bytearray(map(ord, packed))
+
+def bytearray_to_int(bytes, bytesize):
+ """Utility function to convert a bytearray into an integer.
+
+ It interprets the bytearray in the little endian format.
+ It is easy to get the big endian representation, just do
+ ba.reverse() on the bytearray object before passing it in.
+ """
+ from struct import *
+
+ if bytesize == 1:
+ return ba[0]
+
+ # Little endian followed by a format character.
+ template = "<%c"
+ if bytesize == 2:
+ fmt = template % 'h'
+ elif bytesize == 4:
+ fmt = template % 'i'
+ elif bytesize == 4:
+ fmt = template % 'q'
+ else:
+ return None
+
+ unpacked = unpack(fmt, str(bytes))
+ return unpacked[0]
+
+
# ===========================================
# Iterator for lldb aggregate data structures
# ===========================================
Modified: lldb/trunk/test/python_api/process/TestProcessAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/process/TestProcessAPI.py?rev=126813&r1=126812&r2=126813&view=diff
==============================================================================
--- lldb/trunk/test/python_api/process/TestProcessAPI.py (original)
+++ lldb/trunk/test/python_api/process/TestProcessAPI.py Tue Mar 1 19:36:45 2011
@@ -174,20 +174,18 @@
# OK, let's get the hex location of the variable.
location = int(val.GetLocation(frame), 16)
+ from lldbutil import int_to_bytearray, bytearray_to_int
byteSize = val.GetByteSize()
- byteOrder = self.process.GetByteOrder()
- bytes = bytearray(byteSize)
+ bytes = int_to_bytearray(256, byteSize)
+ byteOrder = self.process.GetByteOrder()
if byteOrder == lldb.eByteOrderBig:
- # 256 in big endian => 0x00000100
- # the second byte counted from the end is to be 0b00000001
- bytes[-2] = 0b00000001
+ bytes.reverse()
elif byteOrder == lldb.eByteOrderLittle:
- # 256 in little endian => 0x00010000
- # the second byte counted from the start is to be 0b00000001
- bytes[1] = 0b00000001
+ pass
else:
# Neither big endian nor little endian? Return for now.
+ # Add more logic here if we want to handle other types.
return
# The program logic makes the 'my_int' variable to have int type and value of 0.
@@ -211,12 +209,14 @@
if not error.Success():
self.fail("SBProcess.ReadMemory() failed")
new_bytes = bytearray(content, "ascii")
+
+ # The bytearray_to_int utility function expects a little endian bytearray.
if byteOrder == lldb.eByteOrderBig:
- if new_bytes[-2] != 0b00000001:
- self.fail("Memory content read from 'my_int' does not match (int)256")
- elif byteOrder == lldb.eByteOrderLittle:
- if new_bytes[1] != 0b00000001:
- self.fail("Memory content read from 'my_int' does not match (int)256")
+ new_bytes.reverse()
+
+ new_value = bytearray_to_int(new_bytes, byteSize)
+ if new_value != 256:
+ self.fail("Memory content read from 'my_int' does not match (int)256")
# Dump the memory content....
for i in new_bytes:
More information about the lldb-commits
mailing list