荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: sound (sound), 信区: Linux
标  题: 如何阅读源代码
发信站: 荔园晨风BBS站 (Tue Apr  9 10:01:20 2002), 转信

写在前面的话:
自从我在linuxaid.com.cn上发表一些文章开始,就不断的有网友发来电子邮件,
或者是就其中某些问题进行探讨,或者是查询其他文章的地址(往往这些网友看的
是其他网站转载的我的文章),我很高兴自己写出的文章有这么多人回应,因为这
是对我最好的赞赏,也很高兴有这么多人对我的文章感兴趣。但是常常因为工作关
系。有很多邮件是询问我的其他文章在哪里能够找到,我不一定能够及时回复,也
觉得回复同样的问题比较麻烦,所以在这里重复一下,我为linuxaid.com.cn写的
文章都能在www.linuxaid.com.cn的应用开发栏目中找到,我的一部分文章收集在
bambi.may10.ca/~ariesram/articles下面(这是一个很简陋的网页,只有文本格
式的文章,也欢迎有兴趣的网友帮我设计一下网页),我的邮件地址:
ariesram@linuxaid.com.cn, 或者ariesram@may10.ca。请转载文章的网站保留这
一说明,欢迎网友写email给我探讨问题,虽然我不能保证能及时回复。
正文:

由于工作的关系,我常常需要读一些源代码,并在上面做一些修改并且拿来使用,
或者是借鉴其中的某些部分。可以说,open source对于程序员来说,是很有意义
的事情。根据我的经验,读源代码,至少有3个好处。第一个好处是可以学习到很
多编程的方法,看好的源代码,对于提高自己的编程水平,比自己写源代码的帮助
更大。当然不是说不用自己写,而是说,自己写代码的同时,可以从别人写的好的
源代码中间学习到更多的编程方法和技巧。第二个好处是,可以提高自己把握大规
模源代码的能力。一个比较大型的程序,往往都是经过了很多个版本很长的时间,
有很多人参与开发,修正错误,添加功能而发展起来的。所以往往源代码的规模都
比较大,少则10-100多k, 多的有好几十个MB. 在阅读大量源代码的时候,能够提
高自己对大的软件的把握能力,快速了解脉络,熟悉细节,不仅仅是编程技巧,还
能在程序的架构,设计方面提高自己的能力。(这里说一句题外话,<<设计模式
>>这本书相信很多人都看过,而且很多人对它推崇备至,奉为经典。现在也出了不
少书,都是冠以"设计模式"这一名称。在书中就提到,设计模式并不是一本教材,
不是教你如何去编程序,而是把平时编程中一些固定的模式记录下来,加以不断的
测试和改进,分发给广大程序员的一些经验之谈。我在看这本书的时候,有一些地
方一些设计方法往往让我有似曾相识的感觉,另外一些则是我以前就常常用到的。
而这些经验的获得,一部分得益于自己的编码过程,另外一个很重要的来源就是阅
读别人写的源代码。)阅读源代码第三个好处,就是获得一些好的思想。比如,有
很多人在开始一个软件项目之前都喜欢到sourceforge.net上去找一下,是否有人
以前做过相同或者相似的软件,如果有,则拿下来读一读,可以使自己对这个软件
项目有更多更深的认识。我以前曾经想找一本关于如何阅读源代码的书来看看,却
没有找到。相反,倒是找到了不少分析源代码的书,比如Linux kernel, Apache
source, 等等。所以我想,为什么不自己来写下一些经验和大家交流呢?(当然不
是写书,没有那个能力也没有那个时间。)所以在这里我准备用一个例子来写一下
如何阅读源代码,分享一些经验,算是抛砖引玉吧!
我找的例子是一个统计日志的工具,webalizer. (这个工具我以前用过,似乎记得
以前的版本是用perl写的,不知道现在为什么作者把它完全修改成了C,可能是为
了效率,也可能根本就是我记错了。)之所以选择这个软件来作为例子,一方面是
因为它是用C写的,流程比较简单,没有C++的程序那么多的枝节,而且软件功能不
算复杂,代码规模不大,能够在一篇文章的篇幅里面讲完; 另外一个方面是因为恰
巧前段时间我因为工作的关系把它拿来修改了一下,刚看过,还没有忘记。 :-)我
采用的例子是webalizer2.01-09, 也可以到它的网站http://www.mrunix.
net/webalizer/ 下载最新的版本。这是一个用C写的,处理文本文件(简单的说是
这样,实际上它支持三种日志文本格式:CLF, FTP, SQUID), 并且用html的方式输
出结果。读者可以自己去下载它的源代码包,并一边读文章,一边看程序。解压缩
它的tar包(我download的是它的源代码tar包),在文件目录中看到这样的结果:

$ ls
aclocal.m4 dns_resolv.c lang output.h webalizer.1
CHANGES dns_resolv.h lang.h parser.c webalizer.c
configure graphs.c linklist.c parser.h webalizer.h
configure.in graphs.h linklist.h preserve.c webalizer_lang.h
COPYING hashtab.c Makefile.in preserve.h webalizer.LSM
Copyright hashtab.h Makefile.std README webalizer.png
country-codes.txt INSTALL msfree.png README.FIRST
DNS.README install-sh output.c sample.conf
首先,我阅读了它的README(这是很重要的一个环节), 大体了解了软件的功能,
历史状况,修改日志,安装方法等等。然后是安装并且按照说明中的缺省方式来运
行它,看看它的输出结果。(安装比较简单,因为它带了一个configure, 在没有特
殊情况出现的时候,简单的./configure, make, make install就可以安装好。)然
后就是阅读源代码了。我从makefile开始入手(我觉得这是了解一个软件的最好的
方法)在makefile开头,有这些内容:
prefix = /usr/local
exec_prefix = ${prefix}

BINDIR = ${exec_prefix}/bin
MANDIR = ${prefix}/man/man1
ETCDIR = /etc
CC = gcc
CFLAGS = -Wall -O2
LIBS = -lgd -lpng -lz -lm
DEFS = -DETCDIR="/etc" -DHAVE_GETOPT_H=1 -DHAVE_MATH_H=1
LDFLAGS=
INSTALL= /usr/bin/install -c
INSTALL_PROGRAM=${INSTALL}
INSTALL_DATA=${INSTALL} -m 644

# where are the GD header files?
GDLIB=/usr/include
这些定义了安装的路径,执行程序的安装路径,编译器,配置文件的安装路径,编
译的选项,安装程序,安装程序的选项等等。要注意的是,这些并不是软件的作者
写的,而是./configure的输出结果。呵呵. :-)下面才是主题内容,也是我们关心
的。
# Shouldn have to touch below here!

all: webalizer

webalizer: webalizer.o webalizer.h hashtab.o hashtab.h
linklist.o linklist.h preserve.o preserve.h
dns_resolv.o dns_resolv.h parser.o parser.h
output.o output.h graphs.o graphs.h lang.h
webalizer_lang.h
$(CC) ${LDFLAGS} -o webalizer webalizer.o hashtab.o linklist.o preserv

e.o parser.o output.o dns_resolv.o graphs.o ${LIBS}
rm -f webazolver
ln -s webalizer webazolver

webalizer.o: webalizer.c webalizer.h parser.h output.h preserve.h
graphs.h dns_resolv.h webalizer_lang.h
$(CC) ${CFLAGS} ${DEFS} -c webalizer.c

parser.o: parser.c parser.h webalizer.h lang.h
$(CC) ${CFLAGS} ${DEFS} -c parser.c

hashtab.o: hashtab.c hashtab.h dns_resolv.h webalizer.h lang.h
$(CC) ${CFLAGS} ${DEFS} -c hashtab.c
linklist.o: linklist.c linklist.h webalizer.h lang.h
$(CC) ${CFLAGS} ${DEFS} -c linklist.c

output.o: output.c output.h webalizer.h preserve.h
hashtab.h graphs.h lang.h
$(CC) ${CFLAGS} ${DEFS} -c output.c

preserve.o: preserve.c preserve.h webalizer.h parser.h
hashtab.h graphs.h lang.h
$(CC) ${CFLAGS} ${DEFS} -c preserve.c

dns_resolv.o: dns_resolv.c dns_resolv.h lang.h webalizer.h
$(CC) ${CFLAGS} ${DEFS} -c dns_resolv.c

graphs.o: graphs.c graphs.h webalizer.h lang.h
$(CC) ${CFLAGS} ${DEFS} -I${GDLIB} -c graphs.c

好了,不用再往下看了,这些就已经足够了。从这里我们可以看到这个软件的几个
源代码文件和他们的结构。webalizer.c是主程序所在的文件,其他的是一些辅助
程序模块。对比一下目录里面的文件,
$ ls *.c *.h
dns_resolv.c graphs.h lang.h output.c parser.h webalizer.c
dns_resolv.h hashtab.c linklist.c output.h preserve.c webalizer.h
graphs.c hashtab.h linklist.h parser.c preserve.h webalizer_lang.h
于是,让我们从webalizer.c开始吧。
作为一个C程序,在头文件里面,和C文件里面定义的extern变量,结构等等肯定不
会少,但是,单独看这些东西我们不可能对这个程序有什么认识。所以,从main函
数入手,逐步分析,在需要的时候再回头来看这些数据结构定义才是好的方法。(
顺便说一句,Visual C++, 等windows下的IDE工具提供了很方便的方法来获取函数
列表,C++的类列表以及资源文件,对于阅读源代码很有帮助。Unix/Linux也有这
些工具,但是,我们在这里暂时不说,而只是通过最简单的文本编辑器vi来讲)。
跳过webalizer.c开头的版权说明部分(GPL的),和数据结构定义,全局变量声明
部分,直接进入main()函数。在函数开头,我们看到:
/* initalize epoch */
epoch=jdate(1,1,1970); /* used for timestamp adj. */

/* add default index. alias */
add_nlist("index.",&index_alias);
这两个函数暂时不用仔细看,后面会提到,略过。
sprintf(tmp_buf,"%s/webalizer.conf",ETCDIR);
/* check for default config file */
if (!access("webalizer.conf",F_OK))
get_config("webalizer.conf");
else if (!access(tmp_buf,F_OK))
get_config(tmp_buf);
从注释和程序本身可以看出,这是查找是否存在一个叫做webalizer.conf的配置文
件,如果当前目录下有,则用get_config来读入其中内容,如果没有,则查找
ETCDIR/webalizer.conf是否存在。如果都没有,则进入下一部分。(注意:ETCDIR
 = @ETCDIR@在makefile中有定义)
/* get command line options */
opterr = 0; /* disable parser errors */
while ((i=getopt(argc,argv,"a:A:c:C:dD:e:E:fF:g:GhHiI:l:Lm:M:n:N:o:pP:
qQr:R:s:S:t:Tu:U:vVx:XY"))!=EOF)
{
switch (i)
{
case a: add_nlist(optarg,&hidden_agents); break; /* Hide agents */
case A: ntop_agents=atoi(optarg); break; /* Top agents */
case c: get_config(optarg); break; /* Config file */
case C: ntop_ctrys=atoi(optarg); break; /* Top countries */
case d: debug_mode=1; break; /* Debug */
case D: dns_cache=optarg; break; /* DNS Cache filename */
case e: ntop_entry=atoi(optarg); break; /* Top entry pages */
case E: ntop_exit=atoi(optarg); break; /* Top exit pages */
case f: fold_seq_err=1; break; /* Fold sequence errs */
case F: log_type=(optarg[0]==f)?
LOG_FTP:(optarg[0]==s)?
LOG_SQUID:LOG_CLF; break; /* define log type */
case g: group_domains=atoi(optarg); break; /* GroupDomains (0=no) */
case G: hourly_graph=0; break; /* no hourly graph */
case h: print_opts(argv[0]); break; /* help */
case H: hourly_stats=0; break; /* no hourly stats */
case i: ignore_hist=1; break; /* Ignore history */
case I: add_nlist(optarg,&index_alias); break; /* Index alias */
case l: graph_lines=atoi(optarg); break; /* Graph Lines */
case L: graph_legend=0; break; /* Graph Legends */
case m: visit_timeout=atoi(optarg); break; /* Visit Timeout */
case M: mangle_agent=atoi(optarg); break; /* mangle user agents */
case : hname=optarg; break; /* Hostname */
case N: dns_children=atoi(optarg); break; /* # of DNS children */
case o: out_dir=optarg; break; /* Output directory */
case p: incremental=1; break; /* Incremental run */
case P: add_nlist(optarg,&page_type); break; /* page view types */
case q: verbose=1; break; /* Quiet (verbose=1) */
case Q: verbose=0; break; /* Really Quiet */
case : add_nlist(optarg,&hidden_refs); break; /* Hide referrer */
case R: ntop_refs=atoi(optarg); break; /* Top referrers */
case s: add_nlist(optarg,&hidden_sites); break; /* Hide site */
case S: ntop_sites=atoi(optarg); break; /* Top sites */
case : msg_title=optarg; break; /* Report title */
case T: time_me=1; break; /* TimeMe */
case u: add_nlist(optarg,&hidden_urls); break; /* hide URL */
case U: ntop_urls=atoi(optarg); break; /* Top urls */
case v:
case V: print_version(); break; /* Version */
case x: html_ext=optarg; break; /* HTML file extension */
case X: hide_sites=1; break; /* Hide ind. sites */
case Y: ctry_graph=0; break; /* Supress ctry graph */
}
}

if (argc - optind != 0) log_fname = argv[optind];
if ( log_fname && (log_fname[0]==-)) log_fname=NULL; /* force STDIN?
*/

/* check for gzipped file - .gz */
if (log_fname) if (!strcmp((log_fname+strlen(log_fname)-3),".gz"))
gz_log=1;
这一段是分析命令行参数及开关。(getopt()的用法我在另外一篇文章中讲过,这
里就不再重复了。)可以看到,这个软件虽然功能不太复杂,但是开关选项还是不
少。大多数的unix/linux程序的开头部分都是这个套路,初始化配置文件,并且读
入分析命令行。在这段程序中,我们需要注意一个函数:add_nlist().
print_opts(), get_config()等等一看就明白,就不用多讲了。这里我们已经是第
二次遇到add_nlist这个函数了,就仔细看看吧。
$ grep add_nlist *.h
linklist.h:extern int add_nlist(char *, NLISTPTR *); /* add list item */

可以发现它定义在linklist.h中。
在这个h文件中,当然会有一些数据结构的定义,比如:
struct nlist { char string[80]; /* list struct for HIDE items */
struct nlist *next; };
typedef struct nlist *NLISTPTR;

struct glist { char string[80]; /* list struct for GROUP items */
char name[80];
struct glist *next; };
typedef struct glist *GLISTPTR;
这是两个链表结构。还有
extern GLISTPTR group_sites ; /* "group" lists */
extern GLISTPTR group_urls ;
extern GLISTPTR group_refs ;
这些都是链表, 太多了,不用一一看得很仔细,因为目前也看不出来什么东西。
当然要注意它们是extern的, 也就是说,可以在其他地方(文件)看到它们的数值
(类似于C++中的public变量)。这里还定义了4个函数:
extern char *isinlist(NLISTPTR, char *); /* scan list for str */
extern char *isinglist(GLISTPTR, char *); /* scan glist for str */
extern int add_nlist(char *, NLISTPTR *); /* add list item */
extern int add_glist(char *, GLISTPTR *); /* add group list item */
注意,这些都是extern的,也就是说,可以在其他地方见到它们的调用(有点相当
于C++中的public函数)。再来看看linklist.c,
NLISTPTR new_nlist(char *); /* new list node */
void del_nlist(NLISTPTR *); /* del list */

GLISTPTR new_glist(char *, char *); /* new group list node */
void del_glist(GLISTPTR *); /* del group list */

int isinstr(char *, char *);
这5个函数是内部使用的(相当于C++中的private), 也就是说,这些函数只被
isinlist(NLISTPTR, char *), isinglist(GLISTPTR, char *),
add_nlist(char *, NLISTPTR *), add_glist(char *, GLISTPTR *)调用,而不会
出现在其他地方。所以,我们先来看这几个内部函数。举例来说,
add_nlist(char *)
NLISTPTR new_nlist(char *str)
{
NLISTPTR newptr;

if (sizeof(newptr->string) < strlen(str))
{
if (verbose)
fprintf(stderr,"[new_nlist] %s ",msg_big_one);
}
if (( newptr = malloc(sizeof(struct nlist))) != NULL)
{strncpy(newptr->string, str, sizeof(newptr->string));
newptr->next=NULL;}
return newptr;
}
这个函数分配了一个struct nlist, 并且把其中的string赋值为str, next赋值为
NULL.这实际上是创建了链表中的一个节点。verbose是一个全局变量,定义了输出
信息的类型,如果verbose为1,则输出很详细的信息,否则输出简略信息。这是为
了调试或者使用者详细了解程序情况来用的。不是重要内容,虽然我们常常可以在
这个源程序的其他地方看到它。另外一个函数:
void del_nlist(NLISTPTR *list)
{
NLISTPTR cptr,nptr;

cptr=*list;
while (cptr!=NULL)
{
nptr=cptr->next;
free(cptr);
cptr=nptr;
}
}
这个函数删除了一个nlist(也可能是list所指向的那一个部分开始知道链表结尾
),比较简单。看完了这两个内部函数,可以来看
/*********************************************/
/* ADD_NLIST - add item to FIFO linked list */
/*********************************************/

int add_nlist(char *str, NLISTPTR *list)
{
NLISTPTR newptr,cptr,pptr;

if ( (newptr = new_nlist(str)) != NULL)
{
if (*list==NULL) *list=newptr;
else
{
cptr=pptr=*list;
while(cptr!=NULL) { pptr=cptr; cptr=cptr->next; };
pptr->next = newptr;
}
}
return newptr==NULL;
}
这个函数是建立了一个新的节点,把参数str赋值给新节点的string, 并把它连接
到list所指向链表的结尾。另外的三个函数:new_glist(), del_glist(),
add_glist()完成的功能和上述三个差不多,所不同的只是它们所处理的数据结构
不同。看完了这几个函数,我们回到main程序。接下来是,
/* setup our internal variables */
init_counters(); /* initalize main counters */
我们所阅读的这个软件是用来分析日志并且做出统计的,那么这个函数的名字已经
告诉了我们,这是一个初始化计数器的函数。简略的看看吧!
$ grep init_counters *.h
webalizer.h:extern void init_counters();
在webalizer.c中找到:
void init_counters()
{
int i;
for (i=0;i for (i=0;i<31;i++) /* monthly totals */
{
tm_xfer[i]=0.0;
tm_hit[i]=tm_file[i]=tm_site[i]=tm_page[i]=tm_visit[i]=0;
}
for (i=0;i<24;i++) /* hourly totals */
{
th_hit[i]=th_file[i]=th_page[i]=0;
th_xfer[i]=0.0;
}
......
}略过去一大串代码,不用看了,肯定是计数器清0。在主程序中,接下来是:
if (page_type==NULL) /* check if page types present */
{
if ((log_type == LOG_CLF) || (log_type == LOG_SQUID))
{
add_nlist("htm*" ,&page_type); /* if no page types specified, we */
add_nlist("cgi" ,&page_type); /* use the default ones here... */
if (!isinlist(page_type,html_ext)) add_nlist(html_ext,&page_type);
}
else add_nlist("txt" ,&page_type); /* FTP logs default to .txt */
}
page_type这个变量在前面见过,
case P: add_nlist(optarg,&page_type); break; /* page view types
根据在最开始读过的README文件,这个page_type是用来定义处理的页面的类型的
。在README文件中,
-P name Page type. This is the extension of files you consider to
be pages for Pages calculations (sometimes called pageviews).
The default is htm* and cgi (plus whatever HTMLExtension
you specified if it is different). Don use a period!
我们在程序中也可以看到,如果没有在命令行中或者config文件中指定,则根据处
理的日志文件的类型来添加缺省的文件类型。比如对于CLF文件(WWW日志),处理
html, htm, cgi文件
if (log_type == LOG_FTP)
{
/* disable stuff for ftp logs */
ntop_entry=ntop_exit=0;
ntop_search=0;
}
else
.....

这一段是对于FTP的日志格式,设置搜索列表。
for (i=0;i {
sm_htab[i]=sd_htab[i]=NULL; /* initalize hash tables */
um_htab[i]=NULL;
rm_htab[i]=NULL;
am_htab[i]=NULL;
sr_htab[i]=NULL;
}
清空哈西表,为下面即将进行的排序工作做好准备。关于哈西表,这是数据结构中
常用的一种用来快速排序的结构,如果不清楚,可以参考相关书籍,比如清华的
<<数据结构>>教材或者<<数据结构的C++实现>>等书。
if (verbose>1)
{
uname(&system_info);
printf("Webalizer V%s-%s (%s %s) %s ",
version,editlvl,system_info.sysname,
system_info.release,language);
}
这一段,是打印有关系统的信息和webalizer程序的信息(可以参考uname的函数说
明)。
#ifndef USE_DNS
if (strstr(argv[0],"webazolver")!=0)
{
printf("DNS support not present, aborting... ");
exit(1);
}
#endif /* USE_DNS */
这一段,回忆我们在看README文件的时候,曾经提到过可以在编译的时候设置选项
开关来设定DNS支持,在源代码中可以看到多次这样的代码段出现,如果不指定
DNS支持,这些代码段则会出现(ifdef)或者不出现(ifndef).下面略过这些代码段
,不再重复。
/* open log file */
if (gz_log)
{
gzlog_fp = gzopen(log_fname,"rb");
if (gzlog_fp==Z_NULL)
{
/* Error: Can open log file ... */
fprintf(stderr, "%s %s ",msg_log_err,log_fname);
exit(1);
}
}
else
{
if (log_fname)
{
log_fp = fopen(log_fname,"r");
if (log_fp==NULL)
{
/* Error: Can open log file ... */
fprintf(stderr, "%s %s ",msg_log_err,log_fname);
exit(1);
}
}
}
这一段,回忆在README文件中曾经读到过,如果log文件是gzip压缩格式,则用
gzopen函数打开(可以猜想gz***是一套针对gzip压缩格式的实时解压缩函数),
如果不是,则用fopen打开。
/* switch directories if needed */
if (out_dir)
{
if (chdir(out_dir) != 0)
{
/* Error: Can change directory to ... */
fprintf(stderr, "%s %s ",msg_dir_err,out_dir);
exit(1);
}
}
同样,回忆在README文件中读到过,如果参数行有-o out_dir, 则将输出结果到该
目录,否则,则输出到当前目录。在这一段中,如果输出目录不存在
(chdir(out_dir) != 0)则出错。
#ifdef USE_DNS
if (strstr(argv[0],"webazolver")!=0)
{
if (!dns_children) dns_children=5; /* default dns children if needed
*/
if (!dns_cache)
{
/* No cache file specified, aborting... */
fprintf(stderr,"%s ",msg_dns_nocf); /* Must have a cache file */
exit(1);
}
}
......
在上面曾经提到过,这是DNS解析的代码部分,可以略过不看,不会影响对整个程
序的理解。
/* prep hostname */
if (!hname)
{
if (uname(&system_info)) hname="localhost";
else hname=system_info.nodename;
}
这一段继续处理参数做准备工作。如果在命令行中指定了hostname(机器名)则采
用指定的名称,否则调用uname查找机器名,如果没有,则用localhost来作为机器
名。(同样在README中说得很详细)
/* get past history */
if (ignore_hist) {if (verbose>1) printf("%s ",msg_ign_hist); }
else get_history();
如果在命令行中指定了忽略历史文件,则不读取历史文件,否则调用
get_history()来读取历史数据。在这里,我们可以回想在README文件中同样说过
这一细节,在命令行或者配置文件中都能指定这一开关。需要说明的是,我们在这
里并不一定需要去看get_history这一函数,因为从函数的名称,README文件和程
序注释都能很清楚的得知这一函数的功能,不一定要去看代码。而如果要猜想的话
,也可以想到,history是webalizer在上次运行的时候记录下来的一个文件,而这
个文件则是去读取它,并将它的数据包括到这次的分析中去。不信,我们可以来看
看。
void get_history()
{
int i,numfields;
FILE *hist_fp;
char buffer[BUFSIZE];

/* first initalize internal array */
for (i=0;i<12;i++)
{
hist_month[i]=hist_year[i]=hist_fday[i]=hist_lday[i]=0;
hist_hit[i]=hist_files[i]=hist_site[i]=hist_page[i]=hist_visit[i]=0;
hist_xfer[i]=0.0;
}

hist_fp=fopen(hist_fname,"r");

if (hist_fp)
{
if (verbose>1) printf("%s %s ",msg_get_hist,hist_fname);
while ((fgets(buffer,BUFSIZE,hist_fp)) != NULL)
{
i = atoi(buffer) -1;
if (i>11)
{
if (verbose)
fprintf(stderr,"%s (mth=%d) ",msg_bad_hist,i+1);
continue;
}

/* month# year# requests files sites xfer firstday lastday */
numfields = sscanf(buffer,"%d %d %lu %lu %lu %lf %d %d %lu %lu",
&hist_month[i],
&hist_year[i],
&hist_hit[i],
&hist_files[i],
&hist_site[i],
&hist_xfer[i],
&hist_fday[i],
&hist_lday[i],
&hist_page[i],
&hist_visit[i]);

if (numfields==8) /* kludge for reading 1.20.xx history files */
{
hist_page[i] = 0;
hist_visit[i] = 0;
}
}
fclose(hist_fp);
}
else if (verbose>1) printf("%s ",msg_no_hist);
}
/*********************************************/
/* PUT_HISTORY - write out history file */
/*********************************************/

void put_history()
{
int i;
FILE *hist_fp;

hist_fp = fopen(hist_fname,"w");

if (hist_fp)
{
if (verbose>1) printf("%s ",msg_put_hist);
for (i=0;i<12;i++)
{
if ((hist_month[i] != 0) && (hist_hit[i] != 0))
{
fprintf(hist_fp,"%d %d %lu %lu %lu %.0f %d %d %lu %lu ",
hist_month[i],
hist_year[i],
hist_hit[i],
hist_files[i],
hist_site[i],
hist_xfer[i],
hist_fday[i],
hist_lday[i],
hist_page[i],
hist_visit[i]);
}
}
fclose(hist_fp);
}
else
if (verbose)
fprintf(stderr,"%s %s ",msg_hist_err,hist_fname);
}
在preserve.c中,这两个函数是成对出现的。get_history()读取文件中的数据,
并将其记录到hist_开头的一些数组中去。而put_history()则是将一些数据记录到
同样的数组中去。我们可以推测得知,hist_数组是全局变量(在函数中没有定义
),也可以查找源代码验证。同样,我们可以找一找put_history()出现的地方,
来验证刚才的推测是否正确。在webalizer.c的1311行,出现:
month_update_exit(rec_tstamp); /* calculate exit pages */
write_month_html(); /* write monthly HTML file */
write_main_index(); /* write main HTML file */
put_history(); /* write history */
可以知道,推测是正确的。再往下读代码,
if (incremental) /* incremental processing? */
{
if ((i=restore_state())) /* restore internal data structs */
{
/* Error: Unable to restore run data (error num) */
/* if (verbose) fprintf(stderr,"%s (%d) ",msg_bad_data,i); */
fprintf(stderr,"%s (%d) ",msg_bad_data,i);
exit(1);
}
......
}
同样,这也是处理命令行和做数据准备,而且和get_history(), put_history()有
些类似,读者可以自己练习一下。下面,终于进入了程序的主体部分, 在做完了命
令行分析,数据准备之后,开始从日志文件中读取数据并做分析了。
/*********************************************/
/* MAIN PROCESS LOOP - read through log file */
/*********************************************/

while ( (gz_log)?(our_gzgets(gzlog_fp,buffer,BUFSIZE) != Z_NULL):
(fgets(buffer,BUFSIZE,log_fname?log_fp:stdin) != NULL))
我看到这里的时候,颇有一些不同意作者的这种写法。这一段while中的部分写的
比较复杂而且效率不高。因为从程序推断和从他的代码看来,作者是想根据日志文
件的类型不同来采用不同的方法读取文件,如果是gzip格式,则用our_gzgets来读
取其中一行,如果是普通的文本文件格式,则用fgets()来读取。但是,这段代码
是写在while循环中的,每次读取一行就要重复判断一次,明显是多余的而且降低
了程序的性能。可以在while循环之前做一次这样的判断,然后就不用重复了。
total_rec++;
if (strlen(buffer) == (BUFSIZE-1))
{
if (verbose)
{
fprintf(stderr,"%s",msg_big_rec);
if (debug_mode) fprintf(stderr,": %s",buffer);
else fprintf(stderr," ");
}

total_bad++; /* bump bad record counter */

/* get the rest of the record */
while ( (gz_log)?(our_gzgets(gzlog_fp,buffer,BUFSIZE)!=Z_NULL):
(fgets(buffer,BUFSIZE,log_fname?log_fp:stdin)!=NULL))
{
if (strlen(buffer) < BUFSIZE-1)
{
if (debug_mode && verbose) fprintf(stderr,"%s ",buffer);
break;
}
if (debug_mode && verbose) fprintf(stderr,"%s",buffer);
}
continue; /* go get next record if any */
}
这一段代码,读入一行,如果这一行超过了程序允许的最大字符数(则是错误的日
志数据纪录),则跳过本行剩下的数据,忽略掉(continue进行下一次循环)。同
时把total_bad增加一个。如果没有超过程序允许的最大字符数(则是正确的日志
数据纪录),则
/* got a record... */
strcpy(tmp_buf, buffer); /* save buffer in case of error */
if (parse_record(buffer)) /* parse the record */
将该数据拷贝到一个缓冲区中,然后调用parse_record()进行处理。我们可以同样
的推测一下,get_record()是这个程序的一个主要处理部分,分析了日志数据。在
parse_record.c中,有此函数,
/*********************************************/
/* PARSE_RECORD - uhhh, you know... */
/*********************************************/

int parse_record(char *buffer)
{
/* clear out structure */
memset(&log_rec,0,sizeof(struct log_struct));
/*
log_rec.hostname[0]=0;
log_rec.datetime[0]=0;
log_rec.url[0]=0;
log_rec.resp_code=0;
log_rec.xfer_size=0;
log_rec.refer[0]=0;
log_rec.agent[0]=0;
log_rec.srchstr[0]=0;
log_rec.ident[0]=0;
*/
#ifdef USE_DNS
memset(&log_rec.addr,0,sizeof(struct in_addr));
#endif

/* call appropriate handler */
switch (log_type)
{
default:
case LOG_CLF: return parse_record_web(buffer); break; /* clf */
case LOG_FTP: return parse_record_ftp(buffer); break; /* ftp */
case LOG_SQUID: return parse_record_squid(buffer); break; /* squid */
}
}

可以看到,log_rec是一个全局变量,该函数根据日志文件的类型,分别调用三种
不同的分析函数。在webalizer.h中,找到该变量的定义,从结构定义中可以看到
,结构定义了一个日志文件所可能包含的所有信息(参考CLF,FTP, SQUID日志文
件的格式说明)。
/* log record structure */
struct log_struct { char hostname[MAXHOST]; /* hostname */
char datetime[29]; /* raw timestamp */
char url[MAXURL]; /* raw request field */
int resp_code; /* response code */
u_long xfer_size; /* xfer size in bytes */
#ifdef USE_DNS
struct in_addr addr; /* IP address structure */
#endif /* USE_DNS */
char refer[MAXREF]; /* referrer */
char agent[MAXAGENT]; /* user agent (browser) */
char srchstr[MAXSRCH]; /* search string */
char ident[MAXIDENT]; }; /* ident string (user) */

extern struct log_struct log_rec;

先看一下一个parser.c用的内部函数,然后再来以parse_record_web()为例子看看
这个函数是怎么工作的,parse_record_ftp, parse_record_squid留给读者自己分
析作为练习。
/*********************************************/
/* FMT_LOGREC - terminate log fields w/zeros */
/*********************************************/

void fmt_logrec(char *buffer)
{
char *cp=buffer;
int q=0,b=0,p=0;

while (*cp != )
{
/* break record up, terminate fields with */
switch (*cp)
{
case : if (b || q || p) break; *cp=; break;
case ": q^=1; break;
case [: if (q) break; b++; break;
case ]: if (q) break; if (b>0) b--; break;
case (: if (q) break; p++; break;
case ): if (q) break; if (p>0) p--; break;
}
cp++;
}
}
从parser.h头文件中就可以看到,这个函数是一个内部函数,这个函数把一行字符
串中间的空格字符用字符(结束字符)来代替,同时考虑了不替换在双引号,方括
号,圆括号中间的空格字符以免得将一行数据错误的分隔开了。(请参考WEB日志
的文件格式,可以更清楚的理解这一函数)
int parse_record_web(char *buffer)
{
int size;
char *cp1, *cp2, *cpx, *eob, *eos;

size = strlen(buffer); /* get length of buffer */
eob = buffer+size; /* calculate end of buffer */
fmt_logrec(buffer); /* seperate fields with s */

/* HOSTNAME */
cp1 = cpx = buffer; cp2=log_rec.hostname;
eos = (cp1+MAXHOST)-1;
if (eos >= eob) eos=eob-1;

while ( (*cp1 != ) && (cp1 != eos) ) *cp2++ = *cp1++;
*cp2 = ;
if (*cp1 != )
{
if (verbose)
{
fprintf(stderr,"%s",msg_big_host);
if (debug_mode) fprintf(stderr,": %s ",cpx);
else fprintf(stderr," ");
}
while (*cp1 != ) cp1++;
}
if (cp1 < eob) cp1++;

/* skip next field (ident) */
while ( (*cp1 != ) && (cp1 < eob) ) cp1++;
if (cp1 < eob) cp1++;

/* IDENT (authuser) field */
cpx = cp1;
cp2 = log_rec.ident;
eos = (cp1+MAXIDENT-1);
if (eos >= eob) eos=eob-1;

while ( (*cp1 != [) && (cp1 < eos) ) /* remove embeded spaces */
{
if (*cp1==) *cp1= ;
*cp2++=*cp1++;
}
*cp2--=;

if (cp1 >= eob) return 0;

/* check if oversized username */
if (*cp1 != [)
{
if (verbose)
{
fprintf(stderr,"%s",msg_big_user);
if (debug_mode) fprintf(stderr,": %s ",cpx);
else fprintf(stderr," ");
}
while ( (*cp1 != [) && (cp1 < eob) ) cp1++;
}

/* strip trailing space(s) */
while (*cp2== ) *cp2--=;

/* date/time string */
cpx = cp1;
cp2 = log_rec.datetime;
eos = (cp1+28);
if (eos >= eob) eos=eob-1;

while ( (*cp1 != ) && (cp1 != eos) ) *cp2++ = *cp1++;
*cp2 = ;
if (*cp1 != )
{
if (verbose)
{
fprintf(stderr,"%s",msg_big_date);
if (debug_mode) fprintf(stderr,": %s ",cpx);
else fprintf(stderr," ");
}
while (*cp1 != ) cp1++;
}
if (cp1 < eob) cp1++;

/* minimal sanity check on timestamp */
if ( (log_rec.datetime[0] != [) ||
(log_rec.datetime[3] != /) ||
(cp1 >= eob)) return 0;

/* HTTP request */
cpx = cp1;
cp2 = log_rec.url;
eos = (cp1+MAXURL-1);
if (eos >= eob) eos = eob-1;

while ( (*cp1 != ) && (cp1 != eos) ) *cp2++ = *cp1++;
*cp2 = ;
if (*cp1 != )
{
if (verbose)
{
fprintf(stderr,"%s",msg_big_req);
if (debug_mode) fprintf(stderr,": %s ",cpx);
else fprintf(stderr," ");
}
while (*cp1 != ) cp1++;
}
if (cp1 < eob) cp1++;

if ( (log_rec.url[0] != ") ||
(cp1 >= eob) ) return 0;

/* response code */
log_rec.resp_code = atoi(cp1);

/* xfer size */
while ( (*cp1 != ) && (cp1 < eob) ) cp1++;
if (cp1 < eob) cp1++;
if (*cp1<||*cp1>9) log_rec.xfer_size=0;
else log_rec.xfer_size = strtoul(cp1,NULL,10);

/* done with CLF record */
if (cp1>=eob) return 1;

while ( (*cp1 != ) && (*cp1 != ) && (cp1 < eob) ) cp1++;
if (cp1 < eob) cp1++;
/* get referrer if present */
cpx = cp1;
cp2 = log_rec.refer;
eos = (cp1+MAXREF-1);
if (eos >= eob) eos = eob-1;

while ( (*cp1 != ) && (*cp1 != ) && (cp1 != eos) ) *cp2++ = *cp1++;
*cp2 = ;
if (*cp1 != )
{
if (verbose)
{
fprintf(stderr,"%s",msg_big_ref);
if (debug_mode) fprintf(stderr,": %s ",cpx);
else fprintf(stderr," ");
}
while (*cp1 != ) cp1++;
}
if (cp1 < eob) cp1++;

cpx = cp1;
cp2 = log_rec.agent;
eos = cp1+(MAXAGENT-1);
if (eos >= eob) eos = eob-1;

while ( (*cp1 != ) && (cp1 != eos) ) *cp2++ = *cp1++;
*cp2 = ;

return 1; /* maybe a valid record, return with TRUE */
}
该函数,一次读入一行(其实是一段日志数据中间的一个域,因为该行数据已经被
fmt_logrec分开成多行数据了。根据CLF中的定义,检查该数据并将其拷贝到
log_rec结构中去,如果检查该数据有效,则返回1。回到主程序,
/* convert month name to lowercase */
for (i=4;i<7;i++)
log_rec.datetime[i]=tolower(log_rec.datetime[i]);

/* get year/month/day/hour/min/sec values */
for (i=0;i<12;i++)
{
if (strncmp(log_month[i],&log_rec.datetime[4],3)==0)
{ rec_month = i+1; break; }
}

rec_year=atoi(&log_rec.datetime[8]); /* get year number (int) */
rec_day =atoi(&log_rec.datetime[1]); /* get day number */
rec_hour=atoi(&log_rec.datetime[13]); /* get hour number */
rec_min =atoi(&log_rec.datetime[16]); /* get minute number */
rec_sec =atoi(&log_rec.datetime[19]); /* get second number */
....
在parse_record分析完数据之后,做日期的分析,把日志中的月份等数据转换成机
器可读(可理解)的数据,并存入到log_rec中去。
if ((i>=12)||(rec_min>59)||(rec_sec>59)||(rec_year<1990))
{
total_bad++; /* if a bad date, bump counter */
if (verbose)
{
fprintf(stderr,"%s: %s [%lu]",
msg_bad_date,log_rec.datetime,total_rec);
......
如果日期,时间错误,则把total_bad计数器增加1,并且打印错误信息到标准错误
输出。
good_rec = 1;

/* get current records timestamp (seconds since epoch) */
req_tstamp=cur_tstamp;
rec_tstamp=((jdate(rec_day,rec_month,rec_year)-epoch)*86400)+
(rec_hour*3600)+(rec_min*60)+rec_sec;

/* Do we need to check for duplicate records? (incremental mode) */
if (check_dup)
{
/* check if less than/equal to last record processed */
if ( rec_tstamp <= cur_tstamp )
{
/* if it is, assume we have already processed and ignore it */
total_ignore++;
continue;
}
else
{
/* if it isn .. disable any more checks this run */
check_dup=0;
/* now check if its a new month */
if (cur_month != rec_month)
{
clear_month();
cur_sec = rec_sec; /* set current counters */
cur_min = rec_min;
cur_hour = rec_hour;
cur_day = rec_day;
cur_month = rec_month;
cur_year = rec_year;
cur_tstamp= rec_tstamp;
f_day=l_day=rec_day; /* reset first and last day */
}
}
}

/* check for out of sequence records */
if (rec_tstamp/3600 < cur_tstamp/3600)
{
if (!fold_seq_err && ((rec_tstamp+SLOP_VAL)/3600 { total_ignore++;
continue; }
else
{
rec_sec = cur_sec; /* if folding sequence */
rec_min = cur_min; /* errors, just make it */
rec_hour = cur_hour; /* look like the last */
rec_day = cur_day; /* good records timestamp */
rec_month = cur_month;
rec_year = cur_year;
rec_tstamp= cur_tstamp;
}
}
cur_tstamp=rec_tstamp; /* update current timestamp */
如果该日期、时间没有错误,则该数据是一个好的数据,将good_record计数器加
1,并且检查时间戳,和数据是否重复数据。这里有一个函数,jdate()在主程序一
开头我们就遇到了,当时跳了过去没有深究,这里留给读者做一个练习。(提示:
该函数根据一个日期产生一个字符串,这个字符串是惟一的,可以检查时间的重复
性,是一个通用函数,可以在别的程序中拿来使用)
/*********************************************/
/* DO SOME PRE-PROCESS FORMATTING */
/*********************************************/

/* fix URL field */
cp1 = cp2 = log_rec.url;
/* handle null - case here... */
if (*++cp1 == -) { *cp2++ = -; *cp2 = ; }
else
{
/* strip actual URL out of request */
while ( (*cp1 != ) && (*cp1 != ) ) cp1++;
if (*cp1 != )
{
/* scan to begin of actual URL field */
while ((*cp1 == ) && (*cp1 != )) cp1++;
/* remove duplicate / if needed */
if (( *cp1==/) && (*(cp1+1)==/)) cp1++;
while ((*cp1 != )&&(*cp1 != ")&&(*cp1 != ))
*cp2++ = *cp1++;
*cp2 = ;
}
}

/* un-escape URL */
unescape(log_rec.url);

/* check for service (ie: http://) and lowercase if found */
if ( (cp2=strstr(log_rec.url,"://")) != NULL)
{
cp1=log_rec.url;
while (cp1!=cp2)
{
if ( (*cp1>=A) && (*cp1<=)) *cp1 += a-A;
cp1++;
}
}

/* strip query portion of cgi scripts */
cp1 = log_rec.url;
while (*cp1 != )
if (!isurlchar(*cp1)) { *cp1 = ; break; }
else cp1++;
if (log_rec.url[0]==)
{ log_rec.url[0]=/; log_rec.url[1]=; }

/* strip off index.html (or any aliases) */
lptr=index_alias;
while (lptr!=NULL)
{
if ((cp1=strstr(log_rec.url,lptr->string))!=NULL)
{
if ((cp1==log_rec.url)||(*(cp1-1)==/))
{
*cp1=;
if (log_rec.url[0]==)
{ log_rec.url[0]=/; log_rec.url[1]=; }
break;
}
}
lptr=lptr->next;
}

/* unescape referrer */
unescape(log_rec.refer);
......
这一段,做了一些URL字符串中的字符转换工作,很长,我个人认为为了程序的模
块化,结构化和可复用性,应该将这一段代码改为函数,避免主程序体太长,造成
可读性不强和没有移植性,和不够结构化。跳过这一段乏味的代码,进入到下面一
个部分---后处理。
if (gz_log) gzclose(gzlog_fp);
else if (log_fname) fclose(log_fp);

if (good_rec) /* were any good records? */
{
tm_site[cur_day-1]=dt_site; /* If yes, clean up a bit */
tm_visit[cur_day-1]=tot_visit(sd_htab);
t_visit=tot_visit(sm_htab);
if (ht_hit > mh_hit) mh_hit = ht_hit;

if (total_rec > (total_ignore+total_bad)) /* did we process any? */
{
if (incremental)
{
if (save_state()) /* incremental stuff */
{
/* Error: Unable to save current run data */
if (verbose) fprintf(stderr,"%s ",msg_data_err);
unlink(state_fname);
}
}
month_update_exit(rec_tstamp); /* calculate exit pages */
write_month_html(); /* write monthly HTML file */
write_main_index(); /* write main HTML file */
put_history(); /* write history */
}

end_time = times(&mytms); /* display timing totals? */
if (time_me (verbose>1))
{
printf("%lu %s ",total_rec, msg_records);
if (total_ignore)
{
printf("(%lu %s",total_ignore,msg_ignored);
if (total_bad) printf(", %lu %s) ",total_bad,msg_bad);
else printf(") ");
}
else if (total_bad) printf("(%lu %s) ",total_bad,msg_bad);

/* get processing time (end-start) */
temp_time = (float)(end_time-start_time)/CLK_TCK;
printf("%s %.2f %s", msg_in, temp_time, msg_seconds);

/* calculate records per second */
if (temp_time)
i=( (int)( (float)total_rec/temp_time ) );
else i=0;

if ( (i>0) && (i<=total_rec) ) printf(", %d/sec ", i);
else printf(" ");
}
这一段,做了一些后期的处理。接下来的部分,我想在本文中略过,留给感兴趣的
读者自己去做分析。原因有两点:
1、这个程序在前面结构化比较强,而到了结构上后面有些乱,虽然代码效率还是
比较高,但是可重用性不够强, 限于篇幅,我就不再一一解释了。
2、前面分析程序过程中,也对后面的代码做了一些预测和估计,也略微涉及到了
后面的代码,而且读者可以根据上面提到的原则来自己分析代码,也作为一个实践
吧。

最后,对于在这篇文章中提到的分析源代码程序的一些方法做一下小结,以作为本
文的结束。
分析一个源代码,一个有效的方法是:
1、阅读源代码的说明文档,比如本例中的README, 作者写的非常的详细,仔细读
过之后,在阅读程序的时候往往能够从README文件中找到相应的说明,从而简化了
源程序的阅读工作。
2、如果源代码有文档目录,一般为doc或者docs, 最好也在阅读源程序之前仔细
阅读,因为这些文档同样起了很好的说明注释作用。
3、从makefile文件入手,分析源代码的层次结构,找出哪个是主程序,哪些是函
数包。这对于快速把握程序结构有很大帮助。
4、从main函数入手,一步一步往下阅读,遇到可以猜测出意思来的简单的函数,
可以跳过。但是一定要注意程序中使用的全局变量(如果是C程序),可以把关键
的数据结构说明拷贝到一个文本编辑器中以便随时查找。
5、分析函数包(针对C程序),要注意哪些是全局函数,哪些是内部使用的函数,
注意extern关键字。对于变量,也需要同样注意。先分析清楚内部函数,再来分析
外部函数,因为内部函数肯定是在外部函数中被调用的。
6、需要说明的是数据结构的重要性:对于一个C程序来说,所有的函数都是在操作
同一些数据,而由于没有较好的封装性,这些数据可能出现在程序的任何地方,被
任何函数修改,所以一定要注意这些数据的定义和意义,也要注意是哪些函数在对
它们进行操作,做了哪些改变。
7、在阅读程序的同时,最好能够把程序存入到cvs之类的版本控制器中去,在需要
的时候可以对源代码做一些修改试验,因为动手修改是比仅仅是阅读要好得多的读
程序的方法。在你修改运行程序的时候,可以从cvs中把原来的代码调出来与你改
动的部分进行比较(diff命令), 可以看出一些源代码的优缺点并且能够实际的练习
自己的编程技术。
8、阅读程序的同时,要注意一些小工具的使用,能够提高速度,比如vi中的查找
功能,模式匹配查找,做标记,还有grep,find这两个最强大最常用的文本搜索工
具的使用。


对于一个Unix/Linux下面以命令行方式运行的程序,有这么一些套路,大家可以在
阅读程序的时候作为参考。
1、在程序开头,往往都是分析命令行,根据命令行参数对一些变量或者数组,或
者结构赋值,后面的程序就是根据这些变量来进行不同的操作。
2、分析命令行之后,进行数据准备,往往是计数器清空,结构清零等等。
3、在程序中间有一些预编译选项,可以在makefile中找到相应部分。
4、注意程序中对于日志的处理,和调试选项打开的时候做的动作,这些对于调试
程序有很大的帮助。
5、注意多线程对数据的操作。(这在本例中没有涉及)
结束语:
当然,在这篇文章中,并没有阐述所有的阅读源代码的方法和技巧,也没有涉及任
何辅助工具(除了简单的文本编辑器),也没有涉及面向对象程序的阅读方法。我
想把这些留到以后再做讨论。也请大家可以就这些话题展开讨论。


来源:http://www.linuxaid.com

--
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.32.19]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店