xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbserver/ax.cc (revision 528ce0b18ee40383f14928382d06afd754b01561)
1 /* Agent expression code for remote server.
2    Copyright (C) 2009-2020 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
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.
10 
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.
15 
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/>.  */
18 
19 #include "server.h"
20 #include "ax.h"
21 #include "gdbsupport/format.h"
22 #include "tracepoint.h"
23 #include "gdbsupport/rsp-low.h"
24 
25 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
26 
27 #ifdef IN_PROCESS_AGENT
28 bool debug_agent = 0;
29 #endif
30 
31 static void
32 ax_vdebug (const char *fmt, ...)
33 {
34   char buf[1024];
35   va_list ap;
36 
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);
45 }
46 
47 #define ax_debug_1(level, fmt, args...)	\
48   do {						\
49     if (level <= debug_threads)			\
50       ax_vdebug ((fmt), ##args);		\
51   } while (0)
52 
53 #define ax_debug(FMT, args...)		\
54   ax_debug_1 (1, FMT, ##args)
55 
56 /* This enum must exactly match what is documented in
57    gdb/doc/agentexpr.texi, including all the numerical values.  */
58 
59 enum gdb_agent_op
60   {
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
66   };
67 
68 static const char *gdb_agent_op_names [gdb_agent_op_last] =
69   {
70     "?undef?"
71 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , # NAME
72 #include "gdbsupport/ax.def"
73 #undef DEFOP
74   };
75 
76 #ifndef IN_PROCESS_AGENT
77 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
78   {
79     0
80 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , SIZE
81 #include "gdbsupport/ax.def"
82 #undef DEFOP
83   };
84 #endif
85 
86 /* A wrapper for gdb_agent_op_names that does some bounds-checking.  */
87 
88 static const char *
89 gdb_agent_op_name (int op)
90 {
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];
94 }
95 
96 #ifndef IN_PROCESS_AGENT
97 
98 /* The packet form of an agent expression consists of an 'X', number
99    of bytes in expression, a comma, and then the bytes.  */
100 
101 struct agent_expr *
102 gdb_parse_agent_expr (const char **actparm)
103 {
104   const char *act = *actparm;
105   ULONGEST xlen;
106   struct agent_expr *aexpr;
107 
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;
117 }
118 
119 void
120 gdb_free_agent_expr (struct agent_expr *aexpr)
121 {
122   if (aexpr != NULL)
123     {
124       free (aexpr->bytes);
125       free (aexpr);
126     }
127 }
128 
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.  */
132 
133 char *
134 gdb_unparse_agent_expr (struct agent_expr *aexpr)
135 {
136   char *rslt;
137 
138   rslt = (char *) xmalloc (2 * aexpr->length + 1);
139   bin2hex (aexpr->bytes, rslt, aexpr->length);
140   return rslt;
141 }
142 
143 /* Bytecode compilation.  */
144 
145 CORE_ADDR current_insn_ptr;
146 
147 int emit_error;
148 
149 struct bytecode_address
150 {
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;
158 
159 void
160 emit_prologue (void)
161 {
162   target_emit_ops ()->emit_prologue ();
163 }
164 
165 void
166 emit_epilogue (void)
167 {
168   target_emit_ops ()->emit_epilogue ();
169 }
170 
171 static void
172 emit_add (void)
173 {
174   target_emit_ops ()->emit_add ();
175 }
176 
177 static void
178 emit_sub (void)
179 {
180   target_emit_ops ()->emit_sub ();
181 }
182 
183 static void
184 emit_mul (void)
185 {
186   target_emit_ops ()->emit_mul ();
187 }
188 
189 static void
190 emit_lsh (void)
191 {
192   target_emit_ops ()->emit_lsh ();
193 }
194 
195 static void
196 emit_rsh_signed (void)
197 {
198   target_emit_ops ()->emit_rsh_signed ();
199 }
200 
201 static void
202 emit_rsh_unsigned (void)
203 {
204   target_emit_ops ()->emit_rsh_unsigned ();
205 }
206 
207 static void
208 emit_ext (int arg)
209 {
210   target_emit_ops ()->emit_ext (arg);
211 }
212 
213 static void
214 emit_log_not (void)
215 {
216   target_emit_ops ()->emit_log_not ();
217 }
218 
219 static void
220 emit_bit_and (void)
221 {
222   target_emit_ops ()->emit_bit_and ();
223 }
224 
225 static void
226 emit_bit_or (void)
227 {
228   target_emit_ops ()->emit_bit_or ();
229 }
230 
231 static void
232 emit_bit_xor (void)
233 {
234   target_emit_ops ()->emit_bit_xor ();
235 }
236 
237 static void
238 emit_bit_not (void)
239 {
240   target_emit_ops ()->emit_bit_not ();
241 }
242 
243 static void
244 emit_equal (void)
245 {
246   target_emit_ops ()->emit_equal ();
247 }
248 
249 static void
250 emit_less_signed (void)
251 {
252   target_emit_ops ()->emit_less_signed ();
253 }
254 
255 static void
256 emit_less_unsigned (void)
257 {
258   target_emit_ops ()->emit_less_unsigned ();
259 }
260 
261 static void
262 emit_ref (int size)
263 {
264   target_emit_ops ()->emit_ref (size);
265 }
266 
267 static void
268 emit_if_goto (int *offset_p, int *size_p)
269 {
270   target_emit_ops ()->emit_if_goto (offset_p, size_p);
271 }
272 
273 static void
274 emit_goto (int *offset_p, int *size_p)
275 {
276   target_emit_ops ()->emit_goto (offset_p, size_p);
277 }
278 
279 static void
280 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
281 {
282   target_emit_ops ()->write_goto_address (from, to, size);
283 }
284 
285 static void
286 emit_const (LONGEST num)
287 {
288   target_emit_ops ()->emit_const (num);
289 }
290 
291 static void
292 emit_reg (int reg)
293 {
294   target_emit_ops ()->emit_reg (reg);
295 }
296 
297 static void
298 emit_pop (void)
299 {
300   target_emit_ops ()->emit_pop ();
301 }
302 
303 static void
304 emit_stack_flush (void)
305 {
306   target_emit_ops ()->emit_stack_flush ();
307 }
308 
309 static void
310 emit_zero_ext (int arg)
311 {
312   target_emit_ops ()->emit_zero_ext (arg);
313 }
314 
315 static void
316 emit_swap (void)
317 {
318   target_emit_ops ()->emit_swap ();
319 }
320 
321 static void
322 emit_stack_adjust (int n)
323 {
324   target_emit_ops ()->emit_stack_adjust (n);
325 }
326 
327 /* FN's prototype is `LONGEST(*fn)(int)'.  */
328 
329 static void
330 emit_int_call_1 (CORE_ADDR fn, int arg1)
331 {
332   target_emit_ops ()->emit_int_call_1 (fn, arg1);
333 }
334 
335 /* FN's prototype is `void(*fn)(int,LONGEST)'.  */
336 
337 static void
338 emit_void_call_2 (CORE_ADDR fn, int arg1)
339 {
340   target_emit_ops ()->emit_void_call_2 (fn, arg1);
341 }
342 
343 static void
344 emit_eq_goto (int *offset_p, int *size_p)
345 {
346   target_emit_ops ()->emit_eq_goto (offset_p, size_p);
347 }
348 
349 static void
350 emit_ne_goto (int *offset_p, int *size_p)
351 {
352   target_emit_ops ()->emit_ne_goto (offset_p, size_p);
353 }
354 
355 static void
356 emit_lt_goto (int *offset_p, int *size_p)
357 {
358   target_emit_ops ()->emit_lt_goto (offset_p, size_p);
359 }
360 
361 static void
362 emit_ge_goto (int *offset_p, int *size_p)
363 {
364   target_emit_ops ()->emit_ge_goto (offset_p, size_p);
365 }
366 
367 static void
368 emit_gt_goto (int *offset_p, int *size_p)
369 {
370   target_emit_ops ()->emit_gt_goto (offset_p, size_p);
371 }
372 
373 static void
374 emit_le_goto (int *offset_p, int *size_p)
375 {
376   target_emit_ops ()->emit_le_goto (offset_p, size_p);
377 }
378 
379 /* Scan an agent expression for any evidence that the given PC is the
380    target of a jump bytecode in the expression.  */
381 
382 static int
383 is_goto_target (struct agent_expr *aexpr, int pc)
384 {
385   int i;
386   unsigned char op;
387 
388   for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
389     {
390       op = aexpr->bytes[i];
391 
392       if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
393 	{
394 	  int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
395 	  if (target == pc)
396 	    return 1;
397 	}
398     }
399 
400   return 0;
401 }
402 
403 /* Given an agent expression, turn it into native code.  */
404 
405 enum eval_result_type
406 compile_bytecodes (struct agent_expr *aexpr)
407 {
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;
415 
416 #define UNHANDLED					\
417   do							\
418     {							\
419       ax_debug ("Cannot compile op 0x%x\n", op);	\
420       return expr_eval_unhandled_opcode;		\
421     } while (0)
422 
423   if (aexpr->length == 0)
424     {
425       ax_debug ("empty agent expression\n");
426       return expr_eval_empty_expression;
427     }
428 
429   bytecode_address_table = NULL;
430 
431   while (!done)
432     {
433       op = aexpr->bytes[pc];
434 
435       ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
436 
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;
446 
447       ++pc;
448 
449       emit_error = 0;
450 
451       switch (op)
452 	{
453 	case gdb_agent_op_add:
454 	  emit_add ();
455 	  break;
456 
457 	case gdb_agent_op_sub:
458 	  emit_sub ();
459 	  break;
460 
461 	case gdb_agent_op_mul:
462 	  emit_mul ();
463 	  break;
464 
465 	case gdb_agent_op_div_signed:
466 	  UNHANDLED;
467 	  break;
468 
469 	case gdb_agent_op_div_unsigned:
470 	  UNHANDLED;
471 	  break;
472 
473 	case gdb_agent_op_rem_signed:
474 	  UNHANDLED;
475 	  break;
476 
477 	case gdb_agent_op_rem_unsigned:
478 	  UNHANDLED;
479 	  break;
480 
481 	case gdb_agent_op_lsh:
482 	  emit_lsh ();
483 	  break;
484 
485 	case gdb_agent_op_rsh_signed:
486 	  emit_rsh_signed ();
487 	  break;
488 
489 	case gdb_agent_op_rsh_unsigned:
490 	  emit_rsh_unsigned ();
491 	  break;
492 
493 	case gdb_agent_op_trace:
494 	  UNHANDLED;
495 	  break;
496 
497 	case gdb_agent_op_trace_quick:
498 	  UNHANDLED;
499 	  break;
500 
501 	case gdb_agent_op_log_not:
502 	  emit_log_not ();
503 	  break;
504 
505 	case gdb_agent_op_bit_and:
506 	  emit_bit_and ();
507 	  break;
508 
509 	case gdb_agent_op_bit_or:
510 	  emit_bit_or ();
511 	  break;
512 
513 	case gdb_agent_op_bit_xor:
514 	  emit_bit_xor ();
515 	  break;
516 
517 	case gdb_agent_op_bit_not:
518 	  emit_bit_not ();
519 	  break;
520 
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)
526 	    {
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));
534 	    }
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)
539 	    {
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));
547 	    }
548 	  else
549 	    emit_equal ();
550 	  break;
551 
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))
556 	    {
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));
564 	    }
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))
569 	    {
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));
577 	    }
578 	  else
579 	    emit_less_signed ();
580 	  break;
581 
582 	case gdb_agent_op_less_unsigned:
583 	  emit_less_unsigned ();
584 	  break;
585 
586 	case gdb_agent_op_ext:
587 	  arg = aexpr->bytes[pc++];
588 	  if (arg < (sizeof (LONGEST) * 8))
589 	    emit_ext (arg);
590 	  break;
591 
592 	case gdb_agent_op_ref8:
593 	  emit_ref (1);
594 	  break;
595 
596 	case gdb_agent_op_ref16:
597 	  emit_ref (2);
598 	  break;
599 
600 	case gdb_agent_op_ref32:
601 	  emit_ref (4);
602 	  break;
603 
604 	case gdb_agent_op_ref64:
605 	  emit_ref (8);
606 	  break;
607 
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;
614 
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;
621 
622 	case gdb_agent_op_const8:
623 	  emit_stack_flush ();
624 	  top = aexpr->bytes[pc++];
625 	  emit_const (top);
626 	  break;
627 
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;
634 
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;
643 
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;
656 
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;
663 
664 	case gdb_agent_op_end:
665 	  ax_debug ("At end of expression\n");
666 
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);
670 
671 	  done = 1;
672 	  break;
673 
674 	case gdb_agent_op_dup:
675 	  /* In our design, dup is equivalent to stack flushing.  */
676 	  emit_stack_flush ();
677 	  break;
678 
679 	case gdb_agent_op_pop:
680 	  emit_pop ();
681 	  break;
682 
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;
688 
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))
696 	    {
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));
704 	    }
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))
711 	    {
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));
719 	    }
720 	  else
721 	    emit_swap ();
722 	  break;
723 
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;
731 
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;
738 
739 	case gdb_agent_op_tracev:
740 	  UNHANDLED;
741 	  break;
742 
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;
753 
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;
758 	}
759 
760       /* This catches errors that occur in target-specific code
761 	 emission.  */
762       if (emit_error)
763 	{
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;
767 	}
768 
769       ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
770     }
771 
772   /* Now fill in real addresses as goto destinations.  */
773   for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
774     {
775       int written = 0;
776 
777       if (aentry->goto_pc < 0)
778 	continue;
779 
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)
784 	{
785 	  if (aentry2->pc == aentry->goto_pc)
786 	    {
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;
794 	    }
795 	}
796 
797       /* Error out if we didn't find a destination.  */
798       if (!written)
799 	{
800 	  ax_debug ("Destination of goto %d not found\n",
801 		    aentry->goto_pc);
802 	  return expr_eval_invalid_goto;
803 	}
804     }
805 
806   return expr_eval_no_error;
807 }
808 
809 #endif
810 
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.  */
817 
818 static void
819 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
820 	   int nargs, ULONGEST *args)
821 {
822   const char *f = format;
823   int i;
824   const char *current_substring;
825   int nargs_wanted;
826 
827   ax_debug ("Printf of \"%s\" with %d args", format, nargs);
828 
829   format_pieces fpieces (&f);
830 
831   nargs_wanted = 0;
832   for (auto &&piece : fpieces)
833     if (piece.argclass != literal_piece)
834       ++nargs_wanted;
835 
836   if (nargs != nargs_wanted)
837     error (_("Wrong number of arguments for specified format-string"));
838 
839   i = 0;
840   for (auto &&piece : fpieces)
841     {
842       current_substring = piece.string;
843       ax_debug ("current substring is '%s', class is %d",
844 		current_substring, piece.argclass);
845       switch (piece.argclass)
846 	{
847 	case string_arg:
848 	  {
849 	    gdb_byte *str;
850 	    CORE_ADDR tem;
851 	    int j;
852 
853 	    tem = args[i];
854 	    if (tem == 0)
855 	      {
856 		printf (current_substring, "(null)");
857 		break;
858 	      }
859 
860 	    /* This is a %s argument.  Find the length of the string.  */
861 	    for (j = 0;; j++)
862 	      {
863 		gdb_byte c;
864 
865 		read_inferior_memory (tem + j, &c, 1);
866 		if (c == 0)
867 		  break;
868 	      }
869 
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;
875 
876               printf (current_substring, (char *) str);
877 	    }
878 	    break;
879 
880 	  case long_long_arg:
881 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
882 	    {
883 	      long long val = args[i];
884 
885               printf (current_substring, val);
886 	      break;
887 	    }
888 #else
889 	    error (_("long long not supported in agent printf"));
890 #endif
891 	case int_arg:
892 	  {
893 	    int val = args[i];
894 
895 	    printf (current_substring, val);
896 	    break;
897 	  }
898 
899 	case long_arg:
900 	  {
901 	    long val = args[i];
902 
903 	    printf (current_substring, val);
904 	    break;
905 	  }
906 
907 	case size_t_arg:
908 	  {
909 	    size_t val = args[i];
910 
911 	    printf (current_substring, val);
912 	    break;
913 	  }
914 
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;
926 
927 	default:
928 	  error (_("Format directive in '%s' not supported in agent printf"),
929 		 current_substring);
930 	}
931 
932       /* Maybe advance to the next argument.  */
933       if (piece.argclass != literal_piece)
934 	++i;
935     }
936 
937   fflush (stdout);
938 }
939 
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.  */
943 
944 enum eval_result_type
945 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
946 		     struct agent_expr *aexpr,
947 		     ULONGEST *rslt)
948 {
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;
955 
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
961   {
962     union
963     {
964       unsigned char bytes[1];
965       unsigned char val;
966     } u8;
967     union
968     {
969       unsigned char bytes[2];
970       unsigned short val;
971     } u16;
972     union
973     {
974       unsigned char bytes[4];
975       unsigned int val;
976     } u32;
977     union
978     {
979       unsigned char bytes[8];
980       ULONGEST val;
981     } u64;
982   } cnv;
983 
984   if (aexpr->length == 0)
985     {
986       ax_debug ("empty agent expression");
987       return expr_eval_empty_expression;
988     }
989 
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;
994 
995   while (1)
996     {
997       op = aexpr->bytes[pc++];
998 
999       ax_debug ("About to interpret byte 0x%x", op);
1000 
1001       switch (op)
1002 	{
1003 	case gdb_agent_op_add:
1004 	  top += stack[--sp];
1005 	  break;
1006 
1007 	case gdb_agent_op_sub:
1008 	  top = stack[--sp] - top;
1009 	  break;
1010 
1011 	case gdb_agent_op_mul:
1012 	  top *= stack[--sp];
1013 	  break;
1014 
1015 	case gdb_agent_op_div_signed:
1016 	  if (top == 0)
1017 	    {
1018 	      ax_debug ("Attempted to divide by zero");
1019 	      return expr_eval_divide_by_zero;
1020 	    }
1021 	  top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
1022 	  break;
1023 
1024 	case gdb_agent_op_div_unsigned:
1025 	  if (top == 0)
1026 	    {
1027 	      ax_debug ("Attempted to divide by zero");
1028 	      return expr_eval_divide_by_zero;
1029 	    }
1030 	  top = stack[--sp] / top;
1031 	  break;
1032 
1033 	case gdb_agent_op_rem_signed:
1034 	  if (top == 0)
1035 	    {
1036 	      ax_debug ("Attempted to divide by zero");
1037 	      return expr_eval_divide_by_zero;
1038 	    }
1039 	  top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1040 	  break;
1041 
1042 	case gdb_agent_op_rem_unsigned:
1043 	  if (top == 0)
1044 	    {
1045 	      ax_debug ("Attempted to divide by zero");
1046 	      return expr_eval_divide_by_zero;
1047 	    }
1048 	  top = stack[--sp] % top;
1049 	  break;
1050 
1051 	case gdb_agent_op_lsh:
1052 	  top = stack[--sp] << top;
1053 	  break;
1054 
1055 	case gdb_agent_op_rsh_signed:
1056 	  top = ((LONGEST) stack[--sp]) >> top;
1057 	  break;
1058 
1059 	case gdb_agent_op_rsh_unsigned:
1060 	  top = stack[--sp] >> top;
1061 	  break;
1062 
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;
1069 
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;
1074 
1075 	case gdb_agent_op_log_not:
1076 	  top = !top;
1077 	  break;
1078 
1079 	case gdb_agent_op_bit_and:
1080 	  top &= stack[--sp];
1081 	  break;
1082 
1083 	case gdb_agent_op_bit_or:
1084 	  top |= stack[--sp];
1085 	  break;
1086 
1087 	case gdb_agent_op_bit_xor:
1088 	  top ^= stack[--sp];
1089 	  break;
1090 
1091 	case gdb_agent_op_bit_not:
1092 	  top = ~top;
1093 	  break;
1094 
1095 	case gdb_agent_op_equal:
1096 	  top = (stack[--sp] == top);
1097 	  break;
1098 
1099 	case gdb_agent_op_less_signed:
1100 	  top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1101 	  break;
1102 
1103 	case gdb_agent_op_less_unsigned:
1104 	  top = (stack[--sp] < top);
1105 	  break;
1106 
1107 	case gdb_agent_op_ext:
1108 	  arg = aexpr->bytes[pc++];
1109 	  if (arg < (sizeof (LONGEST) * 8))
1110 	    {
1111 	      LONGEST mask = 1 << (arg - 1);
1112 	      top &= ((LONGEST) 1 << arg) - 1;
1113 	      top = (top ^ mask) - mask;
1114 	    }
1115 	  break;
1116 
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;
1121 
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;
1126 
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;
1131 
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;
1136 
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;
1145 
1146 	case gdb_agent_op_goto:
1147 	  pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1148 	  break;
1149 
1150 	case gdb_agent_op_const8:
1151 	  /* Flush the cached stack top.  */
1152 	  stack[sp++] = top;
1153 	  top = aexpr->bytes[pc++];
1154 	  break;
1155 
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;
1162 
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;
1171 
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;
1184 
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++];
1190 	  {
1191 	    int regnum = arg;
1192 	    struct regcache *regcache = ctx->regcache;
1193 
1194 	    switch (register_size (regcache->tdesc, regnum))
1195 	      {
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");
1215 	      }
1216 	  }
1217 	  break;
1218 
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)
1223 	    {
1224 	      if (sp <= 0)
1225 		{
1226 		  /* This should be an error */
1227 		  ax_debug ("Stack is empty, nothing to return");
1228 		  return expr_eval_empty_stack;
1229 		}
1230 	      *rslt = top;
1231 	    }
1232 	  return expr_eval_no_error;
1233 
1234 	case gdb_agent_op_dup:
1235 	  stack[sp++] = top;
1236 	  break;
1237 
1238 	case gdb_agent_op_pop:
1239 	  if (--sp >= 0)
1240 	    top = stack[sp];
1241 	  break;
1242 
1243 	case gdb_agent_op_pick:
1244 	  arg = aexpr->bytes[pc++];
1245 	  stack[sp] = top;
1246 	  top = stack[sp - arg];
1247 	  ++sp;
1248 	  break;
1249 
1250 	case gdb_agent_op_rot:
1251 	  {
1252 	    ULONGEST tem = stack[sp - 1];
1253 
1254 	    stack[sp - 1] = stack[sp - 2];
1255 	    stack[sp - 2] = top;
1256 	    top = tem;
1257 	  }
1258 	  break;
1259 
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;
1265 
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;
1273 
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;
1281 
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;
1289 
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;
1295 
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;
1302 
1303 	case gdb_agent_op_printf:
1304 	  {
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;
1310 
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)
1325 	      {
1326 		args[i] = top;
1327 		if (--sp >= 0)
1328 		  top = stack[sp];
1329 	      }
1330 
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"));
1335 
1336 	    ax_printf (fn, chan, format, nargs, args);
1337 	  }
1338 	  break;
1339 
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;
1353 
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;
1358 	}
1359 
1360       /* Check for stack badness.  */
1361       if (sp >= (STACK_MAX - 1))
1362 	{
1363 	  ax_debug ("Expression stack overflow");
1364 	  return expr_eval_stack_overflow;
1365 	}
1366 
1367       if (sp < 0)
1368 	{
1369 	  ax_debug ("Expression stack underflow");
1370 	  return expr_eval_stack_underflow;
1371 	}
1372 
1373       ax_debug ("Op %s -> sp=%d, top=0x%s",
1374 		gdb_agent_op_name (op), sp, phex_nz (top, 0));
1375     }
1376 }
1377