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.

341 lines
8.4 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. kernprof.c
  5. Abstract:
  6. This module contains a dumper of the drivers loaded in the current system.
  7. Usage:
  8. drivers
  9. Author:
  10. Mark Lucovsky (markl) 15-Mar-1993
  11. Envirnoment:
  12. Revision History:
  13. --*/
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #include <memory.h>
  19. #include <ctype.h>
  20. #include <nt.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <windows.h>
  24. typedef struct _MODULE_DATA {
  25. ULONG CodeSize;
  26. ULONG DataSize;
  27. ULONG BssSize;
  28. ULONG RoDataSize;
  29. ULONG ImportDataSize;
  30. ULONG ExportDataSize;
  31. ULONG ResourceDataSize;
  32. ULONG PagedSize;
  33. ULONG InitSize;
  34. ULONG CheckSum;
  35. ULONG TimeDateStamp;
  36. } MODULE_DATA, *PMODULE_DATA;
  37. typedef struct _LOADED_IMAGE {
  38. PUCHAR MappedAddress;
  39. PIMAGE_NT_HEADERS FileHeader;
  40. PIMAGE_SECTION_HEADER LastRvaSection;
  41. int NumberOfSections;
  42. PIMAGE_SECTION_HEADER Sections;
  43. } LOADED_IMAGE, *PLOADED_IMAGE;
  44. VOID
  45. SumModuleData(
  46. PMODULE_DATA Sum,
  47. PMODULE_DATA Current
  48. )
  49. {
  50. Sum->CodeSize += Current->CodeSize;
  51. Sum->DataSize += Current->DataSize;
  52. Sum->BssSize += Current->BssSize;
  53. Sum->RoDataSize += Current->RoDataSize;
  54. Sum->ImportDataSize += Current->ImportDataSize;
  55. Sum->ExportDataSize += Current->ExportDataSize;
  56. Sum->ResourceDataSize += Current->ResourceDataSize;
  57. Sum->PagedSize += Current->PagedSize;
  58. Sum->InitSize += Current->InitSize;
  59. }
  60. VOID
  61. PrintModuleSeperator(
  62. VOID
  63. )
  64. {
  65. printf("------------------------------------------------------------------------------\n");
  66. }
  67. VOID
  68. PrintModuleHeader(
  69. VOID
  70. )
  71. {
  72. printf(" ModuleName Code Data Bss Paged Init LinkDate\n");
  73. PrintModuleSeperator();
  74. }
  75. VOID
  76. PrintModuleLine(
  77. LPSTR ModuleName,
  78. PMODULE_DATA Current
  79. )
  80. {
  81. printf("%12s %7d %7d %7d %7d %7d %s",
  82. ModuleName,
  83. Current->CodeSize,
  84. Current->DataSize,
  85. Current->BssSize,
  86. Current->PagedSize,
  87. Current->InitSize,
  88. Current->TimeDateStamp ? ctime((time_t *)&Current->TimeDateStamp) : "\n"
  89. );
  90. }
  91. VOID
  92. GetModuleData(
  93. HANDLE hFile,
  94. PMODULE_DATA Mod
  95. )
  96. {
  97. HANDLE hMappedFile;
  98. PIMAGE_DOS_HEADER DosHeader;
  99. LOADED_IMAGE LoadedImage;
  100. ULONG SectionAlignment;
  101. PIMAGE_SECTION_HEADER Section;
  102. int i;
  103. ULONG Size;
  104. hMappedFile = CreateFileMapping(
  105. hFile,
  106. NULL,
  107. PAGE_READONLY,
  108. 0,
  109. 0,
  110. NULL
  111. );
  112. if ( !hMappedFile ) {
  113. return;
  114. }
  115. LoadedImage.MappedAddress = MapViewOfFile(
  116. hMappedFile,
  117. FILE_MAP_READ,
  118. 0,
  119. 0,
  120. 0
  121. );
  122. CloseHandle(hMappedFile);
  123. if ( !LoadedImage.MappedAddress ) {
  124. return;
  125. }
  126. //
  127. // Everything is mapped. Now check the image and find nt image headers
  128. //
  129. DosHeader = (PIMAGE_DOS_HEADER)LoadedImage.MappedAddress;
  130. if ( DosHeader->e_magic != IMAGE_DOS_SIGNATURE ) {
  131. UnmapViewOfFile(LoadedImage.MappedAddress);
  132. return;
  133. }
  134. LoadedImage.FileHeader = (PIMAGE_NT_HEADERS)((ULONG_PTR)DosHeader + DosHeader->e_lfanew);
  135. if ( LoadedImage.FileHeader->Signature != IMAGE_NT_SIGNATURE ) {
  136. UnmapViewOfFile(LoadedImage.MappedAddress);
  137. return;
  138. }
  139. LoadedImage.NumberOfSections = LoadedImage.FileHeader->FileHeader.NumberOfSections;
  140. LoadedImage.Sections = (PIMAGE_SECTION_HEADER)((ULONG_PTR)LoadedImage.FileHeader + sizeof(IMAGE_NT_HEADERS));
  141. LoadedImage.LastRvaSection = LoadedImage.Sections;
  142. //
  143. // Walk through the sections and tally the dater
  144. //
  145. SectionAlignment = LoadedImage.FileHeader->OptionalHeader.SectionAlignment;
  146. for(Section = LoadedImage.Sections,i=0; i<LoadedImage.NumberOfSections; i++,Section++) {
  147. Size = Section->Misc.VirtualSize;
  148. if (Size == 0) {
  149. Size = Section->SizeOfRawData;
  150. }
  151. Size = (Size + SectionAlignment - 1) & ~(SectionAlignment - 1);
  152. if (!_strnicmp(Section->Name,"PAGE", 4 )) {
  153. Mod->PagedSize += Size;
  154. }
  155. else if (!_stricmp(Section->Name,"INIT" )) {
  156. Mod->InitSize += Size;
  157. }
  158. else if (!_stricmp(Section->Name,".bss" )) {
  159. Mod->BssSize = Size;
  160. }
  161. else if (!_stricmp(Section->Name,".edata" )) {
  162. Mod->ExportDataSize = Size;
  163. }
  164. else if (!_stricmp(Section->Name,".idata" )) {
  165. Mod->ImportDataSize = Size;
  166. }
  167. else if (!_stricmp(Section->Name,".rsrc" )) {
  168. Mod->ResourceDataSize = Size;
  169. }
  170. else if (Section->Characteristics & IMAGE_SCN_MEM_EXECUTE) {
  171. Mod->CodeSize += Size;
  172. }
  173. else if (Section->Characteristics & IMAGE_SCN_MEM_WRITE) {
  174. Mod->DataSize += Size;
  175. }
  176. else if (Section->Characteristics & IMAGE_SCN_MEM_READ) {
  177. Mod->RoDataSize += Size;
  178. }
  179. else {
  180. Mod->DataSize += Size;
  181. }
  182. }
  183. Mod->CheckSum = LoadedImage.FileHeader->OptionalHeader.CheckSum;
  184. Mod->TimeDateStamp = LoadedImage.FileHeader->FileHeader.TimeDateStamp;
  185. UnmapViewOfFile(LoadedImage.MappedAddress);
  186. return;
  187. }
  188. LONGLONG ModuleInfo[16384];
  189. __cdecl main(
  190. int argc,
  191. char *argv[],
  192. char *envp[]
  193. )
  194. {
  195. UINT BytesRequired;
  196. ULONG i;
  197. PCHAR s;
  198. HANDLE FileHandle;
  199. CHAR KernelPath[MAX_PATH];
  200. CHAR DriversPath[MAX_PATH];
  201. ULONG ReturnedLength;
  202. PRTL_PROCESS_MODULES Modules;
  203. PRTL_PROCESS_MODULE_INFORMATION Module;
  204. NTSTATUS Status;
  205. MODULE_DATA Sum;
  206. MODULE_DATA Current;
  207. //
  208. // Locate system drivers.
  209. //
  210. Status = NtQuerySystemInformation (
  211. SystemModuleInformation,
  212. ModuleInfo,
  213. sizeof( ModuleInfo ),
  214. &ReturnedLength);
  215. if (!NT_SUCCESS(Status)) {
  216. printf("query system info failed status - %lx\n",Status);
  217. return(Status);
  218. }
  219. BytesRequired = GetSystemDirectory(KernelPath,sizeof(KernelPath));
  220. if ((BytesRequired == 0) || (BytesRequired > MAX_PATH - sizeof(WCHAR))) {
  221. return STATUS_NAME_TOO_LONG;
  222. }
  223. strcpy(DriversPath,KernelPath);
  224. strcat(DriversPath,"\\Drivers");
  225. ZeroMemory(&Sum,sizeof(Sum));
  226. PrintModuleHeader();
  227. Modules = (PRTL_PROCESS_MODULES)ModuleInfo;
  228. Module = &Modules->Modules[ 0 ];
  229. for (i=0; i<Modules->NumberOfModules; i++) {
  230. ZeroMemory(&Current,sizeof(Current));
  231. s = &Module->FullPathName[ Module->OffsetToFileName ];
  232. //
  233. // try to open the file
  234. //
  235. SetCurrentDirectory(KernelPath);
  236. FileHandle = CreateFile(
  237. s,
  238. GENERIC_READ,
  239. FILE_SHARE_READ,
  240. NULL,
  241. OPEN_EXISTING,
  242. 0,
  243. NULL
  244. );
  245. if ( FileHandle == INVALID_HANDLE_VALUE ) {
  246. SetCurrentDirectory(DriversPath);
  247. FileHandle = CreateFile(
  248. s,
  249. GENERIC_READ,
  250. FILE_SHARE_READ,
  251. NULL,
  252. OPEN_EXISTING,
  253. 0,
  254. NULL
  255. );
  256. if ( ( FileHandle == INVALID_HANDLE_VALUE ) && ( argc > 1 ) ) {
  257. SetCurrentDirectory( argv[1] );
  258. FileHandle = CreateFile(
  259. s,
  260. GENERIC_READ,
  261. FILE_SHARE_READ,
  262. NULL,
  263. OPEN_EXISTING,
  264. 0,
  265. NULL
  266. );
  267. }
  268. }
  269. if ( FileHandle != INVALID_HANDLE_VALUE ) {
  270. GetModuleData(FileHandle,&Current);
  271. CloseHandle(FileHandle);
  272. }
  273. SumModuleData(&Sum,&Current);
  274. PrintModuleLine(s,&Current);
  275. Module++;
  276. }
  277. PrintModuleSeperator();
  278. PrintModuleLine("Total",&Sum);
  279. return 0;
  280. }