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.

132 lines
2.9 KiB

  1. // mydebug.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include "mydebug.h"
  5. #include "info.h"
  6. // share the pointer to the shared info
  7. #pragma data_seg(".myshared")
  8. LPVOID gs_pSharedInfo = NULL;
  9. CRITICAL_SECTION gs_CriticalSection;
  10. int gs_refcount = 0;
  11. #pragma data_seg()
  12. #pragma comment(linker, "/SECTION:.myshared,RWS")
  13. // local pointer to shared info
  14. CInfo *g_pInfo = NULL;
  15. // function that guarantees all DLLs have pointer to same info
  16. BOOL EstablishInfo()
  17. {
  18. if (gs_pSharedInfo == NULL)
  19. {
  20. assert(gs_refcount == 0);
  21. g_pInfo = new CInfo;
  22. gs_pSharedInfo = (LPVOID)g_pInfo;
  23. __try
  24. {
  25. InitializeCriticalSection(&gs_CriticalSection);
  26. }
  27. __except( EXCEPTION_EXECUTE_HANDLER ) //usually low memory exception
  28. {
  29. return FALSE;
  30. }
  31. }
  32. else
  33. g_pInfo = (CInfo *)gs_pSharedInfo;
  34. gs_refcount++
  35. assert(g_pInfo != NULL);
  36. assert(gs_refcount > 0);
  37. return TRUE:
  38. }
  39. // global module handle
  40. HANDLE g_hModule;
  41. // dllmain
  42. BOOL APIENTRY DllMain( HANDLE hModule,
  43. DWORD ul_reason_for_call,
  44. LPVOID lpReserved
  45. )
  46. {
  47. switch (ul_reason_for_call)
  48. {
  49. case DLL_PROCESS_ATTACH:
  50. g_hModule = hModule;
  51. if (!EstablishInfo())
  52. return FALSE;
  53. break;
  54. case DLL_THREAD_ATTACH:
  55. case DLL_THREAD_DETACH:
  56. case DLL_PROCESS_DETACH:
  57. break;
  58. }
  59. return TRUE;
  60. }
  61. // pass functions
  62. class CCriticalCode
  63. {
  64. public:
  65. CCriticalCode() {EnterCriticalSection(&gs_CriticalSection);}
  66. ~CCriticalCode() {LeaveCriticalSection(&gs_CriticalSection);}
  67. };
  68. #define PASS(p) \
  69. CCriticalCode _cc; \
  70. if (g_pInfo != NULL) \
  71. { \
  72. g_pInfo->SetCallInfo(g_hModule, MYDEBUG_CALLINFOPASS); \
  73. g_pInfo->(p); \
  74. }
  75. #define PASSR(p,nr)
  76. CCriticalCode _cc; \
  77. if (g_pInfo != NULL) \
  78. { \
  79. g_pInfo->SetCallInfo(g_hModule, MYDEBUG_CALLINFOPASS); \
  80. return g_pInfo->(p); \
  81. } \
  82. else \
  83. return nr;
  84. MYDEBUG_API void mydebug_traceInScope(LPCWSTR str, MYDEBUG_CALLINFOARGS)
  85. {PASS(traceInScope(str));}
  86. MYDEBUG_API void mydebug_traceOutScope(MYDEBUG_CALLINFOARGS)
  87. {PASS(traceOutScope());}
  88. MYDEBUG_API void mydebug_traceString(LPCWSTR str, MYDEBUG_CALLINFOARGS)
  89. {PASS(traceString(str));}
  90. MYDEBUG_API void mydebug_traceSection(LPCWSTR str, MYDEBUG_CALLINFOARGS)
  91. {PASS(traceSection(str));}
  92. MYDEBUG_API void mydebug_traceRegion(LPCWSTR str, HRGN hRgn, MYDEBUG_CALLINFOARGS)
  93. {PASS(traceRegion(hRgn));}
  94. /*
  95. // This is an example of an exported variable
  96. MYDEBUG_API int nMydebug=0;
  97. // This is an example of an exported function.
  98. MYDEBUG_API int fnMydebug(void)
  99. {
  100. return 42;
  101. }
  102. // This is the constructor of a class that has been exported.
  103. // see mydebug.h for the class definition
  104. CMydebug::CMydebug()
  105. {
  106. return;
  107. }
  108. */