The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / tools / mdb / extract_dmod.c @ e851fef6

History | View | Annotate | Download (2.73 KB)

1
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
 *
3
 * Permission is hereby granted, free of charge, to any person obtaining a copy
4
 * of this software and associated documentation files (the "Software"), to
5
 * deal in the Software without restriction, including without limitation the
6
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
 * sell copies of the Software, and to permit persons to whom the Software is
8
 * furnished to do so, subject to the following conditions:
9
 *
10
 * The above copyright notice and this permission notice shall be included in
11
 * all copies or substantial portions of the Software.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
 * IN THE SOFTWARE.
20
 */
21

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

    
29
#include <stdio.h>
30
#include <unistd.h>
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
34
#include <strings.h>
35
#include <errno.h>
36
#include <libproc.h>
37
#include <sys/sysmacros.h>
38

    
39
const char *g_sym = "mdb_v8.so_start";
40

    
41
int
42
main(int argc, const char *argv[])
43
{
44
        int ifd, ofd, perr, toread, ret;
45
        struct ps_prochandle *p;
46
        GElf_Sym sym;
47
        prsyminfo_t si;
48
        char buf[1024];
49

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

    
55
        ifd = open(argv[1], O_RDONLY);
56
        if (ifd < 0) {
57
                fprintf(stderr, "failed to open: %s: %s\n",
58
                    argv[1], strerror(errno));
59
                return (1);
60
        }
61
        ofd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC);
62
        if (ofd < 0) {
63
                fprintf(stderr, "failed to open: %s: %s\n",
64
                    argv[1], strerror(errno));
65
                return (1);
66
        }
67

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

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

    
79
        while (sym.st_size > 0) {
80
                toread = MIN(sym.st_size, sizeof (buf));
81
                ret = Pread(p, buf, toread, sym.st_value);
82
                if (ret < 0) {
83
                        fprintf(stderr, "failed to Pread...\n");
84
                        return (1);
85
                }
86
                if (ret != 0)
87
                        ret = write(ofd, buf, ret);
88
                if (ret < 0) {
89
                        fprintf(stderr, "failed to write to output file %s\n",
90
                            strerror(errno));
91
                }
92
                sym.st_size -= ret;
93
                sym.st_value += ret;
94
        }
95

    
96
        return (0);
97
}