gdb/gdbserver/ax.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Agent expression code for remote server.
  2.    Copyright (C) 2009-2015 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 "format.h"
  17. #include "tracepoint.h"
  18. #include "rsp-low.h"

  19. static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);

  20. #ifdef IN_PROCESS_AGENT
  21. int 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.   fprintf (stderr, PROG "/ax: %s\n", buf);
  31.   va_end (ap);
  32. }

  33. #define ax_debug_1(level, fmt, args...)        \
  34.   do {                                                \
  35.     if (level <= debug_threads)                        \
  36.       ax_vdebug ((fmt), ##args);                \
  37.   } while (0)

  38. #define ax_debug(FMT, args...)                \
  39.   ax_debug_1 (1, FMT, ##args)

  40. /* This enum must exactly match what is documented in
  41.    gdb/doc/agentexpr.texi, including all the numerical values.  */

  42. enum gdb_agent_op
  43.   {
  44. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  \
  45.     gdb_agent_op_ ## NAME = VALUE,
  46. #include "ax.def"
  47. #undef DEFOP
  48.     gdb_agent_op_last
  49.   };

  50. static const char *gdb_agent_op_names [gdb_agent_op_last] =
  51.   {
  52.     "?undef?"
  53. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , # NAME
  54. #include "ax.def"
  55. #undef DEFOP
  56.   };

  57. static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
  58.   {
  59.     0
  60. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , SIZE
  61. #include "ax.def"
  62. #undef DEFOP
  63.   };

  64. /* A wrapper for gdb_agent_op_names that does some bounds-checking.  */

  65. static const char *
  66. gdb_agent_op_name (int op)
  67. {
  68.   if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
  69.     return "?undef?";
  70.   return gdb_agent_op_names[op];
  71. }

  72. #ifndef IN_PROCESS_AGENT

  73. /* The packet form of an agent expression consists of an 'X', number
  74.    of bytes in expression, a comma, and then the bytes.  */

  75. struct agent_expr *
  76. gdb_parse_agent_expr (char **actparm)
  77. {
  78.   char *act = *actparm;
  79.   ULONGEST xlen;
  80.   struct agent_expr *aexpr;

  81.   ++act;  /* skip the X */
  82.   act = unpack_varlen_hex (act, &xlen);
  83.   ++act;  /* skip a comma */
  84.   aexpr = xmalloc (sizeof (struct agent_expr));
  85.   aexpr->length = xlen;
  86.   aexpr->bytes = xmalloc (xlen);
  87.   hex2bin (act, aexpr->bytes, xlen);
  88.   *actparm = act + (xlen * 2);
  89.   return aexpr;
  90. }

  91. void
  92. gdb_free_agent_expr (struct agent_expr *aexpr)
  93. {
  94.   if (aexpr != NULL)
  95.     {
  96.       free (aexpr->bytes);
  97.       free (aexpr);
  98.     }
  99. }

  100. /* Convert the bytes of an agent expression back into hex digits, so
  101.    they can be printed or uploaded.  This allocates the buffer,
  102.    callers should free when they are done with it.  */

  103. char *
  104. gdb_unparse_agent_expr (struct agent_expr *aexpr)
  105. {
  106.   char *rslt;

  107.   rslt = xmalloc (2 * aexpr->length + 1);
  108.   bin2hex (aexpr->bytes, rslt, aexpr->length);
  109.   return rslt;
  110. }

  111. /* Bytecode compilation.  */

  112. CORE_ADDR current_insn_ptr;

  113. int emit_error;

  114. struct bytecode_address
  115. {
  116.   int pc;
  117.   CORE_ADDR address;
  118.   int goto_pc;
  119.   /* Offset and size of field to be modified in the goto block.  */
  120.   int from_offset, from_size;
  121.   struct bytecode_address *next;
  122. } *bytecode_address_table;

  123. void
  124. emit_prologue (void)
  125. {
  126.   target_emit_ops ()->emit_prologue ();
  127. }

  128. void
  129. emit_epilogue (void)
  130. {
  131.   target_emit_ops ()->emit_epilogue ();
  132. }

  133. static void
  134. emit_add (void)
  135. {
  136.   target_emit_ops ()->emit_add ();
  137. }

  138. static void
  139. emit_sub (void)
  140. {
  141.   target_emit_ops ()->emit_sub ();
  142. }

  143. static void
  144. emit_mul (void)
  145. {
  146.   target_emit_ops ()->emit_mul ();
  147. }

  148. static void
  149. emit_lsh (void)
  150. {
  151.   target_emit_ops ()->emit_lsh ();
  152. }

  153. static void
  154. emit_rsh_signed (void)
  155. {
  156.   target_emit_ops ()->emit_rsh_signed ();
  157. }

  158. static void
  159. emit_rsh_unsigned (void)
  160. {
  161.   target_emit_ops ()->emit_rsh_unsigned ();
  162. }

  163. static void
  164. emit_ext (int arg)
  165. {
  166.   target_emit_ops ()->emit_ext (arg);
  167. }

  168. static void
  169. emit_log_not (void)
  170. {
  171.   target_emit_ops ()->emit_log_not ();
  172. }

  173. static void
  174. emit_bit_and (void)
  175. {
  176.   target_emit_ops ()->emit_bit_and ();
  177. }

  178. static void
  179. emit_bit_or (void)
  180. {
  181.   target_emit_ops ()->emit_bit_or ();
  182. }

  183. static void
  184. emit_bit_xor (void)
  185. {
  186.   target_emit_ops ()->emit_bit_xor ();
  187. }

  188. static void
  189. emit_bit_not (void)
  190. {
  191.   target_emit_ops ()->emit_bit_not ();
  192. }

  193. static void
  194. emit_equal (void)
  195. {
  196.   target_emit_ops ()->emit_equal ();
  197. }

  198. static void
  199. emit_less_signed (void)
  200. {
  201.   target_emit_ops ()->emit_less_signed ();
  202. }

  203. static void
  204. emit_less_unsigned (void)
  205. {
  206.   target_emit_ops ()->emit_less_unsigned ();
  207. }

  208. static void
  209. emit_ref (int size)
  210. {
  211.   target_emit_ops ()->emit_ref (size);
  212. }

  213. static void
  214. emit_if_goto (int *offset_p, int *size_p)
  215. {
  216.   target_emit_ops ()->emit_if_goto (offset_p, size_p);
  217. }

  218. static void
  219. emit_goto (int *offset_p, int *size_p)
  220. {
  221.   target_emit_ops ()->emit_goto (offset_p, size_p);
  222. }

  223. static void
  224. write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
  225. {
  226.   target_emit_ops ()->write_goto_address (from, to, size);
  227. }

  228. static void
  229. emit_const (LONGEST num)
  230. {
  231.   target_emit_ops ()->emit_const (num);
  232. }

  233. static void
  234. emit_reg (int reg)
  235. {
  236.   target_emit_ops ()->emit_reg (reg);
  237. }

  238. static void
  239. emit_pop (void)
  240. {
  241.   target_emit_ops ()->emit_pop ();
  242. }

  243. static void
  244. emit_stack_flush (void)
  245. {
  246.   target_emit_ops ()->emit_stack_flush ();
  247. }

  248. static void
  249. emit_zero_ext (int arg)
  250. {
  251.   target_emit_ops ()->emit_zero_ext (arg);
  252. }

  253. static void
  254. emit_swap (void)
  255. {
  256.   target_emit_ops ()->emit_swap ();
  257. }

  258. static void
  259. emit_stack_adjust (int n)
  260. {
  261.   target_emit_ops ()->emit_stack_adjust (n);
  262. }

  263. /* FN's prototype is `LONGEST(*fn)(int)'.  */

  264. static void
  265. emit_int_call_1 (CORE_ADDR fn, int arg1)
  266. {
  267.   target_emit_ops ()->emit_int_call_1 (fn, arg1);
  268. }

  269. /* FN's prototype is `void(*fn)(int,LONGEST)'.  */

  270. static void
  271. emit_void_call_2 (CORE_ADDR fn, int arg1)
  272. {
  273.   target_emit_ops ()->emit_void_call_2 (fn, arg1);
  274. }

  275. static void
  276. emit_eq_goto (int *offset_p, int *size_p)
  277. {
  278.   target_emit_ops ()->emit_eq_goto (offset_p, size_p);
  279. }

  280. static void
  281. emit_ne_goto (int *offset_p, int *size_p)
  282. {
  283.   target_emit_ops ()->emit_ne_goto (offset_p, size_p);
  284. }

  285. static void
  286. emit_lt_goto (int *offset_p, int *size_p)
  287. {
  288.   target_emit_ops ()->emit_lt_goto (offset_p, size_p);
  289. }

  290. static void
  291. emit_ge_goto (int *offset_p, int *size_p)
  292. {
  293.   target_emit_ops ()->emit_ge_goto (offset_p, size_p);
  294. }

  295. static void
  296. emit_gt_goto (int *offset_p, int *size_p)
  297. {
  298.   target_emit_ops ()->emit_gt_goto (offset_p, size_p);
  299. }

  300. static void
  301. emit_le_goto (int *offset_p, int *size_p)
  302. {
  303.   target_emit_ops ()->emit_le_goto (offset_p, size_p);
  304. }

  305. /* Scan an agent expression for any evidence that the given PC is the
  306.    target of a jump bytecode in the expression.  */

  307. int
  308. is_goto_target (struct agent_expr *aexpr, int pc)
  309. {
  310.   int i;
  311.   unsigned char op;

  312.   for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
  313.     {
  314.       op = aexpr->bytes[i];

  315.       if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
  316.         {
  317.           int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
  318.           if (target == pc)
  319.             return 1;
  320.         }
  321.     }

  322.   return 0;
  323. }

  324. /* Given an agent expression, turn it into native code.  */

  325. enum eval_result_type
  326. compile_bytecodes (struct agent_expr *aexpr)
  327. {
  328.   int pc = 0;
  329.   int done = 0;
  330.   unsigned char op, next_op;
  331.   int arg;
  332.   /* This is only used to build 64-bit value for constants.  */
  333.   ULONGEST top;
  334.   struct bytecode_address *aentry, *aentry2;

  335. #define UNHANDLED                                        \
  336.   do                                                        \
  337.     {                                                        \
  338.       ax_debug ("Cannot compile op 0x%x\n", op);        \
  339.       return expr_eval_unhandled_opcode;                \
  340.     } while (0)

  341.   if (aexpr->length == 0)
  342.     {
  343.       ax_debug ("empty agent expression\n");
  344.       return expr_eval_empty_expression;
  345.     }

  346.   bytecode_address_table = NULL;

  347.   while (!done)
  348.     {
  349.       op = aexpr->bytes[pc];

  350.       ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);

  351.       /* Record the compiled-code address of the bytecode, for use by
  352.          jump instructions.  */
  353.       aentry = xmalloc (sizeof (struct bytecode_address));
  354.       aentry->pc = pc;
  355.       aentry->address = current_insn_ptr;
  356.       aentry->goto_pc = -1;
  357.       aentry->from_offset = aentry->from_size = 0;
  358.       aentry->next = bytecode_address_table;
  359.       bytecode_address_table = aentry;

  360.       ++pc;

  361.       emit_error = 0;

  362.       switch (op)
  363.         {
  364.         case gdb_agent_op_add:
  365.           emit_add ();
  366.           break;

  367.         case gdb_agent_op_sub:
  368.           emit_sub ();
  369.           break;

  370.         case gdb_agent_op_mul:
  371.           emit_mul ();
  372.           break;

  373.         case gdb_agent_op_div_signed:
  374.           UNHANDLED;
  375.           break;

  376.         case gdb_agent_op_div_unsigned:
  377.           UNHANDLED;
  378.           break;

  379.         case gdb_agent_op_rem_signed:
  380.           UNHANDLED;
  381.           break;

  382.         case gdb_agent_op_rem_unsigned:
  383.           UNHANDLED;
  384.           break;

  385.         case gdb_agent_op_lsh:
  386.           emit_lsh ();
  387.           break;

  388.         case gdb_agent_op_rsh_signed:
  389.           emit_rsh_signed ();
  390.           break;

  391.         case gdb_agent_op_rsh_unsigned:
  392.           emit_rsh_unsigned ();
  393.           break;

  394.         case gdb_agent_op_trace:
  395.           UNHANDLED;
  396.           break;

  397.         case gdb_agent_op_trace_quick:
  398.           UNHANDLED;
  399.           break;

  400.         case gdb_agent_op_log_not:
  401.           emit_log_not ();
  402.           break;

  403.         case gdb_agent_op_bit_and:
  404.           emit_bit_and ();
  405.           break;

  406.         case gdb_agent_op_bit_or:
  407.           emit_bit_or ();
  408.           break;

  409.         case gdb_agent_op_bit_xor:
  410.           emit_bit_xor ();
  411.           break;

  412.         case gdb_agent_op_bit_not:
  413.           emit_bit_not ();
  414.           break;

  415.         case gdb_agent_op_equal:
  416.           next_op = aexpr->bytes[pc];
  417.           if (next_op == gdb_agent_op_if_goto
  418.               && !is_goto_target (aexpr, pc)
  419.               && target_emit_ops ()->emit_eq_goto)
  420.             {
  421.               ax_debug ("Combining equal & if_goto");
  422.               pc += 1;
  423.               aentry->pc = pc;
  424.               arg = aexpr->bytes[pc++];
  425.               arg = (arg << 8) + aexpr->bytes[pc++];
  426.               aentry->goto_pc = arg;
  427.               emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
  428.             }
  429.           else if (next_op == gdb_agent_op_log_not
  430.                    && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
  431.                    && !is_goto_target (aexpr, pc + 1)
  432.                    && target_emit_ops ()->emit_ne_goto)
  433.             {
  434.               ax_debug ("Combining equal & log_not & if_goto");
  435.               pc += 2;
  436.               aentry->pc = pc;
  437.               arg = aexpr->bytes[pc++];
  438.               arg = (arg << 8) + aexpr->bytes[pc++];
  439.               aentry->goto_pc = arg;
  440.               emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
  441.             }
  442.           else
  443.             emit_equal ();
  444.           break;

  445.         case gdb_agent_op_less_signed:
  446.           next_op = aexpr->bytes[pc];
  447.           if (next_op == gdb_agent_op_if_goto
  448.               && !is_goto_target (aexpr, pc))
  449.             {
  450.               ax_debug ("Combining less_signed & if_goto");
  451.               pc += 1;
  452.               aentry->pc = pc;
  453.               arg = aexpr->bytes[pc++];
  454.               arg = (arg << 8) + aexpr->bytes[pc++];
  455.               aentry->goto_pc = arg;
  456.               emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
  457.             }
  458.           else if (next_op == gdb_agent_op_log_not
  459.                    && !is_goto_target (aexpr, pc)
  460.                    && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
  461.                    && !is_goto_target (aexpr, pc + 1))
  462.             {
  463.               ax_debug ("Combining less_signed & log_not & if_goto");
  464.               pc += 2;
  465.               aentry->pc = pc;
  466.               arg = aexpr->bytes[pc++];
  467.               arg = (arg << 8) + aexpr->bytes[pc++];
  468.               aentry->goto_pc = arg;
  469.               emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
  470.             }
  471.           else
  472.             emit_less_signed ();
  473.           break;

  474.         case gdb_agent_op_less_unsigned:
  475.           emit_less_unsigned ();
  476.           break;

  477.         case gdb_agent_op_ext:
  478.           arg = aexpr->bytes[pc++];
  479.           if (arg < (sizeof (LONGEST) * 8))
  480.             emit_ext (arg);
  481.           break;

  482.         case gdb_agent_op_ref8:
  483.           emit_ref (1);
  484.           break;

  485.         case gdb_agent_op_ref16:
  486.           emit_ref (2);
  487.           break;

  488.         case gdb_agent_op_ref32:
  489.           emit_ref (4);
  490.           break;

  491.         case gdb_agent_op_ref64:
  492.           emit_ref (8);
  493.           break;

  494.         case gdb_agent_op_if_goto:
  495.           arg = aexpr->bytes[pc++];
  496.           arg = (arg << 8) + aexpr->bytes[pc++];
  497.           aentry->goto_pc = arg;
  498.           emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
  499.           break;

  500.         case gdb_agent_op_goto:
  501.           arg = aexpr->bytes[pc++];
  502.           arg = (arg << 8) + aexpr->bytes[pc++];
  503.           aentry->goto_pc = arg;
  504.           emit_goto (&(aentry->from_offset), &(aentry->from_size));
  505.           break;

  506.         case gdb_agent_op_const8:
  507.           emit_stack_flush ();
  508.           top = aexpr->bytes[pc++];
  509.           emit_const (top);
  510.           break;

  511.         case gdb_agent_op_const16:
  512.           emit_stack_flush ();
  513.           top = aexpr->bytes[pc++];
  514.           top = (top << 8) + aexpr->bytes[pc++];
  515.           emit_const (top);
  516.           break;

  517.         case gdb_agent_op_const32:
  518.           emit_stack_flush ();
  519.           top = aexpr->bytes[pc++];
  520.           top = (top << 8) + aexpr->bytes[pc++];
  521.           top = (top << 8) + aexpr->bytes[pc++];
  522.           top = (top << 8) + aexpr->bytes[pc++];
  523.           emit_const (top);
  524.           break;

  525.         case gdb_agent_op_const64:
  526.           emit_stack_flush ();
  527.           top = aexpr->bytes[pc++];
  528.           top = (top << 8) + aexpr->bytes[pc++];
  529.           top = (top << 8) + aexpr->bytes[pc++];
  530.           top = (top << 8) + aexpr->bytes[pc++];
  531.           top = (top << 8) + 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.           emit_const (top);
  536.           break;

  537.         case gdb_agent_op_reg:
  538.           emit_stack_flush ();
  539.           arg = aexpr->bytes[pc++];
  540.           arg = (arg << 8) + aexpr->bytes[pc++];
  541.           emit_reg (arg);
  542.           break;

  543.         case gdb_agent_op_end:
  544.           ax_debug ("At end of expression\n");

  545.           /* Assume there is one stack element left, and that it is
  546.              cached in "top" where emit_epilogue can get to it.  */
  547.           emit_stack_adjust (1);

  548.           done = 1;
  549.           break;

  550.         case gdb_agent_op_dup:
  551.           /* In our design, dup is equivalent to stack flushing.  */
  552.           emit_stack_flush ();
  553.           break;

  554.         case gdb_agent_op_pop:
  555.           emit_pop ();
  556.           break;

  557.         case gdb_agent_op_zero_ext:
  558.           arg = aexpr->bytes[pc++];
  559.           if (arg < (sizeof (LONGEST) * 8))
  560.             emit_zero_ext (arg);
  561.           break;

  562.         case gdb_agent_op_swap:
  563.           next_op = aexpr->bytes[pc];
  564.           /* Detect greater-than comparison sequences.  */
  565.           if (next_op == gdb_agent_op_less_signed
  566.               && !is_goto_target (aexpr, pc)
  567.               && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
  568.               && !is_goto_target (aexpr, pc + 1))
  569.             {
  570.               ax_debug ("Combining swap & less_signed & if_goto");
  571.               pc += 2;
  572.               aentry->pc = pc;
  573.               arg = aexpr->bytes[pc++];
  574.               arg = (arg << 8) + aexpr->bytes[pc++];
  575.               aentry->goto_pc = arg;
  576.               emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
  577.             }
  578.           else if (next_op == gdb_agent_op_less_signed
  579.                    && !is_goto_target (aexpr, pc)
  580.                    && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
  581.                    && !is_goto_target (aexpr, pc + 1)
  582.                    && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
  583.                    && !is_goto_target (aexpr, pc + 2))
  584.             {
  585.               ax_debug ("Combining swap & less_signed & log_not & if_goto");
  586.               pc += 3;
  587.               aentry->pc = pc;
  588.               arg = aexpr->bytes[pc++];
  589.               arg = (arg << 8) + aexpr->bytes[pc++];
  590.               aentry->goto_pc = arg;
  591.               emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
  592.             }
  593.           else
  594.             emit_swap ();
  595.           break;

  596.         case gdb_agent_op_getv:
  597.           emit_stack_flush ();
  598.           arg = aexpr->bytes[pc++];
  599.           arg = (arg << 8) + aexpr->bytes[pc++];
  600.           emit_int_call_1 (get_get_tsv_func_addr (),
  601.                            arg);
  602.           break;

  603.         case gdb_agent_op_setv:
  604.           arg = aexpr->bytes[pc++];
  605.           arg = (arg << 8) + aexpr->bytes[pc++];
  606.           emit_void_call_2 (get_set_tsv_func_addr (),
  607.                             arg);
  608.           break;

  609.         case gdb_agent_op_tracev:
  610.           UNHANDLED;
  611.           break;

  612.           /* GDB never (currently) generates any of these ops.  */
  613.         case gdb_agent_op_float:
  614.         case gdb_agent_op_ref_float:
  615.         case gdb_agent_op_ref_double:
  616.         case gdb_agent_op_ref_long_double:
  617.         case gdb_agent_op_l_to_d:
  618.         case gdb_agent_op_d_to_l:
  619.         case gdb_agent_op_trace16:
  620.           UNHANDLED;
  621.           break;

  622.         default:
  623.           ax_debug ("Agent expression op 0x%x not recognized\n", op);
  624.           /* Don't struggle on, things will just get worse.  */
  625.           return expr_eval_unrecognized_opcode;
  626.         }

  627.       /* This catches errors that occur in target-specific code
  628.          emission.  */
  629.       if (emit_error)
  630.         {
  631.           ax_debug ("Error %d while emitting code for %s\n",
  632.                     emit_error, gdb_agent_op_name (op));
  633.           return expr_eval_unhandled_opcode;
  634.         }

  635.       ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
  636.     }

  637.   /* Now fill in real addresses as goto destinations.  */
  638.   for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
  639.     {
  640.       int written = 0;

  641.       if (aentry->goto_pc < 0)
  642.         continue;

  643.       /* Find the location that we are going to, and call back into
  644.          target-specific code to write the actual address or
  645.          displacement.  */
  646.       for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
  647.         {
  648.           if (aentry2->pc == aentry->goto_pc)
  649.             {
  650.               ax_debug ("Want to jump from %s to %s\n",
  651.                         paddress (aentry->address),
  652.                         paddress (aentry2->address));
  653.               write_goto_address (aentry->address + aentry->from_offset,
  654.                                   aentry2->address, aentry->from_size);
  655.               written = 1;
  656.               break;
  657.             }
  658.         }

  659.       /* Error out if we didn't find a destination.  */
  660.       if (!written)
  661.         {
  662.           ax_debug ("Destination of goto %d not found\n",
  663.                     aentry->goto_pc);
  664.           return expr_eval_invalid_goto;
  665.         }
  666.     }

  667.   return expr_eval_no_error;
  668. }

  669. #endif

  670. /* Make printf-type calls using arguments supplied from the host.  We
  671.    need to parse the format string ourselves, and call the formatting
  672.    function with one argument at a time, partly because there is no
  673.    safe portable way to construct a varargs call, and partly to serve
  674.    as a security barrier against bad format strings that might get
  675.    in.  */

  676. static void
  677. ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
  678.            int nargs, ULONGEST *args)
  679. {
  680.   const char *f = format;
  681.   struct format_piece *fpieces;
  682.   int i, fp;
  683.   char *current_substring;
  684.   int nargs_wanted;

  685.   ax_debug ("Printf of \"%s\" with %d args", format, nargs);

  686.   fpieces = parse_format_string (&f);

  687.   nargs_wanted = 0;
  688.   for (fp = 0; fpieces[fp].string != NULL; fp++)
  689.     if (fpieces[fp].argclass != literal_piece)
  690.       ++nargs_wanted;

  691.   if (nargs != nargs_wanted)
  692.     error (_("Wrong number of arguments for specified format-string"));

  693.   i = 0;
  694.   for (fp = 0; fpieces[fp].string != NULL; fp++)
  695.     {
  696.       current_substring = fpieces[fp].string;
  697.       ax_debug ("current substring is '%s', class is %d",
  698.                 current_substring, fpieces[fp].argclass);
  699.       switch (fpieces[fp].argclass)
  700.         {
  701.         case string_arg:
  702.           {
  703.             gdb_byte *str;
  704.             CORE_ADDR tem;
  705.             int j;

  706.             tem = args[i];

  707.             /* This is a %s argument.  Find the length of the string.  */
  708.             for (j = 0;; j++)
  709.               {
  710.                 gdb_byte c;

  711.                 read_inferior_memory (tem + j, &c, 1);
  712.                 if (c == 0)
  713.                   break;
  714.               }

  715.               /* Copy the string contents into a string inside GDB.  */
  716.               str = (gdb_byte *) alloca (j + 1);
  717.               if (j != 0)
  718.                 read_inferior_memory (tem, str, j);
  719.               str[j] = 0;

  720.               printf (current_substring, (char *) str);
  721.             }
  722.             break;

  723.           case long_long_arg:
  724. #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
  725.             {
  726.               long long val = args[i];

  727.               printf (current_substring, val);
  728.               break;
  729.             }
  730. #else
  731.             error (_("long long not supported in agent printf"));
  732. #endif
  733.         case int_arg:
  734.           {
  735.             int val = args[i];

  736.             printf (current_substring, val);
  737.             break;
  738.           }

  739.         case long_arg:
  740.           {
  741.             long val = args[i];

  742.             printf (current_substring, val);
  743.             break;
  744.           }

  745.         case literal_piece:
  746.           /* Print a portion of the format string that has no
  747.              directives.  Note that this will not include any
  748.              ordinary %-specs, but it might include "%%".  That is
  749.              why we use printf_filtered and not puts_filtered here.
  750.              Also, we pass a dummy argument because some platforms
  751.              have modified GCC to include -Wformat-security by
  752.              default, which will warn here if there is no
  753.              argument.  */
  754.           printf (current_substring, 0);
  755.           break;

  756.         default:
  757.           error (_("Format directive in '%s' not supported in agent printf"),
  758.                  current_substring);
  759.         }

  760.       /* Maybe advance to the next argument.  */
  761.       if (fpieces[fp].argclass != literal_piece)
  762.         ++i;
  763.     }

  764.   free_format_pieces (fpieces);
  765.   fflush (stdout);
  766. }

  767. /* The agent expression evaluator, as specified by the GDB docs. It
  768.    returns 0 if everything went OK, and a nonzero error code
  769.    otherwise.  */

  770. enum eval_result_type
  771. gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
  772.                      struct agent_expr *aexpr,
  773.                      ULONGEST *rslt)
  774. {
  775.   int pc = 0;
  776. #define STACK_MAX 100
  777.   ULONGEST stack[STACK_MAX], top;
  778.   int sp = 0;
  779.   unsigned char op;
  780.   int arg;

  781.   /* This union is a convenient way to convert representations.  For
  782.      now, assume a standard architecture where the hardware integer
  783.      types have 8, 16, 32, 64 bit types.  A more robust solution would
  784.      be to import stdint.h from gnulib.  */
  785.   union
  786.   {
  787.     union
  788.     {
  789.       unsigned char bytes[1];
  790.       unsigned char val;
  791.     } u8;
  792.     union
  793.     {
  794.       unsigned char bytes[2];
  795.       unsigned short val;
  796.     } u16;
  797.     union
  798.     {
  799.       unsigned char bytes[4];
  800.       unsigned int val;
  801.     } u32;
  802.     union
  803.     {
  804.       unsigned char bytes[8];
  805.       ULONGEST val;
  806.     } u64;
  807.   } cnv;

  808.   if (aexpr->length == 0)
  809.     {
  810.       ax_debug ("empty agent expression");
  811.       return expr_eval_empty_expression;
  812.     }

  813.   /* Cache the stack top in its own variable. Much of the time we can
  814.      operate on this variable, rather than dinking with the stack. It
  815.      needs to be copied to the stack when sp changes.  */
  816.   top = 0;

  817.   while (1)
  818.     {
  819.       op = aexpr->bytes[pc++];

  820.       ax_debug ("About to interpret byte 0x%x", op);

  821.       switch (op)
  822.         {
  823.         case gdb_agent_op_add:
  824.           top += stack[--sp];
  825.           break;

  826.         case gdb_agent_op_sub:
  827.           top = stack[--sp] - top;
  828.           break;

  829.         case gdb_agent_op_mul:
  830.           top *= stack[--sp];
  831.           break;

  832.         case gdb_agent_op_div_signed:
  833.           if (top == 0)
  834.             {
  835.               ax_debug ("Attempted to divide by zero");
  836.               return expr_eval_divide_by_zero;
  837.             }
  838.           top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
  839.           break;

  840.         case gdb_agent_op_div_unsigned:
  841.           if (top == 0)
  842.             {
  843.               ax_debug ("Attempted to divide by zero");
  844.               return expr_eval_divide_by_zero;
  845.             }
  846.           top = stack[--sp] / top;
  847.           break;

  848.         case gdb_agent_op_rem_signed:
  849.           if (top == 0)
  850.             {
  851.               ax_debug ("Attempted to divide by zero");
  852.               return expr_eval_divide_by_zero;
  853.             }
  854.           top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
  855.           break;

  856.         case gdb_agent_op_rem_unsigned:
  857.           if (top == 0)
  858.             {
  859.               ax_debug ("Attempted to divide by zero");
  860.               return expr_eval_divide_by_zero;
  861.             }
  862.           top = stack[--sp] % top;
  863.           break;

  864.         case gdb_agent_op_lsh:
  865.           top = stack[--sp] << top;
  866.           break;

  867.         case gdb_agent_op_rsh_signed:
  868.           top = ((LONGEST) stack[--sp]) >> top;
  869.           break;

  870.         case gdb_agent_op_rsh_unsigned:
  871.           top = stack[--sp] >> top;
  872.           break;

  873.         case gdb_agent_op_trace:
  874.           agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
  875.                           (ULONGEST) top);
  876.           if (--sp >= 0)
  877.             top = stack[sp];
  878.           break;

  879.         case gdb_agent_op_trace_quick:
  880.           arg = aexpr->bytes[pc++];
  881.           agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
  882.           break;

  883.         case gdb_agent_op_log_not:
  884.           top = !top;
  885.           break;

  886.         case gdb_agent_op_bit_and:
  887.           top &= stack[--sp];
  888.           break;

  889.         case gdb_agent_op_bit_or:
  890.           top |= stack[--sp];
  891.           break;

  892.         case gdb_agent_op_bit_xor:
  893.           top ^= stack[--sp];
  894.           break;

  895.         case gdb_agent_op_bit_not:
  896.           top = ~top;
  897.           break;

  898.         case gdb_agent_op_equal:
  899.           top = (stack[--sp] == top);
  900.           break;

  901.         case gdb_agent_op_less_signed:
  902.           top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
  903.           break;

  904.         case gdb_agent_op_less_unsigned:
  905.           top = (stack[--sp] < top);
  906.           break;

  907.         case gdb_agent_op_ext:
  908.           arg = aexpr->bytes[pc++];
  909.           if (arg < (sizeof (LONGEST) * 8))
  910.             {
  911.               LONGEST mask = 1 << (arg - 1);
  912.               top &= ((LONGEST) 1 << arg) - 1;
  913.               top = (top ^ mask) - mask;
  914.             }
  915.           break;

  916.         case gdb_agent_op_ref8:
  917.           agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
  918.           top = cnv.u8.val;
  919.           break;

  920.         case gdb_agent_op_ref16:
  921.           agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
  922.           top = cnv.u16.val;
  923.           break;

  924.         case gdb_agent_op_ref32:
  925.           agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
  926.           top = cnv.u32.val;
  927.           break;

  928.         case gdb_agent_op_ref64:
  929.           agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
  930.           top = cnv.u64.val;
  931.           break;

  932.         case gdb_agent_op_if_goto:
  933.           if (top)
  934.             pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
  935.           else
  936.             pc += 2;
  937.           if (--sp >= 0)
  938.             top = stack[sp];
  939.           break;

  940.         case gdb_agent_op_goto:
  941.           pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
  942.           break;

  943.         case gdb_agent_op_const8:
  944.           /* Flush the cached stack top.  */
  945.           stack[sp++] = top;
  946.           top = aexpr->bytes[pc++];
  947.           break;

  948.         case gdb_agent_op_const16:
  949.           /* Flush the cached stack top.  */
  950.           stack[sp++] = top;
  951.           top = aexpr->bytes[pc++];
  952.           top = (top << 8) + aexpr->bytes[pc++];
  953.           break;

  954.         case gdb_agent_op_const32:
  955.           /* Flush the cached stack top.  */
  956.           stack[sp++] = top;
  957.           top = aexpr->bytes[pc++];
  958.           top = (top << 8) + aexpr->bytes[pc++];
  959.           top = (top << 8) + aexpr->bytes[pc++];
  960.           top = (top << 8) + aexpr->bytes[pc++];
  961.           break;

  962.         case gdb_agent_op_const64:
  963.           /* Flush the cached stack top.  */
  964.           stack[sp++] = top;
  965.           top = aexpr->bytes[pc++];
  966.           top = (top << 8) + aexpr->bytes[pc++];
  967.           top = (top << 8) + aexpr->bytes[pc++];
  968.           top = (top << 8) + aexpr->bytes[pc++];
  969.           top = (top << 8) + aexpr->bytes[pc++];
  970.           top = (top << 8) + aexpr->bytes[pc++];
  971.           top = (top << 8) + aexpr->bytes[pc++];
  972.           top = (top << 8) + aexpr->bytes[pc++];
  973.           break;

  974.         case gdb_agent_op_reg:
  975.           /* Flush the cached stack top.  */
  976.           stack[sp++] = top;
  977.           arg = aexpr->bytes[pc++];
  978.           arg = (arg << 8) + aexpr->bytes[pc++];
  979.           {
  980.             int regnum = arg;
  981.             struct regcache *regcache = ctx->regcache;

  982.             switch (register_size (regcache->tdesc, regnum))
  983.               {
  984.               case 8:
  985.                 collect_register (regcache, regnum, cnv.u64.bytes);
  986.                 top = cnv.u64.val;
  987.                 break;
  988.               case 4:
  989.                 collect_register (regcache, regnum, cnv.u32.bytes);
  990.                 top = cnv.u32.val;
  991.                 break;
  992.               case 2:
  993.                 collect_register (regcache, regnum, cnv.u16.bytes);
  994.                 top = cnv.u16.val;
  995.                 break;
  996.               case 1:
  997.                 collect_register (regcache, regnum, cnv.u8.bytes);
  998.                 top = cnv.u8.val;
  999.                 break;
  1000.               default:
  1001.                 internal_error (__FILE__, __LINE__,
  1002.                                 "unhandled register size");
  1003.               }
  1004.           }
  1005.           break;

  1006.         case gdb_agent_op_end:
  1007.           ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
  1008.                     sp, pulongest (top));
  1009.           if (rslt)
  1010.             {
  1011.               if (sp <= 0)
  1012.                 {
  1013.                   /* This should be an error */
  1014.                   ax_debug ("Stack is empty, nothing to return");
  1015.                   return expr_eval_empty_stack;
  1016.                 }
  1017.               *rslt = top;
  1018.             }
  1019.           return expr_eval_no_error;

  1020.         case gdb_agent_op_dup:
  1021.           stack[sp++] = top;
  1022.           break;

  1023.         case gdb_agent_op_pop:
  1024.           if (--sp >= 0)
  1025.             top = stack[sp];
  1026.           break;

  1027.         case gdb_agent_op_pick:
  1028.           arg = aexpr->bytes[pc++];
  1029.           stack[sp] = top;
  1030.           top = stack[sp - arg];
  1031.           ++sp;
  1032.           break;

  1033.         case gdb_agent_op_rot:
  1034.           {
  1035.             ULONGEST tem = stack[sp - 1];

  1036.             stack[sp - 1] = stack[sp - 2];
  1037.             stack[sp - 2] = top;
  1038.             top = tem;
  1039.           }
  1040.           break;

  1041.         case gdb_agent_op_zero_ext:
  1042.           arg = aexpr->bytes[pc++];
  1043.           if (arg < (sizeof (LONGEST) * 8))
  1044.             top &= ((LONGEST) 1 << arg) - 1;
  1045.           break;

  1046.         case gdb_agent_op_swap:
  1047.           /* Interchange top two stack elements, making sure top gets
  1048.              copied back onto stack.  */
  1049.           stack[sp] = top;
  1050.           top = stack[sp - 1];
  1051.           stack[sp - 1] = stack[sp];
  1052.           break;

  1053.         case gdb_agent_op_getv:
  1054.           /* Flush the cached stack top.  */
  1055.           stack[sp++] = top;
  1056.           arg = aexpr->bytes[pc++];
  1057.           arg = (arg << 8) + aexpr->bytes[pc++];
  1058.           top = agent_get_trace_state_variable_value (arg);
  1059.           break;

  1060.         case gdb_agent_op_setv:
  1061.           arg = aexpr->bytes[pc++];
  1062.           arg = (arg << 8) + aexpr->bytes[pc++];
  1063.           agent_set_trace_state_variable_value (arg, top);
  1064.           /* Note that we leave the value on the stack, for the
  1065.              benefit of later/enclosing expressions.  */
  1066.           break;

  1067.         case gdb_agent_op_tracev:
  1068.           arg = aexpr->bytes[pc++];
  1069.           arg = (arg << 8) + aexpr->bytes[pc++];
  1070.           agent_tsv_read (ctx, arg);
  1071.           break;

  1072.         case gdb_agent_op_tracenz:
  1073.           agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
  1074.                                  (ULONGEST) top);
  1075.           if (--sp >= 0)
  1076.             top = stack[sp];
  1077.           break;

  1078.         case gdb_agent_op_printf:
  1079.           {
  1080.             int nargs, slen, i;
  1081.             CORE_ADDR fn = 0, chan = 0;
  1082.             /* Can't have more args than the entire size of the stack.  */
  1083.             ULONGEST args[STACK_MAX];
  1084.             char *format;

  1085.             nargs = aexpr->bytes[pc++];
  1086.             slen = aexpr->bytes[pc++];
  1087.             slen = (slen << 8) + aexpr->bytes[pc++];
  1088.             format = (char *) &(aexpr->bytes[pc]);
  1089.             pc += slen;
  1090.             /* Pop function and channel.  */
  1091.             fn = top;
  1092.             if (--sp >= 0)
  1093.               top = stack[sp];
  1094.             chan = top;
  1095.             if (--sp >= 0)
  1096.               top = stack[sp];
  1097.             /* Pop arguments into a dedicated array.  */
  1098.             for (i = 0; i < nargs; ++i)
  1099.               {
  1100.                 args[i] = top;
  1101.                 if (--sp >= 0)
  1102.                   top = stack[sp];
  1103.               }

  1104.             /* A bad format string means something is very wrong; give
  1105.                up immediately.  */
  1106.             if (format[slen - 1] != '\0')
  1107.               error (_("Unterminated format string in printf bytecode"));

  1108.             ax_printf (fn, chan, format, nargs, args);
  1109.           }
  1110.           break;

  1111.           /* GDB never (currently) generates any of these ops.  */
  1112.         case gdb_agent_op_float:
  1113.         case gdb_agent_op_ref_float:
  1114.         case gdb_agent_op_ref_double:
  1115.         case gdb_agent_op_ref_long_double:
  1116.         case gdb_agent_op_l_to_d:
  1117.         case gdb_agent_op_d_to_l:
  1118.         case gdb_agent_op_trace16:
  1119.           ax_debug ("Agent expression op 0x%x valid, but not handled",
  1120.                     op);
  1121.           /* If ever GDB generates any of these, we don't have the
  1122.              option of ignoring.  */
  1123.           return 1;

  1124.         default:
  1125.           ax_debug ("Agent expression op 0x%x not recognized", op);
  1126.           /* Don't struggle on, things will just get worse.  */
  1127.           return expr_eval_unrecognized_opcode;
  1128.         }

  1129.       /* Check for stack badness.  */
  1130.       if (sp >= (STACK_MAX - 1))
  1131.         {
  1132.           ax_debug ("Expression stack overflow");
  1133.           return expr_eval_stack_overflow;
  1134.         }

  1135.       if (sp < 0)
  1136.         {
  1137.           ax_debug ("Expression stack underflow");
  1138.           return expr_eval_stack_underflow;
  1139.         }

  1140.       ax_debug ("Op %s -> sp=%d, top=0x%s",
  1141.                 gdb_agent_op_name (op), sp, phex_nz (top, 0));
  1142.     }
  1143. }