home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / image / drwatson / module.c < prev    next >
C/C++ Source or Header  |  1995-11-02  |  4KB  |  160 lines

  1. /*++
  2.  
  3. Copyright (c) 1993  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     module.c
  8.  
  9. Abstract:
  10.  
  11.     This file implements the module load debug events.
  12.  
  13. Author:
  14.  
  15.     Wesley Witt (wesw) 1-May-1993
  16.  
  17. Environment:
  18.  
  19.     User Mode
  20.  
  21. --*/
  22.  
  23. #include <windows.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #include "drwatson.h"
  29. #include "proto.h"
  30. #include "messages.h"
  31.  
  32. //
  33. // defines for symbol (.dbg) file searching
  34. //
  35. #define SYMBOL_PATH             "_NT_SYMBOL_PATH"
  36. #define ALTERNATE_SYMBOL_PATH   "_NT_ALT_SYMBOL_PATH"
  37. char szApp[MAX_PATH];
  38.  
  39. //
  40. // local prototypes
  41. //
  42. LPSTR GetSymbolSearchPath( void );
  43.  
  44.  
  45.  
  46. BOOL
  47. ProcessModuleLoad ( PDEBUGPACKET dp, LPDEBUG_EVENT de )
  48.  
  49. /*++
  50.  
  51. Routine Description:
  52.  
  53.     Process all module load debug events, create process & dll load.
  54.     The purpose is to allocate a MODULEINFO structure, fill in the
  55.     necessary values, and load the symbol table.
  56.  
  57. Arguments:
  58.  
  59.     dp      - pointer to a debug packet
  60.     de      - pointer to a debug event structure
  61.  
  62. Return Value:
  63.  
  64.     TRUE    - everything worked
  65.     FALSE   - we're hosed
  66.  
  67. --*/
  68.  
  69. {
  70.     HANDLE      hFile;
  71.     DWORD       dwBaseOfImage;
  72.     LPSTR       SymbolPath;
  73.  
  74.     if (de->dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
  75.         hFile = de->u.CreateProcessInfo.hFile;
  76.         dwBaseOfImage = (DWORD)de->u.CreateProcessInfo.lpBaseOfImage;
  77.         dp->hProcess = de->u.CreateProcessInfo.hProcess;
  78.         dp->dwProcessId = de->dwProcessId;
  79.         SymInitialize( dp->hProcess, NULL, FALSE );
  80.         SymbolPath = GetSymbolSearchPath();
  81.         SymSetSearchPath( dp->hProcess, SymbolPath );
  82.         free( SymbolPath );
  83.     } else if (de->dwDebugEventCode == LOAD_DLL_DEBUG_EVENT) {
  84.         hFile = de->u.LoadDll.hFile;
  85.         dwBaseOfImage = (DWORD)de->u.LoadDll.lpBaseOfDll;
  86.     }
  87.  
  88.     if ((hFile == NULL) || (hFile == INVALID_HANDLE_VALUE)) {
  89.         return FALSE;
  90.     }
  91.  
  92.     if (!SymLoadModule( dp->hProcess, hFile, NULL, NULL, dwBaseOfImage, 0 )) {
  93.         return FALSE;
  94.     } else {
  95.         if (de->dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
  96.             IMAGEHLP_MODULE   mi;
  97.             if (SymGetModuleInfo( dp->hProcess, dwBaseOfImage, &mi )) {
  98.                 strcpy( szApp, mi.ImageName );
  99.             }
  100.         }
  101.     }
  102.  
  103.     return TRUE;
  104. }
  105.  
  106.  
  107. LPSTR
  108. GetSymbolSearchPath( void )
  109.  
  110. /*++
  111.  
  112. Routine Description:
  113.  
  114.     Gets the search path to be used for locating a .DBG file.
  115.  
  116. Arguments:
  117.  
  118.     None.
  119.  
  120. Return Value:
  121.  
  122.     pointer to the path string
  123.  
  124. --*/
  125.  
  126. {
  127.     LPSTR   lpSymPathEnv      = NULL;
  128.     LPSTR   lpAltSymPathEnv   = NULL;
  129.     LPSTR   lpSystemRootEnv   = NULL;
  130.     LPSTR   SymbolSearchPath  = NULL;
  131.     DWORD   cbSymPath         = 0;
  132.  
  133.     cbSymPath = 16;
  134.     if (lpSymPathEnv = getenv(SYMBOL_PATH)) {
  135.         cbSymPath += strlen(lpSymPathEnv) + 1;
  136.     }
  137.     if (lpAltSymPathEnv = getenv(ALTERNATE_SYMBOL_PATH)) {
  138.         cbSymPath += strlen(lpAltSymPathEnv) + 1;
  139.     }
  140.     if (lpSystemRootEnv = getenv("SystemRoot")) {
  141.         cbSymPath += strlen(lpSystemRootEnv) + 1;
  142.     }
  143.  
  144.     SymbolSearchPath = calloc(cbSymPath,1);
  145.  
  146.     if (lpAltSymPathEnv) {
  147.         strcat(SymbolSearchPath,lpAltSymPathEnv);
  148.         strcat(SymbolSearchPath,";");
  149.     }
  150.     if (lpSymPathEnv) {
  151.         strcat(SymbolSearchPath,lpSymPathEnv);
  152.         strcat(SymbolSearchPath,";");
  153.     }
  154.     if (lpSystemRootEnv) {
  155.         strcat(SymbolSearchPath,lpSystemRootEnv);
  156.         strcat(SymbolSearchPath,";");
  157.     }
  158.     return SymbolSearchPath;
  159. }
  160.