libunwind-ptrace.tex 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. \documentclass{article}
  2. \usepackage[fancyhdr,pdf]{latex2man}
  3. \input{common.tex}
  4. \begin{document}
  5. \begin{Name}{3}{libunwind-ptrace}{David Mosberger-Tang}{Programming Library}{ptrace() support in libunwind}libunwind-ptrace -- ptrace() support in libunwind
  6. \end{Name}
  7. \section{Synopsis}
  8. \File{\#include $<$libunwind-ptrace.h$>$}\\
  9. \noindent
  10. \Type{unw\_accessors\_t} \Var{\_UPT\_accessors};\\
  11. \Type{void~*}\Func{\_UPT\_create}(\Type{pid\_t});\\
  12. \noindent
  13. \Type{void} \Func{\_UPT\_destroy}(\Type{void~*});\\
  14. \noindent
  15. \Type{int} \Func{\_UPT\_find\_proc\_info}(\Type{unw\_addr\_space\_t}, \Type{unw\_word\_t}, \Type{unw\_proc\_info\_t~*}, \Type{int}, \Type{void~*});\\
  16. \noindent
  17. \Type{void} \Func{\_UPT\_put\_unwind\_info}(\Type{unw\_addr\_space\_t}, \Type{unw\_proc\_info\_t~*}, \Type{void~*});\\
  18. \noindent
  19. \Type{int} \Func{\_UPT\_get\_dyn\_info\_list\_addr}(\Type{unw\_addr\_space\_t}, \Type{unw\_word\_t~*}, \Type{void~*});\\
  20. \noindent
  21. \Type{int} \Func{\_UPT\_access\_mem}(\Type{unw\_addr\_space\_t}, \Type{unw\_word\_t}, \Type{unw\_word\_t~*}, \Type{int}, \Type{void~*});\\
  22. \noindent
  23. \Type{int} \Func{\_UPT\_access\_reg}(\Type{unw\_addr\_space\_t}, \Type{unw\_regnum\_t}, \Type{unw\_word\_t~*}, \Type{int}, \Type{void~*});\\
  24. \noindent
  25. \Type{int} \Func{\_UPT\_access\_fpreg}(\Type{unw\_addr\_space\_t}, \Type{unw\_regnum\_t}, \Type{unw\_fpreg\_t~*}, \Type{int}, \Type{void~*});\\
  26. \noindent
  27. \Type{int} \Func{\_UPT\_get\_proc\_name}(\Type{unw\_addr\_space\_t}, \Type{unw\_word\_t}, \Type{char~*}, \Type{size\_t}, \Type{unw\_word\_t~*}, \Type{void~*});\\
  28. \noindent
  29. \Type{int} \Func{\_UPT\_resume}(\Type{unw\_addr\_space\_t}, \Type{unw\_cursor\_t~*}, \Type{void~*});\\
  30. \section{Description}
  31. The \Func{ptrace}(2) system-call makes it possible for a process to
  32. gain access to the machine-state and virtual memory of \emph{another}
  33. process. With the right set of call-back routines, it is therefore
  34. possible to hook up \Prog{libunwind} to another process via
  35. \Func{ptrace}(2). While it's not very difficult to do so directly,
  36. \Prog{libunwind} further facilitates this task by providing
  37. ready-to-use callbacks for this purpose. The routines and variables
  38. implementing this facility use a name-prefix of \Func{\_UPT}, which is
  39. stands for ``unwind-via-ptrace''.
  40. An application that wants to use the \Func{\_UPT}-facility first needs
  41. to create a new \Prog{libunwind} address-space that represents the
  42. target process. This is done by calling
  43. \Func{unw\_create\_addr\_space}(). In many cases, the application
  44. will simply want to pass the address of \Var{\_UPT\_accessors} as the
  45. first argument to this routine. Doing so will ensure that
  46. \Prog{libunwind} will be able to properly unwind the target process.
  47. However, in special circumstances, an application may prefer to use
  48. only portions of the \Prog{\_UPT}-facility. For this reason, the
  49. individual callback routines (\Func{\_UPT\_find\_proc\_info}(),
  50. \Func{\_UPT\_put\_unwind\_info}(), etc.) are also available for direct
  51. use. Of course, the addresses of these routines could also be picked
  52. up from \Var{\_UPT\_accessors}, but doing so would prevent static
  53. initialization. Also, when using \Var{\_UPT\_accessors}, \emph{all}
  54. the callback routines will be linked into the application, even if
  55. they are never actually called.
  56. Next, the application can turn on ptrace-mode on the target process,
  57. either by forking a new process, invoking \Const{PTRACE\_TRACEME}, and
  58. then starting the target program (via \Func{execve}(2)), or by
  59. directly attaching to an already running process (via
  60. \Const{PTRACE\_ATTACH}). Either way, once the process-ID (pid) of the
  61. target process is known, a \Prog{\_UPT}-info-structure can be created
  62. by calling \Func{\_UPT\_create}(), passing the pid of the target process
  63. as the only argument. The returned void-pointer then needs to be
  64. passed as the ``argument'' pointer (third argument) to
  65. \Func{unw\_init\_remote}().
  66. The \Func{\_UPT\_resume}() routine can be used to resume execution of
  67. the target process. It simply invokes \Func{ptrace}(2) with a command
  68. value of \Const{PTRACE\_CONT}.
  69. When the application is done using \Prog{libunwind} on the target
  70. process, \Func{\_UPT\_destroy}() needs to be called, passing it the
  71. void-pointer that was returned by the corresponding call to
  72. \Func{\_UPT\_create}(). This ensures that all memory and other
  73. resources are freed up.
  74. \section{Availability}
  75. Since \Func{ptrace}(2) works within a single machine only, the
  76. \Prog{\_UPT}-facility by definition is not available in
  77. \Prog{libunwind}-versions configured for cross-unwinding.
  78. \section{Thread Safety}
  79. The \Prog{\_UPT}-facility assumes that a single \Prog{\_UPT}-info
  80. structure is never shared between threads. Because of this, no
  81. explicit locking is used. As long as only one thread uses
  82. a \Prog{\_UPT}-info structure at any given time, this facility
  83. is thread-safe.
  84. \section{Return Value}
  85. \Func{\_UPT\_create}() may return a \Const{NULL} pointer if it fails
  86. to create the \Prog{\_UPT}-info-structure for any reason. For the
  87. current implementation, the only reason this call may fail is when the
  88. system is out of memory.
  89. \section{Files}
  90. \begin{Description}
  91. \item[\File{libunwind-ptrace.h}] Headerfile to include when using the
  92. interface defined by this library.
  93. \item[\Opt{-l}\File{unwind-ptrace} \Opt{-l}\File{unwind-generic}]
  94. Linker-switches to add when building a program that uses the
  95. functions defined by this library.
  96. \end{Description}
  97. \section{See Also}
  98. execve(2),
  99. \SeeAlso{libunwind(3)},
  100. ptrace(2)
  101. \section{Author}
  102. \noindent
  103. David Mosberger-Tang\\
  104. Email: \Email{dmosberger@gmail.com}\\
  105. WWW: \URL{http://www.nongnu.org/libunwind/}.
  106. \LatexManEnd
  107. \end{document}