xref: /openbmc/linux/scripts/gdb/linux/config.py (revision 8fe1ee58)
190cf83dbSStephen Boyd# SPDX-License-Identifier: GPL-2.0
290cf83dbSStephen Boyd#
390cf83dbSStephen Boyd# Copyright 2019 Google LLC.
490cf83dbSStephen Boyd
590cf83dbSStephen Boydimport gdb
690cf83dbSStephen Boydimport zlib
790cf83dbSStephen Boyd
890cf83dbSStephen Boydfrom linux import utils
990cf83dbSStephen Boyd
1090cf83dbSStephen Boyd
1190cf83dbSStephen Boydclass LxConfigDump(gdb.Command):
1290cf83dbSStephen Boyd    """Output kernel config to the filename specified as the command
1390cf83dbSStephen Boyd       argument. Equivalent to 'zcat /proc/config.gz > config.txt' on
1490cf83dbSStephen Boyd       a running target"""
1590cf83dbSStephen Boyd
1690cf83dbSStephen Boyd    def __init__(self):
1790cf83dbSStephen Boyd        super(LxConfigDump, self).__init__("lx-configdump", gdb.COMMAND_DATA,
1890cf83dbSStephen Boyd                                           gdb.COMPLETE_FILENAME)
1990cf83dbSStephen Boyd
2090cf83dbSStephen Boyd    def invoke(self, arg, from_tty):
2190cf83dbSStephen Boyd        if len(arg) == 0:
2290cf83dbSStephen Boyd            filename = "config.txt"
2390cf83dbSStephen Boyd        else:
2490cf83dbSStephen Boyd            filename = arg
2590cf83dbSStephen Boyd
2690cf83dbSStephen Boyd        try:
278fe1ee58SKuan-Ying Lee            py_config_ptr = gdb.parse_and_eval("&kernel_config_data")
288fe1ee58SKuan-Ying Lee            py_config_ptr_end = gdb.parse_and_eval("&kernel_config_data_end")
298fe1ee58SKuan-Ying Lee            py_config_size = py_config_ptr_end - py_config_ptr
3090cf83dbSStephen Boyd        except gdb.error as e:
3190cf83dbSStephen Boyd            raise gdb.GdbError("Can't find config, enable CONFIG_IKCONFIG?")
3290cf83dbSStephen Boyd
3390cf83dbSStephen Boyd        inf = gdb.inferiors()[0]
3490cf83dbSStephen Boyd        zconfig_buf = utils.read_memoryview(inf, py_config_ptr,
3590cf83dbSStephen Boyd                                            py_config_size).tobytes()
3690cf83dbSStephen Boyd
3790cf83dbSStephen Boyd        config_buf = zlib.decompress(zconfig_buf, 16)
3890cf83dbSStephen Boyd        with open(filename, 'wb') as f:
3990cf83dbSStephen Boyd            f.write(config_buf)
4090cf83dbSStephen Boyd
4190cf83dbSStephen Boyd        gdb.write("Dumped config to " + filename + "\n")
4290cf83dbSStephen Boyd
4390cf83dbSStephen Boyd
4490cf83dbSStephen BoydLxConfigDump()
45