break.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Debugger break-points handling
  3. *
  4. * Copyright 1994 Martin von Loewis
  5. * Copyright 1995 Alexandre Julliard
  6. * Copyright 1999,2000 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 "debugger.h"
  23. #include "wine/debug.h"
  24. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  25. static BOOL is_xpoint_break(int bpnum)
  26. {
  27. int type = dbg_curr_process->bp[bpnum].xpoint_type;
  28. if (type == be_xpoint_break || type == be_xpoint_watch_exec) return TRUE;
  29. if (type == be_xpoint_watch_read || type == be_xpoint_watch_write) return FALSE;
  30. RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  31. return FALSE; /* never reached */
  32. }
  33. /***********************************************************************
  34. * break_set_xpoints
  35. *
  36. * Set or remove all the breakpoints & watchpoints
  37. */
  38. void break_set_xpoints(BOOL set)
  39. {
  40. static BOOL last; /* = 0 = FALSE */
  41. unsigned int i, ret, size;
  42. void* addr;
  43. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  44. if (set == last) return;
  45. last = set;
  46. for (i = 0; i < dbg_curr_process->next_bp; i++)
  47. {
  48. if (!bp[i].refcount || !bp[i].enabled) continue;
  49. if (is_xpoint_break(i))
  50. size = 0;
  51. else
  52. size = bp[i].w.len + 1;
  53. addr = memory_to_linear_addr(&bp[i].addr);
  54. if (set)
  55. ret = dbg_curr_process->be_cpu->insert_Xpoint(dbg_curr_process->handle,
  56. dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type,
  57. addr, &bp[i].info, size);
  58. else
  59. ret = dbg_curr_process->be_cpu->remove_Xpoint(dbg_curr_process->handle,
  60. dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type,
  61. addr, bp[i].info, size);
  62. if (!ret)
  63. {
  64. dbg_printf("Invalid address (");
  65. print_address(&bp[i].addr, FALSE);
  66. dbg_printf(") for breakpoint %d, disabling it\n", i);
  67. bp[i].enabled = FALSE;
  68. }
  69. }
  70. }
  71. /***********************************************************************
  72. * find_xpoint
  73. *
  74. * Find the breakpoint for a given address. Return the breakpoint
  75. * number or -1 if none.
  76. */
  77. static int find_xpoint(const ADDRESS64* addr, enum be_xpoint_type type)
  78. {
  79. int i;
  80. void* lin = memory_to_linear_addr(addr);
  81. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  82. for (i = 0; i < dbg_curr_process->next_bp; i++)
  83. {
  84. if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type == type &&
  85. memory_to_linear_addr(&bp[i].addr) == lin)
  86. return i;
  87. }
  88. return -1;
  89. }
  90. /***********************************************************************
  91. * init_xpoint
  92. *
  93. * Find an empty slot in BP table to add a new break/watch point
  94. */
  95. static int init_xpoint(int type, const ADDRESS64* addr)
  96. {
  97. int num;
  98. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  99. for (num = (dbg_curr_process->next_bp < MAX_BREAKPOINTS) ?
  100. dbg_curr_process->next_bp++ : 1;
  101. num < MAX_BREAKPOINTS; num++)
  102. {
  103. if (bp[num].refcount == 0)
  104. {
  105. bp[num].refcount = 1;
  106. bp[num].enabled = TRUE;
  107. bp[num].xpoint_type = type;
  108. bp[num].skipcount = 0;
  109. bp[num].addr = *addr;
  110. return num;
  111. }
  112. }
  113. dbg_printf("Too many bp. Please delete some.\n");
  114. return -1;
  115. }
  116. /***********************************************************************
  117. * get_watched_value
  118. *
  119. * Returns the value watched by watch point 'num'.
  120. */
  121. static BOOL get_watched_value(int num, DWORD64* val)
  122. {
  123. DWORD64 buf[1];
  124. if (!dbg_read_memory(memory_to_linear_addr(&dbg_curr_process->bp[num].addr),
  125. buf, dbg_curr_process->bp[num].w.len + 1))
  126. return FALSE;
  127. switch (dbg_curr_process->bp[num].w.len + 1)
  128. {
  129. case 8: *val = *(DWORD64*)buf; break;
  130. case 4: *val = *(DWORD*)buf; break;
  131. case 2: *val = *(WORD*)buf; break;
  132. case 1: *val = *(BYTE*)buf; break;
  133. default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  134. }
  135. return TRUE;
  136. }
  137. /***********************************************************************
  138. * break_add_break
  139. *
  140. * Add a breakpoint.
  141. */
  142. BOOL break_add_break(const ADDRESS64* addr, BOOL verbose, BOOL swbp)
  143. {
  144. int num;
  145. BYTE ch;
  146. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  147. int type = swbp ? be_xpoint_break : be_xpoint_watch_exec;
  148. if ((num = find_xpoint(addr, type)) >= 1)
  149. {
  150. bp[num].refcount++;
  151. dbg_printf("Breakpoint %d at ", num);
  152. print_address(&bp[num].addr, TRUE);
  153. dbg_printf(" (refcount=%d)\n", bp[num].refcount);
  154. return TRUE;
  155. }
  156. if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
  157. {
  158. if (verbose)
  159. {
  160. dbg_printf("Invalid address ");
  161. print_bare_address(addr);
  162. dbg_printf(", can't set breakpoint\n");
  163. }
  164. return FALSE;
  165. }
  166. if ((num = init_xpoint(type, addr)) == -1)
  167. return FALSE;
  168. dbg_printf("Breakpoint %d at ", num);
  169. print_address(&bp[num].addr, TRUE);
  170. dbg_printf("\n");
  171. return TRUE;
  172. }
  173. /***********************************************************************
  174. * break_add_break_from_lvalue
  175. *
  176. * Add a breakpoint.
  177. */
  178. BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
  179. {
  180. ADDRESS64 addr;
  181. types_extract_as_address(lvalue, &addr);
  182. if (!break_add_break(&addr, TRUE, swbp))
  183. {
  184. if (!DBG_IVAR(CanDeferOnBPByAddr))
  185. {
  186. dbg_printf("Invalid address, can't set breakpoint\n"
  187. "You can turn on deferring bp by address by setting $CanDeferOnBPByAddr to 1\n");
  188. return FALSE;
  189. }
  190. dbg_printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
  191. dbg_curr_process->delayed_bp =
  192. dbg_heap_realloc(dbg_curr_process->delayed_bp,
  193. sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
  194. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol = FALSE;
  195. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp = swbp;
  196. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.addr = addr;
  197. return TRUE;
  198. }
  199. return FALSE;
  200. }
  201. /***********************************************************************
  202. * break_add_break_from_id
  203. *
  204. * Add a breakpoint from a function name (and eventually a line #)
  205. */
  206. void break_add_break_from_id(const char *name, int lineno, BOOL swbp)
  207. {
  208. struct dbg_lvalue lvalue;
  209. int i;
  210. switch (symbol_get_lvalue(name, lineno, &lvalue, TRUE))
  211. {
  212. case sglv_found:
  213. break_add_break(&lvalue.addr, TRUE, swbp);
  214. return;
  215. case sglv_unknown:
  216. break;
  217. case sglv_aborted: /* user aborted symbol lookup */
  218. return;
  219. }
  220. dbg_printf("Unable to add breakpoint, will check again when a new DLL is loaded\n");
  221. for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
  222. {
  223. if (dbg_curr_process->delayed_bp[i].is_symbol &&
  224. !strcmp(name, dbg_curr_process->delayed_bp[i].u.symbol.name) &&
  225. lineno == dbg_curr_process->delayed_bp[i].u.symbol.lineno)
  226. return;
  227. }
  228. dbg_curr_process->delayed_bp = dbg_heap_realloc(dbg_curr_process->delayed_bp,
  229. sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
  230. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol = TRUE;
  231. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp = swbp;
  232. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1), name);
  233. dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.lineno = lineno;
  234. }
  235. struct cb_break_lineno
  236. {
  237. const char* filename;
  238. int lineno;
  239. ADDRESS64 addr;
  240. };
  241. static BOOL CALLBACK line_cb(SRCCODEINFO* sci, void* user)
  242. {
  243. struct cb_break_lineno* bkln = user;
  244. if (bkln->lineno == sci->LineNumber)
  245. {
  246. bkln->addr.Mode = AddrModeFlat;
  247. bkln->addr.Offset = sci->Address;
  248. return FALSE;
  249. }
  250. return TRUE;
  251. }
  252. static BOOL CALLBACK mcb(PCWSTR module, DWORD64 base, void* user)
  253. {
  254. struct cb_break_lineno* bkln = user;
  255. SymEnumLines(dbg_curr_process->handle, base, NULL, bkln->filename, line_cb, bkln);
  256. /* continue module enum if no addr found
  257. * FIXME: we don't report when several addresses match the same filename/lineno pair
  258. */
  259. return !bkln->addr.Offset;
  260. }
  261. /***********************************************************************
  262. * break_add_break_from_lineno
  263. *
  264. * Add a breakpoint from a line number in current file
  265. */
  266. void break_add_break_from_lineno(const char *filename, int lineno, BOOL swbp)
  267. {
  268. struct cb_break_lineno bkln;
  269. bkln.addr.Offset = 0;
  270. bkln.lineno = lineno;
  271. if (!filename)
  272. {
  273. DWORD disp;
  274. ADDRESS64 curr;
  275. IMAGEHLP_LINE64 il;
  276. DWORD_PTR linear;
  277. memory_get_current_pc(&curr);
  278. linear = (DWORD_PTR)memory_to_linear_addr(&curr);
  279. il.SizeOfStruct = sizeof(il);
  280. if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il))
  281. {
  282. dbg_printf("Unable to add breakpoint (unknown address %Ix)\n", linear);
  283. return;
  284. }
  285. filename = il.FileName;
  286. SymEnumLines(dbg_curr_process->handle, linear, NULL, filename, line_cb, &bkln);
  287. }
  288. else
  289. {
  290. /* we have to enumerate across modules */
  291. bkln.filename = filename;
  292. SymEnumerateModulesW64(dbg_curr_process->handle, mcb, &bkln);
  293. }
  294. if (bkln.addr.Offset)
  295. break_add_break(&bkln.addr, TRUE, swbp);
  296. else
  297. dbg_printf("Unknown line number\n"
  298. "(either out of file, or no code at given line number)\n");
  299. }
  300. /***********************************************************************
  301. * break_check_delayed_bp
  302. *
  303. * Check is a registered delayed BP is now available.
  304. */
  305. void break_check_delayed_bp(void)
  306. {
  307. struct dbg_lvalue lvalue;
  308. int i;
  309. struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
  310. char hexbuf[MAX_OFFSET_TO_STR_LEN];
  311. for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
  312. {
  313. if (dbp[i].is_symbol)
  314. {
  315. if (symbol_get_lvalue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno,
  316. &lvalue, TRUE) != sglv_found)
  317. continue;
  318. if (!lvalue.in_debuggee) continue;
  319. }
  320. else
  321. lvalue.addr = dbp[i].u.addr;
  322. WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
  323. if (!dbp[i].is_symbol)
  324. WINE_TRACE("\t%04x:%s\n",
  325. dbp[i].u.addr.Segment,
  326. memory_offset_to_string(hexbuf, dbp[i].u.addr.Offset, 0));
  327. else
  328. WINE_TRACE("\t'%s' @ %d\n",
  329. dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
  330. if (break_add_break(&lvalue.addr, FALSE, dbp[i].software_bp))
  331. memmove(&dbp[i], &dbp[i+1], (--dbg_curr_process->num_delayed_bp - i) * sizeof(*dbp));
  332. }
  333. }
  334. /***********************************************************************
  335. * break_add_watch
  336. *
  337. * Add a watchpoint.
  338. */
  339. static void break_add_watch(const struct dbg_lvalue* lvalue, BOOL is_write)
  340. {
  341. int num;
  342. DWORD64 l = 4;
  343. if (!lvalue->in_debuggee)
  344. {
  345. dbg_printf("Cannot set a watch point on register or register-based variable\n");
  346. return;
  347. }
  348. num = init_xpoint((is_write) ? be_xpoint_watch_write : be_xpoint_watch_read,
  349. &lvalue->addr);
  350. if (num == -1) return;
  351. if (lvalue->type.id != dbg_itype_none)
  352. {
  353. if (types_get_info(&lvalue->type, TI_GET_LENGTH, &l))
  354. {
  355. switch (l)
  356. {
  357. case 4: case 2: case 1: break;
  358. default:
  359. dbg_printf("Unsupported length (%I64x) for watch-points, defaulting to 4\n", l);
  360. break;
  361. }
  362. }
  363. else dbg_printf("Cannot get watch size, defaulting to 4\n");
  364. }
  365. dbg_curr_process->bp[num].w.len = (DWORD)l - 1;
  366. if (!get_watched_value(num, &dbg_curr_process->bp[num].w.oldval))
  367. {
  368. dbg_printf("Bad address. Watchpoint not set\n");
  369. dbg_curr_process->bp[num].refcount = 0;
  370. return;
  371. }
  372. dbg_printf("Watchpoint %d at ", num);
  373. print_address(&dbg_curr_process->bp[num].addr, TRUE);
  374. dbg_printf("\n");
  375. }
  376. /******************************************************************
  377. * break_add_watch_from_lvalue
  378. *
  379. * Adds a watch point from an address (stored in a lvalue)
  380. */
  381. void break_add_watch_from_lvalue(const struct dbg_lvalue* lvalue,BOOL is_write)
  382. {
  383. struct dbg_lvalue lval;
  384. types_extract_as_address(lvalue, &lval.addr);
  385. lval.type.id = dbg_itype_none;
  386. break_add_watch(&lval, is_write);
  387. }
  388. /***********************************************************************
  389. * break_add_watch_from_id
  390. *
  391. * Add a watchpoint from a symbol name
  392. */
  393. void break_add_watch_from_id(const char *name, BOOL is_write)
  394. {
  395. struct dbg_lvalue lvalue;
  396. switch (symbol_get_lvalue(name, -1, &lvalue, TRUE))
  397. {
  398. case sglv_found:
  399. break_add_watch(&lvalue, is_write);
  400. break;
  401. case sglv_unknown:
  402. dbg_printf("Unable to add watchpoint\n");
  403. break;
  404. case sglv_aborted: /* user aborted symbol lookup */
  405. break;
  406. }
  407. }
  408. /***********************************************************************
  409. * break_delete_xpoint
  410. *
  411. * Delete a breakpoint.
  412. */
  413. void break_delete_xpoint(int num)
  414. {
  415. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  416. if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
  417. bp[num].refcount == 0)
  418. {
  419. dbg_printf("Invalid breakpoint number %d\n", num);
  420. return;
  421. }
  422. if (--bp[num].refcount > 0)
  423. return;
  424. if (bp[num].condition != NULL)
  425. {
  426. expr_free(bp[num].condition);
  427. bp[num].condition = NULL;
  428. }
  429. bp[num].enabled = FALSE;
  430. bp[num].refcount = 0;
  431. bp[num].skipcount = 0;
  432. }
  433. /******************************************************************
  434. * break_delete_xpoints_from_module
  435. *
  436. * Remove all Xpoints from module which base is 'base'
  437. */
  438. void break_delete_xpoints_from_module(DWORD64 base)
  439. {
  440. IMAGEHLP_MODULE64 im, im_elf;
  441. int i;
  442. DWORD_PTR linear;
  443. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  444. /* FIXME: should do it also on the ELF sibling if any */
  445. im.SizeOfStruct = sizeof(im);
  446. im_elf.SizeOfStruct = sizeof(im_elf);
  447. if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return;
  448. /* try to get in fact the underlying ELF module (if any) */
  449. if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
  450. im_elf.BaseOfImage <= im.BaseOfImage &&
  451. im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize)
  452. im = im_elf;
  453. for (i = 0; i < dbg_curr_process->next_bp; i++)
  454. {
  455. if (bp[i].refcount && bp[i].enabled)
  456. {
  457. linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr);
  458. if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
  459. {
  460. break_delete_xpoint(i);
  461. }
  462. }
  463. }
  464. }
  465. /***********************************************************************
  466. * break_enable_xpoint
  467. *
  468. * Enable or disable a break point.
  469. */
  470. void break_enable_xpoint(int num, BOOL enable)
  471. {
  472. if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
  473. dbg_curr_process->bp[num].refcount == 0)
  474. {
  475. dbg_printf("Invalid breakpoint number %d\n", num);
  476. return;
  477. }
  478. dbg_curr_process->bp[num].enabled = enable != 0;
  479. dbg_curr_process->bp[num].skipcount = 0;
  480. }
  481. /***********************************************************************
  482. * find_triggered_watch
  483. *
  484. * Lookup the watchpoints to see if one has been triggered
  485. * Return >= (watch point index) if one is found
  486. * Return -1 if none found
  487. *
  488. * Unfortunately, Linux used to *NOT* (A REAL PITA) report with ptrace
  489. * the DR6 register value, so we have to look with our own need the
  490. * cause of the TRAP.
  491. * -EP
  492. */
  493. static int find_triggered_watch(void)
  494. {
  495. int found = -1;
  496. int i;
  497. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  498. /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
  499. * 2.2.x). This should be fixed in >= 2.2.16
  500. */
  501. for (i = 0; i < dbg_curr_process->next_bp; i++)
  502. {
  503. DWORD64 val = 0;
  504. if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
  505. (dbg_curr_process->be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
  506. {
  507. dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
  508. if (get_watched_value(i, &val))
  509. {
  510. bp[i].w.oldval = val;
  511. return i;
  512. }
  513. }
  514. }
  515. /* Method 1 failed, trying method 2 */
  516. /* Method 2 => check if value has changed among registered watchpoints
  517. * this really sucks, but this is how gdb 4.18 works on my linux box
  518. * -EP
  519. */
  520. for (i = 0; i < dbg_curr_process->next_bp; i++)
  521. {
  522. DWORD64 val = 0;
  523. if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
  524. get_watched_value(i, &val))
  525. {
  526. if (val != bp[i].w.oldval)
  527. {
  528. dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
  529. bp[i].w.oldval = val;
  530. found = i;
  531. /* cannot break, because two watch points may have been triggered on
  532. * the same access
  533. * only one will be reported to the user (FIXME ?)
  534. */
  535. }
  536. }
  537. }
  538. return found;
  539. }
  540. /***********************************************************************
  541. * break_info
  542. *
  543. * Display break & watch points information.
  544. */
  545. void break_info(void)
  546. {
  547. int i;
  548. int nbp = 0, nwp = 0;
  549. struct dbg_delayed_bp* dbp = dbg_curr_process->delayed_bp;
  550. struct dbg_breakpoint* bp = dbg_curr_process->bp;
  551. for (i = 1; i < dbg_curr_process->next_bp; i++)
  552. {
  553. if (bp[i].refcount)
  554. {
  555. if (is_xpoint_break(i)) nbp++; else nwp++;
  556. }
  557. }
  558. if (nbp)
  559. {
  560. dbg_printf("Breakpoints:\n");
  561. for (i = 1; i < dbg_curr_process->next_bp; i++)
  562. {
  563. if (!bp[i].refcount || !is_xpoint_break(i))
  564. continue;
  565. dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
  566. print_address(&bp[i].addr, TRUE);
  567. dbg_printf(" (%u)%s\n", bp[i].refcount,
  568. bp[i].xpoint_type == be_xpoint_watch_exec ? " (hardware assisted)" : "");
  569. if (bp[i].condition != NULL)
  570. {
  571. dbg_printf("\t\tstop when ");
  572. expr_print(bp[i].condition);
  573. dbg_printf("\n");
  574. }
  575. }
  576. }
  577. else dbg_printf("No breakpoints\n");
  578. if (nwp)
  579. {
  580. dbg_printf("Watchpoints:\n");
  581. for (i = 1; i < dbg_curr_process->next_bp; i++)
  582. {
  583. if (!bp[i].refcount || is_xpoint_break(i))
  584. continue;
  585. dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
  586. print_address(&bp[i].addr, TRUE);
  587. dbg_printf(" on %d byte%s (%c)\n",
  588. bp[i].w.len + 1, bp[i].w.len > 0 ? "s" : "",
  589. bp[i].xpoint_type == be_xpoint_watch_write ? 'W' : 'R');
  590. if (bp[i].condition != NULL)
  591. {
  592. dbg_printf("\t\tstop when ");
  593. expr_print(bp[i].condition);
  594. dbg_printf("\n");
  595. }
  596. }
  597. }
  598. else dbg_printf("No watchpoints\n");
  599. if (dbg_curr_process->num_delayed_bp)
  600. {
  601. dbg_printf("Delayed breakpoints:\n");
  602. for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
  603. {
  604. if (dbp[i].is_symbol)
  605. {
  606. dbg_printf("%d: %s", i, dbp[i].u.symbol.name);
  607. if (dbp[i].u.symbol.lineno != -1)
  608. dbg_printf(" at line %u", dbp[i].u.symbol.lineno);
  609. }
  610. else
  611. {
  612. dbg_printf("%d: ", i);
  613. print_address(&dbp[i].u.addr, FALSE);
  614. }
  615. dbg_printf("\n");
  616. }
  617. }
  618. }
  619. /***********************************************************************
  620. * should_stop
  621. *
  622. * Check whether or not the condition (bp / skipcount) of a break/watch
  623. * point are met.
  624. */
  625. static BOOL should_stop(int bpnum)
  626. {
  627. struct dbg_breakpoint* bp = &dbg_curr_process->bp[bpnum];
  628. if (bp->condition != NULL)
  629. {
  630. struct dbg_lvalue lvalue = expr_eval(bp->condition);
  631. if (lvalue.type.id == dbg_itype_none)
  632. {
  633. /*
  634. * Something wrong - unable to evaluate this expression.
  635. */
  636. dbg_printf("Unable to evaluate expression ");
  637. expr_print(bp->condition);
  638. dbg_printf("\nTurning off condition\n");
  639. break_add_condition(bpnum, NULL);
  640. }
  641. else if (!types_extract_as_integer(&lvalue))
  642. {
  643. return FALSE;
  644. }
  645. }
  646. if (bp->skipcount > 0) bp->skipcount--;
  647. return bp->skipcount == 0;
  648. }
  649. /***********************************************************************
  650. * break_should_continue
  651. *
  652. * Determine if we should continue execution after a SIGTRAP signal when
  653. * executing in the given mode.
  654. */
  655. BOOL break_should_continue(ADDRESS64* addr, DWORD code)
  656. {
  657. enum dbg_exec_mode mode = dbg_curr_thread->exec_mode;
  658. if (dbg_curr_thread->stopped_xpoint > 0)
  659. {
  660. if (!should_stop(dbg_curr_thread->stopped_xpoint)) return TRUE;
  661. switch (dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].xpoint_type)
  662. {
  663. case be_xpoint_break:
  664. case be_xpoint_watch_exec:
  665. dbg_printf("Stopped on breakpoint %d at ", dbg_curr_thread->stopped_xpoint);
  666. print_address(&dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].addr, TRUE);
  667. dbg_printf("\n");
  668. break;
  669. case be_xpoint_watch_read:
  670. case be_xpoint_watch_write:
  671. dbg_printf("Stopped on watchpoint %d at ", dbg_curr_thread->stopped_xpoint);
  672. print_address(addr, TRUE);
  673. dbg_printf(" new value %I64x\n",
  674. dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].w.oldval);
  675. }
  676. return FALSE;
  677. }
  678. /*
  679. * If our mode indicates that we are stepping line numbers,
  680. * get the current function, and figure out if we are exactly
  681. * on a line number or not.
  682. */
  683. if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
  684. {
  685. if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
  686. dbg_curr_thread->exec_count--;
  687. }
  688. else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
  689. dbg_curr_thread->exec_count--;
  690. if (dbg_curr_thread->exec_count > 0 || mode == dbg_exec_finish)
  691. {
  692. /*
  693. * We still need to execute more instructions.
  694. */
  695. return TRUE;
  696. }
  697. /* no breakpoint, continue if in continuous mode */
  698. return mode == dbg_exec_cont || mode == dbg_exec_finish;
  699. }
  700. /***********************************************************************
  701. * break_adjust_pc
  702. *
  703. * Adjust PC to the address where the trap (if any) actually occurred
  704. * Also sets dbg_curr_thread->stopped_xpoint
  705. */
  706. void break_adjust_pc(ADDRESS64* addr, DWORD code, BOOL first_chance, BOOL* is_break)
  707. {
  708. /* break / watch points are handled on first chance */
  709. if ( !first_chance )
  710. {
  711. *is_break = TRUE;
  712. dbg_curr_thread->stopped_xpoint = -1;
  713. return;
  714. }
  715. *is_break = FALSE;
  716. /* If not single-stepping, back up to the break instruction */
  717. if (code == EXCEPTION_BREAKPOINT)
  718. addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, TRUE);
  719. dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_break);
  720. dbg_curr_process->bp[0].enabled = FALSE; /* disable the step-over breakpoint */
  721. if (dbg_curr_thread->stopped_xpoint > 0) return;
  722. if (dbg_curr_thread->stopped_xpoint < 0)
  723. {
  724. dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_watch_exec);
  725. if (dbg_curr_thread->stopped_xpoint < 0)
  726. dbg_curr_thread->stopped_xpoint = find_triggered_watch();
  727. if (dbg_curr_thread->stopped_xpoint > 0)
  728. {
  729. /* If not single-stepping, do not back up over the break instruction */
  730. if (code == EXCEPTION_BREAKPOINT)
  731. addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
  732. return;
  733. }
  734. }
  735. /* If there's no breakpoint and we are not single-stepping, then
  736. * either we must have encountered a break insn in the Windows program
  737. * or someone is trying to stop us
  738. */
  739. if (dbg_curr_thread->stopped_xpoint == -1 && code == EXCEPTION_BREAKPOINT)
  740. {
  741. *is_break = TRUE;
  742. addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
  743. }
  744. }
  745. /***********************************************************************
  746. * break_suspend_execution
  747. *
  748. * Remove all bp before entering the debug loop
  749. */
  750. void break_suspend_execution(void)
  751. {
  752. break_set_xpoints(FALSE);
  753. dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
  754. }
  755. /***********************************************************************
  756. * break_restart_execution
  757. *
  758. * Set the bp to the correct state to restart execution
  759. * in the given mode.
  760. */
  761. void break_restart_execution(int count)
  762. {
  763. ADDRESS64 addr;
  764. enum dbg_line_status status;
  765. enum dbg_exec_mode mode, ret_mode;
  766. ADDRESS64 callee;
  767. void* linear;
  768. memory_get_current_pc(&addr);
  769. linear = memory_to_linear_addr(&addr);
  770. /*
  771. * This is the mode we will be running in after we finish. We would like
  772. * to be able to modify this in certain cases.
  773. */
  774. ret_mode = mode = dbg_curr_thread->exec_mode;
  775. /* we've stopped on a xpoint (other than step over) */
  776. if (dbg_curr_thread->stopped_xpoint > 0)
  777. {
  778. /*
  779. * If we have set a new value, then save it in the BP number.
  780. */
  781. if (count != 0 && mode == dbg_exec_cont)
  782. {
  783. dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count;
  784. }
  785. /* If we've stopped on a breakpoint, single step over it (, then run) */
  786. if (is_xpoint_break(dbg_curr_thread->stopped_xpoint))
  787. mode = dbg_exec_step_into_insn;
  788. }
  789. else if (mode == dbg_exec_cont && count > 1)
  790. {
  791. dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
  792. }
  793. if (mode == dbg_exec_finish && dbg_curr_process->be_cpu->is_function_return(linear))
  794. {
  795. mode = ret_mode = dbg_exec_step_into_insn;
  796. }
  797. /*
  798. * See if the function we are stepping into has debug info
  799. * and line numbers. If not, then we step over it instead.
  800. * FIXME - we need to check for things like thunks or trampolines,
  801. * as the actual function may in fact have debug info.
  802. */
  803. if (dbg_curr_process->be_cpu->is_function_call(linear, &callee))
  804. {
  805. status = symbol_get_function_line_status(&callee);
  806. #if 0
  807. /* FIXME: we need to get the thunk type */
  808. /*
  809. * Anytime we have a trampoline, step over it.
  810. */
  811. if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER)
  812. && status == dbg_in_a_thunk)
  813. {
  814. WINE_WARN("Not stepping into trampoline at %p (no lines)\n",
  815. memory_to_linear_addr(&callee));
  816. mode = EXEC_STEP_OVER_TRAMPOLINE;
  817. }
  818. #endif
  819. if (mode == dbg_exec_step_into_line && status == dbg_no_line_info)
  820. {
  821. WINE_WARN("Not stepping into function at %p (no lines)\n",
  822. memory_to_linear_addr(&callee));
  823. mode = dbg_exec_step_over_line;
  824. }
  825. }
  826. if (mode == dbg_exec_step_into_line &&
  827. symbol_get_function_line_status(&addr) == dbg_no_line_info)
  828. {
  829. dbg_printf("Single stepping until exit from function,\n"
  830. "which has no line number information.\n");
  831. ret_mode = mode = dbg_exec_finish;
  832. }
  833. switch (mode)
  834. {
  835. case dbg_exec_cont: /* Continuous execution */
  836. dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
  837. break_set_xpoints(TRUE);
  838. break;
  839. #if 0
  840. case EXEC_STEP_OVER_TRAMPOLINE:
  841. /*
  842. * This is the means by which we step over our conversion stubs
  843. * in callfrom*.s and callto*.s. We dig the appropriate address
  844. * off the stack, and we set the breakpoint there instead of the
  845. * address just after the call.
  846. */
  847. be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
  848. be_cpu_addr_stack, &addr);
  849. /* FIXME: we assume stack grows as on an i386 */
  850. addr.Offset += 2 * sizeof(unsigned int);
  851. dbg_read_memory(memory_to_linear_addr(&addr),
  852. &addr.Offset, sizeof(addr.Offset));
  853. dbg_curr_process->bp[0].addr = addr;
  854. dbg_curr_process->bp[0].enabled = TRUE;
  855. dbg_curr_process->bp[0].refcount = 1;
  856. dbg_curr_process->bp[0].skipcount = 0;
  857. dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
  858. dbg_curr_process->bp[0].condition = NULL;
  859. be_cpu->single_step(&dbg_context, FALSE);
  860. break_set_xpoints(TRUE);
  861. break;
  862. #endif
  863. case dbg_exec_finish:
  864. case dbg_exec_step_over_insn: /* Stepping over a call */
  865. case dbg_exec_step_over_line: /* Stepping over a call */
  866. if (dbg_curr_process->be_cpu->is_step_over_insn(linear))
  867. {
  868. dbg_curr_process->be_cpu->disasm_one_insn(&addr, FALSE);
  869. dbg_curr_process->bp[0].addr = addr;
  870. dbg_curr_process->bp[0].enabled = TRUE;
  871. dbg_curr_process->bp[0].refcount = 1;
  872. dbg_curr_process->bp[0].skipcount = 0;
  873. dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
  874. dbg_curr_process->bp[0].condition = NULL;
  875. dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
  876. break_set_xpoints(TRUE);
  877. break;
  878. }
  879. /* else fall through to single-stepping */
  880. case dbg_exec_step_into_line: /* Single-stepping a line */
  881. case dbg_exec_step_into_insn: /* Single-stepping an instruction */
  882. dbg_curr_process->be_cpu->single_step(&dbg_context, TRUE);
  883. break;
  884. default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  885. }
  886. dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0];
  887. dbg_curr_thread->exec_mode = ret_mode;
  888. }
  889. BOOL break_add_condition(int num, struct expr* exp)
  890. {
  891. if (num <= 0 || num >= dbg_curr_process->next_bp ||
  892. !dbg_curr_process->bp[num].refcount)
  893. {
  894. dbg_printf("Invalid breakpoint number %d\n", num);
  895. return FALSE;
  896. }
  897. if (dbg_curr_process->bp[num].condition != NULL)
  898. {
  899. expr_free(dbg_curr_process->bp[num].condition);
  900. dbg_curr_process->bp[num].condition = NULL;
  901. }
  902. if (exp != NULL) dbg_curr_process->bp[num].condition = expr_clone(exp, NULL);
  903. return TRUE;
  904. }