xref: /titanic_44/usr/src/cmd/ptools/pldd/pldd.c (revision 587032cf0967234b39ccb50adca936a367841063)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <libproc.h>
36 #include <proc_service.h>
37 
38 static	int	show_map(void *, const prmap_t *, const char *);
39 
40 static	char	*command;
41 
42 int
43 main(int argc, char **argv)
44 {
45 	int rc = 0;
46 	int opt;
47 	int errflg = 0;
48 	int Fflag = 0;
49 	core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON;
50 	struct rlimit rlim;
51 
52 	if ((command = strrchr(argv[0], '/')) != NULL)
53 		command++;
54 	else
55 		command = argv[0];
56 
57 	/* options */
58 	while ((opt = getopt(argc, argv, "F")) != EOF) {
59 		switch (opt) {
60 		case 'F':		/* force grabbing (no O_EXCL) */
61 			Fflag = PGRAB_FORCE;
62 			break;
63 		default:
64 			errflg = 1;
65 			break;
66 		}
67 	}
68 
69 	argc -= optind;
70 	argv += optind;
71 
72 	if (errflg || argc <= 0) {
73 		(void) fprintf(stderr,
74 			"usage:\t%s [-F] { pid | core } ...\n", command);
75 		(void) fprintf(stderr,
76 			"  (report process dynamic libraries)\n");
77 		(void) fprintf(stderr,
78 			"  -F: force grabbing of the target process\n");
79 		return (2);
80 	}
81 
82 	/*
83 	 * Make sure we'll have enough file descriptors to handle a target
84 	 * that has many many mappings.
85 	 */
86 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
87 		rlim.rlim_cur = rlim.rlim_max;
88 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
89 		(void) enable_extended_FILE_stdio(-1, -1);
90 	}
91 
92 	(void) proc_initstdio();
93 
94 	while (argc-- > 0) {
95 		char *arg;
96 		int gcode;
97 		psinfo_t psinfo;
98 		struct ps_prochandle *Pr;
99 
100 		(void) proc_flushstdio();
101 
102 		if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
103 		    PGRAB_RETAIN | Fflag, &gcode)) == NULL) {
104 
105 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
106 			    command, arg, Pgrab_error(gcode));
107 			rc++;
108 			continue;
109 		}
110 
111 		(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
112 		proc_unctrl_psinfo(&psinfo);
113 
114 		if (Pstate(Pr) == PS_DEAD) {
115 			if ((Pcontent(Pr) & content) != content) {
116 				(void) fprintf(stderr, "%s: core '%s' has "
117 				    "insufficient content\n", command, arg);
118 				rc++;
119 				continue;
120 			}
121 			(void) printf("core '%s' of %d:\t%.70s\n",
122 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
123 		} else {
124 			(void) printf("%d:\t%.70s\n",
125 			    (int)psinfo.pr_pid, psinfo.pr_psargs);
126 		}
127 
128 		if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
129 			(void) fprintf(stderr, "%s: warning: librtld_db failed "
130 			    "to initialize; shared library information will "
131 			    "not be available\n", command);
132 		}
133 
134 		rc += Pobject_iter(Pr, show_map, Pr);
135 		Prelease(Pr, 0);
136 	}
137 	(void) proc_finistdio();
138 
139 	return (rc);
140 }
141 
142 static int
143 show_map(void *cd, const prmap_t *pmap, const char *object_name)
144 {
145 	char pathname[PATH_MAX];
146 	struct ps_prochandle *Pr = cd;
147 	const auxv_t *auxv;
148 	int len;
149 
150 	/* omit the executable file */
151 	if (strcmp(pmap->pr_mapname, "a.out") == 0)
152 		return (0);
153 
154 	/* also omit the dynamic linker */
155 	if (ps_pauxv(Pr, &auxv) == PS_OK) {
156 		while (auxv->a_type != AT_NULL) {
157 			if (auxv->a_type == AT_BASE) {
158 				if (pmap->pr_vaddr == auxv->a_un.a_val)
159 					return (0);
160 				break;
161 			}
162 			auxv++;
163 		}
164 	}
165 
166 	/* freedom from symlinks; canonical form */
167 	if ((len = resolvepath(object_name, pathname, sizeof (pathname))) > 0)
168 		pathname[len] = '\0';
169 	else
170 		(void) strncpy(pathname, object_name, sizeof (pathname));
171 
172 	(void) printf("%s\n", pathname);
173 	return (0);
174 }
175