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.

487 lines
11 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. logdump.c
  5. Abstract:
  6. this file implements functrionality to read and dump the sr logs
  7. Author:
  8. Kanwaljit Marok (kmarok) 01-May-2000
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include "logfmt.h"
  17. #include "srapi.h"
  18. struct _EVENT_STR_MAP
  19. {
  20. DWORD EventId;
  21. PCHAR pEventStr;
  22. } EventMap[ 13 ] =
  23. {
  24. {SrEventInvalid , "INVALID " },
  25. {SrEventStreamChange, "FILE-MODIFY" },
  26. {SrEventAclChange, "ACL-CHANGE " },
  27. {SrEventAttribChange, "ATTR-CHANGE" },
  28. {SrEventStreamOverwrite,"FILE-MODIFY" },
  29. {SrEventFileDelete, "FILE-DELETE" },
  30. {SrEventFileCreate, "FILE-CREATE" },
  31. {SrEventFileRename, "FILE-RENAME" },
  32. {SrEventDirectoryCreate,"DIR-CREATE " },
  33. {SrEventDirectoryRename,"DIR-RENAME " },
  34. {SrEventDirectoryDelete,"DIR-DELETE " },
  35. {SrEventMountCreate, "MNT-CREATE " },
  36. {SrEventMountDelete, "MNT-DELETE " }
  37. };
  38. BYTE Buffer[4096];
  39. PCHAR
  40. GetEventString(
  41. DWORD EventId
  42. )
  43. {
  44. PCHAR pStr = NULL;
  45. static CHAR EventStringBuffer[8];
  46. for( int i=0; i<sizeof(EventMap)/sizeof(_EVENT_STR_MAP);i++)
  47. {
  48. if ( EventMap[i].EventId == EventId )
  49. {
  50. pStr = EventMap[i].pEventStr;
  51. }
  52. }
  53. if (pStr == NULL)
  54. {
  55. pStr = &EventStringBuffer[0];
  56. wsprintf(pStr, "0x%X", EventId);
  57. }
  58. return pStr;
  59. }
  60. BOOLEAN
  61. ProcessLogEntry(
  62. BOOLEAN bPrintDebug,
  63. LPCSTR pszSerNo,
  64. LPCSTR pszSize,
  65. LPCSTR pszEndSize,
  66. LPCSTR pszSeqNo,
  67. LPCSTR pszFlags,
  68. LPCSTR pszProcess,
  69. LPCSTR pszOperation,
  70. LPCSTR pszAttr,
  71. LPCSTR pszTmpFile,
  72. LPCSTR pszPath1,
  73. LPCSTR pszPath2,
  74. LPCSTR pszAcl,
  75. LPCSTR pszShortName,
  76. LPCSTR pszProcessHandle,
  77. LPCSTR pszThreadHandle)
  78. {
  79. BOOLEAN Status = TRUE;
  80. if( bPrintDebug == FALSE )
  81. {
  82. printf( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t",
  83. pszSerNo,
  84. pszSize,
  85. pszSeqNo,
  86. pszOperation,
  87. pszAttr,
  88. pszAcl,
  89. pszPath1,
  90. pszShortName);
  91. }
  92. else
  93. {
  94. printf( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t",
  95. pszSerNo,
  96. pszSize,
  97. pszSeqNo,
  98. pszOperation,
  99. pszAttr,
  100. pszProcess,
  101. pszProcessHandle,
  102. pszThreadHandle,
  103. pszAcl,
  104. pszPath1,
  105. pszShortName);
  106. }
  107. if(pszTmpFile)
  108. {
  109. printf( "%s\t", pszTmpFile);
  110. }
  111. if(pszPath2)
  112. {
  113. printf( "%s\t", pszPath2);
  114. }
  115. printf("\n");
  116. return Status;
  117. }
  118. #define SR_MAX_PATH ((1000) + sizeof (CHAR)) // Name will always be at most 1000 characters plus a NULL.
  119. BOOLEAN
  120. ReadLogData(
  121. BOOLEAN bPrintDebugInfo,
  122. LPTSTR pszFileName
  123. )
  124. {
  125. BOOLEAN Status = FALSE;
  126. BOOLEAN bHaveDebugInfo = FALSE;
  127. HANDLE hFile;
  128. DWORD nRead;
  129. DWORD cbSize = 0, dwEntries = 0, dwEntriesAdded = 0;
  130. DWORD dwSizeLow , dwSizeHigh;
  131. CHAR szSerNo [10];
  132. CHAR szSize [10];
  133. CHAR szEndSize[10];
  134. CHAR szSeqNo [20];
  135. CHAR szOperation[50];
  136. CHAR szAttr[50];
  137. CHAR szFlags[10];
  138. PCHAR szPath1 = NULL;
  139. PCHAR szPath2 = NULL;
  140. CHAR szTmpFile[MAX_PATH];
  141. CHAR szAcl[MAX_PATH];
  142. CHAR szShortName[MAX_PATH];
  143. CHAR szProcess[32];
  144. CHAR szProcessHandle[16];
  145. CHAR szThreadHandle[16];
  146. BYTE LogHeader[2048];
  147. PSR_LOG_HEADER pLogHeader = (PSR_LOG_HEADER)LogHeader;
  148. static INT s_dwEntries = -1;
  149. szPath1 = (PCHAR) LocalAlloc( LMEM_FIXED, SR_MAX_PATH );
  150. if (szPath1 == NULL )
  151. {
  152. fprintf( stderr, "Insufficient memory\n" );
  153. }
  154. szPath2 = (PCHAR) LocalAlloc( LMEM_FIXED, SR_MAX_PATH );
  155. if (szPath2 == NULL )
  156. {
  157. fprintf( stderr, "Insufficient memory\n" );
  158. }
  159. hFile = CreateFile(
  160. pszFileName,
  161. GENERIC_READ,
  162. FILE_SHARE_WRITE,
  163. NULL,
  164. OPEN_EXISTING,
  165. FILE_ATTRIBUTE_NORMAL,
  166. NULL );
  167. if ( hFile != INVALID_HANDLE_VALUE )
  168. {
  169. PBYTE pLoc = NULL;
  170. dwSizeLow = GetFileSize( hFile, &dwSizeHigh );
  171. //
  172. // Read the header size
  173. //
  174. ReadFile(
  175. hFile,
  176. &cbSize,
  177. sizeof(DWORD),
  178. &nRead,
  179. NULL );
  180. SetFilePointer( hFile, - (INT)sizeof(DWORD), NULL, FILE_CURRENT );
  181. //
  182. // Read the whole header entry
  183. //
  184. ReadFile(
  185. hFile,
  186. pLogHeader,
  187. cbSize,
  188. &nRead,
  189. NULL );
  190. pLoc = (PBYTE)(&pLogHeader->SubRecords);
  191. fprintf( stderr,
  192. "Header Size: %ld, Version: %ld, Tool Version: %ld\n%S\n",
  193. pLogHeader->Header.RecordSize,
  194. pLogHeader->LogVersion,
  195. SR_LOG_VERSION,
  196. (LPWSTR)(pLoc + sizeof(RECORD_HEADER)) );
  197. if( pLogHeader->LogVersion != SR_LOG_VERSION ||
  198. pLogHeader->MagicNum != SR_LOG_MAGIC_NUMBER )
  199. {
  200. fprintf( stderr, "Invalid version or Corrupt log\n" );
  201. CloseHandle(hFile);
  202. goto End;
  203. }
  204. dwSizeLow -= pLogHeader->Header.RecordSize;
  205. SetFilePointer (hFile,
  206. pLogHeader->Header.RecordSize,
  207. NULL,
  208. FILE_BEGIN);
  209. //
  210. // Start reading the log entries
  211. //
  212. while( dwSizeLow )
  213. {
  214. PSR_LOG_ENTRY pLogEntry = (PSR_LOG_ENTRY)Buffer;
  215. ZeroMemory(pLogEntry, sizeof(Buffer));
  216. //
  217. // Read the size of the entry
  218. //
  219. if ( !ReadFile(
  220. hFile,
  221. &pLogEntry->Header.RecordSize,
  222. sizeof(DWORD),
  223. &nRead,
  224. NULL ) )
  225. {
  226. break;
  227. }
  228. cbSize = pLogEntry->Header.RecordSize;
  229. if (cbSize == 0 )
  230. {
  231. //
  232. // Zero size indicates end of the log
  233. //
  234. break;
  235. }
  236. SetFilePointer( hFile, - (INT)sizeof(DWORD), NULL, FILE_CURRENT );
  237. //
  238. // Read the rest of the entry
  239. //
  240. if ( !ReadFile( hFile,
  241. ((PBYTE)pLogEntry),
  242. cbSize,
  243. &nRead,
  244. NULL ) )
  245. {
  246. break;
  247. }
  248. //
  249. // Check the magic number
  250. //
  251. if( pLogEntry->MagicNum != SR_LOG_MAGIC_NUMBER )
  252. {
  253. fprintf(stderr, "Invalid Entry ( Magic num )\n");
  254. break;
  255. }
  256. //
  257. // Read the entries in to the buffer
  258. //
  259. sprintf( szSerNo , "%05d" , dwEntries + 1);
  260. sprintf( szSize , "%04d" , pLogEntry->Header.RecordSize );
  261. sprintf( szOperation, "%s" , GetEventString(
  262. pLogEntry->EntryType ));
  263. sprintf( szFlags , "%08x" , pLogEntry->EntryFlags );
  264. sprintf( szSeqNo , "%010d" , pLogEntry->SequenceNum);
  265. sprintf( szAttr , "%08x" , pLogEntry->Attributes );
  266. sprintf( szProcess , "%12.12s" , pLogEntry->ProcName );
  267. //
  268. // get the first path
  269. //
  270. PBYTE pLoc = (PBYTE)&pLogEntry->SubRecords;
  271. sprintf( szPath1 , "%S" , pLoc + sizeof(RECORD_HEADER) );
  272. if (pLogEntry->EntryFlags & ENTRYFLAGS_TEMPPATH)
  273. {
  274. pLoc += RECORD_SIZE(pLoc);
  275. sprintf( szTmpFile , "%S" , pLoc + sizeof(RECORD_HEADER) );
  276. }
  277. else
  278. {
  279. sprintf( szTmpFile , "" );
  280. }
  281. if (pLogEntry->EntryFlags & ENTRYFLAGS_SECONDPATH)
  282. {
  283. pLoc += RECORD_SIZE(pLoc);
  284. sprintf( szPath2 , "%S" , pLoc + sizeof(RECORD_HEADER) );
  285. }
  286. else
  287. {
  288. sprintf( szPath2 , "" );
  289. }
  290. if (pLogEntry->EntryFlags & ENTRYFLAGS_ACLINFO)
  291. {
  292. ULONG AclInfoSize;
  293. pLoc += RECORD_SIZE(pLoc);
  294. AclInfoSize = RECORD_SIZE(pLoc);
  295. sprintf( szAcl , "ACL(%04d)%" , AclInfoSize );
  296. }
  297. else
  298. {
  299. sprintf( szAcl , "" );
  300. }
  301. if (pLogEntry->EntryFlags & ENTRYFLAGS_DEBUGINFO)
  302. {
  303. bHaveDebugInfo = TRUE;
  304. pLoc += RECORD_SIZE(pLoc);
  305. sprintf( szProcess , "%12.12s",
  306. ((PSR_LOG_DEBUG_INFO)pLoc)->ProcessName );
  307. sprintf( szProcessHandle,"0x%08X",
  308. ((PSR_LOG_DEBUG_INFO)pLoc)->ProcessId );
  309. sprintf( szThreadHandle,"0x%08X",
  310. ((PSR_LOG_DEBUG_INFO)pLoc)->ThreadId );
  311. }
  312. else
  313. {
  314. bHaveDebugInfo = FALSE;
  315. sprintf( szProcess , "" );
  316. sprintf( szThreadHandle , "" );
  317. sprintf( szProcessHandle , "" );
  318. }
  319. if (pLogEntry->EntryFlags & ENTRYFLAGS_SHORTNAME)
  320. {
  321. pLoc += RECORD_SIZE(pLoc);
  322. sprintf( szShortName , "%S" , pLoc + sizeof(RECORD_HEADER) );
  323. }
  324. else
  325. {
  326. sprintf( szShortName , "" );
  327. }
  328. //
  329. // read the trailing record size
  330. //
  331. sprintf( szEndSize , "%04d", GET_END_SIZE(pLogEntry));
  332. ProcessLogEntry(
  333. bPrintDebugInfo && bHaveDebugInfo,
  334. szSerNo,
  335. szSize,
  336. szEndSize,
  337. szSeqNo,
  338. szFlags,
  339. szProcess,
  340. szOperation,
  341. szAttr,
  342. szTmpFile,
  343. szPath1,
  344. szPath2,
  345. szAcl,
  346. szShortName,
  347. szProcessHandle,
  348. szThreadHandle);
  349. dwEntries++;
  350. dwSizeLow -= cbSize;
  351. cbSize = 0;
  352. }
  353. CloseHandle( hFile );
  354. Status = TRUE;
  355. }
  356. else
  357. {
  358. fprintf( stderr, "Error opening LogFile %s\n", pszFileName );
  359. }
  360. End:
  361. fprintf( stderr, "Number of entries read :%d\n", dwEntries );
  362. if (szPath1 != NULL)
  363. LocalFree( szPath1 );
  364. if (szPath2 != NULL)
  365. LocalFree( szPath2 );
  366. return Status;
  367. }
  368. INT
  369. __cdecl
  370. main(
  371. int argc,
  372. char *argv[] )
  373. {
  374. if( argc < 2 || argc > 3 )
  375. {
  376. fprintf( stderr,
  377. "USAGE: %s [-d] <LogFile> \n\t -d : debug info\n",
  378. argv[0] );
  379. }
  380. else
  381. {
  382. int i = 1;
  383. if ( argc == 3 && !strcmp( argv[i], "-d" ) )
  384. {
  385. i++;
  386. ReadLogData(TRUE, argv[i] );
  387. }
  388. else
  389. {
  390. ReadLogData(FALSE, argv[i] );
  391. }
  392. }
  393. return 0;
  394. }