1 /* Functions for manipulating expressions designed to be executed on the agent
2 Copyright (C) 1998-2024 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 append_const (struct agent_expr
*x
, LONGEST val
, int n
);
32 static LONGEST
read_const (struct agent_expr
*x
, int o
, int n
);
34 static void generic_ext (struct agent_expr
*x
, enum agent_op op
, int n
);
36 /* Functions for building expressions. */
38 /* Append the low N bytes of VAL as an N-byte integer to the
39 expression X, in big-endian order. */
41 append_const (struct agent_expr
*x
, LONGEST val
, int n
)
43 size_t len
= x
->buf
.size ();
44 x
->buf
.resize (len
+ n
);
45 for (int i
= n
- 1; i
>= 0; i
--)
47 x
->buf
[len
+ i
] = val
& 0xff;
53 /* Extract an N-byte big-endian unsigned integer from expression X at
56 read_const (struct agent_expr
*x
, int o
, int n
)
61 /* Make sure we're not reading off the end of the expression. */
62 if (o
+ n
> x
->buf
.size ())
63 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
65 for (i
= 0; i
< n
; i
++)
66 accum
= (accum
<< 8) | x
->buf
[o
+ i
];
74 ax_raw_byte (struct agent_expr
*x
, gdb_byte byte
)
76 x
->buf
.push_back (byte
);
79 /* Append a simple operator OP to EXPR. */
81 ax_simple (struct agent_expr
*x
, enum agent_op op
)
86 /* Append a pick operator to EXPR. DEPTH is the stack item to pick,
87 with 0 being top of stack. */
90 ax_pick (struct agent_expr
*x
, int depth
)
92 if (depth
< 0 || depth
> 255)
93 error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
94 ax_simple (x
, aop_pick
);
95 append_const (x
, 1, depth
);
99 /* Append a sign-extension or zero-extension instruction to EXPR, to
100 extend an N-bit value. */
102 generic_ext (struct agent_expr
*x
, enum agent_op op
, int n
)
104 /* N must fit in a byte. */
105 if (n
< 0 || n
> 255)
106 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
107 /* That had better be enough range. */
108 if (sizeof (LONGEST
) * 8 > 255)
109 error (_("GDB bug: ax-general.c (generic_ext): "
110 "opcode has inadequate range"));
112 x
->buf
.push_back (op
);
113 x
->buf
.push_back (n
);
117 /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
119 ax_ext (struct agent_expr
*x
, int n
)
121 generic_ext (x
, aop_ext
, n
);
125 /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
127 ax_zero_ext (struct agent_expr
*x
, int n
)
129 generic_ext (x
, aop_zero_ext
, n
);
133 /* Append a trace_quick instruction to EXPR, to record N bytes. */
135 ax_trace_quick (struct agent_expr
*x
, int n
)
137 /* N must fit in a byte. */
138 if (n
< 0 || n
> 255)
139 error (_("GDB bug: ax-general.c (ax_trace_quick): "
140 "size out of range for trace_quick"));
142 x
->buf
.push_back (aop_trace_quick
);
143 x
->buf
.push_back (n
);
147 /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or
148 aop_if_goto). We assume we don't know the target offset yet,
149 because it's probably a forward branch, so we leave space in EXPR
150 for the target, and return the offset in EXPR of that space, so we
151 can backpatch it once we do know the target offset. Use ax_label
152 to do the backpatching. */
154 ax_goto (struct agent_expr
*x
, enum agent_op op
)
156 x
->buf
.push_back (op
);
157 x
->buf
.push_back (0xff);
158 x
->buf
.push_back (0xff);
159 return x
->buf
.size () - 2;
162 /* Suppose a given call to ax_goto returns some value PATCH. When you
163 know the offset TARGET that goto should jump to, call
164 ax_label (EXPR, PATCH, TARGET)
165 to patch TARGET into the ax_goto instruction. */
167 ax_label (struct agent_expr
*x
, int patch
, int target
)
169 /* Make sure the value is in range. Don't accept 0xffff as an
170 offset; that's our magic sentinel value for unpatched branches. */
171 if (target
< 0 || target
>= 0xffff)
172 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
174 x
->buf
[patch
] = (target
>> 8) & 0xff;
175 x
->buf
[patch
+ 1] = target
& 0xff;
179 /* Assemble code to push a constant on the stack. */
181 ax_const_l (struct agent_expr
*x
, LONGEST l
)
183 static enum agent_op ops
[]
185 {aop_const8
, aop_const16
, aop_const32
, aop_const64
};
189 /* How big is the number? 'op' keeps track of which opcode to use.
190 Notice that we don't really care whether the original number was
191 signed or unsigned; we always reproduce the value exactly, and
192 use the shortest representation. */
193 for (op
= 0, size
= 8; size
< 64; size
*= 2, op
++)
195 LONGEST lim
= ((LONGEST
) 1) << (size
- 1);
197 if (-lim
<= l
&& l
<= lim
- 1)
201 /* Emit the right opcode... */
202 ax_simple (x
, ops
[op
]);
204 /* Emit the low SIZE bytes as an unsigned number. We know that
205 sign-extending this will yield l. */
206 append_const (x
, l
, size
/ 8);
208 /* Now, if it was negative, and not full-sized, sign-extend it. */
209 if (l
< 0 && size
< 64)
215 ax_const_d (struct agent_expr
*x
, LONGEST d
)
217 /* FIXME: floating-point support not present yet. */
218 error (_("GDB bug: ax-general.c (ax_const_d): "
219 "floating point not supported yet"));
223 /* Assemble code to push the value of register number REG on the
226 ax_reg (struct agent_expr
*x
, int reg
)
228 if (reg
>= gdbarch_num_regs (x
->gdbarch
))
230 /* This is a pseudo-register. */
231 if (!gdbarch_ax_pseudo_register_push_stack_p (x
->gdbarch
))
232 error (_("'%s' is a pseudo-register; "
233 "GDB cannot yet trace its contents."),
234 user_reg_map_regnum_to_name (x
->gdbarch
, reg
));
235 if (gdbarch_ax_pseudo_register_push_stack (x
->gdbarch
, x
, reg
))
236 error (_("Trace '%s' failed."),
237 user_reg_map_regnum_to_name (x
->gdbarch
, reg
));
241 /* Get the remote register number. */
242 reg
= gdbarch_remote_register_number (x
->gdbarch
, reg
);
244 /* Make sure the register number is in range. */
245 if (reg
< 0 || reg
> 0xffff)
246 error (_("GDB bug: ax-general.c (ax_reg): "
247 "register number out of range"));
248 x
->buf
.push_back (aop_reg
);
249 x
->buf
.push_back ((reg
>> 8) & 0xff);
250 x
->buf
.push_back ((reg
) & 0xff);
254 /* Assemble code to operate on a trace state variable. */
257 ax_tsv (struct agent_expr
*x
, enum agent_op op
, int num
)
259 /* Make sure the tsv number is in range. */
260 if (num
< 0 || num
> 0xffff)
261 internal_error (_("ax-general.c (ax_tsv): variable "
262 "number is %d, out of range"), num
);
264 x
->buf
.push_back (op
);
265 x
->buf
.push_back ((num
>> 8) & 0xff);
266 x
->buf
.push_back ((num
) & 0xff);
269 /* Append a string to the expression. Note that the string is going
270 into the bytecodes directly, not on the stack. As a precaution,
271 include both length as prefix, and terminate with a NUL. (The NUL
272 is counted in the length.) */
275 ax_string (struct agent_expr
*x
, const char *str
, int slen
)
279 /* Make sure the string length is reasonable. */
280 if (slen
< 0 || slen
> 0xffff)
281 internal_error (_("ax-general.c (ax_string): string "
282 "length is %d, out of allowed range"), slen
);
284 x
->buf
.push_back (((slen
+ 1) >> 8) & 0xff);
285 x
->buf
.push_back ((slen
+ 1) & 0xff);
286 for (i
= 0; i
< slen
; ++i
)
287 x
->buf
.push_back (str
[i
]);
288 x
->buf
.push_back ('\0');
293 /* Functions for disassembling agent expressions, and otherwise
294 debugging the expression compiler. */
296 /* An entry in the opcode map. */
300 /* The name of the opcode. Null means that this entry is not a
301 valid opcode --- a hole in the opcode space. */
304 /* All opcodes take no operands from the bytecode stream, or take
305 unsigned integers of various sizes. If this is a positive number
306 n, then the opcode is followed by an n-byte operand, which should
307 be printed as an unsigned integer. If this is zero, then the
308 opcode takes no operands from the bytecode stream.
310 If we get more complicated opcodes in the future, don't add other
311 magic values of this; that's a crock. Add an `enum encoding'
312 field to this, or something like that. */
315 /* The size of the data operated upon, in bits, for bytecodes that
316 care about that (ref and const). Zero for all others. */
319 /* Number of stack elements consumed, and number produced. */
320 int consumed
, produced
;
323 /* Map of the bytecodes, indexed by bytecode number. */
325 static struct aop_map aop_map
[] =
328 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
329 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
330 #include "gdbsupport/ax.def"
335 /* Disassemble the expression EXPR, writing to F. */
337 ax_print (struct ui_file
*f
, struct agent_expr
*x
)
341 gdb_printf (f
, _("Scope: %s\n"), paddress (x
->gdbarch
, x
->scope
));
342 gdb_printf (f
, _("Reg mask:"));
343 for (i
= 0; i
< x
->reg_mask
.size (); ++i
)
347 gdb_printf (f
, _("%d"), (int) x
->reg_mask
[i
]);
349 gdb_printf (f
, _("\n"));
351 for (i
= 0; i
< x
->buf
.size ();)
353 enum agent_op op
= (enum agent_op
) x
->buf
[i
];
355 if (op
>= ARRAY_SIZE (aop_map
) || aop_map
[op
].name
== nullptr)
357 gdb_printf (f
, _("%3d <bad opcode %02x>\n"), i
, op
);
361 if (i
+ 1 + aop_map
[op
].op_size
> x
->buf
.size ())
363 gdb_printf (f
, _("%3d <incomplete opcode %s>\n"),
364 i
, aop_map
[op
].name
);
368 gdb_printf (f
, "%3d %s", i
, aop_map
[op
].name
);
369 if (aop_map
[op
].op_size
> 0)
373 print_longest (f
, 'd', 0,
374 read_const (x
, i
+ 1, aop_map
[op
].op_size
));
376 /* Handle the complicated printf arguments specially. */
377 else if (op
== aop_printf
)
384 slen
= slen
* 256 + x
->buf
[i
++];
385 gdb_printf (f
, _(" \"%s\", %d args"),
386 &(x
->buf
[i
]), nargs
);
389 gdb_printf (f
, "\n");
390 i
+= 1 + aop_map
[op
].op_size
;
394 /* Add register REG to the register mask for expression AX. */
396 ax_reg_mask (struct agent_expr
*ax
, int reg
)
398 if (reg
>= gdbarch_num_regs (ax
->gdbarch
))
400 /* This is a pseudo-register. */
401 if (!gdbarch_ax_pseudo_register_collect_p (ax
->gdbarch
))
402 error (_("'%s' is a pseudo-register; "
403 "GDB cannot yet trace its contents."),
404 user_reg_map_regnum_to_name (ax
->gdbarch
, reg
));
405 if (gdbarch_ax_pseudo_register_collect (ax
->gdbarch
, ax
, reg
))
406 error (_("Trace '%s' failed."),
407 user_reg_map_regnum_to_name (ax
->gdbarch
, reg
));
411 /* Get the remote register number. */
412 reg
= gdbarch_remote_register_number (ax
->gdbarch
, reg
);
414 /* Grow the bit mask if necessary. */
415 if (reg
>= ax
->reg_mask
.size ())
416 ax
->reg_mask
.resize (reg
+ 1);
418 ax
->reg_mask
[reg
] = true;
422 /* Given an agent expression AX, fill in requirements and other descriptive
425 ax_reqs (struct agent_expr
*ax
)
430 /* Jump target table. targets[i] is non-zero iff we have found a
432 char *targets
= (char *) alloca (ax
->buf
.size () * sizeof (targets
[0]));
434 /* Instruction boundary table. boundary[i] is non-zero iff our scan
435 has reached an instruction starting at offset i. */
436 char *boundary
= (char *) alloca (ax
->buf
.size () * sizeof (boundary
[0]));
438 /* Stack height record. If either targets[i] or boundary[i] is
439 non-zero, heights[i] is the height the stack should have before
440 executing the bytecode at that point. */
441 int *heights
= (int *) alloca (ax
->buf
.size () * sizeof (heights
[0]));
443 /* Pointer to a description of the present op. */
446 memset (targets
, 0, ax
->buf
.size () * sizeof (targets
[0]));
447 memset (boundary
, 0, ax
->buf
.size () * sizeof (boundary
[0]));
449 ax
->max_height
= ax
->min_height
= height
= 0;
450 ax
->flaw
= agent_flaw_none
;
451 ax
->max_data_size
= 0;
453 for (i
= 0; i
< ax
->buf
.size (); i
+= 1 + op
->op_size
)
455 if (ax
->buf
[i
] >= ARRAY_SIZE (aop_map
))
457 ax
->flaw
= agent_flaw_bad_instruction
;
461 op
= &aop_map
[ax
->buf
[i
]];
465 ax
->flaw
= agent_flaw_bad_instruction
;
469 if (i
+ 1 + op
->op_size
> ax
->buf
.size ())
471 ax
->flaw
= agent_flaw_incomplete_instruction
;
475 /* If this instruction is a forward jump target, does the
476 current stack height match the stack height at the jump
478 if (targets
[i
] && (heights
[i
] != height
))
480 ax
->flaw
= agent_flaw_height_mismatch
;
487 height
-= op
->consumed
;
488 if (height
< ax
->min_height
)
489 ax
->min_height
= height
;
490 height
+= op
->produced
;
491 if (height
> ax
->max_height
)
492 ax
->max_height
= height
;
494 if (op
->data_size
> ax
->max_data_size
)
495 ax
->max_data_size
= op
->data_size
;
497 /* For jump instructions, check that the target is a valid
498 offset. If it is, record the fact that that location is a
499 jump target, and record the height we expect there. */
500 if (aop_goto
== op
- aop_map
501 || aop_if_goto
== op
- aop_map
)
503 int target
= read_const (ax
, i
+ 1, 2);
504 if (target
< 0 || target
>= ax
->buf
.size ())
506 ax
->flaw
= agent_flaw_bad_jump
;
510 /* Do we have any information about what the stack height
511 should be at the target? */
512 if (targets
[target
] || boundary
[target
])
514 if (heights
[target
] != height
)
516 ax
->flaw
= agent_flaw_height_mismatch
;
521 /* Record the target, along with the stack height we expect. */
523 heights
[target
] = height
;
526 /* For unconditional jumps with a successor, check that the
527 successor is a target, and pick up its stack height. */
528 if (aop_goto
== op
- aop_map
529 && i
+ 3 < ax
->buf
.size ())
533 ax
->flaw
= agent_flaw_hole
;
537 height
= heights
[i
+ 3];
540 /* For reg instructions, record the register in the bit mask. */
541 if (aop_reg
== op
- aop_map
)
543 int reg
= read_const (ax
, i
+ 1, 2);
545 ax_reg_mask (ax
, reg
);
549 /* Check that all the targets are on boundaries. */
550 for (i
= 0; i
< ax
->buf
.size (); i
++)
551 if (targets
[i
] && !boundary
[i
])
553 ax
->flaw
= agent_flaw_bad_jump
;
557 ax
->final_height
= height
;