summaryrefslogtreecommitdiff
path: root/write-minicore.c
blob: a42b264d323d5f35e53f35c654d0a8016462100e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of libminidump.

  Copyright 2012 Lennart Poettering

  libminidump is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  libminidump is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with libminidump; If not, see
  <http://www.gnu.org/licenses/>.
***/

#include <assert.h>
#include <string.h>
#include <sys/param.h>
#include <stdlib.h>
#include <errno.h>

#include "write-minicore.h"

static int minicore_append_ph(struct context *c, const ElfW(Phdr) *ph) {
        uint32_t i;

        assert(c);

        i = c->minicore_n_phs++;
        assert(i < c->n_write_maps + 1);

        memcpy(c->minicore_phs+i, ph, sizeof(*ph));

        fprintf(stderr, "Appending segment type=0x%x size=%lu\n",
                (unsigned) ph->p_type,
                (unsigned long) ph->p_filesz);

        return 0;
}

static int minicore_write_maps(struct context *c) {
        unsigned i;
        int r;

        assert(c);

        for (i = 0; i < c->n_write_maps; i++) {
                struct map_info *a;
                ElfW(Phdr) ph;
                void *p;
                size_t offset;

                a = c->write_maps + i;

                r = context_reserve_bytes(c, a->extent.size, &p, &offset);
                if (r < 0)
                        return r;

                r = context_read_memory(c, a->extent.address, p, a->extent.size);
                if (r < 0)
                        return r;

                memset(&ph, 0, sizeof(ph));
                ph.p_type = PT_LOAD;
                ph.p_offset = offset;
                ph.p_filesz = a->extent.size;
                ph.p_memsz = a->extent.size;
                ph.p_vaddr = a->extent.address;
                ph.p_flags = PF_W|PF_R|PF_X;

                r = minicore_append_ph(c, &ph);
                if (r < 0)
                        return r;
        }

        return 0;
}

static int minicore_write_one_note(struct context *c, const char *name, ElfW(Word) type, const void *data, size_t length) {
        int r;
        ElfW(Nhdr) nh;

        assert(c);
        assert(name);
        assert(data || length <= 0);

        if (length <= 0)
                return 0;

        memset(&nh, 0, sizeof(nh));
        nh.n_namesz = strlen(name);
        nh.n_descsz = length;
        nh.n_type = type;

        r = context_append_bytes(c, &nh, sizeof(nh), NULL);
        if (r < 0)
                return r;

        r = context_append_bytes(c, name, nh.n_namesz, NULL);
        if (r < 0)
                return r;

        r = context_null_bytes(c, roundup(nh.n_namesz, sizeof(int)) - nh.n_namesz, NULL);
        if (r < 0)
                return r;

        r = context_append_bytes(c, data, nh.n_descsz, NULL);
        if (r < 0)
                return r;

        r = context_null_bytes(c, roundup(nh.n_descsz, sizeof(int)) - nh.n_descsz, NULL);
        if (r < 0)
                return r;

        return 0;
}

static int minicore_write_note_prstatus(struct context *c, struct thread_info *i) {
        struct elf_prstatus synthetic;
        assert(c);
        assert(i);

        if (i->have_prstatus)
                return minicore_write_one_note(c, "CORE", NT_PRSTATUS, &i->prstatus, sizeof(i->prstatus));

        memset(&synthetic, 0, sizeof(synthetic));
        synthetic.pr_pid = i->tid;
        memcpy(&synthetic.pr_reg, &i->regs, sizeof(i->regs));

        return minicore_write_one_note(c, "CORE", NT_PRSTATUS, &synthetic, sizeof(synthetic));
}

static int minicore_write_note_prpsinfo(struct context *c) {
        struct elf_prpsinfo synthetic;

        assert(c);

        if (c->have_prpsinfo)
                return minicore_write_one_note(c, "CORE", NT_PRPSINFO, &c->prpsinfo, sizeof(c->prpsinfo));

        memset(&synthetic, 0, sizeof(synthetic));
        synthetic.pr_pid = c->pid;
        if (c->proc_comm.data)
                memcpy(synthetic.pr_fname, c->proc_comm.data, MIN(sizeof(synthetic.pr_fname), sizeof(c->proc_comm.size)));

        return minicore_write_one_note(c, "CORE", NT_PRPSINFO, &synthetic, sizeof(synthetic));
}

static int minicore_write_note_auxv(struct context *c) {
        assert(c);

        if (c->auxv.data)
                return minicore_write_one_note(c, "CORE", NT_AUXV, c->auxv.data, c->auxv.size);

        return 0;
}

static int minicore_write_note_fpregset(struct context *c, struct thread_info *i) {
        assert(c);

        return minicore_write_one_note(c, "CORE", NT_FPREGSET, &i->fpregs, sizeof(i->fpregs));
}

#ifdef __i386
static int minicore_write_note_fpregset(struct context *c, struct thread_info *i) {
        assert(c);

        return minicore_write_one_note(c, "LINUX", NT_PRXFPREG, &i->fpxregs, sizeof(i->fpxregs));
}
#endif

static int minicore_write_notes_for_thread(struct context *c, unsigned i) {
        int r;

        r = minicore_write_note_prstatus(c, c->threads+i);
        if (r < 0)
                return r;

        if (i == 0) {
                /* The data for the process is written in the middle
                 * of the data of thread #1 */
                r = minicore_write_note_prpsinfo(c);
                if (r < 0)
                        return r;

                r = minicore_write_note_auxv(c);
                if (r < 0)
                        return r;
        }
        r = minicore_write_note_fpregset(c, c->threads+i);
        if (r < 0)
                return r;

#ifdef __i386
        r = minicore_write_note_prxfpreg(c, c->threads+i);
        if (r < 0)
                return r;
#endif

        return 0;
}

static int minicore_write_meta_notes(struct context *c) {
        int r;

        assert(c);

        /* We use the same type identifiers as the minidump logic */

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_MAPS, c->proc_maps.data, c->proc_maps.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_PROC_STATUS, c->proc_status.data, c->proc_status.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_ENVIRON, c->proc_environ.data, c->proc_environ.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_CMD_LINE, c->proc_cmdline.data, c->proc_cmdline.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_COMM, c->proc_comm.data, c->proc_comm.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_ATTR_CURRENT, c->proc_attr_current.data, c->proc_attr_current.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_EXE, c->proc_exe.data, c->proc_exe.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_CPU_INFO, c->proc_cpuinfo.data, c->proc_cpuinfo.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_LSB_RELEASE, c->lsb_release.data, c->lsb_release.size);
        if (r < 0)
                return r;

        r = minicore_write_one_note(c, "LENNART", MINIDUMP_LINUX_OS_RELEASE, c->os_release.data, c->os_release.size);
        if (r < 0)
                return r;

        return 0;
}

static int minicore_write_notes(struct context *c) {
        ElfW(Phdr) ph;
        unsigned i;
        size_t offset;
        int r;

        assert(c);
        assert(c->n_threads > 0);

        offset = c->output_size;

        for (i = 0; i < c->n_threads; i++) {
                r = minicore_write_notes_for_thread(c, i);
                if (r < 0)
                        return r;
        }

        r = minicore_write_meta_notes(c);
        if (r < 0)
                return r;

        memset(&ph, 0, sizeof(ph));
        ph.p_type = PT_NOTE;
        ph.p_offset = offset;
        ph.p_filesz = c->output_size - offset;

        r = minicore_append_ph(c, &ph);
        if (r < 0)
                return r;

        return 0;
}

static int minicore_write_phs(struct context *c) {
        size_t offset;
        ElfW(Ehdr) *h;
        int r;

        assert(c);

        r = context_append_bytes(c, c->minicore_phs, sizeof(ElfW(Phdr)) * c->minicore_n_phs, &offset);
        if (r < 0)
                return r;

        h = c->output;
        h->e_phnum = c->minicore_n_phs;
        h->e_phoff = offset;

        return 0;
}

int minicore_write(struct context *c) {
        ElfW(Ehdr) h;
        int r;

        assert(c);

        memset(&h, 0, sizeof(h));
        memcpy(h.e_ident, ELFMAG, SELFMAG);
        h.e_type = ET_CORE;
        h.e_ehsize = sizeof(ElfW(Ehdr));
        h.e_phentsize = sizeof(ElfW(Phdr));
        h.e_shentsize = sizeof(ElfW(Shdr));

#if __WORDSIZE == 32
        h.e_ident[EI_CLASS] = ELFCLASS32;
#elif __WORDSIZE == 64
        h.e_ident[EI_CLASS] = ELFCLASS64;
#else
#error "Unknown word size."
#endif

#if __BYTE_ORDER == __LITTLE_ENDIAN
        h.e_ident[EI_DATA] = ELFDATA2LSB;
#elif __BYTE_ORDER == __BIG_ENDIAN
        h.e_ident[EI_DATA] = ELFDATA2MSB;
#else
#error "Unknown endianess."
#endif
        h.e_ident[EI_VERSION] = EV_CURRENT;
        h.e_ident[EI_OSABI] = ELFOSABI_NONE;

#if defined(__i386)
        h.e_machine = EM_386;
#elif defined(__x86_64)
        h.e_machine = EM_X86_64;
#else
#error "Unknown machine."
#endif
        h.e_version = EV_CURRENT;

        r = context_append_bytes(c, &h, sizeof(h), NULL);
        if (r < 0)
                return r;

        /* Allocate an array for one segment per map plus one NOTE segment */
        c->minicore_phs = malloc(sizeof(ElfW(Phdr)) * (1 + c->n_write_maps));
        if (!c->minicore_phs)
                return -ENOMEM;

        r = minicore_write_notes(c);
        if (r < 0)
                return r;

        r = minicore_write_maps(c);
        if (r < 0)
                return r;

        r = minicore_write_phs(c);
        if (r < 0)
                return r;

        return 0;
}