]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ax-general.c
2.41 Release sources
[thirdparty/binutils-gdb.git] / gdb / ax-general.c
1 /* Functions for manipulating expressions designed to be executed on the agent
2 Copyright (C) 1998-2023 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 #include "gdbarch.h"
27
28 #include "value.h"
29 #include "user-regs.h"
30
31 static void append_const (struct agent_expr *x, LONGEST val, int n);
32
33 static LONGEST read_const (struct agent_expr *x, int o, int n);
34
35 static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
36 \f
37 /* Functions for building expressions. */
38
39 /* Append the low N bytes of VAL as an N-byte integer to the
40 expression X, in big-endian order. */
41 static void
42 append_const (struct agent_expr *x, LONGEST val, int n)
43 {
44 size_t len = x->buf.size ();
45 x->buf.resize (len + n);
46 for (int i = n - 1; i >= 0; i--)
47 {
48 x->buf[len + i] = val & 0xff;
49 val >>= 8;
50 }
51 }
52
53
54 /* Extract an N-byte big-endian unsigned integer from expression X at
55 offset O. */
56 static LONGEST
57 read_const (struct agent_expr *x, int o, int n)
58 {
59 int i;
60 LONGEST accum = 0;
61
62 /* Make sure we're not reading off the end of the expression. */
63 if (o + n > x->buf.size ())
64 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
65
66 for (i = 0; i < n; i++)
67 accum = (accum << 8) | x->buf[o + i];
68
69 return accum;
70 }
71
72 /* See ax.h. */
73
74 void
75 ax_raw_byte (struct agent_expr *x, gdb_byte byte)
76 {
77 x->buf.push_back (byte);
78 }
79
80 /* Append a simple operator OP to EXPR. */
81 void
82 ax_simple (struct agent_expr *x, enum agent_op op)
83 {
84 ax_raw_byte (x, op);
85 }
86
87 /* Append a pick operator to EXPR. DEPTH is the stack item to pick,
88 with 0 being top of stack. */
89
90 void
91 ax_pick (struct agent_expr *x, int depth)
92 {
93 if (depth < 0 || depth > 255)
94 error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
95 ax_simple (x, aop_pick);
96 append_const (x, 1, depth);
97 }
98
99
100 /* Append a sign-extension or zero-extension instruction to EXPR, to
101 extend an N-bit value. */
102 static void
103 generic_ext (struct agent_expr *x, enum agent_op op, int n)
104 {
105 /* N must fit in a byte. */
106 if (n < 0 || n > 255)
107 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
108 /* That had better be enough range. */
109 if (sizeof (LONGEST) * 8 > 255)
110 error (_("GDB bug: ax-general.c (generic_ext): "
111 "opcode has inadequate range"));
112
113 x->buf.push_back (op);
114 x->buf.push_back (n);
115 }
116
117
118 /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
119 void
120 ax_ext (struct agent_expr *x, int n)
121 {
122 generic_ext (x, aop_ext, n);
123 }
124
125
126 /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
127 void
128 ax_zero_ext (struct agent_expr *x, int n)
129 {
130 generic_ext (x, aop_zero_ext, n);
131 }
132
133
134 /* Append a trace_quick instruction to EXPR, to record N bytes. */
135 void
136 ax_trace_quick (struct agent_expr *x, int n)
137 {
138 /* N must fit in a byte. */
139 if (n < 0 || n > 255)
140 error (_("GDB bug: ax-general.c (ax_trace_quick): "
141 "size out of range for trace_quick"));
142
143 x->buf.push_back (aop_trace_quick);
144 x->buf.push_back (n);
145 }
146
147
148 /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or
149 aop_if_goto). We assume we don't know the target offset yet,
150 because it's probably a forward branch, so we leave space in EXPR
151 for the target, and return the offset in EXPR of that space, so we
152 can backpatch it once we do know the target offset. Use ax_label
153 to do the backpatching. */
154 int
155 ax_goto (struct agent_expr *x, enum agent_op op)
156 {
157 x->buf.push_back (op);
158 x->buf.push_back (0xff);
159 x->buf.push_back (0xff);
160 return x->buf.size () - 2;
161 }
162
163 /* Suppose a given call to ax_goto returns some value PATCH. When you
164 know the offset TARGET that goto should jump to, call
165 ax_label (EXPR, PATCH, TARGET)
166 to patch TARGET into the ax_goto instruction. */
167 void
168 ax_label (struct agent_expr *x, int patch, int target)
169 {
170 /* Make sure the value is in range. Don't accept 0xffff as an
171 offset; that's our magic sentinel value for unpatched branches. */
172 if (target < 0 || target >= 0xffff)
173 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
174
175 x->buf[patch] = (target >> 8) & 0xff;
176 x->buf[patch + 1] = target & 0xff;
177 }
178
179
180 /* Assemble code to push a constant on the stack. */
181 void
182 ax_const_l (struct agent_expr *x, LONGEST l)
183 {
184 static enum agent_op ops[]
185 =
186 {aop_const8, aop_const16, aop_const32, aop_const64};
187 int size;
188 int op;
189
190 /* How big is the number? 'op' keeps track of which opcode to use.
191 Notice that we don't really care whether the original number was
192 signed or unsigned; we always reproduce the value exactly, and
193 use the shortest representation. */
194 for (op = 0, size = 8; size < 64; size *= 2, op++)
195 {
196 LONGEST lim = ((LONGEST) 1) << (size - 1);
197
198 if (-lim <= l && l <= lim - 1)
199 break;
200 }
201
202 /* Emit the right opcode... */
203 ax_simple (x, ops[op]);
204
205 /* Emit the low SIZE bytes as an unsigned number. We know that
206 sign-extending this will yield l. */
207 append_const (x, l, size / 8);
208
209 /* Now, if it was negative, and not full-sized, sign-extend it. */
210 if (l < 0 && size < 64)
211 ax_ext (x, size);
212 }
213
214
215 void
216 ax_const_d (struct agent_expr *x, LONGEST d)
217 {
218 /* FIXME: floating-point support not present yet. */
219 error (_("GDB bug: ax-general.c (ax_const_d): "
220 "floating point not supported yet"));
221 }
222
223
224 /* Assemble code to push the value of register number REG on the
225 stack. */
226 void
227 ax_reg (struct agent_expr *x, int reg)
228 {
229 if (reg >= gdbarch_num_regs (x->gdbarch))
230 {
231 /* This is a pseudo-register. */
232 if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
233 error (_("'%s' is a pseudo-register; "
234 "GDB cannot yet trace its contents."),
235 user_reg_map_regnum_to_name (x->gdbarch, reg));
236 if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
237 error (_("Trace '%s' failed."),
238 user_reg_map_regnum_to_name (x->gdbarch, reg));
239 }
240 else
241 {
242 /* Get the remote register number. */
243 reg = gdbarch_remote_register_number (x->gdbarch, reg);
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 x->buf.push_back (aop_reg);
250 x->buf.push_back ((reg >> 8) & 0xff);
251 x->buf.push_back ((reg) & 0xff);
252 }
253 }
254
255 /* Assemble code to operate on a trace state variable. */
256
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 (_("ax-general.c (ax_tsv): variable "
263 "number is %d, out of range"), num);
264
265 x->buf.push_back (op);
266 x->buf.push_back ((num >> 8) & 0xff);
267 x->buf.push_back ((num) & 0xff);
268 }
269
270 /* Append a string to the expression. Note that the string is going
271 into the bytecodes directly, not on the stack. As a precaution,
272 include both length as prefix, and terminate with a NUL. (The NUL
273 is counted in the length.) */
274
275 void
276 ax_string (struct agent_expr *x, const char *str, int slen)
277 {
278 int i;
279
280 /* Make sure the string length is reasonable. */
281 if (slen < 0 || slen > 0xffff)
282 internal_error (_("ax-general.c (ax_string): string "
283 "length is %d, out of allowed range"), slen);
284
285 x->buf.push_back (((slen + 1) >> 8) & 0xff);
286 x->buf.push_back ((slen + 1) & 0xff);
287 for (i = 0; i < slen; ++i)
288 x->buf.push_back (str[i]);
289 x->buf.push_back ('\0');
290 }
291 \f
292
293
294 /* Functions for disassembling agent expressions, and otherwise
295 debugging the expression compiler. */
296
297 /* An entry in the opcode map. */
298 struct aop_map
299 {
300
301 /* The name of the opcode. Null means that this entry is not a
302 valid opcode --- a hole in the opcode space. */
303 const char *name;
304
305 /* All opcodes take no operands from the bytecode stream, or take
306 unsigned integers of various sizes. If this is a positive number
307 n, then the opcode is followed by an n-byte operand, which should
308 be printed as an unsigned integer. If this is zero, then the
309 opcode takes no operands from the bytecode stream.
310
311 If we get more complicated opcodes in the future, don't add other
312 magic values of this; that's a crock. Add an `enum encoding'
313 field to this, or something like that. */
314 int op_size;
315
316 /* The size of the data operated upon, in bits, for bytecodes that
317 care about that (ref and const). Zero for all others. */
318 int data_size;
319
320 /* Number of stack elements consumed, and number produced. */
321 int consumed, produced;
322 };
323
324 /* Map of the bytecodes, indexed by bytecode number. */
325
326 static struct aop_map aop_map[] =
327 {
328 {0, 0, 0, 0, 0}
329 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
330 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
331 #include "gdbsupport/ax.def"
332 #undef DEFOP
333 };
334
335
336 /* Disassemble the expression EXPR, writing to F. */
337 void
338 ax_print (struct ui_file *f, struct agent_expr *x)
339 {
340 int i;
341
342 gdb_printf (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
343 gdb_printf (f, _("Reg mask:"));
344 for (i = 0; i < x->reg_mask.size (); ++i)
345 {
346 if ((i % 8) == 0)
347 gdb_printf (f, " ");
348 gdb_printf (f, _("%d"), (int) x->reg_mask[i]);
349 }
350 gdb_printf (f, _("\n"));
351
352 for (i = 0; i < x->buf.size ();)
353 {
354 enum agent_op op = (enum agent_op) x->buf[i];
355
356 if (op >= ARRAY_SIZE (aop_map) || aop_map[op].name == nullptr)
357 {
358 gdb_printf (f, _("%3d <bad opcode %02x>\n"), i, op);
359 i++;
360 continue;
361 }
362 if (i + 1 + aop_map[op].op_size > x->buf.size ())
363 {
364 gdb_printf (f, _("%3d <incomplete opcode %s>\n"),
365 i, aop_map[op].name);
366 break;
367 }
368
369 gdb_printf (f, "%3d %s", i, aop_map[op].name);
370 if (aop_map[op].op_size > 0)
371 {
372 gdb_puts (" ", f);
373
374 print_longest (f, 'd', 0,
375 read_const (x, i + 1, aop_map[op].op_size));
376 }
377 /* Handle the complicated printf arguments specially. */
378 else if (op == aop_printf)
379 {
380 int slen, nargs;
381
382 i++;
383 nargs = x->buf[i++];
384 slen = x->buf[i++];
385 slen = slen * 256 + x->buf[i++];
386 gdb_printf (f, _(" \"%s\", %d args"),
387 &(x->buf[i]), nargs);
388 i += slen - 1;
389 }
390 gdb_printf (f, "\n");
391 i += 1 + aop_map[op].op_size;
392 }
393 }
394
395 /* Add register REG to the register mask for expression AX. */
396 void
397 ax_reg_mask (struct agent_expr *ax, int reg)
398 {
399 if (reg >= gdbarch_num_regs (ax->gdbarch))
400 {
401 /* This is a pseudo-register. */
402 if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
403 error (_("'%s' is a pseudo-register; "
404 "GDB cannot yet trace its contents."),
405 user_reg_map_regnum_to_name (ax->gdbarch, reg));
406 if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
407 error (_("Trace '%s' failed."),
408 user_reg_map_regnum_to_name (ax->gdbarch, reg));
409 }
410 else
411 {
412 /* Get the remote register number. */
413 reg = gdbarch_remote_register_number (ax->gdbarch, reg);
414
415 /* Grow the bit mask if necessary. */
416 if (reg >= ax->reg_mask.size ())
417 ax->reg_mask.resize (reg + 1);
418
419 ax->reg_mask[reg] = true;
420 }
421 }
422
423 /* Given an agent expression AX, fill in requirements and other descriptive
424 bits. */
425 void
426 ax_reqs (struct agent_expr *ax)
427 {
428 int i;
429 int height;
430
431 /* Jump target table. targets[i] is non-zero iff we have found a
432 jump to offset i. */
433 char *targets = (char *) alloca (ax->buf.size () * sizeof (targets[0]));
434
435 /* Instruction boundary table. boundary[i] is non-zero iff our scan
436 has reached an instruction starting at offset i. */
437 char *boundary = (char *) alloca (ax->buf.size () * sizeof (boundary[0]));
438
439 /* Stack height record. If either targets[i] or boundary[i] is
440 non-zero, heights[i] is the height the stack should have before
441 executing the bytecode at that point. */
442 int *heights = (int *) alloca (ax->buf.size () * sizeof (heights[0]));
443
444 /* Pointer to a description of the present op. */
445 struct aop_map *op;
446
447 memset (targets, 0, ax->buf.size () * sizeof (targets[0]));
448 memset (boundary, 0, ax->buf.size () * sizeof (boundary[0]));
449
450 ax->max_height = ax->min_height = height = 0;
451 ax->flaw = agent_flaw_none;
452 ax->max_data_size = 0;
453
454 for (i = 0; i < ax->buf.size (); i += 1 + op->op_size)
455 {
456 if (ax->buf[i] >= ARRAY_SIZE (aop_map))
457 {
458 ax->flaw = agent_flaw_bad_instruction;
459 return;
460 }
461
462 op = &aop_map[ax->buf[i]];
463
464 if (!op->name)
465 {
466 ax->flaw = agent_flaw_bad_instruction;
467 return;
468 }
469
470 if (i + 1 + op->op_size > ax->buf.size ())
471 {
472 ax->flaw = agent_flaw_incomplete_instruction;
473 return;
474 }
475
476 /* If this instruction is a forward jump target, does the
477 current stack height match the stack height at the jump
478 source? */
479 if (targets[i] && (heights[i] != height))
480 {
481 ax->flaw = agent_flaw_height_mismatch;
482 return;
483 }
484
485 boundary[i] = 1;
486 heights[i] = height;
487
488 height -= op->consumed;
489 if (height < ax->min_height)
490 ax->min_height = height;
491 height += op->produced;
492 if (height > ax->max_height)
493 ax->max_height = height;
494
495 if (op->data_size > ax->max_data_size)
496 ax->max_data_size = op->data_size;
497
498 /* For jump instructions, check that the target is a valid
499 offset. If it is, record the fact that that location is a
500 jump target, and record the height we expect there. */
501 if (aop_goto == op - aop_map
502 || aop_if_goto == op - aop_map)
503 {
504 int target = read_const (ax, i + 1, 2);
505 if (target < 0 || target >= ax->buf.size ())
506 {
507 ax->flaw = agent_flaw_bad_jump;
508 return;
509 }
510
511 /* Do we have any information about what the stack height
512 should be at the target? */
513 if (targets[target] || boundary[target])
514 {
515 if (heights[target] != height)
516 {
517 ax->flaw = agent_flaw_height_mismatch;
518 return;
519 }
520 }
521
522 /* Record the target, along with the stack height we expect. */
523 targets[target] = 1;
524 heights[target] = height;
525 }
526
527 /* For unconditional jumps with a successor, check that the
528 successor is a target, and pick up its stack height. */
529 if (aop_goto == op - aop_map
530 && i + 3 < ax->buf.size ())
531 {
532 if (!targets[i + 3])
533 {
534 ax->flaw = agent_flaw_hole;
535 return;
536 }
537
538 height = heights[i + 3];
539 }
540
541 /* For reg instructions, record the register in the bit mask. */
542 if (aop_reg == op - aop_map)
543 {
544 int reg = read_const (ax, i + 1, 2);
545
546 ax_reg_mask (ax, reg);
547 }
548 }
549
550 /* Check that all the targets are on boundaries. */
551 for (i = 0; i < ax->buf.size (); i++)
552 if (targets[i] && !boundary[i])
553 {
554 ax->flaw = agent_flaw_bad_jump;
555 return;
556 }
557
558 ax->final_height = height;
559 }