Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: sym.hxx
  7. //
  8. // Contents: A C++ wrapping of a subset of the interface of imagehlp.dll
  9. //
  10. // Classes: CSym
  11. //
  12. // History: 14-Jul-95 t-stevan Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #ifndef __SYM_HXX__
  16. #define __SYM_HXX__
  17. #include <imagehlp.h>
  18. #define MAXNAMELENGTH 256
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Class: CSym
  22. //
  23. // Purpose: A C++ wrapping of a subset of the interface of imagehlp.dll
  24. //
  25. // Interface: CSym
  26. // ~CSym
  27. // GetSymNameFromAddr
  28. //
  29. // History: 14-Jul-95 t-stevan Created
  30. //
  31. //--------------------------------------------------------------------------
  32. class CSym
  33. {
  34. public:
  35. inline CSym();
  36. inline ~CSym();
  37. inline BOOL GetSymNameFromAddr(DWORD64 dwAddr, PDWORD64 pdwDisplacement, LPSTR lpname, DWORD dwmaxLength) const;
  38. BOOL IsInitialized() const
  39. { return m_fInit; }
  40. private:
  41. BOOL m_fInit;
  42. };
  43. // These functions are dynamically loaded from the imagehlp.dll
  44. BOOL OleSymInitialize(HANDLE hProcess, LPSTR UserSearchPath, BOOL fInvade);
  45. BOOL OleSymCleanup(HANDLE hProcess);
  46. BOOL OleSymGetSymFromAddr(HANDLE hProcess, DWORD64 dwAddr, PDWORD64 pdwDisplacement, PIMAGEHLP_SYMBOL64 pSym);
  47. BOOL OleSymUnDName(PIMAGEHLP_SYMBOL64 pSym, LPSTR lpname, DWORD dwmaxLength);
  48. //+-------------------------------------------------------------------------
  49. //
  50. // Member: CSym::CSym
  51. //
  52. // Synopsis: Constructor
  53. //
  54. // Arguments: none
  55. //
  56. // History: 14-Jul-95 t-stevan Created
  57. //
  58. //--------------------------------------------------------------------------
  59. inline CSym::CSym()
  60. {
  61. m_fInit = OleSymInitialize(GetCurrentProcess(), NULL, TRUE);
  62. }
  63. //+-------------------------------------------------------------------------
  64. //
  65. // Member: CSym::~CSym
  66. //
  67. // Synopsis: Destructor
  68. //
  69. // Arguments: none
  70. //
  71. // History: 14-Jul-95 t-stevan Created
  72. //
  73. //--------------------------------------------------------------------------
  74. inline CSym::~CSym()
  75. {
  76. if(m_fInit)
  77. {
  78. OleSymCleanup(GetCurrentProcess());
  79. }
  80. }
  81. //+-------------------------------------------------------------------------
  82. //
  83. // Member: CSym::GetSymNameFromAddr
  84. //
  85. // Synopsis: Finds the symbol and displacement of a given address
  86. //
  87. // Arguments: [dwAddr] - the address to find the symbol for
  88. // [pdwDisplacement] - somewhere to store the displacement
  89. // [lpname] - buffer to store symbol name
  90. // [dwmaxLength] - size of buffer
  91. //
  92. // Returns: a pointer to the symbol name, or NULL if not successful
  93. //
  94. // History: 14-Jul-95 t-stevan Created
  95. //
  96. //--------------------------------------------------------------------------
  97. inline BOOL CSym::GetSymNameFromAddr(DWORD64 dwAddr, PDWORD64 pdwDisplacement, LPSTR lpname, DWORD dwmaxLength) const
  98. {
  99. if(m_fInit)
  100. {
  101. char dump[sizeof(IMAGEHLP_SYMBOL64) + MAXNAMELENGTH];
  102. PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64) &dump;
  103. // Have to do this because size of sym is dynamically determined
  104. pSym->SizeOfStruct = sizeof(dump);
  105. pSym->MaxNameLength = MAXNAMELENGTH;
  106. if (OleSymGetSymFromAddr(GetCurrentProcess(), dwAddr, pdwDisplacement, pSym))
  107. {
  108. OleSymUnDName(pSym, lpname, dwmaxLength);
  109. return(TRUE);
  110. }
  111. }
  112. return FALSE;
  113. }
  114. #endif // __SYM_HXX__