1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2013 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/>. */
23 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
25 #ifdef IN_PROCESS_AGENT
30 ax_vdebug (const char *fmt, ...)
36 vsprintf (buf, fmt, ap);
37 fprintf (stderr, PROG "/ax: %s\n", buf);
41 #define ax_debug_1(level, fmt, args...) \
43 if (level <= debug_threads) \
44 ax_vdebug ((fmt), ##args); \
47 #define ax_debug(FMT, args...) \
48 ax_debug_1 (1, FMT, ##args)
50 /* This enum must exactly match what is documented in
51 gdb/doc/agentexpr.texi, including all the numerical values. */
55 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
56 gdb_agent_op_ ## NAME = VALUE,
62 static const char *gdb_agent_op_names [gdb_agent_op_last] =
65 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
70 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
73 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
78 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
81 gdb_agent_op_name (int op)
83 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
85 return gdb_agent_op_names[op];
88 #ifndef IN_PROCESS_AGENT
90 /* The packet form of an agent expression consists of an 'X', number
91 of bytes in expression, a comma, and then the bytes. */
94 gdb_parse_agent_expr (char **actparm)
98 struct agent_expr *aexpr;
100 ++act; /* skip the X */
101 act = unpack_varlen_hex (act, &xlen);
102 ++act; /* skip a comma */
103 aexpr = xmalloc (sizeof (struct agent_expr));
104 aexpr->length = xlen;
105 aexpr->bytes = xmalloc (xlen);
106 convert_ascii_to_int (act, aexpr->bytes, xlen);
107 *actparm = act + (xlen * 2);
111 /* Convert the bytes of an agent expression back into hex digits, so
112 they can be printed or uploaded. This allocates the buffer,
113 callers should free when they are done with it. */
116 gdb_unparse_agent_expr (struct agent_expr *aexpr)
120 rslt = xmalloc (2 * aexpr->length + 1);
121 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
125 /* Bytecode compilation. */
127 CORE_ADDR current_insn_ptr;
131 struct bytecode_address
136 /* Offset and size of field to be modified in the goto block. */
137 int from_offset, from_size;
138 struct bytecode_address *next;
139 } *bytecode_address_table;
144 target_emit_ops ()->emit_prologue ();
150 target_emit_ops ()->emit_epilogue ();
156 target_emit_ops ()->emit_add ();
162 target_emit_ops ()->emit_sub ();
168 target_emit_ops ()->emit_mul ();
174 target_emit_ops ()->emit_lsh ();
178 emit_rsh_signed (void)
180 target_emit_ops ()->emit_rsh_signed ();
184 emit_rsh_unsigned (void)
186 target_emit_ops ()->emit_rsh_unsigned ();
192 target_emit_ops ()->emit_ext (arg);
198 target_emit_ops ()->emit_log_not ();
204 target_emit_ops ()->emit_bit_and ();
210 target_emit_ops ()->emit_bit_or ();
216 target_emit_ops ()->emit_bit_xor ();
222 target_emit_ops ()->emit_bit_not ();
228 target_emit_ops ()->emit_equal ();
232 emit_less_signed (void)
234 target_emit_ops ()->emit_less_signed ();
238 emit_less_unsigned (void)
240 target_emit_ops ()->emit_less_unsigned ();
246 target_emit_ops ()->emit_ref (size);
250 emit_if_goto (int *offset_p, int *size_p)
252 target_emit_ops ()->emit_if_goto (offset_p, size_p);
256 emit_goto (int *offset_p, int *size_p)
258 target_emit_ops ()->emit_goto (offset_p, size_p);
262 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
264 target_emit_ops ()->write_goto_address (from, to, size);
268 emit_const (LONGEST num)
270 target_emit_ops ()->emit_const (num);
276 target_emit_ops ()->emit_reg (reg);
282 target_emit_ops ()->emit_pop ();
286 emit_stack_flush (void)
288 target_emit_ops ()->emit_stack_flush ();
292 emit_zero_ext (int arg)
294 target_emit_ops ()->emit_zero_ext (arg);
300 target_emit_ops ()->emit_swap ();
304 emit_stack_adjust (int n)
306 target_emit_ops ()->emit_stack_adjust (n);
309 /* FN's prototype is `LONGEST(*fn)(int)'. */
312 emit_int_call_1 (CORE_ADDR fn, int arg1)
314 target_emit_ops ()->emit_int_call_1 (fn, arg1);
317 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
320 emit_void_call_2 (CORE_ADDR fn, int arg1)
322 target_emit_ops ()->emit_void_call_2 (fn, arg1);
326 emit_eq_goto (int *offset_p, int *size_p)
328 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
332 emit_ne_goto (int *offset_p, int *size_p)
334 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
338 emit_lt_goto (int *offset_p, int *size_p)
340 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
344 emit_ge_goto (int *offset_p, int *size_p)
346 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
350 emit_gt_goto (int *offset_p, int *size_p)
352 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
356 emit_le_goto (int *offset_p, int *size_p)
358 target_emit_ops ()->emit_le_goto (offset_p, size_p);
361 /* Scan an agent expression for any evidence that the given PC is the
362 target of a jump bytecode in the expression. */
365 is_goto_target (struct agent_expr *aexpr, int pc)
370 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
372 op = aexpr->bytes[i];
374 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
376 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
385 /* Given an agent expression, turn it into native code. */
387 enum eval_result_type
388 compile_bytecodes (struct agent_expr *aexpr)
392 unsigned char op, next_op;
394 /* This is only used to build 64-bit value for constants. */
396 struct bytecode_address *aentry, *aentry2;
401 ax_debug ("Cannot compile op 0x%x\n", op); \
402 return expr_eval_unhandled_opcode; \
405 if (aexpr->length == 0)
407 ax_debug ("empty agent expression\n");
408 return expr_eval_empty_expression;
411 bytecode_address_table = NULL;
415 op = aexpr->bytes[pc];
417 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
419 /* Record the compiled-code address of the bytecode, for use by
420 jump instructions. */
421 aentry = xmalloc (sizeof (struct bytecode_address));
423 aentry->address = current_insn_ptr;
424 aentry->goto_pc = -1;
425 aentry->from_offset = aentry->from_size = 0;
426 aentry->next = bytecode_address_table;
427 bytecode_address_table = aentry;
435 case gdb_agent_op_add:
439 case gdb_agent_op_sub:
443 case gdb_agent_op_mul:
447 case gdb_agent_op_div_signed:
451 case gdb_agent_op_div_unsigned:
455 case gdb_agent_op_rem_signed:
459 case gdb_agent_op_rem_unsigned:
463 case gdb_agent_op_lsh:
467 case gdb_agent_op_rsh_signed:
471 case gdb_agent_op_rsh_unsigned:
472 emit_rsh_unsigned ();
475 case gdb_agent_op_trace:
479 case gdb_agent_op_trace_quick:
483 case gdb_agent_op_log_not:
487 case gdb_agent_op_bit_and:
491 case gdb_agent_op_bit_or:
495 case gdb_agent_op_bit_xor:
499 case gdb_agent_op_bit_not:
503 case gdb_agent_op_equal:
504 next_op = aexpr->bytes[pc];
505 if (next_op == gdb_agent_op_if_goto
506 && !is_goto_target (aexpr, pc)
507 && target_emit_ops ()->emit_eq_goto)
509 ax_debug ("Combining equal & if_goto");
512 arg = aexpr->bytes[pc++];
513 arg = (arg << 8) + aexpr->bytes[pc++];
514 aentry->goto_pc = arg;
515 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
517 else if (next_op == gdb_agent_op_log_not
518 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
519 && !is_goto_target (aexpr, pc + 1)
520 && target_emit_ops ()->emit_ne_goto)
522 ax_debug ("Combining equal & log_not & if_goto");
525 arg = aexpr->bytes[pc++];
526 arg = (arg << 8) + aexpr->bytes[pc++];
527 aentry->goto_pc = arg;
528 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
534 case gdb_agent_op_less_signed:
535 next_op = aexpr->bytes[pc];
536 if (next_op == gdb_agent_op_if_goto
537 && !is_goto_target (aexpr, pc))
539 ax_debug ("Combining less_signed & if_goto");
542 arg = aexpr->bytes[pc++];
543 arg = (arg << 8) + aexpr->bytes[pc++];
544 aentry->goto_pc = arg;
545 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
547 else if (next_op == gdb_agent_op_log_not
548 && !is_goto_target (aexpr, pc)
549 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
550 && !is_goto_target (aexpr, pc + 1))
552 ax_debug ("Combining less_signed & log_not & if_goto");
555 arg = aexpr->bytes[pc++];
556 arg = (arg << 8) + aexpr->bytes[pc++];
557 aentry->goto_pc = arg;
558 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
564 case gdb_agent_op_less_unsigned:
565 emit_less_unsigned ();
568 case gdb_agent_op_ext:
569 arg = aexpr->bytes[pc++];
570 if (arg < (sizeof (LONGEST) * 8))
574 case gdb_agent_op_ref8:
578 case gdb_agent_op_ref16:
582 case gdb_agent_op_ref32:
586 case gdb_agent_op_ref64:
590 case gdb_agent_op_if_goto:
591 arg = aexpr->bytes[pc++];
592 arg = (arg << 8) + aexpr->bytes[pc++];
593 aentry->goto_pc = arg;
594 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
597 case gdb_agent_op_goto:
598 arg = aexpr->bytes[pc++];
599 arg = (arg << 8) + aexpr->bytes[pc++];
600 aentry->goto_pc = arg;
601 emit_goto (&(aentry->from_offset), &(aentry->from_size));
604 case gdb_agent_op_const8:
606 top = aexpr->bytes[pc++];
610 case gdb_agent_op_const16:
612 top = aexpr->bytes[pc++];
613 top = (top << 8) + aexpr->bytes[pc++];
617 case gdb_agent_op_const32:
619 top = aexpr->bytes[pc++];
620 top = (top << 8) + aexpr->bytes[pc++];
621 top = (top << 8) + aexpr->bytes[pc++];
622 top = (top << 8) + aexpr->bytes[pc++];
626 case gdb_agent_op_const64:
628 top = aexpr->bytes[pc++];
629 top = (top << 8) + aexpr->bytes[pc++];
630 top = (top << 8) + aexpr->bytes[pc++];
631 top = (top << 8) + aexpr->bytes[pc++];
632 top = (top << 8) + aexpr->bytes[pc++];
633 top = (top << 8) + aexpr->bytes[pc++];
634 top = (top << 8) + aexpr->bytes[pc++];
635 top = (top << 8) + aexpr->bytes[pc++];
639 case gdb_agent_op_reg:
641 arg = aexpr->bytes[pc++];
642 arg = (arg << 8) + aexpr->bytes[pc++];
646 case gdb_agent_op_end:
647 ax_debug ("At end of expression\n");
649 /* Assume there is one stack element left, and that it is
650 cached in "top" where emit_epilogue can get to it. */
651 emit_stack_adjust (1);
656 case gdb_agent_op_dup:
657 /* In our design, dup is equivalent to stack flushing. */
661 case gdb_agent_op_pop:
665 case gdb_agent_op_zero_ext:
666 arg = aexpr->bytes[pc++];
667 if (arg < (sizeof (LONGEST) * 8))
671 case gdb_agent_op_swap:
672 next_op = aexpr->bytes[pc];
673 /* Detect greater-than comparison sequences. */
674 if (next_op == gdb_agent_op_less_signed
675 && !is_goto_target (aexpr, pc)
676 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
677 && !is_goto_target (aexpr, pc + 1))
679 ax_debug ("Combining swap & less_signed & if_goto");
682 arg = aexpr->bytes[pc++];
683 arg = (arg << 8) + aexpr->bytes[pc++];
684 aentry->goto_pc = arg;
685 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
687 else if (next_op == gdb_agent_op_less_signed
688 && !is_goto_target (aexpr, pc)
689 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
690 && !is_goto_target (aexpr, pc + 1)
691 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
692 && !is_goto_target (aexpr, pc + 2))
694 ax_debug ("Combining swap & less_signed & log_not & if_goto");
697 arg = aexpr->bytes[pc++];
698 arg = (arg << 8) + aexpr->bytes[pc++];
699 aentry->goto_pc = arg;
700 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
706 case gdb_agent_op_getv:
708 arg = aexpr->bytes[pc++];
709 arg = (arg << 8) + aexpr->bytes[pc++];
710 emit_int_call_1 (get_get_tsv_func_addr (),
714 case gdb_agent_op_setv:
715 arg = aexpr->bytes[pc++];
716 arg = (arg << 8) + aexpr->bytes[pc++];
717 emit_void_call_2 (get_set_tsv_func_addr (),
721 case gdb_agent_op_tracev:
725 /* GDB never (currently) generates any of these ops. */
726 case gdb_agent_op_float:
727 case gdb_agent_op_ref_float:
728 case gdb_agent_op_ref_double:
729 case gdb_agent_op_ref_long_double:
730 case gdb_agent_op_l_to_d:
731 case gdb_agent_op_d_to_l:
732 case gdb_agent_op_trace16:
737 ax_debug ("Agent expression op 0x%x not recognized\n", op);
738 /* Don't struggle on, things will just get worse. */
739 return expr_eval_unrecognized_opcode;
742 /* This catches errors that occur in target-specific code
746 ax_debug ("Error %d while emitting code for %s\n",
747 emit_error, gdb_agent_op_name (op));
748 return expr_eval_unhandled_opcode;
751 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
754 /* Now fill in real addresses as goto destinations. */
755 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
759 if (aentry->goto_pc < 0)
762 /* Find the location that we are going to, and call back into
763 target-specific code to write the actual address or
765 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
767 if (aentry2->pc == aentry->goto_pc)
769 ax_debug ("Want to jump from %s to %s\n",
770 paddress (aentry->address),
771 paddress (aentry2->address));
772 write_goto_address (aentry->address + aentry->from_offset,
773 aentry2->address, aentry->from_size);
779 /* Error out if we didn't find a destination. */
782 ax_debug ("Destination of goto %d not found\n",
784 return expr_eval_invalid_goto;
788 return expr_eval_no_error;
793 /* Make printf-type calls using arguments supplied from the host. We
794 need to parse the format string ourselves, and call the formatting
795 function with one argument at a time, partly because there is no
796 safe portable way to construct a varargs call, and partly to serve
797 as a security barrier against bad format strings that might get
801 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
802 int nargs, ULONGEST *args)
804 const char *f = format;
805 struct format_piece *fpieces;
807 char *current_substring;
810 ax_debug ("Printf of \"%s\" with %d args", format, nargs);
812 fpieces = parse_format_string (&f);
815 for (fp = 0; fpieces[fp].string != NULL; fp++)
816 if (fpieces[fp].argclass != literal_piece)
819 if (nargs != nargs_wanted)
820 error (_("Wrong number of arguments for specified format-string"));
823 for (fp = 0; fpieces[fp].string != NULL; fp++)
825 current_substring = fpieces[fp].string;
826 ax_debug ("current substring is '%s', class is %d",
827 current_substring, fpieces[fp].argclass);
828 switch (fpieces[fp].argclass)
838 /* This is a %s argument. Find the length of the string. */
843 read_inferior_memory (tem + j, &c, 1);
848 /* Copy the string contents into a string inside GDB. */
849 str = (gdb_byte *) alloca (j + 1);
851 read_inferior_memory (tem, str, j);
854 printf (current_substring, (char *) str);
859 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
861 long long val = args[i];
863 printf (current_substring, val);
867 error (_("long long not supported in agent printf"));
873 printf (current_substring, val);
881 printf (current_substring, val);
886 /* Print a portion of the format string that has no
887 directives. Note that this will not include any
888 ordinary %-specs, but it might include "%%". That is
889 why we use printf_filtered and not puts_filtered here.
890 Also, we pass a dummy argument because some platforms
891 have modified GCC to include -Wformat-security by
892 default, which will warn here if there is no
894 printf (current_substring, 0);
898 error (_("Format directive in '%s' not supported in agent printf"),
902 /* Maybe advance to the next argument. */
903 if (fpieces[fp].argclass != literal_piece)
907 free_format_pieces (fpieces);
910 /* The agent expression evaluator, as specified by the GDB docs. It
911 returns 0 if everything went OK, and a nonzero error code
914 enum eval_result_type
915 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
916 struct agent_expr *aexpr,
920 #define STACK_MAX 100
921 ULONGEST stack[STACK_MAX], top;
926 /* This union is a convenient way to convert representations. For
927 now, assume a standard architecture where the hardware integer
928 types have 8, 16, 32, 64 bit types. A more robust solution would
929 be to import stdint.h from gnulib. */
934 unsigned char bytes[1];
939 unsigned char bytes[2];
944 unsigned char bytes[4];
949 unsigned char bytes[8];
954 if (aexpr->length == 0)
956 ax_debug ("empty agent expression");
957 return expr_eval_empty_expression;
960 /* Cache the stack top in its own variable. Much of the time we can
961 operate on this variable, rather than dinking with the stack. It
962 needs to be copied to the stack when sp changes. */
967 op = aexpr->bytes[pc++];
969 ax_debug ("About to interpret byte 0x%x", op);
973 case gdb_agent_op_add:
977 case gdb_agent_op_sub:
978 top = stack[--sp] - top;
981 case gdb_agent_op_mul:
985 case gdb_agent_op_div_signed:
988 ax_debug ("Attempted to divide by zero");
989 return expr_eval_divide_by_zero;
991 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
994 case gdb_agent_op_div_unsigned:
997 ax_debug ("Attempted to divide by zero");
998 return expr_eval_divide_by_zero;
1000 top = stack[--sp] / top;
1003 case gdb_agent_op_rem_signed:
1006 ax_debug ("Attempted to divide by zero");
1007 return expr_eval_divide_by_zero;
1009 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1012 case gdb_agent_op_rem_unsigned:
1015 ax_debug ("Attempted to divide by zero");
1016 return expr_eval_divide_by_zero;
1018 top = stack[--sp] % top;
1021 case gdb_agent_op_lsh:
1022 top = stack[--sp] << top;
1025 case gdb_agent_op_rsh_signed:
1026 top = ((LONGEST) stack[--sp]) >> top;
1029 case gdb_agent_op_rsh_unsigned:
1030 top = stack[--sp] >> top;
1033 case gdb_agent_op_trace:
1034 agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
1040 case gdb_agent_op_trace_quick:
1041 arg = aexpr->bytes[pc++];
1042 agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1045 case gdb_agent_op_log_not:
1049 case gdb_agent_op_bit_and:
1053 case gdb_agent_op_bit_or:
1057 case gdb_agent_op_bit_xor:
1061 case gdb_agent_op_bit_not:
1065 case gdb_agent_op_equal:
1066 top = (stack[--sp] == top);
1069 case gdb_agent_op_less_signed:
1070 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1073 case gdb_agent_op_less_unsigned:
1074 top = (stack[--sp] < top);
1077 case gdb_agent_op_ext:
1078 arg = aexpr->bytes[pc++];
1079 if (arg < (sizeof (LONGEST) * 8))
1081 LONGEST mask = 1 << (arg - 1);
1082 top &= ((LONGEST) 1 << arg) - 1;
1083 top = (top ^ mask) - mask;
1087 case gdb_agent_op_ref8:
1088 agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
1092 case gdb_agent_op_ref16:
1093 agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
1097 case gdb_agent_op_ref32:
1098 agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
1102 case gdb_agent_op_ref64:
1103 agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
1107 case gdb_agent_op_if_goto:
1109 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1116 case gdb_agent_op_goto:
1117 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1120 case gdb_agent_op_const8:
1121 /* Flush the cached stack top. */
1123 top = aexpr->bytes[pc++];
1126 case gdb_agent_op_const16:
1127 /* Flush the cached stack top. */
1129 top = aexpr->bytes[pc++];
1130 top = (top << 8) + aexpr->bytes[pc++];
1133 case gdb_agent_op_const32:
1134 /* Flush the cached stack top. */
1136 top = aexpr->bytes[pc++];
1137 top = (top << 8) + aexpr->bytes[pc++];
1138 top = (top << 8) + aexpr->bytes[pc++];
1139 top = (top << 8) + aexpr->bytes[pc++];
1142 case gdb_agent_op_const64:
1143 /* Flush the cached stack top. */
1145 top = aexpr->bytes[pc++];
1146 top = (top << 8) + aexpr->bytes[pc++];
1147 top = (top << 8) + aexpr->bytes[pc++];
1148 top = (top << 8) + aexpr->bytes[pc++];
1149 top = (top << 8) + 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_reg:
1156 /* Flush the cached stack top. */
1158 arg = aexpr->bytes[pc++];
1159 arg = (arg << 8) + aexpr->bytes[pc++];
1162 struct regcache *regcache = ctx->regcache;
1164 switch (register_size (regnum))
1167 collect_register (regcache, regnum, cnv.u64.bytes);
1171 collect_register (regcache, regnum, cnv.u32.bytes);
1175 collect_register (regcache, regnum, cnv.u16.bytes);
1179 collect_register (regcache, regnum, cnv.u8.bytes);
1183 internal_error (__FILE__, __LINE__,
1184 "unhandled register size");
1189 case gdb_agent_op_end:
1190 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1191 sp, pulongest (top));
1196 /* This should be an error */
1197 ax_debug ("Stack is empty, nothing to return");
1198 return expr_eval_empty_stack;
1202 return expr_eval_no_error;
1204 case gdb_agent_op_dup:
1208 case gdb_agent_op_pop:
1213 case gdb_agent_op_pick:
1214 arg = aexpr->bytes[pc++];
1216 top = stack[sp - arg];
1220 case gdb_agent_op_rot:
1222 ULONGEST tem = stack[sp - 1];
1224 stack[sp - 1] = stack[sp - 2];
1225 stack[sp - 2] = top;
1230 case gdb_agent_op_zero_ext:
1231 arg = aexpr->bytes[pc++];
1232 if (arg < (sizeof (LONGEST) * 8))
1233 top &= ((LONGEST) 1 << arg) - 1;
1236 case gdb_agent_op_swap:
1237 /* Interchange top two stack elements, making sure top gets
1238 copied back onto stack. */
1240 top = stack[sp - 1];
1241 stack[sp - 1] = stack[sp];
1244 case gdb_agent_op_getv:
1245 /* Flush the cached stack top. */
1247 arg = aexpr->bytes[pc++];
1248 arg = (arg << 8) + aexpr->bytes[pc++];
1249 top = agent_get_trace_state_variable_value (arg);
1252 case gdb_agent_op_setv:
1253 arg = aexpr->bytes[pc++];
1254 arg = (arg << 8) + aexpr->bytes[pc++];
1255 agent_set_trace_state_variable_value (arg, top);
1256 /* Note that we leave the value on the stack, for the
1257 benefit of later/enclosing expressions. */
1260 case gdb_agent_op_tracev:
1261 arg = aexpr->bytes[pc++];
1262 arg = (arg << 8) + aexpr->bytes[pc++];
1263 agent_tsv_read (ctx, arg);
1266 case gdb_agent_op_tracenz:
1267 agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
1273 case gdb_agent_op_printf:
1276 CORE_ADDR fn = 0, chan = 0;
1277 /* Can't have more args than the entire size of the stack. */
1278 ULONGEST args[STACK_MAX];
1281 nargs = aexpr->bytes[pc++];
1282 slen = aexpr->bytes[pc++];
1283 slen = (slen << 8) + aexpr->bytes[pc++];
1284 format = (char *) &(aexpr->bytes[pc]);
1286 /* Pop function and channel. */
1293 /* Pop arguments into a dedicated array. */
1294 for (i = 0; i < nargs; ++i)
1301 /* A bad format string means something is very wrong; give
1303 if (format[slen - 1] != '\0')
1304 error (_("Unterminated format string in printf bytecode"));
1306 ax_printf (fn, chan, format, nargs, args);
1310 /* GDB never (currently) generates any of these ops. */
1311 case gdb_agent_op_float:
1312 case gdb_agent_op_ref_float:
1313 case gdb_agent_op_ref_double:
1314 case gdb_agent_op_ref_long_double:
1315 case gdb_agent_op_l_to_d:
1316 case gdb_agent_op_d_to_l:
1317 case gdb_agent_op_trace16:
1318 ax_debug ("Agent expression op 0x%x valid, but not handled",
1320 /* If ever GDB generates any of these, we don't have the
1321 option of ignoring. */
1325 ax_debug ("Agent expression op 0x%x not recognized", op);
1326 /* Don't struggle on, things will just get worse. */
1327 return expr_eval_unrecognized_opcode;
1330 /* Check for stack badness. */
1331 if (sp >= (STACK_MAX - 1))
1333 ax_debug ("Expression stack overflow");
1334 return expr_eval_stack_overflow;
1339 ax_debug ("Expression stack underflow");
1340 return expr_eval_stack_underflow;
1343 ax_debug ("Op %s -> sp=%d, top=0x%s",
1344 gdb_agent_op_name (op), sp, phex_nz (top, 0));