source: mainline/uspace/app/taskdump/elf_core.c@ 8d2dd7f2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d2dd7f2 was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup taskdump
30 * @{
31 */
32/** @file Write ELF core files.
33 *
34 * Creates ELF core files. Core files do not seem to be specified by some
35 * standard (the System V ABI explicitely states that it does not specify
36 * them).
37 *
38 * Looking at core files produced by Linux, these don't have section headers,
39 * only program headers, although objdump shows them as having sections.
40 * Basically at the beginning there should be a note segment followed
41 * by one loadable segment per memory area.
42 *
43 * The note segment contains a series of records with register state,
44 * process info etc. We only write one record NT_PRSTATUS which contains
45 * process/register state (anything which is not register state we fill
46 * with zeroes).
47 */
48
49#include <align.h>
50#include <elf/elf.h>
51#include <elf/elf_linux.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <stdint.h>
55#include <stddef.h>
56#include <errno.h>
57#include <mem.h>
58#include <as.h>
59#include <udebug.h>
60#include <macros.h>
61#include <libarch/istate.h>
62#include <vfs/vfs.h>
63
64#include "elf_core.h"
65
66static off64_t align_foff_up(off64_t, uintptr_t, size_t);
67static int write_mem_area(int, aoff64_t *, as_area_info_t *, async_sess_t *);
68
69#define BUFFER_SIZE 0x1000
70static uint8_t buffer[BUFFER_SIZE];
71
72/** Save ELF core file.
73 *
74 * @param file_name Name of file to save to.
75 * @param ainfo Array of @a n memory area info structures.
76 * @param n Number of memory areas.
77 * @param sess Debugging session.
78 *
79 * @return EOK on sucess.
80 * @return ENOENT if file cannot be created.
81 * @return ENOMEM on out of memory.
82 * @return EIO on write error.
83 *
84 */
85int elf_core_save(const char *file_name, as_area_info_t *ainfo, unsigned int n,
86 async_sess_t *sess, istate_t *istate)
87{
88 elf_header_t elf_hdr;
89 off64_t foff;
90 size_t n_ph;
91 elf_word flags;
92 elf_segment_header_t *p_hdr;
93 elf_prstatus_t pr_status;
94 elf_note_t note;
95 size_t word_size;
96 aoff64_t pos = 0;
97
98 int fd;
99 ssize_t rc;
100 unsigned int i;
101
102#ifdef __32_BITS__
103 word_size = 4;
104#endif
105#ifdef __64_BITS__
106 /*
107 * This should be 8 per the 64-bit ELF spec, but the Linux kernel
108 * screws up and uses 4 anyway (and screws up elf_note_t as well)
109 * and we are trying to be compatible with Linux GDB target. Sigh.
110 */
111 word_size = 4;
112#endif
113 memset(&pr_status, 0, sizeof(pr_status));
114 istate_to_elf_regs(istate, &pr_status.regs);
115
116 n_ph = n + 1;
117
118 p_hdr = malloc(sizeof(elf_segment_header_t) * n_ph);
119 if (p_hdr == NULL) {
120 printf("Failed allocating memory.\n");
121 return ENOMEM;
122 }
123
124 fd = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MAY_CREATE,
125 MODE_WRITE);
126 if (fd < 0) {
127 printf("Failed opening file.\n");
128 free(p_hdr);
129 return ENOENT;
130 }
131
132 /*
133 * File layout:
134 *
135 * ELF header
136 * program headers
137 * note segment
138 * repeat:
139 * (pad for alignment)
140 * core segment
141 * end repeat
142 */
143
144 memset(&elf_hdr, 0, sizeof(elf_hdr));
145 elf_hdr.e_ident[EI_MAG0] = ELFMAG0;
146 elf_hdr.e_ident[EI_MAG1] = ELFMAG1;
147 elf_hdr.e_ident[EI_MAG2] = ELFMAG2;
148 elf_hdr.e_ident[EI_MAG3] = ELFMAG3;
149 elf_hdr.e_ident[EI_CLASS] = ELF_CLASS;
150 elf_hdr.e_ident[EI_DATA] = ELF_DATA_ENCODING;
151 elf_hdr.e_ident[EI_VERSION] = EV_CURRENT;
152
153 elf_hdr.e_type = ET_CORE;
154 elf_hdr.e_machine = ELF_MACHINE;
155 elf_hdr.e_version = EV_CURRENT;
156 elf_hdr.e_entry = 0;
157 elf_hdr.e_phoff = sizeof(elf_header_t);
158 elf_hdr.e_shoff = 0;
159 elf_hdr.e_flags = 0;
160 elf_hdr.e_ehsize = sizeof(elf_hdr);
161 elf_hdr.e_phentsize = sizeof(elf_segment_header_t);
162 elf_hdr.e_phnum = n_ph;
163 elf_hdr.e_shentsize = 0;
164 elf_hdr.e_shnum = 0;
165 elf_hdr.e_shstrndx = 0;
166
167 /* foff is used for allocation of file space for segment data. */
168 foff = elf_hdr.e_phoff + n_ph * sizeof(elf_segment_header_t);
169
170 memset(&p_hdr[0], 0, sizeof(p_hdr[0]));
171 p_hdr[0].p_type = PT_NOTE;
172 p_hdr[0].p_offset = foff;
173 p_hdr[0].p_vaddr = 0;
174 p_hdr[0].p_paddr = 0;
175 p_hdr[0].p_filesz = sizeof(elf_note_t)
176 + ALIGN_UP((str_size("CORE") + 1), word_size)
177 + ALIGN_UP(sizeof(elf_prstatus_t), word_size);
178 p_hdr[0].p_memsz = 0;
179 p_hdr[0].p_flags = 0;
180 p_hdr[0].p_align = 1;
181
182 foff += p_hdr[0].p_filesz;
183
184 for (i = 0; i < n; ++i) {
185 foff = align_foff_up(foff, ainfo[i].start_addr, PAGE_SIZE);
186
187 flags = 0;
188 if (ainfo[i].flags & AS_AREA_READ)
189 flags |= PF_R;
190 if (ainfo[i].flags & AS_AREA_WRITE)
191 flags |= PF_W;
192 if (ainfo[i].flags & AS_AREA_EXEC)
193 flags |= PF_X;
194
195 memset(&p_hdr[i + 1], 0, sizeof(p_hdr[i + 1]));
196 p_hdr[i + 1].p_type = PT_LOAD;
197 p_hdr[i + 1].p_offset = foff;
198 p_hdr[i + 1].p_vaddr = ainfo[i].start_addr;
199 p_hdr[i + 1].p_paddr = 0;
200 p_hdr[i + 1].p_filesz = ainfo[i].size;
201 p_hdr[i + 1].p_memsz = ainfo[i].size;
202 p_hdr[i + 1].p_flags = flags;
203 p_hdr[i + 1].p_align = PAGE_SIZE;
204
205 foff += ainfo[i].size;
206 }
207
208 rc = vfs_write(fd, &pos, &elf_hdr, sizeof(elf_hdr));
209 if (rc != sizeof(elf_hdr)) {
210 printf("Failed writing ELF header.\n");
211 free(p_hdr);
212 return EIO;
213 }
214
215 for (i = 0; i < n_ph; ++i) {
216 rc = vfs_write(fd, &pos, &p_hdr[i], sizeof(p_hdr[i]));
217 if (rc != sizeof(p_hdr[i])) {
218 printf("Failed writing program header.\n");
219 free(p_hdr);
220 return EIO;
221 }
222 }
223
224 pos = p_hdr[0].p_offset;
225
226 /*
227 * Write note header
228 */
229 note.namesz = str_size("CORE") + 1;
230 note.descsz = sizeof(elf_prstatus_t);
231 note.type = NT_PRSTATUS;
232
233 rc = vfs_write(fd, &pos, &note, sizeof(elf_note_t));
234 if (rc != sizeof(elf_note_t)) {
235 printf("Failed writing note header.\n");
236 free(p_hdr);
237 return EIO;
238 }
239
240 rc = vfs_write(fd, &pos, "CORE", note.namesz);
241 if (rc != (ssize_t) note.namesz) {
242 printf("Failed writing note header.\n");
243 free(p_hdr);
244 return EIO;
245 }
246
247 pos = ALIGN_UP(pos, word_size);
248
249 rc = vfs_write(fd, &pos, &pr_status, sizeof(elf_prstatus_t));
250 if (rc != sizeof(elf_prstatus_t)) {
251 printf("Failed writing register data.\n");
252 free(p_hdr);
253 return EIO;
254 }
255
256 for (i = 1; i < n_ph; ++i) {
257 pos = p_hdr[i].p_offset;
258 if (write_mem_area(fd, &pos, &ainfo[i - 1], sess) != EOK) {
259 printf("Failed writing memory data.\n");
260 free(p_hdr);
261 return EIO;
262 }
263 }
264
265 free(p_hdr);
266
267 return EOK;
268}
269
270/** Align file offset up to be congruent with vaddr modulo page size. */
271static off64_t align_foff_up(off64_t foff, uintptr_t vaddr, size_t page_size)
272{
273 off64_t rva = vaddr % page_size;
274 off64_t rfo = foff % page_size;
275
276 if (rva >= rfo)
277 return (foff + (rva - rfo));
278
279 return (foff + (page_size + (rva - rfo)));
280}
281
282/** Write memory area from application to core file.
283 *
284 * @param fd File to write to.
285 * @param pos Pointer to the position to write to.
286 * @param area Memory area info structure.
287 * @param sess Debugging session.
288 *
289 * @return EOK on success, EIO on failure.
290 *
291 */
292static int write_mem_area(int fd, aoff64_t *pos, as_area_info_t *area,
293 async_sess_t *sess)
294{
295 size_t to_copy;
296 size_t total;
297 uintptr_t addr;
298 ssize_t rc;
299
300 addr = area->start_addr;
301 total = 0;
302
303 while (total < area->size) {
304 to_copy = min(area->size - total, BUFFER_SIZE);
305 rc = udebug_mem_read(sess, buffer, addr, to_copy);
306 if (rc < 0) {
307 printf("Failed reading task memory.\n");
308 return EIO;
309 }
310
311 rc = vfs_write(fd, pos, buffer, to_copy);
312 if (rc != (ssize_t) to_copy) {
313 printf("Failed writing memory contents.\n");
314 return EIO;
315 }
316
317 addr += to_copy;
318 total += to_copy;
319 }
320
321 return EOK;
322}
323
324/** @}
325 */
Note: See TracBrowser for help on using the repository browser.