GDB (xrefs)
/tmp/gdb-8.1/gdb/ax-general.c
Go to the documentation of this file.
1 /* Functions for manipulating expressions designed to be executed on the agent
2  Copyright (C) 1998-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 /* Despite what the above comment says about this file being part of
20  GDB, we would like to keep these functions free of GDB
21  dependencies, since we want to be able to use them in contexts
22  outside of GDB (test suites, the stub, etc.) */
23 
24 #include "defs.h"
25 #include "ax.h"
26 
27 #include "value.h"
28 #include "user-regs.h"
29 
30 static void grow_expr (struct agent_expr *x, int n);
31 
32 static void append_const (struct agent_expr *x, LONGEST val, int n);
33 
34 static LONGEST read_const (struct agent_expr *x, int o, int n);
35 
36 static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
37 
38 /* Functions for building expressions. */
39 
41 {
42  this->len = 0;
43  this->size = 1; /* Change this to a larger value once
44  reallocation code is tested. */
45  this->buf = (unsigned char *) xmalloc (this->size);
46 
47  this->gdbarch = gdbarch;
48  this->scope = scope;
49 
50  /* Bit vector for registers used. */
51  this->reg_mask_len = 1;
52  this->reg_mask = XCNEWVEC (unsigned char, this->reg_mask_len);
53 
54  this->tracing = 0;
55  this->trace_string = 0;
56 }
57 
59 {
60  xfree (this->buf);
61  xfree (this->reg_mask);
62 }
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 = (unsigned char *) xrealloc (x->buf, x->size);
75  }
76 }
77 
78 
79 /* Append the low N bytes of VAL as an N-byte integer to the
80  expression X, in big-endian order. */
81 static void
82 append_const (struct agent_expr *x, LONGEST val, int n)
83 {
84  int i;
85 
86  grow_expr (x, n);
87  for (i = n - 1; i >= 0; i--)
88  {
89  x->buf[x->len + i] = val & 0xff;
90  val >>= 8;
91  }
92  x->len += n;
93 }
94 
95 
96 /* Extract an N-byte big-endian unsigned integer from expression X at
97  offset O. */
98 static LONGEST
99 read_const (struct agent_expr *x, int o, int n)
100 {
101  int i;
102  LONGEST accum = 0;
103 
104  /* Make sure we're not reading off the end of the expression. */
105  if (o + n > x->len)
106  error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
107 
108  for (i = 0; i < n; i++)
109  accum = (accum << 8) | x->buf[o + i];
110 
111  return accum;
112 }
113 
114 /* See ax.h. */
115 
116 void
117 ax_raw_byte (struct agent_expr *x, gdb_byte byte)
118 {
119  grow_expr (x, 1);
120  x->buf[x->len++] = byte;
121 }
122 
123 /* Append a simple operator OP to EXPR. */
124 void
125 ax_simple (struct agent_expr *x, enum agent_op op)
126 {
127  ax_raw_byte (x, op);
128 }
129 
130 /* Append a pick operator to EXPR. DEPTH is the stack item to pick,
131  with 0 being top of stack. */
132 
133 void
134 ax_pick (struct agent_expr *x, int depth)
135 {
136  if (depth < 0 || depth > 255)
137  error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
138  ax_simple (x, aop_pick);
139  append_const (x, 1, depth);
140 }
141 
142 
143 /* Append a sign-extension or zero-extension instruction to EXPR, to
144  extend an N-bit value. */
145 static void
146 generic_ext (struct agent_expr *x, enum agent_op op, int n)
147 {
148  /* N must fit in a byte. */
149  if (n < 0 || n > 255)
150  error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
151  /* That had better be enough range. */
152  if (sizeof (LONGEST) * 8 > 255)
153  error (_("GDB bug: ax-general.c (generic_ext): "
154  "opcode has inadequate range"));
155 
156  grow_expr (x, 2);
157  x->buf[x->len++] = op;
158  x->buf[x->len++] = n;
159 }
160 
161 
162 /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
163 void
164 ax_ext (struct agent_expr *x, int n)
165 {
166  generic_ext (x, aop_ext, n);
167 }
168 
169 
170 /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
171 void
172 ax_zero_ext (struct agent_expr *x, int n)
173 {
174  generic_ext (x, aop_zero_ext, n);
175 }
176 
177 
178 /* Append a trace_quick instruction to EXPR, to record N bytes. */
179 void
180 ax_trace_quick (struct agent_expr *x, int n)
181 {
182  /* N must fit in a byte. */
183  if (n < 0 || n > 255)
184  error (_("GDB bug: ax-general.c (ax_trace_quick): "
185  "size out of range for trace_quick"));
186 
187  grow_expr (x, 2);
188  x->buf[x->len++] = aop_trace_quick;
189  x->buf[x->len++] = n;
190 }
191 
192 
193 /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or
194  aop_if_goto). We assume we don't know the target offset yet,
195  because it's probably a forward branch, so we leave space in EXPR
196  for the target, and return the offset in EXPR of that space, so we
197  can backpatch it once we do know the target offset. Use ax_label
198  to do the backpatching. */
199 int
200 ax_goto (struct agent_expr *x, enum agent_op op)
201 {
202  grow_expr (x, 3);
203  x->buf[x->len + 0] = op;
204  x->buf[x->len + 1] = 0xff;
205  x->buf[x->len + 2] = 0xff;
206  x->len += 3;
207  return x->len - 2;
208 }
209 
210 /* Suppose a given call to ax_goto returns some value PATCH. When you
211  know the offset TARGET that goto should jump to, call
212  ax_label (EXPR, PATCH, TARGET)
213  to patch TARGET into the ax_goto instruction. */
214 void
215 ax_label (struct agent_expr *x, int patch, int target)
216 {
217  /* Make sure the value is in range. Don't accept 0xffff as an
218  offset; that's our magic sentinel value for unpatched branches. */
219  if (target < 0 || target >= 0xffff)
220  error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
221 
222  x->buf[patch] = (target >> 8) & 0xff;
223  x->buf[patch + 1] = target & 0xff;
224 }
225 
226 
227 /* Assemble code to push a constant on the stack. */
228 void
230 {
231  static enum agent_op ops[]
232  =
233  {aop_const8, aop_const16, aop_const32, aop_const64};
234  int size;
235  int op;
236 
237  /* How big is the number? 'op' keeps track of which opcode to use.
238  Notice that we don't really care whether the original number was
239  signed or unsigned; we always reproduce the value exactly, and
240  use the shortest representation. */
241  for (op = 0, size = 8; size < 64; size *= 2, op++)
242  {
243  LONGEST lim = ((LONGEST) 1) << (size - 1);
244 
245  if (-lim <= l && l <= lim - 1)
246  break;
247  }
248 
249  /* Emit the right opcode... */
250  ax_simple (x, ops[op]);
251 
252  /* Emit the low SIZE bytes as an unsigned number. We know that
253  sign-extending this will yield l. */
254  append_const (x, l, size / 8);
255 
256  /* Now, if it was negative, and not full-sized, sign-extend it. */
257  if (l < 0 && size < 64)
258  ax_ext (x, size);
259 }
260 
261 
262 void
264 {
265  /* FIXME: floating-point support not present yet. */
266  error (_("GDB bug: ax-general.c (ax_const_d): "
267  "floating point not supported yet"));
268 }
269 
270 
271 /* Assemble code to push the value of register number REG on the
272  stack. */
273 void
274 ax_reg (struct agent_expr *x, int reg)
275 {
276  if (reg >= gdbarch_num_regs (x->gdbarch))
277  {
278  /* This is a pseudo-register. */
280  error (_("'%s' is a pseudo-register; "
281  "GDB cannot yet trace its contents."),
284  error (_("Trace '%s' failed."),
286  }
287  else
288  {
289  /* Get the remote register number. */
291 
292  /* Make sure the register number is in range. */
293  if (reg < 0 || reg > 0xffff)
294  error (_("GDB bug: ax-general.c (ax_reg): "
295  "register number out of range"));
296  grow_expr (x, 3);
297  x->buf[x->len] = aop_reg;
298  x->buf[x->len + 1] = (reg >> 8) & 0xff;
299  x->buf[x->len + 2] = (reg) & 0xff;
300  x->len += 3;
301  }
302 }
303 
304 /* Assemble code to operate on a trace state variable. */
305 
306 void
307 ax_tsv (struct agent_expr *x, enum agent_op op, int num)
308 {
309  /* Make sure the tsv number is in range. */
310  if (num < 0 || num > 0xffff)
311  internal_error (__FILE__, __LINE__,
312  _("ax-general.c (ax_tsv): variable "
313  "number is %d, out of range"), num);
314 
315  grow_expr (x, 3);
316  x->buf[x->len] = op;
317  x->buf[x->len + 1] = (num >> 8) & 0xff;
318  x->buf[x->len + 2] = (num) & 0xff;
319  x->len += 3;
320 }
321 
322 /* Append a string to the expression. Note that the string is going
323  into the bytecodes directly, not on the stack. As a precaution,
324  include both length as prefix, and terminate with a NUL. (The NUL
325  is counted in the length.) */
326 
327 void
328 ax_string (struct agent_expr *x, const char *str, int slen)
329 {
330  int i;
331 
332  /* Make sure the string length is reasonable. */
333  if (slen < 0 || slen > 0xffff)
334  internal_error (__FILE__, __LINE__,
335  _("ax-general.c (ax_string): string "
336  "length is %d, out of allowed range"), slen);
337 
338  grow_expr (x, 2 + slen + 1);
339  x->buf[x->len++] = ((slen + 1) >> 8) & 0xff;
340  x->buf[x->len++] = (slen + 1) & 0xff;
341  for (i = 0; i < slen; ++i)
342  x->buf[x->len++] = str[i];
343  x->buf[x->len++] = '\0';
344 }
345 
346 
347 
348 /* Functions for disassembling agent expressions, and otherwise
349  debugging the expression compiler. */
350 
351 struct aop_map aop_map[] =
352 {
353  {0, 0, 0, 0, 0}
354 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
355  , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
356 #include "ax.def"
357 #undef DEFOP
358 };
359 
360 
361 /* Disassemble the expression EXPR, writing to F. */
362 void
363 ax_print (struct ui_file *f, struct agent_expr *x)
364 {
365  int i;
366 
367  fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
368  fprintf_filtered (f, _("Reg mask:"));
369  for (i = 0; i < x->reg_mask_len; ++i)
370  fprintf_filtered (f, _(" %02x"), x->reg_mask[i]);
371  fprintf_filtered (f, _("\n"));
372 
373  /* Check the size of the name array against the number of entries in
374  the enum, to catch additions that people didn't sync. */
375  if ((sizeof (aop_map) / sizeof (aop_map[0]))
376  != aop_last)
377  error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
378 
379  for (i = 0; i < x->len;)
380  {
381  enum agent_op op = (enum agent_op) x->buf[i];
382 
383  if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
384  || !aop_map[op].name)
385  {
386  fprintf_filtered (f, _("%3d <bad opcode %02x>\n"), i, op);
387  i++;
388  continue;
389  }
390  if (i + 1 + aop_map[op].op_size > x->len)
391  {
392  fprintf_filtered (f, _("%3d <incomplete opcode %s>\n"),
393  i, aop_map[op].name);
394  break;
395  }
396 
397  fprintf_filtered (f, "%3d %s", i, aop_map[op].name);
398  if (aop_map[op].op_size > 0)
399  {
400  fputs_filtered (" ", f);
401 
402  print_longest (f, 'd', 0,
403  read_const (x, i + 1, aop_map[op].op_size));
404  }
405  /* Handle the complicated printf arguments specially. */
406  else if (op == aop_printf)
407  {
408  int slen, nargs;
409 
410  i++;
411  nargs = x->buf[i++];
412  slen = x->buf[i++];
413  slen = slen * 256 + x->buf[i++];
414  fprintf_filtered (f, _(" \"%s\", %d args"),
415  &(x->buf[i]), nargs);
416  i += slen - 1;
417  }
418  fprintf_filtered (f, "\n");
419  i += 1 + aop_map[op].op_size;
420  }
421 }
422 
423 /* Add register REG to the register mask for expression AX. */
424 void
425 ax_reg_mask (struct agent_expr *ax, int reg)
426 {
427  if (reg >= gdbarch_num_regs (ax->gdbarch))
428  {
429  /* This is a pseudo-register. */
431  error (_("'%s' is a pseudo-register; "
432  "GDB cannot yet trace its contents."),
435  error (_("Trace '%s' failed."),
437  }
438  else
439  {
440  int byte;
441 
442  /* Get the remote register number. */
444  byte = reg / 8;
445 
446  /* Grow the bit mask if necessary. */
447  if (byte >= ax->reg_mask_len)
448  {
449  /* It's not appropriate to double here. This isn't a
450  string buffer. */
451  int new_len = byte + 1;
452  unsigned char *new_reg_mask
453  = XRESIZEVEC (unsigned char, ax->reg_mask, new_len);
454 
455  memset (new_reg_mask + ax->reg_mask_len, 0,
456  (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0]));
457  ax->reg_mask_len = new_len;
458  ax->reg_mask = new_reg_mask;
459  }
460 
461  ax->reg_mask[byte] |= 1 << (reg % 8);
462  }
463 }
464 
465 /* Given an agent expression AX, fill in requirements and other descriptive
466  bits. */
467 void
468 ax_reqs (struct agent_expr *ax)
469 {
470  int i;
471  int height;
472 
473  /* Jump target table. targets[i] is non-zero iff we have found a
474  jump to offset i. */
475  char *targets = (char *) alloca (ax->len * sizeof (targets[0]));
476 
477  /* Instruction boundary table. boundary[i] is non-zero iff our scan
478  has reached an instruction starting at offset i. */
479  char *boundary = (char *) alloca (ax->len * sizeof (boundary[0]));
480 
481  /* Stack height record. If either targets[i] or boundary[i] is
482  non-zero, heights[i] is the height the stack should have before
483  executing the bytecode at that point. */
484  int *heights = (int *) alloca (ax->len * sizeof (heights[0]));
485 
486  /* Pointer to a description of the present op. */
487  struct aop_map *op;
488 
489  memset (targets, 0, ax->len * sizeof (targets[0]));
490  memset (boundary, 0, ax->len * sizeof (boundary[0]));
491 
492  ax->max_height = ax->min_height = height = 0;
493  ax->flaw = agent_flaw_none;
494  ax->max_data_size = 0;
495 
496  for (i = 0; i < ax->len; i += 1 + op->op_size)
497  {
498  if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
499  {
501  return;
502  }
503 
504  op = &aop_map[ax->buf[i]];
505 
506  if (!op->name)
507  {
509  return;
510  }
511 
512  if (i + 1 + op->op_size > ax->len)
513  {
515  return;
516  }
517 
518  /* If this instruction is a forward jump target, does the
519  current stack height match the stack height at the jump
520  source? */
521  if (targets[i] && (heights[i] != height))
522  {
524  return;
525  }
526 
527  boundary[i] = 1;
528  heights[i] = height;
529 
530  height -= op->consumed;
531  if (height < ax->min_height)
532  ax->min_height = height;
533  height += op->produced;
534  if (height > ax->max_height)
535  ax->max_height = height;
536 
537  if (op->data_size > ax->max_data_size)
538  ax->max_data_size = op->data_size;
539 
540  /* For jump instructions, check that the target is a valid
541  offset. If it is, record the fact that that location is a
542  jump target, and record the height we expect there. */
543  if (aop_goto == op - aop_map
544  || aop_if_goto == op - aop_map)
545  {
546  int target = read_const (ax, i + 1, 2);
547  if (target < 0 || target >= ax->len)
548  {
550  return;
551  }
552 
553  /* Do we have any information about what the stack height
554  should be at the target? */
555  if (targets[target] || boundary[target])
556  {
557  if (heights[target] != height)
558  {
560  return;
561  }
562  }
563 
564  /* Record the target, along with the stack height we expect. */
565  targets[target] = 1;
566  heights[target] = height;
567  }
568 
569  /* For unconditional jumps with a successor, check that the
570  successor is a target, and pick up its stack height. */
571  if (aop_goto == op - aop_map
572  && i + 3 < ax->len)
573  {
574  if (!targets[i + 3])
575  {
576  ax->flaw = agent_flaw_hole;
577  return;
578  }
579 
580  height = heights[i + 3];
581  }
582 
583  /* For reg instructions, record the register in the bit mask. */
584  if (aop_reg == op - aop_map)
585  {
586  int reg = read_const (ax, i + 1, 2);
587 
588  ax_reg_mask (ax, reg);
589  }
590  }
591 
592  /* Check that all the targets are on boundaries. */
593  for (i = 0; i < ax->len; i++)
594  if (targets[i] && !boundary[i])
595  {
597  return;
598  }
599 
600  ax->final_height = height;
601 }
int final_height
Definition: ax.h:111
const char * user_reg_map_regnum_to_name(struct gdbarch *gdbarch, int regnum)
Definition: user-regs.c:193
int ax_goto(struct agent_expr *x, enum agent_op op)
Definition: ax-general.c:200
bfd_vma CORE_ADDR
Definition: common-types.h:41
void ax_reqs(struct agent_expr *ax)
Definition: ax-general.c:468
void ax_reg_mask(struct agent_expr *ax, int reg)
Definition: ax-general.c:425
void xfree(void *)
if(!(yy_init))
Definition: ada-lex.c:1075
unsigned char * reg_mask
Definition: ax.h:138
void * memset(T *s, int c, size_t n)=delete
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
void ax_const_d(struct agent_expr *x, LONGEST d)
Definition: ax-general.c:263
Definition: ax.h:267
Definition: ax.h:83
static void generic_ext(struct agent_expr *x, enum agent_op op, int n)
Definition: ax-general.c:146
int produced
Definition: ax.h:260
void ax_reg(struct agent_expr *x, int reg)
Definition: ax-general.c:274
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:2039
int max_height
Definition: ax.h:114
struct gdbarch * gdbarch
Definition: ax.h:100
#define _(String)
Definition: gdb_locale.h:35
agent_expr(struct gdbarch *gdbarch, CORE_ADDR scope)
Definition: ax-general.c:40
enum agent_flaws flaw
Definition: ax.h:107
int trace_string
Definition: ax.h:155
void ax_tsv(struct agent_expr *x, enum agent_op op, int num)
Definition: ax-general.c:307
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: utils.c:2745
static LONGEST read_const(struct agent_expr *x, int o, int n)
Definition: ax-general.c:99
#define XCNEWVEC(T, N)
Definition: poison.h:157
const char *const name
Definition: aarch64-tdep.c:76
Definition: ax.h:237
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
unsigned int tracing
Definition: ax.h:149
static void grow_expr(struct agent_expr *x, int n)
Definition: ax-general.c:67
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1811
void ax_ext(struct agent_expr *x, int n)
Definition: ax-general.c:164
void ax_string(struct agent_expr *x, const char *str, int slen)
Definition: ax-general.c:328
void ax_label(struct agent_expr *x, int patch, int target)
Definition: ax-general.c:215
agent_op
Definition: ax.h:163
void ax_simple(struct agent_expr *x, enum agent_op op)
Definition: ax-general.c:125
void * xmalloc(YYSIZE_T)
#define XRESIZEVEC(T, P, N)
Definition: poison.h:169
int min_height
Definition: ax.h:114
Definition: regdef.h:22
int gdbarch_ax_pseudo_register_push_stack_p(struct gdbarch *gdbarch)
Definition: gdbarch.c:2098
PTR xrealloc(PTR ptr, size_t size)
Definition: common-utils.c:52
void ax_print(struct ui_file *f, struct agent_expr *x)
Definition: ax-general.c:363
bfd_byte gdb_byte
Definition: common-types.h:38
void print_longest(struct ui_file *stream, int format, int use_c_format, LONGEST val_long)
Definition: valprint.c:1303
int data_size
Definition: ax.h:257
const char * name
Definition: ax.h:242
int len
Definition: ax.h:94
int op_size
Definition: ax.h:253
int gdbarch_ax_pseudo_register_push_stack(struct gdbarch *gdbarch, struct agent_expr *ax, int reg)
Definition: gdbarch.c:2105
static void append_const(struct agent_expr *x, LONGEST val, int n)
Definition: ax-general.c:82
void ax_trace_quick(struct agent_expr *x, int n)
Definition: ax-general.c:180
void ax_const_l(struct agent_expr *x, LONGEST l)
Definition: ax-general.c:229
void ax_pick(struct agent_expr *x, int depth)
Definition: ax-general.c:134
unsigned char * buf
Definition: ax.h:91
int gdbarch_ax_pseudo_register_collect_p(struct gdbarch *gdbarch)
Definition: gdbarch.c:2074
void ax_zero_ext(struct agent_expr *x, int n)
Definition: ax-general.c:172
int reg_mask_len
Definition: ax.h:137
int consumed
Definition: ax.h:260
int gdbarch_ax_pseudo_register_collect(struct gdbarch *gdbarch, struct agent_expr *ax, int reg)
Definition: gdbarch.c:2081
void error(const char *fmt,...)
Definition: errors.c:38
void ax_raw_byte(struct agent_expr *x, gdb_byte byte)
Definition: ax-general.c:117
size_t size
Definition: go32-nat.c:242
int max_data_size
Definition: ax.h:118
int gdbarch_remote_register_number(struct gdbarch *gdbarch, int regno)
Definition: gdbarch.c:3004
long long LONGEST
Definition: common-types.h:52
CORE_ADDR scope
Definition: ax.h:103
int size
Definition: ax.h:97