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