代码阅读方法(自己总结地 厚厚)

来源:互联网 发布:沃尔沃北极星软件 编辑:程序博客网 时间:2024/05/16 16:09

 不知道大家是否看到几万行的代码是什么心情。 有人说过,阅读别人的代码是一种享受。下午的时候,泡上一壶好茶,坐在自己家的阳台上,阅读代码。嗯嗯,比看小说要有意思的多了。学习别人的思想,让自己的思维更敏捷。

在07年的第一篇有关技术的文章,把我自己读代码的经验和大家分享一下。

其实主要是做笔记,一个大的项目,或程序,估计几万行代码是下不来的。 就像谁也不能把金庸全集一口气看完一样。那么知道自己看到哪了,上下文如何衔接起来,就要靠记录了。你可以选择有笔纸,选择键盘敲,无所谓。

你可以选择用图,或用自己的大白话。自己习惯,和要适合自己大脑的思维方式来定。

我比较喜欢用图的,有助于逻辑思维的记忆和锻炼。

图大概有:

     --流程图;

     --时序图;

     --数据流图;

对于一个大的项目,一个也不能少。

前一段时间分析linux内核中的usb gadgetfs 驱动,相关文件有$(kernel)/device/usb/gadget/ inode.c和一个

USB官网上的用户态驱动usb.c。

下面是我的记录用的是“树形结构”(自己起的名字,呵呵),不是很全,仅提供思路,希望大家可以在代码中找到乐趣,思维更加灵敏。

这个是USB.C的:

int main (int argc, char **argv)
 |
 |/* random initial serial number */
 |
 | 接收控制参数;
 |
 | 改变路径到/dev/gadget;
 |
 |-- init_device (); /* 设备初始化 */
 | |
 | |-- autoconfig (); /* 自动配置端点 */
 | |     |
 | |     | 取得设备名称,信息;
 | |     |
 | |     | 设置端点地址,和端点名;
 | |     |-[R]
 | | 
 | |-- open (DEVNAME, O_RDWR); /* 打开设备文件 */
 | | 
 | | /* write full then high speed configs */
 | |-- static char *build_config (char *cp, const struct usb_endpoint_descriptor **ep); 
 | |  |         
 | |  | 一个cp指针指向buf, 用buf来存储个描述符;
 | |  |buf
 | | |      ________
 | | |     |                     |
 | | |     | config         |
 | | |     |                     |
 | | |     |interface     |
 | | |     |                     |
 | | |     | ep               |
 | | |     |________  |
 | | |
 | | |-[R]
 | |
 | | /* and device descriptor at the end */
 | |
 | |-- write (fd, &buf [0], cp - &buf [0]); /* write dev descriptor */
 | |
 | |--[return] 一个配置符齐全的fd;
 | 
 | 
 | /* control thread, handles main event loop  */ 
 |-- ep0_thread (&fd); /* 将init_device 后的fd 传给了它 */ 
 | |
 | |当有事件以信号的方式传递给它时,
 | |它是用wait等待的sigwait (&sigs, &tmp);
 | |
 | |然后利用switch(tmp)来处理不同类型的事件,然后做相应的控制
 | |
 | |/* loop */
 | | case GADGETFS_SETUP:
 | |  handle_control (fd, &event [i].u.setup);
 | |  
 | |    static void handle_control (int fd, struct usb_ctrlrequest *setup)
 | |     | |
 | |     | |- struct usb_ctrlrequest 
       | /*this structure is used to send control requests to a USB device.*/
       |
       | 处理标准的USB设备请求命令
       |
       |switch(setup->bRequest)
       | |
       | |
       | |case USB_REQ_SET_CONFIGURATION:
       | | if (setup->bRequestType != USB_DIR_OUT)
       | |  goto stall;
       | | if (verbose)
       | |  fprintf (stderr, "CONFIG #%d/n", setup->wValue);
       | |
       | | ...
       | | switch (setup->wValue) {
       | | case CONFIG_VALUE:
       | |  start_io (); /* 启动线程 */
       | |  break;
       | | case 0:
       | |  stop_io ();
       | |  break;
 

 


 

 


static void start_io ()
 |
 |除ep0外,其他2个端点是以线程的方式运行的,在这里启动2线程
 |
 |/*create source thread*/ /In/
 |
 |/*create sink thread*/  /Out/
 


/* source endpoint thread */
static void *simple_source_thread (void *param)
 |
 | 配置端点 source_open /* 一个可以配置端点的宏,在下面有它的实现 */
 |
 |--while(status > 0) {
   len = fill_in_buf (buf, sizeof buf);
  if (len > 0)
   status = write (source_fd, buf, len);
  }
 |
 |
 
/* sink endpoint thread */
static void *simple_sink_thread (void *param)
 |
 |配置端点 sink_open /* 一个可以配置端点的宏,在下面有它的实现 */
 |
 |--while{
  status = read (sink_fd, buf, sizeof buf);

  if (status < 0)
   break;
  status = empty_out_buf (buf, status);
  }
 |
 |
 |
 
 
 
 
 
 
 
 
 
#define source_open(name) /
 ep_config(name,__FUNCTION__, &fs_source_desc, &hs_source_desc)
#define sink_open(name) /
 ep_config(name,__FUNCTION__, &fs_sink_desc, &hs_sink_desc)
 
/* you should be able to open and configure endpoints
 * whether or not the host is connected
 */
static int
ep_config (char *name, char *label,
 struct usb_endpoint_descriptor *fs,
 struct usb_endpoint_descriptor *hs)
 |
 |
 |

 

这个是linux内核中的inode.c(gadgetfs的实现),不是很全,现在正在看。

/*
 * read kernel/driver/usb/gadget/inode.c
 *
 * gadgetfs gadget api
 *
 * date : 1-6-2007
 *
 */
[mynote]:
1.gadgetfs
2.dev_config
3.dev operation
4.ep_config
5.ep_peration
|
|-- register_filesystem (&gadgetfs_type)
|
| /* when __init module_init */
| /* gadgetfs_type object have a implant structure.*/
|-- struct file_system_type gadgetfs_type
|     |-- gadgetfs_get_sb
|
| /* when  "mount -t gadgetfs path /dev/gadget" ends up here
| will call gadgetfs_get_sb
| */
|--gadgetfs_get_sb
|    |
|    |//call a system call , register gadgetfs_fill_super to kernel.
|    |--get_sb_single (t, flags, opts, gadgetfs_fill_super)
|
| /* a main function, kernel will callback this function of inode.c  */
| 仅仅用于探测一个UDC的设备名字,然后在/DEV/GADGET/下建立如下文件节点
| 1.$CHIP
| 2.$ep0
| 3.$other ep
|-- gadgetfs_fill_super 
| |
 |1.
 |/* fake probe to determine $CHIP */
 |--(void) usb_gadget_register_driver (&probe_driver);
 |
 |  sturct usb_gadget_driver probe_driver
 |    | --gadgetfs_probe
 |    |   | 1.[input] usb_gadget device
 |    |   | 2.CHIP = gadget->name /* 将设备名称给CHIP */
 |
 |
 |/*[??note]when*/ dev_config
 |  |
 |  |/* triggers gadgetfs_bind(); then we can enumerate. */
 |         |--value = usb_gadget_register_driver (&gadgetfs_driver) 
 |
 |
 |2.
 | /*建立跟节点root inode*/
 | gadgetfs_make_inode
 |
 |3. 建立ep0节点文件
 | /* the ep0 file is named after the controller we expect;
        |       user mode code can use it for sanity checks, like we do.
 | */
 |
 |
 |4. 建立其他节点文件
 |/* other endpoint files are available after hardware setup,
 |    from binding to a controller.
 |*/
 
|
|

黏贴过来时,好像制表符没了,看起来有点丑。 那个,推荐用UltraEdit来记录。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 被执行人没有财产执行怎么办 挖到人头了怎么办 取保保证金不退怎么办 被诬陷经济诈骗怎么办 醉驾刑事拘留后怎么办 被别人举报赌博怎么办 涉黄刑事拘留怎么办取保候审 换了车牌保险怎么办 车辆转让后保险怎么办 立案后警察不管怎么办 打架后对方讹人怎么办 工商被恶意举报怎么办 店铺被工商举报怎么办 被买单侠恐吓怎么办? 团伙作案刑事拘留怎么办取保 欢乐麻将老输怎么办 回不了家怎么办身份证 没注意闯红灯了怎么办 摩托车被套牌了怎么办? 发现员工偷钱怎么办 盗窃刑事拘留7天怎么办 盗窃抓不到人怎么办 发现宿舍被盗后怎么办(  ) 发现宿舍被盗后怎么办() 回收到赃物电瓶怎么办 不知情买了赃物怎么办 盗窃单位要报警怎么办 上网吸烟被逮住怎么办 有人在微信骂我怎么办 网吧抽烟被拍照怎么办 诈骗被拘留该怎么办 行政拘留人跑了怎么办 车牌号被偷了怎么办 当员工提出辞职怎么办 老员工提出辞职怎么办 家人进看守所了怎么办 被贷款中介诈骗怎么办 喝完酒吐难受怎么办 犯罪嫌疑人死在看守所怎么办 犯罪嫌疑人死不承认怎么办 高血压进了看守所怎么办