7b4ac7e1 |
1 | /* Find a variable's value in memory, for GDB, the GNU debugger. |
2 | Copyright (C) 1986, 1987 Free Software Foundation, Inc. |
3 | |
4 | GDB is distributed in the hope that it will be useful, but WITHOUT ANY |
5 | WARRANTY. No author or distributor accepts responsibility to anyone |
6 | for the consequences of using it or for whether it serves any |
7 | particular purpose or works at all, unless he says so in writing. |
8 | Refer to the GDB General Public License for full details. |
9 | |
10 | Everyone is granted permission to copy, modify and redistribute GDB, |
11 | but only under the conditions described in the GDB General Public |
12 | License. A copy of this license is supposed to have been given to you |
13 | along with GDB so you can know your rights and responsibilities. It |
14 | should be in a file named COPYING. Among other things, the copyright |
15 | notice and this notice must be preserved on all copies. |
16 | |
17 | In other words, go ahead and share GDB, but don't try to stop |
18 | anyone else from sharing it farther. Help stamp out software hoarding! |
19 | */ |
20 | |
21 | #include "defs.h" |
7b4ac7e1 |
22 | #include "param.h" |
23 | #include "symtab.h" |
24 | #include "frame.h" |
25 | #include "value.h" |
26 | |
27 | CORE_ADDR read_register (); |
28 | |
7b4ac7e1 |
29 | /* Return the address in which frame FRAME's value of register REGNUM |
30 | has been saved in memory. Or return zero if it has not been saved. |
31 | If REGNUM specifies the SP, the value we return is actually |
32 | the SP value, not an address where it was saved. */ |
33 | |
e91b87a3 |
34 | CORE_ADDR |
7b4ac7e1 |
35 | find_saved_register (frame, regnum) |
36 | FRAME frame; |
37 | int regnum; |
38 | { |
e91b87a3 |
39 | struct frame_info *fi; |
7b4ac7e1 |
40 | struct frame_saved_regs saved_regs; |
41 | |
42 | register FRAME frame1 = 0; |
43 | register CORE_ADDR addr = 0; |
44 | |
e91b87a3 |
45 | #ifdef HAVE_REGISTER_WINDOWS |
46 | /* We assume that a register in a register window will only be saved |
47 | in one place (since the name changes and dissapears as you go |
48 | towards inner frames), so we only call get_frame_saved_regs on |
49 | the current frame. This is directly in contradiction to the |
50 | usage below, which assumes that registers used in a frame must be |
51 | saved in a lower (more interior) frame. This change is a result |
52 | of working on a register window machine; get_frame_saved_regs |
53 | always returns the registers saved within a frame, within the |
54 | context (register namespace) of that frame. */ |
55 | |
56 | if (REGISTER_IN_WINDOW_P(regnum)) |
57 | { |
58 | fi = get_frame_info (frame); |
59 | get_frame_saved_regs (fi, &saved_regs); |
60 | return (saved_regs.regs[regnum] ? |
61 | saved_regs.regs[regnum] : 0); |
62 | } |
63 | #endif /* HAVE_REGISTER_WINDOWS */ |
64 | |
65 | /* Note that this next routine assumes that registers used in |
66 | frame x will be saved only in the frame that x calls and |
67 | frames interior to it. This is not true on the sparc, but the |
68 | above macro takes care of it, so we should be all right. */ |
7b4ac7e1 |
69 | while (1) |
70 | { |
71 | QUIT; |
e91b87a3 |
72 | frame1 = get_prev_frame (frame1); |
73 | if (frame1 == 0 || frame1 == frame) |
7b4ac7e1 |
74 | break; |
e91b87a3 |
75 | fi = get_frame_info (frame1); |
76 | get_frame_saved_regs (fi, &saved_regs); |
7b4ac7e1 |
77 | if (saved_regs.regs[regnum]) |
78 | addr = saved_regs.regs[regnum]; |
7b4ac7e1 |
79 | } |
80 | |
81 | return addr; |
82 | } |
83 | |
84 | /* Copy the bytes of register REGNUM, relative to the current stack frame, |
85 | into our memory at MYADDR. |
86 | The number of bytes copied is REGISTER_RAW_SIZE (REGNUM). */ |
87 | |
88 | void |
89 | read_relative_register_raw_bytes (regnum, myaddr) |
90 | int regnum; |
91 | char *myaddr; |
92 | { |
93 | register CORE_ADDR addr; |
94 | |
95 | if (regnum == FP_REGNUM) |
96 | { |
e91b87a3 |
97 | bcopy (&FRAME_FP(selected_frame), myaddr, sizeof (CORE_ADDR)); |
7b4ac7e1 |
98 | return; |
99 | } |
100 | |
101 | addr = find_saved_register (selected_frame, regnum); |
102 | |
103 | if (addr) |
104 | { |
105 | if (regnum == SP_REGNUM) |
106 | { |
107 | CORE_ADDR buffer = addr; |
108 | bcopy (&buffer, myaddr, sizeof (CORE_ADDR)); |
109 | } |
110 | else |
111 | read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum)); |
112 | return; |
113 | } |
114 | read_register_bytes (REGISTER_BYTE (regnum), |
115 | myaddr, REGISTER_RAW_SIZE (regnum)); |
116 | } |
117 | |
118 | /* Return a `value' with the contents of register REGNUM |
119 | in its virtual format, with the type specified by |
120 | REGISTER_VIRTUAL_TYPE. */ |
121 | |
122 | value |
123 | value_of_register (regnum) |
124 | int regnum; |
125 | { |
e91b87a3 |
126 | register CORE_ADDR addr; |
7b4ac7e1 |
127 | register value val; |
128 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; |
129 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; |
130 | |
e91b87a3 |
131 | if (! (have_inferior_p () || have_core_file_p ())) |
132 | error ("Can't get value of register without inferior or core file"); |
133 | |
134 | addr = find_saved_register (selected_frame, regnum); |
7b4ac7e1 |
135 | if (addr) |
136 | { |
137 | if (regnum == SP_REGNUM) |
e91b87a3 |
138 | return value_from_long (builtin_type_int, (LONGEST) addr); |
7b4ac7e1 |
139 | read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum)); |
140 | } |
141 | else |
142 | read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, |
143 | REGISTER_RAW_SIZE (regnum)); |
144 | |
145 | REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer); |
146 | val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum)); |
147 | bcopy (virtual_buffer, VALUE_CONTENTS (val), REGISTER_VIRTUAL_SIZE (regnum)); |
148 | VALUE_LVAL (val) = addr ? lval_memory : lval_register; |
149 | VALUE_ADDRESS (val) = addr ? addr : REGISTER_BYTE (regnum); |
150 | VALUE_REGNO (val) = regnum; |
151 | return val; |
152 | } |
153 | \f |
154 | /* Low level examining and depositing of registers. |
155 | |
156 | Note that you must call `fetch_registers' once |
157 | before examining or depositing any registers. */ |
158 | |
159 | char registers[REGISTER_BYTES]; |
160 | |
161 | /* Copy LEN bytes of consecutive data from registers |
162 | starting with the REGBYTE'th byte of register data |
163 | into memory at MYADDR. */ |
164 | |
e91b87a3 |
165 | void |
7b4ac7e1 |
166 | read_register_bytes (regbyte, myaddr, len) |
167 | int regbyte; |
168 | char *myaddr; |
169 | int len; |
170 | { |
171 | bcopy (®isters[regbyte], myaddr, len); |
172 | } |
173 | |
174 | /* Copy LEN bytes of consecutive data from memory at MYADDR |
175 | into registers starting with the REGBYTE'th byte of register data. */ |
176 | |
e91b87a3 |
177 | void |
7b4ac7e1 |
178 | write_register_bytes (regbyte, myaddr, len) |
179 | int regbyte; |
180 | char *myaddr; |
181 | int len; |
182 | { |
183 | bcopy (myaddr, ®isters[regbyte], len); |
184 | if (have_inferior_p ()) |
185 | store_inferior_registers (-1); |
186 | } |
187 | |
188 | /* Return the contents of register REGNO, |
189 | regarding it as an integer. */ |
190 | |
191 | CORE_ADDR |
192 | read_register (regno) |
193 | int regno; |
194 | { |
195 | /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */ |
196 | return *(int *) ®isters[REGISTER_BYTE (regno)]; |
197 | } |
198 | |
199 | /* Store VALUE in the register number REGNO, regarded as an integer. */ |
200 | |
201 | void |
202 | write_register (regno, val) |
203 | int regno, val; |
204 | { |
205 | /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */ |
bb7592f0 |
206 | #if defined(sun4) |
207 | /* This is a no-op on a Sun 4. */ |
208 | if (regno == 0) |
209 | return; |
210 | #endif |
211 | |
7b4ac7e1 |
212 | *(int *) ®isters[REGISTER_BYTE (regno)] = val; |
213 | |
214 | if (have_inferior_p ()) |
215 | store_inferior_registers (regno); |
216 | } |
217 | |
218 | /* Record that register REGNO contains VAL. |
219 | This is used when the value is obtained from the inferior or core dump, |
220 | so there is no need to store the value there. */ |
221 | |
222 | void |
223 | supply_register (regno, val) |
224 | int regno; |
225 | char *val; |
226 | { |
227 | bcopy (val, ®isters[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno)); |
228 | } |
229 | \f |
230 | /* Given a struct symbol for a variable, |
231 | and a stack frame address, read the value of the variable |
232 | and return a (pointer to a) struct value containing the value. */ |
233 | |
234 | value |
235 | read_var_value (var, frame) |
236 | register struct symbol *var; |
237 | FRAME frame; |
238 | { |
239 | register value v; |
240 | |
e91b87a3 |
241 | struct frame_info *fi; |
7b4ac7e1 |
242 | |
243 | struct type *type = SYMBOL_TYPE (var); |
244 | register CORE_ADDR addr = 0; |
245 | int val = SYMBOL_VALUE (var); |
246 | register int len; |
247 | |
7b4ac7e1 |
248 | v = allocate_value (type); |
249 | VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */ |
250 | len = TYPE_LENGTH (type); |
251 | |
252 | if (frame == 0) frame = selected_frame; |
253 | |
254 | switch (SYMBOL_CLASS (var)) |
255 | { |
256 | case LOC_CONST: |
257 | case LOC_LABEL: |
258 | bcopy (&val, VALUE_CONTENTS (v), len); |
259 | VALUE_LVAL (v) = not_lval; |
260 | return v; |
261 | |
262 | case LOC_CONST_BYTES: |
263 | bcopy (val, VALUE_CONTENTS (v), len); |
264 | VALUE_LVAL (v) = not_lval; |
265 | return v; |
266 | |
267 | case LOC_STATIC: |
268 | addr = val; |
269 | break; |
270 | |
271 | case LOC_ARG: |
272 | fi = get_frame_info (frame); |
273 | addr = val + FRAME_ARGS_ADDRESS (fi); |
274 | break; |
275 | |
276 | case LOC_LOCAL: |
277 | fi = get_frame_info (frame); |
278 | addr = val + FRAME_LOCALS_ADDRESS (fi); |
279 | break; |
280 | |
281 | case LOC_TYPEDEF: |
282 | error ("Cannot look up value of a typedef"); |
283 | |
284 | case LOC_BLOCK: |
285 | VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var)); |
286 | return v; |
287 | |
288 | case LOC_REGISTER: |
bb7592f0 |
289 | case LOC_REGPARM: |
e91b87a3 |
290 | v = value_from_register (type, val, frame); |
291 | return v; |
7b4ac7e1 |
292 | } |
293 | |
294 | read_memory (addr, VALUE_CONTENTS (v), len); |
295 | VALUE_ADDRESS (v) = addr; |
296 | return v; |
297 | } |
e91b87a3 |
298 | |
299 | /* Return a value of type TYPE, stored in register REGNUM, in frame |
300 | FRAME. */ |
301 | |
302 | value |
303 | value_from_register (type, regnum, frame) |
304 | struct type *type; |
305 | int regnum; |
306 | FRAME frame; |
307 | { |
308 | char raw_buffer [MAX_REGISTER_RAW_SIZE]; |
309 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; |
310 | CORE_ADDR addr; |
311 | value v = allocate_value (type); |
312 | int len = TYPE_LENGTH (type); |
313 | char *value_bytes = 0; |
314 | int value_bytes_copied = 0; |
315 | int num_storage_locs; |
316 | |
317 | VALUE_REGNO (v) = regnum; |
318 | |
319 | num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ? |
320 | ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 : |
321 | 1); |
322 | |
323 | if (num_storage_locs > 1) |
324 | { |
325 | /* Value spread across multiple storage locations. */ |
326 | |
327 | int local_regnum; |
328 | int mem_stor = 0, reg_stor = 0; |
329 | int mem_tracking = 1; |
330 | CORE_ADDR last_addr = 0; |
331 | |
332 | value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE); |
333 | |
334 | /* Copy all of the data out, whereever it may be. */ |
335 | |
336 | for (local_regnum = regnum; |
337 | value_bytes_copied < len; |
338 | (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum), |
339 | ++local_regnum)) |
340 | { |
341 | int register_index = local_regnum - regnum; |
342 | addr = find_saved_register (frame, local_regnum); |
343 | if (addr == 0) |
344 | { |
345 | read_register_bytes (REGISTER_BYTE (local_regnum), |
346 | value_bytes + value_bytes_copied, |
347 | REGISTER_RAW_SIZE (local_regnum)); |
348 | reg_stor++; |
349 | } |
350 | else |
351 | { |
352 | read_memory (addr, value_bytes + value_bytes_copied, |
353 | REGISTER_RAW_SIZE (local_regnum)); |
354 | mem_stor++; |
355 | mem_tracking = |
356 | (mem_tracking |
357 | && (regnum == local_regnum |
358 | || addr == last_addr)); |
359 | } |
360 | last_addr = addr; |
361 | } |
362 | |
363 | if ((reg_stor && mem_stor) |
364 | || (mem_stor && !mem_tracking)) |
365 | /* Mixed storage; all of the hassle we just went through was |
366 | for some good purpose. */ |
367 | { |
368 | VALUE_LVAL (v) = lval_reg_frame_relative; |
369 | VALUE_FRAME (v) = FRAME_FP (frame); |
370 | VALUE_FRAME_REGNUM (v) = regnum; |
371 | } |
372 | else if (mem_stor) |
373 | { |
374 | VALUE_LVAL (v) = lval_memory; |
375 | VALUE_ADDRESS (v) = find_saved_register (frame, regnum); |
376 | } |
377 | else if (reg_stor) |
378 | { |
379 | VALUE_LVAL (v) = lval_register; |
380 | VALUE_ADDRESS (v) = REGISTER_BYTE (regnum); |
381 | } |
382 | else |
383 | fatal ("value_from_register: Value not stored anywhere!"); |
384 | |
385 | /* Any structure stored in more than one register will always be |
386 | an inegral number of registers. Otherwise, you'd need to do |
387 | some fiddling with the last register copied here for little |
388 | endian machines. */ |
389 | |
390 | /* Copy into the contents section of the value. */ |
391 | bcopy (value_bytes, VALUE_CONTENTS (v), len); |
392 | |
393 | return v; |
394 | } |
395 | |
396 | /* Data is completely contained within a single register. Locate the |
397 | register's contents in a real register or in core; |
398 | read the data in raw format. */ |
399 | |
400 | addr = find_saved_register (frame, regnum); |
401 | if (addr == 0) |
402 | { |
403 | /* Value is really in a register. */ |
404 | |
405 | VALUE_LVAL (v) = lval_register; |
406 | VALUE_ADDRESS (v) = REGISTER_BYTE (regnum); |
407 | |
408 | read_register_bytes (REGISTER_BYTE (regnum), |
409 | raw_buffer, REGISTER_RAW_SIZE (regnum)); |
410 | } |
411 | else |
412 | { |
413 | /* Value was in a register that has been saved in memory. */ |
414 | |
415 | read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum)); |
416 | VALUE_LVAL (v) = lval_memory; |
417 | VALUE_ADDRESS (v) = addr; |
418 | } |
419 | |
420 | /* Convert the raw contents to virtual contents. |
421 | (Just copy them if the formats are the same.) */ |
422 | |
423 | REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer); |
424 | |
425 | if (REGISTER_CONVERTIBLE (regnum)) |
426 | { |
427 | /* When the raw and virtual formats differ, the virtual format |
428 | corresponds to a specific data type. If we want that type, |
429 | copy the data into the value. |
430 | Otherwise, do a type-conversion. */ |
431 | |
432 | if (type != REGISTER_VIRTUAL_TYPE (regnum)) |
433 | { |
434 | /* eg a variable of type `float' in a 68881 register |
435 | with raw type `extended' and virtual type `double'. |
436 | Fetch it as a `double' and then convert to `float'. */ |
437 | v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum)); |
438 | bcopy (virtual_buffer, VALUE_CONTENTS (v), len); |
439 | v = value_cast (type, v); |
440 | } |
441 | else |
442 | bcopy (virtual_buffer, VALUE_CONTENTS (v), len); |
443 | } |
444 | else |
445 | { |
446 | /* Raw and virtual formats are the same for this register. */ |
447 | |
448 | #ifdef BYTES_BIG_ENDIAN |
449 | if (len < REGISTER_RAW_SIZE (regnum)) |
450 | { |
451 | /* Big-endian, and we want less than full size. */ |
452 | VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len; |
453 | } |
454 | #endif |
455 | |
456 | bcopy (virtual_buffer + VALUE_OFFSET (v), |
457 | VALUE_CONTENTS (v), len); |
458 | } |
459 | |
460 | return v; |
461 | } |
7b4ac7e1 |
462 | \f |
463 | /* Given a struct symbol for a variable, |
464 | and a stack frame address, |
465 | return a (pointer to a) struct value containing the variable's address. */ |
466 | |
467 | value |
468 | locate_var_value (var, frame) |
469 | register struct symbol *var; |
470 | FRAME frame; |
471 | { |
472 | register CORE_ADDR addr = 0; |
473 | int val = SYMBOL_VALUE (var); |
e91b87a3 |
474 | struct frame_info *fi; |
3bf57d21 |
475 | struct type *type = SYMBOL_TYPE (var); |
7b4ac7e1 |
476 | |
477 | if (frame == 0) frame = selected_frame; |
478 | |
479 | switch (SYMBOL_CLASS (var)) |
480 | { |
481 | case LOC_CONST: |
482 | case LOC_CONST_BYTES: |
483 | error ("Address requested for identifier \"%s\" which is a constant.", |
484 | SYMBOL_NAME (var)); |
485 | |
486 | case LOC_REGISTER: |
bb7592f0 |
487 | case LOC_REGPARM: |
7b4ac7e1 |
488 | addr = find_saved_register (frame, val); |
489 | if (addr != 0) |
490 | { |
3bf57d21 |
491 | int len = TYPE_LENGTH (type); |
e91b87a3 |
492 | #ifdef BYTES_BIG_ENDIAN |
493 | if (len < REGISTER_RAW_SIZE (val)) |
7b4ac7e1 |
494 | /* Big-endian, and we want less than full size. */ |
632ea0cc |
495 | addr += REGISTER_RAW_SIZE (val) - len; |
e91b87a3 |
496 | #endif |
7b4ac7e1 |
497 | break; |
498 | } |
499 | error ("Address requested for identifier \"%s\" which is in a register.", |
500 | SYMBOL_NAME (var)); |
501 | |
502 | case LOC_STATIC: |
503 | case LOC_LABEL: |
504 | addr = val; |
505 | break; |
506 | |
507 | case LOC_ARG: |
508 | fi = get_frame_info (frame); |
509 | addr = val + FRAME_ARGS_ADDRESS (fi); |
510 | break; |
511 | |
512 | case LOC_LOCAL: |
513 | fi = get_frame_info (frame); |
514 | addr = val + FRAME_LOCALS_ADDRESS (fi); |
515 | break; |
516 | |
517 | case LOC_TYPEDEF: |
518 | error ("Address requested for identifier \"%s\" which is a typedef.", |
519 | SYMBOL_NAME (var)); |
520 | |
521 | case LOC_BLOCK: |
522 | addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var)); |
523 | break; |
524 | } |
525 | |
3bf57d21 |
526 | return value_cast (lookup_pointer_type (type), |
e91b87a3 |
527 | value_from_long (builtin_type_long, (LONGEST) addr)); |
7b4ac7e1 |
528 | } |
529 | |