values.c (value_virtual_fn_field): If there is no fcontext,
[binutils-gdb.git] / gdb / values.c
1 /* Low level packing and unpacking of values for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "defs.h"
23 #include "param.h"
24 #include "symtab.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "frame.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30
31 /* The value-history records all the values printed
32 by print commands during this session. Each chunk
33 records 60 consecutive values. The first chunk on
34 the chain records the most recent values.
35 The total number of values is in value_history_count. */
36
37 #define VALUE_HISTORY_CHUNK 60
38
39 struct value_history_chunk
40 {
41 struct value_history_chunk *next;
42 value values[VALUE_HISTORY_CHUNK];
43 };
44
45 /* Chain of chunks now in use. */
46
47 static struct value_history_chunk *value_history_chain;
48
49 static int value_history_count; /* Abs number of last entry stored */
50
51 \f
52 /* List of all value objects currently allocated
53 (except for those released by calls to release_value)
54 This is so they can be freed after each command. */
55
56 static value all_values;
57
58 /* Allocate a value that has the correct length for type TYPE. */
59
60 value
61 allocate_value (type)
62 struct type *type;
63 {
64 register value val;
65
66 check_stub_type (type);
67
68 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
69 VALUE_NEXT (val) = all_values;
70 all_values = val;
71 VALUE_TYPE (val) = type;
72 VALUE_LVAL (val) = not_lval;
73 VALUE_ADDRESS (val) = 0;
74 VALUE_FRAME (val) = 0;
75 VALUE_OFFSET (val) = 0;
76 VALUE_BITPOS (val) = 0;
77 VALUE_BITSIZE (val) = 0;
78 VALUE_REPEATED (val) = 0;
79 VALUE_REPETITIONS (val) = 0;
80 VALUE_REGNO (val) = -1;
81 VALUE_LAZY (val) = 0;
82 VALUE_OPTIMIZED_OUT (val) = 0;
83 return val;
84 }
85
86 /* Allocate a value that has the correct length
87 for COUNT repetitions type TYPE. */
88
89 value
90 allocate_repeat_value (type, count)
91 struct type *type;
92 int count;
93 {
94 register value val;
95
96 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
97 VALUE_NEXT (val) = all_values;
98 all_values = val;
99 VALUE_TYPE (val) = type;
100 VALUE_LVAL (val) = not_lval;
101 VALUE_ADDRESS (val) = 0;
102 VALUE_FRAME (val) = 0;
103 VALUE_OFFSET (val) = 0;
104 VALUE_BITPOS (val) = 0;
105 VALUE_BITSIZE (val) = 0;
106 VALUE_REPEATED (val) = 1;
107 VALUE_REPETITIONS (val) = count;
108 VALUE_REGNO (val) = -1;
109 VALUE_LAZY (val) = 0;
110 VALUE_OPTIMIZED_OUT (val) = 0;
111 return val;
112 }
113
114 /* Return a mark in the value chain. All values allocated after the
115 mark is obtained (except for those released) are subject to being freed
116 if a subsequent value_free_to_mark is passed the mark. */
117 value
118 value_mark ()
119 {
120 return all_values;
121 }
122
123 /* Free all values allocated since MARK was obtained by value_mark
124 (except for those released). */
125 void
126 value_free_to_mark (mark)
127 value mark;
128 {
129 value val, next;
130
131 for (val = all_values; val && val != mark; val = next)
132 {
133 next = VALUE_NEXT (val);
134 value_free (val);
135 }
136 all_values = val;
137 }
138
139 /* Free all the values that have been allocated (except for those released).
140 Called after each command, successful or not. */
141
142 void
143 free_all_values ()
144 {
145 register value val, next;
146
147 for (val = all_values; val; val = next)
148 {
149 next = VALUE_NEXT (val);
150 value_free (val);
151 }
152
153 all_values = 0;
154 }
155
156 /* Remove VAL from the chain all_values
157 so it will not be freed automatically. */
158
159 void
160 release_value (val)
161 register value val;
162 {
163 register value v;
164
165 if (all_values == val)
166 {
167 all_values = val->next;
168 return;
169 }
170
171 for (v = all_values; v; v = v->next)
172 {
173 if (v->next == val)
174 {
175 v->next = val->next;
176 break;
177 }
178 }
179 }
180
181 /* Return a copy of the value ARG.
182 It contains the same contents, for same memory address,
183 but it's a different block of storage. */
184
185 static value
186 value_copy (arg)
187 value arg;
188 {
189 register value val;
190 register struct type *type = VALUE_TYPE (arg);
191 if (VALUE_REPEATED (arg))
192 val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
193 else
194 val = allocate_value (type);
195 VALUE_LVAL (val) = VALUE_LVAL (arg);
196 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
197 VALUE_OFFSET (val) = VALUE_OFFSET (arg);
198 VALUE_BITPOS (val) = VALUE_BITPOS (arg);
199 VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
200 VALUE_REGNO (val) = VALUE_REGNO (arg);
201 VALUE_LAZY (val) = VALUE_LAZY (arg);
202 if (!VALUE_LAZY (val))
203 {
204 bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
205 TYPE_LENGTH (VALUE_TYPE (arg))
206 * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
207 }
208 return val;
209 }
210 \f
211 /* Access to the value history. */
212
213 /* Record a new value in the value history.
214 Returns the absolute history index of the entry.
215 Result of -1 indicates the value was not saved; otherwise it is the
216 value history index of this new item. */
217
218 int
219 record_latest_value (val)
220 value val;
221 {
222 int i;
223
224 /* Check error now if about to store an invalid float. We return -1
225 to the caller, but allow them to continue, e.g. to print it as "Nan". */
226 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
227 (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
228 if (i) return -1; /* Indicate value not saved in history */
229 }
230
231 /* Here we treat value_history_count as origin-zero
232 and applying to the value being stored now. */
233
234 i = value_history_count % VALUE_HISTORY_CHUNK;
235 if (i == 0)
236 {
237 register struct value_history_chunk *new
238 = (struct value_history_chunk *)
239 xmalloc (sizeof (struct value_history_chunk));
240 bzero (new->values, sizeof new->values);
241 new->next = value_history_chain;
242 value_history_chain = new;
243 }
244
245 value_history_chain->values[i] = val;
246 release_value (val);
247
248 /* Now we regard value_history_count as origin-one
249 and applying to the value just stored. */
250
251 return ++value_history_count;
252 }
253
254 /* Return a copy of the value in the history with sequence number NUM. */
255
256 value
257 access_value_history (num)
258 int num;
259 {
260 register struct value_history_chunk *chunk;
261 register int i;
262 register int absnum = num;
263
264 if (absnum <= 0)
265 absnum += value_history_count;
266
267 if (absnum <= 0)
268 {
269 if (num == 0)
270 error ("The history is empty.");
271 else if (num == 1)
272 error ("There is only one value in the history.");
273 else
274 error ("History does not go back to $$%d.", -num);
275 }
276 if (absnum > value_history_count)
277 error ("History has not yet reached $%d.", absnum);
278
279 absnum--;
280
281 /* Now absnum is always absolute and origin zero. */
282
283 chunk = value_history_chain;
284 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
285 i > 0; i--)
286 chunk = chunk->next;
287
288 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
289 }
290
291 /* Clear the value history entirely.
292 Must be done when new symbol tables are loaded,
293 because the type pointers become invalid. */
294
295 void
296 clear_value_history ()
297 {
298 register struct value_history_chunk *next;
299 register int i;
300 register value val;
301
302 while (value_history_chain)
303 {
304 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
305 if (val = value_history_chain->values[i])
306 free (val);
307 next = value_history_chain->next;
308 free (value_history_chain);
309 value_history_chain = next;
310 }
311 value_history_count = 0;
312 }
313
314 static void
315 show_values (num_exp, from_tty)
316 char *num_exp;
317 int from_tty;
318 {
319 register int i;
320 register value val;
321 static int num = 1;
322
323 if (num_exp)
324 {
325 if (num_exp[0] == '+' && num_exp[1] == '\0')
326 /* "info history +" should print from the stored position. */
327 ;
328 else
329 /* "info history <exp>" should print around value number <exp>. */
330 num = parse_and_eval_address (num_exp) - 5;
331 }
332 else
333 {
334 /* "info history" means print the last 10 values. */
335 num = value_history_count - 9;
336 }
337
338 if (num <= 0)
339 num = 1;
340
341 for (i = num; i < num + 10 && i <= value_history_count; i++)
342 {
343 val = access_value_history (i);
344 printf_filtered ("$%d = ", i);
345 value_print (val, stdout, 0, Val_pretty_default);
346 printf_filtered ("\n");
347 }
348
349 /* The next "info history +" should start after what we just printed. */
350 num += 10;
351
352 /* Hitting just return after this command should do the same thing as
353 "info history +". If num_exp is null, this is unnecessary, since
354 "info history +" is not useful after "info history". */
355 if (from_tty && num_exp)
356 {
357 num_exp[0] = '+';
358 num_exp[1] = '\0';
359 }
360 }
361 \f
362 /* Internal variables. These are variables within the debugger
363 that hold values assigned by debugger commands.
364 The user refers to them with a '$' prefix
365 that does not appear in the variable names stored internally. */
366
367 static struct internalvar *internalvars;
368
369 /* Look up an internal variable with name NAME. NAME should not
370 normally include a dollar sign.
371
372 If the specified internal variable does not exist,
373 one is created, with a void value. */
374
375 struct internalvar *
376 lookup_internalvar (name)
377 char *name;
378 {
379 register struct internalvar *var;
380
381 for (var = internalvars; var; var = var->next)
382 if (!strcmp (var->name, name))
383 return var;
384
385 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
386 var->name = concat (name, "", "");
387 var->value = allocate_value (builtin_type_void);
388 release_value (var->value);
389 var->next = internalvars;
390 internalvars = var;
391 return var;
392 }
393
394 value
395 value_of_internalvar (var)
396 struct internalvar *var;
397 {
398 register value val;
399
400 #ifdef IS_TRAPPED_INTERNALVAR
401 if (IS_TRAPPED_INTERNALVAR (var->name))
402 return VALUE_OF_TRAPPED_INTERNALVAR (var);
403 #endif
404
405 val = value_copy (var->value);
406 if (VALUE_LAZY (val))
407 value_fetch_lazy (val);
408 VALUE_LVAL (val) = lval_internalvar;
409 VALUE_INTERNALVAR (val) = var;
410 return val;
411 }
412
413 void
414 set_internalvar_component (var, offset, bitpos, bitsize, newval)
415 struct internalvar *var;
416 int offset, bitpos, bitsize;
417 value newval;
418 {
419 register char *addr = VALUE_CONTENTS (var->value) + offset;
420
421 #ifdef IS_TRAPPED_INTERNALVAR
422 if (IS_TRAPPED_INTERNALVAR (var->name))
423 SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
424 #endif
425
426 if (bitsize)
427 modify_field (addr, (int) value_as_long (newval),
428 bitpos, bitsize);
429 else
430 bcopy (VALUE_CONTENTS (newval), addr,
431 TYPE_LENGTH (VALUE_TYPE (newval)));
432 }
433
434 void
435 set_internalvar (var, val)
436 struct internalvar *var;
437 value val;
438 {
439 #ifdef IS_TRAPPED_INTERNALVAR
440 if (IS_TRAPPED_INTERNALVAR (var->name))
441 SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
442 #endif
443
444 free (var->value);
445 var->value = value_copy (val);
446 release_value (var->value);
447 }
448
449 char *
450 internalvar_name (var)
451 struct internalvar *var;
452 {
453 return var->name;
454 }
455
456 /* Free all internalvars. Done when new symtabs are loaded,
457 because that makes the values invalid. */
458
459 void
460 clear_internalvars ()
461 {
462 register struct internalvar *var;
463
464 while (internalvars)
465 {
466 var = internalvars;
467 internalvars = var->next;
468 free (var->name);
469 free (var->value);
470 free (var);
471 }
472 }
473
474 static void
475 show_convenience ()
476 {
477 register struct internalvar *var;
478 int varseen = 0;
479
480 for (var = internalvars; var; var = var->next)
481 {
482 #ifdef IS_TRAPPED_INTERNALVAR
483 if (IS_TRAPPED_INTERNALVAR (var->name))
484 continue;
485 #endif
486 if (!varseen)
487 {
488 #if 0
489 /* Useless noise. */
490 printf ("Debugger convenience variables:\n\n");
491 #endif
492 varseen = 1;
493 }
494 printf ("$%s = ", var->name);
495 value_print (var->value, stdout, 0, Val_pretty_default);
496 printf ("\n");
497 }
498 if (!varseen)
499 printf ("No debugger convenience variables now defined.\n\
500 Convenience variables have names starting with \"$\";\n\
501 use \"set\" as in \"set $foo = 5\" to define them.\n");
502 }
503 \f
504 /* Extract a value as a C number (either long or double).
505 Knows how to convert fixed values to double, or
506 floating values to long.
507 Does not deallocate the value. */
508
509 LONGEST
510 value_as_long (val)
511 register value val;
512 {
513 /* This coerces arrays and functions, which is necessary (e.g.
514 in disassemble_command). It also dereferences references, which
515 I suspect is the most logical thing to do. */
516 if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
517 COERCE_ARRAY (val);
518 return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
519 }
520
521 double
522 value_as_double (val)
523 register value val;
524 {
525 double foo;
526 int inv;
527
528 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
529 if (inv)
530 error ("Invalid floating value found in program.");
531 return foo;
532 }
533 \f
534 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
535 as a long, or as a double, assuming the raw data is described
536 by type TYPE. Knows how to convert different sizes of values
537 and can convert between fixed and floating point. We don't assume
538 any alignment for the raw data. Return value is in host byte order.
539
540 If you want functions and arrays to be coerced to pointers, and
541 references to be dereferenced, call value_as_long() instead.
542
543 C++: It is assumed that the front-end has taken care of
544 all matters concerning pointers to members. A pointer
545 to member which reaches here is considered to be equivalent
546 to an INT (or some size). After all, it is only an offset. */
547
548 LONGEST
549 unpack_long (type, valaddr)
550 struct type *type;
551 char *valaddr;
552 {
553 register enum type_code code = TYPE_CODE (type);
554 register int len = TYPE_LENGTH (type);
555 register int nosign = TYPE_UNSIGNED (type);
556
557 if (code == TYPE_CODE_ENUM)
558 code = TYPE_CODE_INT;
559 if (code == TYPE_CODE_FLT)
560 {
561 if (len == sizeof (float))
562 {
563 float retval;
564 bcopy (valaddr, &retval, sizeof (retval));
565 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
566 return retval;
567 }
568
569 if (len == sizeof (double))
570 {
571 double retval;
572 bcopy (valaddr, &retval, sizeof (retval));
573 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
574 return retval;
575 }
576 else
577 {
578 error ("Unexpected type of floating point number.");
579 }
580 }
581 else if (code == TYPE_CODE_INT && nosign)
582 {
583 if (len == sizeof (char))
584 {
585 unsigned char retval = * (unsigned char *) valaddr;
586 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
587 return retval;
588 }
589
590 if (len == sizeof (short))
591 {
592 unsigned short retval;
593 bcopy (valaddr, &retval, sizeof (retval));
594 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
595 return retval;
596 }
597
598 if (len == sizeof (int))
599 {
600 unsigned int retval;
601 bcopy (valaddr, &retval, sizeof (retval));
602 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
603 return retval;
604 }
605
606 if (len == sizeof (long))
607 {
608 unsigned long retval;
609 bcopy (valaddr, &retval, sizeof (retval));
610 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
611 return retval;
612 }
613 #ifdef LONG_LONG
614 if (len == sizeof (long long))
615 {
616 unsigned long long retval;
617 bcopy (valaddr, &retval, sizeof (retval));
618 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
619 return retval;
620 }
621 #endif
622 else
623 {
624 error ("That operation is not possible on an integer of that size.");
625 }
626 }
627 else if (code == TYPE_CODE_INT)
628 {
629 if (len == sizeof (char))
630 {
631 char retval;
632 bcopy (valaddr, &retval, sizeof (retval));
633 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
634 return retval;
635 }
636
637 if (len == sizeof (short))
638 {
639 short retval;
640 bcopy (valaddr, &retval, sizeof (retval));
641 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
642 return retval;
643 }
644
645 if (len == sizeof (int))
646 {
647 int retval;
648 bcopy (valaddr, &retval, sizeof (retval));
649 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
650 return retval;
651 }
652
653 if (len == sizeof (long))
654 {
655 long retval;
656 bcopy (valaddr, &retval, sizeof (retval));
657 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
658 return retval;
659 }
660
661 #ifdef LONG_LONG
662 if (len == sizeof (long long))
663 {
664 long long retval;
665 bcopy (valaddr, &retval, sizeof (retval));
666 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
667 return retval;
668 }
669 #endif
670 else
671 {
672 error ("That operation is not possible on an integer of that size.");
673 }
674 }
675 else if (code == TYPE_CODE_PTR
676 || code == TYPE_CODE_REF)
677 {
678 if (len == sizeof (char *))
679 {
680 CORE_ADDR retval;
681 bcopy (valaddr, &retval, sizeof (retval));
682 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
683 return retval;
684 }
685 }
686 else if (code == TYPE_CODE_MEMBER)
687 error ("not implemented: member types in unpack_long");
688
689 error ("Value not integer or pointer.");
690 return 0; /* For lint -- never reached */
691 }
692
693 /* Return a double value from the specified type and address.
694 INVP points to an int which is set to 0 for valid value,
695 1 for invalid value (bad float format). In either case,
696 the returned double is OK to use. Argument is in target
697 format, result is in host format. */
698
699 double
700 unpack_double (type, valaddr, invp)
701 struct type *type;
702 char *valaddr;
703 int *invp;
704 {
705 register enum type_code code = TYPE_CODE (type);
706 register int len = TYPE_LENGTH (type);
707 register int nosign = TYPE_UNSIGNED (type);
708
709 *invp = 0; /* Assume valid. */
710 if (code == TYPE_CODE_FLT)
711 {
712 if (INVALID_FLOAT (valaddr, len))
713 {
714 *invp = 1;
715 return 1.234567891011121314;
716 }
717
718 if (len == sizeof (float))
719 {
720 float retval;
721 bcopy (valaddr, &retval, sizeof (retval));
722 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
723 return retval;
724 }
725
726 if (len == sizeof (double))
727 {
728 double retval;
729 bcopy (valaddr, &retval, sizeof (retval));
730 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
731 return retval;
732 }
733 else
734 {
735 error ("Unexpected type of floating point number.");
736 }
737 }
738 else if (nosign) {
739 /* Unsigned -- be sure we compensate for signed LONGEST. */
740 #ifdef LONG_LONG
741 return (unsigned long long) unpack_long (type, valaddr);
742 #else
743 return (unsigned long ) unpack_long (type, valaddr);
744 #endif
745 } else {
746 /* Signed -- we are OK with unpack_long. */
747 return unpack_long (type, valaddr);
748 }
749 }
750 \f
751 /* Given a value ARG1 (offset by OFFSET bytes)
752 of a struct or union type ARG_TYPE,
753 extract and return the value of one of its fields.
754 FIELDNO says which field.
755
756 For C++, must also be able to return values from static fields */
757
758 value
759 value_primitive_field (arg1, offset, fieldno, arg_type)
760 register value arg1;
761 int offset;
762 register int fieldno;
763 register struct type *arg_type;
764 {
765 register value v;
766 register struct type *type;
767
768 check_stub_type (arg_type);
769 type = TYPE_FIELD_TYPE (arg_type, fieldno);
770
771 /* Handle packed fields */
772
773 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
774 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
775 {
776 v = value_from_long (type,
777 unpack_field_as_long (arg_type,
778 VALUE_CONTENTS (arg1),
779 fieldno));
780 VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
781 VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
782 }
783 else
784 {
785 v = allocate_value (type);
786 if (VALUE_LAZY (arg1))
787 VALUE_LAZY (v) = 1;
788 else
789 bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
790 VALUE_CONTENTS_RAW (v),
791 TYPE_LENGTH (type));
792 }
793 VALUE_LVAL (v) = VALUE_LVAL (arg1);
794 if (VALUE_LVAL (arg1) == lval_internalvar)
795 VALUE_LVAL (v) = lval_internalvar_component;
796 VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
797 VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
798 return v;
799 }
800
801 /* Given a value ARG1 of a struct or union type,
802 extract and return the value of one of its fields.
803 FIELDNO says which field.
804
805 For C++, must also be able to return values from static fields */
806
807 value
808 value_field (arg1, fieldno)
809 register value arg1;
810 register int fieldno;
811 {
812 return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
813 }
814
815 value
816 value_fn_field (arg1, fieldno, subfieldno)
817 register value arg1;
818 register int fieldno;
819 int subfieldno;
820 {
821 register value v;
822 struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
823 register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
824 struct symbol *sym;
825
826 sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
827 0, VAR_NAMESPACE, 0, NULL);
828 if (! sym) error ("Internal error: could not find physical method named %s",
829 TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
830
831 v = allocate_value (type);
832 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
833 VALUE_TYPE (v) = type;
834 return v;
835 }
836
837 /* Return a virtual function as a value.
838 ARG1 is the object which provides the virtual function
839 table pointer. ARG1 is side-effected in calling this function.
840 F is the list of member functions which contains the desired virtual
841 function.
842 J is an index into F which provides the desired virtual function.
843
844 TYPE is the type in which F is located. */
845 value
846 value_virtual_fn_field (arg1, f, j, type)
847 value arg1;
848 struct fn_field *f;
849 int j;
850 struct type *type;
851 {
852 /* First, get the virtual function table pointer. That comes
853 with a strange type, so cast it to type `pointer to long' (which
854 should serve just fine as a function type). Then, index into
855 the table, and convert final value to appropriate function type. */
856 value entry, vfn, vtbl;
857 value vi = value_from_long (builtin_type_int,
858 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
859 struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
860 struct type *context;
861 if (fcontext == NULL)
862 /* We don't have an fcontext (e.g. the program was compiled with
863 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
864 This won't work right for multiple inheritance, but at least we
865 should do as well as GDB 3.x did. */
866 fcontext = TYPE_VPTR_BASETYPE (type);
867 context = lookup_pointer_type (fcontext);
868 /* Now context is a pointer to the basetype containing the vtbl. */
869 if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
870 arg1 = value_ind (value_cast (context, value_addr (arg1)));
871
872 context = VALUE_TYPE (arg1);
873 /* Now context is the basetype containing the vtbl. */
874
875 /* This type may have been defined before its virtual function table
876 was. If so, fill in the virtual function table entry for the
877 type now. */
878 if (TYPE_VPTR_FIELDNO (context) < 0)
879 TYPE_VPTR_FIELDNO (context)
880 = fill_in_vptr_fieldno (context);
881
882 /* The virtual function table is now an array of structures
883 which have the form { int16 offset, delta; void *pfn; }. */
884 vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
885
886 /* Index into the virtual function table. This is hard-coded because
887 looking up a field is not cheap, and it may be important to save
888 time, e.g. if the user has set a conditional breakpoint calling
889 a virtual function. */
890 entry = value_subscript (vtbl, vi);
891
892 /* Move the `this' pointer according to the virtual function table. */
893 VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
894 if (! VALUE_LAZY (arg1))
895 {
896 VALUE_LAZY (arg1) = 1;
897 value_fetch_lazy (arg1);
898 }
899
900 vfn = value_field (entry, 2);
901 /* Reinstantiate the function pointer with the correct type. */
902 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
903
904 return vfn;
905 }
906
907 /* The value of a static class member does not depend
908 on its instance, only on its type. If FIELDNO >= 0,
909 then fieldno is a valid field number and is used directly.
910 Otherwise, FIELDNAME is the name of the field we are
911 searching for. If it is not a static field name, an
912 error is signaled. TYPE is the type in which we look for the
913 static field member. */
914 value
915 value_static_field (type, fieldname, fieldno)
916 register struct type *type;
917 char *fieldname;
918 register int fieldno;
919 {
920 register value v;
921 struct symbol *sym;
922 char *phys_name;
923
924 if (fieldno < 0)
925 {
926 register struct type *t = type;
927 /* Look for static field. */
928 while (t)
929 {
930 int i;
931 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
932 if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname))
933 {
934 if (TYPE_FIELD_STATIC (t, i))
935 {
936 fieldno = i;
937 goto found;
938 }
939 else
940 error ("field `%s' is not static");
941 }
942 /* FIXME: this does not recursively check multiple baseclasses. */
943 t = TYPE_N_BASECLASSES (t) ? TYPE_BASECLASS (t, 0) : 0;
944 }
945
946 t = type;
947
948 if (destructor_name_p (fieldname, t))
949 error ("Cannot get value of destructor");
950
951 while (t)
952 {
953 int i;
954
955 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
956 {
957 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname))
958 {
959 error ("Cannot get value of method \"%s\"", fieldname);
960 }
961 }
962 t = TYPE_N_BASECLASSES (t) ? TYPE_BASECLASS (t, 0) : 0;
963 }
964 error("there is no field named %s", fieldname);
965 }
966
967 found:
968 phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
969 sym = lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
970 if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name);
971
972 type = TYPE_FIELD_TYPE (type, fieldno);
973 v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
974 return v;
975 }
976
977 /* Compute the address of the baseclass which is
978 the INDEXth baseclass of TYPE. The TYPE base
979 of the object is at VALADDR. */
980
981 char *
982 baseclass_addr (type, index, valaddr, valuep)
983 struct type *type;
984 int index;
985 char *valaddr;
986 value *valuep;
987 {
988 struct type *basetype = TYPE_BASECLASS (type, index);
989
990 if (BASETYPE_VIA_VIRTUAL (type, index))
991 {
992 /* Must hunt for the pointer to this virtual baseclass. */
993 register int i, len = TYPE_NFIELDS (type);
994 register int n_baseclasses = TYPE_N_BASECLASSES (type);
995 char *vbase_name, *type_name = type_name_no_tag (basetype);
996
997 if (TYPE_MAIN_VARIANT (basetype))
998 basetype = TYPE_MAIN_VARIANT (basetype);
999
1000 vbase_name = (char *)alloca (strlen (type_name) + 8);
1001 sprintf (vbase_name, "_vb$%s", type_name);
1002 /* First look for the virtual baseclass pointer
1003 in the fields. */
1004 for (i = n_baseclasses; i < len; i++)
1005 {
1006 if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
1007 {
1008 value v = value_at (basetype,
1009 unpack_long (TYPE_FIELD_TYPE (type, i),
1010 valaddr + (TYPE_FIELD_BITPOS (type, i) / 8)));
1011 if (valuep)
1012 *valuep = v;
1013 return (char *) VALUE_CONTENTS (v);
1014 }
1015 }
1016 /* Not in the fields, so try looking through the baseclasses. */
1017 for (i = index+1; i < n_baseclasses; i++)
1018 {
1019 char *baddr;
1020
1021 baddr = baseclass_addr (type, i, valaddr, valuep);
1022 if (baddr)
1023 return baddr;
1024 }
1025 /* Not found. */
1026 if (valuep)
1027 *valuep = 0;
1028 return 0;
1029 }
1030
1031 /* Baseclass is easily computed. */
1032 if (valuep)
1033 *valuep = 0;
1034 return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
1035 }
1036
1037 /* Ugly hack to convert method stubs into method types.
1038
1039 He ain't kiddin'. This demangles the name of the method into a string
1040 including argument types, parses out each argument type, generates
1041 a string casting a zero to that type, evaluates the string, and stuffs
1042 the resulting type into an argtype vector!!! Then it knows the type
1043 of the whole function (including argument types for overloading),
1044 which info used to be in the stab's but was removed to hack back
1045 the space required for them. */
1046 void
1047 check_stub_method (type, i, j)
1048 struct type *type;
1049 int i, j;
1050 {
1051 extern char *gdb_mangle_typename (), *strchr ();
1052 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1053 char *inner_name = gdb_mangle_typename (type);
1054 char *mangled_name
1055 = (char *)xmalloc (strlen (TYPE_FN_FIELDLIST_NAME (type, i))
1056 + strlen (inner_name)
1057 + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
1058 + 1);
1059 char *demangled_name, *cplus_demangle ();
1060 char *argtypetext, *p;
1061 int depth = 0, argcount = 1;
1062 struct type **argtypes;
1063
1064 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
1065 strcat (mangled_name, inner_name);
1066 strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
1067 demangled_name = cplus_demangle (mangled_name, 0);
1068
1069 /* Now, read in the parameters that define this type. */
1070 argtypetext = strchr (demangled_name, '(') + 1;
1071 p = argtypetext;
1072 while (*p)
1073 {
1074 if (*p == '(')
1075 depth += 1;
1076 else if (*p == ')')
1077 depth -= 1;
1078 else if (*p == ',' && depth == 0)
1079 argcount += 1;
1080
1081 p += 1;
1082 }
1083 /* We need one more slot for the void [...] or NULL [end of arglist] */
1084 argtypes = (struct type **)xmalloc ((argcount+1) * sizeof (struct type *));
1085 p = argtypetext;
1086 argtypes[0] = lookup_pointer_type (type);
1087 argcount = 1;
1088
1089 if (*p != ')') /* () means no args, skip while */
1090 {
1091 while (*p)
1092 {
1093 if (*p == '(')
1094 depth += 1;
1095 else if (*p == ')')
1096 depth -= 1;
1097
1098 if (depth <= 0 && (*p == ',' || *p == ')'))
1099 {
1100 char *tmp = (char *)alloca (p - argtypetext + 4);
1101 value val;
1102 tmp[0] = '(';
1103 bcopy (argtypetext, tmp+1, p - argtypetext);
1104 tmp[p-argtypetext+1] = ')';
1105 tmp[p-argtypetext+2] = '0';
1106 tmp[p-argtypetext+3] = '\0';
1107 val = parse_and_eval (tmp);
1108 argtypes[argcount] = VALUE_TYPE (val);
1109 argcount += 1;
1110 argtypetext = p + 1;
1111 }
1112 p += 1;
1113 }
1114 }
1115
1116 if (p[-2] != '.') /* ... */
1117 argtypes[argcount] = builtin_type_void; /* Ellist terminator */
1118 else
1119 argtypes[argcount] = NULL; /* List terminator */
1120
1121 free (demangled_name);
1122 smash_to_method_type (TYPE_FN_FIELD_TYPE (f, j), type,
1123 TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
1124 argtypes);
1125 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
1126 TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) &= ~TYPE_FLAG_STUB;
1127 }
1128 \f
1129 long
1130 unpack_field_as_long (type, valaddr, fieldno)
1131 struct type *type;
1132 char *valaddr;
1133 int fieldno;
1134 {
1135 long val;
1136 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1137 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1138
1139 bcopy (valaddr + bitpos / 8, &val, sizeof val);
1140 SWAP_TARGET_AND_HOST (&val, sizeof val);
1141
1142 /* Extracting bits depends on endianness of the machine. */
1143 #ifdef BITS_BIG_ENDIAN
1144 val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
1145 #else
1146 val = val >> (bitpos % 8);
1147 #endif
1148
1149 val &= (1 << bitsize) - 1;
1150 return val;
1151 }
1152
1153 /* Modify the value of a bitfield. ADDR points to a block of memory in
1154 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1155 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1156 indicate which bits (in target bit order) comprise the bitfield. */
1157
1158 void
1159 modify_field (addr, fieldval, bitpos, bitsize)
1160 char *addr;
1161 int fieldval;
1162 int bitpos, bitsize;
1163 {
1164 long oword;
1165
1166 /* Reject values too big to fit in the field in question.
1167 Otherwise adjoining fields may be corrupted. */
1168 if (fieldval & ~((1<<bitsize)-1))
1169 error ("Value %d does not fit in %d bits.", fieldval, bitsize);
1170
1171 bcopy (addr, &oword, sizeof oword);
1172 SWAP_TARGET_AND_HOST (&oword, sizeof oword); /* To host format */
1173
1174 /* Shifting for bit field depends on endianness of the target machine. */
1175 #ifdef BITS_BIG_ENDIAN
1176 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1177 #endif
1178
1179 oword &= ~(((1 << bitsize) - 1) << bitpos);
1180 oword |= fieldval << bitpos;
1181
1182 SWAP_TARGET_AND_HOST (&oword, sizeof oword); /* To target format */
1183 bcopy (&oword, addr, sizeof oword);
1184 }
1185 \f
1186 /* Convert C numbers into newly allocated values */
1187
1188 value
1189 value_from_long (type, num)
1190 struct type *type;
1191 register LONGEST num;
1192 {
1193 register value val = allocate_value (type);
1194 register enum type_code code = TYPE_CODE (type);
1195 register int len = TYPE_LENGTH (type);
1196
1197 if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
1198 {
1199 if (len == sizeof (char))
1200 * (char *) VALUE_CONTENTS_RAW (val) = num;
1201 else if (len == sizeof (short))
1202 * (short *) VALUE_CONTENTS_RAW (val) = num;
1203 else if (len == sizeof (int))
1204 * (int *) VALUE_CONTENTS_RAW (val) = num;
1205 else if (len == sizeof (long))
1206 * (long *) VALUE_CONTENTS_RAW (val) = num;
1207 #ifdef LONG_LONG
1208 else if (len == sizeof (long long))
1209 * (long long *) VALUE_CONTENTS_RAW (val) = num;
1210 #endif
1211 else
1212 error ("Integer type encountered with unexpected data length.");
1213 }
1214 else
1215 error ("Unexpected type encountered for integer constant.");
1216
1217 /* num was in host byte order. So now put the value's contents
1218 into target byte order. */
1219 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1220
1221 return val;
1222 }
1223
1224 value
1225 value_from_double (type, num)
1226 struct type *type;
1227 double num;
1228 {
1229 register value val = allocate_value (type);
1230 register enum type_code code = TYPE_CODE (type);
1231 register int len = TYPE_LENGTH (type);
1232
1233 if (code == TYPE_CODE_FLT)
1234 {
1235 if (len == sizeof (float))
1236 * (float *) VALUE_CONTENTS_RAW (val) = num;
1237 else if (len == sizeof (double))
1238 * (double *) VALUE_CONTENTS_RAW (val) = num;
1239 else
1240 error ("Floating type encountered with unexpected data length.");
1241 }
1242 else
1243 error ("Unexpected type encountered for floating constant.");
1244
1245 /* num was in host byte order. So now put the value's contents
1246 into target byte order. */
1247 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1248
1249 return val;
1250 }
1251 \f
1252 /* Deal with the value that is "about to be returned". */
1253
1254 /* Return the value that a function returning now
1255 would be returning to its caller, assuming its type is VALTYPE.
1256 RETBUF is where we look for what ought to be the contents
1257 of the registers (in raw form). This is because it is often
1258 desirable to restore old values to those registers
1259 after saving the contents of interest, and then call
1260 this function using the saved values.
1261 struct_return is non-zero when the function in question is
1262 using the structure return conventions on the machine in question;
1263 0 when it is using the value returning conventions (this often
1264 means returning pointer to where structure is vs. returning value). */
1265
1266 value
1267 value_being_returned (valtype, retbuf, struct_return)
1268 register struct type *valtype;
1269 char retbuf[REGISTER_BYTES];
1270 int struct_return;
1271 /*ARGSUSED*/
1272 {
1273 register value val;
1274 CORE_ADDR addr;
1275
1276 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1277 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1278 if (struct_return) {
1279 addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
1280 if (!addr)
1281 error ("Function return value unknown");
1282 return value_at (valtype, addr);
1283 }
1284 #endif
1285
1286 val = allocate_value (valtype);
1287 EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1288
1289 return val;
1290 }
1291
1292 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1293 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1294 and TYPE is the type (which is known to be struct, union or array).
1295
1296 On most machines, the struct convention is used unless we are
1297 using gcc and the type is of a special size. */
1298 #if !defined (USE_STRUCT_CONVENTION)
1299 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1300 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1301 || TYPE_LENGTH (value_type) == 2 \
1302 || TYPE_LENGTH (value_type) == 4 \
1303 || TYPE_LENGTH (value_type) == 8 \
1304 ) \
1305 ))
1306 #endif
1307
1308 /* Return true if the function specified is using the structure returning
1309 convention on this machine to return arguments, or 0 if it is using
1310 the value returning convention. FUNCTION is the value representing
1311 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1312 is the type returned by the function. GCC_P is nonzero if compiled
1313 with GCC. */
1314
1315 int
1316 using_struct_return (function, funcaddr, value_type, gcc_p)
1317 value function;
1318 CORE_ADDR funcaddr;
1319 struct type *value_type;
1320 int gcc_p;
1321 /*ARGSUSED*/
1322 {
1323 register enum type_code code = TYPE_CODE (value_type);
1324
1325 if (code == TYPE_CODE_ERROR)
1326 error ("Function return type unknown.");
1327
1328 if (code == TYPE_CODE_STRUCT ||
1329 code == TYPE_CODE_UNION ||
1330 code == TYPE_CODE_ARRAY)
1331 return USE_STRUCT_CONVENTION (gcc_p, value_type);
1332
1333 return 0;
1334 }
1335
1336 /* Store VAL so it will be returned if a function returns now.
1337 Does not verify that VAL's type matches what the current
1338 function wants to return. */
1339
1340 void
1341 set_return_value (val)
1342 value val;
1343 {
1344 register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
1345 double dbuf;
1346 LONGEST lbuf;
1347
1348 if (code == TYPE_CODE_ERROR)
1349 error ("Function return type unknown.");
1350
1351 if (code == TYPE_CODE_STRUCT
1352 || code == TYPE_CODE_UNION)
1353 error ("Specifying a struct or union return value is not supported.");
1354
1355 /* FIXME, this is bogus. We don't know what the return conventions
1356 are, or how values should be promoted.... */
1357 if (code == TYPE_CODE_FLT)
1358 {
1359 dbuf = value_as_double (val);
1360
1361 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
1362 }
1363 else
1364 {
1365 lbuf = value_as_long (val);
1366 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
1367 }
1368 }
1369 \f
1370 void
1371 _initialize_values ()
1372 {
1373 add_cmd ("convenience", no_class, show_convenience,
1374 "Debugger convenience (\"$foo\") variables.\n\
1375 These variables are created when you assign them values;\n\
1376 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1377 A few convenience variables are given values automatically:\n\
1378 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1379 \"$__\" holds the contents of the last address examined with \"x\".",
1380 &showlist);
1381
1382 add_cmd ("values", no_class, show_values,
1383 "Elements of value history around item number IDX (or last ten).",
1384 &showlist);
1385 }