]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Find a variable's value in memory, for GDB, the GNU debugger. |
2 | Copyright 1986, 87, 89, 91, 94, 95, 96, 1998 | |
3 | Free Software Foundation, Inc. | |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "symtab.h" | |
24 | #include "gdbtypes.h" | |
25 | #include "frame.h" | |
26 | #include "value.h" | |
27 | #include "gdbcore.h" | |
28 | #include "inferior.h" | |
29 | #include "target.h" | |
30 | #include "gdb_string.h" | |
31 | #include "floatformat.h" | |
c5aa993b | 32 | #include "symfile.h" /* for overlay functions */ |
c906108c SS |
33 | |
34 | /* This is used to indicate that we don't know the format of the floating point | |
35 | number. Typically, this is useful for native ports, where the actual format | |
36 | is irrelevant, since no conversions will be taking place. */ | |
37 | ||
38 | const struct floatformat floatformat_unknown; | |
39 | ||
40 | /* Registers we shouldn't try to store. */ | |
41 | #if !defined (CANNOT_STORE_REGISTER) | |
42 | #define CANNOT_STORE_REGISTER(regno) 0 | |
43 | #endif | |
44 | ||
45 | static void write_register_gen PARAMS ((int, char *)); | |
46 | ||
c5aa993b | 47 | static int read_relative_register_raw_bytes_for_frame PARAMS ((int regnum, char *myaddr, struct frame_info * frame)); |
7a292a7a | 48 | |
c906108c SS |
49 | /* Basic byte-swapping routines. GDB has needed these for a long time... |
50 | All extract a target-format integer at ADDR which is LEN bytes long. */ | |
51 | ||
52 | #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8 | |
53 | /* 8 bit characters are a pretty safe assumption these days, so we | |
54 | assume it throughout all these swapping routines. If we had to deal with | |
55 | 9 bit characters, we would need to make len be in bits and would have | |
56 | to re-write these routines... */ | |
c5aa993b | 57 | you lose |
c906108c SS |
58 | #endif |
59 | ||
c5aa993b | 60 | LONGEST |
c906108c SS |
61 | extract_signed_integer (addr, len) |
62 | PTR addr; | |
63 | int len; | |
64 | { | |
65 | LONGEST retval; | |
66 | unsigned char *p; | |
c5aa993b | 67 | unsigned char *startaddr = (unsigned char *) addr; |
c906108c SS |
68 | unsigned char *endaddr = startaddr + len; |
69 | ||
70 | if (len > (int) sizeof (LONGEST)) | |
71 | error ("\ | |
72 | That operation is not available on integers of more than %d bytes.", | |
73 | sizeof (LONGEST)); | |
74 | ||
75 | /* Start at the most significant end of the integer, and work towards | |
76 | the least significant. */ | |
77 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
78 | { | |
79 | p = startaddr; | |
80 | /* Do the sign extension once at the start. */ | |
c5aa993b | 81 | retval = ((LONGEST) * p ^ 0x80) - 0x80; |
c906108c SS |
82 | for (++p; p < endaddr; ++p) |
83 | retval = (retval << 8) | *p; | |
84 | } | |
85 | else | |
86 | { | |
87 | p = endaddr - 1; | |
88 | /* Do the sign extension once at the start. */ | |
c5aa993b | 89 | retval = ((LONGEST) * p ^ 0x80) - 0x80; |
c906108c SS |
90 | for (--p; p >= startaddr; --p) |
91 | retval = (retval << 8) | *p; | |
92 | } | |
93 | return retval; | |
94 | } | |
95 | ||
96 | ULONGEST | |
97 | extract_unsigned_integer (addr, len) | |
98 | PTR addr; | |
99 | int len; | |
100 | { | |
101 | ULONGEST retval; | |
102 | unsigned char *p; | |
c5aa993b | 103 | unsigned char *startaddr = (unsigned char *) addr; |
c906108c SS |
104 | unsigned char *endaddr = startaddr + len; |
105 | ||
106 | if (len > (int) sizeof (ULONGEST)) | |
107 | error ("\ | |
108 | That operation is not available on integers of more than %d bytes.", | |
109 | sizeof (ULONGEST)); | |
110 | ||
111 | /* Start at the most significant end of the integer, and work towards | |
112 | the least significant. */ | |
113 | retval = 0; | |
114 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
115 | { | |
116 | for (p = startaddr; p < endaddr; ++p) | |
117 | retval = (retval << 8) | *p; | |
118 | } | |
119 | else | |
120 | { | |
121 | for (p = endaddr - 1; p >= startaddr; --p) | |
122 | retval = (retval << 8) | *p; | |
123 | } | |
124 | return retval; | |
125 | } | |
126 | ||
127 | /* Sometimes a long long unsigned integer can be extracted as a | |
128 | LONGEST value. This is done so that we can print these values | |
129 | better. If this integer can be converted to a LONGEST, this | |
130 | function returns 1 and sets *PVAL. Otherwise it returns 0. */ | |
131 | ||
132 | int | |
133 | extract_long_unsigned_integer (addr, orig_len, pval) | |
134 | PTR addr; | |
135 | int orig_len; | |
136 | LONGEST *pval; | |
137 | { | |
138 | char *p, *first_addr; | |
139 | int len; | |
140 | ||
141 | len = orig_len; | |
142 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
143 | { | |
144 | for (p = (char *) addr; | |
145 | len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len; | |
146 | p++) | |
147 | { | |
148 | if (*p == 0) | |
149 | len--; | |
150 | else | |
151 | break; | |
152 | } | |
153 | first_addr = p; | |
154 | } | |
155 | else | |
156 | { | |
157 | first_addr = (char *) addr; | |
158 | for (p = (char *) addr + orig_len - 1; | |
159 | len > (int) sizeof (LONGEST) && p >= (char *) addr; | |
160 | p--) | |
161 | { | |
162 | if (*p == 0) | |
163 | len--; | |
164 | else | |
165 | break; | |
166 | } | |
167 | } | |
168 | ||
169 | if (len <= (int) sizeof (LONGEST)) | |
170 | { | |
171 | *pval = (LONGEST) extract_unsigned_integer (first_addr, | |
172 | sizeof (LONGEST)); | |
173 | return 1; | |
174 | } | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
179 | CORE_ADDR | |
180 | extract_address (addr, len) | |
181 | PTR addr; | |
182 | int len; | |
183 | { | |
184 | /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure | |
185 | whether we want this to be true eventually. */ | |
c5aa993b | 186 | return (CORE_ADDR) extract_unsigned_integer (addr, len); |
c906108c SS |
187 | } |
188 | ||
189 | void | |
190 | store_signed_integer (addr, len, val) | |
191 | PTR addr; | |
192 | int len; | |
193 | LONGEST val; | |
194 | { | |
195 | unsigned char *p; | |
c5aa993b | 196 | unsigned char *startaddr = (unsigned char *) addr; |
c906108c SS |
197 | unsigned char *endaddr = startaddr + len; |
198 | ||
199 | /* Start at the least significant end of the integer, and work towards | |
200 | the most significant. */ | |
201 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
202 | { | |
203 | for (p = endaddr - 1; p >= startaddr; --p) | |
204 | { | |
205 | *p = val & 0xff; | |
206 | val >>= 8; | |
207 | } | |
208 | } | |
209 | else | |
210 | { | |
211 | for (p = startaddr; p < endaddr; ++p) | |
212 | { | |
213 | *p = val & 0xff; | |
214 | val >>= 8; | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | void | |
220 | store_unsigned_integer (addr, len, val) | |
221 | PTR addr; | |
222 | int len; | |
223 | ULONGEST val; | |
224 | { | |
225 | unsigned char *p; | |
c5aa993b | 226 | unsigned char *startaddr = (unsigned char *) addr; |
c906108c SS |
227 | unsigned char *endaddr = startaddr + len; |
228 | ||
229 | /* Start at the least significant end of the integer, and work towards | |
230 | the most significant. */ | |
231 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
232 | { | |
233 | for (p = endaddr - 1; p >= startaddr; --p) | |
234 | { | |
235 | *p = val & 0xff; | |
236 | val >>= 8; | |
237 | } | |
238 | } | |
239 | else | |
240 | { | |
241 | for (p = startaddr; p < endaddr; ++p) | |
242 | { | |
243 | *p = val & 0xff; | |
244 | val >>= 8; | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | /* Store the literal address "val" into | |
250 | gdb-local memory pointed to by "addr" | |
251 | for "len" bytes. */ | |
252 | void | |
253 | store_address (addr, len, val) | |
254 | PTR addr; | |
255 | int len; | |
256 | LONGEST val; | |
257 | { | |
c906108c SS |
258 | store_unsigned_integer (addr, len, val); |
259 | } | |
260 | \f | |
261 | /* Swap LEN bytes at BUFFER between target and host byte-order. */ | |
262 | #define SWAP_FLOATING(buffer,len) \ | |
263 | do \ | |
264 | { \ | |
265 | if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \ | |
266 | { \ | |
267 | char tmp; \ | |
268 | char *p = (char *)(buffer); \ | |
269 | char *q = ((char *)(buffer)) + len - 1; \ | |
270 | for (; p < q; p++, q--) \ | |
271 | { \ | |
272 | tmp = *q; \ | |
273 | *q = *p; \ | |
274 | *p = tmp; \ | |
275 | } \ | |
276 | } \ | |
277 | } \ | |
278 | while (0) | |
279 | ||
280 | /* Extract a floating-point number from a target-order byte-stream at ADDR. | |
281 | Returns the value as type DOUBLEST. | |
282 | ||
283 | If the host and target formats agree, we just copy the raw data into the | |
284 | appropriate type of variable and return, letting the host increase precision | |
285 | as necessary. Otherwise, we call the conversion routine and let it do the | |
286 | dirty work. */ | |
287 | ||
288 | DOUBLEST | |
289 | extract_floating (addr, len) | |
290 | PTR addr; | |
291 | int len; | |
292 | { | |
293 | DOUBLEST dretval; | |
294 | ||
295 | if (len == sizeof (float)) | |
296 | { | |
297 | if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT) | |
298 | { | |
299 | float retval; | |
300 | ||
301 | memcpy (&retval, addr, sizeof (retval)); | |
302 | return retval; | |
303 | } | |
304 | else | |
305 | floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval); | |
306 | } | |
307 | else if (len == sizeof (double)) | |
308 | { | |
309 | if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT) | |
310 | { | |
311 | double retval; | |
312 | ||
313 | memcpy (&retval, addr, sizeof (retval)); | |
314 | return retval; | |
315 | } | |
316 | else | |
317 | floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval); | |
318 | } | |
319 | else if (len == sizeof (DOUBLEST)) | |
320 | { | |
321 | if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT) | |
322 | { | |
323 | DOUBLEST retval; | |
324 | ||
325 | memcpy (&retval, addr, sizeof (retval)); | |
326 | return retval; | |
327 | } | |
328 | else | |
329 | floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval); | |
330 | } | |
331 | else | |
332 | { | |
333 | error ("Can't deal with a floating point number of %d bytes.", len); | |
334 | } | |
335 | ||
336 | return dretval; | |
337 | } | |
338 | ||
339 | void | |
340 | store_floating (addr, len, val) | |
341 | PTR addr; | |
342 | int len; | |
343 | DOUBLEST val; | |
344 | { | |
345 | if (len == sizeof (float)) | |
346 | { | |
347 | if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT) | |
348 | { | |
349 | float floatval = val; | |
350 | ||
351 | memcpy (addr, &floatval, sizeof (floatval)); | |
352 | } | |
353 | else | |
354 | floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr); | |
355 | } | |
356 | else if (len == sizeof (double)) | |
357 | { | |
358 | if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT) | |
359 | { | |
360 | double doubleval = val; | |
361 | ||
362 | memcpy (addr, &doubleval, sizeof (doubleval)); | |
363 | } | |
364 | else | |
365 | floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr); | |
366 | } | |
367 | else if (len == sizeof (DOUBLEST)) | |
368 | { | |
369 | if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT) | |
370 | memcpy (addr, &val, sizeof (val)); | |
371 | else | |
372 | floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr); | |
373 | } | |
374 | else | |
375 | { | |
376 | error ("Can't deal with a floating point number of %d bytes.", len); | |
377 | } | |
378 | } | |
379 | \f | |
c906108c SS |
380 | |
381 | /* Return the address in which frame FRAME's value of register REGNUM | |
382 | has been saved in memory. Or return zero if it has not been saved. | |
383 | If REGNUM specifies the SP, the value we return is actually | |
384 | the SP value, not an address where it was saved. */ | |
385 | ||
386 | CORE_ADDR | |
387 | find_saved_register (frame, regnum) | |
388 | struct frame_info *frame; | |
389 | int regnum; | |
390 | { | |
391 | register struct frame_info *frame1 = NULL; | |
392 | register CORE_ADDR addr = 0; | |
393 | ||
394 | if (frame == NULL) /* No regs saved if want current frame */ | |
395 | return 0; | |
396 | ||
397 | #ifdef HAVE_REGISTER_WINDOWS | |
398 | /* We assume that a register in a register window will only be saved | |
399 | in one place (since the name changes and/or disappears as you go | |
400 | towards inner frames), so we only call get_frame_saved_regs on | |
401 | the current frame. This is directly in contradiction to the | |
402 | usage below, which assumes that registers used in a frame must be | |
403 | saved in a lower (more interior) frame. This change is a result | |
404 | of working on a register window machine; get_frame_saved_regs | |
405 | always returns the registers saved within a frame, within the | |
406 | context (register namespace) of that frame. */ | |
407 | ||
408 | /* However, note that we don't want this to return anything if | |
409 | nothing is saved (if there's a frame inside of this one). Also, | |
410 | callers to this routine asking for the stack pointer want the | |
411 | stack pointer saved for *this* frame; this is returned from the | |
412 | next frame. */ | |
c5aa993b JM |
413 | |
414 | if (REGISTER_IN_WINDOW_P (regnum)) | |
c906108c SS |
415 | { |
416 | frame1 = get_next_frame (frame); | |
c5aa993b JM |
417 | if (!frame1) |
418 | return 0; /* Registers of this frame are active. */ | |
419 | ||
c906108c | 420 | /* Get the SP from the next frame in; it will be this |
c5aa993b | 421 | current frame. */ |
c906108c | 422 | if (regnum != SP_REGNUM) |
c5aa993b JM |
423 | frame1 = frame; |
424 | ||
c906108c SS |
425 | FRAME_INIT_SAVED_REGS (frame1); |
426 | return frame1->saved_regs[regnum]; /* ... which might be zero */ | |
427 | } | |
428 | #endif /* HAVE_REGISTER_WINDOWS */ | |
429 | ||
430 | /* Note that this next routine assumes that registers used in | |
431 | frame x will be saved only in the frame that x calls and | |
432 | frames interior to it. This is not true on the sparc, but the | |
433 | above macro takes care of it, so we should be all right. */ | |
434 | while (1) | |
435 | { | |
436 | QUIT; | |
437 | frame1 = get_prev_frame (frame1); | |
438 | if (frame1 == 0 || frame1 == frame) | |
439 | break; | |
440 | FRAME_INIT_SAVED_REGS (frame1); | |
441 | if (frame1->saved_regs[regnum]) | |
442 | addr = frame1->saved_regs[regnum]; | |
443 | } | |
444 | ||
445 | return addr; | |
446 | } | |
447 | ||
448 | /* Find register number REGNUM relative to FRAME and put its (raw, | |
449 | target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the | |
450 | variable was optimized out (and thus can't be fetched). Set *LVAL | |
451 | to lval_memory, lval_register, or not_lval, depending on whether | |
452 | the value was fetched from memory, from a register, or in a strange | |
453 | and non-modifiable way (e.g. a frame pointer which was calculated | |
454 | rather than fetched). Set *ADDRP to the address, either in memory | |
455 | on as a REGISTER_BYTE offset into the registers array. | |
456 | ||
457 | Note that this implementation never sets *LVAL to not_lval. But | |
458 | it can be replaced by defining GET_SAVED_REGISTER and supplying | |
459 | your own. | |
460 | ||
461 | The argument RAW_BUFFER must point to aligned memory. */ | |
462 | ||
463 | void | |
7a292a7a | 464 | default_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval) |
c906108c SS |
465 | char *raw_buffer; |
466 | int *optimized; | |
467 | CORE_ADDR *addrp; | |
468 | struct frame_info *frame; | |
469 | int regnum; | |
470 | enum lval_type *lval; | |
471 | { | |
472 | CORE_ADDR addr; | |
473 | ||
474 | if (!target_has_registers) | |
475 | error ("No registers."); | |
476 | ||
477 | /* Normal systems don't optimize out things with register numbers. */ | |
478 | if (optimized != NULL) | |
479 | *optimized = 0; | |
480 | addr = find_saved_register (frame, regnum); | |
481 | if (addr != 0) | |
482 | { | |
483 | if (lval != NULL) | |
484 | *lval = lval_memory; | |
485 | if (regnum == SP_REGNUM) | |
486 | { | |
487 | if (raw_buffer != NULL) | |
488 | { | |
489 | /* Put it back in target format. */ | |
c5aa993b | 490 | store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), (LONGEST) addr); |
c906108c SS |
491 | } |
492 | if (addrp != NULL) | |
493 | *addrp = 0; | |
494 | return; | |
495 | } | |
496 | if (raw_buffer != NULL) | |
497 | read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum)); | |
498 | } | |
499 | else | |
500 | { | |
501 | if (lval != NULL) | |
502 | *lval = lval_register; | |
503 | addr = REGISTER_BYTE (regnum); | |
504 | if (raw_buffer != NULL) | |
505 | read_register_gen (regnum, raw_buffer); | |
506 | } | |
507 | if (addrp != NULL) | |
508 | *addrp = addr; | |
509 | } | |
7a292a7a SS |
510 | |
511 | #if !defined (GET_SAVED_REGISTER) | |
512 | #define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \ | |
513 | default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval) | |
514 | #endif | |
515 | void | |
516 | get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval) | |
517 | char *raw_buffer; | |
518 | int *optimized; | |
519 | CORE_ADDR *addrp; | |
520 | struct frame_info *frame; | |
521 | int regnum; | |
522 | enum lval_type *lval; | |
523 | { | |
524 | GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval); | |
525 | } | |
c906108c SS |
526 | |
527 | /* Copy the bytes of register REGNUM, relative to the input stack frame, | |
528 | into our memory at MYADDR, in target byte order. | |
529 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). | |
530 | ||
531 | Returns 1 if could not be read, 0 if could. */ | |
532 | ||
7a292a7a | 533 | static int |
c906108c SS |
534 | read_relative_register_raw_bytes_for_frame (regnum, myaddr, frame) |
535 | int regnum; | |
536 | char *myaddr; | |
537 | struct frame_info *frame; | |
538 | { | |
539 | int optim; | |
540 | if (regnum == FP_REGNUM && frame) | |
541 | { | |
542 | /* Put it back in target format. */ | |
c5aa993b JM |
543 | store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM), |
544 | (LONGEST) FRAME_FP (frame)); | |
c906108c SS |
545 | |
546 | return 0; | |
547 | } | |
548 | ||
549 | get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame, | |
c5aa993b | 550 | regnum, (enum lval_type *) NULL); |
c906108c | 551 | |
c5aa993b JM |
552 | if (register_valid[regnum] < 0) |
553 | return 1; /* register value not available */ | |
c906108c SS |
554 | |
555 | return optim; | |
556 | } | |
557 | ||
558 | /* Copy the bytes of register REGNUM, relative to the current stack frame, | |
559 | into our memory at MYADDR, in target byte order. | |
560 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). | |
561 | ||
562 | Returns 1 if could not be read, 0 if could. */ | |
563 | ||
564 | int | |
565 | read_relative_register_raw_bytes (regnum, myaddr) | |
566 | int regnum; | |
567 | char *myaddr; | |
568 | { | |
c5aa993b | 569 | return read_relative_register_raw_bytes_for_frame (regnum, myaddr, |
c906108c SS |
570 | selected_frame); |
571 | } | |
572 | ||
573 | /* Return a `value' with the contents of register REGNUM | |
574 | in its virtual format, with the type specified by | |
575 | REGISTER_VIRTUAL_TYPE. | |
576 | ||
577 | NOTE: returns NULL if register value is not available. | |
578 | Caller will check return value or die! */ | |
579 | ||
580 | value_ptr | |
581 | value_of_register (regnum) | |
582 | int regnum; | |
583 | { | |
584 | CORE_ADDR addr; | |
585 | int optim; | |
586 | register value_ptr reg_val; | |
587 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; | |
588 | enum lval_type lval; | |
589 | ||
590 | get_saved_register (raw_buffer, &optim, &addr, | |
591 | selected_frame, regnum, &lval); | |
592 | ||
593 | if (register_valid[regnum] < 0) | |
c5aa993b | 594 | return NULL; /* register value not available */ |
c906108c SS |
595 | |
596 | reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum)); | |
597 | ||
598 | /* Convert raw data to virtual format if necessary. */ | |
599 | ||
c906108c SS |
600 | if (REGISTER_CONVERTIBLE (regnum)) |
601 | { | |
602 | REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum), | |
603 | raw_buffer, VALUE_CONTENTS_RAW (reg_val)); | |
604 | } | |
392a587b JM |
605 | else if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum)) |
606 | memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer, | |
607 | REGISTER_RAW_SIZE (regnum)); | |
c906108c | 608 | else |
96baa820 JM |
609 | internal_error ("Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size", |
610 | REGISTER_NAME (regnum), | |
611 | regnum, | |
612 | REGISTER_RAW_SIZE (regnum), | |
613 | REGISTER_VIRTUAL_SIZE (regnum)); | |
c906108c SS |
614 | VALUE_LVAL (reg_val) = lval; |
615 | VALUE_ADDRESS (reg_val) = addr; | |
616 | VALUE_REGNO (reg_val) = regnum; | |
617 | VALUE_OPTIMIZED_OUT (reg_val) = optim; | |
618 | return reg_val; | |
619 | } | |
620 | \f | |
621 | /* Low level examining and depositing of registers. | |
622 | ||
623 | The caller is responsible for making | |
624 | sure that the inferior is stopped before calling the fetching routines, | |
625 | or it will get garbage. (a change from GDB version 3, in which | |
626 | the caller got the value from the last stop). */ | |
627 | ||
7a292a7a | 628 | /* Contents and state of the registers (in target byte order). */ |
c906108c | 629 | |
7a292a7a | 630 | char *registers; |
c906108c | 631 | |
7a292a7a SS |
632 | /* VALID_REGISTER is non-zero if it has been fetched, -1 if the |
633 | register value was not available. */ | |
634 | ||
635 | signed char *register_valid; | |
c906108c SS |
636 | |
637 | /* The thread/process associated with the current set of registers. For now, | |
638 | -1 is special, and means `no current process'. */ | |
639 | int registers_pid = -1; | |
640 | ||
641 | /* Indicate that registers may have changed, so invalidate the cache. */ | |
642 | ||
643 | void | |
644 | registers_changed () | |
645 | { | |
646 | int i; | |
647 | int numregs = ARCH_NUM_REGS; | |
648 | ||
649 | registers_pid = -1; | |
650 | ||
651 | /* Force cleanup of any alloca areas if using C alloca instead of | |
652 | a builtin alloca. This particular call is used to clean up | |
653 | areas allocated by low level target code which may build up | |
654 | during lengthy interactions between gdb and the target before | |
655 | gdb gives control to the user (ie watchpoints). */ | |
656 | alloca (0); | |
657 | ||
658 | for (i = 0; i < numregs; i++) | |
659 | register_valid[i] = 0; | |
660 | ||
661 | if (registers_changed_hook) | |
662 | registers_changed_hook (); | |
663 | } | |
664 | ||
665 | /* Indicate that all registers have been fetched, so mark them all valid. */ | |
666 | void | |
667 | registers_fetched () | |
668 | { | |
669 | int i; | |
670 | int numregs = ARCH_NUM_REGS; | |
671 | for (i = 0; i < numregs; i++) | |
672 | register_valid[i] = 1; | |
673 | } | |
674 | ||
675 | /* read_register_bytes and write_register_bytes are generally a *BAD* idea. | |
676 | They are inefficient because they need to check for partial updates, which | |
677 | can only be done by scanning through all of the registers and seeing if the | |
678 | bytes that are being read/written fall inside of an invalid register. [The | |
c5aa993b JM |
679 | main reason this is necessary is that register sizes can vary, so a simple |
680 | index won't suffice.] It is far better to call read_register_gen if you | |
c906108c SS |
681 | want to get at the raw register contents, as it only takes a regno as an |
682 | argument, and therefore can't do a partial register update. It would also | |
683 | be good to have a write_register_gen for similar reasons. | |
684 | ||
685 | Prior to the recent fixes to check for partial updates, both read and | |
686 | write_register_bytes always checked to see if any registers were stale, and | |
687 | then called target_fetch_registers (-1) to update the whole set. This | |
688 | caused really slowed things down for remote targets. */ | |
689 | ||
690 | /* Copy INLEN bytes of consecutive data from registers | |
691 | starting with the INREGBYTE'th byte of register data | |
692 | into memory at MYADDR. */ | |
693 | ||
694 | void | |
695 | read_register_bytes (inregbyte, myaddr, inlen) | |
696 | int inregbyte; | |
697 | char *myaddr; | |
698 | int inlen; | |
699 | { | |
700 | int inregend = inregbyte + inlen; | |
701 | int regno; | |
702 | ||
703 | if (registers_pid != inferior_pid) | |
704 | { | |
705 | registers_changed (); | |
706 | registers_pid = inferior_pid; | |
707 | } | |
708 | ||
709 | /* See if we are trying to read bytes from out-of-date registers. If so, | |
710 | update just those registers. */ | |
711 | ||
712 | for (regno = 0; regno < NUM_REGS; regno++) | |
713 | { | |
714 | int regstart, regend; | |
715 | int startin, endin; | |
716 | ||
717 | if (register_valid[regno]) | |
718 | continue; | |
719 | ||
720 | if (REGISTER_NAME (regno) == NULL || *REGISTER_NAME (regno) == '\0') | |
721 | continue; | |
722 | ||
723 | regstart = REGISTER_BYTE (regno); | |
724 | regend = regstart + REGISTER_RAW_SIZE (regno); | |
725 | ||
726 | startin = regstart >= inregbyte && regstart < inregend; | |
727 | endin = regend > inregbyte && regend <= inregend; | |
728 | ||
729 | if (!startin && !endin) | |
730 | continue; | |
731 | ||
732 | /* We've found an invalid register where at least one byte will be read. | |
c5aa993b | 733 | Update it from the target. */ |
c906108c SS |
734 | |
735 | target_fetch_registers (regno); | |
736 | ||
737 | if (!register_valid[regno]) | |
738 | error ("read_register_bytes: Couldn't update register %d.", regno); | |
739 | } | |
740 | ||
741 | if (myaddr != NULL) | |
742 | memcpy (myaddr, ®isters[inregbyte], inlen); | |
743 | } | |
744 | ||
745 | /* Read register REGNO into memory at MYADDR, which must be large enough | |
746 | for REGISTER_RAW_BYTES (REGNO). Target byte-order. | |
747 | If the register is known to be the size of a CORE_ADDR or smaller, | |
748 | read_register can be used instead. */ | |
749 | void | |
750 | read_register_gen (regno, myaddr) | |
751 | int regno; | |
752 | char *myaddr; | |
753 | { | |
754 | if (registers_pid != inferior_pid) | |
755 | { | |
756 | registers_changed (); | |
757 | registers_pid = inferior_pid; | |
758 | } | |
759 | ||
760 | if (!register_valid[regno]) | |
761 | target_fetch_registers (regno); | |
762 | memcpy (myaddr, ®isters[REGISTER_BYTE (regno)], | |
763 | REGISTER_RAW_SIZE (regno)); | |
764 | } | |
765 | ||
766 | /* Write register REGNO at MYADDR to the target. MYADDR points at | |
767 | REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */ | |
768 | ||
769 | static void | |
770 | write_register_gen (regno, myaddr) | |
771 | int regno; | |
772 | char *myaddr; | |
773 | { | |
774 | int size; | |
775 | ||
776 | /* On the sparc, writing %g0 is a no-op, so we don't even want to change | |
777 | the registers array if something writes to this register. */ | |
778 | if (CANNOT_STORE_REGISTER (regno)) | |
779 | return; | |
780 | ||
781 | if (registers_pid != inferior_pid) | |
782 | { | |
783 | registers_changed (); | |
784 | registers_pid = inferior_pid; | |
785 | } | |
786 | ||
c5aa993b | 787 | size = REGISTER_RAW_SIZE (regno); |
c906108c SS |
788 | |
789 | /* If we have a valid copy of the register, and new value == old value, | |
790 | then don't bother doing the actual store. */ | |
791 | ||
c5aa993b | 792 | if (register_valid[regno] |
c906108c SS |
793 | && memcmp (®isters[REGISTER_BYTE (regno)], myaddr, size) == 0) |
794 | return; | |
c5aa993b | 795 | |
c906108c SS |
796 | target_prepare_to_store (); |
797 | ||
798 | memcpy (®isters[REGISTER_BYTE (regno)], myaddr, size); | |
799 | ||
c5aa993b | 800 | register_valid[regno] = 1; |
c906108c SS |
801 | |
802 | target_store_registers (regno); | |
803 | } | |
804 | ||
805 | /* Copy INLEN bytes of consecutive data from memory at MYADDR | |
806 | into registers starting with the MYREGSTART'th byte of register data. */ | |
807 | ||
808 | void | |
809 | write_register_bytes (myregstart, myaddr, inlen) | |
810 | int myregstart; | |
811 | char *myaddr; | |
812 | int inlen; | |
813 | { | |
814 | int myregend = myregstart + inlen; | |
815 | int regno; | |
816 | ||
817 | target_prepare_to_store (); | |
818 | ||
819 | /* Scan through the registers updating any that are covered by the range | |
820 | myregstart<=>myregend using write_register_gen, which does nice things | |
821 | like handling threads, and avoiding updates when the new and old contents | |
822 | are the same. */ | |
823 | ||
824 | for (regno = 0; regno < NUM_REGS; regno++) | |
825 | { | |
826 | int regstart, regend; | |
827 | int startin, endin; | |
828 | char regbuf[MAX_REGISTER_RAW_SIZE]; | |
829 | ||
830 | regstart = REGISTER_BYTE (regno); | |
831 | regend = regstart + REGISTER_RAW_SIZE (regno); | |
832 | ||
833 | startin = regstart >= myregstart && regstart < myregend; | |
834 | endin = regend > myregstart && regend <= myregend; | |
835 | ||
836 | if (!startin && !endin) | |
837 | continue; /* Register is completely out of range */ | |
838 | ||
839 | if (startin && endin) /* register is completely in range */ | |
840 | { | |
841 | write_register_gen (regno, myaddr + (regstart - myregstart)); | |
842 | continue; | |
843 | } | |
844 | ||
845 | /* We may be doing a partial update of an invalid register. Update it | |
c5aa993b | 846 | from the target before scribbling on it. */ |
c906108c SS |
847 | read_register_gen (regno, regbuf); |
848 | ||
849 | if (startin) | |
850 | memcpy (registers + regstart, | |
851 | myaddr + regstart - myregstart, | |
852 | myregend - regstart); | |
853 | else /* endin */ | |
854 | memcpy (registers + myregstart, | |
855 | myaddr, | |
856 | regend - myregstart); | |
857 | target_store_registers (regno); | |
858 | } | |
859 | } | |
860 | ||
861 | /* Return the raw contents of register REGNO, regarding it as an integer. */ | |
862 | /* This probably should be returning LONGEST rather than CORE_ADDR. */ | |
863 | ||
864 | CORE_ADDR | |
865 | read_register (regno) | |
866 | int regno; | |
867 | { | |
868 | if (registers_pid != inferior_pid) | |
869 | { | |
870 | registers_changed (); | |
871 | registers_pid = inferior_pid; | |
872 | } | |
873 | ||
874 | if (!register_valid[regno]) | |
875 | target_fetch_registers (regno); | |
876 | ||
c5aa993b JM |
877 | return (CORE_ADDR) extract_address (®isters[REGISTER_BYTE (regno)], |
878 | REGISTER_RAW_SIZE (regno)); | |
c906108c SS |
879 | } |
880 | ||
881 | CORE_ADDR | |
882 | read_register_pid (regno, pid) | |
883 | int regno, pid; | |
884 | { | |
885 | int save_pid; | |
886 | CORE_ADDR retval; | |
887 | ||
888 | if (pid == inferior_pid) | |
889 | return read_register (regno); | |
890 | ||
891 | save_pid = inferior_pid; | |
892 | ||
893 | inferior_pid = pid; | |
894 | ||
895 | retval = read_register (regno); | |
896 | ||
897 | inferior_pid = save_pid; | |
898 | ||
899 | return retval; | |
900 | } | |
901 | ||
902 | /* Store VALUE, into the raw contents of register number REGNO. | |
903 | This should probably write a LONGEST rather than a CORE_ADDR */ | |
904 | ||
905 | void | |
906 | write_register (regno, val) | |
907 | int regno; | |
908 | LONGEST val; | |
909 | { | |
910 | PTR buf; | |
911 | int size; | |
912 | ||
913 | /* On the sparc, writing %g0 is a no-op, so we don't even want to change | |
914 | the registers array if something writes to this register. */ | |
915 | if (CANNOT_STORE_REGISTER (regno)) | |
916 | return; | |
917 | ||
918 | if (registers_pid != inferior_pid) | |
919 | { | |
920 | registers_changed (); | |
921 | registers_pid = inferior_pid; | |
922 | } | |
923 | ||
c5aa993b | 924 | size = REGISTER_RAW_SIZE (regno); |
c906108c | 925 | buf = alloca (size); |
c5aa993b | 926 | store_signed_integer (buf, size, (LONGEST) val); |
c906108c SS |
927 | |
928 | /* If we have a valid copy of the register, and new value == old value, | |
929 | then don't bother doing the actual store. */ | |
930 | ||
c5aa993b | 931 | if (register_valid[regno] |
c906108c SS |
932 | && memcmp (®isters[REGISTER_BYTE (regno)], buf, size) == 0) |
933 | return; | |
c5aa993b | 934 | |
c906108c SS |
935 | target_prepare_to_store (); |
936 | ||
937 | memcpy (®isters[REGISTER_BYTE (regno)], buf, size); | |
938 | ||
c5aa993b | 939 | register_valid[regno] = 1; |
c906108c SS |
940 | |
941 | target_store_registers (regno); | |
942 | } | |
943 | ||
944 | void | |
945 | write_register_pid (regno, val, pid) | |
946 | int regno; | |
947 | CORE_ADDR val; | |
948 | int pid; | |
949 | { | |
950 | int save_pid; | |
951 | ||
952 | if (pid == inferior_pid) | |
953 | { | |
954 | write_register (regno, val); | |
955 | return; | |
956 | } | |
957 | ||
958 | save_pid = inferior_pid; | |
959 | ||
960 | inferior_pid = pid; | |
961 | ||
962 | write_register (regno, val); | |
963 | ||
964 | inferior_pid = save_pid; | |
965 | } | |
966 | ||
967 | /* Record that register REGNO contains VAL. | |
968 | This is used when the value is obtained from the inferior or core dump, | |
969 | so there is no need to store the value there. | |
970 | ||
971 | If VAL is a NULL pointer, then it's probably an unsupported register. We | |
972 | just set it's value to all zeros. We might want to record this fact, and | |
973 | report it to the users of read_register and friends. | |
c5aa993b | 974 | */ |
c906108c SS |
975 | |
976 | void | |
977 | supply_register (regno, val) | |
978 | int regno; | |
979 | char *val; | |
980 | { | |
981 | #if 1 | |
982 | if (registers_pid != inferior_pid) | |
983 | { | |
984 | registers_changed (); | |
985 | registers_pid = inferior_pid; | |
986 | } | |
987 | #endif | |
988 | ||
989 | register_valid[regno] = 1; | |
990 | if (val) | |
991 | memcpy (®isters[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno)); | |
992 | else | |
993 | memset (®isters[REGISTER_BYTE (regno)], '\000', REGISTER_RAW_SIZE (regno)); | |
994 | ||
995 | /* On some architectures, e.g. HPPA, there are a few stray bits in some | |
996 | registers, that the rest of the code would like to ignore. */ | |
997 | #ifdef CLEAN_UP_REGISTER_VALUE | |
c5aa993b | 998 | CLEAN_UP_REGISTER_VALUE (regno, ®isters[REGISTER_BYTE (regno)]); |
c906108c SS |
999 | #endif |
1000 | } | |
1001 | ||
1002 | ||
1003 | /* This routine is getting awfully cluttered with #if's. It's probably | |
1004 | time to turn this into READ_PC and define it in the tm.h file. | |
0f71a2f6 JM |
1005 | Ditto for write_pc. |
1006 | ||
1007 | 1999-06-08: The following were re-written so that it assumes the | |
1008 | existance of a TARGET_READ_PC et.al. macro. A default generic | |
1009 | version of that macro is made available where needed. | |
1010 | ||
1011 | Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled | |
1012 | by the multi-arch framework, it will eventually be possible to | |
1013 | eliminate the intermediate read_pc_pid(). The client would call | |
1014 | TARGET_READ_PC directly. (cagney). */ | |
1015 | ||
1016 | #ifndef TARGET_READ_PC | |
1017 | #define TARGET_READ_PC generic_target_read_pc | |
1018 | #endif | |
1019 | ||
1020 | CORE_ADDR | |
1021 | generic_target_read_pc (pid) | |
1022 | { | |
1023 | #ifdef PC_REGNUM | |
1024 | if (PC_REGNUM >= 0) | |
1025 | { | |
1026 | CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid)); | |
1027 | return pc_val; | |
1028 | } | |
1029 | #endif | |
96baa820 | 1030 | internal_error ("generic_target_read_pc"); |
0f71a2f6 JM |
1031 | return 0; |
1032 | } | |
c906108c SS |
1033 | |
1034 | CORE_ADDR | |
1035 | read_pc_pid (pid) | |
1036 | int pid; | |
1037 | { | |
c5aa993b JM |
1038 | int saved_inferior_pid; |
1039 | CORE_ADDR pc_val; | |
c906108c SS |
1040 | |
1041 | /* In case pid != inferior_pid. */ | |
1042 | saved_inferior_pid = inferior_pid; | |
1043 | inferior_pid = pid; | |
c5aa993b | 1044 | |
c906108c | 1045 | pc_val = TARGET_READ_PC (pid); |
c906108c SS |
1046 | |
1047 | inferior_pid = saved_inferior_pid; | |
1048 | return pc_val; | |
1049 | } | |
1050 | ||
1051 | CORE_ADDR | |
1052 | read_pc () | |
1053 | { | |
1054 | return read_pc_pid (inferior_pid); | |
1055 | } | |
1056 | ||
0f71a2f6 JM |
1057 | #ifndef TARGET_WRITE_PC |
1058 | #define TARGET_WRITE_PC generic_target_write_pc | |
1059 | #endif | |
1060 | ||
1061 | void | |
1062 | generic_target_write_pc (pc, pid) | |
1063 | CORE_ADDR pc; | |
1064 | int pid; | |
1065 | { | |
1066 | #ifdef PC_REGNUM | |
1067 | if (PC_REGNUM >= 0) | |
1068 | write_register_pid (PC_REGNUM, pc, pid); | |
1069 | #ifdef NPC_REGNUM | |
1070 | if (NPC_REGNUM >= 0) | |
1071 | write_register_pid (NPC_REGNUM, pc + 4, pid); | |
1072 | #ifdef NNPC_REGNUM | |
1073 | if (NNPC_REGNUM >= 0) | |
1074 | write_register_pid (NNPC_REGNUM, pc + 8, pid); | |
1075 | #endif | |
1076 | #endif | |
1077 | #else | |
96baa820 | 1078 | internal_error ("generic_target_write_pc"); |
0f71a2f6 JM |
1079 | #endif |
1080 | } | |
1081 | ||
c906108c SS |
1082 | void |
1083 | write_pc_pid (pc, pid) | |
1084 | CORE_ADDR pc; | |
1085 | int pid; | |
1086 | { | |
c5aa993b | 1087 | int saved_inferior_pid; |
c906108c SS |
1088 | |
1089 | /* In case pid != inferior_pid. */ | |
1090 | saved_inferior_pid = inferior_pid; | |
1091 | inferior_pid = pid; | |
c5aa993b | 1092 | |
c906108c | 1093 | TARGET_WRITE_PC (pc, pid); |
c906108c SS |
1094 | |
1095 | inferior_pid = saved_inferior_pid; | |
1096 | } | |
1097 | ||
1098 | void | |
1099 | write_pc (pc) | |
1100 | CORE_ADDR pc; | |
1101 | { | |
1102 | write_pc_pid (pc, inferior_pid); | |
1103 | } | |
1104 | ||
1105 | /* Cope with strage ways of getting to the stack and frame pointers */ | |
1106 | ||
0f71a2f6 JM |
1107 | #ifndef TARGET_READ_SP |
1108 | #define TARGET_READ_SP generic_target_read_sp | |
1109 | #endif | |
1110 | ||
1111 | CORE_ADDR | |
1112 | generic_target_read_sp () | |
1113 | { | |
1114 | #ifdef SP_REGNUM | |
1115 | if (SP_REGNUM >= 0) | |
1116 | return read_register (SP_REGNUM); | |
1117 | #endif | |
96baa820 | 1118 | internal_error ("generic_target_read_sp"); |
0f71a2f6 JM |
1119 | } |
1120 | ||
c906108c SS |
1121 | CORE_ADDR |
1122 | read_sp () | |
1123 | { | |
c906108c | 1124 | return TARGET_READ_SP (); |
0f71a2f6 JM |
1125 | } |
1126 | ||
1127 | #ifndef TARGET_WRITE_SP | |
1128 | #define TARGET_WRITE_SP generic_target_write_sp | |
1129 | #endif | |
1130 | ||
1131 | void | |
1132 | generic_target_write_sp (val) | |
1133 | CORE_ADDR val; | |
1134 | { | |
1135 | #ifdef SP_REGNUM | |
1136 | if (SP_REGNUM >= 0) | |
1137 | { | |
1138 | write_register (SP_REGNUM, val); | |
1139 | return; | |
1140 | } | |
c906108c | 1141 | #endif |
96baa820 | 1142 | internal_error ("generic_target_write_sp"); |
c906108c SS |
1143 | } |
1144 | ||
1145 | void | |
1146 | write_sp (val) | |
1147 | CORE_ADDR val; | |
1148 | { | |
c906108c | 1149 | TARGET_WRITE_SP (val); |
0f71a2f6 JM |
1150 | } |
1151 | ||
1152 | #ifndef TARGET_READ_FP | |
1153 | #define TARGET_READ_FP generic_target_read_fp | |
c906108c | 1154 | #endif |
0f71a2f6 JM |
1155 | |
1156 | CORE_ADDR | |
1157 | generic_target_read_fp () | |
1158 | { | |
1159 | #ifdef FP_REGNUM | |
1160 | if (FP_REGNUM >= 0) | |
1161 | return read_register (FP_REGNUM); | |
1162 | #endif | |
96baa820 | 1163 | internal_error ("generic_target_read_fp"); |
c906108c SS |
1164 | } |
1165 | ||
1166 | CORE_ADDR | |
1167 | read_fp () | |
1168 | { | |
c906108c | 1169 | return TARGET_READ_FP (); |
0f71a2f6 JM |
1170 | } |
1171 | ||
1172 | #ifndef TARGET_WRITE_FP | |
1173 | #define TARGET_WRITE_FP generic_target_write_fp | |
1174 | #endif | |
1175 | ||
1176 | void | |
1177 | generic_target_write_fp (val) | |
1178 | CORE_ADDR val; | |
1179 | { | |
1180 | #ifdef FP_REGNUM | |
1181 | if (FP_REGNUM >= 0) | |
1182 | { | |
1183 | write_register (FP_REGNUM, val); | |
1184 | return; | |
1185 | } | |
c906108c | 1186 | #endif |
96baa820 | 1187 | internal_error ("generic_target_write_fp"); |
c906108c SS |
1188 | } |
1189 | ||
1190 | void | |
1191 | write_fp (val) | |
1192 | CORE_ADDR val; | |
1193 | { | |
c906108c | 1194 | TARGET_WRITE_FP (val); |
c906108c SS |
1195 | } |
1196 | \f | |
1197 | /* Will calling read_var_value or locate_var_value on SYM end | |
1198 | up caring what frame it is being evaluated relative to? SYM must | |
1199 | be non-NULL. */ | |
1200 | int | |
1201 | symbol_read_needs_frame (sym) | |
1202 | struct symbol *sym; | |
1203 | { | |
1204 | switch (SYMBOL_CLASS (sym)) | |
1205 | { | |
1206 | /* All cases listed explicitly so that gcc -Wall will detect it if | |
c5aa993b | 1207 | we failed to consider one. */ |
c906108c SS |
1208 | case LOC_REGISTER: |
1209 | case LOC_ARG: | |
1210 | case LOC_REF_ARG: | |
1211 | case LOC_REGPARM: | |
1212 | case LOC_REGPARM_ADDR: | |
1213 | case LOC_LOCAL: | |
1214 | case LOC_LOCAL_ARG: | |
1215 | case LOC_BASEREG: | |
1216 | case LOC_BASEREG_ARG: | |
1217 | case LOC_THREAD_LOCAL_STATIC: | |
1218 | return 1; | |
1219 | ||
1220 | case LOC_UNDEF: | |
1221 | case LOC_CONST: | |
1222 | case LOC_STATIC: | |
1223 | case LOC_INDIRECT: | |
1224 | case LOC_TYPEDEF: | |
1225 | ||
1226 | case LOC_LABEL: | |
1227 | /* Getting the address of a label can be done independently of the block, | |
c5aa993b JM |
1228 | even if some *uses* of that address wouldn't work so well without |
1229 | the right frame. */ | |
c906108c SS |
1230 | |
1231 | case LOC_BLOCK: | |
1232 | case LOC_CONST_BYTES: | |
1233 | case LOC_UNRESOLVED: | |
1234 | case LOC_OPTIMIZED_OUT: | |
1235 | return 0; | |
1236 | } | |
1237 | return 1; | |
1238 | } | |
1239 | ||
1240 | /* Given a struct symbol for a variable, | |
1241 | and a stack frame id, read the value of the variable | |
1242 | and return a (pointer to a) struct value containing the value. | |
1243 | If the variable cannot be found, return a zero pointer. | |
1244 | If FRAME is NULL, use the selected_frame. */ | |
1245 | ||
1246 | value_ptr | |
1247 | read_var_value (var, frame) | |
1248 | register struct symbol *var; | |
1249 | struct frame_info *frame; | |
1250 | { | |
1251 | register value_ptr v; | |
1252 | struct type *type = SYMBOL_TYPE (var); | |
1253 | CORE_ADDR addr; | |
1254 | register int len; | |
1255 | ||
1256 | v = allocate_value (type); | |
1257 | VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */ | |
1258 | VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var); | |
1259 | ||
1260 | len = TYPE_LENGTH (type); | |
1261 | ||
c5aa993b JM |
1262 | if (frame == NULL) |
1263 | frame = selected_frame; | |
c906108c SS |
1264 | |
1265 | switch (SYMBOL_CLASS (var)) | |
1266 | { | |
1267 | case LOC_CONST: | |
1268 | /* Put the constant back in target format. */ | |
1269 | store_signed_integer (VALUE_CONTENTS_RAW (v), len, | |
1270 | (LONGEST) SYMBOL_VALUE (var)); | |
1271 | VALUE_LVAL (v) = not_lval; | |
1272 | return v; | |
1273 | ||
1274 | case LOC_LABEL: | |
1275 | /* Put the constant back in target format. */ | |
1276 | if (overlay_debugging) | |
c5aa993b JM |
1277 | store_address (VALUE_CONTENTS_RAW (v), len, |
1278 | (LONGEST) symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var), | |
1279 | SYMBOL_BFD_SECTION (var))); | |
c906108c SS |
1280 | else |
1281 | store_address (VALUE_CONTENTS_RAW (v), len, | |
c5aa993b | 1282 | (LONGEST) SYMBOL_VALUE_ADDRESS (var)); |
c906108c SS |
1283 | VALUE_LVAL (v) = not_lval; |
1284 | return v; | |
1285 | ||
1286 | case LOC_CONST_BYTES: | |
1287 | { | |
1288 | char *bytes_addr; | |
1289 | bytes_addr = SYMBOL_VALUE_BYTES (var); | |
1290 | memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len); | |
1291 | VALUE_LVAL (v) = not_lval; | |
1292 | return v; | |
1293 | } | |
1294 | ||
1295 | case LOC_STATIC: | |
1296 | if (overlay_debugging) | |
1297 | addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var), | |
1298 | SYMBOL_BFD_SECTION (var)); | |
1299 | else | |
1300 | addr = SYMBOL_VALUE_ADDRESS (var); | |
1301 | break; | |
1302 | ||
1303 | case LOC_INDIRECT: | |
1304 | /* The import slot does not have a real address in it from the | |
1305 | dynamic loader (dld.sl on HP-UX), if the target hasn't begun | |
c5aa993b | 1306 | execution yet, so check for that. */ |
c906108c | 1307 | if (!target_has_execution) |
c5aa993b | 1308 | error ("\ |
c906108c SS |
1309 | Attempt to access variable defined in different shared object or load module when\n\ |
1310 | addresses have not been bound by the dynamic loader. Try again when executable is running."); | |
c5aa993b | 1311 | |
c906108c SS |
1312 | addr = SYMBOL_VALUE_ADDRESS (var); |
1313 | addr = read_memory_unsigned_integer | |
1314 | (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT); | |
1315 | break; | |
1316 | ||
1317 | case LOC_ARG: | |
1318 | if (frame == NULL) | |
1319 | return 0; | |
1320 | addr = FRAME_ARGS_ADDRESS (frame); | |
1321 | if (!addr) | |
1322 | return 0; | |
1323 | addr += SYMBOL_VALUE (var); | |
1324 | break; | |
1325 | ||
1326 | case LOC_REF_ARG: | |
1327 | if (frame == NULL) | |
1328 | return 0; | |
1329 | addr = FRAME_ARGS_ADDRESS (frame); | |
1330 | if (!addr) | |
1331 | return 0; | |
1332 | addr += SYMBOL_VALUE (var); | |
1333 | addr = read_memory_unsigned_integer | |
1334 | (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT); | |
1335 | break; | |
1336 | ||
1337 | case LOC_LOCAL: | |
1338 | case LOC_LOCAL_ARG: | |
1339 | if (frame == NULL) | |
1340 | return 0; | |
1341 | addr = FRAME_LOCALS_ADDRESS (frame); | |
1342 | addr += SYMBOL_VALUE (var); | |
1343 | break; | |
1344 | ||
1345 | case LOC_BASEREG: | |
1346 | case LOC_BASEREG_ARG: | |
1347 | { | |
1348 | char buf[MAX_REGISTER_RAW_SIZE]; | |
1349 | get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var), | |
1350 | NULL); | |
1351 | addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var))); | |
1352 | addr += SYMBOL_VALUE (var); | |
1353 | break; | |
1354 | } | |
c5aa993b | 1355 | |
c906108c SS |
1356 | case LOC_THREAD_LOCAL_STATIC: |
1357 | { | |
c5aa993b JM |
1358 | char buf[MAX_REGISTER_RAW_SIZE]; |
1359 | ||
1360 | get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var), | |
c906108c | 1361 | NULL); |
c5aa993b JM |
1362 | addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var))); |
1363 | addr += SYMBOL_VALUE (var); | |
1364 | break; | |
c906108c | 1365 | } |
c5aa993b | 1366 | |
c906108c SS |
1367 | case LOC_TYPEDEF: |
1368 | error ("Cannot look up value of a typedef"); | |
1369 | break; | |
1370 | ||
1371 | case LOC_BLOCK: | |
1372 | if (overlay_debugging) | |
c5aa993b | 1373 | VALUE_ADDRESS (v) = symbol_overlayed_address |
c906108c SS |
1374 | (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var)); |
1375 | else | |
1376 | VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var)); | |
1377 | return v; | |
1378 | ||
1379 | case LOC_REGISTER: | |
1380 | case LOC_REGPARM: | |
1381 | case LOC_REGPARM_ADDR: | |
1382 | { | |
1383 | struct block *b; | |
1384 | int regno = SYMBOL_VALUE (var); | |
1385 | value_ptr regval; | |
1386 | ||
1387 | if (frame == NULL) | |
1388 | return 0; | |
1389 | b = get_frame_block (frame); | |
1390 | ||
1391 | if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR) | |
1392 | { | |
1393 | regval = value_from_register (lookup_pointer_type (type), | |
c5aa993b | 1394 | regno, |
c906108c SS |
1395 | frame); |
1396 | ||
1397 | if (regval == NULL) | |
1398 | error ("Value of register variable not available."); | |
1399 | ||
c5aa993b | 1400 | addr = value_as_pointer (regval); |
c906108c SS |
1401 | VALUE_LVAL (v) = lval_memory; |
1402 | } | |
1403 | else | |
1404 | { | |
1405 | regval = value_from_register (type, regno, frame); | |
1406 | ||
1407 | if (regval == NULL) | |
1408 | error ("Value of register variable not available."); | |
1409 | return regval; | |
1410 | } | |
1411 | } | |
1412 | break; | |
1413 | ||
1414 | case LOC_UNRESOLVED: | |
1415 | { | |
1416 | struct minimal_symbol *msym; | |
1417 | ||
1418 | msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL); | |
1419 | if (msym == NULL) | |
1420 | return 0; | |
1421 | if (overlay_debugging) | |
1422 | addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym), | |
1423 | SYMBOL_BFD_SECTION (msym)); | |
1424 | else | |
1425 | addr = SYMBOL_VALUE_ADDRESS (msym); | |
1426 | } | |
1427 | break; | |
1428 | ||
1429 | case LOC_OPTIMIZED_OUT: | |
1430 | VALUE_LVAL (v) = not_lval; | |
1431 | VALUE_OPTIMIZED_OUT (v) = 1; | |
1432 | return v; | |
1433 | ||
1434 | default: | |
1435 | error ("Cannot look up value of a botched symbol."); | |
1436 | break; | |
1437 | } | |
1438 | ||
1439 | VALUE_ADDRESS (v) = addr; | |
1440 | VALUE_LAZY (v) = 1; | |
1441 | return v; | |
1442 | } | |
1443 | ||
1444 | /* Return a value of type TYPE, stored in register REGNUM, in frame | |
1445 | FRAME. | |
1446 | ||
1447 | NOTE: returns NULL if register value is not available. | |
1448 | Caller will check return value or die! */ | |
1449 | ||
1450 | value_ptr | |
1451 | value_from_register (type, regnum, frame) | |
1452 | struct type *type; | |
1453 | int regnum; | |
1454 | struct frame_info *frame; | |
1455 | { | |
c5aa993b | 1456 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; |
c906108c SS |
1457 | CORE_ADDR addr; |
1458 | int optim; | |
1459 | value_ptr v = allocate_value (type); | |
1460 | char *value_bytes = 0; | |
1461 | int value_bytes_copied = 0; | |
1462 | int num_storage_locs; | |
1463 | enum lval_type lval; | |
1464 | int len; | |
1465 | ||
1466 | CHECK_TYPEDEF (type); | |
1467 | len = TYPE_LENGTH (type); | |
1468 | ||
1469 | VALUE_REGNO (v) = regnum; | |
1470 | ||
1471 | num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ? | |
1472 | ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 : | |
1473 | 1); | |
1474 | ||
1475 | if (num_storage_locs > 1 | |
1476 | #ifdef GDB_TARGET_IS_H8500 | |
1477 | || TYPE_CODE (type) == TYPE_CODE_PTR | |
1478 | #endif | |
c5aa993b | 1479 | ) |
c906108c SS |
1480 | { |
1481 | /* Value spread across multiple storage locations. */ | |
c5aa993b | 1482 | |
c906108c SS |
1483 | int local_regnum; |
1484 | int mem_stor = 0, reg_stor = 0; | |
1485 | int mem_tracking = 1; | |
1486 | CORE_ADDR last_addr = 0; | |
1487 | CORE_ADDR first_addr = 0; | |
1488 | ||
1489 | value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE); | |
1490 | ||
1491 | /* Copy all of the data out, whereever it may be. */ | |
1492 | ||
1493 | #ifdef GDB_TARGET_IS_H8500 | |
1494 | /* This piece of hideosity is required because the H8500 treats registers | |
1495 | differently depending upon whether they are used as pointers or not. As a | |
1496 | pointer, a register needs to have a page register tacked onto the front. | |
1497 | An alternate way to do this would be to have gcc output different register | |
1498 | numbers for the pointer & non-pointer form of the register. But, it | |
1499 | doesn't, so we're stuck with this. */ | |
1500 | ||
1501 | if (TYPE_CODE (type) == TYPE_CODE_PTR | |
1502 | && len > 2) | |
1503 | { | |
1504 | int page_regnum; | |
1505 | ||
1506 | switch (regnum) | |
1507 | { | |
c5aa993b JM |
1508 | case R0_REGNUM: |
1509 | case R1_REGNUM: | |
1510 | case R2_REGNUM: | |
1511 | case R3_REGNUM: | |
c906108c SS |
1512 | page_regnum = SEG_D_REGNUM; |
1513 | break; | |
c5aa993b JM |
1514 | case R4_REGNUM: |
1515 | case R5_REGNUM: | |
c906108c SS |
1516 | page_regnum = SEG_E_REGNUM; |
1517 | break; | |
c5aa993b JM |
1518 | case R6_REGNUM: |
1519 | case R7_REGNUM: | |
c906108c SS |
1520 | page_regnum = SEG_T_REGNUM; |
1521 | break; | |
1522 | } | |
1523 | ||
1524 | value_bytes[0] = 0; | |
1525 | get_saved_register (value_bytes + 1, | |
1526 | &optim, | |
1527 | &addr, | |
1528 | frame, | |
1529 | page_regnum, | |
1530 | &lval); | |
1531 | ||
1532 | if (register_valid[page_regnum] == -1) | |
1533 | return NULL; /* register value not available */ | |
1534 | ||
1535 | if (lval == lval_register) | |
1536 | reg_stor++; | |
1537 | else | |
1538 | mem_stor++; | |
1539 | first_addr = addr; | |
1540 | last_addr = addr; | |
1541 | ||
1542 | get_saved_register (value_bytes + 2, | |
1543 | &optim, | |
1544 | &addr, | |
1545 | frame, | |
1546 | regnum, | |
1547 | &lval); | |
1548 | ||
1549 | if (register_valid[regnum] == -1) | |
1550 | return NULL; /* register value not available */ | |
1551 | ||
1552 | if (lval == lval_register) | |
1553 | reg_stor++; | |
1554 | else | |
1555 | { | |
1556 | mem_stor++; | |
1557 | mem_tracking = mem_tracking && (addr == last_addr); | |
1558 | } | |
1559 | last_addr = addr; | |
1560 | } | |
1561 | else | |
c5aa993b | 1562 | #endif /* GDB_TARGET_IS_H8500 */ |
c906108c SS |
1563 | for (local_regnum = regnum; |
1564 | value_bytes_copied < len; | |
1565 | (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum), | |
1566 | ++local_regnum)) | |
1567 | { | |
1568 | get_saved_register (value_bytes + value_bytes_copied, | |
1569 | &optim, | |
1570 | &addr, | |
1571 | frame, | |
1572 | local_regnum, | |
1573 | &lval); | |
1574 | ||
c5aa993b JM |
1575 | if (register_valid[local_regnum] == -1) |
1576 | return NULL; /* register value not available */ | |
c906108c SS |
1577 | |
1578 | if (regnum == local_regnum) | |
1579 | first_addr = addr; | |
1580 | if (lval == lval_register) | |
1581 | reg_stor++; | |
1582 | else | |
1583 | { | |
1584 | mem_stor++; | |
c5aa993b | 1585 | |
c906108c SS |
1586 | mem_tracking = |
1587 | (mem_tracking | |
1588 | && (regnum == local_regnum | |
1589 | || addr == last_addr)); | |
1590 | } | |
1591 | last_addr = addr; | |
1592 | } | |
1593 | ||
1594 | if ((reg_stor && mem_stor) | |
1595 | || (mem_stor && !mem_tracking)) | |
1596 | /* Mixed storage; all of the hassle we just went through was | |
1597 | for some good purpose. */ | |
1598 | { | |
1599 | VALUE_LVAL (v) = lval_reg_frame_relative; | |
1600 | VALUE_FRAME (v) = FRAME_FP (frame); | |
1601 | VALUE_FRAME_REGNUM (v) = regnum; | |
1602 | } | |
1603 | else if (mem_stor) | |
1604 | { | |
1605 | VALUE_LVAL (v) = lval_memory; | |
1606 | VALUE_ADDRESS (v) = first_addr; | |
1607 | } | |
1608 | else if (reg_stor) | |
1609 | { | |
1610 | VALUE_LVAL (v) = lval_register; | |
1611 | VALUE_ADDRESS (v) = first_addr; | |
1612 | } | |
1613 | else | |
96baa820 | 1614 | internal_error ("value_from_register: Value not stored anywhere!"); |
c906108c SS |
1615 | |
1616 | VALUE_OPTIMIZED_OUT (v) = optim; | |
1617 | ||
1618 | /* Any structure stored in more than one register will always be | |
c5aa993b JM |
1619 | an integral number of registers. Otherwise, you'd need to do |
1620 | some fiddling with the last register copied here for little | |
1621 | endian machines. */ | |
c906108c SS |
1622 | |
1623 | /* Copy into the contents section of the value. */ | |
1624 | memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len); | |
1625 | ||
1626 | /* Finally do any conversion necessary when extracting this | |
1627 | type from more than one register. */ | |
1628 | #ifdef REGISTER_CONVERT_TO_TYPE | |
c5aa993b | 1629 | REGISTER_CONVERT_TO_TYPE (regnum, type, VALUE_CONTENTS_RAW (v)); |
c906108c SS |
1630 | #endif |
1631 | return v; | |
1632 | } | |
1633 | ||
1634 | /* Data is completely contained within a single register. Locate the | |
1635 | register's contents in a real register or in core; | |
1636 | read the data in raw format. */ | |
1637 | ||
1638 | get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval); | |
1639 | ||
1640 | if (register_valid[regnum] == -1) | |
c5aa993b | 1641 | return NULL; /* register value not available */ |
c906108c SS |
1642 | |
1643 | VALUE_OPTIMIZED_OUT (v) = optim; | |
1644 | VALUE_LVAL (v) = lval; | |
1645 | VALUE_ADDRESS (v) = addr; | |
1646 | ||
1647 | /* Convert raw data to virtual format if necessary. */ | |
c5aa993b | 1648 | |
c906108c SS |
1649 | if (REGISTER_CONVERTIBLE (regnum)) |
1650 | { | |
1651 | REGISTER_CONVERT_TO_VIRTUAL (regnum, type, | |
1652 | raw_buffer, VALUE_CONTENTS_RAW (v)); | |
1653 | } | |
1654 | else | |
c906108c SS |
1655 | { |
1656 | /* Raw and virtual formats are the same for this register. */ | |
1657 | ||
1658 | if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum)) | |
1659 | { | |
c5aa993b | 1660 | /* Big-endian, and we want less than full size. */ |
c906108c SS |
1661 | VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len; |
1662 | } | |
1663 | ||
1664 | memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len); | |
1665 | } | |
c5aa993b | 1666 | |
c906108c SS |
1667 | return v; |
1668 | } | |
1669 | \f | |
1670 | /* Given a struct symbol for a variable or function, | |
1671 | and a stack frame id, | |
1672 | return a (pointer to a) struct value containing the properly typed | |
1673 | address. */ | |
1674 | ||
1675 | value_ptr | |
1676 | locate_var_value (var, frame) | |
1677 | register struct symbol *var; | |
1678 | struct frame_info *frame; | |
1679 | { | |
1680 | CORE_ADDR addr = 0; | |
1681 | struct type *type = SYMBOL_TYPE (var); | |
1682 | value_ptr lazy_value; | |
1683 | ||
1684 | /* Evaluate it first; if the result is a memory address, we're fine. | |
1685 | Lazy evaluation pays off here. */ | |
1686 | ||
1687 | lazy_value = read_var_value (var, frame); | |
1688 | if (lazy_value == 0) | |
1689 | error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var)); | |
1690 | ||
1691 | if (VALUE_LAZY (lazy_value) | |
1692 | || TYPE_CODE (type) == TYPE_CODE_FUNC) | |
1693 | { | |
1694 | value_ptr val; | |
1695 | ||
1696 | addr = VALUE_ADDRESS (lazy_value); | |
c5aa993b | 1697 | val = value_from_longest (lookup_pointer_type (type), (LONGEST) addr); |
c906108c SS |
1698 | VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value); |
1699 | return val; | |
1700 | } | |
1701 | ||
1702 | /* Not a memory address; check what the problem was. */ | |
c5aa993b | 1703 | switch (VALUE_LVAL (lazy_value)) |
c906108c SS |
1704 | { |
1705 | case lval_register: | |
1706 | case lval_reg_frame_relative: | |
1707 | error ("Address requested for identifier \"%s\" which is in a register.", | |
1708 | SYMBOL_SOURCE_NAME (var)); | |
1709 | break; | |
1710 | ||
1711 | default: | |
1712 | error ("Can't take address of \"%s\" which isn't an lvalue.", | |
1713 | SYMBOL_SOURCE_NAME (var)); | |
1714 | break; | |
1715 | } | |
c5aa993b | 1716 | return 0; /* For lint -- never reached */ |
c906108c | 1717 | } |
7a292a7a | 1718 | \f |
c5aa993b | 1719 | |
7a292a7a SS |
1720 | static void build_findvar PARAMS ((void)); |
1721 | static void | |
1722 | build_findvar () | |
1723 | { | |
1724 | /* We allocate some extra slop since we do a lot of memcpy's around | |
1725 | `registers', and failing-soft is better than failing hard. */ | |
1726 | int sizeof_registers = REGISTER_BYTES + /* SLOP */ 256; | |
1727 | int sizeof_register_valid = NUM_REGS * sizeof (*register_valid); | |
1728 | registers = xmalloc (sizeof_registers); | |
1729 | memset (registers, 0, sizeof_registers); | |
1730 | register_valid = xmalloc (sizeof_register_valid); | |
1731 | memset (register_valid, 0, sizeof_register_valid); | |
1732 | } | |
1733 | ||
1734 | void _initialize_findvar PARAMS ((void)); | |
1735 | void | |
1736 | _initialize_findvar () | |
1737 | { | |
1738 | build_findvar (); | |
0f71a2f6 JM |
1739 | |
1740 | register_gdbarch_swap (®isters, sizeof (registers), NULL); | |
1741 | register_gdbarch_swap (®ister_valid, sizeof (register_valid), NULL); | |
1742 | register_gdbarch_swap (NULL, 0, build_findvar); | |
7a292a7a | 1743 | } |