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