]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ax-general.c
Fix off-by-one error
[thirdparty/binutils-gdb.git] / gdb / ax-general.c
CommitLineData
c906108c 1/* Functions for manipulating expressions designed to be executed on the agent
213516ef 2 Copyright (C) 1998-2023 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 9 (at your option) any later version.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 18
c906108c
SS
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"
d55e5aa6 25#include "ax.h"
0d12e84c 26#include "gdbarch.h"
4de283e4 27
d55e5aa6 28#include "value.h"
4de283e4 29#include "user-regs.h"
175ff332 30
a14ed312 31static void append_const (struct agent_expr *x, LONGEST val, int n);
392a587b 32
a14ed312 33static LONGEST read_const (struct agent_expr *x, int o, int n);
392a587b 34
a14ed312 35static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
c906108c
SS
36\f
37/* Functions for building expressions. */
38
c906108c
SS
39/* Append the low N bytes of VAL as an N-byte integer to the
40 expression X, in big-endian order. */
41static void
fba45db2 42append_const (struct agent_expr *x, LONGEST val, int n)
c906108c
SS
43{
44 int i;
45
c906108c
SS
46 for (i = n - 1; i >= 0; i--)
47 {
6f96f485 48 x->buf.push_back (val & 0xff);
c906108c
SS
49 val >>= 8;
50 }
c906108c
SS
51}
52
53
54/* Extract an N-byte big-endian unsigned integer from expression X at
55 offset O. */
56static LONGEST
fba45db2 57read_const (struct agent_expr *x, int o, int n)
c906108c
SS
58{
59 int i;
60 LONGEST accum = 0;
61
62 /* Make sure we're not reading off the end of the expression. */
6f96f485 63 if (o + n > x->buf.size ())
3d263c1d 64 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
c906108c
SS
65
66 for (i = 0; i < n; i++)
67 accum = (accum << 8) | x->buf[o + i];
c5aa993b 68
c906108c
SS
69 return accum;
70}
71
70b8286a
SM
72/* See ax.h. */
73
74void
75ax_raw_byte (struct agent_expr *x, gdb_byte byte)
76{
6f96f485 77 x->buf.push_back (byte);
70b8286a 78}
c906108c
SS
79
80/* Append a simple operator OP to EXPR. */
81void
fba45db2 82ax_simple (struct agent_expr *x, enum agent_op op)
c906108c 83{
70b8286a 84 ax_raw_byte (x, op);
c906108c
SS
85}
86
c7f96d2b
TT
87/* Append a pick operator to EXPR. DEPTH is the stack item to pick,
88 with 0 being top of stack. */
2b52013f 89
c7f96d2b
TT
90void
91ax_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
c906108c
SS
99
100/* Append a sign-extension or zero-extension instruction to EXPR, to
101 extend an N-bit value. */
102static void
fba45db2 103generic_ext (struct agent_expr *x, enum agent_op op, int n)
c906108c
SS
104{
105 /* N must fit in a byte. */
106 if (n < 0 || n > 255)
3d263c1d 107 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
c906108c
SS
108 /* That had better be enough range. */
109 if (sizeof (LONGEST) * 8 > 255)
3e43a32a
MS
110 error (_("GDB bug: ax-general.c (generic_ext): "
111 "opcode has inadequate range"));
c906108c 112
6f96f485
TT
113 x->buf.push_back (op);
114 x->buf.push_back (n);
c906108c
SS
115}
116
117
118/* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
119void
fba45db2 120ax_ext (struct agent_expr *x, int n)
c906108c
SS
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. */
127void
fba45db2 128ax_zero_ext (struct agent_expr *x, int n)
c906108c
SS
129{
130 generic_ext (x, aop_zero_ext, n);
131}
132
133
134/* Append a trace_quick instruction to EXPR, to record N bytes. */
135void
fba45db2 136ax_trace_quick (struct agent_expr *x, int n)
c906108c
SS
137{
138 /* N must fit in a byte. */
139 if (n < 0 || n > 255)
3e43a32a
MS
140 error (_("GDB bug: ax-general.c (ax_trace_quick): "
141 "size out of range for trace_quick"));
c906108c 142
6f96f485
TT
143 x->buf.push_back (aop_trace_quick);
144 x->buf.push_back (n);
c906108c
SS
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. */
c5aa993b 154int
fba45db2 155ax_goto (struct agent_expr *x, enum agent_op op)
c906108c 156{
6f96f485
TT
157 x->buf.push_back (op);
158 x->buf.push_back (0xff);
159 x->buf.push_back (0xff);
160 return x->buf.size () - 2;
c906108c
SS
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
c5aa993b 165 ax_label (EXPR, PATCH, TARGET)
c906108c
SS
166 to patch TARGET into the ax_goto instruction. */
167void
fba45db2 168ax_label (struct agent_expr *x, int patch, int target)
c906108c
SS
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)
3d263c1d 173 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
c5aa993b 174
c906108c
SS
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. */
181void
fba45db2 182ax_const_l (struct agent_expr *x, LONGEST l)
c906108c
SS
183{
184 static enum agent_op ops[]
c5aa993b
JM
185 =
186 {aop_const8, aop_const16, aop_const32, aop_const64};
c906108c
SS
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++)
44a81774 195 {
cf3e25ca 196 LONGEST lim = ((LONGEST) 1) << (size - 1);
44a81774
JB
197
198 if (-lim <= l && l <= lim - 1)
dda83cd7 199 break;
44a81774 200 }
c906108c 201
0e2de366 202 /* Emit the right opcode... */
c906108c
SS
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
215void
fba45db2 216ax_const_d (struct agent_expr *x, LONGEST d)
c906108c
SS
217{
218 /* FIXME: floating-point support not present yet. */
3e43a32a
MS
219 error (_("GDB bug: ax-general.c (ax_const_d): "
220 "floating point not supported yet"));
c906108c
SS
221}
222
223
224/* Assemble code to push the value of register number REG on the
225 stack. */
c5aa993b 226void
fba45db2 227ax_reg (struct agent_expr *x, int reg)
c906108c 228{
175ff332
HZ
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 {
1eb7c2d8
AT
242 /* Get the remote register number. */
243 reg = gdbarch_remote_register_number (x->gdbarch, reg);
244
175ff332
HZ
245 /* Make sure the register number is in range. */
246 if (reg < 0 || reg > 0xffff)
dda83cd7 247 error (_("GDB bug: ax-general.c (ax_reg): "
3e43a32a 248 "register number out of range"));
6f96f485
TT
249 x->buf.push_back (aop_reg);
250 x->buf.push_back ((reg >> 8) & 0xff);
251 x->buf.push_back ((reg) & 0xff);
175ff332 252 }
c906108c 253}
f61e138d
SS
254
255/* Assemble code to operate on a trace state variable. */
256
257void
258ax_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)
f34652de 262 internal_error (_("ax-general.c (ax_tsv): variable "
3e43a32a 263 "number is %d, out of range"), num);
f61e138d 264
6f96f485
TT
265 x->buf.push_back (op);
266 x->buf.push_back ((num >> 8) & 0xff);
267 x->buf.push_back ((num) & 0xff);
f61e138d 268}
d3ce09f5
SS
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
275void
741d92cf 276ax_string (struct agent_expr *x, const char *str, int slen)
d3ce09f5
SS
277{
278 int i;
279
280 /* Make sure the string length is reasonable. */
281 if (slen < 0 || slen > 0xffff)
f34652de 282 internal_error (_("ax-general.c (ax_string): string "
d3ce09f5
SS
283 "length is %d, out of allowed range"), slen);
284
6f96f485
TT
285 x->buf.push_back (((slen + 1) >> 8) & 0xff);
286 x->buf.push_back ((slen + 1) & 0xff);
d3ce09f5 287 for (i = 0; i < slen; ++i)
6f96f485
TT
288 x->buf.push_back (str[i]);
289 x->buf.push_back ('\0');
d3ce09f5 290}
c5aa993b 291\f
c906108c
SS
292
293
c906108c
SS
294/* Functions for disassembling agent expressions, and otherwise
295 debugging the expression compiler. */
296
1e0e3ecd
TT
297/* An entry in the opcode map. */
298struct 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
326static struct aop_map aop_map[] =
c5aa993b 327{
94d5e490
TT
328 {0, 0, 0, 0, 0}
329#define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
330 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
268a13a5 331#include "gdbsupport/ax.def"
94d5e490 332#undef DEFOP
c906108c
SS
333};
334
335
336/* Disassemble the expression EXPR, writing to F. */
337void
fba45db2 338ax_print (struct ui_file *f, struct agent_expr *x)
c906108c
SS
339{
340 int i;
c906108c 341
6cb06a8c
TT
342 gdb_printf (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
343 gdb_printf (f, _("Reg mask:"));
a2bbca9f
TT
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 }
6cb06a8c 350 gdb_printf (f, _("\n"));
35c9c7ba 351
6f96f485 352 for (i = 0; i < x->buf.size ();)
c906108c 353 {
aead7601 354 enum agent_op op = (enum agent_op) x->buf[i];
c906108c 355
00a85428 356 if (op >= ARRAY_SIZE (aop_map) || aop_map[op].name == nullptr)
c906108c 357 {
6cb06a8c 358 gdb_printf (f, _("%3d <bad opcode %02x>\n"), i, op);
c906108c
SS
359 i++;
360 continue;
361 }
6f96f485 362 if (i + 1 + aop_map[op].op_size > x->buf.size ())
c906108c 363 {
6cb06a8c
TT
364 gdb_printf (f, _("%3d <incomplete opcode %s>\n"),
365 i, aop_map[op].name);
c906108c
SS
366 break;
367 }
368
6cb06a8c 369 gdb_printf (f, "%3d %s", i, aop_map[op].name);
a04b0428 370 if (aop_map[op].op_size > 0)
c906108c 371 {
0426ad51 372 gdb_puts (" ", f);
c5aa993b 373
c906108c 374 print_longest (f, 'd', 0,
a04b0428 375 read_const (x, i + 1, aop_map[op].op_size));
c906108c 376 }
d3ce09f5
SS
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++];
6cb06a8c
TT
386 gdb_printf (f, _(" \"%s\", %d args"),
387 &(x->buf[i]), nargs);
d3ce09f5
SS
388 i += slen - 1;
389 }
6cb06a8c 390 gdb_printf (f, "\n");
a04b0428 391 i += 1 + aop_map[op].op_size;
c906108c
SS
392 }
393}
394
35c9c7ba
SS
395/* Add register REG to the register mask for expression AX. */
396void
397ax_reg_mask (struct agent_expr *ax, int reg)
398{
175ff332 399 if (reg >= gdbarch_num_regs (ax->gdbarch))
35c9c7ba 400 {
175ff332
HZ
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 {
1eb7c2d8
AT
412 /* Get the remote register number. */
413 reg = gdbarch_remote_register_number (ax->gdbarch, reg);
175ff332
HZ
414
415 /* Grow the bit mask if necessary. */
a2bbca9f 416 if (reg >= ax->reg_mask.size ())
8a269c26 417 ax->reg_mask.resize (reg + 1);
175ff332 418
a2bbca9f 419 ax->reg_mask[reg] = true;
35c9c7ba 420 }
35c9c7ba
SS
421}
422
423/* Given an agent expression AX, fill in requirements and other descriptive
424 bits. */
c906108c 425void
35c9c7ba 426ax_reqs (struct agent_expr *ax)
c906108c
SS
427{
428 int i;
429 int height;
430
3d269a59
JB
431 /* Jump target table. targets[i] is non-zero iff we have found a
432 jump to offset i. */
6f96f485 433 char *targets = (char *) alloca (ax->buf.size () * sizeof (targets[0]));
c906108c 434
3d269a59
JB
435 /* Instruction boundary table. boundary[i] is non-zero iff our scan
436 has reached an instruction starting at offset i. */
6f96f485 437 char *boundary = (char *) alloca (ax->buf.size () * sizeof (boundary[0]));
c906108c 438
3d269a59 439 /* Stack height record. If either targets[i] or boundary[i] is
c906108c
SS
440 non-zero, heights[i] is the height the stack should have before
441 executing the bytecode at that point. */
6f96f485 442 int *heights = (int *) alloca (ax->buf.size () * sizeof (heights[0]));
c906108c
SS
443
444 /* Pointer to a description of the present op. */
445 struct aop_map *op;
446
6f96f485
TT
447 memset (targets, 0, ax->buf.size () * sizeof (targets[0]));
448 memset (boundary, 0, ax->buf.size () * sizeof (boundary[0]));
c906108c 449
35c9c7ba
SS
450 ax->max_height = ax->min_height = height = 0;
451 ax->flaw = agent_flaw_none;
452 ax->max_data_size = 0;
c906108c 453
6f96f485 454 for (i = 0; i < ax->buf.size (); i += 1 + op->op_size)
c906108c 455 {
00a85428 456 if (ax->buf[i] >= ARRAY_SIZE (aop_map))
c906108c 457 {
35c9c7ba 458 ax->flaw = agent_flaw_bad_instruction;
c906108c
SS
459 return;
460 }
461
462 op = &aop_map[ax->buf[i]];
463
c5aa993b 464 if (!op->name)
c906108c 465 {
35c9c7ba 466 ax->flaw = agent_flaw_bad_instruction;
c906108c
SS
467 return;
468 }
c5aa993b 469
6f96f485 470 if (i + 1 + op->op_size > ax->buf.size ())
c906108c 471 {
35c9c7ba 472 ax->flaw = agent_flaw_incomplete_instruction;
c906108c
SS
473 return;
474 }
475
3d269a59 476 /* If this instruction is a forward jump target, does the
dda83cd7
SM
477 current stack height match the stack height at the jump
478 source? */
c906108c
SS
479 if (targets[i] && (heights[i] != height))
480 {
35c9c7ba 481 ax->flaw = agent_flaw_height_mismatch;
c906108c
SS
482 return;
483 }
484
485 boundary[i] = 1;
486 heights[i] = height;
487
a04b0428 488 height -= op->consumed;
35c9c7ba
SS
489 if (height < ax->min_height)
490 ax->min_height = height;
c906108c 491 height += op->produced;
35c9c7ba
SS
492 if (height > ax->max_height)
493 ax->max_height = height;
c906108c 494
35c9c7ba
SS
495 if (op->data_size > ax->max_data_size)
496 ax->max_data_size = op->data_size;
c906108c
SS
497
498 /* For jump instructions, check that the target is a valid
dda83cd7
SM
499 offset. If it is, record the fact that that location is a
500 jump target, and record the height we expect there. */
c906108c
SS
501 if (aop_goto == op - aop_map
502 || aop_if_goto == op - aop_map)
503 {
504 int target = read_const (ax, i + 1, 2);
6f96f485 505 if (target < 0 || target >= ax->buf.size ())
c906108c 506 {
35c9c7ba 507 ax->flaw = agent_flaw_bad_jump;
c906108c
SS
508 return;
509 }
3d269a59
JB
510
511 /* Do we have any information about what the stack height
dda83cd7 512 should be at the target? */
3d269a59 513 if (targets[target] || boundary[target])
c906108c 514 {
3d269a59 515 if (heights[target] != height)
c906108c 516 {
35c9c7ba 517 ax->flaw = agent_flaw_height_mismatch;
c906108c
SS
518 return;
519 }
520 }
3d269a59 521
dda83cd7
SM
522 /* Record the target, along with the stack height we expect. */
523 targets[target] = 1;
524 heights[target] = height;
c906108c 525 }
c5aa993b 526
c906108c 527 /* For unconditional jumps with a successor, check that the
dda83cd7 528 successor is a target, and pick up its stack height. */
c906108c 529 if (aop_goto == op - aop_map
6f96f485 530 && i + 3 < ax->buf.size ())
c906108c 531 {
c5aa993b 532 if (!targets[i + 3])
c906108c 533 {
35c9c7ba 534 ax->flaw = agent_flaw_hole;
c906108c
SS
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);
c906108c 545
35c9c7ba 546 ax_reg_mask (ax, reg);
c906108c
SS
547 }
548 }
549
550 /* Check that all the targets are on boundaries. */
6f96f485 551 for (i = 0; i < ax->buf.size (); i++)
c906108c
SS
552 if (targets[i] && !boundary[i])
553 {
35c9c7ba 554 ax->flaw = agent_flaw_bad_jump;
c906108c
SS
555 return;
556 }
557
35c9c7ba 558 ax->final_height = height;
c906108c 559}