mostang.com!davidm | e91ef29 | 2003-12-10 07:14:38 +0000 | [diff] [blame] | 1 | \documentclass{article} |
| 2 | \usepackage[fancyhdr,pdf]{latex2man} |
mostang.com!davidm | 3434090 | 2003-02-22 08:19:43 +0000 | [diff] [blame] | 3 | |
mostang.com!davidm | e91ef29 | 2003-12-10 07:14:38 +0000 | [diff] [blame] | 4 | \input{common.tex} |
| 5 | |
| 6 | \begin{document} |
| 7 | |
| 8 | \begin{Name}{3}{libunwind-dynamic}{David Mosberger-Tang}{Programming Library}{Introduction to dynamic unwind-info}libunwind-dynamic -- libunwind-support for runtime-generated code |
| 9 | \end{Name} |
| 10 | |
| 11 | \section{Introduction} |
| 12 | |
| 13 | For \Prog{libunwind} to do its work, it needs to be able to |
| 14 | reconstruct the \emph{frame state} of each frame in a call-chain. The |
| 15 | frame state consists of some frame registers (such as the |
| 16 | instruction-pointer and the stack-pointer) and the locations at which |
| 17 | the current values of every callee-saved (``preserved'') resides. |
| 18 | |
| 19 | The purpose of the dynamic unwind-info is therefore to provide |
| 20 | \Prog{libunwind} the minimal information it needs about each |
| 21 | dynamically generated procedure such that it can reconstruct the |
| 22 | procedure's frame state. |
| 23 | |
| 24 | For the purpose of the following discussion, a \emph{procedure} is any |
| 25 | contiguous piece of code. Normally, each procedure directly |
| 26 | corresponds to a function in the source-language but this is not |
| 27 | strictly required. For example, a runtime code-generator could |
| 28 | translate a given function into two separate (discontiguous) |
| 29 | procedures: one for frequently-executed (hot) code and one for |
| 30 | rarely-executed (cold) code. Similarly, simple source-language |
| 31 | functions (usually leaf functions) may get translated into code for |
| 32 | which the default unwind-conventions apply and for such code, no |
| 33 | dynamic unwind info needs to be registered. |
| 34 | |
| 35 | Within a procedure, the code can be thought of as being divided into a |
| 36 | sequence of \emph{regions}. Each region logically consists of an |
| 37 | optional \emph{prologue}, a \emph{body}, and an optional |
| 38 | \emph{epilogue}. If present, the prologue sets up the frame state for |
| 39 | the body, which does the actual work of the procedure. For example, |
| 40 | the prologue may need to allocate a stack-frame and save some |
| 41 | callee-saved registers before the body can start executing. |
| 42 | Correspondingly, the epilogue, if present, restores the previous frame |
| 43 | state and thereby undoes the effect of the prologue. Regions are |
| 44 | nested in the sense that the frame state at the end of a region serves |
| 45 | as the entry-state of the next region. At the end of several nested |
| 46 | regions, there may be a single epilogue which undoes the effect of all |
| 47 | the prologues in the nested regions. |
| 48 | |
| 49 | Even though logically we think of the prologue, body, and epilogue as |
| 50 | separate entities, optimizing code-generators will generally |
| 51 | interleave instructions from all three entities to achieve higher |
| 52 | performance. In fact, as far as the dynamic unwind-info is concerned, |
| 53 | there is no distinction at all between prologue and body. Similarly, |
| 54 | the exact set of instructions that make up an epilogue is also |
| 55 | irrelevant. The only point in the epilogue that needs to be described |
| 56 | explicitly is the point at which the stack-pointer gets restored. The |
| 57 | reason this point needs to be described is that once the stack-pointer |
| 58 | is restored, all values saved in the deallocated portion of the stack |
| 59 | become invalid. All other locations that store the values of |
| 60 | callee-saved register are assumed to remain valid throughout the end |
| 61 | of the region. |
| 62 | |
| 63 | Within a region, each instruction that affects the frame state in some |
| 64 | fashion needs to be described with an operation descriptor. For this |
| 65 | purpose, each instruction in the region is assigned a unique index. |
| 66 | Exactly how this index is derived depends on the architecture. For |
| 67 | example, on RISC and EPIC-style architecture, instructions have a |
| 68 | fixed size so it's possible to simply number the instructions. In |
| 69 | contrast, most CISC use variable-length instruction encodings, so it |
| 70 | is usually necessary to use a byte-offset as the index. Given the |
| 71 | instruction index, the operation descriptor specifies the effect of |
| 72 | the instruction in an abstract manner. For example, it might express |
| 73 | that the instruction stores calle-saved register \Var{r1} at offset 16 |
| 74 | in the stack frame. |
| 75 | |
| 76 | \section{Procedures} |
| 77 | |
| 78 | unw\_dyn\_info\_t |
| 79 | unw\_dyn\_proc\_info\_t |
| 80 | unw\_dyn\_table\_info\_t |
| 81 | unw\_dyn\_remote\_table\_info\_t |
| 82 | |
| 83 | \section{Regions} |
| 84 | |
| 85 | unw\_dyn\_region\_info\_t: |
mostang.com!davidm | 3434090 | 2003-02-22 08:19:43 +0000 | [diff] [blame] | 86 | - insn_count can be negative to indicate that the region is |
| 87 | at the end of the procedure; in such a case, the negated |
| 88 | insn_count value specifies the length of the final region |
| 89 | in number of instructions. There must be at most one region |
| 90 | with a negative insn_count and only the last region in a |
| 91 | procedure's region list may be negative. Furthermore, both |
| 92 | di->start\_ip and di->end\_ip must be valid. |
mostang.com!davidm | e91ef29 | 2003-12-10 07:14:38 +0000 | [diff] [blame] | 93 | |
| 94 | \section{Operations} |
| 95 | |
| 96 | unw\_dyn\_operation\_t |
| 97 | unw\_dyn\_op\_t |
| 98 | \_U\_QP\_TRUE |
| 99 | |
| 100 | unw\_dyn\_info\_format\_t |
| 101 | |
| 102 | - instructions don't have to be sorted in increasing order of ``when'' |
| 103 | values: In general, if you can generate the sorted order easily |
| 104 | (e.g., without an explicit sorting step), I'd recommend doing so |
| 105 | because in that case, should some version of libunwind ever require |
| 106 | sorted order, libunwind can verify in O(N) that the list is sorted |
| 107 | already. In the particular case of the ia64-version of libunwind, a |
| 108 | sorted order won't help, since it always scans the instructions up |
| 109 | to UNW_DYN_STOP. |
| 110 | |
| 111 | \_U\_dyn\_region\_info\_size(opcount); |
| 112 | \_U\_dyn\_op\_save\_reg(); |
| 113 | \_U\_dyn\_op\_spill\_fp\_rel(); |
| 114 | \_U\_dyn\_op\_spill\_sp\_rel(); |
| 115 | \_U\_dyn\_op\_add(); |
| 116 | \_U\_dyn\_op\_pop\_frames(); |
| 117 | \_U\_dyn\_op\_label\_state(); |
| 118 | \_U\_dyn\_op\_copy\_state(); |
| 119 | \_U\_dyn\_op\_alias(); |
| 120 | \_U\_dyn\_op\_stop(); |
| 121 | |
| 122 | \section{See Also} |
| 123 | |
| 124 | \SeeAlso{libunwind(3)}, |
| 125 | \SeeAlso{\_U\_dyn\_register(3)}, |
| 126 | \SeeAlso{\_U\_dyn\_cancel(3)} |
| 127 | |
| 128 | \section{Author} |
| 129 | |
| 130 | \noindent |
| 131 | David Mosberger-Tang\\ |
| 132 | Hewlett-Packard Labs\\ |
| 133 | Palo-Alto, CA 94304\\ |
| 134 | Email: \Email{davidm@hpl.hp.com}\\ |
| 135 | WWW: \URL{http://www.hpl.hp.com/research/linux/libunwind/}. |
| 136 | \LatexManEnd |
| 137 | |
| 138 | \end{document} |