📄 trv_dbg3.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-from-a-unix-process file */#include "stdio.h"#include "fcntl.h"#include "stdlib.h"#include "errno.h"/* Look at very few things (since __KERNEL__ is not defined) in trv_def.h */#include "trv_def.h"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 */FILE* fileDesc[TRV_MAX_DEVICES]; /* The Unix file descriptors */static char fname[100]; char*trv_makeFileName(int ivol) /* Edit a trv path from trv number */{ sprintf(fname,"/dev/trv%d", ivol); return fname;} voidtrv_edit_proc(char* msg) /* Edit a test milestone */{ int len; if (msg) { /* Print and underline the message */ printf("\n%s\n", msg); while (*msg++) printf("-"); /* Underline */ printf("\n\n"); } fflush(stdout); system("dd if=/proc/trvmem bs=4k");} voidedit_read(void) /* Edit the read buffers */{ int dev_nbr; int i; printf("\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]; printf("TRV_%d (n=%d)", dev_nbr, nbchars); if ( nbchars == 0) { printf("\n--- EMPTY DEVICE ---"); } else { for (i = 0; i < nbchars; i++) { char aux = *ptld++; if ( (i % 72) == 0) printf("\n"); if ((aux < ' ') || (aux > '~')) aux = '.'; /* make it printable */ printf("%c", aux); } } printf("\n"); }} voidtrv_dbg2(void){ char *auxbuf = malloc(1000); /* An aux buffer */ int j; int dev_nbr; int result; int more_read; /* Flag telling that more read are needed */ if (!auxbuf) { printf("Cannot get auxbuf\n"); exit (1); } /* Open the 4 devices for writing */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { fileDesc[dev_nbr] = fopen(trv_makeFileName(dev_nbr), "w"); if (!fileDesc[dev_nbr]) { printf("OPEN device %d returns %d\n", dev_nbr, errno); 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; } result = fwrite(auxbuf, nb_chars, 1, fileDesc[dev_nbr]); if (result < 0) { printf("write error device=%d error=%d\n", dev_nbr, result); exit(1); } total_nb_written[dev_nbr] += nb_chars; /* What we waned to write */ printf("write ok: dev=%d wanted=%d written=%d total_written=%d\n", dev_nbr, nb_chars, result, total_nb_written[dev_nbr]); } printf("\n"); } trv_edit_proc("WRITE DONE"); /* Close all the devices */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { result = fclose(fileDesc[dev_nbr]); printf("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++) { fileDesc[dev_nbr] = fopen(trv_makeFileName(dev_nbr), "r"); printf("OPEN READ device %d returns %d\n", dev_nbr, errno); if (!fileDesc[dev_nbr]) { exit (1); } } trv_edit_proc("OPEN READ DONE"); /* Loop to allocate and reset the readback buffers */ 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] = malloc(TRV_MAX_CHARS+4); if (!readback[dev_nbr]) { printf("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]; result = fread(dataptr, nb_chars, 1, fileDesc[dev_nbr]); if (result < 0 ) { printf("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) { printf("Too many bytes out of device %d n = %d\n", dev_nbr, total_nb_read[dev_nbr]); exit(1); } printf("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"); /* Now try to read in "small buffer" modfe */ /* loop to close and reopen the devices in read mode */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { result = fclose(fileDesc[dev_nbr]); if (result != 0) { printf("Close device %d returns %d\n", dev_nbr, result); exit (1); } fileDesc[dev_nbr] = fopen(trv_makeFileName(dev_nbr), "r"); if (!fileDesc[dev_nbr]) { printf("OPEN READ device %d returns %d\n", dev_nbr, errno); 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]; result = fread(dataptr, nb_chars, 1, fileDesc[dev_nbr]); if ((result < 0) || (result > nb_chars)) { printf("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) { printf("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 FEW BYTE DONE"); /* Final loop to close the devices */ for (dev_nbr =0 ; dev_nbr < TRV_MAX_DEVICES; dev_nbr++) { result = fclose(fileDesc[dev_nbr]); if (result != 0) { printf("Close device %d returns %d\n", dev_nbr, result); exit (1); } }}main(){ printf("UNIX trv test starting\n"); trv_dbg2(); printf("UNIX trv test ending ok\n"); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -