summaryrefslogtreecommitdiff
path: root/mkminidump.c
blob: 2fd100007f6d9ab150b90f4dd60bea972d86dbdf (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
/*-*- 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 <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>

#include "minidump.h"

static const char *arg_source = NULL;
static pid_t arg_pid = 0;
static const char *arg_minidump = NULL;
static const char *arg_minicore = NULL;

static int help(void) {

        printf("%s [OPTIONS...] [FILE]\n\n"
               "Generate, convert or show contents of a minidump or minicore/coredump.\n\n"
               "  -h --help             Show this help\n"
               "  -p --pid=PID          Generate from PID\n"
               "  -d --minidump[=FILE]  Generate a minidump\n"
               "  -c --minicore[=FILE]  Generate a minicore\n\n"
               "Examples:\n\n"
               "    Generate a minidump from PID 4711\n"
               "    # mkminidump --pid=4711 --minidump=foobar.dmp\n\n"
               "    Generate a minicore from PID 815\n"
               "    # mkminidump --pid=815 --minicore=foobar.core\n\n"
               "    Convert a coredump to a minidump\n"
               "    # mkminidump foobar.core --minidump=foobar.dmp\n\n"
               "    Convert a minidump to a minicore\n"
               "    # mkminidump foobar.dmp --minicore=foobar.core\n\n"
               "    Show contents of a minidump\n"
               "    # mkminidump fooobar.dmp\n\n"
               "    Show contents of a coredump\n"
               "    # mkminidump foobar.core\n\n",
               program_invocation_short_name);

        return 0;
}

static int parse_argv(int argc, char *argv[]) {

        enum {
                ARG_VERSION = 0x100
        };

        static const struct option options[] = {
                { "help",      no_argument,       NULL, 'h'           },
                { "pid",       required_argument, NULL, 'p'           },
                { "minidump",  optional_argument, NULL, 'd'           },
                { "minicore",  optional_argument, NULL, 'c'           },
                { NULL,        0,                 NULL, 0             }
        };

        int c;

        assert(argc >= 0);
        assert(argv);

        while ((c = getopt_long(argc, argv, "hp:d::c::", options, NULL)) >= 0) {

                switch (c) {

                case 'h':
                        help();
                        return 0;

                case 'p': {
                        unsigned long ul;
                        char *e;

                        errno = 0;
                        ul = strtoul(optarg, &e, 10);
                        if (errno != 0 || !e || *e || ul <= 0) {
                                fprintf(stderr, "Failed to parse PID argument.\n");
                                return -EINVAL;
                        }

                        arg_pid = (pid_t) ul;
                        break;
                }

                case 'd':
                        arg_minidump = optarg ? optarg : "-";
                        break;

                case 'c':
                        arg_minicore = optarg ? optarg : "-";
                        break;

                case '?':
                        return -EINVAL;

                default:
                        fprintf(stderr, "Unknown option code %c\n", c);
                        return -EINVAL;
                }
        }

        if (optind >= argc && !arg_pid) {
                /* No source arguments specified at all */
                help();
                return 0;
        }

        if (argc > optind+1) {
                /* More than one source argument specified */
                help();
                return -EINVAL;
        }

        if (optind < argc)
                arg_source = argv[optind];

        return 1;
}

static int output_and_free(const char *path, void **buffer, size_t *buffer_size) {
        FILE *f, *toclose = NULL;
        int r = 0;

        assert(buffer);
        assert(*buffer);
        assert(buffer_size);
        assert(*buffer_size > 0);

        if (!path || strcmp(path, "-") == 0)
                f = stdout;
        else {
                f = fopen(path, "we");
                if (!f) {
                        r = -errno;
                        fprintf(stderr, "Failed to write output: %m\n");
                        goto finish;
                }
                toclose = f;
        }

        fwrite(*buffer, 1, *buffer_size, f);
        fflush(f);

        if (ferror(f)) {
                r = -errno;
                fprintf(stderr, "Failed to write output: %m\n");
                goto finish;
        }

        free(*buffer);
        *buffer = NULL;
        *buffer_size = 0;

finish:
        if (toclose)
                fclose(toclose);

        return r;
}

int main(int argc, char *argv[]) {
        int r;
        int fd = -1;
        void *buffer = NULL;
        size_t buffer_size = 0;

        r = parse_argv(argc, argv);
        if (r <= 0)
                return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;

        if (arg_source) {
                if (strcmp(arg_source, "-") == 0)
                        fd = STDIN_FILENO;
                else {
                        fd = open(arg_source, O_RDONLY|O_CLOEXEC);

                        if (fd < 0) {
                                r = -errno;
                                fprintf(stderr, "Failed to open source file: %m\n");
                                goto finish;
                        }
                }
        }

        if (arg_pid > 0) {
                /* If a PID specified, the fd definitely refers to a
                 * process or a coredump of some kind */

                if (arg_minidump) {
                        r = minidump_make(arg_pid, fd, &buffer, &buffer_size);
                        if (r < 0) {
                                fprintf(stderr, "Failed to generate minidump: %s\n", strerror(-r));
                                goto finish;
                        }

                        r = output_and_free(arg_minidump, &buffer, &buffer_size);
                        if (r < 0)
                                goto finish;
                }

                if (arg_minicore) {
                        r = minicore_make(arg_pid, fd, &buffer, &buffer_size);
                        if (r < 0) {
                                fprintf(stderr, "Failed to generate minicore: %s\n", strerror(-r));
                                goto finish;
                        }

                        r = output_and_free(arg_minicore, &buffer, &buffer_size);
                        if (r < 0)
                                goto finish;
                }

                if (!arg_minidump && !arg_minicore) {
                        r = coredump_show(stdout, arg_pid, fd);
                        if (r < 0) {
                                fprintf(stderr, "Failed to decode coredump: %s\n", strerror(-r));
                                goto finish;
                        }
                }
        } else {
                assert(fd >= 0);

                /* No PID specified, so let's guess by the output
                 * parameters */

                if (arg_minicore && arg_minidump) {
                        fprintf(stderr, "Can't convert file into its own type.\n");
                        r = -EINVAL;
                        goto finish;
                }

                if (arg_minicore) {
                        r = minidump_to_minicore(fd, &buffer, &buffer_size);
                        if (r == -EINVAL)
                                r = minicore_make(0, fd, &buffer, &buffer_size);

                        if (r < 0) {
                                fprintf(stderr, "Failed to convert to minicore: %s\n", strerror(-r));
                                goto finish;
                        }

                        r = output_and_free(arg_minicore, &buffer, &buffer_size);
                        if (r < 0)
                                goto finish;
                }

                if (arg_minidump) {
                        r = minidump_make(0, fd, &buffer, &buffer_size);
                        if (r < 0) {
                                fprintf(stderr, "Failed to convert to minidump: %s\n", strerror(-r));
                                goto finish;
                        }

                        r = output_and_free(arg_minidump, &buffer, &buffer_size);
                        if (r < 0)
                                goto finish;
                }

                if (!arg_minidump && !arg_minicore) {

                        r = minidump_show(stdout, fd);
                        if (r == -EINVAL)
                                r = coredump_show(stdout, 0, fd);

                        if (r < 0) {
                                fprintf(stderr, "Failed to decode coredump or minidump: %s\n", strerror(-r));
                                goto finish;
                        }
                }
        }

        r = 0;

finish:
        if (fd > 2)
                close(fd);

        free(buffer);

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}