linux: kmsg plugin: improve return types

This commit is contained in:
Gustavo Moreira
2025-01-30 11:33:28 +11:00
parent 6dac7195dc
commit 3bd1c70a9b
+10 -7
View File
@@ -5,7 +5,7 @@ import re
import logging
from abc import ABC, abstractmethod
from enum import Enum
from typing import Generator, Iterator, List, Tuple
from typing import Generator, Iterator, List, Tuple, Union
from volatility3.framework import (
class_subclasses,
@@ -135,10 +135,11 @@ class ABCKmsg(ABC):
bool: True if the kernel being analyzed fulfill the class requirements.
"""
def get_string(self, addr: int, length: int) -> str:
def get_string(self, addr: int, length: int) -> Union[str, None]:
layer = self._context.layers[self.layer_name]
if not layer.is_valid(addr, length):
return ""
vollog.error("Failed to read log record at address 0x%x", addr)
return None
txt = layer.read(addr, length)
@@ -268,7 +269,7 @@ class Kmsg_3_5_to_3_11(ABCKmsg):
def _get_log_struct_name(self):
return "log"
def get_text_from_log(self, msg) -> str:
def get_text_from_log(self, msg) -> Union[str, None]:
log_struct_name = self._get_log_struct_name()
log_struct_size = self.vmlinux.get_type(log_struct_name).size
msg_offset = msg.vol.offset + log_struct_size
@@ -277,7 +278,8 @@ class Kmsg_3_5_to_3_11(ABCKmsg):
def get_log_lines(self, msg) -> Generator[str, None, None]:
if msg.text_len > 0:
text = self.get_text_from_log(msg)
yield from text.splitlines()
if text:
yield from text.splitlines()
def get_dict_lines(self, msg) -> Generator[str, None, None]:
if msg.dict_len == 0:
@@ -412,7 +414,7 @@ class Kmsg_5_10_to_(ABCKmsg):
def symtab_checks(cls, vmlinux) -> bool:
return vmlinux.has_symbol("prb")
def get_text_from_data_ring(self, text_data_ring, desc, info) -> str:
def get_text_from_data_ring(self, text_data_ring, desc, info) -> Union[str, None]:
text_data_sz = text_data_ring.size_bits
text_data_mask = 1 << text_data_sz
@@ -440,7 +442,8 @@ class Kmsg_5_10_to_(ABCKmsg):
def get_log_lines(self, text_data_ring, desc, info) -> Generator[str, None, None]:
text = self.get_text_from_data_ring(text_data_ring, desc, info)
yield from text.splitlines()
if text:
yield from text.splitlines()
def get_dict_lines(self, info) -> Generator[str, None, None]:
dict_text = utility.array_to_string(info.dev_info.subsystem)