xref: /onnv-gate/usr/src/cmd/sgs/librtld_db/demo/common/callstack.c (revision 12927:a27c46eb192b)
1*12927SRod.Evans@Sun.COM /*
2*12927SRod.Evans@Sun.COM  * CDDL HEADER START
3*12927SRod.Evans@Sun.COM  *
4*12927SRod.Evans@Sun.COM  * The contents of this file are subject to the terms of the
5*12927SRod.Evans@Sun.COM  * Common Development and Distribution License (the "License").
6*12927SRod.Evans@Sun.COM  * You may not use this file except in compliance with the License.
7*12927SRod.Evans@Sun.COM  *
8*12927SRod.Evans@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12927SRod.Evans@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12927SRod.Evans@Sun.COM  * See the License for the specific language governing permissions
11*12927SRod.Evans@Sun.COM  * and limitations under the License.
12*12927SRod.Evans@Sun.COM  *
13*12927SRod.Evans@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12927SRod.Evans@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12927SRod.Evans@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12927SRod.Evans@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12927SRod.Evans@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12927SRod.Evans@Sun.COM  *
19*12927SRod.Evans@Sun.COM  * CDDL HEADER END
20*12927SRod.Evans@Sun.COM  */
21*12927SRod.Evans@Sun.COM 
22*12927SRod.Evans@Sun.COM /*
23*12927SRod.Evans@Sun.COM  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24*12927SRod.Evans@Sun.COM  */
25*12927SRod.Evans@Sun.COM 
26*12927SRod.Evans@Sun.COM #include <stdio.h>
27*12927SRod.Evans@Sun.COM #include <unistd.h>
28*12927SRod.Evans@Sun.COM #include <string.h>
29*12927SRod.Evans@Sun.COM #include <sys/types.h>
30*12927SRod.Evans@Sun.COM #include <sys/reg.h>
31*12927SRod.Evans@Sun.COM #include <sys/frame.h>
32*12927SRod.Evans@Sun.COM #include <sys/stack.h>
33*12927SRod.Evans@Sun.COM #include <sys/machelf.h>
34*12927SRod.Evans@Sun.COM #include <procfs.h>
35*12927SRod.Evans@Sun.COM 
36*12927SRod.Evans@Sun.COM #include "rdb.h"
37*12927SRod.Evans@Sun.COM 
38*12927SRod.Evans@Sun.COM #ifndef	STACK_BIAS
39*12927SRod.Evans@Sun.COM #define	STACK_BIAS	0
40*12927SRod.Evans@Sun.COM #endif
41*12927SRod.Evans@Sun.COM 
42*12927SRod.Evans@Sun.COM static int
get_frame(struct ps_prochandle * ph,psaddr_t fp,struct frame * frm)43*12927SRod.Evans@Sun.COM get_frame(struct ps_prochandle *ph, psaddr_t fp, struct frame *frm)
44*12927SRod.Evans@Sun.COM {
45*12927SRod.Evans@Sun.COM #if	defined(_LP64)
46*12927SRod.Evans@Sun.COM 	/*
47*12927SRod.Evans@Sun.COM 	 * Use special structures to read a 32-bit process
48*12927SRod.Evans@Sun.COM 	 * from a 64-bit process.
49*12927SRod.Evans@Sun.COM 	 */
50*12927SRod.Evans@Sun.COM 	if (ph->pp_dmodel == PR_MODEL_ILP32) {
51*12927SRod.Evans@Sun.COM 		struct frame32	frm32;
52*12927SRod.Evans@Sun.COM 
53*12927SRod.Evans@Sun.COM 		if (ps_pread(ph, (psaddr_t)fp, (char *)&frm32,
54*12927SRod.Evans@Sun.COM 		    sizeof (struct frame32)) != PS_OK) {
55*12927SRod.Evans@Sun.COM 			(void) printf("stack trace: bad frame pointer: 0x%lx\n",
56*12927SRod.Evans@Sun.COM 				fp);
57*12927SRod.Evans@Sun.COM 			return (-1);
58*12927SRod.Evans@Sun.COM 		}
59*12927SRod.Evans@Sun.COM 
60*12927SRod.Evans@Sun.COM 		frm->fr_savpc = (long)frm32.fr_savpc;
61*12927SRod.Evans@Sun.COM #if	defined(__sparcv9)
62*12927SRod.Evans@Sun.COM 		frm->fr_savfp = (struct frame *)(uintptr_t)frm32.fr_savfp;
63*12927SRod.Evans@Sun.COM #elif	defined(__amd64)
64*12927SRod.Evans@Sun.COM 		frm->fr_savfp = (long)frm32.fr_savfp;
65*12927SRod.Evans@Sun.COM #endif
66*12927SRod.Evans@Sun.COM 		return (0);
67*12927SRod.Evans@Sun.COM 	}
68*12927SRod.Evans@Sun.COM #endif	/* defined(_LP64) */
69*12927SRod.Evans@Sun.COM 
70*12927SRod.Evans@Sun.COM 	if (ps_pread(ph, (psaddr_t)fp + STACK_BIAS, (char *)frm,
71*12927SRod.Evans@Sun.COM 	    sizeof (struct frame)) != PS_OK) {
72*12927SRod.Evans@Sun.COM 		(void) printf("stack trace: bad frame pointer: 0x%lx\n", fp);
73*12927SRod.Evans@Sun.COM 		return (-1);
74*12927SRod.Evans@Sun.COM 	}
75*12927SRod.Evans@Sun.COM 	return (0);
76*12927SRod.Evans@Sun.COM }
77*12927SRod.Evans@Sun.COM 
78*12927SRod.Evans@Sun.COM /*
79*12927SRod.Evans@Sun.COM  * Relatively architecture neutral routine to display the callstack.
80*12927SRod.Evans@Sun.COM  */
81*12927SRod.Evans@Sun.COM void
CallStack(struct ps_prochandle * ph)82*12927SRod.Evans@Sun.COM CallStack(struct ps_prochandle *ph)
83*12927SRod.Evans@Sun.COM {
84*12927SRod.Evans@Sun.COM 	pstatus_t	pstatus;
85*12927SRod.Evans@Sun.COM 	greg_t		fp;
86*12927SRod.Evans@Sun.COM 	struct frame	frm;
87*12927SRod.Evans@Sun.COM 	char		*symstr;
88*12927SRod.Evans@Sun.COM 
89*12927SRod.Evans@Sun.COM 	if (pread(ph->pp_statusfd, &pstatus, sizeof (pstatus), 0) == -1)
90*12927SRod.Evans@Sun.COM 		perr("cs: reading status");
91*12927SRod.Evans@Sun.COM 
92*12927SRod.Evans@Sun.COM 	symstr = print_address_ps(ph, (ulong_t)pstatus.pr_lwp.pr_reg[R_PC],
93*12927SRod.Evans@Sun.COM 	    FLG_PAP_SONAME);
94*12927SRod.Evans@Sun.COM 	(void) printf(" 0x%08x:%-17s\n", EC_WORD(pstatus.pr_lwp.pr_reg[R_PC]),
95*12927SRod.Evans@Sun.COM 	    symstr);
96*12927SRod.Evans@Sun.COM 
97*12927SRod.Evans@Sun.COM 	fp = pstatus.pr_lwp.pr_reg[R_FP];
98*12927SRod.Evans@Sun.COM 
99*12927SRod.Evans@Sun.COM 	while (fp) {
100*12927SRod.Evans@Sun.COM 		if (get_frame(ph, (psaddr_t)fp, &frm) == -1)
101*12927SRod.Evans@Sun.COM 			return;
102*12927SRod.Evans@Sun.COM 		if (frm.fr_savpc) {
103*12927SRod.Evans@Sun.COM 			symstr = print_address_ps(ph, (ulong_t)frm.fr_savpc,
104*12927SRod.Evans@Sun.COM 			    FLG_PAP_SONAME);
105*12927SRod.Evans@Sun.COM 			(void) printf(" 0x%08x:%-17s\n", EC_WORD(frm.fr_savpc),
106*12927SRod.Evans@Sun.COM 			    symstr);
107*12927SRod.Evans@Sun.COM 		}
108*12927SRod.Evans@Sun.COM 		fp = (greg_t)frm.fr_savfp;
109*12927SRod.Evans@Sun.COM 	}
110*12927SRod.Evans@Sun.COM }
111