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