1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2014 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"
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 fprintf (stderr
, PROG
"/ax: %s\n", buf
);
43 #define ax_debug_1(level, fmt, args...) \
45 if (level <= debug_threads) \
46 ax_vdebug ((fmt), ##args); \
49 #define ax_debug(FMT, args...) \
50 ax_debug_1 (1, FMT, ##args)
52 /* This enum must exactly match what is documented in
53 gdb/doc/agentexpr.texi, including all the numerical values. */
57 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
58 gdb_agent_op_ ## NAME = VALUE,
64 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
67 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
72 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
75 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
80 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
83 gdb_agent_op_name (int op
)
85 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
87 return gdb_agent_op_names
[op
];
90 #ifndef IN_PROCESS_AGENT
92 /* The packet form of an agent expression consists of an 'X', number
93 of bytes in expression, a comma, and then the bytes. */
96 gdb_parse_agent_expr (char **actparm
)
100 struct agent_expr
*aexpr
;
102 ++act
; /* skip the X */
103 act
= unpack_varlen_hex (act
, &xlen
);
104 ++act
; /* skip a comma */
105 aexpr
= xmalloc (sizeof (struct agent_expr
));
106 aexpr
->length
= xlen
;
107 aexpr
->bytes
= xmalloc (xlen
);
108 hex2bin (act
, aexpr
->bytes
, xlen
);
109 *actparm
= act
+ (xlen
* 2);
113 /* Convert the bytes of an agent expression back into hex digits, so
114 they can be printed or uploaded. This allocates the buffer,
115 callers should free when they are done with it. */
118 gdb_unparse_agent_expr (struct agent_expr
*aexpr
)
122 rslt
= xmalloc (2 * aexpr
->length
+ 1);
123 bin2hex (aexpr
->bytes
, rslt
, aexpr
->length
);
127 /* Bytecode compilation. */
129 CORE_ADDR current_insn_ptr
;
133 struct bytecode_address
138 /* Offset and size of field to be modified in the goto block. */
139 int from_offset
, from_size
;
140 struct bytecode_address
*next
;
141 } *bytecode_address_table
;
146 target_emit_ops ()->emit_prologue ();
152 target_emit_ops ()->emit_epilogue ();
158 target_emit_ops ()->emit_add ();
164 target_emit_ops ()->emit_sub ();
170 target_emit_ops ()->emit_mul ();
176 target_emit_ops ()->emit_lsh ();
180 emit_rsh_signed (void)
182 target_emit_ops ()->emit_rsh_signed ();
186 emit_rsh_unsigned (void)
188 target_emit_ops ()->emit_rsh_unsigned ();
194 target_emit_ops ()->emit_ext (arg
);
200 target_emit_ops ()->emit_log_not ();
206 target_emit_ops ()->emit_bit_and ();
212 target_emit_ops ()->emit_bit_or ();
218 target_emit_ops ()->emit_bit_xor ();
224 target_emit_ops ()->emit_bit_not ();
230 target_emit_ops ()->emit_equal ();
234 emit_less_signed (void)
236 target_emit_ops ()->emit_less_signed ();
240 emit_less_unsigned (void)
242 target_emit_ops ()->emit_less_unsigned ();
248 target_emit_ops ()->emit_ref (size
);
252 emit_if_goto (int *offset_p
, int *size_p
)
254 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
258 emit_goto (int *offset_p
, int *size_p
)
260 target_emit_ops ()->emit_goto (offset_p
, size_p
);
264 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
266 target_emit_ops ()->write_goto_address (from
, to
, size
);
270 emit_const (LONGEST num
)
272 target_emit_ops ()->emit_const (num
);
278 target_emit_ops ()->emit_reg (reg
);
284 target_emit_ops ()->emit_pop ();
288 emit_stack_flush (void)
290 target_emit_ops ()->emit_stack_flush ();
294 emit_zero_ext (int arg
)
296 target_emit_ops ()->emit_zero_ext (arg
);
302 target_emit_ops ()->emit_swap ();
306 emit_stack_adjust (int n
)
308 target_emit_ops ()->emit_stack_adjust (n
);
311 /* FN's prototype is `LONGEST(*fn)(int)'. */
314 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
316 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
319 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
322 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
324 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
328 emit_eq_goto (int *offset_p
, int *size_p
)
330 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
334 emit_ne_goto (int *offset_p
, int *size_p
)
336 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
340 emit_lt_goto (int *offset_p
, int *size_p
)
342 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
346 emit_ge_goto (int *offset_p
, int *size_p
)
348 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
352 emit_gt_goto (int *offset_p
, int *size_p
)
354 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
358 emit_le_goto (int *offset_p
, int *size_p
)
360 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
363 /* Scan an agent expression for any evidence that the given PC is the
364 target of a jump bytecode in the expression. */
367 is_goto_target (struct agent_expr
*aexpr
, int pc
)
372 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
374 op
= aexpr
->bytes
[i
];
376 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
378 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
387 /* Given an agent expression, turn it into native code. */
389 enum eval_result_type
390 compile_bytecodes (struct agent_expr
*aexpr
)
394 unsigned char op
, next_op
;
396 /* This is only used to build 64-bit value for constants. */
398 struct bytecode_address
*aentry
, *aentry2
;
403 ax_debug ("Cannot compile op 0x%x\n", op); \
404 return expr_eval_unhandled_opcode; \
407 if (aexpr
->length
== 0)
409 ax_debug ("empty agent expression\n");
410 return expr_eval_empty_expression
;
413 bytecode_address_table
= NULL
;
417 op
= aexpr
->bytes
[pc
];
419 ax_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
421 /* Record the compiled-code address of the bytecode, for use by
422 jump instructions. */
423 aentry
= xmalloc (sizeof (struct bytecode_address
));
425 aentry
->address
= current_insn_ptr
;
426 aentry
->goto_pc
= -1;
427 aentry
->from_offset
= aentry
->from_size
= 0;
428 aentry
->next
= bytecode_address_table
;
429 bytecode_address_table
= aentry
;
437 case gdb_agent_op_add
:
441 case gdb_agent_op_sub
:
445 case gdb_agent_op_mul
:
449 case gdb_agent_op_div_signed
:
453 case gdb_agent_op_div_unsigned
:
457 case gdb_agent_op_rem_signed
:
461 case gdb_agent_op_rem_unsigned
:
465 case gdb_agent_op_lsh
:
469 case gdb_agent_op_rsh_signed
:
473 case gdb_agent_op_rsh_unsigned
:
474 emit_rsh_unsigned ();
477 case gdb_agent_op_trace
:
481 case gdb_agent_op_trace_quick
:
485 case gdb_agent_op_log_not
:
489 case gdb_agent_op_bit_and
:
493 case gdb_agent_op_bit_or
:
497 case gdb_agent_op_bit_xor
:
501 case gdb_agent_op_bit_not
:
505 case gdb_agent_op_equal
:
506 next_op
= aexpr
->bytes
[pc
];
507 if (next_op
== gdb_agent_op_if_goto
508 && !is_goto_target (aexpr
, pc
)
509 && target_emit_ops ()->emit_eq_goto
)
511 ax_debug ("Combining equal & if_goto");
514 arg
= aexpr
->bytes
[pc
++];
515 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
516 aentry
->goto_pc
= arg
;
517 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
519 else if (next_op
== gdb_agent_op_log_not
520 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
521 && !is_goto_target (aexpr
, pc
+ 1)
522 && target_emit_ops ()->emit_ne_goto
)
524 ax_debug ("Combining equal & log_not & if_goto");
527 arg
= aexpr
->bytes
[pc
++];
528 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
529 aentry
->goto_pc
= arg
;
530 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
536 case gdb_agent_op_less_signed
:
537 next_op
= aexpr
->bytes
[pc
];
538 if (next_op
== gdb_agent_op_if_goto
539 && !is_goto_target (aexpr
, pc
))
541 ax_debug ("Combining less_signed & if_goto");
544 arg
= aexpr
->bytes
[pc
++];
545 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
546 aentry
->goto_pc
= arg
;
547 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
549 else if (next_op
== gdb_agent_op_log_not
550 && !is_goto_target (aexpr
, pc
)
551 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
552 && !is_goto_target (aexpr
, pc
+ 1))
554 ax_debug ("Combining less_signed & log_not & if_goto");
557 arg
= aexpr
->bytes
[pc
++];
558 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
559 aentry
->goto_pc
= arg
;
560 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
566 case gdb_agent_op_less_unsigned
:
567 emit_less_unsigned ();
570 case gdb_agent_op_ext
:
571 arg
= aexpr
->bytes
[pc
++];
572 if (arg
< (sizeof (LONGEST
) * 8))
576 case gdb_agent_op_ref8
:
580 case gdb_agent_op_ref16
:
584 case gdb_agent_op_ref32
:
588 case gdb_agent_op_ref64
:
592 case gdb_agent_op_if_goto
:
593 arg
= aexpr
->bytes
[pc
++];
594 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
595 aentry
->goto_pc
= arg
;
596 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
599 case gdb_agent_op_goto
:
600 arg
= aexpr
->bytes
[pc
++];
601 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
602 aentry
->goto_pc
= arg
;
603 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
606 case gdb_agent_op_const8
:
608 top
= aexpr
->bytes
[pc
++];
612 case gdb_agent_op_const16
:
614 top
= aexpr
->bytes
[pc
++];
615 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
619 case gdb_agent_op_const32
:
621 top
= aexpr
->bytes
[pc
++];
622 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
623 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
624 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
628 case gdb_agent_op_const64
:
630 top
= 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
++];
637 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
641 case gdb_agent_op_reg
:
643 arg
= aexpr
->bytes
[pc
++];
644 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
648 case gdb_agent_op_end
:
649 ax_debug ("At end of expression\n");
651 /* Assume there is one stack element left, and that it is
652 cached in "top" where emit_epilogue can get to it. */
653 emit_stack_adjust (1);
658 case gdb_agent_op_dup
:
659 /* In our design, dup is equivalent to stack flushing. */
663 case gdb_agent_op_pop
:
667 case gdb_agent_op_zero_ext
:
668 arg
= aexpr
->bytes
[pc
++];
669 if (arg
< (sizeof (LONGEST
) * 8))
673 case gdb_agent_op_swap
:
674 next_op
= aexpr
->bytes
[pc
];
675 /* Detect greater-than comparison sequences. */
676 if (next_op
== gdb_agent_op_less_signed
677 && !is_goto_target (aexpr
, pc
)
678 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
679 && !is_goto_target (aexpr
, pc
+ 1))
681 ax_debug ("Combining swap & less_signed & if_goto");
684 arg
= aexpr
->bytes
[pc
++];
685 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
686 aentry
->goto_pc
= arg
;
687 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
689 else if (next_op
== gdb_agent_op_less_signed
690 && !is_goto_target (aexpr
, pc
)
691 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
692 && !is_goto_target (aexpr
, pc
+ 1)
693 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
694 && !is_goto_target (aexpr
, pc
+ 2))
696 ax_debug ("Combining swap & less_signed & log_not & if_goto");
699 arg
= aexpr
->bytes
[pc
++];
700 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
701 aentry
->goto_pc
= arg
;
702 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
708 case gdb_agent_op_getv
:
710 arg
= aexpr
->bytes
[pc
++];
711 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
712 emit_int_call_1 (get_get_tsv_func_addr (),
716 case gdb_agent_op_setv
:
717 arg
= aexpr
->bytes
[pc
++];
718 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
719 emit_void_call_2 (get_set_tsv_func_addr (),
723 case gdb_agent_op_tracev
:
727 /* GDB never (currently) generates any of these ops. */
728 case gdb_agent_op_float
:
729 case gdb_agent_op_ref_float
:
730 case gdb_agent_op_ref_double
:
731 case gdb_agent_op_ref_long_double
:
732 case gdb_agent_op_l_to_d
:
733 case gdb_agent_op_d_to_l
:
734 case gdb_agent_op_trace16
:
739 ax_debug ("Agent expression op 0x%x not recognized\n", op
);
740 /* Don't struggle on, things will just get worse. */
741 return expr_eval_unrecognized_opcode
;
744 /* This catches errors that occur in target-specific code
748 ax_debug ("Error %d while emitting code for %s\n",
749 emit_error
, gdb_agent_op_name (op
));
750 return expr_eval_unhandled_opcode
;
753 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
756 /* Now fill in real addresses as goto destinations. */
757 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
761 if (aentry
->goto_pc
< 0)
764 /* Find the location that we are going to, and call back into
765 target-specific code to write the actual address or
767 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
769 if (aentry2
->pc
== aentry
->goto_pc
)
771 ax_debug ("Want to jump from %s to %s\n",
772 paddress (aentry
->address
),
773 paddress (aentry2
->address
));
774 write_goto_address (aentry
->address
+ aentry
->from_offset
,
775 aentry2
->address
, aentry
->from_size
);
781 /* Error out if we didn't find a destination. */
784 ax_debug ("Destination of goto %d not found\n",
786 return expr_eval_invalid_goto
;
790 return expr_eval_no_error
;
795 /* Make printf-type calls using arguments supplied from the host. We
796 need to parse the format string ourselves, and call the formatting
797 function with one argument at a time, partly because there is no
798 safe portable way to construct a varargs call, and partly to serve
799 as a security barrier against bad format strings that might get
803 ax_printf (CORE_ADDR fn
, CORE_ADDR chan
, const char *format
,
804 int nargs
, ULONGEST
*args
)
806 const char *f
= format
;
807 struct format_piece
*fpieces
;
809 char *current_substring
;
812 ax_debug ("Printf of \"%s\" with %d args", format
, nargs
);
814 fpieces
= parse_format_string (&f
);
817 for (fp
= 0; fpieces
[fp
].string
!= NULL
; fp
++)
818 if (fpieces
[fp
].argclass
!= literal_piece
)
821 if (nargs
!= nargs_wanted
)
822 error (_("Wrong number of arguments for specified format-string"));
825 for (fp
= 0; fpieces
[fp
].string
!= NULL
; fp
++)
827 current_substring
= fpieces
[fp
].string
;
828 ax_debug ("current substring is '%s', class is %d",
829 current_substring
, fpieces
[fp
].argclass
);
830 switch (fpieces
[fp
].argclass
)
840 /* This is a %s argument. Find the length of the string. */
845 read_inferior_memory (tem
+ j
, &c
, 1);
850 /* Copy the string contents into a string inside GDB. */
851 str
= (gdb_byte
*) alloca (j
+ 1);
853 read_inferior_memory (tem
, str
, j
);
856 printf (current_substring
, (char *) str
);
861 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
863 long long val
= args
[i
];
865 printf (current_substring
, val
);
869 error (_("long long not supported in agent printf"));
875 printf (current_substring
, val
);
883 printf (current_substring
, val
);
888 /* Print a portion of the format string that has no
889 directives. Note that this will not include any
890 ordinary %-specs, but it might include "%%". That is
891 why we use printf_filtered and not puts_filtered here.
892 Also, we pass a dummy argument because some platforms
893 have modified GCC to include -Wformat-security by
894 default, which will warn here if there is no
896 printf (current_substring
, 0);
900 error (_("Format directive in '%s' not supported in agent printf"),
904 /* Maybe advance to the next argument. */
905 if (fpieces
[fp
].argclass
!= literal_piece
)
909 free_format_pieces (fpieces
);
913 /* The agent expression evaluator, as specified by the GDB docs. It
914 returns 0 if everything went OK, and a nonzero error code
917 enum eval_result_type
918 gdb_eval_agent_expr (struct eval_agent_expr_context
*ctx
,
919 struct agent_expr
*aexpr
,
923 #define STACK_MAX 100
924 ULONGEST stack
[STACK_MAX
], top
;
929 /* This union is a convenient way to convert representations. For
930 now, assume a standard architecture where the hardware integer
931 types have 8, 16, 32, 64 bit types. A more robust solution would
932 be to import stdint.h from gnulib. */
937 unsigned char bytes
[1];
942 unsigned char bytes
[2];
947 unsigned char bytes
[4];
952 unsigned char bytes
[8];
957 if (aexpr
->length
== 0)
959 ax_debug ("empty agent expression");
960 return expr_eval_empty_expression
;
963 /* Cache the stack top in its own variable. Much of the time we can
964 operate on this variable, rather than dinking with the stack. It
965 needs to be copied to the stack when sp changes. */
970 op
= aexpr
->bytes
[pc
++];
972 ax_debug ("About to interpret byte 0x%x", op
);
976 case gdb_agent_op_add
:
980 case gdb_agent_op_sub
:
981 top
= stack
[--sp
] - top
;
984 case gdb_agent_op_mul
:
988 case gdb_agent_op_div_signed
:
991 ax_debug ("Attempted to divide by zero");
992 return expr_eval_divide_by_zero
;
994 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
997 case gdb_agent_op_div_unsigned
:
1000 ax_debug ("Attempted to divide by zero");
1001 return expr_eval_divide_by_zero
;
1003 top
= stack
[--sp
] / top
;
1006 case gdb_agent_op_rem_signed
:
1009 ax_debug ("Attempted to divide by zero");
1010 return expr_eval_divide_by_zero
;
1012 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
1015 case gdb_agent_op_rem_unsigned
:
1018 ax_debug ("Attempted to divide by zero");
1019 return expr_eval_divide_by_zero
;
1021 top
= stack
[--sp
] % top
;
1024 case gdb_agent_op_lsh
:
1025 top
= stack
[--sp
] << top
;
1028 case gdb_agent_op_rsh_signed
:
1029 top
= ((LONGEST
) stack
[--sp
]) >> top
;
1032 case gdb_agent_op_rsh_unsigned
:
1033 top
= stack
[--sp
] >> top
;
1036 case gdb_agent_op_trace
:
1037 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1043 case gdb_agent_op_trace_quick
:
1044 arg
= aexpr
->bytes
[pc
++];
1045 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
1048 case gdb_agent_op_log_not
:
1052 case gdb_agent_op_bit_and
:
1056 case gdb_agent_op_bit_or
:
1060 case gdb_agent_op_bit_xor
:
1064 case gdb_agent_op_bit_not
:
1068 case gdb_agent_op_equal
:
1069 top
= (stack
[--sp
] == top
);
1072 case gdb_agent_op_less_signed
:
1073 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
1076 case gdb_agent_op_less_unsigned
:
1077 top
= (stack
[--sp
] < top
);
1080 case gdb_agent_op_ext
:
1081 arg
= aexpr
->bytes
[pc
++];
1082 if (arg
< (sizeof (LONGEST
) * 8))
1084 LONGEST mask
= 1 << (arg
- 1);
1085 top
&= ((LONGEST
) 1 << arg
) - 1;
1086 top
= (top
^ mask
) - mask
;
1090 case gdb_agent_op_ref8
:
1091 agent_mem_read (ctx
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
1095 case gdb_agent_op_ref16
:
1096 agent_mem_read (ctx
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
1100 case gdb_agent_op_ref32
:
1101 agent_mem_read (ctx
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
1105 case gdb_agent_op_ref64
:
1106 agent_mem_read (ctx
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
1110 case gdb_agent_op_if_goto
:
1112 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1119 case gdb_agent_op_goto
:
1120 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1123 case gdb_agent_op_const8
:
1124 /* Flush the cached stack top. */
1126 top
= aexpr
->bytes
[pc
++];
1129 case gdb_agent_op_const16
:
1130 /* Flush the cached stack top. */
1132 top
= aexpr
->bytes
[pc
++];
1133 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1136 case gdb_agent_op_const32
:
1137 /* Flush the cached stack top. */
1139 top
= aexpr
->bytes
[pc
++];
1140 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1141 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1142 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1145 case gdb_agent_op_const64
:
1146 /* Flush the cached stack top. */
1148 top
= 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
++];
1155 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1158 case gdb_agent_op_reg
:
1159 /* Flush the cached stack top. */
1161 arg
= aexpr
->bytes
[pc
++];
1162 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1165 struct regcache
*regcache
= ctx
->regcache
;
1167 switch (register_size (regcache
->tdesc
, regnum
))
1170 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
1174 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
1178 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
1182 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
1186 internal_error (__FILE__
, __LINE__
,
1187 "unhandled register size");
1192 case gdb_agent_op_end
:
1193 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1194 sp
, pulongest (top
));
1199 /* This should be an error */
1200 ax_debug ("Stack is empty, nothing to return");
1201 return expr_eval_empty_stack
;
1205 return expr_eval_no_error
;
1207 case gdb_agent_op_dup
:
1211 case gdb_agent_op_pop
:
1216 case gdb_agent_op_pick
:
1217 arg
= aexpr
->bytes
[pc
++];
1219 top
= stack
[sp
- arg
];
1223 case gdb_agent_op_rot
:
1225 ULONGEST tem
= stack
[sp
- 1];
1227 stack
[sp
- 1] = stack
[sp
- 2];
1228 stack
[sp
- 2] = top
;
1233 case gdb_agent_op_zero_ext
:
1234 arg
= aexpr
->bytes
[pc
++];
1235 if (arg
< (sizeof (LONGEST
) * 8))
1236 top
&= ((LONGEST
) 1 << arg
) - 1;
1239 case gdb_agent_op_swap
:
1240 /* Interchange top two stack elements, making sure top gets
1241 copied back onto stack. */
1243 top
= stack
[sp
- 1];
1244 stack
[sp
- 1] = stack
[sp
];
1247 case gdb_agent_op_getv
:
1248 /* Flush the cached stack top. */
1250 arg
= aexpr
->bytes
[pc
++];
1251 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1252 top
= agent_get_trace_state_variable_value (arg
);
1255 case gdb_agent_op_setv
:
1256 arg
= aexpr
->bytes
[pc
++];
1257 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1258 agent_set_trace_state_variable_value (arg
, top
);
1259 /* Note that we leave the value on the stack, for the
1260 benefit of later/enclosing expressions. */
1263 case gdb_agent_op_tracev
:
1264 arg
= aexpr
->bytes
[pc
++];
1265 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1266 agent_tsv_read (ctx
, arg
);
1269 case gdb_agent_op_tracenz
:
1270 agent_mem_read_string (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1276 case gdb_agent_op_printf
:
1279 CORE_ADDR fn
= 0, chan
= 0;
1280 /* Can't have more args than the entire size of the stack. */
1281 ULONGEST args
[STACK_MAX
];
1284 nargs
= aexpr
->bytes
[pc
++];
1285 slen
= aexpr
->bytes
[pc
++];
1286 slen
= (slen
<< 8) + aexpr
->bytes
[pc
++];
1287 format
= (char *) &(aexpr
->bytes
[pc
]);
1289 /* Pop function and channel. */
1296 /* Pop arguments into a dedicated array. */
1297 for (i
= 0; i
< nargs
; ++i
)
1304 /* A bad format string means something is very wrong; give
1306 if (format
[slen
- 1] != '\0')
1307 error (_("Unterminated format string in printf bytecode"));
1309 ax_printf (fn
, chan
, format
, nargs
, args
);
1313 /* GDB never (currently) generates any of these ops. */
1314 case gdb_agent_op_float
:
1315 case gdb_agent_op_ref_float
:
1316 case gdb_agent_op_ref_double
:
1317 case gdb_agent_op_ref_long_double
:
1318 case gdb_agent_op_l_to_d
:
1319 case gdb_agent_op_d_to_l
:
1320 case gdb_agent_op_trace16
:
1321 ax_debug ("Agent expression op 0x%x valid, but not handled",
1323 /* If ever GDB generates any of these, we don't have the
1324 option of ignoring. */
1328 ax_debug ("Agent expression op 0x%x not recognized", op
);
1329 /* Don't struggle on, things will just get worse. */
1330 return expr_eval_unrecognized_opcode
;
1333 /* Check for stack badness. */
1334 if (sp
>= (STACK_MAX
- 1))
1336 ax_debug ("Expression stack overflow");
1337 return expr_eval_stack_overflow
;
1342 ax_debug ("Expression stack underflow");
1343 return expr_eval_stack_underflow
;
1346 ax_debug ("Op %s -> sp=%d, top=0x%s",
1347 gdb_agent_op_name (op
), sp
, phex_nz (top
, 0));