source: mainline/uspace/app/taskdump/elf_core.c@ 4cca9a9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4cca9a9 was 125c09c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Fix inadvertently introduced build error.

  • 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 <str_error.h>
58#include <mem.h>
59#include <as.h>
60#include <udebug.h>
61#include <macros.h>
62#include <libarch/istate.h>
63#include <vfs/vfs.h>
64
65#include "elf_core.h"
66
67static off64_t align_foff_up(off64_t, uintptr_t, size_t);
68static int write_mem_area(int, aoff64_t *, as_area_info_t *, async_sess_t *);
69
70#define BUFFER_SIZE 0x1000
71static uint8_t buffer[BUFFER_SIZE];
72
73/** Save ELF core file.
74 *
75 * @param file_name Name of file to save to.
76 * @param ainfo Array of @a n memory area info structures.
77 * @param n Number of memory areas.
78 * @param sess Debugging session.
79 *
80 * @return EOK on sucess.
81 * @return ENOENT if file cannot be created.
82 * @return ENOMEM on out of memory.
83 * @return EIO on write error.
84 *
85 */
86int elf_core_save(const char *file_name, as_area_info_t *ainfo, unsigned int n,
87 async_sess_t *sess, istate_t *istate)
88{
89 elf_header_t elf_hdr;
90 off64_t foff;
91 size_t n_ph;
92 elf_word flags;
93 elf_segment_header_t *p_hdr;
94 elf_prstatus_t pr_status;
95 elf_note_t note;
96 size_t word_size;
97 aoff64_t pos = 0;
98
99 int fd;
100 int rc;
101 size_t nwr;
102 unsigned int i;
103
104#ifdef __32_BITS__
105 word_size = 4;
106#endif
107#ifdef __64_BITS__
108 /*
109 * This should be 8 per the 64-bit ELF spec, but the Linux kernel
110 * screws up and uses 4 anyway (and screws up elf_note_t as well)
111 * and we are trying to be compatible with Linux GDB target. Sigh.
112 */
113 word_size = 4;
114#endif
115 memset(&pr_status, 0, sizeof(pr_status));
116 istate_to_elf_regs(istate, &pr_status.regs);
117
118 n_ph = n + 1;
119
120 p_hdr = malloc(sizeof(elf_segment_header_t) * n_ph);
121 if (p_hdr == NULL) {
122 printf("Failed allocating memory.\n");
123 return ENOMEM;
124 }
125
126 rc = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MAY_CREATE,
127 MODE_WRITE, &fd);
128 if (rc != EOK) {
129 printf("Failed opening file '%s': %s.\n", file_name, str_error(rc));
130 free(p_hdr);
131 return ENOENT;
132 }
133
134 /*
135 * File layout:
136 *
137 * ELF header
138 * program headers
139 * note segment
140 * repeat:
141 * (pad for alignment)
142 * core segment
143 * end repeat
144 */
145
146 memset(&elf_hdr, 0, sizeof(elf_hdr));
147 elf_hdr.e_ident[EI_MAG0] = ELFMAG0;
148 elf_hdr.e_ident[EI_MAG1] = ELFMAG1;
149 elf_hdr.e_ident[EI_MAG2] = ELFMAG2;
150 elf_hdr.e_ident[EI_MAG3] = ELFMAG3;
151 elf_hdr.e_ident[EI_CLASS] = ELF_CLASS;
152 elf_hdr.e_ident[EI_DATA] = ELF_DATA_ENCODING;
153 elf_hdr.e_ident[EI_VERSION] = EV_CURRENT;
154
155 elf_hdr.e_type = ET_CORE;
156 elf_hdr.e_machine = ELF_MACHINE;
157 elf_hdr.e_version = EV_CURRENT;
158 elf_hdr.e_entry = 0;
159 elf_hdr.e_phoff = sizeof(elf_header_t);
160 elf_hdr.e_shoff = 0;
161 elf_hdr.e_flags = 0;
162 elf_hdr.e_ehsize = sizeof(elf_hdr);
163 elf_hdr.e_phentsize = sizeof(elf_segment_header_t);
164 elf_hdr.e_phnum = n_ph;
165 elf_hdr.e_shentsize = 0;
166 elf_hdr.e_shnum = 0;
167 elf_hdr.e_shstrndx = 0;
168
169 /* foff is used for allocation of file space for segment data. */
170 foff = elf_hdr.e_phoff + n_ph * sizeof(elf_segment_header_t);
171
172 memset(&p_hdr[0], 0, sizeof(p_hdr[0]));
173 p_hdr[0].p_type = PT_NOTE;
174 p_hdr[0].p_offset = foff;
175 p_hdr[0].p_vaddr = 0;
176 p_hdr[0].p_paddr = 0;
177 p_hdr[0].p_filesz = sizeof(elf_note_t)
178 + ALIGN_UP((str_size("CORE") + 1), word_size)
179 + ALIGN_UP(sizeof(elf_prstatus_t), word_size);
180 p_hdr[0].p_memsz = 0;
181 p_hdr[0].p_flags = 0;
182 p_hdr[0].p_align = 1;
183
184 foff += p_hdr[0].p_filesz;
185
186 for (i = 0; i < n; ++i) {
187 foff = align_foff_up(foff, ainfo[i].start_addr, PAGE_SIZE);
188
189 flags = 0;
190 if (ainfo[i].flags & AS_AREA_READ)
191 flags |= PF_R;
192 if (ainfo[i].flags & AS_AREA_WRITE)
193 flags |= PF_W;
194 if (ainfo[i].flags & AS_AREA_EXEC)
195 flags |= PF_X;
196
197 memset(&p_hdr[i + 1], 0, sizeof(p_hdr[i + 1]));
198 p_hdr[i + 1].p_type = PT_LOAD;
199 p_hdr[i + 1].p_offset = foff;
200 p_hdr[i + 1].p_vaddr = ainfo[i].start_addr;
201 p_hdr[i + 1].p_paddr = 0;
202 p_hdr[i + 1].p_filesz = ainfo[i].size;
203 p_hdr[i + 1].p_memsz = ainfo[i].size;
204 p_hdr[i + 1].p_flags = flags;
205 p_hdr[i + 1].p_align = PAGE_SIZE;
206
207 foff += ainfo[i].size;
208 }
209
210 rc = vfs_write(fd, &pos, &elf_hdr, sizeof(elf_hdr), &nwr);
211 if (rc != EOK) {
212 printf("Failed writing ELF header.\n");
213 free(p_hdr);
214 return EIO;
215 }
216
217 for (i = 0; i < n_ph; ++i) {
218 rc = vfs_write(fd, &pos, &p_hdr[i], sizeof(p_hdr[i]), &nwr);
219 if (rc != EOK) {
220 printf("Failed writing program header.\n");
221 free(p_hdr);
222 return EIO;
223 }
224 }
225
226 pos = p_hdr[0].p_offset;
227
228 /*
229 * Write note header
230 */
231 note.namesz = str_size("CORE") + 1;
232 note.descsz = sizeof(elf_prstatus_t);
233 note.type = NT_PRSTATUS;
234
235 rc = vfs_write(fd, &pos, &note, sizeof(elf_note_t), &nwr);
236 if (rc != EOK) {
237 printf("Failed writing note header.\n");
238 free(p_hdr);
239 return EIO;
240 }
241
242 rc = vfs_write(fd, &pos, "CORE", note.namesz, &nwr);
243 if (rc != EOK) {
244 printf("Failed writing note header.\n");
245 free(p_hdr);
246 return EIO;
247 }
248
249 pos = ALIGN_UP(pos, word_size);
250
251 rc = vfs_write(fd, &pos, &pr_status, sizeof(elf_prstatus_t), &nwr);
252 if (rc != EOK) {
253 printf("Failed writing register data.\n");
254 free(p_hdr);
255 return EIO;
256 }
257
258 for (i = 1; i < n_ph; ++i) {
259 pos = p_hdr[i].p_offset;
260 if (write_mem_area(fd, &pos, &ainfo[i - 1], sess) != EOK) {
261 printf("Failed writing memory data.\n");
262 free(p_hdr);
263 return EIO;
264 }
265 }
266
267 free(p_hdr);
268
269 return EOK;
270}
271
272/** Align file offset up to be congruent with vaddr modulo page size. */
273static off64_t align_foff_up(off64_t foff, uintptr_t vaddr, size_t page_size)
274{
275 off64_t rva = vaddr % page_size;
276 off64_t rfo = foff % page_size;
277
278 if (rva >= rfo)
279 return (foff + (rva - rfo));
280
281 return (foff + (page_size + (rva - rfo)));
282}
283
284/** Write memory area from application to core file.
285 *
286 * @param fd File to write to.
287 * @param pos Pointer to the position to write to.
288 * @param area Memory area info structure.
289 * @param sess Debugging session.
290 *
291 * @return EOK on success, EIO on failure.
292 *
293 */
294static int write_mem_area(int fd, aoff64_t *pos, as_area_info_t *area,
295 async_sess_t *sess)
296{
297 size_t to_copy;
298 size_t total;
299 uintptr_t addr;
300 int rc;
301 size_t nwr;
302
303 addr = area->start_addr;
304 total = 0;
305
306 while (total < area->size) {
307 to_copy = min(area->size - total, BUFFER_SIZE);
308 rc = udebug_mem_read(sess, buffer, addr, to_copy);
309 if (rc < 0) {
310 printf("Failed reading task memory.\n");
311 return EIO;
312 }
313
314 rc = vfs_write(fd, pos, buffer, to_copy, &nwr);
315 if (rc != EOK) {
316 printf("Failed writing memory contents.\n");
317 return EIO;
318 }
319
320 addr += to_copy;
321 total += to_copy;
322 }
323
324 return EOK;
325}
326
327/** @}
328 */
Note: See TracBrowser for help on using the repository browser.