Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760654AbcDFCik (ORCPT ); Tue, 5 Apr 2016 22:38:40 -0400 Received: from mail-qk0-f178.google.com ([209.85.220.178]:33814 "EHLO mail-qk0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754395AbcDFCii (ORCPT ); Tue, 5 Apr 2016 22:38:38 -0400 From: Dom Cote X-Google-Original-From: Dom Cote Date: Tue, 5 Apr 2016 22:38:34 -0400 To: kieran.bingham@linaro.org, jan.kiszka@siemens.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH] Fix issue with dmesg.py and python 3.X Message-ID: <20160406023831.GA12697@iMacLub> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2786 Lines: 79 When using GDB built with python 2.7, Inferior.read_memory (address, length) returns a buffer object. When using GDB built with python 3.X, it returns a memoryview object, which cannot be added to another memoryview object. Replace the addition (+) of 2 python memoryview objects with the addition of 2 'bytes' objects. Create a read_memoryview() function that always return a memoryview object. Change the read_u16 function so it doesn't need to use ord() anymore. Tested with Python 3.4 and gdb 7.7 Signed-off-by: Dom Cote --- scripts/gdb/linux/dmesg.py | 9 +++++---- scripts/gdb/linux/utils.py | 9 +++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/scripts/gdb/linux/dmesg.py b/scripts/gdb/linux/dmesg.py index 927d0d2a3145..96f4732157d8 100644 --- a/scripts/gdb/linux/dmesg.py +++ b/scripts/gdb/linux/dmesg.py @@ -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__(): @@ -53,7 +54,7 @@ class LxDmesg(gdb.Command): text = log_buf[pos + 16:pos + 16 + text_len] time_stamp = utils.read_u64(log_buf[pos:pos + 8]) - for line in memoryview(text).tobytes().splitlines(): + for line in text.splitlines(): gdb.write("[{time:12.6f}] {line}\n".format( time=time_stamp / 1000000000.0, line=line)) diff --git a/scripts/gdb/linux/utils.py b/scripts/gdb/linux/utils.py index 0893b326a28b..c2b779e7bd26 100644 --- a/scripts/gdb/linux/utils.py +++ b/scripts/gdb/linux/utils.py @@ -87,11 +87,16 @@ def get_target_endianness(): return target_endianness +# Compat between GDB built with python 2.7 vs 3.X +def read_memoryview(inf, start, length): + return memoryview(inf.read_memory(start, length)) + + def read_u16(buffer): if get_target_endianness() == LITTLE_ENDIAN: - return ord(buffer[0]) + (ord(buffer[1]) << 8) + return buffer[0] + (buffer[1] << 8) else: - return ord(buffer[1]) + (ord(buffer[0]) << 8) + return buffer[1] + (buffer[0] << 8) def read_u32(buffer): -- 1.9.1