1 /* Functions for manipulating expressions designed to be executed on the agent
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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.) */
28 #include "user-regs.h"
30 static void grow_expr (struct agent_expr *x, int n);
32 static void append_const (struct agent_expr *x, LONGEST val, int n);
34 static LONGEST read_const (struct agent_expr *x, int o, int n);
36 static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
38 /* Functions for building expressions. */
40 /* Allocate a new, empty agent expression. */
42 new_agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope)
44 struct agent_expr *x = XNEW (struct agent_expr);
47 x->size = 1; /* Change this to a larger value once
48 reallocation code is tested. */
49 x->buf = (unsigned char *) xmalloc (x->size);
54 /* Bit vector for registers used. */
56 x->reg_mask = XCNEWVEC (unsigned char, x->reg_mask_len);
64 /* Free a agent expression. */
66 free_agent_expr (struct agent_expr *x)
74 do_free_agent_expr_cleanup (void *x)
76 free_agent_expr ((struct agent_expr *) x);
80 make_cleanup_free_agent_expr (struct agent_expr *x)
82 return make_cleanup (do_free_agent_expr_cleanup, x);
86 /* Make sure that X has room for at least N more bytes. This doesn't
87 affect the length, just the allocated size. */
89 grow_expr (struct agent_expr *x, int n)
91 if (x->len + n > x->size)
94 if (x->size < x->len + n)
95 x->size = x->len + n + 10;
96 x->buf = (unsigned char *) xrealloc (x->buf, x->size);
101 /* Append the low N bytes of VAL as an N-byte integer to the
102 expression X, in big-endian order. */
104 append_const (struct agent_expr *x, LONGEST val, int n)
109 for (i = n - 1; i >= 0; i--)
111 x->buf[x->len + i] = val & 0xff;
118 /* Extract an N-byte big-endian unsigned integer from expression X at
121 read_const (struct agent_expr *x, int o, int n)
126 /* Make sure we're not reading off the end of the expression. */
128 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
130 for (i = 0; i < n; i++)
131 accum = (accum << 8) | x->buf[o + i];
139 ax_raw_byte (struct agent_expr *x, gdb_byte byte)
142 x->buf[x->len++] = byte;
145 /* Append a simple operator OP to EXPR. */
147 ax_simple (struct agent_expr *x, enum agent_op op)
152 /* Append a pick operator to EXPR. DEPTH is the stack item to pick,
153 with 0 being top of stack. */
156 ax_pick (struct agent_expr *x, int depth)
158 if (depth < 0 || depth > 255)
159 error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
160 ax_simple (x, aop_pick);
161 append_const (x, 1, depth);
165 /* Append a sign-extension or zero-extension instruction to EXPR, to
166 extend an N-bit value. */
168 generic_ext (struct agent_expr *x, enum agent_op op, int n)
170 /* N must fit in a byte. */
171 if (n < 0 || n > 255)
172 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
173 /* That had better be enough range. */
174 if (sizeof (LONGEST) * 8 > 255)
175 error (_("GDB bug: ax-general.c (generic_ext): "
176 "opcode has inadequate range"));
179 x->buf[x->len++] = op;
180 x->buf[x->len++] = n;
184 /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
186 ax_ext (struct agent_expr *x, int n)
188 generic_ext (x, aop_ext, n);
192 /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
194 ax_zero_ext (struct agent_expr *x, int n)
196 generic_ext (x, aop_zero_ext, n);
200 /* Append a trace_quick instruction to EXPR, to record N bytes. */
202 ax_trace_quick (struct agent_expr *x, int n)
204 /* N must fit in a byte. */
205 if (n < 0 || n > 255)
206 error (_("GDB bug: ax-general.c (ax_trace_quick): "
207 "size out of range for trace_quick"));
210 x->buf[x->len++] = aop_trace_quick;
211 x->buf[x->len++] = n;
215 /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or
216 aop_if_goto). We assume we don't know the target offset yet,
217 because it's probably a forward branch, so we leave space in EXPR
218 for the target, and return the offset in EXPR of that space, so we
219 can backpatch it once we do know the target offset. Use ax_label
220 to do the backpatching. */
222 ax_goto (struct agent_expr *x, enum agent_op op)
225 x->buf[x->len + 0] = op;
226 x->buf[x->len + 1] = 0xff;
227 x->buf[x->len + 2] = 0xff;
232 /* Suppose a given call to ax_goto returns some value PATCH. When you
233 know the offset TARGET that goto should jump to, call
234 ax_label (EXPR, PATCH, TARGET)
235 to patch TARGET into the ax_goto instruction. */
237 ax_label (struct agent_expr *x, int patch, int target)
239 /* Make sure the value is in range. Don't accept 0xffff as an
240 offset; that's our magic sentinel value for unpatched branches. */
241 if (target < 0 || target >= 0xffff)
242 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
244 x->buf[patch] = (target >> 8) & 0xff;
245 x->buf[patch + 1] = target & 0xff;
249 /* Assemble code to push a constant on the stack. */
251 ax_const_l (struct agent_expr *x, LONGEST l)
253 static enum agent_op ops[]
255 {aop_const8, aop_const16, aop_const32, aop_const64};
259 /* How big is the number? 'op' keeps track of which opcode to use.
260 Notice that we don't really care whether the original number was
261 signed or unsigned; we always reproduce the value exactly, and
262 use the shortest representation. */
263 for (op = 0, size = 8; size < 64; size *= 2, op++)
265 LONGEST lim = ((LONGEST) 1) << (size - 1);
267 if (-lim <= l && l <= lim - 1)
271 /* Emit the right opcode... */
272 ax_simple (x, ops[op]);
274 /* Emit the low SIZE bytes as an unsigned number. We know that
275 sign-extending this will yield l. */
276 append_const (x, l, size / 8);
278 /* Now, if it was negative, and not full-sized, sign-extend it. */
279 if (l < 0 && size < 64)
285 ax_const_d (struct agent_expr *x, LONGEST d)
287 /* FIXME: floating-point support not present yet. */
288 error (_("GDB bug: ax-general.c (ax_const_d): "
289 "floating point not supported yet"));
293 /* Assemble code to push the value of register number REG on the
296 ax_reg (struct agent_expr *x, int reg)
298 if (reg >= gdbarch_num_regs (x->gdbarch))
300 /* This is a pseudo-register. */
301 if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
302 error (_("'%s' is a pseudo-register; "
303 "GDB cannot yet trace its contents."),
304 user_reg_map_regnum_to_name (x->gdbarch, reg));
305 if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
306 error (_("Trace '%s' failed."),
307 user_reg_map_regnum_to_name (x->gdbarch, reg));
311 /* Make sure the register number is in range. */
312 if (reg < 0 || reg > 0xffff)
313 error (_("GDB bug: ax-general.c (ax_reg): "
314 "register number out of range"));
316 x->buf[x->len] = aop_reg;
317 x->buf[x->len + 1] = (reg >> 8) & 0xff;
318 x->buf[x->len + 2] = (reg) & 0xff;
323 /* Assemble code to operate on a trace state variable. */
326 ax_tsv (struct agent_expr *x, enum agent_op op, int num)
328 /* Make sure the tsv number is in range. */
329 if (num < 0 || num > 0xffff)
330 internal_error (__FILE__, __LINE__,
331 _("ax-general.c (ax_tsv): variable "
332 "number is %d, out of range"), num);
336 x->buf[x->len + 1] = (num >> 8) & 0xff;
337 x->buf[x->len + 2] = (num) & 0xff;
341 /* Append a string to the expression. Note that the string is going
342 into the bytecodes directly, not on the stack. As a precaution,
343 include both length as prefix, and terminate with a NUL. (The NUL
344 is counted in the length.) */
347 ax_string (struct agent_expr *x, const char *str, int slen)
351 /* Make sure the string length is reasonable. */
352 if (slen < 0 || slen > 0xffff)
353 internal_error (__FILE__, __LINE__,
354 _("ax-general.c (ax_string): string "
355 "length is %d, out of allowed range"), slen);
357 grow_expr (x, 2 + slen + 1);
358 x->buf[x->len++] = ((slen + 1) >> 8) & 0xff;
359 x->buf[x->len++] = (slen + 1) & 0xff;
360 for (i = 0; i < slen; ++i)
361 x->buf[x->len++] = str[i];
362 x->buf[x->len++] = '\0';
367 /* Functions for disassembling agent expressions, and otherwise
368 debugging the expression compiler. */
370 struct aop_map aop_map[] =
373 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
374 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
380 /* Disassemble the expression EXPR, writing to F. */
382 ax_print (struct ui_file *f, struct agent_expr *x)
386 fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
387 fprintf_filtered (f, _("Reg mask:"));
388 for (i = 0; i < x->reg_mask_len; ++i)
389 fprintf_filtered (f, _(" %02x"), x->reg_mask[i]);
390 fprintf_filtered (f, _("\n"));
392 /* Check the size of the name array against the number of entries in
393 the enum, to catch additions that people didn't sync. */
394 if ((sizeof (aop_map) / sizeof (aop_map[0]))
396 error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
398 for (i = 0; i < x->len;)
400 enum agent_op op = (enum agent_op) x->buf[i];
402 if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
403 || !aop_map[op].name)
405 fprintf_filtered (f, _("%3d <bad opcode %02x>\n"), i, op);
409 if (i + 1 + aop_map[op].op_size > x->len)
411 fprintf_filtered (f, _("%3d <incomplete opcode %s>\n"),
412 i, aop_map[op].name);
416 fprintf_filtered (f, "%3d %s", i, aop_map[op].name);
417 if (aop_map[op].op_size > 0)
419 fputs_filtered (" ", f);
421 print_longest (f, 'd', 0,
422 read_const (x, i + 1, aop_map[op].op_size));
424 /* Handle the complicated printf arguments specially. */
425 else if (op == aop_printf)
432 slen = slen * 256 + x->buf[i++];
433 fprintf_filtered (f, _(" \"%s\", %d args"),
434 &(x->buf[i]), nargs);
437 fprintf_filtered (f, "\n");
438 i += 1 + aop_map[op].op_size;
442 /* Add register REG to the register mask for expression AX. */
444 ax_reg_mask (struct agent_expr *ax, int reg)
446 if (reg >= gdbarch_num_regs (ax->gdbarch))
448 /* This is a pseudo-register. */
449 if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
450 error (_("'%s' is a pseudo-register; "
451 "GDB cannot yet trace its contents."),
452 user_reg_map_regnum_to_name (ax->gdbarch, reg));
453 if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
454 error (_("Trace '%s' failed."),
455 user_reg_map_regnum_to_name (ax->gdbarch, reg));
461 /* Grow the bit mask if necessary. */
462 if (byte >= ax->reg_mask_len)
464 /* It's not appropriate to double here. This isn't a
466 int new_len = byte + 1;
467 unsigned char *new_reg_mask
468 = XRESIZEVEC (unsigned char, ax->reg_mask, new_len);
470 memset (new_reg_mask + ax->reg_mask_len, 0,
471 (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0]));
472 ax->reg_mask_len = new_len;
473 ax->reg_mask = new_reg_mask;
476 ax->reg_mask[byte] |= 1 << (reg % 8);
480 /* Given an agent expression AX, fill in requirements and other descriptive
483 ax_reqs (struct agent_expr *ax)
488 /* Jump target table. targets[i] is non-zero iff we have found a
490 char *targets = (char *) alloca (ax->len * sizeof (targets[0]));
492 /* Instruction boundary table. boundary[i] is non-zero iff our scan
493 has reached an instruction starting at offset i. */
494 char *boundary = (char *) alloca (ax->len * sizeof (boundary[0]));
496 /* Stack height record. If either targets[i] or boundary[i] is
497 non-zero, heights[i] is the height the stack should have before
498 executing the bytecode at that point. */
499 int *heights = (int *) alloca (ax->len * sizeof (heights[0]));
501 /* Pointer to a description of the present op. */
504 memset (targets, 0, ax->len * sizeof (targets[0]));
505 memset (boundary, 0, ax->len * sizeof (boundary[0]));
507 ax->max_height = ax->min_height = height = 0;
508 ax->flaw = agent_flaw_none;
509 ax->max_data_size = 0;
511 for (i = 0; i < ax->len; i += 1 + op->op_size)
513 if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
515 ax->flaw = agent_flaw_bad_instruction;
519 op = &aop_map[ax->buf[i]];
523 ax->flaw = agent_flaw_bad_instruction;
527 if (i + 1 + op->op_size > ax->len)
529 ax->flaw = agent_flaw_incomplete_instruction;
533 /* If this instruction is a forward jump target, does the
534 current stack height match the stack height at the jump
536 if (targets[i] && (heights[i] != height))
538 ax->flaw = agent_flaw_height_mismatch;
545 height -= op->consumed;
546 if (height < ax->min_height)
547 ax->min_height = height;
548 height += op->produced;
549 if (height > ax->max_height)
550 ax->max_height = height;
552 if (op->data_size > ax->max_data_size)
553 ax->max_data_size = op->data_size;
555 /* For jump instructions, check that the target is a valid
556 offset. If it is, record the fact that that location is a
557 jump target, and record the height we expect there. */
558 if (aop_goto == op - aop_map
559 || aop_if_goto == op - aop_map)
561 int target = read_const (ax, i + 1, 2);
562 if (target < 0 || target >= ax->len)
564 ax->flaw = agent_flaw_bad_jump;
568 /* Do we have any information about what the stack height
569 should be at the target? */
570 if (targets[target] || boundary[target])
572 if (heights[target] != height)
574 ax->flaw = agent_flaw_height_mismatch;
579 /* Record the target, along with the stack height we expect. */
581 heights[target] = height;
584 /* For unconditional jumps with a successor, check that the
585 successor is a target, and pick up its stack height. */
586 if (aop_goto == op - aop_map
591 ax->flaw = agent_flaw_hole;
595 height = heights[i + 3];
598 /* For reg instructions, record the register in the bit mask. */
599 if (aop_reg == op - aop_map)
601 int reg = read_const (ax, i + 1, 2);
603 ax_reg_mask (ax, reg);
607 /* Check that all the targets are on boundaries. */
608 for (i = 0; i < ax->len; i++)
609 if (targets[i] && !boundary[i])
611 ax->flaw = agent_flaw_bad_jump;
615 ax->final_height = height;