gdb/ax-general.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Functions for manipulating expressions designed to be executed on the agent
  2.    Copyright (C) 1998-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* Despite what the above comment says about this file being part of
  15.    GDB, we would like to keep these functions free of GDB
  16.    dependencies, since we want to be able to use them in contexts
  17.    outside of GDB (test suites, the stub, etc.)  */

  18. #include "defs.h"
  19. #include "ax.h"

  20. #include "value.h"
  21. #include "user-regs.h"

  22. static void grow_expr (struct agent_expr *x, int n);

  23. static void append_const (struct agent_expr *x, LONGEST val, int n);

  24. static LONGEST read_const (struct agent_expr *x, int o, int n);

  25. static void generic_ext (struct agent_expr *x, enum agent_op op, int n);

  26. /* Functions for building expressions.  */

  27. /* Allocate a new, empty agent expression.  */
  28. struct agent_expr *
  29. new_agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope)
  30. {
  31.   struct agent_expr *x = xmalloc (sizeof (*x));

  32.   x->len = 0;
  33.   x->size = 1;                        /* Change this to a larger value once
  34.                                    reallocation code is tested.  */
  35.   x->buf = xmalloc (x->size);

  36.   x->gdbarch = gdbarch;
  37.   x->scope = scope;

  38.   /* Bit vector for registers used.  */
  39.   x->reg_mask_len = 1;
  40.   x->reg_mask = xmalloc (x->reg_mask_len * sizeof (x->reg_mask[0]));
  41.   memset (x->reg_mask, 0, x->reg_mask_len * sizeof (x->reg_mask[0]));

  42.   x->tracing = 0;
  43.   x->trace_string = 0;

  44.   return x;
  45. }

  46. /* Free a agent expression.  */
  47. void
  48. free_agent_expr (struct agent_expr *x)
  49. {
  50.   xfree (x->buf);
  51.   xfree (x->reg_mask);
  52.   xfree (x);
  53. }

  54. static void
  55. do_free_agent_expr_cleanup (void *x)
  56. {
  57.   free_agent_expr (x);
  58. }

  59. struct cleanup *
  60. make_cleanup_free_agent_expr (struct agent_expr *x)
  61. {
  62.   return make_cleanup (do_free_agent_expr_cleanup, x);
  63. }


  64. /* Make sure that X has room for at least N more bytes.  This doesn't
  65.    affect the length, just the allocated size.  */
  66. static void
  67. grow_expr (struct agent_expr *x, int n)
  68. {
  69.   if (x->len + n > x->size)
  70.     {
  71.       x->size *= 2;
  72.       if (x->size < x->len + n)
  73.         x->size = x->len + n + 10;
  74.       x->buf = xrealloc (x->buf, x->size);
  75.     }
  76. }


  77. /* Append the low N bytes of VAL as an N-byte integer to the
  78.    expression X, in big-endian order.  */
  79. static void
  80. append_const (struct agent_expr *x, LONGEST val, int n)
  81. {
  82.   int i;

  83.   grow_expr (x, n);
  84.   for (i = n - 1; i >= 0; i--)
  85.     {
  86.       x->buf[x->len + i] = val & 0xff;
  87.       val >>= 8;
  88.     }
  89.   x->len += n;
  90. }


  91. /* Extract an N-byte big-endian unsigned integer from expression X at
  92.    offset O.  */
  93. static LONGEST
  94. read_const (struct agent_expr *x, int o, int n)
  95. {
  96.   int i;
  97.   LONGEST accum = 0;

  98.   /* Make sure we're not reading off the end of the expression.  */
  99.   if (o + n > x->len)
  100.     error (_("GDB bug: ax-general.c (read_const): incomplete constant"));

  101.   for (i = 0; i < n; i++)
  102.     accum = (accum << 8) | x->buf[o + i];

  103.   return accum;
  104. }


  105. /* Append a simple operator OP to EXPR.  */
  106. void
  107. ax_simple (struct agent_expr *x, enum agent_op op)
  108. {
  109.   grow_expr (x, 1);
  110.   x->buf[x->len++] = op;
  111. }

  112. /* Append a pick operator to EXPR.  DEPTH is the stack item to pick,
  113.    with 0 being top of stack.  */

  114. void
  115. ax_pick (struct agent_expr *x, int depth)
  116. {
  117.   if (depth < 0 || depth > 255)
  118.     error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
  119.   ax_simple (x, aop_pick);
  120.   append_const (x, 1, depth);
  121. }


  122. /* Append a sign-extension or zero-extension instruction to EXPR, to
  123.    extend an N-bit value.  */
  124. static void
  125. generic_ext (struct agent_expr *x, enum agent_op op, int n)
  126. {
  127.   /* N must fit in a byte.  */
  128.   if (n < 0 || n > 255)
  129.     error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
  130.   /* That had better be enough range.  */
  131.   if (sizeof (LONGEST) * 8 > 255)
  132.     error (_("GDB bug: ax-general.c (generic_ext): "
  133.              "opcode has inadequate range"));

  134.   grow_expr (x, 2);
  135.   x->buf[x->len++] = op;
  136.   x->buf[x->len++] = n;
  137. }


  138. /* Append a sign-extension instruction to EXPR, to extend an N-bit value.  */
  139. void
  140. ax_ext (struct agent_expr *x, int n)
  141. {
  142.   generic_ext (x, aop_ext, n);
  143. }


  144. /* Append a zero-extension instruction to EXPR, to extend an N-bit value.  */
  145. void
  146. ax_zero_ext (struct agent_expr *x, int n)
  147. {
  148.   generic_ext (x, aop_zero_ext, n);
  149. }


  150. /* Append a trace_quick instruction to EXPR, to record N bytes.  */
  151. void
  152. ax_trace_quick (struct agent_expr *x, int n)
  153. {
  154.   /* N must fit in a byte.  */
  155.   if (n < 0 || n > 255)
  156.     error (_("GDB bug: ax-general.c (ax_trace_quick): "
  157.              "size out of range for trace_quick"));

  158.   grow_expr (x, 2);
  159.   x->buf[x->len++] = aop_trace_quick;
  160.   x->buf[x->len++] = n;
  161. }


  162. /* Append a goto op to EXPR.  OP is the actual op (must be aop_goto or
  163.    aop_if_goto).  We assume we don't know the target offset yet,
  164.    because it's probably a forward branch, so we leave space in EXPR
  165.    for the target, and return the offset in EXPR of that space, so we
  166.    can backpatch it once we do know the target offset.  Use ax_label
  167.    to do the backpatching.  */
  168. int
  169. ax_goto (struct agent_expr *x, enum agent_op op)
  170. {
  171.   grow_expr (x, 3);
  172.   x->buf[x->len + 0] = op;
  173.   x->buf[x->len + 1] = 0xff;
  174.   x->buf[x->len + 2] = 0xff;
  175.   x->len += 3;
  176.   return x->len - 2;
  177. }

  178. /* Suppose a given call to ax_goto returns some value PATCH.  When you
  179.    know the offset TARGET that goto should jump to, call
  180.    ax_label (EXPR, PATCH, TARGET)
  181.    to patch TARGET into the ax_goto instruction.  */
  182. void
  183. ax_label (struct agent_expr *x, int patch, int target)
  184. {
  185.   /* Make sure the value is in range.  Don't accept 0xffff as an
  186.      offset; that's our magic sentinel value for unpatched branches.  */
  187.   if (target < 0 || target >= 0xffff)
  188.     error (_("GDB bug: ax-general.c (ax_label): label target out of range"));

  189.   x->buf[patch] = (target >> 8) & 0xff;
  190.   x->buf[patch + 1] = target & 0xff;
  191. }


  192. /* Assemble code to push a constant on the stack.  */
  193. void
  194. ax_const_l (struct agent_expr *x, LONGEST l)
  195. {
  196.   static enum agent_op ops[]
  197.   =
  198.   {aop_const8, aop_const16, aop_const32, aop_const64};
  199.   int size;
  200.   int op;

  201.   /* How big is the number?  'op' keeps track of which opcode to use.
  202.      Notice that we don't really care whether the original number was
  203.      signed or unsigned; we always reproduce the value exactly, and
  204.      use the shortest representation.  */
  205.   for (op = 0, size = 8; size < 64; size *= 2, op++)
  206.     {
  207.       LONGEST lim = ((LONGEST) 1) << (size - 1);

  208.       if (-lim <= l && l <= lim - 1)
  209.         break;
  210.     }

  211.   /* Emit the right opcode...  */
  212.   ax_simple (x, ops[op]);

  213.   /* Emit the low SIZE bytes as an unsigned number.  We know that
  214.      sign-extending this will yield l.  */
  215.   append_const (x, l, size / 8);

  216.   /* Now, if it was negative, and not full-sized, sign-extend it.  */
  217.   if (l < 0 && size < 64)
  218.     ax_ext (x, size);
  219. }


  220. void
  221. ax_const_d (struct agent_expr *x, LONGEST d)
  222. {
  223.   /* FIXME: floating-point support not present yet.  */
  224.   error (_("GDB bug: ax-general.c (ax_const_d): "
  225.            "floating point not supported yet"));
  226. }


  227. /* Assemble code to push the value of register number REG on the
  228.    stack.  */
  229. void
  230. ax_reg (struct agent_expr *x, int reg)
  231. {
  232.   if (reg >= gdbarch_num_regs (x->gdbarch))
  233.     {
  234.       /* This is a pseudo-register.  */
  235.       if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
  236.         error (_("'%s' is a pseudo-register; "
  237.                  "GDB cannot yet trace its contents."),
  238.                user_reg_map_regnum_to_name (x->gdbarch, reg));
  239.       if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
  240.         error (_("Trace '%s' failed."),
  241.                user_reg_map_regnum_to_name (x->gdbarch, reg));
  242.     }
  243.   else
  244.     {
  245.       /* Make sure the register number is in range.  */
  246.       if (reg < 0 || reg > 0xffff)
  247.         error (_("GDB bug: ax-general.c (ax_reg): "
  248.                  "register number out of range"));
  249.       grow_expr (x, 3);
  250.       x->buf[x->len] = aop_reg;
  251.       x->buf[x->len + 1] = (reg >> 8) & 0xff;
  252.       x->buf[x->len + 2] = (reg) & 0xff;
  253.       x->len += 3;
  254.     }
  255. }

  256. /* Assemble code to operate on a trace state variable.  */

  257. void
  258. ax_tsv (struct agent_expr *x, enum agent_op op, int num)
  259. {
  260.   /* Make sure the tsv number is in range.  */
  261.   if (num < 0 || num > 0xffff)
  262.     internal_error (__FILE__, __LINE__,
  263.                     _("ax-general.c (ax_tsv): variable "
  264.                       "number is %d, out of range"), num);

  265.   grow_expr (x, 3);
  266.   x->buf[x->len] = op;
  267.   x->buf[x->len + 1] = (num >> 8) & 0xff;
  268.   x->buf[x->len + 2] = (num) & 0xff;
  269.   x->len += 3;
  270. }

  271. /* Append a string to the expression.  Note that the string is going
  272.    into the bytecodes directly, not on the stack.  As a precaution,
  273.    include both length as prefix, and terminate with a NUL.  (The NUL
  274.    is counted in the length.)  */

  275. void
  276. ax_string (struct agent_expr *x, const char *str, int slen)
  277. {
  278.   int i;

  279.   /* Make sure the string length is reasonable.  */
  280.   if (slen < 0 || slen > 0xffff)
  281.     internal_error (__FILE__, __LINE__,
  282.                     _("ax-general.c (ax_string): string "
  283.                       "length is %d, out of allowed range"), slen);

  284.   grow_expr (x, 2 + slen + 1);
  285.   x->buf[x->len++] = ((slen + 1) >> 8) & 0xff;
  286.   x->buf[x->len++] = (slen + 1) & 0xff;
  287.   for (i = 0; i < slen; ++i)
  288.     x->buf[x->len++] = str[i];
  289.   x->buf[x->len++] = '\0';
  290. }



  291. /* Functions for disassembling agent expressions, and otherwise
  292.    debugging the expression compiler.  */

  293. struct aop_map aop_map[] =
  294. {
  295.   {0, 0, 0, 0, 0}
  296. #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
  297.   , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
  298. #include "ax.def"
  299. #undef DEFOP
  300. };


  301. /* Disassemble the expression EXPR, writing to F.  */
  302. void
  303. ax_print (struct ui_file *f, struct agent_expr *x)
  304. {
  305.   int i;

  306.   fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
  307.   fprintf_filtered (f, _("Reg mask:"));
  308.   for (i = 0; i < x->reg_mask_len; ++i)
  309.     fprintf_filtered (f, _(" %02x"), x->reg_mask[i]);
  310.   fprintf_filtered (f, _("\n"));

  311.   /* Check the size of the name array against the number of entries in
  312.      the enum, to catch additions that people didn't sync.  */
  313.   if ((sizeof (aop_map) / sizeof (aop_map[0]))
  314.       != aop_last)
  315.     error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));

  316.   for (i = 0; i < x->len;)
  317.     {
  318.       enum agent_op op = x->buf[i];

  319.       if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
  320.           || !aop_map[op].name)
  321.         {
  322.           fprintf_filtered (f, _("%3d  <bad opcode %02x>\n"), i, op);
  323.           i++;
  324.           continue;
  325.         }
  326.       if (i + 1 + aop_map[op].op_size > x->len)
  327.         {
  328.           fprintf_filtered (f, _("%3d  <incomplete opcode %s>\n"),
  329.                             i, aop_map[op].name);
  330.           break;
  331.         }

  332.       fprintf_filtered (f, "%3d  %s", i, aop_map[op].name);
  333.       if (aop_map[op].op_size > 0)
  334.         {
  335.           fputs_filtered (" ", f);

  336.           print_longest (f, 'd', 0,
  337.                          read_const (x, i + 1, aop_map[op].op_size));
  338.         }
  339.       /* Handle the complicated printf arguments specially.  */
  340.       else if (op == aop_printf)
  341.         {
  342.           int slen, nargs;

  343.           i++;
  344.           nargs = x->buf[i++];
  345.           slen = x->buf[i++];
  346.           slen = slen * 256 + x->buf[i++];
  347.           fprintf_filtered (f, _(" \"%s\", %d args"),
  348.                             &(x->buf[i]), nargs);
  349.           i += slen - 1;
  350.         }
  351.       fprintf_filtered (f, "\n");
  352.       i += 1 + aop_map[op].op_size;
  353.     }
  354. }

  355. /* Add register REG to the register mask for expression AX.  */
  356. void
  357. ax_reg_mask (struct agent_expr *ax, int reg)
  358. {
  359.   if (reg >= gdbarch_num_regs (ax->gdbarch))
  360.     {
  361.       /* This is a pseudo-register.  */
  362.       if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
  363.         error (_("'%s' is a pseudo-register; "
  364.                  "GDB cannot yet trace its contents."),
  365.                user_reg_map_regnum_to_name (ax->gdbarch, reg));
  366.       if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
  367.         error (_("Trace '%s' failed."),
  368.                user_reg_map_regnum_to_name (ax->gdbarch, reg));
  369.     }
  370.   else
  371.     {
  372.       int byte = reg / 8;

  373.       /* Grow the bit mask if necessary.  */
  374.       if (byte >= ax->reg_mask_len)
  375.         {
  376.           /* It's not appropriate to double here.  This isn't a
  377.              string buffer.  */
  378.           int new_len = byte + 1;
  379.           unsigned char *new_reg_mask = xrealloc (ax->reg_mask,
  380.                                                   new_len
  381.                                                   * sizeof (ax->reg_mask[0]));
  382.           memset (new_reg_mask + ax->reg_mask_len, 0,
  383.                   (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0]));
  384.           ax->reg_mask_len = new_len;
  385.           ax->reg_mask = new_reg_mask;
  386.         }

  387.       ax->reg_mask[byte] |= 1 << (reg % 8);
  388.     }
  389. }

  390. /* Given an agent expression AX, fill in requirements and other descriptive
  391.    bits.  */
  392. void
  393. ax_reqs (struct agent_expr *ax)
  394. {
  395.   int i;
  396.   int height;

  397.   /* Jump target table.  targets[i] is non-zero iff we have found a
  398.      jump to offset i.  */
  399.   char *targets = (char *) alloca (ax->len * sizeof (targets[0]));

  400.   /* Instruction boundary table.  boundary[i] is non-zero iff our scan
  401.      has reached an instruction starting at offset i.  */
  402.   char *boundary = (char *) alloca (ax->len * sizeof (boundary[0]));

  403.   /* Stack height record.  If either targets[i] or boundary[i] is
  404.      non-zero, heights[i] is the height the stack should have before
  405.      executing the bytecode at that point.  */
  406.   int *heights = (int *) alloca (ax->len * sizeof (heights[0]));

  407.   /* Pointer to a description of the present op.  */
  408.   struct aop_map *op;

  409.   memset (targets, 0, ax->len * sizeof (targets[0]));
  410.   memset (boundary, 0, ax->len * sizeof (boundary[0]));

  411.   ax->max_height = ax->min_height = height = 0;
  412.   ax->flaw = agent_flaw_none;
  413.   ax->max_data_size = 0;

  414.   for (i = 0; i < ax->len; i += 1 + op->op_size)
  415.     {
  416.       if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
  417.         {
  418.           ax->flaw = agent_flaw_bad_instruction;
  419.           return;
  420.         }

  421.       op = &aop_map[ax->buf[i]];

  422.       if (!op->name)
  423.         {
  424.           ax->flaw = agent_flaw_bad_instruction;
  425.           return;
  426.         }

  427.       if (i + 1 + op->op_size > ax->len)
  428.         {
  429.           ax->flaw = agent_flaw_incomplete_instruction;
  430.           return;
  431.         }

  432.       /* If this instruction is a forward jump target, does the
  433.          current stack height match the stack height at the jump
  434.          source?  */
  435.       if (targets[i] && (heights[i] != height))
  436.         {
  437.           ax->flaw = agent_flaw_height_mismatch;
  438.           return;
  439.         }

  440.       boundary[i] = 1;
  441.       heights[i] = height;

  442.       height -= op->consumed;
  443.       if (height < ax->min_height)
  444.         ax->min_height = height;
  445.       height += op->produced;
  446.       if (height > ax->max_height)
  447.         ax->max_height = height;

  448.       if (op->data_size > ax->max_data_size)
  449.         ax->max_data_size = op->data_size;

  450.       /* For jump instructions, check that the target is a valid
  451.          offset.  If it is, record the fact that that location is a
  452.          jump target, and record the height we expect there.  */
  453.       if (aop_goto == op - aop_map
  454.           || aop_if_goto == op - aop_map)
  455.         {
  456.           int target = read_const (ax, i + 1, 2);
  457.           if (target < 0 || target >= ax->len)
  458.             {
  459.               ax->flaw = agent_flaw_bad_jump;
  460.               return;
  461.             }

  462.           /* Do we have any information about what the stack height
  463.              should be at the target?  */
  464.           if (targets[target] || boundary[target])
  465.             {
  466.               if (heights[target] != height)
  467.                 {
  468.                   ax->flaw = agent_flaw_height_mismatch;
  469.                   return;
  470.                 }
  471.             }

  472.           /* Record the target, along with the stack height we expect.  */
  473.           targets[target] = 1;
  474.           heights[target] = height;
  475.         }

  476.       /* For unconditional jumps with a successor, check that the
  477.          successor is a target, and pick up its stack height.  */
  478.       if (aop_goto == op - aop_map
  479.           && i + 3 < ax->len)
  480.         {
  481.           if (!targets[i + 3])
  482.             {
  483.               ax->flaw = agent_flaw_hole;
  484.               return;
  485.             }

  486.           height = heights[i + 3];
  487.         }

  488.       /* For reg instructions, record the register in the bit mask.  */
  489.       if (aop_reg == op - aop_map)
  490.         {
  491.           int reg = read_const (ax, i + 1, 2);

  492.           ax_reg_mask (ax, reg);
  493.         }
  494.     }

  495.   /* Check that all the targets are on boundaries.  */
  496.   for (i = 0; i < ax->len; i++)
  497.     if (targets[i] && !boundary[i])
  498.       {
  499.         ax->flaw = agent_flaw_bad_jump;
  500.         return;
  501.       }

  502.   ax->final_height = height;
  503. }