1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2019 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/>. */
21 #include "common/format.h"
22 #include "tracepoint.h"
23 #include "common/rsp-low.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 #ifdef IN_PROCESS_AGENT
40 fprintf (stderr
, PROG
"/ax: %s\n", buf
);
42 debug_printf (PROG
"/ax: %s\n", buf
);
47 #define ax_debug_1(level, fmt, args...) \
49 if (level <= debug_threads) \
50 ax_vdebug ((fmt), ##args); \
53 #define ax_debug(FMT, args...) \
54 ax_debug_1 (1, FMT, ##args)
56 /* This enum must exactly match what is documented in
57 gdb/doc/agentexpr.texi, including all the numerical values. */
61 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
62 gdb_agent_op_ ## NAME = VALUE,
63 #include "common/ax.def"
68 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
71 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
72 #include "common/ax.def"
76 #ifndef IN_PROCESS_AGENT
77 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
80 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
81 #include "common/ax.def"
86 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
89 gdb_agent_op_name (int op
)
91 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
93 return gdb_agent_op_names
[op
];
96 #ifndef IN_PROCESS_AGENT
98 /* The packet form of an agent expression consists of an 'X', number
99 of bytes in expression, a comma, and then the bytes. */
102 gdb_parse_agent_expr (const char **actparm
)
104 const char *act
= *actparm
;
106 struct agent_expr
*aexpr
;
108 ++act
; /* skip the X */
109 act
= unpack_varlen_hex (act
, &xlen
);
110 ++act
; /* skip a comma */
111 aexpr
= XNEW (struct agent_expr
);
112 aexpr
->length
= xlen
;
113 aexpr
->bytes
= (unsigned char *) xmalloc (xlen
);
114 hex2bin (act
, aexpr
->bytes
, xlen
);
115 *actparm
= act
+ (xlen
* 2);
120 gdb_free_agent_expr (struct agent_expr
*aexpr
)
129 /* Convert the bytes of an agent expression back into hex digits, so
130 they can be printed or uploaded. This allocates the buffer,
131 callers should free when they are done with it. */
134 gdb_unparse_agent_expr (struct agent_expr
*aexpr
)
138 rslt
= (char *) xmalloc (2 * aexpr
->length
+ 1);
139 bin2hex (aexpr
->bytes
, rslt
, aexpr
->length
);
143 /* Bytecode compilation. */
145 CORE_ADDR current_insn_ptr
;
149 struct bytecode_address
154 /* Offset and size of field to be modified in the goto block. */
155 int from_offset
, from_size
;
156 struct bytecode_address
*next
;
157 } *bytecode_address_table
;
162 target_emit_ops ()->emit_prologue ();
168 target_emit_ops ()->emit_epilogue ();
174 target_emit_ops ()->emit_add ();
180 target_emit_ops ()->emit_sub ();
186 target_emit_ops ()->emit_mul ();
192 target_emit_ops ()->emit_lsh ();
196 emit_rsh_signed (void)
198 target_emit_ops ()->emit_rsh_signed ();
202 emit_rsh_unsigned (void)
204 target_emit_ops ()->emit_rsh_unsigned ();
210 target_emit_ops ()->emit_ext (arg
);
216 target_emit_ops ()->emit_log_not ();
222 target_emit_ops ()->emit_bit_and ();
228 target_emit_ops ()->emit_bit_or ();
234 target_emit_ops ()->emit_bit_xor ();
240 target_emit_ops ()->emit_bit_not ();
246 target_emit_ops ()->emit_equal ();
250 emit_less_signed (void)
252 target_emit_ops ()->emit_less_signed ();
256 emit_less_unsigned (void)
258 target_emit_ops ()->emit_less_unsigned ();
264 target_emit_ops ()->emit_ref (size
);
268 emit_if_goto (int *offset_p
, int *size_p
)
270 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
274 emit_goto (int *offset_p
, int *size_p
)
276 target_emit_ops ()->emit_goto (offset_p
, size_p
);
280 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
282 target_emit_ops ()->write_goto_address (from
, to
, size
);
286 emit_const (LONGEST num
)
288 target_emit_ops ()->emit_const (num
);
294 target_emit_ops ()->emit_reg (reg
);
300 target_emit_ops ()->emit_pop ();
304 emit_stack_flush (void)
306 target_emit_ops ()->emit_stack_flush ();
310 emit_zero_ext (int arg
)
312 target_emit_ops ()->emit_zero_ext (arg
);
318 target_emit_ops ()->emit_swap ();
322 emit_stack_adjust (int n
)
324 target_emit_ops ()->emit_stack_adjust (n
);
327 /* FN's prototype is `LONGEST(*fn)(int)'. */
330 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
332 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
335 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
338 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
340 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
344 emit_eq_goto (int *offset_p
, int *size_p
)
346 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
350 emit_ne_goto (int *offset_p
, int *size_p
)
352 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
356 emit_lt_goto (int *offset_p
, int *size_p
)
358 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
362 emit_ge_goto (int *offset_p
, int *size_p
)
364 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
368 emit_gt_goto (int *offset_p
, int *size_p
)
370 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
374 emit_le_goto (int *offset_p
, int *size_p
)
376 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
379 /* Scan an agent expression for any evidence that the given PC is the
380 target of a jump bytecode in the expression. */
383 is_goto_target (struct agent_expr
*aexpr
, int pc
)
388 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
390 op
= aexpr
->bytes
[i
];
392 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
394 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
403 /* Given an agent expression, turn it into native code. */
405 enum eval_result_type
406 compile_bytecodes (struct agent_expr
*aexpr
)
410 unsigned char op
, next_op
;
412 /* This is only used to build 64-bit value for constants. */
414 struct bytecode_address
*aentry
, *aentry2
;
419 ax_debug ("Cannot compile op 0x%x\n", op); \
420 return expr_eval_unhandled_opcode; \
423 if (aexpr
->length
== 0)
425 ax_debug ("empty agent expression\n");
426 return expr_eval_empty_expression
;
429 bytecode_address_table
= NULL
;
433 op
= aexpr
->bytes
[pc
];
435 ax_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
437 /* Record the compiled-code address of the bytecode, for use by
438 jump instructions. */
439 aentry
= XNEW (struct bytecode_address
);
441 aentry
->address
= current_insn_ptr
;
442 aentry
->goto_pc
= -1;
443 aentry
->from_offset
= aentry
->from_size
= 0;
444 aentry
->next
= bytecode_address_table
;
445 bytecode_address_table
= aentry
;
453 case gdb_agent_op_add
:
457 case gdb_agent_op_sub
:
461 case gdb_agent_op_mul
:
465 case gdb_agent_op_div_signed
:
469 case gdb_agent_op_div_unsigned
:
473 case gdb_agent_op_rem_signed
:
477 case gdb_agent_op_rem_unsigned
:
481 case gdb_agent_op_lsh
:
485 case gdb_agent_op_rsh_signed
:
489 case gdb_agent_op_rsh_unsigned
:
490 emit_rsh_unsigned ();
493 case gdb_agent_op_trace
:
497 case gdb_agent_op_trace_quick
:
501 case gdb_agent_op_log_not
:
505 case gdb_agent_op_bit_and
:
509 case gdb_agent_op_bit_or
:
513 case gdb_agent_op_bit_xor
:
517 case gdb_agent_op_bit_not
:
521 case gdb_agent_op_equal
:
522 next_op
= aexpr
->bytes
[pc
];
523 if (next_op
== gdb_agent_op_if_goto
524 && !is_goto_target (aexpr
, pc
)
525 && target_emit_ops ()->emit_eq_goto
)
527 ax_debug ("Combining equal & if_goto");
530 arg
= aexpr
->bytes
[pc
++];
531 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
532 aentry
->goto_pc
= arg
;
533 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
535 else if (next_op
== gdb_agent_op_log_not
536 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
537 && !is_goto_target (aexpr
, pc
+ 1)
538 && target_emit_ops ()->emit_ne_goto
)
540 ax_debug ("Combining equal & log_not & if_goto");
543 arg
= aexpr
->bytes
[pc
++];
544 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
545 aentry
->goto_pc
= arg
;
546 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
552 case gdb_agent_op_less_signed
:
553 next_op
= aexpr
->bytes
[pc
];
554 if (next_op
== gdb_agent_op_if_goto
555 && !is_goto_target (aexpr
, pc
))
557 ax_debug ("Combining less_signed & if_goto");
560 arg
= aexpr
->bytes
[pc
++];
561 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
562 aentry
->goto_pc
= arg
;
563 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
565 else if (next_op
== gdb_agent_op_log_not
566 && !is_goto_target (aexpr
, pc
)
567 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
568 && !is_goto_target (aexpr
, pc
+ 1))
570 ax_debug ("Combining less_signed & log_not & if_goto");
573 arg
= aexpr
->bytes
[pc
++];
574 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
575 aentry
->goto_pc
= arg
;
576 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
582 case gdb_agent_op_less_unsigned
:
583 emit_less_unsigned ();
586 case gdb_agent_op_ext
:
587 arg
= aexpr
->bytes
[pc
++];
588 if (arg
< (sizeof (LONGEST
) * 8))
592 case gdb_agent_op_ref8
:
596 case gdb_agent_op_ref16
:
600 case gdb_agent_op_ref32
:
604 case gdb_agent_op_ref64
:
608 case gdb_agent_op_if_goto
:
609 arg
= aexpr
->bytes
[pc
++];
610 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
611 aentry
->goto_pc
= arg
;
612 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
615 case gdb_agent_op_goto
:
616 arg
= aexpr
->bytes
[pc
++];
617 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
618 aentry
->goto_pc
= arg
;
619 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
622 case gdb_agent_op_const8
:
624 top
= aexpr
->bytes
[pc
++];
628 case gdb_agent_op_const16
:
630 top
= aexpr
->bytes
[pc
++];
631 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
635 case gdb_agent_op_const32
:
637 top
= aexpr
->bytes
[pc
++];
638 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
639 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
640 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
644 case gdb_agent_op_const64
:
646 top
= aexpr
->bytes
[pc
++];
647 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
648 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
649 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
650 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
651 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
652 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
653 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
657 case gdb_agent_op_reg
:
659 arg
= aexpr
->bytes
[pc
++];
660 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
664 case gdb_agent_op_end
:
665 ax_debug ("At end of expression\n");
667 /* Assume there is one stack element left, and that it is
668 cached in "top" where emit_epilogue can get to it. */
669 emit_stack_adjust (1);
674 case gdb_agent_op_dup
:
675 /* In our design, dup is equivalent to stack flushing. */
679 case gdb_agent_op_pop
:
683 case gdb_agent_op_zero_ext
:
684 arg
= aexpr
->bytes
[pc
++];
685 if (arg
< (sizeof (LONGEST
) * 8))
689 case gdb_agent_op_swap
:
690 next_op
= aexpr
->bytes
[pc
];
691 /* Detect greater-than comparison sequences. */
692 if (next_op
== gdb_agent_op_less_signed
693 && !is_goto_target (aexpr
, pc
)
694 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
695 && !is_goto_target (aexpr
, pc
+ 1))
697 ax_debug ("Combining swap & less_signed & if_goto");
700 arg
= aexpr
->bytes
[pc
++];
701 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
702 aentry
->goto_pc
= arg
;
703 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
705 else if (next_op
== gdb_agent_op_less_signed
706 && !is_goto_target (aexpr
, pc
)
707 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
708 && !is_goto_target (aexpr
, pc
+ 1)
709 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
710 && !is_goto_target (aexpr
, pc
+ 2))
712 ax_debug ("Combining swap & less_signed & log_not & if_goto");
715 arg
= aexpr
->bytes
[pc
++];
716 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
717 aentry
->goto_pc
= arg
;
718 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
724 case gdb_agent_op_getv
:
726 arg
= aexpr
->bytes
[pc
++];
727 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
728 emit_int_call_1 (get_get_tsv_func_addr (),
732 case gdb_agent_op_setv
:
733 arg
= aexpr
->bytes
[pc
++];
734 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
735 emit_void_call_2 (get_set_tsv_func_addr (),
739 case gdb_agent_op_tracev
:
743 /* GDB never (currently) generates any of these ops. */
744 case gdb_agent_op_float
:
745 case gdb_agent_op_ref_float
:
746 case gdb_agent_op_ref_double
:
747 case gdb_agent_op_ref_long_double
:
748 case gdb_agent_op_l_to_d
:
749 case gdb_agent_op_d_to_l
:
750 case gdb_agent_op_trace16
:
755 ax_debug ("Agent expression op 0x%x not recognized\n", op
);
756 /* Don't struggle on, things will just get worse. */
757 return expr_eval_unrecognized_opcode
;
760 /* This catches errors that occur in target-specific code
764 ax_debug ("Error %d while emitting code for %s\n",
765 emit_error
, gdb_agent_op_name (op
));
766 return expr_eval_unhandled_opcode
;
769 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
772 /* Now fill in real addresses as goto destinations. */
773 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
777 if (aentry
->goto_pc
< 0)
780 /* Find the location that we are going to, and call back into
781 target-specific code to write the actual address or
783 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
785 if (aentry2
->pc
== aentry
->goto_pc
)
787 ax_debug ("Want to jump from %s to %s\n",
788 paddress (aentry
->address
),
789 paddress (aentry2
->address
));
790 write_goto_address (aentry
->address
+ aentry
->from_offset
,
791 aentry2
->address
, aentry
->from_size
);
797 /* Error out if we didn't find a destination. */
800 ax_debug ("Destination of goto %d not found\n",
802 return expr_eval_invalid_goto
;
806 return expr_eval_no_error
;
811 /* Make printf-type calls using arguments supplied from the host. We
812 need to parse the format string ourselves, and call the formatting
813 function with one argument at a time, partly because there is no
814 safe portable way to construct a varargs call, and partly to serve
815 as a security barrier against bad format strings that might get
819 ax_printf (CORE_ADDR fn
, CORE_ADDR chan
, const char *format
,
820 int nargs
, ULONGEST
*args
)
822 const char *f
= format
;
824 const char *current_substring
;
827 ax_debug ("Printf of \"%s\" with %d args", format
, nargs
);
829 format_pieces
fpieces (&f
);
832 for (auto &&piece
: fpieces
)
833 if (piece
.argclass
!= literal_piece
)
836 if (nargs
!= nargs_wanted
)
837 error (_("Wrong number of arguments for specified format-string"));
840 for (auto &&piece
: fpieces
)
842 current_substring
= piece
.string
;
843 ax_debug ("current substring is '%s', class is %d",
844 current_substring
, piece
.argclass
);
845 switch (piece
.argclass
)
856 printf (current_substring
, "(null)");
860 /* This is a %s argument. Find the length of the string. */
865 read_inferior_memory (tem
+ j
, &c
, 1);
870 /* Copy the string contents into a string inside GDB. */
871 str
= (gdb_byte
*) alloca (j
+ 1);
873 read_inferior_memory (tem
, str
, j
);
876 printf (current_substring
, (char *) str
);
881 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
883 long long val
= args
[i
];
885 printf (current_substring
, val
);
889 error (_("long long not supported in agent printf"));
895 printf (current_substring
, val
);
903 printf (current_substring
, val
);
908 /* Print a portion of the format string that has no
909 directives. Note that this will not include any
910 ordinary %-specs, but it might include "%%". That is
911 why we use printf_filtered and not puts_filtered here.
912 Also, we pass a dummy argument because some platforms
913 have modified GCC to include -Wformat-security by
914 default, which will warn here if there is no
916 printf (current_substring
, 0);
920 error (_("Format directive in '%s' not supported in agent printf"),
924 /* Maybe advance to the next argument. */
925 if (piece
.argclass
!= literal_piece
)
932 /* The agent expression evaluator, as specified by the GDB docs. It
933 returns 0 if everything went OK, and a nonzero error code
936 enum eval_result_type
937 gdb_eval_agent_expr (struct eval_agent_expr_context
*ctx
,
938 struct agent_expr
*aexpr
,
942 #define STACK_MAX 100
943 ULONGEST stack
[STACK_MAX
], top
;
948 /* This union is a convenient way to convert representations. For
949 now, assume a standard architecture where the hardware integer
950 types have 8, 16, 32, 64 bit types. A more robust solution would
951 be to import stdint.h from gnulib. */
956 unsigned char bytes
[1];
961 unsigned char bytes
[2];
966 unsigned char bytes
[4];
971 unsigned char bytes
[8];
976 if (aexpr
->length
== 0)
978 ax_debug ("empty agent expression");
979 return expr_eval_empty_expression
;
982 /* Cache the stack top in its own variable. Much of the time we can
983 operate on this variable, rather than dinking with the stack. It
984 needs to be copied to the stack when sp changes. */
989 op
= aexpr
->bytes
[pc
++];
991 ax_debug ("About to interpret byte 0x%x", op
);
995 case gdb_agent_op_add
:
999 case gdb_agent_op_sub
:
1000 top
= stack
[--sp
] - top
;
1003 case gdb_agent_op_mul
:
1007 case gdb_agent_op_div_signed
:
1010 ax_debug ("Attempted to divide by zero");
1011 return expr_eval_divide_by_zero
;
1013 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
1016 case gdb_agent_op_div_unsigned
:
1019 ax_debug ("Attempted to divide by zero");
1020 return expr_eval_divide_by_zero
;
1022 top
= stack
[--sp
] / top
;
1025 case gdb_agent_op_rem_signed
:
1028 ax_debug ("Attempted to divide by zero");
1029 return expr_eval_divide_by_zero
;
1031 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
1034 case gdb_agent_op_rem_unsigned
:
1037 ax_debug ("Attempted to divide by zero");
1038 return expr_eval_divide_by_zero
;
1040 top
= stack
[--sp
] % top
;
1043 case gdb_agent_op_lsh
:
1044 top
= stack
[--sp
] << top
;
1047 case gdb_agent_op_rsh_signed
:
1048 top
= ((LONGEST
) stack
[--sp
]) >> top
;
1051 case gdb_agent_op_rsh_unsigned
:
1052 top
= stack
[--sp
] >> top
;
1055 case gdb_agent_op_trace
:
1056 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1062 case gdb_agent_op_trace_quick
:
1063 arg
= aexpr
->bytes
[pc
++];
1064 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
1067 case gdb_agent_op_log_not
:
1071 case gdb_agent_op_bit_and
:
1075 case gdb_agent_op_bit_or
:
1079 case gdb_agent_op_bit_xor
:
1083 case gdb_agent_op_bit_not
:
1087 case gdb_agent_op_equal
:
1088 top
= (stack
[--sp
] == top
);
1091 case gdb_agent_op_less_signed
:
1092 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
1095 case gdb_agent_op_less_unsigned
:
1096 top
= (stack
[--sp
] < top
);
1099 case gdb_agent_op_ext
:
1100 arg
= aexpr
->bytes
[pc
++];
1101 if (arg
< (sizeof (LONGEST
) * 8))
1103 LONGEST mask
= 1 << (arg
- 1);
1104 top
&= ((LONGEST
) 1 << arg
) - 1;
1105 top
= (top
^ mask
) - mask
;
1109 case gdb_agent_op_ref8
:
1110 agent_mem_read (ctx
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
1114 case gdb_agent_op_ref16
:
1115 agent_mem_read (ctx
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
1119 case gdb_agent_op_ref32
:
1120 agent_mem_read (ctx
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
1124 case gdb_agent_op_ref64
:
1125 agent_mem_read (ctx
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
1129 case gdb_agent_op_if_goto
:
1131 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1138 case gdb_agent_op_goto
:
1139 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1142 case gdb_agent_op_const8
:
1143 /* Flush the cached stack top. */
1145 top
= aexpr
->bytes
[pc
++];
1148 case gdb_agent_op_const16
:
1149 /* Flush the cached stack top. */
1151 top
= aexpr
->bytes
[pc
++];
1152 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1155 case gdb_agent_op_const32
:
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
++];
1164 case gdb_agent_op_const64
:
1165 /* Flush the cached stack top. */
1167 top
= aexpr
->bytes
[pc
++];
1168 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1169 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1170 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1171 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1172 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1173 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1174 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1177 case gdb_agent_op_reg
:
1178 /* Flush the cached stack top. */
1180 arg
= aexpr
->bytes
[pc
++];
1181 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1184 struct regcache
*regcache
= ctx
->regcache
;
1186 switch (register_size (regcache
->tdesc
, regnum
))
1189 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
1193 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
1197 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
1201 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
1205 internal_error (__FILE__
, __LINE__
,
1206 "unhandled register size");
1211 case gdb_agent_op_end
:
1212 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1213 sp
, pulongest (top
));
1218 /* This should be an error */
1219 ax_debug ("Stack is empty, nothing to return");
1220 return expr_eval_empty_stack
;
1224 return expr_eval_no_error
;
1226 case gdb_agent_op_dup
:
1230 case gdb_agent_op_pop
:
1235 case gdb_agent_op_pick
:
1236 arg
= aexpr
->bytes
[pc
++];
1238 top
= stack
[sp
- arg
];
1242 case gdb_agent_op_rot
:
1244 ULONGEST tem
= stack
[sp
- 1];
1246 stack
[sp
- 1] = stack
[sp
- 2];
1247 stack
[sp
- 2] = top
;
1252 case gdb_agent_op_zero_ext
:
1253 arg
= aexpr
->bytes
[pc
++];
1254 if (arg
< (sizeof (LONGEST
) * 8))
1255 top
&= ((LONGEST
) 1 << arg
) - 1;
1258 case gdb_agent_op_swap
:
1259 /* Interchange top two stack elements, making sure top gets
1260 copied back onto stack. */
1262 top
= stack
[sp
- 1];
1263 stack
[sp
- 1] = stack
[sp
];
1266 case gdb_agent_op_getv
:
1267 /* Flush the cached stack top. */
1269 arg
= aexpr
->bytes
[pc
++];
1270 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1271 top
= agent_get_trace_state_variable_value (arg
);
1274 case gdb_agent_op_setv
:
1275 arg
= aexpr
->bytes
[pc
++];
1276 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1277 agent_set_trace_state_variable_value (arg
, top
);
1278 /* Note that we leave the value on the stack, for the
1279 benefit of later/enclosing expressions. */
1282 case gdb_agent_op_tracev
:
1283 arg
= aexpr
->bytes
[pc
++];
1284 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1285 agent_tsv_read (ctx
, arg
);
1288 case gdb_agent_op_tracenz
:
1289 agent_mem_read_string (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1295 case gdb_agent_op_printf
:
1298 CORE_ADDR fn
= 0, chan
= 0;
1299 /* Can't have more args than the entire size of the stack. */
1300 ULONGEST args
[STACK_MAX
];
1303 nargs
= aexpr
->bytes
[pc
++];
1304 slen
= aexpr
->bytes
[pc
++];
1305 slen
= (slen
<< 8) + aexpr
->bytes
[pc
++];
1306 format
= (char *) &(aexpr
->bytes
[pc
]);
1308 /* Pop function and channel. */
1315 /* Pop arguments into a dedicated array. */
1316 for (i
= 0; i
< nargs
; ++i
)
1323 /* A bad format string means something is very wrong; give
1325 if (format
[slen
- 1] != '\0')
1326 error (_("Unterminated format string in printf bytecode"));
1328 ax_printf (fn
, chan
, format
, nargs
, args
);
1332 /* GDB never (currently) generates any of these ops. */
1333 case gdb_agent_op_float
:
1334 case gdb_agent_op_ref_float
:
1335 case gdb_agent_op_ref_double
:
1336 case gdb_agent_op_ref_long_double
:
1337 case gdb_agent_op_l_to_d
:
1338 case gdb_agent_op_d_to_l
:
1339 case gdb_agent_op_trace16
:
1340 ax_debug ("Agent expression op 0x%x valid, but not handled",
1342 /* If ever GDB generates any of these, we don't have the
1343 option of ignoring. */
1344 return expr_eval_unhandled_opcode
;
1347 ax_debug ("Agent expression op 0x%x not recognized", op
);
1348 /* Don't struggle on, things will just get worse. */
1349 return expr_eval_unrecognized_opcode
;
1352 /* Check for stack badness. */
1353 if (sp
>= (STACK_MAX
- 1))
1355 ax_debug ("Expression stack overflow");
1356 return expr_eval_stack_overflow
;
1361 ax_debug ("Expression stack underflow");
1362 return expr_eval_stack_underflow
;
1365 ax_debug ("Op %s -> sp=%d, top=0x%s",
1366 gdb_agent_op_name (op
), sp
, phex_nz (top
, 0));