美文网首页
Linux代码阅读准备

Linux代码阅读准备

作者: 99金材程序员 | 来源:发表于2020-05-05 20:58 被阅读0次

Linux内核的代码规模庞大。现在发布的linux-5.6.2版本压缩包有100多M,包含7W多个文件,代码规模100多万行。并且Linux支持的CPU体系众多。每种CPU下面还有不少不同的平台实现。再加上Linux的各种功能模块里面还有大量的可以改变的配置参数。在阅读代码的时候会发现一个功能有多个不同的实现,代码里面有大量的CONFIG_XXX的宏定义。这些定义都与内核的具体的配置有关,需要结合编译时的具体配置才能确定值。在阅读代码时很不方便。

spin_lock() 为例。根据内核编译的时候是否定义了
CONFIG_SMP CONFIG_INLINE_SPIN_LOCK CONFIG_LOCKDEP这些宏。spin_lock()的定义完全不同。

提供一个小程序。用内核编译生成的过程文件,对内核内核源代码进行筛选。并对source insight4工程进行一些配置。使内核代码更方便阅读。

build_kernel_src.py:

#!/usr/bin/python

import sys
import os
import re
import tempfile
from xml.etree.ElementTree import Element,ElementTree

def create_condition_xml(gcc, conf):
    for one in open('./include/generated/autoconf.h'):
        m = re.match('#define (.*) ', one)
        if m == None: continue
        conf.discard(m.group(1))
    root = Element('SourceInsightParseConditions')
    tree = ElementTree(root)
    root.attrib['AppVer']='4.00.0096'
    root.attrib['AppVerMinReader']='4.00.0019'
    child = Element('ParseConditions')
    root.append(child)
    defines = Element('Defines')
    child.append(defines)

    for one in conf: 
        define=Element('define')
        define.attrib['id']=f'{one}'
        define.attrib['value']='0'
        defines.append(define)
    tree.write(f'{gcc}.conditions.xml', encoding='UTF-8', xml_declaration=True)
    return f'{gcc}.conditions.xml'

def parse_conf_xxx(fn, conf):
    try:
        with open(fn, 'r') as fn: text=fn.read()
        for one in re.finditer(r'[ (](CONFIG_[\da-zA-Z_]*)', text):
            text=one.group(1)
            #text=re.sub('[()\s]', '', text)
            conf.add(text)
    except:return

def create_gcc_predefine(res, cpio):
    with open('init/.main.o.cmd', 'r') as fn: cmd = fn.read()
    m = re.match(r'cmd_.*:= (.*?) -D"?KBUILD_MODFILE', cmd)
    cmd = m.group(1)
    cmd = re.sub(' -Wp,.*? ', ' ', cmd)
    for one in re.findall('-include .*? ', cmd):
        fn=one.replace('-include','').strip()
        res.add(fn); print(fn, file=cpio)
    cmd = re.sub('-include .*? ', ' ', cmd)
    tmp = re.match('(.*?) ', cmd).group(0);
    tmp = f'{tmp} -v 2>&1|grep Target'
    tmp += '|awk \'{print $2}\''
    with os.popen(tmp) as fn: gcc = fn.read().strip()
    fn = f'{gcc}.h'
    tmp = tempfile.mkstemp(suffix='.c')
    os.system(f'{cmd} -E -dMM {tmp[1]} >{fn}')
    os.unlink(tmp[1])
    res.add(fn); print(fn, file=cpio)
    return gcc

def process_dotcmd(fn, res, cpio, srcdir, objdir, conf, debug=False):
    if(debug): print(f'{fn}', file=sys.stderr)
    with open(fn, 'r') as fn: text = fn.read()
    #get source first
    m = re.search(r'source_(.*) := (.*)', text)
    if m != None: 
        obj=m.group(1)
        src=m.group(2)
        if not os.path.exists(src):
            obj = os.path.dirname(obj.replace(objdir, srcdir))
            src = os.path.join(obj, os.path.basename(src))
        src = os.path.normpath(src)
        if os.path.isabs(src): src=os.path.relpath(src, objdir)
        if not src in res:
            if debug: print(f'src={src}', file=sys.stderr)
            res.add(src); print(src, file=cpio); parse_conf_xxx(src, conf)
    #get dep files
    m = re.search(r'deps_.* := \\\n(.*)\\\n', text, re.M|re.S)
    if m == None: return
    text = m.group(1)
    text = re.split(r'\n', text);
    for line in text:
        m = re.match(r'.*\s(.*?)[\s)]', line)
        if m == None:continue
        fn = m.group(1)
        if srcdir in fn:
            fn=os.path.relpath(fn, objdir)
        try: 
            fn=os.path.normpath(fn)
            if not os.path.getsize(fn) >0: continue
            if fn in res: continue
            res.add(fn); print(fn, file=cpio); parse_conf_xxx(fn, conf)
            if debug: print(f'dep={fn}', file=sys.stderr)
        except: pass

if __name__ ==  "__main__":
    res = set()
    conf = set()
    if(len(sys.argv)<=2 or (not os.path.isdir(sys.argv[1]))):
        print(f'usage {sys.argv[0]}'+' {dir} {cpiofile}')
        sys.exit()
    if not os.path.islink(f'{sys.argv[1]}/source'):
        print(f'{sys.argv[1]}/source not link to kernel source')
        exit(-1)

    objdir=os.path.realpath(f'{sys.argv[1]}')
    srcdir=os.path.realpath(f'{sys.argv[1]}/source')
    objdir=os.path.normpath(objdir)
    srcdir=os.path.normpath(srcdir)
    cpio=os.path.realpath(sys.argv[2])
    os.chdir(objdir)

    #cmd = f'find {sys.argv[1]}/a -type f -name ".*.cmd"'
    cmd = f'find {sys.argv[1]} -type f -name ".*.cmd"'
    cmd = f'{cmd} -and -not -name "*built-in.*"'
    cmd = f'{cmd} -and -not -name ".*.mod.cmd"'
    cmd = f'{cmd} -and -not -path "./tools/*"'
    cmd = f'{cmd} -and -not -path "./scripts/*"'
    cmd = f'{cmd} -and -not -path "./usr/*"'
    cpio = f'cpio --no-absolute-filenames -o >{cpio} 2>/dev/null'
    cpio = os.popen(cpio, mode='w')

    # save .config first
    print('./.config', file=cpio)
    print('./System.map', file=cpio)
    fn = './include/generated/autoconf.h'
    res.add(fn); print(fn, file=cpio)
    gcc = create_gcc_predefine(res, cpio)
    ll = 0
    for fn in os.popen(cmd):
        fn = fn.strip()
        process_dotcmd(fn, res, cpio, srcdir, objdir, conf)
        print(f'\r{len(res)}+%-*s'%(ll,fn), file=sys.stderr, end='', 
              flush=True)
        ll = len(fn)
    fn = create_condition_xml(gcc, conf)
    print(fn, file=cpio)
    cpio.close()
    print(f'\n{len(res)} files')
    os.unlink(fn) #condition.xml
    os.unlink(f'{gcc}.h') # x86_64-linux-gnu.h


程序生成一个cpio文件。解开之后建立source insight4工程。并通过source insight工程的”Edit Conditon"导入cpio中的xml定义:


image-20200505202845978.png

--->>Edit List...

image-20200505202955261.png

--->>Import...


image-20200505203041518.png

--->>Load...

image-20200505203118336.png image-20200505200445146.png

好了。这下Linux的代码就更好阅读了。

相关文章

网友评论

      本文标题:Linux代码阅读准备

      本文链接:https://www.haomeiwen.com/subject/dqmvghtx.html