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