xref: /dflybsd-src/contrib/gdb-7/gdb/ax.h (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Definitions for expressions designed to be executed on the agent
2*cf7f2e2dSJohn Marino    Copyright (C) 1998, 1999, 2000, 2007, 2008, 2009, 2010
35796c8dcSSimon Schubert    Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #ifndef AGENTEXPR_H
215796c8dcSSimon Schubert #define AGENTEXPR_H
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #include "doublest.h"		/* For DOUBLEST.  */
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert /* It's sometimes useful to be able to debug programs that you can't
265796c8dcSSimon Schubert    really stop for more than a fraction of a second.  To this end, the
275796c8dcSSimon Schubert    user can specify a tracepoint (like a breakpoint, but you don't
285796c8dcSSimon Schubert    stop at it), and specify a bunch of expressions to record the
295796c8dcSSimon Schubert    values of when that tracepoint is reached.  As the program runs,
305796c8dcSSimon Schubert    GDB collects the values.  At any point (possibly while values are
315796c8dcSSimon Schubert    still being collected), the user can display the collected values.
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert    This is used with remote debugging; we don't really support it on
345796c8dcSSimon Schubert    native configurations.
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert    This means that expressions are being evaluated by the remote agent,
375796c8dcSSimon Schubert    which doesn't have any access to the symbol table information, and
385796c8dcSSimon Schubert    needs to be small and simple.
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert    The agent_expr routines and datatypes are a bytecode language
415796c8dcSSimon Schubert    designed to be executed by the agent.  Agent expressions work in
425796c8dcSSimon Schubert    terms of fixed-width values, operators, memory references, and
435796c8dcSSimon Schubert    register references.  You can evaluate a agent expression just given
445796c8dcSSimon Schubert    a bunch of memory and register values to sniff at; you don't need
455796c8dcSSimon Schubert    any symbolic information like variable names, types, etc.
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert    GDB translates source expressions, whose meaning depends on
485796c8dcSSimon Schubert    symbolic information, into agent bytecode expressions, whose meaning
495796c8dcSSimon Schubert    is independent of symbolic information.  This means the agent can
505796c8dcSSimon Schubert    evaluate them on the fly without reference to data only available
515796c8dcSSimon Schubert    to the host GDB.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert 
54*cf7f2e2dSJohn Marino /* Different kinds of flaws an agent expression might have, as
55*cf7f2e2dSJohn Marino    detected by ax_reqs.  */
56*cf7f2e2dSJohn Marino enum agent_flaws
57*cf7f2e2dSJohn Marino   {
58*cf7f2e2dSJohn Marino     agent_flaw_none = 0,	/* code is good */
59*cf7f2e2dSJohn Marino 
60*cf7f2e2dSJohn Marino     /* There is an invalid instruction in the stream.  */
61*cf7f2e2dSJohn Marino     agent_flaw_bad_instruction,
62*cf7f2e2dSJohn Marino 
63*cf7f2e2dSJohn Marino     /* There is an incomplete instruction at the end of the expression.  */
64*cf7f2e2dSJohn Marino     agent_flaw_incomplete_instruction,
65*cf7f2e2dSJohn Marino 
66*cf7f2e2dSJohn Marino     /* ax_reqs was unable to prove that every jump target is to a
67*cf7f2e2dSJohn Marino        valid offset.  Valid offsets are within the bounds of the
68*cf7f2e2dSJohn Marino        expression, and to a valid instruction boundary.  */
69*cf7f2e2dSJohn Marino     agent_flaw_bad_jump,
70*cf7f2e2dSJohn Marino 
71*cf7f2e2dSJohn Marino     /* ax_reqs was unable to prove to its satisfaction that, for each
72*cf7f2e2dSJohn Marino        jump target location, the stack will have the same height whether
73*cf7f2e2dSJohn Marino        that location is reached via a jump or by straight execution.  */
74*cf7f2e2dSJohn Marino     agent_flaw_height_mismatch,
75*cf7f2e2dSJohn Marino 
76*cf7f2e2dSJohn Marino     /* ax_reqs was unable to prove that every instruction following
77*cf7f2e2dSJohn Marino        an unconditional jump was the target of some other jump.  */
78*cf7f2e2dSJohn Marino     agent_flaw_hole
79*cf7f2e2dSJohn Marino   };
80*cf7f2e2dSJohn Marino 
815796c8dcSSimon Schubert /* Agent expression data structures.  */
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert /* The type of an element of the agent expression stack.
845796c8dcSSimon Schubert    The bytecode operation indicates which element we should access;
855796c8dcSSimon Schubert    the value itself has no typing information.  GDB generates all
865796c8dcSSimon Schubert    bytecode streams, so we don't have to worry about type errors.  */
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert union agent_val
895796c8dcSSimon Schubert   {
905796c8dcSSimon Schubert     LONGEST l;
915796c8dcSSimon Schubert     DOUBLEST d;
925796c8dcSSimon Schubert   };
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert /* A buffer containing a agent expression.  */
955796c8dcSSimon Schubert struct agent_expr
965796c8dcSSimon Schubert   {
97*cf7f2e2dSJohn Marino     /* The bytes of the expression.  */
985796c8dcSSimon Schubert     unsigned char *buf;
99*cf7f2e2dSJohn Marino 
100*cf7f2e2dSJohn Marino     /* The number of bytecode in the expression.  */
101*cf7f2e2dSJohn Marino     int len;
102*cf7f2e2dSJohn Marino 
103*cf7f2e2dSJohn Marino     /* Allocated space available currently.  */
104*cf7f2e2dSJohn Marino     int size;
105*cf7f2e2dSJohn Marino 
106*cf7f2e2dSJohn Marino     /* The target architecture assumed to be in effect.  */
107*cf7f2e2dSJohn Marino     struct gdbarch *gdbarch;
108*cf7f2e2dSJohn Marino 
109*cf7f2e2dSJohn Marino     /* The address to which the expression applies.  */
1105796c8dcSSimon Schubert     CORE_ADDR scope;
111*cf7f2e2dSJohn Marino 
112*cf7f2e2dSJohn Marino     /* If the following is not equal to agent_flaw_none, the rest of the
113*cf7f2e2dSJohn Marino        information in this structure is suspect.  */
114*cf7f2e2dSJohn Marino     enum agent_flaws flaw;
115*cf7f2e2dSJohn Marino 
116*cf7f2e2dSJohn Marino     /* Number of elements left on stack at end; may be negative if expr
117*cf7f2e2dSJohn Marino        only consumes elements.  */
118*cf7f2e2dSJohn Marino     int final_height;
119*cf7f2e2dSJohn Marino 
120*cf7f2e2dSJohn Marino     /* Maximum and minimum stack height, relative to initial height.  */
121*cf7f2e2dSJohn Marino     int max_height, min_height;
122*cf7f2e2dSJohn Marino 
123*cf7f2e2dSJohn Marino     /* Largest `ref' or `const' opcode used, in bits.  Zero means the
124*cf7f2e2dSJohn Marino        expression has no such instructions.  */
125*cf7f2e2dSJohn Marino     int max_data_size;
126*cf7f2e2dSJohn Marino 
127*cf7f2e2dSJohn Marino     /* Bit vector of registers needed.  Register R is needed iff
128*cf7f2e2dSJohn Marino 
129*cf7f2e2dSJohn Marino        reg_mask[R / 8] & (1 << (R % 8))
130*cf7f2e2dSJohn Marino 
131*cf7f2e2dSJohn Marino        is non-zero.  Note!  You may not assume that this bitmask is long
132*cf7f2e2dSJohn Marino        enough to hold bits for all the registers of the machine; the
133*cf7f2e2dSJohn Marino        agent expression code has no idea how many registers the machine
134*cf7f2e2dSJohn Marino        has.  However, the bitmask is reg_mask_len bytes long, so the
135*cf7f2e2dSJohn Marino        valid register numbers run from 0 to reg_mask_len * 8 - 1.
136*cf7f2e2dSJohn Marino 
137*cf7f2e2dSJohn Marino        Also note that this mask may contain registers that are needed
138*cf7f2e2dSJohn Marino        for the original collection expression to work, but that are
139*cf7f2e2dSJohn Marino        not referenced by any bytecode.  This could, for example, occur
140*cf7f2e2dSJohn Marino        when collecting a local variable allocated to a register; the
141*cf7f2e2dSJohn Marino        compiler sets the mask bit and skips generating a bytecode whose
142*cf7f2e2dSJohn Marino        result is going to be discarded anyway.
143*cf7f2e2dSJohn Marino     */
144*cf7f2e2dSJohn Marino     int reg_mask_len;
145*cf7f2e2dSJohn Marino     unsigned char *reg_mask;
1465796c8dcSSimon Schubert   };
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert /* The actual values of the various bytecode operations.
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert    Other independent implementations of the agent bytecode engine will
1515796c8dcSSimon Schubert    rely on the exact values of these enums, and may not be recompiled
1525796c8dcSSimon Schubert    when we change this table.  The numeric values should remain fixed
1535796c8dcSSimon Schubert    whenever possible.  Thus, we assign them values explicitly here (to
1545796c8dcSSimon Schubert    allow gaps to form safely), and the disassembly table in
1555796c8dcSSimon Schubert    agentexpr.h behaves like an opcode map.  If you want to see them
1565796c8dcSSimon Schubert    grouped logically, see doc/agentexpr.texi.  */
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert enum agent_op
1595796c8dcSSimon Schubert   {
1605796c8dcSSimon Schubert     aop_float = 0x01,
1615796c8dcSSimon Schubert     aop_add = 0x02,
1625796c8dcSSimon Schubert     aop_sub = 0x03,
1635796c8dcSSimon Schubert     aop_mul = 0x04,
1645796c8dcSSimon Schubert     aop_div_signed = 0x05,
1655796c8dcSSimon Schubert     aop_div_unsigned = 0x06,
1665796c8dcSSimon Schubert     aop_rem_signed = 0x07,
1675796c8dcSSimon Schubert     aop_rem_unsigned = 0x08,
1685796c8dcSSimon Schubert     aop_lsh = 0x09,
1695796c8dcSSimon Schubert     aop_rsh_signed = 0x0a,
1705796c8dcSSimon Schubert     aop_rsh_unsigned = 0x0b,
1715796c8dcSSimon Schubert     aop_trace = 0x0c,
1725796c8dcSSimon Schubert     aop_trace_quick = 0x0d,
1735796c8dcSSimon Schubert     aop_log_not = 0x0e,
1745796c8dcSSimon Schubert     aop_bit_and = 0x0f,
1755796c8dcSSimon Schubert     aop_bit_or = 0x10,
1765796c8dcSSimon Schubert     aop_bit_xor = 0x11,
1775796c8dcSSimon Schubert     aop_bit_not = 0x12,
1785796c8dcSSimon Schubert     aop_equal = 0x13,
1795796c8dcSSimon Schubert     aop_less_signed = 0x14,
1805796c8dcSSimon Schubert     aop_less_unsigned = 0x15,
1815796c8dcSSimon Schubert     aop_ext = 0x16,
1825796c8dcSSimon Schubert     aop_ref8 = 0x17,
1835796c8dcSSimon Schubert     aop_ref16 = 0x18,
1845796c8dcSSimon Schubert     aop_ref32 = 0x19,
1855796c8dcSSimon Schubert     aop_ref64 = 0x1a,
1865796c8dcSSimon Schubert     aop_ref_float = 0x1b,
1875796c8dcSSimon Schubert     aop_ref_double = 0x1c,
1885796c8dcSSimon Schubert     aop_ref_long_double = 0x1d,
1895796c8dcSSimon Schubert     aop_l_to_d = 0x1e,
1905796c8dcSSimon Schubert     aop_d_to_l = 0x1f,
1915796c8dcSSimon Schubert     aop_if_goto = 0x20,
1925796c8dcSSimon Schubert     aop_goto = 0x21,
1935796c8dcSSimon Schubert     aop_const8 = 0x22,
1945796c8dcSSimon Schubert     aop_const16 = 0x23,
1955796c8dcSSimon Schubert     aop_const32 = 0x24,
1965796c8dcSSimon Schubert     aop_const64 = 0x25,
1975796c8dcSSimon Schubert     aop_reg = 0x26,
1985796c8dcSSimon Schubert     aop_end = 0x27,
1995796c8dcSSimon Schubert     aop_dup = 0x28,
2005796c8dcSSimon Schubert     aop_pop = 0x29,
2015796c8dcSSimon Schubert     aop_zero_ext = 0x2a,
2025796c8dcSSimon Schubert     aop_swap = 0x2b,
203*cf7f2e2dSJohn Marino     aop_getv = 0x2c,
204*cf7f2e2dSJohn Marino     aop_setv = 0x2d,
205*cf7f2e2dSJohn Marino     aop_tracev = 0x2e,
2065796c8dcSSimon Schubert     aop_trace16 = 0x30,
2075796c8dcSSimon Schubert     aop_last
2085796c8dcSSimon Schubert   };
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert /* Functions for building expressions.  */
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert /* Allocate a new, empty agent expression.  */
215*cf7f2e2dSJohn Marino extern struct agent_expr *new_agent_expr (struct gdbarch *, CORE_ADDR);
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert /* Free a agent expression.  */
2185796c8dcSSimon Schubert extern void free_agent_expr (struct agent_expr *);
2195796c8dcSSimon Schubert extern struct cleanup *make_cleanup_free_agent_expr (struct agent_expr *);
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert /* Append a simple operator OP to EXPR.  */
2225796c8dcSSimon Schubert extern void ax_simple (struct agent_expr *EXPR, enum agent_op OP);
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert /* Append the floating-point prefix, for the next bytecode.  */
2255796c8dcSSimon Schubert #define ax_float(EXPR) (ax_simple ((EXPR), aop_float))
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert /* Append a sign-extension instruction to EXPR, to extend an N-bit value.  */
2285796c8dcSSimon Schubert extern void ax_ext (struct agent_expr *EXPR, int N);
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert /* Append a zero-extension instruction to EXPR, to extend an N-bit value.  */
2315796c8dcSSimon Schubert extern void ax_zero_ext (struct agent_expr *EXPR, int N);
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert /* Append a trace_quick instruction to EXPR, to record N bytes.  */
2345796c8dcSSimon Schubert extern void ax_trace_quick (struct agent_expr *EXPR, int N);
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert /* Append a goto op to EXPR.  OP is the actual op (must be aop_goto or
2375796c8dcSSimon Schubert    aop_if_goto).  We assume we don't know the target offset yet,
2385796c8dcSSimon Schubert    because it's probably a forward branch, so we leave space in EXPR
2395796c8dcSSimon Schubert    for the target, and return the offset in EXPR of that space, so we
2405796c8dcSSimon Schubert    can backpatch it once we do know the target offset.  Use ax_label
2415796c8dcSSimon Schubert    to do the backpatching.  */
2425796c8dcSSimon Schubert extern int ax_goto (struct agent_expr *EXPR, enum agent_op OP);
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert /* Suppose a given call to ax_goto returns some value PATCH.  When you
2455796c8dcSSimon Schubert    know the offset TARGET that goto should jump to, call
2465796c8dcSSimon Schubert    ax_label (EXPR, PATCH, TARGET)
2475796c8dcSSimon Schubert    to patch TARGET into the ax_goto instruction.  */
2485796c8dcSSimon Schubert extern void ax_label (struct agent_expr *EXPR, int patch, int target);
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert /* Assemble code to push a constant on the stack.  */
2515796c8dcSSimon Schubert extern void ax_const_l (struct agent_expr *EXPR, LONGEST l);
2525796c8dcSSimon Schubert extern void ax_const_d (struct agent_expr *EXPR, LONGEST d);
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert /* Assemble code to push the value of register number REG on the
2555796c8dcSSimon Schubert    stack.  */
2565796c8dcSSimon Schubert extern void ax_reg (struct agent_expr *EXPR, int REG);
257*cf7f2e2dSJohn Marino 
258*cf7f2e2dSJohn Marino /* Add the given register to the register mask of the expression.  */
259*cf7f2e2dSJohn Marino extern void ax_reg_mask (struct agent_expr *ax, int reg);
260*cf7f2e2dSJohn Marino 
261*cf7f2e2dSJohn Marino /* Assemble code to operate on a trace state variable.  */
262*cf7f2e2dSJohn Marino extern void ax_tsv (struct agent_expr *expr, enum agent_op op, int num);
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert /* Functions for printing out expressions, and otherwise debugging
2665796c8dcSSimon Schubert    things.  */
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert /* Disassemble the expression EXPR, writing to F.  */
2695796c8dcSSimon Schubert extern void ax_print (struct ui_file *f, struct agent_expr * EXPR);
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert /* An entry in the opcode map.  */
2725796c8dcSSimon Schubert struct aop_map
2735796c8dcSSimon Schubert   {
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert     /* The name of the opcode.  Null means that this entry is not a
2765796c8dcSSimon Schubert        valid opcode --- a hole in the opcode space.  */
2775796c8dcSSimon Schubert     char *name;
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert     /* All opcodes take no operands from the bytecode stream, or take
2805796c8dcSSimon Schubert        unsigned integers of various sizes.  If this is a positive number
2815796c8dcSSimon Schubert        n, then the opcode is followed by an n-byte operand, which should
2825796c8dcSSimon Schubert        be printed as an unsigned integer.  If this is zero, then the
2835796c8dcSSimon Schubert        opcode takes no operands from the bytecode stream.
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert        If we get more complicated opcodes in the future, don't add other
2865796c8dcSSimon Schubert        magic values of this; that's a crock.  Add an `enum encoding'
2875796c8dcSSimon Schubert        field to this, or something like that.  */
2885796c8dcSSimon Schubert     int op_size;
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert     /* The size of the data operated upon, in bits, for bytecodes that
2915796c8dcSSimon Schubert        care about that (ref and const).  Zero for all others.  */
2925796c8dcSSimon Schubert     int data_size;
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert     /* Number of stack elements consumed, and number produced.  */
2955796c8dcSSimon Schubert     int consumed, produced;
2965796c8dcSSimon Schubert   };
2975796c8dcSSimon Schubert 
2985796c8dcSSimon Schubert /* Map of the bytecodes, indexed by bytecode number.  */
2995796c8dcSSimon Schubert extern struct aop_map aop_map[];
3005796c8dcSSimon Schubert 
301*cf7f2e2dSJohn Marino /* Given an agent expression AX, analyze and update its requirements.  */
3025796c8dcSSimon Schubert 
303*cf7f2e2dSJohn Marino extern void ax_reqs (struct agent_expr *ax);
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert #endif /* AGENTEXPR_H */
306