xref: /dflybsd-src/contrib/gdb-7/gdb/prologue-value.h (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Interface to prologue value handling for GDB.
2*ef5ccd6cSJohn Marino    Copyright (C) 2003-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    This file is part of GDB.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert    (at your option) any later version.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145796c8dcSSimon Schubert    GNU General Public License for more details.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert #ifndef PROLOGUE_VALUE_H
205796c8dcSSimon Schubert #define PROLOGUE_VALUE_H
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert /* When we analyze a prologue, we're really doing 'abstract
235796c8dcSSimon Schubert    interpretation' or 'pseudo-evaluation': running the function's code
245796c8dcSSimon Schubert    in simulation, but using conservative approximations of the values
255796c8dcSSimon Schubert    it would have when it actually runs.  For example, if our function
265796c8dcSSimon Schubert    starts with the instruction:
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert       addi r1, 42     # add 42 to r1
295796c8dcSSimon Schubert 
305796c8dcSSimon Schubert    we don't know exactly what value will be in r1 after executing this
315796c8dcSSimon Schubert    instruction, but we do know it'll be 42 greater than its original
325796c8dcSSimon Schubert    value.
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert    If we then see an instruction like:
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert       addi r1, 22     # add 22 to r1
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert    we still don't know what r1's value is, but again, we can say it is
395796c8dcSSimon Schubert    now 64 greater than its original value.
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert    If the next instruction were:
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert       mov r2, r1      # set r2 to r1's value
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert    then we can say that r2's value is now the original value of r1
465796c8dcSSimon Schubert    plus 64.
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert    It's common for prologues to save registers on the stack, so we'll
495796c8dcSSimon Schubert    need to track the values of stack frame slots, as well as the
505796c8dcSSimon Schubert    registers.  So after an instruction like this:
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert       mov (fp+4), r2
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert    then we'd know that the stack slot four bytes above the frame
555796c8dcSSimon Schubert    pointer holds the original value of r1 plus 64.
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert    And so on.
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert    Of course, this can only go so far before it gets unreasonable.  If
605796c8dcSSimon Schubert    we wanted to be able to say anything about the value of r1 after
615796c8dcSSimon Schubert    the instruction:
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert       xor r1, r3      # exclusive-or r1 and r3, place result in r1
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert    then things would get pretty complex.  But remember, we're just
665796c8dcSSimon Schubert    doing a conservative approximation; if exclusive-or instructions
675796c8dcSSimon Schubert    aren't relevant to prologues, we can just say r1's value is now
685796c8dcSSimon Schubert    'unknown'.  We can ignore things that are too complex, if that loss
695796c8dcSSimon Schubert    of information is acceptable for our application.
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert    So when I say "conservative approximation" here, what I mean is an
725796c8dcSSimon Schubert    approximation that is either accurate, or marked "unknown", but
735796c8dcSSimon Schubert    never inaccurate.
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert    Once you've reached the current PC, or an instruction that you
765796c8dcSSimon Schubert    don't know how to simulate, you stop.  Now you can examine the
775796c8dcSSimon Schubert    state of the registers and stack slots you've kept track of.
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert    - To see how large your stack frame is, just check the value of the
805796c8dcSSimon Schubert      stack pointer register; if it's the original value of the SP
815796c8dcSSimon Schubert      minus a constant, then that constant is the stack frame's size.
825796c8dcSSimon Schubert      If the SP's value has been marked as 'unknown', then that means
835796c8dcSSimon Schubert      the prologue has done something too complex for us to track, and
845796c8dcSSimon Schubert      we don't know the frame size.
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert    - To see where we've saved the previous frame's registers, we just
875796c8dcSSimon Schubert      search the values we've tracked --- stack slots, usually, but
885796c8dcSSimon Schubert      registers, too, if you want --- for something equal to the
895796c8dcSSimon Schubert      register's original value.  If the ABI suggests a standard place
905796c8dcSSimon Schubert      to save a given register, then we can check there first, but
915796c8dcSSimon Schubert      really, anything that will get us back the original value will
925796c8dcSSimon Schubert      probably work.
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert    Sure, this takes some work.  But prologue analyzers aren't
955796c8dcSSimon Schubert    quick-and-simple pattern patching to recognize a few fixed prologue
965796c8dcSSimon Schubert    forms any more; they're big, hairy functions.  Along with inferior
975796c8dcSSimon Schubert    function calls, prologue analysis accounts for a substantial
985796c8dcSSimon Schubert    portion of the time needed to stabilize a GDB port.  So I think
995796c8dcSSimon Schubert    it's worthwhile to look for an approach that will be easier to
1005796c8dcSSimon Schubert    understand and maintain.  In the approach used here:
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert    - It's easier to see that the analyzer is correct: you just see
1035796c8dcSSimon Schubert      whether the analyzer properly (albiet conservatively) simulates
1045796c8dcSSimon Schubert      the effect of each instruction.
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert    - It's easier to extend the analyzer: you can add support for new
1075796c8dcSSimon Schubert      instructions, and know that you haven't broken anything that
1085796c8dcSSimon Schubert      wasn't already broken before.
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert    - It's orthogonal: to gather new information, you don't need to
1115796c8dcSSimon Schubert      complicate the code for each instruction.  As long as your domain
1125796c8dcSSimon Schubert      of conservative values is already detailed enough to tell you
1135796c8dcSSimon Schubert      what you need, then all the existing instruction simulations are
1145796c8dcSSimon Schubert      already gathering the right data for you.
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert    A 'struct prologue_value' is a conservative approximation of the
1175796c8dcSSimon Schubert    real value the register or stack slot will have.  */
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert struct prologue_value {
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert   /* What sort of value is this?  This determines the interpretation
1225796c8dcSSimon Schubert      of subsequent fields.  */
1235796c8dcSSimon Schubert   enum {
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert     /* We don't know anything about the value.  This is also used for
1265796c8dcSSimon Schubert        values we could have kept track of, when doing so would have
1275796c8dcSSimon Schubert        been too complex and we don't want to bother.  The bottom of
1285796c8dcSSimon Schubert        our lattice.  */
1295796c8dcSSimon Schubert     pvk_unknown,
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert     /* A known constant.  K is its value.  */
1325796c8dcSSimon Schubert     pvk_constant,
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert     /* The value that register REG originally had *UPON ENTRY TO THE
1355796c8dcSSimon Schubert        FUNCTION*, plus K.  If K is zero, this means, obviously, just
1365796c8dcSSimon Schubert        the value REG had upon entry to the function.  REG is a GDB
1375796c8dcSSimon Schubert        register number.  Before we start interpreting, we initialize
1385796c8dcSSimon Schubert        every register R to { pvk_register, R, 0 }.  */
1395796c8dcSSimon Schubert     pvk_register,
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert   } kind;
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert   /* The meanings of the following fields depend on 'kind'; see the
1445796c8dcSSimon Schubert      comments for the specific 'kind' values.  */
1455796c8dcSSimon Schubert   int reg;
1465796c8dcSSimon Schubert   CORE_ADDR k;
1475796c8dcSSimon Schubert };
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert typedef struct prologue_value pv_t;
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert /* Return the unknown prologue value --- { pvk_unknown, ?, ? }.  */
1535796c8dcSSimon Schubert pv_t pv_unknown (void);
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert /* Return the prologue value representing the constant K.  */
1565796c8dcSSimon Schubert pv_t pv_constant (CORE_ADDR k);
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert /* Return the prologue value representing the original value of
1595796c8dcSSimon Schubert    register REG, plus the constant K.  */
1605796c8dcSSimon Schubert pv_t pv_register (int reg, CORE_ADDR k);
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert /* Return conservative approximations of the results of the following
1645796c8dcSSimon Schubert    operations.  */
1655796c8dcSSimon Schubert pv_t pv_add (pv_t a, pv_t b);               /* a + b */
1665796c8dcSSimon Schubert pv_t pv_add_constant (pv_t v, CORE_ADDR k); /* a + k */
1675796c8dcSSimon Schubert pv_t pv_subtract (pv_t a, pv_t b);          /* a - b */
1685796c8dcSSimon Schubert pv_t pv_logical_and (pv_t a, pv_t b);       /* a & b */
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* Return non-zero iff A and B are identical expressions.
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert    This is not the same as asking if the two values are equal; the
1745796c8dcSSimon Schubert    result of such a comparison would have to be a pv_boolean, and
1755796c8dcSSimon Schubert    asking whether two 'unknown' values were equal would give you
1765796c8dcSSimon Schubert    pv_maybe.  Same for comparing, say, { pvk_register, R1, 0 } and {
1775796c8dcSSimon Schubert    pvk_register, R2, 0}.
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert    Instead, this function asks whether the two representations are the
1805796c8dcSSimon Schubert    same.  */
1815796c8dcSSimon Schubert int pv_is_identical (pv_t a, pv_t b);
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert /* Return non-zero if A is known to be a constant.  */
1855796c8dcSSimon Schubert int pv_is_constant (pv_t a);
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert /* Return non-zero if A is the original value of register number R
1885796c8dcSSimon Schubert    plus some constant, zero otherwise.  */
1895796c8dcSSimon Schubert int pv_is_register (pv_t a, int r);
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert /* Return non-zero if A is the original value of register R plus the
1935796c8dcSSimon Schubert    constant K.  */
1945796c8dcSSimon Schubert int pv_is_register_k (pv_t a, int r, CORE_ADDR k);
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert /* A conservative boolean type, including "maybe", when we can't
1975796c8dcSSimon Schubert    figure out whether something is true or not.  */
1985796c8dcSSimon Schubert enum pv_boolean {
1995796c8dcSSimon Schubert   pv_maybe,
2005796c8dcSSimon Schubert   pv_definite_yes,
2015796c8dcSSimon Schubert   pv_definite_no,
2025796c8dcSSimon Schubert };
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert /* Decide whether a reference to SIZE bytes at ADDR refers exactly to
2065796c8dcSSimon Schubert    an element of an array.  The array starts at ARRAY_ADDR, and has
2075796c8dcSSimon Schubert    ARRAY_LEN values of ELT_SIZE bytes each.  If ADDR definitely does
2085796c8dcSSimon Schubert    refer to an array element, set *I to the index of the referenced
2095796c8dcSSimon Schubert    element in the array, and return pv_definite_yes.  If it definitely
2105796c8dcSSimon Schubert    doesn't, return pv_definite_no.  If we can't tell, return pv_maybe.
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert    If the reference does touch the array, but doesn't fall exactly on
2135796c8dcSSimon Schubert    an element boundary, or doesn't refer to the whole element, return
2145796c8dcSSimon Schubert    pv_maybe.  */
2155796c8dcSSimon Schubert enum pv_boolean pv_is_array_ref (pv_t addr, CORE_ADDR size,
2165796c8dcSSimon Schubert                                  pv_t array_addr, CORE_ADDR array_len,
2175796c8dcSSimon Schubert                                  CORE_ADDR elt_size,
2185796c8dcSSimon Schubert                                  int *i);
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert /* A 'struct pv_area' keeps track of values stored in a particular
2225796c8dcSSimon Schubert    region of memory.  */
2235796c8dcSSimon Schubert struct pv_area;
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert /* Create a new area, tracking stores relative to the original value
2265796c8dcSSimon Schubert    of BASE_REG.  If BASE_REG is SP, then this effectively records the
2275796c8dcSSimon Schubert    contents of the stack frame: the original value of the SP is the
2285796c8dcSSimon Schubert    frame's CFA, or some constant offset from it.
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert    Stores to constant addresses, unknown addresses, or to addresses
2315796c8dcSSimon Schubert    relative to registers other than BASE_REG will trash this area; see
2325796c8dcSSimon Schubert    pv_area_store_would_trash.
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert    To check whether a pointer refers to this area, only the low
2355796c8dcSSimon Schubert    ADDR_BIT bits will be compared.  */
2365796c8dcSSimon Schubert struct pv_area *make_pv_area (int base_reg, int addr_bit);
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert /* Free AREA.  */
2395796c8dcSSimon Schubert void free_pv_area (struct pv_area *area);
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert /* Register a cleanup to free AREA.  */
2435796c8dcSSimon Schubert struct cleanup *make_cleanup_free_pv_area (struct pv_area *area);
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert /* Store the SIZE-byte value VALUE at ADDR in AREA.
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert    If ADDR is not relative to the same base register we used in
2495796c8dcSSimon Schubert    creating AREA, then we can't tell which values here the stored
2505796c8dcSSimon Schubert    value might overlap, and we'll have to mark everything as
2515796c8dcSSimon Schubert    unknown.  */
2525796c8dcSSimon Schubert void pv_area_store (struct pv_area *area,
2535796c8dcSSimon Schubert                     pv_t addr,
2545796c8dcSSimon Schubert                     CORE_ADDR size,
2555796c8dcSSimon Schubert                     pv_t value);
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert /* Return the SIZE-byte value at ADDR in AREA.  This may return
2585796c8dcSSimon Schubert    pv_unknown ().  */
2595796c8dcSSimon Schubert pv_t pv_area_fetch (struct pv_area *area, pv_t addr, CORE_ADDR size);
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert /* Return true if storing to address ADDR in AREA would force us to
2625796c8dcSSimon Schubert    mark the contents of the entire area as unknown.  This could happen
2635796c8dcSSimon Schubert    if, say, ADDR is unknown, since we could be storing anywhere.  Or,
2645796c8dcSSimon Schubert    it could happen if ADDR is relative to a different register than
2655796c8dcSSimon Schubert    the other stores base register, since we don't know the relative
2665796c8dcSSimon Schubert    values of the two registers.
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert    If you've reached such a store, it may be better to simply stop the
2695796c8dcSSimon Schubert    prologue analysis, and return the information you've gathered,
2705796c8dcSSimon Schubert    instead of losing all that information, most of which is probably
2715796c8dcSSimon Schubert    okay.  */
2725796c8dcSSimon Schubert int pv_area_store_would_trash (struct pv_area *area, pv_t addr);
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert /* Search AREA for the original value of REGISTER.  If we can't find
2765796c8dcSSimon Schubert    it, return zero; if we can find it, return a non-zero value, and if
2775796c8dcSSimon Schubert    OFFSET_P is non-zero, set *OFFSET_P to the register's offset within
2785796c8dcSSimon Schubert    AREA.  GDBARCH is the architecture of which REGISTER is a member.
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert    In the worst case, this takes time proportional to the number of
2815796c8dcSSimon Schubert    items stored in AREA.  If you plan to gather a lot of information
2825796c8dcSSimon Schubert    about registers saved in AREA, consider calling pv_area_scan
2835796c8dcSSimon Schubert    instead, and collecting all your information in one pass.  */
2845796c8dcSSimon Schubert int pv_area_find_reg (struct pv_area *area,
2855796c8dcSSimon Schubert                       struct gdbarch *gdbarch,
2865796c8dcSSimon Schubert                       int reg,
2875796c8dcSSimon Schubert                       CORE_ADDR *offset_p);
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert /* For every part of AREA whose value we know, apply FUNC to CLOSURE,
2915796c8dcSSimon Schubert    the value's address, its size, and the value itself.  */
2925796c8dcSSimon Schubert void pv_area_scan (struct pv_area *area,
2935796c8dcSSimon Schubert                    void (*func) (void *closure,
2945796c8dcSSimon Schubert                                  pv_t addr,
2955796c8dcSSimon Schubert                                  CORE_ADDR size,
2965796c8dcSSimon Schubert                                  pv_t value),
2975796c8dcSSimon Schubert                    void *closure);
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert 
3005796c8dcSSimon Schubert #endif /* PROLOGUE_VALUE_H */
301