Improve types abstraction for gdb python scripts
PATCH 1/2 Improve types abstraction for gdb python scripts
From: Dom Cote <[email protected]>
Change the read_u16 function so it accepts both 'str' and 'byte' as type for the arguments.
When calling read_memory() from gdb API, depending on if it was built with 2.7 or 3.X, the format used to return the data will differ ( 'str' for 2.7, and 'byte' for 3.X ).
Add a function read_memoryview() to be able to get a 'memoryview' object back from read_memory() both with python 2.7 and 3.X .
Tested with python 3.4 and 2.7
Tested with gdb 7.7
--- scripts/gdb/linux/utils.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
scripts/gdb/linux/utils.py +15 -2
@@ -87,11 +87,24 @@ def get_target_endianness(): return target_endianness +def read_memoryview(inf, start, length): + return memoryview(inf.read_memory(start, length)) + + def read_u16(buffer): + value = [0, 0] + + if type(buffer[0]) is str: + value[0] = ord(buffer[0]) + value[1] = ord(buffer[1]) + else: + value[0] = buffer[0] + value[1] = buffer[1] + if get_target_endianness() == LITTLE_ENDIAN: - return ord(buffer[0]) + (ord(buffer[1]) << 8) + return value[0] + (value[1] << 8) else: - return ord(buffer[1]) + (ord(buffer[0]) << 8) + return value[1] + (value[0] << 8) def read_u32(buffer):
Signature · 1 line
2.8.2
PATCH 2/2 Fix issue with dmesg.py and python 3.X
From: Dom Cote <[email protected]>
Replace the addition (+) of 2 python 'memoryview' objects with the addition of 2 'bytes' objects, convert the result back to memoryview.
Tested with python 3.4 and 2.7
Tested with gdb 7.7
--- scripts/gdb/linux/dmesg.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/gdb/linux/dmesg.py +4 -3
@@ -33,11 +33,12 @@ class LxDmesg(gdb.Command): if log_first_idx < log_next_idx: log_buf_2nd_half = -1 length = log_next_idx - log_first_idx - log_buf = inf.read_memory(start, length) + log_buf = utils.read_memoryview(inf, start, length).tobytes() else: log_buf_2nd_half = log_buf_len - log_first_idx - log_buf = inf.read_memory(start, log_buf_2nd_half) + \ - inf.read_memory(log_buf_addr, log_next_idx) + a = utils.read_memoryview(inf, start, log_buf_2nd_half) + b = utils.read_memoryview(inf, log_buf_addr, log_next_idx) + log_buf = a.tobytes() + b.tobytes() pos = 0 while pos < log_buf.__len__():
Signature · 1 line
2.8.2