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.

372 lines
9.0 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. ntsdexts.c
  5. Abstract:
  6. This function contains the default ntsd debugger extensions
  7. Author:
  8. Bob Day (bobday) 29-Feb-1992 Grabbed standard header
  9. Revision History:
  10. --*/
  11. #include <precomp.h>
  12. #pragma hdrstop
  13. #include <ctype.h>
  14. VOID
  15. DumpMemory(
  16. UINT DumpType
  17. )
  18. {
  19. VDMCONTEXT ThreadContext;
  20. int mode;
  21. int i, j, lines = 8;
  22. WORD selector;
  23. ULONG offset;
  24. ULONG base;
  25. char ch;
  26. if (!DumpType) {
  27. return;
  28. }
  29. mode = GetContext( &ThreadContext );
  30. if (!GetNextToken()) {
  31. PRINTF("Please specify an address\n");
  32. return;
  33. }
  34. if (!ParseIntelAddress(&mode, &selector, &offset)) {
  35. return;
  36. }
  37. if (GetNextToken()) {
  38. if ((*lpArgumentString == 'l') || (*lpArgumentString == 'L')) {
  39. lpArgumentString++;
  40. }
  41. lines = (EXPRESSION(lpArgumentString)*DumpType+15)/16;
  42. }
  43. base = GetInfoFromSelector(selector, mode, NULL) + GetIntelBase();
  44. for (i=0; i<lines; i++) {
  45. if (offset & 0xFFFF0000) {
  46. PRINTF("%04x:%08lx ", selector, offset);
  47. } else {
  48. PRINTF("%04x:%04x ", selector, LOWORD(offset));
  49. }
  50. switch(DumpType) {
  51. case 1:
  52. for (j=0; j<16; j++) {
  53. if (j==8) {
  54. PRINTF("-");
  55. } else {
  56. PRINTF(" ");
  57. }
  58. PRINTF("%02x", ReadByteSafe(base+offset+j));
  59. }
  60. PRINTF(" ");
  61. for (j=0; j<16; j++) {
  62. ch = ReadByteSafe(base+offset+j);
  63. if (isprint(ch)) {
  64. PRINTF("%c", ch);
  65. } else {
  66. PRINTF(".");
  67. }
  68. }
  69. break;
  70. case 2:
  71. for (j=0; j<16; j+=2) {
  72. PRINTF(" %04x", ReadWordSafe(base+offset+j));
  73. }
  74. break;
  75. case 4:
  76. for (j=0; j<16; j+=4) {
  77. PRINTF(" %08lx", ReadDwordSafe(base+offset+j));
  78. }
  79. break;
  80. }
  81. PRINTF("\n");
  82. offset += 16;
  83. }
  84. }
  85. VOID
  86. DumpRegs(
  87. ) {
  88. VDMCONTEXT ThreadContext;
  89. int mode;
  90. mode = GetContext( &ThreadContext );
  91. PRINTF("eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
  92. ThreadContext.Eax,
  93. ThreadContext.Ebx,
  94. ThreadContext.Ecx,
  95. ThreadContext.Edx,
  96. ThreadContext.Esi,
  97. ThreadContext.Edi );
  98. PRINTF("eip=%08lx esp=%08lx ebp=%08lx ",
  99. ThreadContext.Eip,
  100. ThreadContext.Esp,
  101. ThreadContext.Ebp );
  102. if ( ThreadContext.EFlags & FLAG_OVERFLOW ) {
  103. PRINTF("ov ");
  104. } else {
  105. PRINTF("nv ");
  106. }
  107. if ( ThreadContext.EFlags & FLAG_DIRECTION ) {
  108. PRINTF("dn ");
  109. } else {
  110. PRINTF("up ");
  111. }
  112. if ( ThreadContext.EFlags & FLAG_INTERRUPT ) {
  113. PRINTF("ei ");
  114. } else {
  115. PRINTF("di ");
  116. }
  117. if ( ThreadContext.EFlags & FLAG_SIGN ) {
  118. PRINTF("ng ");
  119. } else {
  120. PRINTF("pl ");
  121. }
  122. if ( ThreadContext.EFlags & FLAG_ZERO ) {
  123. PRINTF("zr ");
  124. } else {
  125. PRINTF("nz ");
  126. }
  127. if ( ThreadContext.EFlags & FLAG_AUXILLIARY ) {
  128. PRINTF("ac ");
  129. } else {
  130. PRINTF("na ");
  131. }
  132. if ( ThreadContext.EFlags & FLAG_PARITY ) {
  133. PRINTF("po ");
  134. } else {
  135. PRINTF("pe ");
  136. }
  137. if ( ThreadContext.EFlags & FLAG_CARRY ) {
  138. PRINTF("cy ");
  139. } else {
  140. PRINTF("nc ");
  141. }
  142. PRINTF("\n");
  143. PRINTF("cs=%04x ss=%04x ds=%04x es=%04x fs=%04x gs=%04x efl=%08lx\n",
  144. ThreadContext.SegCs,
  145. ThreadContext.SegSs,
  146. ThreadContext.SegDs,
  147. ThreadContext.SegEs,
  148. ThreadContext.SegFs,
  149. ThreadContext.SegGs,
  150. ThreadContext.EFlags );
  151. }
  152. VOID
  153. ListModules(
  154. ) {
  155. VDMCONTEXT ThreadContext;
  156. int mode;
  157. HEAPENTRY he = {0};
  158. SELECTORINFO si;
  159. mode = GetContext( &ThreadContext );
  160. if (GetNextToken()) {
  161. he.Selector = (WORD) EXPRESSION( lpArgumentString );
  162. }
  163. PRINTF("Sel Base Limit Type Seg Module\n");
  164. PRINTF("==== ======== ======== ==== ==== ========\n");
  165. while (FindHeapEntry(&he, FALSE)) {
  166. if (he.SegmentNumber != -1) {
  167. GetInfoFromSelector((WORD)(he.gnode.pga_handle | 1), PROT_MODE, &si);
  168. PRINTF("%04X %08lX %08lX",
  169. he.gnode.pga_handle | 1,
  170. he.gnode.pga_address,
  171. he.gnode.pga_size - 1);
  172. PRINTF(" %s", si.bCode ? "Code" : "Data");
  173. PRINTF(" %04X %s\n",
  174. he.SegmentNumber,
  175. he.OwnerName);
  176. }
  177. }
  178. }
  179. VOID
  180. DumpDescriptor(
  181. ) {
  182. VDMCONTEXT ThreadContext;
  183. WORD selector;
  184. ULONG Base;
  185. int mode;
  186. SELECTORINFO si;
  187. int i, count = 16;
  188. mode = GetContext( &ThreadContext );
  189. if (GetNextToken()) {
  190. selector = (WORD) EXPRESSION( lpArgumentString );
  191. } else {
  192. PRINTF("Please enter a selector\n");
  193. return;
  194. }
  195. for (i=0; i<count; i++) {
  196. Base = GetInfoFromSelector( selector, mode, &si );
  197. PRINTF("%04X => Base: %08lX", selector, Base);
  198. #ifndef i386
  199. PRINTF(" (%08X)", Base+GetIntelBase());
  200. #endif
  201. PRINTF(" Limit: %08lX %s %s %s %s\n",
  202. si.Limit,
  203. si.bPresent ? " P" : "NP",
  204. si.bSystem ? "System" : si.bCode ? "Code " : "Data ",
  205. si.bSystem ? "" : si.bWrite ? "W" : "R",
  206. si.bSystem ? "" : si.bAccessed ? "A" : ""
  207. );
  208. selector+=8;
  209. }
  210. }
  211. //
  212. // Dump Taskinfo;
  213. //
  214. // If no argument, dump all wow tasks.
  215. // If 0, dump current WOW task
  216. // Else dump the specifies task {which is thread-id as shown by
  217. // ~ command under ntsd like 37.6b so thread-id is 6b)
  218. //
  219. void DumpTaskInfo (ptd,mode)
  220. PTD ptd;
  221. int mode;
  222. {
  223. ULONG Base;
  224. TDB tdb;
  225. BOOL b;
  226. Base = GetInfoFromSelector( ptd->htask16, mode, NULL );
  227. b = ReadProcessMem( hCurrentProcess,
  228. (LPVOID) (Base+GetIntelBase()),
  229. &tdb,
  230. sizeof(tdb),
  231. NULL );
  232. if ( !b ) {
  233. PRINTF("Failure reading TDB at %X\n", Base );
  234. return;
  235. }
  236. PRINTF("\nDump for ThreadId = %x\n",ptd->dwThreadID);
  237. PRINTF(" Stack = %x:%x\n",HIWORD(ptd->vpStack),LOWORD(ptd->vpStack));
  238. PRINTF(" HTask (TDB) = %x\n", ptd->htask16);
  239. PRINTF(" HInst = %x\n", ptd->hInst16);
  240. PRINTF(" HMod16 = %x\n", ptd->hMod16);
  241. PRINTF(" CompatFlags = %x\n",ptd->dwWOWCompatFlags);
  242. PRINTF(" HThread = %x\n",ptd->hThread);
  243. PRINTF(" TDBFlags = %x\n",tdb.TDB_flags);
  244. PRINTF(" ExpWinVer = %x\n",tdb.TDB_ExpWinVer);
  245. PRINTF(" DTA = %x:%x\n",HIWORD(tdb.TDB_DTA),LOWORD(tdb.TDB_DTA));
  246. PRINTF(" CurDir = %.64s\n",tdb.TDB_Directory);
  247. PRINTF(" ModName = %.8s\n",tdb.TDB_ModName);
  248. }
  249. void TaskInfo (
  250. ) {
  251. VDMCONTEXT ThreadContext;
  252. DWORD ThreadId;
  253. PTD ptd,ptdHead;
  254. TD td;
  255. int mode;
  256. BOOL b,fFound=FALSE;
  257. mode = GetContext( &ThreadContext );
  258. ThreadId = (DWORD)-1; // Assume Dump All Tasks
  259. if (GetNextToken()) {
  260. ThreadId = (DWORD) EXPRESSION( lpArgumentString );
  261. }
  262. ptdHead = (PTD)EXPRESSION("wow32!gptdTaskHead");
  263. // get the pointer to first TD
  264. b = ReadProcessMem( hCurrentProcess,
  265. (LPVOID) (ptdHead),
  266. &ptd,
  267. sizeof(DWORD),
  268. NULL );
  269. if ( !b ) {
  270. PRINTF("Failure reading gptdTaskHead at %08lX\n", ptdHead );
  271. return;
  272. }
  273. // enumerate td list to find the match(es)
  274. while (ptd) {
  275. b = ReadProcessMem( hCurrentProcess,
  276. (LPVOID) (ptd),
  277. &td,
  278. sizeof(TD),
  279. NULL );
  280. if ( !b ) {
  281. PRINTF("Failure reading TD At %08lX\n", ptd );
  282. return;
  283. }
  284. if (ThreadId == -1) {
  285. DumpTaskInfo (&td,mode);
  286. fFound = TRUE;
  287. }
  288. else {
  289. if (ThreadId == td.dwThreadID) {
  290. DumpTaskInfo (&td,mode);
  291. fFound = TRUE;
  292. break;
  293. }
  294. }
  295. ptd = td.ptdNext;
  296. }
  297. if (!fFound) {
  298. if (ThreadId == -1) {
  299. PRINTF("No WOW Task Found.\n");
  300. }
  301. else
  302. PRINTF("WOW Task With Thread Id = %02x Not Found.\n",ThreadId);
  303. }
  304. return;
  305. }