backtrace.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* backtrace.c -- Entry point for stack backtrace library.
  2. Copyright (C) 2012-2021 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <sys/types.h>
  29. #include "unwind.h"
  30. #include "backtrace.h"
  31. #include "internal.h"
  32. /* The main backtrace_full routine. */
  33. /* Data passed through _Unwind_Backtrace. */
  34. struct backtrace_data
  35. {
  36. /* Number of frames to skip. */
  37. int skip;
  38. /* Library state. */
  39. struct backtrace_state *state;
  40. /* Callback routine. */
  41. backtrace_full_callback callback;
  42. /* Error callback routine. */
  43. backtrace_error_callback error_callback;
  44. /* Data to pass to callback routines. */
  45. void *data;
  46. /* Value to return from backtrace_full. */
  47. int ret;
  48. /* Whether there is any memory available. */
  49. int can_alloc;
  50. };
  51. /* Unwind library callback routine. This is passed to
  52. _Unwind_Backtrace. */
  53. static _Unwind_Reason_Code
  54. unwind (struct _Unwind_Context *context, void *vdata)
  55. {
  56. struct backtrace_data *bdata = (struct backtrace_data *) vdata;
  57. uintptr_t pc;
  58. int ip_before_insn = 0;
  59. #ifdef HAVE_GETIPINFO
  60. pc = _Unwind_GetIPInfo (context, &ip_before_insn);
  61. #else
  62. pc = _Unwind_GetIP (context);
  63. #endif
  64. if (bdata->skip > 0)
  65. {
  66. --bdata->skip;
  67. return _URC_NO_REASON;
  68. }
  69. if (!ip_before_insn)
  70. --pc;
  71. if (!bdata->can_alloc)
  72. bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);
  73. else
  74. bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,
  75. bdata->error_callback, bdata->data);
  76. if (bdata->ret != 0)
  77. return _URC_END_OF_STACK;
  78. return _URC_NO_REASON;
  79. }
  80. /* Get a stack backtrace. */
  81. int __attribute__((noinline))
  82. backtrace_full (struct backtrace_state *state, int skip,
  83. backtrace_full_callback callback,
  84. backtrace_error_callback error_callback, void *data)
  85. {
  86. struct backtrace_data bdata;
  87. void *p;
  88. bdata.skip = skip + 1;
  89. bdata.state = state;
  90. bdata.callback = callback;
  91. bdata.error_callback = error_callback;
  92. bdata.data = data;
  93. bdata.ret = 0;
  94. /* If we can't allocate any memory at all, don't try to produce
  95. file/line information. */
  96. p = backtrace_alloc (state, 4096, NULL, NULL);
  97. if (p == NULL)
  98. bdata.can_alloc = 0;
  99. else
  100. {
  101. backtrace_free (state, p, 4096, NULL, NULL);
  102. bdata.can_alloc = 1;
  103. }
  104. _Unwind_Backtrace (unwind, &bdata);
  105. return bdata.ret;
  106. }