xref: /titanic_44/usr/src/cmd/ptools/pstack/pstack.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 <sys/isa_defs.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
33*7c478bd9Sstevel@tonic-gate #include <ctype.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <signal.h>
36*7c478bd9Sstevel@tonic-gate #include <dirent.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
44*7c478bd9Sstevel@tonic-gate #include <link.h>
45*7c478bd9Sstevel@tonic-gate #include <limits.h>
46*7c478bd9Sstevel@tonic-gate #include <libelf.h>
47*7c478bd9Sstevel@tonic-gate #include <thread_db.h>
48*7c478bd9Sstevel@tonic-gate #include <libproc.h>
49*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static	char	*command;
52*7c478bd9Sstevel@tonic-gate static	int	Fflag;
53*7c478bd9Sstevel@tonic-gate static	int	is64;
54*7c478bd9Sstevel@tonic-gate static	GElf_Sym sigh;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * To keep the list of user-level threads for a multithreaded process.
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate struct threadinfo {
60*7c478bd9Sstevel@tonic-gate 	struct threadinfo *next;
61*7c478bd9Sstevel@tonic-gate 	id_t	threadid;
62*7c478bd9Sstevel@tonic-gate 	id_t	lwpid;
63*7c478bd9Sstevel@tonic-gate 	td_thr_state_e state;
64*7c478bd9Sstevel@tonic-gate 	uintptr_t startfunc;
65*7c478bd9Sstevel@tonic-gate 	uintptr_t exitval;
66*7c478bd9Sstevel@tonic-gate 	prgregset_t regs;
67*7c478bd9Sstevel@tonic-gate };
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static struct threadinfo *thr_head, *thr_tail;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #define	TRUE	1
72*7c478bd9Sstevel@tonic-gate #define	FALSE	0
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	MAX_ARGS	8
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * To support debugging java programs, we display java frames within a stack.
78*7c478bd9Sstevel@tonic-gate  * The logic to walk the java frames is contained in libjvm_db.so, which is
79*7c478bd9Sstevel@tonic-gate  * found in the same directory as libjvm.so, linked with the program.  If we are
80*7c478bd9Sstevel@tonic-gate  * debugging a 32-bit app with a 64-binary, then the debugging library is found
81*7c478bd9Sstevel@tonic-gate  * in the '64' subdirectory.  If we find libjvm_db.so, then we fill in these
82*7c478bd9Sstevel@tonic-gate  * stub routines.
83*7c478bd9Sstevel@tonic-gate  */
84*7c478bd9Sstevel@tonic-gate typedef struct jvm_agent jvm_agent_t;
85*7c478bd9Sstevel@tonic-gate typedef int java_stack_f(void *, prgregset_t, const char *, int, int, void *);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  * The j_agent_create function takes a version parameter.  This ensures that the
89*7c478bd9Sstevel@tonic-gate  * interface can evolve appropriately.
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate #define	JVM_DB_VERSION	1
92*7c478bd9Sstevel@tonic-gate static void *libjvm;
93*7c478bd9Sstevel@tonic-gate typedef jvm_agent_t *(*j_agent_create_f)(struct ps_prochandle *, int);
94*7c478bd9Sstevel@tonic-gate typedef void (*j_agent_destroy_f)(jvm_agent_t *);
95*7c478bd9Sstevel@tonic-gate typedef int (*j_frame_iter_f)(jvm_agent_t *, prgregset_t, java_stack_f *,
96*7c478bd9Sstevel@tonic-gate     void *);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate static j_agent_create_f j_agent_create;
99*7c478bd9Sstevel@tonic-gate static j_agent_destroy_f j_agent_destroy;
100*7c478bd9Sstevel@tonic-gate static j_frame_iter_f j_frame_iter;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static jvm_agent_t *load_libjvm(struct ps_prochandle *P);
103*7c478bd9Sstevel@tonic-gate static void reset_libjvm(jvm_agent_t *);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * Since we must maintain both a proc handle and a jvm handle, this structure
107*7c478bd9Sstevel@tonic-gate  * is the basic type that gets passed around.
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate typedef struct pstack_handle {
110*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *proc;
111*7c478bd9Sstevel@tonic-gate 	jvm_agent_t *jvm;
112*7c478bd9Sstevel@tonic-gate 	int ignore_frame;
113*7c478bd9Sstevel@tonic-gate 	const char *lwps;
114*7c478bd9Sstevel@tonic-gate 	int count;
115*7c478bd9Sstevel@tonic-gate } pstack_handle_t;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static	int	thr_stack(const td_thrhandle_t *, void *);
118*7c478bd9Sstevel@tonic-gate static	void	free_threadinfo(void);
119*7c478bd9Sstevel@tonic-gate static	struct threadinfo *find_thread(id_t);
120*7c478bd9Sstevel@tonic-gate static	int	all_call_stacks(pstack_handle_t *, int);
121*7c478bd9Sstevel@tonic-gate static	void	tlhead(id_t, id_t);
122*7c478bd9Sstevel@tonic-gate static	int	print_frame(void *, prgregset_t, uint_t, const long *);
123*7c478bd9Sstevel@tonic-gate static	void	print_zombie(struct ps_prochandle *, struct threadinfo *);
124*7c478bd9Sstevel@tonic-gate static	void	print_syscall(const lwpstatus_t *, prgregset_t);
125*7c478bd9Sstevel@tonic-gate static	void	call_stack(pstack_handle_t *, const lwpstatus_t *);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * The number of active and zombie threads.
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate static	int	nthreads;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate int
133*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	int retc = 0;
136*7c478bd9Sstevel@tonic-gate 	int opt;
137*7c478bd9Sstevel@tonic-gate 	int errflg = FALSE;
138*7c478bd9Sstevel@tonic-gate 	core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON |
139*7c478bd9Sstevel@tonic-gate 	    CC_CONTENT_STACK;
140*7c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
143*7c478bd9Sstevel@tonic-gate 		command++;
144*7c478bd9Sstevel@tonic-gate 	else
145*7c478bd9Sstevel@tonic-gate 		command = argv[0];
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/* options */
148*7c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "F")) != EOF) {
149*7c478bd9Sstevel@tonic-gate 		switch (opt) {
150*7c478bd9Sstevel@tonic-gate 		case 'F':
151*7c478bd9Sstevel@tonic-gate 			/*
152*7c478bd9Sstevel@tonic-gate 			 * If the user specifies the force option, we'll
153*7c478bd9Sstevel@tonic-gate 			 * consent to printing out other threads' stacks
154*7c478bd9Sstevel@tonic-gate 			 * even if the main stack is absent.
155*7c478bd9Sstevel@tonic-gate 			 */
156*7c478bd9Sstevel@tonic-gate 			content &= ~CC_CONTENT_STACK;
157*7c478bd9Sstevel@tonic-gate 			Fflag = PGRAB_FORCE;
158*7c478bd9Sstevel@tonic-gate 			break;
159*7c478bd9Sstevel@tonic-gate 		default:
160*7c478bd9Sstevel@tonic-gate 			errflg = TRUE;
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 		}
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	argc -= optind;
166*7c478bd9Sstevel@tonic-gate 	argv += optind;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if (errflg || argc <= 0) {
169*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
170*7c478bd9Sstevel@tonic-gate 		    "usage:\t%s [-F] { pid | core }[/lwps] ...\n", command);
171*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "  (show process call stack)\n");
172*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
173*7c478bd9Sstevel@tonic-gate 			"  -F: force grabbing of the target process\n");
174*7c478bd9Sstevel@tonic-gate 		exit(2);
175*7c478bd9Sstevel@tonic-gate 	}
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	/*
178*7c478bd9Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
179*7c478bd9Sstevel@tonic-gate 	 * that has many many mappings.
180*7c478bd9Sstevel@tonic-gate 	 */
181*7c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
182*7c478bd9Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
183*7c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	(void) proc_initstdio();
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	while (--argc >= 0) {
189*7c478bd9Sstevel@tonic-gate 		int gcode;
190*7c478bd9Sstevel@tonic-gate 		psinfo_t psinfo;
191*7c478bd9Sstevel@tonic-gate 		const psinfo_t *tpsinfo;
192*7c478bd9Sstevel@tonic-gate 		struct ps_prochandle *Pr = NULL;
193*7c478bd9Sstevel@tonic-gate 		td_thragent_t *Tap;
194*7c478bd9Sstevel@tonic-gate 		int threaded;
195*7c478bd9Sstevel@tonic-gate 		pstack_handle_t handle;
196*7c478bd9Sstevel@tonic-gate 		const char *lwps, *arg;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 		(void) proc_flushstdio();
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		arg = *argv++;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 		if ((Pr = proc_arg_xgrab(arg, NULL, PR_ARG_ANY,
203*7c478bd9Sstevel@tonic-gate 		    Fflag, &gcode, &lwps)) == NULL) {
204*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
205*7c478bd9Sstevel@tonic-gate 			    command, arg, Pgrab_error(gcode));
206*7c478bd9Sstevel@tonic-gate 			retc++;
207*7c478bd9Sstevel@tonic-gate 			continue;
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		if ((tpsinfo = Ppsinfo(Pr)) == NULL) {
211*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: "
212*7c478bd9Sstevel@tonic-gate 			    "lost control of process\n", command, arg);
213*7c478bd9Sstevel@tonic-gate 			Prelease(Pr, 0);
214*7c478bd9Sstevel@tonic-gate 			retc++;
215*7c478bd9Sstevel@tonic-gate 			continue;
216*7c478bd9Sstevel@tonic-gate 		}
217*7c478bd9Sstevel@tonic-gate 		(void) memcpy(&psinfo, tpsinfo, sizeof (psinfo_t));
218*7c478bd9Sstevel@tonic-gate 		proc_unctrl_psinfo(&psinfo);
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 		if (Pstate(Pr) == PS_DEAD) {
221*7c478bd9Sstevel@tonic-gate 			if ((Pcontent(Pr) & content) != content) {
222*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: core '%s' has "
223*7c478bd9Sstevel@tonic-gate 				    "insufficient content\n", command, arg);
224*7c478bd9Sstevel@tonic-gate 				retc++;
225*7c478bd9Sstevel@tonic-gate 				continue;
226*7c478bd9Sstevel@tonic-gate 			}
227*7c478bd9Sstevel@tonic-gate 			(void) printf("core '%s' of %d:\t%.70s\n",
228*7c478bd9Sstevel@tonic-gate 			    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
229*7c478bd9Sstevel@tonic-gate 		} else {
230*7c478bd9Sstevel@tonic-gate 			(void) printf("%d:\t%.70s\n",
231*7c478bd9Sstevel@tonic-gate 			    (int)psinfo.pr_pid, psinfo.pr_psargs);
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		is64 = (psinfo.pr_dmodel == PR_MODEL_LP64);
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
237*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: warning: librtld_db failed "
238*7c478bd9Sstevel@tonic-gate 			    "to initialize; symbols from shared libraries will "
239*7c478bd9Sstevel@tonic-gate 			    "not be available\n", command);
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		/*
243*7c478bd9Sstevel@tonic-gate 		 * First we need to get a thread agent handle.
244*7c478bd9Sstevel@tonic-gate 		 */
245*7c478bd9Sstevel@tonic-gate 		if (td_init() != TD_OK ||
246*7c478bd9Sstevel@tonic-gate 		    td_ta_new(Pr, &Tap) != TD_OK)	/* no libc */
247*7c478bd9Sstevel@tonic-gate 			threaded = FALSE;
248*7c478bd9Sstevel@tonic-gate 		else {
249*7c478bd9Sstevel@tonic-gate 			/*
250*7c478bd9Sstevel@tonic-gate 			 * Iterate over all threads, calling:
251*7c478bd9Sstevel@tonic-gate 			 *   thr_stack(td_thrhandle_t *Thp, NULL);
252*7c478bd9Sstevel@tonic-gate 			 * for each one to generate the list of threads.
253*7c478bd9Sstevel@tonic-gate 			 */
254*7c478bd9Sstevel@tonic-gate 			nthreads = 0;
255*7c478bd9Sstevel@tonic-gate 			(void) td_ta_thr_iter(Tap, thr_stack, NULL,
256*7c478bd9Sstevel@tonic-gate 			    TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
257*7c478bd9Sstevel@tonic-gate 			    TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 			(void) td_ta_delete(Tap);
260*7c478bd9Sstevel@tonic-gate 			threaded = TRUE;
261*7c478bd9Sstevel@tonic-gate 		}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 		handle.proc = Pr;
264*7c478bd9Sstevel@tonic-gate 		handle.jvm = load_libjvm(Pr);
265*7c478bd9Sstevel@tonic-gate 		handle.lwps = lwps;
266*7c478bd9Sstevel@tonic-gate 		handle.count = 0;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		if (all_call_stacks(&handle, threaded) != 0)
269*7c478bd9Sstevel@tonic-gate 			retc++;
270*7c478bd9Sstevel@tonic-gate 		if (threaded)
271*7c478bd9Sstevel@tonic-gate 			free_threadinfo();
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 		reset_libjvm(handle.jvm);
274*7c478bd9Sstevel@tonic-gate 		Prelease(Pr, 0);
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		if (handle.count == 0)
277*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: no matching LWPs found\n",
278*7c478bd9Sstevel@tonic-gate 			    command);
279*7c478bd9Sstevel@tonic-gate 	}
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	(void) proc_finistdio();
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	return (retc);
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate /*
287*7c478bd9Sstevel@tonic-gate  * Thread iteration call-back function.
288*7c478bd9Sstevel@tonic-gate  * Called once for each user-level thread.
289*7c478bd9Sstevel@tonic-gate  * Used to build the list of all threads.
290*7c478bd9Sstevel@tonic-gate  */
291*7c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
292*7c478bd9Sstevel@tonic-gate static int
293*7c478bd9Sstevel@tonic-gate thr_stack(const td_thrhandle_t *Thp, void *cd)
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate 	td_thrinfo_t thrinfo;
296*7c478bd9Sstevel@tonic-gate 	struct threadinfo *tip;
297*7c478bd9Sstevel@tonic-gate 	td_err_e error;
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	if (td_thr_get_info(Thp, &thrinfo) != TD_OK)
300*7c478bd9Sstevel@tonic-gate 		return (0);
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	tip = malloc(sizeof (struct threadinfo));
303*7c478bd9Sstevel@tonic-gate 	tip->next = NULL;
304*7c478bd9Sstevel@tonic-gate 	tip->threadid = thrinfo.ti_tid;
305*7c478bd9Sstevel@tonic-gate 	tip->lwpid = thrinfo.ti_lid;
306*7c478bd9Sstevel@tonic-gate 	tip->state = thrinfo.ti_state;
307*7c478bd9Sstevel@tonic-gate 	tip->startfunc = thrinfo.ti_startfunc;
308*7c478bd9Sstevel@tonic-gate 	tip->exitval = (uintptr_t)thrinfo.ti_exitval;
309*7c478bd9Sstevel@tonic-gate 	nthreads++;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	if (thrinfo.ti_state == TD_THR_ZOMBIE ||
312*7c478bd9Sstevel@tonic-gate 	    ((error = td_thr_getgregs(Thp, tip->regs)) != TD_OK &&
313*7c478bd9Sstevel@tonic-gate 	    error != TD_PARTIALREG))
314*7c478bd9Sstevel@tonic-gate 		(void) memset(tip->regs, 0, sizeof (prgregset_t));
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if (thr_tail)
317*7c478bd9Sstevel@tonic-gate 		thr_tail->next = tip;
318*7c478bd9Sstevel@tonic-gate 	else
319*7c478bd9Sstevel@tonic-gate 		thr_head = tip;
320*7c478bd9Sstevel@tonic-gate 	thr_tail = tip;
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	return (0);
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate static void
326*7c478bd9Sstevel@tonic-gate free_threadinfo()
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate 	struct threadinfo *tip = thr_head;
329*7c478bd9Sstevel@tonic-gate 	struct threadinfo *next;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	while (tip) {
332*7c478bd9Sstevel@tonic-gate 		next = tip->next;
333*7c478bd9Sstevel@tonic-gate 		free(tip);
334*7c478bd9Sstevel@tonic-gate 		tip = next;
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	thr_head = thr_tail = NULL;
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate /*
341*7c478bd9Sstevel@tonic-gate  * Find and eliminate the thread corresponding to the given lwpid.
342*7c478bd9Sstevel@tonic-gate  */
343*7c478bd9Sstevel@tonic-gate static struct threadinfo *
344*7c478bd9Sstevel@tonic-gate find_thread(id_t lwpid)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	struct threadinfo *tip;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	for (tip = thr_head; tip; tip = tip->next) {
349*7c478bd9Sstevel@tonic-gate 		if (lwpid == tip->lwpid) {
350*7c478bd9Sstevel@tonic-gate 			tip->lwpid = 0;
351*7c478bd9Sstevel@tonic-gate 			return (tip);
352*7c478bd9Sstevel@tonic-gate 		}
353*7c478bd9Sstevel@tonic-gate 	}
354*7c478bd9Sstevel@tonic-gate 	return (NULL);
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate static int
358*7c478bd9Sstevel@tonic-gate thread_call_stack(void *data, const lwpstatus_t *psp,
359*7c478bd9Sstevel@tonic-gate     const lwpsinfo_t *pip)
360*7c478bd9Sstevel@tonic-gate {
361*7c478bd9Sstevel@tonic-gate 	pstack_handle_t *h = data;
362*7c478bd9Sstevel@tonic-gate 	lwpstatus_t lwpstatus;
363*7c478bd9Sstevel@tonic-gate 	struct threadinfo *tip;
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	if (!proc_lwp_in_set(h->lwps, pip->pr_lwpid))
366*7c478bd9Sstevel@tonic-gate 		return (0);
367*7c478bd9Sstevel@tonic-gate 	h->count++;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	if ((tip = find_thread(pip->pr_lwpid)) == NULL)
370*7c478bd9Sstevel@tonic-gate 		return (0);
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	tlhead(tip->threadid, pip->pr_lwpid);
373*7c478bd9Sstevel@tonic-gate 	tip->threadid = 0;	/* finish eliminating tid */
374*7c478bd9Sstevel@tonic-gate 	if (psp)
375*7c478bd9Sstevel@tonic-gate 		call_stack(h, psp);
376*7c478bd9Sstevel@tonic-gate 	else {
377*7c478bd9Sstevel@tonic-gate 		if (tip->state == TD_THR_ZOMBIE)
378*7c478bd9Sstevel@tonic-gate 			print_zombie(h->proc, tip);
379*7c478bd9Sstevel@tonic-gate 		else {
380*7c478bd9Sstevel@tonic-gate 			(void) memset(&lwpstatus, 0, sizeof (lwpstatus));
381*7c478bd9Sstevel@tonic-gate 			(void) memcpy(lwpstatus.pr_reg, tip->regs,
382*7c478bd9Sstevel@tonic-gate 				sizeof (prgregset_t));
383*7c478bd9Sstevel@tonic-gate 			call_stack(h, &lwpstatus);
384*7c478bd9Sstevel@tonic-gate 		}
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 	return (0);
387*7c478bd9Sstevel@tonic-gate }
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate static int
390*7c478bd9Sstevel@tonic-gate lwp_call_stack(void *data,
391*7c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp, const lwpsinfo_t *pip)
392*7c478bd9Sstevel@tonic-gate {
393*7c478bd9Sstevel@tonic-gate 	pstack_handle_t *h = data;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	if (!proc_lwp_in_set(h->lwps, pip->pr_lwpid))
396*7c478bd9Sstevel@tonic-gate 		return (0);
397*7c478bd9Sstevel@tonic-gate 	h->count++;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	tlhead(0, pip->pr_lwpid);
400*7c478bd9Sstevel@tonic-gate 	if (psp)
401*7c478bd9Sstevel@tonic-gate 		call_stack(h, psp);
402*7c478bd9Sstevel@tonic-gate 	else
403*7c478bd9Sstevel@tonic-gate 		(void) printf("\t** zombie "
404*7c478bd9Sstevel@tonic-gate 			"(exited, not detached, not yet joined) **\n");
405*7c478bd9Sstevel@tonic-gate 	return (0);
406*7c478bd9Sstevel@tonic-gate }
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate static int
409*7c478bd9Sstevel@tonic-gate all_call_stacks(pstack_handle_t *h, int dothreads)
410*7c478bd9Sstevel@tonic-gate {
411*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr = h->proc;
412*7c478bd9Sstevel@tonic-gate 	pstatus_t status = *Pstatus(Pr);
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	(void) memset(&sigh, 0, sizeof (GElf_Sym));
415*7c478bd9Sstevel@tonic-gate 	(void) Plookup_by_name(Pr, "libc.so", "sigacthandler", &sigh);
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	if ((status.pr_nlwp + status.pr_nzomb) <= 1 &&
418*7c478bd9Sstevel@tonic-gate 	    !(dothreads && nthreads > 1)) {
419*7c478bd9Sstevel@tonic-gate 		if (proc_lwp_in_set(h->lwps, status.pr_lwp.pr_lwpid)) {
420*7c478bd9Sstevel@tonic-gate 			call_stack(h, &status.pr_lwp);
421*7c478bd9Sstevel@tonic-gate 			h->count++;
422*7c478bd9Sstevel@tonic-gate 		}
423*7c478bd9Sstevel@tonic-gate 	} else {
424*7c478bd9Sstevel@tonic-gate 		lwpstatus_t lwpstatus;
425*7c478bd9Sstevel@tonic-gate 		struct threadinfo *tip;
426*7c478bd9Sstevel@tonic-gate 		id_t tid;
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 		if (dothreads)
429*7c478bd9Sstevel@tonic-gate 			(void) Plwp_iter_all(Pr, thread_call_stack, h);
430*7c478bd9Sstevel@tonic-gate 		else
431*7c478bd9Sstevel@tonic-gate 			(void) Plwp_iter_all(Pr, lwp_call_stack, h);
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 		/* for each remaining thread w/o an lwp */
434*7c478bd9Sstevel@tonic-gate 		(void) memset(&lwpstatus, 0, sizeof (lwpstatus));
435*7c478bd9Sstevel@tonic-gate 		for (tip = thr_head; tip; tip = tip->next) {
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 			if (!proc_lwp_in_set(h->lwps, tip->lwpid))
438*7c478bd9Sstevel@tonic-gate 				tip->threadid = 0;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 			if ((tid = tip->threadid) != 0) {
441*7c478bd9Sstevel@tonic-gate 				(void) memcpy(lwpstatus.pr_reg, tip->regs,
442*7c478bd9Sstevel@tonic-gate 					sizeof (prgregset_t));
443*7c478bd9Sstevel@tonic-gate 				tlhead(tid, tip->lwpid);
444*7c478bd9Sstevel@tonic-gate 				if (tip->state == TD_THR_ZOMBIE)
445*7c478bd9Sstevel@tonic-gate 					print_zombie(Pr, tip);
446*7c478bd9Sstevel@tonic-gate 				else
447*7c478bd9Sstevel@tonic-gate 					call_stack(h, &lwpstatus);
448*7c478bd9Sstevel@tonic-gate 			}
449*7c478bd9Sstevel@tonic-gate 			tip->threadid = 0;
450*7c478bd9Sstevel@tonic-gate 			tip->lwpid = 0;
451*7c478bd9Sstevel@tonic-gate 		}
452*7c478bd9Sstevel@tonic-gate 	}
453*7c478bd9Sstevel@tonic-gate 	return (0);
454*7c478bd9Sstevel@tonic-gate }
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate static void
457*7c478bd9Sstevel@tonic-gate tlhead(id_t threadid, id_t lwpid)
458*7c478bd9Sstevel@tonic-gate {
459*7c478bd9Sstevel@tonic-gate 	if (threadid == 0 && lwpid == 0)
460*7c478bd9Sstevel@tonic-gate 		return;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	(void) printf("-----------------");
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	if (threadid && lwpid)
465*7c478bd9Sstevel@tonic-gate 		(void) printf("  lwp# %d / thread# %d  ",
466*7c478bd9Sstevel@tonic-gate 			(int)lwpid, (int)threadid);
467*7c478bd9Sstevel@tonic-gate 	else if (threadid)
468*7c478bd9Sstevel@tonic-gate 		(void) printf("---------  thread# %d  ", (int)threadid);
469*7c478bd9Sstevel@tonic-gate 	else if (lwpid)
470*7c478bd9Sstevel@tonic-gate 		(void) printf("  lwp# %d  ------------", (int)lwpid);
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	(void) printf("--------------------\n");
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
476*7c478bd9Sstevel@tonic-gate static int
477*7c478bd9Sstevel@tonic-gate print_java_frame(void *cld, prgregset_t gregs, const char *name, int bci,
478*7c478bd9Sstevel@tonic-gate     int line, void *handle)
479*7c478bd9Sstevel@tonic-gate {
480*7c478bd9Sstevel@tonic-gate 	int length = (is64 ? 16 : 8);
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	(void) printf(" %.*lx * %s", length, (long)gregs[R_PC], name);
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	if (bci != -1) {
485*7c478bd9Sstevel@tonic-gate 		(void) printf("+%d", bci);
486*7c478bd9Sstevel@tonic-gate 		if (line)
487*7c478bd9Sstevel@tonic-gate 			(void) printf(" (line %d)", line);
488*7c478bd9Sstevel@tonic-gate 	}
489*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	return (0);
492*7c478bd9Sstevel@tonic-gate }
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate static sigjmp_buf jumpbuf;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
497*7c478bd9Sstevel@tonic-gate static void
498*7c478bd9Sstevel@tonic-gate fatal_signal(int signo)
499*7c478bd9Sstevel@tonic-gate {
500*7c478bd9Sstevel@tonic-gate 	siglongjmp(jumpbuf, 1);
501*7c478bd9Sstevel@tonic-gate }
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate static int
504*7c478bd9Sstevel@tonic-gate print_frame(void *cd, prgregset_t gregs, uint_t argc, const long *argv)
505*7c478bd9Sstevel@tonic-gate {
506*7c478bd9Sstevel@tonic-gate 	pstack_handle_t *h = cd;
507*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr = h->proc;
508*7c478bd9Sstevel@tonic-gate 	uintptr_t pc = gregs[R_PC];
509*7c478bd9Sstevel@tonic-gate 	char buff[255];
510*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
511*7c478bd9Sstevel@tonic-gate 	uintptr_t start;
512*7c478bd9Sstevel@tonic-gate 	int length = (is64? 16 : 8);
513*7c478bd9Sstevel@tonic-gate 	int i;
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	/*
516*7c478bd9Sstevel@tonic-gate 	 * If we are in a system call, we display the entry frame in a more
517*7c478bd9Sstevel@tonic-gate 	 * readable manner, using the name of the system call.  In this case, we
518*7c478bd9Sstevel@tonic-gate 	 * want to ignore this first frame, since we already displayed it
519*7c478bd9Sstevel@tonic-gate 	 * separately.
520*7c478bd9Sstevel@tonic-gate 	 */
521*7c478bd9Sstevel@tonic-gate 	if (h->ignore_frame) {
522*7c478bd9Sstevel@tonic-gate 		h->ignore_frame = 0;
523*7c478bd9Sstevel@tonic-gate 		return (0);
524*7c478bd9Sstevel@tonic-gate 	}
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	(void) sprintf(buff, "%.*lx", length, (long)pc);
527*7c478bd9Sstevel@tonic-gate 	(void) strcpy(buff + length, " ????????");
528*7c478bd9Sstevel@tonic-gate 	if (Plookup_by_addr(Pr, pc,
529*7c478bd9Sstevel@tonic-gate 	    buff + 1 + length, sizeof (buff) - 1 - length, &sym) == 0) {
530*7c478bd9Sstevel@tonic-gate 		start = sym.st_value;
531*7c478bd9Sstevel@tonic-gate 	} else if (h->jvm != NULL) {
532*7c478bd9Sstevel@tonic-gate 		int ret;
533*7c478bd9Sstevel@tonic-gate 		void (*segv)(int), (*bus)(int), (*ill)(int);
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 		segv = signal(SIGSEGV, fatal_signal);
536*7c478bd9Sstevel@tonic-gate 		bus = signal(SIGBUS, fatal_signal);
537*7c478bd9Sstevel@tonic-gate 		ill = signal(SIGILL, fatal_signal);
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 		/* Insure against a bad libjvm_db */
540*7c478bd9Sstevel@tonic-gate 		if (sigsetjmp(jumpbuf, 0) == 0)
541*7c478bd9Sstevel@tonic-gate 			ret = j_frame_iter(h->jvm, gregs, print_java_frame,
542*7c478bd9Sstevel@tonic-gate 			    NULL);
543*7c478bd9Sstevel@tonic-gate 		else
544*7c478bd9Sstevel@tonic-gate 			ret = -1;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGSEGV, segv);
547*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGBUS, bus);
548*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGILL, ill);
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 		if (ret == 0)
551*7c478bd9Sstevel@tonic-gate 			return (ret);
552*7c478bd9Sstevel@tonic-gate 	} else {
553*7c478bd9Sstevel@tonic-gate 		start = pc;
554*7c478bd9Sstevel@tonic-gate 	}
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 	(void) printf(" %-17s (", buff);
557*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc && i < MAX_ARGS; i++)
558*7c478bd9Sstevel@tonic-gate 		(void) printf((i+1 == argc)? "%lx" : "%lx, ",
559*7c478bd9Sstevel@tonic-gate 			argv[i]);
560*7c478bd9Sstevel@tonic-gate 	if (i != argc)
561*7c478bd9Sstevel@tonic-gate 		(void) printf("...");
562*7c478bd9Sstevel@tonic-gate 	(void) printf((start != pc)?
563*7c478bd9Sstevel@tonic-gate 		") + %lx\n" : ")\n", (long)(pc - start));
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	/*
566*7c478bd9Sstevel@tonic-gate 	 * If the frame's pc is in the "sigh" (a.k.a. signal handler, signal
567*7c478bd9Sstevel@tonic-gate 	 * hack, or *sigh* ...) range, then we're about to cross a signal
568*7c478bd9Sstevel@tonic-gate 	 * frame.  The signal number is the first argument to this function.
569*7c478bd9Sstevel@tonic-gate 	 */
570*7c478bd9Sstevel@tonic-gate 	if (pc - sigh.st_value < sigh.st_size) {
571*7c478bd9Sstevel@tonic-gate 		if (sig2str((int)argv[0], buff) == -1)
572*7c478bd9Sstevel@tonic-gate 			(void) strcpy(buff, " Unknown");
573*7c478bd9Sstevel@tonic-gate 		(void) printf(" --- called from signal handler with "
574*7c478bd9Sstevel@tonic-gate 		    "signal %d (SIG%s) ---\n", (int)argv[0], buff);
575*7c478bd9Sstevel@tonic-gate 	}
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	return (0);
578*7c478bd9Sstevel@tonic-gate }
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate static void
581*7c478bd9Sstevel@tonic-gate print_zombie(struct ps_prochandle *Pr, struct threadinfo *tip)
582*7c478bd9Sstevel@tonic-gate {
583*7c478bd9Sstevel@tonic-gate 	char buff[255];
584*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
585*7c478bd9Sstevel@tonic-gate 	uintptr_t start;
586*7c478bd9Sstevel@tonic-gate 	int length = (is64? 16 : 8);
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 	(void) sprintf(buff, "%.*lx", length, (long)tip->startfunc);
589*7c478bd9Sstevel@tonic-gate 	(void) strcpy(buff + length, " ????????");
590*7c478bd9Sstevel@tonic-gate 	if (Plookup_by_addr(Pr, tip->startfunc,
591*7c478bd9Sstevel@tonic-gate 	    buff + 1 + length, sizeof (buff) - 1 - length, &sym) == 0)
592*7c478bd9Sstevel@tonic-gate 		start = sym.st_value;
593*7c478bd9Sstevel@tonic-gate 	else
594*7c478bd9Sstevel@tonic-gate 		start = tip->startfunc;
595*7c478bd9Sstevel@tonic-gate 	(void) printf(" %s()", buff);
596*7c478bd9Sstevel@tonic-gate 	if (start != tip->startfunc)	/* doesn't happen? */
597*7c478bd9Sstevel@tonic-gate 		(void) printf("+%lx", (long)(tip->startfunc - start));
598*7c478bd9Sstevel@tonic-gate 	(void) printf(", exit value = 0x%.*lx\n", length, (long)tip->exitval);
599*7c478bd9Sstevel@tonic-gate 	(void) printf("\t** zombie "
600*7c478bd9Sstevel@tonic-gate 		"(exited, not detached, not yet joined) **\n");
601*7c478bd9Sstevel@tonic-gate }
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate static void
604*7c478bd9Sstevel@tonic-gate print_syscall(const lwpstatus_t *psp, prgregset_t reg)
605*7c478bd9Sstevel@tonic-gate {
606*7c478bd9Sstevel@tonic-gate 	char sname[32];
607*7c478bd9Sstevel@tonic-gate 	int length = (is64? 16 : 8);
608*7c478bd9Sstevel@tonic-gate 	uint_t i;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	(void) proc_sysname(psp->pr_syscall, sname, sizeof (sname));
611*7c478bd9Sstevel@tonic-gate 	(void) printf(" %.*lx %-8s (", length, (long)reg[R_PC], sname);
612*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < psp->pr_nsysarg; i++)
613*7c478bd9Sstevel@tonic-gate 		(void) printf((i+1 == psp->pr_nsysarg)? "%lx" : "%lx, ",
614*7c478bd9Sstevel@tonic-gate 			(long)psp->pr_sysarg[i]);
615*7c478bd9Sstevel@tonic-gate 	(void) printf(")\n");
616*7c478bd9Sstevel@tonic-gate }
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate static void
619*7c478bd9Sstevel@tonic-gate call_stack(pstack_handle_t *h, const lwpstatus_t *psp)
620*7c478bd9Sstevel@tonic-gate {
621*7c478bd9Sstevel@tonic-gate 	prgregset_t reg;
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 	(void) memcpy(reg, psp->pr_reg, sizeof (reg));
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	if ((psp->pr_flags & (PR_ASLEEP|PR_VFORKP)) ||
626*7c478bd9Sstevel@tonic-gate 	    ((psp->pr_flags & PR_ISTOP) &&
627*7c478bd9Sstevel@tonic-gate 	    (psp->pr_why == PR_SYSENTRY ||
628*7c478bd9Sstevel@tonic-gate 	    psp->pr_why == PR_SYSEXIT))) {
629*7c478bd9Sstevel@tonic-gate 		print_syscall(psp, reg);
630*7c478bd9Sstevel@tonic-gate 		h->ignore_frame = 1;
631*7c478bd9Sstevel@tonic-gate 	} else {
632*7c478bd9Sstevel@tonic-gate 		h->ignore_frame = 0;
633*7c478bd9Sstevel@tonic-gate 	}
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	(void) Pstack_iter(h->proc, reg, print_frame, h);
636*7c478bd9Sstevel@tonic-gate }
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
639*7c478bd9Sstevel@tonic-gate static int
640*7c478bd9Sstevel@tonic-gate jvm_object_iter(void *cd, const prmap_t *pmp, const char *obj)
641*7c478bd9Sstevel@tonic-gate {
642*7c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
643*7c478bd9Sstevel@tonic-gate 	char *name;
644*7c478bd9Sstevel@tonic-gate 	char *s1, *s2;
645*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr = cd;
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	if ((name = strstr(obj, "/libjvm.so")) == NULL)
648*7c478bd9Sstevel@tonic-gate 		name = strstr(obj, "/libjvm_g.so");
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	if (name) {
651*7c478bd9Sstevel@tonic-gate 		(void) strcpy(path, obj);
652*7c478bd9Sstevel@tonic-gate 		if (Pstatus(Pr)->pr_dmodel != PR_MODEL_NATIVE) {
653*7c478bd9Sstevel@tonic-gate 			s1 = name;
654*7c478bd9Sstevel@tonic-gate 			s2 = path + (s1 - obj);
655*7c478bd9Sstevel@tonic-gate 			(void) strcpy(s2, "/64");
656*7c478bd9Sstevel@tonic-gate 			s2 += 3;
657*7c478bd9Sstevel@tonic-gate 			(void) strcpy(s2, s1);
658*7c478bd9Sstevel@tonic-gate 		}
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 		s1 = strstr(obj, ".so");
661*7c478bd9Sstevel@tonic-gate 		s2 = strstr(path, ".so");
662*7c478bd9Sstevel@tonic-gate 		(void) strcpy(s2, "_db");
663*7c478bd9Sstevel@tonic-gate 		s2 += 3;
664*7c478bd9Sstevel@tonic-gate 		(void) strcpy(s2, s1);
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 		if ((libjvm = dlopen(path, RTLD_LAZY|RTLD_GLOBAL)) != NULL)
667*7c478bd9Sstevel@tonic-gate 			return (1);
668*7c478bd9Sstevel@tonic-gate 	}
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	return (0);
671*7c478bd9Sstevel@tonic-gate }
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate static jvm_agent_t *
674*7c478bd9Sstevel@tonic-gate load_libjvm(struct ps_prochandle *Pr)
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	jvm_agent_t *ret;
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	(void) Pobject_iter(Pr, jvm_object_iter, Pr);
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 	if (libjvm) {
681*7c478bd9Sstevel@tonic-gate 		j_agent_create = (j_agent_create_f)
682*7c478bd9Sstevel@tonic-gate 		    dlsym(libjvm, "Jagent_create");
683*7c478bd9Sstevel@tonic-gate 		j_agent_destroy = (j_agent_destroy_f)
684*7c478bd9Sstevel@tonic-gate 		    dlsym(libjvm, "Jagent_destroy");
685*7c478bd9Sstevel@tonic-gate 		j_frame_iter = (j_frame_iter_f)
686*7c478bd9Sstevel@tonic-gate 		    dlsym(libjvm, "Jframe_iter");
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 		if (j_agent_create == NULL || j_agent_destroy == NULL ||
689*7c478bd9Sstevel@tonic-gate 		    j_frame_iter == NULL ||
690*7c478bd9Sstevel@tonic-gate 		    (ret = j_agent_create(Pr, JVM_DB_VERSION)) == NULL) {
691*7c478bd9Sstevel@tonic-gate 			reset_libjvm(NULL);
692*7c478bd9Sstevel@tonic-gate 			return (NULL);
693*7c478bd9Sstevel@tonic-gate 		}
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 		return (ret);
696*7c478bd9Sstevel@tonic-gate 	}
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	return (NULL);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate static void
702*7c478bd9Sstevel@tonic-gate reset_libjvm(jvm_agent_t *agent)
703*7c478bd9Sstevel@tonic-gate {
704*7c478bd9Sstevel@tonic-gate 	if (libjvm) {
705*7c478bd9Sstevel@tonic-gate 		if (agent)
706*7c478bd9Sstevel@tonic-gate 			j_agent_destroy(agent);
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 		(void) dlclose(libjvm);
709*7c478bd9Sstevel@tonic-gate 	}
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate 	j_agent_create = NULL;
712*7c478bd9Sstevel@tonic-gate 	j_agent_destroy = NULL;
713*7c478bd9Sstevel@tonic-gate 	j_frame_iter = NULL;
714*7c478bd9Sstevel@tonic-gate 	libjvm = NULL;
715*7c478bd9Sstevel@tonic-gate }
716