_UCD_get_proc_name.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "_UCD_lib.h"
  20. #include "_UCD_internal.h"
  21. /* Find the ELF image that contains IP and return the "closest"
  22. procedure name, if there is one. With some caching, this could be
  23. sped up greatly, but until an application materializes that's
  24. sensitive to the performance of this routine, why bother... */
  25. static int
  26. elf_w (CD_get_proc_name) (struct UCD_info *ui, unw_addr_space_t as, unw_word_t ip,
  27. char *buf, size_t buf_len, unw_word_t *offp)
  28. {
  29. unsigned long segbase, mapoff;
  30. int ret;
  31. /* Used to be tdep_get_elf_image() in ptrace unwinding code */
  32. coredump_phdr_t *cphdr = _UCD_get_elf_image(ui, ip);
  33. if (!cphdr)
  34. {
  35. Debug(1, "%s returns error: _UCD_get_elf_image failed\n", __func__);
  36. return -UNW_ENOINFO;
  37. }
  38. /* segbase: where it is mapped in virtual memory */
  39. /* mapoff: offset in the file */
  40. segbase = cphdr->p_vaddr;
  41. /*mapoff = phdr->p_offset; WRONG! phdr->p_offset is the offset in COREDUMP file */
  42. mapoff = 0;
  43. ret = elf_w (get_proc_name_in_image) (as, &ui->edi.ei, segbase, mapoff, ip, buf, buf_len, offp);
  44. return ret;
  45. }
  46. int
  47. _UCD_get_proc_name (unw_addr_space_t as, unw_word_t ip,
  48. char *buf, size_t buf_len, unw_word_t *offp, void *arg)
  49. {
  50. struct UCD_info *ui = arg;
  51. #if ELF_CLASS == ELFCLASS64
  52. return _Uelf64_CD_get_proc_name (ui, as, ip, buf, buf_len, offp);
  53. #elif ELF_CLASS == ELFCLASS32
  54. return _Uelf32_CD_get_proc_name (ui, as, ip, buf, buf_len, offp);
  55. #else
  56. return -UNW_ENOINFO;
  57. #endif
  58. }