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