10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51914Scasper * Common Development and Distribution License (the "License"). 61914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*7675SEdward.Pilatowicz@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <stdio.h> 271914Scasper #include <stdio_ext.h> 280Sstevel@tonic-gate #include <stdlib.h> 290Sstevel@tonic-gate #include <unistd.h> 300Sstevel@tonic-gate #include <ctype.h> 310Sstevel@tonic-gate #include <string.h> 320Sstevel@tonic-gate #include <limits.h> 330Sstevel@tonic-gate #include <libproc.h> 340Sstevel@tonic-gate #include <proc_service.h> 350Sstevel@tonic-gate 360Sstevel@tonic-gate static int show_map(void *, const prmap_t *, const char *); 370Sstevel@tonic-gate 380Sstevel@tonic-gate static char *command; 390Sstevel@tonic-gate 400Sstevel@tonic-gate int 410Sstevel@tonic-gate main(int argc, char **argv) 420Sstevel@tonic-gate { 430Sstevel@tonic-gate int rc = 0; 440Sstevel@tonic-gate int opt; 450Sstevel@tonic-gate int errflg = 0; 460Sstevel@tonic-gate int Fflag = 0; 47*7675SEdward.Pilatowicz@Sun.COM int lflag = 0; 480Sstevel@tonic-gate core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON; 490Sstevel@tonic-gate struct rlimit rlim; 500Sstevel@tonic-gate 510Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 520Sstevel@tonic-gate command++; 530Sstevel@tonic-gate else 540Sstevel@tonic-gate command = argv[0]; 550Sstevel@tonic-gate 560Sstevel@tonic-gate /* options */ 57*7675SEdward.Pilatowicz@Sun.COM while ((opt = getopt(argc, argv, "Fl")) != EOF) { 580Sstevel@tonic-gate switch (opt) { 590Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */ 600Sstevel@tonic-gate Fflag = PGRAB_FORCE; 610Sstevel@tonic-gate break; 62*7675SEdward.Pilatowicz@Sun.COM case 'l': /* show unresolved link map names */ 63*7675SEdward.Pilatowicz@Sun.COM lflag = 1; 64*7675SEdward.Pilatowicz@Sun.COM break; 650Sstevel@tonic-gate default: 660Sstevel@tonic-gate errflg = 1; 670Sstevel@tonic-gate break; 680Sstevel@tonic-gate } 690Sstevel@tonic-gate } 700Sstevel@tonic-gate 710Sstevel@tonic-gate argc -= optind; 720Sstevel@tonic-gate argv += optind; 730Sstevel@tonic-gate 740Sstevel@tonic-gate if (errflg || argc <= 0) { 750Sstevel@tonic-gate (void) fprintf(stderr, 76*7675SEdward.Pilatowicz@Sun.COM "usage:\t%s [-Fl] { pid | core } ...\n", command); 770Sstevel@tonic-gate (void) fprintf(stderr, 78*7675SEdward.Pilatowicz@Sun.COM " (report process dynamic libraries)\n"); 790Sstevel@tonic-gate (void) fprintf(stderr, 80*7675SEdward.Pilatowicz@Sun.COM " -F: force grabbing of the target process\n" 81*7675SEdward.Pilatowicz@Sun.COM " -l: show unresolved dynamic linker map names\n"); 820Sstevel@tonic-gate return (2); 830Sstevel@tonic-gate } 840Sstevel@tonic-gate 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * Make sure we'll have enough file descriptors to handle a target 870Sstevel@tonic-gate * that has many many mappings. 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) { 900Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max; 910Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rlim); 921914Scasper (void) enable_extended_FILE_stdio(-1, -1); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate (void) proc_initstdio(); 960Sstevel@tonic-gate 970Sstevel@tonic-gate while (argc-- > 0) { 980Sstevel@tonic-gate char *arg; 990Sstevel@tonic-gate int gcode; 1000Sstevel@tonic-gate psinfo_t psinfo; 1010Sstevel@tonic-gate struct ps_prochandle *Pr; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate (void) proc_flushstdio(); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_ANY, 1060Sstevel@tonic-gate PGRAB_RETAIN | Fflag, &gcode)) == NULL) { 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n", 1090Sstevel@tonic-gate command, arg, Pgrab_error(gcode)); 1100Sstevel@tonic-gate rc++; 1110Sstevel@tonic-gate continue; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t)); 1150Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate if (Pstate(Pr) == PS_DEAD) { 1180Sstevel@tonic-gate if ((Pcontent(Pr) & content) != content) { 1190Sstevel@tonic-gate (void) fprintf(stderr, "%s: core '%s' has " 1200Sstevel@tonic-gate "insufficient content\n", command, arg); 1210Sstevel@tonic-gate rc++; 1220Sstevel@tonic-gate continue; 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate (void) printf("core '%s' of %d:\t%.70s\n", 1250Sstevel@tonic-gate arg, (int)psinfo.pr_pid, psinfo.pr_psargs); 1260Sstevel@tonic-gate } else { 1270Sstevel@tonic-gate (void) printf("%d:\t%.70s\n", 1280Sstevel@tonic-gate (int)psinfo.pr_pid, psinfo.pr_psargs); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) { 1320Sstevel@tonic-gate (void) fprintf(stderr, "%s: warning: librtld_db failed " 1330Sstevel@tonic-gate "to initialize; shared library information will " 1340Sstevel@tonic-gate "not be available\n", command); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 137*7675SEdward.Pilatowicz@Sun.COM if (lflag) 138*7675SEdward.Pilatowicz@Sun.COM rc += Pobject_iter(Pr, show_map, Pr); 139*7675SEdward.Pilatowicz@Sun.COM else 140*7675SEdward.Pilatowicz@Sun.COM rc += Pobject_iter_resolved(Pr, show_map, Pr); 1410Sstevel@tonic-gate Prelease(Pr, 0); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate (void) proc_finistdio(); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate return (rc); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate static int 1490Sstevel@tonic-gate show_map(void *cd, const prmap_t *pmap, const char *object_name) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate char pathname[PATH_MAX]; 1520Sstevel@tonic-gate struct ps_prochandle *Pr = cd; 1530Sstevel@tonic-gate const auxv_t *auxv; 1540Sstevel@tonic-gate int len; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* omit the executable file */ 1570Sstevel@tonic-gate if (strcmp(pmap->pr_mapname, "a.out") == 0) 1580Sstevel@tonic-gate return (0); 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* also omit the dynamic linker */ 1610Sstevel@tonic-gate if (ps_pauxv(Pr, &auxv) == PS_OK) { 1620Sstevel@tonic-gate while (auxv->a_type != AT_NULL) { 1630Sstevel@tonic-gate if (auxv->a_type == AT_BASE) { 1640Sstevel@tonic-gate if (pmap->pr_vaddr == auxv->a_un.a_val) 1650Sstevel@tonic-gate return (0); 1660Sstevel@tonic-gate break; 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate auxv++; 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* freedom from symlinks; canonical form */ 1730Sstevel@tonic-gate if ((len = resolvepath(object_name, pathname, sizeof (pathname))) > 0) 1740Sstevel@tonic-gate pathname[len] = '\0'; 1750Sstevel@tonic-gate else 1760Sstevel@tonic-gate (void) strncpy(pathname, object_name, sizeof (pathname)); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate (void) printf("%s\n", pathname); 1790Sstevel@tonic-gate return (0); 1800Sstevel@tonic-gate } 181