声明:本文内容由 tracyzhang.blog.chinaunix.net 原创,转载请注明出处,谢谢!
小的从事bsp开发的,每次用source insight看代码,查找一个函数定义或变量引用时,总会跳出好多好多不相干的文件(不是本平台的),还要对着屏幕找老半天
,于是乎有个想法,将所有Kernel编译过的文件挑出来,重新生成一个source tree,这样用source insight找起来就方便多了,排除了其他代码的干扰![](http://blog.chinaunix.net/blog/image/editor/hou/2.gif)
原理很简单,内核在编译时,凡是被编译进内核的代码都会生成对应的.o文件,根据这个.o文件去寻找相应的.c .s .S文件,找到了就拷贝到新的source tree中,对了,凡是遇到头文件都做拷贝(要是能很方便的知道内核都用到了哪些头文件就好了,期待高手解答),生成完新的source tree后再用source insight查看吧![](http://blog.chinaunix.net/blog/image/editor/hou/1.gif)
PS:小的写上层代码的能力停留在n年前,不喜勿喷;
用法:
比如在原始的source tree在/home/tracy/Workspace/linux-2.6.35下(这个source tree一定时要编译过的哦),想拷贝到/home/tracy/下
Step1. 编译源码,例如生成copy_build可执行文件
Step2. cd /home/tracy/Workspace/ (进入原始source tree的同级目录,不然会有问题,代码有bug懒得改了)
Step3. copy_build linux-2.6.35 /home/tracy
Step4. 等待.....
Step5. 新的代码树在/home/tracy/linux-2.6.35下生成咯,用source insight RTFSC![](http://blog.chinaunix.net/blog/image/editor/hou/15.gif)
- #include <unistd.h>
- #include <utime.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <limits.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_PATH_LENGTH 1024
- typedef int Myfunc(const char *, const struct stat *, int);
- static Myfunc myfunc;
- static int myftw(char *, Myfunc *);
- static int dopath(Myfunc *);
- static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
- char *dest_dir = NULL;
- int main(int argc, char **argv)
- {
- int ret;
- if (argc != 3) {
- fprintf (stderr, "Usage: test \n");
- exit (1);
- }
- dest_dir = argv[2];
- ret = myftw(argv[1], myfunc);
- return ret;
- }
- #define FTW_F 1
- #define FTW_D 2
- #define FTW_DNR 3
- #define FTW_NS 4
- static char *fullpath;
- static int myftw(char *pathname, Myfunc * func)
- {
- const int PATH_LEN = 1024;
- int res;
- fullpath = malloc(PATH_LEN);
- strncpy(fullpath, pathname, PATH_LEN);
- fullpath[PATH_LEN - 1] = '\0';
- res = dopath(func);
- free(fullpath);
- return res;
- }
- static int dopath(Myfunc * func)
- {
- struct stat statbuf;
- struct dirent *dirp;
- DIR *dp;
- int ret;
- char *ptr;
- int temp;
-
- temp = lstat(fullpath, &statbuf);
- if (temp < 0)
- return func(fullpath, &statbuf, FTW_NS);
- temp = S_ISDIR(statbuf.st_mode);
- if (0 == temp)
- return func(fullpath, &statbuf, FTW_F);
- ret = func(fullpath, &statbuf, FTW_D);
- if (ret != 0)
- return ret;
- ptr = fullpath + strlen(fullpath);
- *ptr++ = '/';
- *ptr = 0;
- dp = opendir(fullpath);
- if (NULL == dp)
- return func(fullpath, &statbuf, FTW_DNR);
- while ((dirp = readdir(dp)) != NULL) {
- if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
- continue;
- strcpy(ptr, dirp->d_name);
- ret = dopath(func);
- if (ret != 0)
- break;
- }
- ptr[-1] = 0;
- if (closedir(dp) < 0)
- fprintf(stderr, "can't close directory %s\n", fullpath);
- return ret;
- }
- void copy_file(char *src_path)
- {
- char dest_file[MAX_PATH_LENGTH];
- int fd_src, fd_dest;
- struct stat statbuf;
- char buf[MAX_PATH_LENGTH], *p;
- int length = 0, total_length = 0, rlength = 0, Rlength = 0, wlength = 0, ret = 0;
- p = buf;
- sprintf(dest_file, "%s/%s", dest_dir, src_path);
- fd_src = open(src_path, O_RDONLY, 0);
- fstat(fd_src, &statbuf);
- length = (int)statbuf.st_size;
- total_length = length;
- fd_dest = open(dest_file, O_CREAT | O_RDWR, 0644);
- if (fd_dest == -1) {
- perror ("create file error");
- exit (-1);
- }
- while (length > 0) {
- printf ("\r%s(%dB) -> %s %d%% ", src_path, total_length, dest_dir, (total_length - length) * 100 / total_length);
- fflush(stdout);
- rlength = read(fd_src, p, 1024);
- Rlength = rlength;
- while (rlength > 0) {
- wlength = write(fd_dest, p, rlength);
- rlength -= wlength;
- p += wlength;
- }
- p = buf;
- length -= Rlength;
- }
- close(fd_src);
- close(fd_dest);
- printf("\r%s(%d B) -> %s %d%% done!\n", src_path, total_length, dest_file, 100);
-
- return;
- }
- int is_source_file_exist(char *path)
- {
- struct stat filestat;
- int ret = -1, i;
- char suffix[] = { 'c', 's', 'S', 0 };
- char *p;
- for (i = 0; i < strlen (suffix); i++) {
- path[strlen(path) - 1] = suffix[i];
- if (stat(path, &filestat) >= 0) {
- ret = 0;
- break;
- }
- }
- return ret;
- }
- static int myfunc(const char *pathname, const struct stat *statptr, int type)
- {
- char dest_path[1024];
- switch (type)
- {
- case FTW_F:
- switch (statptr->st_mode & S_IFMT)
- {
- case S_IFREG:
- nreg++;
- if (fullpath[strlen(fullpath) - 1] == 'o') {
- if (is_source_file_exist(fullpath) == 0) {
- copy_file(fullpath);
- }
- } else if (fullpath[strlen(fullpath) - 1] == 'h' && fullpath[strlen(fullpath) - 2] == '.')
- copy_file(fullpath);
- break;
- }
- break;
- case FTW_D:
- ndir++;
- sprintf(dest_path, "%s/%s", dest_dir, fullpath);
- mkdir(dest_path, 0755);
- break;
- case FTW_DNR:
- fprintf(stderr, "can't read directory %s\n", pathname);
- break;
- case FTW_NS:
- fprintf(stderr, "stat error for %s\n", pathname);
- break;
- default:
- fprintf(stderr, "unkown type %d for pathname %s\n", type, pathname);
- }
- return 0;
- }
阅读(2210) | 评论(0) | 转发(0) |