ax.cc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /* Agent expression code for remote server.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "server.h"
  15. #include "ax.h"
  16. #include "gdbsupport/format.h"
  17. #include "tracepoint.h"
  18. #include "gdbsupport/rsp-low.h"
  19. static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  20. #ifdef IN_PROCESS_AGENT
  21. bool debug_agent = 0;
  22. #endif
  23. static void
  24. ax_vdebug (const char *fmt, ...)
  25. {
  26. char buf[1024];
  27. va_list ap;
  28. va_start (ap, fmt);
  29. vsprintf (buf, fmt, ap);
  30. #ifdef IN_PROCESS_AGENT
  31. fprintf (stderr, PROG "/ax: %s\n", buf);
  32. #else
  33. threads_debug_printf (PROG "/ax: %s", buf);
  34. #endif
  35. va_end (ap);
  36. }
  37. #define ax_debug(fmt, args...) \
  38. do { \
  39. if (debug_threads) \
  40. ax_vdebug ((fmt), ##args); \
  41. } while (0)
  42. /* This enum must exactly match what is documented in
  43. gdb/doc/agentexpr.texi, including all the numerical values. */
  44. enum gdb_agent_op
  45. {
  46. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
  47. gdb_agent_op_ ## NAME = VALUE,
  48. #include "gdbsupport/ax.def"
  49. #undef DEFOP
  50. gdb_agent_op_last
  51. };
  52. static const char * const gdb_agent_op_names [gdb_agent_op_last] =
  53. {
  54. "?undef?"
  55. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
  56. #include "gdbsupport/ax.def"
  57. #undef DEFOP
  58. };
  59. #ifndef IN_PROCESS_AGENT
  60. static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
  61. {
  62. 0
  63. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
  64. #include "gdbsupport/ax.def"
  65. #undef DEFOP
  66. };
  67. #endif
  68. /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
  69. static const char *
  70. gdb_agent_op_name (int op)
  71. {
  72. if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
  73. return "?undef?";
  74. return gdb_agent_op_names[op];
  75. }
  76. #ifndef IN_PROCESS_AGENT
  77. /* The packet form of an agent expression consists of an 'X', number
  78. of bytes in expression, a comma, and then the bytes. */
  79. struct agent_expr *
  80. gdb_parse_agent_expr (const char **actparm)
  81. {
  82. const char *act = *actparm;
  83. ULONGEST xlen;
  84. struct agent_expr *aexpr;
  85. ++act; /* skip the X */
  86. act = unpack_varlen_hex (act, &xlen);
  87. ++act; /* skip a comma */
  88. aexpr = XNEW (struct agent_expr);
  89. aexpr->length = xlen;
  90. aexpr->bytes = (unsigned char *) xmalloc (xlen);
  91. hex2bin (act, aexpr->bytes, xlen);
  92. *actparm = act + (xlen * 2);
  93. return aexpr;
  94. }
  95. void
  96. gdb_free_agent_expr (struct agent_expr *aexpr)
  97. {
  98. if (aexpr != NULL)
  99. {
  100. free (aexpr->bytes);
  101. free (aexpr);
  102. }
  103. }
  104. /* Convert the bytes of an agent expression back into hex digits, so
  105. they can be printed or uploaded. This allocates the buffer,
  106. callers should free when they are done with it. */
  107. char *
  108. gdb_unparse_agent_expr (struct agent_expr *aexpr)
  109. {
  110. char *rslt;
  111. rslt = (char *) xmalloc (2 * aexpr->length + 1);
  112. bin2hex (aexpr->bytes, rslt, aexpr->length);
  113. return rslt;
  114. }
  115. /* Bytecode compilation. */
  116. CORE_ADDR current_insn_ptr;
  117. int emit_error;
  118. static struct bytecode_address
  119. {
  120. int pc;
  121. CORE_ADDR address;
  122. int goto_pc;
  123. /* Offset and size of field to be modified in the goto block. */
  124. int from_offset, from_size;
  125. struct bytecode_address *next;
  126. } *bytecode_address_table;
  127. void
  128. emit_prologue (void)
  129. {
  130. target_emit_ops ()->emit_prologue ();
  131. }
  132. void
  133. emit_epilogue (void)
  134. {
  135. target_emit_ops ()->emit_epilogue ();
  136. }
  137. static void
  138. emit_add (void)
  139. {
  140. target_emit_ops ()->emit_add ();
  141. }
  142. static void
  143. emit_sub (void)
  144. {
  145. target_emit_ops ()->emit_sub ();
  146. }
  147. static void
  148. emit_mul (void)
  149. {
  150. target_emit_ops ()->emit_mul ();
  151. }
  152. static void
  153. emit_lsh (void)
  154. {
  155. target_emit_ops ()->emit_lsh ();
  156. }
  157. static void
  158. emit_rsh_signed (void)
  159. {
  160. target_emit_ops ()->emit_rsh_signed ();
  161. }
  162. static void
  163. emit_rsh_unsigned (void)
  164. {
  165. target_emit_ops ()->emit_rsh_unsigned ();
  166. }
  167. static void
  168. emit_ext (int arg)
  169. {
  170. target_emit_ops ()->emit_ext (arg);
  171. }
  172. static void
  173. emit_log_not (void)
  174. {
  175. target_emit_ops ()->emit_log_not ();
  176. }
  177. static void
  178. emit_bit_and (void)
  179. {
  180. target_emit_ops ()->emit_bit_and ();
  181. }
  182. static void
  183. emit_bit_or (void)
  184. {
  185. target_emit_ops ()->emit_bit_or ();
  186. }
  187. static void
  188. emit_bit_xor (void)
  189. {
  190. target_emit_ops ()->emit_bit_xor ();
  191. }
  192. static void
  193. emit_bit_not (void)
  194. {
  195. target_emit_ops ()->emit_bit_not ();
  196. }
  197. static void
  198. emit_equal (void)
  199. {
  200. target_emit_ops ()->emit_equal ();
  201. }
  202. static void
  203. emit_less_signed (void)
  204. {
  205. target_emit_ops ()->emit_less_signed ();
  206. }
  207. static void
  208. emit_less_unsigned (void)
  209. {
  210. target_emit_ops ()->emit_less_unsigned ();
  211. }
  212. static void
  213. emit_ref (int size)
  214. {
  215. target_emit_ops ()->emit_ref (size);
  216. }
  217. static void
  218. emit_if_goto (int *offset_p, int *size_p)
  219. {
  220. target_emit_ops ()->emit_if_goto (offset_p, size_p);
  221. }
  222. static void
  223. emit_goto (int *offset_p, int *size_p)
  224. {
  225. target_emit_ops ()->emit_goto (offset_p, size_p);
  226. }
  227. static void
  228. write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
  229. {
  230. target_emit_ops ()->write_goto_address (from, to, size);
  231. }
  232. static void
  233. emit_const (LONGEST num)
  234. {
  235. target_emit_ops ()->emit_const (num);
  236. }
  237. static void
  238. emit_reg (int reg)
  239. {
  240. target_emit_ops ()->emit_reg (reg);
  241. }
  242. static void
  243. emit_pop (void)
  244. {
  245. target_emit_ops ()->emit_pop ();
  246. }
  247. static void
  248. emit_stack_flush (void)
  249. {
  250. target_emit_ops ()->emit_stack_flush ();
  251. }
  252. static void
  253. emit_zero_ext (int arg)
  254. {
  255. target_emit_ops ()->emit_zero_ext (arg);
  256. }
  257. static void
  258. emit_swap (void)
  259. {
  260. target_emit_ops ()->emit_swap ();
  261. }
  262. static void
  263. emit_stack_adjust (int n)
  264. {
  265. target_emit_ops ()->emit_stack_adjust (n);
  266. }
  267. /* FN's prototype is `LONGEST(*fn)(int)'. */
  268. static void
  269. emit_int_call_1 (CORE_ADDR fn, int arg1)
  270. {
  271. target_emit_ops ()->emit_int_call_1 (fn, arg1);
  272. }
  273. /* FN's prototype is `void(*fn)(int,LONGEST)'. */
  274. static void
  275. emit_void_call_2 (CORE_ADDR fn, int arg1)
  276. {
  277. target_emit_ops ()->emit_void_call_2 (fn, arg1);
  278. }
  279. static void
  280. emit_eq_goto (int *offset_p, int *size_p)
  281. {
  282. target_emit_ops ()->emit_eq_goto (offset_p, size_p);
  283. }
  284. static void
  285. emit_ne_goto (int *offset_p, int *size_p)
  286. {
  287. target_emit_ops ()->emit_ne_goto (offset_p, size_p);
  288. }
  289. static void
  290. emit_lt_goto (int *offset_p, int *size_p)
  291. {
  292. target_emit_ops ()->emit_lt_goto (offset_p, size_p);
  293. }
  294. static void
  295. emit_ge_goto (int *offset_p, int *size_p)
  296. {
  297. target_emit_ops ()->emit_ge_goto (offset_p, size_p);
  298. }
  299. static void
  300. emit_gt_goto (int *offset_p, int *size_p)
  301. {
  302. target_emit_ops ()->emit_gt_goto (offset_p, size_p);
  303. }
  304. static void
  305. emit_le_goto (int *offset_p, int *size_p)
  306. {
  307. target_emit_ops ()->emit_le_goto (offset_p, size_p);
  308. }
  309. /* Scan an agent expression for any evidence that the given PC is the
  310. target of a jump bytecode in the expression. */
  311. static int
  312. is_goto_target (struct agent_expr *aexpr, int pc)
  313. {
  314. int i;
  315. unsigned char op;
  316. for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
  317. {
  318. op = aexpr->bytes[i];
  319. if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
  320. {
  321. int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
  322. if (target == pc)
  323. return 1;
  324. }
  325. }
  326. return 0;
  327. }
  328. /* Given an agent expression, turn it into native code. */
  329. enum eval_result_type
  330. compile_bytecodes (struct agent_expr *aexpr)
  331. {
  332. int pc = 0;
  333. int done = 0;
  334. unsigned char op, next_op;
  335. int arg;
  336. /* This is only used to build 64-bit value for constants. */
  337. ULONGEST top;
  338. struct bytecode_address *aentry, *aentry2;
  339. #define UNHANDLED \
  340. do \
  341. { \
  342. ax_debug ("Cannot compile op 0x%x\n", op); \
  343. return expr_eval_unhandled_opcode; \
  344. } while (0)
  345. if (aexpr->length == 0)
  346. {
  347. ax_debug ("empty agent expression\n");
  348. return expr_eval_empty_expression;
  349. }
  350. bytecode_address_table = NULL;
  351. while (!done)
  352. {
  353. op = aexpr->bytes[pc];
  354. ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
  355. /* Record the compiled-code address of the bytecode, for use by
  356. jump instructions. */
  357. aentry = XNEW (struct bytecode_address);
  358. aentry->pc = pc;
  359. aentry->address = current_insn_ptr;
  360. aentry->goto_pc = -1;
  361. aentry->from_offset = aentry->from_size = 0;
  362. aentry->next = bytecode_address_table;
  363. bytecode_address_table = aentry;
  364. ++pc;
  365. emit_error = 0;
  366. switch (op)
  367. {
  368. case gdb_agent_op_add:
  369. emit_add ();
  370. break;
  371. case gdb_agent_op_sub:
  372. emit_sub ();
  373. break;
  374. case gdb_agent_op_mul:
  375. emit_mul ();
  376. break;
  377. case gdb_agent_op_div_signed:
  378. UNHANDLED;
  379. break;
  380. case gdb_agent_op_div_unsigned:
  381. UNHANDLED;
  382. break;
  383. case gdb_agent_op_rem_signed:
  384. UNHANDLED;
  385. break;
  386. case gdb_agent_op_rem_unsigned:
  387. UNHANDLED;
  388. break;
  389. case gdb_agent_op_lsh:
  390. emit_lsh ();
  391. break;
  392. case gdb_agent_op_rsh_signed:
  393. emit_rsh_signed ();
  394. break;
  395. case gdb_agent_op_rsh_unsigned:
  396. emit_rsh_unsigned ();
  397. break;
  398. case gdb_agent_op_trace:
  399. UNHANDLED;
  400. break;
  401. case gdb_agent_op_trace_quick:
  402. UNHANDLED;
  403. break;
  404. case gdb_agent_op_log_not:
  405. emit_log_not ();
  406. break;
  407. case gdb_agent_op_bit_and:
  408. emit_bit_and ();
  409. break;
  410. case gdb_agent_op_bit_or:
  411. emit_bit_or ();
  412. break;
  413. case gdb_agent_op_bit_xor:
  414. emit_bit_xor ();
  415. break;
  416. case gdb_agent_op_bit_not:
  417. emit_bit_not ();
  418. break;
  419. case gdb_agent_op_equal:
  420. next_op = aexpr->bytes[pc];
  421. if (next_op == gdb_agent_op_if_goto
  422. && !is_goto_target (aexpr, pc)
  423. && target_emit_ops ()->emit_eq_goto)
  424. {
  425. ax_debug ("Combining equal & if_goto");
  426. pc += 1;
  427. aentry->pc = pc;
  428. arg = aexpr->bytes[pc++];
  429. arg = (arg << 8) + aexpr->bytes[pc++];
  430. aentry->goto_pc = arg;
  431. emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
  432. }
  433. else if (next_op == gdb_agent_op_log_not
  434. && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
  435. && !is_goto_target (aexpr, pc + 1)
  436. && target_emit_ops ()->emit_ne_goto)
  437. {
  438. ax_debug ("Combining equal & log_not & if_goto");
  439. pc += 2;
  440. aentry->pc = pc;
  441. arg = aexpr->bytes[pc++];
  442. arg = (arg << 8) + aexpr->bytes[pc++];
  443. aentry->goto_pc = arg;
  444. emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
  445. }
  446. else
  447. emit_equal ();
  448. break;
  449. case gdb_agent_op_less_signed:
  450. next_op = aexpr->bytes[pc];
  451. if (next_op == gdb_agent_op_if_goto
  452. && !is_goto_target (aexpr, pc))
  453. {
  454. ax_debug ("Combining less_signed & if_goto");
  455. pc += 1;
  456. aentry->pc = pc;
  457. arg = aexpr->bytes[pc++];
  458. arg = (arg << 8) + aexpr->bytes[pc++];
  459. aentry->goto_pc = arg;
  460. emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
  461. }
  462. else if (next_op == gdb_agent_op_log_not
  463. && !is_goto_target (aexpr, pc)
  464. && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
  465. && !is_goto_target (aexpr, pc + 1))
  466. {
  467. ax_debug ("Combining less_signed & log_not & if_goto");
  468. pc += 2;
  469. aentry->pc = pc;
  470. arg = aexpr->bytes[pc++];
  471. arg = (arg << 8) + aexpr->bytes[pc++];
  472. aentry->goto_pc = arg;
  473. emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
  474. }
  475. else
  476. emit_less_signed ();
  477. break;
  478. case gdb_agent_op_less_unsigned:
  479. emit_less_unsigned ();
  480. break;
  481. case gdb_agent_op_ext:
  482. arg = aexpr->bytes[pc++];
  483. if (arg < (sizeof (LONGEST) * 8))
  484. emit_ext (arg);
  485. break;
  486. case gdb_agent_op_ref8:
  487. emit_ref (1);
  488. break;
  489. case gdb_agent_op_ref16:
  490. emit_ref (2);
  491. break;
  492. case gdb_agent_op_ref32:
  493. emit_ref (4);
  494. break;
  495. case gdb_agent_op_ref64:
  496. emit_ref (8);
  497. break;
  498. case gdb_agent_op_if_goto:
  499. arg = aexpr->bytes[pc++];
  500. arg = (arg << 8) + aexpr->bytes[pc++];
  501. aentry->goto_pc = arg;
  502. emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
  503. break;
  504. case gdb_agent_op_goto:
  505. arg = aexpr->bytes[pc++];
  506. arg = (arg << 8) + aexpr->bytes[pc++];
  507. aentry->goto_pc = arg;
  508. emit_goto (&(aentry->from_offset), &(aentry->from_size));
  509. break;
  510. case gdb_agent_op_const8:
  511. emit_stack_flush ();
  512. top = aexpr->bytes[pc++];
  513. emit_const (top);
  514. break;
  515. case gdb_agent_op_const16:
  516. emit_stack_flush ();
  517. top = aexpr->bytes[pc++];
  518. top = (top << 8) + aexpr->bytes[pc++];
  519. emit_const (top);
  520. break;
  521. case gdb_agent_op_const32:
  522. emit_stack_flush ();
  523. top = aexpr->bytes[pc++];
  524. top = (top << 8) + aexpr->bytes[pc++];
  525. top = (top << 8) + aexpr->bytes[pc++];
  526. top = (top << 8) + aexpr->bytes[pc++];
  527. emit_const (top);
  528. break;
  529. case gdb_agent_op_const64:
  530. emit_stack_flush ();
  531. top = aexpr->bytes[pc++];
  532. top = (top << 8) + aexpr->bytes[pc++];
  533. top = (top << 8) + aexpr->bytes[pc++];
  534. top = (top << 8) + aexpr->bytes[pc++];
  535. top = (top << 8) + aexpr->bytes[pc++];
  536. top = (top << 8) + aexpr->bytes[pc++];
  537. top = (top << 8) + aexpr->bytes[pc++];
  538. top = (top << 8) + aexpr->bytes[pc++];
  539. emit_const (top);
  540. break;
  541. case gdb_agent_op_reg:
  542. emit_stack_flush ();
  543. arg = aexpr->bytes[pc++];
  544. arg = (arg << 8) + aexpr->bytes[pc++];
  545. emit_reg (arg);
  546. break;
  547. case gdb_agent_op_end:
  548. ax_debug ("At end of expression\n");
  549. /* Assume there is one stack element left, and that it is
  550. cached in "top" where emit_epilogue can get to it. */
  551. emit_stack_adjust (1);
  552. done = 1;
  553. break;
  554. case gdb_agent_op_dup:
  555. /* In our design, dup is equivalent to stack flushing. */
  556. emit_stack_flush ();
  557. break;
  558. case gdb_agent_op_pop:
  559. emit_pop ();
  560. break;
  561. case gdb_agent_op_zero_ext:
  562. arg = aexpr->bytes[pc++];
  563. if (arg < (sizeof (LONGEST) * 8))
  564. emit_zero_ext (arg);
  565. break;
  566. case gdb_agent_op_swap:
  567. next_op = aexpr->bytes[pc];
  568. /* Detect greater-than comparison sequences. */
  569. if (next_op == gdb_agent_op_less_signed
  570. && !is_goto_target (aexpr, pc)
  571. && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
  572. && !is_goto_target (aexpr, pc + 1))
  573. {
  574. ax_debug ("Combining swap & less_signed & if_goto");
  575. pc += 2;
  576. aentry->pc = pc;
  577. arg = aexpr->bytes[pc++];
  578. arg = (arg << 8) + aexpr->bytes[pc++];
  579. aentry->goto_pc = arg;
  580. emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
  581. }
  582. else if (next_op == gdb_agent_op_less_signed
  583. && !is_goto_target (aexpr, pc)
  584. && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
  585. && !is_goto_target (aexpr, pc + 1)
  586. && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
  587. && !is_goto_target (aexpr, pc + 2))
  588. {
  589. ax_debug ("Combining swap & less_signed & log_not & if_goto");
  590. pc += 3;
  591. aentry->pc = pc;
  592. arg = aexpr->bytes[pc++];
  593. arg = (arg << 8) + aexpr->bytes[pc++];
  594. aentry->goto_pc = arg;
  595. emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
  596. }
  597. else
  598. emit_swap ();
  599. break;
  600. case gdb_agent_op_getv:
  601. emit_stack_flush ();
  602. arg = aexpr->bytes[pc++];
  603. arg = (arg << 8) + aexpr->bytes[pc++];
  604. emit_int_call_1 (get_get_tsv_func_addr (),
  605. arg);
  606. break;
  607. case gdb_agent_op_setv:
  608. arg = aexpr->bytes[pc++];
  609. arg = (arg << 8) + aexpr->bytes[pc++];
  610. emit_void_call_2 (get_set_tsv_func_addr (),
  611. arg);
  612. break;
  613. case gdb_agent_op_tracev:
  614. UNHANDLED;
  615. break;
  616. /* GDB never (currently) generates any of these ops. */
  617. case gdb_agent_op_float:
  618. case gdb_agent_op_ref_float:
  619. case gdb_agent_op_ref_double:
  620. case gdb_agent_op_ref_long_double:
  621. case gdb_agent_op_l_to_d:
  622. case gdb_agent_op_d_to_l:
  623. case gdb_agent_op_trace16:
  624. UNHANDLED;
  625. break;
  626. default:
  627. ax_debug ("Agent expression op 0x%x not recognized\n", op);
  628. /* Don't struggle on, things will just get worse. */
  629. return expr_eval_unrecognized_opcode;
  630. }
  631. /* This catches errors that occur in target-specific code
  632. emission. */
  633. if (emit_error)
  634. {
  635. ax_debug ("Error %d while emitting code for %s\n",
  636. emit_error, gdb_agent_op_name (op));
  637. return expr_eval_unhandled_opcode;
  638. }
  639. ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
  640. }
  641. /* Now fill in real addresses as goto destinations. */
  642. for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
  643. {
  644. int written = 0;
  645. if (aentry->goto_pc < 0)
  646. continue;
  647. /* Find the location that we are going to, and call back into
  648. target-specific code to write the actual address or
  649. displacement. */
  650. for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
  651. {
  652. if (aentry2->pc == aentry->goto_pc)
  653. {
  654. ax_debug ("Want to jump from %s to %s\n",
  655. paddress (aentry->address),
  656. paddress (aentry2->address));
  657. write_goto_address (aentry->address + aentry->from_offset,
  658. aentry2->address, aentry->from_size);
  659. written = 1;
  660. break;
  661. }
  662. }
  663. /* Error out if we didn't find a destination. */
  664. if (!written)
  665. {
  666. ax_debug ("Destination of goto %d not found\n",
  667. aentry->goto_pc);
  668. return expr_eval_invalid_goto;
  669. }
  670. }
  671. return expr_eval_no_error;
  672. }
  673. #endif
  674. /* Make printf-type calls using arguments supplied from the host. We
  675. need to parse the format string ourselves, and call the formatting
  676. function with one argument at a time, partly because there is no
  677. safe portable way to construct a varargs call, and partly to serve
  678. as a security barrier against bad format strings that might get
  679. in. */
  680. static void
  681. ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
  682. int nargs, ULONGEST *args)
  683. {
  684. const char *f = format;
  685. int i;
  686. const char *current_substring;
  687. int nargs_wanted;
  688. ax_debug ("Printf of \"%s\" with %d args", format, nargs);
  689. format_pieces fpieces (&f);
  690. nargs_wanted = 0;
  691. for (auto &&piece : fpieces)
  692. if (piece.argclass != literal_piece)
  693. ++nargs_wanted;
  694. if (nargs != nargs_wanted)
  695. error (_("Wrong number of arguments for specified format-string"));
  696. i = 0;
  697. for (auto &&piece : fpieces)
  698. {
  699. current_substring = piece.string;
  700. ax_debug ("current substring is '%s', class is %d",
  701. current_substring, piece.argclass);
  702. switch (piece.argclass)
  703. {
  704. case string_arg:
  705. {
  706. gdb_byte *str;
  707. CORE_ADDR tem;
  708. int j;
  709. tem = args[i];
  710. if (tem == 0)
  711. {
  712. printf (current_substring, "(null)");
  713. break;
  714. }
  715. /* This is a %s argument. Find the length of the string. */
  716. for (j = 0;; j++)
  717. {
  718. gdb_byte c;
  719. read_inferior_memory (tem + j, &c, 1);
  720. if (c == 0)
  721. break;
  722. }
  723. /* Copy the string contents into a string inside GDB. */
  724. str = (gdb_byte *) alloca (j + 1);
  725. if (j != 0)
  726. read_inferior_memory (tem, str, j);
  727. str[j] = 0;
  728. printf (current_substring, (char *) str);
  729. }
  730. break;
  731. case long_long_arg:
  732. #if defined (PRINTF_HAS_LONG_LONG)
  733. {
  734. long long val = args[i];
  735. printf (current_substring, val);
  736. break;
  737. }
  738. #else
  739. error (_("long long not supported in agent printf"));
  740. #endif
  741. case int_arg:
  742. {
  743. int val = args[i];
  744. printf (current_substring, val);
  745. break;
  746. }
  747. case long_arg:
  748. {
  749. long val = args[i];
  750. printf (current_substring, val);
  751. break;
  752. }
  753. case size_t_arg:
  754. {
  755. size_t val = args[i];
  756. printf (current_substring, val);
  757. break;
  758. }
  759. case literal_piece:
  760. /* Print a portion of the format string that has no
  761. directives. Note that this will not include any
  762. ordinary %-specs, but it might include "%%". That is
  763. why we use printf_filtered and not puts_filtered here.
  764. Also, we pass a dummy argument because some platforms
  765. have modified GCC to include -Wformat-security by
  766. default, which will warn here if there is no
  767. argument. */
  768. printf (current_substring, 0);
  769. break;
  770. default:
  771. error (_("Format directive in '%s' not supported in agent printf"),
  772. current_substring);
  773. }
  774. /* Maybe advance to the next argument. */
  775. if (piece.argclass != literal_piece)
  776. ++i;
  777. }
  778. fflush (stdout);
  779. }
  780. /* The agent expression evaluator, as specified by the GDB docs. It
  781. returns 0 if everything went OK, and a nonzero error code
  782. otherwise. */
  783. enum eval_result_type
  784. gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
  785. struct agent_expr *aexpr,
  786. ULONGEST *rslt)
  787. {
  788. int pc = 0;
  789. #define STACK_MAX 100
  790. ULONGEST stack[STACK_MAX], top;
  791. int sp = 0;
  792. unsigned char op;
  793. int arg;
  794. /* This union is a convenient way to convert representations. For
  795. now, assume a standard architecture where the hardware integer
  796. types have 8, 16, 32, 64 bit types. A more robust solution would
  797. be to import stdint.h from gnulib. */
  798. union
  799. {
  800. union
  801. {
  802. unsigned char bytes[1];
  803. unsigned char val;
  804. } u8;
  805. union
  806. {
  807. unsigned char bytes[2];
  808. unsigned short val;
  809. } u16;
  810. union
  811. {
  812. unsigned char bytes[4];
  813. unsigned int val;
  814. } u32;
  815. union
  816. {
  817. unsigned char bytes[8];
  818. ULONGEST val;
  819. } u64;
  820. } cnv;
  821. if (aexpr->length == 0)
  822. {
  823. ax_debug ("empty agent expression");
  824. return expr_eval_empty_expression;
  825. }
  826. /* Cache the stack top in its own variable. Much of the time we can
  827. operate on this variable, rather than dinking with the stack. It
  828. needs to be copied to the stack when sp changes. */
  829. top = 0;
  830. while (1)
  831. {
  832. op = aexpr->bytes[pc++];
  833. ax_debug ("About to interpret byte 0x%x", op);
  834. switch (op)
  835. {
  836. case gdb_agent_op_add:
  837. top += stack[--sp];
  838. break;
  839. case gdb_agent_op_sub:
  840. top = stack[--sp] - top;
  841. break;
  842. case gdb_agent_op_mul:
  843. top *= stack[--sp];
  844. break;
  845. case gdb_agent_op_div_signed:
  846. if (top == 0)
  847. {
  848. ax_debug ("Attempted to divide by zero");
  849. return expr_eval_divide_by_zero;
  850. }
  851. top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
  852. break;
  853. case gdb_agent_op_div_unsigned:
  854. if (top == 0)
  855. {
  856. ax_debug ("Attempted to divide by zero");
  857. return expr_eval_divide_by_zero;
  858. }
  859. top = stack[--sp] / top;
  860. break;
  861. case gdb_agent_op_rem_signed:
  862. if (top == 0)
  863. {
  864. ax_debug ("Attempted to divide by zero");
  865. return expr_eval_divide_by_zero;
  866. }
  867. top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
  868. break;
  869. case gdb_agent_op_rem_unsigned:
  870. if (top == 0)
  871. {
  872. ax_debug ("Attempted to divide by zero");
  873. return expr_eval_divide_by_zero;
  874. }
  875. top = stack[--sp] % top;
  876. break;
  877. case gdb_agent_op_lsh:
  878. top = stack[--sp] << top;
  879. break;
  880. case gdb_agent_op_rsh_signed:
  881. top = ((LONGEST) stack[--sp]) >> top;
  882. break;
  883. case gdb_agent_op_rsh_unsigned:
  884. top = stack[--sp] >> top;
  885. break;
  886. case gdb_agent_op_trace:
  887. agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
  888. (ULONGEST) top);
  889. if (--sp >= 0)
  890. top = stack[sp];
  891. break;
  892. case gdb_agent_op_trace_quick:
  893. arg = aexpr->bytes[pc++];
  894. agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
  895. break;
  896. case gdb_agent_op_log_not:
  897. top = !top;
  898. break;
  899. case gdb_agent_op_bit_and:
  900. top &= stack[--sp];
  901. break;
  902. case gdb_agent_op_bit_or:
  903. top |= stack[--sp];
  904. break;
  905. case gdb_agent_op_bit_xor:
  906. top ^= stack[--sp];
  907. break;
  908. case gdb_agent_op_bit_not:
  909. top = ~top;
  910. break;
  911. case gdb_agent_op_equal:
  912. top = (stack[--sp] == top);
  913. break;
  914. case gdb_agent_op_less_signed:
  915. top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
  916. break;
  917. case gdb_agent_op_less_unsigned:
  918. top = (stack[--sp] < top);
  919. break;
  920. case gdb_agent_op_ext:
  921. arg = aexpr->bytes[pc++];
  922. if (arg < (sizeof (LONGEST) * 8))
  923. {
  924. LONGEST mask = 1 << (arg - 1);
  925. top &= ((LONGEST) 1 << arg) - 1;
  926. top = (top ^ mask) - mask;
  927. }
  928. break;
  929. case gdb_agent_op_ref8:
  930. agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
  931. top = cnv.u8.val;
  932. break;
  933. case gdb_agent_op_ref16:
  934. agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
  935. top = cnv.u16.val;
  936. break;
  937. case gdb_agent_op_ref32:
  938. agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
  939. top = cnv.u32.val;
  940. break;
  941. case gdb_agent_op_ref64:
  942. agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
  943. top = cnv.u64.val;
  944. break;
  945. case gdb_agent_op_if_goto:
  946. if (top)
  947. pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
  948. else
  949. pc += 2;
  950. if (--sp >= 0)
  951. top = stack[sp];
  952. break;
  953. case gdb_agent_op_goto:
  954. pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
  955. break;
  956. case gdb_agent_op_const8:
  957. /* Flush the cached stack top. */
  958. stack[sp++] = top;
  959. top = aexpr->bytes[pc++];
  960. break;
  961. case gdb_agent_op_const16:
  962. /* Flush the cached stack top. */
  963. stack[sp++] = top;
  964. top = aexpr->bytes[pc++];
  965. top = (top << 8) + aexpr->bytes[pc++];
  966. break;
  967. case gdb_agent_op_const32:
  968. /* Flush the cached stack top. */
  969. stack[sp++] = top;
  970. top = aexpr->bytes[pc++];
  971. top = (top << 8) + aexpr->bytes[pc++];
  972. top = (top << 8) + aexpr->bytes[pc++];
  973. top = (top << 8) + aexpr->bytes[pc++];
  974. break;
  975. case gdb_agent_op_const64:
  976. /* Flush the cached stack top. */
  977. stack[sp++] = top;
  978. top = aexpr->bytes[pc++];
  979. top = (top << 8) + aexpr->bytes[pc++];
  980. top = (top << 8) + aexpr->bytes[pc++];
  981. top = (top << 8) + aexpr->bytes[pc++];
  982. top = (top << 8) + aexpr->bytes[pc++];
  983. top = (top << 8) + aexpr->bytes[pc++];
  984. top = (top << 8) + aexpr->bytes[pc++];
  985. top = (top << 8) + aexpr->bytes[pc++];
  986. break;
  987. case gdb_agent_op_reg:
  988. /* Flush the cached stack top. */
  989. stack[sp++] = top;
  990. arg = aexpr->bytes[pc++];
  991. arg = (arg << 8) + aexpr->bytes[pc++];
  992. {
  993. int regnum = arg;
  994. struct regcache *regcache = ctx->regcache;
  995. switch (register_size (regcache->tdesc, regnum))
  996. {
  997. case 8:
  998. collect_register (regcache, regnum, cnv.u64.bytes);
  999. top = cnv.u64.val;
  1000. break;
  1001. case 4:
  1002. collect_register (regcache, regnum, cnv.u32.bytes);
  1003. top = cnv.u32.val;
  1004. break;
  1005. case 2:
  1006. collect_register (regcache, regnum, cnv.u16.bytes);
  1007. top = cnv.u16.val;
  1008. break;
  1009. case 1:
  1010. collect_register (regcache, regnum, cnv.u8.bytes);
  1011. top = cnv.u8.val;
  1012. break;
  1013. default:
  1014. internal_error (__FILE__, __LINE__,
  1015. "unhandled register size");
  1016. }
  1017. }
  1018. break;
  1019. case gdb_agent_op_end:
  1020. ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
  1021. sp, pulongest (top));
  1022. if (rslt)
  1023. {
  1024. if (sp <= 0)
  1025. {
  1026. /* This should be an error */
  1027. ax_debug ("Stack is empty, nothing to return");
  1028. return expr_eval_empty_stack;
  1029. }
  1030. *rslt = top;
  1031. }
  1032. return expr_eval_no_error;
  1033. case gdb_agent_op_dup:
  1034. stack[sp++] = top;
  1035. break;
  1036. case gdb_agent_op_pop:
  1037. if (--sp >= 0)
  1038. top = stack[sp];
  1039. break;
  1040. case gdb_agent_op_pick:
  1041. arg = aexpr->bytes[pc++];
  1042. stack[sp] = top;
  1043. top = stack[sp - arg];
  1044. ++sp;
  1045. break;
  1046. case gdb_agent_op_rot:
  1047. {
  1048. ULONGEST tem = stack[sp - 1];
  1049. stack[sp - 1] = stack[sp - 2];
  1050. stack[sp - 2] = top;
  1051. top = tem;
  1052. }
  1053. break;
  1054. case gdb_agent_op_zero_ext:
  1055. arg = aexpr->bytes[pc++];
  1056. if (arg < (sizeof (LONGEST) * 8))
  1057. top &= ((LONGEST) 1 << arg) - 1;
  1058. break;
  1059. case gdb_agent_op_swap:
  1060. /* Interchange top two stack elements, making sure top gets
  1061. copied back onto stack. */
  1062. stack[sp] = top;
  1063. top = stack[sp - 1];
  1064. stack[sp - 1] = stack[sp];
  1065. break;
  1066. case gdb_agent_op_getv:
  1067. /* Flush the cached stack top. */
  1068. stack[sp++] = top;
  1069. arg = aexpr->bytes[pc++];
  1070. arg = (arg << 8) + aexpr->bytes[pc++];
  1071. top = agent_get_trace_state_variable_value (arg);
  1072. break;
  1073. case gdb_agent_op_setv:
  1074. arg = aexpr->bytes[pc++];
  1075. arg = (arg << 8) + aexpr->bytes[pc++];
  1076. agent_set_trace_state_variable_value (arg, top);
  1077. /* Note that we leave the value on the stack, for the
  1078. benefit of later/enclosing expressions. */
  1079. break;
  1080. case gdb_agent_op_tracev:
  1081. arg = aexpr->bytes[pc++];
  1082. arg = (arg << 8) + aexpr->bytes[pc++];
  1083. agent_tsv_read (ctx, arg);
  1084. break;
  1085. case gdb_agent_op_tracenz:
  1086. agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
  1087. (ULONGEST) top);
  1088. if (--sp >= 0)
  1089. top = stack[sp];
  1090. break;
  1091. case gdb_agent_op_printf:
  1092. {
  1093. int nargs, slen, i;
  1094. CORE_ADDR fn = 0, chan = 0;
  1095. /* Can't have more args than the entire size of the stack. */
  1096. ULONGEST args[STACK_MAX];
  1097. char *format;
  1098. nargs = aexpr->bytes[pc++];
  1099. slen = aexpr->bytes[pc++];
  1100. slen = (slen << 8) + aexpr->bytes[pc++];
  1101. format = (char *) &(aexpr->bytes[pc]);
  1102. pc += slen;
  1103. /* Pop function and channel. */
  1104. fn = top;
  1105. if (--sp >= 0)
  1106. top = stack[sp];
  1107. chan = top;
  1108. if (--sp >= 0)
  1109. top = stack[sp];
  1110. /* Pop arguments into a dedicated array. */
  1111. for (i = 0; i < nargs; ++i)
  1112. {
  1113. args[i] = top;
  1114. if (--sp >= 0)
  1115. top = stack[sp];
  1116. }
  1117. /* A bad format string means something is very wrong; give
  1118. up immediately. */
  1119. if (format[slen - 1] != '\0')
  1120. error (_("Unterminated format string in printf bytecode"));
  1121. ax_printf (fn, chan, format, nargs, args);
  1122. }
  1123. break;
  1124. /* GDB never (currently) generates any of these ops. */
  1125. case gdb_agent_op_float:
  1126. case gdb_agent_op_ref_float:
  1127. case gdb_agent_op_ref_double:
  1128. case gdb_agent_op_ref_long_double:
  1129. case gdb_agent_op_l_to_d:
  1130. case gdb_agent_op_d_to_l:
  1131. case gdb_agent_op_trace16:
  1132. ax_debug ("Agent expression op 0x%x valid, but not handled",
  1133. op);
  1134. /* If ever GDB generates any of these, we don't have the
  1135. option of ignoring. */
  1136. return expr_eval_unhandled_opcode;
  1137. default:
  1138. ax_debug ("Agent expression op 0x%x not recognized", op);
  1139. /* Don't struggle on, things will just get worse. */
  1140. return expr_eval_unrecognized_opcode;
  1141. }
  1142. /* Check for stack badness. */
  1143. if (sp >= (STACK_MAX - 1))
  1144. {
  1145. ax_debug ("Expression stack overflow");
  1146. return expr_eval_stack_overflow;
  1147. }
  1148. if (sp < 0)
  1149. {
  1150. ax_debug ("Expression stack underflow");
  1151. return expr_eval_stack_underflow;
  1152. }
  1153. ax_debug ("Op %s -> sp=%d, top=0x%s",
  1154. gdb_agent_op_name (op), sp, phex_nz (top, 0));
  1155. }
  1156. }