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