From 28425cc493677bd0d198a382437875bea08ef200 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sat, 17 Sep 2022 13:26:10 -0700 Subject: [PATCH] add proc-vmallocinfo parser --- docs/parsers/proc_vmallocinfo.md | 126 ++++++++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_vmallocinfo.py | 195 +++++++++++++++++++++++++++++++ man/jc.1 | 5 + 4 files changed, 327 insertions(+) create mode 100644 docs/parsers/proc_vmallocinfo.md create mode 100644 jc/parsers/proc_vmallocinfo.py diff --git a/docs/parsers/proc_vmallocinfo.md b/docs/parsers/proc_vmallocinfo.md new file mode 100644 index 00000000..1eb11bf0 --- /dev/null +++ b/docs/parsers/proc_vmallocinfo.md @@ -0,0 +1,126 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_vmallocinfo + +jc - JSON Convert `/proc/vmallocinfo` file parser + +This parser will attempt to convert number values to integers. If that is +not desired, please use the `--raw` option (cli) or `raw=True` argument +(module). + +Usage (cli): + + $ cat /proc/vmallocinfo | jc --proc + +or + + $ jc /proc/vmallocinfo + +or + + $ cat /proc/vmallocinfo | jc --proc-vmallocinfo + +Usage (module): + + import jc + result = jc.parse('proc', proc_vmallocinfo_file) + +or + + import jc + result = jc.parse('proc_vmallocinfo', proc_vmallocinfo_file) + +Schema: + + [ + { + "start": string, + "end": string, + "size": integer, + "caller": string, + "options": [ + string + ], + "phys": string + "pages": integer, + "N": integer + } + ] + +Examples: + + $ cat /proc/vmallocinfo | jc --proc -p + [ + { + "start": "0xffffb3c1c0000000", + "end": "0xffffb3c1c0005000", + "size": 20480, + "caller": "map_irq_stack+0x93/0xe0", + "options": [ + "vmap" + ], + "phys": "0x00000000bfefe000" + }, + { + "start": "0xffffb3c1c0005000", + "end": "0xffffb3c1c0007000", + "size": 8192, + "caller": "acpi_os_map_iomem+0x1ac/0x1c0", + "options": [ + "ioremap" + ], + "phys": "0x00000000bfeff000" + }, + ... + ] + + $ cat /proc/vmallocinfo | jc --proc -p -r + [ + { + "start": "0xffffb3c1c0000000", + "end": "0xffffb3c1c0005000", + "size": "20480", + "caller": "map_irq_stack+0x93/0xe0", + "options": [ + "vmap" + ], + "phys": "0x00000000bfefe000" + }, + { + "start": "0xffffb3c1c0005000", + "end": "0xffffb3c1c0007000", + "size": "8192", + "caller": "acpi_os_map_iomem+0x1ac/0x1c0", + "options": [ + "ioremap" + ], + "phys": "0x00000000bfeff000" + }, + ... + ] + + + +### parse + +```python +def parse(data: str, raw: bool = False, quiet: bool = False) -> List[Dict] +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + +Returns: + + List of Dictionaries. Raw or processed structured data. + +### Parser Information +Compatibility: linux + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/lib.py b/jc/lib.py index cee0f3e6..88fa425a 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -109,6 +109,7 @@ parsers = [ 'proc-swaps', 'proc-uptime', 'proc-version', + 'proc-vmallocinfo', 'proc-pid-numa-maps', 'ps', 'route', diff --git a/jc/parsers/proc_vmallocinfo.py b/jc/parsers/proc_vmallocinfo.py new file mode 100644 index 00000000..b2044264 --- /dev/null +++ b/jc/parsers/proc_vmallocinfo.py @@ -0,0 +1,195 @@ +"""jc - JSON Convert `/proc/vmallocinfo` file parser + +This parser will attempt to convert number values to integers. If that is +not desired, please use the `--raw` option (cli) or `raw=True` argument +(module). + +Usage (cli): + + $ cat /proc/vmallocinfo | jc --proc + +or + + $ jc /proc/vmallocinfo + +or + + $ cat /proc/vmallocinfo | jc --proc-vmallocinfo + +Usage (module): + + import jc + result = jc.parse('proc', proc_vmallocinfo_file) + +or + + import jc + result = jc.parse('proc_vmallocinfo', proc_vmallocinfo_file) + +Schema: + + [ + { + "start": string, + "end": string, + "size": integer, + "caller": string, + "options": [ + string + ], + "phys": string + "pages": integer, + "N": integer + } + ] + +Examples: + + $ cat /proc/vmallocinfo | jc --proc -p + [ + { + "start": "0xffffb3c1c0000000", + "end": "0xffffb3c1c0005000", + "size": 20480, + "caller": "map_irq_stack+0x93/0xe0", + "options": [ + "vmap" + ], + "phys": "0x00000000bfefe000" + }, + { + "start": "0xffffb3c1c0005000", + "end": "0xffffb3c1c0007000", + "size": 8192, + "caller": "acpi_os_map_iomem+0x1ac/0x1c0", + "options": [ + "ioremap" + ], + "phys": "0x00000000bfeff000" + }, + ... + ] + + $ cat /proc/vmallocinfo | jc --proc -p -r + [ + { + "start": "0xffffb3c1c0000000", + "end": "0xffffb3c1c0005000", + "size": "20480", + "caller": "map_irq_stack+0x93/0xe0", + "options": [ + "vmap" + ], + "phys": "0x00000000bfefe000" + }, + { + "start": "0xffffb3c1c0005000", + "end": "0xffffb3c1c0007000", + "size": "8192", + "caller": "acpi_os_map_iomem+0x1ac/0x1c0", + "options": [ + "ioremap" + ], + "phys": "0x00000000bfeff000" + }, + ... + ] +""" +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/vmallocinfo` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + hidden = True + + +__version__ = info.version + + +def _process(proc_data: List[Dict]) -> List[Dict]: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (List of Dictionaries) raw structured data to process + + Returns: + + List of Dictionaries. Structured to conform to the schema. + """ + for entry in proc_data: + for key in entry: + if isinstance(entry[key], str): + try: + entry[key] = int(entry[key]) + except Exception: + pass + + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> List[Dict]: + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + List of Dictionaries. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: List = [] + output_line: Dict = {} + + if jc.utils.has_data(data): + + for line in filter(None, data.splitlines()): + area, size, details = line.split(maxsplit=2) + start, end = area.split('-', maxsplit=1) + detail_split = details.split() + caller = '' + options: List = [] + + + if details == 'unpurged vm_area': + caller = 'unpurged vm_area' + + else: + caller = detail_split[0] + for item in detail_split[1:]: + if '=' in item: + key, val = item.split('=') + output_line.update({key: val}) + else: + options.append(item) + + output_line = { + 'start': start, + 'end': end, + 'size': size, + 'caller': caller or None, + 'options': options + } + + if output_line: + raw_output.append(output_line) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 7d74df22..d29f7b9a 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -530,6 +530,11 @@ PLIST file parser \fB--proc-version\fP `/proc/version` file parser +.TP +.B +\fB--proc-vmallocinfo\fP +`/proc/vmallocinfo` file parser + .TP .B \fB--proc-pid-numa-maps\fP