symbol.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * Generate hash tables for Wine debugger symbols
  3. *
  4. * Copyright (C) 1993, Eric Youngdale.
  5. * 2004-2005, Eric Pouech.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "debugger.h"
  26. #include "wine/debug.h"
  27. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  28. static BOOL symbol_get_debug_start(const struct dbg_type* func, ULONG64* start)
  29. {
  30. DWORD count, tag;
  31. char buffer[sizeof(TI_FINDCHILDREN_PARAMS) + 256 * sizeof(DWORD)];
  32. TI_FINDCHILDREN_PARAMS* fcp = (TI_FINDCHILDREN_PARAMS*)buffer;
  33. int i;
  34. struct dbg_type child;
  35. if (!func->id) return FALSE; /* native dbghelp not always fills the info field */
  36. if (!types_get_info(func, TI_GET_CHILDRENCOUNT, &count)) return FALSE;
  37. fcp->Start = 0;
  38. while (count)
  39. {
  40. fcp->Count = min(count, 256);
  41. if (types_get_info(func, TI_FINDCHILDREN, fcp))
  42. {
  43. for (i = 0; i < min(fcp->Count, count); i++)
  44. {
  45. child.module = func->module;
  46. child.id = fcp->ChildId[i];
  47. types_get_info(&child, TI_GET_SYMTAG, &tag);
  48. if (tag != SymTagFuncDebugStart) continue;
  49. return types_get_info(&child, TI_GET_ADDRESS, start);
  50. }
  51. count -= min(count, 256);
  52. fcp->Start += 256;
  53. fcp->Start += 256;
  54. }
  55. }
  56. return FALSE;
  57. }
  58. static BOOL fill_sym_lvalue(const SYMBOL_INFO* sym, ULONG_PTR base,
  59. struct dbg_lvalue* lvalue, char* buffer, size_t sz)
  60. {
  61. if (buffer) buffer[0] = '\0';
  62. if (sym->Flags & SYMFLAG_REGISTER)
  63. {
  64. DWORD_PTR* pval;
  65. if (!memory_get_register(sym->Register, &pval, buffer, sz))
  66. return FALSE;
  67. lvalue->cookie = DLV_HOST;
  68. lvalue->addr.Offset = (DWORD_PTR)pval;
  69. }
  70. else if (sym->Flags & SYMFLAG_REGREL)
  71. {
  72. DWORD_PTR* pval;
  73. size_t l;
  74. *buffer++ = '['; sz--;
  75. if (!memory_get_register(sym->Register, &pval, buffer, sz))
  76. return FALSE;
  77. l = strlen(buffer);
  78. sz -= l;
  79. buffer += l;
  80. lvalue->cookie = DLV_TARGET;
  81. lvalue->addr.Offset = (ULONG64)*pval + sym->Address;
  82. if ((LONG_PTR)sym->Address >= 0)
  83. snprintf(buffer, sz, "+%ld]", (ULONG_PTR)sym->Address);
  84. else
  85. snprintf(buffer, sz, "-%ld]", -(LONG_PTR)sym->Address);
  86. }
  87. else if (sym->Flags & SYMFLAG_VALUEPRESENT)
  88. {
  89. struct dbg_type type;
  90. VARIANT v;
  91. type.module = sym->ModBase;
  92. type.id = sym->Index;
  93. if (!types_get_info(&type, TI_GET_VALUE, &v))
  94. {
  95. if (buffer) snprintf(buffer, sz, "Couldn't get full value information for %s", sym->Name);
  96. return FALSE;
  97. }
  98. else if (v.n1.n2.vt & VT_BYREF)
  99. {
  100. /* FIXME: this won't work for pointers or arrays, as we don't always
  101. * know, if the value to be dereferenced lies in debuggee or
  102. * debugger address space.
  103. */
  104. if (sym->Tag == SymTagPointerType || sym->Tag == SymTagArrayType)
  105. {
  106. if (buffer) snprintf(buffer, sz, "Couldn't dereference pointer for const value for %s", sym->Name);
  107. return FALSE;
  108. }
  109. /* this is likely Wine's dbghelp which passes const values by reference
  110. * (object is managed by dbghelp, hence in debugger address space)
  111. */
  112. lvalue->cookie = DLV_HOST;
  113. lvalue->addr.Offset = (DWORD_PTR)sym->Value;
  114. }
  115. else
  116. {
  117. DWORD* pdw = (DWORD*)lexeme_alloc_size(sizeof(*pdw));
  118. lvalue->cookie = DLV_HOST;
  119. lvalue->addr.Offset = (DWORD_PTR)pdw;
  120. *pdw = sym->Value;
  121. }
  122. }
  123. else if (sym->Flags & SYMFLAG_LOCAL)
  124. {
  125. lvalue->cookie = DLV_TARGET;
  126. lvalue->addr.Offset = base + sym->Address;
  127. }
  128. else if (sym->Flags & SYMFLAG_TLSREL)
  129. {
  130. PROCESS_BASIC_INFORMATION pbi;
  131. THREAD_BASIC_INFORMATION tbi;
  132. DWORD_PTR addr;
  133. PEB peb;
  134. PEB_LDR_DATA ldr_data;
  135. PLIST_ENTRY head, current;
  136. LDR_DATA_TABLE_ENTRY ldr_module;
  137. unsigned tlsindex = -1;
  138. if (NtQueryInformationProcess(dbg_curr_process->handle, ProcessBasicInformation,
  139. &pbi, sizeof(pbi), NULL) ||
  140. NtQueryInformationThread(dbg_curr_thread->handle, ThreadBasicInformation,
  141. &tbi, sizeof(tbi), NULL))
  142. {
  143. tls_error:
  144. if (buffer) snprintf(buffer, sz, "Cannot read TLS address\n");
  145. return FALSE;
  146. }
  147. addr = (DWORD_PTR)&(((TEB*)tbi.TebBaseAddress)->ThreadLocalStoragePointer);
  148. if (!dbg_read_memory((void*)addr, &addr, sizeof(addr)) ||
  149. !dbg_read_memory(pbi.PebBaseAddress, &peb, sizeof(peb)) ||
  150. !dbg_read_memory(peb.LdrData, &ldr_data, sizeof(ldr_data)))
  151. goto tls_error;
  152. current = ldr_data.InLoadOrderModuleList.Flink;
  153. head = &((PEB_LDR_DATA*)peb.LdrData)->InLoadOrderModuleList;
  154. do
  155. {
  156. if (!dbg_read_memory(CONTAINING_RECORD(current, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks),
  157. &ldr_module, sizeof(ldr_module))) goto tls_error;
  158. if ((DWORD_PTR)ldr_module.DllBase == sym->ModBase)
  159. {
  160. tlsindex = ldr_module.TlsIndex;
  161. break;
  162. }
  163. current = ldr_module.InLoadOrderLinks.Flink;
  164. } while (current != head);
  165. addr += tlsindex * sizeof(DWORD_PTR);
  166. if (!dbg_read_memory((void*)addr, &addr, sizeof(addr))) goto tls_error;
  167. lvalue->cookie = DLV_TARGET;
  168. lvalue->addr.Offset = addr + sym->Address;
  169. }
  170. else
  171. {
  172. lvalue->cookie = DLV_TARGET;
  173. lvalue->addr.Offset = sym->Address;
  174. }
  175. lvalue->addr.Mode = AddrModeFlat;
  176. lvalue->type.module = sym->ModBase;
  177. lvalue->type.id = sym->TypeIndex;
  178. return TRUE;
  179. }
  180. struct sgv_data
  181. {
  182. #define NUMDBGV 100
  183. struct
  184. {
  185. /* FIXME: NUMDBGV should be made variable */
  186. struct dbg_lvalue lvalue;
  187. DWORD flags;
  188. DWORD sym_info;
  189. } syms[NUMDBGV]; /* out : will be filled in with various found symbols */
  190. int num; /* out : number of found symbols */
  191. int num_thunks; /* out : number of thunks found */
  192. const char* name; /* in : name of symbol to look up */
  193. unsigned do_thunks : 1; /* in : whether we return thunks tags */
  194. ULONG64 frame_offset; /* in : frame for local & parameter variables look up */
  195. };
  196. static BOOL CALLBACK sgv_cb(PSYMBOL_INFO sym, ULONG size, PVOID ctx)
  197. {
  198. struct sgv_data* sgv = ctx;
  199. unsigned insp;
  200. char tmp[64];
  201. if (sym->Flags & SYMFLAG_THUNK)
  202. {
  203. if (!sgv->do_thunks) return TRUE;
  204. sgv->num_thunks++;
  205. }
  206. if (sgv->num >= NUMDBGV)
  207. {
  208. dbg_printf("Too many addresses for symbol '%s', limiting the first %d\n",
  209. sgv->name, NUMDBGV);
  210. return FALSE;
  211. }
  212. WINE_TRACE("==> %s %s%s%s%s%s%s%s%s\n",
  213. sym->Name,
  214. (sym->Flags & SYMFLAG_FUNCTION) ? "func " : "",
  215. (sym->Flags & SYMFLAG_FRAMEREL) ? "framerel " : "",
  216. (sym->Flags & SYMFLAG_TLSREL) ? "tlsrel " : "",
  217. (sym->Flags & SYMFLAG_REGISTER) ? "register " : "",
  218. (sym->Flags & SYMFLAG_REGREL) ? "regrel " : "",
  219. (sym->Flags & SYMFLAG_PARAMETER) ? "param " : "",
  220. (sym->Flags & SYMFLAG_LOCAL) ? "local " : "",
  221. (sym->Flags & SYMFLAG_THUNK) ? "thunk " : "");
  222. /* always keep the thunks at end of the array */
  223. insp = sgv->num;
  224. if (sgv->num_thunks && !(sym->Flags & SYMFLAG_THUNK))
  225. {
  226. insp -= sgv->num_thunks;
  227. memmove(&sgv->syms[insp + 1], &sgv->syms[insp],
  228. sizeof(sgv->syms[0]) * sgv->num_thunks);
  229. }
  230. if (!fill_sym_lvalue(sym, sgv->frame_offset, &sgv->syms[insp].lvalue, tmp, sizeof(tmp)))
  231. {
  232. dbg_printf("%s: %s\n", sym->Name, tmp);
  233. return TRUE;
  234. }
  235. sgv->syms[insp].flags = sym->Flags;
  236. sgv->syms[insp].sym_info = sym->Index;
  237. sgv->num++;
  238. return TRUE;
  239. }
  240. enum sym_get_lval symbol_picker_interactive(const char* name, const struct sgv_data* sgv,
  241. struct dbg_lvalue* rtn)
  242. {
  243. char buffer[512];
  244. unsigned i;
  245. if (!dbg_interactiveP)
  246. {
  247. dbg_printf("More than one symbol named %s, picking the first one\n", name);
  248. *rtn = sgv->syms[0].lvalue;
  249. return sglv_found;
  250. }
  251. dbg_printf("Many symbols with name '%s', "
  252. "choose the one you want (<cr> to abort):\n", name);
  253. for (i = 0; i < sgv->num; i++)
  254. {
  255. if (sgv->num - sgv->num_thunks > 1 && (sgv->syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
  256. continue;
  257. dbg_printf("[%d]: ", i + 1);
  258. if (sgv->syms[i].flags & (SYMFLAG_LOCAL | SYMFLAG_PARAMETER))
  259. {
  260. dbg_printf("%s %sof %s\n",
  261. sgv->syms[i].flags & SYMFLAG_PARAMETER ? "Parameter" : "Local variable",
  262. sgv->syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL) ? "(in a register) " : "",
  263. name);
  264. }
  265. else if (sgv->syms[i].flags & SYMFLAG_THUNK)
  266. {
  267. print_address(&sgv->syms[i].lvalue.addr, TRUE);
  268. /* FIXME: should display where the thunks points to */
  269. dbg_printf(" thunk %s\n", name);
  270. }
  271. else
  272. {
  273. print_address(&sgv->syms[i].lvalue.addr, TRUE);
  274. dbg_printf("\n");
  275. }
  276. }
  277. do
  278. {
  279. if (input_read_line("=> ", buffer, sizeof(buffer)))
  280. {
  281. if (buffer[0] == '\0') return sglv_aborted;
  282. i = atoi(buffer);
  283. if (i < 1 || i > sgv->num)
  284. dbg_printf("Invalid choice %d\n", i);
  285. }
  286. else return sglv_aborted;
  287. } while (i < 1 || i > sgv->num);
  288. /* The array is 0-based, but the choices are 1..n,
  289. * so we have to subtract one before returning.
  290. */
  291. *rtn = sgv->syms[i - 1].lvalue;
  292. return sglv_found;
  293. }
  294. enum sym_get_lval symbol_picker_scoped(const char* name, const struct sgv_data* sgv,
  295. struct dbg_lvalue* rtn)
  296. {
  297. unsigned i;
  298. int local = -1;
  299. for (i = 0; i < sgv->num; i++)
  300. {
  301. if (sgv->num - sgv->num_thunks > 1 && (sgv->syms[i].flags & SYMFLAG_THUNK) && !DBG_IVAR(AlwaysShowThunks))
  302. continue;
  303. if (sgv->syms[i].flags & (SYMFLAG_LOCAL | SYMFLAG_PARAMETER))
  304. {
  305. if (local == -1)
  306. local = i;
  307. else
  308. {
  309. /* FIXME: several locals with same name... which one to pick ?? */
  310. dbg_printf("Several local variables/parameters for %s, aborting\n", name);
  311. return sglv_aborted;
  312. }
  313. }
  314. }
  315. if (local != -1)
  316. {
  317. *rtn = sgv->syms[local].lvalue;
  318. return sglv_found;
  319. }
  320. /* no locals found, multiple globals... abort for now */
  321. dbg_printf("Several global variables for %s, aborting\n", name);
  322. return sglv_aborted;
  323. }
  324. symbol_picker_t symbol_current_picker = symbol_picker_interactive;
  325. /***********************************************************************
  326. * symbol_get_lvalue
  327. *
  328. * Get the address of a named symbol.
  329. * Return values:
  330. * sglv_found: if the symbol is found
  331. * sglv_unknown: if the symbol isn't found
  332. * sglv_aborted: some error occurred (likely, many symbols of same name exist,
  333. * and user didn't pick one of them)
  334. */
  335. enum sym_get_lval symbol_get_lvalue(const char* name, const int lineno,
  336. struct dbg_lvalue* rtn, BOOL bp_disp)
  337. {
  338. struct sgv_data sgv;
  339. int i;
  340. char buffer[512];
  341. BOOL opt;
  342. IMAGEHLP_STACK_FRAME ihsf;
  343. if (strlen(name) + 4 > sizeof(buffer))
  344. {
  345. WINE_WARN("Too long symbol (%s)\n", name);
  346. return sglv_unknown;
  347. }
  348. sgv.num = 0;
  349. sgv.num_thunks = 0;
  350. sgv.name = &buffer[2];
  351. sgv.do_thunks = DBG_IVAR(AlwaysShowThunks);
  352. if (strchr(name, '!'))
  353. {
  354. strcpy(buffer, name);
  355. }
  356. else
  357. {
  358. buffer[0] = '*';
  359. buffer[1] = '!';
  360. strcpy(&buffer[2], name);
  361. }
  362. /* this is a wine specific options to return also ELF modules in the
  363. * enumeration
  364. */
  365. opt = SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, TRUE);
  366. SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv);
  367. if (!sgv.num)
  368. {
  369. const char* ptr = strchr(name, '!');
  370. if ((ptr && ptr[1] != '_') || (!ptr && *name != '_'))
  371. {
  372. if (ptr)
  373. {
  374. int offset = ptr - name;
  375. memcpy(buffer, name, offset + 1);
  376. buffer[offset + 1] = '_';
  377. strcpy(&buffer[offset + 2], ptr + 1);
  378. }
  379. else
  380. {
  381. buffer[0] = '*';
  382. buffer[1] = '!';
  383. buffer[2] = '_';
  384. strcpy(&buffer[3], name);
  385. }
  386. SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv);
  387. }
  388. }
  389. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  390. /* now grab local symbols */
  391. if (stack_get_current_frame(&ihsf) && sgv.num < NUMDBGV)
  392. {
  393. sgv.frame_offset = ihsf.FrameOffset;
  394. SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
  395. }
  396. if (!sgv.num)
  397. {
  398. dbg_printf("No symbols found for %s\n", name);
  399. return sglv_unknown;
  400. }
  401. /* recompute potential offsets for functions (linenumber, skip prolog) */
  402. for (i = 0; i < sgv.num; i++)
  403. {
  404. if (sgv.syms[i].flags & (SYMFLAG_REGISTER|SYMFLAG_REGREL|SYMFLAG_LOCAL|SYMFLAG_THUNK))
  405. continue;
  406. if (lineno == -1)
  407. {
  408. struct dbg_type type;
  409. ULONG64 addr;
  410. type.module = sgv.syms[i].lvalue.type.module;
  411. type.id = sgv.syms[i].sym_info;
  412. if (bp_disp && symbol_get_debug_start(&type, &addr))
  413. sgv.syms[i].lvalue.addr.Offset = addr;
  414. }
  415. else
  416. {
  417. DWORD disp;
  418. IMAGEHLP_LINE64 il;
  419. BOOL found = FALSE;
  420. il.SizeOfStruct = sizeof(il);
  421. SymGetLineFromAddr64(dbg_curr_process->handle,
  422. (DWORD_PTR)memory_to_linear_addr(&sgv.syms[i].lvalue.addr),
  423. &disp, &il);
  424. do
  425. {
  426. if (lineno == il.LineNumber)
  427. {
  428. sgv.syms[i].lvalue.addr.Offset = il.Address;
  429. found = TRUE;
  430. break;
  431. }
  432. } while (SymGetLineNext64(dbg_curr_process->handle, &il));
  433. if (!found)
  434. WINE_FIXME("No line (%d) found for %s (setting to symbol start)\n",
  435. lineno, name);
  436. }
  437. }
  438. if (sgv.num - sgv.num_thunks > 1 || /* many symbols non thunks (and showing only non thunks) */
  439. (sgv.num > 1 && DBG_IVAR(AlwaysShowThunks)) || /* many symbols (showing symbols & thunks) */
  440. (sgv.num == sgv.num_thunks && sgv.num_thunks > 1))
  441. {
  442. return symbol_current_picker(name, &sgv, rtn);
  443. }
  444. /* first symbol is the one we want:
  445. * - only one symbol found,
  446. * - or many symbols but only one non thunk when AlwaysShowThunks is FALSE
  447. */
  448. *rtn = sgv.syms[0].lvalue;
  449. return sglv_found;
  450. }
  451. BOOL symbol_is_local(const char* name)
  452. {
  453. struct sgv_data sgv;
  454. IMAGEHLP_STACK_FRAME ihsf;
  455. sgv.num = 0;
  456. sgv.num_thunks = 0;
  457. sgv.name = name;
  458. sgv.do_thunks = FALSE;
  459. if (stack_get_current_frame(&ihsf))
  460. {
  461. sgv.frame_offset = ihsf.FrameOffset;
  462. SymEnumSymbols(dbg_curr_process->handle, 0, name, sgv_cb, (void*)&sgv);
  463. }
  464. return sgv.num > 0;
  465. }
  466. /***********************************************************************
  467. * symbol_read_symtable
  468. *
  469. * Read a symbol file into the hash table.
  470. */
  471. void symbol_read_symtable(const char* filename, unsigned long offset)
  472. {
  473. dbg_printf("No longer supported\n");
  474. #if 0
  475. /* FIXME: have to implement SymAddSymbol in dbghelp, but likely we'll need to link
  476. * this with an already loaded module !!
  477. */
  478. FILE* symbolfile;
  479. unsigned addr;
  480. char type;
  481. char* cpnt;
  482. char buffer[256];
  483. char name[256];
  484. if (!(symbolfile = fopen(filename, "r")))
  485. {
  486. WINE_WARN("Unable to open symbol table %s\n", filename);
  487. return;
  488. }
  489. dbg_printf("Reading symbols from file %s\n", filename);
  490. while (1)
  491. {
  492. fgets(buffer, sizeof(buffer), symbolfile);
  493. if (feof(symbolfile)) break;
  494. /* Strip any text after a # sign (i.e. comments) */
  495. cpnt = strchr(buffer, '#');
  496. if (cpnt) *cpnt = '\0';
  497. /* Quietly ignore any lines that have just whitespace */
  498. for (cpnt = buffer; *cpnt; cpnt++)
  499. {
  500. if (*cpnt != ' ' && *cpnt != '\t') break;
  501. }
  502. if (!*cpnt || *cpnt == '\n') continue;
  503. if (sscanf(buffer, "%lx %c %s", &addr, &type, name) == 3)
  504. {
  505. if (value.addr.off + offset < value.addr.off)
  506. WINE_WARN("Address wrap around\n");
  507. value.addr.off += offset;
  508. SymAddSymbol(current_process->handle, BaseOfDll,
  509. name, addr, 0, 0);
  510. }
  511. }
  512. fclose(symbolfile);
  513. #endif
  514. }
  515. /***********************************************************************
  516. * symbol_get_function_line_status
  517. *
  518. * Find the symbol nearest to a given address.
  519. */
  520. enum dbg_line_status symbol_get_function_line_status(const ADDRESS64* addr)
  521. {
  522. IMAGEHLP_LINE64 il;
  523. DWORD disp;
  524. ULONG64 disp64, start;
  525. DWORD_PTR lin = (DWORD_PTR)memory_to_linear_addr(addr);
  526. char buffer[sizeof(SYMBOL_INFO) + 256];
  527. SYMBOL_INFO* sym = (SYMBOL_INFO*)buffer;
  528. struct dbg_type func;
  529. il.SizeOfStruct = sizeof(il);
  530. sym->SizeOfStruct = sizeof(SYMBOL_INFO);
  531. sym->MaxNameLen = sizeof(buffer) - sizeof(SYMBOL_INFO);
  532. /* do we have some info for lin address ? */
  533. if (!SymFromAddr(dbg_curr_process->handle, lin, &disp64, sym))
  534. {
  535. ADDRESS64 jumpee;
  536. /* some compilers insert thunks in their code without debug info associated
  537. * take care of this situation
  538. */
  539. if (dbg_curr_process->be_cpu->is_jump((void*)lin, &jumpee))
  540. return symbol_get_function_line_status(&jumpee);
  541. return dbg_no_line_info;
  542. }
  543. switch (sym->Tag)
  544. {
  545. case SymTagThunk:
  546. /* FIXME: so far dbghelp doesn't return the 16 <=> 32 thunks
  547. * and furthermore, we no longer take care of them !!!
  548. */
  549. return dbg_in_a_thunk;
  550. case SymTagFunction:
  551. case SymTagPublicSymbol: break;
  552. default:
  553. WINE_FIXME("Unexpected sym-tag 0x%08x\n", sym->Tag);
  554. case SymTagData:
  555. return dbg_no_line_info;
  556. }
  557. /* we should have a function now */
  558. if (!SymGetLineFromAddr64(dbg_curr_process->handle, lin, &disp, &il))
  559. return dbg_no_line_info;
  560. func.module = sym->ModBase;
  561. func.id = sym->Index;
  562. if (symbol_get_debug_start(&func, &start) && lin < start)
  563. return dbg_not_on_a_line_number;
  564. if (!sym->Size) sym->Size = 0x100000;
  565. if (il.FileName && il.FileName[0] && disp < sym->Size)
  566. return (disp == 0) ? dbg_on_a_line_number : dbg_not_on_a_line_number;
  567. return dbg_no_line_info;
  568. }
  569. /***********************************************************************
  570. * symbol_get_line
  571. *
  572. * Find the symbol nearest to a given address.
  573. * Returns sourcefile name and line number in a format that the listing
  574. * handler can deal with.
  575. */
  576. BOOL symbol_get_line(const char* filename, const char* name,
  577. IMAGEHLP_LINE64* line)
  578. {
  579. struct sgv_data sgv;
  580. char buffer[512];
  581. DWORD opt, disp;
  582. unsigned i;
  583. BOOL found = FALSE;
  584. IMAGEHLP_LINE64 il;
  585. sgv.num = 0;
  586. sgv.num_thunks = 0;
  587. sgv.name = &buffer[2];
  588. sgv.do_thunks = FALSE;
  589. buffer[0] = '*';
  590. buffer[1] = '!';
  591. strcpy(&buffer[2], name);
  592. /* this is a wine specific options to return also ELF modules in the
  593. * enumeration
  594. */
  595. opt = SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, TRUE);
  596. if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
  597. {
  598. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  599. return FALSE;
  600. }
  601. if (!sgv.num && (name[0] != '_'))
  602. {
  603. buffer[2] = '_';
  604. strcpy(&buffer[3], name);
  605. if (!SymEnumSymbols(dbg_curr_process->handle, 0, buffer, sgv_cb, (void*)&sgv))
  606. {
  607. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  608. return FALSE;
  609. }
  610. }
  611. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  612. for (i = 0; i < sgv.num; i++)
  613. {
  614. DWORD_PTR linear = (DWORD_PTR)memory_to_linear_addr(&sgv.syms[i].lvalue.addr);
  615. il.SizeOfStruct = sizeof(il);
  616. if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il))
  617. continue;
  618. if (filename && strcmp(il.FileName, filename)) continue;
  619. if (found)
  620. {
  621. WINE_FIXME("Several found, returning first (may not be what you want)...\n");
  622. break;
  623. }
  624. found = TRUE;
  625. *line = il;
  626. }
  627. if (!found)
  628. {
  629. if (filename) dbg_printf("No such function %s in %s\n", name, filename);
  630. else dbg_printf("No such function %s\n", name);
  631. return FALSE;
  632. }
  633. return TRUE;
  634. }
  635. /******************************************************************
  636. * symbol_print_local
  637. *
  638. * Overall format is:
  639. * <name>=<value> in non detailed form
  640. * <name>=<value> (local|pmt <where>) in detailed form
  641. * Note <value> can be an error message in case of error
  642. */
  643. void symbol_print_local(const SYMBOL_INFO* sym, DWORD_PTR base, BOOL detailed)
  644. {
  645. struct dbg_lvalue lvalue;
  646. char buffer[64];
  647. dbg_printf("%s=", sym->Name);
  648. if (fill_sym_lvalue(sym, base, &lvalue, buffer, sizeof(buffer)))
  649. {
  650. print_value(&lvalue, 0, 1);
  651. if (detailed)
  652. dbg_printf(" (%s %s)",
  653. (sym->Flags & SYMFLAG_PARAMETER) ? "parameter" : "local",
  654. buffer);
  655. }
  656. else
  657. {
  658. dbg_printf("%s", buffer);
  659. if (detailed)
  660. dbg_printf(" (%s)",
  661. (sym->Flags & SYMFLAG_PARAMETER) ? "parameter" : "local");
  662. }
  663. }
  664. static BOOL CALLBACK info_locals_cb(PSYMBOL_INFO sym, ULONG size, PVOID ctx)
  665. {
  666. struct dbg_type type;
  667. dbg_printf("\t");
  668. type.module = sym->ModBase;
  669. type.id = sym->TypeIndex;
  670. types_print_type(&type, FALSE);
  671. dbg_printf(" ");
  672. symbol_print_local(sym, (DWORD_PTR)ctx, TRUE);
  673. dbg_printf("\n");
  674. return TRUE;
  675. }
  676. BOOL symbol_info_locals(void)
  677. {
  678. IMAGEHLP_STACK_FRAME ihsf;
  679. ADDRESS64 addr;
  680. stack_get_current_frame(&ihsf);
  681. addr.Mode = AddrModeFlat;
  682. addr.Offset = ihsf.InstructionOffset;
  683. print_address(&addr, FALSE);
  684. dbg_printf(": (%08lx)\n", (DWORD_PTR)ihsf.FrameOffset);
  685. SymEnumSymbols(dbg_curr_process->handle, 0, NULL, info_locals_cb, (void*)(DWORD_PTR)ihsf.FrameOffset);
  686. return TRUE;
  687. }
  688. static BOOL CALLBACK symbols_info_cb(PSYMBOL_INFO sym, ULONG size, PVOID ctx)
  689. {
  690. struct dbg_type type;
  691. IMAGEHLP_MODULE mi;
  692. mi.SizeOfStruct = sizeof(mi);
  693. if (!SymGetModuleInfo(dbg_curr_process->handle, sym->ModBase, &mi))
  694. mi.ModuleName[0] = '\0';
  695. else
  696. {
  697. size_t len = strlen(mi.ModuleName);
  698. if (len > 5 && !strcmp(mi.ModuleName + len - 5, "<elf>"))
  699. mi.ModuleName[len - 5] = '\0';
  700. }
  701. dbg_printf("%08lx: %s!%s", (ULONG_PTR)sym->Address, mi.ModuleName, sym->Name);
  702. type.id = sym->TypeIndex;
  703. type.module = sym->ModBase;
  704. if (sym->TypeIndex != dbg_itype_none && sym->TypeIndex != 0)
  705. {
  706. dbg_printf(" ");
  707. types_print_type(&type, FALSE);
  708. }
  709. dbg_printf("\n");
  710. return TRUE;
  711. }
  712. void symbol_info(const char* str)
  713. {
  714. char buffer[512];
  715. BOOL opt;
  716. if (strlen(str) + 3 >= sizeof(buffer))
  717. {
  718. dbg_printf("Symbol too long (%s)\n", str);
  719. return;
  720. }
  721. buffer[0] = '*';
  722. buffer[1] = '!';
  723. strcpy(&buffer[2], str);
  724. /* this is a wine specific options to return also ELF modules in the
  725. * enumeration
  726. */
  727. opt = SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, TRUE);
  728. SymEnumSymbols(dbg_curr_process->handle, 0, buffer, symbols_info_cb, NULL);
  729. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  730. }