memory.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * Debugger memory handling
  3. *
  4. * Copyright 1993 Eric Youngdale
  5. * Copyright 1995 Alexandre Julliard
  6. * Copyright 2000-2005 Eric Pouech
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include "debugger.h"
  26. #include "wine/debug.h"
  27. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  28. void* be_cpu_linearize(HANDLE hThread, const ADDRESS64* addr)
  29. {
  30. assert(addr->Mode == AddrModeFlat);
  31. return (void*)(DWORD_PTR)addr->Offset;
  32. }
  33. BOOL be_cpu_build_addr(HANDLE hThread, const dbg_ctx_t *ctx, ADDRESS64* addr,
  34. unsigned seg, DWORD64 offset)
  35. {
  36. addr->Mode = AddrModeFlat;
  37. addr->Segment = 0; /* don't need segment */
  38. addr->Offset = offset;
  39. return TRUE;
  40. }
  41. void* memory_to_linear_addr(const ADDRESS64* addr)
  42. {
  43. return dbg_curr_process->be_cpu->linearize(dbg_curr_thread->handle, addr);
  44. }
  45. BOOL memory_get_current_pc(ADDRESS64* addr)
  46. {
  47. assert(dbg_curr_process->be_cpu->get_addr);
  48. return dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
  49. be_cpu_addr_pc, addr);
  50. }
  51. BOOL memory_get_current_stack(ADDRESS64* addr)
  52. {
  53. assert(dbg_curr_process->be_cpu->get_addr);
  54. return dbg_curr_process->be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
  55. be_cpu_addr_stack, addr);
  56. }
  57. static void memory_report_invalid_addr(const void* addr)
  58. {
  59. ADDRESS64 address;
  60. address.Mode = AddrModeFlat;
  61. address.Segment = 0;
  62. address.Offset = (ULONG_PTR)addr;
  63. dbg_printf("*** Invalid address ");
  64. print_address(&address, FALSE);
  65. dbg_printf(" ***\n");
  66. }
  67. /***********************************************************************
  68. * memory_read_value
  69. *
  70. * Read a memory value.
  71. */
  72. BOOL memory_read_value(const struct dbg_lvalue* lvalue, DWORD size, void* result)
  73. {
  74. BOOL ret = FALSE;
  75. if (lvalue->in_debuggee)
  76. {
  77. void* linear = memory_to_linear_addr(&lvalue->addr);
  78. if (!(ret = dbg_read_memory(linear, result, size)))
  79. memory_report_invalid_addr(linear);
  80. }
  81. else
  82. {
  83. if (lvalue->addr.Offset)
  84. {
  85. memcpy(result, (void*)(DWORD_PTR)lvalue->addr.Offset, size);
  86. ret = TRUE;
  87. }
  88. }
  89. return ret;
  90. }
  91. /***********************************************************************
  92. * memory_write_value
  93. *
  94. * Store a value in memory.
  95. */
  96. BOOL memory_write_value(const struct dbg_lvalue* lvalue, DWORD size, void* value)
  97. {
  98. BOOL ret = TRUE;
  99. DWORD64 os;
  100. if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &os)) return FALSE;
  101. if (size != os)
  102. {
  103. dbg_printf("Size mismatch in memory_write_value, got %u from type while expecting %u\n",
  104. (DWORD)os, size);
  105. return FALSE;
  106. }
  107. /* FIXME: only works on little endian systems */
  108. if (lvalue->in_debuggee)
  109. {
  110. void* linear = memory_to_linear_addr(&lvalue->addr);
  111. if (!(ret = dbg_write_memory(linear, value, size)))
  112. memory_report_invalid_addr(linear);
  113. }
  114. else
  115. {
  116. memcpy((void*)(DWORD_PTR)lvalue->addr.Offset, value, size);
  117. }
  118. return ret;
  119. }
  120. /* transfer a block of memory
  121. * the two lvalue:s are expected to be of same size
  122. */
  123. BOOL memory_transfer_value(const struct dbg_lvalue* to, const struct dbg_lvalue* from)
  124. {
  125. DWORD64 size_to, size_from;
  126. BYTE tmp[256];
  127. BYTE* ptr = tmp;
  128. BOOL ret;
  129. if (to->bitlen || from->bitlen) return FALSE;
  130. if (!types_get_info(&to->type, TI_GET_LENGTH, &size_to) ||
  131. !types_get_info(&from->type, TI_GET_LENGTH, &size_from) ||
  132. size_from != size_to) return FALSE;
  133. /* optimize debugger to debugger transfer */
  134. if (!to->in_debuggee && !from->in_debuggee)
  135. {
  136. memcpy(memory_to_linear_addr(&to->addr), memory_to_linear_addr(&from->addr), size_from);
  137. return TRUE;
  138. }
  139. if (size_to > sizeof(tmp))
  140. {
  141. ptr = malloc(size_from);
  142. if (!ptr) return FALSE;
  143. }
  144. ret = memory_read_value(from, size_from, ptr) &&
  145. memory_write_value(to, size_from, ptr);
  146. if (size_to > sizeof(tmp)) free(ptr);
  147. return ret;
  148. }
  149. /***********************************************************************
  150. * memory_examine
  151. *
  152. * Implementation of the 'x' command.
  153. */
  154. void memory_examine(const struct dbg_lvalue *lvalue, int count, char format)
  155. {
  156. int i;
  157. char buffer[256];
  158. ADDRESS64 addr;
  159. void *linear;
  160. types_extract_as_address(lvalue, &addr);
  161. linear = memory_to_linear_addr(&addr);
  162. if (format != 'i' && count > 1)
  163. {
  164. print_address(&addr, FALSE);
  165. dbg_printf(": ");
  166. }
  167. switch (format)
  168. {
  169. case 'u':
  170. if (count == 1) count = 256;
  171. memory_get_string(dbg_curr_process, linear,
  172. TRUE, TRUE, buffer, min(count, sizeof(buffer)));
  173. dbg_printf("%s\n", buffer);
  174. return;
  175. case 's':
  176. if (count == 1) count = 256;
  177. memory_get_string(dbg_curr_process, linear,
  178. TRUE, FALSE, buffer, min(count, sizeof(buffer)));
  179. dbg_printf("%s\n", buffer);
  180. return;
  181. case 'i':
  182. while (count-- && memory_disasm_one_insn(&addr));
  183. return;
  184. case 'g':
  185. while (count--)
  186. {
  187. GUID guid;
  188. if (!dbg_read_memory(linear, &guid, sizeof(guid)))
  189. {
  190. memory_report_invalid_addr(linear);
  191. break;
  192. }
  193. dbg_printf("{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
  194. guid.Data1, guid.Data2, guid.Data3,
  195. guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
  196. guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
  197. linear = (char*)linear + sizeof(guid);
  198. addr.Offset += sizeof(guid);
  199. if (count)
  200. {
  201. print_address(&addr, FALSE);
  202. dbg_printf(": ");
  203. }
  204. }
  205. return;
  206. #define DO_DUMP2(_t,_l,_f,_vv) { \
  207. _t _v; \
  208. for (i = 0; i < count; i++) { \
  209. if (!dbg_read_memory(linear, &_v, sizeof(_t))) \
  210. { memory_report_invalid_addr(linear); break; } \
  211. dbg_printf(_f, (_vv)); \
  212. addr.Offset += sizeof(_t); \
  213. linear = (char*)linear + sizeof(_t); \
  214. if ((i % (_l)) == (_l) - 1 && i != count - 1) \
  215. { \
  216. dbg_printf("\n"); \
  217. print_address(&addr, FALSE); \
  218. dbg_printf(": "); \
  219. } \
  220. } \
  221. dbg_printf("\n"); \
  222. }
  223. #define DO_DUMP(_t,_l,_f) DO_DUMP2(_t,_l,_f,_v)
  224. case 'x': DO_DUMP(int, 4, " %8.8x"); break;
  225. case 'd': DO_DUMP(unsigned int, 4, " %4.4d"); break;
  226. case 'w': DO_DUMP(unsigned short, 8, " %04x"); break;
  227. case 'a':
  228. if (sizeof(DWORD_PTR) == 4)
  229. {
  230. DO_DUMP(DWORD_PTR, 4, " %8.8Ix");
  231. }
  232. else
  233. {
  234. DO_DUMP(DWORD_PTR, 2, " %16.16Ix");
  235. }
  236. break;
  237. case 'c': DO_DUMP2(char, 32, " %c", (_v < 0x20) ? ' ' : _v); break;
  238. case 'b': DO_DUMP2(char, 16, " %02x", (_v) & 0xff); break;
  239. }
  240. }
  241. BOOL memory_fetch_integer(const struct dbg_lvalue* lvalue, unsigned size,
  242. BOOL is_signed, dbg_lgint_t* ret)
  243. {
  244. /* size must fit in ret and be a power of two */
  245. if (size > sizeof(*ret) || (size & (size - 1))) return FALSE;
  246. if (lvalue->bitlen)
  247. {
  248. struct dbg_lvalue alt_lvalue = *lvalue;
  249. dbg_lguint_t mask;
  250. DWORD bt;
  251. /* FIXME: this test isn't sufficient, depending on start of bitfield
  252. * (ie a 64 bit field can spread across 9 bytes)
  253. */
  254. if (lvalue->bitlen > 8 * sizeof(dbg_lgint_t)) return FALSE;
  255. alt_lvalue.addr.Offset += lvalue->bitstart >> 3;
  256. /*
  257. * Bitfield operation. We have to extract the field.
  258. */
  259. if (!memory_read_value(&alt_lvalue, sizeof(*ret), ret)) return FALSE;
  260. mask = ~(dbg_lguint_t)0 << lvalue->bitlen;
  261. *ret >>= lvalue->bitstart & 7;
  262. *ret &= ~mask;
  263. /*
  264. * OK, now we have the correct part of the number.
  265. * Check to see whether the basic type is signed or not, and if so,
  266. * we need to sign extend the number.
  267. */
  268. if (types_get_info(&lvalue->type, TI_GET_BASETYPE, &bt) &&
  269. (bt == btInt || bt == btLong) && (*ret & (1 << (lvalue->bitlen - 1))))
  270. {
  271. *ret |= mask;
  272. }
  273. }
  274. else
  275. {
  276. /* we are on little endian CPU */
  277. memset(ret, 0, sizeof(*ret)); /* clear unread bytes */
  278. if (!memory_read_value(lvalue, size, ret)) return FALSE;
  279. /* propagate sign information */
  280. if (is_signed && size < 8 && (*ret >> (size * 8 - 1)) != 0)
  281. {
  282. dbg_lguint_t neg = -1;
  283. *ret |= neg << (size * 8);
  284. }
  285. }
  286. return TRUE;
  287. }
  288. BOOL memory_store_integer(const struct dbg_lvalue* lvalue, dbg_lgint_t val)
  289. {
  290. DWORD64 size;
  291. if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size)) return FALSE;
  292. if (lvalue->bitlen)
  293. {
  294. struct dbg_lvalue alt_lvalue = *lvalue;
  295. dbg_lguint_t mask, dst;
  296. /* FIXME: this test isn't sufficient, depending on start of bitfield
  297. * (ie a 64 bit field can spread across 9 bytes)
  298. */
  299. if (lvalue->bitlen > 8 * sizeof(dbg_lgint_t)) return FALSE;
  300. /* mask is 1 where bitfield is present, 0 outside */
  301. mask = (~(dbg_lguint_t)0 >> (sizeof(val) * 8 - lvalue->bitlen)) << (lvalue->bitstart & 7);
  302. alt_lvalue.addr.Offset += lvalue->bitstart >> 3;
  303. val <<= lvalue->bitstart & 7;
  304. if (!memory_read_value(&alt_lvalue, (unsigned)size, &dst)) return FALSE;
  305. val = (dst & ~mask) | (val & mask);
  306. return memory_write_value(&alt_lvalue, (unsigned)size, &val);
  307. }
  308. /* this is simple if we're on a little endian CPU */
  309. return memory_write_value(lvalue, (unsigned)size, &val);
  310. }
  311. BOOL memory_fetch_float(const struct dbg_lvalue* lvalue, double *ret)
  312. {
  313. DWORD64 size;
  314. if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size)) return FALSE;
  315. /* FIXME: this assumes that debuggee and debugger use the same
  316. * representation for reals
  317. */
  318. if (size > sizeof(*ret)) return FALSE;
  319. if (!memory_read_value(lvalue, size, ret)) return FALSE;
  320. if (size == sizeof(float)) *ret = *(float*)ret;
  321. else if (size != sizeof(double)) return FALSE;
  322. return TRUE;
  323. }
  324. BOOL memory_store_float(const struct dbg_lvalue* lvalue, double *ret)
  325. {
  326. DWORD64 size;
  327. if (!types_get_info(&lvalue->type, TI_GET_LENGTH, &size)) return FALSE;
  328. /* FIXME: this assumes that debuggee and debugger use the same
  329. * representation for reals
  330. */
  331. if (size > sizeof(*ret)) return FALSE;
  332. if (size == sizeof(float))
  333. {
  334. float f = *ret;
  335. return memory_write_value(lvalue, size, &f);
  336. }
  337. if (size != sizeof(double)) return FALSE;
  338. return memory_write_value(lvalue, size, ret);
  339. }
  340. BOOL memory_get_string(struct dbg_process* pcs, void* addr, BOOL in_debuggee,
  341. BOOL unicode, char* buffer, int size)
  342. {
  343. SIZE_T sz;
  344. WCHAR* buffW;
  345. buffer[0] = 0;
  346. if (!addr) return FALSE;
  347. if (in_debuggee)
  348. {
  349. BOOL ret;
  350. if (!unicode) ret = pcs->process_io->read(pcs->handle, addr, buffer, size, &sz);
  351. else
  352. {
  353. buffW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  354. ret = pcs->process_io->read(pcs->handle, addr, buffW, size * sizeof(WCHAR), &sz);
  355. WideCharToMultiByte(CP_ACP, 0, buffW, sz / sizeof(WCHAR), buffer, size,
  356. NULL, NULL);
  357. HeapFree(GetProcessHeap(), 0, buffW);
  358. }
  359. if (size) buffer[size-1] = 0;
  360. return ret;
  361. }
  362. else
  363. {
  364. lstrcpynA(buffer, addr, size);
  365. }
  366. return TRUE;
  367. }
  368. BOOL memory_get_string_indirect(struct dbg_process* pcs, void* addr, BOOL unicode, WCHAR* buffer, int size)
  369. {
  370. void* ad = 0;
  371. SIZE_T sz;
  372. buffer[0] = 0;
  373. if (addr &&
  374. pcs->process_io->read(pcs->handle, addr, &ad, pcs->be_cpu->pointer_size, &sz) && sz == pcs->be_cpu->pointer_size && ad)
  375. {
  376. LPSTR buff;
  377. BOOL ret;
  378. if (unicode)
  379. ret = pcs->process_io->read(pcs->handle, ad, buffer, size * sizeof(WCHAR), &sz) && sz != 0;
  380. else
  381. {
  382. if ((buff = HeapAlloc(GetProcessHeap(), 0, size)))
  383. {
  384. ret = pcs->process_io->read(pcs->handle, ad, buff, size, &sz) && sz != 0;
  385. MultiByteToWideChar(CP_ACP, 0, buff, sz, buffer, size);
  386. HeapFree(GetProcessHeap(), 0, buff);
  387. }
  388. else ret = FALSE;
  389. }
  390. if (size) buffer[size-1] = 0;
  391. return ret;
  392. }
  393. return FALSE;
  394. }
  395. /*
  396. * Convert an address offset to hex string. If mode == 32, treat offset as
  397. * 32 bits (discard upper 32 bits), if mode == 64 use all 64 bits, if mode == 0
  398. * treat as either 32 or 64 bits, depending on whether we're running as
  399. * Wine32 or Wine64.
  400. */
  401. char* memory_offset_to_string(char *str, DWORD64 offset, unsigned mode)
  402. {
  403. if (mode != 32 && mode != 64)
  404. {
  405. #ifdef _WIN64
  406. mode = 64;
  407. #else
  408. mode = 32;
  409. #endif
  410. }
  411. if (mode == 32)
  412. sprintf(str, "0x%08x", (unsigned int) offset);
  413. else
  414. sprintf(str, "0x%08x%08x", (unsigned int)(offset >> 32),
  415. (unsigned int)offset);
  416. return str;
  417. }
  418. static void dbg_print_sdecimal(dbg_lgint_t sv)
  419. {
  420. dbg_printf("%I64d", sv);
  421. }
  422. static void dbg_print_hex(DWORD size, dbg_lgint_t sv)
  423. {
  424. if (!sv)
  425. dbg_printf("0");
  426. else
  427. /* clear unneeded high bits, esp. sign extension */
  428. dbg_printf("%#I64x", sv & (~(dbg_lguint_t)0 >> (8 * (sizeof(dbg_lgint_t) - size))));
  429. }
  430. static void print_typed_basic(const struct dbg_lvalue* lvalue)
  431. {
  432. dbg_lgint_t val_int;
  433. void* val_ptr;
  434. double val_real;
  435. DWORD64 size64;
  436. DWORD tag, size, count, bt;
  437. struct dbg_type type = lvalue->type;
  438. struct dbg_type sub_type;
  439. struct dbg_lvalue sub_lvalue;
  440. if (!types_get_real_type(&type, &tag)) return;
  441. switch (tag)
  442. {
  443. case SymTagBaseType:
  444. if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
  445. !types_get_info(&type, TI_GET_BASETYPE, &bt))
  446. {
  447. WINE_ERR("Couldn't get information\n");
  448. RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  449. return;
  450. }
  451. size = (DWORD)size64;
  452. switch (bt)
  453. {
  454. case btInt:
  455. case btLong:
  456. if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
  457. if (size == 1) goto print_char;
  458. dbg_print_hex(size, val_int);
  459. break;
  460. case btUInt:
  461. case btULong:
  462. if (!memory_fetch_integer(lvalue, size, FALSE, &val_int)) return;
  463. dbg_print_hex(size, val_int);
  464. break;
  465. case btFloat:
  466. if (!memory_fetch_float(lvalue, &val_real)) return;
  467. dbg_printf("%f", val_real);
  468. break;
  469. case btChar:
  470. case btWChar:
  471. /* sometimes WCHAR is defined as btChar with size = 2, so discrimate
  472. * Ansi/Unicode based on size, not on basetype
  473. */
  474. if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
  475. print_char:
  476. if ((size == 1 && isprint((char)val_int)) ||
  477. (size == 2 && val_int < 127 && isprint((char)val_int)))
  478. dbg_printf("'%c'", (char)val_int);
  479. else
  480. dbg_printf("%d", (int)val_int);
  481. break;
  482. case btBool:
  483. if (!memory_fetch_integer(lvalue, size, TRUE, &val_int)) return;
  484. dbg_printf("%s", val_int ? "true" : "false");
  485. break;
  486. default:
  487. WINE_FIXME("Unsupported basetype %u\n", bt);
  488. break;
  489. }
  490. break;
  491. case SymTagPointerType:
  492. if (!types_array_index(lvalue, 0, &sub_lvalue))
  493. {
  494. dbg_printf("Internal symbol error: unable to access memory location %p",
  495. memory_to_linear_addr(&lvalue->addr));
  496. break;
  497. }
  498. val_ptr = memory_to_linear_addr(&sub_lvalue.addr);
  499. if (types_get_real_type(&sub_lvalue.type, &tag) && tag == SymTagBaseType &&
  500. types_get_info(&sub_lvalue.type, TI_GET_BASETYPE, &bt) &&
  501. types_get_info(&sub_lvalue.type, TI_GET_LENGTH, &size64))
  502. {
  503. char buffer[1024];
  504. if (!val_ptr) dbg_printf("0x0");
  505. else if (((bt == btChar || bt == btInt) && size64 == 1) || (bt == btUInt && size64 == 2))
  506. {
  507. if (memory_get_string(dbg_curr_process, val_ptr, sub_lvalue.in_debuggee,
  508. size64 == 2, buffer, sizeof(buffer)))
  509. dbg_printf("\"%s\"", buffer);
  510. else
  511. dbg_printf("*** invalid address %p ***", val_ptr);
  512. break;
  513. }
  514. }
  515. dbg_printf("%p", val_ptr);
  516. break;
  517. case SymTagArrayType:
  518. case SymTagUDT:
  519. if (!memory_read_value(lvalue, sizeof(val_ptr), &val_ptr)) return;
  520. dbg_printf("%p", val_ptr);
  521. break;
  522. case SymTagEnum:
  523. {
  524. BOOL ok = FALSE;
  525. if (!types_get_info(&type, TI_GET_LENGTH, &size64) ||
  526. !memory_fetch_integer(lvalue, size64, TRUE, &val_int)) return;
  527. if (types_get_info(&type, TI_GET_CHILDRENCOUNT, &count))
  528. {
  529. char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
  530. TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
  531. WCHAR* ptr;
  532. VARIANT variant;
  533. int i;
  534. fcp->Start = 0;
  535. while (count)
  536. {
  537. fcp->Count = min(count, 256);
  538. if (types_get_info(&type, TI_FINDCHILDREN, fcp))
  539. {
  540. sub_type.module = type.module;
  541. for (i = 0; i < min(fcp->Count, count); i++)
  542. {
  543. sub_type.id = fcp->ChildId[i];
  544. if (!types_get_info(&sub_type, TI_GET_VALUE, &variant))
  545. continue;
  546. switch (V_VT(&variant))
  547. {
  548. case VT_I1: ok = (val_int == V_I1(&variant)); break;
  549. case VT_I2: ok = (val_int == V_I2(&variant)); break;
  550. case VT_I4: ok = (val_int == V_I4(&variant)); break;
  551. case VT_I8: ok = (val_int == V_I8(&variant)); break;
  552. default: WINE_FIXME("Unsupported variant type (%u)\n", V_VT(&variant));
  553. }
  554. if (ok && types_get_info(&sub_type, TI_GET_SYMNAME, &ptr) && ptr)
  555. {
  556. dbg_printf("%ls", ptr);
  557. HeapFree(GetProcessHeap(), 0, ptr);
  558. count = 0; /* so that we'll get away from outer loop */
  559. break;
  560. }
  561. }
  562. }
  563. count -= min(count, 256);
  564. fcp->Start += 256;
  565. }
  566. }
  567. if (!ok) dbg_print_sdecimal(val_int);
  568. }
  569. break;
  570. default:
  571. WINE_FIXME("Unsupported tag %u\n", tag);
  572. break;
  573. }
  574. }
  575. /***********************************************************************
  576. * print_basic
  577. *
  578. * Implementation of the 'print' command.
  579. */
  580. void print_basic(const struct dbg_lvalue* lvalue, char format)
  581. {
  582. if (lvalue->type.id == dbg_itype_none)
  583. {
  584. dbg_printf("Unable to evaluate expression\n");
  585. return;
  586. }
  587. if (format != 0)
  588. {
  589. unsigned size;
  590. dbg_lgint_t res = types_extract_as_lgint(lvalue, &size, NULL);
  591. WCHAR wch;
  592. switch (format)
  593. {
  594. case 'x':
  595. dbg_print_hex(size, res);
  596. return;
  597. case 'd':
  598. dbg_print_sdecimal(res);
  599. return;
  600. case 'c':
  601. dbg_printf("%d = '%c'", (char)(res & 0xff), (char)(res & 0xff));
  602. return;
  603. case 'u':
  604. wch = (WCHAR)(res & 0xFFFF);
  605. dbg_printf("%d = '", wch);
  606. dbg_outputW(&wch, 1);
  607. dbg_printf("'");
  608. return;
  609. case 'i':
  610. case 's':
  611. case 'w':
  612. case 'b':
  613. dbg_printf("Format specifier '%c' is meaningless in 'print' command\n", format);
  614. }
  615. }
  616. if (lvalue->type.id == dbg_itype_segptr)
  617. {
  618. dbg_print_sdecimal(types_extract_as_lgint(lvalue, NULL, NULL));
  619. }
  620. else print_typed_basic(lvalue);
  621. }
  622. void print_bare_address(const ADDRESS64* addr)
  623. {
  624. char hexbuf[MAX_OFFSET_TO_STR_LEN];
  625. switch (addr->Mode)
  626. {
  627. case AddrModeFlat:
  628. dbg_printf("%s", memory_offset_to_string(hexbuf, addr->Offset, 0));
  629. break;
  630. case AddrModeReal:
  631. case AddrMode1616:
  632. dbg_printf("0x%04x:0x%04x", addr->Segment, (unsigned) addr->Offset);
  633. break;
  634. case AddrMode1632:
  635. dbg_printf("0x%04x:%s", addr->Segment,
  636. memory_offset_to_string(hexbuf, addr->Offset, 32));
  637. break;
  638. default:
  639. dbg_printf("Unknown mode %x", addr->Mode);
  640. break;
  641. }
  642. }
  643. /***********************************************************************
  644. * print_address
  645. *
  646. * Print an 16- or 32-bit address, with the nearest symbol if any.
  647. */
  648. void print_address(const ADDRESS64* addr, BOOLEAN with_line)
  649. {
  650. char buffer[sizeof(SYMBOL_INFO) + 256];
  651. SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
  652. DWORD_PTR lin = (DWORD_PTR)memory_to_linear_addr(addr);
  653. DWORD64 disp64;
  654. DWORD disp;
  655. IMAGEHLP_MODULE im;
  656. print_bare_address(addr);
  657. si->SizeOfStruct = sizeof(*si);
  658. si->MaxNameLen = 256;
  659. im.SizeOfStruct = 0;
  660. if (SymFromAddr(dbg_curr_process->handle, lin, &disp64, si) && disp64 < si->Size)
  661. {
  662. dbg_printf(" %s", si->Name);
  663. if (disp64) dbg_printf("+0x%I64x", disp64);
  664. }
  665. else
  666. {
  667. im.SizeOfStruct = sizeof(im);
  668. if (!SymGetModuleInfo(dbg_curr_process->handle, lin, &im)) return;
  669. dbg_printf(" %s", im.ModuleName);
  670. if (lin > im.BaseOfImage)
  671. dbg_printf("+0x%Ix", lin - im.BaseOfImage);
  672. }
  673. if (with_line)
  674. {
  675. IMAGEHLP_LINE64 il;
  676. il.SizeOfStruct = sizeof(il);
  677. if (SymGetLineFromAddr64(dbg_curr_process->handle, lin, &disp, &il))
  678. dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
  679. if (im.SizeOfStruct == 0) /* don't display again module if address is in module+disp form */
  680. {
  681. im.SizeOfStruct = sizeof(im);
  682. if (SymGetModuleInfo(dbg_curr_process->handle, lin, &im))
  683. dbg_printf(" in %s", im.ModuleName);
  684. }
  685. }
  686. }
  687. BOOL memory_disasm_one_insn(ADDRESS64* addr)
  688. {
  689. char ch;
  690. print_address(addr, TRUE);
  691. dbg_printf(": ");
  692. if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
  693. {
  694. dbg_printf("-- no code accessible --\n");
  695. return FALSE;
  696. }
  697. dbg_curr_process->be_cpu->disasm_one_insn(addr, TRUE);
  698. dbg_printf("\n");
  699. return TRUE;
  700. }
  701. void memory_disassemble(const struct dbg_lvalue* xstart,
  702. const struct dbg_lvalue* xend, int instruction_count)
  703. {
  704. static ADDRESS64 last = {0,0,0};
  705. dbg_lgint_t stop = 0;
  706. int i;
  707. if (!xstart && !xend)
  708. {
  709. if (!last.Segment && !last.Offset) memory_get_current_pc(&last);
  710. }
  711. else
  712. {
  713. if (xstart)
  714. types_extract_as_address(xstart, &last);
  715. if (xend)
  716. stop = types_extract_as_integer(xend);
  717. }
  718. for (i = 0; (instruction_count == 0 || i < instruction_count) &&
  719. (stop == 0 || last.Offset <= stop); i++)
  720. memory_disasm_one_insn(&last);
  721. }
  722. BOOL memory_get_register(DWORD regno, DWORD_PTR** value, char* buffer, int len)
  723. {
  724. const struct dbg_internal_var* div;
  725. /* negative register values are wine's dbghelp hacks
  726. * see dlls/dbghelp/dbghelp_private.h for the details
  727. */
  728. switch (regno)
  729. {
  730. case -1:
  731. if (buffer) snprintf(buffer, len, "<internal error>");
  732. return FALSE;
  733. case -2:
  734. if (buffer) snprintf(buffer, len, "<couldn't compute location>");
  735. return FALSE;
  736. case -3:
  737. if (buffer) snprintf(buffer, len, "<is not available>");
  738. return FALSE;
  739. case -4:
  740. if (buffer) snprintf(buffer, len, "<couldn't read memory>");
  741. return FALSE;
  742. case -5:
  743. if (buffer) snprintf(buffer, len, "<has been optimized away by compiler>");
  744. return FALSE;
  745. }
  746. for (div = dbg_curr_process->be_cpu->context_vars; div->name; div++)
  747. {
  748. if (div->val == regno)
  749. {
  750. if (!stack_get_register_frame(div, value))
  751. {
  752. if (buffer) snprintf(buffer, len, "<register %s not accessible in this frame>", div->name);
  753. return FALSE;
  754. }
  755. if (buffer) lstrcpynA(buffer, div->name, len);
  756. return TRUE;
  757. }
  758. }
  759. if (buffer) snprintf(buffer, len, "<unknown register %u>", regno);
  760. return FALSE;
  761. }