simple.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* simple.c -- The backtrace_simple function.
  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 "unwind.h"
  29. #include "backtrace.h"
  30. /* The simple_backtrace routine. */
  31. /* Data passed through _Unwind_Backtrace. */
  32. struct backtrace_simple_data
  33. {
  34. /* Number of frames to skip. */
  35. int skip;
  36. /* Library state. */
  37. struct backtrace_state *state;
  38. /* Callback routine. */
  39. backtrace_simple_callback callback;
  40. /* Error callback routine. */
  41. backtrace_error_callback error_callback;
  42. /* Data to pass to callback routine. */
  43. void *data;
  44. /* Value to return from backtrace. */
  45. int ret;
  46. };
  47. /* Unwind library callback routine. This is passed to
  48. _Unwind_Backtrace. */
  49. static _Unwind_Reason_Code
  50. simple_unwind (struct _Unwind_Context *context, void *vdata)
  51. {
  52. struct backtrace_simple_data *bdata = (struct backtrace_simple_data *) vdata;
  53. uintptr_t pc;
  54. int ip_before_insn = 0;
  55. #ifdef HAVE_GETIPINFO
  56. pc = _Unwind_GetIPInfo (context, &ip_before_insn);
  57. #else
  58. pc = _Unwind_GetIP (context);
  59. #endif
  60. if (bdata->skip > 0)
  61. {
  62. --bdata->skip;
  63. return _URC_NO_REASON;
  64. }
  65. if (!ip_before_insn)
  66. --pc;
  67. bdata->ret = bdata->callback (bdata->data, pc);
  68. if (bdata->ret != 0)
  69. return _URC_END_OF_STACK;
  70. return _URC_NO_REASON;
  71. }
  72. /* Get a simple stack backtrace. */
  73. int __attribute__((noinline))
  74. backtrace_simple (struct backtrace_state *state, int skip,
  75. backtrace_simple_callback callback,
  76. backtrace_error_callback error_callback, void *data)
  77. {
  78. struct backtrace_simple_data bdata;
  79. bdata.skip = skip + 1;
  80. bdata.state = state;
  81. bdata.callback = callback;
  82. bdata.error_callback = error_callback;
  83. bdata.data = data;
  84. bdata.ret = 0;
  85. _Unwind_Backtrace (simple_unwind, &bdata);
  86. return bdata.ret;
  87. }