Commit | Line | Data |
---|---|---|
cc61838f WJ |
1 | /*- |
2 | * This code is derived from software copyrighted by the Free Software | |
3 | * Foundation. | |
4 | * | |
5 | * Modified 1991 by Donn Seeley at UUNET Technologies, Inc. | |
6 | * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory. | |
7 | */ | |
8 | ||
9 | #ifndef lint | |
10 | static char sccsid[] = "@(#)values.c 6.3 (Berkeley) 5/8/91"; | |
11 | #endif /* not lint */ | |
12 | ||
13 | /* Low level packing and unpacking of values for GDB. | |
14 | Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc. | |
15 | ||
16 | This file is part of GDB. | |
17 | ||
18 | GDB is free software; you can redistribute it and/or modify | |
19 | it under the terms of the GNU General Public License as published by | |
20 | the Free Software Foundation; either version 1, or (at your option) | |
21 | any later version. | |
22 | ||
23 | GDB is distributed in the hope that it will be useful, | |
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | GNU General Public License for more details. | |
27 | ||
28 | You should have received a copy of the GNU General Public License | |
29 | along with GDB; see the file COPYING. If not, write to | |
30 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
31 | ||
32 | #include <stdio.h> | |
33 | #include "defs.h" | |
34 | #include "param.h" | |
35 | #include "symtab.h" | |
36 | #include "value.h" | |
37 | ||
38 | /* The value-history records all the values printed | |
39 | by print commands during this session. Each chunk | |
40 | records 60 consecutive values. The first chunk on | |
41 | the chain records the most recent values. | |
42 | The total number of values is in value_history_count. */ | |
43 | ||
44 | #define VALUE_HISTORY_CHUNK 60 | |
45 | ||
46 | struct value_history_chunk | |
47 | { | |
48 | struct value_history_chunk *next; | |
49 | value values[VALUE_HISTORY_CHUNK]; | |
50 | }; | |
51 | ||
52 | /* Chain of chunks now in use. */ | |
53 | ||
54 | static struct value_history_chunk *value_history_chain; | |
55 | ||
56 | static int value_history_count; /* Abs number of last entry stored */ | |
57 | ||
58 | \f | |
59 | /* List of all value objects currently allocated | |
60 | (except for those released by calls to release_value) | |
61 | This is so they can be freed after each command. */ | |
62 | ||
63 | static value all_values; | |
64 | ||
65 | /* Allocate a value that has the correct length for type TYPE. */ | |
66 | ||
67 | value | |
68 | allocate_value (type) | |
69 | struct type *type; | |
70 | { | |
71 | register value val; | |
72 | ||
73 | /* If the type we want had no definition in the file it first | |
74 | * appeared in, it will be marked a `stub'. The real definition | |
75 | * probably appeared later so try to find it. */ | |
76 | if (TYPE_FLAGS(type) & TYPE_FLAG_STUB) | |
77 | { | |
78 | register char *cp; | |
79 | register struct symbol *sym; | |
80 | extern char *index(); | |
81 | ||
82 | if (cp = index(TYPE_NAME(type), ' ')) | |
83 | ++cp; | |
84 | else | |
85 | cp = TYPE_NAME(type); | |
86 | ||
87 | sym = lookup_symbol(cp, 0, STRUCT_NAMESPACE, 0); | |
88 | ||
89 | if (sym && TYPE_CODE(SYMBOL_TYPE(sym)) == TYPE_CODE(type)) | |
90 | bcopy (SYMBOL_TYPE (sym), type, sizeof (*type)); | |
91 | } | |
92 | val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type)); | |
93 | VALUE_NEXT (val) = all_values; | |
94 | all_values = val; | |
95 | VALUE_TYPE (val) = type; | |
96 | VALUE_LVAL (val) = not_lval; | |
97 | VALUE_ADDRESS (val) = 0; | |
98 | VALUE_FRAME (val) = 0; | |
99 | VALUE_OFFSET (val) = 0; | |
100 | VALUE_BITPOS (val) = 0; | |
101 | VALUE_BITSIZE (val) = 0; | |
102 | VALUE_REPEATED (val) = 0; | |
103 | VALUE_REPETITIONS (val) = 0; | |
104 | VALUE_REGNO (val) = -1; | |
105 | return val; | |
106 | } | |
107 | ||
108 | /* Allocate a value that has the correct length | |
109 | for COUNT repetitions type TYPE. */ | |
110 | ||
111 | value | |
112 | allocate_repeat_value (type, count) | |
113 | struct type *type; | |
114 | int count; | |
115 | { | |
116 | register value val; | |
117 | ||
118 | val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count); | |
119 | VALUE_NEXT (val) = all_values; | |
120 | all_values = val; | |
121 | VALUE_TYPE (val) = type; | |
122 | VALUE_LVAL (val) = not_lval; | |
123 | VALUE_ADDRESS (val) = 0; | |
124 | VALUE_FRAME (val) = 0; | |
125 | VALUE_OFFSET (val) = 0; | |
126 | VALUE_BITPOS (val) = 0; | |
127 | VALUE_BITSIZE (val) = 0; | |
128 | VALUE_REPEATED (val) = 1; | |
129 | VALUE_REPETITIONS (val) = count; | |
130 | VALUE_REGNO (val) = -1; | |
131 | return val; | |
132 | } | |
133 | ||
134 | /* Free all the values that have been allocated (except for those released). | |
135 | Called after each command, successful or not. */ | |
136 | ||
137 | void | |
138 | free_all_values () | |
139 | { | |
140 | register value val, next; | |
141 | ||
142 | for (val = all_values; val; val = next) | |
143 | { | |
144 | next = VALUE_NEXT (val); | |
145 | free (val); | |
146 | } | |
147 | ||
148 | all_values = 0; | |
149 | } | |
150 | ||
151 | /* Remove VAL from the chain all_values | |
152 | so it will not be freed automatically. */ | |
153 | ||
154 | void | |
155 | release_value (val) | |
156 | register value val; | |
157 | { | |
158 | register value v; | |
159 | ||
160 | if (all_values == val) | |
161 | { | |
162 | all_values = val->next; | |
163 | return; | |
164 | } | |
165 | ||
166 | for (v = all_values; v; v = v->next) | |
167 | { | |
168 | if (v->next == val) | |
169 | { | |
170 | v->next = val->next; | |
171 | break; | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | /* Return a copy of the value ARG. | |
177 | It contains the same contents, for same memory address, | |
178 | but it's a different block of storage. */ | |
179 | ||
180 | static value | |
181 | value_copy (arg) | |
182 | value arg; | |
183 | { | |
184 | register value val; | |
185 | register struct type *type = VALUE_TYPE (arg); | |
186 | if (VALUE_REPEATED (arg)) | |
187 | val = allocate_repeat_value (type, VALUE_REPETITIONS (arg)); | |
188 | else | |
189 | val = allocate_value (type); | |
190 | VALUE_LVAL (val) = VALUE_LVAL (arg); | |
191 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg); | |
192 | VALUE_OFFSET (val) = VALUE_OFFSET (arg); | |
193 | VALUE_BITPOS (val) = VALUE_BITPOS (arg); | |
194 | VALUE_BITSIZE (val) = VALUE_BITSIZE (arg); | |
195 | VALUE_REGNO (val) = VALUE_REGNO (arg); | |
196 | bcopy (VALUE_CONTENTS (arg), VALUE_CONTENTS (val), | |
197 | TYPE_LENGTH (VALUE_TYPE (arg)) | |
198 | * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1)); | |
199 | return val; | |
200 | } | |
201 | \f | |
202 | /* Access to the value history. */ | |
203 | ||
204 | /* Record a new value in the value history. | |
205 | Returns the absolute history index of the entry. */ | |
206 | ||
207 | int | |
208 | record_latest_value (val) | |
209 | value val; | |
210 | { | |
211 | int i; | |
212 | double foo; | |
213 | ||
214 | /* Check error now if about to store an invalid float. We return -1 | |
215 | to the caller, but allow them to continue, e.g. to print it as "Nan". */ | |
216 | if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) { | |
217 | foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i); | |
218 | if (i) return -1; /* Indicate value not saved in history */ | |
219 | } | |
220 | ||
221 | /* Here we treat value_history_count as origin-zero | |
222 | and applying to the value being stored now. */ | |
223 | ||
224 | i = value_history_count % VALUE_HISTORY_CHUNK; | |
225 | if (i == 0) | |
226 | { | |
227 | register struct value_history_chunk *new | |
228 | = (struct value_history_chunk *) | |
229 | xmalloc (sizeof (struct value_history_chunk)); | |
230 | bzero (new->values, sizeof new->values); | |
231 | new->next = value_history_chain; | |
232 | value_history_chain = new; | |
233 | } | |
234 | ||
235 | value_history_chain->values[i] = val; | |
236 | release_value (val); | |
237 | ||
238 | /* Now we regard value_history_count as origin-one | |
239 | and applying to the value just stored. */ | |
240 | ||
241 | return ++value_history_count; | |
242 | } | |
243 | ||
244 | /* Return a copy of the value in the history with sequence number NUM. */ | |
245 | ||
246 | value | |
247 | access_value_history (num) | |
248 | int num; | |
249 | { | |
250 | register struct value_history_chunk *chunk; | |
251 | register int i; | |
252 | register int absnum = num; | |
253 | ||
254 | if (absnum <= 0) | |
255 | absnum += value_history_count; | |
256 | ||
257 | if (absnum <= 0) | |
258 | { | |
259 | if (num == 0) | |
260 | error ("The history is empty."); | |
261 | else if (num == 1) | |
262 | error ("There is only one value in the history."); | |
263 | else | |
264 | error ("History does not go back to $$%d.", -num); | |
265 | } | |
266 | if (absnum > value_history_count) | |
267 | error ("History has not yet reached $%d.", absnum); | |
268 | ||
269 | absnum--; | |
270 | ||
271 | /* Now absnum is always absolute and origin zero. */ | |
272 | ||
273 | chunk = value_history_chain; | |
274 | for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK; | |
275 | i > 0; i--) | |
276 | chunk = chunk->next; | |
277 | ||
278 | return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]); | |
279 | } | |
280 | ||
281 | /* Clear the value history entirely. | |
282 | Must be done when new symbol tables are loaded, | |
283 | because the type pointers become invalid. */ | |
284 | ||
285 | void | |
286 | clear_value_history () | |
287 | { | |
288 | register struct value_history_chunk *next; | |
289 | register int i; | |
290 | register value val; | |
291 | ||
292 | while (value_history_chain) | |
293 | { | |
294 | for (i = 0; i < VALUE_HISTORY_CHUNK; i++) | |
295 | if (val = value_history_chain->values[i]) | |
296 | free (val); | |
297 | next = value_history_chain->next; | |
298 | free (value_history_chain); | |
299 | value_history_chain = next; | |
300 | } | |
301 | value_history_count = 0; | |
302 | } | |
303 | ||
304 | static void | |
305 | value_history_info (num_exp, from_tty) | |
306 | char *num_exp; | |
307 | int from_tty; | |
308 | { | |
309 | register int i; | |
310 | register value val; | |
311 | static int num = 1; | |
312 | ||
313 | if (num_exp) | |
314 | { | |
315 | if (num_exp[0] == '+' && num_exp[1] == '\0') | |
316 | /* "info history +" should print from the stored position. */ | |
317 | ; | |
318 | else | |
319 | /* "info history <exp>" should print around value number <exp>. */ | |
320 | num = parse_and_eval_address (num_exp) - 5; | |
321 | } | |
322 | else | |
323 | { | |
324 | /* "info history" means print the last 10 values. */ | |
325 | num = value_history_count - 9; | |
326 | } | |
327 | ||
328 | if (num <= 0) | |
329 | num = 1; | |
330 | ||
331 | for (i = num; i < num + 10 && i <= value_history_count; i++) | |
332 | { | |
333 | val = access_value_history (i); | |
334 | printf_filtered ("$%d = ", i); | |
335 | value_print (val, stdout, 0, Val_pretty_default); | |
336 | printf_filtered ("\n"); | |
337 | } | |
338 | ||
339 | /* The next "info history +" should start after what we just printed. */ | |
340 | num += 10; | |
341 | ||
342 | /* Hitting just return after this command should do the same thing as | |
343 | "info history +". If num_exp is null, this is unnecessary, since | |
344 | "info history +" is not useful after "info history". */ | |
345 | if (from_tty && num_exp) | |
346 | { | |
347 | num_exp[0] = '+'; | |
348 | num_exp[1] = '\0'; | |
349 | } | |
350 | } | |
351 | \f | |
352 | /* Internal variables. These are variables within the debugger | |
353 | that hold values assigned by debugger commands. | |
354 | The user refers to them with a '$' prefix | |
355 | that does not appear in the variable names stored internally. */ | |
356 | ||
357 | static struct internalvar *internalvars; | |
358 | ||
359 | /* Look up an internal variable with name NAME. NAME should not | |
360 | normally include a dollar sign. | |
361 | ||
362 | If the specified internal variable does not exist, | |
363 | one is created, with a void value. */ | |
364 | ||
365 | struct internalvar * | |
366 | lookup_internalvar (name) | |
367 | char *name; | |
368 | { | |
369 | register struct internalvar *var; | |
370 | ||
371 | for (var = internalvars; var; var = var->next) | |
372 | if (!strcmp (var->name, name)) | |
373 | return var; | |
374 | ||
375 | var = (struct internalvar *) xmalloc (sizeof (struct internalvar)); | |
376 | var->name = concat (name, "", ""); | |
377 | var->value = allocate_value (builtin_type_void); | |
378 | release_value (var->value); | |
379 | var->next = internalvars; | |
380 | internalvars = var; | |
381 | return var; | |
382 | } | |
383 | ||
384 | value | |
385 | value_of_internalvar (var) | |
386 | struct internalvar *var; | |
387 | { | |
388 | register value val; | |
389 | ||
390 | #ifdef IS_TRAPPED_INTERNALVAR | |
391 | if (IS_TRAPPED_INTERNALVAR (var->name)) | |
392 | return VALUE_OF_TRAPPED_INTERNALVAR (var); | |
393 | #endif | |
394 | ||
395 | val = value_copy (var->value); | |
396 | VALUE_LVAL (val) = lval_internalvar; | |
397 | VALUE_INTERNALVAR (val) = var; | |
398 | return val; | |
399 | } | |
400 | ||
401 | void | |
402 | set_internalvar_component (var, offset, bitpos, bitsize, newval) | |
403 | struct internalvar *var; | |
404 | int offset, bitpos, bitsize; | |
405 | value newval; | |
406 | { | |
407 | register char *addr = VALUE_CONTENTS (var->value) + offset; | |
408 | ||
409 | #ifdef IS_TRAPPED_INTERNALVAR | |
410 | if (IS_TRAPPED_INTERNALVAR (var->name)) | |
411 | SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset); | |
412 | #endif | |
413 | ||
414 | if (bitsize) | |
415 | modify_field (addr, (int) value_as_long (newval), | |
416 | bitpos, bitsize); | |
417 | else | |
418 | bcopy (VALUE_CONTENTS (newval), addr, | |
419 | TYPE_LENGTH (VALUE_TYPE (newval))); | |
420 | } | |
421 | ||
422 | void | |
423 | set_internalvar (var, val) | |
424 | struct internalvar *var; | |
425 | value val; | |
426 | { | |
427 | #ifdef IS_TRAPPED_INTERNALVAR | |
428 | if (IS_TRAPPED_INTERNALVAR (var->name)) | |
429 | SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0); | |
430 | #endif | |
431 | ||
432 | free (var->value); | |
433 | var->value = value_copy (val); | |
434 | release_value (var->value); | |
435 | } | |
436 | ||
437 | char * | |
438 | internalvar_name (var) | |
439 | struct internalvar *var; | |
440 | { | |
441 | return var->name; | |
442 | } | |
443 | ||
444 | /* Free all internalvars. Done when new symtabs are loaded, | |
445 | because that makes the values invalid. */ | |
446 | ||
447 | void | |
448 | clear_internalvars () | |
449 | { | |
450 | register struct internalvar *var; | |
451 | ||
452 | while (internalvars) | |
453 | { | |
454 | var = internalvars; | |
455 | internalvars = var->next; | |
456 | free (var->name); | |
457 | free (var->value); | |
458 | free (var); | |
459 | } | |
460 | } | |
461 | ||
462 | static void | |
463 | convenience_info () | |
464 | { | |
465 | register struct internalvar *var; | |
466 | int varseen = 0; | |
467 | ||
468 | for (var = internalvars; var; var = var->next) | |
469 | { | |
470 | #ifdef IS_TRAPPED_INTERNALVAR | |
471 | if (IS_TRAPPED_INTERNALVAR (var->name)) | |
472 | continue; | |
473 | #endif | |
474 | if (!varseen) | |
475 | { | |
476 | printf ("Debugger convenience variables:\n\n"); | |
477 | varseen = 1; | |
478 | } | |
479 | printf ("$%s: ", var->name); | |
480 | value_print (var->value, stdout, 0, Val_pretty_default); | |
481 | printf ("\n"); | |
482 | } | |
483 | if (!varseen) | |
484 | printf ("No debugger convenience variables now defined.\n\ | |
485 | Convenience variables have names starting with \"$\";\n\ | |
486 | use \"set\" as in \"set $foo = 5\" to define them.\n"); | |
487 | } | |
488 | \f | |
489 | /* Extract a value as a C number (either long or double). | |
490 | Knows how to convert fixed values to double, or | |
491 | floating values to long. | |
492 | Does not deallocate the value. */ | |
493 | ||
494 | LONGEST | |
495 | value_as_long (val) | |
496 | register value val; | |
497 | { | |
498 | return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val)); | |
499 | } | |
500 | ||
501 | double | |
502 | value_as_double (val) | |
503 | register value val; | |
504 | { | |
505 | double foo; | |
506 | int inv; | |
507 | ||
508 | foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv); | |
509 | if (inv) | |
510 | error ("Invalid floating value found in program."); | |
511 | return foo; | |
512 | } | |
513 | \f | |
514 | /* Unpack raw data (copied from debugee) at VALADDR | |
515 | as a long, or as a double, assuming the raw data is described | |
516 | by type TYPE. Knows how to convert different sizes of values | |
517 | and can convert between fixed and floating point. | |
518 | ||
519 | C++: It is assumed that the front-end has taken care of | |
520 | all matters concerning pointers to members. A pointer | |
521 | to member which reaches here is considered to be equivalent | |
522 | to an INT (or some size). After all, it is only an offset. */ | |
523 | ||
524 | LONGEST | |
525 | unpack_long (type, valaddr) | |
526 | struct type *type; | |
527 | char *valaddr; | |
528 | { | |
529 | register enum type_code code = TYPE_CODE (type); | |
530 | register int len = TYPE_LENGTH (type); | |
531 | register int nosign = TYPE_UNSIGNED (type); | |
532 | ||
533 | if (code == TYPE_CODE_ENUM) | |
534 | code = TYPE_CODE_INT; | |
535 | if (code == TYPE_CODE_FLT) | |
536 | { | |
537 | if (len == sizeof (float)) | |
538 | return * (float *) valaddr; | |
539 | ||
540 | if (len == sizeof (double)) | |
541 | return * (double *) valaddr; | |
542 | } | |
543 | else if (code == TYPE_CODE_INT && nosign) | |
544 | { | |
545 | if (len == sizeof (char)) | |
546 | return * (unsigned char *) valaddr; | |
547 | ||
548 | if (len == sizeof (short)) | |
549 | return * (unsigned short *) valaddr; | |
550 | ||
551 | if (len == sizeof (int)) | |
552 | return * (unsigned int *) valaddr; | |
553 | ||
554 | if (len == sizeof (long)) | |
555 | return * (unsigned long *) valaddr; | |
556 | #ifdef LONG_LONG | |
557 | if (len == sizeof (long long)) | |
558 | return * (unsigned long long *) valaddr; | |
559 | #endif | |
560 | } | |
561 | else if (code == TYPE_CODE_INT) | |
562 | { | |
563 | if (len == sizeof (char)) | |
564 | return * (char *) valaddr; | |
565 | ||
566 | if (len == sizeof (short)) | |
567 | return * (short *) valaddr; | |
568 | ||
569 | if (len == sizeof (int)) | |
570 | return * (int *) valaddr; | |
571 | ||
572 | if (len == sizeof (long)) | |
573 | return * (long *) valaddr; | |
574 | ||
575 | #ifdef LONG_LONG | |
576 | if (len == sizeof (long long)) | |
577 | return * (long long *) valaddr; | |
578 | #endif | |
579 | } | |
580 | else if (code == TYPE_CODE_PTR | |
581 | || code == TYPE_CODE_REF) | |
582 | { | |
583 | if (len == sizeof (char *)) | |
584 | return (CORE_ADDR) * (char **) valaddr; | |
585 | } | |
586 | else if (code == TYPE_CODE_MEMBER) | |
587 | error ("not implemented: member types in unpack_long"); | |
588 | ||
589 | error ("Value not integer or pointer."); | |
590 | } | |
591 | ||
592 | /* Return a double value from the specified type and address. | |
593 | INVP points to an int which is set to 0 for valid value, | |
594 | 1 for invalid value (bad float format). In either case, | |
595 | the returned double is OK to use. */ | |
596 | ||
597 | double | |
598 | unpack_double (type, valaddr, invp) | |
599 | struct type *type; | |
600 | char *valaddr; | |
601 | int *invp; | |
602 | { | |
603 | register enum type_code code = TYPE_CODE (type); | |
604 | register int len = TYPE_LENGTH (type); | |
605 | register int nosign = TYPE_UNSIGNED (type); | |
606 | ||
607 | *invp = 0; /* Assume valid. */ | |
608 | if (code == TYPE_CODE_FLT) | |
609 | { | |
610 | if (INVALID_FLOAT (valaddr, len)) | |
611 | { | |
612 | *invp = 1; | |
613 | return 1.234567891011121314; | |
614 | } | |
615 | ||
616 | if (len == sizeof (float)) | |
617 | return * (float *) valaddr; | |
618 | ||
619 | if (len == sizeof (double)) | |
620 | { | |
621 | /* Some machines require doubleword alignment for doubles. | |
622 | This code works on them, and on other machines. */ | |
623 | double temp; | |
624 | bcopy ((char *) valaddr, (char *) &temp, sizeof (double)); | |
625 | return temp; | |
626 | } | |
627 | } | |
628 | else if (code == TYPE_CODE_INT && nosign) | |
629 | { | |
630 | if (len == sizeof (char)) | |
631 | return * (unsigned char *) valaddr; | |
632 | ||
633 | if (len == sizeof (short)) | |
634 | return * (unsigned short *) valaddr; | |
635 | ||
636 | if (len == sizeof (int)) | |
637 | return * (unsigned int *) valaddr; | |
638 | ||
639 | if (len == sizeof (long)) | |
640 | return * (unsigned long *) valaddr; | |
641 | ||
642 | #ifdef LONG_LONG | |
643 | if (len == sizeof (long long)) | |
644 | return * (unsigned long long *) valaddr; | |
645 | #endif | |
646 | } | |
647 | else if (code == TYPE_CODE_INT) | |
648 | { | |
649 | if (len == sizeof (char)) | |
650 | return * (char *) valaddr; | |
651 | ||
652 | if (len == sizeof (short)) | |
653 | return * (short *) valaddr; | |
654 | ||
655 | if (len == sizeof (int)) | |
656 | return * (int *) valaddr; | |
657 | ||
658 | if (len == sizeof (long)) | |
659 | return * (long *) valaddr; | |
660 | ||
661 | #ifdef LONG_LONG | |
662 | if (len == sizeof (long long)) | |
663 | return * (long long *) valaddr; | |
664 | #endif | |
665 | } | |
666 | ||
667 | error ("Value not floating number."); | |
668 | /* NOTREACHED */ | |
669 | return (double) 0; /* To silence compiler warning. */ | |
670 | } | |
671 | \f | |
672 | /* Given a value ARG1 of a struct or union type, | |
673 | extract and return the value of one of its fields. | |
674 | FIELDNO says which field. | |
675 | ||
676 | For C++, must also be able to return values from static fields */ | |
677 | ||
678 | value | |
679 | value_field (arg1, fieldno) | |
680 | register value arg1; | |
681 | register int fieldno; | |
682 | { | |
683 | register value v; | |
684 | register struct type *type = TYPE_FIELD_TYPE (VALUE_TYPE (arg1), fieldno); | |
685 | register int offset; | |
686 | ||
687 | /* Handle packed fields */ | |
688 | ||
689 | offset = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) / 8; | |
690 | if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno)) | |
691 | { | |
692 | v = value_from_long (type, | |
693 | unpack_field_as_long (VALUE_TYPE (arg1), | |
694 | VALUE_CONTENTS (arg1), | |
695 | fieldno)); | |
696 | VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) % 8; | |
697 | VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno); | |
698 | } | |
699 | else | |
700 | { | |
701 | v = allocate_value (type); | |
702 | bcopy (VALUE_CONTENTS (arg1) + offset, | |
703 | VALUE_CONTENTS (v), | |
704 | TYPE_LENGTH (type)); | |
705 | } | |
706 | VALUE_LVAL (v) = VALUE_LVAL (arg1); | |
707 | if (VALUE_LVAL (arg1) == lval_internalvar) | |
708 | VALUE_LVAL (v) = lval_internalvar_component; | |
709 | VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1); | |
710 | VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1); | |
711 | return v; | |
712 | } | |
713 | ||
714 | value | |
715 | value_fn_field (arg1, fieldno, subfieldno) | |
716 | register value arg1; | |
717 | register int fieldno; | |
718 | { | |
719 | register value v; | |
720 | struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno); | |
721 | register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno); | |
722 | struct symbol *sym; | |
723 | ||
724 | sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno), | |
725 | 0, VAR_NAMESPACE, 0); | |
726 | if (! sym) error ("Internal error: could not find physical method named %s", | |
727 | TYPE_FN_FIELD_PHYSNAME (f, subfieldno)); | |
728 | ||
729 | v = allocate_value (type); | |
730 | VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); | |
731 | VALUE_TYPE (v) = type; | |
732 | return v; | |
733 | } | |
734 | ||
735 | /* Return a virtual function as a value. | |
736 | ARG1 is the object which provides the virtual function | |
737 | table pointer. | |
738 | F is the list of member functions which contains the desired virtual | |
739 | function. | |
740 | J is an index into F which provides the desired virtual function. | |
741 | TYPE is the basetype which first provides the virtual function table. */ | |
742 | value | |
743 | value_virtual_fn_field (arg1, f, j, type) | |
744 | value arg1; | |
745 | struct fn_field *f; | |
746 | int j; | |
747 | struct type *type; | |
748 | { | |
749 | /* First, get the virtual function table pointer. That comes | |
750 | with a strange type, so cast it to type `pointer to long' (which | |
751 | should serve just fine as a function type). Then, index into | |
752 | the table, and convert final value to appropriate function type. */ | |
753 | value vfn, vtbl; | |
754 | value vi = value_from_long (builtin_type_int, | |
755 | (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j)); | |
756 | VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (type); | |
757 | ||
758 | /* This type may have been defined before its virtual function table | |
759 | was. If so, fill in the virtual function table entry for the | |
760 | type now. */ | |
761 | if (TYPE_VPTR_FIELDNO (type) < 0) | |
762 | TYPE_VPTR_FIELDNO (type) | |
763 | = fill_in_vptr_fieldno (type); | |
764 | ||
765 | /* The virtual function table is now an array of structures | |
766 | which have the form { int16 offset, delta; void *pfn; }. */ | |
767 | vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (type))); | |
768 | ||
769 | /* Index into the virtual function table. This is hard-coded because | |
770 | looking up a field is not cheap, and it may be important to save | |
771 | time, e.g. if the user has set a conditional breakpoint calling | |
772 | a virtual function. */ | |
773 | vfn = value_field (value_subscript (vtbl, vi), 2); | |
774 | ||
775 | /* Reinstantiate the function pointer with the correct type. */ | |
776 | VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)); | |
777 | return vfn; | |
778 | } | |
779 | ||
780 | /* The value of a static class member does not depend | |
781 | on its instance, only on its type. If FIELDNO >= 0, | |
782 | then fieldno is a valid field number and is used directly. | |
783 | Otherwise, FIELDNAME is the name of the field we are | |
784 | searching for. If it is not a static field name, an | |
785 | error is signaled. TYPE is the type in which we look for the | |
786 | static field member. */ | |
787 | value | |
788 | value_static_field (type, fieldname, fieldno) | |
789 | register struct type *type; | |
790 | char *fieldname; | |
791 | register int fieldno; | |
792 | { | |
793 | register value v; | |
794 | struct symbol *sym; | |
795 | ||
796 | if (fieldno < 0) | |
797 | { | |
798 | register struct type *t = type; | |
799 | /* Look for static field. */ | |
800 | while (t) | |
801 | { | |
802 | int i; | |
803 | for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--) | |
804 | if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname)) | |
805 | { | |
806 | if (TYPE_FIELD_STATIC (t, i)) | |
807 | { | |
808 | fieldno = i; | |
809 | goto found; | |
810 | } | |
811 | else | |
812 | error ("field `%s' is not static"); | |
813 | } | |
814 | t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0; | |
815 | } | |
816 | ||
817 | t = type; | |
818 | ||
819 | if (destructor_name_p (fieldname, t)) | |
820 | error ("use `info method' command to print out value of destructor"); | |
821 | ||
822 | while (t) | |
823 | { | |
824 | int i, j; | |
825 | ||
826 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--) | |
827 | { | |
828 | if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname)) | |
829 | { | |
830 | error ("use `info method' command to print value of method \"%s\"", fieldname); | |
831 | } | |
832 | } | |
833 | t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0; | |
834 | } | |
835 | error("there is no field named %s", fieldname); | |
836 | } | |
837 | ||
838 | found: | |
839 | ||
840 | sym = lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type, fieldno), | |
841 | 0, VAR_NAMESPACE, 0); | |
842 | if (! sym) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type, fieldno)); | |
843 | ||
844 | type = TYPE_FIELD_TYPE (type, fieldno); | |
845 | v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym)); | |
846 | return v; | |
847 | } | |
848 | ||
849 | long | |
850 | unpack_field_as_long (type, valaddr, fieldno) | |
851 | struct type *type; | |
852 | char *valaddr; | |
853 | int fieldno; | |
854 | { | |
855 | long val; | |
856 | int bitpos = TYPE_FIELD_BITPOS (type, fieldno); | |
857 | int bitsize = TYPE_FIELD_BITSIZE (type, fieldno); | |
858 | ||
859 | bcopy (valaddr + bitpos / 8, &val, sizeof val); | |
860 | ||
861 | /* Extracting bits depends on endianness of the machine. */ | |
862 | #ifdef BITS_BIG_ENDIAN | |
863 | val = val >> (sizeof val * 8 - bitpos % 8 - bitsize); | |
864 | #else | |
865 | val = val >> (bitpos % 8); | |
866 | #endif | |
867 | ||
868 | val &= (1 << bitsize) - 1; | |
869 | return val; | |
870 | } | |
871 | ||
872 | void | |
873 | modify_field (addr, fieldval, bitpos, bitsize) | |
874 | char *addr; | |
875 | int fieldval; | |
876 | int bitpos, bitsize; | |
877 | { | |
878 | long oword; | |
879 | ||
880 | /* Reject values too big to fit in the field in question. | |
881 | Otherwise adjoining fields may be corrupted. */ | |
882 | if (fieldval & ~((1<<bitsize)-1)) | |
883 | error ("Value %d does not fit in %d bits.", fieldval, bitsize); | |
884 | ||
885 | bcopy (addr, &oword, sizeof oword); | |
886 | ||
887 | /* Shifting for bit field depends on endianness of the machine. */ | |
888 | #ifdef BITS_BIG_ENDIAN | |
889 | bitpos = sizeof (oword) * 8 - bitpos - bitsize; | |
890 | #endif | |
891 | ||
892 | oword &= ~(((1 << bitsize) - 1) << bitpos); | |
893 | oword |= fieldval << bitpos; | |
894 | bcopy (&oword, addr, sizeof oword); | |
895 | } | |
896 | \f | |
897 | /* Convert C numbers into newly allocated values */ | |
898 | ||
899 | value | |
900 | value_from_long (type, num) | |
901 | struct type *type; | |
902 | register LONGEST num; | |
903 | { | |
904 | register value val = allocate_value (type); | |
905 | register enum type_code code = TYPE_CODE (type); | |
906 | register int len = TYPE_LENGTH (type); | |
907 | ||
908 | if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM) | |
909 | { | |
910 | if (len == sizeof (char)) | |
911 | * (char *) VALUE_CONTENTS (val) = num; | |
912 | else if (len == sizeof (short)) | |
913 | * (short *) VALUE_CONTENTS (val) = num; | |
914 | else if (len == sizeof (int)) | |
915 | * (int *) VALUE_CONTENTS (val) = num; | |
916 | else if (len == sizeof (long)) | |
917 | * (long *) VALUE_CONTENTS (val) = num; | |
918 | #ifdef LONG_LONG | |
919 | else if (len == sizeof (long long)) | |
920 | * (long long *) VALUE_CONTENTS (val) = num; | |
921 | #endif | |
922 | else | |
923 | error ("Integer type encountered with unexpected data length."); | |
924 | } | |
925 | else | |
926 | error ("Unexpected type encountered for integer constant."); | |
927 | ||
928 | return val; | |
929 | } | |
930 | ||
931 | value | |
932 | value_from_double (type, num) | |
933 | struct type *type; | |
934 | double num; | |
935 | { | |
936 | register value val = allocate_value (type); | |
937 | register enum type_code code = TYPE_CODE (type); | |
938 | register int len = TYPE_LENGTH (type); | |
939 | ||
940 | if (code == TYPE_CODE_FLT) | |
941 | { | |
942 | if (len == sizeof (float)) | |
943 | * (float *) VALUE_CONTENTS (val) = num; | |
944 | else if (len == sizeof (double)) | |
945 | * (double *) VALUE_CONTENTS (val) = num; | |
946 | else | |
947 | error ("Floating type encountered with unexpected data length."); | |
948 | } | |
949 | else | |
950 | error ("Unexpected type encountered for floating constant."); | |
951 | ||
952 | return val; | |
953 | } | |
954 | \f | |
955 | /* Deal with the value that is "about to be returned". */ | |
956 | ||
957 | /* Return the value that a function returning now | |
958 | would be returning to its caller, assuming its type is VALTYPE. | |
959 | RETBUF is where we look for what ought to be the contents | |
960 | of the registers (in raw form). This is because it is often | |
961 | desirable to restore old values to those registers | |
962 | after saving the contents of interest, and then call | |
963 | this function using the saved values. | |
964 | struct_return is non-zero when the function in question is | |
965 | using the structure return conventions on the machine in question; | |
966 | 0 when it is using the value returning conventions (this often | |
967 | means returning pointer to where structure is vs. returning value). */ | |
968 | ||
969 | value | |
970 | value_being_returned (valtype, retbuf, struct_return) | |
971 | register struct type *valtype; | |
972 | char retbuf[REGISTER_BYTES]; | |
973 | int struct_return; | |
974 | { | |
975 | register value val; | |
976 | ||
977 | if (struct_return) | |
978 | return value_at (valtype, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf)); | |
979 | ||
980 | val = allocate_value (valtype); | |
981 | EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS (val)); | |
982 | ||
983 | return val; | |
984 | } | |
985 | ||
986 | /* Return true if the function specified is using the structure returning | |
987 | convention on this machine to return arguments, or 0 if it is using | |
988 | the value returning convention. FUNCTION is the value representing | |
989 | the function, FUNCADDR is the address of the function, and VALUE_TYPE | |
990 | is the type returned by the function */ | |
991 | ||
992 | struct block *block_for_pc (); | |
993 | ||
994 | int | |
995 | using_struct_return (function, funcaddr, value_type) | |
996 | value function; | |
997 | CORE_ADDR funcaddr; | |
998 | struct type *value_type; | |
999 | { | |
1000 | register enum type_code code = TYPE_CODE (value_type); | |
1001 | ||
1002 | if (code == TYPE_CODE_STRUCT || | |
1003 | code == TYPE_CODE_UNION || | |
1004 | code == TYPE_CODE_ARRAY) | |
1005 | { | |
1006 | struct block *b = block_for_pc (funcaddr); | |
1007 | ||
1008 | if (!(BLOCK_GCC_COMPILED (b) && TYPE_LENGTH (value_type) < 8)) | |
1009 | return 1; | |
1010 | } | |
1011 | ||
1012 | return 0; | |
1013 | } | |
1014 | ||
1015 | /* Store VAL so it will be returned if a function returns now. | |
1016 | Does not verify that VAL's type matches what the current | |
1017 | function wants to return. */ | |
1018 | ||
1019 | void | |
1020 | set_return_value (val) | |
1021 | value val; | |
1022 | { | |
1023 | register enum type_code code = TYPE_CODE (VALUE_TYPE (val)); | |
1024 | char regbuf[REGISTER_BYTES]; | |
1025 | double dbuf; | |
1026 | LONGEST lbuf; | |
1027 | ||
1028 | if (code == TYPE_CODE_STRUCT | |
1029 | || code == TYPE_CODE_UNION) | |
1030 | error ("Specifying a struct or union return value is not supported."); | |
1031 | ||
1032 | if (code == TYPE_CODE_FLT) | |
1033 | { | |
1034 | dbuf = value_as_double (val); | |
1035 | ||
1036 | STORE_RETURN_VALUE (VALUE_TYPE (val), &dbuf); | |
1037 | } | |
1038 | else | |
1039 | { | |
1040 | lbuf = value_as_long (val); | |
1041 | STORE_RETURN_VALUE (VALUE_TYPE (val), &lbuf); | |
1042 | } | |
1043 | } | |
1044 | \f | |
1045 | void | |
1046 | _initialize_values () | |
1047 | { | |
1048 | add_info ("convenience", convenience_info, | |
1049 | "Debugger convenience (\"$foo\") variables.\n\ | |
1050 | These variables are created when you assign them values;\n\ | |
1051 | thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\ | |
1052 | A few convenience variables are given values automatically GDB:\n\ | |
1053 | \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\ | |
1054 | \"$__\" holds the contents of the last address examined with \"x\"."); | |
1055 | ||
1056 | add_info ("values", value_history_info, | |
1057 | "Elements of value history (around item number IDX, or last ten)."); | |
1058 | add_info_alias ("history", value_history_info, 0); | |
1059 | } |