lkml.org 
[lkml]   [2013]   [Jan]   [29]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH v5 07/20] scripts/gdb: Add lx_modvar convenience function
Date
This function looks up and returns global module variables. Gdb is only
aware of their types and section offsets, not their absolute addresses.
The function either searches the symbol table of a specified module or
it derives the module name from the object file of the current frame.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
scripts/gdb/module.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/utils.py | 7 +++++++
2 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/scripts/gdb/module.py b/scripts/gdb/module.py
index e309c0a..333729a 100644
--- a/scripts/gdb/module.py
+++ b/scripts/gdb/module.py
@@ -12,6 +12,8 @@
#

import gdb
+import os
+import string

from utils import *

@@ -56,3 +58,49 @@ class LxModule(gdb.Function):
raise gdb.GdbError("Unable to find MODULE " + mod_name)

LxModule()
+
+
+class LxModvar(gdb.Function):
+ __doc__ = "Return global variable of a module.\n" \
+ "\n" \
+ "$lx_modvar(\"VAR\"[, \"MODULE\"]): Return the global variable called VAR that is\n" \
+ "defined by given MODULE. If MODULE is omitted, the current frame is used to\n" \
+ "try finding the corresponding module name."
+
+ def __init__(self):
+ super(LxModvar, self).__init__("lx_modvar")
+
+ def _lookup_mod_symbol(self, module, var_name):
+ char_ptr_type = get_char_type().pointer()
+ for i in range(0, int(module['num_symtab'])):
+ symtab_entry = module['symtab'][i]
+ idx = int(symtab_entry['st_name'])
+ synname_addr = module['strtab'][idx].address
+ symname = synname_addr.cast(char_ptr_type)
+ if symname.string() == var_name:
+ return symtab_entry['st_value']
+ return None
+
+ def invoke(self, var_name, mod_name = None):
+ if (mod_name == None):
+ obj = gdb.selected_frame().function().symtab.objfile
+ mod_name = string.replace(
+ os.path.basename(obj.filename), ".ko", "")
+ module = find_module_by_name(mod_name)
+ if module == None:
+ raise gdb.GdbError("Current frame does not " \
+ "belong to a module")
+ else:
+ mod_name = mod_name.string()
+ module = find_module_by_name(mod_name)
+ if module == None:
+ raise gdb.GdbError("Unable to find MODULE " +
+ mod_name)
+ var_name = var_name.string()
+ var_addr = self._lookup_mod_symbol(module, var_name)
+ if var_addr == None:
+ raise gdb.GdbError("Unable to find VAR " + var_name)
+ var = gdb.parse_and_eval(var_name)
+ return var_addr.cast(var.type.pointer()).dereference()
+
+LxModvar()
diff --git a/scripts/gdb/utils.py b/scripts/gdb/utils.py
index d1f6f12..13f903a 100644
--- a/scripts/gdb/utils.py
+++ b/scripts/gdb/utils.py
@@ -45,6 +45,13 @@ def get_long_type():
return long_type.get_type()


+char_type = CachedType("char")
+
+def get_char_type():
+ global char_type
+ return char_type.get_type()
+
+
def offset_of(typeobj, field):
element = gdb.Value(0).cast(typeobj)
return int(str(element[field].address).split()[0], 16)
--
1.7.3.4


\
 
 \ /
  Last update: 2013-01-29 14:21    [W:2.437 / U:0.024 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site