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
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * 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  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
2320c1c355SRod Evans  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/reg.h>
317c478bd9Sstevel@tonic-gate #include <sys/frame.h>
327c478bd9Sstevel@tonic-gate #include <sys/stack.h>
3320c1c355SRod Evans #include <sys/machelf.h>
347c478bd9Sstevel@tonic-gate #include <procfs.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "rdb.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifndef	STACK_BIAS
397c478bd9Sstevel@tonic-gate #define	STACK_BIAS	0
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static int
get_frame(struct ps_prochandle * ph,psaddr_t fp,struct frame * frm)437c478bd9Sstevel@tonic-gate get_frame(struct ps_prochandle *ph, psaddr_t fp, struct frame *frm)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate #if	defined(_LP64)
467c478bd9Sstevel@tonic-gate 	/*
477c478bd9Sstevel@tonic-gate 	 * Use special structures to read a 32-bit process
487c478bd9Sstevel@tonic-gate 	 * from a 64-bit process.
497c478bd9Sstevel@tonic-gate 	 */
507c478bd9Sstevel@tonic-gate 	if (ph->pp_dmodel == PR_MODEL_ILP32) {
517c478bd9Sstevel@tonic-gate 		struct frame32	frm32;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 		if (ps_pread(ph, (psaddr_t)fp, (char *)&frm32,
547c478bd9Sstevel@tonic-gate 		    sizeof (struct frame32)) != PS_OK) {
5520c1c355SRod Evans 			(void) printf("stack trace: bad frame pointer: 0x%lx\n",
567c478bd9Sstevel@tonic-gate 				fp);
577c478bd9Sstevel@tonic-gate 			return (-1);
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 		frm->fr_savpc = (long)frm32.fr_savpc;
6102ca3e02Srie #if	defined(__sparcv9)
625aefb655Srie 		frm->fr_savfp = (struct frame *)(uintptr_t)frm32.fr_savfp;
6302ca3e02Srie #elif	defined(__amd64)
647c478bd9Sstevel@tonic-gate 		frm->fr_savfp = (long)frm32.fr_savfp;
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 		return (0);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate #endif	/* defined(_LP64) */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (ps_pread(ph, (psaddr_t)fp + STACK_BIAS, (char *)frm,
717c478bd9Sstevel@tonic-gate 	    sizeof (struct frame)) != PS_OK) {
7220c1c355SRod Evans 		(void) printf("stack trace: bad frame pointer: 0x%lx\n", fp);
737c478bd9Sstevel@tonic-gate 		return (-1);
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	return (0);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
795aefb655Srie  * Relatively architecture neutral routine to display the callstack.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate void
CallStack(struct ps_prochandle * ph)827c478bd9Sstevel@tonic-gate CallStack(struct ps_prochandle *ph)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	pstatus_t	pstatus;
857c478bd9Sstevel@tonic-gate 	greg_t		fp;
867c478bd9Sstevel@tonic-gate 	struct frame	frm;
877c478bd9Sstevel@tonic-gate 	char		*symstr;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (pread(ph->pp_statusfd, &pstatus, sizeof (pstatus), 0) == -1)
907c478bd9Sstevel@tonic-gate 		perr("cs: reading status");
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	symstr = print_address_ps(ph, (ulong_t)pstatus.pr_lwp.pr_reg[R_PC],
9320c1c355SRod Evans 	    FLG_PAP_SONAME);
9420c1c355SRod Evans 	(void) printf(" 0x%08x:%-17s\n", EC_WORD(pstatus.pr_lwp.pr_reg[R_PC]),
9520c1c355SRod Evans 	    symstr);
967c478bd9Sstevel@tonic-gate 
9702ca3e02Srie 	fp = pstatus.pr_lwp.pr_reg[R_FP];
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	while (fp) {
1007c478bd9Sstevel@tonic-gate 		if (get_frame(ph, (psaddr_t)fp, &frm) == -1)
1017c478bd9Sstevel@tonic-gate 			return;
1027c478bd9Sstevel@tonic-gate 		if (frm.fr_savpc) {
1037c478bd9Sstevel@tonic-gate 			symstr = print_address_ps(ph, (ulong_t)frm.fr_savpc,
10420c1c355SRod Evans 			    FLG_PAP_SONAME);
10520c1c355SRod Evans 			(void) printf(" 0x%08x:%-17s\n", EC_WORD(frm.fr_savpc),
10620c1c355SRod Evans 			    symstr);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		fp = (greg_t)frm.fr_savfp;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate }
111