xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/ax.cc (revision 64f917f5a88990e32dd65fcd4348042fa7f852b9)
18dffb485Schristos /* Agent expression code for remote server.
2*64f917f5Schristos    Copyright (C) 2009-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of GDB.
58dffb485Schristos 
68dffb485Schristos    This program is free software; you can redistribute it and/or modify
78dffb485Schristos    it under the terms of the GNU General Public License as published by
88dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
98dffb485Schristos    (at your option) any later version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful,
128dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148dffb485Schristos    GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
188dffb485Schristos 
198dffb485Schristos #include "ax.h"
208dffb485Schristos #include "gdbsupport/format.h"
218dffb485Schristos #include "tracepoint.h"
228dffb485Schristos #include "gdbsupport/rsp-low.h"
238dffb485Schristos 
248dffb485Schristos static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
258dffb485Schristos 
268dffb485Schristos #ifdef IN_PROCESS_AGENT
278dffb485Schristos bool debug_agent = 0;
288dffb485Schristos #endif
298dffb485Schristos 
308dffb485Schristos static void
318dffb485Schristos ax_vdebug (const char *fmt, ...)
328dffb485Schristos {
338dffb485Schristos   char buf[1024];
348dffb485Schristos   va_list ap;
358dffb485Schristos 
368dffb485Schristos   va_start (ap, fmt);
378dffb485Schristos   vsprintf (buf, fmt, ap);
388dffb485Schristos #ifdef IN_PROCESS_AGENT
398dffb485Schristos   fprintf (stderr, PROG "/ax: %s\n", buf);
408dffb485Schristos #else
414b169a6bSchristos   threads_debug_printf (PROG "/ax: %s", buf);
428dffb485Schristos #endif
438dffb485Schristos   va_end (ap);
448dffb485Schristos }
458dffb485Schristos 
464b169a6bSchristos #define ax_debug(fmt, args...) \
478dffb485Schristos   do {						\
484b169a6bSchristos     if (debug_threads)			\
498dffb485Schristos       ax_vdebug ((fmt), ##args);		\
508dffb485Schristos   } while (0)
518dffb485Schristos 
528dffb485Schristos /* This enum must exactly match what is documented in
538dffb485Schristos    gdb/doc/agentexpr.texi, including all the numerical values.  */
548dffb485Schristos 
558dffb485Schristos enum gdb_agent_op
568dffb485Schristos   {
578dffb485Schristos #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  \
588dffb485Schristos     gdb_agent_op_ ## NAME = VALUE,
598dffb485Schristos #include "gdbsupport/ax.def"
608dffb485Schristos #undef DEFOP
618dffb485Schristos     gdb_agent_op_last
628dffb485Schristos   };
638dffb485Schristos 
644b169a6bSchristos static const char * const gdb_agent_op_names [gdb_agent_op_last] =
658dffb485Schristos   {
668dffb485Schristos     "?undef?"
678dffb485Schristos #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , # NAME
688dffb485Schristos #include "gdbsupport/ax.def"
698dffb485Schristos #undef DEFOP
708dffb485Schristos   };
718dffb485Schristos 
728dffb485Schristos #ifndef IN_PROCESS_AGENT
738dffb485Schristos static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
748dffb485Schristos   {
758dffb485Schristos     0
768dffb485Schristos #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , SIZE
778dffb485Schristos #include "gdbsupport/ax.def"
788dffb485Schristos #undef DEFOP
798dffb485Schristos   };
808dffb485Schristos #endif
818dffb485Schristos 
828dffb485Schristos /* A wrapper for gdb_agent_op_names that does some bounds-checking.  */
838dffb485Schristos 
848dffb485Schristos static const char *
858dffb485Schristos gdb_agent_op_name (int op)
868dffb485Schristos {
878dffb485Schristos   if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
888dffb485Schristos     return "?undef?";
898dffb485Schristos   return gdb_agent_op_names[op];
908dffb485Schristos }
918dffb485Schristos 
928dffb485Schristos #ifndef IN_PROCESS_AGENT
938dffb485Schristos 
948dffb485Schristos /* The packet form of an agent expression consists of an 'X', number
958dffb485Schristos    of bytes in expression, a comma, and then the bytes.  */
968dffb485Schristos 
978dffb485Schristos struct agent_expr *
988dffb485Schristos gdb_parse_agent_expr (const char **actparm)
998dffb485Schristos {
1008dffb485Schristos   const char *act = *actparm;
1018dffb485Schristos   ULONGEST xlen;
1028dffb485Schristos   struct agent_expr *aexpr;
1038dffb485Schristos 
1048dffb485Schristos   ++act;  /* skip the X */
1058dffb485Schristos   act = unpack_varlen_hex (act, &xlen);
1068dffb485Schristos   ++act;  /* skip a comma */
1078dffb485Schristos   aexpr = XNEW (struct agent_expr);
1088dffb485Schristos   aexpr->length = xlen;
1098dffb485Schristos   aexpr->bytes = (unsigned char *) xmalloc (xlen);
1108dffb485Schristos   hex2bin (act, aexpr->bytes, xlen);
1118dffb485Schristos   *actparm = act + (xlen * 2);
1128dffb485Schristos   return aexpr;
1138dffb485Schristos }
1148dffb485Schristos 
1158dffb485Schristos void
1168dffb485Schristos gdb_free_agent_expr (struct agent_expr *aexpr)
1178dffb485Schristos {
1188dffb485Schristos   if (aexpr != NULL)
1198dffb485Schristos     {
1208dffb485Schristos       free (aexpr->bytes);
1218dffb485Schristos       free (aexpr);
1228dffb485Schristos     }
1238dffb485Schristos }
1248dffb485Schristos 
1258dffb485Schristos /* Convert the bytes of an agent expression back into hex digits, so
1268dffb485Schristos    they can be printed or uploaded.  This allocates the buffer,
1278dffb485Schristos    callers should free when they are done with it.  */
1288dffb485Schristos 
1298dffb485Schristos char *
1308dffb485Schristos gdb_unparse_agent_expr (struct agent_expr *aexpr)
1318dffb485Schristos {
1328dffb485Schristos   char *rslt;
1338dffb485Schristos 
1348dffb485Schristos   rslt = (char *) xmalloc (2 * aexpr->length + 1);
1358dffb485Schristos   bin2hex (aexpr->bytes, rslt, aexpr->length);
1368dffb485Schristos   return rslt;
1378dffb485Schristos }
1388dffb485Schristos 
1398dffb485Schristos /* Bytecode compilation.  */
1408dffb485Schristos 
1418dffb485Schristos CORE_ADDR current_insn_ptr;
1428dffb485Schristos 
1438dffb485Schristos int emit_error;
1448dffb485Schristos 
1454b169a6bSchristos static struct bytecode_address
1468dffb485Schristos {
1478dffb485Schristos   int pc;
1488dffb485Schristos   CORE_ADDR address;
1498dffb485Schristos   int goto_pc;
1508dffb485Schristos   /* Offset and size of field to be modified in the goto block.  */
1518dffb485Schristos   int from_offset, from_size;
1528dffb485Schristos   struct bytecode_address *next;
1538dffb485Schristos } *bytecode_address_table;
1548dffb485Schristos 
1558dffb485Schristos void
1568dffb485Schristos emit_prologue (void)
1578dffb485Schristos {
1588dffb485Schristos   target_emit_ops ()->emit_prologue ();
1598dffb485Schristos }
1608dffb485Schristos 
1618dffb485Schristos void
1628dffb485Schristos emit_epilogue (void)
1638dffb485Schristos {
1648dffb485Schristos   target_emit_ops ()->emit_epilogue ();
1658dffb485Schristos }
1668dffb485Schristos 
1678dffb485Schristos static void
1688dffb485Schristos emit_add (void)
1698dffb485Schristos {
1708dffb485Schristos   target_emit_ops ()->emit_add ();
1718dffb485Schristos }
1728dffb485Schristos 
1738dffb485Schristos static void
1748dffb485Schristos emit_sub (void)
1758dffb485Schristos {
1768dffb485Schristos   target_emit_ops ()->emit_sub ();
1778dffb485Schristos }
1788dffb485Schristos 
1798dffb485Schristos static void
1808dffb485Schristos emit_mul (void)
1818dffb485Schristos {
1828dffb485Schristos   target_emit_ops ()->emit_mul ();
1838dffb485Schristos }
1848dffb485Schristos 
1858dffb485Schristos static void
1868dffb485Schristos emit_lsh (void)
1878dffb485Schristos {
1888dffb485Schristos   target_emit_ops ()->emit_lsh ();
1898dffb485Schristos }
1908dffb485Schristos 
1918dffb485Schristos static void
1928dffb485Schristos emit_rsh_signed (void)
1938dffb485Schristos {
1948dffb485Schristos   target_emit_ops ()->emit_rsh_signed ();
1958dffb485Schristos }
1968dffb485Schristos 
1978dffb485Schristos static void
1988dffb485Schristos emit_rsh_unsigned (void)
1998dffb485Schristos {
2008dffb485Schristos   target_emit_ops ()->emit_rsh_unsigned ();
2018dffb485Schristos }
2028dffb485Schristos 
2038dffb485Schristos static void
2048dffb485Schristos emit_ext (int arg)
2058dffb485Schristos {
2068dffb485Schristos   target_emit_ops ()->emit_ext (arg);
2078dffb485Schristos }
2088dffb485Schristos 
2098dffb485Schristos static void
2108dffb485Schristos emit_log_not (void)
2118dffb485Schristos {
2128dffb485Schristos   target_emit_ops ()->emit_log_not ();
2138dffb485Schristos }
2148dffb485Schristos 
2158dffb485Schristos static void
2168dffb485Schristos emit_bit_and (void)
2178dffb485Schristos {
2188dffb485Schristos   target_emit_ops ()->emit_bit_and ();
2198dffb485Schristos }
2208dffb485Schristos 
2218dffb485Schristos static void
2228dffb485Schristos emit_bit_or (void)
2238dffb485Schristos {
2248dffb485Schristos   target_emit_ops ()->emit_bit_or ();
2258dffb485Schristos }
2268dffb485Schristos 
2278dffb485Schristos static void
2288dffb485Schristos emit_bit_xor (void)
2298dffb485Schristos {
2308dffb485Schristos   target_emit_ops ()->emit_bit_xor ();
2318dffb485Schristos }
2328dffb485Schristos 
2338dffb485Schristos static void
2348dffb485Schristos emit_bit_not (void)
2358dffb485Schristos {
2368dffb485Schristos   target_emit_ops ()->emit_bit_not ();
2378dffb485Schristos }
2388dffb485Schristos 
2398dffb485Schristos static void
2408dffb485Schristos emit_equal (void)
2418dffb485Schristos {
2428dffb485Schristos   target_emit_ops ()->emit_equal ();
2438dffb485Schristos }
2448dffb485Schristos 
2458dffb485Schristos static void
2468dffb485Schristos emit_less_signed (void)
2478dffb485Schristos {
2488dffb485Schristos   target_emit_ops ()->emit_less_signed ();
2498dffb485Schristos }
2508dffb485Schristos 
2518dffb485Schristos static void
2528dffb485Schristos emit_less_unsigned (void)
2538dffb485Schristos {
2548dffb485Schristos   target_emit_ops ()->emit_less_unsigned ();
2558dffb485Schristos }
2568dffb485Schristos 
2578dffb485Schristos static void
2588dffb485Schristos emit_ref (int size)
2598dffb485Schristos {
2608dffb485Schristos   target_emit_ops ()->emit_ref (size);
2618dffb485Schristos }
2628dffb485Schristos 
2638dffb485Schristos static void
2648dffb485Schristos emit_if_goto (int *offset_p, int *size_p)
2658dffb485Schristos {
2668dffb485Schristos   target_emit_ops ()->emit_if_goto (offset_p, size_p);
2678dffb485Schristos }
2688dffb485Schristos 
2698dffb485Schristos static void
2708dffb485Schristos emit_goto (int *offset_p, int *size_p)
2718dffb485Schristos {
2728dffb485Schristos   target_emit_ops ()->emit_goto (offset_p, size_p);
2738dffb485Schristos }
2748dffb485Schristos 
2758dffb485Schristos static void
2768dffb485Schristos write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
2778dffb485Schristos {
2788dffb485Schristos   target_emit_ops ()->write_goto_address (from, to, size);
2798dffb485Schristos }
2808dffb485Schristos 
2818dffb485Schristos static void
2828dffb485Schristos emit_const (LONGEST num)
2838dffb485Schristos {
2848dffb485Schristos   target_emit_ops ()->emit_const (num);
2858dffb485Schristos }
2868dffb485Schristos 
2878dffb485Schristos static void
2888dffb485Schristos emit_reg (int reg)
2898dffb485Schristos {
2908dffb485Schristos   target_emit_ops ()->emit_reg (reg);
2918dffb485Schristos }
2928dffb485Schristos 
2938dffb485Schristos static void
2948dffb485Schristos emit_pop (void)
2958dffb485Schristos {
2968dffb485Schristos   target_emit_ops ()->emit_pop ();
2978dffb485Schristos }
2988dffb485Schristos 
2998dffb485Schristos static void
3008dffb485Schristos emit_stack_flush (void)
3018dffb485Schristos {
3028dffb485Schristos   target_emit_ops ()->emit_stack_flush ();
3038dffb485Schristos }
3048dffb485Schristos 
3058dffb485Schristos static void
3068dffb485Schristos emit_zero_ext (int arg)
3078dffb485Schristos {
3088dffb485Schristos   target_emit_ops ()->emit_zero_ext (arg);
3098dffb485Schristos }
3108dffb485Schristos 
3118dffb485Schristos static void
3128dffb485Schristos emit_swap (void)
3138dffb485Schristos {
3148dffb485Schristos   target_emit_ops ()->emit_swap ();
3158dffb485Schristos }
3168dffb485Schristos 
3178dffb485Schristos static void
3188dffb485Schristos emit_stack_adjust (int n)
3198dffb485Schristos {
3208dffb485Schristos   target_emit_ops ()->emit_stack_adjust (n);
3218dffb485Schristos }
3228dffb485Schristos 
3238dffb485Schristos /* FN's prototype is `LONGEST(*fn)(int)'.  */
3248dffb485Schristos 
3258dffb485Schristos static void
3268dffb485Schristos emit_int_call_1 (CORE_ADDR fn, int arg1)
3278dffb485Schristos {
3288dffb485Schristos   target_emit_ops ()->emit_int_call_1 (fn, arg1);
3298dffb485Schristos }
3308dffb485Schristos 
3318dffb485Schristos /* FN's prototype is `void(*fn)(int,LONGEST)'.  */
3328dffb485Schristos 
3338dffb485Schristos static void
3348dffb485Schristos emit_void_call_2 (CORE_ADDR fn, int arg1)
3358dffb485Schristos {
3368dffb485Schristos   target_emit_ops ()->emit_void_call_2 (fn, arg1);
3378dffb485Schristos }
3388dffb485Schristos 
3398dffb485Schristos static void
3408dffb485Schristos emit_eq_goto (int *offset_p, int *size_p)
3418dffb485Schristos {
3428dffb485Schristos   target_emit_ops ()->emit_eq_goto (offset_p, size_p);
3438dffb485Schristos }
3448dffb485Schristos 
3458dffb485Schristos static void
3468dffb485Schristos emit_ne_goto (int *offset_p, int *size_p)
3478dffb485Schristos {
3488dffb485Schristos   target_emit_ops ()->emit_ne_goto (offset_p, size_p);
3498dffb485Schristos }
3508dffb485Schristos 
3518dffb485Schristos static void
3528dffb485Schristos emit_lt_goto (int *offset_p, int *size_p)
3538dffb485Schristos {
3548dffb485Schristos   target_emit_ops ()->emit_lt_goto (offset_p, size_p);
3558dffb485Schristos }
3568dffb485Schristos 
3578dffb485Schristos static void
3588dffb485Schristos emit_ge_goto (int *offset_p, int *size_p)
3598dffb485Schristos {
3608dffb485Schristos   target_emit_ops ()->emit_ge_goto (offset_p, size_p);
3618dffb485Schristos }
3628dffb485Schristos 
3638dffb485Schristos static void
3648dffb485Schristos emit_gt_goto (int *offset_p, int *size_p)
3658dffb485Schristos {
3668dffb485Schristos   target_emit_ops ()->emit_gt_goto (offset_p, size_p);
3678dffb485Schristos }
3688dffb485Schristos 
3698dffb485Schristos static void
3708dffb485Schristos emit_le_goto (int *offset_p, int *size_p)
3718dffb485Schristos {
3728dffb485Schristos   target_emit_ops ()->emit_le_goto (offset_p, size_p);
3738dffb485Schristos }
3748dffb485Schristos 
3758dffb485Schristos /* Scan an agent expression for any evidence that the given PC is the
3768dffb485Schristos    target of a jump bytecode in the expression.  */
3778dffb485Schristos 
3788dffb485Schristos static int
3798dffb485Schristos is_goto_target (struct agent_expr *aexpr, int pc)
3808dffb485Schristos {
3818dffb485Schristos   int i;
3828dffb485Schristos   unsigned char op;
3838dffb485Schristos 
3848dffb485Schristos   for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
3858dffb485Schristos     {
3868dffb485Schristos       op = aexpr->bytes[i];
3878dffb485Schristos 
3888dffb485Schristos       if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
3898dffb485Schristos 	{
3908dffb485Schristos 	  int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
3918dffb485Schristos 	  if (target == pc)
3928dffb485Schristos 	    return 1;
3938dffb485Schristos 	}
3948dffb485Schristos     }
3958dffb485Schristos 
3968dffb485Schristos   return 0;
3978dffb485Schristos }
3988dffb485Schristos 
3998dffb485Schristos /* Given an agent expression, turn it into native code.  */
4008dffb485Schristos 
4018dffb485Schristos enum eval_result_type
4028dffb485Schristos compile_bytecodes (struct agent_expr *aexpr)
4038dffb485Schristos {
4048dffb485Schristos   int pc = 0;
4058dffb485Schristos   int done = 0;
4068dffb485Schristos   unsigned char op, next_op;
4078dffb485Schristos   int arg;
4088dffb485Schristos   /* This is only used to build 64-bit value for constants.  */
4098dffb485Schristos   ULONGEST top;
4108dffb485Schristos   struct bytecode_address *aentry, *aentry2;
4118dffb485Schristos 
4128dffb485Schristos #define UNHANDLED					\
4138dffb485Schristos   do							\
4148dffb485Schristos     {							\
4158dffb485Schristos       ax_debug ("Cannot compile op 0x%x\n", op);	\
4168dffb485Schristos       return expr_eval_unhandled_opcode;		\
4178dffb485Schristos     } while (0)
4188dffb485Schristos 
4198dffb485Schristos   if (aexpr->length == 0)
4208dffb485Schristos     {
4218dffb485Schristos       ax_debug ("empty agent expression\n");
4228dffb485Schristos       return expr_eval_empty_expression;
4238dffb485Schristos     }
4248dffb485Schristos 
4258dffb485Schristos   bytecode_address_table = NULL;
4268dffb485Schristos 
4278dffb485Schristos   while (!done)
4288dffb485Schristos     {
4298dffb485Schristos       op = aexpr->bytes[pc];
4308dffb485Schristos 
4318dffb485Schristos       ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
4328dffb485Schristos 
4338dffb485Schristos       /* Record the compiled-code address of the bytecode, for use by
4348dffb485Schristos 	 jump instructions.  */
4358dffb485Schristos       aentry = XNEW (struct bytecode_address);
4368dffb485Schristos       aentry->pc = pc;
4378dffb485Schristos       aentry->address = current_insn_ptr;
4388dffb485Schristos       aentry->goto_pc = -1;
4398dffb485Schristos       aentry->from_offset = aentry->from_size = 0;
4408dffb485Schristos       aentry->next = bytecode_address_table;
4418dffb485Schristos       bytecode_address_table = aentry;
4428dffb485Schristos 
4438dffb485Schristos       ++pc;
4448dffb485Schristos 
4458dffb485Schristos       emit_error = 0;
4468dffb485Schristos 
4478dffb485Schristos       switch (op)
4488dffb485Schristos 	{
4498dffb485Schristos 	case gdb_agent_op_add:
4508dffb485Schristos 	  emit_add ();
4518dffb485Schristos 	  break;
4528dffb485Schristos 
4538dffb485Schristos 	case gdb_agent_op_sub:
4548dffb485Schristos 	  emit_sub ();
4558dffb485Schristos 	  break;
4568dffb485Schristos 
4578dffb485Schristos 	case gdb_agent_op_mul:
4588dffb485Schristos 	  emit_mul ();
4598dffb485Schristos 	  break;
4608dffb485Schristos 
4618dffb485Schristos 	case gdb_agent_op_div_signed:
4628dffb485Schristos 	  UNHANDLED;
4638dffb485Schristos 	  break;
4648dffb485Schristos 
4658dffb485Schristos 	case gdb_agent_op_div_unsigned:
4668dffb485Schristos 	  UNHANDLED;
4678dffb485Schristos 	  break;
4688dffb485Schristos 
4698dffb485Schristos 	case gdb_agent_op_rem_signed:
4708dffb485Schristos 	  UNHANDLED;
4718dffb485Schristos 	  break;
4728dffb485Schristos 
4738dffb485Schristos 	case gdb_agent_op_rem_unsigned:
4748dffb485Schristos 	  UNHANDLED;
4758dffb485Schristos 	  break;
4768dffb485Schristos 
4778dffb485Schristos 	case gdb_agent_op_lsh:
4788dffb485Schristos 	  emit_lsh ();
4798dffb485Schristos 	  break;
4808dffb485Schristos 
4818dffb485Schristos 	case gdb_agent_op_rsh_signed:
4828dffb485Schristos 	  emit_rsh_signed ();
4838dffb485Schristos 	  break;
4848dffb485Schristos 
4858dffb485Schristos 	case gdb_agent_op_rsh_unsigned:
4868dffb485Schristos 	  emit_rsh_unsigned ();
4878dffb485Schristos 	  break;
4888dffb485Schristos 
4898dffb485Schristos 	case gdb_agent_op_trace:
4908dffb485Schristos 	  UNHANDLED;
4918dffb485Schristos 	  break;
4928dffb485Schristos 
4938dffb485Schristos 	case gdb_agent_op_trace_quick:
4948dffb485Schristos 	  UNHANDLED;
4958dffb485Schristos 	  break;
4968dffb485Schristos 
4978dffb485Schristos 	case gdb_agent_op_log_not:
4988dffb485Schristos 	  emit_log_not ();
4998dffb485Schristos 	  break;
5008dffb485Schristos 
5018dffb485Schristos 	case gdb_agent_op_bit_and:
5028dffb485Schristos 	  emit_bit_and ();
5038dffb485Schristos 	  break;
5048dffb485Schristos 
5058dffb485Schristos 	case gdb_agent_op_bit_or:
5068dffb485Schristos 	  emit_bit_or ();
5078dffb485Schristos 	  break;
5088dffb485Schristos 
5098dffb485Schristos 	case gdb_agent_op_bit_xor:
5108dffb485Schristos 	  emit_bit_xor ();
5118dffb485Schristos 	  break;
5128dffb485Schristos 
5138dffb485Schristos 	case gdb_agent_op_bit_not:
5148dffb485Schristos 	  emit_bit_not ();
5158dffb485Schristos 	  break;
5168dffb485Schristos 
5178dffb485Schristos 	case gdb_agent_op_equal:
5188dffb485Schristos 	  next_op = aexpr->bytes[pc];
5198dffb485Schristos 	  if (next_op == gdb_agent_op_if_goto
5208dffb485Schristos 	      && !is_goto_target (aexpr, pc)
5218dffb485Schristos 	      && target_emit_ops ()->emit_eq_goto)
5228dffb485Schristos 	    {
5238dffb485Schristos 	      ax_debug ("Combining equal & if_goto");
5248dffb485Schristos 	      pc += 1;
5258dffb485Schristos 	      aentry->pc = pc;
5268dffb485Schristos 	      arg = aexpr->bytes[pc++];
5278dffb485Schristos 	      arg = (arg << 8) + aexpr->bytes[pc++];
5288dffb485Schristos 	      aentry->goto_pc = arg;
5298dffb485Schristos 	      emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
5308dffb485Schristos 	    }
5318dffb485Schristos 	  else if (next_op == gdb_agent_op_log_not
5328dffb485Schristos 		   && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
5338dffb485Schristos 		   && !is_goto_target (aexpr, pc + 1)
5348dffb485Schristos 		   && target_emit_ops ()->emit_ne_goto)
5358dffb485Schristos 	    {
5368dffb485Schristos 	      ax_debug ("Combining equal & log_not & if_goto");
5378dffb485Schristos 	      pc += 2;
5388dffb485Schristos 	      aentry->pc = pc;
5398dffb485Schristos 	      arg = aexpr->bytes[pc++];
5408dffb485Schristos 	      arg = (arg << 8) + aexpr->bytes[pc++];
5418dffb485Schristos 	      aentry->goto_pc = arg;
5428dffb485Schristos 	      emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
5438dffb485Schristos 	    }
5448dffb485Schristos 	  else
5458dffb485Schristos 	    emit_equal ();
5468dffb485Schristos 	  break;
5478dffb485Schristos 
5488dffb485Schristos 	case gdb_agent_op_less_signed:
5498dffb485Schristos 	  next_op = aexpr->bytes[pc];
5508dffb485Schristos 	  if (next_op == gdb_agent_op_if_goto
5518dffb485Schristos 	      && !is_goto_target (aexpr, pc))
5528dffb485Schristos 	    {
5538dffb485Schristos 	      ax_debug ("Combining less_signed & if_goto");
5548dffb485Schristos 	      pc += 1;
5558dffb485Schristos 	      aentry->pc = pc;
5568dffb485Schristos 	      arg = aexpr->bytes[pc++];
5578dffb485Schristos 	      arg = (arg << 8) + aexpr->bytes[pc++];
5588dffb485Schristos 	      aentry->goto_pc = arg;
5598dffb485Schristos 	      emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
5608dffb485Schristos 	    }
5618dffb485Schristos 	  else if (next_op == gdb_agent_op_log_not
5628dffb485Schristos 		   && !is_goto_target (aexpr, pc)
5638dffb485Schristos 		   && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
5648dffb485Schristos 		   && !is_goto_target (aexpr, pc + 1))
5658dffb485Schristos 	    {
5668dffb485Schristos 	      ax_debug ("Combining less_signed & log_not & if_goto");
5678dffb485Schristos 	      pc += 2;
5688dffb485Schristos 	      aentry->pc = pc;
5698dffb485Schristos 	      arg = aexpr->bytes[pc++];
5708dffb485Schristos 	      arg = (arg << 8) + aexpr->bytes[pc++];
5718dffb485Schristos 	      aentry->goto_pc = arg;
5728dffb485Schristos 	      emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
5738dffb485Schristos 	    }
5748dffb485Schristos 	  else
5758dffb485Schristos 	    emit_less_signed ();
5768dffb485Schristos 	  break;
5778dffb485Schristos 
5788dffb485Schristos 	case gdb_agent_op_less_unsigned:
5798dffb485Schristos 	  emit_less_unsigned ();
5808dffb485Schristos 	  break;
5818dffb485Schristos 
5828dffb485Schristos 	case gdb_agent_op_ext:
5838dffb485Schristos 	  arg = aexpr->bytes[pc++];
5848dffb485Schristos 	  if (arg < (sizeof (LONGEST) * 8))
5858dffb485Schristos 	    emit_ext (arg);
5868dffb485Schristos 	  break;
5878dffb485Schristos 
5888dffb485Schristos 	case gdb_agent_op_ref8:
5898dffb485Schristos 	  emit_ref (1);
5908dffb485Schristos 	  break;
5918dffb485Schristos 
5928dffb485Schristos 	case gdb_agent_op_ref16:
5938dffb485Schristos 	  emit_ref (2);
5948dffb485Schristos 	  break;
5958dffb485Schristos 
5968dffb485Schristos 	case gdb_agent_op_ref32:
5978dffb485Schristos 	  emit_ref (4);
5988dffb485Schristos 	  break;
5998dffb485Schristos 
6008dffb485Schristos 	case gdb_agent_op_ref64:
6018dffb485Schristos 	  emit_ref (8);
6028dffb485Schristos 	  break;
6038dffb485Schristos 
6048dffb485Schristos 	case gdb_agent_op_if_goto:
6058dffb485Schristos 	  arg = aexpr->bytes[pc++];
6068dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
6078dffb485Schristos 	  aentry->goto_pc = arg;
6088dffb485Schristos 	  emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
6098dffb485Schristos 	  break;
6108dffb485Schristos 
6118dffb485Schristos 	case gdb_agent_op_goto:
6128dffb485Schristos 	  arg = aexpr->bytes[pc++];
6138dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
6148dffb485Schristos 	  aentry->goto_pc = arg;
6158dffb485Schristos 	  emit_goto (&(aentry->from_offset), &(aentry->from_size));
6168dffb485Schristos 	  break;
6178dffb485Schristos 
6188dffb485Schristos 	case gdb_agent_op_const8:
6198dffb485Schristos 	  emit_stack_flush ();
6208dffb485Schristos 	  top = aexpr->bytes[pc++];
6218dffb485Schristos 	  emit_const (top);
6228dffb485Schristos 	  break;
6238dffb485Schristos 
6248dffb485Schristos 	case gdb_agent_op_const16:
6258dffb485Schristos 	  emit_stack_flush ();
6268dffb485Schristos 	  top = aexpr->bytes[pc++];
6278dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6288dffb485Schristos 	  emit_const (top);
6298dffb485Schristos 	  break;
6308dffb485Schristos 
6318dffb485Schristos 	case gdb_agent_op_const32:
6328dffb485Schristos 	  emit_stack_flush ();
6338dffb485Schristos 	  top = aexpr->bytes[pc++];
6348dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6358dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6368dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6378dffb485Schristos 	  emit_const (top);
6388dffb485Schristos 	  break;
6398dffb485Schristos 
6408dffb485Schristos 	case gdb_agent_op_const64:
6418dffb485Schristos 	  emit_stack_flush ();
6428dffb485Schristos 	  top = aexpr->bytes[pc++];
6438dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6448dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6458dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6468dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6478dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6488dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6498dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
6508dffb485Schristos 	  emit_const (top);
6518dffb485Schristos 	  break;
6528dffb485Schristos 
6538dffb485Schristos 	case gdb_agent_op_reg:
6548dffb485Schristos 	  emit_stack_flush ();
6558dffb485Schristos 	  arg = aexpr->bytes[pc++];
6568dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
6578dffb485Schristos 	  emit_reg (arg);
6588dffb485Schristos 	  break;
6598dffb485Schristos 
6608dffb485Schristos 	case gdb_agent_op_end:
6618dffb485Schristos 	  ax_debug ("At end of expression\n");
6628dffb485Schristos 
6638dffb485Schristos 	  /* Assume there is one stack element left, and that it is
6648dffb485Schristos 	     cached in "top" where emit_epilogue can get to it.  */
6658dffb485Schristos 	  emit_stack_adjust (1);
6668dffb485Schristos 
6678dffb485Schristos 	  done = 1;
6688dffb485Schristos 	  break;
6698dffb485Schristos 
6708dffb485Schristos 	case gdb_agent_op_dup:
6718dffb485Schristos 	  /* In our design, dup is equivalent to stack flushing.  */
6728dffb485Schristos 	  emit_stack_flush ();
6738dffb485Schristos 	  break;
6748dffb485Schristos 
6758dffb485Schristos 	case gdb_agent_op_pop:
6768dffb485Schristos 	  emit_pop ();
6778dffb485Schristos 	  break;
6788dffb485Schristos 
6798dffb485Schristos 	case gdb_agent_op_zero_ext:
6808dffb485Schristos 	  arg = aexpr->bytes[pc++];
6818dffb485Schristos 	  if (arg < (sizeof (LONGEST) * 8))
6828dffb485Schristos 	    emit_zero_ext (arg);
6838dffb485Schristos 	  break;
6848dffb485Schristos 
6858dffb485Schristos 	case gdb_agent_op_swap:
6868dffb485Schristos 	  next_op = aexpr->bytes[pc];
6878dffb485Schristos 	  /* Detect greater-than comparison sequences.  */
6888dffb485Schristos 	  if (next_op == gdb_agent_op_less_signed
6898dffb485Schristos 	      && !is_goto_target (aexpr, pc)
6908dffb485Schristos 	      && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6918dffb485Schristos 	      && !is_goto_target (aexpr, pc + 1))
6928dffb485Schristos 	    {
6938dffb485Schristos 	      ax_debug ("Combining swap & less_signed & if_goto");
6948dffb485Schristos 	      pc += 2;
6958dffb485Schristos 	      aentry->pc = pc;
6968dffb485Schristos 	      arg = aexpr->bytes[pc++];
6978dffb485Schristos 	      arg = (arg << 8) + aexpr->bytes[pc++];
6988dffb485Schristos 	      aentry->goto_pc = arg;
6998dffb485Schristos 	      emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
7008dffb485Schristos 	    }
7018dffb485Schristos 	  else if (next_op == gdb_agent_op_less_signed
7028dffb485Schristos 		   && !is_goto_target (aexpr, pc)
7038dffb485Schristos 		   && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
7048dffb485Schristos 		   && !is_goto_target (aexpr, pc + 1)
7058dffb485Schristos 		   && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
7068dffb485Schristos 		   && !is_goto_target (aexpr, pc + 2))
7078dffb485Schristos 	    {
7088dffb485Schristos 	      ax_debug ("Combining swap & less_signed & log_not & if_goto");
7098dffb485Schristos 	      pc += 3;
7108dffb485Schristos 	      aentry->pc = pc;
7118dffb485Schristos 	      arg = aexpr->bytes[pc++];
7128dffb485Schristos 	      arg = (arg << 8) + aexpr->bytes[pc++];
7138dffb485Schristos 	      aentry->goto_pc = arg;
7148dffb485Schristos 	      emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
7158dffb485Schristos 	    }
7168dffb485Schristos 	  else
7178dffb485Schristos 	    emit_swap ();
7188dffb485Schristos 	  break;
7198dffb485Schristos 
7208dffb485Schristos 	case gdb_agent_op_getv:
7218dffb485Schristos 	  emit_stack_flush ();
7228dffb485Schristos 	  arg = aexpr->bytes[pc++];
7238dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
7248dffb485Schristos 	  emit_int_call_1 (get_get_tsv_func_addr (),
7258dffb485Schristos 			   arg);
7268dffb485Schristos 	  break;
7278dffb485Schristos 
7288dffb485Schristos 	case gdb_agent_op_setv:
7298dffb485Schristos 	  arg = aexpr->bytes[pc++];
7308dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
7318dffb485Schristos 	  emit_void_call_2 (get_set_tsv_func_addr (),
7328dffb485Schristos 			    arg);
7338dffb485Schristos 	  break;
7348dffb485Schristos 
7358dffb485Schristos 	case gdb_agent_op_tracev:
7368dffb485Schristos 	  UNHANDLED;
7378dffb485Schristos 	  break;
7388dffb485Schristos 
7398dffb485Schristos 	  /* GDB never (currently) generates any of these ops.  */
7408dffb485Schristos 	case gdb_agent_op_float:
7418dffb485Schristos 	case gdb_agent_op_ref_float:
7428dffb485Schristos 	case gdb_agent_op_ref_double:
7438dffb485Schristos 	case gdb_agent_op_ref_long_double:
7448dffb485Schristos 	case gdb_agent_op_l_to_d:
7458dffb485Schristos 	case gdb_agent_op_d_to_l:
7468dffb485Schristos 	case gdb_agent_op_trace16:
7478dffb485Schristos 	  UNHANDLED;
7488dffb485Schristos 	  break;
7498dffb485Schristos 
7508dffb485Schristos 	default:
7518dffb485Schristos 	  ax_debug ("Agent expression op 0x%x not recognized\n", op);
7528dffb485Schristos 	  /* Don't struggle on, things will just get worse.  */
7538dffb485Schristos 	  return expr_eval_unrecognized_opcode;
7548dffb485Schristos 	}
7558dffb485Schristos 
7568dffb485Schristos       /* This catches errors that occur in target-specific code
7578dffb485Schristos 	 emission.  */
7588dffb485Schristos       if (emit_error)
7598dffb485Schristos 	{
7608dffb485Schristos 	  ax_debug ("Error %d while emitting code for %s\n",
7618dffb485Schristos 		    emit_error, gdb_agent_op_name (op));
7628dffb485Schristos 	  return expr_eval_unhandled_opcode;
7638dffb485Schristos 	}
7648dffb485Schristos 
7658dffb485Schristos       ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
7668dffb485Schristos     }
7678dffb485Schristos 
7688dffb485Schristos   /* Now fill in real addresses as goto destinations.  */
7698dffb485Schristos   for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
7708dffb485Schristos     {
7718dffb485Schristos       int written = 0;
7728dffb485Schristos 
7738dffb485Schristos       if (aentry->goto_pc < 0)
7748dffb485Schristos 	continue;
7758dffb485Schristos 
7768dffb485Schristos       /* Find the location that we are going to, and call back into
7778dffb485Schristos 	 target-specific code to write the actual address or
7788dffb485Schristos 	 displacement.  */
7798dffb485Schristos       for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
7808dffb485Schristos 	{
7818dffb485Schristos 	  if (aentry2->pc == aentry->goto_pc)
7828dffb485Schristos 	    {
7838dffb485Schristos 	      ax_debug ("Want to jump from %s to %s\n",
7848dffb485Schristos 			paddress (aentry->address),
7858dffb485Schristos 			paddress (aentry2->address));
7868dffb485Schristos 	      write_goto_address (aentry->address + aentry->from_offset,
7878dffb485Schristos 				  aentry2->address, aentry->from_size);
7888dffb485Schristos 	      written = 1;
7898dffb485Schristos 	      break;
7908dffb485Schristos 	    }
7918dffb485Schristos 	}
7928dffb485Schristos 
7938dffb485Schristos       /* Error out if we didn't find a destination.  */
7948dffb485Schristos       if (!written)
7958dffb485Schristos 	{
7968dffb485Schristos 	  ax_debug ("Destination of goto %d not found\n",
7978dffb485Schristos 		    aentry->goto_pc);
7988dffb485Schristos 	  return expr_eval_invalid_goto;
7998dffb485Schristos 	}
8008dffb485Schristos     }
8018dffb485Schristos 
8028dffb485Schristos   return expr_eval_no_error;
8038dffb485Schristos }
8048dffb485Schristos 
8058dffb485Schristos #endif
8068dffb485Schristos 
8078dffb485Schristos /* Make printf-type calls using arguments supplied from the host.  We
8088dffb485Schristos    need to parse the format string ourselves, and call the formatting
8098dffb485Schristos    function with one argument at a time, partly because there is no
8108dffb485Schristos    safe portable way to construct a varargs call, and partly to serve
8118dffb485Schristos    as a security barrier against bad format strings that might get
8128dffb485Schristos    in.  */
8138dffb485Schristos 
8148dffb485Schristos static void
8158dffb485Schristos ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
8168dffb485Schristos 	   int nargs, ULONGEST *args)
8178dffb485Schristos {
8188dffb485Schristos   const char *f = format;
8198dffb485Schristos   int i;
8208dffb485Schristos   const char *current_substring;
8218dffb485Schristos   int nargs_wanted;
8228dffb485Schristos 
8238dffb485Schristos   ax_debug ("Printf of \"%s\" with %d args", format, nargs);
8248dffb485Schristos 
8258dffb485Schristos   format_pieces fpieces (&f);
8268dffb485Schristos 
8278dffb485Schristos   nargs_wanted = 0;
8288dffb485Schristos   for (auto &&piece : fpieces)
8298dffb485Schristos     if (piece.argclass != literal_piece)
8308dffb485Schristos       ++nargs_wanted;
8318dffb485Schristos 
8328dffb485Schristos   if (nargs != nargs_wanted)
8338dffb485Schristos     error (_("Wrong number of arguments for specified format-string"));
8348dffb485Schristos 
8358dffb485Schristos   i = 0;
8368dffb485Schristos   for (auto &&piece : fpieces)
8378dffb485Schristos     {
8388dffb485Schristos       current_substring = piece.string;
8398dffb485Schristos       ax_debug ("current substring is '%s', class is %d",
8408dffb485Schristos 		current_substring, piece.argclass);
8418dffb485Schristos       switch (piece.argclass)
8428dffb485Schristos 	{
8438dffb485Schristos 	case string_arg:
8448dffb485Schristos 	  {
8458dffb485Schristos 	    gdb_byte *str;
8468dffb485Schristos 	    CORE_ADDR tem;
8478dffb485Schristos 	    int j;
8488dffb485Schristos 
8498dffb485Schristos 	    tem = args[i];
8508dffb485Schristos 	    if (tem == 0)
8518dffb485Schristos 	      {
8528dffb485Schristos 		printf (current_substring, "(null)");
8538dffb485Schristos 		break;
8548dffb485Schristos 	      }
8558dffb485Schristos 
8568dffb485Schristos 	    /* This is a %s argument.  Find the length of the string.  */
8578dffb485Schristos 	    for (j = 0;; j++)
8588dffb485Schristos 	      {
8598dffb485Schristos 		gdb_byte c;
8608dffb485Schristos 
8618dffb485Schristos 		read_inferior_memory (tem + j, &c, 1);
8628dffb485Schristos 		if (c == 0)
8638dffb485Schristos 		  break;
8648dffb485Schristos 	      }
8658dffb485Schristos 
8668dffb485Schristos 	      /* Copy the string contents into a string inside GDB.  */
8678dffb485Schristos 	      str = (gdb_byte *) alloca (j + 1);
8688dffb485Schristos 	      if (j != 0)
8698dffb485Schristos 		read_inferior_memory (tem, str, j);
8708dffb485Schristos 	      str[j] = 0;
8718dffb485Schristos 
8728dffb485Schristos 	      printf (current_substring, (char *) str);
8738dffb485Schristos 	    }
8748dffb485Schristos 	    break;
8758dffb485Schristos 
8768dffb485Schristos 	  case long_long_arg:
8774b169a6bSchristos #if defined (PRINTF_HAS_LONG_LONG)
8788dffb485Schristos 	    {
8798dffb485Schristos 	      long long val = args[i];
8808dffb485Schristos 
8818dffb485Schristos 	      printf (current_substring, val);
8828dffb485Schristos 	      break;
8838dffb485Schristos 	    }
8848dffb485Schristos #else
8858dffb485Schristos 	    error (_("long long not supported in agent printf"));
8868dffb485Schristos #endif
8878dffb485Schristos 	case int_arg:
8888dffb485Schristos 	  {
8898dffb485Schristos 	    int val = args[i];
8908dffb485Schristos 
8918dffb485Schristos 	    printf (current_substring, val);
8928dffb485Schristos 	    break;
8938dffb485Schristos 	  }
8948dffb485Schristos 
8958dffb485Schristos 	case long_arg:
8968dffb485Schristos 	  {
8978dffb485Schristos 	    long val = args[i];
8988dffb485Schristos 
8998dffb485Schristos 	    printf (current_substring, val);
9008dffb485Schristos 	    break;
9018dffb485Schristos 	  }
9028dffb485Schristos 
9038dffb485Schristos 	case size_t_arg:
9048dffb485Schristos 	  {
9058dffb485Schristos 	    size_t val = args[i];
9068dffb485Schristos 
9078dffb485Schristos 	    printf (current_substring, val);
9088dffb485Schristos 	    break;
9098dffb485Schristos 	  }
9108dffb485Schristos 
9118dffb485Schristos 	case literal_piece:
9128dffb485Schristos 	  /* Print a portion of the format string that has no
9138dffb485Schristos 	     directives.  Note that this will not include any
9148dffb485Schristos 	     ordinary %-specs, but it might include "%%".  That is
9158dffb485Schristos 	     why we use printf_filtered and not puts_filtered here.
9168dffb485Schristos 	     Also, we pass a dummy argument because some platforms
9178dffb485Schristos 	     have modified GCC to include -Wformat-security by
9188dffb485Schristos 	     default, which will warn here if there is no
9198dffb485Schristos 	     argument.  */
9208dffb485Schristos 	  printf (current_substring, 0);
9218dffb485Schristos 	  break;
9228dffb485Schristos 
9238dffb485Schristos 	default:
9248dffb485Schristos 	  error (_("Format directive in '%s' not supported in agent printf"),
9258dffb485Schristos 		 current_substring);
9268dffb485Schristos 	}
9278dffb485Schristos 
9288dffb485Schristos       /* Maybe advance to the next argument.  */
9298dffb485Schristos       if (piece.argclass != literal_piece)
9308dffb485Schristos 	++i;
9318dffb485Schristos     }
9328dffb485Schristos 
9338dffb485Schristos   fflush (stdout);
9348dffb485Schristos }
9358dffb485Schristos 
9368dffb485Schristos /* The agent expression evaluator, as specified by the GDB docs. It
9378dffb485Schristos    returns 0 if everything went OK, and a nonzero error code
9388dffb485Schristos    otherwise.  */
9398dffb485Schristos 
9408dffb485Schristos enum eval_result_type
9418dffb485Schristos gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
9428dffb485Schristos 		     struct agent_expr *aexpr,
9438dffb485Schristos 		     ULONGEST *rslt)
9448dffb485Schristos {
9458dffb485Schristos   int pc = 0;
9468dffb485Schristos #define STACK_MAX 100
9478dffb485Schristos   ULONGEST stack[STACK_MAX], top;
9488dffb485Schristos   int sp = 0;
9498dffb485Schristos   unsigned char op;
9508dffb485Schristos   int arg;
9518dffb485Schristos 
9528dffb485Schristos   /* This union is a convenient way to convert representations.  For
9538dffb485Schristos      now, assume a standard architecture where the hardware integer
9548dffb485Schristos      types have 8, 16, 32, 64 bit types.  A more robust solution would
9558dffb485Schristos      be to import stdint.h from gnulib.  */
9568dffb485Schristos   union
9578dffb485Schristos   {
9588dffb485Schristos     union
9598dffb485Schristos     {
9608dffb485Schristos       unsigned char bytes[1];
9618dffb485Schristos       unsigned char val;
9628dffb485Schristos     } u8;
9638dffb485Schristos     union
9648dffb485Schristos     {
9658dffb485Schristos       unsigned char bytes[2];
9668dffb485Schristos       unsigned short val;
9678dffb485Schristos     } u16;
9688dffb485Schristos     union
9698dffb485Schristos     {
9708dffb485Schristos       unsigned char bytes[4];
9718dffb485Schristos       unsigned int val;
9728dffb485Schristos     } u32;
9738dffb485Schristos     union
9748dffb485Schristos     {
9758dffb485Schristos       unsigned char bytes[8];
9768dffb485Schristos       ULONGEST val;
9778dffb485Schristos     } u64;
9788dffb485Schristos   } cnv;
9798dffb485Schristos 
9808dffb485Schristos   if (aexpr->length == 0)
9818dffb485Schristos     {
9828dffb485Schristos       ax_debug ("empty agent expression");
9838dffb485Schristos       return expr_eval_empty_expression;
9848dffb485Schristos     }
9858dffb485Schristos 
9868dffb485Schristos   /* Cache the stack top in its own variable. Much of the time we can
987*64f917f5Schristos      operate on this variable, rather than syncing with the stack. It
9888dffb485Schristos      needs to be copied to the stack when sp changes.  */
9898dffb485Schristos   top = 0;
9908dffb485Schristos 
9918dffb485Schristos   while (1)
9928dffb485Schristos     {
9938dffb485Schristos       op = aexpr->bytes[pc++];
9948dffb485Schristos 
9958dffb485Schristos       ax_debug ("About to interpret byte 0x%x", op);
9968dffb485Schristos 
9978dffb485Schristos       switch (op)
9988dffb485Schristos 	{
9998dffb485Schristos 	case gdb_agent_op_add:
10008dffb485Schristos 	  top += stack[--sp];
10018dffb485Schristos 	  break;
10028dffb485Schristos 
10038dffb485Schristos 	case gdb_agent_op_sub:
10048dffb485Schristos 	  top = stack[--sp] - top;
10058dffb485Schristos 	  break;
10068dffb485Schristos 
10078dffb485Schristos 	case gdb_agent_op_mul:
10088dffb485Schristos 	  top *= stack[--sp];
10098dffb485Schristos 	  break;
10108dffb485Schristos 
10118dffb485Schristos 	case gdb_agent_op_div_signed:
10128dffb485Schristos 	  if (top == 0)
10138dffb485Schristos 	    {
10148dffb485Schristos 	      ax_debug ("Attempted to divide by zero");
10158dffb485Schristos 	      return expr_eval_divide_by_zero;
10168dffb485Schristos 	    }
10178dffb485Schristos 	  top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
10188dffb485Schristos 	  break;
10198dffb485Schristos 
10208dffb485Schristos 	case gdb_agent_op_div_unsigned:
10218dffb485Schristos 	  if (top == 0)
10228dffb485Schristos 	    {
10238dffb485Schristos 	      ax_debug ("Attempted to divide by zero");
10248dffb485Schristos 	      return expr_eval_divide_by_zero;
10258dffb485Schristos 	    }
10268dffb485Schristos 	  top = stack[--sp] / top;
10278dffb485Schristos 	  break;
10288dffb485Schristos 
10298dffb485Schristos 	case gdb_agent_op_rem_signed:
10308dffb485Schristos 	  if (top == 0)
10318dffb485Schristos 	    {
10328dffb485Schristos 	      ax_debug ("Attempted to divide by zero");
10338dffb485Schristos 	      return expr_eval_divide_by_zero;
10348dffb485Schristos 	    }
10358dffb485Schristos 	  top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
10368dffb485Schristos 	  break;
10378dffb485Schristos 
10388dffb485Schristos 	case gdb_agent_op_rem_unsigned:
10398dffb485Schristos 	  if (top == 0)
10408dffb485Schristos 	    {
10418dffb485Schristos 	      ax_debug ("Attempted to divide by zero");
10428dffb485Schristos 	      return expr_eval_divide_by_zero;
10438dffb485Schristos 	    }
10448dffb485Schristos 	  top = stack[--sp] % top;
10458dffb485Schristos 	  break;
10468dffb485Schristos 
10478dffb485Schristos 	case gdb_agent_op_lsh:
10488dffb485Schristos 	  top = stack[--sp] << top;
10498dffb485Schristos 	  break;
10508dffb485Schristos 
10518dffb485Schristos 	case gdb_agent_op_rsh_signed:
10528dffb485Schristos 	  top = ((LONGEST) stack[--sp]) >> top;
10538dffb485Schristos 	  break;
10548dffb485Schristos 
10558dffb485Schristos 	case gdb_agent_op_rsh_unsigned:
10568dffb485Schristos 	  top = stack[--sp] >> top;
10578dffb485Schristos 	  break;
10588dffb485Schristos 
10598dffb485Schristos 	case gdb_agent_op_trace:
10608dffb485Schristos 	  agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
10618dffb485Schristos 			  (ULONGEST) top);
10628dffb485Schristos 	  if (--sp >= 0)
10638dffb485Schristos 	    top = stack[sp];
10648dffb485Schristos 	  break;
10658dffb485Schristos 
10668dffb485Schristos 	case gdb_agent_op_trace_quick:
10678dffb485Schristos 	  arg = aexpr->bytes[pc++];
10688dffb485Schristos 	  agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
10698dffb485Schristos 	  break;
10708dffb485Schristos 
10718dffb485Schristos 	case gdb_agent_op_log_not:
10728dffb485Schristos 	  top = !top;
10738dffb485Schristos 	  break;
10748dffb485Schristos 
10758dffb485Schristos 	case gdb_agent_op_bit_and:
10768dffb485Schristos 	  top &= stack[--sp];
10778dffb485Schristos 	  break;
10788dffb485Schristos 
10798dffb485Schristos 	case gdb_agent_op_bit_or:
10808dffb485Schristos 	  top |= stack[--sp];
10818dffb485Schristos 	  break;
10828dffb485Schristos 
10838dffb485Schristos 	case gdb_agent_op_bit_xor:
10848dffb485Schristos 	  top ^= stack[--sp];
10858dffb485Schristos 	  break;
10868dffb485Schristos 
10878dffb485Schristos 	case gdb_agent_op_bit_not:
10888dffb485Schristos 	  top = ~top;
10898dffb485Schristos 	  break;
10908dffb485Schristos 
10918dffb485Schristos 	case gdb_agent_op_equal:
10928dffb485Schristos 	  top = (stack[--sp] == top);
10938dffb485Schristos 	  break;
10948dffb485Schristos 
10958dffb485Schristos 	case gdb_agent_op_less_signed:
10968dffb485Schristos 	  top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
10978dffb485Schristos 	  break;
10988dffb485Schristos 
10998dffb485Schristos 	case gdb_agent_op_less_unsigned:
11008dffb485Schristos 	  top = (stack[--sp] < top);
11018dffb485Schristos 	  break;
11028dffb485Schristos 
11038dffb485Schristos 	case gdb_agent_op_ext:
11048dffb485Schristos 	  arg = aexpr->bytes[pc++];
11058dffb485Schristos 	  if (arg < (sizeof (LONGEST) * 8))
11068dffb485Schristos 	    {
11078dffb485Schristos 	      LONGEST mask = 1 << (arg - 1);
11088dffb485Schristos 	      top &= ((LONGEST) 1 << arg) - 1;
11098dffb485Schristos 	      top = (top ^ mask) - mask;
11108dffb485Schristos 	    }
11118dffb485Schristos 	  break;
11128dffb485Schristos 
11138dffb485Schristos 	case gdb_agent_op_ref8:
1114*64f917f5Schristos 	  if (agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1) != 0)
1115*64f917f5Schristos 	    return expr_eval_invalid_memory_access;
11168dffb485Schristos 	  top = cnv.u8.val;
11178dffb485Schristos 	  break;
11188dffb485Schristos 
11198dffb485Schristos 	case gdb_agent_op_ref16:
1120*64f917f5Schristos 	  if (agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2) != 0)
1121*64f917f5Schristos 	    return expr_eval_invalid_memory_access;
11228dffb485Schristos 	  top = cnv.u16.val;
11238dffb485Schristos 	  break;
11248dffb485Schristos 
11258dffb485Schristos 	case gdb_agent_op_ref32:
1126*64f917f5Schristos 	  if (agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4) != 0)
1127*64f917f5Schristos 	    return expr_eval_invalid_memory_access;
11288dffb485Schristos 	  top = cnv.u32.val;
11298dffb485Schristos 	  break;
11308dffb485Schristos 
11318dffb485Schristos 	case gdb_agent_op_ref64:
1132*64f917f5Schristos 	  if (agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8) != 0)
1133*64f917f5Schristos 	    return expr_eval_invalid_memory_access;
11348dffb485Schristos 	  top = cnv.u64.val;
11358dffb485Schristos 	  break;
11368dffb485Schristos 
11378dffb485Schristos 	case gdb_agent_op_if_goto:
11388dffb485Schristos 	  if (top)
11398dffb485Schristos 	    pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
11408dffb485Schristos 	  else
11418dffb485Schristos 	    pc += 2;
11428dffb485Schristos 	  if (--sp >= 0)
11438dffb485Schristos 	    top = stack[sp];
11448dffb485Schristos 	  break;
11458dffb485Schristos 
11468dffb485Schristos 	case gdb_agent_op_goto:
11478dffb485Schristos 	  pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
11488dffb485Schristos 	  break;
11498dffb485Schristos 
11508dffb485Schristos 	case gdb_agent_op_const8:
11518dffb485Schristos 	  /* Flush the cached stack top.  */
11528dffb485Schristos 	  stack[sp++] = top;
11538dffb485Schristos 	  top = aexpr->bytes[pc++];
11548dffb485Schristos 	  break;
11558dffb485Schristos 
11568dffb485Schristos 	case gdb_agent_op_const16:
11578dffb485Schristos 	  /* Flush the cached stack top.  */
11588dffb485Schristos 	  stack[sp++] = top;
11598dffb485Schristos 	  top = aexpr->bytes[pc++];
11608dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11618dffb485Schristos 	  break;
11628dffb485Schristos 
11638dffb485Schristos 	case gdb_agent_op_const32:
11648dffb485Schristos 	  /* Flush the cached stack top.  */
11658dffb485Schristos 	  stack[sp++] = top;
11668dffb485Schristos 	  top = aexpr->bytes[pc++];
11678dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11688dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11698dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11708dffb485Schristos 	  break;
11718dffb485Schristos 
11728dffb485Schristos 	case gdb_agent_op_const64:
11738dffb485Schristos 	  /* Flush the cached stack top.  */
11748dffb485Schristos 	  stack[sp++] = top;
11758dffb485Schristos 	  top = aexpr->bytes[pc++];
11768dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11778dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11788dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11798dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11808dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11818dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11828dffb485Schristos 	  top = (top << 8) + aexpr->bytes[pc++];
11838dffb485Schristos 	  break;
11848dffb485Schristos 
11858dffb485Schristos 	case gdb_agent_op_reg:
11868dffb485Schristos 	  /* Flush the cached stack top.  */
11878dffb485Schristos 	  stack[sp++] = top;
11888dffb485Schristos 	  arg = aexpr->bytes[pc++];
11898dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
11908dffb485Schristos 	  {
11918dffb485Schristos 	    int regnum = arg;
11928dffb485Schristos 	    struct regcache *regcache = ctx->regcache;
11938dffb485Schristos 
11948dffb485Schristos 	    switch (register_size (regcache->tdesc, regnum))
11958dffb485Schristos 	      {
11968dffb485Schristos 	      case 8:
11978dffb485Schristos 		collect_register (regcache, regnum, cnv.u64.bytes);
11988dffb485Schristos 		top = cnv.u64.val;
11998dffb485Schristos 		break;
12008dffb485Schristos 	      case 4:
12018dffb485Schristos 		collect_register (regcache, regnum, cnv.u32.bytes);
12028dffb485Schristos 		top = cnv.u32.val;
12038dffb485Schristos 		break;
12048dffb485Schristos 	      case 2:
12058dffb485Schristos 		collect_register (regcache, regnum, cnv.u16.bytes);
12068dffb485Schristos 		top = cnv.u16.val;
12078dffb485Schristos 		break;
12088dffb485Schristos 	      case 1:
12098dffb485Schristos 		collect_register (regcache, regnum, cnv.u8.bytes);
12108dffb485Schristos 		top = cnv.u8.val;
12118dffb485Schristos 		break;
12128dffb485Schristos 	      default:
12134b169a6bSchristos 		internal_error ("unhandled register size");
12148dffb485Schristos 	      }
12158dffb485Schristos 	  }
12168dffb485Schristos 	  break;
12178dffb485Schristos 
12188dffb485Schristos 	case gdb_agent_op_end:
12198dffb485Schristos 	  ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
12208dffb485Schristos 		    sp, pulongest (top));
12218dffb485Schristos 	  if (rslt)
12228dffb485Schristos 	    {
12238dffb485Schristos 	      if (sp <= 0)
12248dffb485Schristos 		{
12258dffb485Schristos 		  /* This should be an error */
12268dffb485Schristos 		  ax_debug ("Stack is empty, nothing to return");
12278dffb485Schristos 		  return expr_eval_empty_stack;
12288dffb485Schristos 		}
12298dffb485Schristos 	      *rslt = top;
12308dffb485Schristos 	    }
12318dffb485Schristos 	  return expr_eval_no_error;
12328dffb485Schristos 
12338dffb485Schristos 	case gdb_agent_op_dup:
12348dffb485Schristos 	  stack[sp++] = top;
12358dffb485Schristos 	  break;
12368dffb485Schristos 
12378dffb485Schristos 	case gdb_agent_op_pop:
12388dffb485Schristos 	  if (--sp >= 0)
12398dffb485Schristos 	    top = stack[sp];
12408dffb485Schristos 	  break;
12418dffb485Schristos 
12428dffb485Schristos 	case gdb_agent_op_pick:
12438dffb485Schristos 	  arg = aexpr->bytes[pc++];
12448dffb485Schristos 	  stack[sp] = top;
12458dffb485Schristos 	  top = stack[sp - arg];
12468dffb485Schristos 	  ++sp;
12478dffb485Schristos 	  break;
12488dffb485Schristos 
12498dffb485Schristos 	case gdb_agent_op_rot:
12508dffb485Schristos 	  {
12518dffb485Schristos 	    ULONGEST tem = stack[sp - 1];
12528dffb485Schristos 
12538dffb485Schristos 	    stack[sp - 1] = stack[sp - 2];
12548dffb485Schristos 	    stack[sp - 2] = top;
12558dffb485Schristos 	    top = tem;
12568dffb485Schristos 	  }
12578dffb485Schristos 	  break;
12588dffb485Schristos 
12598dffb485Schristos 	case gdb_agent_op_zero_ext:
12608dffb485Schristos 	  arg = aexpr->bytes[pc++];
12618dffb485Schristos 	  if (arg < (sizeof (LONGEST) * 8))
12628dffb485Schristos 	    top &= ((LONGEST) 1 << arg) - 1;
12638dffb485Schristos 	  break;
12648dffb485Schristos 
12658dffb485Schristos 	case gdb_agent_op_swap:
12668dffb485Schristos 	  /* Interchange top two stack elements, making sure top gets
12678dffb485Schristos 	     copied back onto stack.  */
12688dffb485Schristos 	  stack[sp] = top;
12698dffb485Schristos 	  top = stack[sp - 1];
12708dffb485Schristos 	  stack[sp - 1] = stack[sp];
12718dffb485Schristos 	  break;
12728dffb485Schristos 
12738dffb485Schristos 	case gdb_agent_op_getv:
12748dffb485Schristos 	  /* Flush the cached stack top.  */
12758dffb485Schristos 	  stack[sp++] = top;
12768dffb485Schristos 	  arg = aexpr->bytes[pc++];
12778dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
12788dffb485Schristos 	  top = agent_get_trace_state_variable_value (arg);
12798dffb485Schristos 	  break;
12808dffb485Schristos 
12818dffb485Schristos 	case gdb_agent_op_setv:
12828dffb485Schristos 	  arg = aexpr->bytes[pc++];
12838dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
12848dffb485Schristos 	  agent_set_trace_state_variable_value (arg, top);
12858dffb485Schristos 	  /* Note that we leave the value on the stack, for the
12868dffb485Schristos 	     benefit of later/enclosing expressions.  */
12878dffb485Schristos 	  break;
12888dffb485Schristos 
12898dffb485Schristos 	case gdb_agent_op_tracev:
12908dffb485Schristos 	  arg = aexpr->bytes[pc++];
12918dffb485Schristos 	  arg = (arg << 8) + aexpr->bytes[pc++];
12928dffb485Schristos 	  agent_tsv_read (ctx, arg);
12938dffb485Schristos 	  break;
12948dffb485Schristos 
12958dffb485Schristos 	case gdb_agent_op_tracenz:
12968dffb485Schristos 	  agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
12978dffb485Schristos 				 (ULONGEST) top);
12988dffb485Schristos 	  if (--sp >= 0)
12998dffb485Schristos 	    top = stack[sp];
13008dffb485Schristos 	  break;
13018dffb485Schristos 
13028dffb485Schristos 	case gdb_agent_op_printf:
13038dffb485Schristos 	  {
13048dffb485Schristos 	    int nargs, slen, i;
13058dffb485Schristos 	    CORE_ADDR fn = 0, chan = 0;
13068dffb485Schristos 	    /* Can't have more args than the entire size of the stack.  */
13078dffb485Schristos 	    ULONGEST args[STACK_MAX];
13088dffb485Schristos 	    char *format;
13098dffb485Schristos 
13108dffb485Schristos 	    nargs = aexpr->bytes[pc++];
13118dffb485Schristos 	    slen = aexpr->bytes[pc++];
13128dffb485Schristos 	    slen = (slen << 8) + aexpr->bytes[pc++];
13138dffb485Schristos 	    format = (char *) &(aexpr->bytes[pc]);
13148dffb485Schristos 	    pc += slen;
13158dffb485Schristos 	    /* Pop function and channel.  */
13168dffb485Schristos 	    fn = top;
13178dffb485Schristos 	    if (--sp >= 0)
13188dffb485Schristos 	      top = stack[sp];
13198dffb485Schristos 	    chan = top;
13208dffb485Schristos 	    if (--sp >= 0)
13218dffb485Schristos 	      top = stack[sp];
13228dffb485Schristos 	    /* Pop arguments into a dedicated array.  */
13238dffb485Schristos 	    for (i = 0; i < nargs; ++i)
13248dffb485Schristos 	      {
13258dffb485Schristos 		args[i] = top;
13268dffb485Schristos 		if (--sp >= 0)
13278dffb485Schristos 		  top = stack[sp];
13288dffb485Schristos 	      }
13298dffb485Schristos 
13308dffb485Schristos 	    /* A bad format string means something is very wrong; give
13318dffb485Schristos 	       up immediately.  */
13328dffb485Schristos 	    if (format[slen - 1] != '\0')
13338dffb485Schristos 	      error (_("Unterminated format string in printf bytecode"));
13348dffb485Schristos 
13358dffb485Schristos 	    ax_printf (fn, chan, format, nargs, args);
13368dffb485Schristos 	  }
13378dffb485Schristos 	  break;
13388dffb485Schristos 
13398dffb485Schristos 	  /* GDB never (currently) generates any of these ops.  */
13408dffb485Schristos 	case gdb_agent_op_float:
13418dffb485Schristos 	case gdb_agent_op_ref_float:
13428dffb485Schristos 	case gdb_agent_op_ref_double:
13438dffb485Schristos 	case gdb_agent_op_ref_long_double:
13448dffb485Schristos 	case gdb_agent_op_l_to_d:
13458dffb485Schristos 	case gdb_agent_op_d_to_l:
13468dffb485Schristos 	case gdb_agent_op_trace16:
13478dffb485Schristos 	  ax_debug ("Agent expression op 0x%x valid, but not handled",
13488dffb485Schristos 		    op);
13498dffb485Schristos 	  /* If ever GDB generates any of these, we don't have the
13508dffb485Schristos 	     option of ignoring.  */
13518dffb485Schristos 	  return expr_eval_unhandled_opcode;
13528dffb485Schristos 
13538dffb485Schristos 	default:
13548dffb485Schristos 	  ax_debug ("Agent expression op 0x%x not recognized", op);
13558dffb485Schristos 	  /* Don't struggle on, things will just get worse.  */
13568dffb485Schristos 	  return expr_eval_unrecognized_opcode;
13578dffb485Schristos 	}
13588dffb485Schristos 
13598dffb485Schristos       /* Check for stack badness.  */
13608dffb485Schristos       if (sp >= (STACK_MAX - 1))
13618dffb485Schristos 	{
13628dffb485Schristos 	  ax_debug ("Expression stack overflow");
13638dffb485Schristos 	  return expr_eval_stack_overflow;
13648dffb485Schristos 	}
13658dffb485Schristos 
13668dffb485Schristos       if (sp < 0)
13678dffb485Schristos 	{
13688dffb485Schristos 	  ax_debug ("Expression stack underflow");
13698dffb485Schristos 	  return expr_eval_stack_underflow;
13708dffb485Schristos 	}
13718dffb485Schristos 
13728dffb485Schristos       ax_debug ("Op %s -> sp=%d, top=0x%s",
13738dffb485Schristos 		gdb_agent_op_name (op), sp, phex_nz (top, 0));
13748dffb485Schristos     }
13758dffb485Schristos }
1376