_UCD_create.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. /* Endian detection */
  23. #include <limits.h>
  24. #if defined(HAVE_BYTESWAP_H)
  25. #include <byteswap.h>
  26. #endif
  27. #if defined(HAVE_ENDIAN_H)
  28. # include <endian.h>
  29. #elif defined(HAVE_SYS_ENDIAN_H)
  30. # include <sys/endian.h>
  31. #endif
  32. #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
  33. # define WE_ARE_BIG_ENDIAN 1
  34. # define WE_ARE_LITTLE_ENDIAN 0
  35. #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
  36. # define WE_ARE_BIG_ENDIAN 0
  37. # define WE_ARE_LITTLE_ENDIAN 1
  38. #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
  39. # define WE_ARE_BIG_ENDIAN 1
  40. # define WE_ARE_LITTLE_ENDIAN 0
  41. #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
  42. # define WE_ARE_BIG_ENDIAN 0
  43. # define WE_ARE_LITTLE_ENDIAN 1
  44. #elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
  45. # define WE_ARE_BIG_ENDIAN 1
  46. # define WE_ARE_LITTLE_ENDIAN 0
  47. #elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
  48. # define WE_ARE_BIG_ENDIAN 0
  49. # define WE_ARE_LITTLE_ENDIAN 1
  50. #elif defined(__386__)
  51. # define WE_ARE_BIG_ENDIAN 0
  52. # define WE_ARE_LITTLE_ENDIAN 1
  53. #else
  54. # error "Can't determine endianness"
  55. #endif
  56. #include <elf.h>
  57. #include <sys/procfs.h> /* struct elf_prstatus */
  58. #include "_UCD_lib.h"
  59. #include "_UCD_internal.h"
  60. #define NOTE_DATA(_hdr) STRUCT_MEMBER_P((_hdr), sizeof (Elf32_Nhdr) + ALIGN((_hdr)->n_namesz, 4))
  61. #define NOTE_SIZE(_hdr) (sizeof (Elf32_Nhdr) + ALIGN((_hdr)->n_namesz, 4) + (_hdr)->n_descsz)
  62. #define NOTE_NEXT(_hdr) STRUCT_MEMBER_P((_hdr), NOTE_SIZE(_hdr))
  63. #define NOTE_FITS_IN(_hdr, _size) ((_size) >= sizeof (Elf32_Nhdr) && (_size) >= NOTE_SIZE (_hdr))
  64. #define NOTE_FITS(_hdr, _end) NOTE_FITS_IN((_hdr), (unsigned long)((char *)(_end) - (char *)(_hdr)))
  65. struct UCD_info *
  66. _UCD_create(const char *filename)
  67. {
  68. union
  69. {
  70. Elf32_Ehdr h32;
  71. Elf64_Ehdr h64;
  72. } elf_header;
  73. #define elf_header32 elf_header.h32
  74. #define elf_header64 elf_header.h64
  75. bool _64bits;
  76. struct UCD_info *ui = memset(malloc(sizeof(*ui)), 0, sizeof(*ui));
  77. ui->edi.di_cache.format = -1;
  78. ui->edi.di_debug.format = -1;
  79. #if UNW_TARGET_IA64
  80. ui->edi.ktab.format = -1;
  81. #endif
  82. int fd = ui->coredump_fd = open(filename, O_RDONLY);
  83. if (fd < 0)
  84. goto err;
  85. ui->coredump_filename = strdup(filename);
  86. /* No sane ELF32 file is going to be smaller then ELF64 _header_,
  87. * so let's just read 64-bit sized one.
  88. */
  89. if (read(fd, &elf_header64, sizeof(elf_header64)) != sizeof(elf_header64))
  90. {
  91. Debug(0, "'%s' is not an ELF file\n", filename);
  92. goto err;
  93. }
  94. if (memcmp(&elf_header32, "\x7f""ELF", 4) != 0)
  95. {
  96. Debug(0, "'%s' is not an ELF file\n", filename);
  97. goto err;
  98. }
  99. if (elf_header32.e_ident[EI_CLASS] != ELFCLASS32
  100. && elf_header32.e_ident[EI_CLASS] != ELFCLASS64)
  101. {
  102. Debug(0, "'%s' is not a 32/64 bit ELF file\n", filename);
  103. goto err;
  104. }
  105. if (WE_ARE_LITTLE_ENDIAN != (elf_header32.e_ident[EI_DATA] == ELFDATA2LSB))
  106. {
  107. Debug(0, "'%s' is endian-incompatible\n", filename);
  108. goto err;
  109. }
  110. _64bits = (elf_header32.e_ident[EI_CLASS] == ELFCLASS64);
  111. if (_64bits && sizeof(elf_header64.e_entry) > sizeof(off_t))
  112. {
  113. Debug(0, "Can't process '%s': 64-bit file "
  114. "while only %ld bits are supported",
  115. filename, 8L * sizeof(off_t));
  116. goto err;
  117. }
  118. /* paranoia check */
  119. if (_64bits
  120. ? 0 /* todo: (elf_header64.e_ehsize != NN || elf_header64.e_phentsize != NN) */
  121. : (elf_header32.e_ehsize != 52 || elf_header32.e_phentsize != 32)
  122. )
  123. {
  124. Debug(0, "'%s' has wrong e_ehsize or e_phentsize\n", filename);
  125. goto err;
  126. }
  127. off_t ofs = (_64bits ? elf_header64.e_phoff : elf_header32.e_phoff);
  128. if (lseek(fd, ofs, SEEK_SET) != ofs)
  129. {
  130. Debug(0, "Can't read phdrs from '%s'\n", filename);
  131. goto err;
  132. }
  133. unsigned size = ui->phdrs_count = (_64bits ? elf_header64.e_phnum : elf_header32.e_phnum);
  134. coredump_phdr_t *phdrs = ui->phdrs = memset(malloc(size * sizeof(phdrs[0])), 0, size * sizeof(phdrs[0]));
  135. if (_64bits)
  136. {
  137. coredump_phdr_t *cur = phdrs;
  138. unsigned i = 0;
  139. while (i < size)
  140. {
  141. Elf64_Phdr hdr64;
  142. if (read(fd, &hdr64, sizeof(hdr64)) != sizeof(hdr64))
  143. {
  144. Debug(0, "Can't read phdrs from '%s'\n", filename);
  145. goto err;
  146. }
  147. cur->p_type = hdr64.p_type ;
  148. cur->p_flags = hdr64.p_flags ;
  149. cur->p_offset = hdr64.p_offset;
  150. cur->p_vaddr = hdr64.p_vaddr ;
  151. /*cur->p_paddr = hdr32.p_paddr ; always 0 */
  152. //TODO: check that and abort if it isn't?
  153. cur->p_filesz = hdr64.p_filesz;
  154. cur->p_memsz = hdr64.p_memsz ;
  155. cur->p_align = hdr64.p_align ;
  156. /* cur->backing_filename = NULL; - done by memset */
  157. cur->backing_fd = -1;
  158. cur->backing_filesize = hdr64.p_filesz;
  159. i++;
  160. cur++;
  161. }
  162. } else {
  163. coredump_phdr_t *cur = phdrs;
  164. unsigned i = 0;
  165. while (i < size)
  166. {
  167. Elf32_Phdr hdr32;
  168. if (read(fd, &hdr32, sizeof(hdr32)) != sizeof(hdr32))
  169. {
  170. Debug(0, "Can't read phdrs from '%s'\n", filename);
  171. goto err;
  172. }
  173. cur->p_type = hdr32.p_type ;
  174. cur->p_flags = hdr32.p_flags ;
  175. cur->p_offset = hdr32.p_offset;
  176. cur->p_vaddr = hdr32.p_vaddr ;
  177. /*cur->p_paddr = hdr32.p_paddr ; always 0 */
  178. cur->p_filesz = hdr32.p_filesz;
  179. cur->p_memsz = hdr32.p_memsz ;
  180. cur->p_align = hdr32.p_align ;
  181. /* cur->backing_filename = NULL; - done by memset */
  182. cur->backing_fd = -1;
  183. cur->backing_filesize = hdr32.p_memsz;
  184. i++;
  185. cur++;
  186. }
  187. }
  188. unsigned i = 0;
  189. coredump_phdr_t *cur = phdrs;
  190. while (i < size)
  191. {
  192. Debug(2, "phdr[%03d]: type:%d", i, cur->p_type);
  193. if (cur->p_type == PT_NOTE)
  194. {
  195. Elf32_Nhdr *note_hdr, *note_end;
  196. unsigned n_threads;
  197. ui->note_phdr = malloc(cur->p_filesz);
  198. if (lseek(fd, cur->p_offset, SEEK_SET) != (off_t)cur->p_offset
  199. || (uoff_t)read(fd, ui->note_phdr, cur->p_filesz) != cur->p_filesz)
  200. {
  201. Debug(0, "Can't read PT_NOTE from '%s'\n", filename);
  202. goto err;
  203. }
  204. note_end = STRUCT_MEMBER_P (ui->note_phdr, cur->p_filesz);
  205. /* Count number of threads */
  206. n_threads = 0;
  207. note_hdr = (Elf32_Nhdr *)ui->note_phdr;
  208. while (NOTE_FITS (note_hdr, note_end))
  209. {
  210. if (note_hdr->n_type == NT_PRSTATUS)
  211. n_threads++;
  212. note_hdr = NOTE_NEXT (note_hdr);
  213. }
  214. ui->n_threads = n_threads;
  215. ui->threads = malloc(sizeof (void *) * n_threads);
  216. n_threads = 0;
  217. note_hdr = (Elf32_Nhdr *)ui->note_phdr;
  218. while (NOTE_FITS (note_hdr, note_end))
  219. {
  220. if (note_hdr->n_type == NT_PRSTATUS)
  221. ui->threads[n_threads++] = NOTE_DATA (note_hdr);
  222. note_hdr = NOTE_NEXT (note_hdr);
  223. }
  224. }
  225. if (cur->p_type == PT_LOAD)
  226. {
  227. Debug(2, " ofs:%08llx va:%08llx filesize:%08llx memsize:%08llx flg:%x",
  228. (unsigned long long) cur->p_offset,
  229. (unsigned long long) cur->p_vaddr,
  230. (unsigned long long) cur->p_filesz,
  231. (unsigned long long) cur->p_memsz,
  232. cur->p_flags
  233. );
  234. if (cur->p_filesz < cur->p_memsz)
  235. Debug(2, " partial");
  236. if (cur->p_flags & PF_X)
  237. Debug(2, " executable");
  238. }
  239. Debug(2, "\n");
  240. i++;
  241. cur++;
  242. }
  243. if (ui->n_threads == 0)
  244. {
  245. Debug(0, "No NT_PRSTATUS note found in '%s'\n", filename);
  246. goto err;
  247. }
  248. ui->prstatus = ui->threads[0];
  249. return ui;
  250. err:
  251. _UCD_destroy(ui);
  252. return NULL;
  253. }
  254. int _UCD_get_num_threads(struct UCD_info *ui)
  255. {
  256. return ui->n_threads;
  257. }
  258. void _UCD_select_thread(struct UCD_info *ui, int n)
  259. {
  260. if (n >= 0 && n < ui->n_threads)
  261. ui->prstatus = ui->threads[n];
  262. }
  263. pid_t _UCD_get_pid(struct UCD_info *ui)
  264. {
  265. return ui->prstatus->pr_pid;
  266. }
  267. int _UCD_get_cursig(struct UCD_info *ui)
  268. {
  269. return ui->prstatus->pr_cursig;
  270. }
  271. int _UCD_add_backing_file_at_segment(struct UCD_info *ui, int phdr_no, const char *filename)
  272. {
  273. if ((unsigned)phdr_no >= ui->phdrs_count)
  274. {
  275. Debug(0, "There is no segment %d in this coredump\n", phdr_no);
  276. return -1;
  277. }
  278. struct coredump_phdr *phdr = &ui->phdrs[phdr_no];
  279. if (phdr->backing_filename)
  280. {
  281. Debug(0, "Backing file already added to segment %d\n", phdr_no);
  282. return -1;
  283. }
  284. int fd = open(filename, O_RDONLY);
  285. if (fd < 0)
  286. {
  287. Debug(0, "Can't open '%s'\n", filename);
  288. return -1;
  289. }
  290. phdr->backing_fd = fd;
  291. phdr->backing_filename = strdup(filename);
  292. struct stat statbuf;
  293. if (fstat(fd, &statbuf) != 0)
  294. {
  295. Debug(0, "Can't stat '%s'\n", filename);
  296. goto err;
  297. }
  298. phdr->backing_filesize = (uoff_t)statbuf.st_size;
  299. if (phdr->p_flags != (PF_X | PF_R))
  300. Debug(1, "Note: phdr[%u] is not r-x: flags are 0x%x\n", phdr_no, phdr->p_flags);
  301. if (phdr->backing_filesize > phdr->p_memsz)
  302. {
  303. /* This is expected */
  304. Debug(2, "Note: phdr[%u] is %lld bytes, file is larger: %lld bytes\n",
  305. phdr_no,
  306. (unsigned long long)phdr->p_memsz,
  307. (unsigned long long)phdr->backing_filesize
  308. );
  309. }
  310. //TODO: else loudly complain? Maybe even fail?
  311. if (phdr->p_filesz != 0)
  312. {
  313. //TODO: loop and compare in smaller blocks
  314. char *core_buf = malloc(phdr->p_filesz);
  315. char *file_buf = malloc(phdr->p_filesz);
  316. if (lseek(ui->coredump_fd, phdr->p_offset, SEEK_SET) != (off_t)phdr->p_offset
  317. || (uoff_t)read(ui->coredump_fd, core_buf, phdr->p_filesz) != phdr->p_filesz
  318. )
  319. {
  320. Debug(0, "Error reading from coredump file\n");
  321. err_read:
  322. free(core_buf);
  323. free(file_buf);
  324. goto err;
  325. }
  326. if ((uoff_t)read(fd, file_buf, phdr->p_filesz) != phdr->p_filesz)
  327. {
  328. Debug(0, "Error reading from '%s'\n", filename);
  329. goto err_read;
  330. }
  331. int r = memcmp(core_buf, file_buf, phdr->p_filesz);
  332. free(core_buf);
  333. free(file_buf);
  334. if (r != 0)
  335. {
  336. Debug(1, "Note: phdr[%u] first %lld bytes in core dump and in file do not match\n",
  337. phdr_no, (unsigned long long)phdr->p_filesz
  338. );
  339. } else {
  340. Debug(1, "Note: phdr[%u] first %lld bytes in core dump and in file match\n",
  341. phdr_no, (unsigned long long)phdr->p_filesz
  342. );
  343. }
  344. }
  345. /* Success */
  346. return 0;
  347. err:
  348. if (phdr->backing_fd >= 0)
  349. {
  350. close(phdr->backing_fd);
  351. phdr->backing_fd = -1;
  352. }
  353. free(phdr->backing_filename);
  354. phdr->backing_filename = NULL;
  355. return -1;
  356. }
  357. int _UCD_add_backing_file_at_vaddr(struct UCD_info *ui,
  358. unsigned long vaddr,
  359. const char *filename)
  360. {
  361. unsigned i;
  362. for (i = 0; i < ui->phdrs_count; i++)
  363. {
  364. struct coredump_phdr *phdr = &ui->phdrs[i];
  365. if (phdr->p_vaddr != vaddr)
  366. continue;
  367. /* It seems to match. Add it. */
  368. return _UCD_add_backing_file_at_segment(ui, i, filename);
  369. }
  370. return -1;
  371. }