libunwind-dynamic.tex 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. \documentclass{article}
  2. \usepackage[fancyhdr,pdf]{latex2man}
  3. \input{common.tex}
  4. \begin{document}
  5. \begin{Name}{3}{libunwind-dynamic}{David Mosberger-Tang}{Programming Library}{Introduction to dynamic unwind-info}libunwind-dynamic -- libunwind-support for runtime-generated code
  6. \end{Name}
  7. \section{Introduction}
  8. For \Prog{libunwind} to do its job, it needs to be able to reconstruct
  9. the \emph{frame state} of each frame in a call-chain. The frame state
  10. describes the subset of the machine-state that consists of the
  11. \emph{frame registers} (typically the instruction-pointer and the
  12. stack-pointer) and all callee-saved registers (preserved registers).
  13. The frame state describes each register either by providing its
  14. current value (for frame registers) or by providing the location at
  15. which the current value is stored (callee-saved registers).
  16. For statically generated code, the compiler normally takes care of
  17. emitting \emph{unwind-info} which provides the minimum amount of
  18. information needed to reconstruct the frame-state for each instruction
  19. in a procedure. For dynamically generated code, the runtime code
  20. generator must use the dynamic unwind-info interface provided by
  21. \Prog{libunwind} to supply the equivalent information. This manual
  22. page describes the format of this information in detail.
  23. For the purpose of this discussion, a \emph{procedure} is defined to
  24. be an arbitrary piece of \emph{contiguous} code. Normally, each
  25. procedure directly corresponds to a function in the source-language
  26. but this is not strictly required. For example, a runtime
  27. code-generator could translate a given function into two separate
  28. (discontiguous) procedures: one for frequently-executed (hot) code and
  29. one for rarely-executed (cold) code. Similarly, simple
  30. source-language functions (usually leaf functions) may get translated
  31. into code for which the default unwind-conventions apply and for such
  32. code, it is not strictly necessary to register dynamic unwind-info.
  33. A procedure logically consists of a sequence of \emph{regions}.
  34. Regions are nested in the sense that the frame state at the end of one
  35. region is, by default, assumed to be the frame state for the next
  36. region. Each region is thought of as being divided into a
  37. \emph{prologue}, a \emph{body}, and an \emph{epilogue}. Each of them
  38. can be empty. If non-empty, the prologue sets up the frame state for
  39. the body. For example, the prologue may need to allocate some space
  40. on the stack and save certain callee-saved registers. The body
  41. performs the actual work of the procedure but does not change the
  42. frame state in any way. If non-empty, the epilogue restores the
  43. previous frame state and as such it undoes or cancels the effect of
  44. the prologue. In fact, a single epilogue may undo the effect of the
  45. prologues of several (nested) regions.
  46. We should point out that even though the prologue, body, and epilogue
  47. are logically separate entities, optimizing code-generators will
  48. generally interleave instructions from all three entities. For this
  49. reason, the dynamic unwind-info interface of \Prog{libunwind} makes no
  50. distinction whatsoever between prologue and body. Similarly, the
  51. exact set of instructions that make up an epilogue is also irrelevant.
  52. The only point in the epilogue that needs to be described explicitly
  53. by the dynamic unwind-info is the point at which the stack-pointer
  54. gets restored. The reason this point needs to be described is that
  55. once the stack-pointer is restored, all values saved in the
  56. deallocated portion of the stack frame become invalid and hence
  57. \Prog{libunwind} needs to know about it. The portion of the frame
  58. state not saved on the stack is assume to remain valid through the end
  59. of the region. For this reason, there is usually no need to describe
  60. instructions which restore the contents of callee-saved registers.
  61. Within a region, each instruction that affects the frame state in some
  62. fashion needs to be described with an operation descriptor. For this
  63. purpose, each instruction in the region is assigned a unique index.
  64. Exactly how this index is derived depends on the architecture. For
  65. example, on RISC and EPIC-style architecture, instructions have a
  66. fixed size so it's possible to simply number the instructions. In
  67. contrast, most CISC use variable-length instruction encodings, so it
  68. is usually necessary to use a byte-offset as the index. Given the
  69. instruction index, the operation descriptor specifies the effect of
  70. the instruction in an abstract manner. For example, it might express
  71. that the instruction stores calle-saved register \Var{r1} at offset 16
  72. in the stack frame.
  73. \section{Procedures}
  74. A runtime code-generator registers the dynamic unwind-info of a
  75. procedure by setting up a structure of type \Type{unw\_dyn\_info\_t}
  76. and calling \Func{\_U\_dyn\_register}(), passing the address of the
  77. structure as the sole argument. The members of the
  78. \Type{unw\_dyn\_info\_t} structure are described below:
  79. \begin{itemize}
  80. \item[\Type{void~*}next] Private to \Prog{libunwind}. Must not be used
  81. by the application.
  82. \item[\Type{void~*}prev] Private to \Prog{libunwind}. Must not be used
  83. by the application.
  84. \item[\Type{unw\_word\_t} \Var{start\_ip}] The start-address of the
  85. instructions of the procedure (remember: procedure are defined to be
  86. contiguous pieces of code, so a single code-range is sufficient).
  87. \item[\Type{unw\_word\_t} \Var{end\_ip}] The end-address of the
  88. instructions of the procedure (non-inclusive, that is,
  89. \Var{end\_ip}-\Var{start\_ip} is the size of the procedure in
  90. bytes).
  91. \item[\Type{unw\_word\_t} \Var{gp}] The global-pointer value in use
  92. for this procedure. The exact meaing of the global-pointer is
  93. architecture-specific and on some architecture, it is not used at
  94. all.
  95. \item[\Type{int32\_t} \Var{format}] The format of the unwind-info.
  96. This member can be one of \Const{UNW\_INFO\_FORMAT\_DYNAMIC},
  97. \Const{UNW\_INFO\_FORMAT\_TABLE}, or
  98. \Const{UNW\_INFO\_FORMAT\_REMOTE\_TABLE}.
  99. \item[\Type{union} \Var{u}] This union contains one sub-member
  100. structure for every possible unwind-info format:
  101. \begin{description}
  102. \item[\Type{unw\_dyn\_proc\_info\_t} \Var{pi}] This member is used
  103. for format \Const{UNW\_INFO\_FORMAT\_DYNAMIC}.
  104. \item[\Type{unw\_dyn\_table\_info\_t} \Var{ti}] This member is used
  105. for format \Const{UNW\_INFO\_FORMAT\_TABLE}.
  106. \item[\Type{unw\_dyn\_remote\_table\_info\_t} \Var{rti}] This member
  107. is used for format \Const{UNW\_INFO\_FORMAT\_REMOTE\_TABLE}.
  108. \end{description}\
  109. The format of these sub-members is described in detail below.
  110. \end{itemize}
  111. \subsection{Proc-info format}
  112. This is the preferred dynamic unwind-info format and it is generally
  113. the one used by full-blown runtime code-generators. In this format,
  114. the details of a procedure are described by a structure of type
  115. \Type{unw\_dyn\_proc\_info\_t}. This structure contains the following
  116. members:
  117. \begin{description}
  118. \item[\Type{unw\_word\_t} \Var{name\_ptr}] The address of a
  119. (human-readable) name of the procedure or 0 if no such name is
  120. available. If non-zero, The string stored at this address must be
  121. ASCII NUL terminated. For source languages that use name-mangling
  122. (such as C++ or Java) the string stored at this address should be
  123. the \emph{demangled} version of the name.
  124. \item[\Type{unw\_word\_t} \Var{handler}] The address of the
  125. personality-routine for this procedure. Personality-routines are
  126. used in conjunction with exception handling. See the C++ ABI draft
  127. (http://www.codesourcery.com/cxx-abi/) for an overview and a
  128. description of the personality routine. If the procedure has no
  129. personality routine, \Var{handler} must be set to 0.
  130. \item[\Type{uint32\_t} \Var{flags}] A bitmask of flags. At the
  131. moment, no flags have been defined and this member must be
  132. set to 0.
  133. \item[\Type{unw\_dyn\_region\_info\_t~*}\Var{regions}] A NULL-terminated
  134. linked list of region-descriptors. See section ``Region
  135. descriptors'' below for more details.
  136. \end{description}
  137. \subsection{Table-info format}
  138. This format is generally used when the dynamically generated code was
  139. derived from static code and the unwind-info for the dynamic and the
  140. static versions is identical. For example, this format can be useful
  141. when loading statically-generated code into an address-space in a
  142. non-standard fashion (i.e., through some means other than
  143. \Func{dlopen}()). In this format, the details of a group of procedures
  144. is described by a structure of type \Type{unw\_dyn\_table\_info}.
  145. This structure contains the following members:
  146. \begin{description}
  147. \item[\Type{unw\_word\_t} \Var{name\_ptr}] The address of a
  148. (human-readable) name of the procedure or 0 if no such name is
  149. available. If non-zero, The string stored at this address must be
  150. ASCII NUL terminated. For source languages that use name-mangling
  151. (such as C++ or Java) the string stored at this address should be
  152. the \emph{demangled} version of the name.
  153. \item[\Type{unw\_word\_t} \Var{segbase}] The segment-base value
  154. that needs to be added to the segment-relative values stored in the
  155. unwind-info. The exact meaning of this value is
  156. architecture-specific.
  157. \item[\Type{unw\_word\_t} \Var{table\_len}] The length of the
  158. unwind-info (\Var{table\_data}) counted in units of words
  159. (\Type{unw\_word\_t}).
  160. \item[\Type{unw\_word\_t} \Var{table\_data}] A pointer to the actual
  161. data encoding the unwind-info. The exact format is
  162. architecture-specific (see architecture-specific sections below).
  163. \end{description}
  164. \subsection{Remote table-info format}
  165. The remote table-info format has the same basic purpose as the regular
  166. table-info format. The only difference is that when \Prog{libunwind}
  167. uses the unwind-info, it will keep the table data in the target
  168. address-space (which may be remote). Consequently, the type of the
  169. \Var{table\_data} member is \Type{unw\_word\_t} rather than a pointer.
  170. This implies that \Prog{libunwind} will have to access the table-data
  171. via the address-space's \Func{access\_mem}() call-back, rather than
  172. through a direct memory reference.
  173. From the point of view of a runtime-code generator, the remote
  174. table-info format offers no advantage and it is expected that such
  175. generators will describe their procedures either with the proc-info
  176. format or the normal table-info format. The main reason that the
  177. remote table-info format exists is to enable the
  178. address-space-specific \Func{find\_proc\_info}() callback (see
  179. \SeeAlso{unw\_create\_addr\_space}(3)) to return unwind tables whose
  180. data remains in remote memory. This can speed up unwinding (e.g., for
  181. a debugger) because it reduces the amount of data that needs to be
  182. loaded from remote memory.
  183. \section{Regions descriptors}
  184. A region descriptor is a variable length structure that describes how
  185. each instruction in the region affects the frame state. Of course,
  186. most instructions in a region usualy do not change the frame state and
  187. for those, nothing needs to be recorded in the region descriptor. A
  188. region descriptor is a structure of type
  189. \Type{unw\_dyn\_region\_info\_t} and has the following members:
  190. \begin{description}
  191. \item[\Type{unw\_dyn\_region\_info\_t~*}\Var{next}] A pointer to the
  192. next region. If this is the last region, \Var{next} is \Const{NULL}.
  193. \item[\Type{int32\_t} \Var{insn\_count}] The length of the region in
  194. instructions. Each instruction is assumed to have a fixed size (see
  195. architecture-specific sections for details). The value of
  196. \Var{insn\_count} may be negative in the last region of a procedure
  197. (i.e., it may be negative only if \Var{next} is \Const{NULL}). A
  198. negative value indicates that the region covers the last \emph{N}
  199. instructions of the procedure, where \emph{N} is the absolute value
  200. of \Var{insn\_count}.
  201. \item[\Type{uint32\_t} \Var{op\_count}] The (allocated) length of
  202. the \Var{op\_count} array.
  203. \item[\Type{unw\_dyn\_op\_t} \Var{op}] An array of dynamic unwind
  204. directives. See Section ``Dynamic unwind directives'' for a
  205. description of the directives.
  206. \end{description}
  207. A region descriptor with an \Var{insn\_count} of zero is an
  208. \emph{empty region} and such regions are perfectly legal. In fact,
  209. empty regions can be useful to establish a particular frame state
  210. before the start of another region.
  211. A single region list can be shared across multiple procedures provided
  212. those procedures share a common prologue and epilogue (their bodies
  213. may differ, of course). Normally, such procedures consist of a canned
  214. prologue, the body, and a canned epilogue. This could be described by
  215. two regions: one covering the prologue and one covering the epilogue.
  216. Since the body length is variable, the latter region would need to
  217. specify a negative value in \Var{insn\_count} such that
  218. \Prog{libunwind} knows that the region covers the end of the procedure
  219. (up to the address specified by \Var{end\_ip}).
  220. The region descriptor is a variable length structure to make it
  221. possible to allocate all the necessary memory with a single
  222. memory-allocation request. To facilitate the allocation of a region
  223. descriptors \Prog{libunwind} provides a helper routine with the
  224. following synopsis:
  225. \noindent
  226. \Type{size\_t} \Func{\_U\_dyn\_region\_size}(\Type{int} \Var{op\_count});
  227. This routine returns the number of bytes needed to hold a region
  228. descriptor with space for \Var{op\_count} unwind directives. Note
  229. that the length of the \Var{op} array does not have to match exactly
  230. with the number of directives in a region. Instead, it is sufficient
  231. if the \Var{op} array contains at least as many entries as there are
  232. directives, since the end of the directives can always be indicated
  233. with the \Const{UNW\_DYN\_STOP} directive.
  234. \section{Dynamic unwind directives}
  235. A dynamic unwind directive describes how the frame state changes
  236. at a particular point within a region. The description is in
  237. the form of a structure of type \Type{unw\_dyn\_op\_t}. This
  238. structure has the following members:
  239. \begin{description}
  240. \item[\Type{int8\_t} \Var{tag}] The operation tag. Must be one
  241. of the \Type{unw\_dyn\_operation\_t} values described below.
  242. \item[\Type{int8\_t} \Var{qp}] The qualifying predicate that controls
  243. whether or not this directive is active. This is useful for
  244. predicated architecturs such as IA-64 or ARM, where the contents of
  245. another (callee-saved) register determines whether or not an
  246. instruction is executed (takes effect). If the directive is always
  247. active, this member should be set to the manifest constant
  248. \Const{\_U\_QP\_TRUE} (this constant is defined for all
  249. architectures, predicated or not).
  250. \item[\Type{int16\_t} \Var{reg}] The number of the register affected
  251. by the instruction.
  252. \item[\Type{int32\_t} \Var{when}] The region-relative number of
  253. the instruction to which this directive applies. For example,
  254. a value of 0 means that the effect described by this directive
  255. has taken place once the first instruction in the region has
  256. executed.
  257. \item[\Type{unw\_word\_t} \Var{val}] The value to be applied by the
  258. operation tag. The exact meaning of this value varies by tag. See
  259. Section ``Operation tags'' below.
  260. \end{description}
  261. It is perfectly legitimate to specify multiple dynamic unwind
  262. directives with the same \Var{when} value, if a particular instruction
  263. has a complex effect on the frame state.
  264. Empty regions by definition contain no actual instructions and as such
  265. the directives are not tied to a particular instruction. By
  266. convention, the \Var{when} member should be set to 0, however.
  267. There is no need for the dynamic unwind directives to appear
  268. in order of increasing \Var{when} values. If the directives happen to
  269. be sorted in that order, it may result in slightly faster execution,
  270. but a runtime code-generator should not go to extra lengths just to
  271. ensure that the directives are sorted.
  272. IMPLEMENTATION NOTE: should \Prog{libunwind} implementations for
  273. certain architectures prefer the list of unwind directives to be
  274. sorted, it is recommended that such implementations first check
  275. whether the list happens to be sorted already and, if not, sort the
  276. directives explicitly before the first use. With this approach, the
  277. overhead of explicit sorting is only paid when there is a real benefit
  278. and if the runtime code-generator happens to generated sorted lists
  279. naturally, the performance penalty is limited to a simple O(N) check.
  280. \subsection{Operations tags}
  281. The possible operation tags are defined by enumeration type
  282. \Type{unw\_dyn\_operation\_t} which defines the following
  283. values:
  284. \begin{description}
  285. \item[\Const{UNW\_DYN\_STOP}] Marks the end of the dynamic unwind
  286. directive list. All remaining entries in the \Var{op} array of the
  287. region-descriptor are ignored. This tag is guaranteed to have a
  288. value of 0.
  289. \item[\Const{UNW\_DYN\_SAVE\_REG}] Marks an instruction which saves
  290. register \Var{reg} to register \Var{val}.
  291. \item[\Const{UNW\_DYN\_SPILL\_FP\_REL}] Marks an instruction which
  292. spills register \Var{reg} to a frame-pointer-relative location. The
  293. frame-pointer-relative offset is given by the value stored in member
  294. \Var{val}. See the architecture-specific sections for a description
  295. of the stack frame layout.
  296. \item[\Const{UNW\_DYN\_SPILL\_SP\_REL}] Marks an instruction which
  297. spills register \Var{reg} to a stack-pointer-relative location. The
  298. stack-pointer-relative offset is given by the value stored in member
  299. \Var{val}. See the architecture-specific sections for a description
  300. of the stack frame layout.
  301. \item[\Const{UNW\_DYN\_ADD}] Marks an instruction which adds
  302. the constant value \Var{val} to register \Var{reg}. To add subtract
  303. a constant value, store the two's-complement of the value in
  304. \Var{val}. The set of registers that can be specified for this tag
  305. is described in the architecture-specific sections below.
  306. \item[\Const{UNW\_DYN\_POP\_FRAMES}]
  307. \item[\Const{UNW\_DYN\_LABEL\_STATE}]
  308. \item[\Const{UNW\_DYN\_COPY\_STATE}]
  309. \item[\Const{UNW\_DYN\_ALIAS}]
  310. \end{description}
  311. unw\_dyn\_op\_t
  312. \_U\_dyn\_op\_save\_reg();
  313. \_U\_dyn\_op\_spill\_fp\_rel();
  314. \_U\_dyn\_op\_spill\_sp\_rel();
  315. \_U\_dyn\_op\_add();
  316. \_U\_dyn\_op\_pop\_frames();
  317. \_U\_dyn\_op\_label\_state();
  318. \_U\_dyn\_op\_copy\_state();
  319. \_U\_dyn\_op\_alias();
  320. \_U\_dyn\_op\_stop();
  321. \section{IA-64 specifics}
  322. - meaning of segbase member in table-info/table-remote-info format
  323. - format of table\_data in table-info/table-remote-info format
  324. - instruction size: each bundle is counted as 3 instructions, regardless
  325. of template (MLX)
  326. - describe stack-frame layout, especially with regards to sp-relative
  327. and fp-relative addressing
  328. - UNW\_DYN\_ADD can only add to ``sp'' (always a negative value); use
  329. POP\_FRAMES otherwise
  330. \section{See Also}
  331. \SeeAlso{libunwind(3)},
  332. \SeeAlso{\_U\_dyn\_register(3)},
  333. \SeeAlso{\_U\_dyn\_cancel(3)}
  334. \section{Author}
  335. \noindent
  336. David Mosberger-Tang\\
  337. Email: \Email{dmosberger@gmail.com}\\
  338. WWW: \URL{http://www.nongnu.org/libunwind/}.
  339. \LatexManEnd
  340. \end{document}