debug.texi 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. @node Debugging Support
  2. @c @node Debugging Support, Threads, Cryptographic Functions, Top
  3. @c %MENU% Functions to help debugging applications
  4. @chapter Debugging support
  5. Applications are usually debugged using dedicated debugger programs.
  6. But sometimes this is not possible and, in any case, it is useful to
  7. provide the developer with as much information as possible at the time
  8. the problems are experienced. For this reason a few functions are
  9. provided which a program can use to help the developer more easily
  10. locate the problem.
  11. @menu
  12. * Backtraces:: Obtaining and printing a back trace of the
  13. current stack.
  14. @end menu
  15. @node Backtraces, , , Debugging Support
  16. @section Backtraces
  17. @cindex backtrace
  18. @cindex backtrace_symbols
  19. @cindex backtrace_fd
  20. A @dfn{backtrace} is a list of the function calls that are currently
  21. active in a thread. The usual way to inspect a backtrace of a program
  22. is to use an external debugger such as gdb. However, sometimes it is
  23. useful to obtain a backtrace programmatically from within a program,
  24. e.g., for the purposes of logging or diagnostics.
  25. The header file @file{execinfo.h} declares three functions that obtain
  26. and manipulate backtraces of the current thread.
  27. @pindex execinfo.h
  28. @deftypefun int backtrace (void **@var{buffer}, int @var{size})
  29. @standards{GNU, execinfo.h}
  30. @safety{@prelim{}@mtsafe{}@asunsafe{@asuinit{} @ascuheap{} @ascudlopen{} @ascuplugin{} @asulock{}}@acunsafe{@acuinit{} @acsmem{} @aculock{} @acsfd{}}}
  31. @c The generic implementation just does pointer chasing within the local
  32. @c stack, without any guarantees that this will handle signal frames
  33. @c correctly, so it's AS-Unsafe to begin with. However, most (all?)
  34. @c arches defer to libgcc_s's _Unwind_* implementation, dlopening
  35. @c libgcc_s.so to that end except in a static version of libc.
  36. @c libgcc_s's implementation may in turn defer to libunwind. We can't
  37. @c assume those implementations are AS- or AC-safe, but even if we
  38. @c could, our own initialization path isn't, and libgcc's implementation
  39. @c calls malloc and performs internal locking, so...
  40. The @code{backtrace} function obtains a backtrace for the current
  41. thread, as a list of pointers, and places the information into
  42. @var{buffer}. The argument @var{size} should be the number of
  43. @w{@code{void *}} elements that will fit into @var{buffer}. The return
  44. value is the actual number of entries of @var{buffer} that are obtained,
  45. and is at most @var{size}.
  46. The pointers placed in @var{buffer} are actually return addresses
  47. obtained by inspecting the stack, one return address per stack frame.
  48. Note that certain compiler optimizations may interfere with obtaining a
  49. valid backtrace. Function inlining causes the inlined function to not
  50. have a stack frame; tail call optimization replaces one stack frame with
  51. another; frame pointer elimination will stop @code{backtrace} from
  52. interpreting the stack contents correctly.
  53. @end deftypefun
  54. @deftypefun {char **} backtrace_symbols (void *const *@var{buffer}, int @var{size})
  55. @standards{GNU, execinfo.h}
  56. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @aculock{}}}
  57. @c Collects info returned by _dl_addr in an auto array, allocates memory
  58. @c for the whole return buffer with malloc then sprintfs into it storing
  59. @c pointers to the strings into the array entries in the buffer.
  60. @c _dl_addr takes the recursive dl_load_lock then calls
  61. @c _dl_find_dso_for_object and determine_info.
  62. @c _dl_find_dso_for_object calls _dl-addr_inside_object.
  63. @c All of them are safe as long as the lock is held.
  64. @c @asucorrupt? It doesn't look like the dynamic loader's data
  65. @c structures could be in an inconsistent state that would cause
  66. @c malfunction here.
  67. The @code{backtrace_symbols} function translates the information
  68. obtained from the @code{backtrace} function into an array of strings.
  69. The argument @var{buffer} should be a pointer to an array of addresses
  70. obtained via the @code{backtrace} function, and @var{size} is the number
  71. of entries in that array (the return value of @code{backtrace}).
  72. The return value is a pointer to an array of strings, which has
  73. @var{size} entries just like the array @var{buffer}. Each string
  74. contains a printable representation of the corresponding element of
  75. @var{buffer}. It includes the function name (if this can be
  76. determined), an offset into the function, and the actual return address
  77. (in hexadecimal).
  78. Currently, the function name and offset can only be obtained on systems that
  79. use the ELF binary format for programs and libraries. On other systems,
  80. only the hexadecimal return address will be present. Also, you may need
  81. to pass additional flags to the linker to make the function names
  82. available to the program. (For example, on systems using GNU ld, you
  83. must pass @code{-rdynamic}.)
  84. The return value of @code{backtrace_symbols} is a pointer obtained via
  85. the @code{malloc} function, and it is the responsibility of the caller
  86. to @code{free} that pointer. Note that only the return value need be
  87. freed, not the individual strings.
  88. The return value is @code{NULL} if sufficient memory for the strings
  89. cannot be obtained.
  90. @end deftypefun
  91. @deftypefun void backtrace_symbols_fd (void *const *@var{buffer}, int @var{size}, int @var{fd})
  92. @standards{GNU, execinfo.h}
  93. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  94. @c Single loop of _dl_addr over addresses, collecting info into an iovec
  95. @c written out with a writev call per iteration. Addresses and offsets
  96. @c are converted to hex in auto buffers, so the only potential issue
  97. @c here is leaking the dl lock in case of cancellation.
  98. The @code{backtrace_symbols_fd} function performs the same translation
  99. as the function @code{backtrace_symbols} function. Instead of returning
  100. the strings to the caller, it writes the strings to the file descriptor
  101. @var{fd}, one per line. It does not use the @code{malloc} function, and
  102. can therefore be used in situations where that function might fail.
  103. @end deftypefun
  104. The following program illustrates the use of these functions. Note that
  105. the array to contain the return addresses returned by @code{backtrace}
  106. is allocated on the stack. Therefore code like this can be used in
  107. situations where the memory handling via @code{malloc} does not work
  108. anymore (in which case the @code{backtrace_symbols} has to be replaced
  109. by a @code{backtrace_symbols_fd} call as well). The number of return
  110. addresses is normally not very large. Even complicated programs rather
  111. seldom have a nesting level of more than, say, 50 and with 200 possible
  112. entries probably all programs should be covered.
  113. @smallexample
  114. @include execinfo.c.texi
  115. @end smallexample