INIT.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*++
  2. Copyright (c) 1995-1997  Microsoft Corporation
  3. Module Name:
  4.     init.c
  5. Abstract:
  6.     This is the initialization module for the pfmon program.
  7. Author:
  8.     Mark Lucovsky (markl) 26-Jan-1995
  9. Revision History:
  10. --*/
  11. #include "pfmonp.h"
  12. BOOL
  13. InitializePfmon( VOID )
  14. {
  15.     LPTSTR CommandLine;
  16.     BOOL fShowUsage;
  17.     DWORD Pid = 0;
  18.     fShowUsage = FALSE;
  19.     CommandLine = GetCommandLine();
  20.     while (*CommandLine > ' ') {
  21.         CommandLine += 1;
  22.     }
  23.     while (TRUE) {
  24.         while (*CommandLine <= ' ') {
  25.             if (*CommandLine == '') {
  26.                 break;
  27.             } else {
  28.                 CommandLine += 1;
  29.             }
  30.         }
  31.         if (!strnicmp( CommandLine, "/v", 2 ) || !strnicmp( CommandLine, "-v", 2 )) {
  32.             CommandLine += 2;
  33.             fVerbose = TRUE;
  34.         } else if (!strnicmp( CommandLine, "/?", 2 ) || !strnicmp( CommandLine, "-?", 2 )) {
  35.             CommandLine += 2;
  36.             fShowUsage = TRUE;
  37.             goto showusage;
  38.         } else if (!strnicmp( CommandLine, "/c", 2 ) || !strnicmp( CommandLine, "-c", 2 )) {
  39.             CommandLine += 2;
  40.             fCodeOnly = TRUE;
  41.         } else if (!strnicmp( CommandLine, "/h", 2 ) || !strnicmp( CommandLine, "-h", 2 )) {
  42.             CommandLine += 2;
  43.             fHardOnly = TRUE;
  44.         } else if (!strnicmp( CommandLine, "/n", 2 ) || !strnicmp( CommandLine, "-n", 2 )) {
  45.             CommandLine += 2;
  46.             LogFile = fopen("pfmon.log","wt");
  47.             fLogOnly = TRUE;
  48.         } else if (!strnicmp( CommandLine, "/l", 2 ) || !strnicmp( CommandLine, "-l", 2 )) {
  49.             CommandLine += 2;
  50.             LogFile = fopen("pfmon.log","wt");
  51.         } else if (!strnicmp( CommandLine, "/p", 2 ) || !strnicmp( CommandLine, "-p", 2 )) {
  52.             CommandLine += 2;
  53.             while (*CommandLine <= ' ') {
  54.                 if (*CommandLine == '') {
  55.                     break;
  56.                 } else {
  57.                     ++CommandLine;
  58.                 }
  59.             }
  60.             Pid = atoi(CommandLine);
  61.             CommandLine = strchr(CommandLine,' ');
  62.             if (CommandLine==NULL) {
  63.                 break;
  64.             }
  65.         } else if (!strnicmp( CommandLine, "/d", 2 ) || !strnicmp( CommandLine, "-d", 2 )) {
  66.             CommandLine += 2;
  67.             fDatabase = TRUE;
  68.         } else {
  69.             break;
  70.         }
  71.     }
  72. showusage:
  73.     if ( fShowUsage ) {
  74.         fprintf(stdout,"Usage: PFMON [switches] application-command-linen");
  75.         fprintf(stdout,"             [-?] display this messagen");
  76.         fprintf(stdout,"             [-n] don't display running faults, just log to pfmon.logn");
  77.         fprintf(stdout,"             [-l] log faults to pfmon.logn");
  78.         fprintf(stdout,"             [-c] only show code faultsn");
  79.         fprintf(stdout,"             [-h] only show hard faultsn");
  80.         fprintf(stdout,"             [-p pid] attach to existing processn");
  81.         fprintf(stdout,"             [-d] Database format (tab delimited)n");
  82.         fprintf(stdout,"                  format: pagefault number, Page Fault type (Hard or Soft),n");
  83.         fprintf(stdout,"                  Program Counter's Module, Symbol for PC, Decimal value of PC,n");
  84.         fprintf(stdout,"                  Decimal value of PC, Module of the virtual address accessed,n");
  85.         fprintf(stdout,"                  Symbol for VA, value of VAn");
  86.         return FALSE;
  87.         };
  88.     InitializeListHead( &ProcessListHead );
  89.     InitializeListHead( &ModuleListHead );
  90.     SetSymbolSearchPath();
  91.     PfmonModuleHandle = GetModuleHandle( NULL );
  92.     if (Pid != 0) {
  93.         return(AttachApplicationForDebug(Pid));
  94.     } else {
  95.         return (LoadApplicationForDebug( CommandLine ));
  96.     }
  97.     return TRUE;
  98. }
  99. BOOL
  100. LoadApplicationForDebug(
  101.     LPSTR CommandLine
  102.     )
  103. {
  104.     STARTUPINFO StartupInfo;
  105.     PROCESS_INFORMATION ProcessInformation;
  106.     ZeroMemory( &StartupInfo, sizeof( StartupInfo ) );
  107.     StartupInfo.cb = sizeof(StartupInfo);
  108.     if (!CreateProcess( NULL,
  109.                         CommandLine,
  110.                         NULL,
  111.                         NULL,
  112.                         FALSE,                          // No handles to inherit
  113.                         DEBUG_PROCESS,
  114.                         NULL,
  115.                         NULL,
  116.                         &StartupInfo,
  117.                         &ProcessInformation)) {
  118.         DeclareError( PFMON_CANT_DEBUG_PROGRAM,
  119.                       GetLastError(),
  120.                       CommandLine
  121.                     );
  122.         return FALSE;
  123.     } else {
  124.         hProcess = ProcessInformation.hProcess;
  125.         SymInitialize(hProcess,NULL,FALSE);
  126.         return InitializeProcessForWsWatch(hProcess);
  127.     }
  128. }
  129. BOOL
  130. AttachApplicationForDebug(
  131.     DWORD Pid
  132.     )
  133. {
  134.     if (!DebugActiveProcess(Pid)) {
  135.         DeclareError( PFMON_CANT_DEBUG_ACTIVE_PROGRAM,
  136.                       GetLastError(),
  137.                       Pid );
  138.         return FALSE;
  139.     } else {
  140.         hProcess = OpenProcess(PROCESS_VM_READ
  141.                                | PROCESS_QUERY_INFORMATION
  142.                                | PROCESS_SET_INFORMATION,
  143.                                FALSE,
  144.                                Pid);
  145.         SymInitialize(hProcess,NULL,FALSE);
  146.         return InitializeProcessForWsWatch(hProcess);
  147.     }
  148. }