1 /* Functions for manipulating expressions designed to be executed on the agent
2 Copyright (C) 1998-2019 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 agent_expr::agent_expr (struct gdbarch
*gdbarch
, CORE_ADDR scope
)
43 this->size
= 1; /* Change this to a larger value once
44 reallocation code is tested. */
45 this->buf
= (unsigned char *) xmalloc (this->size
);
47 this->gdbarch
= gdbarch
;
50 /* Bit vector for registers used. */
51 this->reg_mask_len
= 1;
52 this->reg_mask
= XCNEWVEC (unsigned char, this->reg_mask_len
);
55 this->trace_string
= 0;
58 agent_expr::~agent_expr ()
61 xfree (this->reg_mask
);
64 /* Make sure that X has room for at least N more bytes. This doesn't
65 affect the length, just the allocated size. */
67 grow_expr (struct agent_expr
*x
, int n
)
69 if (x
->len
+ n
> x
->size
)
72 if (x
->size
< x
->len
+ n
)
73 x
->size
= x
->len
+ n
+ 10;
74 x
->buf
= (unsigned char *) xrealloc (x
->buf
, x
->size
);
79 /* Append the low N bytes of VAL as an N-byte integer to the
80 expression X, in big-endian order. */
82 append_const (struct agent_expr
*x
, LONGEST val
, int n
)
87 for (i
= n
- 1; i
>= 0; i
--)
89 x
->buf
[x
->len
+ i
] = val
& 0xff;
96 /* Extract an N-byte big-endian unsigned integer from expression X at
99 read_const (struct agent_expr
*x
, int o
, int n
)
104 /* Make sure we're not reading off the end of the expression. */
106 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
108 for (i
= 0; i
< n
; i
++)
109 accum
= (accum
<< 8) | x
->buf
[o
+ i
];
117 ax_raw_byte (struct agent_expr
*x
, gdb_byte byte
)
120 x
->buf
[x
->len
++] = byte
;
123 /* Append a simple operator OP to EXPR. */
125 ax_simple (struct agent_expr
*x
, enum agent_op op
)
130 /* Append a pick operator to EXPR. DEPTH is the stack item to pick,
131 with 0 being top of stack. */
134 ax_pick (struct agent_expr
*x
, int depth
)
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
);
143 /* Append a sign-extension or zero-extension instruction to EXPR, to
144 extend an N-bit value. */
146 generic_ext (struct agent_expr
*x
, enum agent_op op
, int n
)
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"));
157 x
->buf
[x
->len
++] = op
;
158 x
->buf
[x
->len
++] = n
;
162 /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
164 ax_ext (struct agent_expr
*x
, int n
)
166 generic_ext (x
, aop_ext
, n
);
170 /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
172 ax_zero_ext (struct agent_expr
*x
, int n
)
174 generic_ext (x
, aop_zero_ext
, n
);
178 /* Append a trace_quick instruction to EXPR, to record N bytes. */
180 ax_trace_quick (struct agent_expr
*x
, int n
)
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"));
188 x
->buf
[x
->len
++] = aop_trace_quick
;
189 x
->buf
[x
->len
++] = n
;
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. */
200 ax_goto (struct agent_expr
*x
, enum agent_op op
)
203 x
->buf
[x
->len
+ 0] = op
;
204 x
->buf
[x
->len
+ 1] = 0xff;
205 x
->buf
[x
->len
+ 2] = 0xff;
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. */
215 ax_label (struct agent_expr
*x
, int patch
, int target
)
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"));
222 x
->buf
[patch
] = (target
>> 8) & 0xff;
223 x
->buf
[patch
+ 1] = target
& 0xff;
227 /* Assemble code to push a constant on the stack. */
229 ax_const_l (struct agent_expr
*x
, LONGEST l
)
231 static enum agent_op ops
[]
233 {aop_const8
, aop_const16
, aop_const32
, aop_const64
};
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
++)
243 LONGEST lim
= ((LONGEST
) 1) << (size
- 1);
245 if (-lim
<= l
&& l
<= lim
- 1)
249 /* Emit the right opcode... */
250 ax_simple (x
, ops
[op
]);
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);
256 /* Now, if it was negative, and not full-sized, sign-extend it. */
257 if (l
< 0 && size
< 64)
263 ax_const_d (struct agent_expr
*x
, LONGEST d
)
265 /* FIXME: floating-point support not present yet. */
266 error (_("GDB bug: ax-general.c (ax_const_d): "
267 "floating point not supported yet"));
271 /* Assemble code to push the value of register number REG on the
274 ax_reg (struct agent_expr
*x
, int reg
)
276 if (reg
>= gdbarch_num_regs (x
->gdbarch
))
278 /* This is a pseudo-register. */
279 if (!gdbarch_ax_pseudo_register_push_stack_p (x
->gdbarch
))
280 error (_("'%s' is a pseudo-register; "
281 "GDB cannot yet trace its contents."),
282 user_reg_map_regnum_to_name (x
->gdbarch
, reg
));
283 if (gdbarch_ax_pseudo_register_push_stack (x
->gdbarch
, x
, reg
))
284 error (_("Trace '%s' failed."),
285 user_reg_map_regnum_to_name (x
->gdbarch
, reg
));
289 /* Get the remote register number. */
290 reg
= gdbarch_remote_register_number (x
->gdbarch
, reg
);
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"));
297 x
->buf
[x
->len
] = aop_reg
;
298 x
->buf
[x
->len
+ 1] = (reg
>> 8) & 0xff;
299 x
->buf
[x
->len
+ 2] = (reg
) & 0xff;
304 /* Assemble code to operate on a trace state variable. */
307 ax_tsv (struct agent_expr
*x
, enum agent_op op
, int num
)
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
);
317 x
->buf
[x
->len
+ 1] = (num
>> 8) & 0xff;
318 x
->buf
[x
->len
+ 2] = (num
) & 0xff;
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.) */
328 ax_string (struct agent_expr
*x
, const char *str
, int slen
)
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
);
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';
348 /* Functions for disassembling agent expressions, and otherwise
349 debugging the expression compiler. */
351 struct aop_map aop_map
[] =
354 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
355 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
356 #include "common/ax.def"
361 /* Disassemble the expression EXPR, writing to F. */
363 ax_print (struct ui_file
*f
, struct agent_expr
*x
)
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"));
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]))
377 error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
379 for (i
= 0; i
< x
->len
;)
381 enum agent_op op
= (enum agent_op
) x
->buf
[i
];
383 if (op
>= (sizeof (aop_map
) / sizeof (aop_map
[0]))
384 || !aop_map
[op
].name
)
386 fprintf_filtered (f
, _("%3d <bad opcode %02x>\n"), i
, op
);
390 if (i
+ 1 + aop_map
[op
].op_size
> x
->len
)
392 fprintf_filtered (f
, _("%3d <incomplete opcode %s>\n"),
393 i
, aop_map
[op
].name
);
397 fprintf_filtered (f
, "%3d %s", i
, aop_map
[op
].name
);
398 if (aop_map
[op
].op_size
> 0)
400 fputs_filtered (" ", f
);
402 print_longest (f
, 'd', 0,
403 read_const (x
, i
+ 1, aop_map
[op
].op_size
));
405 /* Handle the complicated printf arguments specially. */
406 else if (op
== aop_printf
)
413 slen
= slen
* 256 + x
->buf
[i
++];
414 fprintf_filtered (f
, _(" \"%s\", %d args"),
415 &(x
->buf
[i
]), nargs
);
418 fprintf_filtered (f
, "\n");
419 i
+= 1 + aop_map
[op
].op_size
;
423 /* Add register REG to the register mask for expression AX. */
425 ax_reg_mask (struct agent_expr
*ax
, int reg
)
427 if (reg
>= gdbarch_num_regs (ax
->gdbarch
))
429 /* This is a pseudo-register. */
430 if (!gdbarch_ax_pseudo_register_collect_p (ax
->gdbarch
))
431 error (_("'%s' is a pseudo-register; "
432 "GDB cannot yet trace its contents."),
433 user_reg_map_regnum_to_name (ax
->gdbarch
, reg
));
434 if (gdbarch_ax_pseudo_register_collect (ax
->gdbarch
, ax
, reg
))
435 error (_("Trace '%s' failed."),
436 user_reg_map_regnum_to_name (ax
->gdbarch
, reg
));
442 /* Get the remote register number. */
443 reg
= gdbarch_remote_register_number (ax
->gdbarch
, reg
);
446 /* Grow the bit mask if necessary. */
447 if (byte
>= ax
->reg_mask_len
)
449 /* It's not appropriate to double here. This isn't a
451 int new_len
= byte
+ 1;
452 unsigned char *new_reg_mask
453 = XRESIZEVEC (unsigned char, ax
->reg_mask
, new_len
);
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
;
461 ax
->reg_mask
[byte
] |= 1 << (reg
% 8);
465 /* Given an agent expression AX, fill in requirements and other descriptive
468 ax_reqs (struct agent_expr
*ax
)
473 /* Jump target table. targets[i] is non-zero iff we have found a
475 char *targets
= (char *) alloca (ax
->len
* sizeof (targets
[0]));
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]));
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]));
486 /* Pointer to a description of the present op. */
489 memset (targets
, 0, ax
->len
* sizeof (targets
[0]));
490 memset (boundary
, 0, ax
->len
* sizeof (boundary
[0]));
492 ax
->max_height
= ax
->min_height
= height
= 0;
493 ax
->flaw
= agent_flaw_none
;
494 ax
->max_data_size
= 0;
496 for (i
= 0; i
< ax
->len
; i
+= 1 + op
->op_size
)
498 if (ax
->buf
[i
] > (sizeof (aop_map
) / sizeof (aop_map
[0])))
500 ax
->flaw
= agent_flaw_bad_instruction
;
504 op
= &aop_map
[ax
->buf
[i
]];
508 ax
->flaw
= agent_flaw_bad_instruction
;
512 if (i
+ 1 + op
->op_size
> ax
->len
)
514 ax
->flaw
= agent_flaw_incomplete_instruction
;
518 /* If this instruction is a forward jump target, does the
519 current stack height match the stack height at the jump
521 if (targets
[i
] && (heights
[i
] != height
))
523 ax
->flaw
= agent_flaw_height_mismatch
;
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
;
537 if (op
->data_size
> ax
->max_data_size
)
538 ax
->max_data_size
= op
->data_size
;
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
)
546 int target
= read_const (ax
, i
+ 1, 2);
547 if (target
< 0 || target
>= ax
->len
)
549 ax
->flaw
= agent_flaw_bad_jump
;
553 /* Do we have any information about what the stack height
554 should be at the target? */
555 if (targets
[target
] || boundary
[target
])
557 if (heights
[target
] != height
)
559 ax
->flaw
= agent_flaw_height_mismatch
;
564 /* Record the target, along with the stack height we expect. */
566 heights
[target
] = height
;
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
576 ax
->flaw
= agent_flaw_hole
;
580 height
= heights
[i
+ 3];
583 /* For reg instructions, record the register in the bit mask. */
584 if (aop_reg
== op
- aop_map
)
586 int reg
= read_const (ax
, i
+ 1, 2);
588 ax_reg_mask (ax
, reg
);
592 /* Check that all the targets are on boundaries. */
593 for (i
= 0; i
< ax
->len
; i
++)
594 if (targets
[i
] && !boundary
[i
])
596 ax
->flaw
= agent_flaw_bad_jump
;
600 ax
->final_height
= height
;