xref: /titanic_52/usr/src/cmd/ptools/pldd/pldd.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
29*004388ebScasper #include <stdio_ext.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <libproc.h>
367c478bd9Sstevel@tonic-gate #include <proc_service.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static	int	show_map(void *, const prmap_t *, const char *);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static	char	*command;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int
437c478bd9Sstevel@tonic-gate main(int argc, char **argv)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	int rc = 0;
467c478bd9Sstevel@tonic-gate 	int opt;
477c478bd9Sstevel@tonic-gate 	int errflg = 0;
487c478bd9Sstevel@tonic-gate 	int Fflag = 0;
497c478bd9Sstevel@tonic-gate 	core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON;
507c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
537c478bd9Sstevel@tonic-gate 		command++;
547c478bd9Sstevel@tonic-gate 	else
557c478bd9Sstevel@tonic-gate 		command = argv[0];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/* options */
587c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "F")) != EOF) {
597c478bd9Sstevel@tonic-gate 		switch (opt) {
607c478bd9Sstevel@tonic-gate 		case 'F':		/* force grabbing (no O_EXCL) */
617c478bd9Sstevel@tonic-gate 			Fflag = PGRAB_FORCE;
627c478bd9Sstevel@tonic-gate 			break;
637c478bd9Sstevel@tonic-gate 		default:
647c478bd9Sstevel@tonic-gate 			errflg = 1;
657c478bd9Sstevel@tonic-gate 			break;
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	argc -= optind;
707c478bd9Sstevel@tonic-gate 	argv += optind;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (errflg || argc <= 0) {
737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
747c478bd9Sstevel@tonic-gate 			"usage:\t%s [-F] { pid | core } ...\n", command);
757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
767c478bd9Sstevel@tonic-gate 			"  (report process dynamic libraries)\n");
777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
787c478bd9Sstevel@tonic-gate 			"  -F: force grabbing of the target process\n");
797c478bd9Sstevel@tonic-gate 		return (2);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
847c478bd9Sstevel@tonic-gate 	 * that has many many mappings.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
877c478bd9Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
887c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
89*004388ebScasper 		(void) enable_extended_FILE_stdio(-1, -1);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	(void) proc_initstdio();
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
957c478bd9Sstevel@tonic-gate 		char *arg;
967c478bd9Sstevel@tonic-gate 		int gcode;
977c478bd9Sstevel@tonic-gate 		psinfo_t psinfo;
987c478bd9Sstevel@tonic-gate 		struct ps_prochandle *Pr;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		(void) proc_flushstdio();
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
1037c478bd9Sstevel@tonic-gate 		    PGRAB_RETAIN | Fflag, &gcode)) == NULL) {
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1067c478bd9Sstevel@tonic-gate 			    command, arg, Pgrab_error(gcode));
1077c478bd9Sstevel@tonic-gate 			rc++;
1087c478bd9Sstevel@tonic-gate 			continue;
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
1127c478bd9Sstevel@tonic-gate 		proc_unctrl_psinfo(&psinfo);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		if (Pstate(Pr) == PS_DEAD) {
1157c478bd9Sstevel@tonic-gate 			if ((Pcontent(Pr) & content) != content) {
1167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: core '%s' has "
1177c478bd9Sstevel@tonic-gate 				    "insufficient content\n", command, arg);
1187c478bd9Sstevel@tonic-gate 				rc++;
1197c478bd9Sstevel@tonic-gate 				continue;
1207c478bd9Sstevel@tonic-gate 			}
1217c478bd9Sstevel@tonic-gate 			(void) printf("core '%s' of %d:\t%.70s\n",
1227c478bd9Sstevel@tonic-gate 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
1237c478bd9Sstevel@tonic-gate 		} else {
1247c478bd9Sstevel@tonic-gate 			(void) printf("%d:\t%.70s\n",
1257c478bd9Sstevel@tonic-gate 			    (int)psinfo.pr_pid, psinfo.pr_psargs);
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
1297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: warning: librtld_db failed "
1307c478bd9Sstevel@tonic-gate 			    "to initialize; shared library information will "
1317c478bd9Sstevel@tonic-gate 			    "not be available\n", command);
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		rc += Pobject_iter(Pr, show_map, Pr);
1357c478bd9Sstevel@tonic-gate 		Prelease(Pr, 0);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	(void) proc_finistdio();
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (rc);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static int
1437c478bd9Sstevel@tonic-gate show_map(void *cd, const prmap_t *pmap, const char *object_name)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	char pathname[PATH_MAX];
1467c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr = cd;
1477c478bd9Sstevel@tonic-gate 	const auxv_t *auxv;
1487c478bd9Sstevel@tonic-gate 	int len;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* omit the executable file */
1517c478bd9Sstevel@tonic-gate 	if (strcmp(pmap->pr_mapname, "a.out") == 0)
1527c478bd9Sstevel@tonic-gate 		return (0);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* also omit the dynamic linker */
1557c478bd9Sstevel@tonic-gate 	if (ps_pauxv(Pr, &auxv) == PS_OK) {
1567c478bd9Sstevel@tonic-gate 		while (auxv->a_type != AT_NULL) {
1577c478bd9Sstevel@tonic-gate 			if (auxv->a_type == AT_BASE) {
1587c478bd9Sstevel@tonic-gate 				if (pmap->pr_vaddr == auxv->a_un.a_val)
1597c478bd9Sstevel@tonic-gate 					return (0);
1607c478bd9Sstevel@tonic-gate 				break;
1617c478bd9Sstevel@tonic-gate 			}
1627c478bd9Sstevel@tonic-gate 			auxv++;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* freedom from symlinks; canonical form */
1677c478bd9Sstevel@tonic-gate 	if ((len = resolvepath(object_name, pathname, sizeof (pathname))) > 0)
1687c478bd9Sstevel@tonic-gate 		pathname[len] = '\0';
1697c478bd9Sstevel@tonic-gate 	else
1707c478bd9Sstevel@tonic-gate 		(void) strncpy(pathname, object_name, sizeof (pathname));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	(void) printf("%s\n", pathname);
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
175