summaryrefslogtreecommitdiff
path: root/tools/mdb/extract_dmod.c
blob: 0efc94a8f8e6c4b1624f12d7b3bc97a8f3c5e5bf (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
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/*
 * gcc -Wall -lproc -o extract_dmod extract_dmod.c
 *
 * use this tool to extract the mdb_v8.so from a core file for use with older
 * mdb or platforms with an out of date v8.so
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <errno.h>
#include <libproc.h>
#include <sys/sysmacros.h>

const char *g_sym = "mdb_v8.so_start";

int
main(int argc, const char *argv[])
{
	int ifd, ofd, perr, toread, ret;
	struct ps_prochandle *p;
	GElf_Sym sym;
	prsyminfo_t si;
	char buf[1024];

	if (argc != 3) {
		fprintf(stderr, "pdump: <infile> <outfile>\n");
		return (1);
	}

	ifd = open(argv[1], O_RDONLY);
	if (ifd < 0) {
		fprintf(stderr, "failed to open: %s: %s\n",
		    argv[1], strerror(errno));
		return (1);
	}
	ofd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC);
	if (ofd < 0) {
		fprintf(stderr, "failed to open: %s: %s\n",
		    argv[1], strerror(errno));
		return (1);
	}

	p = Pfgrab_core(ifd, NULL, &perr); 
	if (p == NULL) {
		fprintf(stderr, "failed to grab core file\n");
		return (1);
	}

	if (Pxlookup_by_name(p, NULL, "a.out", g_sym, &sym, &si) != 0) {
		fprintf(stderr, "failed to lookup symobl %s\n", g_sym);
		return (0);
	}

	while (sym.st_size > 0) {
		toread = MIN(sym.st_size, sizeof (buf));
		ret = Pread(p, buf, toread, sym.st_value);
		if (ret < 0) {
			fprintf(stderr, "failed to Pread...\n");
			return (1);
		}
		if (ret != 0)
			ret = write(ofd, buf, ret);
		if (ret < 0) {
			fprintf(stderr, "failed to write to output file %s\n",
			    strerror(errno));
		}
		sym.st_size -= ret;
		sym.st_value += ret;
	}

	return (0);
}