xref: /openbsd-src/gnu/usr.bin/binutils/gdb/ax.h (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1*b725ae77Skettenis /* Definitions for expressions designed to be executed on the agent
2*b725ae77Skettenis    Copyright 1998, 1999, 2000 Free Software Foundation, Inc.
3*b725ae77Skettenis 
4*b725ae77Skettenis    This file is part of GDB.
5*b725ae77Skettenis 
6*b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
7*b725ae77Skettenis    it under the terms of the GNU General Public License as published by
8*b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
9*b725ae77Skettenis    (at your option) any later version.
10*b725ae77Skettenis 
11*b725ae77Skettenis    This program is distributed in the hope that it will be useful,
12*b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*b725ae77Skettenis    GNU General Public License for more details.
15*b725ae77Skettenis 
16*b725ae77Skettenis    You should have received a copy of the GNU General Public License
17*b725ae77Skettenis    along with this program; if not, write to the Free Software
18*b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
19*b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
20*b725ae77Skettenis 
21*b725ae77Skettenis #ifndef AGENTEXPR_H
22*b725ae77Skettenis #define AGENTEXPR_H
23*b725ae77Skettenis 
24*b725ae77Skettenis #include "doublest.h"		/* For DOUBLEST.  */
25*b725ae77Skettenis 
26*b725ae77Skettenis /* It's sometimes useful to be able to debug programs that you can't
27*b725ae77Skettenis    really stop for more than a fraction of a second.  To this end, the
28*b725ae77Skettenis    user can specify a tracepoint (like a breakpoint, but you don't
29*b725ae77Skettenis    stop at it), and specify a bunch of expressions to record the
30*b725ae77Skettenis    values of when that tracepoint is reached.  As the program runs,
31*b725ae77Skettenis    GDB collects the values.  At any point (possibly while values are
32*b725ae77Skettenis    still being collected), the user can display the collected values.
33*b725ae77Skettenis 
34*b725ae77Skettenis    This is used with remote debugging; we don't really support it on
35*b725ae77Skettenis    native configurations.
36*b725ae77Skettenis 
37*b725ae77Skettenis    This means that expressions are being evaluated by the remote agent,
38*b725ae77Skettenis    which doesn't have any access to the symbol table information, and
39*b725ae77Skettenis    needs to be small and simple.
40*b725ae77Skettenis 
41*b725ae77Skettenis    The agent_expr routines and datatypes are a bytecode language
42*b725ae77Skettenis    designed to be executed by the agent.  Agent expressions work in
43*b725ae77Skettenis    terms of fixed-width values, operators, memory references, and
44*b725ae77Skettenis    register references.  You can evaluate a agent expression just given
45*b725ae77Skettenis    a bunch of memory and register values to sniff at; you don't need
46*b725ae77Skettenis    any symbolic information like variable names, types, etc.
47*b725ae77Skettenis 
48*b725ae77Skettenis    GDB translates source expressions, whose meaning depends on
49*b725ae77Skettenis    symbolic information, into agent bytecode expressions, whose meaning
50*b725ae77Skettenis    is independent of symbolic information.  This means the agent can
51*b725ae77Skettenis    evaluate them on the fly without reference to data only available
52*b725ae77Skettenis    to the host GDB.  */
53*b725ae77Skettenis 
54*b725ae77Skettenis 
55*b725ae77Skettenis /* Agent expression data structures.  */
56*b725ae77Skettenis 
57*b725ae77Skettenis /* The type of an element of the agent expression stack.
58*b725ae77Skettenis    The bytecode operation indicates which element we should access;
59*b725ae77Skettenis    the value itself has no typing information.  GDB generates all
60*b725ae77Skettenis    bytecode streams, so we don't have to worry about type errors.  */
61*b725ae77Skettenis 
62*b725ae77Skettenis union agent_val
63*b725ae77Skettenis   {
64*b725ae77Skettenis     LONGEST l;
65*b725ae77Skettenis     DOUBLEST d;
66*b725ae77Skettenis   };
67*b725ae77Skettenis 
68*b725ae77Skettenis /* A buffer containing a agent expression.  */
69*b725ae77Skettenis struct agent_expr
70*b725ae77Skettenis   {
71*b725ae77Skettenis     unsigned char *buf;
72*b725ae77Skettenis     int len;			/* number of characters used */
73*b725ae77Skettenis     int size;			/* allocated size */
74*b725ae77Skettenis     CORE_ADDR scope;
75*b725ae77Skettenis   };
76*b725ae77Skettenis 
77*b725ae77Skettenis 
78*b725ae77Skettenis 
79*b725ae77Skettenis 
80*b725ae77Skettenis /* The actual values of the various bytecode operations.
81*b725ae77Skettenis 
82*b725ae77Skettenis    Other independent implementations of the agent bytecode engine will
83*b725ae77Skettenis    rely on the exact values of these enums, and may not be recompiled
84*b725ae77Skettenis    when we change this table.  The numeric values should remain fixed
85*b725ae77Skettenis    whenever possible.  Thus, we assign them values explicitly here (to
86*b725ae77Skettenis    allow gaps to form safely), and the disassembly table in
87*b725ae77Skettenis    agentexpr.h behaves like an opcode map.  If you want to see them
88*b725ae77Skettenis    grouped logically, see doc/agentexpr.texi.  */
89*b725ae77Skettenis 
90*b725ae77Skettenis enum agent_op
91*b725ae77Skettenis   {
92*b725ae77Skettenis     aop_float = 0x01,
93*b725ae77Skettenis     aop_add = 0x02,
94*b725ae77Skettenis     aop_sub = 0x03,
95*b725ae77Skettenis     aop_mul = 0x04,
96*b725ae77Skettenis     aop_div_signed = 0x05,
97*b725ae77Skettenis     aop_div_unsigned = 0x06,
98*b725ae77Skettenis     aop_rem_signed = 0x07,
99*b725ae77Skettenis     aop_rem_unsigned = 0x08,
100*b725ae77Skettenis     aop_lsh = 0x09,
101*b725ae77Skettenis     aop_rsh_signed = 0x0a,
102*b725ae77Skettenis     aop_rsh_unsigned = 0x0b,
103*b725ae77Skettenis     aop_trace = 0x0c,
104*b725ae77Skettenis     aop_trace_quick = 0x0d,
105*b725ae77Skettenis     aop_log_not = 0x0e,
106*b725ae77Skettenis     aop_bit_and = 0x0f,
107*b725ae77Skettenis     aop_bit_or = 0x10,
108*b725ae77Skettenis     aop_bit_xor = 0x11,
109*b725ae77Skettenis     aop_bit_not = 0x12,
110*b725ae77Skettenis     aop_equal = 0x13,
111*b725ae77Skettenis     aop_less_signed = 0x14,
112*b725ae77Skettenis     aop_less_unsigned = 0x15,
113*b725ae77Skettenis     aop_ext = 0x16,
114*b725ae77Skettenis     aop_ref8 = 0x17,
115*b725ae77Skettenis     aop_ref16 = 0x18,
116*b725ae77Skettenis     aop_ref32 = 0x19,
117*b725ae77Skettenis     aop_ref64 = 0x1a,
118*b725ae77Skettenis     aop_ref_float = 0x1b,
119*b725ae77Skettenis     aop_ref_double = 0x1c,
120*b725ae77Skettenis     aop_ref_long_double = 0x1d,
121*b725ae77Skettenis     aop_l_to_d = 0x1e,
122*b725ae77Skettenis     aop_d_to_l = 0x1f,
123*b725ae77Skettenis     aop_if_goto = 0x20,
124*b725ae77Skettenis     aop_goto = 0x21,
125*b725ae77Skettenis     aop_const8 = 0x22,
126*b725ae77Skettenis     aop_const16 = 0x23,
127*b725ae77Skettenis     aop_const32 = 0x24,
128*b725ae77Skettenis     aop_const64 = 0x25,
129*b725ae77Skettenis     aop_reg = 0x26,
130*b725ae77Skettenis     aop_end = 0x27,
131*b725ae77Skettenis     aop_dup = 0x28,
132*b725ae77Skettenis     aop_pop = 0x29,
133*b725ae77Skettenis     aop_zero_ext = 0x2a,
134*b725ae77Skettenis     aop_swap = 0x2b,
135*b725ae77Skettenis     aop_trace16 = 0x30,
136*b725ae77Skettenis     aop_last
137*b725ae77Skettenis   };
138*b725ae77Skettenis 
139*b725ae77Skettenis 
140*b725ae77Skettenis 
141*b725ae77Skettenis /* Functions for building expressions.  */
142*b725ae77Skettenis 
143*b725ae77Skettenis /* Allocate a new, empty agent expression.  */
144*b725ae77Skettenis extern struct agent_expr *new_agent_expr (CORE_ADDR);
145*b725ae77Skettenis 
146*b725ae77Skettenis /* Free a agent expression.  */
147*b725ae77Skettenis extern void free_agent_expr (struct agent_expr *);
148*b725ae77Skettenis extern struct cleanup *make_cleanup_free_agent_expr (struct agent_expr *);
149*b725ae77Skettenis 
150*b725ae77Skettenis /* Append a simple operator OP to EXPR.  */
151*b725ae77Skettenis extern void ax_simple (struct agent_expr *EXPR, enum agent_op OP);
152*b725ae77Skettenis 
153*b725ae77Skettenis /* Append the floating-point prefix, for the next bytecode.  */
154*b725ae77Skettenis #define ax_float(EXPR) (ax_simple ((EXPR), aop_float))
155*b725ae77Skettenis 
156*b725ae77Skettenis /* Append a sign-extension instruction to EXPR, to extend an N-bit value.  */
157*b725ae77Skettenis extern void ax_ext (struct agent_expr *EXPR, int N);
158*b725ae77Skettenis 
159*b725ae77Skettenis /* Append a zero-extension instruction to EXPR, to extend an N-bit value.  */
160*b725ae77Skettenis extern void ax_zero_ext (struct agent_expr *EXPR, int N);
161*b725ae77Skettenis 
162*b725ae77Skettenis /* Append a trace_quick instruction to EXPR, to record N bytes.  */
163*b725ae77Skettenis extern void ax_trace_quick (struct agent_expr *EXPR, int N);
164*b725ae77Skettenis 
165*b725ae77Skettenis /* Append a goto op to EXPR.  OP is the actual op (must be aop_goto or
166*b725ae77Skettenis    aop_if_goto).  We assume we don't know the target offset yet,
167*b725ae77Skettenis    because it's probably a forward branch, so we leave space in EXPR
168*b725ae77Skettenis    for the target, and return the offset in EXPR of that space, so we
169*b725ae77Skettenis    can backpatch it once we do know the target offset.  Use ax_label
170*b725ae77Skettenis    to do the backpatching.  */
171*b725ae77Skettenis extern int ax_goto (struct agent_expr *EXPR, enum agent_op OP);
172*b725ae77Skettenis 
173*b725ae77Skettenis /* Suppose a given call to ax_goto returns some value PATCH.  When you
174*b725ae77Skettenis    know the offset TARGET that goto should jump to, call
175*b725ae77Skettenis    ax_label (EXPR, PATCH, TARGET)
176*b725ae77Skettenis    to patch TARGET into the ax_goto instruction.  */
177*b725ae77Skettenis extern void ax_label (struct agent_expr *EXPR, int patch, int target);
178*b725ae77Skettenis 
179*b725ae77Skettenis /* Assemble code to push a constant on the stack.  */
180*b725ae77Skettenis extern void ax_const_l (struct agent_expr *EXPR, LONGEST l);
181*b725ae77Skettenis extern void ax_const_d (struct agent_expr *EXPR, LONGEST d);
182*b725ae77Skettenis 
183*b725ae77Skettenis /* Assemble code to push the value of register number REG on the
184*b725ae77Skettenis    stack.  */
185*b725ae77Skettenis extern void ax_reg (struct agent_expr *EXPR, int REG);
186*b725ae77Skettenis 
187*b725ae77Skettenis 
188*b725ae77Skettenis /* Functions for printing out expressions, and otherwise debugging
189*b725ae77Skettenis    things.  */
190*b725ae77Skettenis 
191*b725ae77Skettenis /* Disassemble the expression EXPR, writing to F.  */
192*b725ae77Skettenis extern void ax_print (struct ui_file *f, struct agent_expr * EXPR);
193*b725ae77Skettenis 
194*b725ae77Skettenis /* An entry in the opcode map.  */
195*b725ae77Skettenis struct aop_map
196*b725ae77Skettenis   {
197*b725ae77Skettenis 
198*b725ae77Skettenis     /* The name of the opcode.  Null means that this entry is not a
199*b725ae77Skettenis        valid opcode --- a hole in the opcode space.  */
200*b725ae77Skettenis     char *name;
201*b725ae77Skettenis 
202*b725ae77Skettenis     /* All opcodes take no operands from the bytecode stream, or take
203*b725ae77Skettenis        unsigned integers of various sizes.  If this is a positive number
204*b725ae77Skettenis        n, then the opcode is followed by an n-byte operand, which should
205*b725ae77Skettenis        be printed as an unsigned integer.  If this is zero, then the
206*b725ae77Skettenis        opcode takes no operands from the bytecode stream.
207*b725ae77Skettenis 
208*b725ae77Skettenis        If we get more complicated opcodes in the future, don't add other
209*b725ae77Skettenis        magic values of this; that's a crock.  Add an `enum encoding'
210*b725ae77Skettenis        field to this, or something like that.  */
211*b725ae77Skettenis     int op_size;
212*b725ae77Skettenis 
213*b725ae77Skettenis     /* The size of the data operated upon, in bits, for bytecodes that
214*b725ae77Skettenis        care about that (ref and const).  Zero for all others.  */
215*b725ae77Skettenis     int data_size;
216*b725ae77Skettenis 
217*b725ae77Skettenis     /* Number of stack elements consumed, and number produced.  */
218*b725ae77Skettenis     int consumed, produced;
219*b725ae77Skettenis   };
220*b725ae77Skettenis 
221*b725ae77Skettenis /* Map of the bytecodes, indexed by bytecode number.  */
222*b725ae77Skettenis extern struct aop_map aop_map[];
223*b725ae77Skettenis 
224*b725ae77Skettenis /* Different kinds of flaws an agent expression might have, as
225*b725ae77Skettenis    detected by agent_reqs.  */
226*b725ae77Skettenis enum agent_flaws
227*b725ae77Skettenis   {
228*b725ae77Skettenis     agent_flaw_none = 0,	/* code is good */
229*b725ae77Skettenis 
230*b725ae77Skettenis     /* There is an invalid instruction in the stream.  */
231*b725ae77Skettenis     agent_flaw_bad_instruction,
232*b725ae77Skettenis 
233*b725ae77Skettenis     /* There is an incomplete instruction at the end of the expression.  */
234*b725ae77Skettenis     agent_flaw_incomplete_instruction,
235*b725ae77Skettenis 
236*b725ae77Skettenis     /* agent_reqs was unable to prove that every jump target is to a
237*b725ae77Skettenis        valid offset.  Valid offsets are within the bounds of the
238*b725ae77Skettenis        expression, and to a valid instruction boundary.  */
239*b725ae77Skettenis     agent_flaw_bad_jump,
240*b725ae77Skettenis 
241*b725ae77Skettenis     /* agent_reqs was unable to prove to its satisfaction that, for each
242*b725ae77Skettenis        jump target location, the stack will have the same height whether
243*b725ae77Skettenis        that location is reached via a jump or by straight execution.  */
244*b725ae77Skettenis     agent_flaw_height_mismatch,
245*b725ae77Skettenis 
246*b725ae77Skettenis     /* agent_reqs was unable to prove that every instruction following
247*b725ae77Skettenis        an unconditional jump was the target of some other jump.  */
248*b725ae77Skettenis     agent_flaw_hole
249*b725ae77Skettenis   };
250*b725ae77Skettenis 
251*b725ae77Skettenis /* Structure describing the requirements of a bytecode expression.  */
252*b725ae77Skettenis struct agent_reqs
253*b725ae77Skettenis   {
254*b725ae77Skettenis 
255*b725ae77Skettenis     /* If the following is not equal to agent_flaw_none, the rest of the
256*b725ae77Skettenis        information in this structure is suspect.  */
257*b725ae77Skettenis     enum agent_flaws flaw;
258*b725ae77Skettenis 
259*b725ae77Skettenis     /* Number of elements left on stack at end; may be negative if expr
260*b725ae77Skettenis        only consumes elements.  */
261*b725ae77Skettenis     int final_height;
262*b725ae77Skettenis 
263*b725ae77Skettenis     /* Maximum and minimum stack height, relative to initial height.  */
264*b725ae77Skettenis     int max_height, min_height;
265*b725ae77Skettenis 
266*b725ae77Skettenis     /* Largest `ref' or `const' opcode used, in bits.  Zero means the
267*b725ae77Skettenis        expression has no such instructions.  */
268*b725ae77Skettenis     int max_data_size;
269*b725ae77Skettenis 
270*b725ae77Skettenis     /* Bit vector of registers used.  Register R is used iff
271*b725ae77Skettenis 
272*b725ae77Skettenis        reg_mask[R / 8] & (1 << (R % 8))
273*b725ae77Skettenis 
274*b725ae77Skettenis        is non-zero.  Note!  You may not assume that this bitmask is long
275*b725ae77Skettenis        enough to hold bits for all the registers of the machine; the
276*b725ae77Skettenis        agent expression code has no idea how many registers the machine
277*b725ae77Skettenis        has.  However, the bitmask is reg_mask_len bytes long, so the
278*b725ae77Skettenis        valid register numbers run from 0 to reg_mask_len * 8 - 1.
279*b725ae77Skettenis 
280*b725ae77Skettenis        We're assuming eight-bit bytes.  So sue me.
281*b725ae77Skettenis 
282*b725ae77Skettenis        The caller should free reg_list when done.  */
283*b725ae77Skettenis     int reg_mask_len;
284*b725ae77Skettenis     unsigned char *reg_mask;
285*b725ae77Skettenis   };
286*b725ae77Skettenis 
287*b725ae77Skettenis 
288*b725ae77Skettenis /* Given an agent expression AX, fill in an agent_reqs structure REQS
289*b725ae77Skettenis    describing it.  */
290*b725ae77Skettenis extern void ax_reqs (struct agent_expr *ax, struct agent_reqs *reqs);
291*b725ae77Skettenis 
292*b725ae77Skettenis #endif /* AGENTEXPR_H */
293