xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_proc.c (revision 30699046)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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  */
218fd04b83SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
238fd04b83SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
268f68126cSBryan Cantrill /*
27399ca3a7SJohn Levon  * Copyright 2018 Joyent, Inc.
28255ca53cSPrakash Surya  * Copyright (c) 2014 by Delphix. All rights reserved.
29a48fdbefSBryan Cantrill  * Copyright 2024 Oxide Computer Company
308f68126cSBryan Cantrill  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * User Process Target
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * The user process target is invoked when the -u or -p command-line options
367c478bd9Sstevel@tonic-gate  * are used, or when an ELF executable file or ELF core file is specified on
377c478bd9Sstevel@tonic-gate  * the command-line.  This target is also selected by default when no target
387c478bd9Sstevel@tonic-gate  * options are present.  In this case, it defaults the executable name to
397c478bd9Sstevel@tonic-gate  * "a.out".  If no process or core file is currently attached, the target
407c478bd9Sstevel@tonic-gate  * functions as a kind of virtual /dev/zero (in accordance with adb(1)
417c478bd9Sstevel@tonic-gate  * semantics); reads from the virtual address space return zeroes and writes
427c478bd9Sstevel@tonic-gate  * fail silently.  The proc target itself is designed as a wrapper around the
437c478bd9Sstevel@tonic-gate  * services provided by libproc.so: t->t_pshandle is set to the struct
447c478bd9Sstevel@tonic-gate  * ps_prochandle pointer returned as a handle by libproc.  The target also
457c478bd9Sstevel@tonic-gate  * opens the executable file itself using the MDB GElf services, for
467c478bd9Sstevel@tonic-gate  * interpreting the .symtab and .dynsym if no libproc handle has been
477c478bd9Sstevel@tonic-gate  * initialized, and for handling i/o to and from the object file.  Currently,
487c478bd9Sstevel@tonic-gate  * the only ISA-dependent portions of the proc target are the $r and ::fpregs
497c478bd9Sstevel@tonic-gate  * dcmds, the callbacks for t_next() and t_step_out(), and the list of named
507c478bd9Sstevel@tonic-gate  * registers; these are linked in from the proc_isadep.c file for each ISA and
517c478bd9Sstevel@tonic-gate  * called from the common code in this file.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * The user process target implements complete user process control using the
547c478bd9Sstevel@tonic-gate  * facilities provided by libproc.so.  The MDB execution control model and
557c478bd9Sstevel@tonic-gate  * an overview of software event management is described in mdb_target.c.  The
567c478bd9Sstevel@tonic-gate  * proc target implements breakpoints by replacing the instruction of interest
577c478bd9Sstevel@tonic-gate  * with a trap instruction, and then restoring the original instruction to step
587c478bd9Sstevel@tonic-gate  * over the breakpoint.  The idea of replacing program text with instructions
597c478bd9Sstevel@tonic-gate  * that transfer control to the debugger dates back as far as 1951 [1].  When
607c478bd9Sstevel@tonic-gate  * the target stops, we replace each breakpoint with the original instruction
617c478bd9Sstevel@tonic-gate  * as part of the disarm operation.  This means that no special processing is
627c478bd9Sstevel@tonic-gate  * required for t_vread() because the instrumented instructions will never be
637c478bd9Sstevel@tonic-gate  * seen by the debugger once the target stops.  Some debuggers have improved
647c478bd9Sstevel@tonic-gate  * start/stop performance by leaving breakpoint traps in place and then
657c478bd9Sstevel@tonic-gate  * handling a read from a breakpoint address as a special case.  Although this
667c478bd9Sstevel@tonic-gate  * improves efficiency for a source-level debugger, it runs somewhat contrary
677c478bd9Sstevel@tonic-gate  * to the philosophy of the low-level debugger.  Since we remove the
687c478bd9Sstevel@tonic-gate  * instructions, users can apply other external debugging tools to the process
697c478bd9Sstevel@tonic-gate  * once it has stopped (e.g. the proc(1) tools) and not be misled by MDB
707c478bd9Sstevel@tonic-gate  * instrumentation.  The tracing of faults, signals, system calls, and
717c478bd9Sstevel@tonic-gate  * watchpoints and general process inspection is implemented directly using
727c478bd9Sstevel@tonic-gate  * the mechanisms provided by /proc, as described originally in [2] and [3].
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * References
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * [1] S. Gill, "The Diagnosis Of Mistakes In Programmes on the EDSAC",
777c478bd9Sstevel@tonic-gate  *     Proceedings of the Royal Society Series A Mathematical and Physical
787c478bd9Sstevel@tonic-gate  *     Sciences, Cambridge University Press, 206(1087), May 1951, pp. 538-554.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * [2] T.J. Killian, "Processes as Files", Proceedings of the USENIX Association
817c478bd9Sstevel@tonic-gate  *     Summer Conference, Salt Lake City, June 1984, pp. 203-207.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * [3] Roger Faulkner and Ron Gomes, "The Process File System and Process
847c478bd9Sstevel@tonic-gate  *     Model in UNIX System V", Proceedings of the USENIX Association
857c478bd9Sstevel@tonic-gate  *     Winter Conference, Dallas, January 1991, pp. 243-252.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #include <mdb/mdb_proc.h>
897c478bd9Sstevel@tonic-gate #include <mdb/mdb_disasm.h>
907c478bd9Sstevel@tonic-gate #include <mdb/mdb_signal.h>
917c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
927c478bd9Sstevel@tonic-gate #include <mdb/mdb_module.h>
937c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
947c478bd9Sstevel@tonic-gate #include <mdb/mdb_conf.h>
957c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
967c478bd9Sstevel@tonic-gate #include <mdb/mdb_types.h>
977c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
1007c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1017c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1027c478bd9Sstevel@tonic-gate #include <termio.h>
1037c478bd9Sstevel@tonic-gate #include <signal.h>
104004388ebScasper #include <stdio_ext.h>
1057c478bd9Sstevel@tonic-gate #include <stdlib.h>
1067c478bd9Sstevel@tonic-gate #include <string.h>
107350ffdd5SRobert Mustacchi #include <ctype.h>
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #define	PC_FAKE		-1UL			/* illegal pc value unequal 0 */
110f68770eaSRobert Mustacchi #define	PANIC_BUFSIZE	1024
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static const char PT_EXEC_PATH[] = "a.out";	/* Default executable */
1137c478bd9Sstevel@tonic-gate static const char PT_CORE_PATH[] = "core";	/* Default core file */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static const pt_ptl_ops_t proc_lwp_ops;
1167c478bd9Sstevel@tonic-gate static const pt_ptl_ops_t proc_tdb_ops;
1177c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_brkpt_ops;
1187c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_wapt_ops;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static int pt_setrun(mdb_tgt_t *, mdb_tgt_status_t *, int);
1217c478bd9Sstevel@tonic-gate static void pt_activate_common(mdb_tgt_t *);
1227c478bd9Sstevel@tonic-gate static mdb_tgt_vespec_f pt_ignore_sig;
1237c478bd9Sstevel@tonic-gate static mdb_tgt_se_f pt_fork;
1247c478bd9Sstevel@tonic-gate static mdb_tgt_se_f pt_exec;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int pt_lookup_by_name_thr(mdb_tgt_t *, const char *,
1277c478bd9Sstevel@tonic-gate     const char *, GElf_Sym *, mdb_syminfo_t *, mdb_tgt_tid_t);
1287c478bd9Sstevel@tonic-gate static int tlsbase(mdb_tgt_t *, mdb_tgt_tid_t, Lmid_t, const char *,
1297c478bd9Sstevel@tonic-gate     psaddr_t *);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1328f68126cSBryan Cantrill  * When debugging postmortem, we don't resolve names as we may very well not
1338f68126cSBryan Cantrill  * be on a system on which those names resolve.
1348f68126cSBryan Cantrill  */
1358f68126cSBryan Cantrill #define	PT_LIBPROC_RESOLVE(P) \
1368f68126cSBryan Cantrill 	(!(mdb.m_flags & MDB_FL_LMRAW) && Pstate(P) != PS_DEAD)
1378f68126cSBryan Cantrill 
1388f68126cSBryan Cantrill /*
1397c478bd9Sstevel@tonic-gate  * The Perror_printf() function interposes on the default, empty libproc
1407c478bd9Sstevel@tonic-gate  * definition.  It will be called to report additional information on complex
1417c478bd9Sstevel@tonic-gate  * errors, such as a corrupt core file.  We just pass the args to vwarn.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1447c478bd9Sstevel@tonic-gate void
Perror_printf(struct ps_prochandle * P,const char * format,...)1457c478bd9Sstevel@tonic-gate Perror_printf(struct ps_prochandle *P, const char *format, ...)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	va_list alist;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	va_start(alist, format);
1507c478bd9Sstevel@tonic-gate 	vwarn(format, alist);
1517c478bd9Sstevel@tonic-gate 	va_end(alist);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Open the specified i/o backend as the a.out executable file, and attempt to
1567c478bd9Sstevel@tonic-gate  * load its standard and dynamic symbol tables.  Note that if mdb_gelf_create
1577c478bd9Sstevel@tonic-gate  * succeeds, io is assigned to p_fio and is automatically held by gelf_create.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate static mdb_gelf_file_t *
pt_open_aout(mdb_tgt_t * t,mdb_io_t * io)1607c478bd9Sstevel@tonic-gate pt_open_aout(mdb_tgt_t *t, mdb_io_t *io)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
1637c478bd9Sstevel@tonic-gate 	GElf_Sym s1, s2;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if ((pt->p_file = mdb_gelf_create(io, ET_NONE, GF_FILE)) == NULL)
1667c478bd9Sstevel@tonic-gate 		return (NULL);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	pt->p_symtab = mdb_gelf_symtab_create_file(pt->p_file,
1697c478bd9Sstevel@tonic-gate 	    SHT_SYMTAB, MDB_TGT_SYMTAB);
1707c478bd9Sstevel@tonic-gate 	pt->p_dynsym = mdb_gelf_symtab_create_file(pt->p_file,
1717c478bd9Sstevel@tonic-gate 	    SHT_DYNSYM, MDB_TGT_DYNSYM);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * If we've got an _start symbol with a zero size, prime the private
1757c478bd9Sstevel@tonic-gate 	 * symbol table with a copy of _start with its size set to the distance
1767c478bd9Sstevel@tonic-gate 	 * between _mcount and _start.  We do this because DevPro has shipped
1777c478bd9Sstevel@tonic-gate 	 * the Intel crt1.o without proper .size directives for years, which
1787c478bd9Sstevel@tonic-gate 	 * precludes proper identification of _start in stack traces.
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_start", &s1,
1817c478bd9Sstevel@tonic-gate 	    NULL) == 0 && s1.st_size == 0 &&
1827c478bd9Sstevel@tonic-gate 	    GELF_ST_TYPE(s1.st_info) == STT_FUNC) {
1837c478bd9Sstevel@tonic-gate 		if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_mcount",
1847c478bd9Sstevel@tonic-gate 		    &s2, NULL) == 0 && GELF_ST_TYPE(s2.st_info) == STT_FUNC) {
1857c478bd9Sstevel@tonic-gate 			s1.st_size = s2.st_value - s1.st_value;
1867c478bd9Sstevel@tonic-gate 			mdb_gelf_symtab_insert(mdb.m_prsym, "_start", &s1);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	pt->p_fio = io;
1917c478bd9Sstevel@tonic-gate 	return (pt->p_file);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Destroy the symbol tables and GElf file object associated with p_fio.  Note
1967c478bd9Sstevel@tonic-gate  * that we do not need to explicitly free p_fio: its reference count is
1977c478bd9Sstevel@tonic-gate  * automatically decremented by mdb_gelf_destroy, which will free it if needed.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate static void
pt_close_aout(mdb_tgt_t * t)2007c478bd9Sstevel@tonic-gate pt_close_aout(mdb_tgt_t *t)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (pt->p_symtab != NULL) {
2057c478bd9Sstevel@tonic-gate 		mdb_gelf_symtab_destroy(pt->p_symtab);
2067c478bd9Sstevel@tonic-gate 		pt->p_symtab = NULL;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (pt->p_dynsym != NULL) {
2107c478bd9Sstevel@tonic-gate 		mdb_gelf_symtab_destroy(pt->p_dynsym);
2117c478bd9Sstevel@tonic-gate 		pt->p_dynsym = NULL;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (pt->p_file != NULL) {
2157c478bd9Sstevel@tonic-gate 		mdb_gelf_destroy(pt->p_file);
2167c478bd9Sstevel@tonic-gate 		pt->p_file = NULL;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	mdb_gelf_symtab_delete(mdb.m_prsym, "_start", NULL);
2207c478bd9Sstevel@tonic-gate 	pt->p_fio = NULL;
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
22395d62a61SEdward Pilatowicz typedef struct tdb_mapping {
22495d62a61SEdward Pilatowicz 	const char *tm_thr_lib;
22595d62a61SEdward Pilatowicz 	const char *tm_db_dir;
22695d62a61SEdward Pilatowicz 	const char *tm_db_name;
22795d62a61SEdward Pilatowicz } tdb_mapping_t;
22895d62a61SEdward Pilatowicz 
22995d62a61SEdward Pilatowicz static const tdb_mapping_t tdb_map[] = {
23095d62a61SEdward Pilatowicz 	{ "/lwp/amd64/libthread.so",	"/usr/lib/lwp/", "libthread_db.so" },
23195d62a61SEdward Pilatowicz 	{ "/lwp/sparcv9/libthread.so",	"/usr/lib/lwp/", "libthread_db.so" },
23295d62a61SEdward Pilatowicz 	{ "/lwp/libthread.so",		"/usr/lib/lwp/", "libthread_db.so" },
23395d62a61SEdward Pilatowicz 	{ "/libthread.so",		"/lib/", "libthread_db.so" },
23495d62a61SEdward Pilatowicz 	{ "/libc_hwcap",		"/lib/", "libc_db.so" },
23595d62a61SEdward Pilatowicz 	{ "/libc.so",			"/lib/", "libc_db.so" }
23695d62a61SEdward Pilatowicz };
23795d62a61SEdward Pilatowicz 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Pobject_iter callback that we use to search for the presence of libthread in
2407c478bd9Sstevel@tonic-gate  * order to load the corresponding libthread_db support.  We derive the
2417c478bd9Sstevel@tonic-gate  * libthread_db path dynamically based on the libthread path.  If libthread is
2427c478bd9Sstevel@tonic-gate  * found, this function returns 1 (and thus Pobject_iter aborts and returns 1)
2437c478bd9Sstevel@tonic-gate  * regardless of whether it was successful in loading the libthread_db support.
2447c478bd9Sstevel@tonic-gate  * If we iterate over all objects and no libthread is found, 0 is returned.
2457c478bd9Sstevel@tonic-gate  * Since libthread_db support was then merged into libc_db, we load either
2467c478bd9Sstevel@tonic-gate  * libc_db or libthread_db, depending on which library we see first.
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2497c478bd9Sstevel@tonic-gate static int
thr_check(mdb_tgt_t * t,const prmap_t * pmp,const char * name)2507c478bd9Sstevel@tonic-gate thr_check(mdb_tgt_t *t, const prmap_t *pmp, const char *name)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
2537c478bd9Sstevel@tonic-gate 	const mdb_tdb_ops_t *ops;
25495d62a61SEdward Pilatowicz 	char *p;
2557c478bd9Sstevel@tonic-gate 
25695d62a61SEdward Pilatowicz 	char path[MAXPATHLEN];
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	int libn;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (name == NULL)
2617c478bd9Sstevel@tonic-gate 		return (0); /* no rtld_db object name; keep going */
2627c478bd9Sstevel@tonic-gate 
26395d62a61SEdward Pilatowicz 	for (libn = 0; libn < sizeof (tdb_map) / sizeof (tdb_map[0]); libn++) {
26495d62a61SEdward Pilatowicz 		if ((p = strstr(name, tdb_map[libn].tm_thr_lib)) != NULL)
2657c478bd9Sstevel@tonic-gate 			break;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (p == NULL)
2697c478bd9Sstevel@tonic-gate 		return (0); /* no match; keep going */
2707c478bd9Sstevel@tonic-gate 
27195d62a61SEdward Pilatowicz 	path[0] = '\0';
27295d62a61SEdward Pilatowicz 	(void) strlcat(path, mdb.m_root, sizeof (path));
27395d62a61SEdward Pilatowicz 	(void) strlcat(path, tdb_map[libn].tm_db_dir, sizeof (path));
27495d62a61SEdward Pilatowicz #if !defined(_ILP32)
27595d62a61SEdward Pilatowicz 	(void) strlcat(path, "64/", sizeof (path));
27695d62a61SEdward Pilatowicz #endif /* !_ILP32 */
27795d62a61SEdward Pilatowicz 	(void) strlcat(path, tdb_map[libn].tm_db_name, sizeof (path));
2787c478bd9Sstevel@tonic-gate 
27995d62a61SEdward Pilatowicz 	/* Append the trailing library version number. */
28095d62a61SEdward Pilatowicz 	(void) strlcat(path, strrchr(name, '.'), sizeof (path));
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if ((ops = mdb_tdb_load(path)) == NULL) {
2837c478bd9Sstevel@tonic-gate 		if (libn != 0 || errno != ENOENT)
2847c478bd9Sstevel@tonic-gate 			warn("failed to load %s", path);
2857c478bd9Sstevel@tonic-gate 		goto err;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (ops == pt->p_tdb_ops)
2897c478bd9Sstevel@tonic-gate 		return (1); /* no changes needed */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	PTL_DTOR(t);
2927c478bd9Sstevel@tonic-gate 	pt->p_tdb_ops = ops;
2937c478bd9Sstevel@tonic-gate 	pt->p_ptl_ops = &proc_tdb_ops;
2947c478bd9Sstevel@tonic-gate 	pt->p_ptl_hdl = NULL;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (PTL_CTOR(t) == -1) {
2977c478bd9Sstevel@tonic-gate 		warn("failed to initialize %s", path);
2987c478bd9Sstevel@tonic-gate 		goto err;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_TGT, "loaded %s for debugging %s\n", path, name);
3027c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
3037c478bd9Sstevel@tonic-gate 	return (1);
3047c478bd9Sstevel@tonic-gate err:
3057c478bd9Sstevel@tonic-gate 	PTL_DTOR(t);
3067c478bd9Sstevel@tonic-gate 	pt->p_tdb_ops = NULL;
3077c478bd9Sstevel@tonic-gate 	pt->p_ptl_ops = &proc_lwp_ops;
3087c478bd9Sstevel@tonic-gate 	pt->p_ptl_hdl = NULL;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if (libn != 0 || errno != ENOENT) {
3117c478bd9Sstevel@tonic-gate 		warn("warning: debugger will only be able to "
3127c478bd9Sstevel@tonic-gate 		    "examine raw LWPs\n");
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
3167c478bd9Sstevel@tonic-gate 	return (1);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * Whenever the link map is consistent following an add or delete event, we ask
3217c478bd9Sstevel@tonic-gate  * libproc to update its mappings, check to see if we need to load libthread_db,
3227c478bd9Sstevel@tonic-gate  * and then update breakpoints which have been mapped or unmapped.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3257c478bd9Sstevel@tonic-gate static void
pt_rtld_event(mdb_tgt_t * t,int vid,void * private)3267c478bd9Sstevel@tonic-gate pt_rtld_event(mdb_tgt_t *t, int vid, void *private)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
3297c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
3307c478bd9Sstevel@tonic-gate 	rd_event_msg_t rdm;
33195d62a61SEdward Pilatowicz 	int docontinue = 1;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (rd_event_getmsg(pt->p_rtld, &rdm) == RD_OK) {
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_TGT, "rtld event type 0x%x state 0x%x\n",
3367c478bd9Sstevel@tonic-gate 		    rdm.type, rdm.u.state);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_CONSISTENT) {
3397c478bd9Sstevel@tonic-gate 			mdb_sespec_t *sep, *nsep = mdb_list_next(&t->t_active);
3407c478bd9Sstevel@tonic-gate 			pt_brkpt_t *ptb;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			Pupdate_maps(P);
3437c478bd9Sstevel@tonic-gate 
34495d62a61SEdward Pilatowicz 			if (Pobject_iter(P, (proc_map_f *)thr_check, t) == 0 &&
34595d62a61SEdward Pilatowicz 			    pt->p_ptl_ops != &proc_lwp_ops) {
3467c478bd9Sstevel@tonic-gate 				mdb_dprintf(MDB_DBG_TGT, "unloading thread_db "
3477c478bd9Sstevel@tonic-gate 				    "support after dlclose\n");
3487c478bd9Sstevel@tonic-gate 				PTL_DTOR(t);
3497c478bd9Sstevel@tonic-gate 				pt->p_tdb_ops = NULL;
3507c478bd9Sstevel@tonic-gate 				pt->p_ptl_ops = &proc_lwp_ops;
3517c478bd9Sstevel@tonic-gate 				pt->p_ptl_hdl = NULL;
3527c478bd9Sstevel@tonic-gate 				(void) mdb_tgt_status(t, &t->t_status);
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 			for (sep = nsep; sep != NULL; sep = nsep) {
3567c478bd9Sstevel@tonic-gate 				nsep = mdb_list_next(sep);
3577c478bd9Sstevel@tonic-gate 				ptb = sep->se_data;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 				if (sep->se_ops == &proc_brkpt_ops &&
3607c478bd9Sstevel@tonic-gate 				    Paddr_to_map(P, ptb->ptb_addr) == NULL)
3617c478bd9Sstevel@tonic-gate 					mdb_tgt_sespec_idle_one(t, sep,
3627c478bd9Sstevel@tonic-gate 					    EMDB_NOMAP);
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			if (!mdb_tgt_sespec_activate_all(t) &&
3667c478bd9Sstevel@tonic-gate 			    (mdb.m_flags & MDB_FL_BPTNOSYMSTOP) &&
3677c478bd9Sstevel@tonic-gate 			    pt->p_rtld_finished) {
3687c478bd9Sstevel@tonic-gate 				/*
3697c478bd9Sstevel@tonic-gate 				 * We weren't able to activate the breakpoints.
3707c478bd9Sstevel@tonic-gate 				 * If so requested, we'll return without
3717c478bd9Sstevel@tonic-gate 				 * calling continue, thus throwing the user into
3727c478bd9Sstevel@tonic-gate 				 * the debugger.
3737c478bd9Sstevel@tonic-gate 				 */
3747c478bd9Sstevel@tonic-gate 				docontinue = 0;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 			if (pt->p_rdstate == PT_RD_ADD)
3787c478bd9Sstevel@tonic-gate 				pt->p_rdstate = PT_RD_CONSIST;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		if (rdm.type == RD_PREINIT)
3827c478bd9Sstevel@tonic-gate 			(void) mdb_tgt_sespec_activate_all(t);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		if (rdm.type == RD_POSTINIT) {
3857c478bd9Sstevel@tonic-gate 			pt->p_rtld_finished = TRUE;
3867c478bd9Sstevel@tonic-gate 			if (!mdb_tgt_sespec_activate_all(t) &&
3877c478bd9Sstevel@tonic-gate 			    (mdb.m_flags & MDB_FL_BPTNOSYMSTOP)) {
3887c478bd9Sstevel@tonic-gate 				/*
3897c478bd9Sstevel@tonic-gate 				 * Now that rtld has been initialized, we
3907c478bd9Sstevel@tonic-gate 				 * should be able to initialize all deferred
3917c478bd9Sstevel@tonic-gate 				 * breakpoints.  If we can't, don't let the
3927c478bd9Sstevel@tonic-gate 				 * target continue.
3937c478bd9Sstevel@tonic-gate 				 */
3947c478bd9Sstevel@tonic-gate 				docontinue = 0;
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_ADD &&
3997c478bd9Sstevel@tonic-gate 		    pt->p_rtld_finished)
4007c478bd9Sstevel@tonic-gate 			pt->p_rdstate = MAX(pt->p_rdstate, PT_RD_ADD);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (docontinue)
4047c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_continue(t, NULL);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static void
pt_post_attach(mdb_tgt_t * t)4087c478bd9Sstevel@tonic-gate pt_post_attach(mdb_tgt_t *t)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
4117c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
4127c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
4137c478bd9Sstevel@tonic-gate 	int hflag = MDB_TGT_SPEC_HIDDEN;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_TGT, "attach pr_flags=0x%x pr_why=%d pr_what=%d\n",
4167c478bd9Sstevel@tonic-gate 	    psp->pr_flags, psp->pr_why, psp->pr_what);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * When we grab a process, the initial setting of p_rtld_finished
4207c478bd9Sstevel@tonic-gate 	 * should be false if the process was just created by exec; otherwise
4217c478bd9Sstevel@tonic-gate 	 * we permit unscoped references to resolve because we do not know how
4227c478bd9Sstevel@tonic-gate 	 * far the process has proceeded through linker initialization.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	if ((psp->pr_flags & PR_ISTOP) && psp->pr_why == PR_SYSEXIT &&
4258fd04b83SRoger A. Faulkner 	    psp->pr_errno == 0 && psp->pr_what == SYS_execve) {
4267c478bd9Sstevel@tonic-gate 		if (mdb.m_target == NULL) {
4277c478bd9Sstevel@tonic-gate 			warn("target performed exec of %s\n",
4287c478bd9Sstevel@tonic-gate 			    IOP_NAME(pt->p_fio));
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		pt->p_rtld_finished = FALSE;
4317c478bd9Sstevel@tonic-gate 	} else
4327c478bd9Sstevel@tonic-gate 		pt->p_rtld_finished = TRUE;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * When we grab a process, if it is stopped by job control and part of
4367c478bd9Sstevel@tonic-gate 	 * the same session (i.e. same controlling tty), set MDB_FL_JOBCTL so
4377c478bd9Sstevel@tonic-gate 	 * we will know to bring it to the foreground when we continue it.
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	if (mdb.m_term != NULL && (psp->pr_flags & PR_STOPPED) &&
4407c478bd9Sstevel@tonic-gate 	    psp->pr_why == PR_JOBCONTROL && getsid(0) == Pstatus(P)->pr_sid)
4417c478bd9Sstevel@tonic-gate 		mdb.m_flags |= MDB_FL_JOBCTL;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * When we grab control of a live process, set F_RDWR so that the
4457c478bd9Sstevel@tonic-gate 	 * target layer permits writes to the target's address space.
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	t->t_flags |= MDB_TGT_F_RDWR;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	(void) Pfault(P, FLTBPT, TRUE);		/* always trace breakpoints */
4507c478bd9Sstevel@tonic-gate 	(void) Pfault(P, FLTWATCH, TRUE);	/* always trace watchpoints */
4517c478bd9Sstevel@tonic-gate 	(void) Pfault(P, FLTTRACE, TRUE);	/* always trace single-step */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	(void) Punsetflags(P, PR_ASYNC);	/* require synchronous mode */
4547c478bd9Sstevel@tonic-gate 	(void) Psetflags(P, PR_BPTADJ);		/* always adjust eip on x86 */
4557c478bd9Sstevel@tonic-gate 	(void) Psetflags(P, PR_FORK);		/* inherit tracing on fork */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Install event specifiers to track fork and exec activities:
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_sysexit(t, SYS_vfork, hflag, pt_fork, NULL);
461657b1f3dSraf 	(void) mdb_tgt_add_sysexit(t, SYS_forksys, hflag, pt_fork, NULL);
4627c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_sysexit(t, SYS_execve, hflag, pt_exec, NULL);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Attempt to instantiate the librtld_db agent and set breakpoints
4667c478bd9Sstevel@tonic-gate 	 * to track rtld activity.  We will legitimately fail to instantiate
4677c478bd9Sstevel@tonic-gate 	 * the rtld_db agent if the target is statically linked.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	if (pt->p_rtld == NULL && (pt->p_rtld = Prd_agent(P)) != NULL) {
4707c478bd9Sstevel@tonic-gate 		rd_notify_t rdn;
4717c478bd9Sstevel@tonic-gate 		rd_err_e err;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		if ((err = rd_event_enable(pt->p_rtld, TRUE)) != RD_OK) {
4747c478bd9Sstevel@tonic-gate 			warn("failed to enable rtld_db event tracing: %s\n",
4757c478bd9Sstevel@tonic-gate 			    rd_errstr(err));
4767c478bd9Sstevel@tonic-gate 			goto out;
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		if ((err = rd_event_addr(pt->p_rtld, RD_PREINIT,
4807c478bd9Sstevel@tonic-gate 		    &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
4817c478bd9Sstevel@tonic-gate 			(void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
4827c478bd9Sstevel@tonic-gate 			    hflag, pt_rtld_event, NULL);
4837c478bd9Sstevel@tonic-gate 		} else {
4847c478bd9Sstevel@tonic-gate 			warn("failed to install rtld_db preinit tracing: %s\n",
4857c478bd9Sstevel@tonic-gate 			    rd_errstr(err));
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		if ((err = rd_event_addr(pt->p_rtld, RD_POSTINIT,
4897c478bd9Sstevel@tonic-gate 		    &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
4907c478bd9Sstevel@tonic-gate 			(void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
4917c478bd9Sstevel@tonic-gate 			    hflag, pt_rtld_event, NULL);
4927c478bd9Sstevel@tonic-gate 		} else {
4937c478bd9Sstevel@tonic-gate 			warn("failed to install rtld_db postinit tracing: %s\n",
4947c478bd9Sstevel@tonic-gate 			    rd_errstr(err));
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		if ((err = rd_event_addr(pt->p_rtld, RD_DLACTIVITY,
4987c478bd9Sstevel@tonic-gate 		    &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
4997c478bd9Sstevel@tonic-gate 			(void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
5007c478bd9Sstevel@tonic-gate 			    hflag, pt_rtld_event, NULL);
5017c478bd9Sstevel@tonic-gate 		} else {
5027c478bd9Sstevel@tonic-gate 			warn("failed to install rtld_db activity tracing: %s\n",
5037c478bd9Sstevel@tonic-gate 			    rd_errstr(err));
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate out:
5077c478bd9Sstevel@tonic-gate 	Pupdate_maps(P);
5087c478bd9Sstevel@tonic-gate 	Psync(P);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * If librtld_db failed to initialize due to an error or because we are
5127c478bd9Sstevel@tonic-gate 	 * debugging a statically linked executable, allow unscoped references.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	if (pt->p_rtld == NULL)
5157c478bd9Sstevel@tonic-gate 		pt->p_rtld_finished = TRUE;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_sespec_activate_all(t);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5217c478bd9Sstevel@tonic-gate static int
pt_vespec_delete(mdb_tgt_t * t,void * private,int id,void * data)5227c478bd9Sstevel@tonic-gate pt_vespec_delete(mdb_tgt_t *t, void *private, int id, void *data)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	if (id < 0) {
5257c478bd9Sstevel@tonic-gate 		ASSERT(data == NULL); /* we don't use any ve_data */
5267c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_vespec_delete(t, id);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	return (0);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static void
pt_pre_detach(mdb_tgt_t * t,int clear_matched)5327c478bd9Sstevel@tonic-gate pt_pre_detach(mdb_tgt_t *t, int clear_matched)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
5357c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
5367c478bd9Sstevel@tonic-gate 	long cmd = 0;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * If we are about to release the process and it is stopped on a traced
5407c478bd9Sstevel@tonic-gate 	 * SIGINT, breakpoint fault, single-step fault, or watchpoint, make
5417c478bd9Sstevel@tonic-gate 	 * sure to clear this event prior to releasing the process so that it
5427c478bd9Sstevel@tonic-gate 	 * does not subsequently reissue the fault and die from SIGTRAP.
5437c478bd9Sstevel@tonic-gate 	 */
5447c478bd9Sstevel@tonic-gate 	if (psp->pr_flags & PR_ISTOP) {
5457c478bd9Sstevel@tonic-gate 		if (psp->pr_why == PR_FAULTED && (psp->pr_what == FLTBPT ||
5467c478bd9Sstevel@tonic-gate 		    psp->pr_what == FLTTRACE || psp->pr_what == FLTWATCH))
5477c478bd9Sstevel@tonic-gate 			cmd = PCCFAULT;
5487c478bd9Sstevel@tonic-gate 		else if (psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
5497c478bd9Sstevel@tonic-gate 			cmd = PCCSIG;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		if (cmd != 0)
5527c478bd9Sstevel@tonic-gate 			(void) write(Pctlfd(t->t_pshandle), &cmd, sizeof (cmd));
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (Pstate(t->t_pshandle) == PS_UNDEAD)
5567c478bd9Sstevel@tonic-gate 		(void) waitpid(Pstatus(t->t_pshandle)->pr_pid, NULL, WNOHANG);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_vespec_iter(t, pt_vespec_delete, NULL);
5597c478bd9Sstevel@tonic-gate 	mdb_tgt_sespec_idle_all(t, EMDB_NOPROC, clear_matched);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (pt->p_fio != pt->p_aout_fio) {
5627c478bd9Sstevel@tonic-gate 		pt_close_aout(t);
5637c478bd9Sstevel@tonic-gate 		(void) pt_open_aout(t, pt->p_aout_fio);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	PTL_DTOR(t);
5677c478bd9Sstevel@tonic-gate 	pt->p_tdb_ops = NULL;
5687c478bd9Sstevel@tonic-gate 	pt->p_ptl_ops = &proc_lwp_ops;
5697c478bd9Sstevel@tonic-gate 	pt->p_ptl_hdl = NULL;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	pt->p_rtld = NULL;
5727c478bd9Sstevel@tonic-gate 	pt->p_signal = 0;
5737c478bd9Sstevel@tonic-gate 	pt->p_rtld_finished = FALSE;
5747c478bd9Sstevel@tonic-gate 	pt->p_rdstate = PT_RD_NONE;
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate static void
pt_release_parents(mdb_tgt_t * t)5787c478bd9Sstevel@tonic-gate pt_release_parents(mdb_tgt_t *t)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
5817c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	mdb_sespec_t *sep;
5847c478bd9Sstevel@tonic-gate 	pt_vforkp_t *vfp;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	while ((vfp = mdb_list_next(&pt->p_vforkp)) != NULL) {
5877c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_TGT, "releasing vfork parent %d\n",
5887c478bd9Sstevel@tonic-gate 		    (int)Pstatus(vfp->p_pshandle)->pr_pid);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		/*
5917c478bd9Sstevel@tonic-gate 		 * To release vfork parents, we must also wipe out any armed
5927c478bd9Sstevel@tonic-gate 		 * events in the parent by switching t_pshandle and calling
5937c478bd9Sstevel@tonic-gate 		 * se_disarm().  Do not change states or lose the matched list.
5947c478bd9Sstevel@tonic-gate 		 */
5957c478bd9Sstevel@tonic-gate 		t->t_pshandle = vfp->p_pshandle;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		for (sep = mdb_list_next(&t->t_active); sep != NULL;
5987c478bd9Sstevel@tonic-gate 		    sep = mdb_list_next(sep)) {
5997c478bd9Sstevel@tonic-gate 			if (sep->se_state == MDB_TGT_SPEC_ARMED)
6007c478bd9Sstevel@tonic-gate 				(void) sep->se_ops->se_disarm(t, sep);
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		t->t_pshandle = P;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		Prelease(vfp->p_pshandle, PRELEASE_CLEAR);
6067c478bd9Sstevel@tonic-gate 		mdb_list_delete(&pt->p_vforkp, vfp);
6077c478bd9Sstevel@tonic-gate 		mdb_free(vfp, sizeof (pt_vforkp_t));
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6127c478bd9Sstevel@tonic-gate static void
pt_fork(mdb_tgt_t * t,int vid,void * private)6137c478bd9Sstevel@tonic-gate pt_fork(mdb_tgt_t *t, int vid, void *private)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
6167c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
6177c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
6187c478bd9Sstevel@tonic-gate 	mdb_sespec_t *sep;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	int follow_parent = mdb.m_forkmode != MDB_FM_CHILD;
621657b1f3dSraf 	int is_vfork = (psp->pr_what == SYS_vfork ||
622657b1f3dSraf 	    (psp->pr_what == SYS_forksys && psp->pr_sysarg[0] == 2));
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	struct ps_prochandle *C;
6257c478bd9Sstevel@tonic-gate 	const lwpstatus_t *csp;
6267c478bd9Sstevel@tonic-gate 	char sysname[32];
6277c478bd9Sstevel@tonic-gate 	int gcode;
6287c478bd9Sstevel@tonic-gate 	char c;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_TGT, "parent %s: errno=%d rv1=%ld rv2=%ld\n",
6317c478bd9Sstevel@tonic-gate 	    proc_sysname(psp->pr_what, sysname, sizeof (sysname)),
6327c478bd9Sstevel@tonic-gate 	    psp->pr_errno, psp->pr_rval1, psp->pr_rval2);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (psp->pr_errno != 0) {
6357c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_continue(t, NULL);
6367c478bd9Sstevel@tonic-gate 		return; /* fork failed */
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	/*
6407c478bd9Sstevel@tonic-gate 	 * If forkmode is ASK and stdout is a terminal, then ask the user to
6417c478bd9Sstevel@tonic-gate 	 * explicitly set the fork behavior for this particular fork.
6427c478bd9Sstevel@tonic-gate 	 */
6437c478bd9Sstevel@tonic-gate 	if (mdb.m_forkmode == MDB_FM_ASK && mdb.m_term != NULL) {
6447c478bd9Sstevel@tonic-gate 		mdb_iob_printf(mdb.m_err, "%s: %s detected: follow (p)arent "
6457c478bd9Sstevel@tonic-gate 		    "or (c)hild? ", mdb.m_pname, sysname);
6467c478bd9Sstevel@tonic-gate 		mdb_iob_flush(mdb.m_err);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
6497c478bd9Sstevel@tonic-gate 			if (c == 'P' || c == 'p') {
6507c478bd9Sstevel@tonic-gate 				mdb_iob_printf(mdb.m_err, "%c\n", c);
6517c478bd9Sstevel@tonic-gate 				follow_parent = TRUE;
6527c478bd9Sstevel@tonic-gate 				break;
6537c478bd9Sstevel@tonic-gate 			} else if (c == 'C' || c == 'c') {
6547c478bd9Sstevel@tonic-gate 				mdb_iob_printf(mdb.m_err, "%c\n", c);
6557c478bd9Sstevel@tonic-gate 				follow_parent = FALSE;
6567c478bd9Sstevel@tonic-gate 				break;
6577c478bd9Sstevel@tonic-gate 			}
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/*
6627c478bd9Sstevel@tonic-gate 	 * The parent is now stopped on exit from its fork call.  We must now
6637c478bd9Sstevel@tonic-gate 	 * grab the child on its return from fork in order to manipulate it.
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 	if ((C = Pgrab(psp->pr_rval1, PGRAB_RETAIN, &gcode)) == NULL) {
6667c478bd9Sstevel@tonic-gate 		warn("failed to grab forked child process %ld: %s\n",
6677c478bd9Sstevel@tonic-gate 		    psp->pr_rval1, Pgrab_error(gcode));
6687c478bd9Sstevel@tonic-gate 		return; /* just stop if we failed to grab the child */
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/*
6727c478bd9Sstevel@tonic-gate 	 * We may have grabbed the child and stopped it prematurely before it
6737c478bd9Sstevel@tonic-gate 	 * stopped on exit from fork.  If so, wait up to 1 sec for it to settle.
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	if (Pstatus(C)->pr_lwp.pr_why != PR_SYSEXIT)
6767c478bd9Sstevel@tonic-gate 		(void) Pwait(C, MILLISEC);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	csp = &Pstatus(C)->pr_lwp;
6797c478bd9Sstevel@tonic-gate 
680657b1f3dSraf 	if (csp->pr_why != PR_SYSEXIT ||
6818fd04b83SRoger A. Faulkner 	    (csp->pr_what != SYS_vfork && csp->pr_what != SYS_forksys)) {
6827c478bd9Sstevel@tonic-gate 		warn("forked child process %ld did not stop on exit from "
6837c478bd9Sstevel@tonic-gate 		    "fork as expected\n", psp->pr_rval1);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	warn("target forked child process %ld (debugger following %s)\n",
6877c478bd9Sstevel@tonic-gate 	    psp->pr_rval1, follow_parent ? "parent" : "child");
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	(void) Punsetflags(C, PR_ASYNC);	/* require synchronous mode */
6907c478bd9Sstevel@tonic-gate 	(void) Psetflags(C, PR_BPTADJ);		/* always adjust eip on x86 */
6917c478bd9Sstevel@tonic-gate 	(void) Prd_agent(C);			/* initialize librtld_db */
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * At the time pt_fork() is called, the target event engine has already
6957c478bd9Sstevel@tonic-gate 	 * disarmed the specifiers on the active list, clearing out events in
6967c478bd9Sstevel@tonic-gate 	 * the parent process.  However, this means that events that change
6977c478bd9Sstevel@tonic-gate 	 * the address space (e.g. breakpoints) have not been effectively
6987c478bd9Sstevel@tonic-gate 	 * disarmed in the child since its address space reflects the state of
6997c478bd9Sstevel@tonic-gate 	 * the process at the time of fork when events were armed.  We must
7007c478bd9Sstevel@tonic-gate 	 * therefore handle this as a special case and re-invoke the disarm
7017c478bd9Sstevel@tonic-gate 	 * callback of each active specifier to clean out the child process.
7027c478bd9Sstevel@tonic-gate 	 */
7037c478bd9Sstevel@tonic-gate 	if (!is_vfork) {
7047c478bd9Sstevel@tonic-gate 		for (t->t_pshandle = C, sep = mdb_list_next(&t->t_active);
7057c478bd9Sstevel@tonic-gate 		    sep != NULL; sep = mdb_list_next(sep)) {
7067c478bd9Sstevel@tonic-gate 			if (sep->se_state == MDB_TGT_SPEC_ACTIVE)
7077c478bd9Sstevel@tonic-gate 				(void) sep->se_ops->se_disarm(t, sep);
7087c478bd9Sstevel@tonic-gate 		}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		t->t_pshandle = P; /* restore pshandle to parent */
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/*
7147c478bd9Sstevel@tonic-gate 	 * If we're following the parent process, we need to temporarily change
7157c478bd9Sstevel@tonic-gate 	 * t_pshandle to refer to the child handle C so that we can clear out
7167c478bd9Sstevel@tonic-gate 	 * all the events in the child prior to releasing it below.  If we are
7177c478bd9Sstevel@tonic-gate 	 * tracing a vfork, we also need to explicitly wait for the child to
7187c478bd9Sstevel@tonic-gate 	 * exec, exit, or die before we can reset and continue the parent.  We
7197c478bd9Sstevel@tonic-gate 	 * avoid having to deal with the vfork child forking again by clearing
7207c478bd9Sstevel@tonic-gate 	 * PR_FORK and setting PR_RLC; if it does fork it will effectively be
7217c478bd9Sstevel@tonic-gate 	 * released from our control and we will continue following the parent.
7227c478bd9Sstevel@tonic-gate 	 */
7237c478bd9Sstevel@tonic-gate 	if (follow_parent) {
7247c478bd9Sstevel@tonic-gate 		if (is_vfork) {
7257c478bd9Sstevel@tonic-gate 			mdb_tgt_status_t status;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 			ASSERT(psp->pr_flags & PR_VFORKP);
7287c478bd9Sstevel@tonic-gate 			mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
7297c478bd9Sstevel@tonic-gate 			t->t_pshandle = C;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 			(void) Psysexit(C, SYS_execve, TRUE);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 			(void) Punsetflags(C, PR_FORK | PR_KLC);
7347c478bd9Sstevel@tonic-gate 			(void) Psetflags(C, PR_RLC);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 			do {
7377c478bd9Sstevel@tonic-gate 				if (pt_setrun(t, &status, 0) == -1 ||
7387c478bd9Sstevel@tonic-gate 				    status.st_state == MDB_TGT_UNDEAD ||
7397c478bd9Sstevel@tonic-gate 				    status.st_state == MDB_TGT_LOST)
7407c478bd9Sstevel@tonic-gate 					break; /* failure or process died */
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 			} while (csp->pr_why != PR_SYSEXIT ||
7438fd04b83SRoger A. Faulkner 			    csp->pr_errno != 0 || csp->pr_what != SYS_execve);
7447c478bd9Sstevel@tonic-gate 		} else
7457c478bd9Sstevel@tonic-gate 			t->t_pshandle = C;
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * If we are following the child, destroy any active libthread_db
7507c478bd9Sstevel@tonic-gate 	 * handle before we release the parent process.
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 	if (!follow_parent) {
7537c478bd9Sstevel@tonic-gate 		PTL_DTOR(t);
7547c478bd9Sstevel@tonic-gate 		pt->p_tdb_ops = NULL;
7557c478bd9Sstevel@tonic-gate 		pt->p_ptl_ops = &proc_lwp_ops;
7567c478bd9Sstevel@tonic-gate 		pt->p_ptl_hdl = NULL;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Idle all events to make sure the address space and tracing flags are
7617c478bd9Sstevel@tonic-gate 	 * restored, and then release the process we are not tracing.  If we
7627c478bd9Sstevel@tonic-gate 	 * are following the child of a vfork, we push the parent's pshandle
7637c478bd9Sstevel@tonic-gate 	 * on to a list of vfork parents to be released when we exec or exit.
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	if (is_vfork && !follow_parent) {
7667c478bd9Sstevel@tonic-gate 		pt_vforkp_t *vfp = mdb_alloc(sizeof (pt_vforkp_t), UM_SLEEP);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		ASSERT(psp->pr_flags & PR_VFORKP);
7697c478bd9Sstevel@tonic-gate 		vfp->p_pshandle = P;
7707c478bd9Sstevel@tonic-gate 		mdb_list_append(&pt->p_vforkp, vfp);
7717c478bd9Sstevel@tonic-gate 		mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	} else {
7747c478bd9Sstevel@tonic-gate 		mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
7757c478bd9Sstevel@tonic-gate 		Prelease(t->t_pshandle, PRELEASE_CLEAR);
7767c478bd9Sstevel@tonic-gate 		if (!follow_parent)
7777c478bd9Sstevel@tonic-gate 			pt_release_parents(t);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * Now that all the hard stuff is done, switch t_pshandle back to the
7827c478bd9Sstevel@tonic-gate 	 * process we are following and reset our events to the ACTIVE state.
7837c478bd9Sstevel@tonic-gate 	 * If we are following the child, reset the libthread_db handle as well
7847c478bd9Sstevel@tonic-gate 	 * as the rtld agent.
7857c478bd9Sstevel@tonic-gate 	 */
7867c478bd9Sstevel@tonic-gate 	if (follow_parent)
7877c478bd9Sstevel@tonic-gate 		t->t_pshandle = P;
7887c478bd9Sstevel@tonic-gate 	else {
7897c478bd9Sstevel@tonic-gate 		t->t_pshandle = C;
7907c478bd9Sstevel@tonic-gate 		pt->p_rtld = Prd_agent(C);
79195d62a61SEdward Pilatowicz 		(void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_sespec_activate_all(t);
7957c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_continue(t, NULL);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7997c478bd9Sstevel@tonic-gate static void
pt_exec(mdb_tgt_t * t,int vid,void * private)8007c478bd9Sstevel@tonic-gate pt_exec(mdb_tgt_t *t, int vid, void *private)
8017c478bd9Sstevel@tonic-gate {
8027c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
8037c478bd9Sstevel@tonic-gate 	const pstatus_t *psp = Pstatus(P);
8047c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
8057c478bd9Sstevel@tonic-gate 	int follow_exec = mdb.m_execmode == MDB_EM_FOLLOW;
8067c478bd9Sstevel@tonic-gate 	pid_t pid = psp->pr_pid;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	char execname[MAXPATHLEN];
8097c478bd9Sstevel@tonic-gate 	mdb_sespec_t *sep, *nsep;
8107c478bd9Sstevel@tonic-gate 	mdb_io_t *io;
8117c478bd9Sstevel@tonic-gate 	char c;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_TGT, "exit from %s: errno=%d\n", proc_sysname(
8147c478bd9Sstevel@tonic-gate 	    psp->pr_lwp.pr_what, execname, sizeof (execname)),
8157c478bd9Sstevel@tonic-gate 	    psp->pr_lwp.pr_errno);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (psp->pr_lwp.pr_errno != 0) {
8187c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_continue(t, NULL);
8197c478bd9Sstevel@tonic-gate 		return; /* exec failed */
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/*
8237c478bd9Sstevel@tonic-gate 	 * If execmode is ASK and stdout is a terminal, then ask the user to
8247c478bd9Sstevel@tonic-gate 	 * explicitly set the exec behavior for this particular exec.  If
8257c478bd9Sstevel@tonic-gate 	 * Pstate() still shows PS_LOST, we are being called from pt_setrun()
8267c478bd9Sstevel@tonic-gate 	 * directly and therefore we must resume the terminal since it is still
8277c478bd9Sstevel@tonic-gate 	 * in the suspended state as far as tgt_continue() is concerned.
8287c478bd9Sstevel@tonic-gate 	 */
8297c478bd9Sstevel@tonic-gate 	if (mdb.m_execmode == MDB_EM_ASK && mdb.m_term != NULL) {
8307c478bd9Sstevel@tonic-gate 		if (Pstate(P) == PS_LOST)
8317c478bd9Sstevel@tonic-gate 			IOP_RESUME(mdb.m_term);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		mdb_iob_printf(mdb.m_err, "%s: %s detected: (f)ollow new "
8347c478bd9Sstevel@tonic-gate 		    "program or (s)top? ", mdb.m_pname, execname);
8357c478bd9Sstevel@tonic-gate 		mdb_iob_flush(mdb.m_err);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
8387c478bd9Sstevel@tonic-gate 			if (c == 'F' || c == 'f') {
8397c478bd9Sstevel@tonic-gate 				mdb_iob_printf(mdb.m_err, "%c\n", c);
8407c478bd9Sstevel@tonic-gate 				follow_exec = TRUE;
8417c478bd9Sstevel@tonic-gate 				break;
8427c478bd9Sstevel@tonic-gate 			} else if (c == 'S' || c == 's') {
8437c478bd9Sstevel@tonic-gate 				mdb_iob_printf(mdb.m_err, "%c\n", c);
8447c478bd9Sstevel@tonic-gate 				follow_exec = FALSE;
8457c478bd9Sstevel@tonic-gate 				break;
8467c478bd9Sstevel@tonic-gate 			}
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		if (Pstate(P) == PS_LOST)
8507c478bd9Sstevel@tonic-gate 			IOP_SUSPEND(mdb.m_term);
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	pt_release_parents(t);	/* release any waiting vfork parents */
8547c478bd9Sstevel@tonic-gate 	pt_pre_detach(t, FALSE); /* remove our breakpoints and idle events */
8557c478bd9Sstevel@tonic-gate 	Preset_maps(P);		/* libproc must delete mappings and symtabs */
8567c478bd9Sstevel@tonic-gate 	pt_close_aout(t);	/* free pt symbol tables and GElf file data */
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * If we lost control of the process across the exec and are not able
8607c478bd9Sstevel@tonic-gate 	 * to reopen it, we have no choice but to clear the matched event list
8617c478bd9Sstevel@tonic-gate 	 * and wait for the user to quit or otherwise release the process.
8627c478bd9Sstevel@tonic-gate 	 */
8637c478bd9Sstevel@tonic-gate 	if (Pstate(P) == PS_LOST && Preopen(P) == -1) {
8647c478bd9Sstevel@tonic-gate 		int error = errno;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		warn("lost control of PID %d due to exec of %s executable\n",
8677c478bd9Sstevel@tonic-gate 		    (int)pid, error == EOVERFLOW ? "64-bit" : "set-id");
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		for (sep = t->t_matched; sep != T_SE_END; sep = nsep) {
8707c478bd9Sstevel@tonic-gate 			nsep = sep->se_matched;
8717c478bd9Sstevel@tonic-gate 			sep->se_matched = NULL;
8727c478bd9Sstevel@tonic-gate 			mdb_tgt_sespec_rele(t, sep);
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		if (error != EOVERFLOW)
8767c478bd9Sstevel@tonic-gate 			return; /* just stop if we exec'd a set-id executable */
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if (Pstate(P) != PS_LOST) {
8807c478bd9Sstevel@tonic-gate 		if (Pexecname(P, execname, sizeof (execname)) == NULL) {
8817c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(execname, sizeof (execname),
8827c478bd9Sstevel@tonic-gate 			    "/proc/%d/object/a.out", (int)pid);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		if (follow_exec == FALSE || psp->pr_dmodel == PR_MODEL_NATIVE)
8867c478bd9Sstevel@tonic-gate 			warn("target performed exec of %s\n", execname);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		io = mdb_fdio_create_path(NULL, execname, pt->p_oflags, 0);
8897c478bd9Sstevel@tonic-gate 		if (io == NULL) {
8907c478bd9Sstevel@tonic-gate 			warn("failed to open %s", execname);
8917c478bd9Sstevel@tonic-gate 			warn("a.out symbol tables will not be available\n");
8927c478bd9Sstevel@tonic-gate 		} else if (pt_open_aout(t, io) == NULL) {
8937c478bd9Sstevel@tonic-gate 			(void) mdb_dis_select(pt_disasm(NULL));
8947c478bd9Sstevel@tonic-gate 			mdb_io_destroy(io);
8957c478bd9Sstevel@tonic-gate 		} else
8967c478bd9Sstevel@tonic-gate 			(void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * We reset our libthread_db state here, but deliberately do NOT call
9017c478bd9Sstevel@tonic-gate 	 * PTL_DTOR because we do not want to call libthread_db's td_ta_delete.
9027c478bd9Sstevel@tonic-gate 	 * This interface is hopelessly broken in that it writes to the process
9037c478bd9Sstevel@tonic-gate 	 * address space (which we do not want it to do after an exec) and it
9047c478bd9Sstevel@tonic-gate 	 * doesn't bother deallocating any of its storage anyway.
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 	pt->p_tdb_ops = NULL;
9077c478bd9Sstevel@tonic-gate 	pt->p_ptl_ops = &proc_lwp_ops;
9087c478bd9Sstevel@tonic-gate 	pt->p_ptl_hdl = NULL;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if (follow_exec && psp->pr_dmodel != PR_MODEL_NATIVE) {
9117c478bd9Sstevel@tonic-gate 		const char *argv[3];
9127c478bd9Sstevel@tonic-gate 		char *state, *env;
9137c478bd9Sstevel@tonic-gate 		char pidarg[16];
9147c478bd9Sstevel@tonic-gate 		size_t envlen;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		if (realpath(getexecname(), execname) == NULL) {
9177c478bd9Sstevel@tonic-gate 			warn("cannot follow PID %d -- failed to resolve "
9187c478bd9Sstevel@tonic-gate 			    "debugger pathname for re-exec", (int)pid);
9197c478bd9Sstevel@tonic-gate 			return;
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		warn("restarting debugger to follow PID %d ...\n", (int)pid);
9237c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_TGT, "re-exec'ing %s\n", execname);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(pidarg, sizeof (pidarg), "-p%d", (int)pid);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		state = mdb_get_config();
9287c478bd9Sstevel@tonic-gate 		envlen = strlen(MDB_CONFIG_ENV_VAR) + 1 + strlen(state) + 1;
9297c478bd9Sstevel@tonic-gate 		env = mdb_alloc(envlen, UM_SLEEP);
93080148899SSurya Prakki 		(void) snprintf(env, envlen,
93180148899SSurya Prakki 		    "%s=%s", MDB_CONFIG_ENV_VAR, state);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		(void) putenv(env);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		argv[0] = mdb.m_pname;
9367c478bd9Sstevel@tonic-gate 		argv[1] = pidarg;
9377c478bd9Sstevel@tonic-gate 		argv[2] = NULL;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		if (mdb.m_term != NULL)
9407c478bd9Sstevel@tonic-gate 			IOP_SUSPEND(mdb.m_term);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		Prelease(P, PRELEASE_CLEAR | PRELEASE_HANG);
9437c478bd9Sstevel@tonic-gate 		(void) execv(execname, (char *const *)argv);
9447c478bd9Sstevel@tonic-gate 		warn("failed to re-exec debugger");
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		if (mdb.m_term != NULL)
9477c478bd9Sstevel@tonic-gate 			IOP_RESUME(mdb.m_term);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		t->t_pshandle = pt->p_idlehandle;
9507c478bd9Sstevel@tonic-gate 		return;
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	pt_post_attach(t);	/* install tracing flags and activate events */
9547c478bd9Sstevel@tonic-gate 	pt_activate_common(t);	/* initialize librtld_db and libthread_db */
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (psp->pr_dmodel != PR_MODEL_NATIVE && mdb.m_term != NULL) {
9577c478bd9Sstevel@tonic-gate 		warn("loadable dcmds will not operate on non-native %d-bit "
9587c478bd9Sstevel@tonic-gate 		    "data model\n", psp->pr_dmodel == PR_MODEL_ILP32 ? 32 : 64);
9597c478bd9Sstevel@tonic-gate 		warn("use ::release -a and then run mdb -p %d to restart "
9607c478bd9Sstevel@tonic-gate 		    "debugger\n", (int)pid);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (follow_exec)
9647c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_continue(t, NULL);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate static int
pt_setflags(mdb_tgt_t * t,int flags)9687c478bd9Sstevel@tonic-gate pt_setflags(mdb_tgt_t *t, int flags)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if ((flags ^ t->t_flags) & MDB_TGT_F_RDWR) {
9737c478bd9Sstevel@tonic-gate 		int mode = (flags & MDB_TGT_F_RDWR) ? O_RDWR : O_RDONLY;
9747c478bd9Sstevel@tonic-gate 		mdb_io_t *io;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		if (pt->p_fio == NULL)
9777c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_NOEXEC));
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		io = mdb_fdio_create_path(NULL, IOP_NAME(pt->p_fio), mode, 0);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		if (io == NULL)
9827c478bd9Sstevel@tonic-gate 			return (-1); /* errno is set for us */
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		t->t_flags = (t->t_flags & ~MDB_TGT_F_RDWR) |
9857c478bd9Sstevel@tonic-gate 		    (flags & MDB_TGT_F_RDWR);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		pt->p_fio = mdb_io_hold(io);
9887c478bd9Sstevel@tonic-gate 		mdb_io_rele(pt->p_file->gf_io);
9897c478bd9Sstevel@tonic-gate 		pt->p_file->gf_io = pt->p_fio;
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	if (flags & MDB_TGT_F_FORCE) {
9937c478bd9Sstevel@tonic-gate 		t->t_flags |= MDB_TGT_F_FORCE;
9947c478bd9Sstevel@tonic-gate 		pt->p_gflags |= PGRAB_FORCE;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	return (0);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10017c478bd9Sstevel@tonic-gate static int
pt_frame(void * arglim,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)10027c478bd9Sstevel@tonic-gate pt_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
10037c478bd9Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	argc = MIN(argc, (uint_t)(uintptr_t)arglim);
10067c478bd9Sstevel@tonic-gate 	mdb_printf("%a(", pc);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (argc != 0) {
10097c478bd9Sstevel@tonic-gate 		mdb_printf("%lr", *argv++);
10107c478bd9Sstevel@tonic-gate 		for (argc--; argc != 0; argc--)
10117c478bd9Sstevel@tonic-gate 			mdb_printf(", %lr", *argv++);
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	mdb_printf(")\n");
10157c478bd9Sstevel@tonic-gate 	return (0);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate static int
pt_framev(void * arglim,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)10197c478bd9Sstevel@tonic-gate pt_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
10207c478bd9Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	argc = MIN(argc, (uint_t)(uintptr_t)arglim);
10237c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
10247c478bd9Sstevel@tonic-gate 	mdb_printf("%0?lr %a(", gregs->gregs[R_FP], pc);
10257c478bd9Sstevel@tonic-gate #else
10267c478bd9Sstevel@tonic-gate 	mdb_printf("%0?lr %a(", gregs->gregs[R_SP], pc);
10277c478bd9Sstevel@tonic-gate #endif
10287c478bd9Sstevel@tonic-gate 	if (argc != 0) {
10297c478bd9Sstevel@tonic-gate 		mdb_printf("%lr", *argv++);
10307c478bd9Sstevel@tonic-gate 		for (argc--; argc != 0; argc--)
10317c478bd9Sstevel@tonic-gate 			mdb_printf(", %lr", *argv++);
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	mdb_printf(")\n");
10357c478bd9Sstevel@tonic-gate 	return (0);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static int
pt_framer(void * arglim,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)10397c478bd9Sstevel@tonic-gate pt_framer(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
10407c478bd9Sstevel@tonic-gate     const mdb_tgt_gregset_t *gregs)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	if (pt_frameregs(arglim, pc, argc, argv, gregs, pc == PC_FAKE) == -1) {
10437c478bd9Sstevel@tonic-gate 		/*
10447c478bd9Sstevel@tonic-gate 		 * Use verbose format if register format is not supported.
10457c478bd9Sstevel@tonic-gate 		 */
10467c478bd9Sstevel@tonic-gate 		return (pt_framev(arglim, pc, argc, argv, gregs));
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	return (0);
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10537c478bd9Sstevel@tonic-gate static int
pt_stack_common(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv,mdb_tgt_stack_f * func,prgreg_t saved_pc)10547c478bd9Sstevel@tonic-gate pt_stack_common(uintptr_t addr, uint_t flags, int argc,
10557c478bd9Sstevel@tonic-gate     const mdb_arg_t *argv, mdb_tgt_stack_f *func, prgreg_t saved_pc)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	void *arg = (void *)(uintptr_t)mdb.m_nargs;
10587c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
10597c478bd9Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	if (argc != 0) {
10627c478bd9Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
10637c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_STRING)
10667c478bd9Sstevel@tonic-gate 			arg = (void *)(uintptr_t)mdb_strtoull(argv->a_un.a_str);
10677c478bd9Sstevel@tonic-gate 		else
10687c478bd9Sstevel@tonic-gate 			arg = (void *)(uintptr_t)argv->a_un.a_val;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
10727c478bd9Sstevel@tonic-gate 		mdb_warn("no process active\n");
10737c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * In the universe of sparcv7, sparcv9, ia32, and amd64 this code can be
10787c478bd9Sstevel@tonic-gate 	 * common: <sys/procfs_isa.h> conveniently #defines R_FP to be the
10797c478bd9Sstevel@tonic-gate 	 * appropriate register we need to set in order to perform a stack
10807c478bd9Sstevel@tonic-gate 	 * traceback from a given frame address.
10817c478bd9Sstevel@tonic-gate 	 */
10827c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
10837c478bd9Sstevel@tonic-gate 		bzero(&gregs, sizeof (gregs));
10847c478bd9Sstevel@tonic-gate 		gregs.gregs[R_FP] = addr;
10857c478bd9Sstevel@tonic-gate #ifdef __sparc
10867c478bd9Sstevel@tonic-gate 		gregs.gregs[R_I7] = saved_pc;
10877c478bd9Sstevel@tonic-gate #endif /* __sparc */
10887c478bd9Sstevel@tonic-gate 	} else if (PTL_GETREGS(t, PTL_TID(t), gregs.gregs) != 0) {
10897c478bd9Sstevel@tonic-gate 		mdb_warn("failed to get current register set");
10907c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_stack_iter(t, &gregs, func, arg);
10947c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate static int
pt_stack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)10987c478bd9Sstevel@tonic-gate pt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	return (pt_stack_common(addr, flags, argc, argv, pt_frame, 0));
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate static int
pt_stackv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)11047c478bd9Sstevel@tonic-gate pt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	return (pt_stack_common(addr, flags, argc, argv, pt_framev, 0));
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate static int
pt_stackr(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)11107c478bd9Sstevel@tonic-gate pt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	/*
11137c478bd9Sstevel@tonic-gate 	 * Force printing of first register window, by setting  the
11147c478bd9Sstevel@tonic-gate 	 * saved pc (%i7) to PC_FAKE.
11157c478bd9Sstevel@tonic-gate 	 */
11167c478bd9Sstevel@tonic-gate 	return (pt_stack_common(addr, flags, argc, argv, pt_framer, PC_FAKE));
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11207c478bd9Sstevel@tonic-gate static int
pt_ignored(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)11217c478bd9Sstevel@tonic-gate pt_ignored(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = mdb.m_target->t_pshandle;
11247c478bd9Sstevel@tonic-gate 	char buf[PRSIGBUFSZ];
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
11277c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (P == NULL) {
11307c478bd9Sstevel@tonic-gate 		mdb_warn("no process is currently active\n");
11317c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	mdb_printf("%s\n", proc_sigset2str(&Pstatus(P)->pr_sigtrace, " ",
11357c478bd9Sstevel@tonic-gate 	    FALSE, buf, sizeof (buf)));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11417c478bd9Sstevel@tonic-gate static int
pt_lwpid(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)11427c478bd9Sstevel@tonic-gate pt_lwpid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = mdb.m_target->t_pshandle;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
11477c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (P == NULL) {
11507c478bd9Sstevel@tonic-gate 		mdb_warn("no process is currently active\n");
11517c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	mdb_printf("%d\n", Pstatus(P)->pr_lwp.pr_lwpid);
11557c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate static int
pt_print_lwpid(int * n,const lwpstatus_t * psp)11597c478bd9Sstevel@tonic-gate pt_print_lwpid(int *n, const lwpstatus_t *psp)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = mdb.m_target->t_pshandle;
11627c478bd9Sstevel@tonic-gate 	int nlwp = Pstatus(P)->pr_nlwp;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (*n == nlwp - 2)
11657c478bd9Sstevel@tonic-gate 		mdb_printf("%d and ", (int)psp->pr_lwpid);
11667c478bd9Sstevel@tonic-gate 	else if (*n == nlwp - 1)
11677c478bd9Sstevel@tonic-gate 		mdb_printf("%d are", (int)psp->pr_lwpid);
11687c478bd9Sstevel@tonic-gate 	else
11697c478bd9Sstevel@tonic-gate 		mdb_printf("%d, ", (int)psp->pr_lwpid);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	(*n)++;
11727c478bd9Sstevel@tonic-gate 	return (0);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11767c478bd9Sstevel@tonic-gate static int
pt_lwpids(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)11777c478bd9Sstevel@tonic-gate pt_lwpids(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
11787c478bd9Sstevel@tonic-gate {
11797c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = mdb.m_target->t_pshandle;
11807c478bd9Sstevel@tonic-gate 	int n = 0;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	if (P == NULL) {
11837c478bd9Sstevel@tonic-gate 		mdb_warn("no process is currently active\n");
11847c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	switch (Pstatus(P)->pr_nlwp) {
11887c478bd9Sstevel@tonic-gate 	case 0:
11897c478bd9Sstevel@tonic-gate 		mdb_printf("no lwps are");
11907c478bd9Sstevel@tonic-gate 		break;
11917c478bd9Sstevel@tonic-gate 	case 1:
11927c478bd9Sstevel@tonic-gate 		mdb_printf("lwpid %d is the only lwp",
11937c478bd9Sstevel@tonic-gate 		    Pstatus(P)->pr_lwp.pr_lwpid);
11947c478bd9Sstevel@tonic-gate 		break;
11957c478bd9Sstevel@tonic-gate 	default:
11967c478bd9Sstevel@tonic-gate 		mdb_printf("lwpids ");
11977c478bd9Sstevel@tonic-gate 		(void) Plwp_iter(P, (proc_lwp_f *)pt_print_lwpid, &n);
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	switch (Pstate(P)) {
12017c478bd9Sstevel@tonic-gate 	case PS_DEAD:
12027c478bd9Sstevel@tonic-gate 		mdb_printf(" in core of process %d.\n", Pstatus(P)->pr_pid);
12037c478bd9Sstevel@tonic-gate 		break;
12047c478bd9Sstevel@tonic-gate 	case PS_IDLE:
12057c478bd9Sstevel@tonic-gate 		mdb_printf(" in idle target.\n");
12067c478bd9Sstevel@tonic-gate 		break;
12077c478bd9Sstevel@tonic-gate 	default:
12087c478bd9Sstevel@tonic-gate 		mdb_printf(" in process %d.\n", (int)Pstatus(P)->pr_pid);
12097c478bd9Sstevel@tonic-gate 		break;
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12167c478bd9Sstevel@tonic-gate static int
pt_ignore(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)12177c478bd9Sstevel@tonic-gate pt_ignore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	pt_data_t *pt = mdb.m_target->t_data;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
12227c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (addr < 1 || addr > pt->p_maxsig) {
12257c478bd9Sstevel@tonic-gate 		mdb_warn("invalid signal number -- 0t%lu\n", addr);
12267c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_vespec_iter(mdb.m_target, pt_ignore_sig, (void *)addr);
12307c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12347c478bd9Sstevel@tonic-gate static int
pt_attach(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)12357c478bd9Sstevel@tonic-gate pt_attach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
12367c478bd9Sstevel@tonic-gate {
12377c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
12387c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
12397c478bd9Sstevel@tonic-gate 	int state, perr;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) && argc == 0)
12427c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	if (((flags & DCMD_ADDRSPEC) && argc != 0) || argc > 1 ||
12457c478bd9Sstevel@tonic-gate 	    (argc != 0 && argv->a_type != MDB_TYPE_STRING))
12467c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
12497c478bd9Sstevel@tonic-gate 		mdb_warn("debugger is already attached to a %s\n",
12507c478bd9Sstevel@tonic-gate 		    (Pstate(t->t_pshandle) == PS_DEAD) ? "core" : "process");
12517c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (pt->p_fio == NULL) {
12557c478bd9Sstevel@tonic-gate 		mdb_warn("attach requires executable to be specified on "
12567c478bd9Sstevel@tonic-gate 		    "command-line (or use -p)\n");
12577c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC)
12617c478bd9Sstevel@tonic-gate 		t->t_pshandle = Pgrab((pid_t)addr, pt->p_gflags, &perr);
12627c478bd9Sstevel@tonic-gate 	else
12637c478bd9Sstevel@tonic-gate 		t->t_pshandle = proc_arg_grab(argv->a_un.a_str,
12647c478bd9Sstevel@tonic-gate 		    PR_ARG_ANY, pt->p_gflags, &perr);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
12677c478bd9Sstevel@tonic-gate 		t->t_pshandle = pt->p_idlehandle;
12687c478bd9Sstevel@tonic-gate 		mdb_warn("cannot attach: %s\n", Pgrab_error(perr));
12697c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	state = Pstate(t->t_pshandle);
12737c478bd9Sstevel@tonic-gate 	if (state != PS_DEAD && state != PS_IDLE) {
12747c478bd9Sstevel@tonic-gate 		(void) Punsetflags(t->t_pshandle, PR_KLC);
12757c478bd9Sstevel@tonic-gate 		(void) Psetflags(t->t_pshandle, PR_RLC);
12767c478bd9Sstevel@tonic-gate 		pt_post_attach(t);
12777c478bd9Sstevel@tonic-gate 		pt_activate_common(t);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
12817c478bd9Sstevel@tonic-gate 	mdb_module_load_all(0);
12827c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate static int
pt_regstatus(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)12867c478bd9Sstevel@tonic-gate pt_regstatus(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
12917c478bd9Sstevel@tonic-gate 		const pstatus_t *psp = Pstatus(t->t_pshandle);
12927c478bd9Sstevel@tonic-gate 		int cursig = psp->pr_lwp.pr_cursig;
12937c478bd9Sstevel@tonic-gate 		char signame[SIG2STR_MAX];
12947c478bd9Sstevel@tonic-gate 		int state = Pstate(t->t_pshandle);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		if (state != PS_DEAD && state != PS_IDLE)
12977c478bd9Sstevel@tonic-gate 			mdb_printf("process id = %d\n", psp->pr_pid);
12987c478bd9Sstevel@tonic-gate 		else
12997c478bd9Sstevel@tonic-gate 			mdb_printf("no process\n");
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 		if (cursig != 0 && sig2str(cursig, signame) == 0)
13027c478bd9Sstevel@tonic-gate 			mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	return (pt_regs(addr, flags, argc, argv));
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate 
1308a48fdbefSBryan Cantrill static int
pt_thread_name(mdb_tgt_t * t,mdb_tgt_tid_t tid,char * buf,size_t bufsize)1309ab618543SJohn Levon pt_thread_name(mdb_tgt_t *t, mdb_tgt_tid_t tid, char *buf, size_t bufsize)
1310ab618543SJohn Levon {
1311ab618543SJohn Levon 	char name[THREAD_NAME_MAX];
1312ab618543SJohn Levon 
1313ab618543SJohn Levon 	buf[0] = '\0';
1314ab618543SJohn Levon 
1315ab618543SJohn Levon 	if (t->t_pshandle == NULL ||
1316ab618543SJohn Levon 	    Plwp_getname(t->t_pshandle, tid, name, sizeof (name)) != 0 ||
1317ab618543SJohn Levon 	    name[0] == '\0') {
1318a48fdbefSBryan Cantrill 		if (mdb_snprintf(buf, bufsize, "%lu", tid) > bufsize) {
1319a48fdbefSBryan Cantrill 			return (set_errno(EMDB_NAME2BIG));
1320ab618543SJohn Levon 		}
1321ab618543SJohn Levon 
1322a48fdbefSBryan Cantrill 		return (0);
1323a48fdbefSBryan Cantrill 	}
1324a48fdbefSBryan Cantrill 
1325a48fdbefSBryan Cantrill 	if (mdb_snprintf(buf, bufsize, "%lu [%s]", tid, name) > bufsize) {
1326a48fdbefSBryan Cantrill 		return (set_errno(EMDB_NAME2BIG));
1327a48fdbefSBryan Cantrill 	}
1328a48fdbefSBryan Cantrill 
1329a48fdbefSBryan Cantrill 	return (0);
1330ab618543SJohn Levon }
1331ab618543SJohn Levon 
13327c478bd9Sstevel@tonic-gate static int
pt_findstack(uintptr_t tid,uint_t flags,int argc,const mdb_arg_t * argv)13337c478bd9Sstevel@tonic-gate pt_findstack(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
13347c478bd9Sstevel@tonic-gate {
13357c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
13367c478bd9Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
13377c478bd9Sstevel@tonic-gate 	int showargs = 0;
13387c478bd9Sstevel@tonic-gate 	int count;
13397c478bd9Sstevel@tonic-gate 	uintptr_t pc, sp;
1340ab618543SJohn Levon 	char name[128];
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
13437c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	count = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &showargs,
13467c478bd9Sstevel@tonic-gate 	    NULL);
13477c478bd9Sstevel@tonic-gate 	argc -= count;
13487c478bd9Sstevel@tonic-gate 	argv += count;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	if (argc > 1 || (argc == 1 && argv->a_type != MDB_TYPE_STRING))
13517c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if (PTL_GETREGS(t, tid, gregs.gregs) != 0) {
13547c478bd9Sstevel@tonic-gate 		mdb_warn("failed to get register set for thread %p", tid);
13557c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	pc = gregs.gregs[R_PC];
13597c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
13607c478bd9Sstevel@tonic-gate 	sp = gregs.gregs[R_FP];
13617c478bd9Sstevel@tonic-gate #else
13627c478bd9Sstevel@tonic-gate 	sp = gregs.gregs[R_SP];
13637c478bd9Sstevel@tonic-gate #endif
1364ab618543SJohn Levon 
1365a48fdbefSBryan Cantrill 	(void) pt_thread_name(t, tid, name, sizeof (name));
1366ab618543SJohn Levon 
1367ab618543SJohn Levon 	mdb_printf("stack pointer for thread %s: %p\n", name, sp);
13687c478bd9Sstevel@tonic-gate 	if (pc != 0)
13697c478bd9Sstevel@tonic-gate 		mdb_printf("[ %0?lr %a() ]\n", sp, pc);
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	(void) mdb_inc_indent(2);
13727c478bd9Sstevel@tonic-gate 	mdb_set_dot(sp);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (argc == 1)
13757c478bd9Sstevel@tonic-gate 		(void) mdb_eval(argv->a_un.a_str);
13767c478bd9Sstevel@tonic-gate 	else if (showargs)
13777c478bd9Sstevel@tonic-gate 		(void) mdb_eval("<.$C");
13787c478bd9Sstevel@tonic-gate 	else
13797c478bd9Sstevel@tonic-gate 		(void) mdb_eval("<.$C0");
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	(void) mdb_dec_indent(2);
13827c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13867c478bd9Sstevel@tonic-gate static int
pt_gcore(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)13877c478bd9Sstevel@tonic-gate pt_gcore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
13907c478bd9Sstevel@tonic-gate 	char *prefix = "core";
13917c478bd9Sstevel@tonic-gate 	char *content_str = NULL;
13927c478bd9Sstevel@tonic-gate 	core_content_t content = CC_CONTENT_DEFAULT;
13937c478bd9Sstevel@tonic-gate 	size_t size;
13947c478bd9Sstevel@tonic-gate 	char *fname;
13957c478bd9Sstevel@tonic-gate 	pid_t pid;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC)
13987c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
14017c478bd9Sstevel@tonic-gate 	    'o', MDB_OPT_STR, &prefix,
14027c478bd9Sstevel@tonic-gate 	    'c', MDB_OPT_STR, &content_str, NULL) != argc)
14037c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if (content_str != NULL &&
14067c478bd9Sstevel@tonic-gate 	    (proc_str2content(content_str, &content) != 0 ||
14077c478bd9Sstevel@tonic-gate 	    content == CC_CONTENT_INVALID)) {
14087c478bd9Sstevel@tonic-gate 		mdb_warn("invalid content string '%s'\n", content_str);
14097c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 
1412f723faa1Seschrock 	if (t->t_pshandle == NULL) {
1413f723faa1Seschrock 		mdb_warn("no process active\n");
1414f723faa1Seschrock 		return (DCMD_ERR);
1415f723faa1Seschrock 	}
1416f723faa1Seschrock 
14177c478bd9Sstevel@tonic-gate 	pid = Pstatus(t->t_pshandle)->pr_pid;
14187c478bd9Sstevel@tonic-gate 	size = 1 + mdb_snprintf(NULL, 0, "%s.%d", prefix, (int)pid);
14197c478bd9Sstevel@tonic-gate 	fname = mdb_alloc(size, UM_SLEEP | UM_GC);
14207c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(fname, size, "%s.%d", prefix, (int)pid);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	if (Pgcore(t->t_pshandle, fname, content) != 0) {
142364e4e50aSKeith M Wesolowski 		/*
142464e4e50aSKeith M Wesolowski 		 * Short writes during dumping are specifically described by
142564e4e50aSKeith M Wesolowski 		 * EBADE, just as ZFS uses this otherwise-unused code for
142664e4e50aSKeith M Wesolowski 		 * checksum errors.  Translate to and mdb errno.
142764e4e50aSKeith M Wesolowski 		 */
142864e4e50aSKeith M Wesolowski 		if (errno == EBADE)
142964e4e50aSKeith M Wesolowski 			(void) set_errno(EMDB_SHORTWRITE);
14307c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't dump core");
14317c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	mdb_warn("%s dumped\n", fname);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14407c478bd9Sstevel@tonic-gate static int
pt_kill(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)14417c478bd9Sstevel@tonic-gate pt_kill(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
14427c478bd9Sstevel@tonic-gate {
14437c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
14447c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
14457c478bd9Sstevel@tonic-gate 	int state;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
14487c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL &&
14517c478bd9Sstevel@tonic-gate 	    (state = Pstate(t->t_pshandle)) != PS_DEAD && state != PS_IDLE) {
14527c478bd9Sstevel@tonic-gate 		mdb_warn("victim process PID %d forcibly terminated\n",
14537c478bd9Sstevel@tonic-gate 		    (int)Pstatus(t->t_pshandle)->pr_pid);
14547c478bd9Sstevel@tonic-gate 		pt_pre_detach(t, TRUE);
14557c478bd9Sstevel@tonic-gate 		pt_release_parents(t);
14567c478bd9Sstevel@tonic-gate 		Prelease(t->t_pshandle, PRELEASE_KILL);
14577c478bd9Sstevel@tonic-gate 		t->t_pshandle = pt->p_idlehandle;
14587c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_status(t, &t->t_status);
14597c478bd9Sstevel@tonic-gate 		mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
14607c478bd9Sstevel@tonic-gate 	} else
14617c478bd9Sstevel@tonic-gate 		mdb_warn("no victim process is currently under control\n");
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14677c478bd9Sstevel@tonic-gate static int
pt_detach(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)14687c478bd9Sstevel@tonic-gate pt_detach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
14697c478bd9Sstevel@tonic-gate {
14707c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
14717c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
14727c478bd9Sstevel@tonic-gate 	int rflags = pt->p_rflags;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (argc != 0 && argv->a_type == MDB_TYPE_STRING &&
14757c478bd9Sstevel@tonic-gate 	    strcmp(argv->a_un.a_str, "-a") == 0) {
14767c478bd9Sstevel@tonic-gate 		rflags = PRELEASE_HANG | PRELEASE_CLEAR;
14777c478bd9Sstevel@tonic-gate 		argv++;
14787c478bd9Sstevel@tonic-gate 		argc--;
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
14827c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
14857c478bd9Sstevel@tonic-gate 		mdb_warn("debugger is not currently attached to a process "
14867c478bd9Sstevel@tonic-gate 		    "or core file\n");
14877c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	pt_pre_detach(t, TRUE);
14917c478bd9Sstevel@tonic-gate 	pt_release_parents(t);
14927c478bd9Sstevel@tonic-gate 	Prelease(t->t_pshandle, rflags);
14937c478bd9Sstevel@tonic-gate 	t->t_pshandle = pt->p_idlehandle;
14947c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
14957c478bd9Sstevel@tonic-gate 	mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate static uintmax_t
reg_disc_get(const mdb_var_t * v)15017c478bd9Sstevel@tonic-gate reg_disc_get(const mdb_var_t *v)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = MDB_NV_COOKIE(v);
15047c478bd9Sstevel@tonic-gate 	mdb_tgt_tid_t tid = PTL_TID(t);
15057c478bd9Sstevel@tonic-gate 	mdb_tgt_reg_t r = 0;
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	if (tid != (mdb_tgt_tid_t)-1L)
15087c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_getareg(t, tid, mdb_nv_get_name(v), &r);
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	return (r);
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate static void
reg_disc_set(mdb_var_t * v,uintmax_t r)15147c478bd9Sstevel@tonic-gate reg_disc_set(mdb_var_t *v, uintmax_t r)
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = MDB_NV_COOKIE(v);
15177c478bd9Sstevel@tonic-gate 	mdb_tgt_tid_t tid = PTL_TID(t);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	if (tid != (mdb_tgt_tid_t)-1L && mdb_tgt_putareg(t, tid,
15207c478bd9Sstevel@tonic-gate 	    mdb_nv_get_name(v), r) == -1)
15217c478bd9Sstevel@tonic-gate 		mdb_warn("failed to modify %%%s register", mdb_nv_get_name(v));
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate static void
pt_print_reason(const lwpstatus_t * psp)15257c478bd9Sstevel@tonic-gate pt_print_reason(const lwpstatus_t *psp)
15267c478bd9Sstevel@tonic-gate {
15277c478bd9Sstevel@tonic-gate 	char name[SIG2STR_MAX + 4]; /* enough for SIG+name+\0, syscall or flt */
15287c478bd9Sstevel@tonic-gate 	const char *desc;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	switch (psp->pr_why) {
15317c478bd9Sstevel@tonic-gate 	case PR_REQUESTED:
15327c478bd9Sstevel@tonic-gate 		mdb_printf("stopped by debugger");
15337c478bd9Sstevel@tonic-gate 		break;
15347c478bd9Sstevel@tonic-gate 	case PR_SIGNALLED:
15357c478bd9Sstevel@tonic-gate 		mdb_printf("stopped on %s (%s)", proc_signame(psp->pr_what,
15367c478bd9Sstevel@tonic-gate 		    name, sizeof (name)), strsignal(psp->pr_what));
15377c478bd9Sstevel@tonic-gate 		break;
15387c478bd9Sstevel@tonic-gate 	case PR_SYSENTRY:
15397c478bd9Sstevel@tonic-gate 		mdb_printf("stopped on entry to %s system call",
15407c478bd9Sstevel@tonic-gate 		    proc_sysname(psp->pr_what, name, sizeof (name)));
15417c478bd9Sstevel@tonic-gate 		break;
15427c478bd9Sstevel@tonic-gate 	case PR_SYSEXIT:
15437c478bd9Sstevel@tonic-gate 		mdb_printf("stopped on exit from %s system call",
15447c478bd9Sstevel@tonic-gate 		    proc_sysname(psp->pr_what, name, sizeof (name)));
15457c478bd9Sstevel@tonic-gate 		break;
15467c478bd9Sstevel@tonic-gate 	case PR_JOBCONTROL:
15477c478bd9Sstevel@tonic-gate 		mdb_printf("stopped by job control");
15487c478bd9Sstevel@tonic-gate 		break;
15497c478bd9Sstevel@tonic-gate 	case PR_FAULTED:
15507c478bd9Sstevel@tonic-gate 		if (psp->pr_what == FLTBPT) {
15517c478bd9Sstevel@tonic-gate 			mdb_printf("stopped on a breakpoint");
15527c478bd9Sstevel@tonic-gate 		} else if (psp->pr_what == FLTWATCH) {
15537c478bd9Sstevel@tonic-gate 			switch (psp->pr_info.si_code) {
15547c478bd9Sstevel@tonic-gate 			case TRAP_RWATCH:
15557c478bd9Sstevel@tonic-gate 				desc = "read";
15567c478bd9Sstevel@tonic-gate 				break;
15577c478bd9Sstevel@tonic-gate 			case TRAP_WWATCH:
15587c478bd9Sstevel@tonic-gate 				desc = "write";
15597c478bd9Sstevel@tonic-gate 				break;
15607c478bd9Sstevel@tonic-gate 			case TRAP_XWATCH:
15617c478bd9Sstevel@tonic-gate 				desc = "execute";
15627c478bd9Sstevel@tonic-gate 				break;
15637c478bd9Sstevel@tonic-gate 			default:
15647c478bd9Sstevel@tonic-gate 				desc = "unknown";
15657c478bd9Sstevel@tonic-gate 			}
15667c478bd9Sstevel@tonic-gate 			mdb_printf("stopped %s a watchpoint (%s access to %p)",
15677c478bd9Sstevel@tonic-gate 			    psp->pr_info.si_trapafter ? "after" : "on",
15687c478bd9Sstevel@tonic-gate 			    desc, psp->pr_info.si_addr);
15697c478bd9Sstevel@tonic-gate 		} else if (psp->pr_what == FLTTRACE) {
15707c478bd9Sstevel@tonic-gate 			mdb_printf("stopped after a single-step");
15717c478bd9Sstevel@tonic-gate 		} else {
15727c478bd9Sstevel@tonic-gate 			mdb_printf("stopped on a %s fault",
15737c478bd9Sstevel@tonic-gate 			    proc_fltname(psp->pr_what, name, sizeof (name)));
15747c478bd9Sstevel@tonic-gate 		}
15757c478bd9Sstevel@tonic-gate 		break;
15767c478bd9Sstevel@tonic-gate 	case PR_SUSPENDED:
15777c478bd9Sstevel@tonic-gate 	case PR_CHECKPOINT:
15787c478bd9Sstevel@tonic-gate 		mdb_printf("suspended by the kernel");
15797c478bd9Sstevel@tonic-gate 		break;
15807c478bd9Sstevel@tonic-gate 	default:
15817c478bd9Sstevel@tonic-gate 		mdb_printf("stopped for unknown reason (%d/%d)",
15827c478bd9Sstevel@tonic-gate 		    psp->pr_why, psp->pr_what);
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
1586350ffdd5SRobert Mustacchi static void
pt_status_dcmd_upanic(prupanic_t * pru)1587350ffdd5SRobert Mustacchi pt_status_dcmd_upanic(prupanic_t *pru)
1588350ffdd5SRobert Mustacchi {
1589350ffdd5SRobert Mustacchi 	size_t i;
1590350ffdd5SRobert Mustacchi 
1591350ffdd5SRobert Mustacchi 	mdb_printf("process panicked\n");
1592350ffdd5SRobert Mustacchi 	if ((pru->pru_flags & PRUPANIC_FLAG_MSG_ERROR) != 0) {
1593350ffdd5SRobert Mustacchi 		mdb_printf("warning: process upanic message was bad\n");
1594350ffdd5SRobert Mustacchi 		return;
1595350ffdd5SRobert Mustacchi 	}
1596350ffdd5SRobert Mustacchi 
1597350ffdd5SRobert Mustacchi 	if ((pru->pru_flags & PRUPANIC_FLAG_MSG_VALID) == 0)
1598350ffdd5SRobert Mustacchi 		return;
1599350ffdd5SRobert Mustacchi 
1600350ffdd5SRobert Mustacchi 	if ((pru->pru_flags & PRUPANIC_FLAG_MSG_TRUNC) != 0) {
1601350ffdd5SRobert Mustacchi 		mdb_printf("warning: process upanic message truncated\n");
1602350ffdd5SRobert Mustacchi 	}
1603350ffdd5SRobert Mustacchi 
1604350ffdd5SRobert Mustacchi 	mdb_printf("upanic message: ");
1605350ffdd5SRobert Mustacchi 
1606350ffdd5SRobert Mustacchi 	for (i = 0; i < PRUPANIC_BUFLEN; i++) {
1607350ffdd5SRobert Mustacchi 		if (pru->pru_data[i] == '\0')
1608350ffdd5SRobert Mustacchi 			break;
1609350ffdd5SRobert Mustacchi 		if (isascii(pru->pru_data[i]) && isprint(pru->pru_data[i])) {
1610350ffdd5SRobert Mustacchi 			mdb_printf("%c", pru->pru_data[i]);
1611350ffdd5SRobert Mustacchi 		} else {
1612350ffdd5SRobert Mustacchi 			mdb_printf("\\x%02x", pru->pru_data[i]);
1613350ffdd5SRobert Mustacchi 		}
1614350ffdd5SRobert Mustacchi 	}
1615350ffdd5SRobert Mustacchi 	mdb_printf("\n");
1616350ffdd5SRobert Mustacchi }
1617350ffdd5SRobert Mustacchi 
16187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16197c478bd9Sstevel@tonic-gate static int
pt_status_dcmd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)16207c478bd9Sstevel@tonic-gate pt_status_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
16237c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
16247c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if (P != NULL) {
16277c478bd9Sstevel@tonic-gate 		const psinfo_t *pip = Ppsinfo(P);
16287c478bd9Sstevel@tonic-gate 		const pstatus_t *psp = Pstatus(P);
16297c478bd9Sstevel@tonic-gate 		int cursig = 0, bits = 0, coredump = 0;
16307c478bd9Sstevel@tonic-gate 		int state;
16317c478bd9Sstevel@tonic-gate 		GElf_Sym sym;
16327c478bd9Sstevel@tonic-gate 		uintptr_t panicstr;
1633f68770eaSRobert Mustacchi 		char *panicbuf = mdb_alloc(PANIC_BUFSIZE, UM_SLEEP);
163448fd701fSKrishnendu Sadhukhan - Sun Microsystems 		const siginfo_t *sip = &(psp->pr_lwp.pr_info);
1635350ffdd5SRobert Mustacchi 		prupanic_t *pru = NULL;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 		char execname[MAXPATHLEN], buf[BUFSIZ];
16387c478bd9Sstevel@tonic-gate 		char signame[SIG2STR_MAX + 4]; /* enough for SIG+name+\0 */
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		mdb_tgt_spec_desc_t desc;
16417c478bd9Sstevel@tonic-gate 		mdb_sespec_t *sep;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 		struct utsname uts;
16447c478bd9Sstevel@tonic-gate 		prcred_t cred;
16457c478bd9Sstevel@tonic-gate 		psinfo_t pi;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 		(void) strcpy(uts.nodename, "unknown machine");
16487c478bd9Sstevel@tonic-gate 		(void) Puname(P, &uts);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 		if (pip != NULL) {
16517c478bd9Sstevel@tonic-gate 			bcopy(pip, &pi, sizeof (psinfo_t));
16527c478bd9Sstevel@tonic-gate 			proc_unctrl_psinfo(&pi);
16537c478bd9Sstevel@tonic-gate 		} else
16547c478bd9Sstevel@tonic-gate 			bzero(&pi, sizeof (psinfo_t));
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		bits = pi.pr_dmodel == PR_MODEL_ILP32 ? 32 : 64;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 		state = Pstate(P);
16597c478bd9Sstevel@tonic-gate 		if (psp != NULL && state != PS_UNDEAD && state != PS_IDLE)
16607c478bd9Sstevel@tonic-gate 			cursig = psp->pr_lwp.pr_cursig;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 		if (state == PS_DEAD && pip != NULL) {
16637c478bd9Sstevel@tonic-gate 			mdb_printf("debugging core file of %s (%d-bit) "
16647c478bd9Sstevel@tonic-gate 			    "from %s\n", pi.pr_fname, bits, uts.nodename);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		} else if (state == PS_DEAD) {
16677c478bd9Sstevel@tonic-gate 			mdb_printf("debugging core file\n");
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		} else if (state == PS_IDLE) {
16707c478bd9Sstevel@tonic-gate 			const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 			mdb_printf("debugging %s file (%d-bit)\n",
16737c478bd9Sstevel@tonic-gate 			    ehp->e_type == ET_EXEC ? "executable" : "object",
16747c478bd9Sstevel@tonic-gate 			    ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		} else if (state == PS_UNDEAD && pi.pr_pid == 0) {
16777c478bd9Sstevel@tonic-gate 			mdb_printf("debugging defunct process\n");
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 		} else {
16807c478bd9Sstevel@tonic-gate 			mdb_printf("debugging PID %d (%d-bit)\n",
16817c478bd9Sstevel@tonic-gate 			    pi.pr_pid, bits);
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		if (Pexecname(P, execname, sizeof (execname)) != NULL)
16857c478bd9Sstevel@tonic-gate 			mdb_printf("file: %s\n", execname);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 		if (pip != NULL && state == PS_DEAD)
16887c478bd9Sstevel@tonic-gate 			mdb_printf("initial argv: %s\n", pi.pr_psargs);
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 		if (state != PS_UNDEAD && state != PS_IDLE) {
16917c478bd9Sstevel@tonic-gate 			mdb_printf("threading model: ");
16927c478bd9Sstevel@tonic-gate 			if (pt->p_ptl_ops == &proc_lwp_ops)
16937c478bd9Sstevel@tonic-gate 				mdb_printf("raw lwps\n");
16947c478bd9Sstevel@tonic-gate 			else
16957c478bd9Sstevel@tonic-gate 				mdb_printf("native threads\n");
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		mdb_printf("status: ");
16997c478bd9Sstevel@tonic-gate 		switch (state) {
17007c478bd9Sstevel@tonic-gate 		case PS_RUN:
17017c478bd9Sstevel@tonic-gate 			ASSERT(!(psp->pr_flags & PR_STOPPED));
17027c478bd9Sstevel@tonic-gate 			mdb_printf("process is running");
17037c478bd9Sstevel@tonic-gate 			if (psp->pr_flags & PR_DSTOP)
17047c478bd9Sstevel@tonic-gate 				mdb_printf(", debugger stop directive pending");
17057c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
17067c478bd9Sstevel@tonic-gate 			break;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 		case PS_STOP:
17097c478bd9Sstevel@tonic-gate 			ASSERT(psp->pr_flags & PR_STOPPED);
17107c478bd9Sstevel@tonic-gate 			pt_print_reason(&psp->pr_lwp);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 			if (psp->pr_flags & PR_DSTOP)
17137c478bd9Sstevel@tonic-gate 				mdb_printf(", debugger stop directive pending");
17147c478bd9Sstevel@tonic-gate 			if (psp->pr_flags & PR_ASLEEP)
17157c478bd9Sstevel@tonic-gate 				mdb_printf(", sleeping in %s system call",
17167c478bd9Sstevel@tonic-gate 				    proc_sysname(psp->pr_lwp.pr_syscall,
17177c478bd9Sstevel@tonic-gate 				    signame, sizeof (signame)));
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 			for (sep = t->t_matched; sep != T_SE_END;
17227c478bd9Sstevel@tonic-gate 			    sep = sep->se_matched) {
17237c478bd9Sstevel@tonic-gate 				mdb_printf("event: %s\n", sep->se_ops->se_info(
17247c478bd9Sstevel@tonic-gate 				    t, sep, mdb_list_next(&sep->se_velist),
17257c478bd9Sstevel@tonic-gate 				    &desc, buf, sizeof (buf)));
17267c478bd9Sstevel@tonic-gate 			}
17277c478bd9Sstevel@tonic-gate 			break;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 		case PS_LOST:
17307c478bd9Sstevel@tonic-gate 			mdb_printf("debugger lost control of process\n");
17317c478bd9Sstevel@tonic-gate 			break;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 		case PS_UNDEAD:
17347c478bd9Sstevel@tonic-gate 			coredump = WIFSIGNALED(pi.pr_wstat) &&
17357c478bd9Sstevel@tonic-gate 			    WCOREDUMP(pi.pr_wstat);
17367c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		case PS_DEAD:
17397c478bd9Sstevel@tonic-gate 			if (cursig == 0 && WIFSIGNALED(pi.pr_wstat))
17407c478bd9Sstevel@tonic-gate 				cursig = WTERMSIG(pi.pr_wstat);
1741350ffdd5SRobert Mustacchi 
1742350ffdd5SRobert Mustacchi 			(void) Pupanic(P, &pru);
1743350ffdd5SRobert Mustacchi 
17447c478bd9Sstevel@tonic-gate 			/*
1745350ffdd5SRobert Mustacchi 			 * Test for upanic first. We can only use pr_wstat == 0
1746350ffdd5SRobert Mustacchi 			 * as a test for gcore if an NT_PRCRED note is present;
1747350ffdd5SRobert Mustacchi 			 * these features were added at the same time in Solaris
1748350ffdd5SRobert Mustacchi 			 * 8.
17497c478bd9Sstevel@tonic-gate 			 */
1750350ffdd5SRobert Mustacchi 			if (pru != NULL) {
1751350ffdd5SRobert Mustacchi 				pt_status_dcmd_upanic(pru);
1752350ffdd5SRobert Mustacchi 				Pupanic_free(pru);
1753350ffdd5SRobert Mustacchi 			} else if (pi.pr_wstat == 0 && Pstate(P) == PS_DEAD &&
17547c478bd9Sstevel@tonic-gate 			    Pcred(P, &cred, 1) == 0) {
17557c478bd9Sstevel@tonic-gate 				mdb_printf("process core file generated "
17567c478bd9Sstevel@tonic-gate 				    "with gcore(1)\n");
17577c478bd9Sstevel@tonic-gate 			} else if (cursig != 0) {
17587c478bd9Sstevel@tonic-gate 				mdb_printf("process terminated by %s (%s)",
17597c478bd9Sstevel@tonic-gate 				    proc_signame(cursig, signame,
17607c478bd9Sstevel@tonic-gate 				    sizeof (signame)), strsignal(cursig));
176148fd701fSKrishnendu Sadhukhan - Sun Microsystems 
176248fd701fSKrishnendu Sadhukhan - Sun Microsystems 				if (sip->si_signo != 0 && SI_FROMUSER(sip) &&
176348fd701fSKrishnendu Sadhukhan - Sun Microsystems 				    sip->si_pid != 0) {
176448fd701fSKrishnendu Sadhukhan - Sun Microsystems 					mdb_printf(", pid=%d uid=%u",
176548fd701fSKrishnendu Sadhukhan - Sun Microsystems 					    (int)sip->si_pid, sip->si_uid);
176648fd701fSKrishnendu Sadhukhan - Sun Microsystems 					if (sip->si_code != 0) {
176748fd701fSKrishnendu Sadhukhan - Sun Microsystems 						mdb_printf(" code=%d",
176848fd701fSKrishnendu Sadhukhan - Sun Microsystems 						    sip->si_code);
176948fd701fSKrishnendu Sadhukhan - Sun Microsystems 					}
177048fd701fSKrishnendu Sadhukhan - Sun Microsystems 				} else {
177148fd701fSKrishnendu Sadhukhan - Sun Microsystems 					switch (sip->si_signo) {
177248fd701fSKrishnendu Sadhukhan - Sun Microsystems 					case SIGILL:
177348fd701fSKrishnendu Sadhukhan - Sun Microsystems 					case SIGTRAP:
177448fd701fSKrishnendu Sadhukhan - Sun Microsystems 					case SIGFPE:
177548fd701fSKrishnendu Sadhukhan - Sun Microsystems 					case SIGSEGV:
177648fd701fSKrishnendu Sadhukhan - Sun Microsystems 					case SIGBUS:
177748fd701fSKrishnendu Sadhukhan - Sun Microsystems 					case SIGEMT:
177848fd701fSKrishnendu Sadhukhan - Sun Microsystems 						mdb_printf(", addr=%p",
177948fd701fSKrishnendu Sadhukhan - Sun Microsystems 						    sip->si_addr);
178048fd701fSKrishnendu Sadhukhan - Sun Microsystems 					default:
178148fd701fSKrishnendu Sadhukhan - Sun Microsystems 						break;
178248fd701fSKrishnendu Sadhukhan - Sun Microsystems 					}
178348fd701fSKrishnendu Sadhukhan - Sun Microsystems 				}
178448fd701fSKrishnendu Sadhukhan - Sun Microsystems 
17857c478bd9Sstevel@tonic-gate 				if (coredump)
17867c478bd9Sstevel@tonic-gate 					mdb_printf(" - core file dumped");
17877c478bd9Sstevel@tonic-gate 				mdb_printf("\n");
17887c478bd9Sstevel@tonic-gate 			} else {
17897c478bd9Sstevel@tonic-gate 				mdb_printf("process terminated with exit "
17907c478bd9Sstevel@tonic-gate 				    "status %d\n", WEXITSTATUS(pi.pr_wstat));
17917c478bd9Sstevel@tonic-gate 			}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 			if (Plookup_by_name(t->t_pshandle, "libc.so",
17947c478bd9Sstevel@tonic-gate 			    "panicstr", &sym) == 0 &&
17957c478bd9Sstevel@tonic-gate 			    Pread(t->t_pshandle, &panicstr, sizeof (panicstr),
17967c478bd9Sstevel@tonic-gate 			    sym.st_value) == sizeof (panicstr) &&
17977c478bd9Sstevel@tonic-gate 			    Pread_string(t->t_pshandle, panicbuf,
1798f68770eaSRobert Mustacchi 			    PANIC_BUFSIZE, panicstr) > 0) {
1799350ffdd5SRobert Mustacchi 				mdb_printf("libc panic message: %s",
18007c478bd9Sstevel@tonic-gate 				    panicbuf);
18017c478bd9Sstevel@tonic-gate 			}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 			break;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 		case PS_IDLE:
18067c478bd9Sstevel@tonic-gate 			mdb_printf("idle\n");
18077c478bd9Sstevel@tonic-gate 			break;
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 		default:
18107c478bd9Sstevel@tonic-gate 			mdb_printf("unknown libproc Pstate: %d\n", Pstate(P));
18117c478bd9Sstevel@tonic-gate 		}
1812f68770eaSRobert Mustacchi 		mdb_free(panicbuf, PANIC_BUFSIZE);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	} else if (pt->p_file != NULL) {
18157c478bd9Sstevel@tonic-gate 		const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 		mdb_printf("debugging %s file (%d-bit)\n",
18187c478bd9Sstevel@tonic-gate 		    ehp->e_type == ET_EXEC ? "executable" : "object",
18197c478bd9Sstevel@tonic-gate 		    ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
18207c478bd9Sstevel@tonic-gate 		mdb_printf("executable file: %s\n", IOP_NAME(pt->p_fio));
18217c478bd9Sstevel@tonic-gate 		mdb_printf("status: idle\n");
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate static int
pt_tls(uintptr_t tid,uint_t flags,int argc,const mdb_arg_t * argv)18287c478bd9Sstevel@tonic-gate pt_tls(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate 	const char *name;
18317c478bd9Sstevel@tonic-gate 	const char *object;
18327c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
18337c478bd9Sstevel@tonic-gate 	mdb_syminfo_t si;
18347c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || argc > 1)
18377c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if (argc == 0) {
18407c478bd9Sstevel@tonic-gate 		psaddr_t b;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 		if (tlsbase(t, tid, PR_LMID_EVERY, MDB_TGT_OBJ_EXEC, &b) != 0) {
18437c478bd9Sstevel@tonic-gate 			mdb_warn("failed to lookup tlsbase for %r", tid);
18447c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 		mdb_printf("%lr\n", b);
18487c478bd9Sstevel@tonic-gate 		mdb_set_dot(b);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	name = argv[0].a_un.a_str;
18547c478bd9Sstevel@tonic-gate 	object = MDB_TGT_OBJ_EVERY;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	if (pt_lookup_by_name_thr(t, object, name, &sym, &si, tid) != 0) {
18577c478bd9Sstevel@tonic-gate 		mdb_warn("failed to lookup %s", name);
18587c478bd9Sstevel@tonic-gate 		return (DCMD_ABORT); /* avoid repeated failure */
18597c478bd9Sstevel@tonic-gate 	}
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(sym.st_info) != STT_TLS && DCMD_HDRSPEC(flags))
18627c478bd9Sstevel@tonic-gate 		mdb_warn("%s does not refer to thread local storage\n", name);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	mdb_printf("%llr\n", sym.st_value);
18657c478bd9Sstevel@tonic-gate 	mdb_set_dot(sym.st_value);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18717c478bd9Sstevel@tonic-gate static int
pt_tmodel(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)18727c478bd9Sstevel@tonic-gate pt_tmodel(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
18757c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
18767c478bd9Sstevel@tonic-gate 	const pt_ptl_ops_t *ptl_ops;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
18797c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	if (strcmp(argv->a_un.a_str, "thread") == 0)
18827c478bd9Sstevel@tonic-gate 		ptl_ops = &proc_tdb_ops;
18837c478bd9Sstevel@tonic-gate 	else if (strcmp(argv->a_un.a_str, "lwp") == 0)
18847c478bd9Sstevel@tonic-gate 		ptl_ops = &proc_lwp_ops;
18857c478bd9Sstevel@tonic-gate 	else
18867c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL && pt->p_ptl_ops != ptl_ops) {
18897c478bd9Sstevel@tonic-gate 		PTL_DTOR(t);
18907c478bd9Sstevel@tonic-gate 		pt->p_tdb_ops = NULL;
18917c478bd9Sstevel@tonic-gate 		pt->p_ptl_ops = &proc_lwp_ops;
18927c478bd9Sstevel@tonic-gate 		pt->p_ptl_hdl = NULL;
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 		if (ptl_ops == &proc_tdb_ops) {
189595d62a61SEdward Pilatowicz 			(void) Pobject_iter(t->t_pshandle, (proc_map_f *)
189695d62a61SEdward Pilatowicz 			    thr_check, t);
18977c478bd9Sstevel@tonic-gate 		}
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
19017c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate static const char *
env_match(const char * cmp,const char * nameval)19057c478bd9Sstevel@tonic-gate env_match(const char *cmp, const char *nameval)
19067c478bd9Sstevel@tonic-gate {
19077c478bd9Sstevel@tonic-gate 	const char *loc;
19087c478bd9Sstevel@tonic-gate 	size_t cmplen = strlen(cmp);
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	loc = strchr(nameval, '=');
19117c478bd9Sstevel@tonic-gate 	if (loc != NULL && (loc - nameval) == cmplen &&
19127c478bd9Sstevel@tonic-gate 	    strncmp(nameval, cmp, cmplen) == 0) {
19137c478bd9Sstevel@tonic-gate 		return (loc + 1);
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	return (NULL);
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19207c478bd9Sstevel@tonic-gate static int
print_env(void * data,struct ps_prochandle * P,uintptr_t addr,const char * nameval)19217c478bd9Sstevel@tonic-gate print_env(void *data, struct ps_prochandle *P, uintptr_t addr,
19227c478bd9Sstevel@tonic-gate     const char *nameval)
19237c478bd9Sstevel@tonic-gate {
19247c478bd9Sstevel@tonic-gate 	const char *value;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	if (nameval == NULL) {
19277c478bd9Sstevel@tonic-gate 		mdb_printf("<0x%p>\n", addr);
19287c478bd9Sstevel@tonic-gate 	} else {
19297c478bd9Sstevel@tonic-gate 		if (data == NULL)
19307c478bd9Sstevel@tonic-gate 			mdb_printf("%s\n", nameval);
19317c478bd9Sstevel@tonic-gate 		else if ((value = env_match(data, nameval)) != NULL)
19327c478bd9Sstevel@tonic-gate 			mdb_printf("%s\n", value);
19337c478bd9Sstevel@tonic-gate 	}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	return (0);
19367c478bd9Sstevel@tonic-gate }
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19397c478bd9Sstevel@tonic-gate static int
pt_getenv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)19407c478bd9Sstevel@tonic-gate pt_getenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
19417c478bd9Sstevel@tonic-gate {
19427c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
19437c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
19447c478bd9Sstevel@tonic-gate 	int i;
19457c478bd9Sstevel@tonic-gate 	uint_t opt_t = 0;
19467c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	i = mdb_getopts(argc, argv,
19497c478bd9Sstevel@tonic-gate 	    't', MDB_OPT_SETBITS, TRUE, &opt_t, NULL);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	argc -= i;
19527c478bd9Sstevel@tonic-gate 	argv += i;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc > 1)
19557c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
19587c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
19597c478bd9Sstevel@tonic-gate 
1960f723faa1Seschrock 	if (opt_t && t->t_pshandle == NULL) {
1961f723faa1Seschrock 		mdb_warn("no process active\n");
1962f723faa1Seschrock 		return (DCMD_ERR);
1963f723faa1Seschrock 	}
1964f723faa1Seschrock 
19657c478bd9Sstevel@tonic-gate 	if (opt_t && (Pstate(t->t_pshandle) == PS_IDLE ||
19667c478bd9Sstevel@tonic-gate 	    Pstate(t->t_pshandle) == PS_UNDEAD)) {
19677c478bd9Sstevel@tonic-gate 		mdb_warn("-t option requires target to be running\n");
19687c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	if (opt_t != 0) {
19727c478bd9Sstevel@tonic-gate 		if (Penv_iter(t->t_pshandle, print_env,
19737c478bd9Sstevel@tonic-gate 		    argc == 0 ? NULL : (void *)argv->a_un.a_str) != 0)
19747c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
19757c478bd9Sstevel@tonic-gate 	} else if (argc == 1) {
19767c478bd9Sstevel@tonic-gate 		if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) == NULL)
19777c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		ASSERT(strchr(mdb_nv_get_cookie(v), '=') != NULL);
19807c478bd9Sstevel@tonic-gate 		mdb_printf("%s\n", strchr(mdb_nv_get_cookie(v), '=') + 1);
19817c478bd9Sstevel@tonic-gate 	} else {
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 		mdb_nv_rewind(&pt->p_env);
19847c478bd9Sstevel@tonic-gate 		while ((v = mdb_nv_advance(&pt->p_env)) != NULL)
19857c478bd9Sstevel@tonic-gate 			mdb_printf("%s\n", mdb_nv_get_cookie(v));
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
19897c478bd9Sstevel@tonic-gate }
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate /*
19927c478bd9Sstevel@tonic-gate  * Function to set a variable in the internal environment, which is used when
19937c478bd9Sstevel@tonic-gate  * creating new processes.  Note that it is possible that 'nameval' can refer to
19947c478bd9Sstevel@tonic-gate  * read-only memory, if mdb calls putenv() on an existing value before calling
19957c478bd9Sstevel@tonic-gate  * this function.  While we should avoid this situation, this function is
19967c478bd9Sstevel@tonic-gate  * designed to be robust in the face of such changes.
19977c478bd9Sstevel@tonic-gate  */
19987c478bd9Sstevel@tonic-gate static void
pt_env_set(pt_data_t * pt,const char * nameval)19997c478bd9Sstevel@tonic-gate pt_env_set(pt_data_t *pt, const char *nameval)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
20027c478bd9Sstevel@tonic-gate 	char *equals, *val;
20037c478bd9Sstevel@tonic-gate 	const char *name;
20047c478bd9Sstevel@tonic-gate 	size_t len;
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	if ((equals = strchr(nameval, '=')) != NULL) {
20077c478bd9Sstevel@tonic-gate 		val = strdup(nameval);
20087c478bd9Sstevel@tonic-gate 		equals = val + (equals - nameval);
20097c478bd9Sstevel@tonic-gate 	} else {
20107c478bd9Sstevel@tonic-gate 		/*
20117c478bd9Sstevel@tonic-gate 		 * nameval doesn't contain an equals character.  Convert this to
20127c478bd9Sstevel@tonic-gate 		 * be 'nameval='.
20137c478bd9Sstevel@tonic-gate 		 */
20147c478bd9Sstevel@tonic-gate 		len = strlen(nameval);
20157c478bd9Sstevel@tonic-gate 		val = mdb_alloc(len + 2, UM_SLEEP);
20167c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(val, len + 2, "%s=", nameval);
20177c478bd9Sstevel@tonic-gate 		equals = val + len;
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	/* temporary truncate the string for lookup/insert */
20217c478bd9Sstevel@tonic-gate 	*equals = '\0';
20227c478bd9Sstevel@tonic-gate 	v = mdb_nv_lookup(&pt->p_env, val);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	if (v != NULL) {
20257c478bd9Sstevel@tonic-gate 		char *old = mdb_nv_get_cookie(v);
20267c478bd9Sstevel@tonic-gate 		mdb_free(old, strlen(old) + 1);
20277c478bd9Sstevel@tonic-gate 		name = mdb_nv_get_name(v);
20287c478bd9Sstevel@tonic-gate 	} else {
20297c478bd9Sstevel@tonic-gate 		/*
20307c478bd9Sstevel@tonic-gate 		 * The environment is created using MDB_NV_EXTNAME, so we must
20317c478bd9Sstevel@tonic-gate 		 * provide external storage for the variable names.
20327c478bd9Sstevel@tonic-gate 		 */
20337c478bd9Sstevel@tonic-gate 		name = strdup(val);
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	*equals = '=';
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	(void) mdb_nv_insert(&pt->p_env, name, NULL, (uintptr_t)val,
20397c478bd9Sstevel@tonic-gate 	    MDB_NV_EXTNAME);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	*equals = '=';
20427c478bd9Sstevel@tonic-gate }
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate /*
20457c478bd9Sstevel@tonic-gate  * Clears the internal environment.
20467c478bd9Sstevel@tonic-gate  */
20477c478bd9Sstevel@tonic-gate static void
pt_env_clear(pt_data_t * pt)20487c478bd9Sstevel@tonic-gate pt_env_clear(pt_data_t *pt)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
20517c478bd9Sstevel@tonic-gate 	char *val, *name;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	mdb_nv_rewind(&pt->p_env);
20547c478bd9Sstevel@tonic-gate 	while ((v = mdb_nv_advance(&pt->p_env)) != NULL) {
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 		name = (char *)mdb_nv_get_name(v);
20577c478bd9Sstevel@tonic-gate 		val = mdb_nv_get_cookie(v);
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 		mdb_nv_remove(&pt->p_env, v);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 		mdb_free(name, strlen(name) + 1);
20627c478bd9Sstevel@tonic-gate 		mdb_free(val, strlen(val) + 1);
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20677c478bd9Sstevel@tonic-gate static int
pt_setenv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)20687c478bd9Sstevel@tonic-gate pt_setenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
20697c478bd9Sstevel@tonic-gate {
20707c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
20717c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
20727c478bd9Sstevel@tonic-gate 	char *nameval;
20737c478bd9Sstevel@tonic-gate 	size_t len;
20747c478bd9Sstevel@tonic-gate 	int alloc;
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc == 0 || argc > 2)
20777c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	if ((argc > 0 && argv[0].a_type != MDB_TYPE_STRING) ||
20807c478bd9Sstevel@tonic-gate 	    (argc > 1 && argv[1].a_type != MDB_TYPE_STRING))
20817c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
20827c478bd9Sstevel@tonic-gate 
2083f723faa1Seschrock 	if (t->t_pshandle == NULL) {
2084f723faa1Seschrock 		mdb_warn("no process active\n");
2085f723faa1Seschrock 		return (DCMD_ERR);
2086f723faa1Seschrock 	}
2087f723faa1Seschrock 
20887c478bd9Sstevel@tonic-gate 	/*
20897c478bd9Sstevel@tonic-gate 	 * If the process is in some sort of running state, warn the user that
20907c478bd9Sstevel@tonic-gate 	 * changes won't immediately take effect.
20917c478bd9Sstevel@tonic-gate 	 */
20927c478bd9Sstevel@tonic-gate 	if (Pstate(t->t_pshandle) == PS_RUN ||
20937c478bd9Sstevel@tonic-gate 	    Pstate(t->t_pshandle) == PS_STOP) {
20947c478bd9Sstevel@tonic-gate 		mdb_warn("warning: changes will not take effect until process"
20957c478bd9Sstevel@tonic-gate 		    " is restarted\n");
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * We allow two forms of operation.  The first is the usual "name=value"
21007c478bd9Sstevel@tonic-gate 	 * parameter.  We also allow the user to specify two arguments, where
21017c478bd9Sstevel@tonic-gate 	 * the first is the name of the variable, and the second is the value.
21027c478bd9Sstevel@tonic-gate 	 */
21037c478bd9Sstevel@tonic-gate 	alloc = 0;
21047c478bd9Sstevel@tonic-gate 	if (argc == 1) {
21057c478bd9Sstevel@tonic-gate 		nameval = (char *)argv->a_un.a_str;
21067c478bd9Sstevel@tonic-gate 	} else {
21077c478bd9Sstevel@tonic-gate 		len = strlen(argv[0].a_un.a_str) +
21087c478bd9Sstevel@tonic-gate 		    strlen(argv[1].a_un.a_str) + 2;
21097c478bd9Sstevel@tonic-gate 		nameval = mdb_alloc(len, UM_SLEEP);
21107c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(nameval, len, "%s=%s", argv[0].a_un.a_str,
21117c478bd9Sstevel@tonic-gate 		    argv[1].a_un.a_str);
21127c478bd9Sstevel@tonic-gate 		alloc = 1;
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	pt_env_set(pt, nameval);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	if (alloc)
21187c478bd9Sstevel@tonic-gate 		mdb_free(nameval, strlen(nameval) + 1);
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
21217c478bd9Sstevel@tonic-gate }
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21247c478bd9Sstevel@tonic-gate static int
pt_unsetenv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)21257c478bd9Sstevel@tonic-gate pt_unsetenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
21267c478bd9Sstevel@tonic-gate {
21277c478bd9Sstevel@tonic-gate 	mdb_tgt_t *t = mdb.m_target;
21287c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
21297c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
21307c478bd9Sstevel@tonic-gate 	char *value, *name;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc > 1)
21337c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
21367c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
21377c478bd9Sstevel@tonic-gate 
2138f723faa1Seschrock 	if (t->t_pshandle == NULL) {
2139f723faa1Seschrock 		mdb_warn("no process active\n");
2140f723faa1Seschrock 		return (DCMD_ERR);
2141f723faa1Seschrock 	}
2142f723faa1Seschrock 
21437c478bd9Sstevel@tonic-gate 	/*
21447c478bd9Sstevel@tonic-gate 	 * If the process is in some sort of running state, warn the user that
21457c478bd9Sstevel@tonic-gate 	 * changes won't immediately take effect.
21467c478bd9Sstevel@tonic-gate 	 */
21477c478bd9Sstevel@tonic-gate 	if (Pstate(t->t_pshandle) == PS_RUN ||
21487c478bd9Sstevel@tonic-gate 	    Pstate(t->t_pshandle) == PS_STOP) {
21497c478bd9Sstevel@tonic-gate 		mdb_warn("warning: changes will not take effect until process"
21507c478bd9Sstevel@tonic-gate 		    " is restarted\n");
21517c478bd9Sstevel@tonic-gate 	}
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	if (argc == 0) {
21547c478bd9Sstevel@tonic-gate 		pt_env_clear(pt);
21557c478bd9Sstevel@tonic-gate 	} else {
21567c478bd9Sstevel@tonic-gate 		if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) != NULL) {
21577c478bd9Sstevel@tonic-gate 			name = (char *)mdb_nv_get_name(v);
21587c478bd9Sstevel@tonic-gate 			value = mdb_nv_get_cookie(v);
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 			mdb_nv_remove(&pt->p_env, v);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 			mdb_free(name, strlen(name) + 1);
21637c478bd9Sstevel@tonic-gate 			mdb_free(value, strlen(value) + 1);
21647c478bd9Sstevel@tonic-gate 		}
21657c478bd9Sstevel@tonic-gate 	}
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
2170b77e74e4Sjohnlev void
getenv_help(void)2171b77e74e4Sjohnlev getenv_help(void)
2172b77e74e4Sjohnlev {
2173b77e74e4Sjohnlev 	mdb_printf("-t  show current process environment"
2174b77e74e4Sjohnlev 	    " instead of initial environment.\n");
2175b77e74e4Sjohnlev }
2176b77e74e4Sjohnlev 
21777c478bd9Sstevel@tonic-gate static const mdb_dcmd_t pt_dcmds[] = {
21787c478bd9Sstevel@tonic-gate 	{ "$c", "?[cnt]", "print stack backtrace", pt_stack },
21797c478bd9Sstevel@tonic-gate 	{ "$C", "?[cnt]", "print stack backtrace", pt_stackv },
21807c478bd9Sstevel@tonic-gate 	{ "$i", NULL, "print signals that are ignored", pt_ignored },
21817c478bd9Sstevel@tonic-gate 	{ "$l", NULL, "print the representative thread's lwp id", pt_lwpid },
21827c478bd9Sstevel@tonic-gate 	{ "$L", NULL, "print list of the active lwp ids", pt_lwpids },
21838f88a51fSJoshua M. Clulow 	{ "$r", "?[-u]", "print general-purpose registers", pt_regs },
21847c478bd9Sstevel@tonic-gate 	{ "$x", "?", "print floating point registers", pt_fpregs },
21857c478bd9Sstevel@tonic-gate 	{ "$X", "?", "print floating point registers", pt_fpregs },
21867c478bd9Sstevel@tonic-gate 	{ "$y", "?", "print floating point registers", pt_fpregs },
21877c478bd9Sstevel@tonic-gate 	{ "$Y", "?", "print floating point registers", pt_fpregs },
21887c478bd9Sstevel@tonic-gate 	{ "$?", "?", "print status and registers", pt_regstatus },
21897c478bd9Sstevel@tonic-gate 	{ ":A", "?[core|pid]", "attach to process or core file", pt_attach },
21907c478bd9Sstevel@tonic-gate 	{ ":i", ":", "ignore signal (delete all matching events)", pt_ignore },
21917c478bd9Sstevel@tonic-gate 	{ ":k", NULL, "forcibly kill and release target", pt_kill },
21927c478bd9Sstevel@tonic-gate 	{ ":R", "[-a]", "release the previously attached process", pt_detach },
21937c478bd9Sstevel@tonic-gate 	{ "attach", "?[core|pid]",
21947c478bd9Sstevel@tonic-gate 	    "attach to process or core file", pt_attach },
21957c478bd9Sstevel@tonic-gate 	{ "findstack", ":[-v]", "find user thread stack", pt_findstack },
21967c478bd9Sstevel@tonic-gate 	{ "gcore", "[-o prefix] [-c content]",
21977c478bd9Sstevel@tonic-gate 	    "produce a core file for the attached process", pt_gcore },
21987c478bd9Sstevel@tonic-gate 	{ "getenv", "[-t] [name]", "display an environment variable",
2199b77e74e4Sjohnlev 		pt_getenv, getenv_help },
22007c478bd9Sstevel@tonic-gate 	{ "kill", NULL, "forcibly kill and release target", pt_kill },
22017c478bd9Sstevel@tonic-gate 	{ "release", "[-a]",
22027c478bd9Sstevel@tonic-gate 	    "release the previously attached process", pt_detach },
22038f88a51fSJoshua M. Clulow 	{ "regs", "?[-u]", "print general-purpose registers", pt_regs },
22047c478bd9Sstevel@tonic-gate 	{ "fpregs", "?[-dqs]", "print floating point registers", pt_fpregs },
22057c478bd9Sstevel@tonic-gate 	{ "setenv", "name=value", "set an environment variable", pt_setenv },
22067c478bd9Sstevel@tonic-gate 	{ "stack", "?[cnt]", "print stack backtrace", pt_stack },
22077c478bd9Sstevel@tonic-gate 	{ "stackregs", "?", "print stack backtrace and registers", pt_stackr },
22087c478bd9Sstevel@tonic-gate 	{ "status", NULL, "print summary of current target", pt_status_dcmd },
22097c478bd9Sstevel@tonic-gate 	{ "tls", ":symbol",
22107c478bd9Sstevel@tonic-gate 	    "lookup TLS data in the context of a given thread", pt_tls },
22117c478bd9Sstevel@tonic-gate 	{ "tmodel", "{thread|lwp}", NULL, pt_tmodel },
22127c478bd9Sstevel@tonic-gate 	{ "unsetenv", "[name]", "clear an environment variable", pt_unsetenv },
22137c478bd9Sstevel@tonic-gate 	{ NULL }
22147c478bd9Sstevel@tonic-gate };
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate static void
pt_thr_walk_fini(mdb_walk_state_t * wsp)22177c478bd9Sstevel@tonic-gate pt_thr_walk_fini(mdb_walk_state_t *wsp)
22187c478bd9Sstevel@tonic-gate {
22197c478bd9Sstevel@tonic-gate 	mdb_addrvec_destroy(wsp->walk_data);
22207c478bd9Sstevel@tonic-gate 	mdb_free(wsp->walk_data, sizeof (mdb_addrvec_t));
22217c478bd9Sstevel@tonic-gate }
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate static int
pt_thr_walk_init(mdb_walk_state_t * wsp)22247c478bd9Sstevel@tonic-gate pt_thr_walk_init(mdb_walk_state_t *wsp)
22257c478bd9Sstevel@tonic-gate {
22267c478bd9Sstevel@tonic-gate 	wsp->walk_data = mdb_zalloc(sizeof (mdb_addrvec_t), UM_SLEEP);
22277c478bd9Sstevel@tonic-gate 	mdb_addrvec_create(wsp->walk_data);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	if (PTL_ITER(mdb.m_target, wsp->walk_data) == -1) {
22307c478bd9Sstevel@tonic-gate 		mdb_warn("failed to iterate over threads");
22317c478bd9Sstevel@tonic-gate 		pt_thr_walk_fini(wsp);
22327c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate static int
pt_thr_walk_step(mdb_walk_state_t * wsp)22397c478bd9Sstevel@tonic-gate pt_thr_walk_step(mdb_walk_state_t *wsp)
22407c478bd9Sstevel@tonic-gate {
22417c478bd9Sstevel@tonic-gate 	if (mdb_addrvec_length(wsp->walk_data) != 0) {
22427c478bd9Sstevel@tonic-gate 		return (wsp->walk_callback(mdb_addrvec_shift(wsp->walk_data),
22437c478bd9Sstevel@tonic-gate 		    NULL, wsp->walk_cbdata));
22447c478bd9Sstevel@tonic-gate 	}
22457c478bd9Sstevel@tonic-gate 	return (WALK_DONE);
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate static const mdb_walker_t pt_walkers[] = {
22497c478bd9Sstevel@tonic-gate 	{ "thread", "walk list of valid thread identifiers",
22507c478bd9Sstevel@tonic-gate 	    pt_thr_walk_init, pt_thr_walk_step, pt_thr_walk_fini },
22517c478bd9Sstevel@tonic-gate 	{ NULL }
22527c478bd9Sstevel@tonic-gate };
22537c478bd9Sstevel@tonic-gate 
225428ea8e03SBryan Cantrill static int
pt_agent_check(boolean_t * agent,const lwpstatus_t * psp)225528ea8e03SBryan Cantrill pt_agent_check(boolean_t *agent, const lwpstatus_t *psp)
225628ea8e03SBryan Cantrill {
225728ea8e03SBryan Cantrill 	if (psp->pr_flags & PR_AGENT)
225828ea8e03SBryan Cantrill 		*agent = B_TRUE;
225928ea8e03SBryan Cantrill 
226028ea8e03SBryan Cantrill 	return (0);
226128ea8e03SBryan Cantrill }
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate static void
pt_activate_common(mdb_tgt_t * t)22647c478bd9Sstevel@tonic-gate pt_activate_common(mdb_tgt_t *t)
22657c478bd9Sstevel@tonic-gate {
22667c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
226728ea8e03SBryan Cantrill 	boolean_t hasagent = B_FALSE;
22687c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	/*
22717c478bd9Sstevel@tonic-gate 	 * If we have a libproc handle and AT_BASE is set, the process or core
22727c478bd9Sstevel@tonic-gate 	 * is dynamically linked.  We call Prd_agent() to force libproc to
22737c478bd9Sstevel@tonic-gate 	 * try to initialize librtld_db, and issue a warning if that fails.
22747c478bd9Sstevel@tonic-gate 	 */
22757c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL && Pgetauxval(t->t_pshandle,
22767c478bd9Sstevel@tonic-gate 	    AT_BASE) != -1L && Prd_agent(t->t_pshandle) == NULL) {
22777c478bd9Sstevel@tonic-gate 		mdb_warn("warning: librtld_db failed to initialize; shared "
22787c478bd9Sstevel@tonic-gate 		    "library information will not be available\n");
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 
228128ea8e03SBryan Cantrill 	if (t->t_pshandle != NULL) {
228228ea8e03SBryan Cantrill 		(void) Plwp_iter(t->t_pshandle,
228328ea8e03SBryan Cantrill 		    (proc_lwp_f *)pt_agent_check, &hasagent);
228428ea8e03SBryan Cantrill 	}
228528ea8e03SBryan Cantrill 
228628ea8e03SBryan Cantrill 	if (hasagent) {
228728ea8e03SBryan Cantrill 		mdb_warn("agent lwp detected; forcing "
228828ea8e03SBryan Cantrill 		    "lwp thread model (use ::tmodel to change)\n");
228928ea8e03SBryan Cantrill 	} else if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
22907c478bd9Sstevel@tonic-gate 		/*
229128ea8e03SBryan Cantrill 		 * If we have a libproc handle and we do not have an agent LWP,
229228ea8e03SBryan Cantrill 		 * look for the correct thread debugging library.  (If we have
229328ea8e03SBryan Cantrill 		 * an agent LWP, we leave the model as the raw LWP model to
229428ea8e03SBryan Cantrill 		 * allow the agent LWP to be visible to the debugger.)
22957c478bd9Sstevel@tonic-gate 		 */
229695d62a61SEdward Pilatowicz 		(void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
229728ea8e03SBryan Cantrill 	}
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	/*
23007c478bd9Sstevel@tonic-gate 	 * If there's a global object named '_mdb_abort_info', assuming we're
23017c478bd9Sstevel@tonic-gate 	 * debugging mdb itself and load the developer support module.
23027c478bd9Sstevel@tonic-gate 	 */
23037c478bd9Sstevel@tonic-gate 	if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, "_mdb_abort_info",
23047c478bd9Sstevel@tonic-gate 	    &sym, NULL) == 0 && GELF_ST_TYPE(sym.st_info) == STT_OBJECT) {
23057c478bd9Sstevel@tonic-gate 		if (mdb_module_load("mdb_ds", MDB_MOD_SILENT) < 0)
23067c478bd9Sstevel@tonic-gate 			mdb_warn("warning: failed to load developer support\n");
23077c478bd9Sstevel@tonic-gate 	}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	mdb_tgt_elf_export(pt->p_file);
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate static void
pt_activate(mdb_tgt_t * t)23137c478bd9Sstevel@tonic-gate pt_activate(mdb_tgt_t *t)
23147c478bd9Sstevel@tonic-gate {
23150c1b95beSRichard Lowe 	static const mdb_nv_disc_t reg_disc = {
23160c1b95beSRichard Lowe 		.disc_set = reg_disc_set,
23170c1b95beSRichard Lowe 		.disc_get = reg_disc_get
23180c1b95beSRichard Lowe 	};
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
23217c478bd9Sstevel@tonic-gate 	struct utsname u1, u2;
23227c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
23237c478bd9Sstevel@tonic-gate 	core_content_t content;
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	if (t->t_pshandle) {
23267c478bd9Sstevel@tonic-gate 		mdb_prop_postmortem = (Pstate(t->t_pshandle) == PS_DEAD);
23277c478bd9Sstevel@tonic-gate 		mdb_prop_kernel = FALSE;
23287c478bd9Sstevel@tonic-gate 	} else
23297c478bd9Sstevel@tonic-gate 		mdb_prop_kernel = mdb_prop_postmortem = FALSE;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	mdb_prop_datamodel = MDB_TGT_MODEL_NATIVE;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	/*
23347c478bd9Sstevel@tonic-gate 	 * If we're examining a core file that doesn't contain program text,
23357c478bd9Sstevel@tonic-gate 	 * and uname(2) doesn't match the NT_UTSNAME note recorded in the
23367c478bd9Sstevel@tonic-gate 	 * core file, issue a warning.
23377c478bd9Sstevel@tonic-gate 	 */
23387c478bd9Sstevel@tonic-gate 	if (mdb_prop_postmortem == TRUE &&
23397c478bd9Sstevel@tonic-gate 	    ((content = Pcontent(t->t_pshandle)) == CC_CONTENT_INVALID ||
23407c478bd9Sstevel@tonic-gate 	    !(content & CC_CONTENT_TEXT)) &&
23417c478bd9Sstevel@tonic-gate 	    uname(&u1) >= 0 && Puname(t->t_pshandle, &u2) == 0 &&
23427c478bd9Sstevel@tonic-gate 	    (strcmp(u1.release, u2.release) != 0 ||
23437c478bd9Sstevel@tonic-gate 	    strcmp(u1.version, u2.version) != 0)) {
23447c478bd9Sstevel@tonic-gate 		mdb_warn("warning: core file is from %s %s %s; shared text "
23457c478bd9Sstevel@tonic-gate 		    "mappings may not match installed libraries\n",
23467c478bd9Sstevel@tonic-gate 		    u2.sysname, u2.release, u2.version);
23477c478bd9Sstevel@tonic-gate 	}
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	/*
23507c478bd9Sstevel@tonic-gate 	 * Perform the common initialization tasks -- these are shared with
23517c478bd9Sstevel@tonic-gate 	 * the pt_exec() and pt_run() subroutines.
23527c478bd9Sstevel@tonic-gate 	 */
23537c478bd9Sstevel@tonic-gate 	pt_activate_common(t);
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_register_dcmds(t, &pt_dcmds[0], MDB_MOD_FORCE);
23567c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_register_walkers(t, &pt_walkers[0], MDB_MOD_FORCE);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	/*
23597c478bd9Sstevel@tonic-gate 	 * Iterate through our register description list and export
23607c478bd9Sstevel@tonic-gate 	 * each register as a named variable.
23617c478bd9Sstevel@tonic-gate 	 */
23627c478bd9Sstevel@tonic-gate 	mdb_nv_rewind(&pt->p_regs);
23637c478bd9Sstevel@tonic-gate 	while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
23647c478bd9Sstevel@tonic-gate 		ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 		if (!(rd_flags & MDB_TGT_R_EXPORT))
23677c478bd9Sstevel@tonic-gate 			continue; /* Don't export register as a variable */
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 		(void) mdb_nv_insert(&mdb.m_nv, mdb_nv_get_name(v), &reg_disc,
23707c478bd9Sstevel@tonic-gate 		    (uintptr_t)t, MDB_NV_PERSIST);
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate static void
pt_deactivate(mdb_tgt_t * t)23757c478bd9Sstevel@tonic-gate pt_deactivate(mdb_tgt_t *t)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
23787c478bd9Sstevel@tonic-gate 	const mdb_dcmd_t *dcp;
23797c478bd9Sstevel@tonic-gate 	const mdb_walker_t *wp;
23807c478bd9Sstevel@tonic-gate 	mdb_var_t *v, *w;
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	mdb_nv_rewind(&pt->p_regs);
23837c478bd9Sstevel@tonic-gate 	while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
23847c478bd9Sstevel@tonic-gate 		ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 		if (!(rd_flags & MDB_TGT_R_EXPORT))
23877c478bd9Sstevel@tonic-gate 			continue; /* Didn't export register as a variable */
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 		if (w = mdb_nv_lookup(&mdb.m_nv, mdb_nv_get_name(v))) {
23907c478bd9Sstevel@tonic-gate 			w->v_flags &= ~MDB_NV_PERSIST;
23917c478bd9Sstevel@tonic-gate 			mdb_nv_remove(&mdb.m_nv, w);
23927c478bd9Sstevel@tonic-gate 		}
23937c478bd9Sstevel@tonic-gate 	}
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	for (wp = &pt_walkers[0]; wp->walk_name != NULL; wp++) {
23967c478bd9Sstevel@tonic-gate 		if (mdb_module_remove_walker(t->t_module, wp->walk_name) == -1)
23977c478bd9Sstevel@tonic-gate 			warn("failed to remove walk %s", wp->walk_name);
23987c478bd9Sstevel@tonic-gate 	}
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	for (dcp = &pt_dcmds[0]; dcp->dc_name != NULL; dcp++) {
24017c478bd9Sstevel@tonic-gate 		if (mdb_module_remove_dcmd(t->t_module, dcp->dc_name) == -1)
24027c478bd9Sstevel@tonic-gate 			warn("failed to remove dcmd %s", dcp->dc_name);
24037c478bd9Sstevel@tonic-gate 	}
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	mdb_prop_postmortem = FALSE;
24067c478bd9Sstevel@tonic-gate 	mdb_prop_kernel = FALSE;
24077c478bd9Sstevel@tonic-gate 	mdb_prop_datamodel = MDB_TGT_MODEL_UNKNOWN;
24087c478bd9Sstevel@tonic-gate }
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate static void
pt_periodic(mdb_tgt_t * t)24117c478bd9Sstevel@tonic-gate pt_periodic(mdb_tgt_t *t)
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	if (pt->p_rdstate == PT_RD_CONSIST) {
24167c478bd9Sstevel@tonic-gate 		if (t->t_pshandle != NULL && Pstate(t->t_pshandle) < PS_LOST &&
24177c478bd9Sstevel@tonic-gate 		    !(mdb.m_flags & MDB_FL_NOMODS)) {
24187c478bd9Sstevel@tonic-gate 			mdb_printf("%s: You've got symbols!\n", mdb.m_pname);
24197c478bd9Sstevel@tonic-gate 			mdb_module_load_all(0);
24207c478bd9Sstevel@tonic-gate 		}
24217c478bd9Sstevel@tonic-gate 		pt->p_rdstate = PT_RD_NONE;
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate }
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate static void
pt_destroy(mdb_tgt_t * t)24267c478bd9Sstevel@tonic-gate pt_destroy(mdb_tgt_t *t)
24277c478bd9Sstevel@tonic-gate {
24287c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	if (pt->p_idlehandle != NULL && pt->p_idlehandle != t->t_pshandle)
24317c478bd9Sstevel@tonic-gate 		Prelease(pt->p_idlehandle, 0);
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
24347c478bd9Sstevel@tonic-gate 		PTL_DTOR(t);
24357c478bd9Sstevel@tonic-gate 		pt_release_parents(t);
24367c478bd9Sstevel@tonic-gate 		pt_pre_detach(t, TRUE);
24377c478bd9Sstevel@tonic-gate 		Prelease(t->t_pshandle, pt->p_rflags);
24387c478bd9Sstevel@tonic-gate 	}
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
24417c478bd9Sstevel@tonic-gate 	pt_close_aout(t);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if (pt->p_aout_fio != NULL)
24447c478bd9Sstevel@tonic-gate 		mdb_io_rele(pt->p_aout_fio);
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	pt_env_clear(pt);
24477c478bd9Sstevel@tonic-gate 	mdb_nv_destroy(&pt->p_env);
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	mdb_nv_destroy(&pt->p_regs);
24507c478bd9Sstevel@tonic-gate 	mdb_free(pt, sizeof (pt_data_t));
24517c478bd9Sstevel@tonic-gate }
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24547c478bd9Sstevel@tonic-gate static const char *
pt_name(mdb_tgt_t * t)24557c478bd9Sstevel@tonic-gate pt_name(mdb_tgt_t *t)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate 	return ("proc");
24587c478bd9Sstevel@tonic-gate }
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate static const char *
pt_platform(mdb_tgt_t * t)24617c478bd9Sstevel@tonic-gate pt_platform(mdb_tgt_t *t)
24627c478bd9Sstevel@tonic-gate {
24637c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL &&
24667c478bd9Sstevel@tonic-gate 	    Pplatform(t->t_pshandle, pt->p_platform, MAXNAMELEN) != NULL)
24677c478bd9Sstevel@tonic-gate 		return (pt->p_platform);
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 	return (mdb_conf_platform());
24707c478bd9Sstevel@tonic-gate }
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate static int
pt_uname(mdb_tgt_t * t,struct utsname * utsp)24737c478bd9Sstevel@tonic-gate pt_uname(mdb_tgt_t *t, struct utsname *utsp)
24747c478bd9Sstevel@tonic-gate {
24757c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL)
24767c478bd9Sstevel@tonic-gate 		return (Puname(t->t_pshandle, utsp));
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	return (uname(utsp) >= 0 ? 0 : -1);
24797c478bd9Sstevel@tonic-gate }
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate static int
pt_dmodel(mdb_tgt_t * t)24827c478bd9Sstevel@tonic-gate pt_dmodel(mdb_tgt_t *t)
24837c478bd9Sstevel@tonic-gate {
24847c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
24857c478bd9Sstevel@tonic-gate 		return (MDB_TGT_MODEL_NATIVE);
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	switch (Pstatus(t->t_pshandle)->pr_dmodel) {
24887c478bd9Sstevel@tonic-gate 	case PR_MODEL_ILP32:
24897c478bd9Sstevel@tonic-gate 		return (MDB_TGT_MODEL_ILP32);
24907c478bd9Sstevel@tonic-gate 	case PR_MODEL_LP64:
24917c478bd9Sstevel@tonic-gate 		return (MDB_TGT_MODEL_LP64);
24927c478bd9Sstevel@tonic-gate 	}
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	return (MDB_TGT_MODEL_UNKNOWN);
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate static ssize_t
pt_vread(mdb_tgt_t * t,void * buf,size_t nbytes,uintptr_t addr)24987c478bd9Sstevel@tonic-gate pt_vread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
24997c478bd9Sstevel@tonic-gate {
25007c478bd9Sstevel@tonic-gate 	ssize_t n;
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	/*
25037c478bd9Sstevel@tonic-gate 	 * If no handle is open yet, reads from virtual addresses are
25047c478bd9Sstevel@tonic-gate 	 * allowed to succeed but return zero-filled memory.
25057c478bd9Sstevel@tonic-gate 	 */
25067c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
25077c478bd9Sstevel@tonic-gate 		bzero(buf, nbytes);
25087c478bd9Sstevel@tonic-gate 		return (nbytes);
25097c478bd9Sstevel@tonic-gate 	}
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	if ((n = Pread(t->t_pshandle, buf, nbytes, addr)) <= 0)
25127c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOMAP));
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	return (n);
25157c478bd9Sstevel@tonic-gate }
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate static ssize_t
pt_vwrite(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)25187c478bd9Sstevel@tonic-gate pt_vwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
25197c478bd9Sstevel@tonic-gate {
25207c478bd9Sstevel@tonic-gate 	ssize_t n;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	/*
25237c478bd9Sstevel@tonic-gate 	 * If no handle is open yet, writes to virtual addresses are
25247c478bd9Sstevel@tonic-gate 	 * allowed to succeed but do not actually modify anything.
25257c478bd9Sstevel@tonic-gate 	 */
25267c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
25277c478bd9Sstevel@tonic-gate 		return (nbytes);
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	n = Pwrite(t->t_pshandle, buf, nbytes, addr);
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	if (n == -1 && errno == EIO)
25327c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOMAP));
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 	return (n);
25357c478bd9Sstevel@tonic-gate }
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate static ssize_t
pt_fread(mdb_tgt_t * t,void * buf,size_t nbytes,uintptr_t addr)25387c478bd9Sstevel@tonic-gate pt_fread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
25397c478bd9Sstevel@tonic-gate {
25407c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	if (pt->p_file != NULL) {
25437c478bd9Sstevel@tonic-gate 		return (mdb_gelf_rw(pt->p_file, buf, nbytes, addr,
25447c478bd9Sstevel@tonic-gate 		    IOPF_READ(pt->p_fio), GIO_READ));
25457c478bd9Sstevel@tonic-gate 	}
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	bzero(buf, nbytes);
25487c478bd9Sstevel@tonic-gate 	return (nbytes);
25497c478bd9Sstevel@tonic-gate }
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate static ssize_t
pt_fwrite(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)25527c478bd9Sstevel@tonic-gate pt_fwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
25537c478bd9Sstevel@tonic-gate {
25547c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	if (pt->p_file != NULL) {
25577c478bd9Sstevel@tonic-gate 		return (mdb_gelf_rw(pt->p_file, (void *)buf, nbytes, addr,
25587c478bd9Sstevel@tonic-gate 		    IOPF_WRITE(pt->p_fio), GIO_WRITE));
25597c478bd9Sstevel@tonic-gate 	}
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	return (nbytes);
25627c478bd9Sstevel@tonic-gate }
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate static const char *
pt_resolve_lmid(const char * object,Lmid_t * lmidp)25657c478bd9Sstevel@tonic-gate pt_resolve_lmid(const char *object, Lmid_t *lmidp)
25667c478bd9Sstevel@tonic-gate {
25677c478bd9Sstevel@tonic-gate 	Lmid_t lmid = PR_LMID_EVERY;
25687c478bd9Sstevel@tonic-gate 	const char *p;
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	if (object == MDB_TGT_OBJ_EVERY || object == MDB_TGT_OBJ_EXEC)
25717c478bd9Sstevel@tonic-gate 		lmid = LM_ID_BASE; /* restrict scope to a.out's link map */
25727c478bd9Sstevel@tonic-gate 	else if (object != MDB_TGT_OBJ_RTLD && strncmp(object, "LM", 2) == 0 &&
25737c478bd9Sstevel@tonic-gate 	    (p = strchr(object, '`')) != NULL) {
25747c478bd9Sstevel@tonic-gate 		object += 2;	/* skip past initial "LM" prefix */
25757c478bd9Sstevel@tonic-gate 		lmid = strntoul(object, (size_t)(p - object), mdb.m_radix);
25767c478bd9Sstevel@tonic-gate 		object = p + 1;	/* skip past link map specifier */
25777c478bd9Sstevel@tonic-gate 	}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	*lmidp = lmid;
25807c478bd9Sstevel@tonic-gate 	return (object);
25817c478bd9Sstevel@tonic-gate }
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate static int
tlsbase(mdb_tgt_t * t,mdb_tgt_tid_t tid,Lmid_t lmid,const char * object,psaddr_t * basep)25847c478bd9Sstevel@tonic-gate tlsbase(mdb_tgt_t *t, mdb_tgt_tid_t tid, Lmid_t lmid, const char *object,
25857c478bd9Sstevel@tonic-gate     psaddr_t *basep)
25867c478bd9Sstevel@tonic-gate {
25877c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
25887c478bd9Sstevel@tonic-gate 	const rd_loadobj_t *loadobjp;
25897c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
25907c478bd9Sstevel@tonic-gate 	td_err_e err;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	if (object == MDB_TGT_OBJ_EVERY)
25937c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE)
25967c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	if (pt->p_tdb_ops == NULL)
25997c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TDB));
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_ta_map_id2thr(pt->p_ptl_hdl, tid, &th);
26027c478bd9Sstevel@tonic-gate 	if (err != TD_OK)
26037c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	/*
26067c478bd9Sstevel@tonic-gate 	 * If this fails, rtld_db has failed to initialize properly.
26077c478bd9Sstevel@tonic-gate 	 */
26087c478bd9Sstevel@tonic-gate 	if ((loadobjp = Plmid_to_loadobj(t->t_pshandle, lmid, object)) == NULL)
26097c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NORTLD));
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	/*
26127c478bd9Sstevel@tonic-gate 	 * This will fail if the TLS block has not been allocated for the
26137c478bd9Sstevel@tonic-gate 	 * object that contains the TLS symbol in question.
26147c478bd9Sstevel@tonic-gate 	 */
26157c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_thr_tlsbase(&th, loadobjp->rl_tlsmodid, basep);
26167c478bd9Sstevel@tonic-gate 	if (err != TD_OK)
26177c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	return (0);
26207c478bd9Sstevel@tonic-gate }
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate typedef struct {
26237c478bd9Sstevel@tonic-gate 	mdb_tgt_t	*pl_tgt;
26247c478bd9Sstevel@tonic-gate 	const char	*pl_name;
26257c478bd9Sstevel@tonic-gate 	Lmid_t		pl_lmid;
26267c478bd9Sstevel@tonic-gate 	GElf_Sym	*pl_symp;
26277c478bd9Sstevel@tonic-gate 	mdb_syminfo_t	*pl_sip;
26287c478bd9Sstevel@tonic-gate 	mdb_tgt_tid_t	pl_tid;
26297c478bd9Sstevel@tonic-gate 	mdb_bool_t	pl_found;
26307c478bd9Sstevel@tonic-gate } pt_lookup_t;
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26337c478bd9Sstevel@tonic-gate static int
pt_lookup_cb(void * data,const prmap_t * pmp,const char * object)26347c478bd9Sstevel@tonic-gate pt_lookup_cb(void *data, const prmap_t *pmp, const char *object)
26357c478bd9Sstevel@tonic-gate {
26367c478bd9Sstevel@tonic-gate 	pt_lookup_t *plp = data;
26377c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = plp->pl_tgt->t_pshandle;
26387c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
26397c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	if (Pxlookup_by_name(P, plp->pl_lmid, object, plp->pl_name, &sym,
26427c478bd9Sstevel@tonic-gate 	    &si) != 0)
26437c478bd9Sstevel@tonic-gate 		return (0);
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	/*
26467c478bd9Sstevel@tonic-gate 	 * If we encounter a match with SHN_UNDEF, keep looking for a
26477c478bd9Sstevel@tonic-gate 	 * better match. Return the first match with SHN_UNDEF set if no
26487c478bd9Sstevel@tonic-gate 	 * better match is found.
26497c478bd9Sstevel@tonic-gate 	 */
26507c478bd9Sstevel@tonic-gate 	if (sym.st_shndx == SHN_UNDEF) {
26517c478bd9Sstevel@tonic-gate 		if (!plp->pl_found) {
26527c478bd9Sstevel@tonic-gate 			plp->pl_found = TRUE;
26537c478bd9Sstevel@tonic-gate 			*plp->pl_symp = sym;
26547c478bd9Sstevel@tonic-gate 			plp->pl_sip->sym_table = si.prs_table;
26557c478bd9Sstevel@tonic-gate 			plp->pl_sip->sym_id = si.prs_id;
26567c478bd9Sstevel@tonic-gate 		}
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 		return (0);
26597c478bd9Sstevel@tonic-gate 	}
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	/*
26627c478bd9Sstevel@tonic-gate 	 * Note that if the symbol's st_shndx is SHN_UNDEF we don't have the
26637c478bd9Sstevel@tonic-gate 	 * TLS offset anyway, so adding in the tlsbase would be worthless.
26647c478bd9Sstevel@tonic-gate 	 */
26657c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(sym.st_info) == STT_TLS &&
26667c478bd9Sstevel@tonic-gate 	    plp->pl_tid != (mdb_tgt_tid_t)-1) {
26677c478bd9Sstevel@tonic-gate 		psaddr_t base;
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 		if (tlsbase(plp->pl_tgt, plp->pl_tid, plp->pl_lmid, object,
26707c478bd9Sstevel@tonic-gate 		    &base) != 0)
26717c478bd9Sstevel@tonic-gate 			return (-1); /* errno is set for us */
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 		sym.st_value += base;
26747c478bd9Sstevel@tonic-gate 	}
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	plp->pl_found = TRUE;
26777c478bd9Sstevel@tonic-gate 	*plp->pl_symp = sym;
26787c478bd9Sstevel@tonic-gate 	plp->pl_sip->sym_table = si.prs_table;
26797c478bd9Sstevel@tonic-gate 	plp->pl_sip->sym_id = si.prs_id;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	return (1);
26827c478bd9Sstevel@tonic-gate }
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate /*
26857c478bd9Sstevel@tonic-gate  * Lookup the symbol with a thread context so that we can adjust TLS symbols
26867c478bd9Sstevel@tonic-gate  * to get the values as they would appear in the context of the given thread.
26877c478bd9Sstevel@tonic-gate  */
26887c478bd9Sstevel@tonic-gate static int
pt_lookup_by_name_thr(mdb_tgt_t * t,const char * object,const char * name,GElf_Sym * symp,mdb_syminfo_t * sip,mdb_tgt_tid_t tid)26897c478bd9Sstevel@tonic-gate pt_lookup_by_name_thr(mdb_tgt_t *t, const char *object,
26907c478bd9Sstevel@tonic-gate     const char *name, GElf_Sym *symp, mdb_syminfo_t *sip, mdb_tgt_tid_t tid)
26917c478bd9Sstevel@tonic-gate {
26927c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
26937c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
26947c478bd9Sstevel@tonic-gate 	Lmid_t lmid;
26957c478bd9Sstevel@tonic-gate 	uint_t i;
26967c478bd9Sstevel@tonic-gate 	const rd_loadobj_t *aout_lop;
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	object = pt_resolve_lmid(object, &lmid);
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	if (P != NULL) {
27017c478bd9Sstevel@tonic-gate 		pt_lookup_t pl;
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 		pl.pl_tgt = t;
27047c478bd9Sstevel@tonic-gate 		pl.pl_name = name;
27057c478bd9Sstevel@tonic-gate 		pl.pl_lmid = lmid;
27067c478bd9Sstevel@tonic-gate 		pl.pl_symp = symp;
27077c478bd9Sstevel@tonic-gate 		pl.pl_sip = sip;
27087c478bd9Sstevel@tonic-gate 		pl.pl_tid = tid;
27097c478bd9Sstevel@tonic-gate 		pl.pl_found = FALSE;
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 		if (object == MDB_TGT_OBJ_EVERY) {
271295d62a61SEdward Pilatowicz 			if (Pobject_iter_resolved(P, pt_lookup_cb, &pl) == -1)
2713186f7fbfSEdward Pilatowicz 				return (-1); /* errno is set for us */
271495d62a61SEdward Pilatowicz 			if ((!pl.pl_found) &&
271595d62a61SEdward Pilatowicz 			    (Pobject_iter(P, pt_lookup_cb, &pl) == -1))
27167c478bd9Sstevel@tonic-gate 				return (-1); /* errno is set for us */
27177c478bd9Sstevel@tonic-gate 		} else {
27187c478bd9Sstevel@tonic-gate 			const prmap_t *pmp;
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 			/*
27217c478bd9Sstevel@tonic-gate 			 * This can fail either due to an invalid lmid or
27227c478bd9Sstevel@tonic-gate 			 * an invalid object. To determine which is
27237c478bd9Sstevel@tonic-gate 			 * faulty, we test the lmid against known valid
27247c478bd9Sstevel@tonic-gate 			 * lmids and then see if using a wild-card lmid
27257c478bd9Sstevel@tonic-gate 			 * improves ths situation.
27267c478bd9Sstevel@tonic-gate 			 */
27277c478bd9Sstevel@tonic-gate 			if ((pmp = Plmid_to_map(P, lmid, object)) == NULL) {
27287c478bd9Sstevel@tonic-gate 				if (lmid != PR_LMID_EVERY &&
27297c478bd9Sstevel@tonic-gate 				    lmid != LM_ID_BASE &&
27307c478bd9Sstevel@tonic-gate 				    lmid != LM_ID_LDSO &&
27317c478bd9Sstevel@tonic-gate 				    Plmid_to_map(P, PR_LMID_EVERY, object)
27327c478bd9Sstevel@tonic-gate 				    != NULL)
27337c478bd9Sstevel@tonic-gate 					return (set_errno(EMDB_NOLMID));
27347c478bd9Sstevel@tonic-gate 				else
27357c478bd9Sstevel@tonic-gate 					return (set_errno(EMDB_NOOBJ));
27367c478bd9Sstevel@tonic-gate 			}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 			if (pt_lookup_cb(&pl, pmp, object) == -1)
27397c478bd9Sstevel@tonic-gate 				return (-1); /* errno is set for us */
27407c478bd9Sstevel@tonic-gate 		}
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 		if (pl.pl_found)
27437c478bd9Sstevel@tonic-gate 			return (0);
27447c478bd9Sstevel@tonic-gate 	}
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	/*
27477c478bd9Sstevel@tonic-gate 	 * If libproc doesn't have the symbols for rtld, we're cooked --
27487c478bd9Sstevel@tonic-gate 	 * mdb doesn't have those symbols either.
27497c478bd9Sstevel@tonic-gate 	 */
27507c478bd9Sstevel@tonic-gate 	if (object == MDB_TGT_OBJ_RTLD)
27517c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOSYM));
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	if (object != MDB_TGT_OBJ_EXEC && object != MDB_TGT_OBJ_EVERY) {
27547c478bd9Sstevel@tonic-gate 		int status = mdb_gelf_symtab_lookup_by_file(pt->p_symtab,
27557c478bd9Sstevel@tonic-gate 		    object, name, symp, &sip->sym_id);
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 		if (status != 0) {
27587c478bd9Sstevel@tonic-gate 			if (P != NULL &&
27597c478bd9Sstevel@tonic-gate 			    Plmid_to_map(P, PR_LMID_EVERY, object) != NULL)
27607c478bd9Sstevel@tonic-gate 				return (set_errno(EMDB_NOSYM));
27617c478bd9Sstevel@tonic-gate 			else
27627c478bd9Sstevel@tonic-gate 				return (-1); /* errno set from lookup_by_file */
27637c478bd9Sstevel@tonic-gate 		}
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 		goto found;
27667c478bd9Sstevel@tonic-gate 	}
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, name, symp, &i) == 0) {
27697c478bd9Sstevel@tonic-gate 		sip->sym_table = MDB_TGT_SYMTAB;
27707c478bd9Sstevel@tonic-gate 		sip->sym_id = i;
27717c478bd9Sstevel@tonic-gate 		goto local_found;
27727c478bd9Sstevel@tonic-gate 	}
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, name, symp, &i) == 0) {
27757c478bd9Sstevel@tonic-gate 		sip->sym_table = MDB_TGT_DYNSYM;
27767c478bd9Sstevel@tonic-gate 		sip->sym_id = i;
27777c478bd9Sstevel@tonic-gate 		goto local_found;
27787c478bd9Sstevel@tonic-gate 	}
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOSYM));
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate local_found:
27837c478bd9Sstevel@tonic-gate 	if (pt->p_file != NULL &&
27847c478bd9Sstevel@tonic-gate 	    pt->p_file->gf_ehdr.e_type == ET_DYN &&
27857c478bd9Sstevel@tonic-gate 	    P != NULL &&
27867c478bd9Sstevel@tonic-gate 	    (aout_lop = Pname_to_loadobj(P, PR_OBJ_EXEC)) != NULL)
27877c478bd9Sstevel@tonic-gate 		symp->st_value += aout_lop->rl_base;
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate found:
27907c478bd9Sstevel@tonic-gate 	/*
27917c478bd9Sstevel@tonic-gate 	 * If the symbol has type TLS, libproc should have found the symbol
27927c478bd9Sstevel@tonic-gate 	 * if it exists and has been allocated.
27937c478bd9Sstevel@tonic-gate 	 */
27947c478bd9Sstevel@tonic-gate 	if (GELF_ST_TYPE(symp->st_info) == STT_TLS)
27957c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TLS));
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	return (0);
27987c478bd9Sstevel@tonic-gate }
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate static int
pt_lookup_by_name(mdb_tgt_t * t,const char * object,const char * name,GElf_Sym * symp,mdb_syminfo_t * sip)28017c478bd9Sstevel@tonic-gate pt_lookup_by_name(mdb_tgt_t *t, const char *object,
28027c478bd9Sstevel@tonic-gate     const char *name, GElf_Sym *symp, mdb_syminfo_t *sip)
28037c478bd9Sstevel@tonic-gate {
28047c478bd9Sstevel@tonic-gate 	return (pt_lookup_by_name_thr(t, object, name, symp, sip, PTL_TID(t)));
28057c478bd9Sstevel@tonic-gate }
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate static int
pt_lookup_by_addr(mdb_tgt_t * t,uintptr_t addr,uint_t flags,char * buf,size_t nbytes,GElf_Sym * symp,mdb_syminfo_t * sip)28087c478bd9Sstevel@tonic-gate pt_lookup_by_addr(mdb_tgt_t *t, uintptr_t addr, uint_t flags,
28097c478bd9Sstevel@tonic-gate     char *buf, size_t nbytes, GElf_Sym *symp, mdb_syminfo_t *sip)
28107c478bd9Sstevel@tonic-gate {
28117c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
28127c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
28137c478bd9Sstevel@tonic-gate 	rd_plt_info_t rpi = { 0 };
2814186f7fbfSEdward Pilatowicz 
28157c478bd9Sstevel@tonic-gate 	const char *pltsym;
2816186f7fbfSEdward Pilatowicz 	int rv, match, i;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	mdb_gelf_symtab_t *gsts[3];	/* mdb.m_prsym, .symtab, .dynsym */
28197c478bd9Sstevel@tonic-gate 	int gstc = 0;			/* number of valid gsts[] entries */
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	mdb_gelf_symtab_t *gst = NULL;	/* set if 'sym' is from a gst */
28227c478bd9Sstevel@tonic-gate 	const prmap_t *pmp = NULL;	/* set if 'sym' is from libproc */
28237c478bd9Sstevel@tonic-gate 	GElf_Sym sym;			/* best symbol found so far if !exact */
28247c478bd9Sstevel@tonic-gate 	prsyminfo_t si;
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	/*
28277c478bd9Sstevel@tonic-gate 	 * Fill in our array of symbol table pointers with the private symbol
28287c478bd9Sstevel@tonic-gate 	 * table, static symbol table, and dynamic symbol table if applicable.
28297c478bd9Sstevel@tonic-gate 	 * These are done in order of precedence so that if we match and
28307c478bd9Sstevel@tonic-gate 	 * MDB_TGT_SYM_EXACT is set, we need not look any further.
28317c478bd9Sstevel@tonic-gate 	 */
28327c478bd9Sstevel@tonic-gate 	if (mdb.m_prsym != NULL)
28337c478bd9Sstevel@tonic-gate 		gsts[gstc++] = mdb.m_prsym;
28347c478bd9Sstevel@tonic-gate 	if (P == NULL && pt->p_symtab != NULL)
28357c478bd9Sstevel@tonic-gate 		gsts[gstc++] = pt->p_symtab;
28367c478bd9Sstevel@tonic-gate 	if (P == NULL && pt->p_dynsym != NULL)
28377c478bd9Sstevel@tonic-gate 		gsts[gstc++] = pt->p_dynsym;
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	/*
28407c478bd9Sstevel@tonic-gate 	 * Loop through our array attempting to match the address.  If we match
28417c478bd9Sstevel@tonic-gate 	 * and we're in exact mode, we're done.  Otherwise save the symbol in
28427c478bd9Sstevel@tonic-gate 	 * the local sym variable if it is closer than our previous match.
28437c478bd9Sstevel@tonic-gate 	 * We explicitly watch for zero-valued symbols since DevPro insists
28447c478bd9Sstevel@tonic-gate 	 * on storing __fsr_init_value's value as the symbol value instead
28457c478bd9Sstevel@tonic-gate 	 * of storing it in a constant integer.
28467c478bd9Sstevel@tonic-gate 	 */
28477c478bd9Sstevel@tonic-gate 	for (i = 0; i < gstc; i++) {
28487c478bd9Sstevel@tonic-gate 		if (mdb_gelf_symtab_lookup_by_addr(gsts[i], addr, flags, buf,
28497c478bd9Sstevel@tonic-gate 		    nbytes, symp, &sip->sym_id) != 0 || symp->st_value == 0)
28507c478bd9Sstevel@tonic-gate 			continue;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 		if (flags & MDB_TGT_SYM_EXACT) {
28537c478bd9Sstevel@tonic-gate 			gst = gsts[i];
28547c478bd9Sstevel@tonic-gate 			goto found;
28557c478bd9Sstevel@tonic-gate 		}
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 		if (gst == NULL || mdb_gelf_sym_closer(symp, &sym, addr)) {
28587c478bd9Sstevel@tonic-gate 			gst = gsts[i];
28597c478bd9Sstevel@tonic-gate 			sym = *symp;
28607c478bd9Sstevel@tonic-gate 		}
28617c478bd9Sstevel@tonic-gate 	}
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	/*
28647c478bd9Sstevel@tonic-gate 	 * If we have no libproc handle active, we're done: fail if gst is
28657c478bd9Sstevel@tonic-gate 	 * NULL; otherwise copy out our best symbol and skip to the end.
28667c478bd9Sstevel@tonic-gate 	 * We also skip to found if gst is the private symbol table: we
28677c478bd9Sstevel@tonic-gate 	 * want this to always take precedence over PLT re-vectoring.
28687c478bd9Sstevel@tonic-gate 	 */
28697c478bd9Sstevel@tonic-gate 	if (P == NULL || (gst != NULL && gst == mdb.m_prsym)) {
28707c478bd9Sstevel@tonic-gate 		if (gst == NULL)
28717c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_NOSYMADDR));
28727c478bd9Sstevel@tonic-gate 		*symp = sym;
28737c478bd9Sstevel@tonic-gate 		goto found;
28747c478bd9Sstevel@tonic-gate 	}
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 	/*
28777c478bd9Sstevel@tonic-gate 	 * Check to see if the address is in a PLT: if it is, use librtld_db to
28787c478bd9Sstevel@tonic-gate 	 * attempt to resolve the PLT entry.  If the entry is bound, reset addr
28797c478bd9Sstevel@tonic-gate 	 * to the bound address, add a special prefix to the caller's buf,
28807c478bd9Sstevel@tonic-gate 	 * forget our previous guess, and then continue using the new addr.
28817c478bd9Sstevel@tonic-gate 	 * If the entry is not bound, copy the corresponding symbol name into
28827c478bd9Sstevel@tonic-gate 	 * buf and return a fake symbol for the given address.
28837c478bd9Sstevel@tonic-gate 	 */
28847c478bd9Sstevel@tonic-gate 	if ((pltsym = Ppltdest(P, addr)) != NULL) {
28857c478bd9Sstevel@tonic-gate 		const rd_loadobj_t *rlp;
28867c478bd9Sstevel@tonic-gate 		rd_agent_t *rap;
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 		if ((rap = Prd_agent(P)) != NULL &&
28897c478bd9Sstevel@tonic-gate 		    (rlp = Paddr_to_loadobj(P, addr)) != NULL &&
28907c478bd9Sstevel@tonic-gate 		    rd_plt_resolution(rap, addr, Pstatus(P)->pr_lwp.pr_lwpid,
28917c478bd9Sstevel@tonic-gate 		    rlp->rl_plt_base, &rpi) == RD_OK &&
28927c478bd9Sstevel@tonic-gate 		    (rpi.pi_flags & RD_FLG_PI_PLTBOUND)) {
28937c478bd9Sstevel@tonic-gate 			size_t n;
28947c478bd9Sstevel@tonic-gate 			n = mdb_iob_snprintf(buf, nbytes, "PLT=");
28957c478bd9Sstevel@tonic-gate 			addr = rpi.pi_baddr;
28967c478bd9Sstevel@tonic-gate 			if (n > nbytes) {
28977c478bd9Sstevel@tonic-gate 				buf += nbytes;
28987c478bd9Sstevel@tonic-gate 				nbytes = 0;
28997c478bd9Sstevel@tonic-gate 			} else {
29007c478bd9Sstevel@tonic-gate 				buf += n;
29017c478bd9Sstevel@tonic-gate 				nbytes -= n;
29027c478bd9Sstevel@tonic-gate 			}
29037c478bd9Sstevel@tonic-gate 			gst = NULL;
29047c478bd9Sstevel@tonic-gate 		} else {
29057c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(buf, nbytes, "PLT:%s", pltsym);
29067c478bd9Sstevel@tonic-gate 			bzero(symp, sizeof (GElf_Sym));
29077c478bd9Sstevel@tonic-gate 			symp->st_value = addr;
29087c478bd9Sstevel@tonic-gate 			symp->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
29097c478bd9Sstevel@tonic-gate 			return (0);
29107c478bd9Sstevel@tonic-gate 		}
29117c478bd9Sstevel@tonic-gate 	}
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	/*
29147c478bd9Sstevel@tonic-gate 	 * Ask libproc to convert the address to the closest symbol for us.
29157c478bd9Sstevel@tonic-gate 	 * Once we get the closest symbol, we perform the EXACT match or
29167c478bd9Sstevel@tonic-gate 	 * smart-mode or absolute distance check ourself:
29177c478bd9Sstevel@tonic-gate 	 */
29188f68126cSBryan Cantrill 	if (PT_LIBPROC_RESOLVE(P)) {
2919186f7fbfSEdward Pilatowicz 		rv = Pxlookup_by_addr_resolved(P, addr, buf, nbytes,
2920186f7fbfSEdward Pilatowicz 		    symp, &si);
2921186f7fbfSEdward Pilatowicz 	} else {
2922186f7fbfSEdward Pilatowicz 		rv = Pxlookup_by_addr(P, addr, buf, nbytes,
2923186f7fbfSEdward Pilatowicz 		    symp, &si);
2924186f7fbfSEdward Pilatowicz 	}
2925186f7fbfSEdward Pilatowicz 	if ((rv == 0) && (symp->st_value != 0) &&
2926186f7fbfSEdward Pilatowicz 	    (gst == NULL || mdb_gelf_sym_closer(symp, &sym, addr))) {
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 		if (flags & MDB_TGT_SYM_EXACT)
29297c478bd9Sstevel@tonic-gate 			match = (addr == symp->st_value);
29307c478bd9Sstevel@tonic-gate 		else if (mdb.m_symdist == 0)
29317c478bd9Sstevel@tonic-gate 			match = (addr >= symp->st_value &&
29327c478bd9Sstevel@tonic-gate 			    addr < symp->st_value + symp->st_size);
29337c478bd9Sstevel@tonic-gate 		else
29347c478bd9Sstevel@tonic-gate 			match = (addr >= symp->st_value &&
29357c478bd9Sstevel@tonic-gate 			    addr < symp->st_value + mdb.m_symdist);
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 		if (match) {
29387c478bd9Sstevel@tonic-gate 			pmp = Paddr_to_map(P, addr);
29397c478bd9Sstevel@tonic-gate 			gst = NULL;
29407c478bd9Sstevel@tonic-gate 			sip->sym_table = si.prs_table;
29417c478bd9Sstevel@tonic-gate 			sip->sym_id = si.prs_id;
29427c478bd9Sstevel@tonic-gate 			goto found;
29437c478bd9Sstevel@tonic-gate 		}
29447c478bd9Sstevel@tonic-gate 	}
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	/*
29477c478bd9Sstevel@tonic-gate 	 * If we get here, Plookup_by_addr has failed us.  If we have no
29487c478bd9Sstevel@tonic-gate 	 * previous best symbol (gst == NULL), we've failed completely.
29497c478bd9Sstevel@tonic-gate 	 * Otherwise we copy out that symbol and continue on to 'found'.
29507c478bd9Sstevel@tonic-gate 	 */
29517c478bd9Sstevel@tonic-gate 	if (gst == NULL)
29527c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOSYMADDR));
29537c478bd9Sstevel@tonic-gate 	*symp = sym;
29547c478bd9Sstevel@tonic-gate found:
29557c478bd9Sstevel@tonic-gate 	/*
29567c478bd9Sstevel@tonic-gate 	 * Once we've found something, copy the final name into the caller's
29577c478bd9Sstevel@tonic-gate 	 * buffer and prefix it with the mapping name if appropriate.
29587c478bd9Sstevel@tonic-gate 	 */
29597c478bd9Sstevel@tonic-gate 	if (pmp != NULL && pmp != Pname_to_map(P, PR_OBJ_EXEC)) {
29607c478bd9Sstevel@tonic-gate 		const char *prefix = pmp->pr_mapname;
29617c478bd9Sstevel@tonic-gate 		Lmid_t lmid;
29627c478bd9Sstevel@tonic-gate 
29638f68126cSBryan Cantrill 		if (PT_LIBPROC_RESOLVE(P)) {
2964186f7fbfSEdward Pilatowicz 			if (Pobjname_resolved(P, addr, pt->p_objname,
2965186f7fbfSEdward Pilatowicz 			    MDB_TGT_MAPSZ))
2966186f7fbfSEdward Pilatowicz 				prefix = pt->p_objname;
2967186f7fbfSEdward Pilatowicz 		} else {
29687c478bd9Sstevel@tonic-gate 			if (Pobjname(P, addr, pt->p_objname, MDB_TGT_MAPSZ))
29697c478bd9Sstevel@tonic-gate 				prefix = pt->p_objname;
2970186f7fbfSEdward Pilatowicz 		}
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 		if (buf != NULL && nbytes > 1) {
29737c478bd9Sstevel@tonic-gate 			(void) strncpy(pt->p_symname, buf, MDB_TGT_SYM_NAMLEN);
29747c478bd9Sstevel@tonic-gate 			pt->p_symname[MDB_TGT_SYM_NAMLEN - 1] = '\0';
29757c478bd9Sstevel@tonic-gate 		} else {
29767c478bd9Sstevel@tonic-gate 			pt->p_symname[0] = '\0';
29777c478bd9Sstevel@tonic-gate 		}
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 		if (prefix == pt->p_objname && Plmid(P, addr, &lmid) == 0 && (
29807c478bd9Sstevel@tonic-gate 		    (lmid != LM_ID_BASE && lmid != LM_ID_LDSO) ||
29817c478bd9Sstevel@tonic-gate 		    (mdb.m_flags & MDB_FL_SHOWLMID))) {
29827c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(buf, nbytes, "LM%lr`%s`%s",
29837c478bd9Sstevel@tonic-gate 			    lmid, strbasename(prefix), pt->p_symname);
29847c478bd9Sstevel@tonic-gate 		} else {
29857c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(buf, nbytes, "%s`%s",
29867c478bd9Sstevel@tonic-gate 			    strbasename(prefix), pt->p_symname);
29877c478bd9Sstevel@tonic-gate 		}
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	} else if (gst != NULL && buf != NULL && nbytes > 0) {
29907c478bd9Sstevel@tonic-gate 		(void) strncpy(buf, mdb_gelf_sym_name(gst, symp), nbytes);
29917c478bd9Sstevel@tonic-gate 		buf[nbytes - 1] = '\0';
29927c478bd9Sstevel@tonic-gate 	}
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	return (0);
29957c478bd9Sstevel@tonic-gate }
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate static int
pt_symbol_iter_cb(void * arg,const GElf_Sym * sym,const char * name,const prsyminfo_t * sip)29997c478bd9Sstevel@tonic-gate pt_symbol_iter_cb(void *arg, const GElf_Sym *sym, const char *name,
30007c478bd9Sstevel@tonic-gate     const prsyminfo_t *sip)
30017c478bd9Sstevel@tonic-gate {
30027c478bd9Sstevel@tonic-gate 	pt_symarg_t *psp = arg;
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 	psp->psym_info.sym_id = sip->prs_id;
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	return (psp->psym_func(psp->psym_private, sym, name, &psp->psym_info,
30077c478bd9Sstevel@tonic-gate 	    psp->psym_obj));
30087c478bd9Sstevel@tonic-gate }
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate static int
pt_objsym_iter(void * arg,const prmap_t * pmp,const char * object)30117c478bd9Sstevel@tonic-gate pt_objsym_iter(void *arg, const prmap_t *pmp, const char *object)
30127c478bd9Sstevel@tonic-gate {
30137c478bd9Sstevel@tonic-gate 	Lmid_t lmid = PR_LMID_EVERY;
30147c478bd9Sstevel@tonic-gate 	pt_symarg_t *psp = arg;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	psp->psym_obj = object;
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 	(void) Plmid(psp->psym_targ->t_pshandle, pmp->pr_vaddr, &lmid);
30197c478bd9Sstevel@tonic-gate 	(void) Pxsymbol_iter(psp->psym_targ->t_pshandle, lmid, object,
30207c478bd9Sstevel@tonic-gate 	    psp->psym_which, psp->psym_type, pt_symbol_iter_cb, arg);
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	return (0);
30237c478bd9Sstevel@tonic-gate }
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate static int
pt_symbol_filt(void * arg,const GElf_Sym * sym,const char * name,uint_t id)30267c478bd9Sstevel@tonic-gate pt_symbol_filt(void *arg, const GElf_Sym *sym, const char *name, uint_t id)
30277c478bd9Sstevel@tonic-gate {
30287c478bd9Sstevel@tonic-gate 	pt_symarg_t *psp = arg;
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	if (mdb_tgt_sym_match(sym, psp->psym_type)) {
30317c478bd9Sstevel@tonic-gate 		psp->psym_info.sym_id = id;
30327c478bd9Sstevel@tonic-gate 		return (psp->psym_func(psp->psym_private, sym, name,
30337c478bd9Sstevel@tonic-gate 		    &psp->psym_info, psp->psym_obj));
30347c478bd9Sstevel@tonic-gate 	}
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate 	return (0);
30377c478bd9Sstevel@tonic-gate }
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate static int
pt_symbol_iter(mdb_tgt_t * t,const char * object,uint_t which,uint_t type,mdb_tgt_sym_f * func,void * private)30407c478bd9Sstevel@tonic-gate pt_symbol_iter(mdb_tgt_t *t, const char *object, uint_t which,
30417c478bd9Sstevel@tonic-gate     uint_t type, mdb_tgt_sym_f *func, void *private)
30427c478bd9Sstevel@tonic-gate {
30437c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
30447c478bd9Sstevel@tonic-gate 	mdb_gelf_symtab_t *gst;
30457c478bd9Sstevel@tonic-gate 	pt_symarg_t ps;
30467c478bd9Sstevel@tonic-gate 	Lmid_t lmid;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	object = pt_resolve_lmid(object, &lmid);
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	ps.psym_targ = t;
30517c478bd9Sstevel@tonic-gate 	ps.psym_which = which;
30527c478bd9Sstevel@tonic-gate 	ps.psym_type = type;
30537c478bd9Sstevel@tonic-gate 	ps.psym_func = func;
30547c478bd9Sstevel@tonic-gate 	ps.psym_private = private;
30557c478bd9Sstevel@tonic-gate 	ps.psym_obj = object;
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
30587c478bd9Sstevel@tonic-gate 		if (object != MDB_TGT_OBJ_EVERY) {
30597c478bd9Sstevel@tonic-gate 			if (Plmid_to_map(t->t_pshandle, lmid, object) == NULL)
30607c478bd9Sstevel@tonic-gate 				return (set_errno(EMDB_NOOBJ));
30617c478bd9Sstevel@tonic-gate 			(void) Pxsymbol_iter(t->t_pshandle, lmid, object,
30627c478bd9Sstevel@tonic-gate 			    which, type, pt_symbol_iter_cb, &ps);
30637c478bd9Sstevel@tonic-gate 			return (0);
30647c478bd9Sstevel@tonic-gate 		} else if (Prd_agent(t->t_pshandle) != NULL) {
30658f68126cSBryan Cantrill 			if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3066186f7fbfSEdward Pilatowicz 				(void) Pobject_iter_resolved(t->t_pshandle,
3067186f7fbfSEdward Pilatowicz 				    pt_objsym_iter, &ps);
3068186f7fbfSEdward Pilatowicz 			} else {
3069186f7fbfSEdward Pilatowicz 				(void) Pobject_iter(t->t_pshandle,
3070186f7fbfSEdward Pilatowicz 				    pt_objsym_iter, &ps);
3071186f7fbfSEdward Pilatowicz 			}
30727c478bd9Sstevel@tonic-gate 			return (0);
30737c478bd9Sstevel@tonic-gate 		}
30747c478bd9Sstevel@tonic-gate 	}
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	if (lmid != LM_ID_BASE && lmid != PR_LMID_EVERY)
30777c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOLMID));
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	if (object != MDB_TGT_OBJ_EXEC && object != MDB_TGT_OBJ_EVERY &&
30807c478bd9Sstevel@tonic-gate 	    pt->p_fio != NULL &&
30817c478bd9Sstevel@tonic-gate 	    strcmp(object, IOP_NAME(pt->p_fio)) != 0)
30827c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOOBJ));
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	if (which == MDB_TGT_SYMTAB)
30857c478bd9Sstevel@tonic-gate 		gst = pt->p_symtab;
30867c478bd9Sstevel@tonic-gate 	else
30877c478bd9Sstevel@tonic-gate 		gst = pt->p_dynsym;
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	if (gst != NULL) {
30907c478bd9Sstevel@tonic-gate 		ps.psym_info.sym_table = gst->gst_tabid;
30917c478bd9Sstevel@tonic-gate 		mdb_gelf_symtab_iter(gst, pt_symbol_filt, &ps);
30927c478bd9Sstevel@tonic-gate 	}
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	return (0);
30957c478bd9Sstevel@tonic-gate }
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate static const mdb_map_t *
pt_prmap_to_mdbmap(mdb_tgt_t * t,const prmap_t * prp,mdb_map_t * mp)30987c478bd9Sstevel@tonic-gate pt_prmap_to_mdbmap(mdb_tgt_t *t, const prmap_t *prp, mdb_map_t *mp)
30997c478bd9Sstevel@tonic-gate {
31007c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
3101186f7fbfSEdward Pilatowicz 	char *rv, name[MAXPATHLEN];
31027c478bd9Sstevel@tonic-gate 	Lmid_t lmid;
31037c478bd9Sstevel@tonic-gate 
31048f68126cSBryan Cantrill 	if (PT_LIBPROC_RESOLVE(P)) {
3105186f7fbfSEdward Pilatowicz 		rv = Pobjname_resolved(P, prp->pr_vaddr, name, sizeof (name));
3106186f7fbfSEdward Pilatowicz 	} else {
3107186f7fbfSEdward Pilatowicz 		rv = Pobjname(P, prp->pr_vaddr, name, sizeof (name));
3108186f7fbfSEdward Pilatowicz 	}
3109186f7fbfSEdward Pilatowicz 
3110186f7fbfSEdward Pilatowicz 	if (rv != NULL) {
31117c478bd9Sstevel@tonic-gate 		if (Plmid(P, prp->pr_vaddr, &lmid) == 0 && (
31127c478bd9Sstevel@tonic-gate 		    (lmid != LM_ID_BASE && lmid != LM_ID_LDSO) ||
31137c478bd9Sstevel@tonic-gate 		    (mdb.m_flags & MDB_FL_SHOWLMID))) {
31147c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(mp->map_name, MDB_TGT_MAPSZ,
31157c478bd9Sstevel@tonic-gate 			    "LM%lr`%s", lmid, name);
31167c478bd9Sstevel@tonic-gate 		} else {
31177c478bd9Sstevel@tonic-gate 			(void) strncpy(mp->map_name, name, MDB_TGT_MAPSZ - 1);
31187c478bd9Sstevel@tonic-gate 			mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
31197c478bd9Sstevel@tonic-gate 		}
31207c478bd9Sstevel@tonic-gate 	} else {
31217c478bd9Sstevel@tonic-gate 		(void) strncpy(mp->map_name, prp->pr_mapname,
31227c478bd9Sstevel@tonic-gate 		    MDB_TGT_MAPSZ - 1);
31237c478bd9Sstevel@tonic-gate 		mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
31247c478bd9Sstevel@tonic-gate 	}
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 	mp->map_base = prp->pr_vaddr;
31277c478bd9Sstevel@tonic-gate 	mp->map_size = prp->pr_size;
31287c478bd9Sstevel@tonic-gate 	mp->map_flags = 0;
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_READ)
31317c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_R;
31327c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_WRITE)
31337c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_W;
31347c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_EXEC)
31357c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_X;
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_SHM)
31387c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_SHMEM;
31397c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_BREAK)
31407c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_HEAP;
31417c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_STACK)
31427c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_STACK;
31437c478bd9Sstevel@tonic-gate 	if (prp->pr_mflags & MA_ANON)
31447c478bd9Sstevel@tonic-gate 		mp->map_flags |= MDB_TGT_MAP_ANON;
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 	return (mp);
31477c478bd9Sstevel@tonic-gate }
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31507c478bd9Sstevel@tonic-gate static int
pt_map_apply(void * arg,const prmap_t * prp,const char * name)31517c478bd9Sstevel@tonic-gate pt_map_apply(void *arg, const prmap_t *prp, const char *name)
31527c478bd9Sstevel@tonic-gate {
31537c478bd9Sstevel@tonic-gate 	pt_maparg_t *pmp = arg;
31547c478bd9Sstevel@tonic-gate 	mdb_map_t map;
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	return (pmp->pmap_func(pmp->pmap_private,
31577c478bd9Sstevel@tonic-gate 	    pt_prmap_to_mdbmap(pmp->pmap_targ, prp, &map), map.map_name));
31587c478bd9Sstevel@tonic-gate }
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate static int
pt_mapping_iter(mdb_tgt_t * t,mdb_tgt_map_f * func,void * private)31617c478bd9Sstevel@tonic-gate pt_mapping_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
31647c478bd9Sstevel@tonic-gate 		pt_maparg_t pm;
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 		pm.pmap_targ = t;
31677c478bd9Sstevel@tonic-gate 		pm.pmap_func = func;
31687c478bd9Sstevel@tonic-gate 		pm.pmap_private = private;
31697c478bd9Sstevel@tonic-gate 
31708f68126cSBryan Cantrill 		if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3171186f7fbfSEdward Pilatowicz 			(void) Pmapping_iter_resolved(t->t_pshandle,
3172186f7fbfSEdward Pilatowicz 			    pt_map_apply, &pm);
3173186f7fbfSEdward Pilatowicz 		} else {
3174186f7fbfSEdward Pilatowicz 			(void) Pmapping_iter(t->t_pshandle,
3175186f7fbfSEdward Pilatowicz 			    pt_map_apply, &pm);
3176186f7fbfSEdward Pilatowicz 		}
31777c478bd9Sstevel@tonic-gate 		return (0);
31787c478bd9Sstevel@tonic-gate 	}
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
31817c478bd9Sstevel@tonic-gate }
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate static int
pt_object_iter(mdb_tgt_t * t,mdb_tgt_map_f * func,void * private)31847c478bd9Sstevel@tonic-gate pt_object_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
31857c478bd9Sstevel@tonic-gate {
31867c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	/*
31897c478bd9Sstevel@tonic-gate 	 * If we have a libproc handle, we can just call Pobject_iter to
31907c478bd9Sstevel@tonic-gate 	 * iterate over its list of load object information.
31917c478bd9Sstevel@tonic-gate 	 */
31927c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
31937c478bd9Sstevel@tonic-gate 		pt_maparg_t pm;
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 		pm.pmap_targ = t;
31967c478bd9Sstevel@tonic-gate 		pm.pmap_func = func;
31977c478bd9Sstevel@tonic-gate 		pm.pmap_private = private;
31987c478bd9Sstevel@tonic-gate 
31998f68126cSBryan Cantrill 		if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3200186f7fbfSEdward Pilatowicz 			(void) Pobject_iter_resolved(t->t_pshandle,
3201186f7fbfSEdward Pilatowicz 			    pt_map_apply, &pm);
3202186f7fbfSEdward Pilatowicz 		} else {
3203186f7fbfSEdward Pilatowicz 			(void) Pobject_iter(t->t_pshandle,
3204186f7fbfSEdward Pilatowicz 			    pt_map_apply, &pm);
3205186f7fbfSEdward Pilatowicz 		}
32067c478bd9Sstevel@tonic-gate 		return (0);
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 	/*
32107c478bd9Sstevel@tonic-gate 	 * If we're examining an executable or other ELF file but we have no
32117c478bd9Sstevel@tonic-gate 	 * libproc handle, fake up some information based on DT_NEEDED entries.
32127c478bd9Sstevel@tonic-gate 	 */
32137c478bd9Sstevel@tonic-gate 	if (pt->p_dynsym != NULL && pt->p_file->gf_dyns != NULL &&
32147c478bd9Sstevel@tonic-gate 	    pt->p_fio != NULL) {
32157c478bd9Sstevel@tonic-gate 		mdb_gelf_sect_t *gsp = pt->p_dynsym->gst_ssect;
32167c478bd9Sstevel@tonic-gate 		GElf_Dyn *dynp = pt->p_file->gf_dyns;
32177c478bd9Sstevel@tonic-gate 		mdb_map_t *mp = &pt->p_map;
32187c478bd9Sstevel@tonic-gate 		const char *s = IOP_NAME(pt->p_fio);
32197c478bd9Sstevel@tonic-gate 		size_t i;
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 		(void) strncpy(mp->map_name, s, MDB_TGT_MAPSZ);
32227c478bd9Sstevel@tonic-gate 		mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
32237c478bd9Sstevel@tonic-gate 		mp->map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_X;
3224892ad162SToomas Soome 		mp->map_base = 0;
32257c478bd9Sstevel@tonic-gate 		mp->map_size = 0;
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 		if (func(private, mp, s) != 0)
32287c478bd9Sstevel@tonic-gate 			return (0);
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 		for (i = 0; i < pt->p_file->gf_ndyns; i++, dynp++) {
32317c478bd9Sstevel@tonic-gate 			if (dynp->d_tag == DT_NEEDED) {
32327c478bd9Sstevel@tonic-gate 				s = (char *)gsp->gs_data + dynp->d_un.d_val;
32337c478bd9Sstevel@tonic-gate 				(void) strncpy(mp->map_name, s, MDB_TGT_MAPSZ);
32347c478bd9Sstevel@tonic-gate 				mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
32357c478bd9Sstevel@tonic-gate 				if (func(private, mp, s) != 0)
32367c478bd9Sstevel@tonic-gate 					return (0);
32377c478bd9Sstevel@tonic-gate 			}
32387c478bd9Sstevel@tonic-gate 		}
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 		return (0);
32417c478bd9Sstevel@tonic-gate 	}
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate static const mdb_map_t *
pt_addr_to_map(mdb_tgt_t * t,uintptr_t addr)32477c478bd9Sstevel@tonic-gate pt_addr_to_map(mdb_tgt_t *t, uintptr_t addr)
32487c478bd9Sstevel@tonic-gate {
32497c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
32507c478bd9Sstevel@tonic-gate 	const prmap_t *pmp;
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
32537c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOPROC);
32547c478bd9Sstevel@tonic-gate 		return (NULL);
32557c478bd9Sstevel@tonic-gate 	}
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 	if ((pmp = Paddr_to_map(t->t_pshandle, addr)) == NULL) {
32587c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOMAP);
32597c478bd9Sstevel@tonic-gate 		return (NULL);
32607c478bd9Sstevel@tonic-gate 	}
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	return (pt_prmap_to_mdbmap(t, pmp, &pt->p_map));
32637c478bd9Sstevel@tonic-gate }
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate static const mdb_map_t *
pt_name_to_map(mdb_tgt_t * t,const char * object)32667c478bd9Sstevel@tonic-gate pt_name_to_map(mdb_tgt_t *t, const char *object)
32677c478bd9Sstevel@tonic-gate {
32687c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
32697c478bd9Sstevel@tonic-gate 	const prmap_t *pmp;
32707c478bd9Sstevel@tonic-gate 	Lmid_t lmid;
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
32737c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOPROC);
32747c478bd9Sstevel@tonic-gate 		return (NULL);
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	object = pt_resolve_lmid(object, &lmid);
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 	if ((pmp = Plmid_to_map(t->t_pshandle, lmid, object)) == NULL) {
32807c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOOBJ);
32817c478bd9Sstevel@tonic-gate 		return (NULL);
32827c478bd9Sstevel@tonic-gate 	}
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	return (pt_prmap_to_mdbmap(t, pmp, &pt->p_map));
32857c478bd9Sstevel@tonic-gate }
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate static ctf_file_t *
pt_addr_to_ctf(mdb_tgt_t * t,uintptr_t addr)32887c478bd9Sstevel@tonic-gate pt_addr_to_ctf(mdb_tgt_t *t, uintptr_t addr)
32897c478bd9Sstevel@tonic-gate {
32907c478bd9Sstevel@tonic-gate 	ctf_file_t *ret;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
32937c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOPROC);
32947c478bd9Sstevel@tonic-gate 		return (NULL);
32957c478bd9Sstevel@tonic-gate 	}
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	if ((ret = Paddr_to_ctf(t->t_pshandle, addr)) == NULL) {
32987c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOOBJ);
32997c478bd9Sstevel@tonic-gate 		return (NULL);
33007c478bd9Sstevel@tonic-gate 	}
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	return (ret);
33037c478bd9Sstevel@tonic-gate }
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate static ctf_file_t *
pt_name_to_ctf(mdb_tgt_t * t,const char * name)33067c478bd9Sstevel@tonic-gate pt_name_to_ctf(mdb_tgt_t *t, const char *name)
33077c478bd9Sstevel@tonic-gate {
33087c478bd9Sstevel@tonic-gate 	ctf_file_t *ret;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
33117c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOPROC);
33127c478bd9Sstevel@tonic-gate 		return (NULL);
33137c478bd9Sstevel@tonic-gate 	}
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 	if ((ret = Pname_to_ctf(t->t_pshandle, name)) == NULL) {
33167c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOOBJ);
33177c478bd9Sstevel@tonic-gate 		return (NULL);
33187c478bd9Sstevel@tonic-gate 	}
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	return (ret);
33217c478bd9Sstevel@tonic-gate }
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate static int
pt_status(mdb_tgt_t * t,mdb_tgt_status_t * tsp)33247c478bd9Sstevel@tonic-gate pt_status(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
33257c478bd9Sstevel@tonic-gate {
33267c478bd9Sstevel@tonic-gate 	const pstatus_t *psp;
33277c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
33287c478bd9Sstevel@tonic-gate 	int state;
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate 	bzero(tsp, sizeof (mdb_tgt_status_t));
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL) {
33337c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_IDLE;
33347c478bd9Sstevel@tonic-gate 		return (0);
33357c478bd9Sstevel@tonic-gate 	}
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate 	switch (state = Pstate(t->t_pshandle)) {
33387c478bd9Sstevel@tonic-gate 	case PS_RUN:
33397c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_RUNNING;
33407c478bd9Sstevel@tonic-gate 		break;
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	case PS_STOP:
33437c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_STOPPED;
33447c478bd9Sstevel@tonic-gate 		psp = Pstatus(t->t_pshandle);
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 		tsp->st_tid = PTL_TID(t);
33477c478bd9Sstevel@tonic-gate 		if (PTL_GETREGS(t, tsp->st_tid, gregs) == 0)
33487c478bd9Sstevel@tonic-gate 			tsp->st_pc = gregs[R_PC];
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 		if (psp->pr_flags & PR_ISTOP)
33517c478bd9Sstevel@tonic-gate 			tsp->st_flags |= MDB_TGT_ISTOP;
33527c478bd9Sstevel@tonic-gate 		if (psp->pr_flags & PR_DSTOP)
33537c478bd9Sstevel@tonic-gate 			tsp->st_flags |= MDB_TGT_DSTOP;
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 		break;
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	case PS_LOST:
33587c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_LOST;
33597c478bd9Sstevel@tonic-gate 		break;
33607c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
33617c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_UNDEAD;
33627c478bd9Sstevel@tonic-gate 		break;
33637c478bd9Sstevel@tonic-gate 	case PS_DEAD:
33647c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_DEAD;
33657c478bd9Sstevel@tonic-gate 		break;
33667c478bd9Sstevel@tonic-gate 	case PS_IDLE:
33677c478bd9Sstevel@tonic-gate 		tsp->st_state = MDB_TGT_IDLE;
33687c478bd9Sstevel@tonic-gate 		break;
33697c478bd9Sstevel@tonic-gate 	default:
33707c478bd9Sstevel@tonic-gate 		fail("unknown libproc state (%d)\n", state);
33717c478bd9Sstevel@tonic-gate 	}
33727c478bd9Sstevel@tonic-gate 
33737c478bd9Sstevel@tonic-gate 	if (t->t_flags & MDB_TGT_F_BUSY)
33747c478bd9Sstevel@tonic-gate 		tsp->st_flags |= MDB_TGT_BUSY;
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 	return (0);
33777c478bd9Sstevel@tonic-gate }
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate static void
pt_dupfd(const char * file,int oflags,mode_t mode,int dfd)33807c478bd9Sstevel@tonic-gate pt_dupfd(const char *file, int oflags, mode_t mode, int dfd)
33817c478bd9Sstevel@tonic-gate {
33827c478bd9Sstevel@tonic-gate 	int fd;
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	if ((fd = open(file, oflags, mode)) >= 0) {
33857c478bd9Sstevel@tonic-gate 		(void) fcntl(fd, F_DUP2FD, dfd);
33867c478bd9Sstevel@tonic-gate 		(void) close(fd);
33877c478bd9Sstevel@tonic-gate 	} else
33887c478bd9Sstevel@tonic-gate 		warn("failed to open %s as descriptor %d", file, dfd);
33897c478bd9Sstevel@tonic-gate }
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate /*
33927c478bd9Sstevel@tonic-gate  * The Pcreate_callback() function interposes on the default, empty libproc
33937c478bd9Sstevel@tonic-gate  * definition.  It will be called following a fork of a new child process by
33947c478bd9Sstevel@tonic-gate  * Pcreate() below, but before the exec of the new process image.  We use this
33957c478bd9Sstevel@tonic-gate  * callback to optionally redirect stdin and stdout and reset the dispositions
33967c478bd9Sstevel@tonic-gate  * of SIGPIPE and SIGQUIT from SIG_IGN back to SIG_DFL.
33977c478bd9Sstevel@tonic-gate  */
33987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
33997c478bd9Sstevel@tonic-gate void
Pcreate_callback(struct ps_prochandle * P)34007c478bd9Sstevel@tonic-gate Pcreate_callback(struct ps_prochandle *P)
34017c478bd9Sstevel@tonic-gate {
34027c478bd9Sstevel@tonic-gate 	pt_data_t *pt = mdb.m_target->t_data;
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	if (pt->p_stdin != NULL)
34057c478bd9Sstevel@tonic-gate 		pt_dupfd(pt->p_stdin, O_RDWR, 0, STDIN_FILENO);
34067c478bd9Sstevel@tonic-gate 	if (pt->p_stdout != NULL)
34077c478bd9Sstevel@tonic-gate 		pt_dupfd(pt->p_stdout, O_CREAT | O_WRONLY, 0666, STDOUT_FILENO);
34087c478bd9Sstevel@tonic-gate 
3409*30699046SRichard Lowe 	(void) mdb_signal_sethandler(SIGPIPE, MDB_SIG_DFL, NULL);
3410*30699046SRichard Lowe 	(void) mdb_signal_sethandler(SIGQUIT, MDB_SIG_DFL, NULL);
34117c478bd9Sstevel@tonic-gate }
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate static int
pt_run(mdb_tgt_t * t,int argc,const mdb_arg_t * argv)34147c478bd9Sstevel@tonic-gate pt_run(mdb_tgt_t *t, int argc, const mdb_arg_t *argv)
34157c478bd9Sstevel@tonic-gate {
34167c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
34177c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
34187c478bd9Sstevel@tonic-gate 	char execname[MAXPATHLEN];
34197c478bd9Sstevel@tonic-gate 	const char **pargv;
34207c478bd9Sstevel@tonic-gate 	int pargc = 0;
34217c478bd9Sstevel@tonic-gate 	int i, perr;
34227c478bd9Sstevel@tonic-gate 	char **penv;
34237c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 	if (pt->p_aout_fio == NULL) {
34267c478bd9Sstevel@tonic-gate 		warn("run requires executable to be specified on "
34277c478bd9Sstevel@tonic-gate 		    "command-line\n");
34287c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TGT));
34297c478bd9Sstevel@tonic-gate 	}
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	pargv = mdb_alloc(sizeof (char *) * (argc + 2), UM_SLEEP);
34327c478bd9Sstevel@tonic-gate 	pargv[pargc++] = strbasename(IOP_NAME(pt->p_aout_fio));
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
34357c478bd9Sstevel@tonic-gate 		if (argv[i].a_type != MDB_TYPE_STRING) {
34367c478bd9Sstevel@tonic-gate 			mdb_free(pargv, sizeof (char *) * (argc + 2));
34377c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
34387c478bd9Sstevel@tonic-gate 		}
34397c478bd9Sstevel@tonic-gate 		if (argv[i].a_un.a_str[0] == '<')
34407c478bd9Sstevel@tonic-gate 			pt->p_stdin = argv[i].a_un.a_str + 1;
34417c478bd9Sstevel@tonic-gate 		else if (argv[i].a_un.a_str[0] == '>')
34427c478bd9Sstevel@tonic-gate 			pt->p_stdout = argv[i].a_un.a_str + 1;
34437c478bd9Sstevel@tonic-gate 		else
34447c478bd9Sstevel@tonic-gate 			pargv[pargc++] = argv[i].a_un.a_str;
34457c478bd9Sstevel@tonic-gate 	}
34467c478bd9Sstevel@tonic-gate 	pargv[pargc] = NULL;
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	/*
34497c478bd9Sstevel@tonic-gate 	 * Since Pcreate() uses execvp() and "." may not be present in $PATH,
34507c478bd9Sstevel@tonic-gate 	 * we must manually prepend "./" when the executable is a simple name.
34517c478bd9Sstevel@tonic-gate 	 */
34527c478bd9Sstevel@tonic-gate 	if (strchr(IOP_NAME(pt->p_aout_fio), '/') == NULL) {
34537c478bd9Sstevel@tonic-gate 		(void) snprintf(execname, sizeof (execname), "./%s",
34547c478bd9Sstevel@tonic-gate 		    IOP_NAME(pt->p_aout_fio));
34557c478bd9Sstevel@tonic-gate 	} else {
34567c478bd9Sstevel@tonic-gate 		(void) snprintf(execname, sizeof (execname), "%s",
34577c478bd9Sstevel@tonic-gate 		    IOP_NAME(pt->p_aout_fio));
34587c478bd9Sstevel@tonic-gate 	}
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 	penv = mdb_alloc((mdb_nv_size(&pt->p_env)+ 1) * sizeof (char *),
34617c478bd9Sstevel@tonic-gate 	    UM_SLEEP);
34627c478bd9Sstevel@tonic-gate 	for (mdb_nv_rewind(&pt->p_env), i = 0;
34637c478bd9Sstevel@tonic-gate 	    (v = mdb_nv_advance(&pt->p_env)) != NULL; i++)
34647c478bd9Sstevel@tonic-gate 		penv[i] = mdb_nv_get_cookie(v);
34657c478bd9Sstevel@tonic-gate 	penv[i] = NULL;
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 	P = Pxcreate(execname, (char **)pargv, penv, &perr, NULL, 0);
34687c478bd9Sstevel@tonic-gate 	mdb_free(pargv, sizeof (char *) * (argc + 2));
34697c478bd9Sstevel@tonic-gate 	pt->p_stdin = pt->p_stdout = NULL;
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	mdb_free(penv, i * sizeof (char *));
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 	if (P == NULL) {
34747c478bd9Sstevel@tonic-gate 		warn("failed to create process: %s\n", Pcreate_error(perr));
34757c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TGT));
34767c478bd9Sstevel@tonic-gate 	}
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
34797c478bd9Sstevel@tonic-gate 		pt_pre_detach(t, TRUE);
34807c478bd9Sstevel@tonic-gate 		if (t->t_pshandle != pt->p_idlehandle)
34817c478bd9Sstevel@tonic-gate 			Prelease(t->t_pshandle, pt->p_rflags);
34827c478bd9Sstevel@tonic-gate 	}
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	(void) Punsetflags(P, PR_RLC);	/* make sure run-on-last-close is off */
34857c478bd9Sstevel@tonic-gate 	(void) Psetflags(P, PR_KLC);	/* kill on last close by debugger */
34867c478bd9Sstevel@tonic-gate 	pt->p_rflags = PRELEASE_KILL;	/* kill on debugger Prelease */
34877c478bd9Sstevel@tonic-gate 	t->t_pshandle = P;
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate 	pt_post_attach(t);
34907c478bd9Sstevel@tonic-gate 	pt_activate_common(t);
34917c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
34927c478bd9Sstevel@tonic-gate 	mdb.m_flags |= MDB_FL_VCREATE;
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 	return (0);
34957c478bd9Sstevel@tonic-gate }
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate /*
34987c478bd9Sstevel@tonic-gate  * Forward a signal to the victim process in order to force it to stop or die.
34997c478bd9Sstevel@tonic-gate  * Refer to the comments above pt_setrun(), below, for more info.
35007c478bd9Sstevel@tonic-gate  */
35017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35027c478bd9Sstevel@tonic-gate static void
pt_sigfwd(int sig,siginfo_t * sip,ucontext_t * ucp,mdb_tgt_t * t)35037c478bd9Sstevel@tonic-gate pt_sigfwd(int sig, siginfo_t *sip, ucontext_t *ucp, mdb_tgt_t *t)
35047c478bd9Sstevel@tonic-gate {
35057c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
35067c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
35077c478bd9Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
35087c478bd9Sstevel@tonic-gate 	long ctl[2];
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	if (getpgid(pid) != mdb.m_pgid) {
35117c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_TGT, "fwd SIG#%d to %d\n", sig, (int)pid);
35127c478bd9Sstevel@tonic-gate 		(void) kill(pid, sig);
35137c478bd9Sstevel@tonic-gate 	}
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	if (Pwait(P, 1) == 0 && (psp->pr_flags & PR_STOPPED) &&
35167c478bd9Sstevel@tonic-gate 	    psp->pr_why == PR_JOBCONTROL && Pdstop(P) == 0) {
35177c478bd9Sstevel@tonic-gate 		/*
35187c478bd9Sstevel@tonic-gate 		 * If we're job control stopped and our DSTOP is pending, the
35197c478bd9Sstevel@tonic-gate 		 * victim will never see our signal, so undo the kill() and
35207c478bd9Sstevel@tonic-gate 		 * then send SIGCONT the victim to kick it out of the job
35217c478bd9Sstevel@tonic-gate 		 * control stop and force our DSTOP to take effect.
35227c478bd9Sstevel@tonic-gate 		 */
35237c478bd9Sstevel@tonic-gate 		if ((psp->pr_flags & PR_DSTOP) &&
35247c478bd9Sstevel@tonic-gate 		    prismember(&Pstatus(P)->pr_sigpend, sig)) {
35257c478bd9Sstevel@tonic-gate 			ctl[0] = PCUNKILL;
35267c478bd9Sstevel@tonic-gate 			ctl[1] = sig;
35277c478bd9Sstevel@tonic-gate 			(void) write(Pctlfd(P), ctl, sizeof (ctl));
35287c478bd9Sstevel@tonic-gate 		}
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_TGT, "fwd SIGCONT to %d\n", (int)pid);
35317c478bd9Sstevel@tonic-gate 		(void) kill(pid, SIGCONT);
35327c478bd9Sstevel@tonic-gate 	}
35337c478bd9Sstevel@tonic-gate }
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate /*
35367c478bd9Sstevel@tonic-gate  * Common code for step and continue: if no victim process has been created,
35377c478bd9Sstevel@tonic-gate  * call pt_run() to create one.  Then set the victim running, clearing any
35387c478bd9Sstevel@tonic-gate  * pending fault.  One special case is that if the victim was previously
35397c478bd9Sstevel@tonic-gate  * stopped on reception of SIGINT, we know that SIGINT was traced and the user
35407c478bd9Sstevel@tonic-gate  * requested the victim to stop, so clear this signal before continuing.
35417c478bd9Sstevel@tonic-gate  * For all other traced signals, the signal will be delivered on continue.
35427c478bd9Sstevel@tonic-gate  *
35437c478bd9Sstevel@tonic-gate  * Once the victim process is running, we wait for it to stop on an event of
35447c478bd9Sstevel@tonic-gate  * interest.  Although libproc provides the basic primitive to wait for the
35457c478bd9Sstevel@tonic-gate  * victim, we must be careful in our handling of signals.  We want to allow the
35467c478bd9Sstevel@tonic-gate  * user to issue a SIGINT or SIGQUIT using the designated terminal control
35477c478bd9Sstevel@tonic-gate  * character (typically ^C and ^\), and have these signals stop the target and
35487c478bd9Sstevel@tonic-gate  * return control to the debugger if the signals are traced.  There are three
35497c478bd9Sstevel@tonic-gate  * cases to be considered in our implementation:
35507c478bd9Sstevel@tonic-gate  *
35517c478bd9Sstevel@tonic-gate  * (1) If the debugger and victim are in the same process group, both receive
35527c478bd9Sstevel@tonic-gate  * the signal from the terminal driver.  The debugger returns from Pwait() with
35537c478bd9Sstevel@tonic-gate  * errno = EINTR, so we want to loop back and continue waiting until the victim
35547c478bd9Sstevel@tonic-gate  * stops on receipt of its SIGINT or SIGQUIT.
35557c478bd9Sstevel@tonic-gate  *
35567c478bd9Sstevel@tonic-gate  * (2) If the debugger and victim are in different process groups, and the
35577c478bd9Sstevel@tonic-gate  * victim is a member of the foreground process group, it will receive the
35587c478bd9Sstevel@tonic-gate  * signal from the terminal driver and the debugger will not.  As such, we
35597c478bd9Sstevel@tonic-gate  * will remain blocked in Pwait() until the victim stops on its signal.
35607c478bd9Sstevel@tonic-gate  *
35617c478bd9Sstevel@tonic-gate  * (3) If the debugger and victim are in different process groups, and the
35627c478bd9Sstevel@tonic-gate  * debugger is a member of the foreground process group, it will receive the
35637c478bd9Sstevel@tonic-gate  * signal from the terminal driver, and the victim will not.  The debugger
35647c478bd9Sstevel@tonic-gate  * returns from Pwait() with errno = EINTR, so we need to forward the signal
35657c478bd9Sstevel@tonic-gate  * to the victim process directly and then Pwait() again for it to stop.
35667c478bd9Sstevel@tonic-gate  *
35677c478bd9Sstevel@tonic-gate  * We can observe that all three cases are handled by simply calling Pwait()
35687c478bd9Sstevel@tonic-gate  * repeatedly if it fails with EINTR, and forwarding SIGINT and SIGQUIT to
35697c478bd9Sstevel@tonic-gate  * the victim if it is in a different process group, using pt_sigfwd() above.
35707c478bd9Sstevel@tonic-gate  *
35717c478bd9Sstevel@tonic-gate  * An additional complication is that the process may not be able to field
35727c478bd9Sstevel@tonic-gate  * the signal if it is currently stopped by job control.  In this case, we
35737c478bd9Sstevel@tonic-gate  * also DSTOP the process, and then send it a SIGCONT to wake it up from
35747c478bd9Sstevel@tonic-gate  * job control and force it to re-enter stop() under the control of /proc.
35757c478bd9Sstevel@tonic-gate  *
35767c478bd9Sstevel@tonic-gate  * Finally, we would like to allow the user to suspend the process using the
35777c478bd9Sstevel@tonic-gate  * terminal suspend character (typically ^Z) if both are in the same session.
35787c478bd9Sstevel@tonic-gate  * We again employ pt_sigfwd() to forward SIGTSTP to the victim, wait for it to
35797c478bd9Sstevel@tonic-gate  * stop from job control, and then capture it using /proc.  Once the process
35807c478bd9Sstevel@tonic-gate  * has stopped, normal SIGTSTP processing is restored and the user can issue
35817c478bd9Sstevel@tonic-gate  * another ^Z in order to suspend the debugger and return to the parent shell.
35827c478bd9Sstevel@tonic-gate  */
35837c478bd9Sstevel@tonic-gate static int
pt_setrun(mdb_tgt_t * t,mdb_tgt_status_t * tsp,int flags)35847c478bd9Sstevel@tonic-gate pt_setrun(mdb_tgt_t *t, mdb_tgt_status_t *tsp, int flags)
35857c478bd9Sstevel@tonic-gate {
35867c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
35877c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
35887c478bd9Sstevel@tonic-gate 	pid_t old_pgid = -1;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	mdb_signal_f *intf, *quitf, *tstpf;
35917c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp;
35927c478bd9Sstevel@tonic-gate 	void *intd, *quitd, *tstpd;
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	int sig = pt->p_signal;
35957c478bd9Sstevel@tonic-gate 	int error = 0;
35967c478bd9Sstevel@tonic-gate 	int pgid = -1;
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate 	pt->p_signal = 0; /* clear pending signal */
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 	if (P == NULL && pt_run(t, 0, NULL) == -1)
36017c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	P = t->t_pshandle;
36047c478bd9Sstevel@tonic-gate 	psp = &Pstatus(P)->pr_lwp;
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	if (sig == 0 && psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
36077c478bd9Sstevel@tonic-gate 		flags |= PRCSIG; /* clear pending SIGINT */
36087c478bd9Sstevel@tonic-gate 	else
36097c478bd9Sstevel@tonic-gate 		flags |= PRCFAULT; /* clear any pending fault (e.g. BPT) */
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 	intf = mdb_signal_gethandler(SIGINT, &intd);
36127c478bd9Sstevel@tonic-gate 	quitf = mdb_signal_gethandler(SIGQUIT, &quitd);
36137c478bd9Sstevel@tonic-gate 	tstpf = mdb_signal_gethandler(SIGTSTP, &tstpd);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	(void) mdb_signal_sethandler(SIGINT, (mdb_signal_f *)pt_sigfwd, t);
36167c478bd9Sstevel@tonic-gate 	(void) mdb_signal_sethandler(SIGQUIT, (mdb_signal_f *)pt_sigfwd, t);
36177c478bd9Sstevel@tonic-gate 	(void) mdb_signal_sethandler(SIGTSTP, (mdb_signal_f *)pt_sigfwd, t);
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	if (sig != 0 && Pstate(P) == PS_RUN &&
36207c478bd9Sstevel@tonic-gate 	    kill(Pstatus(P)->pr_pid, sig) == -1) {
36217c478bd9Sstevel@tonic-gate 		error = errno;
36227c478bd9Sstevel@tonic-gate 		goto out;
36237c478bd9Sstevel@tonic-gate 	}
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 	/*
36267c478bd9Sstevel@tonic-gate 	 * If we attached to a job stopped background process in the same
36277c478bd9Sstevel@tonic-gate 	 * session, make its pgid the foreground process group before running
36287c478bd9Sstevel@tonic-gate 	 * it.  Ignore SIGTTOU while doing this to avoid being suspended.
36297c478bd9Sstevel@tonic-gate 	 */
36307c478bd9Sstevel@tonic-gate 	if (mdb.m_flags & MDB_FL_JOBCTL) {
3631*30699046SRichard Lowe 		(void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_IGN, NULL);
36327c478bd9Sstevel@tonic-gate 		(void) IOP_CTL(mdb.m_term, TIOCGPGRP, &old_pgid);
36337c478bd9Sstevel@tonic-gate 		(void) IOP_CTL(mdb.m_term, TIOCSPGRP,
36347c478bd9Sstevel@tonic-gate 		    (void *)&Pstatus(P)->pr_pgid);
3635*30699046SRichard Lowe 		(void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_DFL, NULL);
36367c478bd9Sstevel@tonic-gate 	}
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	if (Pstate(P) != PS_RUN && Psetrun(P, sig, flags) == -1) {
36397c478bd9Sstevel@tonic-gate 		error = errno;
36407c478bd9Sstevel@tonic-gate 		goto out;
36417c478bd9Sstevel@tonic-gate 	}
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate 	/*
36447c478bd9Sstevel@tonic-gate 	 * If the process is stopped on job control, resume its process group
36457c478bd9Sstevel@tonic-gate 	 * by sending it a SIGCONT if we are in the same session.  Otherwise
36467c478bd9Sstevel@tonic-gate 	 * we have no choice but to wait for someone else to foreground it.
36477c478bd9Sstevel@tonic-gate 	 */
36487c478bd9Sstevel@tonic-gate 	if (psp->pr_why == PR_JOBCONTROL) {
36497c478bd9Sstevel@tonic-gate 		if (mdb.m_flags & MDB_FL_JOBCTL)
36507c478bd9Sstevel@tonic-gate 			(void) kill(-Pstatus(P)->pr_pgid, SIGCONT);
36517c478bd9Sstevel@tonic-gate 		else if (mdb.m_term != NULL)
36527c478bd9Sstevel@tonic-gate 			warn("process is still suspended by job control ...\n");
36537c478bd9Sstevel@tonic-gate 	}
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	/*
36567c478bd9Sstevel@tonic-gate 	 * Wait for the process to stop.  As described above, we loop around if
36577c478bd9Sstevel@tonic-gate 	 * we are interrupted (EINTR).  If we lose control, attempt to re-open
36587c478bd9Sstevel@tonic-gate 	 * the process, or call pt_exec() if that fails to handle a re-exec.
36597c478bd9Sstevel@tonic-gate 	 * If the process dies (ENOENT) or Pwait() fails, break out of the loop.
36607c478bd9Sstevel@tonic-gate 	 */
36617c478bd9Sstevel@tonic-gate 	while (Pwait(P, 0) == -1) {
36627c478bd9Sstevel@tonic-gate 		if (errno != EINTR) {
36637c478bd9Sstevel@tonic-gate 			if (Pstate(P) == PS_LOST) {
36647c478bd9Sstevel@tonic-gate 				if (Preopen(P) == 0)
36657c478bd9Sstevel@tonic-gate 					continue; /* Pwait() again */
36667c478bd9Sstevel@tonic-gate 				else
36677c478bd9Sstevel@tonic-gate 					pt_exec(t, 0, NULL);
36687c478bd9Sstevel@tonic-gate 			} else if (errno != ENOENT)
36697c478bd9Sstevel@tonic-gate 				warn("failed to wait for event");
36707c478bd9Sstevel@tonic-gate 			break;
36717c478bd9Sstevel@tonic-gate 		}
36727c478bd9Sstevel@tonic-gate 	}
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate 	/*
36757c478bd9Sstevel@tonic-gate 	 * If we changed the foreground process group, restore the old pgid
36767c478bd9Sstevel@tonic-gate 	 * while ignoring SIGTTOU so we are not accidentally suspended.
36777c478bd9Sstevel@tonic-gate 	 */
36787c478bd9Sstevel@tonic-gate 	if (old_pgid != -1) {
3679*30699046SRichard Lowe 		(void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_IGN, NULL);
36807c478bd9Sstevel@tonic-gate 		(void) IOP_CTL(mdb.m_term, TIOCSPGRP, &pgid);
3681*30699046SRichard Lowe 		(void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_DFL, NULL);
36827c478bd9Sstevel@tonic-gate 	}
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 	/*
36857c478bd9Sstevel@tonic-gate 	 * If we're now stopped on exit from a successful exec, release any
36867c478bd9Sstevel@tonic-gate 	 * vfork parents and clean out their address space before returning
36877c478bd9Sstevel@tonic-gate 	 * to tgt_continue() and perturbing the list of armed event specs.
36887c478bd9Sstevel@tonic-gate 	 * If we're stopped for any other reason, just update the mappings.
36897c478bd9Sstevel@tonic-gate 	 */
36907c478bd9Sstevel@tonic-gate 	switch (Pstate(P)) {
36917c478bd9Sstevel@tonic-gate 	case PS_STOP:
36927c478bd9Sstevel@tonic-gate 		if (psp->pr_why == PR_SYSEXIT && psp->pr_errno == 0 &&
36938fd04b83SRoger A. Faulkner 		    psp->pr_what == SYS_execve)
36947c478bd9Sstevel@tonic-gate 			pt_release_parents(t);
36957c478bd9Sstevel@tonic-gate 		else
36967c478bd9Sstevel@tonic-gate 			Pupdate_maps(P);
36977c478bd9Sstevel@tonic-gate 		break;
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
37007c478bd9Sstevel@tonic-gate 	case PS_LOST:
37017c478bd9Sstevel@tonic-gate 		pt_release_parents(t);
37027c478bd9Sstevel@tonic-gate 		break;
37037c478bd9Sstevel@tonic-gate 	}
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate out:
37067c478bd9Sstevel@tonic-gate 	(void) mdb_signal_sethandler(SIGINT, intf, intd);
37077c478bd9Sstevel@tonic-gate 	(void) mdb_signal_sethandler(SIGQUIT, quitf, quitd);
37087c478bd9Sstevel@tonic-gate 	(void) mdb_signal_sethandler(SIGTSTP, tstpf, tstpd);
37097c478bd9Sstevel@tonic-gate 	(void) pt_status(t, tsp);
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
37127c478bd9Sstevel@tonic-gate }
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate static int
pt_step(mdb_tgt_t * t,mdb_tgt_status_t * tsp)37157c478bd9Sstevel@tonic-gate pt_step(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
37167c478bd9Sstevel@tonic-gate {
37177c478bd9Sstevel@tonic-gate 	return (pt_setrun(t, tsp, PRSTEP));
37187c478bd9Sstevel@tonic-gate }
37197c478bd9Sstevel@tonic-gate 
37207c478bd9Sstevel@tonic-gate static int
pt_continue(mdb_tgt_t * t,mdb_tgt_status_t * tsp)37217c478bd9Sstevel@tonic-gate pt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
37227c478bd9Sstevel@tonic-gate {
37237c478bd9Sstevel@tonic-gate 	return (pt_setrun(t, tsp, 0));
37247c478bd9Sstevel@tonic-gate }
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate static int
pt_signal(mdb_tgt_t * t,int sig)37277c478bd9Sstevel@tonic-gate pt_signal(mdb_tgt_t *t, int sig)
37287c478bd9Sstevel@tonic-gate {
37297c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	if (sig > 0 && sig <= pt->p_maxsig) {
37327c478bd9Sstevel@tonic-gate 		pt->p_signal = sig; /* pending until next pt_setrun */
37337c478bd9Sstevel@tonic-gate 		return (0);
37347c478bd9Sstevel@tonic-gate 	}
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_BADSIGNUM));
37377c478bd9Sstevel@tonic-gate }
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate static int
pt_sysenter_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)37407c478bd9Sstevel@tonic-gate pt_sysenter_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
37417c478bd9Sstevel@tonic-gate {
37427c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 	if (P != NULL && Pstate(P) < PS_LOST) {
37457c478bd9Sstevel@tonic-gate 		sep->se_data = args; /* data is raw system call number */
37467c478bd9Sstevel@tonic-gate 		return (Psysentry(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
37477c478bd9Sstevel@tonic-gate 	}
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
37507c478bd9Sstevel@tonic-gate }
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate static void
pt_sysenter_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)37537c478bd9Sstevel@tonic-gate pt_sysenter_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
37547c478bd9Sstevel@tonic-gate {
37557c478bd9Sstevel@tonic-gate 	(void) Psysentry(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
37567c478bd9Sstevel@tonic-gate }
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
37597c478bd9Sstevel@tonic-gate static char *
pt_sysenter_info(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_vespec_t * vep,mdb_tgt_spec_desc_t * sp,char * buf,size_t nbytes)37607c478bd9Sstevel@tonic-gate pt_sysenter_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
37617c478bd9Sstevel@tonic-gate     mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
37627c478bd9Sstevel@tonic-gate {
37637c478bd9Sstevel@tonic-gate 	char name[32];
37647c478bd9Sstevel@tonic-gate 	int sysnum;
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	if (vep != NULL)
37677c478bd9Sstevel@tonic-gate 		sysnum = (intptr_t)vep->ve_args;
37687c478bd9Sstevel@tonic-gate 	else
37697c478bd9Sstevel@tonic-gate 		sysnum = (intptr_t)sep->se_data;
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 	(void) proc_sysname(sysnum, name, sizeof (name));
37727c478bd9Sstevel@tonic-gate 	(void) mdb_iob_snprintf(buf, nbytes, "stop on entry to %s", name);
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	return (buf);
37757c478bd9Sstevel@tonic-gate }
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
37787c478bd9Sstevel@tonic-gate static int
pt_sysenter_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)37797c478bd9Sstevel@tonic-gate pt_sysenter_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
37807c478bd9Sstevel@tonic-gate {
37817c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
37827c478bd9Sstevel@tonic-gate 	int sysnum = (intptr_t)sep->se_data;
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	return (psp->pr_why == PR_SYSENTRY && psp->pr_what == sysnum);
37857c478bd9Sstevel@tonic-gate }
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_sysenter_ops = {
37880c1b95beSRichard Lowe 	.se_ctor = pt_sysenter_ctor,
37890c1b95beSRichard Lowe 	.se_dtor = pt_sysenter_dtor,
37900c1b95beSRichard Lowe 	.se_info = pt_sysenter_info,
37910c1b95beSRichard Lowe 	.se_secmp = no_se_secmp,
37920c1b95beSRichard Lowe 	.se_vecmp = no_se_vecmp,
37930c1b95beSRichard Lowe 	.se_arm = no_se_arm,
37940c1b95beSRichard Lowe 	.se_disarm = no_se_disarm,
37950c1b95beSRichard Lowe 	.se_cont = no_se_cont,
37960c1b95beSRichard Lowe 	.se_match = pt_sysenter_match,
37977c478bd9Sstevel@tonic-gate };
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate static int
pt_sysexit_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)38007c478bd9Sstevel@tonic-gate pt_sysexit_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
38017c478bd9Sstevel@tonic-gate {
38027c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 	if (P != NULL && Pstate(P) < PS_LOST) {
38057c478bd9Sstevel@tonic-gate 		sep->se_data = args; /* data is raw system call number */
38067c478bd9Sstevel@tonic-gate 		return (Psysexit(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
38077c478bd9Sstevel@tonic-gate 	}
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
38107c478bd9Sstevel@tonic-gate }
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate static void
pt_sysexit_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)38137c478bd9Sstevel@tonic-gate pt_sysexit_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
38147c478bd9Sstevel@tonic-gate {
38157c478bd9Sstevel@tonic-gate 	(void) Psysexit(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
38167c478bd9Sstevel@tonic-gate }
38177c478bd9Sstevel@tonic-gate 
38187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38197c478bd9Sstevel@tonic-gate static char *
pt_sysexit_info(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_vespec_t * vep,mdb_tgt_spec_desc_t * sp,char * buf,size_t nbytes)38207c478bd9Sstevel@tonic-gate pt_sysexit_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
38217c478bd9Sstevel@tonic-gate     mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
38227c478bd9Sstevel@tonic-gate {
38237c478bd9Sstevel@tonic-gate 	char name[32];
38247c478bd9Sstevel@tonic-gate 	int sysnum;
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	if (vep != NULL)
38277c478bd9Sstevel@tonic-gate 		sysnum = (intptr_t)vep->ve_args;
38287c478bd9Sstevel@tonic-gate 	else
38297c478bd9Sstevel@tonic-gate 		sysnum = (intptr_t)sep->se_data;
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 	(void) proc_sysname(sysnum, name, sizeof (name));
38327c478bd9Sstevel@tonic-gate 	(void) mdb_iob_snprintf(buf, nbytes, "stop on exit from %s", name);
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	return (buf);
38357c478bd9Sstevel@tonic-gate }
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38387c478bd9Sstevel@tonic-gate static int
pt_sysexit_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)38397c478bd9Sstevel@tonic-gate pt_sysexit_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
38407c478bd9Sstevel@tonic-gate {
38417c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
38427c478bd9Sstevel@tonic-gate 	int sysnum = (intptr_t)sep->se_data;
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	return (psp->pr_why == PR_SYSEXIT && psp->pr_what == sysnum);
38457c478bd9Sstevel@tonic-gate }
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_sysexit_ops = {
38480c1b95beSRichard Lowe 	.se_ctor = pt_sysexit_ctor,
38490c1b95beSRichard Lowe 	.se_dtor = pt_sysexit_dtor,
38500c1b95beSRichard Lowe 	.se_info = pt_sysexit_info,
38510c1b95beSRichard Lowe 	.se_secmp = no_se_secmp,
38520c1b95beSRichard Lowe 	.se_vecmp = no_se_vecmp,
38530c1b95beSRichard Lowe 	.se_arm = no_se_arm,
38540c1b95beSRichard Lowe 	.se_disarm = no_se_disarm,
38550c1b95beSRichard Lowe 	.se_cont = no_se_cont,
38560c1b95beSRichard Lowe 	.se_match = pt_sysexit_match,
38577c478bd9Sstevel@tonic-gate };
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate static int
pt_signal_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)38607c478bd9Sstevel@tonic-gate pt_signal_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
38617c478bd9Sstevel@tonic-gate {
38627c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 	if (P != NULL && Pstate(P) < PS_LOST) {
38657c478bd9Sstevel@tonic-gate 		sep->se_data = args; /* data is raw signal number */
38667c478bd9Sstevel@tonic-gate 		return (Psignal(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
38677c478bd9Sstevel@tonic-gate 	}
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
38707c478bd9Sstevel@tonic-gate }
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate static void
pt_signal_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)38737c478bd9Sstevel@tonic-gate pt_signal_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
38747c478bd9Sstevel@tonic-gate {
38757c478bd9Sstevel@tonic-gate 	(void) Psignal(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
38767c478bd9Sstevel@tonic-gate }
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38797c478bd9Sstevel@tonic-gate static char *
pt_signal_info(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_vespec_t * vep,mdb_tgt_spec_desc_t * sp,char * buf,size_t nbytes)38807c478bd9Sstevel@tonic-gate pt_signal_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
38817c478bd9Sstevel@tonic-gate     mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
38827c478bd9Sstevel@tonic-gate {
38837c478bd9Sstevel@tonic-gate 	char name[SIG2STR_MAX];
38847c478bd9Sstevel@tonic-gate 	int signum;
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate 	if (vep != NULL)
38877c478bd9Sstevel@tonic-gate 		signum = (intptr_t)vep->ve_args;
38887c478bd9Sstevel@tonic-gate 	else
38897c478bd9Sstevel@tonic-gate 		signum = (intptr_t)sep->se_data;
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate 	(void) proc_signame(signum, name, sizeof (name));
38927c478bd9Sstevel@tonic-gate 	(void) mdb_iob_snprintf(buf, nbytes, "stop on %s", name);
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 	return (buf);
38957c478bd9Sstevel@tonic-gate }
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38987c478bd9Sstevel@tonic-gate static int
pt_signal_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)38997c478bd9Sstevel@tonic-gate pt_signal_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
39007c478bd9Sstevel@tonic-gate {
39017c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
39027c478bd9Sstevel@tonic-gate 	int signum = (intptr_t)sep->se_data;
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	return (psp->pr_why == PR_SIGNALLED && psp->pr_what == signum);
39057c478bd9Sstevel@tonic-gate }
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_signal_ops = {
39080c1b95beSRichard Lowe 	.se_ctor = pt_signal_ctor,
39090c1b95beSRichard Lowe 	.se_dtor = pt_signal_dtor,
39100c1b95beSRichard Lowe 	.se_info = pt_signal_info,
39110c1b95beSRichard Lowe 	.se_secmp = no_se_secmp,
39120c1b95beSRichard Lowe 	.se_vecmp = no_se_vecmp,
39130c1b95beSRichard Lowe 	.se_arm = no_se_arm,
39140c1b95beSRichard Lowe 	.se_disarm = no_se_disarm,
39150c1b95beSRichard Lowe 	.se_cont = no_se_cont,
39160c1b95beSRichard Lowe 	.se_match = pt_signal_match,
39177c478bd9Sstevel@tonic-gate };
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate static int
pt_fault_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)39207c478bd9Sstevel@tonic-gate pt_fault_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
39217c478bd9Sstevel@tonic-gate {
39227c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate 	if (P != NULL && Pstate(P) < PS_LOST) {
39257c478bd9Sstevel@tonic-gate 		sep->se_data = args; /* data is raw fault number */
39267c478bd9Sstevel@tonic-gate 		return (Pfault(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
39277c478bd9Sstevel@tonic-gate 	}
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
39307c478bd9Sstevel@tonic-gate }
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate static void
pt_fault_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)39337c478bd9Sstevel@tonic-gate pt_fault_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
39347c478bd9Sstevel@tonic-gate {
39357c478bd9Sstevel@tonic-gate 	int fault = (intptr_t)sep->se_data;
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	if (fault != FLTBPT && fault != FLTTRACE && fault != FLTWATCH)
39387c478bd9Sstevel@tonic-gate 		(void) Pfault(t->t_pshandle, fault, FALSE);
39397c478bd9Sstevel@tonic-gate }
39407c478bd9Sstevel@tonic-gate 
39417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39427c478bd9Sstevel@tonic-gate static char *
pt_fault_info(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_vespec_t * vep,mdb_tgt_spec_desc_t * sp,char * buf,size_t nbytes)39437c478bd9Sstevel@tonic-gate pt_fault_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
39447c478bd9Sstevel@tonic-gate     mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
39457c478bd9Sstevel@tonic-gate {
39467c478bd9Sstevel@tonic-gate 	char name[32];
39477c478bd9Sstevel@tonic-gate 	int fltnum;
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 	if (vep != NULL)
39507c478bd9Sstevel@tonic-gate 		fltnum = (intptr_t)vep->ve_args;
39517c478bd9Sstevel@tonic-gate 	else
39527c478bd9Sstevel@tonic-gate 		fltnum = (intptr_t)sep->se_data;
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate 	(void) proc_fltname(fltnum, name, sizeof (name));
39557c478bd9Sstevel@tonic-gate 	(void) mdb_iob_snprintf(buf, nbytes, "stop on %s", name);
39567c478bd9Sstevel@tonic-gate 
39577c478bd9Sstevel@tonic-gate 	return (buf);
39587c478bd9Sstevel@tonic-gate }
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39617c478bd9Sstevel@tonic-gate static int
pt_fault_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)39627c478bd9Sstevel@tonic-gate pt_fault_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
39637c478bd9Sstevel@tonic-gate {
39647c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
39657c478bd9Sstevel@tonic-gate 	int fltnum = (intptr_t)sep->se_data;
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 	return (psp->pr_why == PR_FAULTED && psp->pr_what == fltnum);
39687c478bd9Sstevel@tonic-gate }
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_fault_ops = {
39710c1b95beSRichard Lowe 	.se_ctor = pt_fault_ctor,
39720c1b95beSRichard Lowe 	.se_dtor = pt_fault_dtor,
39730c1b95beSRichard Lowe 	.se_info = pt_fault_info,
39740c1b95beSRichard Lowe 	.se_secmp = no_se_secmp,
39750c1b95beSRichard Lowe 	.se_vecmp = no_se_vecmp,
39760c1b95beSRichard Lowe 	.se_arm = no_se_arm,
39770c1b95beSRichard Lowe 	.se_disarm = no_se_disarm,
39780c1b95beSRichard Lowe 	.se_cont = no_se_cont,
39790c1b95beSRichard Lowe 	.se_match = pt_fault_match,
39807c478bd9Sstevel@tonic-gate };
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate /*
39837c478bd9Sstevel@tonic-gate  * Callback for pt_ignore() dcmd above: for each VID, determine if it
39847c478bd9Sstevel@tonic-gate  * corresponds to a vespec that traces the specified signal, and delete it.
39857c478bd9Sstevel@tonic-gate  */
39867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39877c478bd9Sstevel@tonic-gate static int
pt_ignore_sig(mdb_tgt_t * t,void * sig,int vid,void * data)39887c478bd9Sstevel@tonic-gate pt_ignore_sig(mdb_tgt_t *t, void *sig, int vid, void *data)
39897c478bd9Sstevel@tonic-gate {
39907c478bd9Sstevel@tonic-gate 	mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, vid);
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate 	if (vep->ve_se->se_ops == &proc_signal_ops && vep->ve_args == sig)
39937c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_vespec_delete(t, vid);
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate 	return (0);
39967c478bd9Sstevel@tonic-gate }
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate static int
pt_brkpt_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)39997c478bd9Sstevel@tonic-gate pt_brkpt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
40007c478bd9Sstevel@tonic-gate {
40017c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
40027c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta = args;
40037c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb;
40047c478bd9Sstevel@tonic-gate 	GElf_Sym s;
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || Pstate(t->t_pshandle) >= PS_LOST)
40077c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 	if (pta->pta_symbol != NULL) {
40107c478bd9Sstevel@tonic-gate 		if (!pt->p_rtld_finished &&
40117c478bd9Sstevel@tonic-gate 		    strchr(pta->pta_symbol, '`') == NULL)
40127c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_NOSYM));
40137c478bd9Sstevel@tonic-gate 		if (mdb_tgt_lookup_by_scope(t, pta->pta_symbol, &s,
40147c478bd9Sstevel@tonic-gate 		    NULL) == -1) {
40157c478bd9Sstevel@tonic-gate 			if (errno != EMDB_NOOBJ && !(errno == EMDB_NOSYM &&
40167c478bd9Sstevel@tonic-gate 			    (!(mdb.m_flags & MDB_FL_BPTNOSYMSTOP) ||
40177c478bd9Sstevel@tonic-gate 			    !pt->p_rtld_finished))) {
40187c478bd9Sstevel@tonic-gate 				warn("breakpoint %s activation failed",
40197c478bd9Sstevel@tonic-gate 				    pta->pta_symbol);
40207c478bd9Sstevel@tonic-gate 			}
40217c478bd9Sstevel@tonic-gate 			return (-1); /* errno is set for us */
40227c478bd9Sstevel@tonic-gate 		}
40237c478bd9Sstevel@tonic-gate 
40247c478bd9Sstevel@tonic-gate 		pta->pta_addr = (uintptr_t)s.st_value;
40257c478bd9Sstevel@tonic-gate 	}
40267c478bd9Sstevel@tonic-gate 
40277c478bd9Sstevel@tonic-gate #ifdef __sparc
40287c478bd9Sstevel@tonic-gate 	if (pta->pta_addr & 3)
40297c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_BPALIGN));
40307c478bd9Sstevel@tonic-gate #endif
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 	if (Paddr_to_map(t->t_pshandle, pta->pta_addr) == NULL)
40337c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOMAP));
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	ptb = mdb_alloc(sizeof (pt_brkpt_t), UM_SLEEP);
40367c478bd9Sstevel@tonic-gate 	ptb->ptb_addr = pta->pta_addr;
4037892ad162SToomas Soome 	ptb->ptb_instr = 0;
40387c478bd9Sstevel@tonic-gate 	sep->se_data = ptb;
40397c478bd9Sstevel@tonic-gate 
40407c478bd9Sstevel@tonic-gate 	return (0);
40417c478bd9Sstevel@tonic-gate }
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40447c478bd9Sstevel@tonic-gate static void
pt_brkpt_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)40457c478bd9Sstevel@tonic-gate pt_brkpt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
40467c478bd9Sstevel@tonic-gate {
40477c478bd9Sstevel@tonic-gate 	mdb_free(sep->se_data, sizeof (pt_brkpt_t));
40487c478bd9Sstevel@tonic-gate }
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40517c478bd9Sstevel@tonic-gate static char *
pt_brkpt_info(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_vespec_t * vep,mdb_tgt_spec_desc_t * sp,char * buf,size_t nbytes)40527c478bd9Sstevel@tonic-gate pt_brkpt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
40537c478bd9Sstevel@tonic-gate     mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
40547c478bd9Sstevel@tonic-gate {
4055892ad162SToomas Soome 	uintptr_t addr = 0;
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 	if (vep != NULL) {
40587c478bd9Sstevel@tonic-gate 		pt_bparg_t *pta = vep->ve_args;
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 		if (pta->pta_symbol != NULL) {
40617c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(buf, nbytes, "stop at %s",
40627c478bd9Sstevel@tonic-gate 			    pta->pta_symbol);
40637c478bd9Sstevel@tonic-gate 		} else {
40647c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(buf, nbytes, "stop at %a",
40657c478bd9Sstevel@tonic-gate 			    pta->pta_addr);
40667c478bd9Sstevel@tonic-gate 			addr = pta->pta_addr;
40677c478bd9Sstevel@tonic-gate 		}
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	} else {
40707c478bd9Sstevel@tonic-gate 		addr = ((pt_brkpt_t *)sep->se_data)->ptb_addr;
40717c478bd9Sstevel@tonic-gate 		(void) mdb_iob_snprintf(buf, nbytes, "stop at %a", addr);
40727c478bd9Sstevel@tonic-gate 	}
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate 	sp->spec_base = addr;
40757c478bd9Sstevel@tonic-gate 	sp->spec_size = sizeof (instr_t);
40767c478bd9Sstevel@tonic-gate 
40777c478bd9Sstevel@tonic-gate 	return (buf);
40787c478bd9Sstevel@tonic-gate }
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate static int
pt_brkpt_secmp(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)40817c478bd9Sstevel@tonic-gate pt_brkpt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
40827c478bd9Sstevel@tonic-gate {
40837c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb = sep->se_data;
40847c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta = args;
40857c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	if (pta->pta_symbol != NULL) {
40887c478bd9Sstevel@tonic-gate 		return (mdb_tgt_lookup_by_scope(t, pta->pta_symbol,
40897c478bd9Sstevel@tonic-gate 		    &sym, NULL) == 0 && sym.st_value == ptb->ptb_addr);
40907c478bd9Sstevel@tonic-gate 	}
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate 	return (pta->pta_addr == ptb->ptb_addr);
40937c478bd9Sstevel@tonic-gate }
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40967c478bd9Sstevel@tonic-gate static int
pt_brkpt_vecmp(mdb_tgt_t * t,mdb_vespec_t * vep,void * args)40977c478bd9Sstevel@tonic-gate pt_brkpt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
40987c478bd9Sstevel@tonic-gate {
40997c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta1 = vep->ve_args;
41007c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta2 = args;
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate 	if (pta1->pta_symbol != NULL && pta2->pta_symbol != NULL)
41037c478bd9Sstevel@tonic-gate 		return (strcmp(pta1->pta_symbol, pta2->pta_symbol) == 0);
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 	if (pta1->pta_symbol == NULL && pta2->pta_symbol == NULL)
41067c478bd9Sstevel@tonic-gate 		return (pta1->pta_addr == pta2->pta_addr);
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	return (0); /* fail if one is symbolic, other is an explicit address */
41097c478bd9Sstevel@tonic-gate }
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate static int
pt_brkpt_arm(mdb_tgt_t * t,mdb_sespec_t * sep)41127c478bd9Sstevel@tonic-gate pt_brkpt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
41137c478bd9Sstevel@tonic-gate {
41147c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb = sep->se_data;
41157c478bd9Sstevel@tonic-gate 	return (Psetbkpt(t->t_pshandle, ptb->ptb_addr, &ptb->ptb_instr));
41167c478bd9Sstevel@tonic-gate }
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate /*
41197c478bd9Sstevel@tonic-gate  * In order to disarm a breakpoint, we replace the trap instruction at ptb_addr
41207c478bd9Sstevel@tonic-gate  * with the saved instruction.  However, if we have stopped after a successful
41217c478bd9Sstevel@tonic-gate  * exec(2), we do not want to restore ptb_instr because the address space has
41227c478bd9Sstevel@tonic-gate  * now been replaced with the text of a different executable, and so restoring
41237c478bd9Sstevel@tonic-gate  * the saved instruction would be incorrect.  The exec itself has effectively
41247c478bd9Sstevel@tonic-gate  * removed all breakpoint trap instructions for us, so we can just return.
41257c478bd9Sstevel@tonic-gate  */
41267c478bd9Sstevel@tonic-gate static int
pt_brkpt_disarm(mdb_tgt_t * t,mdb_sespec_t * sep)41277c478bd9Sstevel@tonic-gate pt_brkpt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
41287c478bd9Sstevel@tonic-gate {
41297c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
41307c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb = sep->se_data;
41317c478bd9Sstevel@tonic-gate 
41328fd04b83SRoger A. Faulkner 	if (psp->pr_why == PR_SYSEXIT && psp->pr_errno == 0 &&
41338fd04b83SRoger A. Faulkner 	    psp->pr_what == SYS_execve)
41347c478bd9Sstevel@tonic-gate 		return (0); /* do not restore saved instruction */
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 	return (Pdelbkpt(t->t_pshandle, ptb->ptb_addr, ptb->ptb_instr));
41377c478bd9Sstevel@tonic-gate }
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate /*
41407c478bd9Sstevel@tonic-gate  * Determine whether the specified sespec is an armed watchpoint that overlaps
41417c478bd9Sstevel@tonic-gate  * with the given breakpoint and has the given flags set.  We use this to find
41427c478bd9Sstevel@tonic-gate  * conflicts with breakpoints, below.
41437c478bd9Sstevel@tonic-gate  */
41447c478bd9Sstevel@tonic-gate static int
pt_wp_overlap(mdb_sespec_t * sep,pt_brkpt_t * ptb,int flags)41457c478bd9Sstevel@tonic-gate pt_wp_overlap(mdb_sespec_t *sep, pt_brkpt_t *ptb, int flags)
41467c478bd9Sstevel@tonic-gate {
41477c478bd9Sstevel@tonic-gate 	const prwatch_t *wp = sep->se_data;
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 	return (sep->se_state == MDB_TGT_SPEC_ARMED &&
41507c478bd9Sstevel@tonic-gate 	    sep->se_ops == &proc_wapt_ops && (wp->pr_wflags & flags) &&
41517c478bd9Sstevel@tonic-gate 	    ptb->ptb_addr - wp->pr_vaddr < wp->pr_size);
41527c478bd9Sstevel@tonic-gate }
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate /*
41557c478bd9Sstevel@tonic-gate  * We step over breakpoints using Pxecbkpt() in libproc.  If a conflicting
41567c478bd9Sstevel@tonic-gate  * watchpoint is present, we must temporarily remove it before stepping over
41577c478bd9Sstevel@tonic-gate  * the breakpoint so we do not immediately re-trigger the watchpoint.  We know
41587c478bd9Sstevel@tonic-gate  * the watchpoint has already triggered on our trap instruction as part of
41597c478bd9Sstevel@tonic-gate  * fetching it.  Before we return, we must re-install any disabled watchpoints.
41607c478bd9Sstevel@tonic-gate  */
41617c478bd9Sstevel@tonic-gate static int
pt_brkpt_cont(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)41627c478bd9Sstevel@tonic-gate pt_brkpt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
41637c478bd9Sstevel@tonic-gate {
41647c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb = sep->se_data;
41657c478bd9Sstevel@tonic-gate 	int status = -1;
41667c478bd9Sstevel@tonic-gate 	int error;
41677c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 	/*
41707c478bd9Sstevel@tonic-gate 	 * If the PC no longer matches our original address, then the user has
41717c478bd9Sstevel@tonic-gate 	 * changed it while we have been stopped. In this case, it no longer
41727c478bd9Sstevel@tonic-gate 	 * makes any sense to continue over this breakpoint.  We return as if we
41737c478bd9Sstevel@tonic-gate 	 * continued normally.
41747c478bd9Sstevel@tonic-gate 	 */
41757c478bd9Sstevel@tonic-gate 	if ((uintptr_t)psp->pr_info.si_addr != psp->pr_reg[R_PC])
41767c478bd9Sstevel@tonic-gate 		return (pt_status(t, tsp));
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate 	for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
41797c478bd9Sstevel@tonic-gate 		if (pt_wp_overlap(sep, ptb, WA_EXEC))
41807c478bd9Sstevel@tonic-gate 			(void) Pdelwapt(t->t_pshandle, sep->se_data);
41817c478bd9Sstevel@tonic-gate 	}
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	if (Pxecbkpt(t->t_pshandle, ptb->ptb_instr) == 0 &&
41847c478bd9Sstevel@tonic-gate 	    Pdelbkpt(t->t_pshandle, ptb->ptb_addr, ptb->ptb_instr) == 0)
41857c478bd9Sstevel@tonic-gate 		status = pt_status(t, tsp);
41867c478bd9Sstevel@tonic-gate 
41877c478bd9Sstevel@tonic-gate 	error = errno; /* save errno from Pxecbkpt, Pdelbkpt, or pt_status */
41887c478bd9Sstevel@tonic-gate 
41897c478bd9Sstevel@tonic-gate 	for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
41907c478bd9Sstevel@tonic-gate 		if (pt_wp_overlap(sep, ptb, WA_EXEC) &&
41917c478bd9Sstevel@tonic-gate 		    Psetwapt(t->t_pshandle, sep->se_data) == -1) {
41927c478bd9Sstevel@tonic-gate 			sep->se_state = MDB_TGT_SPEC_ERROR;
41937c478bd9Sstevel@tonic-gate 			sep->se_errno = errno;
41947c478bd9Sstevel@tonic-gate 		}
41957c478bd9Sstevel@tonic-gate 	}
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 	(void) set_errno(error);
41987c478bd9Sstevel@tonic-gate 	return (status);
41997c478bd9Sstevel@tonic-gate }
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42027c478bd9Sstevel@tonic-gate static int
pt_brkpt_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)42037c478bd9Sstevel@tonic-gate pt_brkpt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
42047c478bd9Sstevel@tonic-gate {
42057c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
42067c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb = sep->se_data;
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate 	return (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT &&
42097c478bd9Sstevel@tonic-gate 	    psp->pr_reg[R_PC] == ptb->ptb_addr);
42107c478bd9Sstevel@tonic-gate }
42117c478bd9Sstevel@tonic-gate 
42127c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_brkpt_ops = {
42130c1b95beSRichard Lowe 	.se_ctor = pt_brkpt_ctor,
42140c1b95beSRichard Lowe 	.se_dtor = pt_brkpt_dtor,
42150c1b95beSRichard Lowe 	.se_info = pt_brkpt_info,
42160c1b95beSRichard Lowe 	.se_secmp = pt_brkpt_secmp,
42170c1b95beSRichard Lowe 	.se_vecmp = pt_brkpt_vecmp,
42180c1b95beSRichard Lowe 	.se_arm = pt_brkpt_arm,
42190c1b95beSRichard Lowe 	.se_disarm = pt_brkpt_disarm,
42200c1b95beSRichard Lowe 	.se_cont = pt_brkpt_cont,
42210c1b95beSRichard Lowe 	.se_match = pt_brkpt_match,
42227c478bd9Sstevel@tonic-gate };
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate static int
pt_wapt_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)42257c478bd9Sstevel@tonic-gate pt_wapt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
42267c478bd9Sstevel@tonic-gate {
42277c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || Pstate(t->t_pshandle) >= PS_LOST)
42287c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	sep->se_data = mdb_alloc(sizeof (prwatch_t), UM_SLEEP);
42317c478bd9Sstevel@tonic-gate 	bcopy(args, sep->se_data, sizeof (prwatch_t));
42327c478bd9Sstevel@tonic-gate 	return (0);
42337c478bd9Sstevel@tonic-gate }
42347c478bd9Sstevel@tonic-gate 
42357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42367c478bd9Sstevel@tonic-gate static void
pt_wapt_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)42377c478bd9Sstevel@tonic-gate pt_wapt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
42387c478bd9Sstevel@tonic-gate {
42397c478bd9Sstevel@tonic-gate 	mdb_free(sep->se_data, sizeof (prwatch_t));
42407c478bd9Sstevel@tonic-gate }
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42437c478bd9Sstevel@tonic-gate static char *
pt_wapt_info(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_vespec_t * vep,mdb_tgt_spec_desc_t * sp,char * buf,size_t nbytes)42447c478bd9Sstevel@tonic-gate pt_wapt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
42457c478bd9Sstevel@tonic-gate     mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
42467c478bd9Sstevel@tonic-gate {
42477c478bd9Sstevel@tonic-gate 	prwatch_t *wp = vep != NULL ? vep->ve_args : sep->se_data;
42487c478bd9Sstevel@tonic-gate 	char desc[24];
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate 	ASSERT(wp->pr_wflags != 0);
42517c478bd9Sstevel@tonic-gate 	desc[0] = '\0';
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	switch (wp->pr_wflags) {
42547c478bd9Sstevel@tonic-gate 	case WA_READ:
42557c478bd9Sstevel@tonic-gate 		(void) strcat(desc, "/read");
42567c478bd9Sstevel@tonic-gate 		break;
42577c478bd9Sstevel@tonic-gate 	case WA_WRITE:
42587c478bd9Sstevel@tonic-gate 		(void) strcat(desc, "/write");
42597c478bd9Sstevel@tonic-gate 		break;
42607c478bd9Sstevel@tonic-gate 	case WA_EXEC:
42617c478bd9Sstevel@tonic-gate 		(void) strcat(desc, "/exec");
42627c478bd9Sstevel@tonic-gate 		break;
42637c478bd9Sstevel@tonic-gate 	default:
42647c478bd9Sstevel@tonic-gate 		if (wp->pr_wflags & WA_READ)
42657c478bd9Sstevel@tonic-gate 			(void) strcat(desc, "/r");
42667c478bd9Sstevel@tonic-gate 		if (wp->pr_wflags & WA_WRITE)
42677c478bd9Sstevel@tonic-gate 			(void) strcat(desc, "/w");
42687c478bd9Sstevel@tonic-gate 		if (wp->pr_wflags & WA_EXEC)
42697c478bd9Sstevel@tonic-gate 			(void) strcat(desc, "/x");
42707c478bd9Sstevel@tonic-gate 	}
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate 	(void) mdb_iob_snprintf(buf, nbytes, "stop on %s of [%la, %la)",
42737c478bd9Sstevel@tonic-gate 	    desc + 1, wp->pr_vaddr, wp->pr_vaddr + wp->pr_size);
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	sp->spec_base = wp->pr_vaddr;
42767c478bd9Sstevel@tonic-gate 	sp->spec_size = wp->pr_size;
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	return (buf);
42797c478bd9Sstevel@tonic-gate }
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42827c478bd9Sstevel@tonic-gate static int
pt_wapt_secmp(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)42837c478bd9Sstevel@tonic-gate pt_wapt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
42847c478bd9Sstevel@tonic-gate {
42857c478bd9Sstevel@tonic-gate 	prwatch_t *wp1 = sep->se_data;
42867c478bd9Sstevel@tonic-gate 	prwatch_t *wp2 = args;
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 	return (wp1->pr_vaddr == wp2->pr_vaddr &&
42897c478bd9Sstevel@tonic-gate 	    wp1->pr_size == wp2->pr_size && wp1->pr_wflags == wp2->pr_wflags);
42907c478bd9Sstevel@tonic-gate }
42917c478bd9Sstevel@tonic-gate 
42927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42937c478bd9Sstevel@tonic-gate static int
pt_wapt_vecmp(mdb_tgt_t * t,mdb_vespec_t * vep,void * args)42947c478bd9Sstevel@tonic-gate pt_wapt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
42957c478bd9Sstevel@tonic-gate {
42967c478bd9Sstevel@tonic-gate 	prwatch_t *wp1 = vep->ve_args;
42977c478bd9Sstevel@tonic-gate 	prwatch_t *wp2 = args;
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 	return (wp1->pr_vaddr == wp2->pr_vaddr &&
43007c478bd9Sstevel@tonic-gate 	    wp1->pr_size == wp2->pr_size && wp1->pr_wflags == wp2->pr_wflags);
43017c478bd9Sstevel@tonic-gate }
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate static int
pt_wapt_arm(mdb_tgt_t * t,mdb_sespec_t * sep)43047c478bd9Sstevel@tonic-gate pt_wapt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
43057c478bd9Sstevel@tonic-gate {
43067c478bd9Sstevel@tonic-gate 	return (Psetwapt(t->t_pshandle, sep->se_data));
43077c478bd9Sstevel@tonic-gate }
43087c478bd9Sstevel@tonic-gate 
43097c478bd9Sstevel@tonic-gate static int
pt_wapt_disarm(mdb_tgt_t * t,mdb_sespec_t * sep)43107c478bd9Sstevel@tonic-gate pt_wapt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
43117c478bd9Sstevel@tonic-gate {
43127c478bd9Sstevel@tonic-gate 	return (Pdelwapt(t->t_pshandle, sep->se_data));
43137c478bd9Sstevel@tonic-gate }
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate /*
43167c478bd9Sstevel@tonic-gate  * Determine whether the specified sespec is an armed breakpoint at the
43177c478bd9Sstevel@tonic-gate  * given %pc.  We use this to find conflicts with watchpoints below.
43187c478bd9Sstevel@tonic-gate  */
43197c478bd9Sstevel@tonic-gate static int
pt_bp_overlap(mdb_sespec_t * sep,uintptr_t pc)43207c478bd9Sstevel@tonic-gate pt_bp_overlap(mdb_sespec_t *sep, uintptr_t pc)
43217c478bd9Sstevel@tonic-gate {
43227c478bd9Sstevel@tonic-gate 	pt_brkpt_t *ptb = sep->se_data;
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	return (sep->se_state == MDB_TGT_SPEC_ARMED &&
43257c478bd9Sstevel@tonic-gate 	    sep->se_ops == &proc_brkpt_ops && ptb->ptb_addr == pc);
43267c478bd9Sstevel@tonic-gate }
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate /*
43297c478bd9Sstevel@tonic-gate  * We step over watchpoints using Pxecwapt() in libproc.  If a conflicting
43307c478bd9Sstevel@tonic-gate  * breakpoint is present, we must temporarily disarm it before stepping
43317c478bd9Sstevel@tonic-gate  * over the watchpoint so we do not immediately re-trigger the breakpoint.
43327c478bd9Sstevel@tonic-gate  * This is similar to the case handled in pt_brkpt_cont(), above.
43337c478bd9Sstevel@tonic-gate  */
43347c478bd9Sstevel@tonic-gate static int
pt_wapt_cont(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)43357c478bd9Sstevel@tonic-gate pt_wapt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
43367c478bd9Sstevel@tonic-gate {
43377c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
43387c478bd9Sstevel@tonic-gate 	mdb_sespec_t *bep = NULL;
43397c478bd9Sstevel@tonic-gate 	int status = -1;
43407c478bd9Sstevel@tonic-gate 	int error;
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate 	/*
43437c478bd9Sstevel@tonic-gate 	 * If the PC no longer matches our original address, then the user has
43447c478bd9Sstevel@tonic-gate 	 * changed it while we have been stopped. In this case, it no longer
43457c478bd9Sstevel@tonic-gate 	 * makes any sense to continue over this instruction.  We return as if
43467c478bd9Sstevel@tonic-gate 	 * we continued normally.
43477c478bd9Sstevel@tonic-gate 	 */
43487c478bd9Sstevel@tonic-gate 	if ((uintptr_t)psp->pr_info.si_pc != psp->pr_reg[R_PC])
43497c478bd9Sstevel@tonic-gate 		return (pt_status(t, tsp));
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 	if (psp->pr_info.si_code != TRAP_XWATCH) {
43527c478bd9Sstevel@tonic-gate 		for (bep = mdb_list_next(&t->t_active); bep != NULL;
43537c478bd9Sstevel@tonic-gate 		    bep = mdb_list_next(bep)) {
43547c478bd9Sstevel@tonic-gate 			if (pt_bp_overlap(bep, psp->pr_reg[R_PC])) {
43557c478bd9Sstevel@tonic-gate 				(void) bep->se_ops->se_disarm(t, bep);
43567c478bd9Sstevel@tonic-gate 				bep->se_state = MDB_TGT_SPEC_ACTIVE;
43577c478bd9Sstevel@tonic-gate 				break;
43587c478bd9Sstevel@tonic-gate 			}
43597c478bd9Sstevel@tonic-gate 		}
43607c478bd9Sstevel@tonic-gate 	}
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	if (Pxecwapt(t->t_pshandle, sep->se_data) == 0)
43637c478bd9Sstevel@tonic-gate 		status = pt_status(t, tsp);
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	error = errno; /* save errno from Pxecwapt or pt_status */
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 	if (bep != NULL)
43687c478bd9Sstevel@tonic-gate 		mdb_tgt_sespec_arm_one(t, bep);
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate 	(void) set_errno(error);
43717c478bd9Sstevel@tonic-gate 	return (status);
43727c478bd9Sstevel@tonic-gate }
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43757c478bd9Sstevel@tonic-gate static int
pt_wapt_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)43767c478bd9Sstevel@tonic-gate pt_wapt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
43777c478bd9Sstevel@tonic-gate {
43787c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
43797c478bd9Sstevel@tonic-gate 	prwatch_t *wp = sep->se_data;
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate 	return (psp->pr_why == PR_FAULTED && psp->pr_what == FLTWATCH &&
43827c478bd9Sstevel@tonic-gate 	    (uintptr_t)psp->pr_info.si_addr - wp->pr_vaddr < wp->pr_size);
43837c478bd9Sstevel@tonic-gate }
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate static const mdb_se_ops_t proc_wapt_ops = {
43860c1b95beSRichard Lowe 	.se_ctor = pt_wapt_ctor,
43870c1b95beSRichard Lowe 	.se_dtor = pt_wapt_dtor,
43880c1b95beSRichard Lowe 	.se_info = pt_wapt_info,
43890c1b95beSRichard Lowe 	.se_secmp = pt_wapt_secmp,
43900c1b95beSRichard Lowe 	.se_vecmp = pt_wapt_vecmp,
43910c1b95beSRichard Lowe 	.se_arm = pt_wapt_arm,
43920c1b95beSRichard Lowe 	.se_disarm = pt_wapt_disarm,
43930c1b95beSRichard Lowe 	.se_cont = pt_wapt_cont,
43940c1b95beSRichard Lowe 	.se_match = pt_wapt_match,
43957c478bd9Sstevel@tonic-gate };
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate static void
pt_bparg_dtor(mdb_vespec_t * vep)43987c478bd9Sstevel@tonic-gate pt_bparg_dtor(mdb_vespec_t *vep)
43997c478bd9Sstevel@tonic-gate {
44007c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta = vep->ve_args;
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 	if (pta->pta_symbol != NULL)
44037c478bd9Sstevel@tonic-gate 		strfree(pta->pta_symbol);
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate 	mdb_free(pta, sizeof (pt_bparg_t));
44067c478bd9Sstevel@tonic-gate }
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate static int
pt_add_vbrkpt(mdb_tgt_t * t,uintptr_t addr,int spec_flags,mdb_tgt_se_f * func,void * data)44097c478bd9Sstevel@tonic-gate pt_add_vbrkpt(mdb_tgt_t *t, uintptr_t addr,
44107c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
44117c478bd9Sstevel@tonic-gate {
44127c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta = mdb_alloc(sizeof (pt_bparg_t), UM_SLEEP);
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate 	pta->pta_symbol = NULL;
44157c478bd9Sstevel@tonic-gate 	pta->pta_addr = addr;
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_brkpt_ops, spec_flags,
44187c478bd9Sstevel@tonic-gate 	    func, data, pta, pt_bparg_dtor));
44197c478bd9Sstevel@tonic-gate }
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate static int
pt_add_sbrkpt(mdb_tgt_t * t,const char * sym,int spec_flags,mdb_tgt_se_f * func,void * data)44227c478bd9Sstevel@tonic-gate pt_add_sbrkpt(mdb_tgt_t *t, const char *sym,
44237c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
44247c478bd9Sstevel@tonic-gate {
44257c478bd9Sstevel@tonic-gate 	pt_bparg_t *pta;
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	if (sym[0] == '`') {
44287c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOOBJ);
44297c478bd9Sstevel@tonic-gate 		return (0);
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	if (sym[strlen(sym) - 1] == '`') {
44337c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_NOSYM);
44347c478bd9Sstevel@tonic-gate 		return (0);
44357c478bd9Sstevel@tonic-gate 	}
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 	pta = mdb_alloc(sizeof (pt_bparg_t), UM_SLEEP);
44387c478bd9Sstevel@tonic-gate 	pta->pta_symbol = strdup(sym);
4439892ad162SToomas Soome 	pta->pta_addr = 0;
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_brkpt_ops, spec_flags,
44427c478bd9Sstevel@tonic-gate 	    func, data, pta, pt_bparg_dtor));
44437c478bd9Sstevel@tonic-gate }
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate static int
pt_wparg_overlap(const prwatch_t * wp1,const prwatch_t * wp2)44467c478bd9Sstevel@tonic-gate pt_wparg_overlap(const prwatch_t *wp1, const prwatch_t *wp2)
44477c478bd9Sstevel@tonic-gate {
44487c478bd9Sstevel@tonic-gate 	if (wp2->pr_vaddr + wp2->pr_size <= wp1->pr_vaddr)
44497c478bd9Sstevel@tonic-gate 		return (0); /* no range overlap */
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate 	if (wp1->pr_vaddr + wp1->pr_size <= wp2->pr_vaddr)
44527c478bd9Sstevel@tonic-gate 		return (0); /* no range overlap */
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate 	return (wp1->pr_vaddr != wp2->pr_vaddr ||
44557c478bd9Sstevel@tonic-gate 	    wp1->pr_size != wp2->pr_size || wp1->pr_wflags != wp2->pr_wflags);
44567c478bd9Sstevel@tonic-gate }
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate static void
pt_wparg_dtor(mdb_vespec_t * vep)44597c478bd9Sstevel@tonic-gate pt_wparg_dtor(mdb_vespec_t *vep)
44607c478bd9Sstevel@tonic-gate {
44617c478bd9Sstevel@tonic-gate 	mdb_free(vep->ve_args, sizeof (prwatch_t));
44627c478bd9Sstevel@tonic-gate }
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate static int
pt_add_vwapt(mdb_tgt_t * t,uintptr_t addr,size_t len,uint_t wflags,int spec_flags,mdb_tgt_se_f * func,void * data)44657c478bd9Sstevel@tonic-gate pt_add_vwapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags,
44667c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
44677c478bd9Sstevel@tonic-gate {
44687c478bd9Sstevel@tonic-gate 	prwatch_t *wp = mdb_alloc(sizeof (prwatch_t), UM_SLEEP);
44697c478bd9Sstevel@tonic-gate 	mdb_sespec_t *sep;
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	wp->pr_vaddr = addr;
44727c478bd9Sstevel@tonic-gate 	wp->pr_size = len;
44737c478bd9Sstevel@tonic-gate 	wp->pr_wflags = 0;
44747c478bd9Sstevel@tonic-gate 
44757c478bd9Sstevel@tonic-gate 	if (wflags & MDB_TGT_WA_R)
44767c478bd9Sstevel@tonic-gate 		wp->pr_wflags |= WA_READ;
44777c478bd9Sstevel@tonic-gate 	if (wflags & MDB_TGT_WA_W)
44787c478bd9Sstevel@tonic-gate 		wp->pr_wflags |= WA_WRITE;
44797c478bd9Sstevel@tonic-gate 	if (wflags & MDB_TGT_WA_X)
44807c478bd9Sstevel@tonic-gate 		wp->pr_wflags |= WA_EXEC;
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 	for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
44837c478bd9Sstevel@tonic-gate 		if (sep->se_ops == &proc_wapt_ops &&
44847c478bd9Sstevel@tonic-gate 		    mdb_list_next(&sep->se_velist) != NULL &&
44857c478bd9Sstevel@tonic-gate 		    pt_wparg_overlap(wp, sep->se_data))
44867c478bd9Sstevel@tonic-gate 			goto dup;
44877c478bd9Sstevel@tonic-gate 	}
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 	for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
44907c478bd9Sstevel@tonic-gate 		if (sep->se_ops == &proc_wapt_ops && pt_wparg_overlap(wp,
44917c478bd9Sstevel@tonic-gate 		    ((mdb_vespec_t *)mdb_list_next(&sep->se_velist))->ve_args))
44927c478bd9Sstevel@tonic-gate 			goto dup;
44937c478bd9Sstevel@tonic-gate 	}
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_wapt_ops, spec_flags,
44967c478bd9Sstevel@tonic-gate 	    func, data, wp, pt_wparg_dtor));
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate dup:
44997c478bd9Sstevel@tonic-gate 	mdb_free(wp, sizeof (prwatch_t));
45007c478bd9Sstevel@tonic-gate 	(void) set_errno(EMDB_WPDUP);
45017c478bd9Sstevel@tonic-gate 	return (0);
45027c478bd9Sstevel@tonic-gate }
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate static int
pt_add_sysenter(mdb_tgt_t * t,int sysnum,int spec_flags,mdb_tgt_se_f * func,void * data)45057c478bd9Sstevel@tonic-gate pt_add_sysenter(mdb_tgt_t *t, int sysnum,
45067c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
45077c478bd9Sstevel@tonic-gate {
45087c478bd9Sstevel@tonic-gate 	if (sysnum <= 0 || sysnum > PRMAXSYS) {
45097c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_BADSYSNUM);
45107c478bd9Sstevel@tonic-gate 		return (0);
45117c478bd9Sstevel@tonic-gate 	}
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_sysenter_ops, spec_flags,
45147c478bd9Sstevel@tonic-gate 	    func, data, (void *)(uintptr_t)sysnum, no_ve_dtor));
45157c478bd9Sstevel@tonic-gate }
45167c478bd9Sstevel@tonic-gate 
45177c478bd9Sstevel@tonic-gate static int
pt_add_sysexit(mdb_tgt_t * t,int sysnum,int spec_flags,mdb_tgt_se_f * func,void * data)45187c478bd9Sstevel@tonic-gate pt_add_sysexit(mdb_tgt_t *t, int sysnum,
45197c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
45207c478bd9Sstevel@tonic-gate {
45217c478bd9Sstevel@tonic-gate 	if (sysnum <= 0 || sysnum > PRMAXSYS) {
45227c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_BADSYSNUM);
45237c478bd9Sstevel@tonic-gate 		return (0);
45247c478bd9Sstevel@tonic-gate 	}
45257c478bd9Sstevel@tonic-gate 
45267c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_sysexit_ops, spec_flags,
45277c478bd9Sstevel@tonic-gate 	    func, data, (void *)(uintptr_t)sysnum, no_ve_dtor));
45287c478bd9Sstevel@tonic-gate }
45297c478bd9Sstevel@tonic-gate 
45307c478bd9Sstevel@tonic-gate static int
pt_add_signal(mdb_tgt_t * t,int signum,int spec_flags,mdb_tgt_se_f * func,void * data)45317c478bd9Sstevel@tonic-gate pt_add_signal(mdb_tgt_t *t, int signum,
45327c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
45337c478bd9Sstevel@tonic-gate {
45347c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate 	if (signum <= 0 || signum > pt->p_maxsig) {
45377c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_BADSIGNUM);
45387c478bd9Sstevel@tonic-gate 		return (0);
45397c478bd9Sstevel@tonic-gate 	}
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_signal_ops, spec_flags,
45427c478bd9Sstevel@tonic-gate 	    func, data, (void *)(uintptr_t)signum, no_ve_dtor));
45437c478bd9Sstevel@tonic-gate }
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate static int
pt_add_fault(mdb_tgt_t * t,int fltnum,int spec_flags,mdb_tgt_se_f * func,void * data)45467c478bd9Sstevel@tonic-gate pt_add_fault(mdb_tgt_t *t, int fltnum,
45477c478bd9Sstevel@tonic-gate     int spec_flags, mdb_tgt_se_f *func, void *data)
45487c478bd9Sstevel@tonic-gate {
45497c478bd9Sstevel@tonic-gate 	if (fltnum <= 0 || fltnum > PRMAXFAULT) {
45507c478bd9Sstevel@tonic-gate 		(void) set_errno(EMDB_BADFLTNUM);
45517c478bd9Sstevel@tonic-gate 		return (0);
45527c478bd9Sstevel@tonic-gate 	}
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	return (mdb_tgt_vespec_insert(t, &proc_fault_ops, spec_flags,
45557c478bd9Sstevel@tonic-gate 	    func, data, (void *)(uintptr_t)fltnum, no_ve_dtor));
45567c478bd9Sstevel@tonic-gate }
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate static int
pt_getareg(mdb_tgt_t * t,mdb_tgt_tid_t tid,const char * rname,mdb_tgt_reg_t * rp)45597c478bd9Sstevel@tonic-gate pt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid,
45607c478bd9Sstevel@tonic-gate     const char *rname, mdb_tgt_reg_t *rp)
45617c478bd9Sstevel@tonic-gate {
45627c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
45637c478bd9Sstevel@tonic-gate 	prgregset_t grs;
45647c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
45677c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&pt->p_regs, rname)) != NULL) {
45707c478bd9Sstevel@tonic-gate 		uintmax_t rd_nval = mdb_nv_get_value(v);
45717c478bd9Sstevel@tonic-gate 		ushort_t rd_num = MDB_TGT_R_NUM(rd_nval);
45727c478bd9Sstevel@tonic-gate 		ushort_t rd_flags = MDB_TGT_R_FLAGS(rd_nval);
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 		if (!MDB_TGT_R_IS_FP(rd_flags)) {
45757c478bd9Sstevel@tonic-gate 			mdb_tgt_reg_t r = 0;
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate #if defined(__sparc) && defined(_ILP32)
45787c478bd9Sstevel@tonic-gate 			/*
45797c478bd9Sstevel@tonic-gate 			 * If we are debugging on 32-bit SPARC, the globals and
45807c478bd9Sstevel@tonic-gate 			 * outs can have 32 upper bits hiding in the xregs.
45817c478bd9Sstevel@tonic-gate 			 */
458289518a1cSdmick 			/* gcc doesn't like >= R_G0 because R_G0 == 0 */
458389518a1cSdmick 			int is_g = (rd_num == R_G0 ||
458489518a1cSdmick 			    rd_num >= R_G1 && rd_num <= R_G7);
45857c478bd9Sstevel@tonic-gate 			int is_o = (rd_num >= R_O0 && rd_num <= R_O7);
45867c478bd9Sstevel@tonic-gate 			prxregset_t xrs;
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate 			if (is_g && PTL_GETXREGS(t, tid, &xrs) == 0 &&
45897c478bd9Sstevel@tonic-gate 			    xrs.pr_type == XR_TYPE_V8P) {
45907c478bd9Sstevel@tonic-gate 				r |= (uint64_t)xrs.pr_un.pr_v8p.pr_xg[
45917c478bd9Sstevel@tonic-gate 				    rd_num - R_G0 + XR_G0] << 32;
45927c478bd9Sstevel@tonic-gate 			}
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 			if (is_o && PTL_GETXREGS(t, tid, &xrs) == 0 &&
45957c478bd9Sstevel@tonic-gate 			    xrs.pr_type == XR_TYPE_V8P) {
45967c478bd9Sstevel@tonic-gate 				r |= (uint64_t)xrs.pr_un.pr_v8p.pr_xo[
45977c478bd9Sstevel@tonic-gate 				    rd_num - R_O0 + XR_O0] << 32;
45987c478bd9Sstevel@tonic-gate 			}
45997c478bd9Sstevel@tonic-gate #endif	/* __sparc && _ILP32 */
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 			/*
46027c478bd9Sstevel@tonic-gate 			 * Avoid sign-extension by casting: recall that procfs
46037c478bd9Sstevel@tonic-gate 			 * defines prgreg_t as a long or int and our native
46047c478bd9Sstevel@tonic-gate 			 * register handling uses uint64_t's.
46057c478bd9Sstevel@tonic-gate 			 */
46067c478bd9Sstevel@tonic-gate 			if (PTL_GETREGS(t, tid, grs) == 0) {
46077c478bd9Sstevel@tonic-gate 				*rp = r | (ulong_t)grs[rd_num];
46080a47c91cSRobert Mustacchi 				if (rd_flags & MDB_TGT_R_32)
46090a47c91cSRobert Mustacchi 					*rp &= 0xffffffffULL;
46100a47c91cSRobert Mustacchi 				else if (rd_flags & MDB_TGT_R_16)
46110a47c91cSRobert Mustacchi 					*rp &= 0xffffULL;
46120a47c91cSRobert Mustacchi 				else if (rd_flags & MDB_TGT_R_8H)
46130a47c91cSRobert Mustacchi 					*rp = (*rp & 0xff00ULL) >> 8;
46140a47c91cSRobert Mustacchi 				else if (rd_flags & MDB_TGT_R_8L)
46150a47c91cSRobert Mustacchi 					*rp &= 0xffULL;
46167c478bd9Sstevel@tonic-gate 				return (0);
46177c478bd9Sstevel@tonic-gate 			}
46187c478bd9Sstevel@tonic-gate 			return (-1);
46197c478bd9Sstevel@tonic-gate 		} else
46207c478bd9Sstevel@tonic-gate 			return (pt_getfpreg(t, tid, rd_num, rd_flags, rp));
46217c478bd9Sstevel@tonic-gate 	}
46227c478bd9Sstevel@tonic-gate 
46237c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_BADREG));
46247c478bd9Sstevel@tonic-gate }
46257c478bd9Sstevel@tonic-gate 
46267c478bd9Sstevel@tonic-gate static int
pt_putareg(mdb_tgt_t * t,mdb_tgt_tid_t tid,const char * rname,mdb_tgt_reg_t r)46277c478bd9Sstevel@tonic-gate pt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r)
46287c478bd9Sstevel@tonic-gate {
46297c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
46307c478bd9Sstevel@tonic-gate 	prgregset_t grs;
46317c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
46347c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
46357c478bd9Sstevel@tonic-gate 
46367c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&pt->p_regs, rname)) != NULL) {
46377c478bd9Sstevel@tonic-gate 		uintmax_t rd_nval = mdb_nv_get_value(v);
46387c478bd9Sstevel@tonic-gate 		ushort_t rd_num = MDB_TGT_R_NUM(rd_nval);
46397c478bd9Sstevel@tonic-gate 		ushort_t rd_flags = MDB_TGT_R_FLAGS(rd_nval);
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate 		if (!MDB_TGT_R_IS_FP(rd_flags)) {
46420a47c91cSRobert Mustacchi 
46430a47c91cSRobert Mustacchi 			if (rd_flags & MDB_TGT_R_32)
46440a47c91cSRobert Mustacchi 				r &= 0xffffffffULL;
46450a47c91cSRobert Mustacchi 			else if (rd_flags & MDB_TGT_R_16)
46460a47c91cSRobert Mustacchi 				r &= 0xffffULL;
46470a47c91cSRobert Mustacchi 			else if (rd_flags & MDB_TGT_R_8H)
46480a47c91cSRobert Mustacchi 				r = (r & 0xffULL) << 8;
46490a47c91cSRobert Mustacchi 			else if (rd_flags & MDB_TGT_R_8L)
46500a47c91cSRobert Mustacchi 				r &= 0xffULL;
46510a47c91cSRobert Mustacchi 
46527c478bd9Sstevel@tonic-gate 			if (PTL_GETREGS(t, tid, grs) == 0) {
46537c478bd9Sstevel@tonic-gate 				grs[rd_num] = (prgreg_t)r;
46547c478bd9Sstevel@tonic-gate 				return (PTL_SETREGS(t, tid, grs));
46557c478bd9Sstevel@tonic-gate 			}
46567c478bd9Sstevel@tonic-gate 			return (-1);
46577c478bd9Sstevel@tonic-gate 		} else
46587c478bd9Sstevel@tonic-gate 			return (pt_putfpreg(t, tid, rd_num, rd_flags, r));
46597c478bd9Sstevel@tonic-gate 	}
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_BADREG));
46627c478bd9Sstevel@tonic-gate }
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate static int
pt_stack_call(pt_stkarg_t * psp,const prgregset_t grs,uint_t argc,long * argv)46657c478bd9Sstevel@tonic-gate pt_stack_call(pt_stkarg_t *psp, const prgregset_t grs, uint_t argc, long *argv)
46667c478bd9Sstevel@tonic-gate {
46677c478bd9Sstevel@tonic-gate 	psp->pstk_gotpc |= (grs[R_PC] != 0);
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate 	if (!psp->pstk_gotpc)
46707c478bd9Sstevel@tonic-gate 		return (0); /* skip initial zeroed frames */
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate 	return (psp->pstk_func(psp->pstk_private, grs[R_PC],
46737c478bd9Sstevel@tonic-gate 	    argc, argv, (const struct mdb_tgt_gregset *)grs));
46747c478bd9Sstevel@tonic-gate }
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate static int
pt_stack_iter(mdb_tgt_t * t,const mdb_tgt_gregset_t * gsp,mdb_tgt_stack_f * func,void * arg)46777c478bd9Sstevel@tonic-gate pt_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
46787c478bd9Sstevel@tonic-gate     mdb_tgt_stack_f *func, void *arg)
46797c478bd9Sstevel@tonic-gate {
46807c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
46817c478bd9Sstevel@tonic-gate 		pt_stkarg_t pstk;
46827c478bd9Sstevel@tonic-gate 
46837c478bd9Sstevel@tonic-gate 		pstk.pstk_func = func;
46847c478bd9Sstevel@tonic-gate 		pstk.pstk_private = arg;
46857c478bd9Sstevel@tonic-gate 		pstk.pstk_gotpc = FALSE;
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate 		(void) Pstack_iter(t->t_pshandle, gsp->gregs,
46887c478bd9Sstevel@tonic-gate 		    (proc_stack_f *)pt_stack_call, &pstk);
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 		return (0);
46917c478bd9Sstevel@tonic-gate 	}
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
46947c478bd9Sstevel@tonic-gate }
46957c478bd9Sstevel@tonic-gate 
46969acbbeafSnn35248 static int
pt_auxv(mdb_tgt_t * t,const auxv_t ** auxvp)46979acbbeafSnn35248 pt_auxv(mdb_tgt_t *t, const auxv_t **auxvp)
46989acbbeafSnn35248 {
46999acbbeafSnn35248 	if (t->t_pshandle != NULL) {
47009acbbeafSnn35248 		*auxvp = Pgetauxvec(t->t_pshandle);
47019acbbeafSnn35248 		return (0);
47029acbbeafSnn35248 	}
47039acbbeafSnn35248 
47049acbbeafSnn35248 	return (set_errno(EMDB_NOPROC));
47059acbbeafSnn35248 }
47069acbbeafSnn35248 
47079acbbeafSnn35248 
47087c478bd9Sstevel@tonic-gate static const mdb_tgt_ops_t proc_ops = {
47090c1b95beSRichard Lowe 	.t_setflags = pt_setflags,
47100c1b95beSRichard Lowe 	.t_setcontext = (int (*)())(uintptr_t)mdb_tgt_notsup,
47110c1b95beSRichard Lowe 	.t_activate = pt_activate,
47120c1b95beSRichard Lowe 	.t_deactivate = pt_deactivate,
47130c1b95beSRichard Lowe 	.t_periodic = pt_periodic,
47140c1b95beSRichard Lowe 	.t_destroy = pt_destroy,
47150c1b95beSRichard Lowe 	.t_name = pt_name,
47160c1b95beSRichard Lowe 	.t_isa = (const char *(*)())mdb_conf_isa,
47170c1b95beSRichard Lowe 	.t_platform = pt_platform,
47180c1b95beSRichard Lowe 	.t_uname = pt_uname,
47190c1b95beSRichard Lowe 	.t_dmodel = pt_dmodel,
47200c1b95beSRichard Lowe 	.t_aread = (ssize_t (*)())mdb_tgt_notsup,
47210c1b95beSRichard Lowe 	.t_awrite = (ssize_t (*)())mdb_tgt_notsup,
47220c1b95beSRichard Lowe 	.t_vread = pt_vread,
47230c1b95beSRichard Lowe 	.t_vwrite = pt_vwrite,
47240c1b95beSRichard Lowe 	.t_pread = (ssize_t (*)())mdb_tgt_notsup,
47250c1b95beSRichard Lowe 	.t_pwrite = (ssize_t (*)())mdb_tgt_notsup,
47260c1b95beSRichard Lowe 	.t_fread = pt_fread,
47270c1b95beSRichard Lowe 	.t_fwrite = pt_fwrite,
47280c1b95beSRichard Lowe 	.t_ioread = (ssize_t (*)())mdb_tgt_notsup,
47290c1b95beSRichard Lowe 	.t_iowrite = (ssize_t (*)())mdb_tgt_notsup,
47300c1b95beSRichard Lowe 	.t_vtop = (int (*)())(uintptr_t)mdb_tgt_notsup,
47310c1b95beSRichard Lowe 	.t_lookup_by_name = pt_lookup_by_name,
47320c1b95beSRichard Lowe 	.t_lookup_by_addr = pt_lookup_by_addr,
47330c1b95beSRichard Lowe 	.t_symbol_iter = pt_symbol_iter,
47340c1b95beSRichard Lowe 	.t_mapping_iter = pt_mapping_iter,
47350c1b95beSRichard Lowe 	.t_object_iter = pt_object_iter,
47360c1b95beSRichard Lowe 	.t_addr_to_map = pt_addr_to_map,
47370c1b95beSRichard Lowe 	.t_name_to_map = pt_name_to_map,
47380c1b95beSRichard Lowe 	.t_addr_to_ctf = pt_addr_to_ctf,
47390c1b95beSRichard Lowe 	.t_name_to_ctf = pt_name_to_ctf,
47400c1b95beSRichard Lowe 	.t_status = pt_status,
47410c1b95beSRichard Lowe 	.t_run = pt_run,
47420c1b95beSRichard Lowe 	.t_step = pt_step,
47430c1b95beSRichard Lowe 	.t_step_out = pt_step_out,
47440c1b95beSRichard Lowe 	.t_next = pt_next,
47450c1b95beSRichard Lowe 	.t_cont = pt_continue,
47460c1b95beSRichard Lowe 	.t_signal = pt_signal,
47470c1b95beSRichard Lowe 	.t_add_vbrkpt = pt_add_vbrkpt,
47480c1b95beSRichard Lowe 	.t_add_sbrkpt = pt_add_sbrkpt,
47490c1b95beSRichard Lowe 	.t_add_pwapt = (int (*)())(uintptr_t)mdb_tgt_null,
47500c1b95beSRichard Lowe 	.t_add_vwapt = pt_add_vwapt,
47510c1b95beSRichard Lowe 	.t_add_iowapt = (int (*)())(uintptr_t)mdb_tgt_null,
47520c1b95beSRichard Lowe 	.t_add_sysenter = pt_add_sysenter,
47530c1b95beSRichard Lowe 	.t_add_sysexit = pt_add_sysexit,
47540c1b95beSRichard Lowe 	.t_add_signal = pt_add_signal,
47550c1b95beSRichard Lowe 	.t_add_fault = pt_add_fault,
47560c1b95beSRichard Lowe 	.t_getareg = pt_getareg,
47570c1b95beSRichard Lowe 	.t_putareg = pt_putareg,
47580c1b95beSRichard Lowe 	.t_stack_iter = pt_stack_iter,
47590c1b95beSRichard Lowe 	.t_auxv = pt_auxv,
47600c1b95beSRichard Lowe 	.t_thread_name = pt_thread_name,
47617c478bd9Sstevel@tonic-gate };
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate /*
47647c478bd9Sstevel@tonic-gate  * Utility function for converting libproc errno values to mdb error values
47657c478bd9Sstevel@tonic-gate  * for the ptl calls below.  Currently, we only need to convert ENOENT to
47667c478bd9Sstevel@tonic-gate  * EMDB_NOTHREAD to produce a more useful error message for the user.
47677c478bd9Sstevel@tonic-gate  */
47687c478bd9Sstevel@tonic-gate static int
ptl_err(int error)47697c478bd9Sstevel@tonic-gate ptl_err(int error)
47707c478bd9Sstevel@tonic-gate {
47717c478bd9Sstevel@tonic-gate 	if (error != 0 && errno == ENOENT)
47727c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOTHREAD));
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 	return (error);
47757c478bd9Sstevel@tonic-gate }
47767c478bd9Sstevel@tonic-gate 
47777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47787c478bd9Sstevel@tonic-gate static mdb_tgt_tid_t
pt_lwp_tid(mdb_tgt_t * t,void * tap)47797c478bd9Sstevel@tonic-gate pt_lwp_tid(mdb_tgt_t *t, void *tap)
47807c478bd9Sstevel@tonic-gate {
47817c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL)
47827c478bd9Sstevel@tonic-gate 		return (Pstatus(t->t_pshandle)->pr_lwp.pr_lwpid);
47837c478bd9Sstevel@tonic-gate 
47847c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
47857c478bd9Sstevel@tonic-gate }
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate static int
pt_lwp_add(mdb_addrvec_t * ap,const lwpstatus_t * psp)47887c478bd9Sstevel@tonic-gate pt_lwp_add(mdb_addrvec_t *ap, const lwpstatus_t *psp)
47897c478bd9Sstevel@tonic-gate {
47907c478bd9Sstevel@tonic-gate 	mdb_addrvec_unshift(ap, psp->pr_lwpid);
47917c478bd9Sstevel@tonic-gate 	return (0);
47927c478bd9Sstevel@tonic-gate }
47937c478bd9Sstevel@tonic-gate 
47947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47957c478bd9Sstevel@tonic-gate static int
pt_lwp_iter(mdb_tgt_t * t,void * tap,mdb_addrvec_t * ap)47967c478bd9Sstevel@tonic-gate pt_lwp_iter(mdb_tgt_t *t, void *tap, mdb_addrvec_t *ap)
47977c478bd9Sstevel@tonic-gate {
47987c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL)
47997c478bd9Sstevel@tonic-gate 		return (Plwp_iter(t->t_pshandle, (proc_lwp_f *)pt_lwp_add, ap));
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48027c478bd9Sstevel@tonic-gate }
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48057c478bd9Sstevel@tonic-gate static int
pt_lwp_getregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)48067c478bd9Sstevel@tonic-gate pt_lwp_getregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
48077c478bd9Sstevel@tonic-gate {
48087c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
48097c478bd9Sstevel@tonic-gate 		return (ptl_err(Plwp_getregs(t->t_pshandle,
48107c478bd9Sstevel@tonic-gate 		    (lwpid_t)tid, gregs)));
48117c478bd9Sstevel@tonic-gate 	}
48127c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48137c478bd9Sstevel@tonic-gate }
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48167c478bd9Sstevel@tonic-gate static int
pt_lwp_setregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)48177c478bd9Sstevel@tonic-gate pt_lwp_setregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
48187c478bd9Sstevel@tonic-gate {
48197c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
48207c478bd9Sstevel@tonic-gate 		return (ptl_err(Plwp_setregs(t->t_pshandle,
48217c478bd9Sstevel@tonic-gate 		    (lwpid_t)tid, gregs)));
48227c478bd9Sstevel@tonic-gate 	}
48237c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48247c478bd9Sstevel@tonic-gate }
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48287c478bd9Sstevel@tonic-gate static int
pt_lwp_getxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prxregset_t ** xregs,size_t * sizep)4829ed093b41SRobert Mustacchi pt_lwp_getxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prxregset_t **xregs,
4830ed093b41SRobert Mustacchi     size_t *sizep)
48317c478bd9Sstevel@tonic-gate {
48327c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
48337c478bd9Sstevel@tonic-gate 		return (ptl_err(Plwp_getxregs(t->t_pshandle,
4834ed093b41SRobert Mustacchi 		    (lwpid_t)tid, xregs, sizep)));
48357c478bd9Sstevel@tonic-gate 	}
48367c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48377c478bd9Sstevel@tonic-gate }
48387c478bd9Sstevel@tonic-gate 
4839ed093b41SRobert Mustacchi static void
pt_lwp_freexregs(mdb_tgt_t * t,void * tap,prxregset_t * xregs,size_t size)4840ed093b41SRobert Mustacchi pt_lwp_freexregs(mdb_tgt_t *t, void *tap, prxregset_t *xregs, size_t size)
4841ed093b41SRobert Mustacchi {
4842ed093b41SRobert Mustacchi 	if (t->t_pshandle != NULL) {
4843ed093b41SRobert Mustacchi 		Plwp_freexregs(t->t_pshandle, xregs, size);
4844ed093b41SRobert Mustacchi 	}
4845ed093b41SRobert Mustacchi }
4846ed093b41SRobert Mustacchi 
48477c478bd9Sstevel@tonic-gate static int
pt_lwp_setxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prxregset_t * xregs,size_t len)48487c478bd9Sstevel@tonic-gate pt_lwp_setxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4849ed093b41SRobert Mustacchi     const prxregset_t *xregs, size_t len)
48507c478bd9Sstevel@tonic-gate {
48517c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
48527c478bd9Sstevel@tonic-gate 		return (ptl_err(Plwp_setxregs(t->t_pshandle,
4853ed093b41SRobert Mustacchi 		    (lwpid_t)tid, xregs, len)));
48547c478bd9Sstevel@tonic-gate 	}
48557c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48567c478bd9Sstevel@tonic-gate }
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48597c478bd9Sstevel@tonic-gate static int
pt_lwp_getfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prfpregset_t * fpregs)48607c478bd9Sstevel@tonic-gate pt_lwp_getfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
48617c478bd9Sstevel@tonic-gate     prfpregset_t *fpregs)
48627c478bd9Sstevel@tonic-gate {
48637c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
48647c478bd9Sstevel@tonic-gate 		return (ptl_err(Plwp_getfpregs(t->t_pshandle,
48657c478bd9Sstevel@tonic-gate 		    (lwpid_t)tid, fpregs)));
48667c478bd9Sstevel@tonic-gate 	}
48677c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48687c478bd9Sstevel@tonic-gate }
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48717c478bd9Sstevel@tonic-gate static int
pt_lwp_setfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prfpregset_t * fpregs)48727c478bd9Sstevel@tonic-gate pt_lwp_setfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
48737c478bd9Sstevel@tonic-gate     const prfpregset_t *fpregs)
48747c478bd9Sstevel@tonic-gate {
48757c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL) {
48767c478bd9Sstevel@tonic-gate 		return (ptl_err(Plwp_setfpregs(t->t_pshandle,
48777c478bd9Sstevel@tonic-gate 		    (lwpid_t)tid, fpregs)));
48787c478bd9Sstevel@tonic-gate 	}
48797c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_NOPROC));
48807c478bd9Sstevel@tonic-gate }
48817c478bd9Sstevel@tonic-gate 
48827c478bd9Sstevel@tonic-gate static const pt_ptl_ops_t proc_lwp_ops = {
4883ed093b41SRobert Mustacchi 	.ptl_ctor = (int (*)())(uintptr_t)mdb_tgt_nop,
4884ed093b41SRobert Mustacchi 	.ptl_dtor = (void (*)())(uintptr_t)mdb_tgt_nop,
4885ed093b41SRobert Mustacchi 	.ptl_tid = pt_lwp_tid,
4886ed093b41SRobert Mustacchi 	.ptl_iter = pt_lwp_iter,
4887ed093b41SRobert Mustacchi 	.ptl_getregs = pt_lwp_getregs,
4888ed093b41SRobert Mustacchi 	.ptl_setregs = pt_lwp_setregs,
4889ed093b41SRobert Mustacchi 	.ptl_getxregs = pt_lwp_getxregs,
4890ed093b41SRobert Mustacchi 	.ptl_freexregs = pt_lwp_freexregs,
4891ed093b41SRobert Mustacchi 	.ptl_setxregs = pt_lwp_setxregs,
4892ed093b41SRobert Mustacchi 	.ptl_getfpregs = pt_lwp_getfpregs,
4893ed093b41SRobert Mustacchi 	.ptl_setfpregs = pt_lwp_setfpregs
48947c478bd9Sstevel@tonic-gate };
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate static int
pt_tdb_ctor(mdb_tgt_t * t)48977c478bd9Sstevel@tonic-gate pt_tdb_ctor(mdb_tgt_t *t)
48987c478bd9Sstevel@tonic-gate {
48997c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
49007c478bd9Sstevel@tonic-gate 	td_thragent_t *tap;
49017c478bd9Sstevel@tonic-gate 	td_err_e err;
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_new(t->t_pshandle, &tap)) != TD_OK)
49047c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate 	pt->p_ptl_hdl = tap;
49077c478bd9Sstevel@tonic-gate 	return (0);
49087c478bd9Sstevel@tonic-gate }
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate static void
pt_tdb_dtor(mdb_tgt_t * t,void * tap)49117c478bd9Sstevel@tonic-gate pt_tdb_dtor(mdb_tgt_t *t, void *tap)
49127c478bd9Sstevel@tonic-gate {
49137c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
49147c478bd9Sstevel@tonic-gate 
49157c478bd9Sstevel@tonic-gate 	ASSERT(tap == pt->p_ptl_hdl);
49167c478bd9Sstevel@tonic-gate 	(void) pt->p_tdb_ops->td_ta_delete(tap);
49177c478bd9Sstevel@tonic-gate 	pt->p_ptl_hdl = NULL;
49187c478bd9Sstevel@tonic-gate }
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate static mdb_tgt_tid_t
pt_tdb_tid(mdb_tgt_t * t,void * tap)49217c478bd9Sstevel@tonic-gate pt_tdb_tid(mdb_tgt_t *t, void *tap)
49227c478bd9Sstevel@tonic-gate {
49237c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
49267c478bd9Sstevel@tonic-gate 	td_thrinfo_t ti;
49277c478bd9Sstevel@tonic-gate 	td_err_e err;
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
49307c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_lwp2thr(tap,
49337c478bd9Sstevel@tonic-gate 	    Pstatus(t->t_pshandle)->pr_lwp.pr_lwpid, &th)) != TD_OK)
49347c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_thr_get_info(&th, &ti)) != TD_OK)
49377c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
49387c478bd9Sstevel@tonic-gate 
49397c478bd9Sstevel@tonic-gate 	return (ti.ti_tid);
49407c478bd9Sstevel@tonic-gate }
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate static int
pt_tdb_add(const td_thrhandle_t * thp,pt_addarg_t * pap)49437c478bd9Sstevel@tonic-gate pt_tdb_add(const td_thrhandle_t *thp, pt_addarg_t *pap)
49447c478bd9Sstevel@tonic-gate {
49457c478bd9Sstevel@tonic-gate 	td_thrinfo_t ti;
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate 	if (pap->pa_pt->p_tdb_ops->td_thr_get_info(thp, &ti) == TD_OK &&
49487c478bd9Sstevel@tonic-gate 	    ti.ti_state != TD_THR_ZOMBIE)
49497c478bd9Sstevel@tonic-gate 		mdb_addrvec_unshift(pap->pa_ap, ti.ti_tid);
49507c478bd9Sstevel@tonic-gate 
49517c478bd9Sstevel@tonic-gate 	return (0);
49527c478bd9Sstevel@tonic-gate }
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate static int
pt_tdb_iter(mdb_tgt_t * t,void * tap,mdb_addrvec_t * ap)49557c478bd9Sstevel@tonic-gate pt_tdb_iter(mdb_tgt_t *t, void *tap, mdb_addrvec_t *ap)
49567c478bd9Sstevel@tonic-gate {
49577c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
49587c478bd9Sstevel@tonic-gate 	pt_addarg_t arg;
49597c478bd9Sstevel@tonic-gate 	int err;
49607c478bd9Sstevel@tonic-gate 
49617c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
49627c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate 	arg.pa_pt = pt;
49657c478bd9Sstevel@tonic-gate 	arg.pa_ap = ap;
49667c478bd9Sstevel@tonic-gate 
49677c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_thr_iter(tap, (td_thr_iter_f *)
49687c478bd9Sstevel@tonic-gate 	    pt_tdb_add, &arg, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
49697c478bd9Sstevel@tonic-gate 	    TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS)) != TD_OK)
49707c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate 	return (0);
49737c478bd9Sstevel@tonic-gate }
49747c478bd9Sstevel@tonic-gate 
49757c478bd9Sstevel@tonic-gate static int
pt_tdb_getregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)49767c478bd9Sstevel@tonic-gate pt_tdb_getregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
49777c478bd9Sstevel@tonic-gate {
49787c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
49817c478bd9Sstevel@tonic-gate 	td_err_e err;
49827c478bd9Sstevel@tonic-gate 
49837c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
49847c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
49877c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_thr_getgregs(&th, gregs);
49907c478bd9Sstevel@tonic-gate 	if (err != TD_OK && err != TD_PARTIALREG)
49917c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate 	return (0);
49947c478bd9Sstevel@tonic-gate }
49957c478bd9Sstevel@tonic-gate 
49967c478bd9Sstevel@tonic-gate static int
pt_tdb_setregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)49977c478bd9Sstevel@tonic-gate pt_tdb_setregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
49987c478bd9Sstevel@tonic-gate {
49997c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
50027c478bd9Sstevel@tonic-gate 	td_err_e err;
50037c478bd9Sstevel@tonic-gate 
50047c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
50057c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
50087c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50097c478bd9Sstevel@tonic-gate 
50107c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_thr_setgregs(&th, gregs);
50117c478bd9Sstevel@tonic-gate 	if (err != TD_OK && err != TD_PARTIALREG)
50127c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 	return (0);
50157c478bd9Sstevel@tonic-gate }
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate static int
pt_tdb_getxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prxregset_t ** xregs,size_t * sizep)5018ed093b41SRobert Mustacchi pt_tdb_getxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prxregset_t **xregs,
5019ed093b41SRobert Mustacchi     size_t *sizep)
50207c478bd9Sstevel@tonic-gate {
50217c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
50227c478bd9Sstevel@tonic-gate 
50237c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
50247c478bd9Sstevel@tonic-gate 	td_err_e err;
5025ed093b41SRobert Mustacchi 	int xregsize;
5026ed093b41SRobert Mustacchi 	prxregset_t *pxr;
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
50297c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
50307c478bd9Sstevel@tonic-gate 
50317c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
50327c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50337c478bd9Sstevel@tonic-gate 
5034ed093b41SRobert Mustacchi 	if ((err = pt->p_tdb_ops->td_thr_getxregsize(&th, &xregsize)) != TD_OK)
50357c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50367c478bd9Sstevel@tonic-gate 
5037ed093b41SRobert Mustacchi 	if (xregsize == 0) {
5038ed093b41SRobert Mustacchi 		return (set_errno(ENODATA));
5039ed093b41SRobert Mustacchi 	}
5040ed093b41SRobert Mustacchi 
5041ed093b41SRobert Mustacchi 	pxr = mdb_alloc(xregsize, UM_SLEEP);
5042ed093b41SRobert Mustacchi 
5043ed093b41SRobert Mustacchi 	err = pt->p_tdb_ops->td_thr_getxregs(&th, pxr);
5044ed093b41SRobert Mustacchi 	if (err != TD_OK && err != TD_PARTIALREG) {
5045ed093b41SRobert Mustacchi 		mdb_free(pxr, xregsize);
5046ed093b41SRobert Mustacchi 		return (set_errno(tdb_to_errno(err)));
5047ed093b41SRobert Mustacchi 	}
5048ed093b41SRobert Mustacchi 
5049ed093b41SRobert Mustacchi 	*xregs = pxr;
5050ed093b41SRobert Mustacchi 	*sizep = xregsize;
50517c478bd9Sstevel@tonic-gate 	return (0);
50527c478bd9Sstevel@tonic-gate }
50537c478bd9Sstevel@tonic-gate 
5054ed093b41SRobert Mustacchi static void
pt_tdb_freexregs(mdb_tgt_t * t __unused,void * tap __unused,prxregset_t * pxr,size_t size)5055ed093b41SRobert Mustacchi pt_tdb_freexregs(mdb_tgt_t *t __unused, void *tap __unused, prxregset_t *pxr,
5056ed093b41SRobert Mustacchi     size_t size)
5057ed093b41SRobert Mustacchi {
5058ed093b41SRobert Mustacchi 	mdb_free(pxr, size);
5059ed093b41SRobert Mustacchi }
5060ed093b41SRobert Mustacchi 
50617c478bd9Sstevel@tonic-gate static int
pt_tdb_setxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prxregset_t * xregs,size_t len __unused)50627c478bd9Sstevel@tonic-gate pt_tdb_setxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5063ed093b41SRobert Mustacchi     const prxregset_t *xregs, size_t len __unused)
50647c478bd9Sstevel@tonic-gate {
50657c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
50667c478bd9Sstevel@tonic-gate 
50677c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
50687c478bd9Sstevel@tonic-gate 	td_err_e err;
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
50717c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
50727c478bd9Sstevel@tonic-gate 
50737c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
50747c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_thr_setxregs(&th, xregs);
50777c478bd9Sstevel@tonic-gate 	if (err != TD_OK && err != TD_PARTIALREG)
50787c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50797c478bd9Sstevel@tonic-gate 
50807c478bd9Sstevel@tonic-gate 	return (0);
50817c478bd9Sstevel@tonic-gate }
50827c478bd9Sstevel@tonic-gate 
50837c478bd9Sstevel@tonic-gate static int
pt_tdb_getfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prfpregset_t * fpregs)50847c478bd9Sstevel@tonic-gate pt_tdb_getfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
50857c478bd9Sstevel@tonic-gate     prfpregset_t *fpregs)
50867c478bd9Sstevel@tonic-gate {
50877c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
50887c478bd9Sstevel@tonic-gate 
50897c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
50907c478bd9Sstevel@tonic-gate 	td_err_e err;
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
50937c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
50967c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_thr_getfpregs(&th, fpregs);
50997c478bd9Sstevel@tonic-gate 	if (err != TD_OK && err != TD_PARTIALREG)
51007c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate 	return (0);
51037c478bd9Sstevel@tonic-gate }
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate static int
pt_tdb_setfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prfpregset_t * fpregs)51067c478bd9Sstevel@tonic-gate pt_tdb_setfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
51077c478bd9Sstevel@tonic-gate     const prfpregset_t *fpregs)
51087c478bd9Sstevel@tonic-gate {
51097c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 	td_thrhandle_t th;
51127c478bd9Sstevel@tonic-gate 	td_err_e err;
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL)
51157c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOPROC));
51167c478bd9Sstevel@tonic-gate 
51177c478bd9Sstevel@tonic-gate 	if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
51187c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
51197c478bd9Sstevel@tonic-gate 
51207c478bd9Sstevel@tonic-gate 	err = pt->p_tdb_ops->td_thr_setfpregs(&th, fpregs);
51217c478bd9Sstevel@tonic-gate 	if (err != TD_OK && err != TD_PARTIALREG)
51227c478bd9Sstevel@tonic-gate 		return (set_errno(tdb_to_errno(err)));
51237c478bd9Sstevel@tonic-gate 
51247c478bd9Sstevel@tonic-gate 	return (0);
51257c478bd9Sstevel@tonic-gate }
51267c478bd9Sstevel@tonic-gate 
51277c478bd9Sstevel@tonic-gate static const pt_ptl_ops_t proc_tdb_ops = {
5128ed093b41SRobert Mustacchi 	.ptl_ctor = pt_tdb_ctor,
5129ed093b41SRobert Mustacchi 	.ptl_dtor = pt_tdb_dtor,
5130ed093b41SRobert Mustacchi 	.ptl_tid = pt_tdb_tid,
5131ed093b41SRobert Mustacchi 	.ptl_iter = pt_tdb_iter,
5132ed093b41SRobert Mustacchi 	.ptl_getregs = pt_tdb_getregs,
5133ed093b41SRobert Mustacchi 	.ptl_setregs = pt_tdb_setregs,
5134ed093b41SRobert Mustacchi 	.ptl_getxregs = pt_tdb_getxregs,
5135ed093b41SRobert Mustacchi 	.ptl_freexregs = pt_tdb_freexregs,
5136ed093b41SRobert Mustacchi 	.ptl_setxregs = pt_tdb_setxregs,
5137ed093b41SRobert Mustacchi 	.ptl_getfpregs = pt_tdb_getfpregs,
5138ed093b41SRobert Mustacchi 	.ptl_setfpregs = pt_tdb_setfpregs
51397c478bd9Sstevel@tonic-gate };
51407c478bd9Sstevel@tonic-gate 
51417c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_auxv(mdb_tgt_t * t,void * buf,size_t nbytes)51427c478bd9Sstevel@tonic-gate pt_xd_auxv(mdb_tgt_t *t, void *buf, size_t nbytes)
51437c478bd9Sstevel@tonic-gate {
51447c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = t->t_pshandle;
51457c478bd9Sstevel@tonic-gate 	const auxv_t *auxp, *auxv = NULL;
51467c478bd9Sstevel@tonic-gate 	int auxn = 0;
51477c478bd9Sstevel@tonic-gate 
51487c478bd9Sstevel@tonic-gate 	if (P != NULL && (auxv = Pgetauxvec(P)) != NULL &&
51497c478bd9Sstevel@tonic-gate 	    auxv->a_type != AT_NULL) {
5150892ad162SToomas Soome 		for (auxp = auxv, auxn = 1; auxp->a_type != 0; auxp++)
51517c478bd9Sstevel@tonic-gate 			auxn++;
51527c478bd9Sstevel@tonic-gate 	}
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
51557c478bd9Sstevel@tonic-gate 		return (sizeof (auxv_t) * auxn);
51567c478bd9Sstevel@tonic-gate 
51577c478bd9Sstevel@tonic-gate 	if (auxn == 0)
51587c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
51597c478bd9Sstevel@tonic-gate 
51607c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, sizeof (auxv_t) * auxn);
51617c478bd9Sstevel@tonic-gate 	bcopy(auxv, buf, nbytes);
51627c478bd9Sstevel@tonic-gate 	return (nbytes);
51637c478bd9Sstevel@tonic-gate }
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_cred(mdb_tgt_t * t,void * buf,size_t nbytes)51667c478bd9Sstevel@tonic-gate pt_xd_cred(mdb_tgt_t *t, void *buf, size_t nbytes)
51677c478bd9Sstevel@tonic-gate {
51687c478bd9Sstevel@tonic-gate 	prcred_t cr, *crp;
51697c478bd9Sstevel@tonic-gate 	size_t cbytes = 0;
51707c478bd9Sstevel@tonic-gate 
51717c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL && Pcred(t->t_pshandle, &cr, 1) == 0) {
51727c478bd9Sstevel@tonic-gate 		cbytes = (cr.pr_ngroups <= 1) ? sizeof (prcred_t) :
51737c478bd9Sstevel@tonic-gate 		    (sizeof (prcred_t) + (cr.pr_ngroups - 1) * sizeof (gid_t));
51747c478bd9Sstevel@tonic-gate 	}
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
51777c478bd9Sstevel@tonic-gate 		return (cbytes);
51787c478bd9Sstevel@tonic-gate 
51797c478bd9Sstevel@tonic-gate 	if (cbytes == 0)
51807c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 	crp = mdb_alloc(cbytes, UM_SLEEP);
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate 	if (Pcred(t->t_pshandle, crp, cr.pr_ngroups) == -1)
51857c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
51867c478bd9Sstevel@tonic-gate 
51877c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, cbytes);
51887c478bd9Sstevel@tonic-gate 	bcopy(crp, buf, nbytes);
51897c478bd9Sstevel@tonic-gate 	mdb_free(crp, cbytes);
51907c478bd9Sstevel@tonic-gate 	return (nbytes);
51917c478bd9Sstevel@tonic-gate }
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_ehdr(mdb_tgt_t * t,void * buf,size_t nbytes)51947c478bd9Sstevel@tonic-gate pt_xd_ehdr(mdb_tgt_t *t, void *buf, size_t nbytes)
51957c478bd9Sstevel@tonic-gate {
51967c478bd9Sstevel@tonic-gate 	pt_data_t *pt = t->t_data;
51977c478bd9Sstevel@tonic-gate 
51987c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
51997c478bd9Sstevel@tonic-gate 		return (sizeof (GElf_Ehdr));
52007c478bd9Sstevel@tonic-gate 
52017c478bd9Sstevel@tonic-gate 	if (pt->p_file == NULL)
52027c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
52037c478bd9Sstevel@tonic-gate 
52047c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, sizeof (GElf_Ehdr));
52057c478bd9Sstevel@tonic-gate 	bcopy(&pt->p_file->gf_ehdr, buf, nbytes);
52067c478bd9Sstevel@tonic-gate 	return (nbytes);
52077c478bd9Sstevel@tonic-gate }
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate static int
pt_copy_lwp(lwpstatus_t ** lspp,const lwpstatus_t * lsp)52107c478bd9Sstevel@tonic-gate pt_copy_lwp(lwpstatus_t **lspp, const lwpstatus_t *lsp)
52117c478bd9Sstevel@tonic-gate {
52127c478bd9Sstevel@tonic-gate 	bcopy(lsp, *lspp, sizeof (lwpstatus_t));
52137c478bd9Sstevel@tonic-gate 	(*lspp)++;
52147c478bd9Sstevel@tonic-gate 	return (0);
52157c478bd9Sstevel@tonic-gate }
52167c478bd9Sstevel@tonic-gate 
52177c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_lwpstatus(mdb_tgt_t * t,void * buf,size_t nbytes)52187c478bd9Sstevel@tonic-gate pt_xd_lwpstatus(mdb_tgt_t *t, void *buf, size_t nbytes)
52197c478bd9Sstevel@tonic-gate {
52207c478bd9Sstevel@tonic-gate 	lwpstatus_t *lsp, *lbuf;
52217c478bd9Sstevel@tonic-gate 	const pstatus_t *psp;
52227c478bd9Sstevel@tonic-gate 	int nlwp = 0;
52237c478bd9Sstevel@tonic-gate 
52247c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL && (psp = Pstatus(t->t_pshandle)) != NULL)
52257c478bd9Sstevel@tonic-gate 		nlwp = psp->pr_nlwp;
52267c478bd9Sstevel@tonic-gate 
52277c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
52287c478bd9Sstevel@tonic-gate 		return (sizeof (lwpstatus_t) * nlwp);
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	if (nlwp == 0)
52317c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate 	lsp = lbuf = mdb_alloc(sizeof (lwpstatus_t) * nlwp, UM_SLEEP);
52347c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, sizeof (lwpstatus_t) * nlwp);
52357c478bd9Sstevel@tonic-gate 
52367c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(t->t_pshandle, (proc_lwp_f *)pt_copy_lwp, &lsp);
52377c478bd9Sstevel@tonic-gate 	bcopy(lbuf, buf, nbytes);
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 	mdb_free(lbuf, sizeof (lwpstatus_t) * nlwp);
52407c478bd9Sstevel@tonic-gate 	return (nbytes);
52417c478bd9Sstevel@tonic-gate }
52427c478bd9Sstevel@tonic-gate 
52437c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_pshandle(mdb_tgt_t * t,void * buf,size_t nbytes)52447c478bd9Sstevel@tonic-gate pt_xd_pshandle(mdb_tgt_t *t, void *buf, size_t nbytes)
52457c478bd9Sstevel@tonic-gate {
52467c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
52477c478bd9Sstevel@tonic-gate 		return (sizeof (struct ps_prochandle *));
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || nbytes != sizeof (struct ps_prochandle *))
52507c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
52517c478bd9Sstevel@tonic-gate 
52527c478bd9Sstevel@tonic-gate 	bcopy(&t->t_pshandle, buf, nbytes);
52537c478bd9Sstevel@tonic-gate 	return (nbytes);
52547c478bd9Sstevel@tonic-gate }
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_psinfo(mdb_tgt_t * t,void * buf,size_t nbytes)52577c478bd9Sstevel@tonic-gate pt_xd_psinfo(mdb_tgt_t *t, void *buf, size_t nbytes)
52587c478bd9Sstevel@tonic-gate {
52597c478bd9Sstevel@tonic-gate 	const psinfo_t *psp;
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
52627c478bd9Sstevel@tonic-gate 		return (sizeof (psinfo_t));
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || (psp = Ppsinfo(t->t_pshandle)) == NULL)
52657c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
52667c478bd9Sstevel@tonic-gate 
52677c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, sizeof (psinfo_t));
52687c478bd9Sstevel@tonic-gate 	bcopy(psp, buf, nbytes);
52697c478bd9Sstevel@tonic-gate 	return (nbytes);
52707c478bd9Sstevel@tonic-gate }
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_pstatus(mdb_tgt_t * t,void * buf,size_t nbytes)52737c478bd9Sstevel@tonic-gate pt_xd_pstatus(mdb_tgt_t *t, void *buf, size_t nbytes)
52747c478bd9Sstevel@tonic-gate {
52757c478bd9Sstevel@tonic-gate 	const pstatus_t *psp;
52767c478bd9Sstevel@tonic-gate 
52777c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
52787c478bd9Sstevel@tonic-gate 		return (sizeof (pstatus_t));
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || (psp = Pstatus(t->t_pshandle)) == NULL)
52817c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
52827c478bd9Sstevel@tonic-gate 
52837c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, sizeof (pstatus_t));
52847c478bd9Sstevel@tonic-gate 	bcopy(psp, buf, nbytes);
52857c478bd9Sstevel@tonic-gate 	return (nbytes);
52867c478bd9Sstevel@tonic-gate }
52877c478bd9Sstevel@tonic-gate 
52887c478bd9Sstevel@tonic-gate static ssize_t
pt_xd_utsname(mdb_tgt_t * t,void * buf,size_t nbytes)52897c478bd9Sstevel@tonic-gate pt_xd_utsname(mdb_tgt_t *t, void *buf, size_t nbytes)
52907c478bd9Sstevel@tonic-gate {
52917c478bd9Sstevel@tonic-gate 	struct utsname uts;
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 	if (buf == NULL && nbytes == 0)
52947c478bd9Sstevel@tonic-gate 		return (sizeof (struct utsname));
52957c478bd9Sstevel@tonic-gate 
52967c478bd9Sstevel@tonic-gate 	if (t->t_pshandle == NULL || Puname(t->t_pshandle, &uts) != 0)
52977c478bd9Sstevel@tonic-gate 		return (set_errno(ENODATA));
52987c478bd9Sstevel@tonic-gate 
52997c478bd9Sstevel@tonic-gate 	nbytes = MIN(nbytes, sizeof (struct utsname));
53007c478bd9Sstevel@tonic-gate 	bcopy(&uts, buf, nbytes);
53017c478bd9Sstevel@tonic-gate 	return (nbytes);
53027c478bd9Sstevel@tonic-gate }
53037c478bd9Sstevel@tonic-gate 
53047c478bd9Sstevel@tonic-gate int
mdb_proc_tgt_create(mdb_tgt_t * t,int argc,const char * argv[])53057c478bd9Sstevel@tonic-gate mdb_proc_tgt_create(mdb_tgt_t *t, int argc, const char *argv[])
53067c478bd9Sstevel@tonic-gate {
53077c478bd9Sstevel@tonic-gate 	pt_data_t *pt = mdb_zalloc(sizeof (pt_data_t), UM_SLEEP);
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 	const char *aout_path = argc > 0 ? argv[0] : PT_EXEC_PATH;
53107c478bd9Sstevel@tonic-gate 	const char *core_path = argc > 1 ? argv[1] : NULL;
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 	const mdb_tgt_regdesc_t *rdp;
53137c478bd9Sstevel@tonic-gate 	char execname[MAXPATHLEN];
53147c478bd9Sstevel@tonic-gate 	struct stat64 st;
53157c478bd9Sstevel@tonic-gate 	int perr;
531624537d3eSToomas Soome 	int state = 0;
53177c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
53187c478bd9Sstevel@tonic-gate 	int i;
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate 	if (argc > 2) {
53217c478bd9Sstevel@tonic-gate 		mdb_free(pt, sizeof (pt_data_t));
53227c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
53237c478bd9Sstevel@tonic-gate 	}
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate 	if (t->t_flags & MDB_TGT_F_RDWR)
53267c478bd9Sstevel@tonic-gate 		pt->p_oflags = O_RDWR;
53277c478bd9Sstevel@tonic-gate 	else
53287c478bd9Sstevel@tonic-gate 		pt->p_oflags = O_RDONLY;
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 	if (t->t_flags & MDB_TGT_F_FORCE)
53317c478bd9Sstevel@tonic-gate 		pt->p_gflags |= PGRAB_FORCE;
53327c478bd9Sstevel@tonic-gate 	if (t->t_flags & MDB_TGT_F_NOSTOP)
53337c478bd9Sstevel@tonic-gate 		pt->p_gflags |= PGRAB_NOSTOP;
53347c478bd9Sstevel@tonic-gate 
53357c478bd9Sstevel@tonic-gate 	pt->p_ptl_ops = &proc_lwp_ops;
53367c478bd9Sstevel@tonic-gate 	pt->p_maxsig = sysconf(_SC_SIGRT_MAX);
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate 	(void) mdb_nv_create(&pt->p_regs, UM_SLEEP);
53397c478bd9Sstevel@tonic-gate 	(void) mdb_nv_create(&pt->p_env, UM_SLEEP);
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 	t->t_ops = &proc_ops;
53427c478bd9Sstevel@tonic-gate 	t->t_data = pt;
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 	/*
53457c478bd9Sstevel@tonic-gate 	 * If no core file name was specified, but the file ./core is present,
53467c478bd9Sstevel@tonic-gate 	 * infer that we want to debug it.  I find this behavior confusing,
53477c478bd9Sstevel@tonic-gate 	 * so we only do this when precise adb(1) compatibility is required.
53487c478bd9Sstevel@tonic-gate 	 */
53497c478bd9Sstevel@tonic-gate 	if (core_path == NULL && (mdb.m_flags & MDB_FL_ADB) &&
53507c478bd9Sstevel@tonic-gate 	    access(PT_CORE_PATH, F_OK) == 0)
53517c478bd9Sstevel@tonic-gate 		core_path = PT_CORE_PATH;
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate 	/*
53547c478bd9Sstevel@tonic-gate 	 * For compatibility with adb(1), the special name "-" may be used
53557c478bd9Sstevel@tonic-gate 	 * to suppress the loading of the executable or core file.
53567c478bd9Sstevel@tonic-gate 	 */
53577c478bd9Sstevel@tonic-gate 	if (aout_path != NULL && strcmp(aout_path, "-") == 0)
53587c478bd9Sstevel@tonic-gate 		aout_path = NULL;
53597c478bd9Sstevel@tonic-gate 	if (core_path != NULL && strcmp(core_path, "-") == 0)
53607c478bd9Sstevel@tonic-gate 		core_path = NULL;
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	/*
53637c478bd9Sstevel@tonic-gate 	 * If a core file or pid was specified, attempt to grab it now using
53647c478bd9Sstevel@tonic-gate 	 * proc_arg_grab(); otherwise we'll create a fresh process later.
53657c478bd9Sstevel@tonic-gate 	 */
53667c478bd9Sstevel@tonic-gate 	if (core_path != NULL && (t->t_pshandle = proc_arg_xgrab(core_path,
53677c478bd9Sstevel@tonic-gate 	    aout_path == PT_EXEC_PATH ? NULL : aout_path, PR_ARG_ANY,
53687c478bd9Sstevel@tonic-gate 	    pt->p_gflags, &perr, NULL)) == NULL) {
53697c478bd9Sstevel@tonic-gate 		mdb_warn("cannot debug %s: %s\n", core_path, Pgrab_error(perr));
53707c478bd9Sstevel@tonic-gate 		goto err;
53717c478bd9Sstevel@tonic-gate 	}
53727c478bd9Sstevel@tonic-gate 
53737c478bd9Sstevel@tonic-gate 	if (aout_path != NULL &&
53747c478bd9Sstevel@tonic-gate 	    (pt->p_idlehandle = Pgrab_file(aout_path, &perr)) != NULL &&
53757c478bd9Sstevel@tonic-gate 	    t->t_pshandle == NULL)
53767c478bd9Sstevel@tonic-gate 		t->t_pshandle = pt->p_idlehandle;
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL)
53797c478bd9Sstevel@tonic-gate 		state = Pstate(t->t_pshandle);
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 	/*
53827c478bd9Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
53837c478bd9Sstevel@tonic-gate 	 * has many many mappings.
53847c478bd9Sstevel@tonic-gate 	 */
53857c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
53867c478bd9Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
53877c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
5388004388ebScasper 		(void) enable_extended_FILE_stdio(-1, -1);
53897c478bd9Sstevel@tonic-gate 	}
53907c478bd9Sstevel@tonic-gate 
53917c478bd9Sstevel@tonic-gate 	/*
53927c478bd9Sstevel@tonic-gate 	 * If we don't have an executable path or the executable path is the
5393a48fdbefSBryan Cantrill 	 * /proc/<pid>/object/a.out path, but we now have a libproc handle (and
5394a48fdbefSBryan Cantrill 	 * it didn't come from a core file), attempt to derive the executable
5395a48fdbefSBryan Cantrill 	 * path using Pexecname().  We need to do this in the /proc case in
5396a48fdbefSBryan Cantrill 	 * order to open the executable for writing because /proc/object/<file>
5397a48fdbefSBryan Cantrill 	 * permission are masked with 0555.  If Pexecname() fails us, fall back
5398a48fdbefSBryan Cantrill 	 * to /proc/<pid>/object/a.out.
53997c478bd9Sstevel@tonic-gate 	 */
5400a48fdbefSBryan Cantrill 	if (t->t_pshandle != NULL && core_path == NULL &&
5401a48fdbefSBryan Cantrill 	    (aout_path == NULL || (stat64(aout_path, &st) == 0 &&
5402a48fdbefSBryan Cantrill 	    strcmp(st.st_fstype, "proc") == 0))) {
54037c478bd9Sstevel@tonic-gate 		GElf_Sym s;
54047c478bd9Sstevel@tonic-gate 		aout_path = Pexecname(t->t_pshandle, execname, MAXPATHLEN);
54057c478bd9Sstevel@tonic-gate 		if (aout_path == NULL && state != PS_DEAD && state != PS_IDLE) {
54067c478bd9Sstevel@tonic-gate 			(void) mdb_iob_snprintf(execname, sizeof (execname),
54077c478bd9Sstevel@tonic-gate 			    "/proc/%d/object/a.out",
54087c478bd9Sstevel@tonic-gate 			    (int)Pstatus(t->t_pshandle)->pr_pid);
54097c478bd9Sstevel@tonic-gate 			aout_path = execname;
54107c478bd9Sstevel@tonic-gate 		}
54117c478bd9Sstevel@tonic-gate 		if (aout_path == NULL &&
54127c478bd9Sstevel@tonic-gate 		    Plookup_by_name(t->t_pshandle, "a.out", "_start", &s) != 0)
54137c478bd9Sstevel@tonic-gate 			mdb_warn("warning: failed to infer pathname to "
54147c478bd9Sstevel@tonic-gate 			    "executable; symbol table will not be available\n");
54157c478bd9Sstevel@tonic-gate 
54167c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_TGT, "a.out is %s\n", aout_path);
54177c478bd9Sstevel@tonic-gate 	}
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 	/*
54207c478bd9Sstevel@tonic-gate 	 * Attempt to open the executable file.  We only want this operation
54217c478bd9Sstevel@tonic-gate 	 * to actually cause the constructor to abort if the executable file
54227c478bd9Sstevel@tonic-gate 	 * name was given explicitly.  If we defaulted to PT_EXEC_PATH or
54237c478bd9Sstevel@tonic-gate 	 * derived the executable using Pexecname, then we want to continue
54247c478bd9Sstevel@tonic-gate 	 * along with p_fio and p_file set to NULL.
54257c478bd9Sstevel@tonic-gate 	 */
54267c478bd9Sstevel@tonic-gate 	if (aout_path != NULL && (pt->p_aout_fio = mdb_fdio_create_path(NULL,
54277c478bd9Sstevel@tonic-gate 	    aout_path, pt->p_oflags, 0)) == NULL && argc > 0) {
54287c478bd9Sstevel@tonic-gate 		mdb_warn("failed to open %s", aout_path);
54297c478bd9Sstevel@tonic-gate 		goto err;
54307c478bd9Sstevel@tonic-gate 	}
54317c478bd9Sstevel@tonic-gate 
54327c478bd9Sstevel@tonic-gate 	/*
54337c478bd9Sstevel@tonic-gate 	 * Now create an ELF file from the input file, if we have one.  Again,
54347c478bd9Sstevel@tonic-gate 	 * only abort the constructor if the name was given explicitly.
54357c478bd9Sstevel@tonic-gate 	 */
54367c478bd9Sstevel@tonic-gate 	if (pt->p_aout_fio != NULL && pt_open_aout(t,
54377c478bd9Sstevel@tonic-gate 	    mdb_io_hold(pt->p_aout_fio)) == NULL && argc > 0)
54387c478bd9Sstevel@tonic-gate 		goto err;
54397c478bd9Sstevel@tonic-gate 
54407c478bd9Sstevel@tonic-gate 	/*
54417c478bd9Sstevel@tonic-gate 	 * If we've successfully opened an ELF file, select the appropriate
54427c478bd9Sstevel@tonic-gate 	 * disassembler based on the ELF header.
54437c478bd9Sstevel@tonic-gate 	 */
54447c478bd9Sstevel@tonic-gate 	if (pt->p_file != NULL)
54457c478bd9Sstevel@tonic-gate 		(void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
54467c478bd9Sstevel@tonic-gate 	else
54477c478bd9Sstevel@tonic-gate 		(void) mdb_dis_select(pt_disasm(NULL));
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 	/*
54507c478bd9Sstevel@tonic-gate 	 * Add each register described in the target ISA register description
54517c478bd9Sstevel@tonic-gate 	 * list to our hash table of register descriptions and then add any
54527c478bd9Sstevel@tonic-gate 	 * appropriate ISA-specific floating-point register descriptions.
54537c478bd9Sstevel@tonic-gate 	 */
54547c478bd9Sstevel@tonic-gate 	for (rdp = pt_regdesc; rdp->rd_name != NULL; rdp++) {
54557c478bd9Sstevel@tonic-gate 		(void) mdb_nv_insert(&pt->p_regs, rdp->rd_name, NULL,
54567c478bd9Sstevel@tonic-gate 		    MDB_TGT_R_NVAL(rdp->rd_num, rdp->rd_flags), MDB_NV_RDONLY);
54577c478bd9Sstevel@tonic-gate 	}
54587c478bd9Sstevel@tonic-gate 	pt_addfpregs(t);
54597c478bd9Sstevel@tonic-gate 
54607c478bd9Sstevel@tonic-gate 	/*
54617c478bd9Sstevel@tonic-gate 	 * Certain important /proc structures may be of interest to mdb
54627c478bd9Sstevel@tonic-gate 	 * modules and their dcmds.  Export these using the xdata interface:
54637c478bd9Sstevel@tonic-gate 	 */
54647c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "auxv",
54657c478bd9Sstevel@tonic-gate 	    "procfs auxv_t array", pt_xd_auxv);
54667c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "cred",
54677c478bd9Sstevel@tonic-gate 	    "procfs prcred_t structure", pt_xd_cred);
54687c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "ehdr",
54697c478bd9Sstevel@tonic-gate 	    "executable file GElf_Ehdr structure", pt_xd_ehdr);
54707c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "lwpstatus",
54717c478bd9Sstevel@tonic-gate 	    "procfs lwpstatus_t array", pt_xd_lwpstatus);
54727c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "pshandle",
54737c478bd9Sstevel@tonic-gate 	    "libproc proc service API handle", pt_xd_pshandle);
54747c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "psinfo",
54757c478bd9Sstevel@tonic-gate 	    "procfs psinfo_t structure", pt_xd_psinfo);
54767c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "pstatus",
54777c478bd9Sstevel@tonic-gate 	    "procfs pstatus_t structure", pt_xd_pstatus);
54787c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_xdata_insert(t, "utsname",
54797c478bd9Sstevel@tonic-gate 	    "utsname structure", pt_xd_utsname);
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 	/*
54827c478bd9Sstevel@tonic-gate 	 * Force a status update now so that we fill in t_status with the
54837c478bd9Sstevel@tonic-gate 	 * latest information based on any successful grab.
54847c478bd9Sstevel@tonic-gate 	 */
54857c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_status(t, &t->t_status);
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	/*
54887c478bd9Sstevel@tonic-gate 	 * If we're not examining a core file, trace SIGINT and all signals
54897c478bd9Sstevel@tonic-gate 	 * that cause the process to dump core as part of our initialization.
54907c478bd9Sstevel@tonic-gate 	 */
54917c478bd9Sstevel@tonic-gate 	if ((t->t_pshandle != NULL && state != PS_DEAD && state != PS_IDLE) ||
54927c478bd9Sstevel@tonic-gate 	    (pt->p_file != NULL && pt->p_file->gf_ehdr.e_type == ET_EXEC)) {
54937c478bd9Sstevel@tonic-gate 
54947c478bd9Sstevel@tonic-gate 		int tflag = MDB_TGT_SPEC_STICKY; /* default sigs are sticky */
54957c478bd9Sstevel@tonic-gate 
54967c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGINT, tflag, no_se_f, NULL);
54977c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGQUIT, tflag, no_se_f, NULL);
54987c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGILL, tflag, no_se_f, NULL);
54997c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGTRAP, tflag, no_se_f, NULL);
55007c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGABRT, tflag, no_se_f, NULL);
55017c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGEMT, tflag, no_se_f, NULL);
55027c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGFPE, tflag, no_se_f, NULL);
55037c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGBUS, tflag, no_se_f, NULL);
55047c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGSEGV, tflag, no_se_f, NULL);
55057c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGSYS, tflag, no_se_f, NULL);
55067c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGXCPU, tflag, no_se_f, NULL);
55077c478bd9Sstevel@tonic-gate 		(void) mdb_tgt_add_signal(t, SIGXFSZ, tflag, no_se_f, NULL);
55087c478bd9Sstevel@tonic-gate 	}
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	/*
55117c478bd9Sstevel@tonic-gate 	 * If we've grabbed a live process, establish our initial breakpoints
55127c478bd9Sstevel@tonic-gate 	 * and librtld_db agent so we can track rtld activity.  If FL_VCREATE
55137c478bd9Sstevel@tonic-gate 	 * is set, this process was created by a previous instantiation of
55147c478bd9Sstevel@tonic-gate 	 * the debugger, so reset pr_flags to kill it; otherwise we attached
55157c478bd9Sstevel@tonic-gate 	 * to an already running process.  Pgrab() has already set the PR_RLC
55167c478bd9Sstevel@tonic-gate 	 * flag appropriately based on whether the process was stopped when we
55177c478bd9Sstevel@tonic-gate 	 * attached.
55187c478bd9Sstevel@tonic-gate 	 */
55197c478bd9Sstevel@tonic-gate 	if (t->t_pshandle != NULL && state != PS_DEAD && state != PS_IDLE) {
55207c478bd9Sstevel@tonic-gate 		if (mdb.m_flags & MDB_FL_VCREATE) {
55217c478bd9Sstevel@tonic-gate 			(void) Punsetflags(t->t_pshandle, PR_RLC);
55227c478bd9Sstevel@tonic-gate 			(void) Psetflags(t->t_pshandle, PR_KLC);
55237c478bd9Sstevel@tonic-gate 			pt->p_rflags = PRELEASE_KILL;
55247c478bd9Sstevel@tonic-gate 		} else {
55257c478bd9Sstevel@tonic-gate 			(void) Punsetflags(t->t_pshandle, PR_KLC);
55267c478bd9Sstevel@tonic-gate 		}
55277c478bd9Sstevel@tonic-gate 		pt_post_attach(t);
55287c478bd9Sstevel@tonic-gate 	}
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 	/*
55317c478bd9Sstevel@tonic-gate 	 * Initialize a local copy of the environment, which can be modified
55327c478bd9Sstevel@tonic-gate 	 * before running the program.
55337c478bd9Sstevel@tonic-gate 	 */
55347c478bd9Sstevel@tonic-gate 	for (i = 0; mdb.m_env[i] != NULL; i++)
55357c478bd9Sstevel@tonic-gate 		pt_env_set(pt, mdb.m_env[i]);
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate 	/*
55387c478bd9Sstevel@tonic-gate 	 * If adb(1) compatibility mode is on, then print the appropriate
55397c478bd9Sstevel@tonic-gate 	 * greeting message if we have grabbed a core file.
55407c478bd9Sstevel@tonic-gate 	 */
55417c478bd9Sstevel@tonic-gate 	if ((mdb.m_flags & MDB_FL_ADB) && t->t_pshandle != NULL &&
55427c478bd9Sstevel@tonic-gate 	    state == PS_DEAD) {
55437c478bd9Sstevel@tonic-gate 		const pstatus_t *psp = Pstatus(t->t_pshandle);
55447c478bd9Sstevel@tonic-gate 		int cursig = psp->pr_lwp.pr_cursig;
55457c478bd9Sstevel@tonic-gate 		char signame[SIG2STR_MAX];
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 		mdb_printf("core file = %s -- program ``%s'' on platform %s\n",
55487c478bd9Sstevel@tonic-gate 		    core_path, aout_path ? aout_path : "?", pt_platform(t));
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 		if (cursig != 0 && sig2str(cursig, signame) == 0)
55517c478bd9Sstevel@tonic-gate 			mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
55527c478bd9Sstevel@tonic-gate 	}
55537c478bd9Sstevel@tonic-gate 
55547c478bd9Sstevel@tonic-gate 	return (0);
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate err:
55577c478bd9Sstevel@tonic-gate 	pt_destroy(t);
55587c478bd9Sstevel@tonic-gate 	return (-1);
55597c478bd9Sstevel@tonic-gate }
5560