Skip to content

Commit 18a7787

Browse files
committed
Add a whatis command to sdb
1 parent db1b967 commit 18a7787

File tree

8 files changed

+89
-1
lines changed

8 files changed

+89
-1
lines changed

.github/scripts/install-drgn.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# build/install of "drgn" will fail below.
66
#
77
sudo apt update
8-
sudo apt install bison flex libelf-dev libdw-dev libomp5 libomp-dev
8+
sudo apt install bison flex libelf-dev=0.170-0.4ubuntu0.1 libdw-dev=0.170-0.4ubuntu0.1 libomp5 libomp-dev
99

1010
git clone https://github.com/osandov/drgn.git
1111

sdb/commands/linux/whatis.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# Copyright 2020 Delphix
3+
# Copyright 2021 Datto Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# pylint: disable=missing-docstring
19+
20+
import argparse
21+
from typing import Iterable
22+
23+
import drgn
24+
import drgn.helpers.linux.mm as mm
25+
import sdb
26+
27+
28+
def lookup_in_kmem(obj: drgn.Object) -> None:
29+
pfn = mm.virt_to_pfn(sdb.get_prog(), obj)
30+
page = mm.pfn_to_page(pfn)
31+
try:
32+
cache = page.slab_cache
33+
cache_nm = cache.name.string_().decode('utf-8')
34+
print(f"{hex(int(obj))} is allocated from {cache_nm}")
35+
except drgn.FaultError:
36+
print(f"{hex(int(obj))} does not map to a kmem_cache")
37+
38+
39+
class WhatIs(sdb.Command):
40+
"""
41+
Print the name of the kmem cache from which the address is allocated.
42+
43+
DESCRIPTION
44+
The address can be specified as an argument, or
45+
passed through a pipe.
46+
47+
EXAMPLES
48+
Determine the kmem_cache for a given address:
49+
50+
sdb> whatis 0xfffffe06596e21b8
51+
0xfffffe06596e21b8 is allocated from dmu_buf_impl_t
52+
53+
Determine the kmem_cache of address passed through a
54+
pipe:
55+
56+
sdb> dbuf |head 1 |deref |member db_buf |whatis
57+
0xffff8e804368ac80 is allocated from arc_buf_t
58+
"""
59+
60+
names = ["whatis"]
61+
62+
@classmethod
63+
def _init_parser(cls, name: str) -> argparse.ArgumentParser:
64+
parser = super()._init_parser(name)
65+
parser.add_argument("address", nargs="*", metavar="<address>")
66+
return parser
67+
68+
def _call(self, objs: Iterable[drgn.Object]) -> None:
69+
for obj in objs:
70+
lookup_in_kmem(obj)
71+
for addr in self.args.address:
72+
try:
73+
lookup_in_kmem(drgn.Object(sdb.get_prog(), "void *", int(addr, 16)))
74+
except ValueError:
75+
print(f"{addr} is not a valid address")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0xffffa08943a90050 is allocated from arc_buf_t
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0xf987kkbbh is not a valid address
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0xffff does not map to a kmem_cache
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0xffffa0888c766000 is allocated from arc_buf_hdr_t_full
2+
0xffffa089407ca870 is allocated from dmu_buf_impl_t
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0xffffa089407ca870 is allocated from dmu_buf_impl_t

tests/integration/test_linux_generic.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
"threads | count",
9595
'threads | filter \'obj.comm == "java"\' | threads',
9696
"thread",
97+
98+
# whatis
99+
"dbuf |head 1 |deref |member db_buf |whatis",
100+
"whatis 0xffffa089407ca870",
101+
"whatis 0xffffa0888c766000 0xffffa089407ca870",
102+
"whatis 0xffff",
103+
"whatis 0xf987kkbbh"
97104
]
98105

99106
STRIPPED_POS_CMDS = [

0 commit comments

Comments
 (0)