Skip to content

Commit

Permalink
sysctl: prints sysctl entries
Browse files Browse the repository at this point in the history
Orabug: 37191878
Signed-off-by: Richard Li <[email protected]>
  • Loading branch information
richl9 authored and brenns10 committed Dec 16, 2024
1 parent 5477af2 commit 9e32869
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions drgn_tools/sysctl.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright (c) 2023, Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
"""
Helpers for sysctl.
Helpers for sysctl. Further improvements are still needed to cover more corner cases and stay consistent with sysctl command output.
"""
import argparse
from stat import S_ISDIR
from stat import S_ISLNK
from types import MappingProxyType
Expand All @@ -18,6 +19,11 @@
from drgn import Program
from drgn.helpers.linux.rbtree import rbtree_inorder_for_each

from drgn_tools.corelens import CorelensModule


MAX_ELEMENTS = 100


def get_sysctl_table(prog: Program) -> MappingProxyType:
"""
Expand Down Expand Up @@ -157,8 +163,16 @@ def get_data_entry(prog: Program, ct: Object) -> Any:
except LookupError:
pass

sys_text_info = {
"proc_dostring",
"proc_dostring_coredump",
"proc_do_uts_string",
"devkmsg_sysctl_set_loglvl",
"proc_do_large_bitmap",
"proc_watchdog_cpumask",
}
# for the cases where the data encoded is a text string
if phandler_name == "proc_dostring":
if phandler_name in sys_text_info:
return __decode_sysctl_string(prog.read(ct.data, maxlen))
if phandler_name == "cdrom_sysctl_info":
idstr = __decode_sysctl_string(prog.read(ct.data, maxlen))
Expand Down Expand Up @@ -205,14 +219,23 @@ def get_data_entry(prog: Program, ct: Object) -> Any:
iv = iv // 1000
out.append(iv)
data += interval
# print first 5 elements
if len(out) > 5:
out = out[:5] + ["... %d more elements" % (len(out) - 5)]

return out


def __decode_sysctl_string(data: bytes) -> str:
"""Decode bytes to readable string"""

bytes_before_nul = data.split(b"\x00", 1)[0]
return bytes_before_nul.decode("ascii", errors="backslashreplace")
return bytes_before_nul.decode("ascii", errors="ignore")


class SysCtl(CorelensModule):
"""
Print out sysctl entries.
"""

name = "sysctl"

def run(self, prog: Program, args: argparse.Namespace) -> None:
print_sysctl_table(prog)

0 comments on commit 9e32869

Please sign in to comment.