home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / memsz331.zip / Source.zip / DEBUG.CPP < prev    next >
Text File  |  1995-10-16  |  9KB  |  246 lines

  1. /****************************************************************** DEBUG.CPP
  2.  *                                                                          *
  3.  *  Debugging Aids                                                          *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_PM
  9. #include <os2.h>
  10.  
  11. #include <ctype.h>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17.  
  18. #include "debug.h"
  19. #include "mutex.h"
  20.  
  21. extern HFILE Timer = 0 ;
  22. extern BOOL Trace = FALSE ;
  23.  
  24.  
  25. /****************************************************************************
  26.  *                                                                          *
  27.  *                       Display Debug Message                              *
  28.  *                                                                          *
  29.  ****************************************************************************/
  30.  
  31. extern VOID Debug ( HWND hwnd, char *Message, ... ) {
  32.  
  33.  /***************************************************************************
  34.   * Local Declarations                                                      *
  35.   ***************************************************************************/
  36.  
  37.   va_list Marker ;
  38.   char Text [500] ;
  39.  
  40.  /***************************************************************************
  41.   * Format the debug message.                                               *
  42.   ***************************************************************************/
  43.  
  44.   va_start ( Marker, Message ) ;
  45.   vsprintf ( Text, Message, Marker ) ;
  46.   va_end ( Marker ) ;
  47.  
  48.  /***************************************************************************
  49.   * Display the log message and wait for the user to press ENTER.           *
  50.   ***************************************************************************/
  51.  
  52.   WinMessageBox ( HWND_DESKTOP, hwnd, PSZ(Text), PSZ("Debug"), 0, MB_ENTER ) ;
  53. }
  54.  
  55. /****************************************************************************
  56.  *                                                                          *
  57.  *                         Log Debug Message                                *
  58.  *                                                                          *
  59.  ****************************************************************************/
  60.  
  61. Mutex LogSemaphore ( PSZ(NULL) ) ;
  62.  
  63. extern VOID Log ( char *Message, ... ) {
  64.  
  65.  /***************************************************************************
  66.   * Serialize this function by requesting its semaphore.                    *
  67.   ***************************************************************************/
  68.  
  69.   LogSemaphore.Request ( SEM_INDEFINITE_WAIT ) ;
  70.  
  71.  /***************************************************************************
  72.   * Open the log file.                                                      *
  73.   ***************************************************************************/
  74.  
  75.   FILE *File = fopen ( "MEMSIZE.LOG", "a" ) ;
  76.  
  77.  /***************************************************************************
  78.   * If the file got opened, write the message to the log file and close it. *
  79.   ***************************************************************************/
  80.  
  81.   if ( File ) {
  82.     char Time [9], Date [9] ;
  83.     fprintf ( File, "%s %s ", _strtime(Time), _strdate(Date) ) ;
  84.     va_list Marker ;
  85.     va_start ( Marker, Message ) ;
  86.     vfprintf ( File, Message, Marker ) ;
  87.     va_end ( Marker ) ;
  88.     fprintf ( File, "\n" ) ;
  89.     fclose ( File ) ;
  90.   } /* endif */
  91.  
  92.  /***************************************************************************
  93.   * Release the function semaphore and return.                              *
  94.   ***************************************************************************/
  95.  
  96.   LogSemaphore.Release ( ) ;
  97. }
  98.  
  99. /****************************************************************************
  100.  *                                                                          *
  101.  *                          Dump Memory to Log                              *
  102.  *                                                                          *
  103.  ****************************************************************************/
  104.  
  105. extern void DumpMemory ( char *Note, PVOID pMemory, int Count ) {
  106.    Log ( "Dump of memory at %08lX for %i bytes (%s)", pMemory, Count, Note ) ;
  107.    for ( int i=0; i<Count; i+=16 ) {
  108.       char Buffer [100] ;
  109.       sprintf ( Buffer, "%08lX: ", PCHAR(pMemory)+i ) ;
  110.       for ( int j=i; j<i+16 && j<Count; j++ ) {
  111.          sprintf ( Buffer+strlen(Buffer), "%02X ", *(PUCHAR(pMemory)+j) ) ;
  112.       } /* endfor */
  113.       while ( j<i+16 ) {
  114.          strcat ( Buffer, "   " ) ;
  115.          j ++ ;
  116.       } /* endwhile */
  117.       strcat ( Buffer, "|" ) ;
  118.       for ( j=i; j<i+16 && j<Count; j++ ) {
  119.          sprintf ( Buffer+strlen(Buffer), "%c", isprint(*(PUCHAR(pMemory)+j)) ? *(PUCHAR(pMemory)+j) : ' ' ) ;
  120.       } /* endfor */
  121.       while ( j<i+16 ) {
  122.          strcat ( Buffer, " " ) ;
  123.          j ++ ;
  124.       } /* endwhile */
  125.       strcat ( Buffer, "|" ) ;
  126.       Log ( "  %s", Buffer ) ;
  127.    } /* endfor */
  128. }
  129.  
  130. /****************************************************************************
  131.  *                                                                          *
  132.  *                          Open Timer for Use                              *
  133.  *                                                                          *
  134.  ****************************************************************************/
  135.  
  136. extern BOOL OpenTimer ( VOID ) {
  137.  
  138.   if ( Timer )
  139.     DosClose ( Timer ) ;
  140.  
  141.   ULONG Action ;
  142.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) ) {
  143.     return ( FALSE ) ;
  144.   }
  145.  
  146.   return ( TRUE ) ;
  147. }
  148.  
  149. /****************************************************************************
  150.  *                                                                          *
  151.  *                              Close Timer                                 *
  152.  *                                                                          *
  153.  ****************************************************************************/
  154.  
  155. extern VOID CloseTimer ( VOID ) {
  156.   DosClose ( Timer ) ;
  157. }
  158.  
  159. /****************************************************************************
  160.  *                                                                          *
  161.  *                       Read Time from HRTIMER.SYS                         *
  162.  *                                                                          *
  163.  ****************************************************************************/
  164.  
  165. extern BOOL GetTime ( PTIMESTAMP pts ) {
  166.  
  167.   ULONG ByteCount ;
  168.  
  169.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  170.     return ( FALSE ) ;
  171.  
  172.   return ( TRUE ) ;
  173. }
  174.  
  175. /****************************************************************************
  176.  *                                                                          *
  177.  *                         Calculate Elapsed Time                           *
  178.  *                                                                          *
  179.  ****************************************************************************/
  180.  
  181. extern ULONG ComputeElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs ) {
  182.  
  183.   ULONG ulMsecs, ulNsecs;
  184.   TIMESTAMP tsStart, tsStop ;
  185.  
  186.   tsStart = *ptsStart ;                       // De-reference timestamp
  187.                                               //     structures for speed
  188.   tsStop  = *ptsStop ;
  189.  
  190.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  191.  
  192.   if( tsStart.ulNs > tsStop.ulNs )            // If nanosecond overflow ...
  193.   {
  194.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  195.     ulMsecs--;                                        // Adjust milliseconds
  196.   }
  197.   else
  198.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  199.  
  200.   *pulNs = ulNsecs ;
  201.  
  202.   return ( ulMsecs ) ;
  203. }
  204.  
  205. /****************************************************************************
  206.  *                                                                          *
  207.  *  Allocate Memory                                                         *
  208.  *                                                                          *
  209.  ****************************************************************************/
  210.  
  211. //#define ALLOCATE_THROUGH_DOS
  212.  
  213. extern PVOID AllocateMemory ( size_t ByteCount ) {
  214.  
  215.   #ifdef ALLOCATE_THROUGH_DOS
  216.  
  217.     PVOID Memory ;
  218.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  219.     return ( Memory ) ;
  220.  
  221.   #else
  222.  
  223.     return ( malloc ( ByteCount ) ) ;
  224.  
  225.   #endif
  226. }
  227.  
  228. /****************************************************************************
  229.  *                                                                          *
  230.  *  Free Memory                                                             *
  231.  *                                                                          *
  232.  ****************************************************************************/
  233.  
  234. extern VOID FreeMemory ( PVOID Memory ) {
  235.  
  236.   #ifdef ALLOCATE_THROUGH_DOS
  237.  
  238.     DosFreeMem ( Memory ) ;
  239.  
  240.   #else
  241.  
  242.     free ( Memory ) ;
  243.  
  244.   #endif
  245. }
  246.