Home | History | Annotate | Download | only in coredump
      1 /* libunwind - a platform-independent unwind library
      2 
      3 This file is part of libunwind.
      4 
      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 
     13 The above copyright notice and this permission notice shall be
     14 included in all copies or substantial portions of the Software.
     15 
     16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
     23 
     24 #include "_UCD_lib.h"
     25 #include "_UCD_internal.h"
     26 
     27 int
     28 _UCD_access_mem(unw_addr_space_t as, unw_word_t addr, unw_word_t *val,
     29 		 int write, void *arg)
     30 {
     31   if (write)
     32     {
     33       Debug(0, "write is not supported\n");
     34       return -UNW_EINVAL;
     35     }
     36 
     37   struct UCD_info *ui = arg;
     38 
     39   unw_word_t addr_last = addr + sizeof(*val)-1;
     40   coredump_phdr_t *phdr;
     41   unsigned i;
     42   for (i = 0; i < ui->phdrs_count; i++)
     43     {
     44       phdr = &ui->phdrs[i];
     45       if (phdr->p_vaddr <= addr && addr_last < phdr->p_vaddr + phdr->p_memsz)
     46 	{
     47 	  goto found;
     48 	}
     49     }
     50   Debug(1, "addr 0x%llx is unmapped\n", (unsigned long long)addr);
     51   return -UNW_EINVAL;
     52 
     53  found: ;
     54 
     55   const char *filename UNUSED;
     56   off_t fileofs;
     57   int fd;
     58   if (addr_last >= phdr->p_vaddr + phdr->p_filesz)
     59     {
     60       /* This part of mapped address space is not present in coredump file */
     61       /* Do we have it in the backup file? */
     62       if (phdr->backing_fd < 0)
     63         {
     64           Debug(1, "access to not-present data in phdr[%d]: addr:0x%llx\n",
     65 				i, (unsigned long long)addr
     66 			);
     67           return -UNW_EINVAL;
     68         }
     69       filename = phdr->backing_filename;
     70       fileofs = addr - phdr->p_vaddr;
     71       fd = phdr->backing_fd;
     72       goto read;
     73     }
     74 
     75   filename = ui->coredump_filename;
     76   fileofs = phdr->p_offset + (addr - phdr->p_vaddr);
     77   fd = ui->coredump_fd;
     78  read:
     79   if (lseek(fd, fileofs, SEEK_SET) != fileofs)
     80     goto read_error;
     81   if (read(fd, val, sizeof(*val)) != sizeof(*val))
     82     goto read_error;
     83 
     84   Debug(1, "0x%llx <- [addr:0x%llx fileofs:0x%llx]\n",
     85 	(unsigned long long)(*val),
     86 	(unsigned long long)addr,
     87 	(unsigned long long)fileofs
     88   );
     89   return 0;
     90 
     91  read_error:
     92   Debug(1, "access out of file: addr:0x%llx fileofs:%llx file:'%s'\n",
     93 	(unsigned long long)addr,
     94 	(unsigned long long)fileofs,
     95 	filename
     96   );
     97   return -UNW_EINVAL;
     98 }
     99