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