Commit 3a237609 authored by Sasha Goldshtein's avatar Sasha Goldshtein

python: Allow module=None when resolving kernel symbols

An earlier commit 7b35436a introduced an encode call designed to
support Python 3.x when passing the module name to `bcc_symcache_resolve_name`.
This breaks current code because the module may be None when
resolving kernel symbols, e.g. using the `BPF.ksymname` API.

This commit fixes the regression and introduces a test for `ksym`
and `ksymname` that would have caught it in the first place.

Resolves #1054.
parent f387f5dd
...@@ -71,7 +71,7 @@ class SymbolCache(object): ...@@ -71,7 +71,7 @@ class SymbolCache(object):
def resolve_name(self, module, name): def resolve_name(self, module, name):
addr = ct.c_ulonglong() addr = ct.c_ulonglong()
if lib.bcc_symcache_resolve_name( if lib.bcc_symcache_resolve_name(
self.cache, module.encode("ascii"), self.cache, module.encode("ascii") if module else None,
name.encode("ascii"), ct.pointer(addr)) < 0: name.encode("ascii"), ct.pointer(addr)) < 0:
return -1 return -1
return addr.value return addr.value
......
...@@ -4,9 +4,28 @@ ...@@ -4,9 +4,28 @@
import os import os
import subprocess import subprocess
from bcc import SymbolCache from bcc import SymbolCache, BPF
from unittest import main, TestCase from unittest import main, TestCase
class TestKSyms(TestCase):
def grab_sym(self):
# Grab the first symbol in kallsyms that has type 't'.
with open("/proc/kallsyms") as f:
for line in f:
(addr, t, name) = line.strip().split()
if t == "t":
return (addr, name)
def test_ksymname(self):
sym = BPF.ksymname("__kmalloc")
self.assertIsNotNone(sym)
self.assertNotEqual(sym, 0)
def test_ksym(self):
(addr, name) = self.grab_sym()
sym = BPF.ksym(int(addr, 16))
self.assertEqual(sym, name)
class Harness(TestCase): class Harness(TestCase):
def setUp(self): def setUp(self):
self.build_command() self.build_command()
......
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