119370Spst/* Low level packing and unpacking of values for GDB, the GNU Debugger. 2130803Smarcel 398944Sobrien Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 4130803Smarcel 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003 Free Software 5130803Smarcel Foundation, Inc. 619370Spst 798944Sobrien This file is part of GDB. 819370Spst 998944Sobrien This program is free software; you can redistribute it and/or modify 1098944Sobrien it under the terms of the GNU General Public License as published by 1198944Sobrien the Free Software Foundation; either version 2 of the License, or 1298944Sobrien (at your option) any later version. 1319370Spst 1498944Sobrien This program is distributed in the hope that it will be useful, 1598944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1698944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1798944Sobrien GNU General Public License for more details. 1819370Spst 1998944Sobrien You should have received a copy of the GNU General Public License 2098944Sobrien along with this program; if not, write to the Free Software 2198944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2298944Sobrien Boston, MA 02111-1307, USA. */ 2319370Spst 2419370Spst#include "defs.h" 2519370Spst#include "gdb_string.h" 2619370Spst#include "symtab.h" 2719370Spst#include "gdbtypes.h" 2819370Spst#include "value.h" 2919370Spst#include "gdbcore.h" 3019370Spst#include "command.h" 3119370Spst#include "gdbcmd.h" 3219370Spst#include "target.h" 3319370Spst#include "language.h" 3419370Spst#include "scm-lang.h" 3519370Spst#include "demangle.h" 3698944Sobrien#include "doublest.h" 3798944Sobrien#include "gdb_assert.h" 38130803Smarcel#include "regcache.h" 39130803Smarcel#include "block.h" 4019370Spst 4146283Sdfr/* Prototypes for exported functions. */ 4219370Spst 4398944Sobrienvoid _initialize_values (void); 4446283Sdfr 4546283Sdfr/* Prototypes for local functions. */ 4646283Sdfr 4798944Sobrienstatic void show_values (char *, int); 4819370Spst 4998944Sobrienstatic void show_convenience (char *, int); 5019370Spst 5146283Sdfr 5219370Spst/* The value-history records all the values printed 5319370Spst by print commands during this session. Each chunk 5419370Spst records 60 consecutive values. The first chunk on 5519370Spst the chain records the most recent values. 5619370Spst The total number of values is in value_history_count. */ 5719370Spst 5819370Spst#define VALUE_HISTORY_CHUNK 60 5919370Spst 6019370Spststruct value_history_chunk 6198944Sobrien { 6298944Sobrien struct value_history_chunk *next; 6398944Sobrien struct value *values[VALUE_HISTORY_CHUNK]; 6498944Sobrien }; 6519370Spst 6619370Spst/* Chain of chunks now in use. */ 6719370Spst 6819370Spststatic struct value_history_chunk *value_history_chain; 6919370Spst 7019370Spststatic int value_history_count; /* Abs number of last entry stored */ 7119370Spst 7219370Spst/* List of all value objects currently allocated 7319370Spst (except for those released by calls to release_value) 7419370Spst This is so they can be freed after each command. */ 7519370Spst 7698944Sobrienstatic struct value *all_values; 7719370Spst 7819370Spst/* Allocate a value that has the correct length for type TYPE. */ 7919370Spst 8098944Sobrienstruct value * 8198944Sobrienallocate_value (struct type *type) 8219370Spst{ 8398944Sobrien struct value *val; 8419370Spst struct type *atype = check_typedef (type); 8519370Spst 8619370Spst val = (struct value *) xmalloc (sizeof (struct value) + TYPE_LENGTH (atype)); 8719370Spst VALUE_NEXT (val) = all_values; 8819370Spst all_values = val; 8919370Spst VALUE_TYPE (val) = type; 9046283Sdfr VALUE_ENCLOSING_TYPE (val) = type; 9119370Spst VALUE_LVAL (val) = not_lval; 9219370Spst VALUE_ADDRESS (val) = 0; 93130803Smarcel VALUE_FRAME_ID (val) = null_frame_id; 9419370Spst VALUE_OFFSET (val) = 0; 9519370Spst VALUE_BITPOS (val) = 0; 9619370Spst VALUE_BITSIZE (val) = 0; 9719370Spst VALUE_REGNO (val) = -1; 9819370Spst VALUE_LAZY (val) = 0; 9919370Spst VALUE_OPTIMIZED_OUT (val) = 0; 10046283Sdfr VALUE_BFD_SECTION (val) = NULL; 10146283Sdfr VALUE_EMBEDDED_OFFSET (val) = 0; 10246283Sdfr VALUE_POINTED_TO_OFFSET (val) = 0; 10319370Spst val->modifiable = 1; 10419370Spst return val; 10519370Spst} 10619370Spst 10719370Spst/* Allocate a value that has the correct length 10819370Spst for COUNT repetitions type TYPE. */ 10919370Spst 11098944Sobrienstruct value * 11198944Sobrienallocate_repeat_value (struct type *type, int count) 11219370Spst{ 11398944Sobrien int low_bound = current_language->string_lower_bound; /* ??? */ 11419370Spst /* FIXME-type-allocation: need a way to free this type when we are 11519370Spst done with it. */ 11619370Spst struct type *range_type 11798944Sobrien = create_range_type ((struct type *) NULL, builtin_type_int, 11898944Sobrien low_bound, count + low_bound - 1); 11919370Spst /* FIXME-type-allocation: need a way to free this type when we are 12019370Spst done with it. */ 12119370Spst return allocate_value (create_array_type ((struct type *) NULL, 12219370Spst type, range_type)); 12319370Spst} 12419370Spst 12519370Spst/* Return a mark in the value chain. All values allocated after the 12619370Spst mark is obtained (except for those released) are subject to being freed 12719370Spst if a subsequent value_free_to_mark is passed the mark. */ 12898944Sobrienstruct value * 12998944Sobrienvalue_mark (void) 13019370Spst{ 13119370Spst return all_values; 13219370Spst} 13319370Spst 13419370Spst/* Free all values allocated since MARK was obtained by value_mark 13519370Spst (except for those released). */ 13619370Spstvoid 13798944Sobrienvalue_free_to_mark (struct value *mark) 13819370Spst{ 13998944Sobrien struct value *val; 14098944Sobrien struct value *next; 14119370Spst 14219370Spst for (val = all_values; val && val != mark; val = next) 14319370Spst { 14419370Spst next = VALUE_NEXT (val); 14519370Spst value_free (val); 14619370Spst } 14719370Spst all_values = val; 14819370Spst} 14919370Spst 15019370Spst/* Free all the values that have been allocated (except for those released). 15119370Spst Called after each command, successful or not. */ 15219370Spst 15319370Spstvoid 15498944Sobrienfree_all_values (void) 15519370Spst{ 15698944Sobrien struct value *val; 15798944Sobrien struct value *next; 15819370Spst 15919370Spst for (val = all_values; val; val = next) 16019370Spst { 16119370Spst next = VALUE_NEXT (val); 16219370Spst value_free (val); 16319370Spst } 16419370Spst 16519370Spst all_values = 0; 16619370Spst} 16719370Spst 16819370Spst/* Remove VAL from the chain all_values 16919370Spst so it will not be freed automatically. */ 17019370Spst 17119370Spstvoid 17298944Sobrienrelease_value (struct value *val) 17319370Spst{ 17498944Sobrien struct value *v; 17519370Spst 17619370Spst if (all_values == val) 17719370Spst { 17819370Spst all_values = val->next; 17919370Spst return; 18019370Spst } 18119370Spst 18219370Spst for (v = all_values; v; v = v->next) 18319370Spst { 18419370Spst if (v->next == val) 18519370Spst { 18619370Spst v->next = val->next; 18719370Spst break; 18819370Spst } 18919370Spst } 19019370Spst} 19119370Spst 19219370Spst/* Release all values up to mark */ 19398944Sobrienstruct value * 19498944Sobrienvalue_release_to_mark (struct value *mark) 19519370Spst{ 19698944Sobrien struct value *val; 19798944Sobrien struct value *next; 19819370Spst 19919370Spst for (val = next = all_values; next; next = VALUE_NEXT (next)) 20019370Spst if (VALUE_NEXT (next) == mark) 20119370Spst { 20219370Spst all_values = VALUE_NEXT (next); 20319370Spst VALUE_NEXT (next) = 0; 20419370Spst return val; 20519370Spst } 20619370Spst all_values = 0; 20719370Spst return val; 20819370Spst} 20919370Spst 21019370Spst/* Return a copy of the value ARG. 21119370Spst It contains the same contents, for same memory address, 21219370Spst but it's a different block of storage. */ 21319370Spst 21498944Sobrienstruct value * 21598944Sobrienvalue_copy (struct value *arg) 21619370Spst{ 217130803Smarcel struct type *encl_type = VALUE_ENCLOSING_TYPE (arg); 21898944Sobrien struct value *val = allocate_value (encl_type); 21946283Sdfr VALUE_TYPE (val) = VALUE_TYPE (arg); 22019370Spst VALUE_LVAL (val) = VALUE_LVAL (arg); 22119370Spst VALUE_ADDRESS (val) = VALUE_ADDRESS (arg); 22219370Spst VALUE_OFFSET (val) = VALUE_OFFSET (arg); 22319370Spst VALUE_BITPOS (val) = VALUE_BITPOS (arg); 22419370Spst VALUE_BITSIZE (val) = VALUE_BITSIZE (arg); 225130803Smarcel VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg); 22619370Spst VALUE_REGNO (val) = VALUE_REGNO (arg); 22719370Spst VALUE_LAZY (val) = VALUE_LAZY (arg); 22819370Spst VALUE_OPTIMIZED_OUT (val) = VALUE_OPTIMIZED_OUT (arg); 22946283Sdfr VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (arg); 23046283Sdfr VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (arg); 23146283Sdfr VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (arg); 23219370Spst val->modifiable = arg->modifiable; 23319370Spst if (!VALUE_LAZY (val)) 23419370Spst { 23546283Sdfr memcpy (VALUE_CONTENTS_ALL_RAW (val), VALUE_CONTENTS_ALL_RAW (arg), 23646283Sdfr TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg))); 23746283Sdfr 23819370Spst } 23919370Spst return val; 24019370Spst} 24119370Spst 24219370Spst/* Access to the value history. */ 24319370Spst 24419370Spst/* Record a new value in the value history. 24519370Spst Returns the absolute history index of the entry. 24619370Spst Result of -1 indicates the value was not saved; otherwise it is the 24719370Spst value history index of this new item. */ 24819370Spst 24919370Spstint 25098944Sobrienrecord_latest_value (struct value *val) 25119370Spst{ 25219370Spst int i; 25319370Spst 25419370Spst /* We don't want this value to have anything to do with the inferior anymore. 25519370Spst In particular, "set $1 = 50" should not affect the variable from which 25619370Spst the value was taken, and fast watchpoints should be able to assume that 25719370Spst a value on the value history never changes. */ 25819370Spst if (VALUE_LAZY (val)) 25919370Spst value_fetch_lazy (val); 26019370Spst /* We preserve VALUE_LVAL so that the user can find out where it was fetched 26119370Spst from. This is a bit dubious, because then *&$1 does not just return $1 26219370Spst but the current contents of that location. c'est la vie... */ 26319370Spst val->modifiable = 0; 26419370Spst release_value (val); 26519370Spst 26619370Spst /* Here we treat value_history_count as origin-zero 26719370Spst and applying to the value being stored now. */ 26819370Spst 26919370Spst i = value_history_count % VALUE_HISTORY_CHUNK; 27019370Spst if (i == 0) 27119370Spst { 27298944Sobrien struct value_history_chunk *new 27398944Sobrien = (struct value_history_chunk *) 27498944Sobrien xmalloc (sizeof (struct value_history_chunk)); 27519370Spst memset (new->values, 0, sizeof new->values); 27619370Spst new->next = value_history_chain; 27719370Spst value_history_chain = new; 27819370Spst } 27919370Spst 28019370Spst value_history_chain->values[i] = val; 28119370Spst 28219370Spst /* Now we regard value_history_count as origin-one 28319370Spst and applying to the value just stored. */ 28419370Spst 28519370Spst return ++value_history_count; 28619370Spst} 28719370Spst 28819370Spst/* Return a copy of the value in the history with sequence number NUM. */ 28919370Spst 29098944Sobrienstruct value * 29198944Sobrienaccess_value_history (int num) 29219370Spst{ 29398944Sobrien struct value_history_chunk *chunk; 294130803Smarcel int i; 295130803Smarcel int absnum = num; 29619370Spst 29719370Spst if (absnum <= 0) 29819370Spst absnum += value_history_count; 29919370Spst 30019370Spst if (absnum <= 0) 30119370Spst { 30219370Spst if (num == 0) 30319370Spst error ("The history is empty."); 30419370Spst else if (num == 1) 30519370Spst error ("There is only one value in the history."); 30619370Spst else 30719370Spst error ("History does not go back to $$%d.", -num); 30819370Spst } 30919370Spst if (absnum > value_history_count) 31019370Spst error ("History has not yet reached $%d.", absnum); 31119370Spst 31219370Spst absnum--; 31319370Spst 31419370Spst /* Now absnum is always absolute and origin zero. */ 31519370Spst 31619370Spst chunk = value_history_chain; 31719370Spst for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK; 31819370Spst i > 0; i--) 31919370Spst chunk = chunk->next; 32019370Spst 32119370Spst return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]); 32219370Spst} 32319370Spst 32419370Spst/* Clear the value history entirely. 32519370Spst Must be done when new symbol tables are loaded, 32619370Spst because the type pointers become invalid. */ 32719370Spst 32819370Spstvoid 32998944Sobrienclear_value_history (void) 33019370Spst{ 33198944Sobrien struct value_history_chunk *next; 332130803Smarcel int i; 33398944Sobrien struct value *val; 33419370Spst 33519370Spst while (value_history_chain) 33619370Spst { 33719370Spst for (i = 0; i < VALUE_HISTORY_CHUNK; i++) 33819370Spst if ((val = value_history_chain->values[i]) != NULL) 33998944Sobrien xfree (val); 34019370Spst next = value_history_chain->next; 34198944Sobrien xfree (value_history_chain); 34219370Spst value_history_chain = next; 34319370Spst } 34419370Spst value_history_count = 0; 34519370Spst} 34619370Spst 34719370Spststatic void 34898944Sobrienshow_values (char *num_exp, int from_tty) 34919370Spst{ 350130803Smarcel int i; 35198944Sobrien struct value *val; 35219370Spst static int num = 1; 35319370Spst 35419370Spst if (num_exp) 35519370Spst { 35698944Sobrien /* "info history +" should print from the stored position. 35798944Sobrien "info history <exp>" should print around value number <exp>. */ 35819370Spst if (num_exp[0] != '+' || num_exp[1] != '\0') 35998944Sobrien num = parse_and_eval_long (num_exp) - 5; 36019370Spst } 36119370Spst else 36219370Spst { 36319370Spst /* "info history" means print the last 10 values. */ 36419370Spst num = value_history_count - 9; 36519370Spst } 36619370Spst 36719370Spst if (num <= 0) 36819370Spst num = 1; 36919370Spst 37019370Spst for (i = num; i < num + 10 && i <= value_history_count; i++) 37119370Spst { 37219370Spst val = access_value_history (i); 37319370Spst printf_filtered ("$%d = ", i); 37419370Spst value_print (val, gdb_stdout, 0, Val_pretty_default); 37519370Spst printf_filtered ("\n"); 37619370Spst } 37719370Spst 37819370Spst /* The next "info history +" should start after what we just printed. */ 37919370Spst num += 10; 38019370Spst 38119370Spst /* Hitting just return after this command should do the same thing as 38219370Spst "info history +". If num_exp is null, this is unnecessary, since 38319370Spst "info history +" is not useful after "info history". */ 38419370Spst if (from_tty && num_exp) 38519370Spst { 38619370Spst num_exp[0] = '+'; 38719370Spst num_exp[1] = '\0'; 38819370Spst } 38919370Spst} 39019370Spst 39119370Spst/* Internal variables. These are variables within the debugger 39219370Spst that hold values assigned by debugger commands. 39319370Spst The user refers to them with a '$' prefix 39419370Spst that does not appear in the variable names stored internally. */ 39519370Spst 39619370Spststatic struct internalvar *internalvars; 39719370Spst 39819370Spst/* Look up an internal variable with name NAME. NAME should not 39919370Spst normally include a dollar sign. 40019370Spst 40119370Spst If the specified internal variable does not exist, 40219370Spst one is created, with a void value. */ 40319370Spst 40419370Spststruct internalvar * 40598944Sobrienlookup_internalvar (char *name) 40619370Spst{ 407130803Smarcel struct internalvar *var; 40819370Spst 40919370Spst for (var = internalvars; var; var = var->next) 410130803Smarcel if (strcmp (var->name, name) == 0) 41119370Spst return var; 41219370Spst 41319370Spst var = (struct internalvar *) xmalloc (sizeof (struct internalvar)); 41419370Spst var->name = concat (name, NULL); 41519370Spst var->value = allocate_value (builtin_type_void); 41619370Spst release_value (var->value); 41719370Spst var->next = internalvars; 41819370Spst internalvars = var; 41919370Spst return var; 42019370Spst} 42119370Spst 42298944Sobrienstruct value * 42398944Sobrienvalue_of_internalvar (struct internalvar *var) 42419370Spst{ 42598944Sobrien struct value *val; 42619370Spst 42719370Spst val = value_copy (var->value); 42819370Spst if (VALUE_LAZY (val)) 42919370Spst value_fetch_lazy (val); 43019370Spst VALUE_LVAL (val) = lval_internalvar; 43119370Spst VALUE_INTERNALVAR (val) = var; 43219370Spst return val; 43319370Spst} 43419370Spst 43519370Spstvoid 43698944Sobrienset_internalvar_component (struct internalvar *var, int offset, int bitpos, 43798944Sobrien int bitsize, struct value *newval) 43819370Spst{ 439130803Smarcel char *addr = VALUE_CONTENTS (var->value) + offset; 44019370Spst 44119370Spst if (bitsize) 44219370Spst modify_field (addr, value_as_long (newval), 44319370Spst bitpos, bitsize); 44419370Spst else 44519370Spst memcpy (addr, VALUE_CONTENTS (newval), TYPE_LENGTH (VALUE_TYPE (newval))); 44619370Spst} 44719370Spst 44819370Spstvoid 44998944Sobrienset_internalvar (struct internalvar *var, struct value *val) 45019370Spst{ 45198944Sobrien struct value *newval; 45219370Spst 45319370Spst newval = value_copy (val); 45419370Spst newval->modifiable = 1; 45519370Spst 45619370Spst /* Force the value to be fetched from the target now, to avoid problems 45719370Spst later when this internalvar is referenced and the target is gone or 45819370Spst has changed. */ 45919370Spst if (VALUE_LAZY (newval)) 46019370Spst value_fetch_lazy (newval); 46119370Spst 46219370Spst /* Begin code which must not call error(). If var->value points to 46319370Spst something free'd, an error() obviously leaves a dangling pointer. 46419370Spst But we also get a danling pointer if var->value points to 46519370Spst something in the value chain (i.e., before release_value is 46619370Spst called), because after the error free_all_values will get called before 46719370Spst long. */ 46898944Sobrien xfree (var->value); 46919370Spst var->value = newval; 47019370Spst release_value (newval); 47119370Spst /* End code which must not call error(). */ 47219370Spst} 47319370Spst 47419370Spstchar * 47598944Sobrieninternalvar_name (struct internalvar *var) 47619370Spst{ 47719370Spst return var->name; 47819370Spst} 47919370Spst 48019370Spst/* Free all internalvars. Done when new symtabs are loaded, 48119370Spst because that makes the values invalid. */ 48219370Spst 48319370Spstvoid 48498944Sobrienclear_internalvars (void) 48519370Spst{ 486130803Smarcel struct internalvar *var; 48719370Spst 48819370Spst while (internalvars) 48919370Spst { 49019370Spst var = internalvars; 49119370Spst internalvars = var->next; 49298944Sobrien xfree (var->name); 49398944Sobrien xfree (var->value); 49498944Sobrien xfree (var); 49519370Spst } 49619370Spst} 49719370Spst 49819370Spststatic void 49998944Sobrienshow_convenience (char *ignore, int from_tty) 50019370Spst{ 501130803Smarcel struct internalvar *var; 50219370Spst int varseen = 0; 50319370Spst 50419370Spst for (var = internalvars; var; var = var->next) 50519370Spst { 50619370Spst if (!varseen) 50719370Spst { 50819370Spst varseen = 1; 50919370Spst } 51019370Spst printf_filtered ("$%s = ", var->name); 51119370Spst value_print (var->value, gdb_stdout, 0, Val_pretty_default); 51219370Spst printf_filtered ("\n"); 51319370Spst } 51419370Spst if (!varseen) 51519370Spst printf_unfiltered ("No debugger convenience variables now defined.\n\ 51619370SpstConvenience variables have names starting with \"$\";\n\ 51719370Spstuse \"set\" as in \"set $foo = 5\" to define them.\n"); 51819370Spst} 51919370Spst 52019370Spst/* Extract a value as a C number (either long or double). 52119370Spst Knows how to convert fixed values to double, or 52219370Spst floating values to long. 52319370Spst Does not deallocate the value. */ 52419370Spst 52519370SpstLONGEST 52698944Sobrienvalue_as_long (struct value *val) 52719370Spst{ 52819370Spst /* This coerces arrays and functions, which is necessary (e.g. 52919370Spst in disassemble_command). It also dereferences references, which 53019370Spst I suspect is the most logical thing to do. */ 53119370Spst COERCE_ARRAY (val); 53219370Spst return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val)); 53319370Spst} 53419370Spst 53519370SpstDOUBLEST 53698944Sobrienvalue_as_double (struct value *val) 53719370Spst{ 53819370Spst DOUBLEST foo; 53919370Spst int inv; 54098944Sobrien 54119370Spst foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv); 54219370Spst if (inv) 54319370Spst error ("Invalid floating value found in program."); 54419370Spst return foo; 54519370Spst} 54698944Sobrien/* Extract a value as a C pointer. Does not deallocate the value. 54798944Sobrien Note that val's type may not actually be a pointer; value_as_long 54898944Sobrien handles all the cases. */ 54919370SpstCORE_ADDR 55098944Sobrienvalue_as_address (struct value *val) 55119370Spst{ 55219370Spst /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure 55319370Spst whether we want this to be true eventually. */ 55419370Spst#if 0 55519370Spst /* ADDR_BITS_REMOVE is wrong if we are being called for a 55619370Spst non-address (e.g. argument to "signal", "info break", etc.), or 55719370Spst for pointers to char, in which the low bits *are* significant. */ 55898944Sobrien return ADDR_BITS_REMOVE (value_as_long (val)); 55919370Spst#else 56098944Sobrien 56198944Sobrien /* There are several targets (IA-64, PowerPC, and others) which 56298944Sobrien don't represent pointers to functions as simply the address of 56398944Sobrien the function's entry point. For example, on the IA-64, a 56498944Sobrien function pointer points to a two-word descriptor, generated by 56598944Sobrien the linker, which contains the function's entry point, and the 56698944Sobrien value the IA-64 "global pointer" register should have --- to 56798944Sobrien support position-independent code. The linker generates 56898944Sobrien descriptors only for those functions whose addresses are taken. 56998944Sobrien 57098944Sobrien On such targets, it's difficult for GDB to convert an arbitrary 57198944Sobrien function address into a function pointer; it has to either find 57298944Sobrien an existing descriptor for that function, or call malloc and 57398944Sobrien build its own. On some targets, it is impossible for GDB to 57498944Sobrien build a descriptor at all: the descriptor must contain a jump 57598944Sobrien instruction; data memory cannot be executed; and code memory 57698944Sobrien cannot be modified. 57798944Sobrien 57898944Sobrien Upon entry to this function, if VAL is a value of type `function' 57998944Sobrien (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then 58098944Sobrien VALUE_ADDRESS (val) is the address of the function. This is what 58198944Sobrien you'll get if you evaluate an expression like `main'. The call 58298944Sobrien to COERCE_ARRAY below actually does all the usual unary 58398944Sobrien conversions, which includes converting values of type `function' 58498944Sobrien to `pointer to function'. This is the challenging conversion 58598944Sobrien discussed above. Then, `unpack_long' will convert that pointer 58698944Sobrien back into an address. 58798944Sobrien 58898944Sobrien So, suppose the user types `disassemble foo' on an architecture 58998944Sobrien with a strange function pointer representation, on which GDB 59098944Sobrien cannot build its own descriptors, and suppose further that `foo' 59198944Sobrien has no linker-built descriptor. The address->pointer conversion 59298944Sobrien will signal an error and prevent the command from running, even 59398944Sobrien though the next step would have been to convert the pointer 59498944Sobrien directly back into the same address. 59598944Sobrien 59698944Sobrien The following shortcut avoids this whole mess. If VAL is a 59798944Sobrien function, just return its address directly. */ 59898944Sobrien if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC 59998944Sobrien || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_METHOD) 60098944Sobrien return VALUE_ADDRESS (val); 60198944Sobrien 60298944Sobrien COERCE_ARRAY (val); 60398944Sobrien 60498944Sobrien /* Some architectures (e.g. Harvard), map instruction and data 60598944Sobrien addresses onto a single large unified address space. For 60698944Sobrien instance: An architecture may consider a large integer in the 60798944Sobrien range 0x10000000 .. 0x1000ffff to already represent a data 60898944Sobrien addresses (hence not need a pointer to address conversion) while 60998944Sobrien a small integer would still need to be converted integer to 61098944Sobrien pointer to address. Just assume such architectures handle all 61198944Sobrien integer conversions in a single function. */ 61298944Sobrien 61398944Sobrien /* JimB writes: 61498944Sobrien 61598944Sobrien I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we 61698944Sobrien must admonish GDB hackers to make sure its behavior matches the 61798944Sobrien compiler's, whenever possible. 61898944Sobrien 61998944Sobrien In general, I think GDB should evaluate expressions the same way 62098944Sobrien the compiler does. When the user copies an expression out of 62198944Sobrien their source code and hands it to a `print' command, they should 62298944Sobrien get the same value the compiler would have computed. Any 62398944Sobrien deviation from this rule can cause major confusion and annoyance, 62498944Sobrien and needs to be justified carefully. In other words, GDB doesn't 62598944Sobrien really have the freedom to do these conversions in clever and 62698944Sobrien useful ways. 62798944Sobrien 62898944Sobrien AndrewC pointed out that users aren't complaining about how GDB 62998944Sobrien casts integers to pointers; they are complaining that they can't 63098944Sobrien take an address from a disassembly listing and give it to `x/i'. 63198944Sobrien This is certainly important. 63298944Sobrien 63398944Sobrien Adding an architecture method like INTEGER_TO_ADDRESS certainly 63498944Sobrien makes it possible for GDB to "get it right" in all circumstances 63598944Sobrien --- the target has complete control over how things get done, so 63698944Sobrien people can Do The Right Thing for their target without breaking 63798944Sobrien anyone else. The standard doesn't specify how integers get 63898944Sobrien converted to pointers; usually, the ABI doesn't either, but 63998944Sobrien ABI-specific code is a more reasonable place to handle it. */ 64098944Sobrien 64198944Sobrien if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_PTR 64298944Sobrien && TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_REF 64398944Sobrien && INTEGER_TO_ADDRESS_P ()) 64498944Sobrien return INTEGER_TO_ADDRESS (VALUE_TYPE (val), VALUE_CONTENTS (val)); 64598944Sobrien 64698944Sobrien return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val)); 64719370Spst#endif 64819370Spst} 64919370Spst 65019370Spst/* Unpack raw data (copied from debugee, target byte order) at VALADDR 65119370Spst as a long, or as a double, assuming the raw data is described 65219370Spst by type TYPE. Knows how to convert different sizes of values 65319370Spst and can convert between fixed and floating point. We don't assume 65419370Spst any alignment for the raw data. Return value is in host byte order. 65519370Spst 65619370Spst If you want functions and arrays to be coerced to pointers, and 65719370Spst references to be dereferenced, call value_as_long() instead. 65819370Spst 65919370Spst C++: It is assumed that the front-end has taken care of 66019370Spst all matters concerning pointers to members. A pointer 66119370Spst to member which reaches here is considered to be equivalent 66219370Spst to an INT (or some size). After all, it is only an offset. */ 66319370Spst 66419370SpstLONGEST 665130803Smarcelunpack_long (struct type *type, const char *valaddr) 66619370Spst{ 667130803Smarcel enum type_code code = TYPE_CODE (type); 668130803Smarcel int len = TYPE_LENGTH (type); 669130803Smarcel int nosign = TYPE_UNSIGNED (type); 67019370Spst 67119370Spst if (current_language->la_language == language_scm 67219370Spst && is_scmvalue_type (type)) 67319370Spst return scm_unpack (type, valaddr, TYPE_CODE_INT); 67419370Spst 67519370Spst switch (code) 67619370Spst { 67719370Spst case TYPE_CODE_TYPEDEF: 67819370Spst return unpack_long (check_typedef (type), valaddr); 67919370Spst case TYPE_CODE_ENUM: 68019370Spst case TYPE_CODE_BOOL: 68119370Spst case TYPE_CODE_INT: 68219370Spst case TYPE_CODE_CHAR: 68319370Spst case TYPE_CODE_RANGE: 68419370Spst if (nosign) 68519370Spst return extract_unsigned_integer (valaddr, len); 68619370Spst else 68719370Spst return extract_signed_integer (valaddr, len); 68819370Spst 68919370Spst case TYPE_CODE_FLT: 69098944Sobrien return extract_typed_floating (valaddr, type); 69119370Spst 69219370Spst case TYPE_CODE_PTR: 69319370Spst case TYPE_CODE_REF: 69419370Spst /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure 69598944Sobrien whether we want this to be true eventually. */ 69698944Sobrien return extract_typed_address (valaddr, type); 69719370Spst 69819370Spst case TYPE_CODE_MEMBER: 69919370Spst error ("not implemented: member types in unpack_long"); 70019370Spst 70119370Spst default: 70219370Spst error ("Value can't be converted to integer."); 70319370Spst } 70498944Sobrien return 0; /* Placate lint. */ 70519370Spst} 70619370Spst 70719370Spst/* Return a double value from the specified type and address. 70819370Spst INVP points to an int which is set to 0 for valid value, 70919370Spst 1 for invalid value (bad float format). In either case, 71019370Spst the returned double is OK to use. Argument is in target 71119370Spst format, result is in host format. */ 71219370Spst 71319370SpstDOUBLEST 714130803Smarcelunpack_double (struct type *type, const char *valaddr, int *invp) 71519370Spst{ 71646283Sdfr enum type_code code; 71746283Sdfr int len; 71846283Sdfr int nosign; 71919370Spst 72019370Spst *invp = 0; /* Assume valid. */ 72119370Spst CHECK_TYPEDEF (type); 72246283Sdfr code = TYPE_CODE (type); 72346283Sdfr len = TYPE_LENGTH (type); 72446283Sdfr nosign = TYPE_UNSIGNED (type); 72519370Spst if (code == TYPE_CODE_FLT) 72619370Spst { 72798944Sobrien /* NOTE: cagney/2002-02-19: There was a test here to see if the 72898944Sobrien floating-point value was valid (using the macro 72998944Sobrien INVALID_FLOAT). That test/macro have been removed. 73098944Sobrien 73198944Sobrien It turns out that only the VAX defined this macro and then 73298944Sobrien only in a non-portable way. Fixing the portability problem 73398944Sobrien wouldn't help since the VAX floating-point code is also badly 73498944Sobrien bit-rotten. The target needs to add definitions for the 73598944Sobrien methods TARGET_FLOAT_FORMAT and TARGET_DOUBLE_FORMAT - these 73698944Sobrien exactly describe the target floating-point format. The 73798944Sobrien problem here is that the corresponding floatformat_vax_f and 73898944Sobrien floatformat_vax_d values these methods should be set to are 73998944Sobrien also not defined either. Oops! 74098944Sobrien 74198944Sobrien Hopefully someone will add both the missing floatformat 742130803Smarcel definitions and the new cases for floatformat_is_valid (). */ 743130803Smarcel 744130803Smarcel if (!floatformat_is_valid (floatformat_from_type (type), valaddr)) 745130803Smarcel { 746130803Smarcel *invp = 1; 747130803Smarcel return 0.0; 748130803Smarcel } 749130803Smarcel 75098944Sobrien return extract_typed_floating (valaddr, type); 75119370Spst } 75219370Spst else if (nosign) 75319370Spst { 75419370Spst /* Unsigned -- be sure we compensate for signed LONGEST. */ 75546283Sdfr return (ULONGEST) unpack_long (type, valaddr); 75619370Spst } 75719370Spst else 75819370Spst { 75919370Spst /* Signed -- we are OK with unpack_long. */ 76019370Spst return unpack_long (type, valaddr); 76119370Spst } 76219370Spst} 76319370Spst 76419370Spst/* Unpack raw data (copied from debugee, target byte order) at VALADDR 76519370Spst as a CORE_ADDR, assuming the raw data is described by type TYPE. 76619370Spst We don't assume any alignment for the raw data. Return value is in 76719370Spst host byte order. 76819370Spst 76919370Spst If you want functions and arrays to be coerced to pointers, and 77098944Sobrien references to be dereferenced, call value_as_address() instead. 77119370Spst 77219370Spst C++: It is assumed that the front-end has taken care of 77319370Spst all matters concerning pointers to members. A pointer 77419370Spst to member which reaches here is considered to be equivalent 77519370Spst to an INT (or some size). After all, it is only an offset. */ 77619370Spst 77719370SpstCORE_ADDR 778130803Smarcelunpack_pointer (struct type *type, const char *valaddr) 77919370Spst{ 78019370Spst /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure 78119370Spst whether we want this to be true eventually. */ 78219370Spst return unpack_long (type, valaddr); 78319370Spst} 78498944Sobrien 78519370Spst 786130803Smarcel/* Get the value of the FIELDN'th field (which must be static) of 787130803Smarcel TYPE. Return NULL if the field doesn't exist or has been 788130803Smarcel optimized out. */ 78946283Sdfr 79098944Sobrienstruct value * 79198944Sobrienvalue_static_field (struct type *type, int fieldno) 79246283Sdfr{ 793130803Smarcel struct value *retval; 794130803Smarcel 79546283Sdfr if (TYPE_FIELD_STATIC_HAS_ADDR (type, fieldno)) 79646283Sdfr { 797130803Smarcel retval = value_at (TYPE_FIELD_TYPE (type, fieldno), 798130803Smarcel TYPE_FIELD_STATIC_PHYSADDR (type, fieldno), 799130803Smarcel NULL); 80046283Sdfr } 80146283Sdfr else 80246283Sdfr { 80346283Sdfr char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno); 804130803Smarcel struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0, NULL); 80546283Sdfr if (sym == NULL) 80646283Sdfr { 80746283Sdfr /* With some compilers, e.g. HP aCC, static data members are reported 80898944Sobrien as non-debuggable symbols */ 80998944Sobrien struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL); 81046283Sdfr if (!msym) 81146283Sdfr return NULL; 81246283Sdfr else 81398944Sobrien { 814130803Smarcel retval = value_at (TYPE_FIELD_TYPE (type, fieldno), 815130803Smarcel SYMBOL_VALUE_ADDRESS (msym), 816130803Smarcel SYMBOL_BFD_SECTION (msym)); 81746283Sdfr } 81846283Sdfr } 81946283Sdfr else 82046283Sdfr { 821130803Smarcel /* SYM should never have a SYMBOL_CLASS which will require 822130803Smarcel read_var_value to use the FRAME parameter. */ 823130803Smarcel if (symbol_read_needs_frame (sym)) 824130803Smarcel warning ("static field's value depends on the current " 825130803Smarcel "frame - bad debug info?"); 826130803Smarcel retval = read_var_value (sym, NULL); 82798944Sobrien } 828130803Smarcel if (retval && VALUE_LVAL (retval) == lval_memory) 829130803Smarcel SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno), 830130803Smarcel VALUE_ADDRESS (retval)); 83146283Sdfr } 832130803Smarcel return retval; 83346283Sdfr} 83446283Sdfr 83598944Sobrien/* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE. 83698944Sobrien You have to be careful here, since the size of the data area for the value 83798944Sobrien is set by the length of the enclosing type. So if NEW_ENCL_TYPE is bigger 83898944Sobrien than the old enclosing type, you have to allocate more space for the data. 83998944Sobrien The return value is a pointer to the new version of this value structure. */ 84098944Sobrien 84198944Sobrienstruct value * 84298944Sobrienvalue_change_enclosing_type (struct value *val, struct type *new_encl_type) 84398944Sobrien{ 84498944Sobrien if (TYPE_LENGTH (new_encl_type) <= TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val))) 84598944Sobrien { 84698944Sobrien VALUE_ENCLOSING_TYPE (val) = new_encl_type; 84798944Sobrien return val; 84898944Sobrien } 84998944Sobrien else 85098944Sobrien { 85198944Sobrien struct value *new_val; 85298944Sobrien struct value *prev; 85398944Sobrien 85498944Sobrien new_val = (struct value *) xrealloc (val, sizeof (struct value) + TYPE_LENGTH (new_encl_type)); 855130803Smarcel 856130803Smarcel VALUE_ENCLOSING_TYPE (new_val) = new_encl_type; 857130803Smarcel 85898944Sobrien /* We have to make sure this ends up in the same place in the value 85998944Sobrien chain as the original copy, so it's clean-up behavior is the same. 86098944Sobrien If the value has been released, this is a waste of time, but there 86198944Sobrien is no way to tell that in advance, so... */ 86298944Sobrien 86398944Sobrien if (val != all_values) 86498944Sobrien { 86598944Sobrien for (prev = all_values; prev != NULL; prev = prev->next) 86698944Sobrien { 86798944Sobrien if (prev->next == val) 86898944Sobrien { 86998944Sobrien prev->next = new_val; 87098944Sobrien break; 87198944Sobrien } 87298944Sobrien } 87398944Sobrien } 87498944Sobrien 87598944Sobrien return new_val; 87698944Sobrien } 87798944Sobrien} 87898944Sobrien 87919370Spst/* Given a value ARG1 (offset by OFFSET bytes) 88019370Spst of a struct or union type ARG_TYPE, 88146283Sdfr extract and return the value of one of its (non-static) fields. 88246283Sdfr FIELDNO says which field. */ 88319370Spst 88498944Sobrienstruct value * 88598944Sobrienvalue_primitive_field (struct value *arg1, int offset, 886130803Smarcel int fieldno, struct type *arg_type) 88719370Spst{ 88898944Sobrien struct value *v; 889130803Smarcel struct type *type; 89019370Spst 89119370Spst CHECK_TYPEDEF (arg_type); 89219370Spst type = TYPE_FIELD_TYPE (arg_type, fieldno); 89319370Spst 89419370Spst /* Handle packed fields */ 89519370Spst 89619370Spst if (TYPE_FIELD_BITSIZE (arg_type, fieldno)) 89719370Spst { 89819370Spst v = value_from_longest (type, 89946283Sdfr unpack_field_as_long (arg_type, 90046283Sdfr VALUE_CONTENTS (arg1) 90198944Sobrien + offset, 90246283Sdfr fieldno)); 90319370Spst VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8; 90419370Spst VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno); 90598944Sobrien VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset 90698944Sobrien + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; 90719370Spst } 90846283Sdfr else if (fieldno < TYPE_N_BASECLASSES (arg_type)) 90946283Sdfr { 91046283Sdfr /* This field is actually a base subobject, so preserve the 91146283Sdfr entire object's contents for later references to virtual 91246283Sdfr bases, etc. */ 91346283Sdfr v = allocate_value (VALUE_ENCLOSING_TYPE (arg1)); 91498944Sobrien VALUE_TYPE (v) = type; 91546283Sdfr if (VALUE_LAZY (arg1)) 91646283Sdfr VALUE_LAZY (v) = 1; 91746283Sdfr else 91846283Sdfr memcpy (VALUE_CONTENTS_ALL_RAW (v), VALUE_CONTENTS_ALL_RAW (arg1), 91946283Sdfr TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg1))); 92046283Sdfr VALUE_OFFSET (v) = VALUE_OFFSET (arg1); 92146283Sdfr VALUE_EMBEDDED_OFFSET (v) 92298944Sobrien = offset + 92398944Sobrien VALUE_EMBEDDED_OFFSET (arg1) + 92498944Sobrien TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; 92546283Sdfr } 92619370Spst else 92719370Spst { 92846283Sdfr /* Plain old data member */ 92946283Sdfr offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; 93019370Spst v = allocate_value (type); 93119370Spst if (VALUE_LAZY (arg1)) 93219370Spst VALUE_LAZY (v) = 1; 93319370Spst else 93446283Sdfr memcpy (VALUE_CONTENTS_RAW (v), 93546283Sdfr VALUE_CONTENTS_RAW (arg1) + offset, 93619370Spst TYPE_LENGTH (type)); 93798944Sobrien VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset 93898944Sobrien + VALUE_EMBEDDED_OFFSET (arg1); 93919370Spst } 94019370Spst VALUE_LVAL (v) = VALUE_LVAL (arg1); 94119370Spst if (VALUE_LVAL (arg1) == lval_internalvar) 94219370Spst VALUE_LVAL (v) = lval_internalvar_component; 94319370Spst VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1); 94498944Sobrien VALUE_REGNO (v) = VALUE_REGNO (arg1); 94546283Sdfr/* VALUE_OFFSET (v) = VALUE_OFFSET (arg1) + offset 94698944Sobrien + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8; */ 94719370Spst return v; 94819370Spst} 94919370Spst 95019370Spst/* Given a value ARG1 of a struct or union type, 95146283Sdfr extract and return the value of one of its (non-static) fields. 95246283Sdfr FIELDNO says which field. */ 95319370Spst 95498944Sobrienstruct value * 955130803Smarcelvalue_field (struct value *arg1, int fieldno) 95619370Spst{ 95719370Spst return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1)); 95819370Spst} 95919370Spst 96019370Spst/* Return a non-virtual function as a value. 96119370Spst F is the list of member functions which contains the desired method. 96298944Sobrien J is an index into F which provides the desired method. 96319370Spst 96498944Sobrien We only use the symbol for its address, so be happy with either a 96598944Sobrien full symbol or a minimal symbol. 96698944Sobrien */ 96798944Sobrien 96898944Sobrienstruct value * 96998944Sobrienvalue_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type, 97098944Sobrien int offset) 97119370Spst{ 97298944Sobrien struct value *v; 973130803Smarcel struct type *ftype = TYPE_FN_FIELD_TYPE (f, j); 97498944Sobrien char *physname = TYPE_FN_FIELD_PHYSNAME (f, j); 97519370Spst struct symbol *sym; 97698944Sobrien struct minimal_symbol *msym; 97719370Spst 978130803Smarcel sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0, NULL); 97998944Sobrien if (sym != NULL) 98098944Sobrien { 98198944Sobrien msym = NULL; 98298944Sobrien } 98398944Sobrien else 98498944Sobrien { 98598944Sobrien gdb_assert (sym == NULL); 98698944Sobrien msym = lookup_minimal_symbol (physname, NULL, NULL); 98798944Sobrien if (msym == NULL) 98819370Spst return NULL; 98998944Sobrien } 99098944Sobrien 99119370Spst v = allocate_value (ftype); 99298944Sobrien if (sym) 99398944Sobrien { 99498944Sobrien VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); 99598944Sobrien } 99698944Sobrien else 99798944Sobrien { 99898944Sobrien VALUE_ADDRESS (v) = SYMBOL_VALUE_ADDRESS (msym); 99998944Sobrien } 100019370Spst 100119370Spst if (arg1p) 100298944Sobrien { 100398944Sobrien if (type != VALUE_TYPE (*arg1p)) 100498944Sobrien *arg1p = value_ind (value_cast (lookup_pointer_type (type), 100598944Sobrien value_addr (*arg1p))); 100619370Spst 100798944Sobrien /* Move the `this' pointer according to the offset. 100898944Sobrien VALUE_OFFSET (*arg1p) += offset; 100998944Sobrien */ 101019370Spst } 101119370Spst 101219370Spst return v; 101319370Spst} 101419370Spst 101519370Spst 101619370Spst/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at 101719370Spst VALADDR. 101819370Spst 101919370Spst Extracting bits depends on endianness of the machine. Compute the 102019370Spst number of least significant bits to discard. For big endian machines, 102119370Spst we compute the total number of bits in the anonymous object, subtract 102219370Spst off the bit count from the MSB of the object to the MSB of the 102319370Spst bitfield, then the size of the bitfield, which leaves the LSB discard 102419370Spst count. For little endian machines, the discard count is simply the 102519370Spst number of bits from the LSB of the anonymous object to the LSB of the 102619370Spst bitfield. 102719370Spst 102819370Spst If the field is signed, we also do sign extension. */ 102919370Spst 103019370SpstLONGEST 1031130803Smarcelunpack_field_as_long (struct type *type, const char *valaddr, int fieldno) 103219370Spst{ 103346283Sdfr ULONGEST val; 103446283Sdfr ULONGEST valmask; 103519370Spst int bitpos = TYPE_FIELD_BITPOS (type, fieldno); 103619370Spst int bitsize = TYPE_FIELD_BITSIZE (type, fieldno); 103719370Spst int lsbcount; 103846283Sdfr struct type *field_type; 103919370Spst 104019370Spst val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val)); 104146283Sdfr field_type = TYPE_FIELD_TYPE (type, fieldno); 104246283Sdfr CHECK_TYPEDEF (field_type); 104319370Spst 104419370Spst /* Extract bits. See comment above. */ 104519370Spst 104619370Spst if (BITS_BIG_ENDIAN) 104719370Spst lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize); 104819370Spst else 104919370Spst lsbcount = (bitpos % 8); 105019370Spst val >>= lsbcount; 105119370Spst 105219370Spst /* If the field does not entirely fill a LONGEST, then zero the sign bits. 105319370Spst If the field is signed, and is negative, then sign extend. */ 105419370Spst 105519370Spst if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val))) 105619370Spst { 105746283Sdfr valmask = (((ULONGEST) 1) << bitsize) - 1; 105819370Spst val &= valmask; 105946283Sdfr if (!TYPE_UNSIGNED (field_type)) 106019370Spst { 106119370Spst if (val & (valmask ^ (valmask >> 1))) 106219370Spst { 106319370Spst val |= ~valmask; 106419370Spst } 106519370Spst } 106619370Spst } 106719370Spst return (val); 106819370Spst} 106919370Spst 107019370Spst/* Modify the value of a bitfield. ADDR points to a block of memory in 107119370Spst target byte order; the bitfield starts in the byte pointed to. FIELDVAL 107219370Spst is the desired value of the field, in host byte order. BITPOS and BITSIZE 107319370Spst indicate which bits (in target bit order) comprise the bitfield. */ 107419370Spst 107519370Spstvoid 107698944Sobrienmodify_field (char *addr, LONGEST fieldval, int bitpos, int bitsize) 107719370Spst{ 107819370Spst LONGEST oword; 107919370Spst 108019370Spst /* If a negative fieldval fits in the field in question, chop 108119370Spst off the sign extension bits. */ 108219370Spst if (bitsize < (8 * (int) sizeof (fieldval)) 108319370Spst && (~fieldval & ~((1 << (bitsize - 1)) - 1)) == 0) 108419370Spst fieldval = fieldval & ((1 << bitsize) - 1); 108519370Spst 108619370Spst /* Warn if value is too big to fit in the field in question. */ 108719370Spst if (bitsize < (8 * (int) sizeof (fieldval)) 108898944Sobrien && 0 != (fieldval & ~((1 << bitsize) - 1))) 108919370Spst { 109019370Spst /* FIXME: would like to include fieldval in the message, but 109198944Sobrien we don't have a sprintf_longest. */ 109219370Spst warning ("Value does not fit in %d bits.", bitsize); 109319370Spst 109419370Spst /* Truncate it, otherwise adjoining fields may be corrupted. */ 109519370Spst fieldval = fieldval & ((1 << bitsize) - 1); 109619370Spst } 109719370Spst 109819370Spst oword = extract_signed_integer (addr, sizeof oword); 109919370Spst 110019370Spst /* Shifting for bit field depends on endianness of the target machine. */ 110119370Spst if (BITS_BIG_ENDIAN) 110219370Spst bitpos = sizeof (oword) * 8 - bitpos - bitsize; 110319370Spst 110419370Spst /* Mask out old value, while avoiding shifts >= size of oword */ 110519370Spst if (bitsize < 8 * (int) sizeof (oword)) 110698944Sobrien oword &= ~(((((ULONGEST) 1) << bitsize) - 1) << bitpos); 110719370Spst else 110898944Sobrien oword &= ~((~(ULONGEST) 0) << bitpos); 110919370Spst oword |= fieldval << bitpos; 111019370Spst 111119370Spst store_signed_integer (addr, sizeof oword, oword); 111219370Spst} 111319370Spst 111419370Spst/* Convert C numbers into newly allocated values */ 111519370Spst 111698944Sobrienstruct value * 1117130803Smarcelvalue_from_longest (struct type *type, LONGEST num) 111819370Spst{ 111998944Sobrien struct value *val = allocate_value (type); 1120130803Smarcel enum type_code code; 1121130803Smarcel int len; 112298944Sobrienretry: 112319370Spst code = TYPE_CODE (type); 112419370Spst len = TYPE_LENGTH (type); 112519370Spst 112619370Spst switch (code) 112719370Spst { 112819370Spst case TYPE_CODE_TYPEDEF: 112919370Spst type = check_typedef (type); 113019370Spst goto retry; 113119370Spst case TYPE_CODE_INT: 113219370Spst case TYPE_CODE_CHAR: 113319370Spst case TYPE_CODE_ENUM: 113419370Spst case TYPE_CODE_BOOL: 113519370Spst case TYPE_CODE_RANGE: 113619370Spst store_signed_integer (VALUE_CONTENTS_RAW (val), len, num); 113719370Spst break; 113898944Sobrien 113919370Spst case TYPE_CODE_REF: 114019370Spst case TYPE_CODE_PTR: 114198944Sobrien store_typed_address (VALUE_CONTENTS_RAW (val), type, (CORE_ADDR) num); 114219370Spst break; 114398944Sobrien 114419370Spst default: 114546283Sdfr error ("Unexpected type (%d) encountered for integer constant.", code); 114619370Spst } 114719370Spst return val; 114819370Spst} 114919370Spst 115098944Sobrien 115198944Sobrien/* Create a value representing a pointer of type TYPE to the address 115298944Sobrien ADDR. */ 115398944Sobrienstruct value * 115498944Sobrienvalue_from_pointer (struct type *type, CORE_ADDR addr) 115519370Spst{ 115698944Sobrien struct value *val = allocate_value (type); 115798944Sobrien store_typed_address (VALUE_CONTENTS_RAW (val), type, addr); 115898944Sobrien return val; 115998944Sobrien} 116098944Sobrien 116198944Sobrien 116298944Sobrien/* Create a value for a string constant to be stored locally 116398944Sobrien (not in the inferior's memory space, but in GDB memory). 116498944Sobrien This is analogous to value_from_longest, which also does not 116598944Sobrien use inferior memory. String shall NOT contain embedded nulls. */ 116698944Sobrien 116798944Sobrienstruct value * 116898944Sobrienvalue_from_string (char *ptr) 116998944Sobrien{ 117098944Sobrien struct value *val; 117198944Sobrien int len = strlen (ptr); 117298944Sobrien int lowbound = current_language->string_lower_bound; 117398944Sobrien struct type *rangetype = 117498944Sobrien create_range_type ((struct type *) NULL, 117598944Sobrien builtin_type_int, 117698944Sobrien lowbound, len + lowbound - 1); 117798944Sobrien struct type *stringtype = 117898944Sobrien create_array_type ((struct type *) NULL, 117998944Sobrien *current_language->string_char_type, 118098944Sobrien rangetype); 118198944Sobrien 118298944Sobrien val = allocate_value (stringtype); 118398944Sobrien memcpy (VALUE_CONTENTS_RAW (val), ptr, len); 118498944Sobrien return val; 118598944Sobrien} 118698944Sobrien 118798944Sobrienstruct value * 118898944Sobrienvalue_from_double (struct type *type, DOUBLEST num) 118998944Sobrien{ 119098944Sobrien struct value *val = allocate_value (type); 119119370Spst struct type *base_type = check_typedef (type); 1192130803Smarcel enum type_code code = TYPE_CODE (base_type); 1193130803Smarcel int len = TYPE_LENGTH (base_type); 119419370Spst 119519370Spst if (code == TYPE_CODE_FLT) 119619370Spst { 119798944Sobrien store_typed_floating (VALUE_CONTENTS_RAW (val), base_type, num); 119819370Spst } 119919370Spst else 120019370Spst error ("Unexpected type encountered for floating constant."); 120119370Spst 120219370Spst return val; 120319370Spst} 120419370Spst 1205130803Smarcel/* Deal with the return-value of a function that has "just returned". 120619370Spst 1207130803Smarcel Extract the return-value (as a "struct value") that a function, 1208130803Smarcel using register convention, has just returned to its caller. Assume 1209130803Smarcel that the type of the function is VALTYPE, and that the "just 1210130803Smarcel returned" register state is found in RETBUF. 121119370Spst 1212130803Smarcel The function has "just returned" because GDB halts a returning 1213130803Smarcel function by setting a breakpoint at the return address (in the 1214130803Smarcel caller), and not the return instruction (in the callee). 1215130803Smarcel 1216130803Smarcel Because, in the case of a return from an inferior function call, 1217130803Smarcel GDB needs to restore the inferiors registers, RETBUF is normally a 1218130803Smarcel copy of the inferior's registers. */ 1219130803Smarcel 122098944Sobrienstruct value * 1221130803Smarcelregister_value_being_returned (struct type *valtype, struct regcache *retbuf) 122219370Spst{ 1223130803Smarcel struct value *val = allocate_value (valtype); 122419370Spst 1225130803Smarcel /* If the function returns void, don't bother fetching the return 1226130803Smarcel value. See also "using_struct_return". */ 1227130803Smarcel if (TYPE_CODE (valtype) == TYPE_CODE_VOID) 1228130803Smarcel return val; 122919370Spst 1230130803Smarcel if (!gdbarch_return_value_p (current_gdbarch)) 1231130803Smarcel { 1232130803Smarcel /* NOTE: cagney/2003-10-20: Unlike "gdbarch_return_value", the 1233130803Smarcel EXTRACT_RETURN_VALUE and USE_STRUCT_CONVENTION methods do not 1234130803Smarcel handle the edge case of a function returning a small 1235130803Smarcel structure / union in registers. */ 1236130803Smarcel CHECK_TYPEDEF (valtype); 1237130803Smarcel EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val)); 1238130803Smarcel return val; 1239130803Smarcel } 124019370Spst 1241130803Smarcel /* This function only handles "register convention". */ 1242130803Smarcel gdb_assert (gdbarch_return_value (current_gdbarch, valtype, 1243130803Smarcel NULL, NULL, NULL) 1244130803Smarcel == RETURN_VALUE_REGISTER_CONVENTION); 1245130803Smarcel gdbarch_return_value (current_gdbarch, valtype, retbuf, 1246130803Smarcel VALUE_CONTENTS_RAW (val) /*read*/, NULL /*write*/); 124719370Spst return val; 124819370Spst} 124919370Spst 1250130803Smarcel/* Should we use DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS instead of 1251130803Smarcel EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc and TYPE 1252130803Smarcel is the type (which is known to be struct, union or array). 125319370Spst 125419370Spst On most machines, the struct convention is used unless we are 125519370Spst using gcc and the type is of a special size. */ 125619370Spst/* As of about 31 Mar 93, GCC was changed to be compatible with the 125719370Spst native compiler. GCC 2.3.3 was the last release that did it the 125819370Spst old way. Since gcc2_compiled was not changed, we have no 125919370Spst way to correctly win in all cases, so we just do the right thing 126019370Spst for gcc1 and for gcc2 after this change. Thus it loses for gcc 126119370Spst 2.0-2.3.3. This is somewhat unfortunate, but changing gcc2_compiled 126219370Spst would cause more chaos than dealing with some struct returns being 126319370Spst handled wrong. */ 126446283Sdfr 126546283Sdfrint 126698944Sobriengeneric_use_struct_convention (int gcc_p, struct type *value_type) 126798944Sobrien{ 126846283Sdfr return !((gcc_p == 1) 126998944Sobrien && (TYPE_LENGTH (value_type) == 1 127098944Sobrien || TYPE_LENGTH (value_type) == 2 127198944Sobrien || TYPE_LENGTH (value_type) == 4 127298944Sobrien || TYPE_LENGTH (value_type) == 8)); 127346283Sdfr} 127446283Sdfr 1275130803Smarcel/* Return true if the function returning the specified type is using 1276130803Smarcel the convention of returning structures in memory (passing in the 1277130803Smarcel address as a hidden first parameter). GCC_P is nonzero if compiled 127819370Spst with GCC. */ 127919370Spst 128019370Spstint 1281130803Smarcelusing_struct_return (struct type *value_type, int gcc_p) 128219370Spst{ 1283130803Smarcel enum type_code code = TYPE_CODE (value_type); 128419370Spst 128519370Spst if (code == TYPE_CODE_ERROR) 128619370Spst error ("Function return type unknown."); 128719370Spst 1288130803Smarcel if (code == TYPE_CODE_VOID) 1289130803Smarcel /* A void return value is never in memory. See also corresponding 1290130803Smarcel code in "register_value_being_returned". */ 1291130803Smarcel return 0; 129219370Spst 1293130803Smarcel if (!gdbarch_return_value_p (current_gdbarch)) 1294130803Smarcel { 1295130803Smarcel /* FIXME: cagney/2003-10-01: The below is dead. Instead an 1296130803Smarcel architecture should implement "gdbarch_return_value". Using 1297130803Smarcel that new function it is possible to exactly specify the ABIs 1298130803Smarcel "struct return" vs "register return" conventions. */ 1299130803Smarcel if (code == TYPE_CODE_STRUCT 1300130803Smarcel || code == TYPE_CODE_UNION 1301130803Smarcel || code == TYPE_CODE_ARRAY 1302130803Smarcel || RETURN_VALUE_ON_STACK (value_type)) 1303130803Smarcel return USE_STRUCT_CONVENTION (gcc_p, value_type); 1304130803Smarcel else 1305130803Smarcel return 0; 1306130803Smarcel } 1307130803Smarcel 1308130803Smarcel /* Probe the architecture for the return-value convention. */ 1309130803Smarcel return (gdbarch_return_value (current_gdbarch, value_type, 1310130803Smarcel NULL, NULL, NULL) 1311130803Smarcel == RETURN_VALUE_STRUCT_CONVENTION); 131219370Spst} 131319370Spst 131419370Spstvoid 131598944Sobrien_initialize_values (void) 131619370Spst{ 131719370Spst add_cmd ("convenience", no_class, show_convenience, 131898944Sobrien "Debugger convenience (\"$foo\") variables.\n\ 131919370SpstThese variables are created when you assign them values;\n\ 132019370Spstthus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\ 132119370SpstA few convenience variables are given values automatically:\n\ 132219370Spst\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\ 132319370Spst\"$__\" holds the contents of the last address examined with \"x\".", 132419370Spst &showlist); 132519370Spst 132619370Spst add_cmd ("values", no_class, show_values, 132719370Spst "Elements of value history around item number IDX (or last ten).", 132819370Spst &showlist); 132919370Spst} 1330