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