def call(self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]: try: for obj in objs: lhs = eval(self.lhs_code, {'__builtins__': None}, {'obj': obj}) rhs = eval(self.rhs_code, {'__builtins__': None}, {'obj': obj}) if not isinstance(lhs, drgn.Object): raise sdb.CommandInvalidInputError( self.name, "left hand side has unsupported type ({})".format( type(lhs).__name__)) if isinstance(rhs, str): lhs = lhs.string_().decode("utf-8") elif isinstance(rhs, int): rhs = drgn.Object(self.prog, type=lhs.type_, value=rhs) elif isinstance(rhs, bool): pass elif isinstance(rhs, drgn.Object): pass else: raise sdb.CommandInvalidInputError( self.name, "right hand side has unsupported type ({})".format( type(rhs).__name__)) if eval("lhs {} rhs".format(self.compare), {'__builtins__': None}, { 'lhs': lhs, 'rhs': rhs }): yield obj except (AttributeError, TypeError, ValueError) as err: raise sdb.CommandError(self.name, str(err))
def __init__(self, prog: drgn.Program, args: str = "", name: str = "_") -> None: super().__init__(prog, args, name) if not self.args.expr: self.parser.error("the following arguments are required: expr") index = None operators = ["==", "/=", ">", "<", ">=", "<="] for operator in operators: try: index = self.args.expr.index(operator) # Use the first comparison operator we find. break except ValueError: continue if index is None: # If the comparison index is still None, this means not # operator was found. This is an error. raise sdb.CommandInvalidInputError( self.name, "comparison operator is missing") if index == 0: # If the comparison index is found to be 0, this means # there's no left hand side of the comparison to compare the # right hand side to. This is an error. raise sdb.CommandInvalidInputError( self.name, "left hand side of expression is missing") if index == len(self.args.expr) - 1: # If the index is found to be at the very end of the list, # this means there's no right hand side of the comparison to # compare the left hand side to. This is an error. raise sdb.CommandInvalidInputError( self.name, "right hand side of expression is missing") try: self.lhs_code = compile(" ".join(self.args.expr[:index]), "<string>", "eval") self.rhs_code = compile(" ".join(self.args.expr[index + 1:]), "<string>", "eval") except SyntaxError as err: raise sdb.CommandEvalSyntaxError(self.name, err) self.compare = self.args.expr[index] if self.compare == "/=": self.compare = "!="
def __init__(self, args: Optional[List[str]] = None, name: str = "_") -> None: super().__init__(args, name) self.expr = self.args.expr[0].split() index = None operators = ["==", "!=", ">", "<", ">=", "<="] for operator in operators: try: index = self.expr.index(operator) # Use the first comparison operator we find. break except ValueError: continue if index is None: # If the comparison index is still None, this means not # operator was found. This is an error. raise sdb.CommandInvalidInputError( self.name, "comparison operator is missing") if index == 0: # If the comparison index is found to be 0, this means # there's no left hand side of the comparison to compare the # right hand side to. This is an error. raise sdb.CommandInvalidInputError( self.name, "left hand side of expression is missing") if index == len(self.expr) - 1: # If the index is found to be at the very end of the list, # this means there's no right hand side of the comparison to # compare the left hand side to. This is an error. raise sdb.CommandInvalidInputError( self.name, "right hand side of expression is missing") try: self.lhs_code = compile(" ".join(self.expr[:index]), "<string>", "eval") self.rhs_code = compile(" ".join(self.expr[index + 1:]), "<string>", "eval") except SyntaxError as err: raise sdb.CommandEvalSyntaxError(self.name, err) self.compare = self.expr[index]
def _call(self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]: for obj in objs: yield obj for addr in self.args.addrs: try: value_ = int(addr, 0) except ValueError: raise sdb.CommandInvalidInputError(self.name, addr) yield sdb.create_object("void *", value_)
def __pp_parse_args(self ) -> (str, List[str], Dict[str, Callable[[Any], str]]): fields = SplKmemCaches.DEFAULT_FIELDS if self.args.o: # # HACK: Until we have a proper lexer for SDB we can # only pass the comma-separated list as a # string (e.g. quoted). Until this is fixed # we make sure to unquote such strings. # if self.args.o[0] == '"' and self.args.o[-1] == '"': self.args.o = self.args.o[1:-1] fields = self.args.o.split(",") elif self.args.v: fields = list(SplKmemCaches.FIELDS.keys()) for field in fields: if field not in SplKmemCaches.FIELDS: raise sdb.CommandError( self.name, "'{:s}' is not a valid field".format(field)) sort_field = "" if self.args.s: if self.args.s not in fields: msg = f"'{self.args.s}' is not in field set ({', '.join(fields)})" raise sdb.CommandInvalidInputError(self.name, textwrap.fill(msg, width=80)) sort_field = self.args.s else: # # If a sort_field hasn't been specified try the following # defaults. If these are not part of the field-set then # sort by the first field in the set. # for field in self.DEFAULT_SORT_FIELDS: if field in fields: sort_field = field break if not sort_field: sort_field = fields[0] formatters = { "active_memory": size_nicenum, "total_memory": size_nicenum } if self.args.p: formatters = {} return sort_field, fields, formatters
def no_input(self) -> Iterable[drgn.Object]: # # If this command is not the last term of a pipeline (which # means that the pretty-printing code won't run) we still # need the `-s` option to work in order to sort the output # that will be the input of the next command in the pipeline. # if self.args.s and not self.islast: if self.args.s not in Slabs.FIELDS.keys(): raise sdb.CommandInvalidInputError( self.name, f"'{self.args.s}' is not a valid field") yield from sorted( self.__no_input_iterator(), key=Slabs.FIELDS[self.args.s], reverse=(self.args.s not in Slabs.DEFAULT_INCREASING_ORDER_FIELDS)) else: yield from self.__no_input_iterator()
def __pp_parse_args(self) -> Tuple[str, List[str], Dict[str, Any]]: fields = self.DEFAULT_FIELDS if self.args.o: fields = self.args.o.split(",") elif self.args.v: fields = list(Slabs.FIELDS.keys()) for field in fields: if field not in self.FIELDS: raise sdb.CommandError( self.name, "'{:s}' is not a valid field".format(field)) sort_field = "" if self.args.s: if self.args.s not in fields: msg = f"'{self.args.s}' is not in field set ({', '.join(fields)})" raise sdb.CommandInvalidInputError( self.name, textwrap.fill(msg, width=80)) sort_field = self.args.s else: # # If a sort_field hasn't been specified try the following # defaults. If these are not part of the field-set then # sort by the first field in the set. # for field in self.DEFAULT_SORT_FIELDS: if field in fields: sort_field = field break if not sort_field: sort_field = fields[0] formatters = { "total_memory": size_nicenum, "active_memory": size_nicenum } if self.args.p: formatters = {} return sort_field, fields, formatters