Windows NT 4.0 source code leak
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.

552 lines
8.5 KiB

4 years ago
  1. /***********************************************************************
  2. * Microsoft (R) 32-Bit Incremental Linker
  3. *
  4. * Copyright (C) Microsoft Corp 1992-95. All rights reserved.
  5. *
  6. * File: log.cpp
  7. *
  8. * File Comments:
  9. *
  10. * Log routines to track Writes, Seeks and Flushes in the linker.
  11. *
  12. ***********************************************************************/
  13. #include "link.h"
  14. #if DBG
  15. #undef lseek
  16. #undef write
  17. #undef fwrite
  18. #undef fseek
  19. #undef fread
  20. #undef read
  21. #undef open
  22. #undef fopen
  23. #undef close
  24. #undef fclose
  25. #undef BufferedWrite
  26. #undef BufferedSeek
  27. #undef BufferedRead
  28. #undef FlushBuffer
  29. #include <stdarg.h>
  30. static BOOL fSemaphore = 0;
  31. static BOOL fStartLogging = 0;
  32. extern INT fdExeFile;
  33. void
  34. On_LOG(
  35. VOID)
  36. /*++
  37. Routine Description:
  38. Turn loggin on.
  39. Arguments:
  40. None.
  41. Return Value:
  42. None.
  43. --*/
  44. {
  45. fStartLogging = 1;
  46. }
  47. INT
  48. write_LOG(
  49. INT handle,
  50. const void *pvBuf,
  51. DWORD cb)
  52. /*++
  53. Routine Description:
  54. stub routine to log writes
  55. Arguments:
  56. handle - file handle to write to
  57. pvBuf - buffer to write to file
  58. cb - count of bytes to write
  59. Return Value:
  60. number of bytes written
  61. --*/
  62. {
  63. DBEXEC(
  64. DB_IO_WRITE,
  65. Trans_LOG(LOG_write, handle, _tell(handle), cb, 0, NULL));
  66. return _write(handle, pvBuf, cb);
  67. }
  68. INT
  69. open_LOG(
  70. const char *szFileName,
  71. INT flags,
  72. ... )
  73. /*++
  74. Routine Description:
  75. stub routine to log opens
  76. Arguments:
  77. szFileName - name of file to open
  78. flags - flags to open file with
  79. mode - mode to open file with
  80. Return Value:
  81. file handle
  82. --*/
  83. {
  84. INT fd;
  85. INT mode;
  86. va_list valist;
  87. va_start( valist, flags );
  88. mode = va_arg( valist, INT );
  89. va_end( valist );
  90. fd = _open(szFileName, flags, mode);
  91. DBEXEC(
  92. DB_IO_OC,
  93. Trans_LOG(LOG_open, fd, 0, 0, 0, szFileName));
  94. return (fd);
  95. }
  96. FILE *
  97. fopen_LOG(
  98. const char *szFileName,
  99. const char *szFlags)
  100. /*++
  101. Routine Description:
  102. stub routine to log fopens
  103. Arguments:
  104. szFileName - name of file to open
  105. szFlags - flags to open file with
  106. Return Value:
  107. file descriptor
  108. --*/
  109. {
  110. FILE *pfile;
  111. pfile = fopen(szFileName, szFlags);
  112. if (pfile) {
  113. DBEXEC(
  114. DB_IO_OC,
  115. Trans_LOG(LOG_fopen, pfile->_file, 0, 0, 0, szFileName));
  116. } else {
  117. DBEXEC(
  118. DB_IO_OC,
  119. Trans_LOG(LOG_fopen, 255, 0, 0, 0, szFileName));
  120. }
  121. return (pfile);
  122. }
  123. INT
  124. close_LOG(
  125. IN INT handle)
  126. /*++
  127. Routine Description:
  128. stub routine to log closes
  129. Arguments:
  130. handle - file handle to close
  131. Return Value:
  132. 0 on success, -1 on failure
  133. --*/
  134. {
  135. DBEXEC(
  136. DB_IO_OC,
  137. Trans_LOG(LOG_close, handle, 0, 0, 0, ""));
  138. return (_close(handle));
  139. }
  140. INT
  141. fclose_LOG(
  142. IN FILE *pfile)
  143. /*++
  144. Routine Description:
  145. stub routine to log closes
  146. Arguments:
  147. pfile - file descriptor to close
  148. Return Value:
  149. 0 on success, EOF on failure
  150. --*/
  151. {
  152. DBEXEC(
  153. DB_IO_OC,
  154. Trans_LOG(LOG_fclose, pfile->_file, 0, 0, 0, ""));
  155. return (fclose(pfile));
  156. }
  157. INT
  158. read_LOG(
  159. IN INT handle,
  160. IN PVOID pvBuf,
  161. IN DWORD cb)
  162. /*++
  163. Routine Description:
  164. stub routine to log reads
  165. Arguments:
  166. handle - file handle to write to
  167. pvBuf - buffer to write to file
  168. cb - count of bytes to write
  169. Return Value:
  170. section name
  171. --*/
  172. {
  173. DBEXEC(
  174. DB_IO_READ,
  175. Trans_LOG(LOG_read, handle, _tell(handle), cb, 0, NULL));
  176. return _read(handle, pvBuf, cb);
  177. }
  178. size_t
  179. fread_LOG(
  180. PVOID pvBuf,
  181. INT cb,
  182. INT num,
  183. FILE *pfile)
  184. /*++
  185. Routine Description:
  186. stub routine to log reads
  187. Arguments:
  188. pvBuf - buffer to write to file
  189. cb - count of bytes to write
  190. num - number of chucks to read
  191. pfile - file handle
  192. Return Value:
  193. section name
  194. --*/
  195. {
  196. DBEXEC(
  197. DB_IO_READ,
  198. Trans_LOG(LOG_fread, pfile->_file, ftell(pfile), cb * num, 0, NULL));
  199. return fread(pvBuf, cb, num, pfile);
  200. }
  201. size_t
  202. fwrite_LOG(
  203. const void *pvBuf,
  204. INT cb,
  205. INT num,
  206. FILE *pfile)
  207. /*++
  208. Routine Description:
  209. stub routine to log fwrites
  210. Arguments:
  211. pvBuf - buffer
  212. cb - size of element
  213. num - number of elements to write out
  214. pfile - file handle
  215. Return Value:
  216. section name
  217. --*/
  218. {
  219. INT handle;
  220. handle = pfile->_file;
  221. if (!fSemaphore) {
  222. DBEXEC(
  223. DB_IO_WRITE,
  224. Trans_LOG(LOG_fwrite, handle, ftell(pfile), cb * num, 0, NULL));
  225. }
  226. return fwrite(pvBuf, cb, num, pfile);
  227. }
  228. LONG
  229. lseek_LOG(
  230. IN INT handle,
  231. IN LONG off,
  232. IN INT origin)
  233. /*++
  234. Routine Description:
  235. stub routine to log lseeks
  236. Arguments:
  237. handle - file handle
  238. off - file offset
  239. origin - SEEK_SET, etc.
  240. Return Value:
  241. new position of the file pointer
  242. --*/
  243. {
  244. LONG ibFile;
  245. ibFile = _lseek(handle, off, origin);
  246. DBEXEC(
  247. DB_IO_SEEK,
  248. Trans_LOG(LOG_lseek, handle, off, 0, origin, NULL));
  249. return ibFile;
  250. }
  251. INT
  252. fseek_LOG(
  253. IN FILE *pfile,
  254. IN LONG off,
  255. IN INT origin)
  256. /*++
  257. Routine Description:
  258. stub routine to log lseeks
  259. Arguments:
  260. pfile - file descriptor
  261. off - file offset
  262. origin - SEEK_SET, etc.
  263. Return Value:
  264. new position of the file pointer
  265. --*/
  266. {
  267. DBEXEC(
  268. DB_IO_SEEK,
  269. Trans_LOG(LOG_fseek, pfile->_file, off, 0UL, origin, NULL));
  270. return (fseek(pfile, off, origin));
  271. }
  272. void
  273. Trans_LOG(
  274. WORD iTrans,
  275. INT fd,
  276. LONG off,
  277. DWORD cb,
  278. INT origin,
  279. const char *sz)
  280. /*++
  281. Routine Description:
  282. log a write, seek or flush
  283. Arguments:
  284. trans - transaction type
  285. fd - file descriptor
  286. off - offset to write in file
  287. cb - count of bytes to write
  288. origin - type of seek
  289. sz - file name for open & close
  290. Return Value:
  291. None.
  292. --*/
  293. {
  294. static const char * const rgszTrans[] = {
  295. "MS", "MR", "MW",
  296. "BS", "LS", "FS",
  297. "BW", "LW", "FW",
  298. "BF", "LR", "FR",
  299. "BR", "LO", "FO",
  300. "LC", "FC"};
  301. static const char * const rgszOrigin[] = {
  302. "SET", "CUR", "END"};
  303. if (fSemaphore) {
  304. return;
  305. }
  306. // set semaphore to doing logging
  307. fSemaphore = 1;
  308. switch (iTrans) {
  309. case LOG_BufSeek:
  310. case LOG_MapSeek:
  311. case LOG_lseek:
  312. case LOG_fseek:
  313. assert(origin == SEEK_SET ||
  314. origin == SEEK_CUR ||
  315. origin == SEEK_END);
  316. assert(origin >= 0 && origin <= 2);
  317. if (fd == fdExeFile) {
  318. DBPRINT("cmd=%s fd=%2lX or=%s off=%8lX (exe: %s)\n",
  319. rgszTrans[iTrans], fd, rgszOrigin[origin], off,
  320. NULL); // SzFromOffsetInExe(off));
  321. } else {
  322. DBPRINT("cmd=%s fd=%2lX or=%s off=%8lX (obj: %s)\n",
  323. rgszTrans[iTrans], fd, rgszOrigin[origin], off,
  324. NULL); // SzFromOffsetInObj(fd, off));
  325. }
  326. break;
  327. case LOG_BufWrite:
  328. case LOG_MapWrite:
  329. case LOG_write:
  330. case LOG_fwrite:
  331. case LOG_FlushBuffer:
  332. DBPRINT("cmd=%s fd=%2lX cb=%4lX off=%8lX (%s: %s)\n",
  333. rgszTrans[iTrans], fd, cb, off,
  334. "exe", NULL); // SzFromOffsetInExe(off));
  335. break;
  336. case LOG_BufRead:
  337. case LOG_MapRead:
  338. if (FileReadHandle == fdExeFile) {
  339. DBPRINT("cmd=%s fd=%2lX cb=%4lX off=%8lX (exe: %s)\n",
  340. rgszTrans[iTrans], fd, cb, off, NULL); //SzFromOffsetInExe(off));
  341. } else {
  342. DBPRINT("cmd=%s fd=%2lX cb=%4lX off=%8lX (%s: %s)\n",
  343. rgszTrans[iTrans], fd, cb, off,
  344. rgpfi[fd]->szFileName, NULL); //SzFromOffsetInObj(fd, off));
  345. }
  346. break;
  347. case LOG_read:
  348. case LOG_fread:
  349. if (FileReadHandle == fdExeFile) {
  350. DBPRINT("cmd=%s fd=%2lX cb=%4lX off=%8lX (exe: %s)\n",
  351. rgszTrans[iTrans], fd, cb, off, NULL); // SzFromOffsetInExe(off));
  352. } else {
  353. DBPRINT("cmd=%s fd=%2lX cb=%4lX off=%8lX\n",
  354. rgszTrans[iTrans], fd, cb, off);
  355. }
  356. break;
  357. case LOG_open:
  358. case LOG_fopen:
  359. case LOG_close:
  360. case LOG_fclose:
  361. DBPRINT("cmd=%s fd=%2lX (%s)\n",
  362. rgszTrans[iTrans], fd, sz);
  363. break;
  364. default:
  365. assert(0);
  366. }
  367. fflush(stdout);
  368. // set semaphore to not doing logging
  369. fSemaphore = 0;
  370. }
  371. #endif // DBG