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