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/>. */
22 #include "tracepoint.h"
24 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
26 #ifdef IN_PROCESS_AGENT
31 ax_vdebug (const char *fmt
, ...)
37 vsprintf (buf
, fmt
, ap
);
38 fprintf (stderr
, PROG
"/ax: %s\n", buf
);
42 #define ax_debug_1(level, fmt, args...) \
44 if (level <= debug_threads) \
45 ax_vdebug ((fmt), ##args); \
48 #define ax_debug(FMT, args...) \
49 ax_debug_1 (1, FMT, ##args)
51 /* This enum must exactly match what is documented in
52 gdb/doc/agentexpr.texi, including all the numerical values. */
56 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
57 gdb_agent_op_ ## NAME = VALUE,
63 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
66 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
71 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
74 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
79 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
82 gdb_agent_op_name (int op
)
84 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
86 return gdb_agent_op_names
[op
];
89 #ifndef IN_PROCESS_AGENT
91 /* The packet form of an agent expression consists of an 'X', number
92 of bytes in expression, a comma, and then the bytes. */
95 gdb_parse_agent_expr (char **actparm
)
99 struct agent_expr
*aexpr
;
101 ++act
; /* skip the X */
102 act
= unpack_varlen_hex (act
, &xlen
);
103 ++act
; /* skip a comma */
104 aexpr
= xmalloc (sizeof (struct agent_expr
));
105 aexpr
->length
= xlen
;
106 aexpr
->bytes
= xmalloc (xlen
);
107 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
108 *actparm
= act
+ (xlen
* 2);
112 /* Convert the bytes of an agent expression back into hex digits, so
113 they can be printed or uploaded. This allocates the buffer,
114 callers should free when they are done with it. */
117 gdb_unparse_agent_expr (struct agent_expr
*aexpr
)
121 rslt
= xmalloc (2 * aexpr
->length
+ 1);
122 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
126 /* Bytecode compilation. */
128 CORE_ADDR current_insn_ptr
;
132 struct bytecode_address
137 /* Offset and size of field to be modified in the goto block. */
138 int from_offset
, from_size
;
139 struct bytecode_address
*next
;
140 } *bytecode_address_table
;
145 target_emit_ops ()->emit_prologue ();
151 target_emit_ops ()->emit_epilogue ();
157 target_emit_ops ()->emit_add ();
163 target_emit_ops ()->emit_sub ();
169 target_emit_ops ()->emit_mul ();
175 target_emit_ops ()->emit_lsh ();
179 emit_rsh_signed (void)
181 target_emit_ops ()->emit_rsh_signed ();
185 emit_rsh_unsigned (void)
187 target_emit_ops ()->emit_rsh_unsigned ();
193 target_emit_ops ()->emit_ext (arg
);
199 target_emit_ops ()->emit_log_not ();
205 target_emit_ops ()->emit_bit_and ();
211 target_emit_ops ()->emit_bit_or ();
217 target_emit_ops ()->emit_bit_xor ();
223 target_emit_ops ()->emit_bit_not ();
229 target_emit_ops ()->emit_equal ();
233 emit_less_signed (void)
235 target_emit_ops ()->emit_less_signed ();
239 emit_less_unsigned (void)
241 target_emit_ops ()->emit_less_unsigned ();
247 target_emit_ops ()->emit_ref (size
);
251 emit_if_goto (int *offset_p
, int *size_p
)
253 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
257 emit_goto (int *offset_p
, int *size_p
)
259 target_emit_ops ()->emit_goto (offset_p
, size_p
);
263 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
265 target_emit_ops ()->write_goto_address (from
, to
, size
);
269 emit_const (LONGEST num
)
271 target_emit_ops ()->emit_const (num
);
277 target_emit_ops ()->emit_reg (reg
);
283 target_emit_ops ()->emit_pop ();
287 emit_stack_flush (void)
289 target_emit_ops ()->emit_stack_flush ();
293 emit_zero_ext (int arg
)
295 target_emit_ops ()->emit_zero_ext (arg
);
301 target_emit_ops ()->emit_swap ();
305 emit_stack_adjust (int n
)
307 target_emit_ops ()->emit_stack_adjust (n
);
310 /* FN's prototype is `LONGEST(*fn)(int)'. */
313 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
315 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
318 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
321 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
323 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
327 emit_eq_goto (int *offset_p
, int *size_p
)
329 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
333 emit_ne_goto (int *offset_p
, int *size_p
)
335 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
339 emit_lt_goto (int *offset_p
, int *size_p
)
341 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
345 emit_ge_goto (int *offset_p
, int *size_p
)
347 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
351 emit_gt_goto (int *offset_p
, int *size_p
)
353 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
357 emit_le_goto (int *offset_p
, int *size_p
)
359 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
362 /* Scan an agent expression for any evidence that the given PC is the
363 target of a jump bytecode in the expression. */
366 is_goto_target (struct agent_expr
*aexpr
, int pc
)
371 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
373 op
= aexpr
->bytes
[i
];
375 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
377 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
386 /* Given an agent expression, turn it into native code. */
388 enum eval_result_type
389 compile_bytecodes (struct agent_expr
*aexpr
)
393 unsigned char op
, next_op
;
395 /* This is only used to build 64-bit value for constants. */
397 struct bytecode_address
*aentry
, *aentry2
;
402 ax_debug ("Cannot compile op 0x%x\n", op); \
403 return expr_eval_unhandled_opcode; \
406 if (aexpr
->length
== 0)
408 ax_debug ("empty agent expression\n");
409 return expr_eval_empty_expression
;
412 bytecode_address_table
= NULL
;
416 op
= aexpr
->bytes
[pc
];
418 ax_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
420 /* Record the compiled-code address of the bytecode, for use by
421 jump instructions. */
422 aentry
= xmalloc (sizeof (struct bytecode_address
));
424 aentry
->address
= current_insn_ptr
;
425 aentry
->goto_pc
= -1;
426 aentry
->from_offset
= aentry
->from_size
= 0;
427 aentry
->next
= bytecode_address_table
;
428 bytecode_address_table
= aentry
;
436 case gdb_agent_op_add
:
440 case gdb_agent_op_sub
:
444 case gdb_agent_op_mul
:
448 case gdb_agent_op_div_signed
:
452 case gdb_agent_op_div_unsigned
:
456 case gdb_agent_op_rem_signed
:
460 case gdb_agent_op_rem_unsigned
:
464 case gdb_agent_op_lsh
:
468 case gdb_agent_op_rsh_signed
:
472 case gdb_agent_op_rsh_unsigned
:
473 emit_rsh_unsigned ();
476 case gdb_agent_op_trace
:
480 case gdb_agent_op_trace_quick
:
484 case gdb_agent_op_log_not
:
488 case gdb_agent_op_bit_and
:
492 case gdb_agent_op_bit_or
:
496 case gdb_agent_op_bit_xor
:
500 case gdb_agent_op_bit_not
:
504 case gdb_agent_op_equal
:
505 next_op
= aexpr
->bytes
[pc
];
506 if (next_op
== gdb_agent_op_if_goto
507 && !is_goto_target (aexpr
, pc
)
508 && target_emit_ops ()->emit_eq_goto
)
510 ax_debug ("Combining equal & if_goto");
513 arg
= aexpr
->bytes
[pc
++];
514 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
515 aentry
->goto_pc
= arg
;
516 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
518 else if (next_op
== gdb_agent_op_log_not
519 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
520 && !is_goto_target (aexpr
, pc
+ 1)
521 && target_emit_ops ()->emit_ne_goto
)
523 ax_debug ("Combining equal & log_not & if_goto");
526 arg
= aexpr
->bytes
[pc
++];
527 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
528 aentry
->goto_pc
= arg
;
529 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
535 case gdb_agent_op_less_signed
:
536 next_op
= aexpr
->bytes
[pc
];
537 if (next_op
== gdb_agent_op_if_goto
538 && !is_goto_target (aexpr
, pc
))
540 ax_debug ("Combining less_signed & if_goto");
543 arg
= aexpr
->bytes
[pc
++];
544 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
545 aentry
->goto_pc
= arg
;
546 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
548 else if (next_op
== gdb_agent_op_log_not
549 && !is_goto_target (aexpr
, pc
)
550 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
551 && !is_goto_target (aexpr
, pc
+ 1))
553 ax_debug ("Combining less_signed & log_not & if_goto");
556 arg
= aexpr
->bytes
[pc
++];
557 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
558 aentry
->goto_pc
= arg
;
559 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
565 case gdb_agent_op_less_unsigned
:
566 emit_less_unsigned ();
569 case gdb_agent_op_ext
:
570 arg
= aexpr
->bytes
[pc
++];
571 if (arg
< (sizeof (LONGEST
) * 8))
575 case gdb_agent_op_ref8
:
579 case gdb_agent_op_ref16
:
583 case gdb_agent_op_ref32
:
587 case gdb_agent_op_ref64
:
591 case gdb_agent_op_if_goto
:
592 arg
= aexpr
->bytes
[pc
++];
593 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
594 aentry
->goto_pc
= arg
;
595 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
598 case gdb_agent_op_goto
:
599 arg
= aexpr
->bytes
[pc
++];
600 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
601 aentry
->goto_pc
= arg
;
602 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
605 case gdb_agent_op_const8
:
607 top
= aexpr
->bytes
[pc
++];
611 case gdb_agent_op_const16
:
613 top
= aexpr
->bytes
[pc
++];
614 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
618 case gdb_agent_op_const32
:
620 top
= aexpr
->bytes
[pc
++];
621 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
622 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
623 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
627 case gdb_agent_op_const64
:
629 top
= 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
++];
636 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
640 case gdb_agent_op_reg
:
642 arg
= aexpr
->bytes
[pc
++];
643 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
647 case gdb_agent_op_end
:
648 ax_debug ("At end of expression\n");
650 /* Assume there is one stack element left, and that it is
651 cached in "top" where emit_epilogue can get to it. */
652 emit_stack_adjust (1);
657 case gdb_agent_op_dup
:
658 /* In our design, dup is equivalent to stack flushing. */
662 case gdb_agent_op_pop
:
666 case gdb_agent_op_zero_ext
:
667 arg
= aexpr
->bytes
[pc
++];
668 if (arg
< (sizeof (LONGEST
) * 8))
672 case gdb_agent_op_swap
:
673 next_op
= aexpr
->bytes
[pc
];
674 /* Detect greater-than comparison sequences. */
675 if (next_op
== gdb_agent_op_less_signed
676 && !is_goto_target (aexpr
, pc
)
677 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
678 && !is_goto_target (aexpr
, pc
+ 1))
680 ax_debug ("Combining swap & less_signed & if_goto");
683 arg
= aexpr
->bytes
[pc
++];
684 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
685 aentry
->goto_pc
= arg
;
686 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
688 else if (next_op
== gdb_agent_op_less_signed
689 && !is_goto_target (aexpr
, pc
)
690 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
691 && !is_goto_target (aexpr
, pc
+ 1)
692 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
693 && !is_goto_target (aexpr
, pc
+ 2))
695 ax_debug ("Combining swap & less_signed & log_not & if_goto");
698 arg
= aexpr
->bytes
[pc
++];
699 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
700 aentry
->goto_pc
= arg
;
701 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
707 case gdb_agent_op_getv
:
709 arg
= aexpr
->bytes
[pc
++];
710 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
711 emit_int_call_1 (get_get_tsv_func_addr (),
715 case gdb_agent_op_setv
:
716 arg
= aexpr
->bytes
[pc
++];
717 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
718 emit_void_call_2 (get_set_tsv_func_addr (),
722 case gdb_agent_op_tracev
:
726 /* GDB never (currently) generates any of these ops. */
727 case gdb_agent_op_float
:
728 case gdb_agent_op_ref_float
:
729 case gdb_agent_op_ref_double
:
730 case gdb_agent_op_ref_long_double
:
731 case gdb_agent_op_l_to_d
:
732 case gdb_agent_op_d_to_l
:
733 case gdb_agent_op_trace16
:
738 ax_debug ("Agent expression op 0x%x not recognized\n", op
);
739 /* Don't struggle on, things will just get worse. */
740 return expr_eval_unrecognized_opcode
;
743 /* This catches errors that occur in target-specific code
747 ax_debug ("Error %d while emitting code for %s\n",
748 emit_error
, gdb_agent_op_name (op
));
749 return expr_eval_unhandled_opcode
;
752 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
755 /* Now fill in real addresses as goto destinations. */
756 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
760 if (aentry
->goto_pc
< 0)
763 /* Find the location that we are going to, and call back into
764 target-specific code to write the actual address or
766 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
768 if (aentry2
->pc
== aentry
->goto_pc
)
770 ax_debug ("Want to jump from %s to %s\n",
771 paddress (aentry
->address
),
772 paddress (aentry2
->address
));
773 write_goto_address (aentry
->address
+ aentry
->from_offset
,
774 aentry2
->address
, aentry
->from_size
);
780 /* Error out if we didn't find a destination. */
783 ax_debug ("Destination of goto %d not found\n",
785 return expr_eval_invalid_goto
;
789 return expr_eval_no_error
;
794 /* Make printf-type calls using arguments supplied from the host. We
795 need to parse the format string ourselves, and call the formatting
796 function with one argument at a time, partly because there is no
797 safe portable way to construct a varargs call, and partly to serve
798 as a security barrier against bad format strings that might get
802 ax_printf (CORE_ADDR fn
, CORE_ADDR chan
, const char *format
,
803 int nargs
, ULONGEST
*args
)
805 const char *f
= format
;
806 struct format_piece
*fpieces
;
808 char *current_substring
;
811 ax_debug ("Printf of \"%s\" with %d args", format
, nargs
);
813 fpieces
= parse_format_string (&f
);
816 for (fp
= 0; fpieces
[fp
].string
!= NULL
; fp
++)
817 if (fpieces
[fp
].argclass
!= literal_piece
)
820 if (nargs
!= nargs_wanted
)
821 error (_("Wrong number of arguments for specified format-string"));
824 for (fp
= 0; fpieces
[fp
].string
!= NULL
; fp
++)
826 current_substring
= fpieces
[fp
].string
;
827 ax_debug ("current substring is '%s', class is %d",
828 current_substring
, fpieces
[fp
].argclass
);
829 switch (fpieces
[fp
].argclass
)
839 /* This is a %s argument. Find the length of the string. */
844 read_inferior_memory (tem
+ j
, &c
, 1);
849 /* Copy the string contents into a string inside GDB. */
850 str
= (gdb_byte
*) alloca (j
+ 1);
852 read_inferior_memory (tem
, str
, j
);
855 printf (current_substring
, (char *) str
);
860 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
862 long long val
= args
[i
];
864 printf (current_substring
, val
);
868 error (_("long long not supported in agent printf"));
874 printf (current_substring
, val
);
882 printf (current_substring
, val
);
887 /* Print a portion of the format string that has no
888 directives. Note that this will not include any
889 ordinary %-specs, but it might include "%%". That is
890 why we use printf_filtered and not puts_filtered here.
891 Also, we pass a dummy argument because some platforms
892 have modified GCC to include -Wformat-security by
893 default, which will warn here if there is no
895 printf (current_substring
, 0);
899 error (_("Format directive in '%s' not supported in agent printf"),
903 /* Maybe advance to the next argument. */
904 if (fpieces
[fp
].argclass
!= literal_piece
)
908 free_format_pieces (fpieces
);
912 /* The agent expression evaluator, as specified by the GDB docs. It
913 returns 0 if everything went OK, and a nonzero error code
916 enum eval_result_type
917 gdb_eval_agent_expr (struct eval_agent_expr_context
*ctx
,
918 struct agent_expr
*aexpr
,
922 #define STACK_MAX 100
923 ULONGEST stack
[STACK_MAX
], top
;
928 /* This union is a convenient way to convert representations. For
929 now, assume a standard architecture where the hardware integer
930 types have 8, 16, 32, 64 bit types. A more robust solution would
931 be to import stdint.h from gnulib. */
936 unsigned char bytes
[1];
941 unsigned char bytes
[2];
946 unsigned char bytes
[4];
951 unsigned char bytes
[8];
956 if (aexpr
->length
== 0)
958 ax_debug ("empty agent expression");
959 return expr_eval_empty_expression
;
962 /* Cache the stack top in its own variable. Much of the time we can
963 operate on this variable, rather than dinking with the stack. It
964 needs to be copied to the stack when sp changes. */
969 op
= aexpr
->bytes
[pc
++];
971 ax_debug ("About to interpret byte 0x%x", op
);
975 case gdb_agent_op_add
:
979 case gdb_agent_op_sub
:
980 top
= stack
[--sp
] - top
;
983 case gdb_agent_op_mul
:
987 case gdb_agent_op_div_signed
:
990 ax_debug ("Attempted to divide by zero");
991 return expr_eval_divide_by_zero
;
993 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
996 case gdb_agent_op_div_unsigned
:
999 ax_debug ("Attempted to divide by zero");
1000 return expr_eval_divide_by_zero
;
1002 top
= stack
[--sp
] / top
;
1005 case gdb_agent_op_rem_signed
:
1008 ax_debug ("Attempted to divide by zero");
1009 return expr_eval_divide_by_zero
;
1011 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
1014 case gdb_agent_op_rem_unsigned
:
1017 ax_debug ("Attempted to divide by zero");
1018 return expr_eval_divide_by_zero
;
1020 top
= stack
[--sp
] % top
;
1023 case gdb_agent_op_lsh
:
1024 top
= stack
[--sp
] << top
;
1027 case gdb_agent_op_rsh_signed
:
1028 top
= ((LONGEST
) stack
[--sp
]) >> top
;
1031 case gdb_agent_op_rsh_unsigned
:
1032 top
= stack
[--sp
] >> top
;
1035 case gdb_agent_op_trace
:
1036 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1042 case gdb_agent_op_trace_quick
:
1043 arg
= aexpr
->bytes
[pc
++];
1044 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
1047 case gdb_agent_op_log_not
:
1051 case gdb_agent_op_bit_and
:
1055 case gdb_agent_op_bit_or
:
1059 case gdb_agent_op_bit_xor
:
1063 case gdb_agent_op_bit_not
:
1067 case gdb_agent_op_equal
:
1068 top
= (stack
[--sp
] == top
);
1071 case gdb_agent_op_less_signed
:
1072 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
1075 case gdb_agent_op_less_unsigned
:
1076 top
= (stack
[--sp
] < top
);
1079 case gdb_agent_op_ext
:
1080 arg
= aexpr
->bytes
[pc
++];
1081 if (arg
< (sizeof (LONGEST
) * 8))
1083 LONGEST mask
= 1 << (arg
- 1);
1084 top
&= ((LONGEST
) 1 << arg
) - 1;
1085 top
= (top
^ mask
) - mask
;
1089 case gdb_agent_op_ref8
:
1090 agent_mem_read (ctx
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
1094 case gdb_agent_op_ref16
:
1095 agent_mem_read (ctx
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
1099 case gdb_agent_op_ref32
:
1100 agent_mem_read (ctx
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
1104 case gdb_agent_op_ref64
:
1105 agent_mem_read (ctx
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
1109 case gdb_agent_op_if_goto
:
1111 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1118 case gdb_agent_op_goto
:
1119 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1122 case gdb_agent_op_const8
:
1123 /* Flush the cached stack top. */
1125 top
= aexpr
->bytes
[pc
++];
1128 case gdb_agent_op_const16
:
1129 /* Flush the cached stack top. */
1131 top
= aexpr
->bytes
[pc
++];
1132 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1135 case gdb_agent_op_const32
:
1136 /* Flush the cached stack top. */
1138 top
= aexpr
->bytes
[pc
++];
1139 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1140 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1141 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1144 case gdb_agent_op_const64
:
1145 /* Flush the cached stack top. */
1147 top
= 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
++];
1153 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1154 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1157 case gdb_agent_op_reg
:
1158 /* Flush the cached stack top. */
1160 arg
= aexpr
->bytes
[pc
++];
1161 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1164 struct regcache
*regcache
= ctx
->regcache
;
1166 switch (register_size (regcache
->tdesc
, regnum
))
1169 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
1173 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
1177 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
1181 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
1185 internal_error (__FILE__
, __LINE__
,
1186 "unhandled register size");
1191 case gdb_agent_op_end
:
1192 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1193 sp
, pulongest (top
));
1198 /* This should be an error */
1199 ax_debug ("Stack is empty, nothing to return");
1200 return expr_eval_empty_stack
;
1204 return expr_eval_no_error
;
1206 case gdb_agent_op_dup
:
1210 case gdb_agent_op_pop
:
1215 case gdb_agent_op_pick
:
1216 arg
= aexpr
->bytes
[pc
++];
1218 top
= stack
[sp
- arg
];
1222 case gdb_agent_op_rot
:
1224 ULONGEST tem
= stack
[sp
- 1];
1226 stack
[sp
- 1] = stack
[sp
- 2];
1227 stack
[sp
- 2] = top
;
1232 case gdb_agent_op_zero_ext
:
1233 arg
= aexpr
->bytes
[pc
++];
1234 if (arg
< (sizeof (LONGEST
) * 8))
1235 top
&= ((LONGEST
) 1 << arg
) - 1;
1238 case gdb_agent_op_swap
:
1239 /* Interchange top two stack elements, making sure top gets
1240 copied back onto stack. */
1242 top
= stack
[sp
- 1];
1243 stack
[sp
- 1] = stack
[sp
];
1246 case gdb_agent_op_getv
:
1247 /* Flush the cached stack top. */
1249 arg
= aexpr
->bytes
[pc
++];
1250 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1251 top
= agent_get_trace_state_variable_value (arg
);
1254 case gdb_agent_op_setv
:
1255 arg
= aexpr
->bytes
[pc
++];
1256 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1257 agent_set_trace_state_variable_value (arg
, top
);
1258 /* Note that we leave the value on the stack, for the
1259 benefit of later/enclosing expressions. */
1262 case gdb_agent_op_tracev
:
1263 arg
= aexpr
->bytes
[pc
++];
1264 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1265 agent_tsv_read (ctx
, arg
);
1268 case gdb_agent_op_tracenz
:
1269 agent_mem_read_string (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1275 case gdb_agent_op_printf
:
1278 CORE_ADDR fn
= 0, chan
= 0;
1279 /* Can't have more args than the entire size of the stack. */
1280 ULONGEST args
[STACK_MAX
];
1283 nargs
= aexpr
->bytes
[pc
++];
1284 slen
= aexpr
->bytes
[pc
++];
1285 slen
= (slen
<< 8) + aexpr
->bytes
[pc
++];
1286 format
= (char *) &(aexpr
->bytes
[pc
]);
1288 /* Pop function and channel. */
1295 /* Pop arguments into a dedicated array. */
1296 for (i
= 0; i
< nargs
; ++i
)
1303 /* A bad format string means something is very wrong; give
1305 if (format
[slen
- 1] != '\0')
1306 error (_("Unterminated format string in printf bytecode"));
1308 ax_printf (fn
, chan
, format
, nargs
, args
);
1312 /* GDB never (currently) generates any of these ops. */
1313 case gdb_agent_op_float
:
1314 case gdb_agent_op_ref_float
:
1315 case gdb_agent_op_ref_double
:
1316 case gdb_agent_op_ref_long_double
:
1317 case gdb_agent_op_l_to_d
:
1318 case gdb_agent_op_d_to_l
:
1319 case gdb_agent_op_trace16
:
1320 ax_debug ("Agent expression op 0x%x valid, but not handled",
1322 /* If ever GDB generates any of these, we don't have the
1323 option of ignoring. */
1327 ax_debug ("Agent expression op 0x%x not recognized", op
);
1328 /* Don't struggle on, things will just get worse. */
1329 return expr_eval_unrecognized_opcode
;
1332 /* Check for stack badness. */
1333 if (sp
>= (STACK_MAX
- 1))
1335 ax_debug ("Expression stack overflow");
1336 return expr_eval_stack_overflow
;
1341 ax_debug ("Expression stack underflow");
1342 return expr_eval_stack_underflow
;
1345 ax_debug ("Op %s -> sp=%d, top=0x%s",
1346 gdb_agent_op_name (op
), sp
, phex_nz (top
, 0));