home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / windr440.zip / windrive.zip / WinDriver / samples / wddebug / wddebug.c
C/C++ Source or Header  |  2000-03-30  |  14KB  |  418 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - WDDEBUG.C
  3. //
  4. // A utility that turns WinDriver's debug mode on and off.
  5. // Debug mode checks every IO and memory transfer command,
  6. // making sure it fits in with the card's registered
  7. // resources. If an illegal command is given, WinDriver
  8. // will ignore the command and show a warning message on
  9. // screen. Debug mode slows down transfer operations, 
  10. // therefore it should be used only in the development proccess.
  11. // Running this command without parameters will print the
  12. // version of WinDriver installed.
  13. // 
  14. // If debug mode is set, this utility also enables you to print
  15. // out debug messages from the kernel, by the dump command.
  16. // 
  17. ////////////////////////////////////////////////////////////////
  18.  
  19. #include "../../include/windrvr.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #if defined(UNIX) || defined(OS2)
  23.     #include <unistd.h>
  24.     #include <fcntl.h>
  25. #elif !defined(WINCE)
  26.   #include <conio.h> 
  27. #endif
  28. #if !defined(WINCE)
  29.   #include <time.h>
  30. #else
  31.     extern time_t time();
  32. #endif
  33. #if defined(UNIX) || defined(WINCE)
  34.     int _stricmp(const char *s1, const char *s2)
  35.     {
  36.         int i;
  37.         for (i=0; s1[i] && s2[i] && toupper(s1[i])==toupper(s2[i]); i++);
  38.         if (s1[i]=='\0' && s2[i]=='\0') return 0;
  39.         return -1;
  40.     }
  41. #endif
  42. #if defined(UNIX) || defined(OS2)
  43.     void Sleep(int nMilli)
  44.     {
  45.         #if defined(VXWORKS)
  46.             taskDelay( 1 );
  47.         #elif defined(OS2)
  48.             DosSleep(nMilli);
  49.         #else
  50.             usleep(nMilli * 1000);
  51.         #endif
  52.     }
  53. #endif
  54.  
  55. void Usage()
  56. {
  57.     printf ("This program sets debug mode of " WD_PROD_NAME " on or off\n");
  58.     printf ("WDDEBUG off      - sets debugging mode OFF\n");
  59.     printf ("WDDEBUG on       - sets debugging mode ON at ERROR level, for all sections\n");
  60.     printf ("WDDEBUG on [level] [sections...] - sets debugging mode on for specific sections\n");
  61.     printf ("WDDEBUG status   - prints the current version of " WD_PROD_NAME " and debug status\n");
  62.     printf ("WDDEBUG dump     - prints out debug messages\n");
  63.     printf ("\n");
  64.     printf ("    level - ERROR, WARN, INFO, TRACE\n");
  65.     printf ("    sections - ALL, IO, MEM, INT, PCI, PCMCIA, ISAPNP, \n");
  66.     printf ("               DMA, KER_PLUG, MISC, LICENSE, CARD_REG\n");
  67.     printf ("               ISAPNP, PCMCIA, KER_PLUG, KER_DRV\n");
  68.     printf ("\n");
  69.     printf ("Example: Turn on and view debugging for PCI and DMA routines, at info level\n");
  70.     printf ("    WDDEBUG on info \"pci dma\"\n");
  71.     printf ("    WDDEBUG dump\n");
  72. }
  73.  
  74. BOOL LevelToDword(char *sLevel, DWORD *pdwLevel)
  75. {
  76.     DWORD dwLevel = (DWORD)-1;
  77.     if (_stricmp(sLevel,"ERROR")==0) dwLevel = D_ERROR;
  78.     if (_stricmp(sLevel,"WARN")==0) dwLevel = D_WARN;
  79.     if (_stricmp(sLevel,"INFO")==0) dwLevel = D_INFO;
  80.     if (_stricmp(sLevel,"TRACE")==0) dwLevel = D_TRACE;
  81.     if (_stricmp(sLevel,"OFF")==0) dwLevel = D_OFF;
  82.     if (dwLevel==(DWORD)-1) return FALSE;
  83.     *pdwLevel = dwLevel;
  84.     return TRUE;
  85. }
  86.  
  87. char *LevelToString(DWORD dwLevel)
  88. {
  89.     if (dwLevel==D_OFF) return "OFF";
  90.     if (dwLevel==D_ERROR) return "ERROR";
  91.     if (dwLevel==D_WARN) return "WARN";
  92.     if (dwLevel==D_INFO) return "INFO";
  93.     if (dwLevel==D_TRACE) return "TRACE";
  94.     return "";
  95. }
  96.  
  97. BOOL SectionToDword(char *sSection, DWORD *pdwSection)
  98. {
  99.     char tokBuf[1024];
  100.     char *tok;
  101.     *pdwSection = 0;
  102.     strcpy (tokBuf, sSection);
  103.     for (tok = strtok(tokBuf, " "); tok; tok = strtok(NULL, " "))
  104.     {
  105.         if (_stricmp(tok, "ALL")==0) *pdwSection |= S_ALL;
  106.         else if (_stricmp(tok, "IO")==0)        *pdwSection |= S_IO;
  107.         else if (_stricmp(tok, "MEM")==0)       *pdwSection |= S_MEM;
  108.         else if (_stricmp(tok, "INT")==0)       *pdwSection |= S_INT;
  109.         else if (_stricmp(tok, "PCI")==0)       *pdwSection |= S_PCI;
  110.         else if (_stricmp(tok, "DMA")==0)       *pdwSection |= S_DMA;
  111.         else if (_stricmp(tok, "ISAPNP")==0)    *pdwSection |= S_ISAPNP;
  112.         else if (_stricmp(tok, "PCMCIA")==0)    *pdwSection |= S_PCMCIA;
  113.         else if (_stricmp(tok, "KER_PLUG")==0)  *pdwSection |= S_KER_PLUG;
  114.         else if (_stricmp(tok, "MISC")==0)      *pdwSection |= S_MISC;
  115.         else if (_stricmp(tok, "LICENSE")==0)   *pdwSection |= S_LICENSE;
  116.         else if (_stricmp(tok, "ISAPNP")==0)    *pdwSection |= S_ISAPNP;
  117.         else if (_stricmp(tok, "PCMCIA")==0)    *pdwSection |= S_PCMCIA;
  118.         else if (_stricmp(tok, "KER_PLUG")==0)  *pdwSection |= S_KER_PLUG;
  119.         else if (_stricmp(tok, "CARD_REG")==0)  *pdwSection |= S_CARD_REG;
  120.         else if (_stricmp(tok, "KER_DRV")==0)   *pdwSection |= S_KER_DRV;
  121.         else if (tok[0]=='0' && toupper(tok[1])=='x')
  122.         {
  123.             DWORD dwSection;
  124.             sscanf(tok+2, "%x", &dwSection);
  125.             *pdwSection |= dwSection;
  126.         }
  127.         else return FALSE;
  128.     }
  129.  
  130.     return TRUE;
  131. }
  132.  
  133. char *SectionToString(DWORD dwSection)
  134. {
  135.     static char sSection[1024];
  136.  
  137.     sSection[0] = '\0';
  138.     if (dwSection==S_ALL) 
  139.     {
  140.         strcat (sSection, "ALL ");
  141.         return sSection;
  142.     }
  143.     if (dwSection & S_IO)       { strcat (sSection, "IO ");       dwSection &= ~S_IO; }
  144.     if (dwSection & S_MEM)      { strcat (sSection, "MEM ");      dwSection &= ~S_MEM; }
  145.     if (dwSection & S_INT)      { strcat (sSection, "INT ");      dwSection &= ~S_INT; }
  146.     if (dwSection & S_PCI)      { strcat (sSection, "PCI ");      dwSection &= ~S_PCI; }
  147.     if (dwSection & S_DMA)      { strcat (sSection, "DMA ");      dwSection &= ~S_DMA; }
  148.     if (dwSection & S_ISAPNP)   { strcat (sSection, "ISAPNP ");   dwSection &= ~S_ISAPNP; }
  149.     if (dwSection & S_PCMCIA)   { strcat (sSection, "PCMCIA ");   dwSection &= ~S_PCMCIA; }
  150.     if (dwSection & S_KER_PLUG) { strcat (sSection, "KER_PLUG "); dwSection &= ~S_KER_PLUG; }
  151.     if (dwSection & S_MISC)     { strcat (sSection, "MISC ");     dwSection &= ~S_MISC; }
  152.     if (dwSection & S_LICENSE)  { strcat (sSection, "LICENSE ");  dwSection &= ~S_LICENSE; }
  153.     if (dwSection & S_ISAPNP)   { strcat (sSection, "ISAPNP ");   dwSection &= ~S_ISAPNP; }
  154.     if (dwSection & S_PCMCIA)   { strcat (sSection, "PCMCIA ");   dwSection &= ~S_PCMCIA; }
  155.     if (dwSection & S_KER_PLUG) { strcat (sSection, "KER_PLUG "); dwSection &= ~S_KER_PLUG; }
  156.     if (dwSection & S_CARD_REG) { strcat (sSection, "CARD_REG "); dwSection &= ~S_CARD_REG; }
  157.     if (dwSection & S_KER_DRV)  { strcat (sSection, "KER_DRV ");  dwSection &= ~S_KER_DRV; }
  158.  
  159.     if (dwSection)
  160.         sprintf (sSection+strlen(sSection), "0x%08x", dwSection);
  161.     return sSection;
  162. }
  163.  
  164. void Print_version(WD_VERSION *pVer)
  165. {
  166.     printf (WD_PROD_NAME " v%d.%02d installed (%s)\n", pVer->dwVer / 100, pVer->dwVer % 100, pVer->cVer);
  167. }
  168.  
  169. void Print_status(HANDLE hWD)
  170. {
  171.     WD_DEBUG debug;
  172.     BZERO (debug);
  173.     debug.dwCmd = DEBUG_STATUS;
  174.     WD_Debug (hWD, &debug);
  175.     printf ("Debug level (%d) %s, Debug sections (0x%08x) %s, Buffer size %d\n", 
  176.         debug.dwLevel, LevelToString(debug.dwLevel), debug.dwSection, SectionToString(debug.dwSection), debug.dwBufferSize);
  177. }
  178.  
  179. #if defined(VXWORKS)
  180.     int main (char *arg1, char *arg2, char *arg3 )
  181. #else
  182.     int main (int argc, char *argv[]) 
  183. #endif
  184. {
  185.     WD_VERSION verBuf;
  186.     DWORD debug_mode = 2;
  187.     HANDLE hWD = WD_Open ();
  188.  
  189.     #if defined(VXWORKS)
  190.         int argc=1;
  191.         char *argv[4];
  192.  
  193.         argv[0] = "wddebug_main";
  194.  
  195.         if(*arg1)
  196.         {   argv[1] = arg1;
  197.             argc++;
  198.         }
  199.         if(*arg2)
  200.         {   argv[2] = arg2;
  201.             argc++;
  202.         }
  203.         if(*arg3)
  204.         {   argv[3] = arg3;
  205.             argc++;
  206.         }
  207.     #endif
  208.     
  209.     if (hWD==INVALID_HANDLE_VALUE)
  210.     {
  211.         printf ("Error: " WD_PROD_NAME " device not installed.\n");
  212.         return EXIT_FAILURE;
  213.     }
  214.  
  215.     BZERO(verBuf);
  216.     WD_Version (hWD, &verBuf);
  217.  
  218.     if (argc<2)
  219.     {
  220.         Print_version (&verBuf);
  221.         printf ("\n");
  222.         Usage();
  223.         return EXIT_SUCCESS;
  224.    }
  225.  
  226.     if (verBuf.dwVer<WD_VER)
  227.     {
  228.         Print_version (&verBuf);
  229.         printf ("Please update the " WD_PROD_NAME " installed to v%d.%02d, or newer.\n", WD_VER / 100, WD_VER % 100);
  230.         return EXIT_FAILURE;
  231.     }
  232.  
  233.     if (_stricmp(argv[1],"on")==0)
  234.     {
  235.         WD_DEBUG debug;
  236.         BZERO (debug);
  237.         debug.dwCmd = DEBUG_SET_FILTER;
  238.         debug.dwLevel = D_ERROR;
  239.         debug.dwSection = (DWORD) S_ALL;
  240.  
  241.         if (argc>2)
  242.         {
  243.             if (argc>4)
  244.             {
  245.                 printf ("Too many arguments\n");
  246.                 Usage();
  247.                 return EXIT_FAILURE;
  248.             }
  249.  
  250.             if (!LevelToDword(argv[2], &debug.dwLevel))
  251.             {
  252.                 printf ("invalid level name (%s)\n", argv[2]);
  253.                 Usage();
  254.                 return EXIT_FAILURE;
  255.             }
  256.  
  257.             if (argc==4 && !SectionToDword(argv[3], &debug.dwSection))
  258.             {
  259.                 printf ("invalid section name (%s)\n", argv[3]);
  260.                 Usage();
  261.                 return EXIT_FAILURE;
  262.             }
  263.         }
  264.  
  265.         WD_Debug (hWD, &debug);
  266.         Print_status (hWD);
  267.     }
  268.     else if (_stricmp(argv[1],"off")==0)
  269.     {
  270.         WD_DEBUG debug;
  271.  
  272.         if (argc>2)
  273.         {
  274.             printf ("Too many arguments\n");
  275.             Usage();
  276.             return EXIT_FAILURE;
  277.         }
  278.  
  279.         BZERO (debug);
  280.         debug.dwCmd = DEBUG_SET_FILTER;
  281.         debug.dwLevel = D_OFF;
  282.         debug.dwSection = 0;
  283.         WD_Debug (hWD, &debug);
  284.     }
  285.     else if (_stricmp(argv[1],"status")==0)
  286.     {
  287.         if (argc>2)
  288.         {
  289.             printf ("Too many arguments\n");
  290.             Usage();
  291.             return EXIT_FAILURE;
  292.         }
  293.  
  294.         Print_version (&verBuf);
  295.         Print_status (hWD);
  296.     }
  297.     else if (_stricmp(argv[1],"dump")==0)
  298.     {
  299.         WD_DEBUG_DUMP debugDump;
  300.         char sOSName[50]; 
  301.         char buf[2048];
  302.         time_t ltime;
  303.         #if defined(UNIX) || defined(OS2)
  304.             int stdin_fileno = 
  305.             #if defined(VXWORKS)
  306.                 STD_IN;
  307.             #else
  308.                 STDIN_FILENO;
  309.             #endif
  310.         #elif defined(WIN32)
  311.             OSVERSIONINFO lVerInfo;
  312.         #endif
  313.  
  314.         if (argc>2)
  315.         {
  316.             printf ("Too many arguments\n");
  317.             Usage();
  318.             return EXIT_FAILURE;
  319.         }
  320.  
  321.         time(<ime);
  322.         #if defined(WIN32)
  323.             lVerInfo.dwOSVersionInfoSize = sizeof (lVerInfo);
  324.             GetVersionEx (&lVerInfo);
  325.         #endif
  326.  
  327.         printf ("WDDEBUG v%d.%02d Debugging Monitor.\n", WD_VER / 100, 
  328.             WD_VER % 100);
  329.         printf ("Running %s\n", verBuf.cVer);
  330.         #if !defined(WINCE)
  331.             printf ("Time: %s", ctime (<ime));
  332.         #endif
  333.         #if defined(WIN32)
  334.             switch (lVerInfo.dwPlatformId)
  335.             {
  336.             case VER_PLATFORM_WIN32_NT:
  337.                 strcpy(sOSName, "NT");
  338.                 break;
  339.             case VER_PLATFORM_WIN32_WINDOWS:
  340.                 if (lVerInfo.dwMinorVersion)
  341.                     strcpy(sOSName, "98");
  342.                 else
  343.                     strcpy(sOSName, "95");
  344.             break;
  345.             #if defined(VER_PLATFORM_WIN32_CE)
  346.                 case VER_PLATFORM_WIN32_CE:
  347.                     strcpy(sOSName, "CE");
  348.                     break;
  349.             #endif
  350.             default:
  351.                 strcpy(sOSName, "");
  352.             }
  353.             printf ("OS: Windows %s %d.%d Build %d.%d.%d %s\n",
  354.                 sOSName,
  355.                 lVerInfo.dwMajorVersion, lVerInfo.dwMinorVersion,
  356.                 lVerInfo.dwBuildNumber >> 24 & 0xff,
  357.                 lVerInfo.dwBuildNumber >> 16 & 0xff,
  358.                 lVerInfo.dwBuildNumber & 0xffff,
  359.                 lVerInfo.szCSDVersion);
  360.         #elif defined(VXWORKS)
  361.             printf("OS: VxWorks\n");
  362.         #elif defined(OS2)
  363.             printf("OS: OS/2\n");
  364.         #elif defined(LINUX)
  365.             printf("OS: Linux\n");
  366.         #elif defined(SOLARIS)
  367.             printf("OS: Solaris\n");
  368.         #else
  369.             printf("OS: Unknown\n");
  370.         #endif
  371.         #if defined(UNIX)
  372.             #if defined(VXWORKS)
  373.                 pritnf("Press CTRL-BREAK to exit\n");
  374.             #else
  375.                 printf("Press enter to exit\n");
  376.             #endif
  377.         #else
  378.             printf ("Press ESC to exit\n");
  379.         #endif
  380.         printf ("\n");
  381.         BZERO (debugDump);
  382.         debugDump.pcBuffer = buf;
  383.         debugDump.dwSize = sizeof (buf);
  384.         #if (defined(UNIX) && !defined(VXWORKS)) || defined(OS2)
  385.             fcntl(stdin_fileno, F_SETFL, fcntl(stdin_fileno, F_GETFL, 0) |
  386.                 O_NONBLOCK);
  387.         #endif
  388.         for (;;)
  389.         {
  390.             #if (defined(UNIX) && !defined(VXWORKS)) || defined(OS2)
  391.                 char buf[4];
  392.                 int nRead = read(stdin_fileno, buf, 4);
  393.                 if (nRead>0)
  394.                     break;
  395.             #elif defined(VXWORKS)
  396.                  // Will be implemented 
  397.             #elif defined(WIN32) && !defined(WINCE)
  398.                 if (_kbhit() && getch()==27) break;
  399.             #endif
  400.             do
  401.             {
  402.                 WD_DebugDump(hWD, &debugDump);
  403.                 printf ("%s", debugDump.pcBuffer);
  404.             } while (debugDump.pcBuffer[0]);
  405.             Sleep(100);
  406.         }
  407.     }
  408.     else
  409.     {
  410.         printf ("invalid option (%s)\n", argv[1]);
  411.         Usage();
  412.     }
  413.  
  414.     WD_Close (hWD);
  415.     return EXIT_SUCCESS;
  416. }
  417.  
  418.