Skip to contentSkip to conversation

scripts: add two new scripts to look up Kconfigs

PATCH In review Brian Masney 3 participants 8 messages Aug 1, 2023 – Jul 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover scripts: add two new scripts to look up Kconfigs

Here's two new scripts that I think that other people will find useful. One is to look up a Kconfig name based on the module path and the second script looks up the full Kconfig information based on the symbol name.

Brian Masney (2):
scripts: add mod-to-kconfig.sh
scripts: add kconfig lookup script

 scripts/kconfig/lookup.sh         | 77 +++++++++++++++++++++++++
 scripts/kconfig/mod-to-kconfig.sh | 93 +++++++++++++++++++++++++++++++
 2 files changed, 170 insertions(+)
 create mode 100755 scripts/kconfig/lookup.sh
 create mode 100755 scripts/kconfig/mod-to-kconfig.sh
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/2 scripts: add mod-to-kconfig.sh

Add a useful script that can be used to convert a kernel module path to the relevant Kconfig symbol name. Examples showing how to use this is in the help at the top of the script.

---
 scripts/kconfig/mod-to-kconfig.sh | 93 +++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100755 scripts/kconfig/mod-to-kconfig.sh
scripts/kconfig/mod-to-kconfig.sh new file +93 -0
new file mode 100755
index 000000000000..1b69b638ebf5
@@ -0,0 +1,93 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
+# Written by Brian Masney <[email protected]>
+#
+# This script takes as input one or more kernel module names, and outputs
+# the relevant Kconfig symbol name. This script was originally intended to help
+# identify which Kconfig options are enabled inside an initramfs.
+#
+#   x1:~/src/linux$ find /usr/lib/modules/`uname -r` -name *.ko.xz | \
+#                       ./scripts/kconfig/mod-to-kconfig.sh
+#   CONFIG_CRYPTO_USER
+#   CONFIG_CRYPTO_ESSIV
+#   CONFIG_CRYPTO_CHACHA20
+#   CONFIG_CRYPTO_TWOFISH
+#   ...
+#
+# You can also use this to walk the modules.builtin file:
+#
+#     x1:~/src/linux$ ./scripts/kconfig/mod-to-kconfig.sh < \
+#            /usr/lib/modules/`uname -r`/modules.builtin
+#
+# Pipe the output of this script into scripts/kconfig/lookup.sh if you want to
+# view the full Kconfig entries.
+#
+# Note that there is a fair bit of variability in the Makefiles across the
+# kernel and this script won't match everything. It's only been written to
+# cover the 95% use case.
+
+process_module()
+{
+	# We may have a full path, like /usr/lib/modules/`uname -r`/kernel/...`,
+	# or a relative path, like kernel/... that's found in modules.builtin.
+	local DIR
+	DIR=$(dirname "$1")
+	if [[ "${DIR}" =~ ^/ ]] ; then
+		DIR=${DIR##*/kernel/}
+	else
+		DIR=${DIR##kernel/}
+	fi
+
+	# Handle compressed module names
+	local MOD_NAME
+	MOD_NAME=$(basename "$1")
+	MOD_NAME="${MOD_NAME/.xz/}" # CONFIG_FW_LOADER_COMPRESS_XZ
+	MOD_NAME="${MOD_NAME/.zst/}" # CONFIG_FW_LOADER_COMPRESS_ZSTD
+	MOD_NAME="${MOD_NAME/.ko/}"
+	MOD_NAME="${MOD_NAME/.c/}"
+
+	local MAKEFILE
+	MAKEFILE="${DIR}/Makefile"
+	if [ ! -f "${MAKEFILE}" ] ; then
+		echo "Skipping $1 since ${MAKEFILE} is not found" >&2
+		return 1
+	fi
+
+	# There's probably a more elegant way you could do this with sed or awk,
+	# however personally I find this approach more readable in this
+	# particular case.
+	local CONFIG
+	CONFIG=$(grep --before-context=200 -w -E "${MOD_NAME}(\.o|/)" \
+		 "${DIR}/Makefile" | grep CONFIG_ | tail -n 1 | \
+		 awk -F\( '{print $2}' | awk -F\) '{print $1}')
+	if [ "${CONFIG}" = "" ] ; then
+		echo "Skipping $1 since CONFIG cannot be determined" >&2
+		return 1
+	fi
+	echo "${CONFIG}"
+}
+
+# Run this script from the toplevel kernel source directory.
+SCRIPT_PATH=$(readlink -f "$0")
+cd "$(dirname "${SCRIPT_PATH}")/../.." || exit 1
+
+RET=0
+if [[ $# == 0 ]] ; then
+	# Read module paths from stdin
+	while read -r MODULE_PATH ; do
+		if ! process_module "${MODULE_PATH}" ; then
+			RET=1
+		fi
+	done
+else
+	# Read module paths from the command line arguments
+	for NUM in $(seq 1 "$#") ; do
+		if ! process_module "${!NUM}" ; then
+			RET=1
+		fi
+	done
+fi
+
+exit "${RET}"
Signature · 1 line
2.41.0
#2 lore raw

PATCH 2/2 scripts: add kconfig lookup script

In reply to Brian Masney

Add a script that allows looking up the full Kconfig entry based on the symbol name. Documentation and example usage is found at the top of the script itself.

---
 scripts/kconfig/lookup.sh | 77 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100755 scripts/kconfig/lookup.sh
scripts/kconfig/lookup.sh new file +77 -0
new file mode 100755
index 000000000000..d1ff52b23835
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
+# Written by Brian Masney <[email protected]>
+#
+# This script takes as input one or more Kconfig symbols and outputs the full
+# entry from the Kconfig file. It can be invoked by reading a list of symbol
+# names from either stdin or as command line arguments. Example output:
+#
+#   x1:~/src/linux$ ./scripts/kconfig/lookup.sh TSL2772 SOUND
+#   # drivers/iio/light/Kconfig
+#   config TSL2772
+#     tristate "TAOS TSL/TMD2x71 and TSL/TMD2x72 Family of light and proximity sensors"
+#     depends on I2C
+#     help
+#       Support for: tsl2571, tsl2671, tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
+#       tmd2672, tsl2772, tmd2772 devices.
+#       Provides iio_events and direct access via sysfs.
+#
+#   # arch/um/drivers/Kconfig
+#   config SOUND
+#     tristate
+#     default UML_SOUND
+#
+#   # sound/Kconfig
+#   menuconfig SOUND
+#     tristate "Sound card support"
+#     depends on HAS_IOMEM
+#     help
+#       If you have a sound card in your computer, i.e. if it can say more
+#       than an occasional beep, say Y.
+
+
+process_kconfig()
+{
+	KCONFIG="${1/CONFIG_/}"
+
+	FOUND=0
+	for KCONFIG_FILE in $(git grep -E "^(config|menuconfig) ${KCONFIG}$" | \
+	                      awk -F: '{print $1}') ; do
+		echo "# ${KCONFIG_FILE}"
+		awk "/^(config|menuconfig) ${KCONFIG}$/{ m=1; print; next; } \
+		     /^(choice|comment|config|end|if|menuconfig|source)/ { m=0; } m" \
+		    "${KCONFIG_FILE}"
+		FOUND=1
+	done
+
+	if [[ "${FOUND}" = "0" ]] ; then
+		echo "Skipping ${KCONFIG} since Kconfig symbol is not found" >&2
+		return 1
+	fi
+
+}
+
+# Run this script from the toplevel kernel source directory.
+SCRIPT_PATH=$(readlink -f "$0")
+cd "$(dirname "${SCRIPT_PATH}")/../.." || exit 1
+
+RET=0
+if [[ $# == 0 ]] ; then
+	# Read Kconfig names from stdin
+	while read -r KCONFIG ; do
+		if ! process_kconfig "${KCONFIG}" ; then
+			RET=1
+		fi
+	done
+else
+	# Read Kconfig names from the command line arguments
+	for NUM in $(seq 1 "$#") ; do
+		if ! process_kconfig "${!NUM}" ; then
+			RET=1
+		fi
+	done
+fi
+
+exit "${RET}"
Signature · 1 line
2.41.0
#3 lore raw

Re: [PATCH 2/2] scripts: add kconfig lookup script

Quoted 10 lines · Brian Masney

Add a script that allows looking up the full Kconfig entry based on the symbol name. Documentation and example usage is found at the top of the script itself.

---
 scripts/kconfig/lookup.sh | 77 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100755 scripts/kconfig/lookup.sh

Everyone tends to have their own utility scripts on their machines.

I think this patch set falls into that category as "create a wrapper script of grep" is what everyone does to reduce typing.

FWIW, I have the following scripts in my ~/bin directory.

$ cat ~/bin/kgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Kconfig*' -o -name 'Config.in' \) \
-print0 | xargs -0 grep --color -n "$@"

$ cat ~/bin/mgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
-print0 | xargs -0 grep --color -n "$@"

masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
./drivers/iio/light/Kconfig:564:config TSL2772
./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
TSL/TMD2x72 Family of light and proximity sensors"
./drivers/iio/light/Kconfig-566- depends on I2C
./drivers/iio/light/Kconfig-567- help
./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.
masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o

That's my local way to satisfy my demand.
I do not intend to force my way or merge it in the upstream.

Quoted 85 lines
scripts/kconfig/lookup.sh new file +77 -1
new file mode 100755
index 000000000000..d1ff52b23835
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
+# Written by Brian Masney <[email protected]>
+#
+# This script takes as input one or more Kconfig symbols and outputs the full
+# entry from the Kconfig file. It can be invoked by reading a list of symbol
+# names from either stdin or as command line arguments. Example output:
+#
+#   x1:~/src/linux$ ./scripts/kconfig/lookup.sh TSL2772 SOUND
+#   # drivers/iio/light/Kconfig
+#   config TSL2772
+#     tristate "TAOS TSL/TMD2x71 and TSL/TMD2x72 Family of light and proximity sensors"
+#     depends on I2C
+#     help
+#       Support for: tsl2571, tsl2671, tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
+#       tmd2672, tsl2772, tmd2772 devices.
+#       Provides iio_events and direct access via sysfs.
+#
+#   # arch/um/drivers/Kconfig
+#   config SOUND
+#     tristate
+#     default UML_SOUND
+#
+#   # sound/Kconfig
+#   menuconfig SOUND
+#     tristate "Sound card support"
+#     depends on HAS_IOMEM
+#     help
+#       If you have a sound card in your computer, i.e. if it can say more
+#       than an occasional beep, say Y.
+
+
+process_kconfig()
+{
+       KCONFIG="${1/CONFIG_/}"
+
+       FOUND=0
+       for KCONFIG_FILE in $(git grep -E "^(config|menuconfig) ${KCONFIG}$" | \
+                             awk -F: '{print $1}') ; do
+               echo "# ${KCONFIG_FILE}"
+               awk "/^(config|menuconfig) ${KCONFIG}$/{ m=1; print; next; } \
+                    /^(choice|comment|config|end|if|menuconfig|source)/ { m=0; } m" \
+                   "${KCONFIG_FILE}"
+               FOUND=1
+       done
+
+       if [[ "${FOUND}" = "0" ]] ; then
+               echo "Skipping ${KCONFIG} since Kconfig symbol is not found" >&2
+               return 1
+       fi
+
+}
+
+# Run this script from the toplevel kernel source directory.
+SCRIPT_PATH=$(readlink -f "$0")
+cd "$(dirname "${SCRIPT_PATH}")/../.." || exit 1
+
+RET=0
+if [[ $# == 0 ]] ; then
+       # Read Kconfig names from stdin
+       while read -r KCONFIG ; do
+               if ! process_kconfig "${KCONFIG}" ; then
+                       RET=1
+               fi
+       done
+else
+       # Read Kconfig names from the command line arguments
+       for NUM in $(seq 1 "$#") ; do
+               if ! process_kconfig "${!NUM}" ; then
+                       RET=1
+               fi
+       done
+fi
+
+exit "${RET}"
--
Quoted 2 lines

2.41.0

--
Best Regards

Masahiro Yamada

#4 lore raw

Re: [PATCH 2/2] scripts: add kconfig lookup script

Quoted 42 lines · Masahiro Yamada

Everyone tends to have their own utility scripts on their machines.

I think this patch set falls into that category as "create a wrapper script of grep" is what everyone does to reduce typing.

FWIW, I have the following scripts in my ~/bin directory.

$ cat ~/bin/kgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Kconfig*' -o -name 'Config.in' \) \
-print0 | xargs -0 grep --color -n "$@"

$ cat ~/bin/mgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
-print0 | xargs -0 grep --color -n "$@"

masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
./drivers/iio/light/Kconfig:564:config TSL2772
./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
TSL/TMD2x72 Family of light and proximity sensors"
./drivers/iio/light/Kconfig-566- depends on I2C
./drivers/iio/light/Kconfig-567- help
./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.
Quoted 8 lines
masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o

That's my local way to satisfy my demand.
I do not intend to force my way or merge it in the upstream.

OK, fair enough.

Those are useful little utilities and simpler than what I posted. If something like these had been in the scripts/ directory, then I wouldn't have spent the time to write yet another script that does basically the same thing. I get what you are saying, however having a script to lookup a Kconfig by name or module will be useful to other people.

Brian

#5 lore raw

Re: [PATCH 2/2] scripts: add kconfig lookup script

Quoted 55 lines · Brian Masney
Quoted 42 lines · Masahiro Yamada

Everyone tends to have their own utility scripts on their machines.

I think this patch set falls into that category as "create a wrapper script of grep" is what everyone does to reduce typing.

FWIW, I have the following scripts in my ~/bin directory.

$ cat ~/bin/kgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Kconfig*' -o -name 'Config.in' \) \
-print0 | xargs -0 grep --color -n "$@"

$ cat ~/bin/mgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
-print0 | xargs -0 grep --color -n "$@"

masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
./drivers/iio/light/Kconfig:564:config TSL2772
./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
TSL/TMD2x72 Family of light and proximity sensors"
./drivers/iio/light/Kconfig-566- depends on I2C
./drivers/iio/light/Kconfig-567- help
./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.
Quoted 8 lines
masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o

That's my local way to satisfy my demand.
I do not intend to force my way or merge it in the upstream.

OK, fair enough.

Quoted 9 lines

Those are useful little utilities and simpler than what I posted. If something like these had been in the scripts/ directory, then I wouldn't have spent the time to write yet another script that does basically the same thing. I get what you are saying, however having a script to lookup a Kconfig by name or module will be useful to other people.

Brian

I do not want to fill the scripts/ directory
with random trivial scripts.

I need to draw a line between
"the time cost for writing the code by myself" vs "the time cost for searching for a similar script".

I'd rather write the code by myself
if it is a script of a few lines.

Signature · 2 lines
Best Regards
Masahiro Yamada
#6 lore raw

Re: [PATCH 2/2] scripts: add kconfig lookup script

In reply to Masahiro Yamada

Hi,
Sorry about the very late feedback.

Quoted 28 lines · Masahiro Yamada
Quoted 10 lines · Brian Masney

Add a script that allows looking up the full Kconfig entry based on the symbol name. Documentation and example usage is found at the top of the script itself.

---
 scripts/kconfig/lookup.sh | 77 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100755 scripts/kconfig/lookup.sh

Everyone tends to have their own utility scripts on their machines.

I think this patch set falls into that category as "create a wrapper script of grep" is what everyone does to reduce typing.

FWIW, I have the following scripts in my ~/bin directory.

Quoted 7 lines
$ cat ~/bin/kgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Kconfig*' -o -name 'Config.in' \) \
-print0 | xargs -0 grep --color -n "$@"

Sometimes I just want to see a symbol's definition. So using

$ kgrep -A8 config.TSL2772
finds only the config|menuconfig definition for the symbol
without printing other mentions of it (in selects or depends on
or help text or comments etc.).
Quoted 9 lines
$ cat ~/bin/mgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
-print0 | xargs -0 grep --color -n "$@"

Using

$ mgrep floppy.o
shows me nothing. It never completes, just hangs (OK, I killed it
after 30 seconds). Any ideas?

Thanks for the tooling.

Quoted 20 lines

masahiro@zoe:~/ref/linux(master)$ kgrep -A5 TSL2772
./drivers/iio/light/Kconfig:564:config TSL2772
./drivers/iio/light/Kconfig-565- tristate "TAOS TSL/TMD2x71 and
TSL/TMD2x72 Family of light and proximity sensors"
./drivers/iio/light/Kconfig-566- depends on I2C
./drivers/iio/light/Kconfig-567- help
./drivers/iio/light/Kconfig-568-   Support for: tsl2571, tsl2671,
tmd2671, tsl2771, tmd2771, tsl2572, tsl2672,
./drivers/iio/light/Kconfig-569-   tmd2672, tsl2772, tmd2772 devices.
masahiro@zoe:~/ref/linux(master)$ mgrep efivarfs.o
./fs/efivarfs/Makefile:6:obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
./fs/efivarfs/Makefile:8:efivarfs-objs := inode.o file.o super.o vars.o

That's my local way to satisfy my demand.
I do not intend to force my way or merge it in the upstream.

Signature · 1 line
~Randy
#7 lore raw

Re: [PATCH 2/2] scripts: add kconfig lookup script

Quoted 58 lines · Randy Dunlap

Hi,
Sorry about the very late feedback.

Quoted 28 lines · Masahiro Yamada
Quoted 10 lines · Brian Masney

Add a script that allows looking up the full Kconfig entry based on the symbol name. Documentation and example usage is found at the top of the script itself.

---
 scripts/kconfig/lookup.sh | 77 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100755 scripts/kconfig/lookup.sh

Everyone tends to have their own utility scripts
on their machines.

I think this patch set falls into that category as "create a wrapper script of grep" is what everyone does to reduce typing.

FWIW, I have the following scripts in my ~/bin directory.

Quoted 7 lines
$ cat ~/bin/kgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Kconfig*' -o -name 'Config.in' \) \
-print0 | xargs -0 grep --color -n "$@"

Sometimes I just want to see a symbol's definition. So using

$ kgrep -A8 config.TSL2772
finds only the config|menuconfig definition for the symbol
without printing other mentions of it (in selects or depends on
or help text or comments etc.).
Quoted 9 lines
$ cat ~/bin/mgrep
#!/bin/sh

exec find . -name .repo -prune -o -name .git -prune -o -type f \
\( -name 'Makefile*' -o -name 'Kbuild*' -o -name "*.mk" \) \
-print0 | xargs -0 grep --color -n "$@"

Quoted 6 lines

Using

$ mgrep floppy.o
shows me nothing. It never completes, just hangs (OK, I killed it
after 30 seconds). Any ideas?

Thanks for the tooling.

I do not know.
It worked for me.

masahiro@zoe:~/ref/linux(master)$ mgrep floppy.o
./drivers/block/Makefile:17:obj-$(CONFIG_BLK_DEV_FD) += floppy.o
Signature · 2 lines
Best Regards
Masahiro Yamada