update name of several Ada fixed-point type handling functions
[binutils-gdb.git] / gdbserver / ax.cc
blob213db410a07e9d5764d1f8c6e9f033a5b630c243
1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2020 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 "server.h"
20 #include "ax.h"
21 #include "gdbsupport/format.h"
22 #include "tracepoint.h"
23 #include "gdbsupport/rsp-low.h"
25 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
27 #ifdef IN_PROCESS_AGENT
28 int debug_agent = 0;
29 #endif
31 static void
32 ax_vdebug (const char *fmt, ...)
34 char buf[1024];
35 va_list ap;
37 va_start (ap, fmt);
38 vsprintf (buf, fmt, ap);
39 #ifdef IN_PROCESS_AGENT
40 fprintf (stderr, PROG "/ax: %s\n", buf);
41 #else
42 debug_printf (PROG "/ax: %s\n", buf);
43 #endif
44 va_end (ap);
47 #define ax_debug_1(level, fmt, args...) \
48 do { \
49 if (level <= debug_threads) \
50 ax_vdebug ((fmt), ##args); \
51 } while (0)
53 #define ax_debug(FMT, args...) \
54 ax_debug_1 (1, FMT, ##args)
56 /* This enum must exactly match what is documented in
57 gdb/doc/agentexpr.texi, including all the numerical values. */
59 enum gdb_agent_op
61 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
62 gdb_agent_op_ ## NAME = VALUE,
63 #include "gdbsupport/ax.def"
64 #undef DEFOP
65 gdb_agent_op_last
68 static const char *gdb_agent_op_names [gdb_agent_op_last] =
70 "?undef?"
71 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
72 #include "gdbsupport/ax.def"
73 #undef DEFOP
76 #ifndef IN_PROCESS_AGENT
77 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
80 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
81 #include "gdbsupport/ax.def"
82 #undef DEFOP
84 #endif
86 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
88 static const char *
89 gdb_agent_op_name (int op)
91 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
92 return "?undef?";
93 return gdb_agent_op_names[op];
96 #ifndef IN_PROCESS_AGENT
98 /* The packet form of an agent expression consists of an 'X', number
99 of bytes in expression, a comma, and then the bytes. */
101 struct agent_expr *
102 gdb_parse_agent_expr (const char **actparm)
104 const char *act = *actparm;
105 ULONGEST xlen;
106 struct agent_expr *aexpr;
108 ++act; /* skip the X */
109 act = unpack_varlen_hex (act, &xlen);
110 ++act; /* skip a comma */
111 aexpr = XNEW (struct agent_expr);
112 aexpr->length = xlen;
113 aexpr->bytes = (unsigned char *) xmalloc (xlen);
114 hex2bin (act, aexpr->bytes, xlen);
115 *actparm = act + (xlen * 2);
116 return aexpr;
119 void
120 gdb_free_agent_expr (struct agent_expr *aexpr)
122 if (aexpr != NULL)
124 free (aexpr->bytes);
125 free (aexpr);
129 /* Convert the bytes of an agent expression back into hex digits, so
130 they can be printed or uploaded. This allocates the buffer,
131 callers should free when they are done with it. */
133 char *
134 gdb_unparse_agent_expr (struct agent_expr *aexpr)
136 char *rslt;
138 rslt = (char *) xmalloc (2 * aexpr->length + 1);
139 bin2hex (aexpr->bytes, rslt, aexpr->length);
140 return rslt;
143 /* Bytecode compilation. */
145 CORE_ADDR current_insn_ptr;
147 int emit_error;
149 struct bytecode_address
151 int pc;
152 CORE_ADDR address;
153 int goto_pc;
154 /* Offset and size of field to be modified in the goto block. */
155 int from_offset, from_size;
156 struct bytecode_address *next;
157 } *bytecode_address_table;
159 void
160 emit_prologue (void)
162 target_emit_ops ()->emit_prologue ();
165 void
166 emit_epilogue (void)
168 target_emit_ops ()->emit_epilogue ();
171 static void
172 emit_add (void)
174 target_emit_ops ()->emit_add ();
177 static void
178 emit_sub (void)
180 target_emit_ops ()->emit_sub ();
183 static void
184 emit_mul (void)
186 target_emit_ops ()->emit_mul ();
189 static void
190 emit_lsh (void)
192 target_emit_ops ()->emit_lsh ();
195 static void
196 emit_rsh_signed (void)
198 target_emit_ops ()->emit_rsh_signed ();
201 static void
202 emit_rsh_unsigned (void)
204 target_emit_ops ()->emit_rsh_unsigned ();
207 static void
208 emit_ext (int arg)
210 target_emit_ops ()->emit_ext (arg);
213 static void
214 emit_log_not (void)
216 target_emit_ops ()->emit_log_not ();
219 static void
220 emit_bit_and (void)
222 target_emit_ops ()->emit_bit_and ();
225 static void
226 emit_bit_or (void)
228 target_emit_ops ()->emit_bit_or ();
231 static void
232 emit_bit_xor (void)
234 target_emit_ops ()->emit_bit_xor ();
237 static void
238 emit_bit_not (void)
240 target_emit_ops ()->emit_bit_not ();
243 static void
244 emit_equal (void)
246 target_emit_ops ()->emit_equal ();
249 static void
250 emit_less_signed (void)
252 target_emit_ops ()->emit_less_signed ();
255 static void
256 emit_less_unsigned (void)
258 target_emit_ops ()->emit_less_unsigned ();
261 static void
262 emit_ref (int size)
264 target_emit_ops ()->emit_ref (size);
267 static void
268 emit_if_goto (int *offset_p, int *size_p)
270 target_emit_ops ()->emit_if_goto (offset_p, size_p);
273 static void
274 emit_goto (int *offset_p, int *size_p)
276 target_emit_ops ()->emit_goto (offset_p, size_p);
279 static void
280 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
282 target_emit_ops ()->write_goto_address (from, to, size);
285 static void
286 emit_const (LONGEST num)
288 target_emit_ops ()->emit_const (num);
291 static void
292 emit_reg (int reg)
294 target_emit_ops ()->emit_reg (reg);
297 static void
298 emit_pop (void)
300 target_emit_ops ()->emit_pop ();
303 static void
304 emit_stack_flush (void)
306 target_emit_ops ()->emit_stack_flush ();
309 static void
310 emit_zero_ext (int arg)
312 target_emit_ops ()->emit_zero_ext (arg);
315 static void
316 emit_swap (void)
318 target_emit_ops ()->emit_swap ();
321 static void
322 emit_stack_adjust (int n)
324 target_emit_ops ()->emit_stack_adjust (n);
327 /* FN's prototype is `LONGEST(*fn)(int)'. */
329 static void
330 emit_int_call_1 (CORE_ADDR fn, int arg1)
332 target_emit_ops ()->emit_int_call_1 (fn, arg1);
335 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
337 static void
338 emit_void_call_2 (CORE_ADDR fn, int arg1)
340 target_emit_ops ()->emit_void_call_2 (fn, arg1);
343 static void
344 emit_eq_goto (int *offset_p, int *size_p)
346 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
349 static void
350 emit_ne_goto (int *offset_p, int *size_p)
352 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
355 static void
356 emit_lt_goto (int *offset_p, int *size_p)
358 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
361 static void
362 emit_ge_goto (int *offset_p, int *size_p)
364 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
367 static void
368 emit_gt_goto (int *offset_p, int *size_p)
370 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
373 static void
374 emit_le_goto (int *offset_p, int *size_p)
376 target_emit_ops ()->emit_le_goto (offset_p, size_p);
379 /* Scan an agent expression for any evidence that the given PC is the
380 target of a jump bytecode in the expression. */
382 static int
383 is_goto_target (struct agent_expr *aexpr, int pc)
385 int i;
386 unsigned char op;
388 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
390 op = aexpr->bytes[i];
392 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
394 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
395 if (target == pc)
396 return 1;
400 return 0;
403 /* Given an agent expression, turn it into native code. */
405 enum eval_result_type
406 compile_bytecodes (struct agent_expr *aexpr)
408 int pc = 0;
409 int done = 0;
410 unsigned char op, next_op;
411 int arg;
412 /* This is only used to build 64-bit value for constants. */
413 ULONGEST top;
414 struct bytecode_address *aentry, *aentry2;
416 #define UNHANDLED \
417 do \
419 ax_debug ("Cannot compile op 0x%x\n", op); \
420 return expr_eval_unhandled_opcode; \
421 } while (0)
423 if (aexpr->length == 0)
425 ax_debug ("empty agent expression\n");
426 return expr_eval_empty_expression;
429 bytecode_address_table = NULL;
431 while (!done)
433 op = aexpr->bytes[pc];
435 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
437 /* Record the compiled-code address of the bytecode, for use by
438 jump instructions. */
439 aentry = XNEW (struct bytecode_address);
440 aentry->pc = pc;
441 aentry->address = current_insn_ptr;
442 aentry->goto_pc = -1;
443 aentry->from_offset = aentry->from_size = 0;
444 aentry->next = bytecode_address_table;
445 bytecode_address_table = aentry;
447 ++pc;
449 emit_error = 0;
451 switch (op)
453 case gdb_agent_op_add:
454 emit_add ();
455 break;
457 case gdb_agent_op_sub:
458 emit_sub ();
459 break;
461 case gdb_agent_op_mul:
462 emit_mul ();
463 break;
465 case gdb_agent_op_div_signed:
466 UNHANDLED;
467 break;
469 case gdb_agent_op_div_unsigned:
470 UNHANDLED;
471 break;
473 case gdb_agent_op_rem_signed:
474 UNHANDLED;
475 break;
477 case gdb_agent_op_rem_unsigned:
478 UNHANDLED;
479 break;
481 case gdb_agent_op_lsh:
482 emit_lsh ();
483 break;
485 case gdb_agent_op_rsh_signed:
486 emit_rsh_signed ();
487 break;
489 case gdb_agent_op_rsh_unsigned:
490 emit_rsh_unsigned ();
491 break;
493 case gdb_agent_op_trace:
494 UNHANDLED;
495 break;
497 case gdb_agent_op_trace_quick:
498 UNHANDLED;
499 break;
501 case gdb_agent_op_log_not:
502 emit_log_not ();
503 break;
505 case gdb_agent_op_bit_and:
506 emit_bit_and ();
507 break;
509 case gdb_agent_op_bit_or:
510 emit_bit_or ();
511 break;
513 case gdb_agent_op_bit_xor:
514 emit_bit_xor ();
515 break;
517 case gdb_agent_op_bit_not:
518 emit_bit_not ();
519 break;
521 case gdb_agent_op_equal:
522 next_op = aexpr->bytes[pc];
523 if (next_op == gdb_agent_op_if_goto
524 && !is_goto_target (aexpr, pc)
525 && target_emit_ops ()->emit_eq_goto)
527 ax_debug ("Combining equal & if_goto");
528 pc += 1;
529 aentry->pc = pc;
530 arg = aexpr->bytes[pc++];
531 arg = (arg << 8) + aexpr->bytes[pc++];
532 aentry->goto_pc = arg;
533 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
535 else if (next_op == gdb_agent_op_log_not
536 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
537 && !is_goto_target (aexpr, pc + 1)
538 && target_emit_ops ()->emit_ne_goto)
540 ax_debug ("Combining equal & log_not & if_goto");
541 pc += 2;
542 aentry->pc = pc;
543 arg = aexpr->bytes[pc++];
544 arg = (arg << 8) + aexpr->bytes[pc++];
545 aentry->goto_pc = arg;
546 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
548 else
549 emit_equal ();
550 break;
552 case gdb_agent_op_less_signed:
553 next_op = aexpr->bytes[pc];
554 if (next_op == gdb_agent_op_if_goto
555 && !is_goto_target (aexpr, pc))
557 ax_debug ("Combining less_signed & if_goto");
558 pc += 1;
559 aentry->pc = pc;
560 arg = aexpr->bytes[pc++];
561 arg = (arg << 8) + aexpr->bytes[pc++];
562 aentry->goto_pc = arg;
563 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
565 else if (next_op == gdb_agent_op_log_not
566 && !is_goto_target (aexpr, pc)
567 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
568 && !is_goto_target (aexpr, pc + 1))
570 ax_debug ("Combining less_signed & log_not & if_goto");
571 pc += 2;
572 aentry->pc = pc;
573 arg = aexpr->bytes[pc++];
574 arg = (arg << 8) + aexpr->bytes[pc++];
575 aentry->goto_pc = arg;
576 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
578 else
579 emit_less_signed ();
580 break;
582 case gdb_agent_op_less_unsigned:
583 emit_less_unsigned ();
584 break;
586 case gdb_agent_op_ext:
587 arg = aexpr->bytes[pc++];
588 if (arg < (sizeof (LONGEST) * 8))
589 emit_ext (arg);
590 break;
592 case gdb_agent_op_ref8:
593 emit_ref (1);
594 break;
596 case gdb_agent_op_ref16:
597 emit_ref (2);
598 break;
600 case gdb_agent_op_ref32:
601 emit_ref (4);
602 break;
604 case gdb_agent_op_ref64:
605 emit_ref (8);
606 break;
608 case gdb_agent_op_if_goto:
609 arg = aexpr->bytes[pc++];
610 arg = (arg << 8) + aexpr->bytes[pc++];
611 aentry->goto_pc = arg;
612 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
613 break;
615 case gdb_agent_op_goto:
616 arg = aexpr->bytes[pc++];
617 arg = (arg << 8) + aexpr->bytes[pc++];
618 aentry->goto_pc = arg;
619 emit_goto (&(aentry->from_offset), &(aentry->from_size));
620 break;
622 case gdb_agent_op_const8:
623 emit_stack_flush ();
624 top = aexpr->bytes[pc++];
625 emit_const (top);
626 break;
628 case gdb_agent_op_const16:
629 emit_stack_flush ();
630 top = aexpr->bytes[pc++];
631 top = (top << 8) + aexpr->bytes[pc++];
632 emit_const (top);
633 break;
635 case gdb_agent_op_const32:
636 emit_stack_flush ();
637 top = aexpr->bytes[pc++];
638 top = (top << 8) + aexpr->bytes[pc++];
639 top = (top << 8) + aexpr->bytes[pc++];
640 top = (top << 8) + aexpr->bytes[pc++];
641 emit_const (top);
642 break;
644 case gdb_agent_op_const64:
645 emit_stack_flush ();
646 top = aexpr->bytes[pc++];
647 top = (top << 8) + aexpr->bytes[pc++];
648 top = (top << 8) + aexpr->bytes[pc++];
649 top = (top << 8) + aexpr->bytes[pc++];
650 top = (top << 8) + aexpr->bytes[pc++];
651 top = (top << 8) + aexpr->bytes[pc++];
652 top = (top << 8) + aexpr->bytes[pc++];
653 top = (top << 8) + aexpr->bytes[pc++];
654 emit_const (top);
655 break;
657 case gdb_agent_op_reg:
658 emit_stack_flush ();
659 arg = aexpr->bytes[pc++];
660 arg = (arg << 8) + aexpr->bytes[pc++];
661 emit_reg (arg);
662 break;
664 case gdb_agent_op_end:
665 ax_debug ("At end of expression\n");
667 /* Assume there is one stack element left, and that it is
668 cached in "top" where emit_epilogue can get to it. */
669 emit_stack_adjust (1);
671 done = 1;
672 break;
674 case gdb_agent_op_dup:
675 /* In our design, dup is equivalent to stack flushing. */
676 emit_stack_flush ();
677 break;
679 case gdb_agent_op_pop:
680 emit_pop ();
681 break;
683 case gdb_agent_op_zero_ext:
684 arg = aexpr->bytes[pc++];
685 if (arg < (sizeof (LONGEST) * 8))
686 emit_zero_ext (arg);
687 break;
689 case gdb_agent_op_swap:
690 next_op = aexpr->bytes[pc];
691 /* Detect greater-than comparison sequences. */
692 if (next_op == gdb_agent_op_less_signed
693 && !is_goto_target (aexpr, pc)
694 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
695 && !is_goto_target (aexpr, pc + 1))
697 ax_debug ("Combining swap & less_signed & if_goto");
698 pc += 2;
699 aentry->pc = pc;
700 arg = aexpr->bytes[pc++];
701 arg = (arg << 8) + aexpr->bytes[pc++];
702 aentry->goto_pc = arg;
703 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
705 else if (next_op == gdb_agent_op_less_signed
706 && !is_goto_target (aexpr, pc)
707 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
708 && !is_goto_target (aexpr, pc + 1)
709 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
710 && !is_goto_target (aexpr, pc + 2))
712 ax_debug ("Combining swap & less_signed & log_not & if_goto");
713 pc += 3;
714 aentry->pc = pc;
715 arg = aexpr->bytes[pc++];
716 arg = (arg << 8) + aexpr->bytes[pc++];
717 aentry->goto_pc = arg;
718 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
720 else
721 emit_swap ();
722 break;
724 case gdb_agent_op_getv:
725 emit_stack_flush ();
726 arg = aexpr->bytes[pc++];
727 arg = (arg << 8) + aexpr->bytes[pc++];
728 emit_int_call_1 (get_get_tsv_func_addr (),
729 arg);
730 break;
732 case gdb_agent_op_setv:
733 arg = aexpr->bytes[pc++];
734 arg = (arg << 8) + aexpr->bytes[pc++];
735 emit_void_call_2 (get_set_tsv_func_addr (),
736 arg);
737 break;
739 case gdb_agent_op_tracev:
740 UNHANDLED;
741 break;
743 /* GDB never (currently) generates any of these ops. */
744 case gdb_agent_op_float:
745 case gdb_agent_op_ref_float:
746 case gdb_agent_op_ref_double:
747 case gdb_agent_op_ref_long_double:
748 case gdb_agent_op_l_to_d:
749 case gdb_agent_op_d_to_l:
750 case gdb_agent_op_trace16:
751 UNHANDLED;
752 break;
754 default:
755 ax_debug ("Agent expression op 0x%x not recognized\n", op);
756 /* Don't struggle on, things will just get worse. */
757 return expr_eval_unrecognized_opcode;
760 /* This catches errors that occur in target-specific code
761 emission. */
762 if (emit_error)
764 ax_debug ("Error %d while emitting code for %s\n",
765 emit_error, gdb_agent_op_name (op));
766 return expr_eval_unhandled_opcode;
769 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
772 /* Now fill in real addresses as goto destinations. */
773 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
775 int written = 0;
777 if (aentry->goto_pc < 0)
778 continue;
780 /* Find the location that we are going to, and call back into
781 target-specific code to write the actual address or
782 displacement. */
783 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
785 if (aentry2->pc == aentry->goto_pc)
787 ax_debug ("Want to jump from %s to %s\n",
788 paddress (aentry->address),
789 paddress (aentry2->address));
790 write_goto_address (aentry->address + aentry->from_offset,
791 aentry2->address, aentry->from_size);
792 written = 1;
793 break;
797 /* Error out if we didn't find a destination. */
798 if (!written)
800 ax_debug ("Destination of goto %d not found\n",
801 aentry->goto_pc);
802 return expr_eval_invalid_goto;
806 return expr_eval_no_error;
809 #endif
811 /* Make printf-type calls using arguments supplied from the host. We
812 need to parse the format string ourselves, and call the formatting
813 function with one argument at a time, partly because there is no
814 safe portable way to construct a varargs call, and partly to serve
815 as a security barrier against bad format strings that might get
816 in. */
818 static void
819 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
820 int nargs, ULONGEST *args)
822 const char *f = format;
823 int i;
824 const char *current_substring;
825 int nargs_wanted;
827 ax_debug ("Printf of \"%s\" with %d args", format, nargs);
829 format_pieces fpieces (&f);
831 nargs_wanted = 0;
832 for (auto &&piece : fpieces)
833 if (piece.argclass != literal_piece)
834 ++nargs_wanted;
836 if (nargs != nargs_wanted)
837 error (_("Wrong number of arguments for specified format-string"));
839 i = 0;
840 for (auto &&piece : fpieces)
842 current_substring = piece.string;
843 ax_debug ("current substring is '%s', class is %d",
844 current_substring, piece.argclass);
845 switch (piece.argclass)
847 case string_arg:
849 gdb_byte *str;
850 CORE_ADDR tem;
851 int j;
853 tem = args[i];
854 if (tem == 0)
856 printf (current_substring, "(null)");
857 break;
860 /* This is a %s argument. Find the length of the string. */
861 for (j = 0;; j++)
863 gdb_byte c;
865 read_inferior_memory (tem + j, &c, 1);
866 if (c == 0)
867 break;
870 /* Copy the string contents into a string inside GDB. */
871 str = (gdb_byte *) alloca (j + 1);
872 if (j != 0)
873 read_inferior_memory (tem, str, j);
874 str[j] = 0;
876 printf (current_substring, (char *) str);
878 break;
880 case long_long_arg:
881 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
883 long long val = args[i];
885 printf (current_substring, val);
886 break;
888 #else
889 error (_("long long not supported in agent printf"));
890 #endif
891 case int_arg:
893 int val = args[i];
895 printf (current_substring, val);
896 break;
899 case long_arg:
901 long val = args[i];
903 printf (current_substring, val);
904 break;
907 case size_t_arg:
909 size_t val = args[i];
911 printf (current_substring, val);
912 break;
915 case literal_piece:
916 /* Print a portion of the format string that has no
917 directives. Note that this will not include any
918 ordinary %-specs, but it might include "%%". That is
919 why we use printf_filtered and not puts_filtered here.
920 Also, we pass a dummy argument because some platforms
921 have modified GCC to include -Wformat-security by
922 default, which will warn here if there is no
923 argument. */
924 printf (current_substring, 0);
925 break;
927 default:
928 error (_("Format directive in '%s' not supported in agent printf"),
929 current_substring);
932 /* Maybe advance to the next argument. */
933 if (piece.argclass != literal_piece)
934 ++i;
937 fflush (stdout);
940 /* The agent expression evaluator, as specified by the GDB docs. It
941 returns 0 if everything went OK, and a nonzero error code
942 otherwise. */
944 enum eval_result_type
945 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
946 struct agent_expr *aexpr,
947 ULONGEST *rslt)
949 int pc = 0;
950 #define STACK_MAX 100
951 ULONGEST stack[STACK_MAX], top;
952 int sp = 0;
953 unsigned char op;
954 int arg;
956 /* This union is a convenient way to convert representations. For
957 now, assume a standard architecture where the hardware integer
958 types have 8, 16, 32, 64 bit types. A more robust solution would
959 be to import stdint.h from gnulib. */
960 union
962 union
964 unsigned char bytes[1];
965 unsigned char val;
966 } u8;
967 union
969 unsigned char bytes[2];
970 unsigned short val;
971 } u16;
972 union
974 unsigned char bytes[4];
975 unsigned int val;
976 } u32;
977 union
979 unsigned char bytes[8];
980 ULONGEST val;
981 } u64;
982 } cnv;
984 if (aexpr->length == 0)
986 ax_debug ("empty agent expression");
987 return expr_eval_empty_expression;
990 /* Cache the stack top in its own variable. Much of the time we can
991 operate on this variable, rather than dinking with the stack. It
992 needs to be copied to the stack when sp changes. */
993 top = 0;
995 while (1)
997 op = aexpr->bytes[pc++];
999 ax_debug ("About to interpret byte 0x%x", op);
1001 switch (op)
1003 case gdb_agent_op_add:
1004 top += stack[--sp];
1005 break;
1007 case gdb_agent_op_sub:
1008 top = stack[--sp] - top;
1009 break;
1011 case gdb_agent_op_mul:
1012 top *= stack[--sp];
1013 break;
1015 case gdb_agent_op_div_signed:
1016 if (top == 0)
1018 ax_debug ("Attempted to divide by zero");
1019 return expr_eval_divide_by_zero;
1021 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
1022 break;
1024 case gdb_agent_op_div_unsigned:
1025 if (top == 0)
1027 ax_debug ("Attempted to divide by zero");
1028 return expr_eval_divide_by_zero;
1030 top = stack[--sp] / top;
1031 break;
1033 case gdb_agent_op_rem_signed:
1034 if (top == 0)
1036 ax_debug ("Attempted to divide by zero");
1037 return expr_eval_divide_by_zero;
1039 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1040 break;
1042 case gdb_agent_op_rem_unsigned:
1043 if (top == 0)
1045 ax_debug ("Attempted to divide by zero");
1046 return expr_eval_divide_by_zero;
1048 top = stack[--sp] % top;
1049 break;
1051 case gdb_agent_op_lsh:
1052 top = stack[--sp] << top;
1053 break;
1055 case gdb_agent_op_rsh_signed:
1056 top = ((LONGEST) stack[--sp]) >> top;
1057 break;
1059 case gdb_agent_op_rsh_unsigned:
1060 top = stack[--sp] >> top;
1061 break;
1063 case gdb_agent_op_trace:
1064 agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
1065 (ULONGEST) top);
1066 if (--sp >= 0)
1067 top = stack[sp];
1068 break;
1070 case gdb_agent_op_trace_quick:
1071 arg = aexpr->bytes[pc++];
1072 agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1073 break;
1075 case gdb_agent_op_log_not:
1076 top = !top;
1077 break;
1079 case gdb_agent_op_bit_and:
1080 top &= stack[--sp];
1081 break;
1083 case gdb_agent_op_bit_or:
1084 top |= stack[--sp];
1085 break;
1087 case gdb_agent_op_bit_xor:
1088 top ^= stack[--sp];
1089 break;
1091 case gdb_agent_op_bit_not:
1092 top = ~top;
1093 break;
1095 case gdb_agent_op_equal:
1096 top = (stack[--sp] == top);
1097 break;
1099 case gdb_agent_op_less_signed:
1100 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1101 break;
1103 case gdb_agent_op_less_unsigned:
1104 top = (stack[--sp] < top);
1105 break;
1107 case gdb_agent_op_ext:
1108 arg = aexpr->bytes[pc++];
1109 if (arg < (sizeof (LONGEST) * 8))
1111 LONGEST mask = 1 << (arg - 1);
1112 top &= ((LONGEST) 1 << arg) - 1;
1113 top = (top ^ mask) - mask;
1115 break;
1117 case gdb_agent_op_ref8:
1118 agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
1119 top = cnv.u8.val;
1120 break;
1122 case gdb_agent_op_ref16:
1123 agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
1124 top = cnv.u16.val;
1125 break;
1127 case gdb_agent_op_ref32:
1128 agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
1129 top = cnv.u32.val;
1130 break;
1132 case gdb_agent_op_ref64:
1133 agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
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 (__FILE__, __LINE__,
1214 "unhandled register size");
1217 break;
1219 case gdb_agent_op_end:
1220 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1221 sp, pulongest (top));
1222 if (rslt)
1224 if (sp <= 0)
1226 /* This should be an error */
1227 ax_debug ("Stack is empty, nothing to return");
1228 return expr_eval_empty_stack;
1230 *rslt = top;
1232 return expr_eval_no_error;
1234 case gdb_agent_op_dup:
1235 stack[sp++] = top;
1236 break;
1238 case gdb_agent_op_pop:
1239 if (--sp >= 0)
1240 top = stack[sp];
1241 break;
1243 case gdb_agent_op_pick:
1244 arg = aexpr->bytes[pc++];
1245 stack[sp] = top;
1246 top = stack[sp - arg];
1247 ++sp;
1248 break;
1250 case gdb_agent_op_rot:
1252 ULONGEST tem = stack[sp - 1];
1254 stack[sp - 1] = stack[sp - 2];
1255 stack[sp - 2] = top;
1256 top = tem;
1258 break;
1260 case gdb_agent_op_zero_ext:
1261 arg = aexpr->bytes[pc++];
1262 if (arg < (sizeof (LONGEST) * 8))
1263 top &= ((LONGEST) 1 << arg) - 1;
1264 break;
1266 case gdb_agent_op_swap:
1267 /* Interchange top two stack elements, making sure top gets
1268 copied back onto stack. */
1269 stack[sp] = top;
1270 top = stack[sp - 1];
1271 stack[sp - 1] = stack[sp];
1272 break;
1274 case gdb_agent_op_getv:
1275 /* Flush the cached stack top. */
1276 stack[sp++] = top;
1277 arg = aexpr->bytes[pc++];
1278 arg = (arg << 8) + aexpr->bytes[pc++];
1279 top = agent_get_trace_state_variable_value (arg);
1280 break;
1282 case gdb_agent_op_setv:
1283 arg = aexpr->bytes[pc++];
1284 arg = (arg << 8) + aexpr->bytes[pc++];
1285 agent_set_trace_state_variable_value (arg, top);
1286 /* Note that we leave the value on the stack, for the
1287 benefit of later/enclosing expressions. */
1288 break;
1290 case gdb_agent_op_tracev:
1291 arg = aexpr->bytes[pc++];
1292 arg = (arg << 8) + aexpr->bytes[pc++];
1293 agent_tsv_read (ctx, arg);
1294 break;
1296 case gdb_agent_op_tracenz:
1297 agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
1298 (ULONGEST) top);
1299 if (--sp >= 0)
1300 top = stack[sp];
1301 break;
1303 case gdb_agent_op_printf:
1305 int nargs, slen, i;
1306 CORE_ADDR fn = 0, chan = 0;
1307 /* Can't have more args than the entire size of the stack. */
1308 ULONGEST args[STACK_MAX];
1309 char *format;
1311 nargs = aexpr->bytes[pc++];
1312 slen = aexpr->bytes[pc++];
1313 slen = (slen << 8) + aexpr->bytes[pc++];
1314 format = (char *) &(aexpr->bytes[pc]);
1315 pc += slen;
1316 /* Pop function and channel. */
1317 fn = top;
1318 if (--sp >= 0)
1319 top = stack[sp];
1320 chan = top;
1321 if (--sp >= 0)
1322 top = stack[sp];
1323 /* Pop arguments into a dedicated array. */
1324 for (i = 0; i < nargs; ++i)
1326 args[i] = top;
1327 if (--sp >= 0)
1328 top = stack[sp];
1331 /* A bad format string means something is very wrong; give
1332 up immediately. */
1333 if (format[slen - 1] != '\0')
1334 error (_("Unterminated format string in printf bytecode"));
1336 ax_printf (fn, chan, format, nargs, args);
1338 break;
1340 /* GDB never (currently) generates any of these ops. */
1341 case gdb_agent_op_float:
1342 case gdb_agent_op_ref_float:
1343 case gdb_agent_op_ref_double:
1344 case gdb_agent_op_ref_long_double:
1345 case gdb_agent_op_l_to_d:
1346 case gdb_agent_op_d_to_l:
1347 case gdb_agent_op_trace16:
1348 ax_debug ("Agent expression op 0x%x valid, but not handled",
1349 op);
1350 /* If ever GDB generates any of these, we don't have the
1351 option of ignoring. */
1352 return expr_eval_unhandled_opcode;
1354 default:
1355 ax_debug ("Agent expression op 0x%x not recognized", op);
1356 /* Don't struggle on, things will just get worse. */
1357 return expr_eval_unrecognized_opcode;
1360 /* Check for stack badness. */
1361 if (sp >= (STACK_MAX - 1))
1363 ax_debug ("Expression stack overflow");
1364 return expr_eval_stack_overflow;
1367 if (sp < 0)
1369 ax_debug ("Expression stack underflow");
1370 return expr_eval_stack_underflow;
1373 ax_debug ("Op %s -> sp=%d, top=0x%s",
1374 gdb_agent_op_name (op), sp, phex_nz (top, 0));