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