xref: /onnv-gate/usr/src/cmd/mdb/intel/mdb/mdb_amd64util.c (revision 2244:0bd5e8ed2b79)
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
5*2244Sdmick  * Common Development and Distribution License (the "License").
6*2244Sdmick  * 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  */
21*2244Sdmick 
220Sstevel@tonic-gate /*
23*2244Sdmick  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/reg.h>
310Sstevel@tonic-gate #include <sys/privregs.h>
320Sstevel@tonic-gate #include <sys/stack.h>
330Sstevel@tonic-gate #include <sys/frame.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <mdb/mdb_target_impl.h>
360Sstevel@tonic-gate #include <mdb/mdb_kreg_impl.h>
370Sstevel@tonic-gate #include <mdb/mdb_debug.h>
380Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
390Sstevel@tonic-gate #include <mdb/mdb_amd64util.h>
40170Ssherrym #include <mdb/mdb_ctf.h>
410Sstevel@tonic-gate #include <mdb/mdb_err.h>
420Sstevel@tonic-gate #include <mdb/mdb.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * This array is used by the getareg and putareg entry points, and also by our
460Sstevel@tonic-gate  * register variable discipline.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate const mdb_tgt_regdesc_t mdb_amd64_kregs[] = {
500Sstevel@tonic-gate 	{ "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT },
510Sstevel@tonic-gate 	{ "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT },
520Sstevel@tonic-gate 	{ "rdi", KREG_RDI, MDB_TGT_R_EXPORT },
530Sstevel@tonic-gate 	{ "rsi", KREG_RSI, MDB_TGT_R_EXPORT },
540Sstevel@tonic-gate 	{ "rdx", KREG_RDX, MDB_TGT_R_EXPORT },
550Sstevel@tonic-gate 	{ "rcx", KREG_RCX, MDB_TGT_R_EXPORT },
560Sstevel@tonic-gate 	{ "r8", KREG_R8, MDB_TGT_R_EXPORT },
570Sstevel@tonic-gate 	{ "r9", KREG_R9, MDB_TGT_R_EXPORT },
580Sstevel@tonic-gate 	{ "rax", KREG_RAX, MDB_TGT_R_EXPORT },
590Sstevel@tonic-gate 	{ "rbx", KREG_RBX, MDB_TGT_R_EXPORT },
600Sstevel@tonic-gate 	{ "rbp", KREG_RBP, MDB_TGT_R_EXPORT },
610Sstevel@tonic-gate 	{ "r10", KREG_R10, MDB_TGT_R_EXPORT },
620Sstevel@tonic-gate 	{ "r11", KREG_R11, MDB_TGT_R_EXPORT },
630Sstevel@tonic-gate 	{ "r12", KREG_R12, MDB_TGT_R_EXPORT },
640Sstevel@tonic-gate 	{ "r13", KREG_R13, MDB_TGT_R_EXPORT },
650Sstevel@tonic-gate 	{ "r14", KREG_R14, MDB_TGT_R_EXPORT },
660Sstevel@tonic-gate 	{ "r15", KREG_R15, MDB_TGT_R_EXPORT },
670Sstevel@tonic-gate 	{ "fsbase", KREG_FSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
680Sstevel@tonic-gate 	{ "gsbase", KREG_GSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
690Sstevel@tonic-gate 	{ "kgsbase", KREG_KGSBASE, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
700Sstevel@tonic-gate 	{ "ds", KREG_DS, MDB_TGT_R_EXPORT },
710Sstevel@tonic-gate 	{ "es", KREG_ES, MDB_TGT_R_EXPORT },
720Sstevel@tonic-gate 	{ "fs", KREG_FS, MDB_TGT_R_EXPORT },
730Sstevel@tonic-gate 	{ "gs", KREG_GS, MDB_TGT_R_EXPORT },
740Sstevel@tonic-gate 	{ "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
750Sstevel@tonic-gate 	{ "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV },
760Sstevel@tonic-gate 	{ "rip", KREG_RIP, MDB_TGT_R_EXPORT },
770Sstevel@tonic-gate 	{ "cs", KREG_CS, MDB_TGT_R_EXPORT },
780Sstevel@tonic-gate 	{ "rflags", KREG_RFLAGS, MDB_TGT_R_EXPORT },
790Sstevel@tonic-gate 	{ "rsp", KREG_RSP, MDB_TGT_R_EXPORT },
800Sstevel@tonic-gate 	{ "ss", KREG_SS, MDB_TGT_R_EXPORT },
810Sstevel@tonic-gate 	{ NULL, 0, 0 }
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate void
850Sstevel@tonic-gate mdb_amd64_printregs(const mdb_tgt_gregset_t *gregs)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	const kreg_t *kregs = &gregs->kregs[0];
880Sstevel@tonic-gate 	kreg_t rflags = kregs[KREG_RFLAGS];
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #define	GETREG2(x) ((uintptr_t)kregs[(x)]), ((uintptr_t)kregs[(x)])
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	mdb_printf("%%rax = 0x%0?p %15A %%r9  = 0x%0?p %A\n",
930Sstevel@tonic-gate 	    GETREG2(KREG_RAX), GETREG2(KREG_R9));
940Sstevel@tonic-gate 	mdb_printf("%%rbx = 0x%0?p %15A %%r10 = 0x%0?p %A\n",
950Sstevel@tonic-gate 	    GETREG2(KREG_RBX), GETREG2(KREG_R10));
960Sstevel@tonic-gate 	mdb_printf("%%rcx = 0x%0?p %15A %%r11 = 0x%0?p %A\n",
970Sstevel@tonic-gate 	    GETREG2(KREG_RCX), GETREG2(KREG_R11));
980Sstevel@tonic-gate 	mdb_printf("%%rdx = 0x%0?p %15A %%r12 = 0x%0?p %A\n",
990Sstevel@tonic-gate 	    GETREG2(KREG_RDX), GETREG2(KREG_R12));
1000Sstevel@tonic-gate 	mdb_printf("%%rsi = 0x%0?p %15A %%r13 = 0x%0?p %A\n",
1010Sstevel@tonic-gate 	    GETREG2(KREG_RSI), GETREG2(KREG_R13));
1020Sstevel@tonic-gate 	mdb_printf("%%rdi = 0x%0?p %15A %%r14 = 0x%0?p %A\n",
1030Sstevel@tonic-gate 	    GETREG2(KREG_RDI), GETREG2(KREG_R14));
1040Sstevel@tonic-gate 	mdb_printf("%%r8  = 0x%0?p %15A %%r15 = 0x%0?p %A\n\n",
1050Sstevel@tonic-gate 	    GETREG2(KREG_R8), GETREG2(KREG_R15));
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	mdb_printf("%%rip = 0x%0?p %A\n", GETREG2(KREG_RIP));
1080Sstevel@tonic-gate 	mdb_printf("%%rbp = 0x%0?p\n", kregs[KREG_RBP]);
1090Sstevel@tonic-gate 	mdb_printf("%%rsp = 0x%0?p\n", kregs[KREG_RSP]);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	mdb_printf("%%rflags = 0x%08x\n", rflags);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	mdb_printf("  id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n",
1140Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT,
1150Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT,
1160Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT,
1170Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT,
1180Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT,
1190Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT,
1200Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT,
1210Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	mdb_printf("  status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n",
1240Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of",
1250Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df",
1260Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if",
1270Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf",
1280Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf",
1290Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf",
1300Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af",
1310Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf",
1320Sstevel@tonic-gate 	    (rflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf");
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	mdb_printf("%24s%%cs = 0x%04x\t%%ds = 0x%04x\t%%es = 0x%04x\n",
1350Sstevel@tonic-gate 	    " ", kregs[KREG_CS], kregs[KREG_DS], kregs[KREG_ES]);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	mdb_printf("%%trapno = 0x%x\t\t%%fs = 0x%04x\tfsbase = 0x%0?p\n",
1380Sstevel@tonic-gate 	    kregs[KREG_TRAPNO], (kregs[KREG_FS] & 0xffff), kregs[KREG_FSBASE]);
1390Sstevel@tonic-gate 	mdb_printf("   %%err = 0x%x\t\t%%gs = 0x%04x\tgsbase = 0x%0?p\n",
1400Sstevel@tonic-gate 	    kregs[KREG_ERR], (kregs[KREG_GS] & 0xffff), kregs[KREG_GSBASE]);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
143170Ssherrym 
144170Ssherrym 
145170Ssherrym /*
146170Ssherrym  * Sun Studio 10 patch compiler and gcc 3.4.3 Sun branch implemented a
147170Ssherrym  * "-save_args" option on amd64.  When the option is specified, INTEGER
148170Ssherrym  * type function arguments passed via registers will be saved on the stack
149170Ssherrym  * immediately after %rbp, and will not be modified through out the life
150170Ssherrym  * of the routine.
151170Ssherrym  *
152170Ssherrym  *				+--------+
153170Ssherrym  *		%rbp	-->     |  %rbp  |
154170Ssherrym  *				+--------+
155170Ssherrym  *		-0x8(%rbp)	|  %rdi  |
156170Ssherrym  *				+--------+
157170Ssherrym  *		-0x10(%rbp)	|  %rsi  |
158170Ssherrym  *				+--------+
159170Ssherrym  *		-0x18(%rbp)	|  %rdx  |
160170Ssherrym  *				+--------+
161170Ssherrym  *		-0x20(%rbp)	|  %rcx  |
162170Ssherrym  *				+--------+
163170Ssherrym  *		-0x28(%rbp)	|  %r8   |
164170Ssherrym  *				+--------+
165170Ssherrym  *		-0x30(%rbp)	|  %r9   |
166170Ssherrym  *				+--------+
167170Ssherrym  *
168170Ssherrym  *
169170Ssherrym  * For example, for the following function,
170170Ssherrym  *
171170Ssherrym  * void
172170Ssherrym  * foo(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
173170Ssherrym  * {
174170Ssherrym  * ...
175170Ssherrym  * }
176170Ssherrym  *
177170Ssherrym  * Disassembled code will look something like the following:
178170Ssherrym  *
179170Ssherrym  *     pushq	%rbp
180170Ssherrym  *     movq	%rsp, %rbp
181170Ssherrym  *     subq	$imm8, %rsp			**
182170Ssherrym  *     movq	%rdi, -0x8(%rbp)
183170Ssherrym  *     movq	%rsi, -0x10(%rbp)
184170Ssherrym  *     movq	%rdx, -0x18(%rbp)
185170Ssherrym  *     movq	%rcx, -0x20(%rbp)
186170Ssherrym  *     movq	%r8, -0x28(%rbp)
187170Ssherrym  *     movq	%r9, -0x30(%rbp)
188170Ssherrym  *     ...
189170Ssherrym  * or
190170Ssherrym  *     pushq	%rbp
191170Ssherrym  *     movq	%rsp, %rbp
192170Ssherrym  *     subq	$imm8, %rsp			**
193170Ssherrym  *     movq	%r9, -0x30(%rbp)
194170Ssherrym  *     movq	%r8, -0x28(%rbp)
195170Ssherrym  *     movq	%rcx, -0x20(%rbp)
196170Ssherrym  *     movq	%rdx, -0x18(%rbp)
197170Ssherrym  *     movq	%rsi, -0x10(%rbp)
198170Ssherrym  *     movq	%rdi, -0x8(%rbp)
199170Ssherrym  *     ...
200170Ssherrym  *
201170Ssherrym  * **: The space being reserved is in addition to what the current
202170Ssherrym  *     function prolog already reserves.
203170Ssherrym  *
204170Ssherrym  * If there are odd number of arguments to a function, additional space is
205170Ssherrym  * reserved on the stack to maintain 16-byte alignment.  For example,
206170Ssherrym  *
207170Ssherrym  *     argc == 0: no argument saving.
208170Ssherrym  *     argc == 3: save 3, but space for 4 is reserved
209170Ssherrym  *     argc == 7: save 6.
210170Ssherrym  */
211170Ssherrym 
212170Ssherrym /*
213170Ssherrym  * The longest instruction sequence in bytes before all 6 arguments are
214170Ssherrym  * saved on the stack.  This value depends on compiler implementation,
215170Ssherrym  * therefore it should be examined periodically to guarantee accuracy.
216170Ssherrym  */
217170Ssherrym #define	SEQ_LEN		80
218170Ssherrym 
219170Ssherrym /*
220170Ssherrym  * Size of the instruction sequence arrays.  It should correspond to
221170Ssherrym  * the maximum number of arguments passed via registers.
222170Ssherrym  */
223170Ssherrym #define	INSTR_ARRAY_SIZE	6
224170Ssherrym 
225170Ssherrym #define	INSTR4(ins, off)	\
226170Ssherrym 	(ins[(off)] + (ins[(off) + 1] << 8) + (ins[(off + 2)] << 16) + \
227170Ssherrym 	(ins[(off) + 3] << 24))
228170Ssherrym 
229170Ssherrym /*
230170Ssherrym  * Sun Studio 10 patch implementation saves %rdi first;
231170Ssherrym  * GCC 3.4.3 Sun branch implementation saves them in reverse order.
232170Ssherrym  */
233170Ssherrym static const uint32_t save_instr[INSTR_ARRAY_SIZE] = {
234170Ssherrym 	0xf87d8948,	/* movq %rdi, -0x8(%rbp) */
235170Ssherrym 	0xf0758948,	/* movq %rsi, -0x10(%rbp) */
236170Ssherrym 	0xe8558948,	/* movq %rdx, -0x18(%rbp) */
237170Ssherrym 	0xe04d8948,	/* movq %rcx, -0x20(%rbp) */
238170Ssherrym 	0xd845894c,	/* movq %r8, -0x28(%rbp) */
239170Ssherrym 	0xd04d894c	/* movq %r9, -0x30(%rbp) */
240170Ssherrym };
241170Ssherrym 
242275Ssherrym static const uint32_t save_fp_instr[] = {
243170Ssherrym 	0xe5894855,	/* pushq %rbp; movq %rsp,%rbp, encoding 1 */
244275Ssherrym 	0xec8b4855,	/* pushq %rbp; movq %rsp,%rbp, encoding 2 */
245275Ssherrym 	0xe58948cc,	/* int $0x3; movq %rsp,%rbp, encoding 1 */
246275Ssherrym 	0xec8b48cc,	/* int $0x3; movq %rsp,%rbp, encoding 2 */
247275Ssherrym 	NULL
248170Ssherrym };
249170Ssherrym 
250170Ssherrym /*
251170Ssherrym  * Look for the above instruction sequences as indicators for register
252170Ssherrym  * arguments being available on the stack.
253170Ssherrym  */
254170Ssherrym static int
255170Ssherrym is_argsaved(mdb_tgt_t *t, uintptr_t fstart, uint64_t size, uint_t argc,
256170Ssherrym     int start_index)
257170Ssherrym {
258170Ssherrym 	uint8_t		ins[SEQ_LEN];
259170Ssherrym 	int		i, j;
260170Ssherrym 	uint32_t	n;
261170Ssherrym 
262170Ssherrym 	size = MIN(size, SEQ_LEN);
263170Ssherrym 	argc = MIN((start_index + argc), INSTR_ARRAY_SIZE);
264170Ssherrym 
265170Ssherrym 	if (mdb_tgt_vread(t, ins, size, fstart) != size)
266170Ssherrym 		return (0);
267170Ssherrym 
268170Ssherrym 	/*
269170Ssherrym 	 * Make sure framepointer has been saved.
270170Ssherrym 	 */
271170Ssherrym 	n = INSTR4(ins, 0);
272275Ssherrym 	for (i = 0; save_fp_instr[i] != NULL; i++) {
273170Ssherrym 		if (n == save_fp_instr[i])
274170Ssherrym 			break;
275170Ssherrym 	}
276170Ssherrym 
277275Ssherrym 	if (save_fp_instr[i] == NULL)
278170Ssherrym 		return (0);
279170Ssherrym 
280170Ssherrym 	/*
281170Ssherrym 	 * Compare against Sun Studio implementation
282170Ssherrym 	 */
283170Ssherrym 	for (i = 8, j = start_index; i < size - 4; i++) {
284170Ssherrym 		n = INSTR4(ins, i);
285170Ssherrym 
286170Ssherrym 		if (n == save_instr[j]) {
287170Ssherrym 			i += 3;
288170Ssherrym 			if (++j >= argc)
289170Ssherrym 				return (1);
290170Ssherrym 		}
291170Ssherrym 	}
292170Ssherrym 
293170Ssherrym 	/*
294170Ssherrym 	 * Compare against GCC implementation
295170Ssherrym 	 */
296170Ssherrym 	for (i = 8, j = argc - 1; i < size - 4; i++) {
297170Ssherrym 		n = INSTR4(ins, i);
298170Ssherrym 
299170Ssherrym 		if (n == save_instr[j]) {
300170Ssherrym 			i += 3;
301170Ssherrym 			if (--j < start_index)
302170Ssherrym 				return (1);
303170Ssherrym 		}
304170Ssherrym 	}
305170Ssherrym 
306170Ssherrym 	return (0);
307170Ssherrym }
308170Ssherrym 
3090Sstevel@tonic-gate int
3100Sstevel@tonic-gate mdb_amd64_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
3110Sstevel@tonic-gate     mdb_tgt_stack_f *func, void *arg)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
3140Sstevel@tonic-gate 	kreg_t *kregs = &gregs.kregs[0];
3150Sstevel@tonic-gate 	int got_pc = (gsp->kregs[KREG_RIP] != 0);
316170Ssherrym 	uint_t argc, reg_argc;
317170Ssherrym 	long fr_argv[32];
318170Ssherrym 	int start_index; /* index to save_instr where to start comparison */
319170Ssherrym 	int i;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	struct {
3220Sstevel@tonic-gate 		uintptr_t fr_savfp;
3230Sstevel@tonic-gate 		uintptr_t fr_savpc;
3240Sstevel@tonic-gate 	} fr;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	uintptr_t fp = gsp->kregs[KREG_RBP];
3270Sstevel@tonic-gate 	uintptr_t pc = gsp->kregs[KREG_RIP];
328170Ssherrym 	uintptr_t curpc;
329170Ssherrym 
330170Ssherrym 	ssize_t size;
331170Ssherrym 
332170Ssherrym 	GElf_Sym s;
333170Ssherrym 	mdb_syminfo_t sip;
334170Ssherrym 	mdb_ctf_funcinfo_t mfp;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	bcopy(gsp, &gregs, sizeof (gregs));
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	while (fp != 0) {
3390Sstevel@tonic-gate 
340170Ssherrym 		curpc = pc;
341170Ssherrym 
3420Sstevel@tonic-gate 		if (fp & (STACK_ALIGN - 1))
3430Sstevel@tonic-gate 			return (set_errno(EMDB_STKALIGN));
3440Sstevel@tonic-gate 
345170Ssherrym 		if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) != sizeof (fr))
346170Ssherrym 			return (-1);	/* errno has been set for us */
347170Ssherrym 
348170Ssherrym 		if ((mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
349170Ssherrym 		    NULL, 0, &s, &sip) == 0) &&
350170Ssherrym 		    (mdb_ctf_func_info(&s, &sip, &mfp) == 0)) {
351170Ssherrym 			int return_type = mdb_ctf_type_kind(mfp.mtf_return);
352170Ssherrym 			argc = mfp.mtf_argc;
353170Ssherrym 			/*
354170Ssherrym 			 * If the function returns a structure or union,
355170Ssherrym 			 * %rdi contains the address in which to store the
356170Ssherrym 			 * return value rather than for an argument.
357170Ssherrym 			 */
358170Ssherrym 			if (return_type == CTF_K_STRUCT ||
359170Ssherrym 			    return_type == CTF_K_UNION)
360170Ssherrym 				start_index = 1;
361170Ssherrym 			else
362170Ssherrym 				start_index = 0;
363170Ssherrym 		} else {
364170Ssherrym 			argc = 0;
365170Ssherrym 		}
366170Ssherrym 
367170Ssherrym 		if (argc != 0 && is_argsaved(t, s.st_value, s.st_size,
368170Ssherrym 		    argc, start_index)) {
3690Sstevel@tonic-gate 
370170Ssherrym 			/* Upto to 6 arguments are passed via registers */
371170Ssherrym 			reg_argc = MIN(6, mfp.mtf_argc);
372170Ssherrym 			size = reg_argc * sizeof (long);
373170Ssherrym 
374170Ssherrym 			if (mdb_tgt_vread(t, fr_argv, size, (fp - size))
375170Ssherrym 			    != size)
376170Ssherrym 				return (-1);	/* errno has been set for us */
377170Ssherrym 
378170Ssherrym 			/*
379170Ssherrym 			 * Arrange the arguments in the right order for
380170Ssherrym 			 * printing.
381170Ssherrym 			 */
382170Ssherrym 			for (i = 0; i < (reg_argc >> 1); i++) {
383170Ssherrym 				long t = fr_argv[i];
384170Ssherrym 
385170Ssherrym 				fr_argv[i] = fr_argv[reg_argc - i - 1];
386170Ssherrym 				fr_argv[reg_argc - i - 1] = t;
387170Ssherrym 			}
388170Ssherrym 
389170Ssherrym 			if (argc > 6) {
390170Ssherrym 				size = (argc - 6) * sizeof (long);
391170Ssherrym 				if (mdb_tgt_vread(t, &fr_argv[6], size,
392170Ssherrym 				    fp + sizeof (fr)) != size)
393170Ssherrym 					return (-1); /* errno has been set */
394170Ssherrym 			}
395170Ssherrym 		} else
396170Ssherrym 			argc = 0;
397170Ssherrym 
398170Ssherrym 		if (got_pc && func(arg, pc, argc, fr_argv, &gregs) != 0)
3990Sstevel@tonic-gate 			break;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		kregs[KREG_RSP] = kregs[KREG_RBP];
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		kregs[KREG_RBP] = fp = fr.fr_savfp;
4040Sstevel@tonic-gate 		kregs[KREG_RIP] = pc = fr.fr_savpc;
4050Sstevel@tonic-gate 
406170Ssherrym 		if (curpc == pc)
407170Ssherrym 			break;
408170Ssherrym 
4090Sstevel@tonic-gate 		got_pc = (pc != 0);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	return (0);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * Determine the return address for the current frame.  Typically this is the
4170Sstevel@tonic-gate  * fr_savpc value from the current frame, but we also perform some special
4180Sstevel@tonic-gate  * handling to see if we are stopped on one of the first two instructions of
4190Sstevel@tonic-gate  * a typical function prologue, in which case %rbp will not be set up yet.
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate int
4220Sstevel@tonic-gate mdb_amd64_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp,
4230Sstevel@tonic-gate     mdb_instr_t curinstr)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	struct frame fr;
4260Sstevel@tonic-gate 	GElf_Sym s;
4270Sstevel@tonic-gate 	char buf[1];
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	enum {
4300Sstevel@tonic-gate 		M_PUSHQ_RBP	= 0x55,	/* pushq %rbp */
4310Sstevel@tonic-gate 		M_REX_W		= 0x48, /* REX prefix with only W set */
4320Sstevel@tonic-gate 		M_MOVL_RBP	= 0x8b	/* movq %rsp, %rbp with prefix */
4330Sstevel@tonic-gate 	};
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY,
4360Sstevel@tonic-gate 	    buf, 0, &s, NULL) == 0) {
4370Sstevel@tonic-gate 		if (pc == s.st_value && curinstr == M_PUSHQ_RBP)
4380Sstevel@tonic-gate 			fp = sp - 8;
4390Sstevel@tonic-gate 		else if (pc == s.st_value + 1 && curinstr == M_REX_W) {
4400Sstevel@tonic-gate 			if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr),
4410Sstevel@tonic-gate 			    pc + 1) == sizeof (curinstr) && curinstr ==
4420Sstevel@tonic-gate 			    M_MOVL_RBP)
4430Sstevel@tonic-gate 				fp = sp;
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) {
4480Sstevel@tonic-gate 		*p = fr.fr_savpc;
4490Sstevel@tonic-gate 		return (0);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	return (-1); /* errno is set for us */
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*ARGSUSED*/
4560Sstevel@tonic-gate int
4570Sstevel@tonic-gate mdb_amd64_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	mdb_tgt_addr_t npc;
460*2244Sdmick 	mdb_tgt_addr_t callpc;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	enum {
4630Sstevel@tonic-gate 		M_CALL_REL = 0xe8, /* call near with relative displacement */
4640Sstevel@tonic-gate 		M_CALL_REG = 0xff, /* call near indirect or call far register */
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		M_REX_LO = 0x40,
4670Sstevel@tonic-gate 		M_REX_HI = 0x4f
4680Sstevel@tonic-gate 	};
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * If the opcode is a near call with relative displacement, assume the
4720Sstevel@tonic-gate 	 * displacement is a rel32 from the next instruction.
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	if (curinstr == M_CALL_REL) {
4750Sstevel@tonic-gate 		*p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t);
4760Sstevel@tonic-gate 		return (0);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* Skip the rex prefix, if any */
480*2244Sdmick 	callpc = pc;
481*2244Sdmick 	while (curinstr >= M_REX_LO && curinstr <= M_REX_HI) {
482*2244Sdmick 		if (mdb_tgt_vread(t, &curinstr, sizeof (curinstr), ++callpc) !=
483*2244Sdmick 		    sizeof (curinstr))
484*2244Sdmick 			return (-1); /* errno is set for us */
485*2244Sdmick 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (curinstr != M_CALL_REG) {
4880Sstevel@tonic-gate 		/* It's not a call */
4890Sstevel@tonic-gate 		return (set_errno(EAGAIN));
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if ((npc = mdb_dis_nextins(mdb.m_disasm, t, MDB_TGT_AS_VIRT, pc)) == pc)
4930Sstevel@tonic-gate 		return (-1); /* errno is set for us */
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	*p = npc;
4960Sstevel@tonic-gate 	return (0);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*ARGSUSED*/
5000Sstevel@tonic-gate int
5010Sstevel@tonic-gate mdb_amd64_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
5020Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	argc = MIN(argc, (uintptr_t)arglim);
5050Sstevel@tonic-gate 	mdb_printf("%a(", pc);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (argc != 0) {
5080Sstevel@tonic-gate 		mdb_printf("%lr", *argv++);
5090Sstevel@tonic-gate 		for (argc--; argc != 0; argc--)
5100Sstevel@tonic-gate 			mdb_printf(", %lr", *argv++);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	mdb_printf(")\n");
5140Sstevel@tonic-gate 	return (0);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate int
5180Sstevel@tonic-gate mdb_amd64_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
5190Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
5200Sstevel@tonic-gate {
521170Ssherrym 	/*
522170Ssherrym 	 * Historically adb limited stack trace argument display to a fixed-
523170Ssherrym 	 * size number of arguments since no symbolic debugging info existed.
524170Ssherrym 	 * On amd64 we can detect the true number of saved arguments so only
525170Ssherrym 	 * respect an arglim of zero; otherwise display the entire argv[].
526170Ssherrym 	 */
527170Ssherrym 	if (arglim == 0)
528170Ssherrym 		argc = 0;
529170Ssherrym 
5300Sstevel@tonic-gate 	mdb_printf("%0?lr %a(", gregs->kregs[KREG_RBP], pc);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (argc != 0) {
5330Sstevel@tonic-gate 		mdb_printf("%lr", *argv++);
5340Sstevel@tonic-gate 		for (argc--; argc != 0; argc--)
5350Sstevel@tonic-gate 			mdb_printf(", %lr", *argv++);
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	mdb_printf(")\n");
5390Sstevel@tonic-gate 	return (0);
5400Sstevel@tonic-gate }
541