1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "tracepoint.h"
25 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
27 #ifdef IN_PROCESS_AGENT
32 ax_vdebug (const char *fmt, ...)
38 vsprintf (buf, fmt, ap);
39 fprintf (stderr, PROG "/ax: %s\n", buf);
43 #define ax_debug_1(level, fmt, args...) \
45 if (level <= debug_threads) \
46 ax_vdebug ((fmt), ##args); \
49 #define ax_debug(FMT, args...) \
50 ax_debug_1 (1, FMT, ##args)
52 /* This enum must exactly match what is documented in
53 gdb/doc/agentexpr.texi, including all the numerical values. */
57 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
58 gdb_agent_op_ ## NAME = VALUE,
64 static const char *gdb_agent_op_names [gdb_agent_op_last] =
67 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
72 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
75 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
80 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
83 gdb_agent_op_name (int op)
85 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
87 return gdb_agent_op_names[op];
90 #ifndef IN_PROCESS_AGENT
92 /* The packet form of an agent expression consists of an 'X', number
93 of bytes in expression, a comma, and then the bytes. */
96 gdb_parse_agent_expr (char **actparm)
100 struct agent_expr *aexpr;
102 ++act; /* skip the X */
103 act = unpack_varlen_hex (act, &xlen);
104 ++act; /* skip a comma */
105 aexpr = xmalloc (sizeof (struct agent_expr));
106 aexpr->length = xlen;
107 aexpr->bytes = xmalloc (xlen);
108 hex2bin (act, aexpr->bytes, xlen);
109 *actparm = act + (xlen * 2);
114 gdb_free_agent_expr (struct agent_expr *aexpr)
123 /* Convert the bytes of an agent expression back into hex digits, so
124 they can be printed or uploaded. This allocates the buffer,
125 callers should free when they are done with it. */
128 gdb_unparse_agent_expr (struct agent_expr *aexpr)
132 rslt = xmalloc (2 * aexpr->length + 1);
133 bin2hex (aexpr->bytes, rslt, aexpr->length);
137 /* Bytecode compilation. */
139 CORE_ADDR current_insn_ptr;
143 struct bytecode_address
148 /* Offset and size of field to be modified in the goto block. */
149 int from_offset, from_size;
150 struct bytecode_address *next;
151 } *bytecode_address_table;
156 target_emit_ops ()->emit_prologue ();
162 target_emit_ops ()->emit_epilogue ();
168 target_emit_ops ()->emit_add ();
174 target_emit_ops ()->emit_sub ();
180 target_emit_ops ()->emit_mul ();
186 target_emit_ops ()->emit_lsh ();
190 emit_rsh_signed (void)
192 target_emit_ops ()->emit_rsh_signed ();
196 emit_rsh_unsigned (void)
198 target_emit_ops ()->emit_rsh_unsigned ();
204 target_emit_ops ()->emit_ext (arg);
210 target_emit_ops ()->emit_log_not ();
216 target_emit_ops ()->emit_bit_and ();
222 target_emit_ops ()->emit_bit_or ();
228 target_emit_ops ()->emit_bit_xor ();
234 target_emit_ops ()->emit_bit_not ();
240 target_emit_ops ()->emit_equal ();
244 emit_less_signed (void)
246 target_emit_ops ()->emit_less_signed ();
250 emit_less_unsigned (void)
252 target_emit_ops ()->emit_less_unsigned ();
258 target_emit_ops ()->emit_ref (size);
262 emit_if_goto (int *offset_p, int *size_p)
264 target_emit_ops ()->emit_if_goto (offset_p, size_p);
268 emit_goto (int *offset_p, int *size_p)
270 target_emit_ops ()->emit_goto (offset_p, size_p);
274 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
276 target_emit_ops ()->write_goto_address (from, to, size);
280 emit_const (LONGEST num)
282 target_emit_ops ()->emit_const (num);
288 target_emit_ops ()->emit_reg (reg);
294 target_emit_ops ()->emit_pop ();
298 emit_stack_flush (void)
300 target_emit_ops ()->emit_stack_flush ();
304 emit_zero_ext (int arg)
306 target_emit_ops ()->emit_zero_ext (arg);
312 target_emit_ops ()->emit_swap ();
316 emit_stack_adjust (int n)
318 target_emit_ops ()->emit_stack_adjust (n);
321 /* FN's prototype is `LONGEST(*fn)(int)'. */
324 emit_int_call_1 (CORE_ADDR fn, int arg1)
326 target_emit_ops ()->emit_int_call_1 (fn, arg1);
329 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
332 emit_void_call_2 (CORE_ADDR fn, int arg1)
334 target_emit_ops ()->emit_void_call_2 (fn, arg1);
338 emit_eq_goto (int *offset_p, int *size_p)
340 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
344 emit_ne_goto (int *offset_p, int *size_p)
346 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
350 emit_lt_goto (int *offset_p, int *size_p)
352 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
356 emit_ge_goto (int *offset_p, int *size_p)
358 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
362 emit_gt_goto (int *offset_p, int *size_p)
364 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
368 emit_le_goto (int *offset_p, int *size_p)
370 target_emit_ops ()->emit_le_goto (offset_p, size_p);
373 /* Scan an agent expression for any evidence that the given PC is the
374 target of a jump bytecode in the expression. */
377 is_goto_target (struct agent_expr *aexpr, int pc)
382 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
384 op = aexpr->bytes[i];
386 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
388 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
397 /* Given an agent expression, turn it into native code. */
399 enum eval_result_type
400 compile_bytecodes (struct agent_expr *aexpr)
404 unsigned char op, next_op;
406 /* This is only used to build 64-bit value for constants. */
408 struct bytecode_address *aentry, *aentry2;
413 ax_debug ("Cannot compile op 0x%x\n", op); \
414 return expr_eval_unhandled_opcode; \
417 if (aexpr->length == 0)
419 ax_debug ("empty agent expression\n");
420 return expr_eval_empty_expression;
423 bytecode_address_table = NULL;
427 op = aexpr->bytes[pc];
429 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
431 /* Record the compiled-code address of the bytecode, for use by
432 jump instructions. */
433 aentry = xmalloc (sizeof (struct bytecode_address));
435 aentry->address = current_insn_ptr;
436 aentry->goto_pc = -1;
437 aentry->from_offset = aentry->from_size = 0;
438 aentry->next = bytecode_address_table;
439 bytecode_address_table = aentry;
447 case gdb_agent_op_add:
451 case gdb_agent_op_sub:
455 case gdb_agent_op_mul:
459 case gdb_agent_op_div_signed:
463 case gdb_agent_op_div_unsigned:
467 case gdb_agent_op_rem_signed:
471 case gdb_agent_op_rem_unsigned:
475 case gdb_agent_op_lsh:
479 case gdb_agent_op_rsh_signed:
483 case gdb_agent_op_rsh_unsigned:
484 emit_rsh_unsigned ();
487 case gdb_agent_op_trace:
491 case gdb_agent_op_trace_quick:
495 case gdb_agent_op_log_not:
499 case gdb_agent_op_bit_and:
503 case gdb_agent_op_bit_or:
507 case gdb_agent_op_bit_xor:
511 case gdb_agent_op_bit_not:
515 case gdb_agent_op_equal:
516 next_op = aexpr->bytes[pc];
517 if (next_op == gdb_agent_op_if_goto
518 && !is_goto_target (aexpr, pc)
519 && target_emit_ops ()->emit_eq_goto)
521 ax_debug ("Combining equal & if_goto");
524 arg = aexpr->bytes[pc++];
525 arg = (arg << 8) + aexpr->bytes[pc++];
526 aentry->goto_pc = arg;
527 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
529 else if (next_op == gdb_agent_op_log_not
530 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
531 && !is_goto_target (aexpr, pc + 1)
532 && target_emit_ops ()->emit_ne_goto)
534 ax_debug ("Combining equal & log_not & if_goto");
537 arg = aexpr->bytes[pc++];
538 arg = (arg << 8) + aexpr->bytes[pc++];
539 aentry->goto_pc = arg;
540 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
546 case gdb_agent_op_less_signed:
547 next_op = aexpr->bytes[pc];
548 if (next_op == gdb_agent_op_if_goto
549 && !is_goto_target (aexpr, pc))
551 ax_debug ("Combining less_signed & if_goto");
554 arg = aexpr->bytes[pc++];
555 arg = (arg << 8) + aexpr->bytes[pc++];
556 aentry->goto_pc = arg;
557 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
559 else if (next_op == gdb_agent_op_log_not
560 && !is_goto_target (aexpr, pc)
561 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
562 && !is_goto_target (aexpr, pc + 1))
564 ax_debug ("Combining less_signed & log_not & if_goto");
567 arg = aexpr->bytes[pc++];
568 arg = (arg << 8) + aexpr->bytes[pc++];
569 aentry->goto_pc = arg;
570 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
576 case gdb_agent_op_less_unsigned:
577 emit_less_unsigned ();
580 case gdb_agent_op_ext:
581 arg = aexpr->bytes[pc++];
582 if (arg < (sizeof (LONGEST) * 8))
586 case gdb_agent_op_ref8:
590 case gdb_agent_op_ref16:
594 case gdb_agent_op_ref32:
598 case gdb_agent_op_ref64:
602 case gdb_agent_op_if_goto:
603 arg = aexpr->bytes[pc++];
604 arg = (arg << 8) + aexpr->bytes[pc++];
605 aentry->goto_pc = arg;
606 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
609 case gdb_agent_op_goto:
610 arg = aexpr->bytes[pc++];
611 arg = (arg << 8) + aexpr->bytes[pc++];
612 aentry->goto_pc = arg;
613 emit_goto (&(aentry->from_offset), &(aentry->from_size));
616 case gdb_agent_op_const8:
618 top = aexpr->bytes[pc++];
622 case gdb_agent_op_const16:
624 top = aexpr->bytes[pc++];
625 top = (top << 8) + aexpr->bytes[pc++];
629 case gdb_agent_op_const32:
631 top = aexpr->bytes[pc++];
632 top = (top << 8) + aexpr->bytes[pc++];
633 top = (top << 8) + aexpr->bytes[pc++];
634 top = (top << 8) + aexpr->bytes[pc++];
638 case gdb_agent_op_const64:
640 top = aexpr->bytes[pc++];
641 top = (top << 8) + aexpr->bytes[pc++];
642 top = (top << 8) + aexpr->bytes[pc++];
643 top = (top << 8) + aexpr->bytes[pc++];
644 top = (top << 8) + aexpr->bytes[pc++];
645 top = (top << 8) + aexpr->bytes[pc++];
646 top = (top << 8) + aexpr->bytes[pc++];
647 top = (top << 8) + aexpr->bytes[pc++];
651 case gdb_agent_op_reg:
653 arg = aexpr->bytes[pc++];
654 arg = (arg << 8) + aexpr->bytes[pc++];
658 case gdb_agent_op_end:
659 ax_debug ("At end of expression\n");
661 /* Assume there is one stack element left, and that it is
662 cached in "top" where emit_epilogue can get to it. */
663 emit_stack_adjust (1);
668 case gdb_agent_op_dup:
669 /* In our design, dup is equivalent to stack flushing. */
673 case gdb_agent_op_pop:
677 case gdb_agent_op_zero_ext:
678 arg = aexpr->bytes[pc++];
679 if (arg < (sizeof (LONGEST) * 8))
683 case gdb_agent_op_swap:
684 next_op = aexpr->bytes[pc];
685 /* Detect greater-than comparison sequences. */
686 if (next_op == gdb_agent_op_less_signed
687 && !is_goto_target (aexpr, pc)
688 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
689 && !is_goto_target (aexpr, pc + 1))
691 ax_debug ("Combining swap & less_signed & if_goto");
694 arg = aexpr->bytes[pc++];
695 arg = (arg << 8) + aexpr->bytes[pc++];
696 aentry->goto_pc = arg;
697 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
699 else if (next_op == gdb_agent_op_less_signed
700 && !is_goto_target (aexpr, pc)
701 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
702 && !is_goto_target (aexpr, pc + 1)
703 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
704 && !is_goto_target (aexpr, pc + 2))
706 ax_debug ("Combining swap & less_signed & log_not & if_goto");
709 arg = aexpr->bytes[pc++];
710 arg = (arg << 8) + aexpr->bytes[pc++];
711 aentry->goto_pc = arg;
712 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
718 case gdb_agent_op_getv:
720 arg = aexpr->bytes[pc++];
721 arg = (arg << 8) + aexpr->bytes[pc++];
722 emit_int_call_1 (get_get_tsv_func_addr (),
726 case gdb_agent_op_setv:
727 arg = aexpr->bytes[pc++];
728 arg = (arg << 8) + aexpr->bytes[pc++];
729 emit_void_call_2 (get_set_tsv_func_addr (),
733 case gdb_agent_op_tracev:
737 /* GDB never (currently) generates any of these ops. */
738 case gdb_agent_op_float:
739 case gdb_agent_op_ref_float:
740 case gdb_agent_op_ref_double:
741 case gdb_agent_op_ref_long_double:
742 case gdb_agent_op_l_to_d:
743 case gdb_agent_op_d_to_l:
744 case gdb_agent_op_trace16:
749 ax_debug ("Agent expression op 0x%x not recognized\n", op);
750 /* Don't struggle on, things will just get worse. */
751 return expr_eval_unrecognized_opcode;
754 /* This catches errors that occur in target-specific code
758 ax_debug ("Error %d while emitting code for %s\n",
759 emit_error, gdb_agent_op_name (op));
760 return expr_eval_unhandled_opcode;
763 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
766 /* Now fill in real addresses as goto destinations. */
767 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
771 if (aentry->goto_pc < 0)
774 /* Find the location that we are going to, and call back into
775 target-specific code to write the actual address or
777 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
779 if (aentry2->pc == aentry->goto_pc)
781 ax_debug ("Want to jump from %s to %s\n",
782 paddress (aentry->address),
783 paddress (aentry2->address));
784 write_goto_address (aentry->address + aentry->from_offset,
785 aentry2->address, aentry->from_size);
791 /* Error out if we didn't find a destination. */
794 ax_debug ("Destination of goto %d not found\n",
796 return expr_eval_invalid_goto;
800 return expr_eval_no_error;
805 /* Make printf-type calls using arguments supplied from the host. We
806 need to parse the format string ourselves, and call the formatting
807 function with one argument at a time, partly because there is no
808 safe portable way to construct a varargs call, and partly to serve
809 as a security barrier against bad format strings that might get
813 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
814 int nargs, ULONGEST *args)
816 const char *f = format;
817 struct format_piece *fpieces;
819 char *current_substring;
822 ax_debug ("Printf of \"%s\" with %d args", format, nargs);
824 fpieces = parse_format_string (&f);
827 for (fp = 0; fpieces[fp].string != NULL; fp++)
828 if (fpieces[fp].argclass != literal_piece)
831 if (nargs != nargs_wanted)
832 error (_("Wrong number of arguments for specified format-string"));
835 for (fp = 0; fpieces[fp].string != NULL; fp++)
837 current_substring = fpieces[fp].string;
838 ax_debug ("current substring is '%s', class is %d",
839 current_substring, fpieces[fp].argclass);
840 switch (fpieces[fp].argclass)
850 /* This is a %s argument. Find the length of the string. */
855 read_inferior_memory (tem + j, &c, 1);
860 /* Copy the string contents into a string inside GDB. */
861 str = (gdb_byte *) alloca (j + 1);
863 read_inferior_memory (tem, str, j);
866 printf (current_substring, (char *) str);
871 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
873 long long val = args[i];
875 printf (current_substring, val);
879 error (_("long long not supported in agent printf"));
885 printf (current_substring, val);
893 printf (current_substring, val);
898 /* Print a portion of the format string that has no
899 directives. Note that this will not include any
900 ordinary %-specs, but it might include "%%". That is
901 why we use printf_filtered and not puts_filtered here.
902 Also, we pass a dummy argument because some platforms
903 have modified GCC to include -Wformat-security by
904 default, which will warn here if there is no
906 printf (current_substring, 0);
910 error (_("Format directive in '%s' not supported in agent printf"),
914 /* Maybe advance to the next argument. */
915 if (fpieces[fp].argclass != literal_piece)
919 free_format_pieces (fpieces);
923 /* The agent expression evaluator, as specified by the GDB docs. It
924 returns 0 if everything went OK, and a nonzero error code
927 enum eval_result_type
928 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
929 struct agent_expr *aexpr,
933 #define STACK_MAX 100
934 ULONGEST stack[STACK_MAX], top;
939 /* This union is a convenient way to convert representations. For
940 now, assume a standard architecture where the hardware integer
941 types have 8, 16, 32, 64 bit types. A more robust solution would
942 be to import stdint.h from gnulib. */
947 unsigned char bytes[1];
952 unsigned char bytes[2];
957 unsigned char bytes[4];
962 unsigned char bytes[8];
967 if (aexpr->length == 0)
969 ax_debug ("empty agent expression");
970 return expr_eval_empty_expression;
973 /* Cache the stack top in its own variable. Much of the time we can
974 operate on this variable, rather than dinking with the stack. It
975 needs to be copied to the stack when sp changes. */
980 op = aexpr->bytes[pc++];
982 ax_debug ("About to interpret byte 0x%x", op);
986 case gdb_agent_op_add:
990 case gdb_agent_op_sub:
991 top = stack[--sp] - top;
994 case gdb_agent_op_mul:
998 case gdb_agent_op_div_signed:
1001 ax_debug ("Attempted to divide by zero");
1002 return expr_eval_divide_by_zero;
1004 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
1007 case gdb_agent_op_div_unsigned:
1010 ax_debug ("Attempted to divide by zero");
1011 return expr_eval_divide_by_zero;
1013 top = stack[--sp] / top;
1016 case gdb_agent_op_rem_signed:
1019 ax_debug ("Attempted to divide by zero");
1020 return expr_eval_divide_by_zero;
1022 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1025 case gdb_agent_op_rem_unsigned:
1028 ax_debug ("Attempted to divide by zero");
1029 return expr_eval_divide_by_zero;
1031 top = stack[--sp] % top;
1034 case gdb_agent_op_lsh:
1035 top = stack[--sp] << top;
1038 case gdb_agent_op_rsh_signed:
1039 top = ((LONGEST) stack[--sp]) >> top;
1042 case gdb_agent_op_rsh_unsigned:
1043 top = stack[--sp] >> top;
1046 case gdb_agent_op_trace:
1047 agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
1053 case gdb_agent_op_trace_quick:
1054 arg = aexpr->bytes[pc++];
1055 agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1058 case gdb_agent_op_log_not:
1062 case gdb_agent_op_bit_and:
1066 case gdb_agent_op_bit_or:
1070 case gdb_agent_op_bit_xor:
1074 case gdb_agent_op_bit_not:
1078 case gdb_agent_op_equal:
1079 top = (stack[--sp] == top);
1082 case gdb_agent_op_less_signed:
1083 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1086 case gdb_agent_op_less_unsigned:
1087 top = (stack[--sp] < top);
1090 case gdb_agent_op_ext:
1091 arg = aexpr->bytes[pc++];
1092 if (arg < (sizeof (LONGEST) * 8))
1094 LONGEST mask = 1 << (arg - 1);
1095 top &= ((LONGEST) 1 << arg) - 1;
1096 top = (top ^ mask) - mask;
1100 case gdb_agent_op_ref8:
1101 agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
1105 case gdb_agent_op_ref16:
1106 agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
1110 case gdb_agent_op_ref32:
1111 agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
1115 case gdb_agent_op_ref64:
1116 agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
1120 case gdb_agent_op_if_goto:
1122 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1129 case gdb_agent_op_goto:
1130 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1133 case gdb_agent_op_const8:
1134 /* Flush the cached stack top. */
1136 top = aexpr->bytes[pc++];
1139 case gdb_agent_op_const16:
1140 /* Flush the cached stack top. */
1142 top = aexpr->bytes[pc++];
1143 top = (top << 8) + aexpr->bytes[pc++];
1146 case gdb_agent_op_const32:
1147 /* Flush the cached stack top. */
1149 top = aexpr->bytes[pc++];
1150 top = (top << 8) + aexpr->bytes[pc++];
1151 top = (top << 8) + aexpr->bytes[pc++];
1152 top = (top << 8) + aexpr->bytes[pc++];
1155 case gdb_agent_op_const64:
1156 /* Flush the cached stack top. */
1158 top = aexpr->bytes[pc++];
1159 top = (top << 8) + aexpr->bytes[pc++];
1160 top = (top << 8) + aexpr->bytes[pc++];
1161 top = (top << 8) + aexpr->bytes[pc++];
1162 top = (top << 8) + aexpr->bytes[pc++];
1163 top = (top << 8) + aexpr->bytes[pc++];
1164 top = (top << 8) + aexpr->bytes[pc++];
1165 top = (top << 8) + aexpr->bytes[pc++];
1168 case gdb_agent_op_reg:
1169 /* Flush the cached stack top. */
1171 arg = aexpr->bytes[pc++];
1172 arg = (arg << 8) + aexpr->bytes[pc++];
1175 struct regcache *regcache = ctx->regcache;
1177 switch (register_size (regcache->tdesc, regnum))
1180 collect_register (regcache, regnum, cnv.u64.bytes);
1184 collect_register (regcache, regnum, cnv.u32.bytes);
1188 collect_register (regcache, regnum, cnv.u16.bytes);
1192 collect_register (regcache, regnum, cnv.u8.bytes);
1196 internal_error (__FILE__, __LINE__,
1197 "unhandled register size");
1202 case gdb_agent_op_end:
1203 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1204 sp, pulongest (top));
1209 /* This should be an error */
1210 ax_debug ("Stack is empty, nothing to return");
1211 return expr_eval_empty_stack;
1215 return expr_eval_no_error;
1217 case gdb_agent_op_dup:
1221 case gdb_agent_op_pop:
1226 case gdb_agent_op_pick:
1227 arg = aexpr->bytes[pc++];
1229 top = stack[sp - arg];
1233 case gdb_agent_op_rot:
1235 ULONGEST tem = stack[sp - 1];
1237 stack[sp - 1] = stack[sp - 2];
1238 stack[sp - 2] = top;
1243 case gdb_agent_op_zero_ext:
1244 arg = aexpr->bytes[pc++];
1245 if (arg < (sizeof (LONGEST) * 8))
1246 top &= ((LONGEST) 1 << arg) - 1;
1249 case gdb_agent_op_swap:
1250 /* Interchange top two stack elements, making sure top gets
1251 copied back onto stack. */
1253 top = stack[sp - 1];
1254 stack[sp - 1] = stack[sp];
1257 case gdb_agent_op_getv:
1258 /* Flush the cached stack top. */
1260 arg = aexpr->bytes[pc++];
1261 arg = (arg << 8) + aexpr->bytes[pc++];
1262 top = agent_get_trace_state_variable_value (arg);
1265 case gdb_agent_op_setv:
1266 arg = aexpr->bytes[pc++];
1267 arg = (arg << 8) + aexpr->bytes[pc++];
1268 agent_set_trace_state_variable_value (arg, top);
1269 /* Note that we leave the value on the stack, for the
1270 benefit of later/enclosing expressions. */
1273 case gdb_agent_op_tracev:
1274 arg = aexpr->bytes[pc++];
1275 arg = (arg << 8) + aexpr->bytes[pc++];
1276 agent_tsv_read (ctx, arg);
1279 case gdb_agent_op_tracenz:
1280 agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
1286 case gdb_agent_op_printf:
1289 CORE_ADDR fn = 0, chan = 0;
1290 /* Can't have more args than the entire size of the stack. */
1291 ULONGEST args[STACK_MAX];
1294 nargs = aexpr->bytes[pc++];
1295 slen = aexpr->bytes[pc++];
1296 slen = (slen << 8) + aexpr->bytes[pc++];
1297 format = (char *) &(aexpr->bytes[pc]);
1299 /* Pop function and channel. */
1306 /* Pop arguments into a dedicated array. */
1307 for (i = 0; i < nargs; ++i)
1314 /* A bad format string means something is very wrong; give
1316 if (format[slen - 1] != '\0')
1317 error (_("Unterminated format string in printf bytecode"));
1319 ax_printf (fn, chan, format, nargs, args);
1323 /* GDB never (currently) generates any of these ops. */
1324 case gdb_agent_op_float:
1325 case gdb_agent_op_ref_float:
1326 case gdb_agent_op_ref_double:
1327 case gdb_agent_op_ref_long_double:
1328 case gdb_agent_op_l_to_d:
1329 case gdb_agent_op_d_to_l:
1330 case gdb_agent_op_trace16:
1331 ax_debug ("Agent expression op 0x%x valid, but not handled",
1333 /* If ever GDB generates any of these, we don't have the
1334 option of ignoring. */
1338 ax_debug ("Agent expression op 0x%x not recognized", op);
1339 /* Don't struggle on, things will just get worse. */
1340 return expr_eval_unrecognized_opcode;
1343 /* Check for stack badness. */
1344 if (sp >= (STACK_MAX - 1))
1346 ax_debug ("Expression stack overflow");
1347 return expr_eval_stack_overflow;
1352 ax_debug ("Expression stack underflow");
1353 return expr_eval_stack_underflow;
1356 ax_debug ("Op %s -> sp=%d, top=0x%s",
1357 gdb_agent_op_name (op), sp, phex_nz (top, 0));