_UCD_find_proc_info.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* libunwind - a platform-independent unwind library
  2. This file is part of libunwind.
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  19. #include <elf.h>
  20. #include "_UCD_lib.h"
  21. #include "_UCD_internal.h"
  22. static int
  23. get_unwind_info(struct UCD_info *ui, unw_addr_space_t as, unw_word_t ip)
  24. {
  25. unsigned long segbase, mapoff;
  26. #if UNW_TARGET_IA64 && defined(__linux)
  27. if (!ui->edi.ktab.start_ip && _Uia64_get_kernel_table (&ui->edi.ktab) < 0)
  28. return -UNW_ENOINFO;
  29. if (ui->edi.ktab.format != -1 && ip >= ui->edi.ktab.start_ip && ip < ui->edi.ktab.end_ip)
  30. return 0;
  31. #endif
  32. if ((ui->edi.di_cache.format != -1
  33. && ip >= ui->edi.di_cache.start_ip && ip < ui->edi.di_cache.end_ip)
  34. #if UNW_TARGET_ARM
  35. || (ui->edi.di_debug.format != -1
  36. && ip >= ui->edi.di_arm.start_ip && ip < ui->edi.di_arm.end_ip)
  37. #endif
  38. || (ui->edi.di_debug.format != -1
  39. && ip >= ui->edi.di_debug.start_ip && ip < ui->edi.di_debug.end_ip))
  40. return 0;
  41. invalidate_edi (&ui->edi);
  42. /* Used to be tdep_get_elf_image() in ptrace unwinding code */
  43. coredump_phdr_t *phdr = _UCD_get_elf_image(ui, ip);
  44. if (!phdr)
  45. {
  46. Debug(1, "%s returns error: _UCD_get_elf_image failed\n", __func__);
  47. return -UNW_ENOINFO;
  48. }
  49. /* segbase: where it is mapped in virtual memory */
  50. /* mapoff: offset in the file */
  51. segbase = phdr->p_vaddr;
  52. /*mapoff = phdr->p_offset; WRONG! phdr->p_offset is the offset in COREDUMP file */
  53. mapoff = 0;
  54. ///FIXME. text segment is USUALLY, not always, at offset 0 in the binary/.so file.
  55. // ensure that at initialization.
  56. /* Here, SEGBASE is the starting-address of the (mmap'ped) segment
  57. which covers the IP we're looking for. */
  58. if (dwarf_find_unwind_table(&ui->edi, as, phdr->backing_filename, segbase, mapoff, ip) < 0)
  59. {
  60. Debug(1, "%s returns error: dwarf_find_unwind_table failed\n", __func__);
  61. return -UNW_ENOINFO;
  62. }
  63. /* This can happen in corner cases where dynamically generated
  64. code falls into the same page that contains the data-segment
  65. and the page-offset of the code is within the first page of
  66. the executable. */
  67. if (ui->edi.di_cache.format != -1
  68. && (ip < ui->edi.di_cache.start_ip || ip >= ui->edi.di_cache.end_ip))
  69. ui->edi.di_cache.format = -1;
  70. if (ui->edi.di_debug.format != -1
  71. && (ip < ui->edi.di_debug.start_ip || ip >= ui->edi.di_debug.end_ip))
  72. ui->edi.di_debug.format = -1;
  73. if (ui->edi.di_cache.format == -1
  74. #if UNW_TARGET_ARM
  75. && ui->edi.di_arm.format == -1
  76. #endif
  77. && ui->edi.di_debug.format == -1)
  78. {
  79. Debug(1, "%s returns error: all formats are -1\n", __func__);
  80. return -UNW_ENOINFO;
  81. }
  82. Debug(1, "%s returns success\n", __func__);
  83. return 0;
  84. }
  85. int
  86. _UCD_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
  87. int need_unwind_info, void *arg)
  88. {
  89. struct UCD_info *ui = arg;
  90. Debug(1, "%s: entering\n", __func__);
  91. int ret = -UNW_ENOINFO;
  92. if (get_unwind_info(ui, as, ip) < 0) {
  93. Debug(1, "%s returns error: get_unwind_info failed\n", __func__);
  94. return -UNW_ENOINFO;
  95. }
  96. #if UNW_TARGET_IA64
  97. if (ui->edi.ktab.format != -1)
  98. {
  99. /* The kernel unwind table resides in local memory, so we have
  100. to use the local address space to search it. Since
  101. _UCD_put_unwind_info() has no easy way of detecting this
  102. case, we simply make a copy of the unwind-info, so
  103. _UCD_put_unwind_info() can always free() the unwind-info
  104. without ill effects. */
  105. ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi,
  106. need_unwind_info, arg);
  107. if (ret >= 0)
  108. {
  109. if (!need_unwind_info)
  110. pi->unwind_info = NULL;
  111. else
  112. {
  113. void *mem = malloc (pi->unwind_info_size);
  114. if (!mem)
  115. return -UNW_ENOMEM;
  116. memcpy (mem, pi->unwind_info, pi->unwind_info_size);
  117. pi->unwind_info = mem;
  118. }
  119. }
  120. }
  121. #endif
  122. if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1)
  123. ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache,
  124. pi, need_unwind_info, arg);
  125. #if UNW_TARGET_ARM
  126. if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
  127. ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
  128. need_unwind_info, arg);
  129. #endif
  130. if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
  131. ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
  132. need_unwind_info, arg);
  133. Debug(1, "%s: returns %d\n", __func__, ret);
  134. return ret;
  135. }