I have been studying the Linux 0.11 source code recently and debugging it. In the experiment, I needed to print process messages when a process exits. Therefore, I added the fprintk function in /kernel/printk.c as shown below:
// static char logbuf[1024];
int fprintk(int fd)
{
printk("--------------------------------------n");
printk("fprintk called with fd: %dn", fd); // Debugging information
// va_list args;
// int count;
// va_start(args, fmt);
// count = vsprintf(logbuf, fmt, args);
// va_end(args);
// printk("fprintk: formatted string: %sn", logbuf); // Debugging information
// if (fd == 3) {
// count = sys_write(fd, logbuf, count);
// printk("fprintk: written %d bytes to fd %dn", count, fd); // Debugging information
// }
printk("--------------------------------------n");
// return count;
return 0;
}
Then, I added the function call in the do_exit() function of /kernel/exit.c:
int fd = 3;
......
printk("exit fd: %dn", fd);
// fprintk(fd, "%ldt%ct%ldn", current->pid, 'E', jiffies);
fprintk(fd);
...
In the actual experiment, I found that the printed fd is not 3, but a very strange value, and I don’t know why, I’ll appreciate it very much if you can give some adivices.