_UCD_access_mem.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. int
  22. _UCD_access_mem(unw_addr_space_t as, unw_word_t addr, unw_word_t *val,
  23. int write, void *arg)
  24. {
  25. if (write)
  26. {
  27. Debug(0, "%s: write is not supported\n", __func__);
  28. return -UNW_EINVAL;
  29. }
  30. struct UCD_info *ui = arg;
  31. unw_word_t addr_last = addr + sizeof(*val)-1;
  32. coredump_phdr_t *phdr;
  33. unsigned i;
  34. for (i = 0; i < ui->phdrs_count; i++)
  35. {
  36. phdr = &ui->phdrs[i];
  37. if (phdr->p_vaddr <= addr && addr_last < phdr->p_vaddr + phdr->p_memsz)
  38. {
  39. goto found;
  40. }
  41. }
  42. Debug(1, "%s: addr 0x%llx is unmapped\n",
  43. __func__, (unsigned long long)addr
  44. );
  45. return -UNW_EINVAL;
  46. found: ;
  47. const char *filename;
  48. off_t fileofs;
  49. int fd;
  50. if (addr_last >= phdr->p_vaddr + phdr->p_filesz)
  51. {
  52. /* This part of mapped address space is not present in coredump file */
  53. /* Do we have it in the backup file? */
  54. if (phdr->backing_fd < 0)
  55. {
  56. Debug(1, "%s: access to not-present data in phdr[%d]: addr:0x%llx\n",
  57. __func__, i, (unsigned long long)addr
  58. );
  59. return -UNW_EINVAL;
  60. }
  61. filename = phdr->backing_filename;
  62. fileofs = addr - phdr->p_vaddr;
  63. fd = phdr->backing_fd;
  64. goto read;
  65. }
  66. filename = ui->coredump_filename;
  67. fileofs = phdr->p_offset + (addr - phdr->p_vaddr);
  68. fd = ui->coredump_fd;
  69. read:
  70. if (lseek(fd, fileofs, SEEK_SET) != fileofs)
  71. goto read_error;
  72. if (read(fd, val, sizeof(*val)) != sizeof(*val))
  73. goto read_error;
  74. Debug(1, "%s: 0x%llx <- [addr:0x%llx fileofs:0x%llx]\n",
  75. __func__,
  76. (unsigned long long)(*val),
  77. (unsigned long long)addr,
  78. (unsigned long long)fileofs
  79. );
  80. return 0;
  81. read_error:
  82. Debug(1, "%s: access out of file: addr:0x%llx fileofs:%llx file:'%s'\n",
  83. __func__,
  84. (unsigned long long)addr,
  85. (unsigned long long)fileofs,
  86. filename
  87. );
  88. return -UNW_EINVAL;
  89. }