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