146283Sdfr/* Definitions for expressions designed to be executed on the agent 298944Sobrien Copyright 1998, 1999, 2000 Free Software Foundation, Inc. 346283Sdfr 498944Sobrien This file is part of GDB. 546283Sdfr 698944Sobrien This program is free software; you can redistribute it and/or modify 798944Sobrien it under the terms of the GNU General Public License as published by 898944Sobrien the Free Software Foundation; either version 2 of the License, or 998944Sobrien (at your option) any later version. 1046283Sdfr 1198944Sobrien This program is distributed in the hope that it will be useful, 1298944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1398944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1498944Sobrien GNU General Public License for more details. 1546283Sdfr 1698944Sobrien You should have received a copy of the GNU General Public License 1798944Sobrien along with this program; if not, write to the Free Software 1898944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 1998944Sobrien Boston, MA 02111-1307, USA. */ 2046283Sdfr 2146283Sdfr#ifndef AGENTEXPR_H 2246283Sdfr#define AGENTEXPR_H 2346283Sdfr 2498944Sobrien#include "doublest.h" /* For DOUBLEST. */ 2598944Sobrien 2646283Sdfr/* It's sometimes useful to be able to debug programs that you can't 2746283Sdfr really stop for more than a fraction of a second. To this end, the 2846283Sdfr user can specify a tracepoint (like a breakpoint, but you don't 2946283Sdfr stop at it), and specify a bunch of expressions to record the 3046283Sdfr values of when that tracepoint is reached. As the program runs, 3146283Sdfr GDB collects the values. At any point (possibly while values are 3246283Sdfr still being collected), the user can display the collected values. 3346283Sdfr 3446283Sdfr This is used with remote debugging; we don't really support it on 3546283Sdfr native configurations. 3646283Sdfr 3746283Sdfr This means that expressions are being evaluated by the remote agent, 3846283Sdfr which doesn't have any access to the symbol table information, and 3946283Sdfr needs to be small and simple. 4046283Sdfr 4146283Sdfr The agent_expr routines and datatypes are a bytecode language 4246283Sdfr designed to be executed by the agent. Agent expressions work in 4346283Sdfr terms of fixed-width values, operators, memory references, and 4446283Sdfr register references. You can evaluate a agent expression just given 4546283Sdfr a bunch of memory and register values to sniff at; you don't need 4646283Sdfr any symbolic information like variable names, types, etc. 4746283Sdfr 4846283Sdfr GDB translates source expressions, whose meaning depends on 4946283Sdfr symbolic information, into agent bytecode expressions, whose meaning 5046283Sdfr is independent of symbolic information. This means the agent can 5146283Sdfr evaluate them on the fly without reference to data only available 5246283Sdfr to the host GDB. */ 5398944Sobrien 5446283Sdfr 5546283Sdfr/* Agent expression data structures. */ 5646283Sdfr 5746283Sdfr/* The type of an element of the agent expression stack. 5846283Sdfr The bytecode operation indicates which element we should access; 5946283Sdfr the value itself has no typing information. GDB generates all 6046283Sdfr bytecode streams, so we don't have to worry about type errors. */ 6146283Sdfr 6298944Sobrienunion agent_val 6398944Sobrien { 6498944Sobrien LONGEST l; 6598944Sobrien DOUBLEST d; 6698944Sobrien }; 6746283Sdfr 6846283Sdfr/* A buffer containing a agent expression. */ 6998944Sobrienstruct agent_expr 7098944Sobrien { 7198944Sobrien unsigned char *buf; 7298944Sobrien int len; /* number of characters used */ 7398944Sobrien int size; /* allocated size */ 7498944Sobrien CORE_ADDR scope; 7598944Sobrien }; 7646283Sdfr 7746283Sdfr 7846283Sdfr 7946283Sdfr 8046283Sdfr/* The actual values of the various bytecode operations. 8146283Sdfr 8246283Sdfr Other independent implementations of the agent bytecode engine will 8346283Sdfr rely on the exact values of these enums, and may not be recompiled 8446283Sdfr when we change this table. The numeric values should remain fixed 8546283Sdfr whenever possible. Thus, we assign them values explicitly here (to 8646283Sdfr allow gaps to form safely), and the disassembly table in 8746283Sdfr agentexpr.h behaves like an opcode map. If you want to see them 8846283Sdfr grouped logically, see doc/agentexpr.texi. */ 8946283Sdfr 9098944Sobrienenum agent_op 9198944Sobrien { 9298944Sobrien aop_float = 0x01, 9398944Sobrien aop_add = 0x02, 9498944Sobrien aop_sub = 0x03, 9598944Sobrien aop_mul = 0x04, 9698944Sobrien aop_div_signed = 0x05, 9798944Sobrien aop_div_unsigned = 0x06, 9898944Sobrien aop_rem_signed = 0x07, 9998944Sobrien aop_rem_unsigned = 0x08, 10098944Sobrien aop_lsh = 0x09, 10198944Sobrien aop_rsh_signed = 0x0a, 10298944Sobrien aop_rsh_unsigned = 0x0b, 10398944Sobrien aop_trace = 0x0c, 10498944Sobrien aop_trace_quick = 0x0d, 10598944Sobrien aop_log_not = 0x0e, 10698944Sobrien aop_bit_and = 0x0f, 10798944Sobrien aop_bit_or = 0x10, 10898944Sobrien aop_bit_xor = 0x11, 10998944Sobrien aop_bit_not = 0x12, 11098944Sobrien aop_equal = 0x13, 11198944Sobrien aop_less_signed = 0x14, 11298944Sobrien aop_less_unsigned = 0x15, 11398944Sobrien aop_ext = 0x16, 11498944Sobrien aop_ref8 = 0x17, 11598944Sobrien aop_ref16 = 0x18, 11698944Sobrien aop_ref32 = 0x19, 11798944Sobrien aop_ref64 = 0x1a, 11898944Sobrien aop_ref_float = 0x1b, 11998944Sobrien aop_ref_double = 0x1c, 12098944Sobrien aop_ref_long_double = 0x1d, 12198944Sobrien aop_l_to_d = 0x1e, 12298944Sobrien aop_d_to_l = 0x1f, 12398944Sobrien aop_if_goto = 0x20, 12498944Sobrien aop_goto = 0x21, 12598944Sobrien aop_const8 = 0x22, 12698944Sobrien aop_const16 = 0x23, 12798944Sobrien aop_const32 = 0x24, 12898944Sobrien aop_const64 = 0x25, 12998944Sobrien aop_reg = 0x26, 13098944Sobrien aop_end = 0x27, 13198944Sobrien aop_dup = 0x28, 13298944Sobrien aop_pop = 0x29, 13398944Sobrien aop_zero_ext = 0x2a, 13498944Sobrien aop_swap = 0x2b, 13598944Sobrien aop_trace16 = 0x30, 13698944Sobrien aop_last 13798944Sobrien }; 13898944Sobrien 13946283Sdfr 14046283Sdfr 14146283Sdfr/* Functions for building expressions. */ 14246283Sdfr 14346283Sdfr/* Allocate a new, empty agent expression. */ 14498944Sobrienextern struct agent_expr *new_agent_expr (CORE_ADDR); 14546283Sdfr 14646283Sdfr/* Free a agent expression. */ 14798944Sobrienextern void free_agent_expr (struct agent_expr *); 14898944Sobrienextern struct cleanup *make_cleanup_free_agent_expr (struct agent_expr *); 14946283Sdfr 15046283Sdfr/* Append a simple operator OP to EXPR. */ 15198944Sobrienextern void ax_simple (struct agent_expr *EXPR, enum agent_op OP); 15246283Sdfr 15346283Sdfr/* Append the floating-point prefix, for the next bytecode. */ 15446283Sdfr#define ax_float(EXPR) (ax_simple ((EXPR), aop_float)) 15546283Sdfr 15646283Sdfr/* Append a sign-extension instruction to EXPR, to extend an N-bit value. */ 15798944Sobrienextern void ax_ext (struct agent_expr *EXPR, int N); 15846283Sdfr 15946283Sdfr/* Append a zero-extension instruction to EXPR, to extend an N-bit value. */ 16098944Sobrienextern void ax_zero_ext (struct agent_expr *EXPR, int N); 16146283Sdfr 16246283Sdfr/* Append a trace_quick instruction to EXPR, to record N bytes. */ 16398944Sobrienextern void ax_trace_quick (struct agent_expr *EXPR, int N); 16446283Sdfr 16546283Sdfr/* Append a goto op to EXPR. OP is the actual op (must be aop_goto or 16646283Sdfr aop_if_goto). We assume we don't know the target offset yet, 16746283Sdfr because it's probably a forward branch, so we leave space in EXPR 16846283Sdfr for the target, and return the offset in EXPR of that space, so we 16946283Sdfr can backpatch it once we do know the target offset. Use ax_label 17046283Sdfr to do the backpatching. */ 17198944Sobrienextern int ax_goto (struct agent_expr *EXPR, enum agent_op OP); 17246283Sdfr 17346283Sdfr/* Suppose a given call to ax_goto returns some value PATCH. When you 17446283Sdfr know the offset TARGET that goto should jump to, call 17598944Sobrien ax_label (EXPR, PATCH, TARGET) 17646283Sdfr to patch TARGET into the ax_goto instruction. */ 17798944Sobrienextern void ax_label (struct agent_expr *EXPR, int patch, int target); 17846283Sdfr 17946283Sdfr/* Assemble code to push a constant on the stack. */ 18098944Sobrienextern void ax_const_l (struct agent_expr *EXPR, LONGEST l); 18198944Sobrienextern void ax_const_d (struct agent_expr *EXPR, LONGEST d); 18246283Sdfr 18346283Sdfr/* Assemble code to push the value of register number REG on the 18446283Sdfr stack. */ 18598944Sobrienextern void ax_reg (struct agent_expr *EXPR, int REG); 18698944Sobrien 18746283Sdfr 18846283Sdfr/* Functions for printing out expressions, and otherwise debugging 18946283Sdfr things. */ 19046283Sdfr 19146283Sdfr/* Disassemble the expression EXPR, writing to F. */ 19298944Sobrienextern void ax_print (struct ui_file *f, struct agent_expr * EXPR); 19346283Sdfr 19446283Sdfr/* An entry in the opcode map. */ 19598944Sobrienstruct aop_map 19698944Sobrien { 19746283Sdfr 19898944Sobrien /* The name of the opcode. Null means that this entry is not a 19998944Sobrien valid opcode --- a hole in the opcode space. */ 20098944Sobrien char *name; 20146283Sdfr 20298944Sobrien /* All opcodes take no operands from the bytecode stream, or take 20398944Sobrien unsigned integers of various sizes. If this is a positive number 20498944Sobrien n, then the opcode is followed by an n-byte operand, which should 20598944Sobrien be printed as an unsigned integer. If this is zero, then the 20698944Sobrien opcode takes no operands from the bytecode stream. 20746283Sdfr 20898944Sobrien If we get more complicated opcodes in the future, don't add other 20998944Sobrien magic values of this; that's a crock. Add an `enum encoding' 21098944Sobrien field to this, or something like that. */ 21198944Sobrien int op_size; 21246283Sdfr 21398944Sobrien /* The size of the data operated upon, in bits, for bytecodes that 21498944Sobrien care about that (ref and const). Zero for all others. */ 21598944Sobrien int data_size; 21646283Sdfr 21798944Sobrien /* Number of stack elements consumed, and number produced. */ 21898944Sobrien int consumed, produced; 21998944Sobrien }; 22046283Sdfr 22146283Sdfr/* Map of the bytecodes, indexed by bytecode number. */ 22246283Sdfrextern struct aop_map aop_map[]; 22346283Sdfr 22446283Sdfr/* Different kinds of flaws an agent expression might have, as 22546283Sdfr detected by agent_reqs. */ 22698944Sobrienenum agent_flaws 22798944Sobrien { 22898944Sobrien agent_flaw_none = 0, /* code is good */ 22946283Sdfr 23098944Sobrien /* There is an invalid instruction in the stream. */ 23198944Sobrien agent_flaw_bad_instruction, 23246283Sdfr 23398944Sobrien /* There is an incomplete instruction at the end of the expression. */ 23498944Sobrien agent_flaw_incomplete_instruction, 23546283Sdfr 23698944Sobrien /* agent_reqs was unable to prove that every jump target is to a 23798944Sobrien valid offset. Valid offsets are within the bounds of the 23898944Sobrien expression, and to a valid instruction boundary. */ 23998944Sobrien agent_flaw_bad_jump, 24046283Sdfr 24198944Sobrien /* agent_reqs was unable to prove to its satisfaction that, for each 24298944Sobrien jump target location, the stack will have the same height whether 24398944Sobrien that location is reached via a jump or by straight execution. */ 24498944Sobrien agent_flaw_height_mismatch, 24546283Sdfr 24698944Sobrien /* agent_reqs was unable to prove that every instruction following 24798944Sobrien an unconditional jump was the target of some other jump. */ 24898944Sobrien agent_flaw_hole 24998944Sobrien }; 25046283Sdfr 25146283Sdfr/* Structure describing the requirements of a bytecode expression. */ 25298944Sobrienstruct agent_reqs 25398944Sobrien { 25446283Sdfr 25598944Sobrien /* If the following is not equal to agent_flaw_none, the rest of the 25698944Sobrien information in this structure is suspect. */ 25798944Sobrien enum agent_flaws flaw; 25846283Sdfr 25998944Sobrien /* Number of elements left on stack at end; may be negative if expr 26098944Sobrien only consumes elements. */ 26198944Sobrien int final_height; 26246283Sdfr 26398944Sobrien /* Maximum and minimum stack height, relative to initial height. */ 26498944Sobrien int max_height, min_height; 26546283Sdfr 26698944Sobrien /* Largest `ref' or `const' opcode used, in bits. Zero means the 26798944Sobrien expression has no such instructions. */ 26898944Sobrien int max_data_size; 26946283Sdfr 27098944Sobrien /* Bit vector of registers used. Register R is used iff 27146283Sdfr 27298944Sobrien reg_mask[R / 8] & (1 << (R % 8)) 27346283Sdfr 27498944Sobrien is non-zero. Note! You may not assume that this bitmask is long 27598944Sobrien enough to hold bits for all the registers of the machine; the 27698944Sobrien agent expression code has no idea how many registers the machine 27798944Sobrien has. However, the bitmask is reg_mask_len bytes long, so the 27898944Sobrien valid register numbers run from 0 to reg_mask_len * 8 - 1. 27946283Sdfr 28098944Sobrien We're assuming eight-bit bytes. So sue me. 28146283Sdfr 28298944Sobrien The caller should free reg_list when done. */ 28398944Sobrien int reg_mask_len; 28498944Sobrien unsigned char *reg_mask; 28598944Sobrien }; 28646283Sdfr 28798944Sobrien 28846283Sdfr/* Given an agent expression AX, fill in an agent_reqs structure REQS 28946283Sdfr describing it. */ 29098944Sobrienextern void ax_reqs (struct agent_expr *ax, struct agent_reqs *reqs); 29146283Sdfr 29246283Sdfr#endif /* AGENTEXPR_H */ 293