Gstep.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002-2004 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #include "unwind_i.h"
  23. #include <signal.h>
  24. /* Recognise PLT entries such as:
  25. 3bdf0: ff 25 e2 49 13 00 jmpq *0x1349e2(%rip)
  26. 3bdf6: 68 ae 03 00 00 pushq $0x3ae
  27. 3bdfb: e9 00 c5 ff ff jmpq 38300 <_init+0x18> */
  28. static int
  29. is_plt_entry (struct dwarf_cursor *c)
  30. {
  31. unw_word_t w0, w1;
  32. unw_accessors_t *a;
  33. int ret;
  34. a = unw_get_accessors (c->as);
  35. if ((ret = (*a->access_mem) (c->as, c->ip, &w0, 0, c->as_arg)) < 0
  36. || (ret = (*a->access_mem) (c->as, c->ip + 8, &w1, 0, c->as_arg)) < 0)
  37. return 0;
  38. ret = (((w0 & 0xffff) == 0x25ff)
  39. && (((w0 >> 48) & 0xff) == 0x68)
  40. && (((w1 >> 24) & 0xff) == 0xe9));
  41. Debug (14, "ip=0x%lx => 0x%016lx 0x%016lx, ret = %d\n", c->ip, w0, w1, ret);
  42. return ret;
  43. }
  44. PROTECTED int
  45. unw_step (unw_cursor_t *cursor)
  46. {
  47. struct cursor *c = (struct cursor *) cursor;
  48. int ret, i;
  49. #if CONSERVATIVE_CHECKS
  50. int val = c->validate;
  51. c->validate = 1;
  52. #endif
  53. Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx)\n",
  54. c, c->dwarf.ip, c->dwarf.cfa);
  55. /* Try DWARF-based unwinding... */
  56. c->sigcontext_format = X86_64_SCF_NONE;
  57. ret = dwarf_step (&c->dwarf);
  58. #if CONSERVATIVE_CHECKS
  59. c->validate = val;
  60. #endif
  61. if (ret < 0 && ret != -UNW_ENOINFO)
  62. {
  63. Debug (2, "returning %d\n", ret);
  64. return ret;
  65. }
  66. if (likely (ret >= 0))
  67. {
  68. /* x86_64 ABI specifies that end of call-chain is marked with a
  69. NULL RBP. */
  70. if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
  71. {
  72. c->dwarf.ip = 0;
  73. ret = 0;
  74. }
  75. }
  76. else
  77. {
  78. /* DWARF failed. There isn't much of a usable frame-chain on x86-64,
  79. but we do need to handle two special-cases:
  80. (i) signal trampoline: Old kernels and older libcs don't
  81. export the vDSO needed to get proper unwind info for the
  82. trampoline. Recognize that case by looking at the code
  83. and filling in things by hand.
  84. (ii) PLT (shared-library) call-stubs: PLT stubs are invoked
  85. via CALLQ. Try this for all non-signal trampoline
  86. code. */
  87. unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
  88. struct dwarf_loc rbp_loc, rsp_loc, rip_loc;
  89. /* We could get here because of missing/bad unwind information.
  90. Validate all addresses before dereferencing. */
  91. c->validate = 1;
  92. Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
  93. if (unw_is_signal_frame (cursor))
  94. {
  95. ret = unw_handle_signal_frame(cursor);
  96. if (ret < 0)
  97. {
  98. Debug (2, "returning 0\n");
  99. return 0;
  100. }
  101. }
  102. else if (is_plt_entry (&c->dwarf))
  103. {
  104. /* Like regular frame, CFA = RSP+8, RA = [CFA-8], no regs saved. */
  105. Debug (2, "found plt entry\n");
  106. c->frame_info.cfa_reg_offset = 8;
  107. c->frame_info.cfa_reg_rsp = -1;
  108. c->frame_info.frame_type = UNW_X86_64_FRAME_STANDARD;
  109. c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
  110. c->dwarf.cfa += 8;
  111. }
  112. else if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
  113. {
  114. for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
  115. c->dwarf.loc[i] = DWARF_NULL_LOC;
  116. }
  117. else
  118. {
  119. unw_word_t rbp;
  120. ret = dwarf_get (&c->dwarf, c->dwarf.loc[RBP], &rbp);
  121. if (ret < 0)
  122. {
  123. Debug (2, "returning %d [RBP=0x%lx]\n", ret,
  124. DWARF_GET_LOC (c->dwarf.loc[RBP]));
  125. return ret;
  126. }
  127. if (!rbp)
  128. {
  129. /* Looks like we may have reached the end of the call-chain. */
  130. rbp_loc = DWARF_NULL_LOC;
  131. rsp_loc = DWARF_NULL_LOC;
  132. rip_loc = DWARF_NULL_LOC;
  133. }
  134. else
  135. {
  136. unw_word_t rbp1 = 0;
  137. rbp_loc = DWARF_LOC(rbp, 0);
  138. rsp_loc = DWARF_NULL_LOC;
  139. rip_loc = DWARF_LOC (rbp + 8, 0);
  140. ret = dwarf_get (&c->dwarf, rbp_loc, &rbp1);
  141. Debug (1, "[RBP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
  142. (unsigned long) DWARF_GET_LOC (c->dwarf.loc[RBP]),
  143. rbp, c->dwarf.cfa, rbp1);
  144. /* Heuristic to determine incorrect guess. For RBP to be a
  145. valid frame it needs to be above current CFA, but don't
  146. let it go more than a little. Note that we can't deduce
  147. anything about new RBP (rbp1) since it may not be a frame
  148. pointer in the frame above. Just check we get the value. */
  149. if (ret < 0
  150. || rbp <= c->dwarf.cfa
  151. || (rbp - c->dwarf.cfa) > 0x4000)
  152. {
  153. rip_loc = DWARF_NULL_LOC;
  154. rbp_loc = DWARF_NULL_LOC;
  155. }
  156. c->frame_info.frame_type = UNW_X86_64_FRAME_GUESSED;
  157. c->frame_info.cfa_reg_rsp = 0;
  158. c->frame_info.cfa_reg_offset = 16;
  159. c->frame_info.rbp_cfa_offset = -16;
  160. c->dwarf.cfa += 16;
  161. }
  162. /* Mark all registers unsaved */
  163. for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
  164. c->dwarf.loc[i] = DWARF_NULL_LOC;
  165. c->dwarf.loc[RBP] = rbp_loc;
  166. c->dwarf.loc[RSP] = rsp_loc;
  167. c->dwarf.loc[RIP] = rip_loc;
  168. }
  169. c->dwarf.ret_addr_column = RIP;
  170. if (!DWARF_IS_NULL_LOC (c->dwarf.loc[RIP]))
  171. {
  172. ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
  173. Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
  174. (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
  175. (unsigned long long) c->dwarf.ip);
  176. if (ret < 0)
  177. {
  178. Debug (2, "returning %d\n", ret);
  179. return ret;
  180. }
  181. ret = 1;
  182. }
  183. else
  184. c->dwarf.ip = 0;
  185. if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
  186. return -UNW_EBADFRAME;
  187. }
  188. Debug (2, "returning %d\n", ret);
  189. return ret;
  190. }