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