Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts/gdb: port to python3 / gdb7.7

I tried to use these scripts in an ubuntu 14.04 host (gdb 7.7 compiled
against python 3.3) but there were several errors.

I believe this patch fixes these issues so that the commands now work (I
tested lx-symbols, lx-dmesg, lx-lsmod).

Main issues that needed to be resolved:

* In python 2 iterators have a "next()" method. In python 3 it is
__next__() instead (so let's just add both).

* In older python versions there was an implicit conversion
in object.__format__() (used when an object is in string.format())
where it was converting the object to str first and then
calling str's __format__(). This has now been removed so
we must explicitly convert to str the objects for which
we need to keep this behavior.

* In dmesg.py: in python 3 log_buf is now a "memoryview" object
which needs to be converted to a string in order to use string
methods like "splitlines()". Luckily memoryview exists in
python 2.7.6 as well, so we can convert log_buf to memoryview
and use the same code in both python 2 and python 3.

This version of the patch has now been tested with gdb 7.7 and both python
3.4 and python 2.7.6 (I think asking for at least python 2.7.6 is a
reasonable requirement instead of complicating the code with version
checks etc).

Signed-off-by: Pantelis Koukousoulas <pktoss@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Ben Widawsky <ben@bwidawsk.net>
Cc: Borislav Petkov <bp@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Pantelis Koukousoulas and committed by
Linus Torvalds
276d97d9 bda1a921

+18 -9
+4 -1
scripts/gdb/linux/cpus.py
··· 82 82 def __iter__(self): 83 83 return self 84 84 85 - def next(self): 85 + def __next__(self): 86 86 while self.bits == 0: 87 87 self.entry += 1 88 88 if self.entry == self.num_entries: ··· 102 102 self.bit += 1 103 103 104 104 return cpu 105 + 106 + def next(self): 107 + return self.__next__() 105 108 106 109 107 110 class PerCpu(gdb.Function):
+2 -1
scripts/gdb/linux/dmesg.py
··· 51 51 continue 52 52 53 53 text_len = utils.read_u16(log_buf[pos + 10:pos + 12]) 54 + text = log_buf[pos + 16:pos + 16 + text_len] 54 55 time_stamp = utils.read_u64(log_buf[pos:pos + 8]) 55 56 56 - for line in log_buf[pos + 16:pos + 16 + text_len].splitlines(): 57 + for line in memoryview(text).tobytes().splitlines(): 57 58 gdb.write("[{time:12.6f}] {line}\n".format( 58 59 time=time_stamp / 1000000000.0, 59 60 line=line))
+6 -3
scripts/gdb/linux/modules.py
··· 30 30 def __iter__(self): 31 31 return self 32 32 33 - def next(self): 33 + def __next__(self): 34 34 entry = self.curr_entry 35 35 if entry != self.end_of_list: 36 36 self.curr_entry = entry['next'] 37 37 return utils.container_of(entry, self.module_ptr_type, "list") 38 38 else: 39 39 raise StopIteration 40 + 41 + def next(self): 42 + return self.__next__() 40 43 41 44 42 45 def find_module_by_name(name): ··· 94 91 gdb.write("{address} {name:<19} {size:>8} {ref}".format( 95 92 address=str(module['module_core']).split()[0], 96 93 name=module['name'].string(), 97 - size=module['core_size'], 98 - ref=ref)) 94 + size=str(module['core_size']), 95 + ref=str(ref))) 99 96 100 97 source_list = module['source_list'] 101 98 t = self._module_use_type.get_type().pointer()
+2 -2
scripts/gdb/linux/symbols.py
··· 73 73 74 74 def _get_module_file(self, module_name): 75 75 module_pattern = ".*/{0}\.ko$".format( 76 - string.replace(module_name, "_", r"[_\-]")) 76 + module_name.replace("_", r"[_\-]")) 77 77 for name in self.module_files: 78 78 if re.match(module_pattern, name) and os.path.exists(name): 79 79 return name ··· 87 87 attrs = sect_attrs['attrs'] 88 88 section_name_to_address = { 89 89 attrs[n]['name'].string() : attrs[n]['address'] 90 - for n in range(sect_attrs['nsections'])} 90 + for n in range(int(sect_attrs['nsections']))} 91 91 args = [] 92 92 for section_name in [".data", ".data..read_mostly", ".rodata", ".bss"]: 93 93 address = section_name_to_address.get(section_name)
+3 -1
scripts/gdb/linux/tasks.py
··· 30 30 def __iter__(self): 31 31 return self 32 32 33 - def next(self): 33 + def __next__(self): 34 34 t = self.curr_task 35 35 if not t or t == self.curr_group: 36 36 self.curr_group = \ ··· 45 45 self.task_ptr_type, "thread_group") 46 46 return t 47 47 48 + def next(self): 49 + return self.__next__() 48 50 49 51 def get_task_by_pid(pid): 50 52 for task in TaskList():
+1 -1
scripts/gdb/linux/utils.py
··· 83 83 elif "big endian" in endian: 84 84 target_endianness = BIG_ENDIAN 85 85 else: 86 - raise gdb.GdgError("unknown endianness '{0}'".format(endian)) 86 + raise gdb.GdgError("unknown endianness '{0}'".format(str(endian))) 87 87 return target_endianness 88 88 89 89