Automatic date update in version.in
[binutils-gdb.git] / gdbserver / ax.cc
blobff42795582ff08c1f8416f52afaede8aaf3704e1
1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2024 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/>. */
19 #include "ax.h"
20 #include "gdbsupport/format.h"
21 #include "tracepoint.h"
22 #include "gdbsupport/rsp-low.h"
24 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
26 #ifdef IN_PROCESS_AGENT
27 bool debug_agent = 0;
28 #endif
30 static void
31 ax_vdebug (const char *fmt, ...)
33 char buf[1024];
34 va_list ap;
36 va_start (ap, fmt);
37 vsprintf (buf, fmt, ap);
38 #ifdef IN_PROCESS_AGENT
39 fprintf (stderr, PROG "/ax: %s\n", buf);
40 #else
41 threads_debug_printf (PROG "/ax: %s", buf);
42 #endif
43 va_end (ap);
46 #define ax_debug(fmt, args...) \
47 do { \
48 if (debug_threads) \
49 ax_vdebug ((fmt), ##args); \
50 } while (0)
52 /* This enum must exactly match what is documented in
53 gdb/doc/agentexpr.texi, including all the numerical values. */
55 enum gdb_agent_op
57 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
58 gdb_agent_op_ ## NAME = VALUE,
59 #include "gdbsupport/ax.def"
60 #undef DEFOP
61 gdb_agent_op_last
64 static const char * const gdb_agent_op_names [gdb_agent_op_last] =
66 "?undef?"
67 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
68 #include "gdbsupport/ax.def"
69 #undef DEFOP
72 #ifndef IN_PROCESS_AGENT
73 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
76 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
77 #include "gdbsupport/ax.def"
78 #undef DEFOP
80 #endif
82 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
84 static const char *
85 gdb_agent_op_name (int op)
87 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
88 return "?undef?";
89 return gdb_agent_op_names[op];
92 #ifndef IN_PROCESS_AGENT
94 /* The packet form of an agent expression consists of an 'X', number
95 of bytes in expression, a comma, and then the bytes. */
97 struct agent_expr *
98 gdb_parse_agent_expr (const char **actparm)
100 const char *act = *actparm;
101 ULONGEST xlen;
102 struct agent_expr *aexpr;
104 ++act; /* skip the X */
105 act = unpack_varlen_hex (act, &xlen);
106 ++act; /* skip a comma */
107 aexpr = XNEW (struct agent_expr);
108 aexpr->length = xlen;
109 aexpr->bytes = (unsigned char *) xmalloc (xlen);
110 hex2bin (act, aexpr->bytes, xlen);
111 *actparm = act + (xlen * 2);
112 return aexpr;
115 void
116 gdb_free_agent_expr (struct agent_expr *aexpr)
118 if (aexpr != NULL)
120 free (aexpr->bytes);
121 free (aexpr);
125 /* Convert the bytes of an agent expression back into hex digits, so
126 they can be printed or uploaded. This allocates the buffer,
127 callers should free when they are done with it. */
129 char *
130 gdb_unparse_agent_expr (struct agent_expr *aexpr)
132 char *rslt;
134 rslt = (char *) xmalloc (2 * aexpr->length + 1);
135 bin2hex (aexpr->bytes, rslt, aexpr->length);
136 return rslt;
139 /* Bytecode compilation. */
141 CORE_ADDR current_insn_ptr;
143 int emit_error;
145 static struct bytecode_address
147 int pc;
148 CORE_ADDR address;
149 int goto_pc;
150 /* Offset and size of field to be modified in the goto block. */
151 int from_offset, from_size;
152 struct bytecode_address *next;
153 } *bytecode_address_table;
155 void
156 emit_prologue (void)
158 target_emit_ops ()->emit_prologue ();
161 void
162 emit_epilogue (void)
164 target_emit_ops ()->emit_epilogue ();
167 static void
168 emit_add (void)
170 target_emit_ops ()->emit_add ();
173 static void
174 emit_sub (void)
176 target_emit_ops ()->emit_sub ();
179 static void
180 emit_mul (void)
182 target_emit_ops ()->emit_mul ();
185 static void
186 emit_lsh (void)
188 target_emit_ops ()->emit_lsh ();
191 static void
192 emit_rsh_signed (void)
194 target_emit_ops ()->emit_rsh_signed ();
197 static void
198 emit_rsh_unsigned (void)
200 target_emit_ops ()->emit_rsh_unsigned ();
203 static void
204 emit_ext (int arg)
206 target_emit_ops ()->emit_ext (arg);
209 static void
210 emit_log_not (void)
212 target_emit_ops ()->emit_log_not ();
215 static void
216 emit_bit_and (void)
218 target_emit_ops ()->emit_bit_and ();
221 static void
222 emit_bit_or (void)
224 target_emit_ops ()->emit_bit_or ();
227 static void
228 emit_bit_xor (void)
230 target_emit_ops ()->emit_bit_xor ();
233 static void
234 emit_bit_not (void)
236 target_emit_ops ()->emit_bit_not ();
239 static void
240 emit_equal (void)
242 target_emit_ops ()->emit_equal ();
245 static void
246 emit_less_signed (void)
248 target_emit_ops ()->emit_less_signed ();
251 static void
252 emit_less_unsigned (void)
254 target_emit_ops ()->emit_less_unsigned ();
257 static void
258 emit_ref (int size)
260 target_emit_ops ()->emit_ref (size);
263 static void
264 emit_if_goto (int *offset_p, int *size_p)
266 target_emit_ops ()->emit_if_goto (offset_p, size_p);
269 static void
270 emit_goto (int *offset_p, int *size_p)
272 target_emit_ops ()->emit_goto (offset_p, size_p);
275 static void
276 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
278 target_emit_ops ()->write_goto_address (from, to, size);
281 static void
282 emit_const (LONGEST num)
284 target_emit_ops ()->emit_const (num);
287 static void
288 emit_reg (int reg)
290 target_emit_ops ()->emit_reg (reg);
293 static void
294 emit_pop (void)
296 target_emit_ops ()->emit_pop ();
299 static void
300 emit_stack_flush (void)
302 target_emit_ops ()->emit_stack_flush ();
305 static void
306 emit_zero_ext (int arg)
308 target_emit_ops ()->emit_zero_ext (arg);
311 static void
312 emit_swap (void)
314 target_emit_ops ()->emit_swap ();
317 static void
318 emit_stack_adjust (int n)
320 target_emit_ops ()->emit_stack_adjust (n);
323 /* FN's prototype is `LONGEST(*fn)(int)'. */
325 static void
326 emit_int_call_1 (CORE_ADDR fn, int arg1)
328 target_emit_ops ()->emit_int_call_1 (fn, arg1);
331 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
333 static void
334 emit_void_call_2 (CORE_ADDR fn, int arg1)
336 target_emit_ops ()->emit_void_call_2 (fn, arg1);
339 static void
340 emit_eq_goto (int *offset_p, int *size_p)
342 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
345 static void
346 emit_ne_goto (int *offset_p, int *size_p)
348 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
351 static void
352 emit_lt_goto (int *offset_p, int *size_p)
354 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
357 static void
358 emit_ge_goto (int *offset_p, int *size_p)
360 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
363 static void
364 emit_gt_goto (int *offset_p, int *size_p)
366 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
369 static void
370 emit_le_goto (int *offset_p, int *size_p)
372 target_emit_ops ()->emit_le_goto (offset_p, size_p);
375 /* Scan an agent expression for any evidence that the given PC is the
376 target of a jump bytecode in the expression. */
378 static int
379 is_goto_target (struct agent_expr *aexpr, int pc)
381 int i;
382 unsigned char op;
384 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
386 op = aexpr->bytes[i];
388 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
390 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
391 if (target == pc)
392 return 1;
396 return 0;
399 /* Given an agent expression, turn it into native code. */
401 enum eval_result_type
402 compile_bytecodes (struct agent_expr *aexpr)
404 int pc = 0;
405 int done = 0;
406 unsigned char op, next_op;
407 int arg;
408 /* This is only used to build 64-bit value for constants. */
409 ULONGEST top;
410 struct bytecode_address *aentry, *aentry2;
412 #define UNHANDLED \
413 do \
415 ax_debug ("Cannot compile op 0x%x\n", op); \
416 return expr_eval_unhandled_opcode; \
417 } while (0)
419 if (aexpr->length == 0)
421 ax_debug ("empty agent expression\n");
422 return expr_eval_empty_expression;
425 bytecode_address_table = NULL;
427 while (!done)
429 op = aexpr->bytes[pc];
431 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
433 /* Record the compiled-code address of the bytecode, for use by
434 jump instructions. */
435 aentry = XNEW (struct bytecode_address);
436 aentry->pc = pc;
437 aentry->address = current_insn_ptr;
438 aentry->goto_pc = -1;
439 aentry->from_offset = aentry->from_size = 0;
440 aentry->next = bytecode_address_table;
441 bytecode_address_table = aentry;
443 ++pc;
445 emit_error = 0;
447 switch (op)
449 case gdb_agent_op_add:
450 emit_add ();
451 break;
453 case gdb_agent_op_sub:
454 emit_sub ();
455 break;
457 case gdb_agent_op_mul:
458 emit_mul ();
459 break;
461 case gdb_agent_op_div_signed:
462 UNHANDLED;
463 break;
465 case gdb_agent_op_div_unsigned:
466 UNHANDLED;
467 break;
469 case gdb_agent_op_rem_signed:
470 UNHANDLED;
471 break;
473 case gdb_agent_op_rem_unsigned:
474 UNHANDLED;
475 break;
477 case gdb_agent_op_lsh:
478 emit_lsh ();
479 break;
481 case gdb_agent_op_rsh_signed:
482 emit_rsh_signed ();
483 break;
485 case gdb_agent_op_rsh_unsigned:
486 emit_rsh_unsigned ();
487 break;
489 case gdb_agent_op_trace:
490 UNHANDLED;
491 break;
493 case gdb_agent_op_trace_quick:
494 UNHANDLED;
495 break;
497 case gdb_agent_op_log_not:
498 emit_log_not ();
499 break;
501 case gdb_agent_op_bit_and:
502 emit_bit_and ();
503 break;
505 case gdb_agent_op_bit_or:
506 emit_bit_or ();
507 break;
509 case gdb_agent_op_bit_xor:
510 emit_bit_xor ();
511 break;
513 case gdb_agent_op_bit_not:
514 emit_bit_not ();
515 break;
517 case gdb_agent_op_equal:
518 next_op = aexpr->bytes[pc];
519 if (next_op == gdb_agent_op_if_goto
520 && !is_goto_target (aexpr, pc)
521 && target_emit_ops ()->emit_eq_goto)
523 ax_debug ("Combining equal & if_goto");
524 pc += 1;
525 aentry->pc = pc;
526 arg = aexpr->bytes[pc++];
527 arg = (arg << 8) + aexpr->bytes[pc++];
528 aentry->goto_pc = arg;
529 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
531 else if (next_op == gdb_agent_op_log_not
532 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
533 && !is_goto_target (aexpr, pc + 1)
534 && target_emit_ops ()->emit_ne_goto)
536 ax_debug ("Combining equal & log_not & if_goto");
537 pc += 2;
538 aentry->pc = pc;
539 arg = aexpr->bytes[pc++];
540 arg = (arg << 8) + aexpr->bytes[pc++];
541 aentry->goto_pc = arg;
542 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
544 else
545 emit_equal ();
546 break;
548 case gdb_agent_op_less_signed:
549 next_op = aexpr->bytes[pc];
550 if (next_op == gdb_agent_op_if_goto
551 && !is_goto_target (aexpr, pc))
553 ax_debug ("Combining less_signed & if_goto");
554 pc += 1;
555 aentry->pc = pc;
556 arg = aexpr->bytes[pc++];
557 arg = (arg << 8) + aexpr->bytes[pc++];
558 aentry->goto_pc = arg;
559 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
561 else if (next_op == gdb_agent_op_log_not
562 && !is_goto_target (aexpr, pc)
563 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
564 && !is_goto_target (aexpr, pc + 1))
566 ax_debug ("Combining less_signed & log_not & if_goto");
567 pc += 2;
568 aentry->pc = pc;
569 arg = aexpr->bytes[pc++];
570 arg = (arg << 8) + aexpr->bytes[pc++];
571 aentry->goto_pc = arg;
572 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
574 else
575 emit_less_signed ();
576 break;
578 case gdb_agent_op_less_unsigned:
579 emit_less_unsigned ();
580 break;
582 case gdb_agent_op_ext:
583 arg = aexpr->bytes[pc++];
584 if (arg < (sizeof (LONGEST) * 8))
585 emit_ext (arg);
586 break;
588 case gdb_agent_op_ref8:
589 emit_ref (1);
590 break;
592 case gdb_agent_op_ref16:
593 emit_ref (2);
594 break;
596 case gdb_agent_op_ref32:
597 emit_ref (4);
598 break;
600 case gdb_agent_op_ref64:
601 emit_ref (8);
602 break;
604 case gdb_agent_op_if_goto:
605 arg = aexpr->bytes[pc++];
606 arg = (arg << 8) + aexpr->bytes[pc++];
607 aentry->goto_pc = arg;
608 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
609 break;
611 case gdb_agent_op_goto:
612 arg = aexpr->bytes[pc++];
613 arg = (arg << 8) + aexpr->bytes[pc++];
614 aentry->goto_pc = arg;
615 emit_goto (&(aentry->from_offset), &(aentry->from_size));
616 break;
618 case gdb_agent_op_const8:
619 emit_stack_flush ();
620 top = aexpr->bytes[pc++];
621 emit_const (top);
622 break;
624 case gdb_agent_op_const16:
625 emit_stack_flush ();
626 top = aexpr->bytes[pc++];
627 top = (top << 8) + aexpr->bytes[pc++];
628 emit_const (top);
629 break;
631 case gdb_agent_op_const32:
632 emit_stack_flush ();
633 top = 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 emit_const (top);
638 break;
640 case gdb_agent_op_const64:
641 emit_stack_flush ();
642 top = aexpr->bytes[pc++];
643 top = (top << 8) + 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 emit_const (top);
651 break;
653 case gdb_agent_op_reg:
654 emit_stack_flush ();
655 arg = aexpr->bytes[pc++];
656 arg = (arg << 8) + aexpr->bytes[pc++];
657 emit_reg (arg);
658 break;
660 case gdb_agent_op_end:
661 ax_debug ("At end of expression\n");
663 /* Assume there is one stack element left, and that it is
664 cached in "top" where emit_epilogue can get to it. */
665 emit_stack_adjust (1);
667 done = 1;
668 break;
670 case gdb_agent_op_dup:
671 /* In our design, dup is equivalent to stack flushing. */
672 emit_stack_flush ();
673 break;
675 case gdb_agent_op_pop:
676 emit_pop ();
677 break;
679 case gdb_agent_op_zero_ext:
680 arg = aexpr->bytes[pc++];
681 if (arg < (sizeof (LONGEST) * 8))
682 emit_zero_ext (arg);
683 break;
685 case gdb_agent_op_swap:
686 next_op = aexpr->bytes[pc];
687 /* Detect greater-than comparison sequences. */
688 if (next_op == gdb_agent_op_less_signed
689 && !is_goto_target (aexpr, pc)
690 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
691 && !is_goto_target (aexpr, pc + 1))
693 ax_debug ("Combining swap & less_signed & if_goto");
694 pc += 2;
695 aentry->pc = pc;
696 arg = aexpr->bytes[pc++];
697 arg = (arg << 8) + aexpr->bytes[pc++];
698 aentry->goto_pc = arg;
699 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
701 else if (next_op == gdb_agent_op_less_signed
702 && !is_goto_target (aexpr, pc)
703 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
704 && !is_goto_target (aexpr, pc + 1)
705 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
706 && !is_goto_target (aexpr, pc + 2))
708 ax_debug ("Combining swap & less_signed & log_not & if_goto");
709 pc += 3;
710 aentry->pc = pc;
711 arg = aexpr->bytes[pc++];
712 arg = (arg << 8) + aexpr->bytes[pc++];
713 aentry->goto_pc = arg;
714 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
716 else
717 emit_swap ();
718 break;
720 case gdb_agent_op_getv:
721 emit_stack_flush ();
722 arg = aexpr->bytes[pc++];
723 arg = (arg << 8) + aexpr->bytes[pc++];
724 emit_int_call_1 (get_get_tsv_func_addr (),
725 arg);
726 break;
728 case gdb_agent_op_setv:
729 arg = aexpr->bytes[pc++];
730 arg = (arg << 8) + aexpr->bytes[pc++];
731 emit_void_call_2 (get_set_tsv_func_addr (),
732 arg);
733 break;
735 case gdb_agent_op_tracev:
736 UNHANDLED;
737 break;
739 /* GDB never (currently) generates any of these ops. */
740 case gdb_agent_op_float:
741 case gdb_agent_op_ref_float:
742 case gdb_agent_op_ref_double:
743 case gdb_agent_op_ref_long_double:
744 case gdb_agent_op_l_to_d:
745 case gdb_agent_op_d_to_l:
746 case gdb_agent_op_trace16:
747 UNHANDLED;
748 break;
750 default:
751 ax_debug ("Agent expression op 0x%x not recognized\n", op);
752 /* Don't struggle on, things will just get worse. */
753 return expr_eval_unrecognized_opcode;
756 /* This catches errors that occur in target-specific code
757 emission. */
758 if (emit_error)
760 ax_debug ("Error %d while emitting code for %s\n",
761 emit_error, gdb_agent_op_name (op));
762 return expr_eval_unhandled_opcode;
765 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
768 /* Now fill in real addresses as goto destinations. */
769 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
771 int written = 0;
773 if (aentry->goto_pc < 0)
774 continue;
776 /* Find the location that we are going to, and call back into
777 target-specific code to write the actual address or
778 displacement. */
779 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
781 if (aentry2->pc == aentry->goto_pc)
783 ax_debug ("Want to jump from %s to %s\n",
784 paddress (aentry->address),
785 paddress (aentry2->address));
786 write_goto_address (aentry->address + aentry->from_offset,
787 aentry2->address, aentry->from_size);
788 written = 1;
789 break;
793 /* Error out if we didn't find a destination. */
794 if (!written)
796 ax_debug ("Destination of goto %d not found\n",
797 aentry->goto_pc);
798 return expr_eval_invalid_goto;
802 return expr_eval_no_error;
805 #endif
807 /* Make printf-type calls using arguments supplied from the host. We
808 need to parse the format string ourselves, and call the formatting
809 function with one argument at a time, partly because there is no
810 safe portable way to construct a varargs call, and partly to serve
811 as a security barrier against bad format strings that might get
812 in. */
814 static void
815 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
816 int nargs, ULONGEST *args)
818 const char *f = format;
819 int i;
820 const char *current_substring;
821 int nargs_wanted;
823 ax_debug ("Printf of \"%s\" with %d args", format, nargs);
825 format_pieces fpieces (&f);
827 nargs_wanted = 0;
828 for (auto &&piece : fpieces)
829 if (piece.argclass != literal_piece)
830 ++nargs_wanted;
832 if (nargs != nargs_wanted)
833 error (_("Wrong number of arguments for specified format-string"));
835 i = 0;
836 for (auto &&piece : fpieces)
838 current_substring = piece.string;
839 ax_debug ("current substring is '%s', class is %d",
840 current_substring, piece.argclass);
841 switch (piece.argclass)
843 case string_arg:
845 gdb_byte *str;
846 CORE_ADDR tem;
847 int j;
849 tem = args[i];
850 if (tem == 0)
852 printf (current_substring, "(null)");
853 break;
856 /* This is a %s argument. Find the length of the string. */
857 for (j = 0;; j++)
859 gdb_byte c;
861 read_inferior_memory (tem + j, &c, 1);
862 if (c == 0)
863 break;
866 /* Copy the string contents into a string inside GDB. */
867 str = (gdb_byte *) alloca (j + 1);
868 if (j != 0)
869 read_inferior_memory (tem, str, j);
870 str[j] = 0;
872 printf (current_substring, (char *) str);
874 break;
876 case long_long_arg:
877 #if defined (PRINTF_HAS_LONG_LONG)
879 long long val = args[i];
881 printf (current_substring, val);
882 break;
884 #else
885 error (_("long long not supported in agent printf"));
886 #endif
887 case int_arg:
889 int val = args[i];
891 printf (current_substring, val);
892 break;
895 case long_arg:
897 long val = args[i];
899 printf (current_substring, val);
900 break;
903 case size_t_arg:
905 size_t val = args[i];
907 printf (current_substring, val);
908 break;
911 case literal_piece:
912 /* Print a portion of the format string that has no
913 directives. Note that this will not include any
914 ordinary %-specs, but it might include "%%". That is
915 why we use printf_filtered and not puts_filtered here.
916 Also, we pass a dummy argument because some platforms
917 have modified GCC to include -Wformat-security by
918 default, which will warn here if there is no
919 argument. */
920 printf (current_substring, 0);
921 break;
923 default:
924 error (_("Format directive in '%s' not supported in agent printf"),
925 current_substring);
928 /* Maybe advance to the next argument. */
929 if (piece.argclass != literal_piece)
930 ++i;
933 fflush (stdout);
936 /* The agent expression evaluator, as specified by the GDB docs. It
937 returns 0 if everything went OK, and a nonzero error code
938 otherwise. */
940 enum eval_result_type
941 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
942 struct agent_expr *aexpr,
943 ULONGEST *rslt)
945 int pc = 0;
946 #define STACK_MAX 100
947 ULONGEST stack[STACK_MAX], top;
948 int sp = 0;
949 unsigned char op;
950 int arg;
952 /* This union is a convenient way to convert representations. For
953 now, assume a standard architecture where the hardware integer
954 types have 8, 16, 32, 64 bit types. A more robust solution would
955 be to import stdint.h from gnulib. */
956 union
958 union
960 unsigned char bytes[1];
961 unsigned char val;
962 } u8;
963 union
965 unsigned char bytes[2];
966 unsigned short val;
967 } u16;
968 union
970 unsigned char bytes[4];
971 unsigned int val;
972 } u32;
973 union
975 unsigned char bytes[8];
976 ULONGEST val;
977 } u64;
978 } cnv;
980 if (aexpr->length == 0)
982 ax_debug ("empty agent expression");
983 return expr_eval_empty_expression;
986 /* Cache the stack top in its own variable. Much of the time we can
987 operate on this variable, rather than syncing with the stack. It
988 needs to be copied to the stack when sp changes. */
989 top = 0;
991 while (1)
993 op = aexpr->bytes[pc++];
995 ax_debug ("About to interpret byte 0x%x", op);
997 switch (op)
999 case gdb_agent_op_add:
1000 top += stack[--sp];
1001 break;
1003 case gdb_agent_op_sub:
1004 top = stack[--sp] - top;
1005 break;
1007 case gdb_agent_op_mul:
1008 top *= stack[--sp];
1009 break;
1011 case gdb_agent_op_div_signed:
1012 if (top == 0)
1014 ax_debug ("Attempted to divide by zero");
1015 return expr_eval_divide_by_zero;
1017 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
1018 break;
1020 case gdb_agent_op_div_unsigned:
1021 if (top == 0)
1023 ax_debug ("Attempted to divide by zero");
1024 return expr_eval_divide_by_zero;
1026 top = stack[--sp] / top;
1027 break;
1029 case gdb_agent_op_rem_signed:
1030 if (top == 0)
1032 ax_debug ("Attempted to divide by zero");
1033 return expr_eval_divide_by_zero;
1035 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1036 break;
1038 case gdb_agent_op_rem_unsigned:
1039 if (top == 0)
1041 ax_debug ("Attempted to divide by zero");
1042 return expr_eval_divide_by_zero;
1044 top = stack[--sp] % top;
1045 break;
1047 case gdb_agent_op_lsh:
1048 top = stack[--sp] << top;
1049 break;
1051 case gdb_agent_op_rsh_signed:
1052 top = ((LONGEST) stack[--sp]) >> top;
1053 break;
1055 case gdb_agent_op_rsh_unsigned:
1056 top = stack[--sp] >> top;
1057 break;
1059 case gdb_agent_op_trace:
1060 agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
1061 (ULONGEST) top);
1062 if (--sp >= 0)
1063 top = stack[sp];
1064 break;
1066 case gdb_agent_op_trace_quick:
1067 arg = aexpr->bytes[pc++];
1068 agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1069 break;
1071 case gdb_agent_op_log_not:
1072 top = !top;
1073 break;
1075 case gdb_agent_op_bit_and:
1076 top &= stack[--sp];
1077 break;
1079 case gdb_agent_op_bit_or:
1080 top |= stack[--sp];
1081 break;
1083 case gdb_agent_op_bit_xor:
1084 top ^= stack[--sp];
1085 break;
1087 case gdb_agent_op_bit_not:
1088 top = ~top;
1089 break;
1091 case gdb_agent_op_equal:
1092 top = (stack[--sp] == top);
1093 break;
1095 case gdb_agent_op_less_signed:
1096 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1097 break;
1099 case gdb_agent_op_less_unsigned:
1100 top = (stack[--sp] < top);
1101 break;
1103 case gdb_agent_op_ext:
1104 arg = aexpr->bytes[pc++];
1105 if (arg < (sizeof (LONGEST) * 8))
1107 LONGEST mask = 1 << (arg - 1);
1108 top &= ((LONGEST) 1 << arg) - 1;
1109 top = (top ^ mask) - mask;
1111 break;
1113 case gdb_agent_op_ref8:
1114 if (agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1) != 0)
1115 return expr_eval_invalid_memory_access;
1116 top = cnv.u8.val;
1117 break;
1119 case gdb_agent_op_ref16:
1120 if (agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2) != 0)
1121 return expr_eval_invalid_memory_access;
1122 top = cnv.u16.val;
1123 break;
1125 case gdb_agent_op_ref32:
1126 if (agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4) != 0)
1127 return expr_eval_invalid_memory_access;
1128 top = cnv.u32.val;
1129 break;
1131 case gdb_agent_op_ref64:
1132 if (agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8) != 0)
1133 return expr_eval_invalid_memory_access;
1134 top = cnv.u64.val;
1135 break;
1137 case gdb_agent_op_if_goto:
1138 if (top)
1139 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1140 else
1141 pc += 2;
1142 if (--sp >= 0)
1143 top = stack[sp];
1144 break;
1146 case gdb_agent_op_goto:
1147 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1148 break;
1150 case gdb_agent_op_const8:
1151 /* Flush the cached stack top. */
1152 stack[sp++] = top;
1153 top = aexpr->bytes[pc++];
1154 break;
1156 case gdb_agent_op_const16:
1157 /* Flush the cached stack top. */
1158 stack[sp++] = top;
1159 top = aexpr->bytes[pc++];
1160 top = (top << 8) + aexpr->bytes[pc++];
1161 break;
1163 case gdb_agent_op_const32:
1164 /* Flush the cached stack top. */
1165 stack[sp++] = top;
1166 top = aexpr->bytes[pc++];
1167 top = (top << 8) + aexpr->bytes[pc++];
1168 top = (top << 8) + aexpr->bytes[pc++];
1169 top = (top << 8) + aexpr->bytes[pc++];
1170 break;
1172 case gdb_agent_op_const64:
1173 /* Flush the cached stack top. */
1174 stack[sp++] = top;
1175 top = aexpr->bytes[pc++];
1176 top = (top << 8) + aexpr->bytes[pc++];
1177 top = (top << 8) + aexpr->bytes[pc++];
1178 top = (top << 8) + aexpr->bytes[pc++];
1179 top = (top << 8) + aexpr->bytes[pc++];
1180 top = (top << 8) + aexpr->bytes[pc++];
1181 top = (top << 8) + aexpr->bytes[pc++];
1182 top = (top << 8) + aexpr->bytes[pc++];
1183 break;
1185 case gdb_agent_op_reg:
1186 /* Flush the cached stack top. */
1187 stack[sp++] = top;
1188 arg = aexpr->bytes[pc++];
1189 arg = (arg << 8) + aexpr->bytes[pc++];
1191 int regnum = arg;
1192 struct regcache *regcache = ctx->regcache;
1194 switch (register_size (regcache->tdesc, regnum))
1196 case 8:
1197 collect_register (regcache, regnum, cnv.u64.bytes);
1198 top = cnv.u64.val;
1199 break;
1200 case 4:
1201 collect_register (regcache, regnum, cnv.u32.bytes);
1202 top = cnv.u32.val;
1203 break;
1204 case 2:
1205 collect_register (regcache, regnum, cnv.u16.bytes);
1206 top = cnv.u16.val;
1207 break;
1208 case 1:
1209 collect_register (regcache, regnum, cnv.u8.bytes);
1210 top = cnv.u8.val;
1211 break;
1212 default:
1213 internal_error ("unhandled register size");
1216 break;
1218 case gdb_agent_op_end:
1219 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1220 sp, pulongest (top));
1221 if (rslt)
1223 if (sp <= 0)
1225 /* This should be an error */
1226 ax_debug ("Stack is empty, nothing to return");
1227 return expr_eval_empty_stack;
1229 *rslt = top;
1231 return expr_eval_no_error;
1233 case gdb_agent_op_dup:
1234 stack[sp++] = top;
1235 break;
1237 case gdb_agent_op_pop:
1238 if (--sp >= 0)
1239 top = stack[sp];
1240 break;
1242 case gdb_agent_op_pick:
1243 arg = aexpr->bytes[pc++];
1244 stack[sp] = top;
1245 top = stack[sp - arg];
1246 ++sp;
1247 break;
1249 case gdb_agent_op_rot:
1251 ULONGEST tem = stack[sp - 1];
1253 stack[sp - 1] = stack[sp - 2];
1254 stack[sp - 2] = top;
1255 top = tem;
1257 break;
1259 case gdb_agent_op_zero_ext:
1260 arg = aexpr->bytes[pc++];
1261 if (arg < (sizeof (LONGEST) * 8))
1262 top &= ((LONGEST) 1 << arg) - 1;
1263 break;
1265 case gdb_agent_op_swap:
1266 /* Interchange top two stack elements, making sure top gets
1267 copied back onto stack. */
1268 stack[sp] = top;
1269 top = stack[sp - 1];
1270 stack[sp - 1] = stack[sp];
1271 break;
1273 case gdb_agent_op_getv:
1274 /* Flush the cached stack top. */
1275 stack[sp++] = top;
1276 arg = aexpr->bytes[pc++];
1277 arg = (arg << 8) + aexpr->bytes[pc++];
1278 top = agent_get_trace_state_variable_value (arg);
1279 break;
1281 case gdb_agent_op_setv:
1282 arg = aexpr->bytes[pc++];
1283 arg = (arg << 8) + aexpr->bytes[pc++];
1284 agent_set_trace_state_variable_value (arg, top);
1285 /* Note that we leave the value on the stack, for the
1286 benefit of later/enclosing expressions. */
1287 break;
1289 case gdb_agent_op_tracev:
1290 arg = aexpr->bytes[pc++];
1291 arg = (arg << 8) + aexpr->bytes[pc++];
1292 agent_tsv_read (ctx, arg);
1293 break;
1295 case gdb_agent_op_tracenz:
1296 agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
1297 (ULONGEST) top);
1298 if (--sp >= 0)
1299 top = stack[sp];
1300 break;
1302 case gdb_agent_op_printf:
1304 int nargs, slen, i;
1305 CORE_ADDR fn = 0, chan = 0;
1306 /* Can't have more args than the entire size of the stack. */
1307 ULONGEST args[STACK_MAX];
1308 char *format;
1310 nargs = aexpr->bytes[pc++];
1311 slen = aexpr->bytes[pc++];
1312 slen = (slen << 8) + aexpr->bytes[pc++];
1313 format = (char *) &(aexpr->bytes[pc]);
1314 pc += slen;
1315 /* Pop function and channel. */
1316 fn = top;
1317 if (--sp >= 0)
1318 top = stack[sp];
1319 chan = top;
1320 if (--sp >= 0)
1321 top = stack[sp];
1322 /* Pop arguments into a dedicated array. */
1323 for (i = 0; i < nargs; ++i)
1325 args[i] = top;
1326 if (--sp >= 0)
1327 top = stack[sp];
1330 /* A bad format string means something is very wrong; give
1331 up immediately. */
1332 if (format[slen - 1] != '\0')
1333 error (_("Unterminated format string in printf bytecode"));
1335 ax_printf (fn, chan, format, nargs, args);
1337 break;
1339 /* GDB never (currently) generates any of these ops. */
1340 case gdb_agent_op_float:
1341 case gdb_agent_op_ref_float:
1342 case gdb_agent_op_ref_double:
1343 case gdb_agent_op_ref_long_double:
1344 case gdb_agent_op_l_to_d:
1345 case gdb_agent_op_d_to_l:
1346 case gdb_agent_op_trace16:
1347 ax_debug ("Agent expression op 0x%x valid, but not handled",
1348 op);
1349 /* If ever GDB generates any of these, we don't have the
1350 option of ignoring. */
1351 return expr_eval_unhandled_opcode;
1353 default:
1354 ax_debug ("Agent expression op 0x%x not recognized", op);
1355 /* Don't struggle on, things will just get worse. */
1356 return expr_eval_unrecognized_opcode;
1359 /* Check for stack badness. */
1360 if (sp >= (STACK_MAX - 1))
1362 ax_debug ("Expression stack overflow");
1363 return expr_eval_stack_overflow;
1366 if (sp < 0)
1368 ax_debug ("Expression stack underflow");
1369 return expr_eval_stack_underflow;
1372 ax_debug ("Op %s -> sp=%d, top=0x%s",
1373 gdb_agent_op_name (op), sp, phex_nz (top, 0));