gcore.c revision 1.8 1 1.8 christos /* $NetBSD: gcore.c,v 1.8 2005/01/09 17:47:45 christos Exp $ */
2 1.2 tls
3 1.1 tls /*-
4 1.4 christos * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.4 christos * All rights reserved.
6 1.4 christos *
7 1.4 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.4 christos * by Christos Zoulas.
9 1.1 tls *
10 1.1 tls * Redistribution and use in source and binary forms, with or without
11 1.1 tls * modification, are permitted provided that the following conditions
12 1.1 tls * are met:
13 1.1 tls * 1. Redistributions of source code must retain the above copyright
14 1.1 tls * notice, this list of conditions and the following disclaimer.
15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tls * notice, this list of conditions and the following disclaimer in the
17 1.1 tls * documentation and/or other materials provided with the distribution.
18 1.1 tls * 3. All advertising materials mentioning features or use of this software
19 1.1 tls * must display the following acknowledgement:
20 1.4 christos * This product includes software developed by the NetBSD
21 1.4 christos * Foundation, Inc. and its contributors.
22 1.4 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4 christos * contributors may be used to endorse or promote products derived
24 1.4 christos * from this software without specific prior written permission.
25 1.1 tls *
26 1.4 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.4 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 tls */
38 1.1 tls
39 1.4 christos #include <sys/cdefs.h>
40 1.8 christos __RCSID("$NetBSD: gcore.c,v 1.8 2005/01/09 17:47:45 christos Exp $");
41 1.4 christos
42 1.4 christos #include <sys/types.h>
43 1.1 tls #include <sys/param.h>
44 1.1 tls #include <sys/time.h>
45 1.4 christos #include <sys/ptrace.h>
46 1.1 tls #include <sys/proc.h>
47 1.1 tls
48 1.4 christos #include <stdio.h>
49 1.8 christos #include <string.h>
50 1.4 christos #include <err.h>
51 1.1 tls #include <limits.h>
52 1.1 tls #include <stdlib.h>
53 1.1 tls #include <unistd.h>
54 1.4 christos #include <errno.h>
55 1.1 tls
56 1.4 christos static void usage(void) __attribute__((__noreturn__));
57 1.1 tls
58 1.4 christos static void
59 1.4 christos usage(void)
60 1.4 christos {
61 1.8 christos (void)fprintf(stderr, "Usage: %s [-c <corename>] <pid> [<pid> ...]\n",
62 1.8 christos getprogname());
63 1.4 christos exit(1);
64 1.4 christos }
65 1.1 tls
66 1.1 tls
67 1.1 tls int
68 1.4 christos main(int argc, char **argv)
69 1.1 tls {
70 1.4 christos int c;
71 1.8 christos char *corename = NULL;
72 1.8 christos int corelen = 0;
73 1.4 christos
74 1.8 christos while ((c = getopt(argc, argv, "c:")) != -1)
75 1.4 christos switch (c) {
76 1.8 christos case 'c':
77 1.8 christos corename = optarg;
78 1.8 christos corelen = strlen(corename);
79 1.8 christos break;
80 1.4 christos case '?':
81 1.1 tls default:
82 1.1 tls usage();
83 1.4 christos /*NOTREACHED*/
84 1.1 tls }
85 1.1 tls
86 1.4 christos if (optind == argc)
87 1.1 tls usage();
88 1.1 tls
89 1.4 christos for (c = optind; c < argc; c++) {
90 1.4 christos char *ep;
91 1.6 lukem long lval;
92 1.4 christos
93 1.6 lukem errno = 0;
94 1.6 lukem lval = strtol(argv[c], &ep, 0);
95 1.4 christos if (argv[c] == '\0' || *ep)
96 1.4 christos errx(1, "`%s' is not a number.", argv[c]);
97 1.4 christos if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
98 1.4 christos err(1, "Pid `%s'", argv[c]);
99 1.8 christos if (ptrace(PT_DUMPCORE, (pid_t)lval, corename, corelen) == -1)
100 1.4 christos err(1, "ptrace(PT_DUMPCORE) failed");
101 1.1 tls }
102 1.4 christos return 0;
103 1.1 tls }
104