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