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