15796c8dcSSimon Schubert /* Definitions for expressions designed to be executed on the agent 2*ef5ccd6cSJohn Marino Copyright (C) 1998-2013 Free Software Foundation, Inc. 35796c8dcSSimon Schubert 45796c8dcSSimon Schubert This file is part of GDB. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 75796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 85796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 95796c8dcSSimon Schubert (at your option) any later version. 105796c8dcSSimon Schubert 115796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 125796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 135796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 145796c8dcSSimon Schubert GNU General Public License for more details. 155796c8dcSSimon Schubert 165796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 175796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 185796c8dcSSimon Schubert 195796c8dcSSimon Schubert #ifndef AGENTEXPR_H 205796c8dcSSimon Schubert #define AGENTEXPR_H 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "doublest.h" /* For DOUBLEST. */ 23*ef5ccd6cSJohn Marino #include "vec.h" 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 54cf7f2e2dSJohn Marino /* Different kinds of flaws an agent expression might have, as 55cf7f2e2dSJohn Marino detected by ax_reqs. */ 56cf7f2e2dSJohn Marino enum agent_flaws 57cf7f2e2dSJohn Marino { 58cf7f2e2dSJohn Marino agent_flaw_none = 0, /* code is good */ 59cf7f2e2dSJohn Marino 60cf7f2e2dSJohn Marino /* There is an invalid instruction in the stream. */ 61cf7f2e2dSJohn Marino agent_flaw_bad_instruction, 62cf7f2e2dSJohn Marino 63cf7f2e2dSJohn Marino /* There is an incomplete instruction at the end of the expression. */ 64cf7f2e2dSJohn Marino agent_flaw_incomplete_instruction, 65cf7f2e2dSJohn Marino 66cf7f2e2dSJohn Marino /* ax_reqs was unable to prove that every jump target is to a 67cf7f2e2dSJohn Marino valid offset. Valid offsets are within the bounds of the 68cf7f2e2dSJohn Marino expression, and to a valid instruction boundary. */ 69cf7f2e2dSJohn Marino agent_flaw_bad_jump, 70cf7f2e2dSJohn Marino 71cf7f2e2dSJohn Marino /* ax_reqs was unable to prove to its satisfaction that, for each 72cf7f2e2dSJohn Marino jump target location, the stack will have the same height whether 73cf7f2e2dSJohn Marino that location is reached via a jump or by straight execution. */ 74cf7f2e2dSJohn Marino agent_flaw_height_mismatch, 75cf7f2e2dSJohn Marino 76cf7f2e2dSJohn Marino /* ax_reqs was unable to prove that every instruction following 77cf7f2e2dSJohn Marino an unconditional jump was the target of some other jump. */ 78cf7f2e2dSJohn Marino agent_flaw_hole 79cf7f2e2dSJohn Marino }; 80cf7f2e2dSJohn 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 { 97cf7f2e2dSJohn Marino /* The bytes of the expression. */ 985796c8dcSSimon Schubert unsigned char *buf; 99cf7f2e2dSJohn Marino 100cf7f2e2dSJohn Marino /* The number of bytecode in the expression. */ 101cf7f2e2dSJohn Marino int len; 102cf7f2e2dSJohn Marino 103cf7f2e2dSJohn Marino /* Allocated space available currently. */ 104cf7f2e2dSJohn Marino int size; 105cf7f2e2dSJohn Marino 106cf7f2e2dSJohn Marino /* The target architecture assumed to be in effect. */ 107cf7f2e2dSJohn Marino struct gdbarch *gdbarch; 108cf7f2e2dSJohn Marino 109cf7f2e2dSJohn Marino /* The address to which the expression applies. */ 1105796c8dcSSimon Schubert CORE_ADDR scope; 111cf7f2e2dSJohn Marino 112cf7f2e2dSJohn Marino /* If the following is not equal to agent_flaw_none, the rest of the 113cf7f2e2dSJohn Marino information in this structure is suspect. */ 114cf7f2e2dSJohn Marino enum agent_flaws flaw; 115cf7f2e2dSJohn Marino 116cf7f2e2dSJohn Marino /* Number of elements left on stack at end; may be negative if expr 117cf7f2e2dSJohn Marino only consumes elements. */ 118cf7f2e2dSJohn Marino int final_height; 119cf7f2e2dSJohn Marino 120cf7f2e2dSJohn Marino /* Maximum and minimum stack height, relative to initial height. */ 121cf7f2e2dSJohn Marino int max_height, min_height; 122cf7f2e2dSJohn Marino 123cf7f2e2dSJohn Marino /* Largest `ref' or `const' opcode used, in bits. Zero means the 124cf7f2e2dSJohn Marino expression has no such instructions. */ 125cf7f2e2dSJohn Marino int max_data_size; 126cf7f2e2dSJohn Marino 127cf7f2e2dSJohn Marino /* Bit vector of registers needed. Register R is needed iff 128cf7f2e2dSJohn Marino 129cf7f2e2dSJohn Marino reg_mask[R / 8] & (1 << (R % 8)) 130cf7f2e2dSJohn Marino 131cf7f2e2dSJohn Marino is non-zero. Note! You may not assume that this bitmask is long 132cf7f2e2dSJohn Marino enough to hold bits for all the registers of the machine; the 133cf7f2e2dSJohn Marino agent expression code has no idea how many registers the machine 134cf7f2e2dSJohn Marino has. However, the bitmask is reg_mask_len bytes long, so the 135cf7f2e2dSJohn Marino valid register numbers run from 0 to reg_mask_len * 8 - 1. 136cf7f2e2dSJohn Marino 137cf7f2e2dSJohn Marino Also note that this mask may contain registers that are needed 138cf7f2e2dSJohn Marino for the original collection expression to work, but that are 139cf7f2e2dSJohn Marino not referenced by any bytecode. This could, for example, occur 140cf7f2e2dSJohn Marino when collecting a local variable allocated to a register; the 141cf7f2e2dSJohn Marino compiler sets the mask bit and skips generating a bytecode whose 142cf7f2e2dSJohn Marino result is going to be discarded anyway. 143cf7f2e2dSJohn Marino */ 144cf7f2e2dSJohn Marino int reg_mask_len; 145cf7f2e2dSJohn Marino unsigned char *reg_mask; 1465796c8dcSSimon Schubert }; 1475796c8dcSSimon Schubert 148*ef5ccd6cSJohn Marino /* Pointer to an agent_expr structure. */ 149*ef5ccd6cSJohn Marino typedef struct agent_expr *agent_expr_p; 150*ef5ccd6cSJohn Marino 151*ef5ccd6cSJohn Marino /* Vector of pointers to agent expressions. */ 152*ef5ccd6cSJohn Marino DEF_VEC_P (agent_expr_p); 153*ef5ccd6cSJohn Marino 154c50c785cSJohn Marino /* The actual values of the various bytecode operations. */ 1555796c8dcSSimon Schubert 1565796c8dcSSimon Schubert enum agent_op 1575796c8dcSSimon Schubert { 158c50c785cSJohn Marino #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \ 159c50c785cSJohn Marino aop_ ## NAME = VALUE, 160c50c785cSJohn Marino #include "ax.def" 161c50c785cSJohn Marino #undef DEFOP 1625796c8dcSSimon Schubert aop_last 1635796c8dcSSimon Schubert }; 1645796c8dcSSimon Schubert 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert 1675796c8dcSSimon Schubert /* Functions for building expressions. */ 1685796c8dcSSimon Schubert 1695796c8dcSSimon Schubert /* Allocate a new, empty agent expression. */ 170cf7f2e2dSJohn Marino extern struct agent_expr *new_agent_expr (struct gdbarch *, CORE_ADDR); 1715796c8dcSSimon Schubert 1725796c8dcSSimon Schubert /* Free a agent expression. */ 1735796c8dcSSimon Schubert extern void free_agent_expr (struct agent_expr *); 1745796c8dcSSimon Schubert extern struct cleanup *make_cleanup_free_agent_expr (struct agent_expr *); 1755796c8dcSSimon Schubert 1765796c8dcSSimon Schubert /* Append a simple operator OP to EXPR. */ 1775796c8dcSSimon Schubert extern void ax_simple (struct agent_expr *EXPR, enum agent_op OP); 1785796c8dcSSimon Schubert 179c50c785cSJohn Marino /* Append a pick operator to EXPR. DEPTH is the stack item to pick, 180c50c785cSJohn Marino with 0 being top of stack. */ 181c50c785cSJohn Marino extern void ax_pick (struct agent_expr *EXPR, int DEPTH); 182c50c785cSJohn Marino 1835796c8dcSSimon Schubert /* Append the floating-point prefix, for the next bytecode. */ 1845796c8dcSSimon Schubert #define ax_float(EXPR) (ax_simple ((EXPR), aop_float)) 1855796c8dcSSimon Schubert 1865796c8dcSSimon Schubert /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */ 1875796c8dcSSimon Schubert extern void ax_ext (struct agent_expr *EXPR, int N); 1885796c8dcSSimon Schubert 1895796c8dcSSimon Schubert /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */ 1905796c8dcSSimon Schubert extern void ax_zero_ext (struct agent_expr *EXPR, int N); 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert /* Append a trace_quick instruction to EXPR, to record N bytes. */ 1935796c8dcSSimon Schubert extern void ax_trace_quick (struct agent_expr *EXPR, int N); 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or 1965796c8dcSSimon Schubert aop_if_goto). We assume we don't know the target offset yet, 1975796c8dcSSimon Schubert because it's probably a forward branch, so we leave space in EXPR 1985796c8dcSSimon Schubert for the target, and return the offset in EXPR of that space, so we 1995796c8dcSSimon Schubert can backpatch it once we do know the target offset. Use ax_label 2005796c8dcSSimon Schubert to do the backpatching. */ 2015796c8dcSSimon Schubert extern int ax_goto (struct agent_expr *EXPR, enum agent_op OP); 2025796c8dcSSimon Schubert 2035796c8dcSSimon Schubert /* Suppose a given call to ax_goto returns some value PATCH. When you 2045796c8dcSSimon Schubert know the offset TARGET that goto should jump to, call 2055796c8dcSSimon Schubert ax_label (EXPR, PATCH, TARGET) 2065796c8dcSSimon Schubert to patch TARGET into the ax_goto instruction. */ 2075796c8dcSSimon Schubert extern void ax_label (struct agent_expr *EXPR, int patch, int target); 2085796c8dcSSimon Schubert 2095796c8dcSSimon Schubert /* Assemble code to push a constant on the stack. */ 2105796c8dcSSimon Schubert extern void ax_const_l (struct agent_expr *EXPR, LONGEST l); 2115796c8dcSSimon Schubert extern void ax_const_d (struct agent_expr *EXPR, LONGEST d); 2125796c8dcSSimon Schubert 2135796c8dcSSimon Schubert /* Assemble code to push the value of register number REG on the 2145796c8dcSSimon Schubert stack. */ 2155796c8dcSSimon Schubert extern void ax_reg (struct agent_expr *EXPR, int REG); 216cf7f2e2dSJohn Marino 217cf7f2e2dSJohn Marino /* Add the given register to the register mask of the expression. */ 218cf7f2e2dSJohn Marino extern void ax_reg_mask (struct agent_expr *ax, int reg); 219cf7f2e2dSJohn Marino 220cf7f2e2dSJohn Marino /* Assemble code to operate on a trace state variable. */ 221cf7f2e2dSJohn Marino extern void ax_tsv (struct agent_expr *expr, enum agent_op op, int num); 222*ef5ccd6cSJohn Marino 223*ef5ccd6cSJohn Marino /* Append a string to the bytecode stream. */ 224*ef5ccd6cSJohn Marino extern void ax_string (struct agent_expr *x, const char *str, int slen); 2255796c8dcSSimon Schubert 2265796c8dcSSimon Schubert 2275796c8dcSSimon Schubert /* Functions for printing out expressions, and otherwise debugging 2285796c8dcSSimon Schubert things. */ 2295796c8dcSSimon Schubert 2305796c8dcSSimon Schubert /* Disassemble the expression EXPR, writing to F. */ 2315796c8dcSSimon Schubert extern void ax_print (struct ui_file *f, struct agent_expr * EXPR); 2325796c8dcSSimon Schubert 2335796c8dcSSimon Schubert /* An entry in the opcode map. */ 2345796c8dcSSimon Schubert struct aop_map 2355796c8dcSSimon Schubert { 2365796c8dcSSimon Schubert 2375796c8dcSSimon Schubert /* The name of the opcode. Null means that this entry is not a 2385796c8dcSSimon Schubert valid opcode --- a hole in the opcode space. */ 239c50c785cSJohn Marino const char *name; 2405796c8dcSSimon Schubert 2415796c8dcSSimon Schubert /* All opcodes take no operands from the bytecode stream, or take 2425796c8dcSSimon Schubert unsigned integers of various sizes. If this is a positive number 2435796c8dcSSimon Schubert n, then the opcode is followed by an n-byte operand, which should 2445796c8dcSSimon Schubert be printed as an unsigned integer. If this is zero, then the 2455796c8dcSSimon Schubert opcode takes no operands from the bytecode stream. 2465796c8dcSSimon Schubert 2475796c8dcSSimon Schubert If we get more complicated opcodes in the future, don't add other 2485796c8dcSSimon Schubert magic values of this; that's a crock. Add an `enum encoding' 2495796c8dcSSimon Schubert field to this, or something like that. */ 2505796c8dcSSimon Schubert int op_size; 2515796c8dcSSimon Schubert 2525796c8dcSSimon Schubert /* The size of the data operated upon, in bits, for bytecodes that 2535796c8dcSSimon Schubert care about that (ref and const). Zero for all others. */ 2545796c8dcSSimon Schubert int data_size; 2555796c8dcSSimon Schubert 2565796c8dcSSimon Schubert /* Number of stack elements consumed, and number produced. */ 2575796c8dcSSimon Schubert int consumed, produced; 2585796c8dcSSimon Schubert }; 2595796c8dcSSimon Schubert 2605796c8dcSSimon Schubert /* Map of the bytecodes, indexed by bytecode number. */ 2615796c8dcSSimon Schubert extern struct aop_map aop_map[]; 2625796c8dcSSimon Schubert 263cf7f2e2dSJohn Marino /* Given an agent expression AX, analyze and update its requirements. */ 2645796c8dcSSimon Schubert 265cf7f2e2dSJohn Marino extern void ax_reqs (struct agent_expr *ax); 2665796c8dcSSimon Schubert 2675796c8dcSSimon Schubert #endif /* AGENTEXPR_H */ 268