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