home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / trace2.zip / TRACE2.C < prev    next >
C/C++ Source or Header  |  1994-04-16  |  15KB  |  474 lines

  1. //----------------------------------------------
  2. //-TRACER ( OS/2 PM Debugging Trace Facility )--
  3. //----------------------------------------------
  4. //-(c) 1990 Daniel Hildebrand-------------------
  5. //----------------------------------------------
  6.  
  7. // updated for OS/2 2.X by Dan Bright, April/94
  8.  
  9.  
  10.  
  11. #define INCL_PM
  12. #define INCL_WIN
  13. #define INCL_DOS
  14. #define INCL_WINLISTBOXES
  15. #define INCL_WINHOOKS
  16. #define INCL_ERRORS
  17.  
  18. #include <os2.h>
  19. #include <process.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "trace2.h"
  23. #include "traceid.h"
  24.  
  25.  
  26. //----------------------------------------------
  27. //-Module declarations--------------------------
  28. //----------------------------------------------
  29. int               main          ( void );
  30.  
  31. MRESULT EXPENTRY  TracerWndProc ( HWND hWnd, USHORT msg,
  32.                                   MPARAM mp1, MPARAM mp2 );
  33. VOID              TracerCommand ( HWND hWnd, SHORT id,
  34.                                   SHORT source, BOOL mouse );
  35.  
  36. static VOID              ClearSharedMem( PSZ );
  37.  
  38. BOOL EXPENTRY     HelpHook      ( HAB hab, USHORT usMode,
  39.                                   USHORT idTopic, USHORT idSubTopic,
  40.                                   PRECTL prcPosition );
  41. static VOID       HelpMessage   ( void );
  42.  
  43. //----------------------------------------------
  44. //-External references--------------------------
  45. //----------------------------------------------
  46. extern
  47. VOID            TracerPaint      ( HWND hWnd, USHORT msg,
  48.                                      MPARAM mp1, MPARAM mp2 );
  49. extern
  50. VOID            ClearLB          ( HWND hLB );
  51. extern
  52. VOID              LogToFile        ( PSZ selector_string );
  53.  
  54. extern
  55. MRESULT EXPENTRY  TracerAboutDlg   ( HWND hWndDlg,
  56.                                      USHORT message,
  57.                                      MPARAM mp1, MPARAM mp2 );
  58. extern
  59. MRESULT EXPENTRY  TracerLogFileDlg ( HWND hWndDlg,
  60.                                      USHORT message,
  61.                                      MPARAM mp1, MPARAM mp2 );
  62.  
  63.  
  64. //----------------------------------------------
  65. //-Global variables-----------------------------
  66. //----------------------------------------------
  67. HAB        hAB;                           /* anchor block   */
  68. HMQ        hmqTracer;                     /* handle queue   */
  69. HMTX       hSysSem;                       /* sys semaphore  */
  70. HFILE      pLogFile;                      /* handle to file */
  71. HWND       hParentWnd;                    /* client hWnd    */
  72. HWND       hParentFrm;                    /* frame hWnd     */
  73. HWND       hLB;                           /* handle ListBox */
  74. BOOL       bUserRequestsScroll = TRUE;    /* Scroll ON ?    */
  75. BOOL       bUserRequestsNewLog = TRUE;    /* Truncate Log?  */
  76. BOOL       bUserRequestsLog    = FALSE;   /* LogFile ON ?   */
  77. BOOL       bLoadFail;                     /* TRACER Load OK?*/
  78. CHAR       szMessage[]     =   "           PM TRACER             ";
  79.                                           /* Log File       */
  80. CHAR       szLogFile[]     =   "\\tracer.log";
  81.                                           /* Window Class   */
  82. CHAR       szParentClass[] =   "PClass";
  83. PSZ        pTracerSysString;              /* pointer to named shared mem */
  84. int        iNumberItems;                  /* # error strings*/
  85. PVOID      pBaseAddr;                     /* pointer to named shared mem*/
  86. ULONG      ulAction;                     /*  file IO       */
  87. ULONG      ulBytesWritten;               /*  file IO       */
  88.  
  89. //----------------------------------------------
  90. //-main()---------------------------------------
  91. //----------------------------------------------
  92.  
  93. int main( )
  94. {
  95.     QMSG      qmsg;
  96.     ULONG     ctldata;
  97.     SWP       swpCurrent;
  98.     int       aiGen;
  99.  
  100.     // initialize this process
  101.     hAB = WinInitialize( 0 );
  102.  
  103.     // create a PM message queue
  104.     hmqTracer = WinCreateMsgQueue( hAB, 0 );
  105.  
  106.     // register window class
  107.     if ( !WinRegisterClass( hAB,
  108.                             (PCH)szParentClass,
  109.                             (PFNWP)TracerWndProc,
  110.                             CS_SIZEREDRAW,
  111.                             0 ) )
  112.         return( 0 );
  113.  
  114.     // file control flags
  115.     ctldata = FCF_TITLEBAR      | FCF_SYSMENU  | FCF_BORDER |
  116.               FCF_MINBUTTON     | FCF_MENU     | FCF_ICON   |
  117.               FCF_SHELLPOSITION | FCF_TASKLIST | FCF_ACCELTABLE;
  118.  
  119.     // create standard window
  120.     hParentFrm = WinCreateStdWindow( HWND_DESKTOP,
  121.                                      WS_SYNCPAINT,
  122.                                      &ctldata,
  123.                                      (PCH)szParentClass,
  124.                                      NULL,
  125.                                      0L,
  126.                                      (HMODULE)NULL,
  127.                                      TRACERICON,
  128.                                      (HWND*)&hParentWnd );
  129.  
  130.     // if semaphore was obtained
  131.     // if named shared memory page was obtained
  132.     // if tracer file was opened
  133.     // ...
  134.     if ( ! bLoadFail )
  135.     {
  136.  
  137.         // auditory feedback to user that app coming up OK.
  138.         for ( aiGen = 0; aiGen < 12; aiGen += 2 )
  139.              DosBeep( ((aiGen + 1) * 100), 1 );
  140.  
  141.         WinSetWindowText( hParentFrm, "OS\\2 TRACER Version 1.0");
  142.         WinSetWindowPos ( hParentFrm, HWND_TOP,
  143.                           20,
  144.                           10,
  145.                           500,
  146.                           125,
  147.                           SWP_SIZE | SWP_MOVE | SWP_SHOW );
  148.     }
  149.  
  150.     // enter message loop
  151.     while( WinGetMsg( hAB, (PQMSG)&qmsg, (HWND)NULL, 0, 0 ) )
  152.         WinDispatchMsg( hAB, (PQMSG)&qmsg );
  153.  
  154.     // destroy TRACER window
  155.     WinDestroyWindow( hParentFrm );
  156.     // destroy message queue
  157.     WinDestroyMsgQueue( hmqTracer );
  158.     // bye bye
  159.     WinTerminate( hAB );
  160. }
  161.  
  162. //----------------------------------------------
  163. //-TracerWndProc()------------------------------
  164. //----------------------------------------------
  165. //----------------------------------------------
  166.  
  167. MRESULT EXPENTRY TracerWndProc( HWND hWnd, USHORT msg,
  168.                                 MPARAM mp1, MPARAM mp2 )
  169. {
  170. RECTL    rRect;
  171. int      aiGen;
  172.  
  173.     switch (msg)
  174.     {
  175.  
  176.     case WM_CREATE:
  177.  
  178.         // if semaphore already exists ...
  179.         if ( DosCreateMutexSem( TRACER_SEMAPHORE, &hSysSem,
  180.                                        0L, FALSE ) )
  181.             // ... and we cannot open it, then abort
  182.             if ( DosOpenMutexSem( TRACER_SEMAPHORE, &hSysSem ) )
  183.             {
  184.                 DosBeep(300,300);
  185.                 WinMessageBox( HWND_DESKTOP, hWnd,
  186.                                (PCH)"Failed to Load!",
  187.                                (PCH)"OS/2 TRACER Version 1.0", 0L,
  188.                                MB_OK| MB_ICONEXCLAMATION );
  189.                 bLoadFail = 1;
  190.                 WinPostMsg( hWnd, WM_QUIT, 0L, 0L );
  191.                 break;
  192.             }
  193.  
  194.         // allocate a page of memory
  195.         if (DosAllocSharedMem( &pBaseAddr, TRACER_MEM,
  196.                                 4096L, fPERM |PAG_COMMIT |OBJ_TILE ))
  197.         {
  198.             DosBeep(300,300);
  199.             WinMessageBox( HWND_DESKTOP, hWnd,
  200.                            (PCH)"Failed to Load!",
  201.                            (PCH)"OS/2 TRACER Version 1.0", 0L,
  202.                            MB_OK| MB_ICONEXCLAMATION );
  203.             bLoadFail = 1;
  204.             WinPostMsg( hWnd, WM_QUIT, 0L, 0L );
  205.             break;
  206.         }
  207.  
  208.         // open TRACER file
  209.         DosOpen( (PSZ)szLogFile, &pLogFile, &ulAction, 100L,
  210.                                             0,
  211.                                             0x11,
  212.                                             0x41, 0L );
  213.  
  214.         // obtain a pointer to the named shared memory
  215.         pTracerSysString  = (PSZ) pBaseAddr;
  216.  
  217.         // clear the shared memory
  218.         ClearSharedMem( pTracerSysString );
  219.  
  220.         // F1 help hook
  221.         WinSetHook( hAB, hmqTracer, HK_HELP, (PFN)HelpHook, 0L );
  222.  
  223.         // Send to anyone who was brought up BEFORE the TRACER
  224.         WinBroadcastMsg( hWnd, TRACER_RECEIVE_HANDLE,
  225.                               MPFROMHWND( hWnd ), 0L,
  226.                               BMSG_FRAMEONLY | BMSG_POSTQUEUE );
  227.  
  228.         return( WinDefWindowProc( hWnd, msg, mp1, mp2 ) );
  229.  
  230.         break;
  231.  
  232.     case WM_COMMAND:
  233.  
  234.             // handle menu commands
  235.             TracerCommand( hWnd, SHORT1FROMMP(mp1),
  236.                       SHORT1FROMMP(mp2), SHORT2FROMMP(mp2));
  237.             break;
  238.  
  239.     case TRACER_REQUEST_HANDLE:
  240.  
  241.          // a client has broadcasted a request for handshake
  242.          WinPostMsg( HWNDFROMMP(mp1), TRACER_RECEIVE_HANDLE,
  243.                            MPFROMHWND(hWnd), 0L );
  244.  
  245.         break;
  246.  
  247.     case TRACER_REQUEST_ACTION:
  248.  
  249.          // client has issued a TRACER() request
  250.  
  251.          // is screen toggled ON ?
  252.          if ( bUserRequestsScroll )
  253.          {
  254.              pTracerSysString[80] = '\0';
  255.  
  256.              WinSendMsg( hLB, LM_INSERTITEM,
  257.                 (MPARAM)-1L, (MPARAM)(PCH)pTracerSysString );
  258.              WinSendMsg( hLB, LM_SELECTITEM,
  259.                 (MPARAM)(iNumberItems - 1), (MPARAM)FALSE );
  260.              WinSendMsg( hLB, LM_SELECTITEM,
  261.                          (MPARAM)iNumberItems++, (MPARAM)TRUE );
  262.              WinSendMsg( hLB, LM_SETTOPINDEX,
  263.                          (MPARAM)(iNumberItems - 1),
  264.                          (MPARAM)TRUE );
  265.          }
  266.  
  267.          // is file toggled ON ?
  268.          if ( bUserRequestsLog )
  269.          {
  270.              pTracerSysString[80] = '\0';
  271.              LogToFile( pTracerSysString );
  272.  
  273.          }
  274.  
  275.          ClearSharedMem( pTracerSysString );
  276.  
  277.         break;
  278.  
  279.     case WM_CLOSE:
  280.  
  281.         // return semaphore to system
  282.  
  283.         DosCloseMutexSem( hSysSem );
  284.  
  285.         // is semaphore available to be obtained ?
  286.         if ( DosCreateMutexSem( TRACER_SEMAPHORE, &hSysSem,
  287.                                        0L, FALSE ) )
  288.         {
  289.             // that's no good.  a client must still have it opened !
  290.             DosBeep(75,200);
  291.             WinMessageBox( HWND_DESKTOP, hWnd,
  292.             (PCH)
  293.             "Will not shut down while client(s) are still attached!",
  294.             (PCH)"OS/2 TRACER Version 1.0", 0L,
  295.                              MB_OK| MB_ICONEXCLAMATION );
  296.  
  297.             DosOpenMutexSem( TRACER_SEMAPHORE, &hSysSem );
  298.             break;
  299.         }
  300.         else
  301.             DosCloseMutexSem( hSysSem );  /* one for create */
  302.  
  303.         // continue close down
  304.         WinPostMsg( hWnd, TRACER_MYCLOSE, 0L, 0L );
  305.  
  306.         break;
  307.  
  308.     case TRACER_MYCLOSE:
  309.  
  310.         // free the named shared memory
  311.         if ( DosFreeMem( pBaseAddr ) ) DosBeep( 100, 450 );
  312.  
  313.         // release the F1 help hook
  314.         WinReleaseHook ( hAB, hmqTracer, HK_HELP,
  315.                                  (PFN)HelpHook, 0L );
  316.  
  317.         // flush the buffer to the TRACER file / close it
  318.         DosResetBuffer( pLogFile );
  319.         DosClose( pLogFile );
  320.  
  321.         // auditory feedback to user that app coming down OK.
  322.         for ( aiGen = 12; aiGen > 0; aiGen -= 2 )
  323.              DosBeep( ((aiGen - 1) * 100), 1 );
  324.  
  325.         WinPostMsg( hWnd, WM_QUIT, 0L, 0L );
  326.  
  327.         break;
  328.  
  329.     case WM_PAINT:
  330.  
  331.         // paint routine for TRACER window
  332.         TracerPaint( hWnd, msg, mp1, mp2 );
  333.  
  334.         break;
  335.  
  336.     case WM_ERASEBACKGROUND:
  337.  
  338.         return((MRESULT)TRUE);
  339.  
  340.         break;
  341.  
  342.     default:
  343.  
  344.         return( WinDefWindowProc( hWnd, msg, mp1, mp2 ) );
  345.  
  346.         break;
  347.     }
  348.  
  349.     return(0L);
  350. }
  351.  
  352. //----------------------------------------------
  353. //--HelpHook------------------------------------
  354. //----------------------------------------------
  355. //----------------------------------------------
  356.  
  357. BOOL EXPENTRY HelpHook( HAB hab, USHORT usMode,
  358.      USHORT idTopic, USHORT idSubTopic, PRECTL prcPosition )
  359. {
  360.  
  361. WinMessageBox( HWND_DESKTOP, hParentWnd,
  362. (PCH)
  363. "TRACER.H has instructions on how to use TRACER in your app.",
  364.                    (PCH)"OS/2 TRACER HelpHook", 0L,
  365.                                   MB_OK| MB_CUANOTIFICATION );
  366.  
  367.     #ifdef HelpHook
  368.     switch ( usMode )
  369.     {
  370.  
  371.       case HLPM_MENU:
  372.  
  373.       // idtopic is submenu identifier
  374.       // idsubtopic is item identifier
  375.       // prcposition is boundary of item
  376.  
  377.            switch ( idTopic )
  378.            {
  379.              case ID_HELPBUTTON:
  380.              HelpMessage();
  381.            }
  382.       break;
  383.       case HLPM_FRAME:
  384.  
  385.       // idtopic is frame identifier
  386.       // idsubtopic is focus window identifier
  387.       // prcposition is boundary of focus window
  388.  
  389.            switch ( idTopic )
  390.            {
  391.              case ID_HELPBUTTON:
  392.              HelpMessage();
  393.            }
  394.       break;
  395.  
  396.       case HLPM_WINDOW:
  397.  
  398.       // idtopic is parent of focus window
  399.       // idsuptopic is focus window identifier
  400.       // prcposition is boundary of focus window
  401.  
  402.            switch ( idTopic )
  403.            {
  404.              case ID_HELPBUTTON:
  405.              HelpMessage();
  406.            }
  407.       break;
  408.  
  409.     }
  410.     #endif
  411. return TRUE;
  412. }
  413.  
  414. //----------------------------------------------
  415. //--TracerCommand-------------------------------
  416. //----------------------------------------------
  417. //----------------------------------------------
  418.  
  419. VOID TracerCommand( HWND hWnd, SHORT id, SHORT source, BOOL mouse )
  420. {
  421.  
  422.     switch( id )
  423.     {
  424.         case IDMABOUT:
  425.  
  426.             WinDlgBox( HWND_DESKTOP, hWnd, (PFNWP)TracerAboutDlg,
  427.                             0L, IDMABOUT, 0L );
  428.             break;
  429.  
  430.         case IDMLOG:
  431.  
  432.             WinDlgBox( HWND_DESKTOP, hWnd, (PFNWP)TracerLogFileDlg,
  433.                             0L, IDMLOG, 0L );
  434.             break;
  435.  
  436.         case IDMCLEAR:
  437.  
  438.             ClearLB( hLB );
  439.             break;
  440.  
  441.     }
  442. }
  443.  
  444. //----------------------------------------------
  445. //-HelpMessage()--------------------------------
  446. //----------------------------------------------
  447. //----------------------------------------------
  448.  
  449. static VOID       HelpMessage   ( void )
  450. {
  451.  
  452.  WinMessageBox( HWND_DESKTOP, hParentWnd,
  453.                 (PCH)
  454.  "TRACER.H has instructions on how to use TRACER in your app.",
  455.                 (PCH)"OS/2 TRACER HelpHook", 0L,
  456.                                    MB_OK| MB_CUANOTIFICATION );
  457. return;
  458. }
  459.  
  460. //----------------------------------------------
  461. //
  462. //  ClearSharedMem
  463. //
  464. //----------------------------------------------
  465.  
  466. static VOID ClearSharedMem  ( PSZ pTracerSysString )
  467. {
  468. int aiX;
  469.  
  470.     for ( aiX = 0; aiX < 80; aiX++ )
  471.          pTracerSysString[aiX] = ' ';
  472. return;
  473. }
  474.