gcore.c revision 1.4 1 1.4 christos /* $NetBSD: gcore.c,v 1.4 2003/01/23 17:34:50 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.4 christos __RCSID("$NetBSD: gcore.c,v 1.4 2003/01/23 17:34:50 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.4 christos #include <err.h>
50 1.1 tls #include <limits.h>
51 1.1 tls #include <stdlib.h>
52 1.1 tls #include <unistd.h>
53 1.4 christos #include <errno.h>
54 1.1 tls
55 1.4 christos static void usage(void) __attribute__((__noreturn__));
56 1.1 tls
57 1.4 christos static void
58 1.4 christos usage(void)
59 1.4 christos {
60 1.4 christos (void)fprintf(stderr, "Usage: %s <pid> [...<pid>]", getprogname());
61 1.4 christos exit(1);
62 1.4 christos }
63 1.1 tls
64 1.1 tls
65 1.1 tls int
66 1.4 christos main(int argc, char **argv)
67 1.1 tls {
68 1.4 christos int c;
69 1.4 christos
70 1.4 christos while ((c = getopt(argc, argv, "o:")) != -1)
71 1.4 christos switch (c) {
72 1.4 christos case 'o':
73 1.4 christos errx(1, "-o is not supported yet.");
74 1.4 christos /*NOTREACHED*/
75 1.4 christos case '?':
76 1.1 tls default:
77 1.1 tls usage();
78 1.4 christos /*NOTREACHED*/
79 1.1 tls }
80 1.1 tls
81 1.4 christos if (optind == argc)
82 1.1 tls usage();
83 1.1 tls
84 1.4 christos for (c = optind; c < argc; c++) {
85 1.4 christos char *ep;
86 1.4 christos long lval = strtol(argv[c], &ep, 0);
87 1.4 christos
88 1.4 christos if (argv[c] == '\0' || *ep)
89 1.4 christos errx(1, "`%s' is not a number.", argv[c]);
90 1.4 christos if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
91 1.4 christos err(1, "Pid `%s'", argv[c]);
92 1.4 christos if (ptrace(PT_DUMPCORE, (pid_t)lval, NULL, 0) == -1)
93 1.4 christos err(1, "ptrace(PT_DUMPCORE) failed");
94 1.1 tls }
95 1.4 christos return 0;
96 1.1 tls }
97