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