OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [value.c] - Blame information for rev 157

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Low level packing and unpacking of values for GDB, the GNU Debugger.
2
 
3
   Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4
   1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5
   Free Software Foundation, Inc.
6
 
7
   This file is part of GDB.
8
 
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
 
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
 
22
#include "defs.h"
23
#include "gdb_string.h"
24
#include "symtab.h"
25
#include "gdbtypes.h"
26
#include "value.h"
27
#include "gdbcore.h"
28
#include "command.h"
29
#include "gdbcmd.h"
30
#include "target.h"
31
#include "language.h"
32
#include "demangle.h"
33
#include "doublest.h"
34
#include "gdb_assert.h"
35
#include "regcache.h"
36
#include "block.h"
37
#include "dfp.h"
38
 
39
/* Prototypes for exported functions. */
40
 
41
void _initialize_values (void);
42
 
43
struct value
44
{
45
  /* Type of value; either not an lval, or one of the various
46
     different possible kinds of lval.  */
47
  enum lval_type lval;
48
 
49
  /* Is it modifiable?  Only relevant if lval != not_lval.  */
50
  int modifiable;
51
 
52
  /* Location of value (if lval).  */
53
  union
54
  {
55
    /* If lval == lval_memory, this is the address in the inferior.
56
       If lval == lval_register, this is the byte offset into the
57
       registers structure.  */
58
    CORE_ADDR address;
59
 
60
    /* Pointer to internal variable.  */
61
    struct internalvar *internalvar;
62
  } location;
63
 
64
  /* Describes offset of a value within lval of a structure in bytes.
65
     If lval == lval_memory, this is an offset to the address.  If
66
     lval == lval_register, this is a further offset from
67
     location.address within the registers structure.  Note also the
68
     member embedded_offset below.  */
69
  int offset;
70
 
71
  /* Only used for bitfields; number of bits contained in them.  */
72
  int bitsize;
73
 
74
  /* Only used for bitfields; position of start of field.  For
75
     gdbarch_bits_big_endian=0 targets, it is the position of the LSB.  For
76
     gdbarch_bits_big_endian=1 targets, it is the position of the MSB. */
77
  int bitpos;
78
 
79
  /* Frame register value is relative to.  This will be described in
80
     the lval enum above as "lval_register".  */
81
  struct frame_id frame_id;
82
 
83
  /* Type of the value.  */
84
  struct type *type;
85
 
86
  /* If a value represents a C++ object, then the `type' field gives
87
     the object's compile-time type.  If the object actually belongs
88
     to some class derived from `type', perhaps with other base
89
     classes and additional members, then `type' is just a subobject
90
     of the real thing, and the full object is probably larger than
91
     `type' would suggest.
92
 
93
     If `type' is a dynamic class (i.e. one with a vtable), then GDB
94
     can actually determine the object's run-time type by looking at
95
     the run-time type information in the vtable.  When this
96
     information is available, we may elect to read in the entire
97
     object, for several reasons:
98
 
99
     - When printing the value, the user would probably rather see the
100
     full object, not just the limited portion apparent from the
101
     compile-time type.
102
 
103
     - If `type' has virtual base classes, then even printing `type'
104
     alone may require reaching outside the `type' portion of the
105
     object to wherever the virtual base class has been stored.
106
 
107
     When we store the entire object, `enclosing_type' is the run-time
108
     type -- the complete object -- and `embedded_offset' is the
109
     offset of `type' within that larger type, in bytes.  The
110
     value_contents() macro takes `embedded_offset' into account, so
111
     most GDB code continues to see the `type' portion of the value,
112
     just as the inferior would.
113
 
114
     If `type' is a pointer to an object, then `enclosing_type' is a
115
     pointer to the object's run-time type, and `pointed_to_offset' is
116
     the offset in bytes from the full object to the pointed-to object
117
     -- that is, the value `embedded_offset' would have if we followed
118
     the pointer and fetched the complete object.  (I don't really see
119
     the point.  Why not just determine the run-time type when you
120
     indirect, and avoid the special case?  The contents don't matter
121
     until you indirect anyway.)
122
 
123
     If we're not doing anything fancy, `enclosing_type' is equal to
124
     `type', and `embedded_offset' is zero, so everything works
125
     normally.  */
126
  struct type *enclosing_type;
127
  int embedded_offset;
128
  int pointed_to_offset;
129
 
130
  /* Values are stored in a chain, so that they can be deleted easily
131
     over calls to the inferior.  Values assigned to internal
132
     variables or put into the value history are taken off this
133
     list.  */
134
  struct value *next;
135
 
136
  /* Register number if the value is from a register.  */
137
  short regnum;
138
 
139
  /* If zero, contents of this value are in the contents field.  If
140
     nonzero, contents are in inferior memory at address in the
141
     location.address field plus the offset field (and the lval field
142
     should be lval_memory).
143
 
144
     WARNING: This field is used by the code which handles watchpoints
145
     (see breakpoint.c) to decide whether a particular value can be
146
     watched by hardware watchpoints.  If the lazy flag is set for
147
     some member of a value chain, it is assumed that this member of
148
     the chain doesn't need to be watched as part of watching the
149
     value itself.  This is how GDB avoids watching the entire struct
150
     or array when the user wants to watch a single struct member or
151
     array element.  If you ever change the way lazy flag is set and
152
     reset, be sure to consider this use as well!  */
153
  char lazy;
154
 
155
  /* If nonzero, this is the value of a variable which does not
156
     actually exist in the program.  */
157
  char optimized_out;
158
 
159
  /* If value is a variable, is it initialized or not.  */
160
  int initialized;
161
 
162
  /* Actual contents of the value.  For use of this value; setting it
163
     uses the stuff above.  Not valid if lazy is nonzero.  Target
164
     byte-order.  We force it to be aligned properly for any possible
165
     value.  Note that a value therefore extends beyond what is
166
     declared here.  */
167
  union
168
  {
169
    gdb_byte contents[1];
170
    DOUBLEST force_doublest_align;
171
    LONGEST force_longest_align;
172
    CORE_ADDR force_core_addr_align;
173
    void *force_pointer_align;
174
  } aligner;
175
  /* Do not add any new members here -- contents above will trash
176
     them.  */
177
};
178
 
179
/* Prototypes for local functions. */
180
 
181
static void show_values (char *, int);
182
 
183
static void show_convenience (char *, int);
184
 
185
 
186
/* The value-history records all the values printed
187
   by print commands during this session.  Each chunk
188
   records 60 consecutive values.  The first chunk on
189
   the chain records the most recent values.
190
   The total number of values is in value_history_count.  */
191
 
192
#define VALUE_HISTORY_CHUNK 60
193
 
194
struct value_history_chunk
195
  {
196
    struct value_history_chunk *next;
197
    struct value *values[VALUE_HISTORY_CHUNK];
198
  };
199
 
200
/* Chain of chunks now in use.  */
201
 
202
static struct value_history_chunk *value_history_chain;
203
 
204
static int value_history_count; /* Abs number of last entry stored */
205
 
206
/* List of all value objects currently allocated
207
   (except for those released by calls to release_value)
208
   This is so they can be freed after each command.  */
209
 
210
static struct value *all_values;
211
 
212
/* Allocate a  value  that has the correct length for type TYPE.  */
213
 
214
struct value *
215
allocate_value (struct type *type)
216
{
217
  struct value *val;
218
  struct type *atype = check_typedef (type);
219
 
220
  val = (struct value *) xzalloc (sizeof (struct value) + TYPE_LENGTH (atype));
221
  val->next = all_values;
222
  all_values = val;
223
  val->type = type;
224
  val->enclosing_type = type;
225
  VALUE_LVAL (val) = not_lval;
226
  VALUE_ADDRESS (val) = 0;
227
  VALUE_FRAME_ID (val) = null_frame_id;
228
  val->offset = 0;
229
  val->bitpos = 0;
230
  val->bitsize = 0;
231
  VALUE_REGNUM (val) = -1;
232
  val->lazy = 0;
233
  val->optimized_out = 0;
234
  val->embedded_offset = 0;
235
  val->pointed_to_offset = 0;
236
  val->modifiable = 1;
237
  val->initialized = 1;  /* Default to initialized.  */
238
  return val;
239
}
240
 
241
/* Allocate a  value  that has the correct length
242
   for COUNT repetitions type TYPE.  */
243
 
244
struct value *
245
allocate_repeat_value (struct type *type, int count)
246
{
247
  int low_bound = current_language->string_lower_bound;         /* ??? */
248
  /* FIXME-type-allocation: need a way to free this type when we are
249
     done with it.  */
250
  struct type *range_type
251
  = create_range_type ((struct type *) NULL, builtin_type_int,
252
                       low_bound, count + low_bound - 1);
253
  /* FIXME-type-allocation: need a way to free this type when we are
254
     done with it.  */
255
  return allocate_value (create_array_type ((struct type *) NULL,
256
                                            type, range_type));
257
}
258
 
259
/* Accessor methods.  */
260
 
261
struct value *
262
value_next (struct value *value)
263
{
264
  return value->next;
265
}
266
 
267
struct type *
268
value_type (struct value *value)
269
{
270
  return value->type;
271
}
272
void
273
deprecated_set_value_type (struct value *value, struct type *type)
274
{
275
  value->type = type;
276
}
277
 
278
int
279
value_offset (struct value *value)
280
{
281
  return value->offset;
282
}
283
void
284
set_value_offset (struct value *value, int offset)
285
{
286
  value->offset = offset;
287
}
288
 
289
int
290
value_bitpos (struct value *value)
291
{
292
  return value->bitpos;
293
}
294
void
295
set_value_bitpos (struct value *value, int bit)
296
{
297
  value->bitpos = bit;
298
}
299
 
300
int
301
value_bitsize (struct value *value)
302
{
303
  return value->bitsize;
304
}
305
void
306
set_value_bitsize (struct value *value, int bit)
307
{
308
  value->bitsize = bit;
309
}
310
 
311
gdb_byte *
312
value_contents_raw (struct value *value)
313
{
314
  return value->aligner.contents + value->embedded_offset;
315
}
316
 
317
gdb_byte *
318
value_contents_all_raw (struct value *value)
319
{
320
  return value->aligner.contents;
321
}
322
 
323
struct type *
324
value_enclosing_type (struct value *value)
325
{
326
  return value->enclosing_type;
327
}
328
 
329
const gdb_byte *
330
value_contents_all (struct value *value)
331
{
332
  if (value->lazy)
333
    value_fetch_lazy (value);
334
  return value->aligner.contents;
335
}
336
 
337
int
338
value_lazy (struct value *value)
339
{
340
  return value->lazy;
341
}
342
 
343
void
344
set_value_lazy (struct value *value, int val)
345
{
346
  value->lazy = val;
347
}
348
 
349
const gdb_byte *
350
value_contents (struct value *value)
351
{
352
  return value_contents_writeable (value);
353
}
354
 
355
gdb_byte *
356
value_contents_writeable (struct value *value)
357
{
358
  if (value->lazy)
359
    value_fetch_lazy (value);
360
  return value_contents_raw (value);
361
}
362
 
363
/* Return non-zero if VAL1 and VAL2 have the same contents.  Note that
364
   this function is different from value_equal; in C the operator ==
365
   can return 0 even if the two values being compared are equal.  */
366
 
367
int
368
value_contents_equal (struct value *val1, struct value *val2)
369
{
370
  struct type *type1;
371
  struct type *type2;
372
  int len;
373
 
374
  type1 = check_typedef (value_type (val1));
375
  type2 = check_typedef (value_type (val2));
376
  len = TYPE_LENGTH (type1);
377
  if (len != TYPE_LENGTH (type2))
378
    return 0;
379
 
380
  return (memcmp (value_contents (val1), value_contents (val2), len) == 0);
381
}
382
 
383
int
384
value_optimized_out (struct value *value)
385
{
386
  return value->optimized_out;
387
}
388
 
389
void
390
set_value_optimized_out (struct value *value, int val)
391
{
392
  value->optimized_out = val;
393
}
394
 
395
int
396
value_embedded_offset (struct value *value)
397
{
398
  return value->embedded_offset;
399
}
400
 
401
void
402
set_value_embedded_offset (struct value *value, int val)
403
{
404
  value->embedded_offset = val;
405
}
406
 
407
int
408
value_pointed_to_offset (struct value *value)
409
{
410
  return value->pointed_to_offset;
411
}
412
 
413
void
414
set_value_pointed_to_offset (struct value *value, int val)
415
{
416
  value->pointed_to_offset = val;
417
}
418
 
419
enum lval_type *
420
deprecated_value_lval_hack (struct value *value)
421
{
422
  return &value->lval;
423
}
424
 
425
CORE_ADDR *
426
deprecated_value_address_hack (struct value *value)
427
{
428
  return &value->location.address;
429
}
430
 
431
struct internalvar **
432
deprecated_value_internalvar_hack (struct value *value)
433
{
434
  return &value->location.internalvar;
435
}
436
 
437
struct frame_id *
438
deprecated_value_frame_id_hack (struct value *value)
439
{
440
  return &value->frame_id;
441
}
442
 
443
short *
444
deprecated_value_regnum_hack (struct value *value)
445
{
446
  return &value->regnum;
447
}
448
 
449
int
450
deprecated_value_modifiable (struct value *value)
451
{
452
  return value->modifiable;
453
}
454
void
455
deprecated_set_value_modifiable (struct value *value, int modifiable)
456
{
457
  value->modifiable = modifiable;
458
}
459
 
460
/* Return a mark in the value chain.  All values allocated after the
461
   mark is obtained (except for those released) are subject to being freed
462
   if a subsequent value_free_to_mark is passed the mark.  */
463
struct value *
464
value_mark (void)
465
{
466
  return all_values;
467
}
468
 
469
/* Free all values allocated since MARK was obtained by value_mark
470
   (except for those released).  */
471
void
472
value_free_to_mark (struct value *mark)
473
{
474
  struct value *val;
475
  struct value *next;
476
 
477
  for (val = all_values; val && val != mark; val = next)
478
    {
479
      next = val->next;
480
      value_free (val);
481
    }
482
  all_values = val;
483
}
484
 
485
/* Free all the values that have been allocated (except for those released).
486
   Called after each command, successful or not.  */
487
 
488
void
489
free_all_values (void)
490
{
491
  struct value *val;
492
  struct value *next;
493
 
494
  for (val = all_values; val; val = next)
495
    {
496
      next = val->next;
497
      value_free (val);
498
    }
499
 
500
  all_values = 0;
501
}
502
 
503
/* Remove VAL from the chain all_values
504
   so it will not be freed automatically.  */
505
 
506
void
507
release_value (struct value *val)
508
{
509
  struct value *v;
510
 
511
  if (all_values == val)
512
    {
513
      all_values = val->next;
514
      return;
515
    }
516
 
517
  for (v = all_values; v; v = v->next)
518
    {
519
      if (v->next == val)
520
        {
521
          v->next = val->next;
522
          break;
523
        }
524
    }
525
}
526
 
527
/* Release all values up to mark  */
528
struct value *
529
value_release_to_mark (struct value *mark)
530
{
531
  struct value *val;
532
  struct value *next;
533
 
534
  for (val = next = all_values; next; next = next->next)
535
    if (next->next == mark)
536
      {
537
        all_values = next->next;
538
        next->next = NULL;
539
        return val;
540
      }
541
  all_values = 0;
542
  return val;
543
}
544
 
545
/* Return a copy of the value ARG.
546
   It contains the same contents, for same memory address,
547
   but it's a different block of storage.  */
548
 
549
struct value *
550
value_copy (struct value *arg)
551
{
552
  struct type *encl_type = value_enclosing_type (arg);
553
  struct value *val = allocate_value (encl_type);
554
  val->type = arg->type;
555
  VALUE_LVAL (val) = VALUE_LVAL (arg);
556
  val->location = arg->location;
557
  val->offset = arg->offset;
558
  val->bitpos = arg->bitpos;
559
  val->bitsize = arg->bitsize;
560
  VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
561
  VALUE_REGNUM (val) = VALUE_REGNUM (arg);
562
  val->lazy = arg->lazy;
563
  val->optimized_out = arg->optimized_out;
564
  val->embedded_offset = value_embedded_offset (arg);
565
  val->pointed_to_offset = arg->pointed_to_offset;
566
  val->modifiable = arg->modifiable;
567
  if (!value_lazy (val))
568
    {
569
      memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
570
              TYPE_LENGTH (value_enclosing_type (arg)));
571
 
572
    }
573
  return val;
574
}
575
 
576
/* Access to the value history.  */
577
 
578
/* Record a new value in the value history.
579
   Returns the absolute history index of the entry.
580
   Result of -1 indicates the value was not saved; otherwise it is the
581
   value history index of this new item.  */
582
 
583
int
584
record_latest_value (struct value *val)
585
{
586
  int i;
587
 
588
  /* We don't want this value to have anything to do with the inferior anymore.
589
     In particular, "set $1 = 50" should not affect the variable from which
590
     the value was taken, and fast watchpoints should be able to assume that
591
     a value on the value history never changes.  */
592
  if (value_lazy (val))
593
    value_fetch_lazy (val);
594
  /* We preserve VALUE_LVAL so that the user can find out where it was fetched
595
     from.  This is a bit dubious, because then *&$1 does not just return $1
596
     but the current contents of that location.  c'est la vie...  */
597
  val->modifiable = 0;
598
  release_value (val);
599
 
600
  /* Here we treat value_history_count as origin-zero
601
     and applying to the value being stored now.  */
602
 
603
  i = value_history_count % VALUE_HISTORY_CHUNK;
604
  if (i == 0)
605
    {
606
      struct value_history_chunk *new
607
      = (struct value_history_chunk *)
608
      xmalloc (sizeof (struct value_history_chunk));
609
      memset (new->values, 0, sizeof new->values);
610
      new->next = value_history_chain;
611
      value_history_chain = new;
612
    }
613
 
614
  value_history_chain->values[i] = val;
615
 
616
  /* Now we regard value_history_count as origin-one
617
     and applying to the value just stored.  */
618
 
619
  return ++value_history_count;
620
}
621
 
622
/* Return a copy of the value in the history with sequence number NUM.  */
623
 
624
struct value *
625
access_value_history (int num)
626
{
627
  struct value_history_chunk *chunk;
628
  int i;
629
  int absnum = num;
630
 
631
  if (absnum <= 0)
632
    absnum += value_history_count;
633
 
634
  if (absnum <= 0)
635
    {
636
      if (num == 0)
637
        error (_("The history is empty."));
638
      else if (num == 1)
639
        error (_("There is only one value in the history."));
640
      else
641
        error (_("History does not go back to $$%d."), -num);
642
    }
643
  if (absnum > value_history_count)
644
    error (_("History has not yet reached $%d."), absnum);
645
 
646
  absnum--;
647
 
648
  /* Now absnum is always absolute and origin zero.  */
649
 
650
  chunk = value_history_chain;
651
  for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
652
       i > 0; i--)
653
    chunk = chunk->next;
654
 
655
  return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
656
}
657
 
658
static void
659
show_values (char *num_exp, int from_tty)
660
{
661
  int i;
662
  struct value *val;
663
  static int num = 1;
664
 
665
  if (num_exp)
666
    {
667
      /* "info history +" should print from the stored position.
668
         "info history <exp>" should print around value number <exp>.  */
669
      if (num_exp[0] != '+' || num_exp[1] != '\0')
670
        num = parse_and_eval_long (num_exp) - 5;
671
    }
672
  else
673
    {
674
      /* "info history" means print the last 10 values.  */
675
      num = value_history_count - 9;
676
    }
677
 
678
  if (num <= 0)
679
    num = 1;
680
 
681
  for (i = num; i < num + 10 && i <= value_history_count; i++)
682
    {
683
      val = access_value_history (i);
684
      printf_filtered (("$%d = "), i);
685
      value_print (val, gdb_stdout, 0, Val_pretty_default);
686
      printf_filtered (("\n"));
687
    }
688
 
689
  /* The next "info history +" should start after what we just printed.  */
690
  num += 10;
691
 
692
  /* Hitting just return after this command should do the same thing as
693
     "info history +".  If num_exp is null, this is unnecessary, since
694
     "info history +" is not useful after "info history".  */
695
  if (from_tty && num_exp)
696
    {
697
      num_exp[0] = '+';
698
      num_exp[1] = '\0';
699
    }
700
}
701
 
702
/* Internal variables.  These are variables within the debugger
703
   that hold values assigned by debugger commands.
704
   The user refers to them with a '$' prefix
705
   that does not appear in the variable names stored internally.  */
706
 
707
static struct internalvar *internalvars;
708
 
709
/* If the variable does not already exist create it and give it the value given.
710
   If no value is given then the default is zero.  */
711
static void
712
init_if_undefined_command (char* args, int from_tty)
713
{
714
  struct internalvar* intvar;
715
 
716
  /* Parse the expression - this is taken from set_command().  */
717
  struct expression *expr = parse_expression (args);
718
  register struct cleanup *old_chain =
719
    make_cleanup (free_current_contents, &expr);
720
 
721
  /* Validate the expression.
722
     Was the expression an assignment?
723
     Or even an expression at all?  */
724
  if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
725
    error (_("Init-if-undefined requires an assignment expression."));
726
 
727
  /* Extract the variable from the parsed expression.
728
     In the case of an assign the lvalue will be in elts[1] and elts[2].  */
729
  if (expr->elts[1].opcode != OP_INTERNALVAR)
730
    error (_("The first parameter to init-if-undefined should be a GDB variable."));
731
  intvar = expr->elts[2].internalvar;
732
 
733
  /* Only evaluate the expression if the lvalue is void.
734
     This may still fail if the expresssion is invalid.  */
735
  if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID)
736
    evaluate_expression (expr);
737
 
738
  do_cleanups (old_chain);
739
}
740
 
741
 
742
/* Look up an internal variable with name NAME.  NAME should not
743
   normally include a dollar sign.
744
 
745
   If the specified internal variable does not exist,
746
   the return value is NULL.  */
747
 
748
struct internalvar *
749
lookup_only_internalvar (char *name)
750
{
751
  struct internalvar *var;
752
 
753
  for (var = internalvars; var; var = var->next)
754
    if (strcmp (var->name, name) == 0)
755
      return var;
756
 
757
  return NULL;
758
}
759
 
760
 
761
/* Create an internal variable with name NAME and with a void value.
762
   NAME should not normally include a dollar sign.  */
763
 
764
struct internalvar *
765
create_internalvar (char *name)
766
{
767
  struct internalvar *var;
768
  var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
769
  var->name = concat (name, (char *)NULL);
770
  var->value = allocate_value (builtin_type_void);
771
  var->endian = gdbarch_byte_order (current_gdbarch);
772
  release_value (var->value);
773
  var->next = internalvars;
774
  internalvars = var;
775
  return var;
776
}
777
 
778
 
779
/* Look up an internal variable with name NAME.  NAME should not
780
   normally include a dollar sign.
781
 
782
   If the specified internal variable does not exist,
783
   one is created, with a void value.  */
784
 
785
struct internalvar *
786
lookup_internalvar (char *name)
787
{
788
  struct internalvar *var;
789
 
790
  var = lookup_only_internalvar (name);
791
  if (var)
792
    return var;
793
 
794
  return create_internalvar (name);
795
}
796
 
797
struct value *
798
value_of_internalvar (struct internalvar *var)
799
{
800
  struct value *val;
801
  int i, j;
802
  gdb_byte temp;
803
 
804
  val = value_copy (var->value);
805
  if (value_lazy (val))
806
    value_fetch_lazy (val);
807
  VALUE_LVAL (val) = lval_internalvar;
808
  VALUE_INTERNALVAR (val) = var;
809
 
810
  /* Values are always stored in the target's byte order.  When connected to a
811
     target this will most likely always be correct, so there's normally no
812
     need to worry about it.
813
 
814
     However, internal variables can be set up before the target endian is
815
     known and so may become out of date.  Fix it up before anybody sees.
816
 
817
     Internal variables usually hold simple scalar values, and we can
818
     correct those.  More complex values (e.g. structures and floating
819
     point types) are left alone, because they would be too complicated
820
     to correct.  */
821
 
822
  if (var->endian != gdbarch_byte_order (current_gdbarch))
823
    {
824
      gdb_byte *array = value_contents_raw (val);
825
      struct type *type = check_typedef (value_enclosing_type (val));
826
      switch (TYPE_CODE (type))
827
        {
828
        case TYPE_CODE_INT:
829
        case TYPE_CODE_PTR:
830
          /* Reverse the bytes.  */
831
          for (i = 0, j = TYPE_LENGTH (type) - 1; i < j; i++, j--)
832
            {
833
              temp = array[j];
834
              array[j] = array[i];
835
              array[i] = temp;
836
            }
837
          break;
838
        }
839
    }
840
 
841
  return val;
842
}
843
 
844
void
845
set_internalvar_component (struct internalvar *var, int offset, int bitpos,
846
                           int bitsize, struct value *newval)
847
{
848
  gdb_byte *addr = value_contents_writeable (var->value) + offset;
849
 
850
  if (bitsize)
851
    modify_field (addr, value_as_long (newval),
852
                  bitpos, bitsize);
853
  else
854
    memcpy (addr, value_contents (newval), TYPE_LENGTH (value_type (newval)));
855
}
856
 
857
void
858
set_internalvar (struct internalvar *var, struct value *val)
859
{
860
  struct value *newval;
861
 
862
  newval = value_copy (val);
863
  newval->modifiable = 1;
864
 
865
  /* Force the value to be fetched from the target now, to avoid problems
866
     later when this internalvar is referenced and the target is gone or
867
     has changed.  */
868
  if (value_lazy (newval))
869
    value_fetch_lazy (newval);
870
 
871
  /* Begin code which must not call error().  If var->value points to
872
     something free'd, an error() obviously leaves a dangling pointer.
873
     But we also get a danling pointer if var->value points to
874
     something in the value chain (i.e., before release_value is
875
     called), because after the error free_all_values will get called before
876
     long.  */
877
  xfree (var->value);
878
  var->value = newval;
879
  var->endian = gdbarch_byte_order (current_gdbarch);
880
  release_value (newval);
881
  /* End code which must not call error().  */
882
}
883
 
884
char *
885
internalvar_name (struct internalvar *var)
886
{
887
  return var->name;
888
}
889
 
890
/* Update VALUE before discarding OBJFILE.  COPIED_TYPES is used to
891
   prevent cycles / duplicates.  */
892
 
893
static void
894
preserve_one_value (struct value *value, struct objfile *objfile,
895
                    htab_t copied_types)
896
{
897
  if (TYPE_OBJFILE (value->type) == objfile)
898
    value->type = copy_type_recursive (objfile, value->type, copied_types);
899
 
900
  if (TYPE_OBJFILE (value->enclosing_type) == objfile)
901
    value->enclosing_type = copy_type_recursive (objfile,
902
                                                 value->enclosing_type,
903
                                                 copied_types);
904
}
905
 
906
/* Update the internal variables and value history when OBJFILE is
907
   discarded; we must copy the types out of the objfile.  New global types
908
   will be created for every convenience variable which currently points to
909
   this objfile's types, and the convenience variables will be adjusted to
910
   use the new global types.  */
911
 
912
void
913
preserve_values (struct objfile *objfile)
914
{
915
  htab_t copied_types;
916
  struct value_history_chunk *cur;
917
  struct internalvar *var;
918
  int i;
919
 
920
  /* Create the hash table.  We allocate on the objfile's obstack, since
921
     it is soon to be deleted.  */
922
  copied_types = create_copied_types_hash (objfile);
923
 
924
  for (cur = value_history_chain; cur; cur = cur->next)
925
    for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
926
      if (cur->values[i])
927
        preserve_one_value (cur->values[i], objfile, copied_types);
928
 
929
  for (var = internalvars; var; var = var->next)
930
    preserve_one_value (var->value, objfile, copied_types);
931
 
932
  htab_delete (copied_types);
933
}
934
 
935
static void
936
show_convenience (char *ignore, int from_tty)
937
{
938
  struct internalvar *var;
939
  int varseen = 0;
940
 
941
  for (var = internalvars; var; var = var->next)
942
    {
943
      if (!varseen)
944
        {
945
          varseen = 1;
946
        }
947
      printf_filtered (("$%s = "), var->name);
948
      value_print (value_of_internalvar (var), gdb_stdout,
949
                   0, Val_pretty_default);
950
      printf_filtered (("\n"));
951
    }
952
  if (!varseen)
953
    printf_unfiltered (_("\
954
No debugger convenience variables now defined.\n\
955
Convenience variables have names starting with \"$\";\n\
956
use \"set\" as in \"set $foo = 5\" to define them.\n"));
957
}
958
 
959
/* Extract a value as a C number (either long or double).
960
   Knows how to convert fixed values to double, or
961
   floating values to long.
962
   Does not deallocate the value.  */
963
 
964
LONGEST
965
value_as_long (struct value *val)
966
{
967
  /* This coerces arrays and functions, which is necessary (e.g.
968
     in disassemble_command).  It also dereferences references, which
969
     I suspect is the most logical thing to do.  */
970
  val = coerce_array (val);
971
  return unpack_long (value_type (val), value_contents (val));
972
}
973
 
974
DOUBLEST
975
value_as_double (struct value *val)
976
{
977
  DOUBLEST foo;
978
  int inv;
979
 
980
  foo = unpack_double (value_type (val), value_contents (val), &inv);
981
  if (inv)
982
    error (_("Invalid floating value found in program."));
983
  return foo;
984
}
985
 
986
/* Extract a value as a C pointer. Does not deallocate the value.
987
   Note that val's type may not actually be a pointer; value_as_long
988
   handles all the cases.  */
989
CORE_ADDR
990
value_as_address (struct value *val)
991
{
992
  /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
993
     whether we want this to be true eventually.  */
994
#if 0
995
  /* gdbarch_addr_bits_remove is wrong if we are being called for a
996
     non-address (e.g. argument to "signal", "info break", etc.), or
997
     for pointers to char, in which the low bits *are* significant.  */
998
  return gdbarch_addr_bits_remove (current_gdbarch, value_as_long (val));
999
#else
1000
 
1001
  /* There are several targets (IA-64, PowerPC, and others) which
1002
     don't represent pointers to functions as simply the address of
1003
     the function's entry point.  For example, on the IA-64, a
1004
     function pointer points to a two-word descriptor, generated by
1005
     the linker, which contains the function's entry point, and the
1006
     value the IA-64 "global pointer" register should have --- to
1007
     support position-independent code.  The linker generates
1008
     descriptors only for those functions whose addresses are taken.
1009
 
1010
     On such targets, it's difficult for GDB to convert an arbitrary
1011
     function address into a function pointer; it has to either find
1012
     an existing descriptor for that function, or call malloc and
1013
     build its own.  On some targets, it is impossible for GDB to
1014
     build a descriptor at all: the descriptor must contain a jump
1015
     instruction; data memory cannot be executed; and code memory
1016
     cannot be modified.
1017
 
1018
     Upon entry to this function, if VAL is a value of type `function'
1019
     (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
1020
     VALUE_ADDRESS (val) is the address of the function.  This is what
1021
     you'll get if you evaluate an expression like `main'.  The call
1022
     to COERCE_ARRAY below actually does all the usual unary
1023
     conversions, which includes converting values of type `function'
1024
     to `pointer to function'.  This is the challenging conversion
1025
     discussed above.  Then, `unpack_long' will convert that pointer
1026
     back into an address.
1027
 
1028
     So, suppose the user types `disassemble foo' on an architecture
1029
     with a strange function pointer representation, on which GDB
1030
     cannot build its own descriptors, and suppose further that `foo'
1031
     has no linker-built descriptor.  The address->pointer conversion
1032
     will signal an error and prevent the command from running, even
1033
     though the next step would have been to convert the pointer
1034
     directly back into the same address.
1035
 
1036
     The following shortcut avoids this whole mess.  If VAL is a
1037
     function, just return its address directly.  */
1038
  if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1039
      || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
1040
    return VALUE_ADDRESS (val);
1041
 
1042
  val = coerce_array (val);
1043
 
1044
  /* Some architectures (e.g. Harvard), map instruction and data
1045
     addresses onto a single large unified address space.  For
1046
     instance: An architecture may consider a large integer in the
1047
     range 0x10000000 .. 0x1000ffff to already represent a data
1048
     addresses (hence not need a pointer to address conversion) while
1049
     a small integer would still need to be converted integer to
1050
     pointer to address.  Just assume such architectures handle all
1051
     integer conversions in a single function.  */
1052
 
1053
  /* JimB writes:
1054
 
1055
     I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
1056
     must admonish GDB hackers to make sure its behavior matches the
1057
     compiler's, whenever possible.
1058
 
1059
     In general, I think GDB should evaluate expressions the same way
1060
     the compiler does.  When the user copies an expression out of
1061
     their source code and hands it to a `print' command, they should
1062
     get the same value the compiler would have computed.  Any
1063
     deviation from this rule can cause major confusion and annoyance,
1064
     and needs to be justified carefully.  In other words, GDB doesn't
1065
     really have the freedom to do these conversions in clever and
1066
     useful ways.
1067
 
1068
     AndrewC pointed out that users aren't complaining about how GDB
1069
     casts integers to pointers; they are complaining that they can't
1070
     take an address from a disassembly listing and give it to `x/i'.
1071
     This is certainly important.
1072
 
1073
     Adding an architecture method like integer_to_address() certainly
1074
     makes it possible for GDB to "get it right" in all circumstances
1075
     --- the target has complete control over how things get done, so
1076
     people can Do The Right Thing for their target without breaking
1077
     anyone else.  The standard doesn't specify how integers get
1078
     converted to pointers; usually, the ABI doesn't either, but
1079
     ABI-specific code is a more reasonable place to handle it.  */
1080
 
1081
  if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
1082
      && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
1083
      && gdbarch_integer_to_address_p (current_gdbarch))
1084
    return gdbarch_integer_to_address (current_gdbarch, value_type (val),
1085
                                       value_contents (val));
1086
 
1087
  return unpack_long (value_type (val), value_contents (val));
1088
#endif
1089
}
1090
 
1091
/* Unpack raw data (copied from debugee, target byte order) at VALADDR
1092
   as a long, or as a double, assuming the raw data is described
1093
   by type TYPE.  Knows how to convert different sizes of values
1094
   and can convert between fixed and floating point.  We don't assume
1095
   any alignment for the raw data.  Return value is in host byte order.
1096
 
1097
   If you want functions and arrays to be coerced to pointers, and
1098
   references to be dereferenced, call value_as_long() instead.
1099
 
1100
   C++: It is assumed that the front-end has taken care of
1101
   all matters concerning pointers to members.  A pointer
1102
   to member which reaches here is considered to be equivalent
1103
   to an INT (or some size).  After all, it is only an offset.  */
1104
 
1105
LONGEST
1106
unpack_long (struct type *type, const gdb_byte *valaddr)
1107
{
1108
  enum type_code code = TYPE_CODE (type);
1109
  int len = TYPE_LENGTH (type);
1110
  int nosign = TYPE_UNSIGNED (type);
1111
 
1112
  switch (code)
1113
    {
1114
    case TYPE_CODE_TYPEDEF:
1115
      return unpack_long (check_typedef (type), valaddr);
1116
    case TYPE_CODE_ENUM:
1117
    case TYPE_CODE_FLAGS:
1118
    case TYPE_CODE_BOOL:
1119
    case TYPE_CODE_INT:
1120
    case TYPE_CODE_CHAR:
1121
    case TYPE_CODE_RANGE:
1122
    case TYPE_CODE_MEMBERPTR:
1123
      if (nosign)
1124
        return extract_unsigned_integer (valaddr, len);
1125
      else
1126
        return extract_signed_integer (valaddr, len);
1127
 
1128
    case TYPE_CODE_FLT:
1129
      return extract_typed_floating (valaddr, type);
1130
 
1131
    case TYPE_CODE_DECFLOAT:
1132
      /* libdecnumber has a function to convert from decimal to integer, but
1133
         it doesn't work when the decimal number has a fractional part.  */
1134
      return decimal_to_doublest (valaddr, len);
1135
 
1136
    case TYPE_CODE_PTR:
1137
    case TYPE_CODE_REF:
1138
      /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
1139
         whether we want this to be true eventually.  */
1140
      return extract_typed_address (valaddr, type);
1141
 
1142
    default:
1143
      error (_("Value can't be converted to integer."));
1144
    }
1145
  return 0;                      /* Placate lint.  */
1146
}
1147
 
1148
/* Return a double value from the specified type and address.
1149
   INVP points to an int which is set to 0 for valid value,
1150
   1 for invalid value (bad float format).  In either case,
1151
   the returned double is OK to use.  Argument is in target
1152
   format, result is in host format.  */
1153
 
1154
DOUBLEST
1155
unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
1156
{
1157
  enum type_code code;
1158
  int len;
1159
  int nosign;
1160
 
1161
  *invp = 0;                     /* Assume valid.   */
1162
  CHECK_TYPEDEF (type);
1163
  code = TYPE_CODE (type);
1164
  len = TYPE_LENGTH (type);
1165
  nosign = TYPE_UNSIGNED (type);
1166
  if (code == TYPE_CODE_FLT)
1167
    {
1168
      /* NOTE: cagney/2002-02-19: There was a test here to see if the
1169
         floating-point value was valid (using the macro
1170
         INVALID_FLOAT).  That test/macro have been removed.
1171
 
1172
         It turns out that only the VAX defined this macro and then
1173
         only in a non-portable way.  Fixing the portability problem
1174
         wouldn't help since the VAX floating-point code is also badly
1175
         bit-rotten.  The target needs to add definitions for the
1176
         methods gdbarch_float_format and gdbarch_double_format - these
1177
         exactly describe the target floating-point format.  The
1178
         problem here is that the corresponding floatformat_vax_f and
1179
         floatformat_vax_d values these methods should be set to are
1180
         also not defined either.  Oops!
1181
 
1182
         Hopefully someone will add both the missing floatformat
1183
         definitions and the new cases for floatformat_is_valid ().  */
1184
 
1185
      if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
1186
        {
1187
          *invp = 1;
1188
          return 0.0;
1189
        }
1190
 
1191
      return extract_typed_floating (valaddr, type);
1192
    }
1193
  else if (code == TYPE_CODE_DECFLOAT)
1194
    return decimal_to_doublest (valaddr, len);
1195
  else if (nosign)
1196
    {
1197
      /* Unsigned -- be sure we compensate for signed LONGEST.  */
1198
      return (ULONGEST) unpack_long (type, valaddr);
1199
    }
1200
  else
1201
    {
1202
      /* Signed -- we are OK with unpack_long.  */
1203
      return unpack_long (type, valaddr);
1204
    }
1205
}
1206
 
1207
/* Unpack raw data (copied from debugee, target byte order) at VALADDR
1208
   as a CORE_ADDR, assuming the raw data is described by type TYPE.
1209
   We don't assume any alignment for the raw data.  Return value is in
1210
   host byte order.
1211
 
1212
   If you want functions and arrays to be coerced to pointers, and
1213
   references to be dereferenced, call value_as_address() instead.
1214
 
1215
   C++: It is assumed that the front-end has taken care of
1216
   all matters concerning pointers to members.  A pointer
1217
   to member which reaches here is considered to be equivalent
1218
   to an INT (or some size).  After all, it is only an offset.  */
1219
 
1220
CORE_ADDR
1221
unpack_pointer (struct type *type, const gdb_byte *valaddr)
1222
{
1223
  /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
1224
     whether we want this to be true eventually.  */
1225
  return unpack_long (type, valaddr);
1226
}
1227
 
1228
 
1229
/* Get the value of the FIELDN'th field (which must be static) of
1230
   TYPE.  Return NULL if the field doesn't exist or has been
1231
   optimized out. */
1232
 
1233
struct value *
1234
value_static_field (struct type *type, int fieldno)
1235
{
1236
  struct value *retval;
1237
 
1238
  if (TYPE_FIELD_STATIC_HAS_ADDR (type, fieldno))
1239
    {
1240
      retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
1241
                         TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1242
    }
1243
  else
1244
    {
1245
      char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1246
      struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0, NULL);
1247
      if (sym == NULL)
1248
        {
1249
          /* With some compilers, e.g. HP aCC, static data members are reported
1250
             as non-debuggable symbols */
1251
          struct minimal_symbol *msym = lookup_minimal_symbol (phys_name, NULL, NULL);
1252
          if (!msym)
1253
            return NULL;
1254
          else
1255
            {
1256
              retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
1257
                                 SYMBOL_VALUE_ADDRESS (msym));
1258
            }
1259
        }
1260
      else
1261
        {
1262
          /* SYM should never have a SYMBOL_CLASS which will require
1263
             read_var_value to use the FRAME parameter.  */
1264
          if (symbol_read_needs_frame (sym))
1265
            warning (_("static field's value depends on the current "
1266
                     "frame - bad debug info?"));
1267
          retval = read_var_value (sym, NULL);
1268
        }
1269
      if (retval && VALUE_LVAL (retval) == lval_memory)
1270
        SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
1271
                            VALUE_ADDRESS (retval));
1272
    }
1273
  return retval;
1274
}
1275
 
1276
/* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
1277
   You have to be careful here, since the size of the data area for the value
1278
   is set by the length of the enclosing type.  So if NEW_ENCL_TYPE is bigger
1279
   than the old enclosing type, you have to allocate more space for the data.
1280
   The return value is a pointer to the new version of this value structure. */
1281
 
1282
struct value *
1283
value_change_enclosing_type (struct value *val, struct type *new_encl_type)
1284
{
1285
  if (TYPE_LENGTH (new_encl_type) <= TYPE_LENGTH (value_enclosing_type (val)))
1286
    {
1287
      val->enclosing_type = new_encl_type;
1288
      return val;
1289
    }
1290
  else
1291
    {
1292
      struct value *new_val;
1293
      struct value *prev;
1294
 
1295
      new_val = (struct value *) xrealloc (val, sizeof (struct value) + TYPE_LENGTH (new_encl_type));
1296
 
1297
      new_val->enclosing_type = new_encl_type;
1298
 
1299
      /* We have to make sure this ends up in the same place in the value
1300
         chain as the original copy, so it's clean-up behavior is the same.
1301
         If the value has been released, this is a waste of time, but there
1302
         is no way to tell that in advance, so... */
1303
 
1304
      if (val != all_values)
1305
        {
1306
          for (prev = all_values; prev != NULL; prev = prev->next)
1307
            {
1308
              if (prev->next == val)
1309
                {
1310
                  prev->next = new_val;
1311
                  break;
1312
                }
1313
            }
1314
        }
1315
 
1316
      return new_val;
1317
    }
1318
}
1319
 
1320
/* Given a value ARG1 (offset by OFFSET bytes)
1321
   of a struct or union type ARG_TYPE,
1322
   extract and return the value of one of its (non-static) fields.
1323
   FIELDNO says which field. */
1324
 
1325
struct value *
1326
value_primitive_field (struct value *arg1, int offset,
1327
                       int fieldno, struct type *arg_type)
1328
{
1329
  struct value *v;
1330
  struct type *type;
1331
 
1332
  CHECK_TYPEDEF (arg_type);
1333
  type = TYPE_FIELD_TYPE (arg_type, fieldno);
1334
 
1335
  /* Handle packed fields */
1336
 
1337
  if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
1338
    {
1339
      v = value_from_longest (type,
1340
                              unpack_field_as_long (arg_type,
1341
                                                    value_contents (arg1)
1342
                                                    + offset,
1343
                                                    fieldno));
1344
      v->bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
1345
      v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
1346
      v->offset = value_offset (arg1) + offset
1347
        + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
1348
    }
1349
  else if (fieldno < TYPE_N_BASECLASSES (arg_type))
1350
    {
1351
      /* This field is actually a base subobject, so preserve the
1352
         entire object's contents for later references to virtual
1353
         bases, etc.  */
1354
      v = allocate_value (value_enclosing_type (arg1));
1355
      v->type = type;
1356
      if (value_lazy (arg1))
1357
        set_value_lazy (v, 1);
1358
      else
1359
        memcpy (value_contents_all_raw (v), value_contents_all_raw (arg1),
1360
                TYPE_LENGTH (value_enclosing_type (arg1)));
1361
      v->offset = value_offset (arg1);
1362
      v->embedded_offset = (offset + value_embedded_offset (arg1)
1363
                            + TYPE_FIELD_BITPOS (arg_type, fieldno) / 8);
1364
    }
1365
  else
1366
    {
1367
      /* Plain old data member */
1368
      offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
1369
      v = allocate_value (type);
1370
      if (value_lazy (arg1))
1371
        set_value_lazy (v, 1);
1372
      else
1373
        memcpy (value_contents_raw (v),
1374
                value_contents_raw (arg1) + offset,
1375
                TYPE_LENGTH (type));
1376
      v->offset = (value_offset (arg1) + offset
1377
                   + value_embedded_offset (arg1));
1378
    }
1379
  VALUE_LVAL (v) = VALUE_LVAL (arg1);
1380
  if (VALUE_LVAL (arg1) == lval_internalvar)
1381
    VALUE_LVAL (v) = lval_internalvar_component;
1382
  v->location = arg1->location;
1383
  VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
1384
  VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
1385
  return v;
1386
}
1387
 
1388
/* Given a value ARG1 of a struct or union type,
1389
   extract and return the value of one of its (non-static) fields.
1390
   FIELDNO says which field. */
1391
 
1392
struct value *
1393
value_field (struct value *arg1, int fieldno)
1394
{
1395
  return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
1396
}
1397
 
1398
/* Return a non-virtual function as a value.
1399
   F is the list of member functions which contains the desired method.
1400
   J is an index into F which provides the desired method.
1401
 
1402
   We only use the symbol for its address, so be happy with either a
1403
   full symbol or a minimal symbol.
1404
 */
1405
 
1406
struct value *
1407
value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
1408
                int offset)
1409
{
1410
  struct value *v;
1411
  struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
1412
  char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1413
  struct symbol *sym;
1414
  struct minimal_symbol *msym;
1415
 
1416
  sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0, NULL);
1417
  if (sym != NULL)
1418
    {
1419
      msym = NULL;
1420
    }
1421
  else
1422
    {
1423
      gdb_assert (sym == NULL);
1424
      msym = lookup_minimal_symbol (physname, NULL, NULL);
1425
      if (msym == NULL)
1426
        return NULL;
1427
    }
1428
 
1429
  v = allocate_value (ftype);
1430
  if (sym)
1431
    {
1432
      VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1433
    }
1434
  else
1435
    {
1436
      VALUE_ADDRESS (v) = SYMBOL_VALUE_ADDRESS (msym);
1437
    }
1438
 
1439
  if (arg1p)
1440
    {
1441
      if (type != value_type (*arg1p))
1442
        *arg1p = value_ind (value_cast (lookup_pointer_type (type),
1443
                                        value_addr (*arg1p)));
1444
 
1445
      /* Move the `this' pointer according to the offset.
1446
         VALUE_OFFSET (*arg1p) += offset;
1447
       */
1448
    }
1449
 
1450
  return v;
1451
}
1452
 
1453
 
1454
/* Unpack a field FIELDNO of the specified TYPE, from the anonymous object at
1455
   VALADDR.
1456
 
1457
   Extracting bits depends on endianness of the machine.  Compute the
1458
   number of least significant bits to discard.  For big endian machines,
1459
   we compute the total number of bits in the anonymous object, subtract
1460
   off the bit count from the MSB of the object to the MSB of the
1461
   bitfield, then the size of the bitfield, which leaves the LSB discard
1462
   count.  For little endian machines, the discard count is simply the
1463
   number of bits from the LSB of the anonymous object to the LSB of the
1464
   bitfield.
1465
 
1466
   If the field is signed, we also do sign extension. */
1467
 
1468
LONGEST
1469
unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
1470
{
1471
  ULONGEST val;
1472
  ULONGEST valmask;
1473
  int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1474
  int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1475
  int lsbcount;
1476
  struct type *field_type;
1477
 
1478
  val = extract_unsigned_integer (valaddr + bitpos / 8, sizeof (val));
1479
  field_type = TYPE_FIELD_TYPE (type, fieldno);
1480
  CHECK_TYPEDEF (field_type);
1481
 
1482
  /* Extract bits.  See comment above. */
1483
 
1484
  if (gdbarch_bits_big_endian (current_gdbarch))
1485
    lsbcount = (sizeof val * 8 - bitpos % 8 - bitsize);
1486
  else
1487
    lsbcount = (bitpos % 8);
1488
  val >>= lsbcount;
1489
 
1490
  /* If the field does not entirely fill a LONGEST, then zero the sign bits.
1491
     If the field is signed, and is negative, then sign extend. */
1492
 
1493
  if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
1494
    {
1495
      valmask = (((ULONGEST) 1) << bitsize) - 1;
1496
      val &= valmask;
1497
      if (!TYPE_UNSIGNED (field_type))
1498
        {
1499
          if (val & (valmask ^ (valmask >> 1)))
1500
            {
1501
              val |= ~valmask;
1502
            }
1503
        }
1504
    }
1505
  return (val);
1506
}
1507
 
1508
/* Modify the value of a bitfield.  ADDR points to a block of memory in
1509
   target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
1510
   is the desired value of the field, in host byte order.  BITPOS and BITSIZE
1511
   indicate which bits (in target bit order) comprise the bitfield.
1512
   Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
1513
 
1514
 
1515
void
1516
modify_field (gdb_byte *addr, LONGEST fieldval, int bitpos, int bitsize)
1517
{
1518
  ULONGEST oword;
1519
  ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
1520
 
1521
  /* If a negative fieldval fits in the field in question, chop
1522
     off the sign extension bits.  */
1523
  if ((~fieldval & ~(mask >> 1)) == 0)
1524
    fieldval &= mask;
1525
 
1526
  /* Warn if value is too big to fit in the field in question.  */
1527
  if (0 != (fieldval & ~mask))
1528
    {
1529
      /* FIXME: would like to include fieldval in the message, but
1530
         we don't have a sprintf_longest.  */
1531
      warning (_("Value does not fit in %d bits."), bitsize);
1532
 
1533
      /* Truncate it, otherwise adjoining fields may be corrupted.  */
1534
      fieldval &= mask;
1535
    }
1536
 
1537
  oword = extract_unsigned_integer (addr, sizeof oword);
1538
 
1539
  /* Shifting for bit field depends on endianness of the target machine.  */
1540
  if (gdbarch_bits_big_endian (current_gdbarch))
1541
    bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1542
 
1543
  oword &= ~(mask << bitpos);
1544
  oword |= fieldval << bitpos;
1545
 
1546
  store_unsigned_integer (addr, sizeof oword, oword);
1547
}
1548
 
1549
/* Pack NUM into BUF using a target format of TYPE.  */
1550
 
1551
void
1552
pack_long (gdb_byte *buf, struct type *type, LONGEST num)
1553
{
1554
  int len;
1555
 
1556
  type = check_typedef (type);
1557
  len = TYPE_LENGTH (type);
1558
 
1559
  switch (TYPE_CODE (type))
1560
    {
1561
    case TYPE_CODE_INT:
1562
    case TYPE_CODE_CHAR:
1563
    case TYPE_CODE_ENUM:
1564
    case TYPE_CODE_FLAGS:
1565
    case TYPE_CODE_BOOL:
1566
    case TYPE_CODE_RANGE:
1567
    case TYPE_CODE_MEMBERPTR:
1568
      store_signed_integer (buf, len, num);
1569
      break;
1570
 
1571
    case TYPE_CODE_REF:
1572
    case TYPE_CODE_PTR:
1573
      store_typed_address (buf, type, (CORE_ADDR) num);
1574
      break;
1575
 
1576
    default:
1577
      error (_("Unexpected type (%d) encountered for integer constant."),
1578
             TYPE_CODE (type));
1579
    }
1580
}
1581
 
1582
 
1583
/* Convert C numbers into newly allocated values.  */
1584
 
1585
struct value *
1586
value_from_longest (struct type *type, LONGEST num)
1587
{
1588
  struct value *val = allocate_value (type);
1589
 
1590
  pack_long (value_contents_raw (val), type, num);
1591
 
1592
  return val;
1593
}
1594
 
1595
 
1596
/* Create a value representing a pointer of type TYPE to the address
1597
   ADDR.  */
1598
struct value *
1599
value_from_pointer (struct type *type, CORE_ADDR addr)
1600
{
1601
  struct value *val = allocate_value (type);
1602
  store_typed_address (value_contents_raw (val), type, addr);
1603
  return val;
1604
}
1605
 
1606
 
1607
/* Create a value for a string constant to be stored locally
1608
   (not in the inferior's memory space, but in GDB memory).
1609
   This is analogous to value_from_longest, which also does not
1610
   use inferior memory.  String shall NOT contain embedded nulls.  */
1611
 
1612
struct value *
1613
value_from_string (char *ptr)
1614
{
1615
  struct value *val;
1616
  int len = strlen (ptr);
1617
  int lowbound = current_language->string_lower_bound;
1618
  struct type *string_char_type;
1619
  struct type *rangetype;
1620
  struct type *stringtype;
1621
 
1622
  rangetype = create_range_type ((struct type *) NULL,
1623
                                 builtin_type_int,
1624
                                 lowbound, len + lowbound - 1);
1625
  string_char_type = language_string_char_type (current_language,
1626
                                                current_gdbarch);
1627
  stringtype = create_array_type ((struct type *) NULL,
1628
                                  string_char_type,
1629
                                  rangetype);
1630
  val = allocate_value (stringtype);
1631
  memcpy (value_contents_raw (val), ptr, len);
1632
  return val;
1633
}
1634
 
1635
struct value *
1636
value_from_double (struct type *type, DOUBLEST num)
1637
{
1638
  struct value *val = allocate_value (type);
1639
  struct type *base_type = check_typedef (type);
1640
  enum type_code code = TYPE_CODE (base_type);
1641
  int len = TYPE_LENGTH (base_type);
1642
 
1643
  if (code == TYPE_CODE_FLT)
1644
    {
1645
      store_typed_floating (value_contents_raw (val), base_type, num);
1646
    }
1647
  else
1648
    error (_("Unexpected type encountered for floating constant."));
1649
 
1650
  return val;
1651
}
1652
 
1653
struct value *
1654
value_from_decfloat (struct type *type, const gdb_byte *dec)
1655
{
1656
  struct value *val = allocate_value (type);
1657
 
1658
  memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
1659
 
1660
  return val;
1661
}
1662
 
1663
struct value *
1664
coerce_ref (struct value *arg)
1665
{
1666
  struct type *value_type_arg_tmp = check_typedef (value_type (arg));
1667
  if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF)
1668
    arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),
1669
                         unpack_pointer (value_type (arg),
1670
                                         value_contents (arg)));
1671
  return arg;
1672
}
1673
 
1674
struct value *
1675
coerce_array (struct value *arg)
1676
{
1677
  arg = coerce_ref (arg);
1678
  if (current_language->c_style_arrays
1679
      && TYPE_CODE (value_type (arg)) == TYPE_CODE_ARRAY)
1680
    arg = value_coerce_array (arg);
1681
  if (TYPE_CODE (value_type (arg)) == TYPE_CODE_FUNC)
1682
    arg = value_coerce_function (arg);
1683
  return arg;
1684
}
1685
 
1686
struct value *
1687
coerce_number (struct value *arg)
1688
{
1689
  arg = coerce_array (arg);
1690
  arg = coerce_enum (arg);
1691
  return arg;
1692
}
1693
 
1694
struct value *
1695
coerce_enum (struct value *arg)
1696
{
1697
  if (TYPE_CODE (check_typedef (value_type (arg))) == TYPE_CODE_ENUM)
1698
    arg = value_cast (builtin_type_unsigned_int, arg);
1699
  return arg;
1700
}
1701
 
1702
 
1703
/* Return true if the function returning the specified type is using
1704
   the convention of returning structures in memory (passing in the
1705
   address as a hidden first parameter).  */
1706
 
1707
int
1708
using_struct_return (struct type *value_type)
1709
{
1710
  enum type_code code = TYPE_CODE (value_type);
1711
 
1712
  if (code == TYPE_CODE_ERROR)
1713
    error (_("Function return type unknown."));
1714
 
1715
  if (code == TYPE_CODE_VOID)
1716
    /* A void return value is never in memory.  See also corresponding
1717
       code in "print_return_value".  */
1718
    return 0;
1719
 
1720
  /* Probe the architecture for the return-value convention.  */
1721
  return (gdbarch_return_value (current_gdbarch, value_type,
1722
                                NULL, NULL, NULL)
1723
          != RETURN_VALUE_REGISTER_CONVENTION);
1724
}
1725
 
1726
/* Set the initialized field in a value struct.  */
1727
 
1728
void
1729
set_value_initialized (struct value *val, int status)
1730
{
1731
  val->initialized = status;
1732
}
1733
 
1734
/* Return the initialized field in a value struct.  */
1735
 
1736
int
1737
value_initialized (struct value *val)
1738
{
1739
  return val->initialized;
1740
}
1741
 
1742
void
1743
_initialize_values (void)
1744
{
1745
  add_cmd ("convenience", no_class, show_convenience, _("\
1746
Debugger convenience (\"$foo\") variables.\n\
1747
These variables are created when you assign them values;\n\
1748
thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\
1749
\n\
1750
A few convenience variables are given values automatically:\n\
1751
\"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1752
\"$__\" holds the contents of the last address examined with \"x\"."),
1753
           &showlist);
1754
 
1755
  add_cmd ("values", no_class, show_values,
1756
           _("Elements of value history around item number IDX (or last ten)."),
1757
           &showlist);
1758
 
1759
  add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
1760
Initialize a convenience variable if necessary.\n\
1761
init-if-undefined VARIABLE = EXPRESSION\n\
1762
Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
1763
exist or does not contain a value.  The EXPRESSION is not evaluated if the\n\
1764
VARIABLE is already initialized."));
1765
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.