_UPT_find_proc_info.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2004 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include <elf.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/mman.h>
  26. #include "_UPT_internal.h"
  27. static int
  28. get_unwind_info (struct elf_dyn_info *edi, pid_t pid, unw_addr_space_t as, unw_word_t ip)
  29. {
  30. unsigned long segbase, mapoff;
  31. char path[PATH_MAX];
  32. #if UNW_TARGET_IA64 && defined(__linux)
  33. if (!edi->ktab.start_ip && _Uia64_get_kernel_table (&edi->ktab) < 0)
  34. return -UNW_ENOINFO;
  35. if (edi->ktab.format != -1 && ip >= edi->ktab.start_ip && ip < edi->ktab.end_ip)
  36. return 0;
  37. #endif
  38. if ((edi->di_cache.format != -1
  39. && ip >= edi->di_cache.start_ip && ip < edi->di_cache.end_ip)
  40. #if UNW_TARGET_ARM
  41. || (edi->di_debug.format != -1
  42. && ip >= edi->di_arm.start_ip && ip < edi->di_arm.end_ip)
  43. #endif
  44. || (edi->di_debug.format != -1
  45. && ip >= edi->di_debug.start_ip && ip < edi->di_debug.end_ip))
  46. return 0;
  47. invalidate_edi(edi);
  48. if (tdep_get_elf_image (&edi->ei, pid, ip, &segbase, &mapoff, path,
  49. sizeof(path)) < 0)
  50. return -UNW_ENOINFO;
  51. /* Here, SEGBASE is the starting-address of the (mmap'ped) segment
  52. which covers the IP we're looking for. */
  53. if (tdep_find_unwind_table (edi, as, path, segbase, mapoff, ip) < 0)
  54. return -UNW_ENOINFO;
  55. /* This can happen in corner cases where dynamically generated
  56. code falls into the same page that contains the data-segment
  57. and the page-offset of the code is within the first page of
  58. the executable. */
  59. if (edi->di_cache.format != -1
  60. && (ip < edi->di_cache.start_ip || ip >= edi->di_cache.end_ip))
  61. edi->di_cache.format = -1;
  62. if (edi->di_debug.format != -1
  63. && (ip < edi->di_debug.start_ip || ip >= edi->di_debug.end_ip))
  64. edi->di_debug.format = -1;
  65. if (edi->di_cache.format == -1
  66. #if UNW_TARGET_ARM
  67. && edi->di_arm.format == -1
  68. #endif
  69. && edi->di_debug.format == -1)
  70. return -UNW_ENOINFO;
  71. return 0;
  72. }
  73. int
  74. _UPT_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
  75. int need_unwind_info, void *arg)
  76. {
  77. struct UPT_info *ui = arg;
  78. int ret = -UNW_ENOINFO;
  79. if (get_unwind_info (&ui->edi, ui->pid, as, ip) < 0)
  80. return -UNW_ENOINFO;
  81. #if UNW_TARGET_IA64
  82. if (ui->edi.ktab.format != -1)
  83. {
  84. /* The kernel unwind table resides in local memory, so we have
  85. to use the local address space to search it. Since
  86. _UPT_put_unwind_info() has no easy way of detecting this
  87. case, we simply make a copy of the unwind-info, so
  88. _UPT_put_unwind_info() can always free() the unwind-info
  89. without ill effects. */
  90. ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi,
  91. need_unwind_info, arg);
  92. if (ret >= 0)
  93. {
  94. if (!need_unwind_info)
  95. pi->unwind_info = NULL;
  96. else
  97. {
  98. void *mem = malloc (pi->unwind_info_size);
  99. if (!mem)
  100. return -UNW_ENOMEM;
  101. memcpy (mem, pi->unwind_info, pi->unwind_info_size);
  102. pi->unwind_info = mem;
  103. }
  104. }
  105. }
  106. #endif
  107. if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1)
  108. ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache,
  109. pi, need_unwind_info, arg);
  110. #if UNW_TARGET_ARM
  111. if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
  112. ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
  113. need_unwind_info, arg);
  114. #endif
  115. if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
  116. ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
  117. need_unwind_info, arg);
  118. return ret;
  119. }