summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ptools/pldd/pldd.c
blob: c9158ef34c980b841db01e2a7aac227ace22e89c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <libproc.h>
#include <proc_service.h>

static	int	show_map(void *, const prmap_t *, const char *);

static	char	*command;

int
main(int argc, char **argv)
{
	int rc = 0;
	int opt;
	int errflg = 0;
	int Fflag = 0;
	int lflag = 0;
	core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON;
	struct rlimit rlim;

	if ((command = strrchr(argv[0], '/')) != NULL)
		command++;
	else
		command = argv[0];

	/* options */
	while ((opt = getopt(argc, argv, "Fl")) != EOF) {
		switch (opt) {
		case 'F':		/* force grabbing (no O_EXCL) */
			Fflag = PGRAB_FORCE;
			break;
		case 'l':		/* show unresolved link map names */
			lflag = 1;
			break;
		default:
			errflg = 1;
			break;
		}
	}

	argc -= optind;
	argv += optind;

	if (errflg || argc <= 0) {
		(void) fprintf(stderr,
		    "usage:\t%s [-Fl] { pid | core } ...\n", command);
		(void) fprintf(stderr,
		    "  (report process dynamic libraries)\n");
		(void) fprintf(stderr,
		    "  -F: force grabbing of the target process\n"
		    "  -l: show unresolved dynamic linker map names\n");
		return (2);
	}

	/*
	 * Make sure we'll have enough file descriptors to handle a target
	 * that has many many mappings.
	 */
	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
		rlim.rlim_cur = rlim.rlim_max;
		(void) setrlimit(RLIMIT_NOFILE, &rlim);
		(void) enable_extended_FILE_stdio(-1, -1);
	}

	(void) proc_initstdio();

	while (argc-- > 0) {
		char *arg;
		int gcode;
		psinfo_t psinfo;
		struct ps_prochandle *Pr;

		(void) proc_flushstdio();

		if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
		    PGRAB_RETAIN | Fflag, &gcode)) == NULL) {

			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
			    command, arg, Pgrab_error(gcode));
			rc++;
			continue;
		}

		(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
		proc_unctrl_psinfo(&psinfo);

		if (Pstate(Pr) == PS_DEAD) {
			if ((Pcontent(Pr) & content) != content) {
				(void) fprintf(stderr, "%s: core '%s' has "
				    "insufficient content\n", command, arg);
				rc++;
				continue;
			}
			(void) printf("core '%s' of %d:\t%.70s\n",
			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
		} else {
			(void) printf("%d:\t%.70s\n",
			    (int)psinfo.pr_pid, psinfo.pr_psargs);
		}

		if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
			(void) fprintf(stderr, "%s: warning: librtld_db failed "
			    "to initialize; shared library information will "
			    "not be available\n", command);
		}

		if (lflag)
			rc += Pobject_iter(Pr, show_map, Pr);
		else
			rc += Pobject_iter_resolved(Pr, show_map, Pr);
		Prelease(Pr, 0);
	}
	(void) proc_finistdio();

	return (rc);
}

static int
show_map(void *cd, const prmap_t *pmap, const char *object_name)
{
	char pathname[PATH_MAX];
	struct ps_prochandle *Pr = cd;
	const auxv_t *auxv;
	int len;

	/* omit the executable file */
	if (strcmp(pmap->pr_mapname, "a.out") == 0)
		return (0);

	/* also omit the dynamic linker */
	if (ps_pauxv(Pr, &auxv) == PS_OK) {
		while (auxv->a_type != AT_NULL) {
			if (auxv->a_type == AT_BASE) {
				if (pmap->pr_vaddr == auxv->a_un.a_val)
					return (0);
				break;
			}
			auxv++;
		}
	}

	/* freedom from symlinks; canonical form */
	if ((len = resolvepath(object_name, pathname, sizeof (pathname))) > 0)
		pathname[len] = '\0';
	else
		(void) strncpy(pathname, object_name, sizeof (pathname));

	(void) printf("%s\n", pathname);
	return (0);
}