📄 trv_dbg2.c
字号:
/*********************************************************************** * * (C) Copyright 2000 * Jean-Jacques Germond, Fr閐閞ic Soulier, Christian Batt; Alcatel * C/O jjg@sxb.bsf.alcatel.fr * * All rights reserved. * * This code is free software; you can redistribute it and/or * modify it under the terms of the GNU *Library* General Public License * as published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * *Library* General Public License for more details. * * You should have received a copy of the GNU *Library* General Public * License along with this program (see file COPYING.LIB); if not, * write to the Free Software Foundation, Inc., 675 Mass Ave, * Cambridge, MA 02139, USA. * ***********************************************************************//* The "trivial driver" debug-in-a-unix-process module file * Because of compatiblity problems of std include and kern includes * the debug code is dispatched in 2 files: the fisrt file * include stdio.h, stdlib.h, ... and the second file includes * internal kernel definitions. *//* See AR pg:21 */#define __NO_VERSION__ #include "trv_def.h"extern void init_module(void);extern void cleanup_module(void);extern void exit(int);/* Ptr to the file_operation str. passed to us at device init */struct file_operations *fileop; intregister_chrdev(unsigned int major, const char *name, struct file_operations *fop){ printk("Register major = %d --- module = %s\n", major, name); fileop = fop; return major; /* OK return */} intunregister_chrdev(unsigned int major, const char * name){ printk("Unregister major=%d %s\n", major, name); return 0;} extern inttrv_edit_mem(char* buf, char **start, off_t offset, int len, int unused); voidtrv_edit_proc(char* msg){ int len; char *edit_buf = kmalloc(PAGE_SIZE, 0); if (msg) { /* Print and underline the message */ printk("\n%s\n", msg); while (*msg++) printk("-"); /* Underline */ printk("\n\n"); } if (!edit_buf) { printk("Cannot allocate in trv_edit_mem\n"); exit(1); } len = trv_edit_mem(edit_buf, 0,0,0,0); printk("len=%d\n%s\n", len, edit_buf); kfree(edit_buf);}struct file filestr[TRV_MAX_DEVICES];struct inode inodestr[TRV_MAX_DEVICES];int total_nb_written[TRV_MAX_DEVICES]; /* The nbr. of caracs written */int total_nb_read[TRV_MAX_DEVICES]; /* The nbr. of caracs read back */char* readback[TRV_MAX_DEVICES]; /* The buffers to read back the devices */ voidedit_read(void) /* Edit the read buffers */{ int dev_nbr; int i; printk("\nTHE READ BUFFERS\n\n"); for (dev_nbr = 0; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { int nbchars = total_nb_read[dev_nbr]; char* ptld = readback[dev_nbr]; printk("TRV_%d (n=%d)", dev_nbr, nbchars); if ( nbchars == 0) { printk("\n--- EMPTY DEVICE ---"); } else { for (i = 0; i < nbchars; i++) { char aux = *ptld++; if ( (i % 72) == 0) printk("\n"); if ((aux < ' ') || (aux > '~')) aux = '.'; /* make it printable */ printk("%c", aux); } } printk("\n"); }} voidinit_inode(struct inode* inodep, int dev_nbr){ inodep->i_rdev = MKDEV(TRV_MAJOR,dev_nbr);} voidinit_file(struct file* filp, int dev_nbr){ return; /* nothing yet */} voidtrv_dbg2(void){ char *auxbuf = kmalloc(1000,0); /* An aux buffer */ int j; int dev_nbr; int result; struct inode* inodep; struct file* filp; int more_read; /* Flag telling that more read are needed */ if (!auxbuf) { printk("Cannot get auxbuf\n"); exit (1); } /* the root of the kernel simulation */ init_module(); trv_edit_proc("INIT DONE"); /* Open the 4 devices for writing */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { inodep = &inodestr[dev_nbr]; filp = &filestr[dev_nbr]; init_inode(inodep,dev_nbr); init_file(filp, dev_nbr); filp->f_flags = O_WRONLY ; /* Typical call to the driver */ result = fileop->open(inodep, filp); printk("OPEN device %d returns %d\n", dev_nbr, result); if (result != 0) { exit (1); } total_nb_written[dev_nbr] = 0; /* For local accounting */ } trv_edit_proc("OPEN DONE"); /* Write stuff into each device. Use 2 nested loops. Outer loops * forces device 3 to staturate. Device 0 never get anything. */ for (j = 0; j < 10; j++) { for (dev_nbr = 0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { int i; int nb_chars = (dev_nbr + 1) * dev_nbr; char carac = 'A' + dev_nbr + j; /* Fill the aux buff for this device */ for (i = 0; i < nb_chars; i++) { auxbuf[i] = carac; } filp = &filestr[dev_nbr]; result = fileop->write(filp, auxbuf, nb_chars, &filp->f_pos); if (result < 0) { printk("write error device=%d error=%d\n", dev_nbr, result); exit(1); } total_nb_written[dev_nbr] += nb_chars; /* What we waned to write */ printk("write ok: dev=%d wanted=%d written=%d total_written=%d\n", dev_nbr, nb_chars, result, total_nb_written[dev_nbr]); } printk("\n"); } trv_edit_proc("WRITE DONE"); /* Close all the devices */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { filp = &filestr[dev_nbr]; init_file(filp, dev_nbr); result = fileop->flush(filp); printk("Close device %d returns %d\n", dev_nbr, result); if (result != 0) { exit (1); } } trv_edit_proc("CLOSE DONE"); /* Open the devices for reading */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { inodep = &inodestr[dev_nbr]; filp = &filestr[dev_nbr]; init_inode(inodep,dev_nbr); init_file(filp, dev_nbr); filp->f_flags = O_RDONLY ; result = fileop->open(inodep, filp); printk("OPEN READ device %d returns %d\n", dev_nbr, result); if (result != 0) { exit (1); } } trv_edit_proc("OPEN READ DONE"); /* Loop to allocate and reset the readback buffer */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { int i; char* dataptr; total_nb_read[dev_nbr] = 0; /* What we did read */ readback[dev_nbr] = kmalloc(TRV_MAX_CHARS+4,0); if (!readback[dev_nbr]) { printk("Cannot allocate readback buffer\n"); exit(1); } /* Loop to reset the readback buffer */ dataptr = readback[dev_nbr]; /* Reset the aux buff for this device */ for (i = 0; i < TRV_MAX_CHARS; i++) { dataptr[i] = '*'; } } /* Loop to read all the devices in "only-one-read-mode" */ for (dev_nbr = 0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { int nb_chars = TRV_MAX_CHARS+100; char* dataptr = readback[dev_nbr]; filp = &filestr[dev_nbr]; result = fileop->read(filp, dataptr, nb_chars, &filp->f_pos); if (result < 0 ) { printk("Read error device=%d error=%d\n", dev_nbr, result); exit(1); } total_nb_read[dev_nbr] += result; /* What we did read */ if (total_nb_read[dev_nbr] > TRV_MAX_CHARS) { printk("Too many bytes out of device %d n = %d\n", dev_nbr, total_nb_read[dev_nbr]); exit(1); } printk("read ok: dev=%d wanted=%d read=%d total_read=%d\n", dev_nbr, nb_chars, result, total_nb_read[dev_nbr]); } edit_read(); trv_edit_proc("READ BLOCKED DONE"); /* loop to close and reopen the devices in read mode */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { inodep = &inodestr[dev_nbr]; filp = &filestr[dev_nbr]; init_inode(inodep,dev_nbr); init_file(filp, dev_nbr); result = fileop->flush(filp); if (result != 0) { printk("Close device %d returns %d\n", dev_nbr, result); exit (1); } filp->f_flags = O_RDONLY ; result = fileop->open(inodep, filp); if (result != 0) { printk("OPEN READ device %d returns %d\n", dev_nbr, result); exit (1); } } trv_edit_proc("RE_OPEN READ DONE"); /* Loop to reset the readback buffer */ for (dev_nbr = 0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { int i; char* dataptr; total_nb_read[dev_nbr] = 0; /* What we did read */ dataptr = readback[dev_nbr]; for (i = 0; i < TRV_MAX_CHARS; i++) { dataptr[i] = '*'; } } /* Loop to read the devices few chars at a time. */ more_read = 1; /* Tells that some device still returns data */ while(more_read) { more_read = 0; /* Loop to read all the devices in "few-char-at-a-time-mode" */ for (dev_nbr = 0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { int nb_chars = 1 + (dev_nbr * 2); char* dataptr = readback[dev_nbr] + total_nb_read[dev_nbr]; filp = &filestr[dev_nbr]; result = fileop->read(filp, dataptr, nb_chars, &filp->f_pos); if ((result < 0) || (result > nb_chars)) { printk("Read error device=%d error=%d\n", dev_nbr, result); exit(1); } total_nb_read[dev_nbr] += result; /* What we did read */ /* Rearm the loop if at least one device is still active */ if (result) more_read = 1; if (total_nb_read[dev_nbr] > TRV_MAX_CHARS) { printk("Too many bytes out of device %d n = %d\n", dev_nbr, total_nb_read[dev_nbr]); exit(1); } } } edit_read(); trv_edit_proc("READ ONE BYTE DONE"); /* Final loop to close the devices */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { filp = &filestr[dev_nbr]; init_file(filp, dev_nbr); result = fileop->flush(filp); if (result != 0) { printk("Close device %d returns %d\n", dev_nbr, result); exit (1); } } /* And disconnect the trivial driver */ cleanup_module();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -