1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2021 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 "gdbsupport/format.h"
22 #include "tracepoint.h"
23 #include "gdbsupport/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 "gdbsupport/ax.def"
68 static const char * const gdb_agent_op_names
[gdb_agent_op_last
] =
71 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
72 #include "gdbsupport/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 "gdbsupport/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 static 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
);
909 size_t val
= args
[i
];
911 printf (current_substring
, val
);
916 /* Print a portion of the format string that has no
917 directives. Note that this will not include any
918 ordinary %-specs, but it might include "%%". That is
919 why we use printf_filtered and not puts_filtered here.
920 Also, we pass a dummy argument because some platforms
921 have modified GCC to include -Wformat-security by
922 default, which will warn here if there is no
924 printf (current_substring
, 0);
928 error (_("Format directive in '%s' not supported in agent printf"),
932 /* Maybe advance to the next argument. */
933 if (piece
.argclass
!= literal_piece
)
940 /* The agent expression evaluator, as specified by the GDB docs. It
941 returns 0 if everything went OK, and a nonzero error code
944 enum eval_result_type
945 gdb_eval_agent_expr (struct eval_agent_expr_context
*ctx
,
946 struct agent_expr
*aexpr
,
950 #define STACK_MAX 100
951 ULONGEST stack
[STACK_MAX
], top
;
956 /* This union is a convenient way to convert representations. For
957 now, assume a standard architecture where the hardware integer
958 types have 8, 16, 32, 64 bit types. A more robust solution would
959 be to import stdint.h from gnulib. */
964 unsigned char bytes
[1];
969 unsigned char bytes
[2];
974 unsigned char bytes
[4];
979 unsigned char bytes
[8];
984 if (aexpr
->length
== 0)
986 ax_debug ("empty agent expression");
987 return expr_eval_empty_expression
;
990 /* Cache the stack top in its own variable. Much of the time we can
991 operate on this variable, rather than dinking with the stack. It
992 needs to be copied to the stack when sp changes. */
997 op
= aexpr
->bytes
[pc
++];
999 ax_debug ("About to interpret byte 0x%x", op
);
1003 case gdb_agent_op_add
:
1007 case gdb_agent_op_sub
:
1008 top
= stack
[--sp
] - top
;
1011 case gdb_agent_op_mul
:
1015 case gdb_agent_op_div_signed
:
1018 ax_debug ("Attempted to divide by zero");
1019 return expr_eval_divide_by_zero
;
1021 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
1024 case gdb_agent_op_div_unsigned
:
1027 ax_debug ("Attempted to divide by zero");
1028 return expr_eval_divide_by_zero
;
1030 top
= stack
[--sp
] / top
;
1033 case gdb_agent_op_rem_signed
:
1036 ax_debug ("Attempted to divide by zero");
1037 return expr_eval_divide_by_zero
;
1039 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
1042 case gdb_agent_op_rem_unsigned
:
1045 ax_debug ("Attempted to divide by zero");
1046 return expr_eval_divide_by_zero
;
1048 top
= stack
[--sp
] % top
;
1051 case gdb_agent_op_lsh
:
1052 top
= stack
[--sp
] << top
;
1055 case gdb_agent_op_rsh_signed
:
1056 top
= ((LONGEST
) stack
[--sp
]) >> top
;
1059 case gdb_agent_op_rsh_unsigned
:
1060 top
= stack
[--sp
] >> top
;
1063 case gdb_agent_op_trace
:
1064 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1070 case gdb_agent_op_trace_quick
:
1071 arg
= aexpr
->bytes
[pc
++];
1072 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
1075 case gdb_agent_op_log_not
:
1079 case gdb_agent_op_bit_and
:
1083 case gdb_agent_op_bit_or
:
1087 case gdb_agent_op_bit_xor
:
1091 case gdb_agent_op_bit_not
:
1095 case gdb_agent_op_equal
:
1096 top
= (stack
[--sp
] == top
);
1099 case gdb_agent_op_less_signed
:
1100 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
1103 case gdb_agent_op_less_unsigned
:
1104 top
= (stack
[--sp
] < top
);
1107 case gdb_agent_op_ext
:
1108 arg
= aexpr
->bytes
[pc
++];
1109 if (arg
< (sizeof (LONGEST
) * 8))
1111 LONGEST mask
= 1 << (arg
- 1);
1112 top
&= ((LONGEST
) 1 << arg
) - 1;
1113 top
= (top
^ mask
) - mask
;
1117 case gdb_agent_op_ref8
:
1118 agent_mem_read (ctx
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
1122 case gdb_agent_op_ref16
:
1123 agent_mem_read (ctx
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
1127 case gdb_agent_op_ref32
:
1128 agent_mem_read (ctx
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
1132 case gdb_agent_op_ref64
:
1133 agent_mem_read (ctx
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
1137 case gdb_agent_op_if_goto
:
1139 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1146 case gdb_agent_op_goto
:
1147 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1150 case gdb_agent_op_const8
:
1151 /* Flush the cached stack top. */
1153 top
= aexpr
->bytes
[pc
++];
1156 case gdb_agent_op_const16
:
1157 /* Flush the cached stack top. */
1159 top
= aexpr
->bytes
[pc
++];
1160 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1163 case gdb_agent_op_const32
:
1164 /* Flush the cached stack top. */
1166 top
= aexpr
->bytes
[pc
++];
1167 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1168 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1169 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1172 case gdb_agent_op_const64
:
1173 /* Flush the cached stack top. */
1175 top
= aexpr
->bytes
[pc
++];
1176 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1177 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1178 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1179 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1180 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1181 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1182 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1185 case gdb_agent_op_reg
:
1186 /* Flush the cached stack top. */
1188 arg
= aexpr
->bytes
[pc
++];
1189 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1192 struct regcache
*regcache
= ctx
->regcache
;
1194 switch (register_size (regcache
->tdesc
, regnum
))
1197 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
1201 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
1205 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
1209 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
1213 internal_error (__FILE__
, __LINE__
,
1214 "unhandled register size");
1219 case gdb_agent_op_end
:
1220 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1221 sp
, pulongest (top
));
1226 /* This should be an error */
1227 ax_debug ("Stack is empty, nothing to return");
1228 return expr_eval_empty_stack
;
1232 return expr_eval_no_error
;
1234 case gdb_agent_op_dup
:
1238 case gdb_agent_op_pop
:
1243 case gdb_agent_op_pick
:
1244 arg
= aexpr
->bytes
[pc
++];
1246 top
= stack
[sp
- arg
];
1250 case gdb_agent_op_rot
:
1252 ULONGEST tem
= stack
[sp
- 1];
1254 stack
[sp
- 1] = stack
[sp
- 2];
1255 stack
[sp
- 2] = top
;
1260 case gdb_agent_op_zero_ext
:
1261 arg
= aexpr
->bytes
[pc
++];
1262 if (arg
< (sizeof (LONGEST
) * 8))
1263 top
&= ((LONGEST
) 1 << arg
) - 1;
1266 case gdb_agent_op_swap
:
1267 /* Interchange top two stack elements, making sure top gets
1268 copied back onto stack. */
1270 top
= stack
[sp
- 1];
1271 stack
[sp
- 1] = stack
[sp
];
1274 case gdb_agent_op_getv
:
1275 /* Flush the cached stack top. */
1277 arg
= aexpr
->bytes
[pc
++];
1278 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1279 top
= agent_get_trace_state_variable_value (arg
);
1282 case gdb_agent_op_setv
:
1283 arg
= aexpr
->bytes
[pc
++];
1284 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1285 agent_set_trace_state_variable_value (arg
, top
);
1286 /* Note that we leave the value on the stack, for the
1287 benefit of later/enclosing expressions. */
1290 case gdb_agent_op_tracev
:
1291 arg
= aexpr
->bytes
[pc
++];
1292 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1293 agent_tsv_read (ctx
, arg
);
1296 case gdb_agent_op_tracenz
:
1297 agent_mem_read_string (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1303 case gdb_agent_op_printf
:
1306 CORE_ADDR fn
= 0, chan
= 0;
1307 /* Can't have more args than the entire size of the stack. */
1308 ULONGEST args
[STACK_MAX
];
1311 nargs
= aexpr
->bytes
[pc
++];
1312 slen
= aexpr
->bytes
[pc
++];
1313 slen
= (slen
<< 8) + aexpr
->bytes
[pc
++];
1314 format
= (char *) &(aexpr
->bytes
[pc
]);
1316 /* Pop function and channel. */
1323 /* Pop arguments into a dedicated array. */
1324 for (i
= 0; i
< nargs
; ++i
)
1331 /* A bad format string means something is very wrong; give
1333 if (format
[slen
- 1] != '\0')
1334 error (_("Unterminated format string in printf bytecode"));
1336 ax_printf (fn
, chan
, format
, nargs
, args
);
1340 /* GDB never (currently) generates any of these ops. */
1341 case gdb_agent_op_float
:
1342 case gdb_agent_op_ref_float
:
1343 case gdb_agent_op_ref_double
:
1344 case gdb_agent_op_ref_long_double
:
1345 case gdb_agent_op_l_to_d
:
1346 case gdb_agent_op_d_to_l
:
1347 case gdb_agent_op_trace16
:
1348 ax_debug ("Agent expression op 0x%x valid, but not handled",
1350 /* If ever GDB generates any of these, we don't have the
1351 option of ignoring. */
1352 return expr_eval_unhandled_opcode
;
1355 ax_debug ("Agent expression op 0x%x not recognized", op
);
1356 /* Don't struggle on, things will just get worse. */
1357 return expr_eval_unrecognized_opcode
;
1360 /* Check for stack badness. */
1361 if (sp
>= (STACK_MAX
- 1))
1363 ax_debug ("Expression stack overflow");
1364 return expr_eval_stack_overflow
;
1369 ax_debug ("Expression stack underflow");
1370 return expr_eval_stack_underflow
;
1373 ax_debug ("Op %s -> sp=%d, top=0x%s",
1374 gdb_agent_op_name (op
), sp
, phex_nz (top
, 0));