xref: /titanic_41/usr/src/cmd/ptools/pldd/pldd.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.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 	}
90 
91 	(void) proc_initstdio();
92 
93 	while (argc-- > 0) {
94 		char *arg;
95 		int gcode;
96 		psinfo_t psinfo;
97 		struct ps_prochandle *Pr;
98 
99 		(void) proc_flushstdio();
100 
101 		if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
102 		    PGRAB_RETAIN | Fflag, &gcode)) == NULL) {
103 
104 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
105 			    command, arg, Pgrab_error(gcode));
106 			rc++;
107 			continue;
108 		}
109 
110 		(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
111 		proc_unctrl_psinfo(&psinfo);
112 
113 		if (Pstate(Pr) == PS_DEAD) {
114 			if ((Pcontent(Pr) & content) != content) {
115 				(void) fprintf(stderr, "%s: core '%s' has "
116 				    "insufficient content\n", command, arg);
117 				rc++;
118 				continue;
119 			}
120 			(void) printf("core '%s' of %d:\t%.70s\n",
121 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
122 		} else {
123 			(void) printf("%d:\t%.70s\n",
124 			    (int)psinfo.pr_pid, psinfo.pr_psargs);
125 		}
126 
127 		if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
128 			(void) fprintf(stderr, "%s: warning: librtld_db failed "
129 			    "to initialize; shared library information will "
130 			    "not be available\n", command);
131 		}
132 
133 		rc += Pobject_iter(Pr, show_map, Pr);
134 		Prelease(Pr, 0);
135 	}
136 	(void) proc_finistdio();
137 
138 	return (rc);
139 }
140 
141 static int
142 show_map(void *cd, const prmap_t *pmap, const char *object_name)
143 {
144 	char pathname[PATH_MAX];
145 	struct ps_prochandle *Pr = cd;
146 	const auxv_t *auxv;
147 	int len;
148 
149 	/* omit the executable file */
150 	if (strcmp(pmap->pr_mapname, "a.out") == 0)
151 		return (0);
152 
153 	/* also omit the dynamic linker */
154 	if (ps_pauxv(Pr, &auxv) == PS_OK) {
155 		while (auxv->a_type != AT_NULL) {
156 			if (auxv->a_type == AT_BASE) {
157 				if (pmap->pr_vaddr == auxv->a_un.a_val)
158 					return (0);
159 				break;
160 			}
161 			auxv++;
162 		}
163 	}
164 
165 	/* freedom from symlinks; canonical form */
166 	if ((len = resolvepath(object_name, pathname, sizeof (pathname))) > 0)
167 		pathname[len] = '\0';
168 	else
169 		(void) strncpy(pathname, object_name, sizeof (pathname));
170 
171 	(void) printf("%s\n", pathname);
172 	return (0);
173 }
174