Chinaunix

标题: 帮助你阅读内核源码 [打印本页]

作者: prc    时间: 2015-11-23 14:53
标题: 帮助你阅读内核源码
本帖最后由 prc 于 2015-11-26 16:34 编辑

在阅读大型C/C++工程源码的过程中常常遇到以下问题
这些都会打断思维过程,使得我不得不停下来花费时间精力去判断哪个代码块为“真”,哪个为“假”。 CL针对这个问题提供了一套简单的解决方案,在编译过程中模仿预处理器计算条件编译语句的值,然后删除“假”的代码块, 仅剩下有效代码。

运行环境

运行
以linux为例,依次执行下列命令:
--yz开头的参数传给CL的;--yz-cc表示编译器,--yz-postprocess表示先编译后清理源文件;一般来说只需要这两个参数就可以了。 --yz-server-addr表示启动server并指定server的地址(该地址可以是任何有效的路径)。启动server可以减少临时文件的大小, 并对程序的性能有轻微的改善。而其它的参数如-j8则是传给make的。
编译完成后,在linux目录下会发现大量的.bak文件,它们都是被清理过的同名的.c/.cpp/.h文件的备份。通过比较.bak与原文件,可以发现源文件中的条件编译代码已经被删除:

为了方便恢复代码,建议使用git或者其它的版本管理工具。
为了保证目标文件vmlinux与源代码的一致性,还需要再次编译linux。

限制
只能使用下列编译器(其它的编译器未经测试)
并且${CROSS_COMILE}gcc展开后只能包含字母,数字,下划线_和减号-

副产品
${YOUR_DIR}目录下的projlist.txt列出了本次编译涉及到的所有.c/.cpp/.h文件,我们可以利用它来生成一个更精确的cscope或者ctags的数据库。
cscope -bkq -i <(tail -n +2 projlist.txt)
通过这个数据库查找可以避免大量重名的符号。


y-Make的命令行参数

  1. --yz-cc=CC                 指定编译器
  2. --yz-postprocess=DIR       开启后处理模式,并使用DIR为其工作目录
  3. --yz-save-dep=FILE         将所有依赖关系保存在文件FILE中
  4. --yz-save-cl=FILE          将所有命令行保存在文件FILE中
  5. --yz-save-condvals=FILE    将所有条件编译语句的计算结果保存在文件FILE中
  6. --yz-save-proj=FILE        将编译中所涉及到的文件保存在文件FILE中
  7. --yz-server-addr=FILE      启动server,并以FILE作为server的地址
  8. --yz-runtime-dir=DIR       指定server的工作目录为DIR;默认为/var/tmp
  9. --yz-xcc=FILE              指定FILE为parser;默认为${CL_DIR}/cl.exe
  10. --yz-server-program=FILE   指定FILE为server;默认为${CL_DIR}/cl-server.exe
复制代码
Post Process(后处理)
后处理模式是为了解决编译过程中的实际问题而引入的,即一个.c/.cpp文件有可能被多次编译。如果在编译之前清理文件(这是默认模式), 被删除掉的代码很可能在第二次编译时应该被引用,从而导致整个编译过程失败。 并且,.h文件几乎100%会被多次引用,如果未开启后处理模式,清理.h文件是不可能的。

适用范围
下列开源项目经过测试

作者: Godbach    时间: 2015-11-23 16:39
回复 1# prc

感谢分享。


   
作者: nswcfd    时间: 2015-11-25 14:52
应该会有很多网友需要这样的工具。3x

作者: amarant    时间: 2015-11-26 14:18
这个diao
作者: yihect    时间: 2015-11-28 12:05
有动态 开启/关闭 功能否?
有时候确实想知道走的是哪个预处理块,但如果写死,难免会起一些纠结
作者: prc    时间: 2015-11-30 19:24
这个需求可以通过git完成
git checkout YOUR_FILE #恢复文件
CL_DIR/tool/cl-tool strip -f TMP_DIR/tmp.pp YOUR_FILE #清理文件
回复 5# yihect


作者: firocu    时间: 2015-12-01 12:46
Cool!
It seems very useful!
Thanks for share!
作者: hnwyllmm    时间: 2015-12-10 10:17
../codeless/y-Make --yz-cc=gcc --yz-postprocess=cl

我使用上面的命令处理zeromq,出现这样的异常:

../libtool: line 1:  4300 Segmentation fault      (core dumped) /home/bm186/codeless/cl.exe --yz-no-output --yz-save-dep=/home/bm186/zeromq-wangyl11/cl/depends.txt --yz-save-cl=/home/bm186/zeromq-wangyl11/cl/commands.txt --yz-save-condvals=/home/bm186/zeromq-wangyl11/cl/condvals.txt --yz-top-dir=/home/bm186/zeromq-wangyl11 --yz-cc=gcc --yz-verbose=0 "$@"

我的系统环境是
186_bm186%uname -a
Linux rhel13160 2.6.32-131.0.15.el6.x86_64 #1 SMP Tue May 10 15:42:40 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

楼主见过这个错误吗?
作者: prc    时间: 2015-12-10 11:33
回复 8# hnwyllmm
这个可能是个BUG,请给出zeromq的下载路径

   
作者: hnwyllmm    时间: 2015-12-10 12:51
回复 9# prc
github.com/zeromq/zeromq4-1  (抱歉没有URL权限)
这是官方版本的下载地址,但是我自己测试的是4.0.4版本代码
另外,我把gcc改成g++就不会core了,但是却没有发现任何结果。

作者: prc    时间: 2015-12-11 18:48
本帖最后由 prc 于 2015-12-11 18:48 编辑
hnwyllmm 发表于 2015-12-10 12:51
回复 9# prc
github.com/zeromq/zeromq4-1  (抱歉没有URL权限)
这是官方版本的下载地址,但是我自己测试 ...

更新一下codeless的代码,在zeromq4.1上是没问题的;4.04由于没有代码就没试。
作者: shihyu    时间: 2015-12-19 23:13
本帖最后由 shihyu 于 2015-12-19 23:13 编辑

测试 linux-2.6.32.69

make defconfig
../codeless/y-Make --yz-cc=gcc --yz-postprocess=cl
  1. function gcc()
  2. {
  3.     /home/shihyu/github/codeless/cl.exe --yz-no-output --yz-save-dep=/home/shihyu/github/linux-2.6.32.69/cl/depends.txt --yz-save-cl=/home/shihyu/github/linux-2.6.32.69/cl/commands.txt --yz-save-condvals=/home/shihyu/github/linux-2.6.32.69/cl/condvals.txt --yz-top-dir=/home/shihyu/github/linux-2.6.32.69 --yz-cc=gcc --yz-verbose=0 "$@" || return $?
  4.     /usr/bin/gcc $*
  5. }
  6. function g++()
  7. {
  8.     /home/shihyu/github/codeless/cl.exe --yz-no-output --yz-save-dep=/home/shihyu/github/linux-2.6.32.69/cl/depends.txt --yz-save-cl=/home/shihyu/github/linux-2.6.32.69/cl/commands.txt --yz-save-condvals=/home/shihyu/github/linux-2.6.32.69/cl/condvals.txt --yz-top-dir=/home/shihyu/github/linux-2.6.32.69 --yz-cc=gcc --yz-verbose=0 "$@" || return $?
  9.     /usr/bin/g++ $*
  10. }
  11. export -f gcc g++
  12. /usr/bin/make SHELL=bash

  13. bash: line 1: 13811 Segmentation fault      (core dumped) /home/shihyu/github/codeless/cl.exe --yz-no-output --yz-save-dep=/home/shihyu/github/linux-2.6.32.69/cl/depends.txt --yz-save-cl=/home/shihyu/github/linux-2.6.32.69/cl/commands.txt --yz-save-condvals=/home/shihyu/github/linux-2.6.32.69/cl/condvals.txt --yz-top-dir=/home/shihyu/github/linux-2.6.32.69 --yz-cc=gcc --yz-verbose=0 "$@"
  14.   HOSTCC  scripts/kconfig/conf.o
  15. bash: line 1: 13907 Segmentation fault      (core dumped) /home/shihyu/github/codeless/cl.exe --yz-no-output --yz-save-dep=/home/shihyu/github/linux-2.6.32.69/cl/depends.txt --yz-save-cl=/home/shihyu/github/linux-2.6.32.69/cl/commands.txt --yz-save-condvals=/home/shihyu/github/linux-2.6.32.69/cl/condvals.txt --yz-top-dir=/home/shihyu/github/linux-2.6.32.69 --yz-cc=gcc --yz-verbose=0 "$@"
  16. make[2]: *** [scripts/kconfig/conf.o] Error 139
  17. make[1]: *** [silentoldconfig] Error 2
  18. make: *** No rule to make target `include/config/auto.conf', needed by `include/config/kernel.release'.  Stop
复制代码

作者: prc    时间: 2015-12-23 15:34
回复 12# shihyu
bug已修复,请更新代码

   
作者: shihyu    时间: 2015-12-23 22:49
本帖最后由 shihyu 于 2015-12-23 22:49 编辑


还是一样错误讯息xd
作者: hnwyllmm    时间: 2015-12-25 12:51
楼主,我这里也是同样的BUG
hnwyllmm@ubuntu:~/linux-2.6.32.63$ uname -a
Linux ubuntu 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
使用的是Ubuntu 14.04 Server版

hnwyllmm@ubuntu:~/linux-2.6.32.63$ file core
core: ELF 64-bit LSB  core file x86-64, version 1 (SYSV), SVR4-style, from '/home/hnwyllmm/codeless/cl.exe --yz-no-output --yz-save-dep=/home/hnwyllmm/linu'
  1. hnwyllmm@ubuntu:~/linux-2.6.32.63$ gdb ../codeless/cl.exe core
  2. GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
  3. Copyright (C) 2014 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "x86_64-linux-gnu".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. >.
  12. Find the GDB manual and other documentation resources online at:
  13. <>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word"...
  16. Reading symbols from ../codeless/cl.exe...done.
  17. [New LWP 5458]
  18. [Thread debugging using libthread_db enabled]
  19. Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  20. Core was generated by `/home/hnwyllmm/codeless/cl.exe --yz-no-output --yz-save-dep=/home/hnwyllmm/linu'.
  21. Program terminated with signal SIGSEGV, Segmentation fault.
  22. #0  __GI___pthread_mutex_lock (mutex=0x0) at ../nptl/pthread_mutex_lock.c:66
  23. 66      ../nptl/pthread_mutex_lock.c: No such file or directory.
  24. (gdb) bt
  25. #0  __GI___pthread_mutex_lock (mutex=0x0) at ../nptl/pthread_mutex_lock.c:66
  26. #1  0x0000000000416fe1 in ipsc_acc_bytes (sc=0x0, index=0, val=512) at ../common/ip_sc.c:54
  27. #2  0x0000000000404066 in OsFileWriter::Write (this=0x12b1400, buf=0x12b1c18, count=<optimized out>) at FileWriter.cpp:106
  28. #3  0x0000000000402d8f in save_command_line (filename=..., my_args=..., cc_args=..., host_cc=...) at startup.cpp:86
  29. #4  main (argc=<optimized out>, argv=<optimized out>) at startup.cpp:215
  30. (gdb)
复制代码
  1. (gdb) f 1
  2. #1  0x0000000000416fe1 in ipsc_acc_bytes (sc=0x0, index=0, val=512) at ../common/ip_sc.c:54
  3. 54          pthread_mutex_lock(&sc->lock);
  4. (gdb) info local
  5. No locals.
  6. (gdb) info reg
  7. rax            0x0      0
  8. rbx            0x0      0
  9. rcx            0xffffffffffffffff       -1
  10. rdx            0x200    512
  11. rsi            0x0      0
  12. rdi            0x0      0
  13. rbp            0x200    0x200
  14. rsp            0x7fff0d77cf00   0x7fff0d77cf00
  15. r8             0x0      0
  16. r9             0x1      1
  17. r10            0x7fff0d77ccc0   140733419343040
  18. r11            0x2b3edc166410   47548980421648
  19. r12            0x0      0
  20. r13            0x12b1c18        19602456
  21. r14            0x0      0
  22. r15            0x0      0
  23. rip            0x416fe1 0x416fe1 <ipsc_acc_bytes+17>
  24. eflags         0x10206  [ PF IF RF ]
  25. cs             0x33     51
  26. ss             0x2b     43
  27. ds             0x0      0
  28. es             0x0      0
  29. fs             0x0      0
  30. gs             0x0      0
  31. (gdb)
复制代码

作者: arm-linux-gcc    时间: 2015-12-25 14:42
useful, thx
作者: prc    时间: 2015-12-25 19:31
回复 14# shihyu

能把core和cl.exe发过来吗
ijkxyz@msn.com


   
作者: prc    时间: 2015-12-25 19:32
回复 15# hnwyllmm
请更新代码

   
作者: shihyu    时间: 2015-12-27 11:07
linux-2.6.32.69  可以

可是測試 linux-4.3.3 會失敗
  1. ../codeless/y-Make --yz-cc=gcc --yz-postprocess=cl
  2. function gcc()
  3. {
  4.     /home/shihyu/github/codeless/cl.exe --yz-no-output --yz-save-dep=/home/shihyu/github/linux-4.3.3/cl/depends.txt --yz-save-cl=/home/shihyu/github/linux-4.3.3/cl/commands.txt --yz-save-condvals=/home/shihyu/github/linux-4.3.3/cl/condvals.txt --yz-top-dir=/home/shihyu/github/linux-4.3.3 --yz-cc=gcc --yz-verbose=0 "$@" || return $?
  5.     /usr/bin/gcc $*
  6. }
  7. function g++()
  8. {
  9.     /home/shihyu/github/codeless/cl.exe --yz-no-output --yz-save-dep=/home/shihyu/github/linux-4.3.3/cl/depends.txt --yz-save-cl=/home/shihyu/github/linux-4.3.3/cl/commands.txt --yz-save-condvals=/home/shihyu/github/linux-4.3.3/cl/condvals.txt --yz-top-dir=/home/shihyu/github/linux-4.3.3 --yz-cc=gcc --yz-verbose=0 "$@" || return $?
  10.     /usr/bin/g++ $*
  11. }
  12. export -f gcc g++
  13. /usr/bin/make SHELL=bash

  14.   CHK     include/config/kernel.release
  15.   CHK     include/generated/uapi/linux/version.h
  16.   CHK     include/generated/utsrelease.h
  17.   CHK     include/generated/bounds.h
  18.   CHK     include/generated/timeconst.h
  19.   CC      arch/x86/kernel/asm-offsets.s
  20.   include/asm-generic/pgtable.h:12
  21.   ./arch/x86/include/asm/pgtable.h:884
  22.   include/linux/mm.h:55
  23.   include/linux/suspend.h:8
  24.   arch/x86/kernel/asm-offsets.c:12
  25. defined following -
  26. make[1]: *** [arch/x86/kernel/asm-offsets.s] Error 120
  27. make: *** [prepare0] Error 2
复制代码

作者: prc    时间: 2015-12-31 15:43
回复 19# shihyu
已修复




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2