GDB (API)
|
00001 /* Prologue value handling for GDB. 00002 Copyright (C) 2003-2013 Free Software Foundation, Inc. 00003 00004 This file is part of GDB. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00018 00019 #include "defs.h" 00020 #include "gdb_string.h" 00021 #include "gdb_assert.h" 00022 #include "prologue-value.h" 00023 #include "regcache.h" 00024 00025 00026 /* Constructors. */ 00027 00028 pv_t 00029 pv_unknown (void) 00030 { 00031 pv_t v = { pvk_unknown, 0, 0 }; 00032 00033 return v; 00034 } 00035 00036 00037 pv_t 00038 pv_constant (CORE_ADDR k) 00039 { 00040 pv_t v; 00041 00042 v.kind = pvk_constant; 00043 v.reg = -1; /* for debugging */ 00044 v.k = k; 00045 00046 return v; 00047 } 00048 00049 00050 pv_t 00051 pv_register (int reg, CORE_ADDR k) 00052 { 00053 pv_t v; 00054 00055 v.kind = pvk_register; 00056 v.reg = reg; 00057 v.k = k; 00058 00059 return v; 00060 } 00061 00062 00063 00064 /* Arithmetic operations. */ 00065 00066 /* If one of *A and *B is a constant, and the other isn't, swap the 00067 values as necessary to ensure that *B is the constant. This can 00068 reduce the number of cases we need to analyze in the functions 00069 below. */ 00070 static void 00071 constant_last (pv_t *a, pv_t *b) 00072 { 00073 if (a->kind == pvk_constant 00074 && b->kind != pvk_constant) 00075 { 00076 pv_t temp = *a; 00077 *a = *b; 00078 *b = temp; 00079 } 00080 } 00081 00082 00083 pv_t 00084 pv_add (pv_t a, pv_t b) 00085 { 00086 constant_last (&a, &b); 00087 00088 /* We can add a constant to a register. */ 00089 if (a.kind == pvk_register 00090 && b.kind == pvk_constant) 00091 return pv_register (a.reg, a.k + b.k); 00092 00093 /* We can add a constant to another constant. */ 00094 else if (a.kind == pvk_constant 00095 && b.kind == pvk_constant) 00096 return pv_constant (a.k + b.k); 00097 00098 /* Anything else we don't know how to add. We don't have a 00099 representation for, say, the sum of two registers, or a multiple 00100 of a register's value (adding a register to itself). */ 00101 else 00102 return pv_unknown (); 00103 } 00104 00105 00106 pv_t 00107 pv_add_constant (pv_t v, CORE_ADDR k) 00108 { 00109 /* Rather than thinking of all the cases we can and can't handle, 00110 we'll just let pv_add take care of that for us. */ 00111 return pv_add (v, pv_constant (k)); 00112 } 00113 00114 00115 pv_t 00116 pv_subtract (pv_t a, pv_t b) 00117 { 00118 /* This isn't quite the same as negating B and adding it to A, since 00119 we don't have a representation for the negation of anything but a 00120 constant. For example, we can't negate { pvk_register, R1, 10 }, 00121 but we do know that { pvk_register, R1, 10 } minus { pvk_register, 00122 R1, 5 } is { pvk_constant, <ignored>, 5 }. 00123 00124 This means, for example, that we could subtract two stack 00125 addresses; they're both relative to the original SP. Since the 00126 frame pointer is set based on the SP, its value will be the 00127 original SP plus some constant (probably zero), so we can use its 00128 value just fine, too. */ 00129 00130 constant_last (&a, &b); 00131 00132 /* We can subtract two constants. */ 00133 if (a.kind == pvk_constant 00134 && b.kind == pvk_constant) 00135 return pv_constant (a.k - b.k); 00136 00137 /* We can subtract a constant from a register. */ 00138 else if (a.kind == pvk_register 00139 && b.kind == pvk_constant) 00140 return pv_register (a.reg, a.k - b.k); 00141 00142 /* We can subtract a register from itself, yielding a constant. */ 00143 else if (a.kind == pvk_register 00144 && b.kind == pvk_register 00145 && a.reg == b.reg) 00146 return pv_constant (a.k - b.k); 00147 00148 /* We don't know how to subtract anything else. */ 00149 else 00150 return pv_unknown (); 00151 } 00152 00153 00154 pv_t 00155 pv_logical_and (pv_t a, pv_t b) 00156 { 00157 constant_last (&a, &b); 00158 00159 /* We can 'and' two constants. */ 00160 if (a.kind == pvk_constant 00161 && b.kind == pvk_constant) 00162 return pv_constant (a.k & b.k); 00163 00164 /* We can 'and' anything with the constant zero. */ 00165 else if (b.kind == pvk_constant 00166 && b.k == 0) 00167 return pv_constant (0); 00168 00169 /* We can 'and' anything with ~0. */ 00170 else if (b.kind == pvk_constant 00171 && b.k == ~ (CORE_ADDR) 0) 00172 return a; 00173 00174 /* We can 'and' a register with itself. */ 00175 else if (a.kind == pvk_register 00176 && b.kind == pvk_register 00177 && a.reg == b.reg 00178 && a.k == b.k) 00179 return a; 00180 00181 /* Otherwise, we don't know. */ 00182 else 00183 return pv_unknown (); 00184 } 00185 00186 00187 00188 /* Examining prologue values. */ 00189 00190 int 00191 pv_is_identical (pv_t a, pv_t b) 00192 { 00193 if (a.kind != b.kind) 00194 return 0; 00195 00196 switch (a.kind) 00197 { 00198 case pvk_unknown: 00199 return 1; 00200 case pvk_constant: 00201 return (a.k == b.k); 00202 case pvk_register: 00203 return (a.reg == b.reg && a.k == b.k); 00204 default: 00205 gdb_assert_not_reached ("unexpected prologue value kind"); 00206 } 00207 } 00208 00209 00210 int 00211 pv_is_constant (pv_t a) 00212 { 00213 return (a.kind == pvk_constant); 00214 } 00215 00216 00217 int 00218 pv_is_register (pv_t a, int r) 00219 { 00220 return (a.kind == pvk_register 00221 && a.reg == r); 00222 } 00223 00224 00225 int 00226 pv_is_register_k (pv_t a, int r, CORE_ADDR k) 00227 { 00228 return (a.kind == pvk_register 00229 && a.reg == r 00230 && a.k == k); 00231 } 00232 00233 00234 enum pv_boolean 00235 pv_is_array_ref (pv_t addr, CORE_ADDR size, 00236 pv_t array_addr, CORE_ADDR array_len, 00237 CORE_ADDR elt_size, 00238 int *i) 00239 { 00240 /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if 00241 addr is *before* the start of the array, then this isn't going to 00242 be negative... */ 00243 pv_t offset = pv_subtract (addr, array_addr); 00244 00245 if (offset.kind == pvk_constant) 00246 { 00247 /* This is a rather odd test. We want to know if the SIZE bytes 00248 at ADDR don't overlap the array at all, so you'd expect it to 00249 be an || expression: "if we're completely before || we're 00250 completely after". But with unsigned arithmetic, things are 00251 different: since it's a number circle, not a number line, the 00252 right values for offset.k are actually one contiguous range. */ 00253 if (offset.k <= -size 00254 && offset.k >= array_len * elt_size) 00255 return pv_definite_no; 00256 else if (offset.k % elt_size != 0 00257 || size != elt_size) 00258 return pv_maybe; 00259 else 00260 { 00261 *i = offset.k / elt_size; 00262 return pv_definite_yes; 00263 } 00264 } 00265 else 00266 return pv_maybe; 00267 } 00268 00269 00270 00271 /* Areas. */ 00272 00273 00274 /* A particular value known to be stored in an area. 00275 00276 Entries form a ring, sorted by unsigned offset from the area's base 00277 register's value. Since entries can straddle the wrap-around point, 00278 unsigned offsets form a circle, not a number line, so the list 00279 itself is structured the same way --- there is no inherent head. 00280 The entry with the lowest offset simply follows the entry with the 00281 highest offset. Entries may abut, but never overlap. The area's 00282 'entry' pointer points to an arbitrary node in the ring. */ 00283 struct area_entry 00284 { 00285 /* Links in the doubly-linked ring. */ 00286 struct area_entry *prev, *next; 00287 00288 /* Offset of this entry's address from the value of the base 00289 register. */ 00290 CORE_ADDR offset; 00291 00292 /* The size of this entry. Note that an entry may wrap around from 00293 the end of the address space to the beginning. */ 00294 CORE_ADDR size; 00295 00296 /* The value stored here. */ 00297 pv_t value; 00298 }; 00299 00300 00301 struct pv_area 00302 { 00303 /* This area's base register. */ 00304 int base_reg; 00305 00306 /* The mask to apply to addresses, to make the wrap-around happen at 00307 the right place. */ 00308 CORE_ADDR addr_mask; 00309 00310 /* An element of the doubly-linked ring of entries, or zero if we 00311 have none. */ 00312 struct area_entry *entry; 00313 }; 00314 00315 00316 struct pv_area * 00317 make_pv_area (int base_reg, int addr_bit) 00318 { 00319 struct pv_area *a = (struct pv_area *) xmalloc (sizeof (*a)); 00320 00321 memset (a, 0, sizeof (*a)); 00322 00323 a->base_reg = base_reg; 00324 a->entry = 0; 00325 00326 /* Remember that shift amounts equal to the type's width are 00327 undefined. */ 00328 a->addr_mask = ((((CORE_ADDR) 1 << (addr_bit - 1)) - 1) << 1) | 1; 00329 00330 return a; 00331 } 00332 00333 00334 /* Delete all entries from AREA. */ 00335 static void 00336 clear_entries (struct pv_area *area) 00337 { 00338 struct area_entry *e = area->entry; 00339 00340 if (e) 00341 { 00342 /* This needs to be a do-while loop, in order to actually 00343 process the node being checked for in the terminating 00344 condition. */ 00345 do 00346 { 00347 struct area_entry *next = e->next; 00348 00349 xfree (e); 00350 e = next; 00351 } 00352 while (e != area->entry); 00353 00354 area->entry = 0; 00355 } 00356 } 00357 00358 00359 void 00360 free_pv_area (struct pv_area *area) 00361 { 00362 clear_entries (area); 00363 xfree (area); 00364 } 00365 00366 00367 static void 00368 do_free_pv_area_cleanup (void *arg) 00369 { 00370 free_pv_area ((struct pv_area *) arg); 00371 } 00372 00373 00374 struct cleanup * 00375 make_cleanup_free_pv_area (struct pv_area *area) 00376 { 00377 return make_cleanup (do_free_pv_area_cleanup, (void *) area); 00378 } 00379 00380 00381 int 00382 pv_area_store_would_trash (struct pv_area *area, pv_t addr) 00383 { 00384 /* It may seem odd that pvk_constant appears here --- after all, 00385 that's the case where we know the most about the address! But 00386 pv_areas are always relative to a register, and we don't know the 00387 value of the register, so we can't compare entry addresses to 00388 constants. */ 00389 return (addr.kind == pvk_unknown 00390 || addr.kind == pvk_constant 00391 || (addr.kind == pvk_register && addr.reg != area->base_reg)); 00392 } 00393 00394 00395 /* Return a pointer to the first entry we hit in AREA starting at 00396 OFFSET and going forward. 00397 00398 This may return zero, if AREA has no entries. 00399 00400 And since the entries are a ring, this may return an entry that 00401 entirely precedes OFFSET. This is the correct behavior: depending 00402 on the sizes involved, we could still overlap such an area, with 00403 wrap-around. */ 00404 static struct area_entry * 00405 find_entry (struct pv_area *area, CORE_ADDR offset) 00406 { 00407 struct area_entry *e = area->entry; 00408 00409 if (! e) 00410 return 0; 00411 00412 /* If the next entry would be better than the current one, then scan 00413 forward. Since we use '<' in this loop, it always terminates. 00414 00415 Note that, even setting aside the addr_mask stuff, we must not 00416 simplify this, in high school algebra fashion, to 00417 (e->next->offset < e->offset), because of the way < interacts 00418 with wrap-around. We have to subtract offset from both sides to 00419 make sure both things we're comparing are on the same side of the 00420 discontinuity. */ 00421 while (((e->next->offset - offset) & area->addr_mask) 00422 < ((e->offset - offset) & area->addr_mask)) 00423 e = e->next; 00424 00425 /* If the previous entry would be better than the current one, then 00426 scan backwards. */ 00427 while (((e->prev->offset - offset) & area->addr_mask) 00428 < ((e->offset - offset) & area->addr_mask)) 00429 e = e->prev; 00430 00431 /* In case there's some locality to the searches, set the area's 00432 pointer to the entry we've found. */ 00433 area->entry = e; 00434 00435 return e; 00436 } 00437 00438 00439 /* Return non-zero if the SIZE bytes at OFFSET would overlap ENTRY; 00440 return zero otherwise. AREA is the area to which ENTRY belongs. */ 00441 static int 00442 overlaps (struct pv_area *area, 00443 struct area_entry *entry, 00444 CORE_ADDR offset, 00445 CORE_ADDR size) 00446 { 00447 /* Think carefully about wrap-around before simplifying this. */ 00448 return (((entry->offset - offset) & area->addr_mask) < size 00449 || ((offset - entry->offset) & area->addr_mask) < entry->size); 00450 } 00451 00452 00453 void 00454 pv_area_store (struct pv_area *area, 00455 pv_t addr, 00456 CORE_ADDR size, 00457 pv_t value) 00458 { 00459 /* Remove any (potentially) overlapping entries. */ 00460 if (pv_area_store_would_trash (area, addr)) 00461 clear_entries (area); 00462 else 00463 { 00464 CORE_ADDR offset = addr.k; 00465 struct area_entry *e = find_entry (area, offset); 00466 00467 /* Delete all entries that we would overlap. */ 00468 while (e && overlaps (area, e, offset, size)) 00469 { 00470 struct area_entry *next = (e->next == e) ? 0 : e->next; 00471 00472 e->prev->next = e->next; 00473 e->next->prev = e->prev; 00474 00475 xfree (e); 00476 e = next; 00477 } 00478 00479 /* Move the area's pointer to the next remaining entry. This 00480 will also zero the pointer if we've deleted all the entries. */ 00481 area->entry = e; 00482 } 00483 00484 /* Now, there are no entries overlapping us, and area->entry is 00485 either zero or pointing at the closest entry after us. We can 00486 just insert ourselves before that. 00487 00488 But if we're storing an unknown value, don't bother --- that's 00489 the default. */ 00490 if (value.kind == pvk_unknown) 00491 return; 00492 else 00493 { 00494 CORE_ADDR offset = addr.k; 00495 struct area_entry *e = (struct area_entry *) xmalloc (sizeof (*e)); 00496 00497 e->offset = offset; 00498 e->size = size; 00499 e->value = value; 00500 00501 if (area->entry) 00502 { 00503 e->prev = area->entry->prev; 00504 e->next = area->entry; 00505 e->prev->next = e->next->prev = e; 00506 } 00507 else 00508 { 00509 e->prev = e->next = e; 00510 area->entry = e; 00511 } 00512 } 00513 } 00514 00515 00516 pv_t 00517 pv_area_fetch (struct pv_area *area, pv_t addr, CORE_ADDR size) 00518 { 00519 /* If we have no entries, or we can't decide how ADDR relates to the 00520 entries we do have, then the value is unknown. */ 00521 if (! area->entry 00522 || pv_area_store_would_trash (area, addr)) 00523 return pv_unknown (); 00524 else 00525 { 00526 CORE_ADDR offset = addr.k; 00527 struct area_entry *e = find_entry (area, offset); 00528 00529 /* If this entry exactly matches what we're looking for, then 00530 we're set. Otherwise, say it's unknown. */ 00531 if (e->offset == offset && e->size == size) 00532 return e->value; 00533 else 00534 return pv_unknown (); 00535 } 00536 } 00537 00538 00539 int 00540 pv_area_find_reg (struct pv_area *area, 00541 struct gdbarch *gdbarch, 00542 int reg, 00543 CORE_ADDR *offset_p) 00544 { 00545 struct area_entry *e = area->entry; 00546 00547 if (e) 00548 do 00549 { 00550 if (e->value.kind == pvk_register 00551 && e->value.reg == reg 00552 && e->value.k == 0 00553 && e->size == register_size (gdbarch, reg)) 00554 { 00555 if (offset_p) 00556 *offset_p = e->offset; 00557 return 1; 00558 } 00559 00560 e = e->next; 00561 } 00562 while (e != area->entry); 00563 00564 return 0; 00565 } 00566 00567 00568 void 00569 pv_area_scan (struct pv_area *area, 00570 void (*func) (void *closure, 00571 pv_t addr, 00572 CORE_ADDR size, 00573 pv_t value), 00574 void *closure) 00575 { 00576 struct area_entry *e = area->entry; 00577 pv_t addr; 00578 00579 addr.kind = pvk_register; 00580 addr.reg = area->base_reg; 00581 00582 if (e) 00583 do 00584 { 00585 addr.k = e->offset; 00586 func (closure, addr, e->size, e->value); 00587 e = e->next; 00588 } 00589 while (e != area->entry); 00590 }