Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42724
  • 博文数量: 2
  • 博客积分: 127
  • 博客等级: 民兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-05 17:17
文章分类

全部博文(2)

文章存档

2012年(1)

2011年(1)

分类: 嵌入式

2012-09-19 17:28:06

声明:本文内容由 tracyzhang.blog.chinaunix.net 原创,转载请注明出处,谢谢!

小的从事bsp开发的,每次用source insight看代码,查找一个函数定义或变量引用时,总会跳出好多好多不相干的文件(不是本平台的),还要对着屏幕找老半天
于是乎有个想法,将所有Kernel编译过的文件挑出来,重新生成一个source tree,这样用source insight找起来就方便多了,排除了其他代码的干扰
原理很简单,内核在编译时,凡是被编译进内核的代码都会生成对应的.o文件,根据这个.o文件去寻找相应的.c .s .S文件,找到了就拷贝到新的source tree中,对了,凡是遇到头文件都做拷贝(要是能很方便的知道内核都用到了哪些头文件就好了,期待高手解答),生成完新的source tree后再用source insight查看吧
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

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <utime.h>
  3. #include <dirent.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <stdio.h>
  7. #include <limits.h>
  8. #include <fcntl.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <string.h>

  12. #define MAX_PATH_LENGTH 1024

  13. typedef int Myfunc(const char *, const struct stat *, int);
  14. static Myfunc myfunc;
  15. static int myftw(char *, Myfunc *);
  16. static int dopath(Myfunc *);
  17. static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
  18. char *dest_dir = NULL;

  19. int main(int argc, char **argv)
  20. {
  21.     int ret;
  22.     if (argc != 3) {
  23.         fprintf (stderr, "Usage: test \n");
  24.         exit (1);
  25.     }
  26.     dest_dir = argv[2];
  27.     ret = myftw(argv[1], myfunc);

  28.     return ret;
  29. }

  30. #define FTW_F 1
  31. #define FTW_D 2
  32. #define FTW_DNR 3
  33. #define FTW_NS 4

  34. static char *fullpath;

  35. static int myftw(char *pathname, Myfunc * func)
  36. {
  37.     const int PATH_LEN = 1024;
  38.     int res;

  39.     fullpath = malloc(PATH_LEN);
  40.     strncpy(fullpath, pathname, PATH_LEN);
  41.     fullpath[PATH_LEN - 1] = '\0';
  42.     res = dopath(func);
  43.     free(fullpath);
  44.     return res;
  45. }

  46. static int dopath(Myfunc * func)
  47. {
  48.     struct stat statbuf;
  49.     struct dirent *dirp;
  50.     DIR *dp;
  51.     int ret;
  52.     char *ptr;
  53.     int temp;
  54.     
  55.     temp = lstat(fullpath, &statbuf);

  56.     if (temp < 0)
  57.         return func(fullpath, &statbuf, FTW_NS);

  58.     temp = S_ISDIR(statbuf.st_mode);

  59.     if (0 == temp)
  60.         return func(fullpath, &statbuf, FTW_F);

  61.     ret = func(fullpath, &statbuf, FTW_D);
  62.     if (ret != 0)
  63.         return ret;
  64.     ptr = fullpath + strlen(fullpath);
  65.     *ptr++ = '/';
  66.     *ptr = 0;

  67.     dp = opendir(fullpath);
  68.     if (NULL == dp)
  69.         return func(fullpath, &statbuf, FTW_DNR);

  70.     while ((dirp = readdir(dp)) != NULL) {
  71.         if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
  72.             continue;
  73.         strcpy(ptr, dirp->d_name);
  74.         ret = dopath(func);
  75.         if (ret != 0)
  76.             break;
  77.     }

  78.     ptr[-1] = 0;
  79.     if (closedir(dp) < 0)
  80.         fprintf(stderr, "can't close directory %s\n", fullpath);

  81.     return ret;
  82. }

  83. void copy_file(char *src_path)
  84. {
  85.     char dest_file[MAX_PATH_LENGTH];
  86.     int fd_src, fd_dest;
  87.     struct stat statbuf;
  88.     char buf[MAX_PATH_LENGTH], *p;
  89.     int length = 0, total_length = 0, rlength = 0, Rlength = 0, wlength = 0, ret = 0;

  90.     p = buf;
  91.     sprintf(dest_file, "%s/%s", dest_dir, src_path);

  92.     fd_src = open(src_path, O_RDONLY, 0);
  93.     fstat(fd_src, &statbuf);
  94.     length = (int)statbuf.st_size;
  95.     total_length = length;

  96.     fd_dest = open(dest_file, O_CREAT | O_RDWR, 0644);
  97.     if (fd_dest == -1) {
  98.         perror ("create file error");
  99.         exit (-1);
  100.     }

  101.     while (length > 0) {
  102.         printf ("\r%s(%dB) -> %s %d%% ", src_path, total_length, dest_dir,    (total_length - length) * 100 / total_length);
  103.         fflush(stdout);
  104.         rlength = read(fd_src, p, 1024);
  105.         Rlength = rlength;
  106.         while (rlength > 0) {
  107.             wlength = write(fd_dest, p, rlength);
  108.             rlength -= wlength;
  109.             p += wlength;
  110.         }
  111.         p = buf;
  112.         length -= Rlength;
  113.     }

  114.     close(fd_src);
  115.     close(fd_dest);
  116.     printf("\r%s(%d B) -> %s %d%% done!\n", src_path, total_length, dest_file,     100);
  117.     
  118.     return;
  119. }

  120. int is_source_file_exist(char *path)
  121. {
  122.     struct stat filestat;
  123.     int ret = -1, i;
  124.     char suffix[] = { 'c', 's', 'S', 0 };
  125.     char *p;

  126.     for (i = 0; i < strlen (suffix); i++) {
  127.         path[strlen(path) - 1] = suffix[i];
  128.         if (stat(path, &filestat) >= 0) {
  129.             ret = 0;
  130.             break;
  131.         }
  132.     }

  133.     return ret;
  134. }

  135. static int myfunc(const char *pathname, const struct stat *statptr, int type)
  136. {
  137.     char dest_path[1024];

  138.     switch (type)
  139.     {
  140.         case FTW_F:
  141.             switch (statptr->st_mode & S_IFMT)
  142.             {
  143.                 case S_IFREG:
  144.                     nreg++;
  145.                     if (fullpath[strlen(fullpath) - 1] == 'o') {
  146.                         if (is_source_file_exist(fullpath) == 0) {
  147.                             copy_file(fullpath);
  148.                         }
  149.                     } else if (fullpath[strlen(fullpath) - 1] == 'h' && fullpath[strlen(fullpath) - 2] == '.')
  150.                         copy_file(fullpath);
  151.                     break;
  152.             }
  153.             break;
  154.         case FTW_D:
  155.             ndir++;
  156.             sprintf(dest_path, "%s/%s", dest_dir, fullpath);
  157.             mkdir(dest_path, 0755);
  158.             break;
  159.         case FTW_DNR:
  160.             fprintf(stderr, "can't read directory %s\n", pathname);
  161.             break;
  162.         case FTW_NS:
  163.             fprintf(stderr, "stat error for %s\n", pathname);
  164.             break;
  165.         default:
  166.             fprintf(stderr, "unkown type %d for pathname %s\n", type, pathname);
  167.     }
  168.     return 0;
  169. }

阅读(2030) | 评论(0) | 转发(0) |
0

上一篇:[原创] Android添加开机音乐

下一篇:没有了

给主人留下些什么吧!~~