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/>. */
23 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
25 #ifdef IN_PROCESS_AGENT
30 ax_vdebug (const char *fmt
, ...)
36 vsprintf (buf
, fmt
, ap
);
37 fprintf (stderr
, PROG
"/ax: %s\n", buf
);
41 #define ax_debug_1(level, fmt, args...) \
43 if (level <= debug_threads) \
44 ax_vdebug ((fmt), ##args); \
47 #define ax_debug(FMT, args...) \
48 ax_debug_1 (1, FMT, ##args)
50 /* This enum must exactly match what is documented in
51 gdb/doc/agentexpr.texi, including all the numerical values. */
55 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
56 gdb_agent_op_ ## NAME = VALUE,
62 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
65 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
70 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
73 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
78 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
81 gdb_agent_op_name (int op
)
83 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
85 return gdb_agent_op_names
[op
];
88 #ifndef IN_PROCESS_AGENT
90 /* The packet form of an agent expression consists of an 'X', number
91 of bytes in expression, a comma, and then the bytes. */
94 gdb_parse_agent_expr (char **actparm
)
98 struct agent_expr
*aexpr
;
100 ++act
; /* skip the X */
101 act
= unpack_varlen_hex (act
, &xlen
);
102 ++act
; /* skip a comma */
103 aexpr
= xmalloc (sizeof (struct agent_expr
));
104 aexpr
->length
= xlen
;
105 aexpr
->bytes
= xmalloc (xlen
);
106 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
107 *actparm
= act
+ (xlen
* 2);
111 /* Convert the bytes of an agent expression back into hex digits, so
112 they can be printed or uploaded. This allocates the buffer,
113 callers should free when they are done with it. */
116 gdb_unparse_agent_expr (struct agent_expr
*aexpr
)
120 rslt
= xmalloc (2 * aexpr
->length
+ 1);
121 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
125 /* Bytecode compilation. */
127 CORE_ADDR current_insn_ptr
;
131 struct bytecode_address
136 /* Offset and size of field to be modified in the goto block. */
137 int from_offset
, from_size
;
138 struct bytecode_address
*next
;
139 } *bytecode_address_table
;
144 target_emit_ops ()->emit_prologue ();
150 target_emit_ops ()->emit_epilogue ();
156 target_emit_ops ()->emit_add ();
162 target_emit_ops ()->emit_sub ();
168 target_emit_ops ()->emit_mul ();
174 target_emit_ops ()->emit_lsh ();
178 emit_rsh_signed (void)
180 target_emit_ops ()->emit_rsh_signed ();
184 emit_rsh_unsigned (void)
186 target_emit_ops ()->emit_rsh_unsigned ();
192 target_emit_ops ()->emit_ext (arg
);
198 target_emit_ops ()->emit_log_not ();
204 target_emit_ops ()->emit_bit_and ();
210 target_emit_ops ()->emit_bit_or ();
216 target_emit_ops ()->emit_bit_xor ();
222 target_emit_ops ()->emit_bit_not ();
228 target_emit_ops ()->emit_equal ();
232 emit_less_signed (void)
234 target_emit_ops ()->emit_less_signed ();
238 emit_less_unsigned (void)
240 target_emit_ops ()->emit_less_unsigned ();
246 target_emit_ops ()->emit_ref (size
);
250 emit_if_goto (int *offset_p
, int *size_p
)
252 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
256 emit_goto (int *offset_p
, int *size_p
)
258 target_emit_ops ()->emit_goto (offset_p
, size_p
);
262 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
264 target_emit_ops ()->write_goto_address (from
, to
, size
);
268 emit_const (LONGEST num
)
270 target_emit_ops ()->emit_const (num
);
276 target_emit_ops ()->emit_reg (reg
);
282 target_emit_ops ()->emit_pop ();
286 emit_stack_flush (void)
288 target_emit_ops ()->emit_stack_flush ();
292 emit_zero_ext (int arg
)
294 target_emit_ops ()->emit_zero_ext (arg
);
300 target_emit_ops ()->emit_swap ();
304 emit_stack_adjust (int n
)
306 target_emit_ops ()->emit_stack_adjust (n
);
309 /* FN's prototype is `LONGEST(*fn)(int)'. */
312 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
314 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
317 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
320 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
322 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
326 emit_eq_goto (int *offset_p
, int *size_p
)
328 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
332 emit_ne_goto (int *offset_p
, int *size_p
)
334 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
338 emit_lt_goto (int *offset_p
, int *size_p
)
340 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
344 emit_ge_goto (int *offset_p
, int *size_p
)
346 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
350 emit_gt_goto (int *offset_p
, int *size_p
)
352 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
356 emit_le_goto (int *offset_p
, int *size_p
)
358 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
361 /* Scan an agent expression for any evidence that the given PC is the
362 target of a jump bytecode in the expression. */
365 is_goto_target (struct agent_expr
*aexpr
, int pc
)
370 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
372 op
= aexpr
->bytes
[i
];
374 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
376 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
385 /* Given an agent expression, turn it into native code. */
387 enum eval_result_type
388 compile_bytecodes (struct agent_expr
*aexpr
)
392 unsigned char op
, next_op
;
394 /* This is only used to build 64-bit value for constants. */
396 struct bytecode_address
*aentry
, *aentry2
;
401 ax_debug ("Cannot compile op 0x%x\n", op); \
402 return expr_eval_unhandled_opcode; \
405 if (aexpr
->length
== 0)
407 ax_debug ("empty agent expression\n");
408 return expr_eval_empty_expression
;
411 bytecode_address_table
= NULL
;
415 op
= aexpr
->bytes
[pc
];
417 ax_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
419 /* Record the compiled-code address of the bytecode, for use by
420 jump instructions. */
421 aentry
= xmalloc (sizeof (struct bytecode_address
));
423 aentry
->address
= current_insn_ptr
;
424 aentry
->goto_pc
= -1;
425 aentry
->from_offset
= aentry
->from_size
= 0;
426 aentry
->next
= bytecode_address_table
;
427 bytecode_address_table
= aentry
;
435 case gdb_agent_op_add
:
439 case gdb_agent_op_sub
:
443 case gdb_agent_op_mul
:
447 case gdb_agent_op_div_signed
:
451 case gdb_agent_op_div_unsigned
:
455 case gdb_agent_op_rem_signed
:
459 case gdb_agent_op_rem_unsigned
:
463 case gdb_agent_op_lsh
:
467 case gdb_agent_op_rsh_signed
:
471 case gdb_agent_op_rsh_unsigned
:
472 emit_rsh_unsigned ();
475 case gdb_agent_op_trace
:
479 case gdb_agent_op_trace_quick
:
483 case gdb_agent_op_log_not
:
487 case gdb_agent_op_bit_and
:
491 case gdb_agent_op_bit_or
:
495 case gdb_agent_op_bit_xor
:
499 case gdb_agent_op_bit_not
:
503 case gdb_agent_op_equal
:
504 next_op
= aexpr
->bytes
[pc
];
505 if (next_op
== gdb_agent_op_if_goto
506 && !is_goto_target (aexpr
, pc
)
507 && target_emit_ops ()->emit_eq_goto
)
509 ax_debug ("Combining equal & if_goto");
512 arg
= aexpr
->bytes
[pc
++];
513 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
514 aentry
->goto_pc
= arg
;
515 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
517 else if (next_op
== gdb_agent_op_log_not
518 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
519 && !is_goto_target (aexpr
, pc
+ 1)
520 && target_emit_ops ()->emit_ne_goto
)
522 ax_debug ("Combining equal & log_not & if_goto");
525 arg
= aexpr
->bytes
[pc
++];
526 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
527 aentry
->goto_pc
= arg
;
528 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
534 case gdb_agent_op_less_signed
:
535 next_op
= aexpr
->bytes
[pc
];
536 if (next_op
== gdb_agent_op_if_goto
537 && !is_goto_target (aexpr
, pc
))
539 ax_debug ("Combining less_signed & if_goto");
542 arg
= aexpr
->bytes
[pc
++];
543 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
544 aentry
->goto_pc
= arg
;
545 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
547 else if (next_op
== gdb_agent_op_log_not
548 && !is_goto_target (aexpr
, pc
)
549 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
550 && !is_goto_target (aexpr
, pc
+ 1))
552 ax_debug ("Combining less_signed & log_not & if_goto");
555 arg
= aexpr
->bytes
[pc
++];
556 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
557 aentry
->goto_pc
= arg
;
558 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
564 case gdb_agent_op_less_unsigned
:
565 emit_less_unsigned ();
568 case gdb_agent_op_ext
:
569 arg
= aexpr
->bytes
[pc
++];
570 if (arg
< (sizeof (LONGEST
) * 8))
574 case gdb_agent_op_ref8
:
578 case gdb_agent_op_ref16
:
582 case gdb_agent_op_ref32
:
586 case gdb_agent_op_ref64
:
590 case gdb_agent_op_if_goto
:
591 arg
= aexpr
->bytes
[pc
++];
592 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
593 aentry
->goto_pc
= arg
;
594 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
597 case gdb_agent_op_goto
:
598 arg
= aexpr
->bytes
[pc
++];
599 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
600 aentry
->goto_pc
= arg
;
601 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
604 case gdb_agent_op_const8
:
606 top
= aexpr
->bytes
[pc
++];
610 case gdb_agent_op_const16
:
612 top
= aexpr
->bytes
[pc
++];
613 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
617 case gdb_agent_op_const32
:
619 top
= aexpr
->bytes
[pc
++];
620 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
621 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
622 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
626 case gdb_agent_op_const64
:
628 top
= aexpr
->bytes
[pc
++];
629 top
= (top
<< 8) + 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
++];
639 case gdb_agent_op_reg
:
641 arg
= aexpr
->bytes
[pc
++];
642 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
646 case gdb_agent_op_end
:
647 ax_debug ("At end of expression\n");
649 /* Assume there is one stack element left, and that it is
650 cached in "top" where emit_epilogue can get to it. */
651 emit_stack_adjust (1);
656 case gdb_agent_op_dup
:
657 /* In our design, dup is equivalent to stack flushing. */
661 case gdb_agent_op_pop
:
665 case gdb_agent_op_zero_ext
:
666 arg
= aexpr
->bytes
[pc
++];
667 if (arg
< (sizeof (LONGEST
) * 8))
671 case gdb_agent_op_swap
:
672 next_op
= aexpr
->bytes
[pc
];
673 /* Detect greater-than comparison sequences. */
674 if (next_op
== gdb_agent_op_less_signed
675 && !is_goto_target (aexpr
, pc
)
676 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
677 && !is_goto_target (aexpr
, pc
+ 1))
679 ax_debug ("Combining swap & less_signed & if_goto");
682 arg
= aexpr
->bytes
[pc
++];
683 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
684 aentry
->goto_pc
= arg
;
685 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
687 else if (next_op
== gdb_agent_op_less_signed
688 && !is_goto_target (aexpr
, pc
)
689 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
690 && !is_goto_target (aexpr
, pc
+ 1)
691 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
692 && !is_goto_target (aexpr
, pc
+ 2))
694 ax_debug ("Combining swap & less_signed & log_not & if_goto");
697 arg
= aexpr
->bytes
[pc
++];
698 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
699 aentry
->goto_pc
= arg
;
700 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
706 case gdb_agent_op_getv
:
708 arg
= aexpr
->bytes
[pc
++];
709 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
710 emit_int_call_1 (get_get_tsv_func_addr (),
714 case gdb_agent_op_setv
:
715 arg
= aexpr
->bytes
[pc
++];
716 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
717 emit_void_call_2 (get_set_tsv_func_addr (),
721 case gdb_agent_op_tracev
:
725 /* GDB never (currently) generates any of these ops. */
726 case gdb_agent_op_float
:
727 case gdb_agent_op_ref_float
:
728 case gdb_agent_op_ref_double
:
729 case gdb_agent_op_ref_long_double
:
730 case gdb_agent_op_l_to_d
:
731 case gdb_agent_op_d_to_l
:
732 case gdb_agent_op_trace16
:
737 ax_debug ("Agent expression op 0x%x not recognized\n", op
);
738 /* Don't struggle on, things will just get worse. */
739 return expr_eval_unrecognized_opcode
;
742 /* This catches errors that occur in target-specific code
746 ax_debug ("Error %d while emitting code for %s\n",
747 emit_error
, gdb_agent_op_name (op
));
748 return expr_eval_unhandled_opcode
;
751 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
754 /* Now fill in real addresses as goto destinations. */
755 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
759 if (aentry
->goto_pc
< 0)
762 /* Find the location that we are going to, and call back into
763 target-specific code to write the actual address or
765 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
767 if (aentry2
->pc
== aentry
->goto_pc
)
769 ax_debug ("Want to jump from %s to %s\n",
770 paddress (aentry
->address
),
771 paddress (aentry2
->address
));
772 write_goto_address (aentry
->address
+ aentry
->from_offset
,
773 aentry2
->address
, aentry
->from_size
);
779 /* Error out if we didn't find a destination. */
782 ax_debug ("Destination of goto %d not found\n",
784 return expr_eval_invalid_goto
;
788 return expr_eval_no_error
;
793 /* Make printf-type calls using arguments supplied from the host. We
794 need to parse the format string ourselves, and call the formatting
795 function with one argument at a time, partly because there is no
796 safe portable way to construct a varargs call, and partly to serve
797 as a security barrier against bad format strings that might get
801 ax_printf (CORE_ADDR fn
, CORE_ADDR chan
, const char *format
,
802 int nargs
, ULONGEST
*args
)
804 const char *f
= format
;
805 struct format_piece
*fpieces
;
807 char *current_substring
;
810 ax_debug ("Printf of \"%s\" with %d args", format
, nargs
);
812 fpieces
= parse_format_string (&f
);
815 for (fp
= 0; fpieces
[fp
].string
!= NULL
; fp
++)
816 if (fpieces
[fp
].argclass
!= literal_piece
)
819 if (nargs
!= nargs_wanted
)
820 error (_("Wrong number of arguments for specified format-string"));
823 for (fp
= 0; fpieces
[fp
].string
!= NULL
; fp
++)
825 current_substring
= fpieces
[fp
].string
;
826 ax_debug ("current substring is '%s', class is %d",
827 current_substring
, fpieces
[fp
].argclass
);
828 switch (fpieces
[fp
].argclass
)
838 /* This is a %s argument. Find the length of the string. */
843 read_inferior_memory (tem
+ j
, &c
, 1);
848 /* Copy the string contents into a string inside GDB. */
849 str
= (gdb_byte
*) alloca (j
+ 1);
851 read_inferior_memory (tem
, str
, j
);
854 printf (current_substring
, (char *) str
);
859 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
861 long long val
= args
[i
];
863 printf (current_substring
, val
);
867 error (_("long long not supported in agent printf"));
873 printf (current_substring
, val
);
881 printf (current_substring
, val
);
886 /* Print a portion of the format string that has no
887 directives. Note that this will not include any
888 ordinary %-specs, but it might include "%%". That is
889 why we use printf_filtered and not puts_filtered here.
890 Also, we pass a dummy argument because some platforms
891 have modified GCC to include -Wformat-security by
892 default, which will warn here if there is no
894 printf (current_substring
, 0);
898 error (_("Format directive in '%s' not supported in agent printf"),
902 /* Maybe advance to the next argument. */
903 if (fpieces
[fp
].argclass
!= literal_piece
)
907 free_format_pieces (fpieces
);
911 /* The agent expression evaluator, as specified by the GDB docs. It
912 returns 0 if everything went OK, and a nonzero error code
915 enum eval_result_type
916 gdb_eval_agent_expr (struct eval_agent_expr_context
*ctx
,
917 struct agent_expr
*aexpr
,
921 #define STACK_MAX 100
922 ULONGEST stack
[STACK_MAX
], top
;
927 /* This union is a convenient way to convert representations. For
928 now, assume a standard architecture where the hardware integer
929 types have 8, 16, 32, 64 bit types. A more robust solution would
930 be to import stdint.h from gnulib. */
935 unsigned char bytes
[1];
940 unsigned char bytes
[2];
945 unsigned char bytes
[4];
950 unsigned char bytes
[8];
955 if (aexpr
->length
== 0)
957 ax_debug ("empty agent expression");
958 return expr_eval_empty_expression
;
961 /* Cache the stack top in its own variable. Much of the time we can
962 operate on this variable, rather than dinking with the stack. It
963 needs to be copied to the stack when sp changes. */
968 op
= aexpr
->bytes
[pc
++];
970 ax_debug ("About to interpret byte 0x%x", op
);
974 case gdb_agent_op_add
:
978 case gdb_agent_op_sub
:
979 top
= stack
[--sp
] - top
;
982 case gdb_agent_op_mul
:
986 case gdb_agent_op_div_signed
:
989 ax_debug ("Attempted to divide by zero");
990 return expr_eval_divide_by_zero
;
992 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
995 case gdb_agent_op_div_unsigned
:
998 ax_debug ("Attempted to divide by zero");
999 return expr_eval_divide_by_zero
;
1001 top
= stack
[--sp
] / top
;
1004 case gdb_agent_op_rem_signed
:
1007 ax_debug ("Attempted to divide by zero");
1008 return expr_eval_divide_by_zero
;
1010 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
1013 case gdb_agent_op_rem_unsigned
:
1016 ax_debug ("Attempted to divide by zero");
1017 return expr_eval_divide_by_zero
;
1019 top
= stack
[--sp
] % top
;
1022 case gdb_agent_op_lsh
:
1023 top
= stack
[--sp
] << top
;
1026 case gdb_agent_op_rsh_signed
:
1027 top
= ((LONGEST
) stack
[--sp
]) >> top
;
1030 case gdb_agent_op_rsh_unsigned
:
1031 top
= stack
[--sp
] >> top
;
1034 case gdb_agent_op_trace
:
1035 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1041 case gdb_agent_op_trace_quick
:
1042 arg
= aexpr
->bytes
[pc
++];
1043 agent_mem_read (ctx
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
1046 case gdb_agent_op_log_not
:
1050 case gdb_agent_op_bit_and
:
1054 case gdb_agent_op_bit_or
:
1058 case gdb_agent_op_bit_xor
:
1062 case gdb_agent_op_bit_not
:
1066 case gdb_agent_op_equal
:
1067 top
= (stack
[--sp
] == top
);
1070 case gdb_agent_op_less_signed
:
1071 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
1074 case gdb_agent_op_less_unsigned
:
1075 top
= (stack
[--sp
] < top
);
1078 case gdb_agent_op_ext
:
1079 arg
= aexpr
->bytes
[pc
++];
1080 if (arg
< (sizeof (LONGEST
) * 8))
1082 LONGEST mask
= 1 << (arg
- 1);
1083 top
&= ((LONGEST
) 1 << arg
) - 1;
1084 top
= (top
^ mask
) - mask
;
1088 case gdb_agent_op_ref8
:
1089 agent_mem_read (ctx
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
1093 case gdb_agent_op_ref16
:
1094 agent_mem_read (ctx
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
1098 case gdb_agent_op_ref32
:
1099 agent_mem_read (ctx
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
1103 case gdb_agent_op_ref64
:
1104 agent_mem_read (ctx
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
1108 case gdb_agent_op_if_goto
:
1110 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1117 case gdb_agent_op_goto
:
1118 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
1121 case gdb_agent_op_const8
:
1122 /* Flush the cached stack top. */
1124 top
= aexpr
->bytes
[pc
++];
1127 case gdb_agent_op_const16
:
1128 /* Flush the cached stack top. */
1130 top
= aexpr
->bytes
[pc
++];
1131 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1134 case gdb_agent_op_const32
:
1135 /* Flush the cached stack top. */
1137 top
= aexpr
->bytes
[pc
++];
1138 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1139 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1140 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
1143 case gdb_agent_op_const64
:
1144 /* Flush the cached stack top. */
1146 top
= aexpr
->bytes
[pc
++];
1147 top
= (top
<< 8) + 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
++];
1156 case gdb_agent_op_reg
:
1157 /* Flush the cached stack top. */
1159 arg
= aexpr
->bytes
[pc
++];
1160 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1163 struct regcache
*regcache
= ctx
->regcache
;
1165 switch (register_size (regnum
))
1168 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
1172 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
1176 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
1180 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
1184 internal_error (__FILE__
, __LINE__
,
1185 "unhandled register size");
1190 case gdb_agent_op_end
:
1191 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1192 sp
, pulongest (top
));
1197 /* This should be an error */
1198 ax_debug ("Stack is empty, nothing to return");
1199 return expr_eval_empty_stack
;
1203 return expr_eval_no_error
;
1205 case gdb_agent_op_dup
:
1209 case gdb_agent_op_pop
:
1214 case gdb_agent_op_pick
:
1215 arg
= aexpr
->bytes
[pc
++];
1217 top
= stack
[sp
- arg
];
1221 case gdb_agent_op_rot
:
1223 ULONGEST tem
= stack
[sp
- 1];
1225 stack
[sp
- 1] = stack
[sp
- 2];
1226 stack
[sp
- 2] = top
;
1231 case gdb_agent_op_zero_ext
:
1232 arg
= aexpr
->bytes
[pc
++];
1233 if (arg
< (sizeof (LONGEST
) * 8))
1234 top
&= ((LONGEST
) 1 << arg
) - 1;
1237 case gdb_agent_op_swap
:
1238 /* Interchange top two stack elements, making sure top gets
1239 copied back onto stack. */
1241 top
= stack
[sp
- 1];
1242 stack
[sp
- 1] = stack
[sp
];
1245 case gdb_agent_op_getv
:
1246 /* Flush the cached stack top. */
1248 arg
= aexpr
->bytes
[pc
++];
1249 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1250 top
= agent_get_trace_state_variable_value (arg
);
1253 case gdb_agent_op_setv
:
1254 arg
= aexpr
->bytes
[pc
++];
1255 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1256 agent_set_trace_state_variable_value (arg
, top
);
1257 /* Note that we leave the value on the stack, for the
1258 benefit of later/enclosing expressions. */
1261 case gdb_agent_op_tracev
:
1262 arg
= aexpr
->bytes
[pc
++];
1263 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
1264 agent_tsv_read (ctx
, arg
);
1267 case gdb_agent_op_tracenz
:
1268 agent_mem_read_string (ctx
, NULL
, (CORE_ADDR
) stack
[--sp
],
1274 case gdb_agent_op_printf
:
1277 CORE_ADDR fn
= 0, chan
= 0;
1278 /* Can't have more args than the entire size of the stack. */
1279 ULONGEST args
[STACK_MAX
];
1282 nargs
= aexpr
->bytes
[pc
++];
1283 slen
= aexpr
->bytes
[pc
++];
1284 slen
= (slen
<< 8) + aexpr
->bytes
[pc
++];
1285 format
= (char *) &(aexpr
->bytes
[pc
]);
1287 /* Pop function and channel. */
1294 /* Pop arguments into a dedicated array. */
1295 for (i
= 0; i
< nargs
; ++i
)
1302 /* A bad format string means something is very wrong; give
1304 if (format
[slen
- 1] != '\0')
1305 error (_("Unterminated format string in printf bytecode"));
1307 ax_printf (fn
, chan
, format
, nargs
, args
);
1311 /* GDB never (currently) generates any of these ops. */
1312 case gdb_agent_op_float
:
1313 case gdb_agent_op_ref_float
:
1314 case gdb_agent_op_ref_double
:
1315 case gdb_agent_op_ref_long_double
:
1316 case gdb_agent_op_l_to_d
:
1317 case gdb_agent_op_d_to_l
:
1318 case gdb_agent_op_trace16
:
1319 ax_debug ("Agent expression op 0x%x valid, but not handled",
1321 /* If ever GDB generates any of these, we don't have the
1322 option of ignoring. */
1326 ax_debug ("Agent expression op 0x%x not recognized", op
);
1327 /* Don't struggle on, things will just get worse. */
1328 return expr_eval_unrecognized_opcode
;
1331 /* Check for stack badness. */
1332 if (sp
>= (STACK_MAX
- 1))
1334 ax_debug ("Expression stack overflow");
1335 return expr_eval_stack_overflow
;
1340 ax_debug ("Expression stack underflow");
1341 return expr_eval_stack_underflow
;
1344 ax_debug ("Op %s -> sp=%d, top=0x%s",
1345 gdb_agent_op_name (op
), sp
, phex_nz (top
, 0));