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.

380 lines
8.5 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. win32obj.c
  5. Abstract:
  6. This module contains helper functions for creating debug-specific
  7. named Win32 objects. Functions are included for named events,
  8. semaphores, and mutexes.
  9. Object names created by these routines have the following format:
  10. filename.ext:line_number member:address PID:pid
  11. Where:
  12. filename.ext = The file name where the object was created.
  13. line_number = The line number within the file.
  14. member = The member/global variable name where the handle is
  15. stored. This name is provided by the caller, but is usually
  16. of the form "g_Global" for globals and "CLASS::m_Member" for
  17. class members.
  18. address = An address, used to guarantee uniqueness of the objects
  19. created. This is provided by the caller. For global variables,
  20. this is typically the address of the global. For class members,
  21. this is typically the address of the containing class.
  22. pid = The current process ID. This ensures uniqueness across all
  23. processes.
  24. Here are a couple of examples:
  25. main.cxx:796 g_hShutdownEvent:683a42bc PID:373
  26. resource.cxx:136 RTL_RESOURCE::SharedSemaphore:00250970 PID:373
  27. Author:
  28. Keith Moore (keithmo) 23-Sep-1997
  29. Revision History:
  30. --*/
  31. #include <nt.h>
  32. #include <ntrtl.h>
  33. #include <nturtl.h>
  34. #include <windows.h>
  35. #include <pudebug.h>
  36. #define MAX_OBJECT_NAME 256 // chars
  37. LONG g_PuDbgEventsCreated = 0;
  38. LONG g_PuDbgSemaphoresCreated = 0;
  39. LONG g_PuDbgMutexesCreated = 0;
  40. LPSTR
  41. PuDbgpBuildObjectName(
  42. IN LPSTR ObjectNameBuffer,
  43. IN LPSTR FileName,
  44. IN ULONG LineNumber,
  45. IN LPSTR MemberName,
  46. IN PVOID Address
  47. )
  48. /*++
  49. Routine Description:
  50. Internal routine that builds an appropriate object name based on
  51. the file name, line number, member name, address, and process ID.
  52. Arguments:
  53. ObjectNameBuffer - Pointer to the target buffer for the name.
  54. FileName - The filename of the source creating the object. This
  55. is __FILE__ of the caller.
  56. LineNumber - The line number within the source. This is __LINE__
  57. of the caller.
  58. MemberName - The member/global variable name where the object handle
  59. is to be stored.
  60. Address - The address of the containing structure/class or of the
  61. global itself.
  62. Return Value:
  63. LPSTR - Pointer to ObjectNameBuffer if successful, NULL otherwise.
  64. N.B. This routine always returns NULL when running under Win9x.
  65. --*/
  66. {
  67. PLATFORM_TYPE platformType;
  68. LPSTR fileNamePart;
  69. LPSTR result;
  70. //
  71. // We have no convenient way to dump objects w/ names from
  72. // Win9x, so we'll only enable this functionality under NT.
  73. //
  74. platformType = IISGetPlatformType();
  75. result = NULL;
  76. if( platformType == PtNtServer ||
  77. platformType == PtNtWorkstation ) {
  78. //
  79. // Find the filename part of the incoming source file name.
  80. //
  81. fileNamePart = strrchr( FileName, '\\' );
  82. if( fileNamePart == NULL ) {
  83. fileNamePart = strrchr( FileName, '/' );
  84. }
  85. if( fileNamePart == NULL ) {
  86. fileNamePart = strrchr( FileName, ':' );
  87. }
  88. if( fileNamePart == NULL ) {
  89. fileNamePart = FileName;
  90. } else {
  91. fileNamePart++;
  92. }
  93. //
  94. // Ensure we don't overwrite our object name buffer.
  95. //
  96. if( ( sizeof(":1234567890 :12345678 PID:1234567890") +
  97. strlen( fileNamePart ) +
  98. strlen( MemberName ) ) < MAX_OBJECT_NAME ) {
  99. wsprintfA(
  100. ObjectNameBuffer,
  101. "%s:%lu %s:%08lx PID:%lu",
  102. fileNamePart,
  103. LineNumber,
  104. MemberName,
  105. Address,
  106. GetCurrentProcessId()
  107. );
  108. result = ObjectNameBuffer;
  109. }
  110. }
  111. return result;
  112. } // PuDbgpBuildObjectName
  113. HANDLE
  114. PuDbgCreateEvent(
  115. IN LPSTR FileName,
  116. IN ULONG LineNumber,
  117. IN LPSTR MemberName,
  118. IN PVOID Address,
  119. IN BOOL ManualReset,
  120. IN BOOL InitialState
  121. )
  122. /*++
  123. Routine Description:
  124. Creates a new event object.
  125. Arguments:
  126. FileName - The filename of the source creating the object. This
  127. is __FILE__ of the caller.
  128. LineNumber - The line number within the source. This is __LINE__
  129. of the caller.
  130. MemberName - The member/global variable name where the object handle
  131. is to be stored.
  132. Address - The address of the containing structure/class or of the
  133. global itself.
  134. ManualReset - TRUE to create a manual reset event, FALSE to create
  135. an automatic reset event.
  136. InitialState - The intitial state of the event object.
  137. Return Value:
  138. HANDLE - Handle to the object if successful, NULL otherwise.
  139. --*/
  140. {
  141. LPSTR objName;
  142. HANDLE objHandle;
  143. CHAR objNameBuffer[MAX_OBJECT_NAME];
  144. objName = PuDbgpBuildObjectName(
  145. objNameBuffer,
  146. FileName,
  147. LineNumber,
  148. MemberName,
  149. Address
  150. );
  151. objHandle = CreateEventA(
  152. NULL, // lpEventAttributes
  153. ManualReset, // bManualReset
  154. InitialState, // bInitialState
  155. objName // lpName
  156. );
  157. if( objHandle != NULL ) {
  158. InterlockedIncrement( &g_PuDbgEventsCreated );
  159. }
  160. return objHandle;
  161. } // PuDbgCreateEvent
  162. HANDLE
  163. PuDbgCreateSemaphore(
  164. IN LPSTR FileName,
  165. IN ULONG LineNumber,
  166. IN LPSTR MemberName,
  167. IN PVOID Address,
  168. IN LONG InitialCount,
  169. IN LONG MaximumCount
  170. )
  171. /*++
  172. Routine Description:
  173. Creates a new semaphore object.
  174. Arguments:
  175. FileName - The filename of the source creating the object. This
  176. is __FILE__ of the caller.
  177. LineNumber - The line number within the source. This is __LINE__
  178. of the caller.
  179. MemberName - The member/global variable name where the object handle
  180. is to be stored.
  181. Address - The address of the containing structure/class or of the
  182. global itself.
  183. InitialCount - The initial count of the semaphore.
  184. MaximumCount - The maximum count of the semaphore.
  185. Return Value:
  186. HANDLE - Handle to the object if successful, NULL otherwise.
  187. --*/
  188. {
  189. LPSTR objName;
  190. HANDLE objHandle;
  191. CHAR objNameBuffer[MAX_OBJECT_NAME];
  192. objName = PuDbgpBuildObjectName(
  193. objNameBuffer,
  194. FileName,
  195. LineNumber,
  196. MemberName,
  197. Address
  198. );
  199. objHandle = CreateSemaphoreA(
  200. NULL, // lpSemaphoreAttributes
  201. InitialCount, // lInitialCount
  202. MaximumCount, // lMaximumCount
  203. objName // lpName
  204. );
  205. if( objHandle != NULL ) {
  206. InterlockedIncrement( &g_PuDbgSemaphoresCreated );
  207. }
  208. return objHandle;
  209. } // PuDbgCreateSemaphore
  210. HANDLE
  211. PuDbgCreateMutex(
  212. IN LPSTR FileName,
  213. IN ULONG LineNumber,
  214. IN LPSTR MemberName,
  215. IN PVOID Address,
  216. IN BOOL InitialOwner
  217. )
  218. /*++
  219. Routine Description:
  220. Creates a new mutex object.
  221. Arguments:
  222. FileName - The filename of the source creating the object. This
  223. is __FILE__ of the caller.
  224. LineNumber - The line number within the source. This is __LINE__
  225. of the caller.
  226. MemberName - The member/global variable name where the object handle
  227. is to be stored.
  228. Address - The address of the containing structure/class or of the
  229. global itself.
  230. InitialOwner - TRUE if the mutex should be created "owned".
  231. Return Value:
  232. HANDLE - Handle to the object if successful, NULL otherwise.
  233. --*/
  234. {
  235. LPSTR objName;
  236. HANDLE objHandle;
  237. CHAR objNameBuffer[MAX_OBJECT_NAME];
  238. objName = PuDbgpBuildObjectName(
  239. objNameBuffer,
  240. FileName,
  241. LineNumber,
  242. MemberName,
  243. Address
  244. );
  245. objHandle = CreateMutexA(
  246. NULL, // lpMutexAttributes
  247. InitialOwner, // bInitialOwner,
  248. objName // lpName
  249. );
  250. if( objHandle != NULL ) {
  251. InterlockedIncrement( &g_PuDbgMutexesCreated );
  252. }
  253. return objHandle;
  254. } // PuDbgCreateMutex