V850 Linker: do not complain about RWX segments.
[binutils-gdb.git] / gdbserver / ax.cc
blobe5b4542825c2e13b308173fab7e3a9a25ec47682
1 /* Agent expression code for remote server.
2 Copyright (C) 2009-2022 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 bool 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 threads_debug_printf (PROG "/ax: %s", buf);
43 #endif
44 va_end (ap);
47 #define ax_debug(fmt, args...) \
48 do { \
49 if (debug_threads) \
50 ax_vdebug ((fmt), ##args); \
51 } while (0)
53 /* This enum must exactly match what is documented in
54 gdb/doc/agentexpr.texi, including all the numerical values. */
56 enum gdb_agent_op
58 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
59 gdb_agent_op_ ## NAME = VALUE,
60 #include "gdbsupport/ax.def"
61 #undef DEFOP
62 gdb_agent_op_last
65 static const char * const gdb_agent_op_names [gdb_agent_op_last] =
67 "?undef?"
68 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
69 #include "gdbsupport/ax.def"
70 #undef DEFOP
73 #ifndef IN_PROCESS_AGENT
74 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
77 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
78 #include "gdbsupport/ax.def"
79 #undef DEFOP
81 #endif
83 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
85 static const char *
86 gdb_agent_op_name (int op)
88 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
89 return "?undef?";
90 return gdb_agent_op_names[op];
93 #ifndef IN_PROCESS_AGENT
95 /* The packet form of an agent expression consists of an 'X', number
96 of bytes in expression, a comma, and then the bytes. */
98 struct agent_expr *
99 gdb_parse_agent_expr (const char **actparm)
101 const char *act = *actparm;
102 ULONGEST xlen;
103 struct agent_expr *aexpr;
105 ++act; /* skip the X */
106 act = unpack_varlen_hex (act, &xlen);
107 ++act; /* skip a comma */
108 aexpr = XNEW (struct agent_expr);
109 aexpr->length = xlen;
110 aexpr->bytes = (unsigned char *) xmalloc (xlen);
111 hex2bin (act, aexpr->bytes, xlen);
112 *actparm = act + (xlen * 2);
113 return aexpr;
116 void
117 gdb_free_agent_expr (struct agent_expr *aexpr)
119 if (aexpr != NULL)
121 free (aexpr->bytes);
122 free (aexpr);
126 /* Convert the bytes of an agent expression back into hex digits, so
127 they can be printed or uploaded. This allocates the buffer,
128 callers should free when they are done with it. */
130 char *
131 gdb_unparse_agent_expr (struct agent_expr *aexpr)
133 char *rslt;
135 rslt = (char *) xmalloc (2 * aexpr->length + 1);
136 bin2hex (aexpr->bytes, rslt, aexpr->length);
137 return rslt;
140 /* Bytecode compilation. */
142 CORE_ADDR current_insn_ptr;
144 int emit_error;
146 static struct bytecode_address
148 int pc;
149 CORE_ADDR address;
150 int goto_pc;
151 /* Offset and size of field to be modified in the goto block. */
152 int from_offset, from_size;
153 struct bytecode_address *next;
154 } *bytecode_address_table;
156 void
157 emit_prologue (void)
159 target_emit_ops ()->emit_prologue ();
162 void
163 emit_epilogue (void)
165 target_emit_ops ()->emit_epilogue ();
168 static void
169 emit_add (void)
171 target_emit_ops ()->emit_add ();
174 static void
175 emit_sub (void)
177 target_emit_ops ()->emit_sub ();
180 static void
181 emit_mul (void)
183 target_emit_ops ()->emit_mul ();
186 static void
187 emit_lsh (void)
189 target_emit_ops ()->emit_lsh ();
192 static void
193 emit_rsh_signed (void)
195 target_emit_ops ()->emit_rsh_signed ();
198 static void
199 emit_rsh_unsigned (void)
201 target_emit_ops ()->emit_rsh_unsigned ();
204 static void
205 emit_ext (int arg)
207 target_emit_ops ()->emit_ext (arg);
210 static void
211 emit_log_not (void)
213 target_emit_ops ()->emit_log_not ();
216 static void
217 emit_bit_and (void)
219 target_emit_ops ()->emit_bit_and ();
222 static void
223 emit_bit_or (void)
225 target_emit_ops ()->emit_bit_or ();
228 static void
229 emit_bit_xor (void)
231 target_emit_ops ()->emit_bit_xor ();
234 static void
235 emit_bit_not (void)
237 target_emit_ops ()->emit_bit_not ();
240 static void
241 emit_equal (void)
243 target_emit_ops ()->emit_equal ();
246 static void
247 emit_less_signed (void)
249 target_emit_ops ()->emit_less_signed ();
252 static void
253 emit_less_unsigned (void)
255 target_emit_ops ()->emit_less_unsigned ();
258 static void
259 emit_ref (int size)
261 target_emit_ops ()->emit_ref (size);
264 static void
265 emit_if_goto (int *offset_p, int *size_p)
267 target_emit_ops ()->emit_if_goto (offset_p, size_p);
270 static void
271 emit_goto (int *offset_p, int *size_p)
273 target_emit_ops ()->emit_goto (offset_p, size_p);
276 static void
277 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
279 target_emit_ops ()->write_goto_address (from, to, size);
282 static void
283 emit_const (LONGEST num)
285 target_emit_ops ()->emit_const (num);
288 static void
289 emit_reg (int reg)
291 target_emit_ops ()->emit_reg (reg);
294 static void
295 emit_pop (void)
297 target_emit_ops ()->emit_pop ();
300 static void
301 emit_stack_flush (void)
303 target_emit_ops ()->emit_stack_flush ();
306 static void
307 emit_zero_ext (int arg)
309 target_emit_ops ()->emit_zero_ext (arg);
312 static void
313 emit_swap (void)
315 target_emit_ops ()->emit_swap ();
318 static void
319 emit_stack_adjust (int n)
321 target_emit_ops ()->emit_stack_adjust (n);
324 /* FN's prototype is `LONGEST(*fn)(int)'. */
326 static void
327 emit_int_call_1 (CORE_ADDR fn, int arg1)
329 target_emit_ops ()->emit_int_call_1 (fn, arg1);
332 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
334 static void
335 emit_void_call_2 (CORE_ADDR fn, int arg1)
337 target_emit_ops ()->emit_void_call_2 (fn, arg1);
340 static void
341 emit_eq_goto (int *offset_p, int *size_p)
343 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
346 static void
347 emit_ne_goto (int *offset_p, int *size_p)
349 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
352 static void
353 emit_lt_goto (int *offset_p, int *size_p)
355 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
358 static void
359 emit_ge_goto (int *offset_p, int *size_p)
361 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
364 static void
365 emit_gt_goto (int *offset_p, int *size_p)
367 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
370 static void
371 emit_le_goto (int *offset_p, int *size_p)
373 target_emit_ops ()->emit_le_goto (offset_p, size_p);
376 /* Scan an agent expression for any evidence that the given PC is the
377 target of a jump bytecode in the expression. */
379 static int
380 is_goto_target (struct agent_expr *aexpr, int pc)
382 int i;
383 unsigned char op;
385 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
387 op = aexpr->bytes[i];
389 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
391 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
392 if (target == pc)
393 return 1;
397 return 0;
400 /* Given an agent expression, turn it into native code. */
402 enum eval_result_type
403 compile_bytecodes (struct agent_expr *aexpr)
405 int pc = 0;
406 int done = 0;
407 unsigned char op, next_op;
408 int arg;
409 /* This is only used to build 64-bit value for constants. */
410 ULONGEST top;
411 struct bytecode_address *aentry, *aentry2;
413 #define UNHANDLED \
414 do \
416 ax_debug ("Cannot compile op 0x%x\n", op); \
417 return expr_eval_unhandled_opcode; \
418 } while (0)
420 if (aexpr->length == 0)
422 ax_debug ("empty agent expression\n");
423 return expr_eval_empty_expression;
426 bytecode_address_table = NULL;
428 while (!done)
430 op = aexpr->bytes[pc];
432 ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
434 /* Record the compiled-code address of the bytecode, for use by
435 jump instructions. */
436 aentry = XNEW (struct bytecode_address);
437 aentry->pc = pc;
438 aentry->address = current_insn_ptr;
439 aentry->goto_pc = -1;
440 aentry->from_offset = aentry->from_size = 0;
441 aentry->next = bytecode_address_table;
442 bytecode_address_table = aentry;
444 ++pc;
446 emit_error = 0;
448 switch (op)
450 case gdb_agent_op_add:
451 emit_add ();
452 break;
454 case gdb_agent_op_sub:
455 emit_sub ();
456 break;
458 case gdb_agent_op_mul:
459 emit_mul ();
460 break;
462 case gdb_agent_op_div_signed:
463 UNHANDLED;
464 break;
466 case gdb_agent_op_div_unsigned:
467 UNHANDLED;
468 break;
470 case gdb_agent_op_rem_signed:
471 UNHANDLED;
472 break;
474 case gdb_agent_op_rem_unsigned:
475 UNHANDLED;
476 break;
478 case gdb_agent_op_lsh:
479 emit_lsh ();
480 break;
482 case gdb_agent_op_rsh_signed:
483 emit_rsh_signed ();
484 break;
486 case gdb_agent_op_rsh_unsigned:
487 emit_rsh_unsigned ();
488 break;
490 case gdb_agent_op_trace:
491 UNHANDLED;
492 break;
494 case gdb_agent_op_trace_quick:
495 UNHANDLED;
496 break;
498 case gdb_agent_op_log_not:
499 emit_log_not ();
500 break;
502 case gdb_agent_op_bit_and:
503 emit_bit_and ();
504 break;
506 case gdb_agent_op_bit_or:
507 emit_bit_or ();
508 break;
510 case gdb_agent_op_bit_xor:
511 emit_bit_xor ();
512 break;
514 case gdb_agent_op_bit_not:
515 emit_bit_not ();
516 break;
518 case gdb_agent_op_equal:
519 next_op = aexpr->bytes[pc];
520 if (next_op == gdb_agent_op_if_goto
521 && !is_goto_target (aexpr, pc)
522 && target_emit_ops ()->emit_eq_goto)
524 ax_debug ("Combining equal & if_goto");
525 pc += 1;
526 aentry->pc = pc;
527 arg = aexpr->bytes[pc++];
528 arg = (arg << 8) + aexpr->bytes[pc++];
529 aentry->goto_pc = arg;
530 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
532 else if (next_op == gdb_agent_op_log_not
533 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
534 && !is_goto_target (aexpr, pc + 1)
535 && target_emit_ops ()->emit_ne_goto)
537 ax_debug ("Combining equal & log_not & if_goto");
538 pc += 2;
539 aentry->pc = pc;
540 arg = aexpr->bytes[pc++];
541 arg = (arg << 8) + aexpr->bytes[pc++];
542 aentry->goto_pc = arg;
543 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
545 else
546 emit_equal ();
547 break;
549 case gdb_agent_op_less_signed:
550 next_op = aexpr->bytes[pc];
551 if (next_op == gdb_agent_op_if_goto
552 && !is_goto_target (aexpr, pc))
554 ax_debug ("Combining less_signed & if_goto");
555 pc += 1;
556 aentry->pc = pc;
557 arg = aexpr->bytes[pc++];
558 arg = (arg << 8) + aexpr->bytes[pc++];
559 aentry->goto_pc = arg;
560 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
562 else if (next_op == gdb_agent_op_log_not
563 && !is_goto_target (aexpr, pc)
564 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
565 && !is_goto_target (aexpr, pc + 1))
567 ax_debug ("Combining less_signed & log_not & if_goto");
568 pc += 2;
569 aentry->pc = pc;
570 arg = aexpr->bytes[pc++];
571 arg = (arg << 8) + aexpr->bytes[pc++];
572 aentry->goto_pc = arg;
573 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
575 else
576 emit_less_signed ();
577 break;
579 case gdb_agent_op_less_unsigned:
580 emit_less_unsigned ();
581 break;
583 case gdb_agent_op_ext:
584 arg = aexpr->bytes[pc++];
585 if (arg < (sizeof (LONGEST) * 8))
586 emit_ext (arg);
587 break;
589 case gdb_agent_op_ref8:
590 emit_ref (1);
591 break;
593 case gdb_agent_op_ref16:
594 emit_ref (2);
595 break;
597 case gdb_agent_op_ref32:
598 emit_ref (4);
599 break;
601 case gdb_agent_op_ref64:
602 emit_ref (8);
603 break;
605 case gdb_agent_op_if_goto:
606 arg = aexpr->bytes[pc++];
607 arg = (arg << 8) + aexpr->bytes[pc++];
608 aentry->goto_pc = arg;
609 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
610 break;
612 case gdb_agent_op_goto:
613 arg = aexpr->bytes[pc++];
614 arg = (arg << 8) + aexpr->bytes[pc++];
615 aentry->goto_pc = arg;
616 emit_goto (&(aentry->from_offset), &(aentry->from_size));
617 break;
619 case gdb_agent_op_const8:
620 emit_stack_flush ();
621 top = aexpr->bytes[pc++];
622 emit_const (top);
623 break;
625 case gdb_agent_op_const16:
626 emit_stack_flush ();
627 top = aexpr->bytes[pc++];
628 top = (top << 8) + aexpr->bytes[pc++];
629 emit_const (top);
630 break;
632 case gdb_agent_op_const32:
633 emit_stack_flush ();
634 top = aexpr->bytes[pc++];
635 top = (top << 8) + aexpr->bytes[pc++];
636 top = (top << 8) + aexpr->bytes[pc++];
637 top = (top << 8) + aexpr->bytes[pc++];
638 emit_const (top);
639 break;
641 case gdb_agent_op_const64:
642 emit_stack_flush ();
643 top = 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 top = (top << 8) + aexpr->bytes[pc++];
651 emit_const (top);
652 break;
654 case gdb_agent_op_reg:
655 emit_stack_flush ();
656 arg = aexpr->bytes[pc++];
657 arg = (arg << 8) + aexpr->bytes[pc++];
658 emit_reg (arg);
659 break;
661 case gdb_agent_op_end:
662 ax_debug ("At end of expression\n");
664 /* Assume there is one stack element left, and that it is
665 cached in "top" where emit_epilogue can get to it. */
666 emit_stack_adjust (1);
668 done = 1;
669 break;
671 case gdb_agent_op_dup:
672 /* In our design, dup is equivalent to stack flushing. */
673 emit_stack_flush ();
674 break;
676 case gdb_agent_op_pop:
677 emit_pop ();
678 break;
680 case gdb_agent_op_zero_ext:
681 arg = aexpr->bytes[pc++];
682 if (arg < (sizeof (LONGEST) * 8))
683 emit_zero_ext (arg);
684 break;
686 case gdb_agent_op_swap:
687 next_op = aexpr->bytes[pc];
688 /* Detect greater-than comparison sequences. */
689 if (next_op == gdb_agent_op_less_signed
690 && !is_goto_target (aexpr, pc)
691 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
692 && !is_goto_target (aexpr, pc + 1))
694 ax_debug ("Combining swap & less_signed & if_goto");
695 pc += 2;
696 aentry->pc = pc;
697 arg = aexpr->bytes[pc++];
698 arg = (arg << 8) + aexpr->bytes[pc++];
699 aentry->goto_pc = arg;
700 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
702 else if (next_op == gdb_agent_op_less_signed
703 && !is_goto_target (aexpr, pc)
704 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
705 && !is_goto_target (aexpr, pc + 1)
706 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
707 && !is_goto_target (aexpr, pc + 2))
709 ax_debug ("Combining swap & less_signed & log_not & if_goto");
710 pc += 3;
711 aentry->pc = pc;
712 arg = aexpr->bytes[pc++];
713 arg = (arg << 8) + aexpr->bytes[pc++];
714 aentry->goto_pc = arg;
715 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
717 else
718 emit_swap ();
719 break;
721 case gdb_agent_op_getv:
722 emit_stack_flush ();
723 arg = aexpr->bytes[pc++];
724 arg = (arg << 8) + aexpr->bytes[pc++];
725 emit_int_call_1 (get_get_tsv_func_addr (),
726 arg);
727 break;
729 case gdb_agent_op_setv:
730 arg = aexpr->bytes[pc++];
731 arg = (arg << 8) + aexpr->bytes[pc++];
732 emit_void_call_2 (get_set_tsv_func_addr (),
733 arg);
734 break;
736 case gdb_agent_op_tracev:
737 UNHANDLED;
738 break;
740 /* GDB never (currently) generates any of these ops. */
741 case gdb_agent_op_float:
742 case gdb_agent_op_ref_float:
743 case gdb_agent_op_ref_double:
744 case gdb_agent_op_ref_long_double:
745 case gdb_agent_op_l_to_d:
746 case gdb_agent_op_d_to_l:
747 case gdb_agent_op_trace16:
748 UNHANDLED;
749 break;
751 default:
752 ax_debug ("Agent expression op 0x%x not recognized\n", op);
753 /* Don't struggle on, things will just get worse. */
754 return expr_eval_unrecognized_opcode;
757 /* This catches errors that occur in target-specific code
758 emission. */
759 if (emit_error)
761 ax_debug ("Error %d while emitting code for %s\n",
762 emit_error, gdb_agent_op_name (op));
763 return expr_eval_unhandled_opcode;
766 ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
769 /* Now fill in real addresses as goto destinations. */
770 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
772 int written = 0;
774 if (aentry->goto_pc < 0)
775 continue;
777 /* Find the location that we are going to, and call back into
778 target-specific code to write the actual address or
779 displacement. */
780 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
782 if (aentry2->pc == aentry->goto_pc)
784 ax_debug ("Want to jump from %s to %s\n",
785 paddress (aentry->address),
786 paddress (aentry2->address));
787 write_goto_address (aentry->address + aentry->from_offset,
788 aentry2->address, aentry->from_size);
789 written = 1;
790 break;
794 /* Error out if we didn't find a destination. */
795 if (!written)
797 ax_debug ("Destination of goto %d not found\n",
798 aentry->goto_pc);
799 return expr_eval_invalid_goto;
803 return expr_eval_no_error;
806 #endif
808 /* Make printf-type calls using arguments supplied from the host. We
809 need to parse the format string ourselves, and call the formatting
810 function with one argument at a time, partly because there is no
811 safe portable way to construct a varargs call, and partly to serve
812 as a security barrier against bad format strings that might get
813 in. */
815 static void
816 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
817 int nargs, ULONGEST *args)
819 const char *f = format;
820 int i;
821 const char *current_substring;
822 int nargs_wanted;
824 ax_debug ("Printf of \"%s\" with %d args", format, nargs);
826 format_pieces fpieces (&f);
828 nargs_wanted = 0;
829 for (auto &&piece : fpieces)
830 if (piece.argclass != literal_piece)
831 ++nargs_wanted;
833 if (nargs != nargs_wanted)
834 error (_("Wrong number of arguments for specified format-string"));
836 i = 0;
837 for (auto &&piece : fpieces)
839 current_substring = piece.string;
840 ax_debug ("current substring is '%s', class is %d",
841 current_substring, piece.argclass);
842 switch (piece.argclass)
844 case string_arg:
846 gdb_byte *str;
847 CORE_ADDR tem;
848 int j;
850 tem = args[i];
851 if (tem == 0)
853 printf (current_substring, "(null)");
854 break;
857 /* This is a %s argument. Find the length of the string. */
858 for (j = 0;; j++)
860 gdb_byte c;
862 read_inferior_memory (tem + j, &c, 1);
863 if (c == 0)
864 break;
867 /* Copy the string contents into a string inside GDB. */
868 str = (gdb_byte *) alloca (j + 1);
869 if (j != 0)
870 read_inferior_memory (tem, str, j);
871 str[j] = 0;
873 printf (current_substring, (char *) str);
875 break;
877 case long_long_arg:
878 #if defined (PRINTF_HAS_LONG_LONG)
880 long long val = args[i];
882 printf (current_substring, val);
883 break;
885 #else
886 error (_("long long not supported in agent printf"));
887 #endif
888 case int_arg:
890 int val = args[i];
892 printf (current_substring, val);
893 break;
896 case long_arg:
898 long val = args[i];
900 printf (current_substring, val);
901 break;
904 case size_t_arg:
906 size_t val = args[i];
908 printf (current_substring, val);
909 break;
912 case literal_piece:
913 /* Print a portion of the format string that has no
914 directives. Note that this will not include any
915 ordinary %-specs, but it might include "%%". That is
916 why we use printf_filtered and not puts_filtered here.
917 Also, we pass a dummy argument because some platforms
918 have modified GCC to include -Wformat-security by
919 default, which will warn here if there is no
920 argument. */
921 printf (current_substring, 0);
922 break;
924 default:
925 error (_("Format directive in '%s' not supported in agent printf"),
926 current_substring);
929 /* Maybe advance to the next argument. */
930 if (piece.argclass != literal_piece)
931 ++i;
934 fflush (stdout);
937 /* The agent expression evaluator, as specified by the GDB docs. It
938 returns 0 if everything went OK, and a nonzero error code
939 otherwise. */
941 enum eval_result_type
942 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
943 struct agent_expr *aexpr,
944 ULONGEST *rslt)
946 int pc = 0;
947 #define STACK_MAX 100
948 ULONGEST stack[STACK_MAX], top;
949 int sp = 0;
950 unsigned char op;
951 int arg;
953 /* This union is a convenient way to convert representations. For
954 now, assume a standard architecture where the hardware integer
955 types have 8, 16, 32, 64 bit types. A more robust solution would
956 be to import stdint.h from gnulib. */
957 union
959 union
961 unsigned char bytes[1];
962 unsigned char val;
963 } u8;
964 union
966 unsigned char bytes[2];
967 unsigned short val;
968 } u16;
969 union
971 unsigned char bytes[4];
972 unsigned int val;
973 } u32;
974 union
976 unsigned char bytes[8];
977 ULONGEST val;
978 } u64;
979 } cnv;
981 if (aexpr->length == 0)
983 ax_debug ("empty agent expression");
984 return expr_eval_empty_expression;
987 /* Cache the stack top in its own variable. Much of the time we can
988 operate on this variable, rather than dinking with the stack. It
989 needs to be copied to the stack when sp changes. */
990 top = 0;
992 while (1)
994 op = aexpr->bytes[pc++];
996 ax_debug ("About to interpret byte 0x%x", op);
998 switch (op)
1000 case gdb_agent_op_add:
1001 top += stack[--sp];
1002 break;
1004 case gdb_agent_op_sub:
1005 top = stack[--sp] - top;
1006 break;
1008 case gdb_agent_op_mul:
1009 top *= stack[--sp];
1010 break;
1012 case gdb_agent_op_div_signed:
1013 if (top == 0)
1015 ax_debug ("Attempted to divide by zero");
1016 return expr_eval_divide_by_zero;
1018 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
1019 break;
1021 case gdb_agent_op_div_unsigned:
1022 if (top == 0)
1024 ax_debug ("Attempted to divide by zero");
1025 return expr_eval_divide_by_zero;
1027 top = stack[--sp] / top;
1028 break;
1030 case gdb_agent_op_rem_signed:
1031 if (top == 0)
1033 ax_debug ("Attempted to divide by zero");
1034 return expr_eval_divide_by_zero;
1036 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1037 break;
1039 case gdb_agent_op_rem_unsigned:
1040 if (top == 0)
1042 ax_debug ("Attempted to divide by zero");
1043 return expr_eval_divide_by_zero;
1045 top = stack[--sp] % top;
1046 break;
1048 case gdb_agent_op_lsh:
1049 top = stack[--sp] << top;
1050 break;
1052 case gdb_agent_op_rsh_signed:
1053 top = ((LONGEST) stack[--sp]) >> top;
1054 break;
1056 case gdb_agent_op_rsh_unsigned:
1057 top = stack[--sp] >> top;
1058 break;
1060 case gdb_agent_op_trace:
1061 agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
1062 (ULONGEST) top);
1063 if (--sp >= 0)
1064 top = stack[sp];
1065 break;
1067 case gdb_agent_op_trace_quick:
1068 arg = aexpr->bytes[pc++];
1069 agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1070 break;
1072 case gdb_agent_op_log_not:
1073 top = !top;
1074 break;
1076 case gdb_agent_op_bit_and:
1077 top &= stack[--sp];
1078 break;
1080 case gdb_agent_op_bit_or:
1081 top |= stack[--sp];
1082 break;
1084 case gdb_agent_op_bit_xor:
1085 top ^= stack[--sp];
1086 break;
1088 case gdb_agent_op_bit_not:
1089 top = ~top;
1090 break;
1092 case gdb_agent_op_equal:
1093 top = (stack[--sp] == top);
1094 break;
1096 case gdb_agent_op_less_signed:
1097 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1098 break;
1100 case gdb_agent_op_less_unsigned:
1101 top = (stack[--sp] < top);
1102 break;
1104 case gdb_agent_op_ext:
1105 arg = aexpr->bytes[pc++];
1106 if (arg < (sizeof (LONGEST) * 8))
1108 LONGEST mask = 1 << (arg - 1);
1109 top &= ((LONGEST) 1 << arg) - 1;
1110 top = (top ^ mask) - mask;
1112 break;
1114 case gdb_agent_op_ref8:
1115 agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
1116 top = cnv.u8.val;
1117 break;
1119 case gdb_agent_op_ref16:
1120 agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
1121 top = cnv.u16.val;
1122 break;
1124 case gdb_agent_op_ref32:
1125 agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
1126 top = cnv.u32.val;
1127 break;
1129 case gdb_agent_op_ref64:
1130 agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
1131 top = cnv.u64.val;
1132 break;
1134 case gdb_agent_op_if_goto:
1135 if (top)
1136 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1137 else
1138 pc += 2;
1139 if (--sp >= 0)
1140 top = stack[sp];
1141 break;
1143 case gdb_agent_op_goto:
1144 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1145 break;
1147 case gdb_agent_op_const8:
1148 /* Flush the cached stack top. */
1149 stack[sp++] = top;
1150 top = aexpr->bytes[pc++];
1151 break;
1153 case gdb_agent_op_const16:
1154 /* Flush the cached stack top. */
1155 stack[sp++] = top;
1156 top = aexpr->bytes[pc++];
1157 top = (top << 8) + aexpr->bytes[pc++];
1158 break;
1160 case gdb_agent_op_const32:
1161 /* Flush the cached stack top. */
1162 stack[sp++] = top;
1163 top = aexpr->bytes[pc++];
1164 top = (top << 8) + aexpr->bytes[pc++];
1165 top = (top << 8) + aexpr->bytes[pc++];
1166 top = (top << 8) + aexpr->bytes[pc++];
1167 break;
1169 case gdb_agent_op_const64:
1170 /* Flush the cached stack top. */
1171 stack[sp++] = top;
1172 top = aexpr->bytes[pc++];
1173 top = (top << 8) + aexpr->bytes[pc++];
1174 top = (top << 8) + aexpr->bytes[pc++];
1175 top = (top << 8) + 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 break;
1182 case gdb_agent_op_reg:
1183 /* Flush the cached stack top. */
1184 stack[sp++] = top;
1185 arg = aexpr->bytes[pc++];
1186 arg = (arg << 8) + aexpr->bytes[pc++];
1188 int regnum = arg;
1189 struct regcache *regcache = ctx->regcache;
1191 switch (register_size (regcache->tdesc, regnum))
1193 case 8:
1194 collect_register (regcache, regnum, cnv.u64.bytes);
1195 top = cnv.u64.val;
1196 break;
1197 case 4:
1198 collect_register (regcache, regnum, cnv.u32.bytes);
1199 top = cnv.u32.val;
1200 break;
1201 case 2:
1202 collect_register (regcache, regnum, cnv.u16.bytes);
1203 top = cnv.u16.val;
1204 break;
1205 case 1:
1206 collect_register (regcache, regnum, cnv.u8.bytes);
1207 top = cnv.u8.val;
1208 break;
1209 default:
1210 internal_error ("unhandled register size");
1213 break;
1215 case gdb_agent_op_end:
1216 ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1217 sp, pulongest (top));
1218 if (rslt)
1220 if (sp <= 0)
1222 /* This should be an error */
1223 ax_debug ("Stack is empty, nothing to return");
1224 return expr_eval_empty_stack;
1226 *rslt = top;
1228 return expr_eval_no_error;
1230 case gdb_agent_op_dup:
1231 stack[sp++] = top;
1232 break;
1234 case gdb_agent_op_pop:
1235 if (--sp >= 0)
1236 top = stack[sp];
1237 break;
1239 case gdb_agent_op_pick:
1240 arg = aexpr->bytes[pc++];
1241 stack[sp] = top;
1242 top = stack[sp - arg];
1243 ++sp;
1244 break;
1246 case gdb_agent_op_rot:
1248 ULONGEST tem = stack[sp - 1];
1250 stack[sp - 1] = stack[sp - 2];
1251 stack[sp - 2] = top;
1252 top = tem;
1254 break;
1256 case gdb_agent_op_zero_ext:
1257 arg = aexpr->bytes[pc++];
1258 if (arg < (sizeof (LONGEST) * 8))
1259 top &= ((LONGEST) 1 << arg) - 1;
1260 break;
1262 case gdb_agent_op_swap:
1263 /* Interchange top two stack elements, making sure top gets
1264 copied back onto stack. */
1265 stack[sp] = top;
1266 top = stack[sp - 1];
1267 stack[sp - 1] = stack[sp];
1268 break;
1270 case gdb_agent_op_getv:
1271 /* Flush the cached stack top. */
1272 stack[sp++] = top;
1273 arg = aexpr->bytes[pc++];
1274 arg = (arg << 8) + aexpr->bytes[pc++];
1275 top = agent_get_trace_state_variable_value (arg);
1276 break;
1278 case gdb_agent_op_setv:
1279 arg = aexpr->bytes[pc++];
1280 arg = (arg << 8) + aexpr->bytes[pc++];
1281 agent_set_trace_state_variable_value (arg, top);
1282 /* Note that we leave the value on the stack, for the
1283 benefit of later/enclosing expressions. */
1284 break;
1286 case gdb_agent_op_tracev:
1287 arg = aexpr->bytes[pc++];
1288 arg = (arg << 8) + aexpr->bytes[pc++];
1289 agent_tsv_read (ctx, arg);
1290 break;
1292 case gdb_agent_op_tracenz:
1293 agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
1294 (ULONGEST) top);
1295 if (--sp >= 0)
1296 top = stack[sp];
1297 break;
1299 case gdb_agent_op_printf:
1301 int nargs, slen, i;
1302 CORE_ADDR fn = 0, chan = 0;
1303 /* Can't have more args than the entire size of the stack. */
1304 ULONGEST args[STACK_MAX];
1305 char *format;
1307 nargs = aexpr->bytes[pc++];
1308 slen = aexpr->bytes[pc++];
1309 slen = (slen << 8) + aexpr->bytes[pc++];
1310 format = (char *) &(aexpr->bytes[pc]);
1311 pc += slen;
1312 /* Pop function and channel. */
1313 fn = top;
1314 if (--sp >= 0)
1315 top = stack[sp];
1316 chan = top;
1317 if (--sp >= 0)
1318 top = stack[sp];
1319 /* Pop arguments into a dedicated array. */
1320 for (i = 0; i < nargs; ++i)
1322 args[i] = top;
1323 if (--sp >= 0)
1324 top = stack[sp];
1327 /* A bad format string means something is very wrong; give
1328 up immediately. */
1329 if (format[slen - 1] != '\0')
1330 error (_("Unterminated format string in printf bytecode"));
1332 ax_printf (fn, chan, format, nargs, args);
1334 break;
1336 /* GDB never (currently) generates any of these ops. */
1337 case gdb_agent_op_float:
1338 case gdb_agent_op_ref_float:
1339 case gdb_agent_op_ref_double:
1340 case gdb_agent_op_ref_long_double:
1341 case gdb_agent_op_l_to_d:
1342 case gdb_agent_op_d_to_l:
1343 case gdb_agent_op_trace16:
1344 ax_debug ("Agent expression op 0x%x valid, but not handled",
1345 op);
1346 /* If ever GDB generates any of these, we don't have the
1347 option of ignoring. */
1348 return expr_eval_unhandled_opcode;
1350 default:
1351 ax_debug ("Agent expression op 0x%x not recognized", op);
1352 /* Don't struggle on, things will just get worse. */
1353 return expr_eval_unrecognized_opcode;
1356 /* Check for stack badness. */
1357 if (sp >= (STACK_MAX - 1))
1359 ax_debug ("Expression stack overflow");
1360 return expr_eval_stack_overflow;
1363 if (sp < 0)
1365 ax_debug ("Expression stack underflow");
1366 return expr_eval_stack_underflow;
1369 ax_debug ("Op %s -> sp=%d, top=0x%s",
1370 gdb_agent_op_name (op), sp, phex_nz (top, 0));