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
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * 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*186f7fbfSEdward Pilatowicz * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <stdio.h>
27004388ebScasper #include <stdio_ext.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <libproc.h>
347c478bd9Sstevel@tonic-gate #include <proc_service.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate static int show_map(void *, const prmap_t *, const char *);
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static char *command;
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)417c478bd9Sstevel@tonic-gate main(int argc, char **argv)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate int rc = 0;
447c478bd9Sstevel@tonic-gate int opt;
457c478bd9Sstevel@tonic-gate int errflg = 0;
467c478bd9Sstevel@tonic-gate int Fflag = 0;
47*186f7fbfSEdward Pilatowicz int lflag = 0;
487c478bd9Sstevel@tonic-gate core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON;
497c478bd9Sstevel@tonic-gate struct rlimit rlim;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
527c478bd9Sstevel@tonic-gate command++;
537c478bd9Sstevel@tonic-gate else
547c478bd9Sstevel@tonic-gate command = argv[0];
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate /* options */
57*186f7fbfSEdward Pilatowicz while ((opt = getopt(argc, argv, "Fl")) != EOF) {
587c478bd9Sstevel@tonic-gate switch (opt) {
597c478bd9Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */
607c478bd9Sstevel@tonic-gate Fflag = PGRAB_FORCE;
617c478bd9Sstevel@tonic-gate break;
62*186f7fbfSEdward Pilatowicz case 'l': /* show unresolved link map names */
63*186f7fbfSEdward Pilatowicz lflag = 1;
64*186f7fbfSEdward Pilatowicz break;
657c478bd9Sstevel@tonic-gate default:
667c478bd9Sstevel@tonic-gate errflg = 1;
677c478bd9Sstevel@tonic-gate break;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate argc -= optind;
727c478bd9Sstevel@tonic-gate argv += optind;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate if (errflg || argc <= 0) {
757c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
76*186f7fbfSEdward Pilatowicz "usage:\t%s [-Fl] { pid | core } ...\n", command);
777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
787c478bd9Sstevel@tonic-gate " (report process dynamic libraries)\n");
797c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
80*186f7fbfSEdward Pilatowicz " -F: force grabbing of the target process\n"
81*186f7fbfSEdward Pilatowicz " -l: show unresolved dynamic linker map names\n");
827c478bd9Sstevel@tonic-gate return (2);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate * Make sure we'll have enough file descriptors to handle a target
877c478bd9Sstevel@tonic-gate * that has many many mappings.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
907c478bd9Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max;
917c478bd9Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rlim);
92004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate (void) proc_initstdio();
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate while (argc-- > 0) {
987c478bd9Sstevel@tonic-gate char *arg;
997c478bd9Sstevel@tonic-gate int gcode;
1007c478bd9Sstevel@tonic-gate psinfo_t psinfo;
1017c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate (void) proc_flushstdio();
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY,
1067c478bd9Sstevel@tonic-gate PGRAB_RETAIN | Fflag, &gcode)) == NULL) {
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1097c478bd9Sstevel@tonic-gate command, arg, Pgrab_error(gcode));
1107c478bd9Sstevel@tonic-gate rc++;
1117c478bd9Sstevel@tonic-gate continue;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
1157c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate if (Pstate(Pr) == PS_DEAD) {
1187c478bd9Sstevel@tonic-gate if ((Pcontent(Pr) & content) != content) {
1197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: core '%s' has "
1207c478bd9Sstevel@tonic-gate "insufficient content\n", command, arg);
1217c478bd9Sstevel@tonic-gate rc++;
1227c478bd9Sstevel@tonic-gate continue;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate (void) printf("core '%s' of %d:\t%.70s\n",
1257c478bd9Sstevel@tonic-gate arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
1267c478bd9Sstevel@tonic-gate } else {
1277c478bd9Sstevel@tonic-gate (void) printf("%d:\t%.70s\n",
1287c478bd9Sstevel@tonic-gate (int)psinfo.pr_pid, psinfo.pr_psargs);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
1327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: warning: librtld_db failed "
1337c478bd9Sstevel@tonic-gate "to initialize; shared library information will "
1347c478bd9Sstevel@tonic-gate "not be available\n", command);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
137*186f7fbfSEdward Pilatowicz if (lflag)
1387c478bd9Sstevel@tonic-gate rc += Pobject_iter(Pr, show_map, Pr);
139*186f7fbfSEdward Pilatowicz else
140*186f7fbfSEdward Pilatowicz rc += Pobject_iter_resolved(Pr, show_map, Pr);
1417c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate (void) proc_finistdio();
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate return (rc);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate static int
show_map(void * cd,const prmap_t * pmap,const char * object_name)1497c478bd9Sstevel@tonic-gate show_map(void *cd, const prmap_t *pmap, const char *object_name)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate char pathname[PATH_MAX];
1527c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr = cd;
1537c478bd9Sstevel@tonic-gate const auxv_t *auxv;
1547c478bd9Sstevel@tonic-gate int len;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /* omit the executable file */
1577c478bd9Sstevel@tonic-gate if (strcmp(pmap->pr_mapname, "a.out") == 0)
1587c478bd9Sstevel@tonic-gate return (0);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /* also omit the dynamic linker */
1617c478bd9Sstevel@tonic-gate if (ps_pauxv(Pr, &auxv) == PS_OK) {
1627c478bd9Sstevel@tonic-gate while (auxv->a_type != AT_NULL) {
1637c478bd9Sstevel@tonic-gate if (auxv->a_type == AT_BASE) {
1647c478bd9Sstevel@tonic-gate if (pmap->pr_vaddr == auxv->a_un.a_val)
1657c478bd9Sstevel@tonic-gate return (0);
1667c478bd9Sstevel@tonic-gate break;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate auxv++;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* freedom from symlinks; canonical form */
1737c478bd9Sstevel@tonic-gate if ((len = resolvepath(object_name, pathname, sizeof (pathname))) > 0)
1747c478bd9Sstevel@tonic-gate pathname[len] = '\0';
1757c478bd9Sstevel@tonic-gate else
1767c478bd9Sstevel@tonic-gate (void) strncpy(pathname, object_name, sizeof (pathname));
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate (void) printf("%s\n", pathname);
1797c478bd9Sstevel@tonic-gate return (0);
1807c478bd9Sstevel@tonic-gate }
181