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 5843e1988Sjohnlev * Common Development and Distribution License (the "License"). 6843e1988Sjohnlev * 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 */ 217c478bd9Sstevel@tonic-gate /* 22843e1988Sjohnlev * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 250a47c91cSRobert Mustacchi /* 260a47c91cSRobert Mustacchi * Copyright (c) 2012, Joyent, Inc. All rights reserved. 270b453801SGordon Ross * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 280a47c91cSRobert Mustacchi */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/reg.h> 327c478bd9Sstevel@tonic-gate #include <sys/privregs.h> 337c478bd9Sstevel@tonic-gate #include <sys/stack.h> 347c478bd9Sstevel@tonic-gate #include <sys/frame.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <mdb/mdb_ia32util.h> 377c478bd9Sstevel@tonic-gate #include <mdb/mdb_target_impl.h> 387c478bd9Sstevel@tonic-gate #include <mdb/mdb_kreg_impl.h> 397c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h> 407c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h> 417c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h> 427c478bd9Sstevel@tonic-gate #include <mdb/mdb.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * We also define an array of register names and their corresponding 467c478bd9Sstevel@tonic-gate * array indices. This is used by the getareg and putareg entry points, 477c478bd9Sstevel@tonic-gate * and also by our register variable discipline. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate const mdb_tgt_regdesc_t mdb_ia32_kregs[] = { 507c478bd9Sstevel@tonic-gate { "savfp", KREG_SAVFP, MDB_TGT_R_EXPORT }, 517c478bd9Sstevel@tonic-gate { "savpc", KREG_SAVPC, MDB_TGT_R_EXPORT }, 527c478bd9Sstevel@tonic-gate { "eax", KREG_EAX, MDB_TGT_R_EXPORT }, 530a47c91cSRobert Mustacchi { "ax", KREG_EAX, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 540a47c91cSRobert Mustacchi { "ah", KREG_EAX, MDB_TGT_R_EXPORT | MDB_TGT_R_8H }, 550a47c91cSRobert Mustacchi { "al", KREG_EAX, MDB_TGT_R_EXPORT | MDB_TGT_R_8L }, 567c478bd9Sstevel@tonic-gate { "ebx", KREG_EBX, MDB_TGT_R_EXPORT }, 570a47c91cSRobert Mustacchi { "bx", KREG_EBX, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 580a47c91cSRobert Mustacchi { "bh", KREG_EBX, MDB_TGT_R_EXPORT | MDB_TGT_R_8H }, 590a47c91cSRobert Mustacchi { "bl", KREG_EBX, MDB_TGT_R_EXPORT | MDB_TGT_R_8L }, 607c478bd9Sstevel@tonic-gate { "ecx", KREG_ECX, MDB_TGT_R_EXPORT }, 610a47c91cSRobert Mustacchi { "cx", KREG_ECX, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 620a47c91cSRobert Mustacchi { "ch", KREG_ECX, MDB_TGT_R_EXPORT | MDB_TGT_R_8H }, 630a47c91cSRobert Mustacchi { "cl", KREG_ECX, MDB_TGT_R_EXPORT | MDB_TGT_R_8L }, 647c478bd9Sstevel@tonic-gate { "edx", KREG_EDX, MDB_TGT_R_EXPORT }, 650a47c91cSRobert Mustacchi { "dx", KREG_EDX, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 660a47c91cSRobert Mustacchi { "dh", KREG_EDX, MDB_TGT_R_EXPORT | MDB_TGT_R_8H }, 670a47c91cSRobert Mustacchi { "dl", KREG_EDX, MDB_TGT_R_EXPORT | MDB_TGT_R_8L }, 687c478bd9Sstevel@tonic-gate { "esi", KREG_ESI, MDB_TGT_R_EXPORT }, 690a47c91cSRobert Mustacchi { "si", KREG_ESI, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 707c478bd9Sstevel@tonic-gate { "edi", KREG_EDI, MDB_TGT_R_EXPORT }, 710a47c91cSRobert Mustacchi { "di", EDI, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 727c478bd9Sstevel@tonic-gate { "ebp", KREG_EBP, MDB_TGT_R_EXPORT }, 730a47c91cSRobert Mustacchi { "bp", KREG_EBP, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 747c478bd9Sstevel@tonic-gate { "esp", KREG_ESP, MDB_TGT_R_EXPORT }, 750a47c91cSRobert Mustacchi { "sp", KREG_ESP, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 767c478bd9Sstevel@tonic-gate { "cs", KREG_CS, MDB_TGT_R_EXPORT }, 777c478bd9Sstevel@tonic-gate { "ds", KREG_DS, MDB_TGT_R_EXPORT }, 787c478bd9Sstevel@tonic-gate { "ss", KREG_SS, MDB_TGT_R_EXPORT }, 797c478bd9Sstevel@tonic-gate { "es", KREG_ES, MDB_TGT_R_EXPORT }, 807c478bd9Sstevel@tonic-gate { "fs", KREG_FS, MDB_TGT_R_EXPORT }, 817c478bd9Sstevel@tonic-gate { "gs", KREG_GS, MDB_TGT_R_EXPORT }, 827c478bd9Sstevel@tonic-gate { "eflags", KREG_EFLAGS, MDB_TGT_R_EXPORT }, 837c478bd9Sstevel@tonic-gate { "eip", KREG_EIP, MDB_TGT_R_EXPORT }, 847c478bd9Sstevel@tonic-gate { "uesp", KREG_UESP, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 850a47c91cSRobert Mustacchi { "usp", KREG_UESP, MDB_TGT_R_EXPORT | MDB_TGT_R_16 }, 867c478bd9Sstevel@tonic-gate { "trapno", KREG_TRAPNO, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 877c478bd9Sstevel@tonic-gate { "err", KREG_ERR, MDB_TGT_R_EXPORT | MDB_TGT_R_PRIV }, 887c478bd9Sstevel@tonic-gate { NULL, 0, 0 } 897c478bd9Sstevel@tonic-gate }; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate void 927c478bd9Sstevel@tonic-gate mdb_ia32_printregs(const mdb_tgt_gregset_t *gregs) 937c478bd9Sstevel@tonic-gate { 947c478bd9Sstevel@tonic-gate const kreg_t *kregs = &gregs->kregs[0]; 957c478bd9Sstevel@tonic-gate kreg_t eflags = kregs[KREG_EFLAGS]; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate mdb_printf("%%cs = 0x%04x\t\t%%eax = 0x%0?p %A\n", 987c478bd9Sstevel@tonic-gate kregs[KREG_CS], kregs[KREG_EAX], kregs[KREG_EAX]); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate mdb_printf("%%ds = 0x%04x\t\t%%ebx = 0x%0?p %A\n", 1017c478bd9Sstevel@tonic-gate kregs[KREG_DS], kregs[KREG_EBX], kregs[KREG_EBX]); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate mdb_printf("%%ss = 0x%04x\t\t%%ecx = 0x%0?p %A\n", 1047c478bd9Sstevel@tonic-gate kregs[KREG_SS], kregs[KREG_ECX], kregs[KREG_ECX]); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate mdb_printf("%%es = 0x%04x\t\t%%edx = 0x%0?p %A\n", 1077c478bd9Sstevel@tonic-gate kregs[KREG_ES], kregs[KREG_EDX], kregs[KREG_EDX]); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate mdb_printf("%%fs = 0x%04x\t\t%%esi = 0x%0?p %A\n", 1107c478bd9Sstevel@tonic-gate kregs[KREG_FS], kregs[KREG_ESI], kregs[KREG_ESI]); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate mdb_printf("%%gs = 0x%04x\t\t%%edi = 0x%0?p %A\n\n", 1137c478bd9Sstevel@tonic-gate kregs[KREG_GS], kregs[KREG_EDI], kregs[KREG_EDI]); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate mdb_printf("%%eip = 0x%0?p %A\n", kregs[KREG_EIP], kregs[KREG_EIP]); 1167c478bd9Sstevel@tonic-gate mdb_printf("%%ebp = 0x%0?p\n", kregs[KREG_EBP]); 1177c478bd9Sstevel@tonic-gate mdb_printf("%%esp = 0x%0?p\n\n", kregs[KREG_ESP]); 1187c478bd9Sstevel@tonic-gate mdb_printf("%%eflags = 0x%08x\n", eflags); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate mdb_printf(" id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n", 1217c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT, 1227c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT, 1237c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT, 1247c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT, 1257c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT, 1267c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT, 1277c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT, 1287c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate mdb_printf(" status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n\n", 1317c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of", 1327c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df", 1337c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if", 1347c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf", 1357c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf", 1367c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf", 1377c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af", 1387c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf", 1397c478bd9Sstevel@tonic-gate (eflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf"); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate #ifndef _KMDB 1427c478bd9Sstevel@tonic-gate mdb_printf(" %%uesp = 0x%0?x\n", kregs[KREG_UESP]); 1437c478bd9Sstevel@tonic-gate #endif 1447c478bd9Sstevel@tonic-gate mdb_printf("%%trapno = 0x%x\n", kregs[KREG_TRAPNO]); 1457c478bd9Sstevel@tonic-gate mdb_printf(" %%err = 0x%x\n", kregs[KREG_ERR]); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * Given a return address (%eip), determine the likely number of arguments 1507c478bd9Sstevel@tonic-gate * that were pushed on the stack prior to its execution. We do this by 1517c478bd9Sstevel@tonic-gate * expecting that a typical call sequence consists of pushing arguments on 1527c478bd9Sstevel@tonic-gate * the stack, executing a call instruction, and then performing an add 1537c478bd9Sstevel@tonic-gate * on %esp to restore it to the value prior to pushing the arguments for 1547c478bd9Sstevel@tonic-gate * the call. We attempt to detect such an add, and divide the addend 1557c478bd9Sstevel@tonic-gate * by the size of a word to determine the number of pushed arguments. 1567c478bd9Sstevel@tonic-gate */ 1577c478bd9Sstevel@tonic-gate static uint_t 1587c478bd9Sstevel@tonic-gate kvm_argcount(mdb_tgt_t *t, uintptr_t eip, ssize_t size) 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate uint8_t ins[6]; 1617c478bd9Sstevel@tonic-gate ulong_t n; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate enum { 1647c478bd9Sstevel@tonic-gate M_MODRM_ESP = 0xc4, /* Mod/RM byte indicates %esp */ 1657c478bd9Sstevel@tonic-gate M_ADD_IMM32 = 0x81, /* ADD imm32 to r/m32 */ 1667c478bd9Sstevel@tonic-gate M_ADD_IMM8 = 0x83 /* ADD imm8 to r/m32 */ 1677c478bd9Sstevel@tonic-gate }; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate if (mdb_tgt_vread(t, ins, sizeof (ins), eip) != sizeof (ins)) 1707c478bd9Sstevel@tonic-gate return (0); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if (ins[1] != M_MODRM_ESP) 1737c478bd9Sstevel@tonic-gate return (0); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate switch (ins[0]) { 1767c478bd9Sstevel@tonic-gate case M_ADD_IMM32: 1777c478bd9Sstevel@tonic-gate n = ins[2] + (ins[3] << 8) + (ins[4] << 16) + (ins[5] << 24); 1787c478bd9Sstevel@tonic-gate break; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate case M_ADD_IMM8: 1817c478bd9Sstevel@tonic-gate n = ins[2]; 1827c478bd9Sstevel@tonic-gate break; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate default: 1857c478bd9Sstevel@tonic-gate n = 0; 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate return (MIN((ssize_t)n, size) / sizeof (long)); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate int 1927c478bd9Sstevel@tonic-gate mdb_ia32_kvm_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp, 1937c478bd9Sstevel@tonic-gate mdb_tgt_stack_f *func, void *arg) 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate mdb_tgt_gregset_t gregs; 1967c478bd9Sstevel@tonic-gate kreg_t *kregs = &gregs.kregs[0]; 1977c478bd9Sstevel@tonic-gate int got_pc = (gsp->kregs[KREG_EIP] != 0); 1980b453801SGordon Ross int err; 1997c478bd9Sstevel@tonic-gate 200*32b5e9f0SRichard Lowe struct fr { 2017c478bd9Sstevel@tonic-gate uintptr_t fr_savfp; 2027c478bd9Sstevel@tonic-gate uintptr_t fr_savpc; 2037c478bd9Sstevel@tonic-gate long fr_argv[32]; 2047c478bd9Sstevel@tonic-gate } fr; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate uintptr_t fp = gsp->kregs[KREG_EBP]; 2077c478bd9Sstevel@tonic-gate uintptr_t pc = gsp->kregs[KREG_EIP]; 2080b453801SGordon Ross uintptr_t lastfp = 0; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate ssize_t size; 2117c478bd9Sstevel@tonic-gate uint_t argc; 212843e1988Sjohnlev int detect_exception_frames = 0; 213*32b5e9f0SRichard Lowe int advance_tortoise = 1; 214*32b5e9f0SRichard Lowe uintptr_t tortoise_fp = 0; 215843e1988Sjohnlev #ifndef _KMDB 216843e1988Sjohnlev int xp; 217843e1988Sjohnlev 218843e1988Sjohnlev if ((mdb_readsym(&xp, sizeof (xp), "xpv_panicking") != -1) && (xp > 0)) 219843e1988Sjohnlev detect_exception_frames = 1; 220843e1988Sjohnlev #endif 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate bcopy(gsp, &gregs, sizeof (gregs)); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate while (fp != 0) { 2250b453801SGordon Ross if (fp & (STACK_ALIGN - 1)) { 2260b453801SGordon Ross err = EMDB_STKALIGN; 2270b453801SGordon Ross goto badfp; 2280b453801SGordon Ross } 2297c478bd9Sstevel@tonic-gate if ((size = mdb_tgt_vread(t, &fr, sizeof (fr), fp)) >= 2307c478bd9Sstevel@tonic-gate (ssize_t)(2 * sizeof (uintptr_t))) { 2317c478bd9Sstevel@tonic-gate size -= (ssize_t)(2 * sizeof (uintptr_t)); 2327c478bd9Sstevel@tonic-gate argc = kvm_argcount(t, fr.fr_savpc, size); 2337c478bd9Sstevel@tonic-gate } else { 2340b453801SGordon Ross err = EMDB_NOMAP; 2350b453801SGordon Ross goto badfp; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 238*32b5e9f0SRichard Lowe if (tortoise_fp == 0) { 239*32b5e9f0SRichard Lowe tortoise_fp = fp; 240*32b5e9f0SRichard Lowe } else { 241*32b5e9f0SRichard Lowe /* 242*32b5e9f0SRichard Lowe * Advance tortoise_fp every other frame, so we detect 243*32b5e9f0SRichard Lowe * cycles with Floyd's tortoise/hare. 244*32b5e9f0SRichard Lowe */ 245*32b5e9f0SRichard Lowe if (advance_tortoise != 0) { 246*32b5e9f0SRichard Lowe struct fr tfr; 247*32b5e9f0SRichard Lowe 248*32b5e9f0SRichard Lowe if (mdb_tgt_vread(t, &tfr, sizeof (tfr), 249*32b5e9f0SRichard Lowe tortoise_fp) != sizeof (tfr)) { 250*32b5e9f0SRichard Lowe err = EMDB_NOMAP; 251*32b5e9f0SRichard Lowe goto badfp; 252*32b5e9f0SRichard Lowe } 253*32b5e9f0SRichard Lowe 254*32b5e9f0SRichard Lowe tortoise_fp = tfr.fr_savfp; 255*32b5e9f0SRichard Lowe } 256*32b5e9f0SRichard Lowe 257*32b5e9f0SRichard Lowe if (fp == tortoise_fp) { 258*32b5e9f0SRichard Lowe err = EMDB_STKFRAME; 259*32b5e9f0SRichard Lowe goto badfp; 260*32b5e9f0SRichard Lowe } 261*32b5e9f0SRichard Lowe } 262*32b5e9f0SRichard Lowe 263*32b5e9f0SRichard Lowe advance_tortoise = !advance_tortoise; 264*32b5e9f0SRichard Lowe 2657c478bd9Sstevel@tonic-gate if (got_pc && func(arg, pc, argc, fr.fr_argv, &gregs) != 0) 2667c478bd9Sstevel@tonic-gate break; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate kregs[KREG_ESP] = kregs[KREG_EBP]; 2697c478bd9Sstevel@tonic-gate 270843e1988Sjohnlev lastfp = fp; 271843e1988Sjohnlev fp = fr.fr_savfp; 272843e1988Sjohnlev /* 273843e1988Sjohnlev * The Xen hypervisor marks a stack frame as belonging to 274843e1988Sjohnlev * an exception by inverting the bits of the pointer to 275843e1988Sjohnlev * that frame. We attempt to identify these frames by 276843e1988Sjohnlev * inverting the pointer and seeing if it is within 0xfff 277843e1988Sjohnlev * bytes of the last frame. 278843e1988Sjohnlev */ 279843e1988Sjohnlev if (detect_exception_frames) 280843e1988Sjohnlev if ((fp != 0) && (fp < lastfp) && 281843e1988Sjohnlev ((lastfp ^ ~fp) < 0xfff)) 282843e1988Sjohnlev fp = ~fp; 283843e1988Sjohnlev 284843e1988Sjohnlev kregs[KREG_EBP] = fp; 2857c478bd9Sstevel@tonic-gate kregs[KREG_EIP] = pc = fr.fr_savpc; 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate got_pc = (pc != 0); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate return (0); 2910b453801SGordon Ross 2920b453801SGordon Ross badfp: 2930b453801SGordon Ross mdb_printf("%p [%s]", fp, mdb_strerror(err)); 2940b453801SGordon Ross return (set_errno(err)); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* 2987c478bd9Sstevel@tonic-gate * Determine the return address for the current frame. Typically this is the 2997c478bd9Sstevel@tonic-gate * fr_savpc value from the current frame, but we also perform some special 3007c478bd9Sstevel@tonic-gate * handling to see if we are stopped on one of the first two instructions of a 3017c478bd9Sstevel@tonic-gate * typical function prologue, in which case %ebp will not be set up yet. 3027c478bd9Sstevel@tonic-gate */ 3037c478bd9Sstevel@tonic-gate int 3047c478bd9Sstevel@tonic-gate mdb_ia32_step_out(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, kreg_t fp, kreg_t sp, 3057c478bd9Sstevel@tonic-gate mdb_instr_t curinstr) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate struct frame fr; 3087c478bd9Sstevel@tonic-gate GElf_Sym s; 3097c478bd9Sstevel@tonic-gate char buf[1]; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate enum { 3127c478bd9Sstevel@tonic-gate M_PUSHL_EBP = 0x55, /* pushl %ebp */ 3137c478bd9Sstevel@tonic-gate M_MOVL_EBP = 0x8b /* movl %esp, %ebp */ 3147c478bd9Sstevel@tonic-gate }; 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, 3177c478bd9Sstevel@tonic-gate buf, 0, &s, NULL) == 0) { 3187c478bd9Sstevel@tonic-gate if (pc == s.st_value && curinstr == M_PUSHL_EBP) 3197c478bd9Sstevel@tonic-gate fp = sp - 4; 3207c478bd9Sstevel@tonic-gate else if (pc == s.st_value + 1 && curinstr == M_MOVL_EBP) 3217c478bd9Sstevel@tonic-gate fp = sp; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if (mdb_tgt_vread(t, &fr, sizeof (fr), fp) == sizeof (fr)) { 3257c478bd9Sstevel@tonic-gate *p = fr.fr_savpc; 3267c478bd9Sstevel@tonic-gate return (0); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate return (-1); /* errno is set for us */ 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate * Return the address of the next instruction following a call, or return -1 3347c478bd9Sstevel@tonic-gate * and set errno to EAGAIN if the target should just single-step. We perform 3357c478bd9Sstevel@tonic-gate * a bit of disassembly on the current instruction in order to determine if it 3367c478bd9Sstevel@tonic-gate * is a call and how many bytes should be skipped, depending on the exact form 3377c478bd9Sstevel@tonic-gate * of the call instruction that is being used. 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate int 3407c478bd9Sstevel@tonic-gate mdb_ia32_next(mdb_tgt_t *t, uintptr_t *p, kreg_t pc, mdb_instr_t curinstr) 3417c478bd9Sstevel@tonic-gate { 3427c478bd9Sstevel@tonic-gate uint8_t m; 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate enum { 3457c478bd9Sstevel@tonic-gate M_CALL_REL = 0xe8, /* call near with relative displacement */ 3467c478bd9Sstevel@tonic-gate M_CALL_REG = 0xff, /* call near indirect or call far register */ 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate M_MODRM_MD = 0xc0, /* mask for Mod/RM byte Mod field */ 3497c478bd9Sstevel@tonic-gate M_MODRM_OP = 0x38, /* mask for Mod/RM byte opcode field */ 3507c478bd9Sstevel@tonic-gate M_MODRM_RM = 0x07, /* mask for Mod/RM byte R/M field */ 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate M_MD_IND = 0x00, /* Mod code for [REG] */ 3537c478bd9Sstevel@tonic-gate M_MD_DSP8 = 0x40, /* Mod code for disp8[REG] */ 3547c478bd9Sstevel@tonic-gate M_MD_DSP32 = 0x80, /* Mod code for disp32[REG] */ 3557c478bd9Sstevel@tonic-gate M_MD_REG = 0xc0, /* Mod code for REG */ 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate M_OP_IND = 0x10, /* Opcode for call near indirect */ 3587c478bd9Sstevel@tonic-gate M_RM_DSP32 = 0x05 /* R/M code for disp32 */ 3597c478bd9Sstevel@tonic-gate }; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * If the opcode is a near call with relative displacement, assume the 3637c478bd9Sstevel@tonic-gate * displacement is a rel32 from the next instruction. 3647c478bd9Sstevel@tonic-gate */ 3657c478bd9Sstevel@tonic-gate if (curinstr == M_CALL_REL) { 3667c478bd9Sstevel@tonic-gate *p = pc + sizeof (mdb_instr_t) + sizeof (uint32_t); 3677c478bd9Sstevel@tonic-gate return (0); 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * If the opcode is a call near indirect or call far register opcode, 3727c478bd9Sstevel@tonic-gate * read the subsequent Mod/RM byte to perform additional decoding. 3737c478bd9Sstevel@tonic-gate */ 3747c478bd9Sstevel@tonic-gate if (curinstr == M_CALL_REG) { 3757c478bd9Sstevel@tonic-gate if (mdb_tgt_vread(t, &m, sizeof (m), pc + 1) != sizeof (m)) 3767c478bd9Sstevel@tonic-gate return (-1); /* errno is set for us */ 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate /* 3797c478bd9Sstevel@tonic-gate * If the Mod/RM opcode extension indicates a near indirect 3807c478bd9Sstevel@tonic-gate * call, then skip the appropriate number of additional 3817c478bd9Sstevel@tonic-gate * bytes depending on the addressing form that is used. 3827c478bd9Sstevel@tonic-gate */ 3837c478bd9Sstevel@tonic-gate if ((m & M_MODRM_OP) == M_OP_IND) { 3847c478bd9Sstevel@tonic-gate switch (m & M_MODRM_MD) { 3857c478bd9Sstevel@tonic-gate case M_MD_DSP8: 3867c478bd9Sstevel@tonic-gate *p = pc + 3; /* skip pr_instr, m, disp8 */ 3877c478bd9Sstevel@tonic-gate break; 3887c478bd9Sstevel@tonic-gate case M_MD_DSP32: 3897c478bd9Sstevel@tonic-gate *p = pc + 6; /* skip pr_instr, m, disp32 */ 3907c478bd9Sstevel@tonic-gate break; 3917c478bd9Sstevel@tonic-gate case M_MD_IND: 3927c478bd9Sstevel@tonic-gate if ((m & M_MODRM_RM) == M_RM_DSP32) { 3937c478bd9Sstevel@tonic-gate *p = pc + 6; 3947c478bd9Sstevel@tonic-gate break; /* skip pr_instr, m, disp32 */ 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate /* FALLTHRU */ 3977c478bd9Sstevel@tonic-gate case M_MD_REG: 3987c478bd9Sstevel@tonic-gate *p = pc + 2; /* skip pr_instr, m */ 3997c478bd9Sstevel@tonic-gate break; 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate return (0); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate return (set_errno(EAGAIN)); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4097c478bd9Sstevel@tonic-gate int 4107c478bd9Sstevel@tonic-gate mdb_ia32_kvm_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv, 4117c478bd9Sstevel@tonic-gate const mdb_tgt_gregset_t *gregs) 4127c478bd9Sstevel@tonic-gate { 4137c478bd9Sstevel@tonic-gate argc = MIN(argc, (uint_t)arglim); 4147c478bd9Sstevel@tonic-gate mdb_printf("%a(", pc); 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate if (argc != 0) { 4177c478bd9Sstevel@tonic-gate mdb_printf("%lr", *argv++); 4187c478bd9Sstevel@tonic-gate for (argc--; argc != 0; argc--) 4197c478bd9Sstevel@tonic-gate mdb_printf(", %lr", *argv++); 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate mdb_printf(")\n"); 4237c478bd9Sstevel@tonic-gate return (0); 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate int 4277c478bd9Sstevel@tonic-gate mdb_ia32_kvm_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv, 4287c478bd9Sstevel@tonic-gate const mdb_tgt_gregset_t *gregs) 4297c478bd9Sstevel@tonic-gate { 4307c478bd9Sstevel@tonic-gate argc = MIN(argc, (uint_t)arglim); 4317c478bd9Sstevel@tonic-gate mdb_printf("%0?lr %a(", gregs->kregs[KREG_EBP], pc); 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate if (argc != 0) { 4347c478bd9Sstevel@tonic-gate mdb_printf("%lr", *argv++); 4357c478bd9Sstevel@tonic-gate for (argc--; argc != 0; argc--) 4367c478bd9Sstevel@tonic-gate mdb_printf(", %lr", *argv++); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate mdb_printf(")\n"); 4407c478bd9Sstevel@tonic-gate return (0); 4417c478bd9Sstevel@tonic-gate } 442