]> Git Repo - binutils.git/blob - gdbserver/ax.cc
Automatic date update in version.in
[binutils.git] / gdbserver / ax.cc
1 /* Agent expression code for remote server.
2    Copyright (C) 2009-2022 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   threads_debug_printf (PROG "/ax: %s", buf);
43 #endif
44   va_end (ap);
45 }
46
47 #define ax_debug(fmt, args...) \
48   do {                                          \
49     if (debug_threads)                  \
50       ax_vdebug ((fmt), ##args);                \
51   } while (0)
52
53 /* This enum must exactly match what is documented in
54    gdb/doc/agentexpr.texi, including all the numerical values.  */
55
56 enum gdb_agent_op
57   {
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
63   };
64
65 static const char * const gdb_agent_op_names [gdb_agent_op_last] =
66   {
67     "?undef?"
68 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , # NAME
69 #include "gdbsupport/ax.def"
70 #undef DEFOP
71   };
72
73 #ifndef IN_PROCESS_AGENT
74 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
75   {
76     0
77 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , SIZE
78 #include "gdbsupport/ax.def"
79 #undef DEFOP
80   };
81 #endif
82
83 /* A wrapper for gdb_agent_op_names that does some bounds-checking.  */
84
85 static const char *
86 gdb_agent_op_name (int op)
87 {
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];
91 }
92
93 #ifndef IN_PROCESS_AGENT
94
95 /* The packet form of an agent expression consists of an 'X', number
96    of bytes in expression, a comma, and then the bytes.  */
97
98 struct agent_expr *
99 gdb_parse_agent_expr (const char **actparm)
100 {
101   const char *act = *actparm;
102   ULONGEST xlen;
103   struct agent_expr *aexpr;
104
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;
114 }
115
116 void
117 gdb_free_agent_expr (struct agent_expr *aexpr)
118 {
119   if (aexpr != NULL)
120     {
121       free (aexpr->bytes);
122       free (aexpr);
123     }
124 }
125
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.  */
129
130 char *
131 gdb_unparse_agent_expr (struct agent_expr *aexpr)
132 {
133   char *rslt;
134
135   rslt = (char *) xmalloc (2 * aexpr->length + 1);
136   bin2hex (aexpr->bytes, rslt, aexpr->length);
137   return rslt;
138 }
139
140 /* Bytecode compilation.  */
141
142 CORE_ADDR current_insn_ptr;
143
144 int emit_error;
145
146 static struct bytecode_address
147 {
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;
155
156 void
157 emit_prologue (void)
158 {
159   target_emit_ops ()->emit_prologue ();
160 }
161
162 void
163 emit_epilogue (void)
164 {
165   target_emit_ops ()->emit_epilogue ();
166 }
167
168 static void
169 emit_add (void)
170 {
171   target_emit_ops ()->emit_add ();
172 }
173
174 static void
175 emit_sub (void)
176 {
177   target_emit_ops ()->emit_sub ();
178 }
179
180 static void
181 emit_mul (void)
182 {
183   target_emit_ops ()->emit_mul ();
184 }
185
186 static void
187 emit_lsh (void)
188 {
189   target_emit_ops ()->emit_lsh ();
190 }
191
192 static void
193 emit_rsh_signed (void)
194 {
195   target_emit_ops ()->emit_rsh_signed ();
196 }
197
198 static void
199 emit_rsh_unsigned (void)
200 {
201   target_emit_ops ()->emit_rsh_unsigned ();
202 }
203
204 static void
205 emit_ext (int arg)
206 {
207   target_emit_ops ()->emit_ext (arg);
208 }
209
210 static void
211 emit_log_not (void)
212 {
213   target_emit_ops ()->emit_log_not ();
214 }
215
216 static void
217 emit_bit_and (void)
218 {
219   target_emit_ops ()->emit_bit_and ();
220 }
221
222 static void
223 emit_bit_or (void)
224 {
225   target_emit_ops ()->emit_bit_or ();
226 }
227
228 static void
229 emit_bit_xor (void)
230 {
231   target_emit_ops ()->emit_bit_xor ();
232 }
233
234 static void
235 emit_bit_not (void)
236 {
237   target_emit_ops ()->emit_bit_not ();
238 }
239
240 static void
241 emit_equal (void)
242 {
243   target_emit_ops ()->emit_equal ();
244 }
245
246 static void
247 emit_less_signed (void)
248 {
249   target_emit_ops ()->emit_less_signed ();
250 }
251
252 static void
253 emit_less_unsigned (void)
254 {
255   target_emit_ops ()->emit_less_unsigned ();
256 }
257
258 static void
259 emit_ref (int size)
260 {
261   target_emit_ops ()->emit_ref (size);
262 }
263
264 static void
265 emit_if_goto (int *offset_p, int *size_p)
266 {
267   target_emit_ops ()->emit_if_goto (offset_p, size_p);
268 }
269
270 static void
271 emit_goto (int *offset_p, int *size_p)
272 {
273   target_emit_ops ()->emit_goto (offset_p, size_p);
274 }
275
276 static void
277 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
278 {
279   target_emit_ops ()->write_goto_address (from, to, size);
280 }
281
282 static void
283 emit_const (LONGEST num)
284 {
285   target_emit_ops ()->emit_const (num);
286 }
287
288 static void
289 emit_reg (int reg)
290 {
291   target_emit_ops ()->emit_reg (reg);
292 }
293
294 static void
295 emit_pop (void)
296 {
297   target_emit_ops ()->emit_pop ();
298 }
299
300 static void
301 emit_stack_flush (void)
302 {
303   target_emit_ops ()->emit_stack_flush ();
304 }
305
306 static void
307 emit_zero_ext (int arg)
308 {
309   target_emit_ops ()->emit_zero_ext (arg);
310 }
311
312 static void
313 emit_swap (void)
314 {
315   target_emit_ops ()->emit_swap ();
316 }
317
318 static void
319 emit_stack_adjust (int n)
320 {
321   target_emit_ops ()->emit_stack_adjust (n);
322 }
323
324 /* FN's prototype is `LONGEST(*fn)(int)'.  */
325
326 static void
327 emit_int_call_1 (CORE_ADDR fn, int arg1)
328 {
329   target_emit_ops ()->emit_int_call_1 (fn, arg1);
330 }
331
332 /* FN's prototype is `void(*fn)(int,LONGEST)'.  */
333
334 static void
335 emit_void_call_2 (CORE_ADDR fn, int arg1)
336 {
337   target_emit_ops ()->emit_void_call_2 (fn, arg1);
338 }
339
340 static void
341 emit_eq_goto (int *offset_p, int *size_p)
342 {
343   target_emit_ops ()->emit_eq_goto (offset_p, size_p);
344 }
345
346 static void
347 emit_ne_goto (int *offset_p, int *size_p)
348 {
349   target_emit_ops ()->emit_ne_goto (offset_p, size_p);
350 }
351
352 static void
353 emit_lt_goto (int *offset_p, int *size_p)
354 {
355   target_emit_ops ()->emit_lt_goto (offset_p, size_p);
356 }
357
358 static void
359 emit_ge_goto (int *offset_p, int *size_p)
360 {
361   target_emit_ops ()->emit_ge_goto (offset_p, size_p);
362 }
363
364 static void
365 emit_gt_goto (int *offset_p, int *size_p)
366 {
367   target_emit_ops ()->emit_gt_goto (offset_p, size_p);
368 }
369
370 static void
371 emit_le_goto (int *offset_p, int *size_p)
372 {
373   target_emit_ops ()->emit_le_goto (offset_p, size_p);
374 }
375
376 /* Scan an agent expression for any evidence that the given PC is the
377    target of a jump bytecode in the expression.  */
378
379 static int
380 is_goto_target (struct agent_expr *aexpr, int pc)
381 {
382   int i;
383   unsigned char op;
384
385   for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
386     {
387       op = aexpr->bytes[i];
388
389       if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
390         {
391           int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
392           if (target == pc)
393             return 1;
394         }
395     }
396
397   return 0;
398 }
399
400 /* Given an agent expression, turn it into native code.  */
401
402 enum eval_result_type
403 compile_bytecodes (struct agent_expr *aexpr)
404 {
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;
412
413 #define UNHANDLED                                       \
414   do                                                    \
415     {                                                   \
416       ax_debug ("Cannot compile op 0x%x\n", op);        \
417       return expr_eval_unhandled_opcode;                \
418     } while (0)
419
420   if (aexpr->length == 0)
421     {
422       ax_debug ("empty agent expression\n");
423       return expr_eval_empty_expression;
424     }
425
426   bytecode_address_table = NULL;
427
428   while (!done)
429     {
430       op = aexpr->bytes[pc];
431
432       ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
433
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;
443
444       ++pc;
445
446       emit_error = 0;
447
448       switch (op)
449         {
450         case gdb_agent_op_add:
451           emit_add ();
452           break;
453
454         case gdb_agent_op_sub:
455           emit_sub ();
456           break;
457
458         case gdb_agent_op_mul:
459           emit_mul ();
460           break;
461
462         case gdb_agent_op_div_signed:
463           UNHANDLED;
464           break;
465
466         case gdb_agent_op_div_unsigned:
467           UNHANDLED;
468           break;
469
470         case gdb_agent_op_rem_signed:
471           UNHANDLED;
472           break;
473
474         case gdb_agent_op_rem_unsigned:
475           UNHANDLED;
476           break;
477
478         case gdb_agent_op_lsh:
479           emit_lsh ();
480           break;
481
482         case gdb_agent_op_rsh_signed:
483           emit_rsh_signed ();
484           break;
485
486         case gdb_agent_op_rsh_unsigned:
487           emit_rsh_unsigned ();
488           break;
489
490         case gdb_agent_op_trace:
491           UNHANDLED;
492           break;
493
494         case gdb_agent_op_trace_quick:
495           UNHANDLED;
496           break;
497
498         case gdb_agent_op_log_not:
499           emit_log_not ();
500           break;
501
502         case gdb_agent_op_bit_and:
503           emit_bit_and ();
504           break;
505
506         case gdb_agent_op_bit_or:
507           emit_bit_or ();
508           break;
509
510         case gdb_agent_op_bit_xor:
511           emit_bit_xor ();
512           break;
513
514         case gdb_agent_op_bit_not:
515           emit_bit_not ();
516           break;
517
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)
523             {
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));
531             }
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)
536             {
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));
544             }
545           else
546             emit_equal ();
547           break;
548
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))
553             {
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));
561             }
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))
566             {
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));
574             }
575           else
576             emit_less_signed ();
577           break;
578
579         case gdb_agent_op_less_unsigned:
580           emit_less_unsigned ();
581           break;
582
583         case gdb_agent_op_ext:
584           arg = aexpr->bytes[pc++];
585           if (arg < (sizeof (LONGEST) * 8))
586             emit_ext (arg);
587           break;
588
589         case gdb_agent_op_ref8:
590           emit_ref (1);
591           break;
592
593         case gdb_agent_op_ref16:
594           emit_ref (2);
595           break;
596
597         case gdb_agent_op_ref32:
598           emit_ref (4);
599           break;
600
601         case gdb_agent_op_ref64:
602           emit_ref (8);
603           break;
604
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;
611
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;
618
619         case gdb_agent_op_const8:
620           emit_stack_flush ();
621           top = aexpr->bytes[pc++];
622           emit_const (top);
623           break;
624
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;
631
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;
640
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;
653
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;
660
661         case gdb_agent_op_end:
662           ax_debug ("At end of expression\n");
663
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);
667
668           done = 1;
669           break;
670
671         case gdb_agent_op_dup:
672           /* In our design, dup is equivalent to stack flushing.  */
673           emit_stack_flush ();
674           break;
675
676         case gdb_agent_op_pop:
677           emit_pop ();
678           break;
679
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;
685
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))
693             {
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));
701             }
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))
708             {
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));
716             }
717           else
718             emit_swap ();
719           break;
720
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;
728
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;
735
736         case gdb_agent_op_tracev:
737           UNHANDLED;
738           break;
739
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;
750
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;
755         }
756
757       /* This catches errors that occur in target-specific code
758          emission.  */
759       if (emit_error)
760         {
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;
764         }
765
766       ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
767     }
768
769   /* Now fill in real addresses as goto destinations.  */
770   for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
771     {
772       int written = 0;
773
774       if (aentry->goto_pc < 0)
775         continue;
776
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)
781         {
782           if (aentry2->pc == aentry->goto_pc)
783             {
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;
791             }
792         }
793
794       /* Error out if we didn't find a destination.  */
795       if (!written)
796         {
797           ax_debug ("Destination of goto %d not found\n",
798                     aentry->goto_pc);
799           return expr_eval_invalid_goto;
800         }
801     }
802
803   return expr_eval_no_error;
804 }
805
806 #endif
807
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.  */
814
815 static void
816 ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
817            int nargs, ULONGEST *args)
818 {
819   const char *f = format;
820   int i;
821   const char *current_substring;
822   int nargs_wanted;
823
824   ax_debug ("Printf of \"%s\" with %d args", format, nargs);
825
826   format_pieces fpieces (&f);
827
828   nargs_wanted = 0;
829   for (auto &&piece : fpieces)
830     if (piece.argclass != literal_piece)
831       ++nargs_wanted;
832
833   if (nargs != nargs_wanted)
834     error (_("Wrong number of arguments for specified format-string"));
835
836   i = 0;
837   for (auto &&piece : fpieces)
838     {
839       current_substring = piece.string;
840       ax_debug ("current substring is '%s', class is %d",
841                 current_substring, piece.argclass);
842       switch (piece.argclass)
843         {
844         case string_arg:
845           {
846             gdb_byte *str;
847             CORE_ADDR tem;
848             int j;
849
850             tem = args[i];
851             if (tem == 0)
852               {
853                 printf (current_substring, "(null)");
854                 break;
855               }
856
857             /* This is a %s argument.  Find the length of the string.  */
858             for (j = 0;; j++)
859               {
860                 gdb_byte c;
861
862                 read_inferior_memory (tem + j, &c, 1);
863                 if (c == 0)
864                   break;
865               }
866
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;
872
873               printf (current_substring, (char *) str);
874             }
875             break;
876
877           case long_long_arg:
878 #if defined (PRINTF_HAS_LONG_LONG)
879             {
880               long long val = args[i];
881
882               printf (current_substring, val);
883               break;
884             }
885 #else
886             error (_("long long not supported in agent printf"));
887 #endif
888         case int_arg:
889           {
890             int val = args[i];
891
892             printf (current_substring, val);
893             break;
894           }
895
896         case long_arg:
897           {
898             long val = args[i];
899
900             printf (current_substring, val);
901             break;
902           }
903
904         case size_t_arg:
905           {
906             size_t val = args[i];
907
908             printf (current_substring, val);
909             break;
910           }
911
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;
923
924         default:
925           error (_("Format directive in '%s' not supported in agent printf"),
926                  current_substring);
927         }
928
929       /* Maybe advance to the next argument.  */
930       if (piece.argclass != literal_piece)
931         ++i;
932     }
933
934   fflush (stdout);
935 }
936
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.  */
940
941 enum eval_result_type
942 gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
943                      struct agent_expr *aexpr,
944                      ULONGEST *rslt)
945 {
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;
952
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
958   {
959     union
960     {
961       unsigned char bytes[1];
962       unsigned char val;
963     } u8;
964     union
965     {
966       unsigned char bytes[2];
967       unsigned short val;
968     } u16;
969     union
970     {
971       unsigned char bytes[4];
972       unsigned int val;
973     } u32;
974     union
975     {
976       unsigned char bytes[8];
977       ULONGEST val;
978     } u64;
979   } cnv;
980
981   if (aexpr->length == 0)
982     {
983       ax_debug ("empty agent expression");
984       return expr_eval_empty_expression;
985     }
986
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;
991
992   while (1)
993     {
994       op = aexpr->bytes[pc++];
995
996       ax_debug ("About to interpret byte 0x%x", op);
997
998       switch (op)
999         {
1000         case gdb_agent_op_add:
1001           top += stack[--sp];
1002           break;
1003
1004         case gdb_agent_op_sub:
1005           top = stack[--sp] - top;
1006           break;
1007
1008         case gdb_agent_op_mul:
1009           top *= stack[--sp];
1010           break;
1011
1012         case gdb_agent_op_div_signed:
1013           if (top == 0)
1014             {
1015               ax_debug ("Attempted to divide by zero");
1016               return expr_eval_divide_by_zero;
1017             }
1018           top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
1019           break;
1020
1021         case gdb_agent_op_div_unsigned:
1022           if (top == 0)
1023             {
1024               ax_debug ("Attempted to divide by zero");
1025               return expr_eval_divide_by_zero;
1026             }
1027           top = stack[--sp] / top;
1028           break;
1029
1030         case gdb_agent_op_rem_signed:
1031           if (top == 0)
1032             {
1033               ax_debug ("Attempted to divide by zero");
1034               return expr_eval_divide_by_zero;
1035             }
1036           top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
1037           break;
1038
1039         case gdb_agent_op_rem_unsigned:
1040           if (top == 0)
1041             {
1042               ax_debug ("Attempted to divide by zero");
1043               return expr_eval_divide_by_zero;
1044             }
1045           top = stack[--sp] % top;
1046           break;
1047
1048         case gdb_agent_op_lsh:
1049           top = stack[--sp] << top;
1050           break;
1051
1052         case gdb_agent_op_rsh_signed:
1053           top = ((LONGEST) stack[--sp]) >> top;
1054           break;
1055
1056         case gdb_agent_op_rsh_unsigned:
1057           top = stack[--sp] >> top;
1058           break;
1059
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;
1066
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;
1071
1072         case gdb_agent_op_log_not:
1073           top = !top;
1074           break;
1075
1076         case gdb_agent_op_bit_and:
1077           top &= stack[--sp];
1078           break;
1079
1080         case gdb_agent_op_bit_or:
1081           top |= stack[--sp];
1082           break;
1083
1084         case gdb_agent_op_bit_xor:
1085           top ^= stack[--sp];
1086           break;
1087
1088         case gdb_agent_op_bit_not:
1089           top = ~top;
1090           break;
1091
1092         case gdb_agent_op_equal:
1093           top = (stack[--sp] == top);
1094           break;
1095
1096         case gdb_agent_op_less_signed:
1097           top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
1098           break;
1099
1100         case gdb_agent_op_less_unsigned:
1101           top = (stack[--sp] < top);
1102           break;
1103
1104         case gdb_agent_op_ext:
1105           arg = aexpr->bytes[pc++];
1106           if (arg < (sizeof (LONGEST) * 8))
1107             {
1108               LONGEST mask = 1 << (arg - 1);
1109               top &= ((LONGEST) 1 << arg) - 1;
1110               top = (top ^ mask) - mask;
1111             }
1112           break;
1113
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;
1118
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;
1123
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;
1128
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;
1133
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;
1142
1143         case gdb_agent_op_goto:
1144           pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
1145           break;
1146
1147         case gdb_agent_op_const8:
1148           /* Flush the cached stack top.  */
1149           stack[sp++] = top;
1150           top = aexpr->bytes[pc++];
1151           break;
1152
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;
1159
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;
1168
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;
1181
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++];
1187           {
1188             int regnum = arg;
1189             struct regcache *regcache = ctx->regcache;
1190
1191             switch (register_size (regcache->tdesc, regnum))
1192               {
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");
1211               }
1212           }
1213           break;
1214
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)
1219             {
1220               if (sp <= 0)
1221                 {
1222                   /* This should be an error */
1223                   ax_debug ("Stack is empty, nothing to return");
1224                   return expr_eval_empty_stack;
1225                 }
1226               *rslt = top;
1227             }
1228           return expr_eval_no_error;
1229
1230         case gdb_agent_op_dup:
1231           stack[sp++] = top;
1232           break;
1233
1234         case gdb_agent_op_pop:
1235           if (--sp >= 0)
1236             top = stack[sp];
1237           break;
1238
1239         case gdb_agent_op_pick:
1240           arg = aexpr->bytes[pc++];
1241           stack[sp] = top;
1242           top = stack[sp - arg];
1243           ++sp;
1244           break;
1245
1246         case gdb_agent_op_rot:
1247           {
1248             ULONGEST tem = stack[sp - 1];
1249
1250             stack[sp - 1] = stack[sp - 2];
1251             stack[sp - 2] = top;
1252             top = tem;
1253           }
1254           break;
1255
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;
1261
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;
1269
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;
1277
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;
1285
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;
1291
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;
1298
1299         case gdb_agent_op_printf:
1300           {
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;
1306
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)
1321               {
1322                 args[i] = top;
1323                 if (--sp >= 0)
1324                   top = stack[sp];
1325               }
1326
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"));
1331
1332             ax_printf (fn, chan, format, nargs, args);
1333           }
1334           break;
1335
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;
1349
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;
1354         }
1355
1356       /* Check for stack badness.  */
1357       if (sp >= (STACK_MAX - 1))
1358         {
1359           ax_debug ("Expression stack overflow");
1360           return expr_eval_stack_overflow;
1361         }
1362
1363       if (sp < 0)
1364         {
1365           ax_debug ("Expression stack underflow");
1366           return expr_eval_stack_underflow;
1367         }
1368
1369       ax_debug ("Op %s -> sp=%d, top=0x%s",
1370                 gdb_agent_op_name (op), sp, phex_nz (top, 0));
1371     }
1372 }
This page took 0.099759 seconds and 4 git commands to generate.