Commit 2e2b26d8 authored by Brenden Blanco's avatar Brenden Blanco

Add name to address ksym helper

Add a helper to translate from kernel name to symbol. As part of this,
slightly refactor the ksym globals (which are internal implementation
and not part of the api) so that there is one array of tuples instead of
two arrays.
Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent 54797f8b
...@@ -33,8 +33,8 @@ open_uprobes = {} ...@@ -33,8 +33,8 @@ open_uprobes = {}
tracefile = None tracefile = None
TRACEFS = "/sys/kernel/debug/tracing" TRACEFS = "/sys/kernel/debug/tracing"
KALLSYMS = "/proc/kallsyms" KALLSYMS = "/proc/kallsyms"
ksym_addrs = [] ksyms = []
ksym_names = [] ksym_names = {}
ksym_loaded = 0 ksym_loaded = 0
@atexit.register @atexit.register
...@@ -629,7 +629,7 @@ class BPF(object): ...@@ -629,7 +629,7 @@ class BPF(object):
@staticmethod @staticmethod
def _load_kallsyms(): def _load_kallsyms():
global ksym_loaded, ksym_addrs, ksym_names global ksym_loaded, ksyms, ksym_names
if ksym_loaded: if ksym_loaded:
return return
try: try:
...@@ -641,19 +641,20 @@ class BPF(object): ...@@ -641,19 +641,20 @@ class BPF(object):
cols = line.split() cols = line.split()
name = cols[2] name = cols[2]
addr = int(cols[0], 16) addr = int(cols[0], 16)
ksym_addrs.append(addr) # keep a mapping of names to ksyms index
ksym_names.append(name) ksym_names[name] = len(ksyms)
ksyms.append((name, addr))
syms.close() syms.close()
ksym_loaded = 1 ksym_loaded = 1
@staticmethod @staticmethod
def _ksym_addr2index(addr): def _ksym_addr2index(addr):
global ksym_addrs global ksyms
start = -1 start = -1
end = len(ksym_addrs) end = len(ksyms)
while end != start + 1: while end != start + 1:
mid = int((start + end) / 2) mid = int((start + end) / 2)
if addr < ksym_addrs[mid]: if addr < ksyms[mid][1]:
end = mid end = mid
else: else:
start = mid start = mid
...@@ -666,12 +667,12 @@ class BPF(object): ...@@ -666,12 +667,12 @@ class BPF(object):
Translate a kernel memory address into a kernel function name, which is Translate a kernel memory address into a kernel function name, which is
returned. This is a simple translator that uses /proc/kallsyms. returned. This is a simple translator that uses /proc/kallsyms.
""" """
global ksym_names global ksyms
BPF._load_kallsyms() BPF._load_kallsyms()
idx = BPF._ksym_addr2index(addr) idx = BPF._ksym_addr2index(addr)
if idx == -1: if idx == -1:
return "[unknown]" return "[unknown]"
return ksym_names[idx] return ksyms[idx][0]
@staticmethod @staticmethod
def ksymaddr(addr): def ksymaddr(addr):
...@@ -681,13 +682,27 @@ class BPF(object): ...@@ -681,13 +682,27 @@ class BPF(object):
instruction offset as a hexidecimal number, which is returned as a instruction offset as a hexidecimal number, which is returned as a
string. This is a simple translator that uses /proc/kallsyms. string. This is a simple translator that uses /proc/kallsyms.
""" """
global ksym_addrs, ksym_names global ksyms
BPF._load_kallsyms() BPF._load_kallsyms()
idx = BPF._ksym_addr2index(addr) idx = BPF._ksym_addr2index(addr)
if idx == -1: if idx == -1:
return "[unknown]" return "[unknown]"
offset = int(addr - ksym_addrs[idx]) offset = int(addr - ksyms[idx][1])
return ksym_names[idx] + hex(offset) return ksyms[idx][0] + hex(offset)
@staticmethod
def ksymname(name):
"""ksymname(name)
Translate a kernel name into an address. This is the reverse of
ksymaddr. Returns -1 when the function name is unknown."""
global ksyms, ksym_names
BPF._load_kallsyms()
idx = ksym_names.get(name, -1)
if idx == -1:
return 0
return ksyms[idx][1]
@staticmethod @staticmethod
def num_open_kprobes(): def num_open_kprobes():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment