diff options
Diffstat (limited to '')
-rwxr-xr-x | bin/leb128 | 36 | ||||
-rwxr-xr-x | bin/scotch | 71 | ||||
-rwxr-xr-x | bin/virttop | 2 |
3 files changed, 61 insertions, 48 deletions
@@ -1,16 +1,11 @@ -#!/bin/python3 +#!/usr/bin/env python3 +"""a leb128 decoder,encoder""" import argparse -import signal -import sys - - -def SigHandler_SIGINT(signum, frame): - print() - sys.exit(0) def devibytes(val): + """A custom arg handler, just to handle bytes in hex format""" ret = [] for byte in val.split(","): ret.append(int(byte, 16)) @@ -18,6 +13,7 @@ def devibytes(val): def LEB128UnsignedDecode(bytelist): + """LEB128 unsiged decoder""" result = 0 shift = 0 for byte in bytelist: @@ -29,6 +25,7 @@ def LEB128UnsignedDecode(bytelist): def LEB128SignedDecode(bytelist): + """LEB128 signed decoder""" result = 0 shift = 0 for byte in bytelist: @@ -43,9 +40,10 @@ def LEB128SignedDecode(bytelist): def LEB128UnsignedEncode(int_val): + """LEB128 signed encoder""" if int_val < 0: raise Exception("value must not be negative") - elif int_val == 0: + if int_val == 0: return bytes([0]) byte_array = bytearray() while int_val: @@ -57,6 +55,7 @@ def LEB128UnsignedEncode(int_val): def LEB128SignedEncode(int_val): + """LEB128 signed encoder""" byte_array = bytearray() while True: byte = int_val & 0x7F @@ -70,7 +69,10 @@ def LEB128SignedEncode(int_val): return byte_array -class Argparser(object): +# pylint: disable=too-few-public-methods +class Argparser: + """CLI args""" + def __init__(self): parser = argparse.ArgumentParser() parser.add_argument("--se", type=int, help="leb128 signed encode") @@ -88,11 +90,8 @@ class Argparser(object): self.args = parser.parse_args() -# write code here - - -def premain(): - signal.signal(signal.SIGINT, SigHandler_SIGINT) +def main(): + """entrypoint""" argparser = Argparser() # here if argparser.args.se: @@ -111,12 +110,5 @@ def premain(): print(LEB128UnsignedDecode(argparser.args.ud)) -def main(): - try: - premain() - except Exception as e: - print(e) - - if __name__ == "__main__": main() @@ -1,6 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +"""A strace wrapper with colors.""" +import enum import sys import subprocess @@ -478,7 +480,10 @@ syscall_set = set( ) -class Color: +# pylint: disable=too-few-public-methods +class Color(enum.EnumType): + """Color enumes""" + bold = "\033[1m" faint = "\033[2m" italic = "\033[3m" @@ -506,41 +511,57 @@ class Color: def call_from_shell_list(command_list): - if sys.version_info < (3, 7): - return subprocess.run(command_list, stdout=subprocess.PIPE) - else: - return subprocess.run(command_list, capture_output=True) + """run a shell command""" + # we explicitly dont want to check for a non-zero return code + # since we sometimes use strace to debuf failing applications + return subprocess.run(command_list, capture_output=True) def main(): + """entrypoint""" if len(sys.argv) < 2: print("you want to run something right?\nright?") sys.exit(1) args = sys.argv[1:] - args.insert(0, "strace") + args.insert(0, "/usr/bin/strace") result = call_from_shell_list(args) lines = result.stderr.decode("utf-8").split("\n") - end_line = lines[-2] - lines = lines[:-2] - for line in lines: - if line[0 : line.find("(")] in syscall_set: - syscall = line[0 : line.find("(")] - sysargs = line[line.find("(") + 1 : line.find(")")].split() - exitvalue = line[line.find(")") + 1 :] - print(Color.one + syscall, end=" ") - print(Color.two, end=" ") - sysargs = list(filter(None, sysargs)) - for arg in sysargs: - if arg.find("|") > 0: - print(Color.five, arg, end=" ") - else: - print(arg, end=" ") - print(Color.three + exitvalue + Color.reset) - else: - # leave regular stdout/stderr alone. we only want syscalls + if not sys.stdout.isatty(): + for line in lines: print(line) - print(Color.reset + Color.bold + Color.nine + end_line + Color.reset) + else: + end_line = lines[-2] + lines = lines[:-2] + for line in lines: + # this is here to support the -i option + if line[0] == "[": + idx = line.find("]") + if idx - 1 < 16: + print(Color.twelve + line[0 : idx + 1], end=" ") + line = line[idx + 2 :] + if line[0] == "[": + idx = line.find("]") + if idx - 1 == 16: + print(Color.thirteen + line[0 : idx + 1], end=" ") + line = line[idx + 2 :] + if line[0 : line.find("(")] in syscall_set: + syscall = line[0 : line.find("(")] + sysargs = line[line.find("(") + 1 : line.find(")")].split() + exitvalue = line[line.find(")") + 1 :] + print(Color.one + syscall, end=" ") + print(Color.two, end=" ") + sysargs = list(filter(None, sysargs)) + for arg in sysargs: + if arg.find("|") > 0: + print(Color.five, arg, end=" ") + else: + print(arg, end=" ") + print(Color.three + exitvalue + Color.reset) + else: + # leave regular stdout/stderr alone. we only want syscalls + print(line) + print(Color.reset + Color.bold + Color.nine + end_line + Color.reset) if __name__ == "__main__": diff --git a/bin/virttop b/bin/virttop index 34c93ba..64d53be 100755 --- a/bin/virttop +++ b/bin/virttop @@ -10,9 +10,9 @@ import csv import dataclasses import enum import sys - import typing from xml.dom.minidom import Document + from defusedxml import ElementTree # type:ignore from defusedxml import minidom import libvirt # type:ignore |