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