symbol.c 26 KB

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