xref: /openbmc/linux/scripts/gdb/linux/config.py (revision 8fe1ee58)
1*90cf83dbSStephen Boyd# SPDX-License-Identifier: GPL-2.0
2*90cf83dbSStephen Boyd#
3*90cf83dbSStephen Boyd# Copyright 2019 Google LLC.
4*90cf83dbSStephen Boyd
5*90cf83dbSStephen Boydimport gdb
6*90cf83dbSStephen Boydimport zlib
7*90cf83dbSStephen Boyd
8*90cf83dbSStephen Boydfrom linux import utils
9*90cf83dbSStephen Boyd
10*90cf83dbSStephen Boyd
11*90cf83dbSStephen Boydclass LxConfigDump(gdb.Command):
12*90cf83dbSStephen Boyd    """Output kernel config to the filename specified as the command
13*90cf83dbSStephen Boyd       argument. Equivalent to 'zcat /proc/config.gz > config.txt' on
14*90cf83dbSStephen Boyd       a running target"""
15*90cf83dbSStephen Boyd
16*90cf83dbSStephen Boyd    def __init__(self):
17*90cf83dbSStephen Boyd        super(LxConfigDump, self).__init__("lx-configdump", gdb.COMMAND_DATA,
18*90cf83dbSStephen Boyd                                           gdb.COMPLETE_FILENAME)
19*90cf83dbSStephen Boyd
20*90cf83dbSStephen Boyd    def invoke(self, arg, from_tty):
21*90cf83dbSStephen Boyd        if len(arg) == 0:
22*90cf83dbSStephen Boyd            filename = "config.txt"
23*90cf83dbSStephen Boyd        else:
24*90cf83dbSStephen Boyd            filename = arg
25*90cf83dbSStephen Boyd
26*90cf83dbSStephen 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
30*90cf83dbSStephen Boyd        except gdb.error as e:
31*90cf83dbSStephen Boyd            raise gdb.GdbError("Can't find config, enable CONFIG_IKCONFIG?")
32*90cf83dbSStephen Boyd
33*90cf83dbSStephen Boyd        inf = gdb.inferiors()[0]
34*90cf83dbSStephen Boyd        zconfig_buf = utils.read_memoryview(inf, py_config_ptr,
35*90cf83dbSStephen Boyd                                            py_config_size).tobytes()
36*90cf83dbSStephen Boyd
37*90cf83dbSStephen Boyd        config_buf = zlib.decompress(zconfig_buf, 16)
38*90cf83dbSStephen Boyd        with open(filename, 'wb') as f:
39*90cf83dbSStephen Boyd            f.write(config_buf)
40*90cf83dbSStephen Boyd
41*90cf83dbSStephen Boyd        gdb.write("Dumped config to " + filename + "\n")
42*90cf83dbSStephen Boyd
43*90cf83dbSStephen Boyd
44*90cf83dbSStephen BoydLxConfigDump()
45