OSDN Git Service

modified: utilsrc/src/Admin/Makefile
[eos/others.git] / utilsrc / srcX86MAC64 / Admin / gdb-7.7.1 / gdb / gdbserver / ax.c
1 /* Agent expression code for remote server.
2    Copyright (C) 2009-2014 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 "format.h"
22 #include "tracepoint.h"
23
24 static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
25
26 #ifdef IN_PROCESS_AGENT
27 int debug_agent = 0;
28 #endif
29
30 static void
31 ax_vdebug (const char *fmt, ...)
32 {
33   char buf[1024];
34   va_list ap;
35
36   va_start (ap, fmt);
37   vsprintf (buf, fmt, ap);
38   fprintf (stderr, PROG "/ax: %s\n", buf);
39   va_end (ap);
40 }
41
42 #define ax_debug_1(level, fmt, args...) \
43   do {                                          \
44     if (level <= debug_threads)                 \
45       ax_vdebug ((fmt), ##args);                \
46   } while (0)
47
48 #define ax_debug(FMT, args...)          \
49   ax_debug_1 (1, FMT, ##args)
50
51 /* This enum must exactly match what is documented in
52    gdb/doc/agentexpr.texi, including all the numerical values.  */
53
54 enum gdb_agent_op
55   {
56 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  \
57     gdb_agent_op_ ## NAME = VALUE,
58 #include "ax.def"
59 #undef DEFOP
60     gdb_agent_op_last
61   };
62
63 static const char *gdb_agent_op_names [gdb_agent_op_last] =
64   {
65     "?undef?"
66 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , # NAME
67 #include "ax.def"
68 #undef DEFOP
69   };
70
71 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
72   {
73     0
74 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , SIZE
75 #include "ax.def"
76 #undef DEFOP
77   };
78
79 /* A wrapper for gdb_agent_op_names that does some bounds-checking.  */
80
81 static const char *
82 gdb_agent_op_name (int op)
83 {
84   if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
85     return "?undef?";
86   return gdb_agent_op_names[op];
87 }
88
89 #ifndef IN_PROCESS_AGENT
90
91 /* The packet form of an agent expression consists of an 'X', number
92    of bytes in expression, a comma, and then the bytes.  */
93
94 struct agent_expr *
95 gdb_parse_agent_expr (char **actparm)
96 {
97   char *act = *actparm;
98   ULONGEST xlen;
99   struct agent_expr *aexpr;
100
101   ++act;  /* skip the X */
102   act = unpack_varlen_hex (act, &xlen);
103   ++act;  /* skip a comma */
104   aexpr = xmalloc (sizeof (struct agent_expr));
105   aexpr->length = xlen;
106   aexpr->bytes = xmalloc (xlen);
107   convert_ascii_to_int (act, aexpr->bytes, xlen);
108   *actparm = act + (xlen * 2);
109   return aexpr;
110 }
111
112 /* Convert the bytes of an agent expression back into hex digits, so
113    they can be printed or uploaded.  This allocates the buffer,
114    callers should free when they are done with it.  */
115
116 char *
117 gdb_unparse_agent_expr (struct agent_expr *aexpr)
118 {
119   char *rslt;
120
121   rslt = xmalloc (2 * aexpr->length + 1);
122   convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
123   return rslt;
124 }
125
126 /* Bytecode compilation.  */
127
128 CORE_ADDR current_insn_ptr;
129
130 int emit_error;
131
132 struct bytecode_address
133 {
134   int pc;
135   CORE_ADDR address;
136   int goto_pc;
137   /* Offset and size of field to be modified in the goto block.  */
138   int from_offset, from_size;
139   struct bytecode_address *next;
140 } *bytecode_address_table;
141
142 void
143 emit_prologue (void)
144 {
145   target_emit_ops ()->emit_prologue ();
146 }
147
148 void
149 emit_epilogue (void)
150 {
151   target_emit_ops ()->emit_epilogue ();
152 }
153
154 static void
155 emit_add (void)
156 {
157   target_emit_ops ()->emit_add ();
158 }
159
160 static void
161 emit_sub (void)
162 {
163   target_emit_ops ()->emit_sub ();
164 }
165
166 static void
167 emit_mul (void)
168 {
169   target_emit_ops ()->emit_mul ();
170 }
171
172 static void
173 emit_lsh (void)
174 {
175   target_emit_ops ()->emit_lsh ();
176 }
177
178 static void
179 emit_rsh_signed (void)
180 {
181   target_emit_ops ()->emit_rsh_signed ();
182 }
183
184 static void
185 emit_rsh_unsigned (void)
186 {
187   target_emit_ops ()->emit_rsh_unsigned ();
188 }
189
190 static void
191 emit_ext (int arg)
192 {
193   target_emit_ops ()->emit_ext (arg);
194 }
195
196 static void
197 emit_log_not (void)
198 {
199   target_emit_ops ()->emit_log_not ();
200 }
201
202 static void
203 emit_bit_and (void)
204 {
205   target_emit_ops ()->emit_bit_and ();
206 }
207
208 static void
209 emit_bit_or (void)
210 {
211   target_emit_ops ()->emit_bit_or ();
212 }
213
214 static void
215 emit_bit_xor (void)
216 {
217   target_emit_ops ()->emit_bit_xor ();
218 }
219
220 static void
221 emit_bit_not (void)
222 {
223   target_emit_ops ()->emit_bit_not ();
224 }
225
226 static void
227 emit_equal (void)
228 {
229   target_emit_ops ()->emit_equal ();
230 }
231
232 static void
233 emit_less_signed (void)
234 {
235   target_emit_ops ()->emit_less_signed ();
236 }
237
238 static void
239 emit_less_unsigned (void)
240 {
241   target_emit_ops ()->emit_less_unsigned ();
242 }
243
244 static void
245 emit_ref (int size)
246 {
247   target_emit_ops ()->emit_ref (size);
248 }
249
250 static void
251 emit_if_goto (int *offset_p, int *size_p)
252 {
253   target_emit_ops ()->emit_if_goto (offset_p, size_p);
254 }
255
256 static void
257 emit_goto (int *offset_p, int *size_p)
258 {
259   target_emit_ops ()->emit_goto (offset_p, size_p);
260 }
261
262 static void
263 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
264 {
265   target_emit_ops ()->write_goto_address (from, to, size);
266 }
267
268 static void
269 emit_const (LONGEST num)
270 {
271   target_emit_ops ()->emit_const (num);
272 }
273
274 static void
275 emit_reg (int reg)
276 {
277   target_emit_ops ()->emit_reg (reg);
278 }
279
280 static void
281 emit_pop (void)
282 {
283   target_emit_ops ()->emit_pop ();
284 }
285
286 static void
287 emit_stack_flush (void)
288 {
289   target_emit_ops ()->emit_stack_flush ();
290 }
291
292 static void
293 emit_zero_ext (int arg)
294 {
295   target_emit_ops ()->emit_zero_ext (arg);
296 }
297
298 static void
299 emit_swap (void)
300 {
301   target_emit_ops ()->emit_swap ();
302 }
303
304 static void
305 emit_stack_adjust (int n)
306 {
307   target_emit_ops ()->emit_stack_adjust (n);
308 }
309
310 /* FN's prototype is `LONGEST(*fn)(int)'.  */
311
312 static void
313 emit_int_call_1 (CORE_ADDR fn, int arg1)
314 {
315   target_emit_ops ()->emit_int_call_1 (fn, arg1);
316 }
317
318 /* FN's prototype is `void(*fn)(int,LONGEST)'.  */
319
320 static void
321 emit_void_call_2 (CORE_ADDR fn, int arg1)
322 {
323   target_emit_ops ()->emit_void_call_2 (fn, arg1);
324 }
325
326 static void
327 emit_eq_goto (int *offset_p, int *size_p)
328 {
329   target_emit_ops ()->emit_eq_goto (offset_p, size_p);
330 }
331
332 static void
333 emit_ne_goto (int *offset_p, int *size_p)
334 {
335   target_emit_ops ()->emit_ne_goto (offset_p, size_p);
336 }
337
338 static void
339 emit_lt_goto (int *offset_p, int *size_p)
340 {
341   target_emit_ops ()->emit_lt_goto (offset_p, size_p);
342 }
343
344 static void
345 emit_ge_goto (int *offset_p, int *size_p)
346 {
347   target_emit_ops ()->emit_ge_goto (offset_p, size_p);
348 }
349
350 static void
351 emit_gt_goto (int *offset_p, int *size_p)
352 {
353   target_emit_ops ()->emit_gt_goto (offset_p, size_p);
354 }
355
356 static void
357 emit_le_goto (int *offset_p, int *size_p)
358 {
359   target_emit_ops ()->emit_le_goto (offset_p, size_p);
360 }
361
362 /* Scan an agent expression for any evidence that the given PC is the
363    target of a jump bytecode in the expression.  */
364
365 int
366 is_goto_target (struct agent_expr *aexpr, int pc)
367 {
368   int i;
369   unsigned char op;
370
371   for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
372     {
373       op = aexpr->bytes[i];
374
375       if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
376         {
377           int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
378           if (target == pc)
379             return 1;
380         }
381     }
382
383   return 0;
384 }
385
386 /* Given an agent expression, turn it into native code.  */
387
388 enum eval_result_type
389 compile_bytecodes (struct agent_expr *aexpr)
390 {
391   int pc = 0;
392   int done = 0;
393   unsigned char op, next_op;
394   int arg;
395   /* This is only used to build 64-bit value for constants.  */
396   ULONGEST top;
397   struct bytecode_address *aentry, *aentry2;
398
399 #define UNHANDLED                                       \
400   do                                                    \
401     {                                                   \
402       ax_debug ("Cannot compile op 0x%x\n", op);        \
403       return expr_eval_unhandled_opcode;                \
404     } while (0)
405
406   if (aexpr->length == 0)
407     {
408       ax_debug ("empty agent expression\n");
409       return expr_eval_empty_expression;
410     }
411
412   bytecode_address_table = NULL;
413
414   while (!done)
415     {
416       op = aexpr->bytes[pc];
417
418       ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
419
420       /* Record the compiled-code address of the bytecode, for use by
421          jump instructions.  */
422       aentry = xmalloc (sizeof (struct bytecode_address));
423       aentry->pc = pc;
424       aentry->address = current_insn_ptr;
425       aentry->goto_pc = -1;
426       aentry->from_offset = aentry->from_size = 0;
427       aentry->next = bytecode_address_table;
428       bytecode_address_table = aentry;
429
430       ++pc;
431
432       emit_error = 0;
433
434       switch (op)
435         {
436         case gdb_agent_op_add:
437           emit_add ();
438           break;
439
440         case gdb_agent_op_sub:
441           emit_sub ();
442           break;
443
444         case gdb_agent_op_mul:
445           emit_mul ();
446           break;
447
448         case gdb_agent_op_div_signed:
449           UNHANDLED;
450           break;
451
452         case gdb_agent_op_div_unsigned:
453           UNHANDLED;
454           break;
455
456         case gdb_agent_op_rem_signed:
457           UNHANDLED;
458           break;
459
460         case gdb_agent_op_rem_unsigned:
461           UNHANDLED;
462           break;
463
464         case gdb_agent_op_lsh:
465           emit_lsh ();
466           break;
467
468         case gdb_agent_op_rsh_signed:
469           emit_rsh_signed ();
470           break;
471
472         case gdb_agent_op_rsh_unsigned:
473           emit_rsh_unsigned ();
474           break;
475
476         case gdb_agent_op_trace:
477           UNHANDLED;
478           break;
479
480         case gdb_agent_op_trace_quick:
481           UNHANDLED;
482           break;
483
484         case gdb_agent_op_log_not:
485           emit_log_not ();
486           break;
487
488         case gdb_agent_op_bit_and:
489           emit_bit_and ();
490           break;
491
492         case gdb_agent_op_bit_or:
493           emit_bit_or ();
494           break;
495
496         case gdb_agent_op_bit_xor:
497           emit_bit_xor ();
498           break;
499
500         case gdb_agent_op_bit_not:
501           emit_bit_not ();
502           break;
503
504         case gdb_agent_op_equal:
505           next_op = aexpr->bytes[pc];
506           if (next_op == gdb_agent_op_if_goto
507               && !is_goto_target (aexpr, pc)
508               && target_emit_ops ()->emit_eq_goto)
509             {
510               ax_debug ("Combining equal & if_goto");
511               pc += 1;
512               aentry->pc = pc;
513               arg = aexpr->bytes[pc++];
514               arg = (arg << 8) + aexpr->bytes[pc++];
515               aentry->goto_pc = arg;
516               emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
517             }
518           else if (next_op == gdb_agent_op_log_not
519                    && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
520                    && !is_goto_target (aexpr, pc + 1)
521                    && target_emit_ops ()->emit_ne_goto)
522             {
523               ax_debug ("Combining equal & log_not & if_goto");
524               pc += 2;
525               aentry->pc = pc;
526               arg = aexpr->bytes[pc++];
527               arg = (arg << 8) + aexpr->bytes[pc++];
528               aentry->goto_pc = arg;
529               emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
530             }
531           else
532             emit_equal ();
533           break;
534
535         case gdb_agent_op_less_signed:
536           next_op = aexpr->bytes[pc];
537           if (next_op == gdb_agent_op_if_goto
538               && !is_goto_target (aexpr, pc))
539             {
540               ax_debug ("Combining less_signed & if_goto");
541               pc += 1;
542               aentry->pc = pc;
543               arg = aexpr->bytes[pc++];
544               arg = (arg << 8) + aexpr->bytes[pc++];
545               aentry->goto_pc = arg;
546               emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
547             }
548           else if (next_op == gdb_agent_op_log_not
549                    && !is_goto_target (aexpr, pc)
550                    && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
551                    && !is_goto_target (aexpr, pc + 1))
552             {
553               ax_debug ("Combining less_signed & log_not & if_goto");
554               pc += 2;
555               aentry->pc = pc;
556               arg = aexpr->bytes[pc++];
557               arg = (arg << 8) + aexpr->bytes[pc++];
558               aentry->goto_pc = arg;
559               emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
560             }
561           else
562             emit_less_signed ();
563           break;
564
565         case gdb_agent_op_less_unsigned:
566           emit_less_unsigned ();
567           break;
568
569         case gdb_agent_op_ext:
570           arg = aexpr->bytes[pc++];
571           if (arg < (sizeof (LONGEST) * 8))
572             emit_ext (arg);
573           break;
574
575         case gdb_agent_op_ref8:
576           emit_ref (1);
577           break;
578
579         case gdb_agent_op_ref16:
580           emit_ref (2);
581           break;
582
583         case gdb_agent_op_ref32:
584           emit_ref (4);
585           break;
586
587         case gdb_agent_op_ref64:
588           emit_ref (8);
589           break;
590
591         case gdb_agent_op_if_goto:
592           arg = aexpr->bytes[pc++];
593           arg = (arg << 8) + aexpr->bytes[pc++];
594           aentry->goto_pc = arg;
595           emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
596           break;
597
598         case gdb_agent_op_goto:
599           arg = aexpr->bytes[pc++];
600           arg = (arg << 8) + aexpr->bytes[pc++];
601           aentry->goto_pc = arg;
602           emit_goto (&(aentry->from_offset), &(aentry->from_size));
603           break;
604
605         case gdb_agent_op_const8:
606           emit_stack_flush ();
607           top = aexpr->bytes[pc++];
608           emit_const (top);
609           break;
610
611         case gdb_agent_op_const16:
612           emit_stack_flush ();
613           top = aexpr->bytes[pc++];
614           top = (top << 8) + aexpr->bytes[pc++];
615           emit_const (top);
616           break;
617
618         case gdb_agent_op_const32:
619           emit_stack_flush ();
620           top = aexpr->bytes[pc++];
621           top = (top << 8) + aexpr->bytes[pc++];
622           top = (top << 8) + aexpr->bytes[pc++];
623           top = (top << 8) + aexpr->bytes[pc++];
624           emit_const (top);
625           break;
626
627         case gdb_agent_op_const64:
628           emit_stack_flush ();
629           top = aexpr->bytes[pc++];
630           top = (top << 8) + aexpr->bytes[pc++];
631           top = (top << 8) + aexpr->bytes[pc++];
632           top = (top << 8) + aexpr->bytes[pc++];
633           top = (top << 8) + aexpr->bytes[pc++];
634           top = (top << 8) + aexpr->bytes[pc++];
635           top = (top << 8) + aexpr->bytes[pc++];
636           top = (top << 8) + aexpr->bytes[pc++];
637           emit_const (top);
638           break;
639
640         case gdb_agent_op_reg:
641           emit_stack_flush ();
642           arg = aexpr->bytes[pc++];
643           arg = (arg << 8) + aexpr->bytes[pc++];
644           emit_reg (arg);
645           break;
646
647         case gdb_agent_op_end:
648           ax_debug ("At end of expression\n");
649
650           /* Assume there is one stack element left, and that it is
651              cached in "top" where emit_epilogue can get to it.  */
652           emit_stack_adjust (1);
653
654           done = 1;
655           break;
656
657         case gdb_agent_op_dup:
658           /* In our design, dup is equivalent to stack flushing.  */
659           emit_stack_flush ();
660           break;
661
662         case gdb_agent_op_pop:
663           emit_pop ();
664           break;
665
666         case gdb_agent_op_zero_ext:
667           arg = aexpr->bytes[pc++];
668           if (arg < (sizeof (LONGEST) * 8))
669             emit_zero_ext (arg);
670           break;
671
672         case gdb_agent_op_swap:
673           next_op = aexpr->bytes[pc];
674           /* Detect greater-than comparison sequences.  */
675           if (next_op == gdb_agent_op_less_signed
676               && !is_goto_target (aexpr, pc)
677               && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
678               && !is_goto_target (aexpr, pc + 1))
679             {
680               ax_debug ("Combining swap & less_signed & if_goto");
681               pc += 2;
682               aentry->pc = pc;
683               arg = aexpr->bytes[pc++];
684               arg = (arg << 8) + aexpr->bytes[pc++];
685               aentry->goto_pc = arg;
686               emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
687             }
688           else if (next_op == gdb_agent_op_less_signed
689                    && !is_goto_target (aexpr, pc)
690                    && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
691                    && !is_goto_target (aexpr, pc + 1)
692                    && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
693                    && !is_goto_target (aexpr, pc + 2))
694             {
695               ax_debug ("Combining swap & less_signed & log_not & if_goto");
696               pc += 3;
697               aentry->pc = pc;
698               arg = aexpr->bytes[pc++];
699               arg = (arg << 8) + aexpr->bytes[pc++];
700               aentry->goto_pc = arg;
701               emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
702             }
703           else
704             emit_swap ();
705           break;
706
707         case gdb_agent_op_getv:
708           emit_stack_flush ();
709           arg = aexpr->bytes[pc++];
710           arg = (arg << 8) + aexpr->bytes[pc++];
711           emit_int_call_1 (get_get_tsv_func_addr (),
712                            arg);
713           break;
714
715         case gdb_agent_op_setv:
716           arg = aexpr->bytes[pc++];
717           arg = (arg << 8) + aexpr->bytes[pc++];
718           emit_void_call_2 (get_set_tsv_func_addr (),
719                             arg);
720           break;
721
722         case gdb_agent_op_tracev:
723           UNHANDLED;
724           break;
725
726           /* GDB never (currently) generates any of these ops.  */
727         case gdb_agent_op_float:
728         case gdb_agent_op_ref_float:
729         case gdb_agent_op_ref_double:
730         case gdb_agent_op_ref_long_double:
731         case gdb_agent_op_l_to_d:
732         case gdb_agent_op_d_to_l:
733         case gdb_agent_op_trace16:
734           UNHANDLED;
735           break;
736
737         default:
738           ax_debug ("Agent expression op 0x%x not recognized\n", op);
739           /* Don't struggle on, things will just get worse.  */
740           return expr_eval_unrecognized_opcode;
741         }
742
743       /* This catches errors that occur in target-specific code
744          emission.  */
745       if (emit_error)
746         {
747           ax_debug ("Error %d while emitting code for %s\n",
748                     emit_error, gdb_agent_op_name (op));
749           return expr_eval_unhandled_opcode;
750         }
751
752       ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
753     }
754
755   /* Now fill in real addresses as goto destinations.  */
756   for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
757     {
758       int written = 0;
759
760       if (aentry->goto_pc < 0)
761         continue;
762
763       /* Find the location that we are going to, and call back into
764          target-specific code to write the actual address or
765          displacement.  */
766       for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
767         {
768           if (aentry2->pc == aentry->goto_pc)
769             {
770               ax_debug ("Want to jump from %s to %s\n",
771                         paddress (aentry->address),
772                         paddress (aentry2->address));
773               write_goto_address (aentry->address + aentry->from_offset,
774                                   aentry2->address, aentry->from_size);
775               written = 1;
776               break;
777             }
778         }
779
780       /* Error out if we didn't find a destination.  */
781       if (!written)
782         {
783           ax_debug ("Destination of goto %d not found\n",
784                     aentry->goto_pc);
785           return expr_eval_invalid_goto;
786         }
787     }
788
789   return expr_eval_no_error;
790 }
791
792 #endif
793
794 /* Make printf-type calls using arguments supplied from the host.  We
795    need to parse the format string ourselves, and call the formatting
796    function with one argument at a time, partly because there is no
797    safe portable way to construct a varargs call, and partly to serve
798    as a security barrier against bad format strings that might get
799    in.  */
800
801 static void
802 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
803            int nargs, ULONGEST *args)
804 {
805   const char *f = format;
806   struct format_piece *fpieces;
807   int i, fp;
808   char *current_substring;
809   int nargs_wanted;
810
811   ax_debug ("Printf of \"%s\" with %d args", format, nargs);
812
813   fpieces = parse_format_string (&f);
814
815   nargs_wanted = 0;
816   for (fp = 0; fpieces[fp].string != NULL; fp++)
817     if (fpieces[fp].argclass != literal_piece)
818       ++nargs_wanted;
819
820   if (nargs != nargs_wanted)
821     error (_("Wrong number of arguments for specified format-string"));
822
823   i = 0;
824   for (fp = 0; fpieces[fp].string != NULL; fp++)
825     {
826       current_substring = fpieces[fp].string;
827       ax_debug ("current substring is '%s', class is %d",
828                 current_substring, fpieces[fp].argclass);
829       switch (fpieces[fp].argclass)
830         {
831         case string_arg:
832           {
833             gdb_byte *str;
834             CORE_ADDR tem;
835             int j;
836
837             tem = args[i];
838
839             /* This is a %s argument.  Find the length of the string.  */
840             for (j = 0;; j++)
841               {
842                 gdb_byte c;
843
844                 read_inferior_memory (tem + j, &c, 1);
845                 if (c == 0)
846                   break;
847               }
848
849               /* Copy the string contents into a string inside GDB.  */
850               str = (gdb_byte *) alloca (j + 1);
851               if (j != 0)
852                 read_inferior_memory (tem, str, j);
853               str[j] = 0;
854
855               printf (current_substring, (char *) str);
856             }
857             break;
858
859           case long_long_arg:
860 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
861             {
862               long long val = args[i];
863
864               printf (current_substring, val);
865               break;
866             }
867 #else
868             error (_("long long not supported in agent printf"));
869 #endif
870         case int_arg:
871           {
872             int val = args[i];
873
874             printf (current_substring, val);
875             break;
876           }
877
878         case long_arg:
879           {
880             long val = args[i];
881
882             printf (current_substring, val);
883             break;
884           }
885
886         case literal_piece:
887           /* Print a portion of the format string that has no
888              directives.  Note that this will not include any
889              ordinary %-specs, but it might include "%%".  That is
890              why we use printf_filtered and not puts_filtered here.
891              Also, we pass a dummy argument because some platforms
892              have modified GCC to include -Wformat-security by
893              default, which will warn here if there is no
894              argument.  */
895           printf (current_substring, 0);
896           break;
897
898         default:
899           error (_("Format directive in '%s' not supported in agent printf"),
900                  current_substring);
901         }
902
903       /* Maybe advance to the next argument.  */
904       if (fpieces[fp].argclass != literal_piece)
905         ++i;
906     }
907
908   free_format_pieces (fpieces);
909   fflush (stdout);
910 }
911
912 /* The agent expression evaluator, as specified by the GDB docs. It
913    returns 0 if everything went OK, and a nonzero error code
914    otherwise.  */
915
916 enum eval_result_type
917 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
918                      struct agent_expr *aexpr,
919                      ULONGEST *rslt)
920 {
921   int pc = 0;
922 #define STACK_MAX 100
923   ULONGEST stack[STACK_MAX], top;
924   int sp = 0;
925   unsigned char op;
926   int arg;
927
928   /* This union is a convenient way to convert representations.  For
929      now, assume a standard architecture where the hardware integer
930      types have 8, 16, 32, 64 bit types.  A more robust solution would
931      be to import stdint.h from gnulib.  */
932   union
933   {
934     union
935     {
936       unsigned char bytes[1];
937       unsigned char val;
938     } u8;
939     union
940     {
941       unsigned char bytes[2];
942       unsigned short val;
943     } u16;
944     union
945     {
946       unsigned char bytes[4];
947       unsigned int val;
948     } u32;
949     union
950     {
951       unsigned char bytes[8];
952       ULONGEST val;
953     } u64;
954   } cnv;
955
956   if (aexpr->length == 0)
957     {
958       ax_debug ("empty agent expression");
959       return expr_eval_empty_expression;
960     }
961
962   /* Cache the stack top in its own variable. Much of the time we can
963      operate on this variable, rather than dinking with the stack. It
964      needs to be copied to the stack when sp changes.  */
965   top = 0;
966
967   while (1)
968     {
969       op = aexpr->bytes[pc++];
970
971       ax_debug ("About to interpret byte 0x%x", op);
972
973       switch (op)
974         {
975         case gdb_agent_op_add:
976           top += stack[--sp];
977           break;
978
979         case gdb_agent_op_sub:
980           top = stack[--sp] - top;
981           break;
982
983         case gdb_agent_op_mul:
984           top *= stack[--sp];
985           break;
986
987         case gdb_agent_op_div_signed:
988           if (top == 0)
989             {
990               ax_debug ("Attempted to divide by zero");
991               return expr_eval_divide_by_zero;
992             }
993           top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
994           break;
995
996         case gdb_agent_op_div_unsigned:
997           if (top == 0)
998             {
999               ax_debug ("Attempted to divide by zero");
1000               return expr_eval_divide_by_zero;
1001             }
1002           top = stack[--sp] / top;
1003           break;
1004
1005         case gdb_agent_op_rem_signed:
1006           if (top == 0)
1007             {
1008               ax_debug ("Attempted to divide by zero");
1009               return expr_eval_divide_by_zero;
1010             }
1011           top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1012           break;
1013
1014         case gdb_agent_op_rem_unsigned:
1015           if (top == 0)
1016             {
1017               ax_debug ("Attempted to divide by zero");
1018               return expr_eval_divide_by_zero;
1019             }
1020           top = stack[--sp] % top;
1021           break;
1022
1023         case gdb_agent_op_lsh:
1024           top = stack[--sp] << top;
1025           break;
1026
1027         case gdb_agent_op_rsh_signed:
1028           top = ((LONGEST) stack[--sp]) >> top;
1029           break;
1030
1031         case gdb_agent_op_rsh_unsigned:
1032           top = stack[--sp] >> top;
1033           break;
1034
1035         case gdb_agent_op_trace:
1036           agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
1037                           (ULONGEST) top);
1038           if (--sp >= 0)
1039             top = stack[sp];
1040           break;
1041
1042         case gdb_agent_op_trace_quick:
1043           arg = aexpr->bytes[pc++];
1044           agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
1045           break;
1046
1047         case gdb_agent_op_log_not:
1048           top = !top;
1049           break;
1050
1051         case gdb_agent_op_bit_and:
1052           top &= stack[--sp];
1053           break;
1054
1055         case gdb_agent_op_bit_or:
1056           top |= stack[--sp];
1057           break;
1058
1059         case gdb_agent_op_bit_xor:
1060           top ^= stack[--sp];
1061           break;
1062
1063         case gdb_agent_op_bit_not:
1064           top = ~top;
1065           break;
1066
1067         case gdb_agent_op_equal:
1068           top = (stack[--sp] == top);
1069           break;
1070
1071         case gdb_agent_op_less_signed:
1072           top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1073           break;
1074
1075         case gdb_agent_op_less_unsigned:
1076           top = (stack[--sp] < top);
1077           break;
1078
1079         case gdb_agent_op_ext:
1080           arg = aexpr->bytes[pc++];
1081           if (arg < (sizeof (LONGEST) * 8))
1082             {
1083               LONGEST mask = 1 << (arg - 1);
1084               top &= ((LONGEST) 1 << arg) - 1;
1085               top = (top ^ mask) - mask;
1086             }
1087           break;
1088
1089         case gdb_agent_op_ref8:
1090           agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
1091           top = cnv.u8.val;
1092           break;
1093
1094         case gdb_agent_op_ref16:
1095           agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
1096           top = cnv.u16.val;
1097           break;
1098
1099         case gdb_agent_op_ref32:
1100           agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
1101           top = cnv.u32.val;
1102           break;
1103
1104         case gdb_agent_op_ref64:
1105           agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
1106           top = cnv.u64.val;
1107           break;
1108
1109         case gdb_agent_op_if_goto:
1110           if (top)
1111             pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1112           else
1113             pc += 2;
1114           if (--sp >= 0)
1115             top = stack[sp];
1116           break;
1117
1118         case gdb_agent_op_goto:
1119           pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1120           break;
1121
1122         case gdb_agent_op_const8:
1123           /* Flush the cached stack top.  */
1124           stack[sp++] = top;
1125           top = aexpr->bytes[pc++];
1126           break;
1127
1128         case gdb_agent_op_const16:
1129           /* Flush the cached stack top.  */
1130           stack[sp++] = top;
1131           top = aexpr->bytes[pc++];
1132           top = (top << 8) + aexpr->bytes[pc++];
1133           break;
1134
1135         case gdb_agent_op_const32:
1136           /* Flush the cached stack top.  */
1137           stack[sp++] = top;
1138           top = aexpr->bytes[pc++];
1139           top = (top << 8) + aexpr->bytes[pc++];
1140           top = (top << 8) + aexpr->bytes[pc++];
1141           top = (top << 8) + aexpr->bytes[pc++];
1142           break;
1143
1144         case gdb_agent_op_const64:
1145           /* Flush the cached stack top.  */
1146           stack[sp++] = top;
1147           top = aexpr->bytes[pc++];
1148           top = (top << 8) + aexpr->bytes[pc++];
1149           top = (top << 8) + aexpr->bytes[pc++];
1150           top = (top << 8) + aexpr->bytes[pc++];
1151           top = (top << 8) + aexpr->bytes[pc++];
1152           top = (top << 8) + aexpr->bytes[pc++];
1153           top = (top << 8) + aexpr->bytes[pc++];
1154           top = (top << 8) + aexpr->bytes[pc++];
1155           break;
1156
1157         case gdb_agent_op_reg:
1158           /* Flush the cached stack top.  */
1159           stack[sp++] = top;
1160           arg = aexpr->bytes[pc++];
1161           arg = (arg << 8) + aexpr->bytes[pc++];
1162           {
1163             int regnum = arg;
1164             struct regcache *regcache = ctx->regcache;
1165
1166             switch (register_size (regcache->tdesc, regnum))
1167               {
1168               case 8:
1169                 collect_register (regcache, regnum, cnv.u64.bytes);
1170                 top = cnv.u64.val;
1171                 break;
1172               case 4:
1173                 collect_register (regcache, regnum, cnv.u32.bytes);
1174                 top = cnv.u32.val;
1175                 break;
1176               case 2:
1177                 collect_register (regcache, regnum, cnv.u16.bytes);
1178                 top = cnv.u16.val;
1179                 break;
1180               case 1:
1181                 collect_register (regcache, regnum, cnv.u8.bytes);
1182                 top = cnv.u8.val;
1183                 break;
1184               default:
1185                 internal_error (__FILE__, __LINE__,
1186                                 "unhandled register size");
1187               }
1188           }
1189           break;
1190
1191         case gdb_agent_op_end:
1192           ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
1193                     sp, pulongest (top));
1194           if (rslt)
1195             {
1196               if (sp <= 0)
1197                 {
1198                   /* This should be an error */
1199                   ax_debug ("Stack is empty, nothing to return");
1200                   return expr_eval_empty_stack;
1201                 }
1202               *rslt = top;
1203             }
1204           return expr_eval_no_error;
1205
1206         case gdb_agent_op_dup:
1207           stack[sp++] = top;
1208           break;
1209
1210         case gdb_agent_op_pop:
1211           if (--sp >= 0)
1212             top = stack[sp];
1213           break;
1214
1215         case gdb_agent_op_pick:
1216           arg = aexpr->bytes[pc++];
1217           stack[sp] = top;
1218           top = stack[sp - arg];
1219           ++sp;
1220           break;
1221
1222         case gdb_agent_op_rot:
1223           {
1224             ULONGEST tem = stack[sp - 1];
1225
1226             stack[sp - 1] = stack[sp - 2];
1227             stack[sp - 2] = top;
1228             top = tem;
1229           }
1230           break;
1231
1232         case gdb_agent_op_zero_ext:
1233           arg = aexpr->bytes[pc++];
1234           if (arg < (sizeof (LONGEST) * 8))
1235             top &= ((LONGEST) 1 << arg) - 1;
1236           break;
1237
1238         case gdb_agent_op_swap:
1239           /* Interchange top two stack elements, making sure top gets
1240              copied back onto stack.  */
1241           stack[sp] = top;
1242           top = stack[sp - 1];
1243           stack[sp - 1] = stack[sp];
1244           break;
1245
1246         case gdb_agent_op_getv:
1247           /* Flush the cached stack top.  */
1248           stack[sp++] = top;
1249           arg = aexpr->bytes[pc++];
1250           arg = (arg << 8) + aexpr->bytes[pc++];
1251           top = agent_get_trace_state_variable_value (arg);
1252           break;
1253
1254         case gdb_agent_op_setv:
1255           arg = aexpr->bytes[pc++];
1256           arg = (arg << 8) + aexpr->bytes[pc++];
1257           agent_set_trace_state_variable_value (arg, top);
1258           /* Note that we leave the value on the stack, for the
1259              benefit of later/enclosing expressions.  */
1260           break;
1261
1262         case gdb_agent_op_tracev:
1263           arg = aexpr->bytes[pc++];
1264           arg = (arg << 8) + aexpr->bytes[pc++];
1265           agent_tsv_read (ctx, arg);
1266           break;
1267
1268         case gdb_agent_op_tracenz:
1269           agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
1270                                  (ULONGEST) top);
1271           if (--sp >= 0)
1272             top = stack[sp];
1273           break;
1274
1275         case gdb_agent_op_printf:
1276           {
1277             int nargs, slen, i;
1278             CORE_ADDR fn = 0, chan = 0;
1279             /* Can't have more args than the entire size of the stack.  */
1280             ULONGEST args[STACK_MAX];
1281             char *format;
1282
1283             nargs = aexpr->bytes[pc++];
1284             slen = aexpr->bytes[pc++];
1285             slen = (slen << 8) + aexpr->bytes[pc++];
1286             format = (char *) &(aexpr->bytes[pc]);
1287             pc += slen;
1288             /* Pop function and channel.  */
1289             fn = top;
1290             if (--sp >= 0)
1291               top = stack[sp];
1292             chan = top;
1293             if (--sp >= 0)
1294               top = stack[sp];
1295             /* Pop arguments into a dedicated array.  */
1296             for (i = 0; i < nargs; ++i)
1297               {
1298                 args[i] = top;
1299                 if (--sp >= 0)
1300                   top = stack[sp];
1301               }
1302
1303             /* A bad format string means something is very wrong; give
1304                up immediately.  */
1305             if (format[slen - 1] != '\0')
1306               error (_("Unterminated format string in printf bytecode"));
1307
1308             ax_printf (fn, chan, format, nargs, args);
1309           }
1310           break;
1311
1312           /* GDB never (currently) generates any of these ops.  */
1313         case gdb_agent_op_float:
1314         case gdb_agent_op_ref_float:
1315         case gdb_agent_op_ref_double:
1316         case gdb_agent_op_ref_long_double:
1317         case gdb_agent_op_l_to_d:
1318         case gdb_agent_op_d_to_l:
1319         case gdb_agent_op_trace16:
1320           ax_debug ("Agent expression op 0x%x valid, but not handled",
1321                     op);
1322           /* If ever GDB generates any of these, we don't have the
1323              option of ignoring.  */
1324           return 1;
1325
1326         default:
1327           ax_debug ("Agent expression op 0x%x not recognized", op);
1328           /* Don't struggle on, things will just get worse.  */
1329           return expr_eval_unrecognized_opcode;
1330         }
1331
1332       /* Check for stack badness.  */
1333       if (sp >= (STACK_MAX - 1))
1334         {
1335           ax_debug ("Expression stack overflow");
1336           return expr_eval_stack_overflow;
1337         }
1338
1339       if (sp < 0)
1340         {
1341           ax_debug ("Expression stack underflow");
1342           return expr_eval_stack_underflow;
1343         }
1344
1345       ax_debug ("Op %s -> sp=%d, top=0x%s",
1346                 gdb_agent_op_name (op), sp, phex_nz (top, 0));
1347     }
1348 }