Home | History | Annotate | Line # | Download | only in gcore
gcore.c revision 1.9.12.1
      1  1.9.12.1      matt /*	$NetBSD: gcore.c,v 1.9.12.1 2008/01/09 02:00:41 matt 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.9.12.1      matt __RCSID("$NetBSD: gcore.c,v 1.9.12.1 2008/01/09 02:00:41 matt 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.9  christos #include <sys/wait.h>
     48       1.1       tls 
     49       1.4  christos #include <stdio.h>
     50       1.8  christos #include <string.h>
     51       1.4  christos #include <err.h>
     52       1.1       tls #include <limits.h>
     53       1.1       tls #include <stdlib.h>
     54       1.1       tls #include <unistd.h>
     55       1.4  christos #include <errno.h>
     56       1.1       tls 
     57  1.9.12.1      matt static void usage(void) __dead;
     58       1.1       tls 
     59       1.4  christos static void
     60       1.4  christos usage(void)
     61       1.4  christos {
     62       1.8  christos 	(void)fprintf(stderr, "Usage: %s [-c <corename>] <pid> [<pid> ...]\n",
     63       1.8  christos 	    getprogname());
     64       1.4  christos 	exit(1);
     65       1.4  christos }
     66       1.1       tls 
     67       1.1       tls 
     68       1.1       tls int
     69       1.4  christos main(int argc, char **argv)
     70       1.1       tls {
     71       1.4  christos 	int c;
     72       1.8  christos 	char *corename = NULL;
     73       1.8  christos 	int corelen = 0;
     74       1.4  christos 
     75       1.8  christos 	while ((c = getopt(argc, argv, "c:")) != -1)
     76       1.4  christos 		switch (c) {
     77       1.8  christos 		case 'c':
     78       1.8  christos 			corename = optarg;
     79       1.8  christos 			corelen = strlen(corename);
     80       1.8  christos 			break;
     81       1.4  christos 		case '?':
     82       1.1       tls 		default:
     83       1.1       tls 			usage();
     84       1.4  christos 			/*NOTREACHED*/
     85       1.1       tls 		}
     86       1.1       tls 
     87       1.4  christos 	if (optind == argc)
     88       1.1       tls 		usage();
     89       1.1       tls 
     90       1.4  christos 	for (c = optind; c < argc; c++) {
     91       1.4  christos 		char *ep;
     92       1.9  christos 		u_long lval;
     93       1.9  christos 		int status, serrno;
     94       1.9  christos 		pid_t pid, cpid;
     95       1.4  christos 
     96       1.6     lukem 		errno = 0;
     97       1.9  christos 		lval = strtoul(argv[c], &ep, 0);
     98       1.4  christos 		if (argv[c] == '\0' || *ep)
     99       1.4  christos 			errx(1, "`%s' is not a number.", argv[c]);
    100       1.9  christos 
    101       1.9  christos 		if (errno == ERANGE && lval == ULONG_MAX)
    102       1.4  christos 			err(1, "Pid `%s'", argv[c]);
    103       1.9  christos 
    104       1.9  christos 		pid = (pid_t)lval;
    105       1.9  christos 
    106       1.9  christos 		if (ptrace(PT_ATTACH, pid, 0, 0) == -1)
    107       1.9  christos 			err(1, "ptrace(PT_ATTACH) to %lu failed", lval);
    108       1.9  christos 
    109       1.9  christos 		if ((cpid = waitpid(pid, &status, 0)) != pid) {
    110       1.9  christos 			serrno = errno;
    111       1.9  christos 			(void)ptrace(PT_DETACH, pid, (void *)1, 0);
    112       1.9  christos 			errno = serrno;
    113       1.9  christos 			if (cpid == -1)
    114       1.9  christos 				err(1, "Cannot wait for %lu", lval);
    115       1.9  christos 			else
    116       1.9  christos 				errx(1, "Unexpected process %lu waited",
    117       1.9  christos 				    (unsigned long)cpid);
    118       1.9  christos 		}
    119       1.9  christos 
    120       1.9  christos 		if (ptrace(PT_DUMPCORE, pid, corename, corelen) == -1) {
    121       1.9  christos 			serrno = errno;
    122       1.9  christos 			(void)ptrace(PT_DETACH, pid, (void *)1, 0);
    123       1.9  christos 			errno = serrno;
    124       1.9  christos 			err(1, "ptrace(PT_DUMPCORE) to %lu failed", lval);
    125       1.9  christos 		}
    126       1.9  christos 		if (ptrace(PT_DETACH, pid, (void *)1, 0) == -1)
    127       1.9  christos 			err(1, "ptrace(PT_DETACH) to %lu failed", lval);
    128       1.1       tls 	}
    129       1.4  christos 	return 0;
    130       1.1       tls }
    131