10.3. Building and DebuggingAdding your code to the kernel typically involves cycles of programming and bug fixing. In this section, we describe how to debug the kernel code you've written and how to build debugging-related tools. 10.3.1. Debugging Device DriversIn previous sections, we used the /proc filesystem to gather information about the kernel. We can also make information about our device driver accessible to users via /proc, and it is an excellent way to debug parts of your device driver. Every node in the /proc filesystem connects to a kernel function when it is read or written to. In the 2.6 kernel, most writes to part of the kernel, devices included, are done through sysfs instead of /proc. The operations modify specific kernel object attributes while the kernel is running. /proc remains a useful tool for read-only operations that require a larger amount of data than an attribute-value pair, and this section deals only with reading from /proc enTRies. The first step in allowing read access to your device is to create an entry in the /proc filesystem, which is done by create_proc_read_entry(): ----------------------------------------------------------------------- include/linux/proc_fs.h 146 static inline struct proc_dir_entry *create_proc_read_entry(const char *name, 147 mode_t mode, struct proc_dir_entry *base, 148 read_proc_t *read_proc, void * data) ----------------------------------------------------------------------- *name is the entry of the node that appears under /proc, a mode of 0 allows the file to be world-readable. If you are creating many different proc files for a single device driver, it could be advantageous to first create a proc directory by using proc_mkdir(), and then base each file under that. *base is the directory path under /proc to place the file; a value of NULL places the file directly under /proc. The *read_proc function is called when the file is read, and *data is a pointer that is passed back into *read_proc: ----------------------------------------------------------------------- include/linux/proc_fs.h 44 typedef int (read_proc_t)(char *page, char **start, off_t off, 45 int count, int *eof, void *data); ----------------------------------------------------------------------- This is the prototype for functions that want to be read via the /proc filesystem. *page is a pointer to the buffer where the function writes its data for the process reading the /proc file. The function should start writing at off bytes into *page and write no more than count bytes. As most reads return only a small amount of information, many implementations ignore both off and count. In addition, **start is normally ignored and is rarely used anywhere in the kernel. If you implement a read function that returns a vast amount of data, **start, off, and count can be used to manage reading small chunks at a time. When the read is finished, the function should write 1 to *eof. Finally, *data is the parameter passed to the read function defined in create_proc_read_entry(). |