1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2022 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 threads_debug_printf (PROG
"/ax: %s", buf
);
47 #define ax_debug(fmt, args...) \
50 ax_vdebug ((fmt), ##args); \
53 /* This enum must exactly match what is documented in
54 gdb/doc/agentexpr.texi, including all the numerical values. */
58 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
59 gdb_agent_op_ ## NAME = VALUE,
60 #include "gdbsupport/ax.def"
65 static const char * const gdb_agent_op_names
[gdb_agent_op_last
] =
68 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
69 #include "gdbsupport/ax.def"
73 #ifndef IN_PROCESS_AGENT
74 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
77 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
78 #include "gdbsupport/ax.def"
83 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
86 gdb_agent_op_name (int op
)
88 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
90 return gdb_agent_op_names
[op
];
93 #ifndef IN_PROCESS_AGENT
95 /* The packet form of an agent expression consists of an 'X', number
96 of bytes in expression, a comma, and then the bytes. */
99 gdb_parse_agent_expr (const char **actparm
)
101 const char *act
= *actparm
;
103 struct agent_expr
*aexpr
;
105 ++act
; /* skip the X */
106 act
= unpack_varlen_hex (act
, &xlen
);
107 ++act
; /* skip a comma */
108 aexpr
= XNEW (struct agent_expr
);
109 aexpr
->length
= xlen
;
110 aexpr
->bytes
= (unsigned char *) xmalloc (xlen
);
111 hex2bin (act
, aexpr
->bytes
, xlen
);
112 *actparm
= act
+ (xlen
* 2);
117 gdb_free_agent_expr (struct agent_expr
*aexpr
)
126 /* Convert the bytes of an agent expression back into hex digits, so
127 they can be printed or uploaded. This allocates the buffer,
128 callers should free when they are done with it. */
131 gdb_unparse_agent_expr (struct agent_expr
*aexpr
)
135 rslt
= (char *) xmalloc (2 * aexpr
->length
+ 1);
136 bin2hex (aexpr
->bytes
, rslt
, aexpr
->length
);
140 /* Bytecode compilation. */
142 CORE_ADDR current_insn_ptr
;
146 static struct bytecode_address
151 /* Offset and size of field to be modified in the goto block. */
152 int from_offset
, from_size
;
153 struct bytecode_address
*next
;
154 } *bytecode_address_table
;
159 target_emit_ops ()->emit_prologue ();
165 target_emit_ops ()->emit_epilogue ();
171 target_emit_ops ()->emit_add ();
177 target_emit_ops ()->emit_sub ();
183 target_emit_ops ()->emit_mul ();
189 target_emit_ops ()->emit_lsh ();
193 emit_rsh_signed (void)
195 target_emit_ops ()->emit_rsh_signed ();
199 emit_rsh_unsigned (void)
201 target_emit_ops ()->emit_rsh_unsigned ();
207 target_emit_ops ()->emit_ext (arg
);
213 target_emit_ops ()->emit_log_not ();
219 target_emit_ops ()->emit_bit_and ();
225 target_emit_ops ()->emit_bit_or ();
231 target_emit_ops ()->emit_bit_xor ();
237 target_emit_ops ()->emit_bit_not ();
243 target_emit_ops ()->emit_equal ();
247 emit_less_signed (void)
249 target_emit_ops ()->emit_less_signed ();
253 emit_less_unsigned (void)
255 target_emit_ops ()->emit_less_unsigned ();
261 target_emit_ops ()->emit_ref (size
);
265 emit_if_goto (int *offset_p
, int *size_p
)
267 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
271 emit_goto (int *offset_p
, int *size_p
)
273 target_emit_ops ()->emit_goto (offset_p
, size_p
);
277 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
279 target_emit_ops ()->write_goto_address (from
, to
, size
);
283 emit_const (LONGEST num
)
285 target_emit_ops ()->emit_const (num
);
291 target_emit_ops ()->emit_reg (reg
);
297 target_emit_ops ()->emit_pop ();
301 emit_stack_flush (void)
303 target_emit_ops ()->emit_stack_flush ();
307 emit_zero_ext (int arg
)
309 target_emit_ops ()->emit_zero_ext (arg
);
315 target_emit_ops ()->emit_swap ();
319 emit_stack_adjust (int n
)
321 target_emit_ops ()->emit_stack_adjust (n
);
324 /* FN's prototype is `LONGEST(*fn)(int)'. */
327 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
329 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
332 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
335 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
337 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
341 emit_eq_goto (int *offset_p
, int *size_p
)
343 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
347 emit_ne_goto (int *offset_p
, int *size_p
)
349 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
353 emit_lt_goto (int *offset_p
, int *size_p
)
355 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
359 emit_ge_goto (int *offset_p
, int *size_p
)
361 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
365 emit_gt_goto (int *offset_p
, int *size_p
)
367 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
371 emit_le_goto (int *offset_p
, int *size_p
)
373 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
376 /* Scan an agent expression for any evidence that the given PC is the
377 target of a jump bytecode in the expression. */
380 is_goto_target (struct agent_expr
*aexpr
, int pc
)
385 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
387 op
= aexpr
->bytes
[i
];
389 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
391 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
400 /* Given an agent expression, turn it into native code. */
402 enum eval_result_type
403 compile_bytecodes (struct agent_expr
*aexpr
)
407 unsigned char op
, next_op
;
409 /* This is only used to build 64-bit value for constants. */
411 struct bytecode_address
*aentry
, *aentry2
;
416 ax_debug ("Cannot compile op 0x%x\n", op); \
417 return expr_eval_unhandled_opcode; \
420 if (aexpr
->length
== 0)
422 ax_debug ("empty agent expression\n");
423 return expr_eval_empty_expression
;
426 bytecode_address_table
= NULL
;
430 op
= aexpr
->bytes
[pc
];
432 ax_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
434 /* Record the compiled-code address of the bytecode, for use by
435 jump instructions. */
436 aentry
= XNEW (struct bytecode_address
);
438 aentry
->address
= current_insn_ptr
;
439 aentry
->goto_pc
= -1;
440 aentry
->from_offset
= aentry
->from_size
= 0;
441 aentry
->next
= bytecode_address_table
;
442 bytecode_address_table
= aentry
;
450 case gdb_agent_op_add
:
454 case gdb_agent_op_sub
:
458 case gdb_agent_op_mul
:
462 case gdb_agent_op_div_signed
:
466 case gdb_agent_op_div_unsigned
:
470 case gdb_agent_op_rem_signed
:
474 case gdb_agent_op_rem_unsigned
:
478 case gdb_agent_op_lsh
:
482 case gdb_agent_op_rsh_signed
:
486 case gdb_agent_op_rsh_unsigned
:
487 emit_rsh_unsigned ();
490 case gdb_agent_op_trace
:
494 case gdb_agent_op_trace_quick
:
498 case gdb_agent_op_log_not
:
502 case gdb_agent_op_bit_and
:
506 case gdb_agent_op_bit_or
:
510 case gdb_agent_op_bit_xor
:
514 case gdb_agent_op_bit_not
:
518 case gdb_agent_op_equal
:
519 next_op
= aexpr
->bytes
[pc
];
520 if (next_op
== gdb_agent_op_if_goto
521 && !is_goto_target (aexpr
, pc
)
522 && target_emit_ops ()->emit_eq_goto
)
524 ax_debug ("Combining equal & if_goto");
527 arg
= aexpr
->bytes
[pc
++];
528 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
529 aentry
->goto_pc
= arg
;
530 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
532 else if (next_op
== gdb_agent_op_log_not
533 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
534 && !is_goto_target (aexpr
, pc
+ 1)
535 && target_emit_ops ()->emit_ne_goto
)
537 ax_debug ("Combining equal & log_not & if_goto");
540 arg
= aexpr
->bytes
[pc
++];
541 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
542 aentry
->goto_pc
= arg
;
543 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
549 case gdb_agent_op_less_signed
:
550 next_op
= aexpr
->bytes
[pc
];
551 if (next_op
== gdb_agent_op_if_goto
552 && !is_goto_target (aexpr
, pc
))
554 ax_debug ("Combining less_signed & if_goto");
557 arg
= aexpr
->bytes
[pc
++];
558 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
559 aentry
->goto_pc
= arg
;
560 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
562 else if (next_op
== gdb_agent_op_log_not
563 && !is_goto_target (aexpr
, pc
)
564 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
565 && !is_goto_target (aexpr
, pc
+ 1))
567 ax_debug ("Combining less_signed & log_not & if_goto");
570 arg
= aexpr
->bytes
[pc
++];
571 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
572 aentry
->goto_pc
= arg
;
573 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
579 case gdb_agent_op_less_unsigned
:
580 emit_less_unsigned ();
583 case gdb_agent_op_ext
:
584 arg
= aexpr
->bytes
[pc
++];
585 if (arg
< (sizeof (LONGEST
) * 8))
589 case gdb_agent_op_ref8
:
593 case gdb_agent_op_ref16
:
597 case gdb_agent_op_ref32
:
601 case gdb_agent_op_ref64
:
605 case gdb_agent_op_if_goto
:
606 arg
= aexpr
->bytes
[pc
++];
607 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
608 aentry
->goto_pc
= arg
;
609 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
612 case gdb_agent_op_goto
:
613 arg
= aexpr
->bytes
[pc
++];
614 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
615 aentry
->goto_pc
= arg
;
616 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
619 case gdb_agent_op_const8
:
621 top
= aexpr
->bytes
[pc
++];
625 case gdb_agent_op_const16
:
627 top
= aexpr
->bytes
[pc
++];
628 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
632 case gdb_agent_op_const32
:
634 top
= aexpr
->bytes
[pc
++];
635 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
636 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
637 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
641 case gdb_agent_op_const64
:
643 top
= 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
++];
648 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
649 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
650 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
654 case gdb_agent_op_reg
:
656 arg
= aexpr
->bytes
[pc
++];
657 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
661 case gdb_agent_op_end
:
662 ax_debug ("At end of expression\n");
664 /* Assume there is one stack element left, and that it is
665 cached in "top" where emit_epilogue can get to it. */
666 emit_stack_adjust (1);
671 case gdb_agent_op_dup
:
672 /* In our design, dup is equivalent to stack flushing. */
676 case gdb_agent_op_pop
:
680 case gdb_agent_op_zero_ext
:
681 arg
= aexpr
->bytes
[pc
++];
682 if (arg
< (sizeof (LONGEST
) * 8))
686 case gdb_agent_op_swap
:
687 next_op
= aexpr
->bytes
[pc
];
688 /* Detect greater-than comparison sequences. */
689 if (next_op
== gdb_agent_op_less_signed
690 && !is_goto_target (aexpr
, pc
)
691 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
692 && !is_goto_target (aexpr
, pc
+ 1))
694 ax_debug ("Combining swap & less_signed & if_goto");
697 arg
= aexpr
->bytes
[pc
++];
698 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
699 aentry
->goto_pc
= arg
;
700 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
702 else if (next_op
== gdb_agent_op_less_signed
703 && !is_goto_target (aexpr
, pc
)
704 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
705 && !is_goto_target (aexpr
, pc
+ 1)
706 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
707 && !is_goto_target (aexpr
, pc
+ 2))
709 ax_debug ("Combining swap & less_signed & log_not & if_goto");
712 arg
= aexpr
->bytes
[pc
++];
713 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
714 aentry
->goto_pc
= arg
;
715 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
721 case gdb_agent_op_getv
:
723 arg
= aexpr
->bytes
[pc
++];
724 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
725 emit_int_call_1 (get_get_tsv_func_addr (),
729 case gdb_agent_op_setv
:
730 arg
= aexpr
->bytes
[pc
++];
731 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
732 emit_void_call_2 (get_set_tsv_func_addr (),
736 case gdb_agent_op_tracev
:
740 /* GDB never (currently) generates any of these ops. */
741 case gdb_agent_op_float
:
742 case gdb_agent_op_ref_float
:
743 case gdb_agent_op_ref_double
:
744 case gdb_agent_op_ref_long_double
:
745 case gdb_agent_op_l_to_d
:
746 case gdb_agent_op_d_to_l
:
747 case gdb_agent_op_trace16
:
752 ax_debug ("Agent expression op 0x%x not recognized\n", op
);
753 /* Don't struggle on, things will just get worse. */
754 return expr_eval_unrecognized_opcode
;
757 /* This catches errors that occur in target-specific code
761 ax_debug ("Error %d while emitting code for %s\n",
762 emit_error
, gdb_agent_op_name (op
));
763 return expr_eval_unhandled_opcode
;
766 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
769 /* Now fill in real addresses as goto destinations. */
770 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
774 if (aentry
->goto_pc
< 0)
777 /* Find the location that we are going to, and call back into
778 target-specific code to write the actual address or
780 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
782 if (aentry2
->pc
== aentry
->goto_pc
)
784 ax_debug ("Want to jump from %s to %s\n",
785 paddress (aentry
->address
),
786 paddress (aentry2
->address
));
787 write_goto_address (aentry
->address
+ aentry
->from_offset
,
788 aentry2
->address
, aentry
->from_size
);
794 /* Error out if we didn't find a destination. */
797 ax_debug ("Destination of goto %d not found\n",
799 return expr_eval_invalid_goto
;
803 return expr_eval_no_error
;
808 /* Make printf-type calls using arguments supplied from the host. We
809 need to parse the format string ourselves, and call the formatting
810 function with one argument at a time, partly because there is no
811 safe portable way to construct a varargs call, and partly to serve
812 as a security barrier against bad format strings that might get
816 ax_printf (CORE_ADDR fn
, CORE_ADDR chan
, const char *format
,
817 int nargs
, ULONGEST
*args
)
819 const char *f
= format
;
821 const char *current_substring
;
824 ax_debug ("Printf of \"%s\" with %d args", format
, nargs
);
826 format_pieces
fpieces (&f
);
829 for (auto &&piece
: fpieces
)
830 if (piece
.argclass
!= literal_piece
)
833 if (nargs
!= nargs_wanted
)
834 error (_("Wrong number of arguments for specified format-string"));
837 for (auto &&piece
: fpieces
)
839 current_substring
= piece
.string
;
840 ax_debug ("current substring is '%s', class is %d",
841 current_substring
, piece
.argclass
);
842 switch (piece
.argclass
)
853 printf (current_substring
, "(null)");
857 /* This is a %s argument. Find the length of the string. */
862 read_inferior_memory (tem
+ j
, &c
, 1);
867 /* Copy the string contents into a string inside GDB. */
868 str
= (gdb_byte
*) alloca (j
+ 1);
870 read_inferior_memory (tem
, str
, j
);
873 printf (current_substring
, (char *) str
);
878 #if defined (PRINTF_HAS_LONG_LONG)
880 long long val
= args
[i
];
882 printf (current_substring
, val
);
886 error (_("long long not supported in agent printf"));
892 printf (current_substring
, val
);
900 printf (current_substring
, val
);
906 size_t val
= args
[i
];
908 printf (current_substring
, val
);
913 /* Print a portion of the format string that has no
914 directives. Note that this will not include any
915 ordinary %-specs, but it might include "%%". That is
916 why we use printf_filtered and not puts_filtered here.
917 Also, we pass a dummy argument because some platforms
918 have modified GCC to include -Wformat-security by
919 default, which will warn here if there is no
921 printf (current_substring
, 0);
925 error (_("Format directive in '%s' not supported in agent printf"),
929 /* Maybe advance to the next argument. */
930 if (piece
.argclass
!= literal_piece
)
937 /* The agent expression evaluator, as specified by the GDB docs. It
938 returns 0 if everything went OK, and a nonzero error code
941 enum eval_result_type
942 gdb_eval_agent_expr (struct eval_agent_expr_context
*ctx
,
943 struct agent_expr
*aexpr
,
947 #define STACK_MAX 100
948 ULONGEST stack
[STACK_MAX
], top
;
953 /* This union is a convenient way to convert representations. For
954 now, assume a standard architecture where the hardware integer
955 types have 8, 16, 32, 64 bit types. A more robust solution would
956 be to import stdint.h from gnulib. */
961 unsigned char bytes
[1];
966 unsigned char bytes
[2];
971 unsigned char bytes
[4];
976 unsigned char bytes
[8];
981 if (aexpr
->length
== 0)
983 ax_debug ("empty agent expression");
984 return expr_eval_empty_expression
;
987 /* Cache the stack top in its own variable. Much of the time we can
988 operate on this variable, rather than dinking with the stack. It
989 needs to be copied to the stack when sp changes. */
994 op
= aexpr
->bytes
[pc
++];
996 ax_debug ("About to interpret byte 0x%x", op
);
1000 case gdb_agent_op_add
:
1004 case gdb_agent_op_sub
:
1005 top
= stack
[--sp
] - top
;
1008 case gdb_agent_op_mul
:
1012 case gdb_agent_op_div_signed
:
1015 ax_debug ("Attempted to divide by zero");
1016 return expr_eval_divide_by_zero
;
1018 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
1021 case gdb_agent_op_div_unsigned
:
1024 ax_debug ("Attempted to divide by zero");
1025 return expr_eval_divide_by_zero
;
1027 top
= stack
[--sp
] / top
;
1030 case gdb_agent_op_rem_signed
:
1033 ax_debug ("Attempted to divide by zero");
1034 return expr_eval_divide_by_zero
;
1036 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
1039 case gdb_agent_op_rem_unsigned
:
1042 ax_debug ("Attempted to divide by zero");
1043 return expr_eval_divide_by_zero
;
1045 top
= stack
[--sp
] % top
;
1048 case gdb_agent_op_lsh
:
1049 top
= stack
[--sp
] << top
;
1052 case gdb_agent_op_rsh_signed
:
1053 top
= ((LONGEST
) stack
[--sp
]) >> top
;
1056 case gdb_agent_op_rsh_unsigned
:
1057 top
= stack
[--sp
] >> top
;
1060 case gdb_agent_op_trace
:
1061 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1067 case gdb_agent_op_trace_quick
:
1068 arg
= aexpr
->bytes
[pc
++];
1069 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
1072 case gdb_agent_op_log_not
:
1076 case gdb_agent_op_bit_and
:
1080 case gdb_agent_op_bit_or
:
1084 case gdb_agent_op_bit_xor
:
1088 case gdb_agent_op_bit_not
:
1092 case gdb_agent_op_equal
:
1093 top
= (stack
[--sp
] == top
);
1096 case gdb_agent_op_less_signed
:
1097 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
1100 case gdb_agent_op_less_unsigned
:
1101 top
= (stack
[--sp
] < top
);
1104 case gdb_agent_op_ext
:
1105 arg
= aexpr
->bytes
[pc
++];
1106 if (arg
< (sizeof (LONGEST
) * 8))
1108 LONGEST mask
= 1 << (arg
- 1);
1109 top
&= ((LONGEST
) 1 << arg
) - 1;
1110 top
= (top
^ mask
) - mask
;
1114 case gdb_agent_op_ref8
:
1115 agent_mem_read (ctx
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
1119 case gdb_agent_op_ref16
:
1120 agent_mem_read (ctx
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
1124 case gdb_agent_op_ref32
:
1125 agent_mem_read (ctx
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
1129 case gdb_agent_op_ref64
:
1130 agent_mem_read (ctx
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
1134 case gdb_agent_op_if_goto
:
1136 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1143 case gdb_agent_op_goto
:
1144 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1147 case gdb_agent_op_const8
:
1148 /* Flush the cached stack top. */
1150 top
= aexpr
->bytes
[pc
++];
1153 case gdb_agent_op_const16
:
1154 /* Flush the cached stack top. */
1156 top
= aexpr
->bytes
[pc
++];
1157 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1160 case gdb_agent_op_const32
:
1161 /* Flush the cached stack top. */
1163 top
= aexpr
->bytes
[pc
++];
1164 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1165 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1166 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1169 case gdb_agent_op_const64
:
1170 /* Flush the cached stack top. */
1172 top
= aexpr
->bytes
[pc
++];
1173 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1174 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1175 top
= (top
<< 8) + 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
++];
1182 case gdb_agent_op_reg
:
1183 /* Flush the cached stack top. */
1185 arg
= aexpr
->bytes
[pc
++];
1186 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1189 struct regcache
*regcache
= ctx
->regcache
;
1191 switch (register_size (regcache
->tdesc
, regnum
))
1194 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
1198 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
1202 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
1206 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
1210 internal_error ("unhandled register size");
1215 case gdb_agent_op_end
:
1216 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1217 sp
, pulongest (top
));
1222 /* This should be an error */
1223 ax_debug ("Stack is empty, nothing to return");
1224 return expr_eval_empty_stack
;
1228 return expr_eval_no_error
;
1230 case gdb_agent_op_dup
:
1234 case gdb_agent_op_pop
:
1239 case gdb_agent_op_pick
:
1240 arg
= aexpr
->bytes
[pc
++];
1242 top
= stack
[sp
- arg
];
1246 case gdb_agent_op_rot
:
1248 ULONGEST tem
= stack
[sp
- 1];
1250 stack
[sp
- 1] = stack
[sp
- 2];
1251 stack
[sp
- 2] = top
;
1256 case gdb_agent_op_zero_ext
:
1257 arg
= aexpr
->bytes
[pc
++];
1258 if (arg
< (sizeof (LONGEST
) * 8))
1259 top
&= ((LONGEST
) 1 << arg
) - 1;
1262 case gdb_agent_op_swap
:
1263 /* Interchange top two stack elements, making sure top gets
1264 copied back onto stack. */
1266 top
= stack
[sp
- 1];
1267 stack
[sp
- 1] = stack
[sp
];
1270 case gdb_agent_op_getv
:
1271 /* Flush the cached stack top. */
1273 arg
= aexpr
->bytes
[pc
++];
1274 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1275 top
= agent_get_trace_state_variable_value (arg
);
1278 case gdb_agent_op_setv
:
1279 arg
= aexpr
->bytes
[pc
++];
1280 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1281 agent_set_trace_state_variable_value (arg
, top
);
1282 /* Note that we leave the value on the stack, for the
1283 benefit of later/enclosing expressions. */
1286 case gdb_agent_op_tracev
:
1287 arg
= aexpr
->bytes
[pc
++];
1288 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1289 agent_tsv_read (ctx
, arg
);
1292 case gdb_agent_op_tracenz
:
1293 agent_mem_read_string (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1299 case gdb_agent_op_printf
:
1302 CORE_ADDR fn
= 0, chan
= 0;
1303 /* Can't have more args than the entire size of the stack. */
1304 ULONGEST args
[STACK_MAX
];
1307 nargs
= aexpr
->bytes
[pc
++];
1308 slen
= aexpr
->bytes
[pc
++];
1309 slen
= (slen
<< 8) + aexpr
->bytes
[pc
++];
1310 format
= (char *) &(aexpr
->bytes
[pc
]);
1312 /* Pop function and channel. */
1319 /* Pop arguments into a dedicated array. */
1320 for (i
= 0; i
< nargs
; ++i
)
1327 /* A bad format string means something is very wrong; give
1329 if (format
[slen
- 1] != '\0')
1330 error (_("Unterminated format string in printf bytecode"));
1332 ax_printf (fn
, chan
, format
, nargs
, args
);
1336 /* GDB never (currently) generates any of these ops. */
1337 case gdb_agent_op_float
:
1338 case gdb_agent_op_ref_float
:
1339 case gdb_agent_op_ref_double
:
1340 case gdb_agent_op_ref_long_double
:
1341 case gdb_agent_op_l_to_d
:
1342 case gdb_agent_op_d_to_l
:
1343 case gdb_agent_op_trace16
:
1344 ax_debug ("Agent expression op 0x%x valid, but not handled",
1346 /* If ever GDB generates any of these, we don't have the
1347 option of ignoring. */
1348 return expr_eval_unhandled_opcode
;
1351 ax_debug ("Agent expression op 0x%x not recognized", op
);
1352 /* Don't struggle on, things will just get worse. */
1353 return expr_eval_unrecognized_opcode
;
1356 /* Check for stack badness. */
1357 if (sp
>= (STACK_MAX
- 1))
1359 ax_debug ("Expression stack overflow");
1360 return expr_eval_stack_overflow
;
1365 ax_debug ("Expression stack underflow");
1366 return expr_eval_stack_underflow
;
1369 ax_debug ("Op %s -> sp=%d, top=0x%s",
1370 gdb_agent_op_name (op
), sp
, phex_nz (top
, 0));