Linux内核scripts脚本-2-pl脚本汇总
基于 linux-6.1 + A16
一、简介
这些 .pl 脚本是用 Perl 编写的,准确说是 Perl 5。语法上整体风格有点像 C (花括号、分号、运算符、for/if 结构),文本处理思路很像 awk 和 sed(正则特别强,行处理很方便), 写脚本的使用体验又有点像 shell 和 Python。
1. pl脚本列表
android/kernel_platform/msm-kernel/scripts$ find ./ -name '*.pl' ./get_feat.pl ./checkdeclares.pl ./split-man.pl ./leaking_addresses.pl ./get_abi.pl ./generate_initcall_order.pl ./export_report.pl ./extract_xc3028.pl ./checkpatch.pl ./get_maintainer.pl ./profile2linkerlist.pl ./extract-module-sig.pl ./markup_oops.pl ./bootgraph.pl ./checkstack.pl ./headerdep.pl ./parse-maintainers.pl ./extract-sys-certs.pl ./checkversion.pl ./recordmcount.pl ./checkincludes.pl ./kconfig/streamline_config.pl
2. 简单分类
(1) 代码质量检查类
checkpatch.pl、checkdeclares.pl、checkincludes.pl、checkversion.pl、headerdep.pl 等.
(2) 调试和崩溃分析类
bootgraph.pl、checkstack.pl、markup_oops.pl、leaking_addresses.pl 等.
(3) 文档和维护者类
get_maintainer.pl、parse-maintainers.pl
(4) 模块和符号类
export_report.pl、recordmcount.pl 等.
(5) 固件和数据提取类
extract_xc3028.pl、extract-module-sig.pl、extract-sys-certs.pl
(6) 性能和分析类
profile2linkerlist.pl、generate_initcall_order.pl
(7) ABI和特性类
get_abi.pl、get_feat.pl
(8) 文档处理类
split-man.pl
(9) 配置精简类
streamline_config.pl
3. 快速参考表
------------------------------------------------------------- 脚本名 主要用途 使用场景 ------------------------------------------------------------- checkpatch.pl 补丁风格检查 提交代码前必用 get_maintainer.pl 找维护者 提交补丁时必用 checkstack.pl 栈占用分析 性能优化/安全审计 markup_oops.pl Oops 标注 崩溃分析必用 decode_stacktrace.sh 栈解码 崩溃分析必用 bootgraph.pl 启动时序 启动性能分析 leaking_addresses.pl 地址泄露扫描 安全审计 checkincludes.pl 重复 include 清理 代码维护 checkdeclares.pl 重复声明检查 代码维护 export_report.pl 导出符号报告 模块依赖分析 streamline_config.pl 配置精简 自定义内核优化 -------------------------------------------------------------
二、代码质量检查类
1. checkpatch.pl
checkpatch.pl - 补丁和代码风格检查。
(1) 作用:
检查补丁或源码文件是否符合 Linux 内核代码规范(缩进、命名、空白、注释等)。是提交补丁前的必检脚本。
(2) 常用参数:
--strict: 启用严格检查
--show-types: 显示错误类型
--fix: 尝试自动修复
-f: 检查完整文件,而非补丁
--git: 从 git commit 提取补丁
(3) 示例:
# 检查补丁文件 perl scripts/checkpatch.pl 0001-my-patch.patch # 严格模式检查补丁 perl scripts/checkpatch.pl --strict 0001-my-patch.patch # 检查单个源文件(而非补丁) perl scripts/checkpatch.pl -f drivers/android/binder.c # 检查最近一次 commit 的补丁 perl scripts/checkpatch.pl --git HEAD # 检查并显示错误类型 perl scripts/checkpatch.pl --show-types patch.diff # 自动修复可修复的问题 perl scripts/checkpatch.pl --fix -f drivers/example.c # 检查一个 git 范围(如最后 5 个 commit) perl scripts/checkpatch.pl --git HEAD~5..HEAD
2. checkdeclares.pl
checkdeclares.pl - 重复 struct 声明检查
(1) 作用:
检查头文件中的 struct 是否被重复声明,有助于发现重复定义问题。
(2) 示例:
# 检查单个头文件 perl scripts/checkdeclares.pl include/linux/list.h # 检查多个头文件 perl scripts/checkdeclares.pl include/linux/*.h # 检查 android/binder 相关头文件 perl scripts/checkdeclares.pl drivers/android/binder.h include/uapi/linux/binder.h # 如果没有重复,会输出: # No duplicate struct declares found. # 如果有重复,会输出: # include/linux/example.h: struct my_struct is declared more than once.
3. checkincludes.pl
checkincludes.pl - 重复 include 检查和清理
(1) 作用:
检查源文件中是否有重复的 #include 语句,支持自动删除。
(2) 示例:
# 只检查,不修改(默认) perl scripts/checkincludes.pl drivers/android/binder.c # 检查多个文件 perl scripts/checkincludes.pl drivers/android/*.c # 自动删除重复的 include(-r 选项) perl scripts/checkincludes.pl -r drivers/android/binder.c # 检查并删除 binder 模块中的重复 include perl scripts/checkincludes.pl -r drivers/android/*.c # 输出示例, 若找到重复: # drivers/android/binder.c: linux/list.h is included more than once.
4. checkversion.pl
checkversion.pl - version.h 宏使用检查.
(2) 作用:
检查源文件中是否使用 LINUX_VERSION_CODE、KERNEL_VERSION 等宏而缺少对应 #include 或有不必要的 include。
(2) 示例:
# 检查单个文件 perl scripts/checkversion.pl drivers/android/binder.c # 检查多个文件 perl scripts/checkversion.pl drivers/android/*.c kernel/binder.c # 检查带版本宏的代码 perl scripts/checkversion.pl -v drivers/example.c # 输出示例 # drivers/example.c: 10: need linux/version.h # 缺少 include # drivers/example.c: 5 linux/version.h not needed. # 不必要的 include
5. headerdep.pl
headerdep.pl - 头文件依赖分析
(1) 作用:
分析头文件之间的依赖关系,检查循环依赖或错误的头文件链。
(2) 参数:
--all: 分析所有依赖
--graph: 以 graphviz 格式输出图形
-I: 指定 include 路径
(3) 示例:
# 简单分析头文件依赖 perl scripts/headerdep.pl include/linux/list.h # 分析多个头文件 perl scripts/headerdep.pl include/linux/list.h include/linux/spinlock.h # 生成图形化输出(用于 graphviz) perl scripts/headerdep.pl --graph include/linux/kernel.h | dot -Tpng -o deps.png # 完整依赖分析 perl scripts/headerdep.pl --all -I include include/linux/binder.h # 输出示例(如果有循环依赖) # include/linux/a.h -> include/linux/b.h -> include/linux/a.h [cycle]
二、调试和崩溃分析类
1. bootgraph.pl
bootgraph.pl - 启动时序图生成
(1) 作用:
将 dmesg 启动日志转换为 SVG 时序图,可视化各模块/驱动的初始化时间。需要 CONFIG_PRINTK_TIME=y 和 initcall_debug kernel 参数。
(2) Demo 示例:
# 从 dmesg 生成启动时序图 dmesg | perl scripts/bootgraph.pl > boot.svg # 保存 dmesg 到文件后再生成 dmesg > dmesg.log perl scripts/bootgraph.pl < dmesg.log > boot.svg # 生成带版本和日期的图形 perl scripts/bootgraph.pl -header < dmesg.log > boot_with_header.svg # 用浏览器或 Inkscape 打开 firefox boot.svg
2. checkstack.pl
checkstack.pl - 栈占用检查
(1) 作用:
分析二进制文件中各函数的栈帧大小,帮助定位栈溢出风险函数。
(2) 参数:
可以指定架构(x86、arm、ppc64 等)和最小栈大小阈值。
(3) 示例:
# 从 objdump 输出中分析栈占用(自动检测架构) objdump -d vmlinux | perl scripts/checkstack.pl | head -20 # 指定架构(x86_64) objdump -d vmlinux | perl scripts/checkstack.pl x86_64 | head -20 # 设置最小栈大小阈值(默认 100 字节) objdump -d vmlinux | perl scripts/checkstack.pl x86_64 200 | head -30 # 输出栈占用最大的函数(按降序排列)#### objdump -d vmlinux | perl scripts/checkstack.pl | sort -k2 -rn | head -10 # 保存结果到文件 objdump -d vmlinux | perl scripts/checkstack.pl > stack_usage.txt # 查找特定驱动的栈占用 objdump -d drivers/android/binder.o | perl scripts/checkstack.pl
3. markup_oops.pl
markup_oops.pl - Oops 日志标注
(1) 作用:
为 oops/panic 日志中的调用栈地址添加符号名和源码位置,增强可读性。
(2) 参数:
可指定 vmlinux、模块位置、cross-compile 前缀等。
(3) 示例:
# 简单用法(需要 vmlinux 在当前目录或标准位置) perl scripts/markup_oops.pl < oops.txt # 指定 vmlinux 位置 perl scripts/markup_oops.pl < oops.txt -m vmlinux # 指定 cross-compile 前缀(ARM 编译) perl scripts/markup_oops.pl -c arm-linux-gnueabihf- < oops_arm.txt # 从 dmesg 提取 oops 并标注 grep -A100 "Oops:" /var/log/kern.log | perl scripts/markup_oops.pl # 输出示例(标注前后对比) # 标注前: [ 45.123456] [<ffffffff8109e421>] schedule+0x0/0x60 # 标注后: [ 45.123456] [<ffffffff8109e421>] schedule+0x0/0x60 # kernel/sched/core.c:4123
4. leaking_addresses.pl
leaking_addresses.pl - 内核地址泄露检查
(1) 作用:
扫描 dmesg、/proc、/sys 等,检查是否有内核地址(指针)被不当泄露到日志/用户空间。
(2) 参数:
--debug: 调试模式
--raw: 原始模式
--32-bit: 32 位内核扫描
--input-raw: 从文件读取原始数据
(3) 示例:
# 扫描 dmesg 和 /proc、/sys dmesg | perl scripts/leaking_addresses.pl # 调试模式(显示处理的路径) dmesg | perl scripts/leaking_addresses.pl --debug # 32 位内核检查 dmesg | perl scripts/leaking_addresses.pl --32-bit # 扫描系统文件(需要 root) sudo perl scripts/leaking_addresses.pl /proc /sys # 输出示例 # [kernel.log:123] 0xffffffff81234567 (address leak found) # Leaked addresses in /proc/kmem: 3 instances detected
三、文档和维护者类
1. get_maintainer.pl
get_maintainer.pl - 获取维护者信息。
(1) 作用:
根据改动的文件或补丁,从 MAINTAINERS 文件中提取对应的维护者、邮件列表等。
(2) 参数:
-f: 查询特定文件
-a, --git-all: 包含所有可能的提交者
--self-test: 自测 MAINTAINERS 文件
(3) 示例:
# 查询特定文件的维护者 perl scripts/get_maintainer.pl -f drivers/android/binder.c # 从补丁文件提取涉及的文件并找维护者 perl scripts/get_maintainer.pl 0001-my-patch.patch # 包含 git blame 信息和所有可能的提交者 perl scripts/get_maintainer.pl -f drivers/android/binder.c --git-all # 显示角色信息(M=Maintainer, R=Reviewer, L=List) perl scripts/get_maintainer.pl -f drivers/android/binder.c --roles # 输出示例 # Martijn Coenen <maco@android.com> (maintainer:ANDROID BINDER DRIVER) # Todd Kjos <tkjos@android.com> (reviewer) # devel@driverdev.osuosl.org (moderated list:ANDROID BINDER DRIVER)
2. parse-maintainers.pl
parse-maintainers.pl - MAINTAINERS 文件解析和排序
(1) 作用:
解析、验证、排序 MAINTAINERS 文件条目。可按指定模式提取或排序维护者信息。
(2) 参数:
--input: 输入 MAINTAINERS 文件
--output: 排序后输出
--order: 使用首选顺序排序
(3) 示例:
# 解析并排序 MAINTAINERS perl scripts/parse-maintainers.pl --input MAINTAINERS --output MAINTAINERS.sorted # 按首选顺序排序 perl scripts/parse-maintainers.pl --input MAINTAINERS --output MAINTAINERS.new --order # 提取包含 "BINDER" 的条目 perl scripts/parse-maintainers.pl BINDER < MAINTAINERS # 验证 MAINTAINERS 文件 perl scripts/parse-maintainers.pl --validate # 输出示例 # ANDROID BINDER DRIVER # M: Martijn Coenen <maco@android.com> # M: Todd Kjos <tkjos@android.com> # R: Reviewer Name # L: devel@driverdev.osuosl.org # S: Supported # W: https://... # F: drivers/android/binder.c # F: drivers/android/binder_alloc.c
四、模块和符号类
1. export_report.pl
export_report.pl - 导出符号报告
(1) 作用:
从 Module.symvers 和 .mod.c 文件生成导出符号使用报告,帮助分析模块间的依赖。
(2) 参数:
-k: Module.symvers 路径
-f: 要分析的 .mod.c 文件
-o: 输出文件
(4) 示例:
# 生成导出符号报告(自动扫描 modules.order) perl scripts/export_report.pl -k Module.symvers # 指定输出文件 perl scripts/export_report.pl -k Module.symvers -o export_report.txt # 指定特定的 .mod.c 文件 perl scripts/export_report.pl -k Module.symvers -f drivers/android/binder.mod.c # 指定多个 .mod.c 文件(-f 必须是最后一个参数) perl scripts/export_report.pl -k Module.symvers -f drivers/android/*.mod.c # 输出示例 # Module: binder # Depends on: # printk # kmalloc # mutex_lock
2. recordmcount.pl
recordmcount.pl - Ftrace mcount 处理
(1) 作用:
处理编译产物中的 mcount 调用点,为 ftrace/kprobes 提供必要的偏移信息。内核构建系统内部使用。
(2) 示例:
# 通常由构建系统自动调用,但也可以手动运行 perl scripts/recordmcount.pl objdump nm ld input.o output.o # 仅在深度优化或调试 ftrace 时需要手动使用, 检查对象文件中的 mcount 记录: objdump -d output.o | grep mcount
五、固件和数据提取类
1. extract_xc3028.pl
extract_xc3028.pl - XC3028 调谐器固件提取
(1) 作用:
从 Windows 驱动二进制文件中提取 XC3028 TV 调谐器固件数据。
(2) 使用前准备:
a. 下载 Windows 驱动(如 UDXTTM6000.sys)
b. 解压到脚本目录
(3) 示例:
# 在包含驱动文件的目录中运行 cd /tmp/firmware # 下载驱动(以 v2.4 为例) wget http://www.stefanringel.de/pub/20080303_V1.0.6.7.zip unzip 20080303_V1.0.6.7.zip # 运行提取脚本 perl scripts/extract_xc3028.pl # 验证提取的固件 md5sum xc3028-v24.fw xc3028-v27.fw # 安装固件 sudo cp xc3028-v24.fw /lib/firmware/ sudo cp xc3028-v27.fw /lib/firmware/ # 输出示例 # Extracting firmware v2.4... # xc3028-v24.fw: extracted successfully (hash: abc123def456...) # xc3028-v27.fw: extracted successfully (hash: xyz789...)
2. extract-module-sig.pl
(1) 作用:
从已签名的内核模块中提取签名信息、密钥 ID 等。
(2) 参数:
`-0 -a -d -n -k -s` 分别表示不同的提取部分。
(3) 示例:
# 提取完整签名数据 perl scripts/extract-module-sig.pl -a drivers/android/binder.ko # 提取签名者名称 perl scripts/extract-module-sig.pl -n drivers/android/binder.ko # 提取密钥 ID perl scripts/extract-module-sig.pl -k drivers/android/binder.ko # 提取加密签名 perl scripts/extract-module-sig.pl -s drivers/android/binder.ko # 提取签名描述符 perl scripts/extract-module-sig.pl -d drivers/android/binder.ko # 提取未签名的模块数据 perl scripts/extract-module-sig.pl -0 drivers/android/binder.ko > binder.unsigned.ko # 输出示例 # Signer Name: Linux Kernel # Key ID: 0x01234567 # Signature Algorithm: sha256
3. extract-sys-certs.pl
extract-sys-certs.pl - 系统证书提取
(1) 作用:
从编译的 vmlinux 或 vmlinuz 中提取内嵌的系统证书和密钥。
(2) 示例:
# 提取证书(需要 System.map) perl scripts/extract-sys-certs.pl vmlinux > system_certs.pem # 指定 System.map 路径 perl scripts/extract-sys-certs.pl -s System.map vmlinux > certs.der # 验证提取的证书 openssl x509 -inform DER -in certs.der -text -noout # 输出示例 # Have 2 sections # Extracting .system_certificates section... # Certificate 1: CN=Linux Kernel, O=...
六、性能和分析类
1. profile2linkerlist.pl
profile2linkerlist.pl - Profile 结果转链接列表
(1) 作用:
将 readprofile 的输出转换为链接脚本格式,用于按 profile 结果优化函数顺序。
(2) 示例:
# 获取 profile 数据 readprofile | sort -rn > profile.txt # 转换为链接列表 perl scripts/profile2linkerlist.pl < profile.txt > function.order # 或者一行完成 readprofile | sort -rn | perl scripts/profile2linkerlist.pl > function.order # 使用生成的列表进行优化编译, 在 Makefile 中使用 function.order... # 输出示例 # *(.text.schedule) # *(.text.do_schedule) # *(.text.try_to_wake_up)
2. generate_initcall_order.pl
generate_initcall_order.pl - Initcall 顺序生成
(1) 作用:
并行分析多个对象文件中的 initcall 函数,生成正确的链接顺序脚本。
环境变量: NM 应指向 nm 工具。
(2) 示例:
# 检查 initcall 顺序(由构建系统自动调用) export NM=nm perl scripts/generate_initcall_order.pl > init_order.txt # 指定特定的 nm 工具(cross-compile) export NM=arm-linux-gnueabihf-nm perl scripts/generate_initcall_order.pl > init_order_arm.txt # 输出示例 # 0 pure_initcall module_init_func # 1 core_initcall other_init_func # 2 postcore_initcall third_init_func
7. ABI 和特性类
1. get_abi.pl
(1) 作用:
从 Documentation/ABI 目录提取内核 ABI 信息,支持搜索、验证等。
命令**: `search`, `rest`, `validate`, `undefined`
(2) 示例:
# 搜索特定 ABI 项 perl scripts/get_abi.pl search what sysfs > abi_search_results.txt # 验证 ABI 文档完整性 perl scripts/get_abi.pl validate # 生成 ReST 格式输出 perl scripts/get_abi.pl rest Documentation/ABI > abi_docs.rst # 查找未定义的 ABI perl scripts/get_abi.pl undefined # 搜索 binder 相关 ABI perl scripts/get_abi.pl search what binder # 输出示例 # /sys/kernel/debug/binder/transactions # /sys/kernel/debug/binder/state # Description: Binder transaction history and state
2. get_feat.pl
get_feat.pl - 特性信息提取
(1) 作用:
从 Documentation/features 目录提取内核特性信息、支持状态等。
命令: current, rest, validate, ls, list
(3) 示例:
# 列出所有特性 perl scripts/get_feat.pl list # 查看当前架构支持的特性#### perl scripts/get_feat.pl current arm64 # 验证特性文档 perl scripts/get_feat.pl validate # 生成 ReST 格式文档 perl scripts/get_feat.pl rest Documentation/features > features_docs.rst # 查看特定特性 perl scripts/get_feat.pl rest NUMA # 输出示例 # NUMA - Supported # ARM64 - Supported (v5.0+) # PPC64 - Supported (v4.15+)
八、文档处理类
1. split-man.pl
split-man.pl - Man 页拆分
(1) 作用:
将生成的内核文档 man 页按函数/小节拆分成单个文件。
(2) 示例:
# 生成内核文档并拆分 perl scripts/kernel-doc -man drivers/android/binder.c | perl scripts/split-man.pl man_output_dir/ # 查看生成的 man 页 ls man_output_dir/ # binder.9 # binder_alloc_new_buf.9 # binder_transaction.9 # 安装到系统 man 目录 sudo cp man_output_dir/*.9 /usr/share/man/man9/ # 查看 man 9 binder
九、配置精简类
1. streamline_config.pl
(1) 作用:
通过扫描当前运行系统的已加载模块,生成精简的内核配置文件,去除不必要的模块。流程:
a. 启动使用当前配置编译的内核
b. 让所有需要的驱动/模块加载
c. 运行此脚本
d. 用生成的配置重新编译
(2) 示例:
# 进入内核源码目录 cd linux-6.1/ # 复制当前系统的配置 cp /boot/config-$(uname -r) .config # 运行精简脚本 perl scripts/kconfig/streamline_config.pl > config_streamlined # 备份原配置 cp .config .config.bak # 使用精简配置 cp config_streamlined .config # 验证配置(会提示新选项,直接回车使用默认) make oldconfig # 编译精简后的内核 make -j$(nproc) # 输出示例 # Processing lsmod output... # Loaded modules found: 156 # Generating streamlined config... # Modules disabled: 1024 # Modules kept: 156
posted on 2026-07-10 16:03 Hello-World3 阅读(11) 评论(0) 收藏 举报
浙公网安备 33010602011771号