blob: 33994c11e65124d99a132dbe3b6d93a18f4ef9ff [file] [log] [blame]
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2018 Joyent, Inc.
* Copyright (c) 2014 by Delphix. All rights reserved.
*/
/*
* User Process Target
*
* The user process target is invoked when the -u or -p command-line options
* are used, or when an ELF executable file or ELF core file is specified on
* the command-line. This target is also selected by default when no target
* options are present. In this case, it defaults the executable name to
* "a.out". If no process or core file is currently attached, the target
* functions as a kind of virtual /dev/zero (in accordance with adb(1)
* semantics); reads from the virtual address space return zeroes and writes
* fail silently. The proc target itself is designed as a wrapper around the
* services provided by libproc.so: t->t_pshandle is set to the struct
* ps_prochandle pointer returned as a handle by libproc. The target also
* opens the executable file itself using the MDB GElf services, for
* interpreting the .symtab and .dynsym if no libproc handle has been
* initialized, and for handling i/o to and from the object file. Currently,
* the only ISA-dependent portions of the proc target are the $r and ::fpregs
* dcmds, the callbacks for t_next() and t_step_out(), and the list of named
* registers; these are linked in from the proc_isadep.c file for each ISA and
* called from the common code in this file.
*
* The user process target implements complete user process control using the
* facilities provided by libproc.so. The MDB execution control model and
* an overview of software event management is described in mdb_target.c. The
* proc target implements breakpoints by replacing the instruction of interest
* with a trap instruction, and then restoring the original instruction to step
* over the breakpoint. The idea of replacing program text with instructions
* that transfer control to the debugger dates back as far as 1951 [1]. When
* the target stops, we replace each breakpoint with the original instruction
* as part of the disarm operation. This means that no special processing is
* required for t_vread() because the instrumented instructions will never be
* seen by the debugger once the target stops. Some debuggers have improved
* start/stop performance by leaving breakpoint traps in place and then
* handling a read from a breakpoint address as a special case. Although this
* improves efficiency for a source-level debugger, it runs somewhat contrary
* to the philosophy of the low-level debugger. Since we remove the
* instructions, users can apply other external debugging tools to the process
* once it has stopped (e.g. the proc(1) tools) and not be misled by MDB
* instrumentation. The tracing of faults, signals, system calls, and
* watchpoints and general process inspection is implemented directly using
* the mechanisms provided by /proc, as described originally in [2] and [3].
*
* References
*
* [1] S. Gill, "The Diagnosis Of Mistakes In Programmes on the EDSAC",
* Proceedings of the Royal Society Series A Mathematical and Physical
* Sciences, Cambridge University Press, 206(1087), May 1951, pp. 538-554.
*
* [2] T.J. Killian, "Processes as Files", Proceedings of the USENIX Association
* Summer Conference, Salt Lake City, June 1984, pp. 203-207.
*
* [3] Roger Faulkner and Ron Gomes, "The Process File System and Process
* Model in UNIX System V", Proceedings of the USENIX Association
* Winter Conference, Dallas, January 1991, pp. 243-252.
*/
#include <mdb/mdb_proc.h>
#include <mdb/mdb_disasm.h>
#include <mdb/mdb_signal.h>
#include <mdb/mdb_string.h>
#include <mdb/mdb_module.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_conf.h>
#include <mdb/mdb_err.h>
#include <mdb/mdb_types.h>
#include <mdb/mdb.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <termio.h>
#include <signal.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#define PC_FAKE -1UL /* illegal pc value unequal 0 */
#define PANIC_BUFSIZE 1024
static const char PT_EXEC_PATH[] = "a.out"; /* Default executable */
static const char PT_CORE_PATH[] = "core"; /* Default core file */
static const pt_ptl_ops_t proc_lwp_ops;
static const pt_ptl_ops_t proc_tdb_ops;
static const mdb_se_ops_t proc_brkpt_ops;
static const mdb_se_ops_t proc_wapt_ops;
static int pt_setrun(mdb_tgt_t *, mdb_tgt_status_t *, int);
static void pt_activate_common(mdb_tgt_t *);
static mdb_tgt_vespec_f pt_ignore_sig;
static mdb_tgt_se_f pt_fork;
static mdb_tgt_se_f pt_exec;
static int pt_lookup_by_name_thr(mdb_tgt_t *, const char *,
const char *, GElf_Sym *, mdb_syminfo_t *, mdb_tgt_tid_t);
static int tlsbase(mdb_tgt_t *, mdb_tgt_tid_t, Lmid_t, const char *,
psaddr_t *);
/*
* When debugging postmortem, we don't resolve names as we may very well not
* be on a system on which those names resolve.
*/
#define PT_LIBPROC_RESOLVE(P) \
(!(mdb.m_flags & MDB_FL_LMRAW) && Pstate(P) != PS_DEAD)
/*
* The Perror_printf() function interposes on the default, empty libproc
* definition. It will be called to report additional information on complex
* errors, such as a corrupt core file. We just pass the args to vwarn.
*/
/*ARGSUSED*/
void
Perror_printf(struct ps_prochandle *P, const char *format, ...)
{
va_list alist;
va_start(alist, format);
vwarn(format, alist);
va_end(alist);
}
/*
* Open the specified i/o backend as the a.out executable file, and attempt to
* load its standard and dynamic symbol tables. Note that if mdb_gelf_create
* succeeds, io is assigned to p_fio and is automatically held by gelf_create.
*/
static mdb_gelf_file_t *
pt_open_aout(mdb_tgt_t *t, mdb_io_t *io)
{
pt_data_t *pt = t->t_data;
GElf_Sym s1, s2;
if ((pt->p_file = mdb_gelf_create(io, ET_NONE, GF_FILE)) == NULL)
return (NULL);
pt->p_symtab = mdb_gelf_symtab_create_file(pt->p_file,
SHT_SYMTAB, MDB_TGT_SYMTAB);
pt->p_dynsym = mdb_gelf_symtab_create_file(pt->p_file,
SHT_DYNSYM, MDB_TGT_DYNSYM);
/*
* If we've got an _start symbol with a zero size, prime the private
* symbol table with a copy of _start with its size set to the distance
* between _mcount and _start. We do this because DevPro has shipped
* the Intel crt1.o without proper .size directives for years, which
* precludes proper identification of _start in stack traces.
*/
if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_start", &s1,
NULL) == 0 && s1.st_size == 0 &&
GELF_ST_TYPE(s1.st_info) == STT_FUNC) {
if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_mcount",
&s2, NULL) == 0 && GELF_ST_TYPE(s2.st_info) == STT_FUNC) {
s1.st_size = s2.st_value - s1.st_value;
mdb_gelf_symtab_insert(mdb.m_prsym, "_start", &s1);
}
}
pt->p_fio = io;
return (pt->p_file);
}
/*
* Destroy the symbol tables and GElf file object associated with p_fio. Note
* that we do not need to explicitly free p_fio: its reference count is
* automatically decremented by mdb_gelf_destroy, which will free it if needed.
*/
static void
pt_close_aout(mdb_tgt_t *t)
{
pt_data_t *pt = t->t_data;
if (pt->p_symtab != NULL) {
mdb_gelf_symtab_destroy(pt->p_symtab);
pt->p_symtab = NULL;
}
if (pt->p_dynsym != NULL) {
mdb_gelf_symtab_destroy(pt->p_dynsym);
pt->p_dynsym = NULL;
}
if (pt->p_file != NULL) {
mdb_gelf_destroy(pt->p_file);
pt->p_file = NULL;
}
mdb_gelf_symtab_delete(mdb.m_prsym, "_start", NULL);
pt->p_fio = NULL;
}
typedef struct tdb_mapping {
const char *tm_thr_lib;
const char *tm_db_dir;
const char *tm_db_name;
} tdb_mapping_t;
static const tdb_mapping_t tdb_map[] = {
{ "/lwp/amd64/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
{ "/lwp/sparcv9/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
{ "/lwp/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
{ "/libthread.so", "/lib/", "libthread_db.so" },
{ "/libc_hwcap", "/lib/", "libc_db.so" },
{ "/libc.so", "/lib/", "libc_db.so" }
};
/*
* Pobject_iter callback that we use to search for the presence of libthread in
* order to load the corresponding libthread_db support. We derive the
* libthread_db path dynamically based on the libthread path. If libthread is
* found, this function returns 1 (and thus Pobject_iter aborts and returns 1)
* regardless of whether it was successful in loading the libthread_db support.
* If we iterate over all objects and no libthread is found, 0 is returned.
* Since libthread_db support was then merged into libc_db, we load either
* libc_db or libthread_db, depending on which library we see first.
*/
/*ARGSUSED*/
static int
thr_check(mdb_tgt_t *t, const prmap_t *pmp, const char *name)
{
pt_data_t *pt = t->t_data;
const mdb_tdb_ops_t *ops;
char *p;
char path[MAXPATHLEN];
int libn;
if (name == NULL)
return (0); /* no rtld_db object name; keep going */
for (libn = 0; libn < sizeof (tdb_map) / sizeof (tdb_map[0]); libn++) {
if ((p = strstr(name, tdb_map[libn].tm_thr_lib)) != NULL)
break;
}
if (p == NULL)
return (0); /* no match; keep going */
path[0] = '\0';
(void) strlcat(path, mdb.m_root, sizeof (path));
(void) strlcat(path, tdb_map[libn].tm_db_dir, sizeof (path));
#if !defined(_ILP32)
(void) strlcat(path, "64/", sizeof (path));
#endif /* !_ILP32 */
(void) strlcat(path, tdb_map[libn].tm_db_name, sizeof (path));
/* Append the trailing library version number. */
(void) strlcat(path, strrchr(name, '.'), sizeof (path));
if ((ops = mdb_tdb_load(path)) == NULL) {
if (libn != 0 || errno != ENOENT)
warn("failed to load %s", path);
goto err;
}
if (ops == pt->p_tdb_ops)
return (1); /* no changes needed */
PTL_DTOR(t);
pt->p_tdb_ops = ops;
pt->p_ptl_ops = &proc_tdb_ops;
pt->p_ptl_hdl = NULL;
if (PTL_CTOR(t) == -1) {
warn("failed to initialize %s", path);
goto err;
}
mdb_dprintf(MDB_DBG_TGT, "loaded %s for debugging %s\n", path, name);
(void) mdb_tgt_status(t, &t->t_status);
return (1);
err:
PTL_DTOR(t);
pt->p_tdb_ops = NULL;
pt->p_ptl_ops = &proc_lwp_ops;
pt->p_ptl_hdl = NULL;
if (libn != 0 || errno != ENOENT) {
warn("warning: debugger will only be able to "
"examine raw LWPs\n");
}
(void) mdb_tgt_status(t, &t->t_status);
return (1);
}
/*
* Whenever the link map is consistent following an add or delete event, we ask
* libproc to update its mappings, check to see if we need to load libthread_db,
* and then update breakpoints which have been mapped or unmapped.
*/
/*ARGSUSED*/
static void
pt_rtld_event(mdb_tgt_t *t, int vid, void *private)
{
struct ps_prochandle *P = t->t_pshandle;
pt_data_t *pt = t->t_data;
rd_event_msg_t rdm;
int docontinue = 1;
if (rd_event_getmsg(pt->p_rtld, &rdm) == RD_OK) {
mdb_dprintf(MDB_DBG_TGT, "rtld event type 0x%x state 0x%x\n",
rdm.type, rdm.u.state);
if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_CONSISTENT) {
mdb_sespec_t *sep, *nsep = mdb_list_next(&t->t_active);
pt_brkpt_t *ptb;
Pupdate_maps(P);
if (Pobject_iter(P, (proc_map_f *)thr_check, t) == 0 &&
pt->p_ptl_ops != &proc_lwp_ops) {
mdb_dprintf(MDB_DBG_TGT, "unloading thread_db "
"support after dlclose\n");
PTL_DTOR(t);
pt->p_tdb_ops = NULL;
pt->p_ptl_ops = &proc_lwp_ops;
pt->p_ptl_hdl = NULL;
(void) mdb_tgt_status(t, &t->t_status);
}
for (sep = nsep; sep != NULL; sep = nsep) {
nsep = mdb_list_next(sep);
ptb = sep->se_data;
if (sep->se_ops == &proc_brkpt_ops &&
Paddr_to_map(P, ptb->ptb_addr) == NULL)
mdb_tgt_sespec_idle_one(t, sep,
EMDB_NOMAP);
}
if (!mdb_tgt_sespec_activate_all(t) &&
(mdb.m_flags & MDB_FL_BPTNOSYMSTOP) &&
pt->p_rtld_finished) {
/*
* We weren't able to activate the breakpoints.
* If so requested, we'll return without
* calling continue, thus throwing the user into
* the debugger.
*/
docontinue = 0;
}
if (pt->p_rdstate == PT_RD_ADD)
pt->p_rdstate = PT_RD_CONSIST;
}
if (rdm.type == RD_PREINIT)
(void) mdb_tgt_sespec_activate_all(t);
if (rdm.type == RD_POSTINIT) {
pt->p_rtld_finished = TRUE;
if (!mdb_tgt_sespec_activate_all(t) &&
(mdb.m_flags & MDB_FL_BPTNOSYMSTOP)) {
/*
* Now that rtld has been initialized, we
* should be able to initialize all deferred
* breakpoints. If we can't, don't let the
* target continue.
*/
docontinue = 0;
}
}
if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_ADD &&
pt->p_rtld_finished)
pt->p_rdstate = MAX(pt->p_rdstate, PT_RD_ADD);
}
if (docontinue)
(void) mdb_tgt_continue(t, NULL);
}
static void
pt_post_attach(mdb_tgt_t *t)
{
struct ps_prochandle *P = t->t_pshandle;
const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
pt_data_t *pt = t->t_data;
int hflag = MDB_TGT_SPEC_HIDDEN;
mdb_dprintf(MDB_DBG_TGT, "attach pr_flags=0x%x pr_why=%d pr_what=%d\n",
psp->pr_flags, psp->pr_why, psp->pr_what);
/*
* When we grab a process, the initial setting of p_rtld_finished
* should be false if the process was just created by exec; otherwise
* we permit unscoped references to resolve because we do not know how
* far the process has proceeded through linker initialization.
*/
if ((psp->pr_flags & PR_ISTOP) && psp->pr_why == PR_SYSEXIT &&
psp->pr_errno == 0 && psp->pr_what == SYS_execve) {
if (mdb.m_target == NULL) {
warn("target performed exec of %s\n",
IOP_NAME(pt->p_fio));
}
pt->p_rtld_finished = FALSE;
} else
pt->p_rtld_finished = TRUE;
/*
* When we grab a process, if it is stopped by job control and part of
* the same session (i.e. same controlling tty), set MDB_FL_JOBCTL so
* we will know to bring it to the foreground when we continue it.
*/
if (mdb.m_term != NULL && (psp->pr_flags & PR_STOPPED) &&
psp->pr_why == PR_JOBCONTROL && getsid(0) == Pstatus(P)->pr_sid)
mdb.m_flags |= MDB_FL_JOBCTL;
/*
* When we grab control of a live process, set F_RDWR so that the
* target layer permits writes to the target's address space.
*/
t->t_flags |= MDB_TGT_F_RDWR;
(void) Pfault(P, FLTBPT, TRUE); /* always trace breakpoints */
(void) Pfault(P, FLTWATCH, TRUE); /* always trace watchpoints */
(void) Pfault(P, FLTTRACE, TRUE); /* always trace single-step */
(void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */
(void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */
(void) Psetflags(P, PR_FORK); /* inherit tracing on fork */
/*
* Install event specifiers to track fork and exec activities:
*/
(void) mdb_tgt_add_sysexit(t, SYS_vfork, hflag, pt_fork, NULL);
(void) mdb_tgt_add_sysexit(t, SYS_forksys, hflag, pt_fork, NULL);
(void) mdb_tgt_add_sysexit(t, SYS_execve, hflag, pt_exec, NULL);
/*
* Attempt to instantiate the librtld_db agent and set breakpoints
* to track rtld activity. We will legitimately fail to instantiate
* the rtld_db agent if the target is statically linked.
*/
if (pt->p_rtld == NULL && (pt->p_rtld = Prd_agent(P)) != NULL) {
rd_notify_t rdn;
rd_err_e err;
if ((err = rd_event_enable(pt->p_rtld, TRUE)) != RD_OK) {
warn("failed to enable rtld_db event tracing: %s\n",
rd_errstr(err));
goto out;
}
if ((err = rd_event_addr(pt->p_rtld, RD_PREINIT,
&rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
(void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
hflag, pt_rtld_event, NULL);
} else {
warn("failed to install rtld_db preinit tracing: %s\n",
rd_errstr(err));
}
if ((err = rd_event_addr(pt->p_rtld, RD_POSTINIT,
&rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
(void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
hflag, pt_rtld_event, NULL);
} else {
warn("failed to install rtld_db postinit tracing: %s\n",
rd_errstr(err));
}
if ((err = rd_event_addr(pt->p_rtld, RD_DLACTIVITY,
&rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
(void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
hflag, pt_rtld_event, NULL);
} else {
warn("failed to install rtld_db activity tracing: %s\n",
rd_errstr(err));
}
}
out:
Pupdate_maps(P);
Psync(P);
/*
* If librtld_db failed to initialize due to an error or because we are
* debugging a statically linked executable, allow unscoped references.
*/
if (pt->p_rtld == NULL)
pt->p_rtld_finished = TRUE;
(void) mdb_tgt_sespec_activate_all(t);
}
/*ARGSUSED*/
static int
pt_vespec_delete(mdb_tgt_t *t, void *private, int id, void *data)
{
if (id < 0) {
ASSERT(data == NULL); /* we don't use any ve_data */
(void) mdb_tgt_vespec_delete(t, id);
}
return (0);
}
static void
pt_pre_detach(mdb_tgt_t *t, int clear_matched)
{
const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
pt_data_t *pt = t->t_data;
long cmd = 0;
/*
* If we are about to release the process and it is stopped on a traced
* SIGINT, breakpoint fault, single-step fault, or watchpoint, make
* sure to clear this event prior to releasing the process so that it
* does not subsequently reissue the fault and die from SIGTRAP.
*/
if (psp->pr_flags & PR_ISTOP) {
if (psp->pr_why == PR_FAULTED && (psp->pr_what == FLTBPT ||
psp->pr_what == FLTTRACE || psp->pr_what == FLTWATCH))
cmd = PCCFAULT;
else if (psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
cmd = PCCSIG;
if (cmd != 0)
(void) write(Pctlfd(t->t_pshandle), &cmd, sizeof (cmd));
}
if (Pstate(t->t_pshandle) == PS_UNDEAD)
(void) waitpid(Pstatus(t->t_pshandle)->pr_pid, NULL, WNOHANG);
(void) mdb_tgt_vespec_iter(t, pt_vespec_delete, NULL);
mdb_tgt_sespec_idle_all(t, EMDB_NOPROC, clear_matched);
if (pt->p_fio != pt->p_aout_fio) {
pt_close_aout(t);
(void) pt_open_aout(t, pt->p_aout_fio);
}
PTL_DTOR(t);
pt->p_tdb_ops = NULL;
pt->p_ptl_ops = &proc_lwp_ops;
pt->p_ptl_hdl = NULL;
pt->p_rtld = NULL;
pt->p_signal = 0;
pt->p_rtld_finished = FALSE;
pt->p_rdstate = PT_RD_NONE;
}
static void
pt_release_parents(mdb_tgt_t *t)
{
struct ps_prochandle *P = t->t_pshandle;
pt_data_t *pt = t->t_data;
mdb_sespec_t *sep;
pt_vforkp_t *vfp;
while ((vfp = mdb_list_next(&pt->p_vforkp)) != NULL) {
mdb_dprintf(MDB_DBG_TGT, "releasing vfork parent %d\n",
(int)Pstatus(vfp->p_pshandle)->pr_pid);
/*
* To release vfork parents, we must also wipe out any armed
* events in the parent by switching t_pshandle and calling
* se_disarm(). Do not change states or lose the matched list.
*/
t->t_pshandle = vfp->p_pshandle;
for (sep = mdb_list_next(&t->t_active); sep != NULL;
sep = mdb_list_next(sep)) {
if (sep->se_state == MDB_TGT_SPEC_ARMED)
(void) sep->se_ops->se_disarm(t, sep);
}
t->t_pshandle = P;
Prelease(vfp->p_pshandle, PRELEASE_CLEAR);
mdb_list_delete(&pt->p_vforkp, vfp);
mdb_free(vfp, sizeof (pt_vforkp_t));
}
}
/*ARGSUSED*/
static void
pt_fork(mdb_tgt_t *t, int vid, void *private)
{
struct ps_prochandle *P = t->t_pshandle;
const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
pt_data_t *pt = t->t_data;
mdb_sespec_t *sep;
int follow_parent = mdb.m_forkmode != MDB_FM_CHILD;
int is_vfork = (psp->pr_what == SYS_vfork ||
(psp->pr_what == SYS_forksys && psp->pr_sysarg[0] == 2));
struct ps_prochandle *C;
const lwpstatus_t *csp;
char sysname[32];
int gcode;
char c;
mdb_dprintf(MDB_DBG_TGT, "parent %s: errno=%d rv1=%ld rv2=%ld\n",
proc_sysname(psp->pr_what, sysname, sizeof (sysname)),
psp->pr_errno, psp->pr_rval1, psp->pr_rval2);
if (psp->pr_errno != 0) {
(void) mdb_tgt_continue(t, NULL);
return; /* fork failed */
}
/*
* If forkmode is ASK and stdout is a terminal, then ask the user to
* explicitly set the fork behavior for this particular fork.
*/
if (mdb.m_forkmode == MDB_FM_ASK && mdb.m_term != NULL) {
mdb_iob_printf(mdb.m_err, "%s: %s detected: follow (p)arent "
"or (c)hild? ", mdb.m_pname, sysname);
mdb_iob_flush(mdb.m_err);
while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
if (c == 'P' || c == 'p') {
mdb_iob_printf(mdb.m_err, "%c\n", c);
follow_parent = TRUE;
break;
} else if (c == 'C' || c == 'c') {
mdb_iob_printf(mdb.m_err, "%c\n", c);
follow_parent = FALSE;
break;
}
}
}
/*
* The parent is now stopped on exit from its fork call. We must now
* grab the child on its return from fork in order to manipulate it.
*/
if ((C = Pgrab(psp->pr_rval1, PGRAB_RETAIN, &gcode)) == NULL) {
warn("failed to grab forked child process %ld: %s\n",
psp->pr_rval1, Pgrab_error(gcode));
return; /* just stop if we failed to grab the child */
}
/*
* We may have grabbed the child and stopped it prematurely before it
* stopped on exit from fork. If so, wait up to 1 sec for it to settle.
*/
if (Pstatus(C)->pr_lwp.pr_why != PR_SYSEXIT)
(void) Pwait(C, MILLISEC);
csp = &Pstatus(C)->pr_lwp;
if (csp->pr_why != PR_SYSEXIT ||
(csp->pr_what != SYS_vfork && csp->pr_what != SYS_forksys)) {
warn("forked child process %ld did not stop on exit from "
"fork as expected\n", psp->pr_rval1);
}
warn("target forked child process %ld (debugger following %s)\n",
psp->pr_rval1, follow_parent ? "parent" : "child");
(void) Punsetflags(C, PR_ASYNC); /* require synchronous mode */
(void) Psetflags(C, PR_BPTADJ); /* always adjust eip on x86 */
(void) Prd_agent(C); /* initialize librtld_db */
/*
* At the time pt_fork() is called, the target event engine has already
* disarmed the specifiers on the active list, clearing out events in
* the parent process. However, this means that events that change
* the address space (e.g. breakpoints) have not been effectively
* disarmed in the child since its address space reflects the state of
* the process at the time of fork when events were armed. We must
* therefore handle this as a special case and re-invoke the disarm
* callback of each active specifier to clean out the child process.
*/
if (!is_vfork) {
for (t->t_pshandle = C, sep = mdb_list_next(&t->t_active);
sep != NULL; sep = mdb_list_next(sep)) {
if (sep->se_state == MDB_TGT_SPEC_ACTIVE)
(void) sep->se_ops->se_disarm(t, sep);
}
t->t_pshandle = P; /* restore pshandle to parent */
}
/*
* If we're following the parent process, we need to temporarily change
* t_pshandle to refer to the child handle C so that we can clear out
* all the events in the child prior to releasing it below. If we are
* tracing a vfork, we also need to explicitly wait for the child to
* exec, exit, or die before we can reset and continue the parent. We
* avoid having to deal with the vfork child forking again by clearing
* PR_FORK and setting PR_RLC; if it does fork it will effectively be
* released from our control and we will continue following the parent.
*/
if (follow_parent) {
if (is_vfork) {
mdb_tgt_status_t status;
ASSERT(psp->pr_flags & PR_VFORKP);
mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
t->t_pshandle = C;
(void) Psysexit(C, SYS_execve, TRUE);
(void) Punsetflags(C, PR_FORK | PR_KLC);
(void) Psetflags(C, PR_RLC);
do {
if (pt_setrun(t, &status, 0) == -1 ||
status.st_state == MDB_TGT_UNDEAD ||
status.st_state == MDB_TGT_LOST)
break; /* failure or process died */
} while (csp->pr_why != PR_SYSEXIT ||
csp->pr_errno != 0 || csp->pr_what != SYS_execve);
} else
t->t_pshandle = C;
}
/*
* If we are following the child, destroy any active libthread_db
* handle before we release the parent process.
*/
if (!follow_parent) {
PTL_DTOR(t);
pt->p_tdb_ops = NULL;
pt->p_ptl_ops = &proc_lwp_ops;
pt->p_ptl_hdl = NULL;
}
/*
* Idle all events to make sure the address space and tracing flags are
* restored, and then release the process we are not tracing. If we
* are following the child of a vfork, we push the parent's pshandle
* on to a list of vfork parents to be released when we exec or exit.
*/
if (is_vfork && !follow_parent) {
pt_vforkp_t *vfp = mdb_alloc(sizeof (pt_vforkp_t), UM_SLEEP);
ASSERT(psp->pr_flags & PR_VFORKP);
vfp->p_pshandle = P;
mdb_list_append(&pt->p_vforkp, vfp);
mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
} else {
mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
Prelease(t->t_pshandle, PRELEASE_CLEAR);
if (!follow_parent)
pt_release_parents(t);
}
/*
* Now that all the hard stuff is done, switch t_pshandle back to the
* process we are following and reset our events to the ACTIVE state.
* If we are following the child, reset the libthread_db handle as well
* as the rtld agent.
*/
if (follow_parent)
t->t_pshandle = P;
else {
t->t_pshandle = C;
pt->p_rtld = Prd_agent(C);
(void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
}
(void) mdb_tgt_sespec_activate_all(t);
(void) mdb_tgt_continue(t, NULL);
}
/*ARGSUSED*/
static void
pt_exec(mdb_tgt_t *t, int vid, void *private)
{
struct ps_prochandle *P = t->t_pshandle;
const pstatus_t *psp = Pstatus(P);
pt_data_t *pt = t->t_data;
int follow_exec = mdb.m_execmode == MDB_EM_FOLLOW;
pid_t pid = psp->pr_pid;
char execname[MAXPATHLEN];
mdb_sespec_t *sep, *nsep;
mdb_io_t *io;
char c;
mdb_dprintf(MDB_DBG_TGT, "exit from %s: errno=%d\n", proc_sysname(
psp->pr_lwp.pr_what, execname, sizeof (execname)),
psp->pr_lwp.pr_errno);
if (psp->pr_lwp.pr_errno != 0) {
(void) mdb_tgt_continue(t, NULL);
return; /* exec failed */
}
/*
* If execmode is ASK and stdout is a terminal, then ask the user to
* explicitly set the exec behavior for this particular exec. If
* Pstate() still shows PS_LOST, we are being called from pt_setrun()
* directly and therefore we must resume the terminal since it is still
* in the suspended state as far as tgt_continue() is concerned.
*/
if (mdb.m_execmode == MDB_EM_ASK && mdb.m_term != NULL) {
if (Pstate(P) == PS_LOST)
IOP_RESUME(mdb.m_term);
mdb_iob_printf(mdb.m_err, "%s: %s detected: (f)ollow new "
"program or (s)top? ", mdb.m_pname, execname);
mdb_iob_flush(mdb.m_err);
while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
if (c == 'F' || c == 'f') {
mdb_iob_printf(mdb.m_err, "%c\n", c);
follow_exec = TRUE;
break;
} else if (c == 'S' || c == 's') {
mdb_iob_printf(mdb.m_err, "%c\n", c);
follow_exec = FALSE;
break;
}
}
if (Pstate(P) == PS_LOST)
IOP_SUSPEND(mdb.m_term);
}
pt_release_parents(t); /* release any waiting vfork parents */
pt_pre_detach(t, FALSE); /* remove our breakpoints and idle events */
Preset_maps(P); /* libproc must delete mappings and symtabs */
pt_close_aout(t); /* free pt symbol tables and GElf file data */
/*
* If we lost control of the process across the exec and are not able
* to reopen it, we have no choice but to clear the matched event list
* and wait for the user to quit or otherwise release the process.
*/
if (Pstate(P) == PS_LOST && Preopen(P) == -1) {
int error = errno;
warn("lost control of PID %d due to exec of %s executable\n",
(int)pid, error == EOVERFLOW ? "64-bit" : "set-id");
for (sep = t->t_matched; sep != T_SE_END; sep = nsep) {
nsep = sep->se_matched;
sep->se_matched = NULL;
mdb_tgt_sespec_rele(t, sep);
}
if (error != EOVERFLOW)
return; /* just stop if we exec'd a set-id executable */
}
if (Pstate(P) != PS_LOST) {
if (Pexecname(P, execname, sizeof (execname)) == NULL) {
(void) mdb_iob_snprintf(execname, sizeof (execname),
"/proc/%d/object/a.out", (int)pid);
}
if (follow_exec == FALSE || psp->pr_dmodel == PR_MODEL_NATIVE)
warn("target performed exec of %s\n", execname);
io = mdb_fdio_create_path(NULL, execname, pt->p_oflags, 0);
if (io == NULL) {
warn("failed to open %s", execname);
warn("a.out symbol tables will not be available\n");
} else if (pt_open_aout(t, io) == NULL) {
(void) mdb_dis_select(pt_disasm(NULL));
mdb_io_destroy(io);
} else
(void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
}
/*
* We reset our libthread_db state here, but deliberately do NOT call
* PTL_DTOR because we do not want to call libthread_db's td_ta_delete.
* This interface is hopelessly broken in that it writes to the process
* address space (which we do not want it to do after an exec) and it
* doesn't bother deallocating any of its storage anyway.
*/
pt->p_tdb_ops = NULL;
pt->p_ptl_ops = &proc_lwp_ops;
pt->p_ptl_hdl = NULL;
if (follow_exec && psp->pr_dmodel != PR_MODEL_NATIVE) {
const char *argv[3];
char *state, *env;
char pidarg[16];
size_t envlen;
if (realpath(getexecname(), execname) == NULL) {
warn("cannot follow PID %d -- failed to resolve "
"debugger pathname for re-exec", (int)pid);
return;
}
warn("restarting debugger to follow PID %d ...\n", (int)pid);
mdb_dprintf(MDB_DBG_TGT, "re-exec'ing %s\n", execname);
(void) mdb_snprintf(pidarg, sizeof (pidarg), "-p%d", (int)pid);
state = mdb_get_config();
envlen = strlen(MDB_CONFIG_ENV_VAR) + 1 + strlen(state) + 1;
env = mdb_alloc(envlen, UM_SLEEP);
(void) snprintf(env, envlen,
"%s=%s", MDB_CONFIG_ENV_VAR, state);
(void) putenv(env);
argv[0] = mdb.m_pname;
argv[1] = pidarg;
argv[2] = NULL;
if (mdb.m_term != NULL)
IOP_SUSPEND(mdb.m_term);
Prelease(P, PRELEASE_CLEAR | PRELEASE_HANG);
(void) execv(execname, (char *const *)argv);
warn("failed to re-exec debugger");
if (mdb.m_term != NULL)
IOP_RESUME(mdb.m_term);
t->t_pshandle = pt->p_idlehandle;
return;
}
pt_post_attach(t); /* install tracing flags and activate events */
pt_activate_common(t); /* initialize librtld_db and libthread_db */
if (psp->pr_dmodel != PR_MODEL_NATIVE && mdb.m_term != NULL) {
warn("loadable dcmds will not operate on non-native %d-bit "
"data model\n", psp->pr_dmodel == PR_MODEL_ILP32 ? 32 : 64);
warn("use ::release -a and then run mdb -p %d to restart "
"debugger\n", (int)pid);
}
if (follow_exec)
(void) mdb_tgt_continue(t, NULL);
}
static int
pt_setflags(mdb_tgt_t *t, int flags)
{
pt_data_t *pt = t->t_data;
if ((flags ^ t->t_flags) & MDB_TGT_F_RDWR) {
int mode = (flags & MDB_TGT_F_RDWR) ? O_RDWR : O_RDONLY;
mdb_io_t *io;
if (pt->p_fio == NULL)
return (set_errno(EMDB_NOEXEC));
io = mdb_fdio_create_path(NULL, IOP_NAME(pt->p_fio), mode, 0);
if (io == NULL)
return (-1); /* errno is set for us */
t->t_flags = (t->t_flags & ~MDB_TGT_F_RDWR) |
(flags & MDB_TGT_F_RDWR);
pt->p_fio = mdb_io_hold(io);
mdb_io_rele(pt->p_file->gf_io);
pt->p_file->gf_io = pt->p_fio;
}
if (flags & MDB_TGT_F_FORCE) {
t->t_flags |= MDB_TGT_F_FORCE;
pt->p_gflags |= PGRAB_FORCE;
}
return (0);
}
/*ARGSUSED*/
static int
pt_frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
const mdb_tgt_gregset_t *gregs)
{
argc = MIN(argc, (uint_t)(uintptr_t)arglim);
mdb_printf("%a(", pc);
if (argc != 0) {
mdb_printf("%lr", *argv++);
for (argc--; argc != 0; argc--)
mdb_printf(", %lr", *argv++);
}
mdb_printf(")\n");
return (0);
}
static int
pt_framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
const mdb_tgt_gregset_t *gregs)
{
argc = MIN(argc, (uint_t)(uintptr_t)arglim);
#if defined(__i386) || defined(__amd64)
mdb_printf("%0?lr %a(", gregs->gregs[R_FP], pc);
#else
mdb_printf("%0?lr %a(", gregs->gregs[R_SP], pc);
#endif
if (argc != 0) {
mdb_printf("%lr", *argv++);
for (argc--; argc != 0; argc--)
mdb_printf(", %lr", *argv++);
}
mdb_printf(")\n");
return (0);
}
static int
pt_framer(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
const mdb_tgt_gregset_t *gregs)
{
if (pt_frameregs(arglim, pc, argc, argv, gregs, pc == PC_FAKE) == -1) {
/*
* Use verbose format if register format is not supported.
*/
return (pt_framev(arglim, pc, argc, argv, gregs));
}
return (0);
}
/*ARGSUSED*/
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)
{
void *arg = (void *)(uintptr_t)mdb.m_nargs;
mdb_tgt_t *t = mdb.m_target;
mdb_tgt_gregset_t gregs;
if (argc != 0) {
if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
return (DCMD_USAGE);
if (argv->a_type == MDB_TYPE_STRING)
arg = (void *)(uintptr_t)mdb_strtoull(argv->a_un.a_str);
else
arg = (void *)(uintptr_t)argv->a_un.a_val;
}
if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
mdb_warn("no process active\n");
return (DCMD_ERR);
}
/*
* In the universe of sparcv7, sparcv9, ia32, and amd64 this code can be
* common: <sys/procfs_isa.h> conveniently #defines R_FP to be the
* appropriate register we need to set in order to perform a stack
* traceback from a given frame address.
*/
if (flags & DCMD_ADDRSPEC) {
bzero(&gregs, sizeof (gregs));
gregs.gregs[R_FP] = addr;
#ifdef __sparc
gregs.gregs[R_I7] = saved_pc;
#endif /* __sparc */
} else if (PTL_GETREGS(t, PTL_TID(t), gregs.gregs) != 0) {
mdb_warn("failed to get current register set");
return (DCMD_ERR);
}
(void) mdb_tgt_stack_iter(t, &gregs, func, arg);
return (DCMD_OK);
}
static int
pt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
return (pt_stack_common(addr, flags, argc, argv, pt_frame, 0));
}
static int
pt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
return (pt_stack_common(addr, flags, argc, argv, pt_framev, 0));
}
static int
pt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
/*
* Force printing of first register window, by setting the
* saved pc (%i7) to PC_FAKE.
*/
return (pt_stack_common(addr, flags, argc, argv, pt_framer, PC_FAKE));
}
/*ARGSUSED*/
static int
pt_ignored(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
struct ps_prochandle *P = mdb.m_target->t_pshandle;
char buf[PRSIGBUFSZ];
if ((flags & DCMD_ADDRSPEC) || argc != 0)
return (DCMD_USAGE);
if (P == NULL) {
mdb_warn("no process is currently active\n");
return (DCMD_ERR);
}
mdb_printf("%s\n", proc_sigset2str(&Pstatus(P)->pr_sigtrace, " ",
FALSE, buf, sizeof (buf)));
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_lwpid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
struct ps_prochandle *P = mdb.m_target->t_pshandle;
if ((flags & DCMD_ADDRSPEC) || argc != 0)
return (DCMD_USAGE);
if (P == NULL) {
mdb_warn("no process is currently active\n");
return (DCMD_ERR);
}
mdb_printf("%d\n", Pstatus(P)->pr_lwp.pr_lwpid);
return (DCMD_OK);
}
static int
pt_print_lwpid(int *n, const lwpstatus_t *psp)
{
struct ps_prochandle *P = mdb.m_target->t_pshandle;
int nlwp = Pstatus(P)->pr_nlwp;
if (*n == nlwp - 2)
mdb_printf("%d and ", (int)psp->pr_lwpid);
else if (*n == nlwp - 1)
mdb_printf("%d are", (int)psp->pr_lwpid);
else
mdb_printf("%d, ", (int)psp->pr_lwpid);
(*n)++;
return (0);
}
/*ARGSUSED*/
static int
pt_lwpids(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
struct ps_prochandle *P = mdb.m_target->t_pshandle;
int n = 0;
if (P == NULL) {
mdb_warn("no process is currently active\n");
return (DCMD_ERR);
}
switch (Pstatus(P)->pr_nlwp) {
case 0:
mdb_printf("no lwps are");
break;
case 1:
mdb_printf("lwpid %d is the only lwp",
Pstatus(P)->pr_lwp.pr_lwpid);
break;
default:
mdb_printf("lwpids ");
(void) Plwp_iter(P, (proc_lwp_f *)pt_print_lwpid, &n);
}
switch (Pstate(P)) {
case PS_DEAD:
mdb_printf(" in core of process %d.\n", Pstatus(P)->pr_pid);
break;
case PS_IDLE:
mdb_printf(" in idle target.\n");
break;
default:
mdb_printf(" in process %d.\n", (int)Pstatus(P)->pr_pid);
break;
}
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_ignore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
pt_data_t *pt = mdb.m_target->t_data;
if (!(flags & DCMD_ADDRSPEC) || argc != 0)
return (DCMD_USAGE);
if (addr < 1 || addr > pt->p_maxsig) {
mdb_warn("invalid signal number -- 0t%lu\n", addr);
return (DCMD_ERR);
}
(void) mdb_tgt_vespec_iter(mdb.m_target, pt_ignore_sig, (void *)addr);
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_attach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
int state, perr;
if (!(flags & DCMD_ADDRSPEC) && argc == 0)
return (DCMD_USAGE);
if (((flags & DCMD_ADDRSPEC) && argc != 0) || argc > 1 ||
(argc != 0 && argv->a_type != MDB_TYPE_STRING))
return (DCMD_USAGE);
if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
mdb_warn("debugger is already attached to a %s\n",
(Pstate(t->t_pshandle) == PS_DEAD) ? "core" : "process");
return (DCMD_ERR);
}
if (pt->p_fio == NULL) {
mdb_warn("attach requires executable to be specified on "
"command-line (or use -p)\n");
return (DCMD_ERR);
}
if (flags & DCMD_ADDRSPEC)
t->t_pshandle = Pgrab((pid_t)addr, pt->p_gflags, &perr);
else
t->t_pshandle = proc_arg_grab(argv->a_un.a_str,
PR_ARG_ANY, pt->p_gflags, &perr);
if (t->t_pshandle == NULL) {
t->t_pshandle = pt->p_idlehandle;
mdb_warn("cannot attach: %s\n", Pgrab_error(perr));
return (DCMD_ERR);
}
state = Pstate(t->t_pshandle);
if (state != PS_DEAD && state != PS_IDLE) {
(void) Punsetflags(t->t_pshandle, PR_KLC);
(void) Psetflags(t->t_pshandle, PR_RLC);
pt_post_attach(t);
pt_activate_common(t);
}
(void) mdb_tgt_status(t, &t->t_status);
mdb_module_load_all(0);
return (DCMD_OK);
}
static int
pt_regstatus(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
if (t->t_pshandle != NULL) {
const pstatus_t *psp = Pstatus(t->t_pshandle);
int cursig = psp->pr_lwp.pr_cursig;
char signame[SIG2STR_MAX];
int state = Pstate(t->t_pshandle);
if (state != PS_DEAD && state != PS_IDLE)
mdb_printf("process id = %d\n", psp->pr_pid);
else
mdb_printf("no process\n");
if (cursig != 0 && sig2str(cursig, signame) == 0)
mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
}
return (pt_regs(addr, flags, argc, argv));
}
static void
pt_thread_name(mdb_tgt_t *t, mdb_tgt_tid_t tid, char *buf, size_t bufsize)
{
char name[THREAD_NAME_MAX];
buf[0] = '\0';
if (t->t_pshandle == NULL ||
Plwp_getname(t->t_pshandle, tid, name, sizeof (name)) != 0 ||
name[0] == '\0') {
(void) mdb_snprintf(buf, bufsize, "%lu", tid);
return;
}
(void) mdb_snprintf(buf, bufsize, "%lu [%s]", tid, name);
}
static int
pt_findstack(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
mdb_tgt_gregset_t gregs;
int showargs = 0;
int count;
uintptr_t pc, sp;
char name[128];
if (!(flags & DCMD_ADDRSPEC))
return (DCMD_USAGE);
count = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &showargs,
NULL);
argc -= count;
argv += count;
if (argc > 1 || (argc == 1 && argv->a_type != MDB_TYPE_STRING))
return (DCMD_USAGE);
if (PTL_GETREGS(t, tid, gregs.gregs) != 0) {
mdb_warn("failed to get register set for thread %p", tid);
return (DCMD_ERR);
}
pc = gregs.gregs[R_PC];
#if defined(__i386) || defined(__amd64)
sp = gregs.gregs[R_FP];
#else
sp = gregs.gregs[R_SP];
#endif
pt_thread_name(t, tid, name, sizeof (name));
mdb_printf("stack pointer for thread %s: %p\n", name, sp);
if (pc != 0)
mdb_printf("[ %0?lr %a() ]\n", sp, pc);
(void) mdb_inc_indent(2);
mdb_set_dot(sp);
if (argc == 1)
(void) mdb_eval(argv->a_un.a_str);
else if (showargs)
(void) mdb_eval("<.$C");
else
(void) mdb_eval("<.$C0");
(void) mdb_dec_indent(2);
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_gcore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
char *prefix = "core";
char *content_str = NULL;
core_content_t content = CC_CONTENT_DEFAULT;
size_t size;
char *fname;
pid_t pid;
if (flags & DCMD_ADDRSPEC)
return (DCMD_USAGE);
if (mdb_getopts(argc, argv,
'o', MDB_OPT_STR, &prefix,
'c', MDB_OPT_STR, &content_str, NULL) != argc)
return (DCMD_USAGE);
if (content_str != NULL &&
(proc_str2content(content_str, &content) != 0 ||
content == CC_CONTENT_INVALID)) {
mdb_warn("invalid content string '%s'\n", content_str);
return (DCMD_ERR);
}
if (t->t_pshandle == NULL) {
mdb_warn("no process active\n");
return (DCMD_ERR);
}
pid = Pstatus(t->t_pshandle)->pr_pid;
size = 1 + mdb_snprintf(NULL, 0, "%s.%d", prefix, (int)pid);
fname = mdb_alloc(size, UM_SLEEP | UM_GC);
(void) mdb_snprintf(fname, size, "%s.%d", prefix, (int)pid);
if (Pgcore(t->t_pshandle, fname, content) != 0) {
/*
* Short writes during dumping are specifically described by
* EBADE, just as ZFS uses this otherwise-unused code for
* checksum errors. Translate to and mdb errno.
*/
if (errno == EBADE)
(void) set_errno(EMDB_SHORTWRITE);
mdb_warn("couldn't dump core");
return (DCMD_ERR);
}
mdb_warn("%s dumped\n", fname);
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_kill(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
int state;
if ((flags & DCMD_ADDRSPEC) || argc != 0)
return (DCMD_USAGE);
if (t->t_pshandle != NULL &&
(state = Pstate(t->t_pshandle)) != PS_DEAD && state != PS_IDLE) {
mdb_warn("victim process PID %d forcibly terminated\n",
(int)Pstatus(t->t_pshandle)->pr_pid);
pt_pre_detach(t, TRUE);
pt_release_parents(t);
Prelease(t->t_pshandle, PRELEASE_KILL);
t->t_pshandle = pt->p_idlehandle;
(void) mdb_tgt_status(t, &t->t_status);
mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
} else
mdb_warn("no victim process is currently under control\n");
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_detach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
int rflags = pt->p_rflags;
if (argc != 0 && argv->a_type == MDB_TYPE_STRING &&
strcmp(argv->a_un.a_str, "-a") == 0) {
rflags = PRELEASE_HANG | PRELEASE_CLEAR;
argv++;
argc--;
}
if ((flags & DCMD_ADDRSPEC) || argc != 0)
return (DCMD_USAGE);
if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
mdb_warn("debugger is not currently attached to a process "
"or core file\n");
return (DCMD_ERR);
}
pt_pre_detach(t, TRUE);
pt_release_parents(t);
Prelease(t->t_pshandle, rflags);
t->t_pshandle = pt->p_idlehandle;
(void) mdb_tgt_status(t, &t->t_status);
mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
return (DCMD_OK);
}
static uintmax_t
reg_disc_get(const mdb_var_t *v)
{
mdb_tgt_t *t = MDB_NV_COOKIE(v);
mdb_tgt_tid_t tid = PTL_TID(t);
mdb_tgt_reg_t r = 0;
if (tid != (mdb_tgt_tid_t)-1L)
(void) mdb_tgt_getareg(t, tid, mdb_nv_get_name(v), &r);
return (r);
}
static void
reg_disc_set(mdb_var_t *v, uintmax_t r)
{
mdb_tgt_t *t = MDB_NV_COOKIE(v);
mdb_tgt_tid_t tid = PTL_TID(t);
if (tid != (mdb_tgt_tid_t)-1L && mdb_tgt_putareg(t, tid,
mdb_nv_get_name(v), r) == -1)
mdb_warn("failed to modify %%%s register", mdb_nv_get_name(v));
}
static void
pt_print_reason(const lwpstatus_t *psp)
{
char name[SIG2STR_MAX + 4]; /* enough for SIG+name+\0, syscall or flt */
const char *desc;
switch (psp->pr_why) {
case PR_REQUESTED:
mdb_printf("stopped by debugger");
break;
case PR_SIGNALLED:
mdb_printf("stopped on %s (%s)", proc_signame(psp->pr_what,
name, sizeof (name)), strsignal(psp->pr_what));
break;
case PR_SYSENTRY:
mdb_printf("stopped on entry to %s system call",
proc_sysname(psp->pr_what, name, sizeof (name)));
break;
case PR_SYSEXIT:
mdb_printf("stopped on exit from %s system call",
proc_sysname(psp->pr_what, name, sizeof (name)));
break;
case PR_JOBCONTROL:
mdb_printf("stopped by job control");
break;
case PR_FAULTED:
if (psp->pr_what == FLTBPT) {
mdb_printf("stopped on a breakpoint");
} else if (psp->pr_what == FLTWATCH) {
switch (psp->pr_info.si_code) {
case TRAP_RWATCH:
desc = "read";
break;
case TRAP_WWATCH:
desc = "write";
break;
case TRAP_XWATCH:
desc = "execute";
break;
default:
desc = "unknown";
}
mdb_printf("stopped %s a watchpoint (%s access to %p)",
psp->pr_info.si_trapafter ? "after" : "on",
desc, psp->pr_info.si_addr);
} else if (psp->pr_what == FLTTRACE) {
mdb_printf("stopped after a single-step");
} else {
mdb_printf("stopped on a %s fault",
proc_fltname(psp->pr_what, name, sizeof (name)));
}
break;
case PR_SUSPENDED:
case PR_CHECKPOINT:
mdb_printf("suspended by the kernel");
break;
default:
mdb_printf("stopped for unknown reason (%d/%d)",
psp->pr_why, psp->pr_what);
}
}
/*ARGSUSED*/
static int
pt_status_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
struct ps_prochandle *P = t->t_pshandle;
pt_data_t *pt = t->t_data;
if (P != NULL) {
const psinfo_t *pip = Ppsinfo(P);
const pstatus_t *psp = Pstatus(P);
int cursig = 0, bits = 0, coredump = 0;
int state;
GElf_Sym sym;
uintptr_t panicstr;
char *panicbuf = mdb_alloc(PANIC_BUFSIZE, UM_SLEEP);
const siginfo_t *sip = &(psp->pr_lwp.pr_info);
char execname[MAXPATHLEN], buf[BUFSIZ];
char signame[SIG2STR_MAX + 4]; /* enough for SIG+name+\0 */
mdb_tgt_spec_desc_t desc;
mdb_sespec_t *sep;
struct utsname uts;
prcred_t cred;
psinfo_t pi;
(void) strcpy(uts.nodename, "unknown machine");
(void) Puname(P, &uts);
if (pip != NULL) {
bcopy(pip, &pi, sizeof (psinfo_t));
proc_unctrl_psinfo(&pi);
} else
bzero(&pi, sizeof (psinfo_t));
bits = pi.pr_dmodel == PR_MODEL_ILP32 ? 32 : 64;
state = Pstate(P);
if (psp != NULL && state != PS_UNDEAD && state != PS_IDLE)
cursig = psp->pr_lwp.pr_cursig;
if (state == PS_DEAD && pip != NULL) {
mdb_printf("debugging core file of %s (%d-bit) "
"from %s\n", pi.pr_fname, bits, uts.nodename);
} else if (state == PS_DEAD) {
mdb_printf("debugging core file\n");
} else if (state == PS_IDLE) {
const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
mdb_printf("debugging %s file (%d-bit)\n",
ehp->e_type == ET_EXEC ? "executable" : "object",
ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
} else if (state == PS_UNDEAD && pi.pr_pid == 0) {
mdb_printf("debugging defunct process\n");
} else {
mdb_printf("debugging PID %d (%d-bit)\n",
pi.pr_pid, bits);
}
if (Pexecname(P, execname, sizeof (execname)) != NULL)
mdb_printf("file: %s\n", execname);
if (pip != NULL && state == PS_DEAD)
mdb_printf("initial argv: %s\n", pi.pr_psargs);
if (state != PS_UNDEAD && state != PS_IDLE) {
mdb_printf("threading model: ");
if (pt->p_ptl_ops == &proc_lwp_ops)
mdb_printf("raw lwps\n");
else
mdb_printf("native threads\n");
}
mdb_printf("status: ");
switch (state) {
case PS_RUN:
ASSERT(!(psp->pr_flags & PR_STOPPED));
mdb_printf("process is running");
if (psp->pr_flags & PR_DSTOP)
mdb_printf(", debugger stop directive pending");
mdb_printf("\n");
break;
case PS_STOP:
ASSERT(psp->pr_flags & PR_STOPPED);
pt_print_reason(&psp->pr_lwp);
if (psp->pr_flags & PR_DSTOP)
mdb_printf(", debugger stop directive pending");
if (psp->pr_flags & PR_ASLEEP)
mdb_printf(", sleeping in %s system call",
proc_sysname(psp->pr_lwp.pr_syscall,
signame, sizeof (signame)));
mdb_printf("\n");
for (sep = t->t_matched; sep != T_SE_END;
sep = sep->se_matched) {
mdb_printf("event: %s\n", sep->se_ops->se_info(
t, sep, mdb_list_next(&sep->se_velist),
&desc, buf, sizeof (buf)));
}
break;
case PS_LOST:
mdb_printf("debugger lost control of process\n");
break;
case PS_UNDEAD:
coredump = WIFSIGNALED(pi.pr_wstat) &&
WCOREDUMP(pi.pr_wstat);
/*FALLTHRU*/
case PS_DEAD:
if (cursig == 0 && WIFSIGNALED(pi.pr_wstat))
cursig = WTERMSIG(pi.pr_wstat);
/*
* We can only use pr_wstat == 0 as a test for gcore if
* an NT_PRCRED note is present; these features were
* added at the same time in Solaris 8.
*/
if (pi.pr_wstat == 0 && Pstate(P) == PS_DEAD &&
Pcred(P, &cred, 1) == 0) {
mdb_printf("process core file generated "
"with gcore(1)\n");
} else if (cursig != 0) {
mdb_printf("process terminated by %s (%s)",
proc_signame(cursig, signame,
sizeof (signame)), strsignal(cursig));
if (sip->si_signo != 0 && SI_FROMUSER(sip) &&
sip->si_pid != 0) {
mdb_printf(", pid=%d uid=%u",
(int)sip->si_pid, sip->si_uid);
if (sip->si_code != 0) {
mdb_printf(" code=%d",
sip->si_code);
}
} else {
switch (sip->si_signo) {
case SIGILL:
case SIGTRAP:
case SIGFPE:
case SIGSEGV:
case SIGBUS:
case SIGEMT:
mdb_printf(", addr=%p",
sip->si_addr);
default:
break;
}
}
if (coredump)
mdb_printf(" - core file dumped");
mdb_printf("\n");
} else {
mdb_printf("process terminated with exit "
"status %d\n", WEXITSTATUS(pi.pr_wstat));
}
if (Plookup_by_name(t->t_pshandle, "libc.so",
"panicstr", &sym) == 0 &&
Pread(t->t_pshandle, &panicstr, sizeof (panicstr),
sym.st_value) == sizeof (panicstr) &&
Pread_string(t->t_pshandle, panicbuf,
PANIC_BUFSIZE, panicstr) > 0) {
mdb_printf("panic message: %s",
panicbuf);
}
break;
case PS_IDLE:
mdb_printf("idle\n");
break;
default:
mdb_printf("unknown libproc Pstate: %d\n", Pstate(P));
}
mdb_free(panicbuf, PANIC_BUFSIZE);
} else if (pt->p_file != NULL) {
const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
mdb_printf("debugging %s file (%d-bit)\n",
ehp->e_type == ET_EXEC ? "executable" : "object",
ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
mdb_printf("executable file: %s\n", IOP_NAME(pt->p_fio));
mdb_printf("status: idle\n");
}
return (DCMD_OK);
}
static int
pt_tls(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
{
const char *name;
const char *object;
GElf_Sym sym;
mdb_syminfo_t si;
mdb_tgt_t *t = mdb.m_target;
if (!(flags & DCMD_ADDRSPEC) || argc > 1)
return (DCMD_USAGE);
if (argc == 0) {
psaddr_t b;
if (tlsbase(t, tid, PR_LMID_EVERY, MDB_TGT_OBJ_EXEC, &b) != 0) {
mdb_warn("failed to lookup tlsbase for %r", tid);
return (DCMD_ERR);
}
mdb_printf("%lr\n", b);
mdb_set_dot(b);
return (DCMD_OK);
}
name = argv[0].a_un.a_str;
object = MDB_TGT_OBJ_EVERY;
if (pt_lookup_by_name_thr(t, object, name, &sym, &si, tid) != 0) {
mdb_warn("failed to lookup %s", name);
return (DCMD_ABORT); /* avoid repeated failure */
}
if (GELF_ST_TYPE(sym.st_info) != STT_TLS && DCMD_HDRSPEC(flags))
mdb_warn("%s does not refer to thread local storage\n", name);
mdb_printf("%llr\n", sym.st_value);
mdb_set_dot(sym.st_value);
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_tmodel(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
const pt_ptl_ops_t *ptl_ops;
if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
return (DCMD_USAGE);
if (strcmp(argv->a_un.a_str, "thread") == 0)
ptl_ops = &proc_tdb_ops;
else if (strcmp(argv->a_un.a_str, "lwp") == 0)
ptl_ops = &proc_lwp_ops;
else
return (DCMD_USAGE);
if (t->t_pshandle != NULL && pt->p_ptl_ops != ptl_ops) {
PTL_DTOR(t);
pt->p_tdb_ops = NULL;
pt->p_ptl_ops = &proc_lwp_ops;
pt->p_ptl_hdl = NULL;
if (ptl_ops == &proc_tdb_ops) {
(void) Pobject_iter(t->t_pshandle, (proc_map_f *)
thr_check, t);
}
}
(void) mdb_tgt_status(t, &t->t_status);
return (DCMD_OK);
}
static const char *
env_match(const char *cmp, const char *nameval)
{
const char *loc;
size_t cmplen = strlen(cmp);
loc = strchr(nameval, '=');
if (loc != NULL && (loc - nameval) == cmplen &&
strncmp(nameval, cmp, cmplen) == 0) {
return (loc + 1);
}
return (NULL);
}
/*ARGSUSED*/
static int
print_env(void *data, struct ps_prochandle *P, uintptr_t addr,
const char *nameval)
{
const char *value;
if (nameval == NULL) {
mdb_printf("<0x%p>\n", addr);
} else {
if (data == NULL)
mdb_printf("%s\n", nameval);
else if ((value = env_match(data, nameval)) != NULL)
mdb_printf("%s\n", value);
}
return (0);
}
/*ARGSUSED*/
static int
pt_getenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
int i;
uint_t opt_t = 0;
mdb_var_t *v;
i = mdb_getopts(argc, argv,
't', MDB_OPT_SETBITS, TRUE, &opt_t, NULL);
argc -= i;
argv += i;
if ((flags & DCMD_ADDRSPEC) || argc > 1)
return (DCMD_USAGE);
if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
return (DCMD_USAGE);
if (opt_t && t->t_pshandle == NULL) {
mdb_warn("no process active\n");
return (DCMD_ERR);
}
if (opt_t && (Pstate(t->t_pshandle) == PS_IDLE ||
Pstate(t->t_pshandle) == PS_UNDEAD)) {
mdb_warn("-t option requires target to be running\n");
return (DCMD_ERR);
}
if (opt_t != 0) {
if (Penv_iter(t->t_pshandle, print_env,
argc == 0 ? NULL : (void *)argv->a_un.a_str) != 0)
return (DCMD_ERR);
} else if (argc == 1) {
if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) == NULL)
return (DCMD_ERR);
ASSERT(strchr(mdb_nv_get_cookie(v), '=') != NULL);
mdb_printf("%s\n", strchr(mdb_nv_get_cookie(v), '=') + 1);
} else {
mdb_nv_rewind(&pt->p_env);
while ((v = mdb_nv_advance(&pt->p_env)) != NULL)
mdb_printf("%s\n", mdb_nv_get_cookie(v));
}
return (DCMD_OK);
}
/*
* Function to set a variable in the internal environment, which is used when
* creating new processes. Note that it is possible that 'nameval' can refer to
* read-only memory, if mdb calls putenv() on an existing value before calling
* this function. While we should avoid this situation, this function is
* designed to be robust in the face of such changes.
*/
static void
pt_env_set(pt_data_t *pt, const char *nameval)
{
mdb_var_t *v;
char *equals, *val;
const char *name;
size_t len;
if ((equals = strchr(nameval, '=')) != NULL) {
val = strdup(nameval);
equals = val + (equals - nameval);
} else {
/*
* nameval doesn't contain an equals character. Convert this to
* be 'nameval='.
*/
len = strlen(nameval);
val = mdb_alloc(len + 2, UM_SLEEP);
(void) mdb_snprintf(val, len + 2, "%s=", nameval);
equals = val + len;
}
/* temporary truncate the string for lookup/insert */
*equals = '\0';
v = mdb_nv_lookup(&pt->p_env, val);
if (v != NULL) {
char *old = mdb_nv_get_cookie(v);
mdb_free(old, strlen(old) + 1);
name = mdb_nv_get_name(v);
} else {
/*
* The environment is created using MDB_NV_EXTNAME, so we must
* provide external storage for the variable names.
*/
name = strdup(val);
}
*equals = '=';
(void) mdb_nv_insert(&pt->p_env, name, NULL, (uintptr_t)val,
MDB_NV_EXTNAME);
if (equals)
*equals = '=';
}
/*
* Clears the internal environment.
*/
static void
pt_env_clear(pt_data_t *pt)
{
mdb_var_t *v;
char *val, *name;
mdb_nv_rewind(&pt->p_env);
while ((v = mdb_nv_advance(&pt->p_env)) != NULL) {
name = (char *)mdb_nv_get_name(v);
val = mdb_nv_get_cookie(v);
mdb_nv_remove(&pt->p_env, v);
mdb_free(name, strlen(name) + 1);
mdb_free(val, strlen(val) + 1);
}
}
/*ARGSUSED*/
static int
pt_setenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
char *nameval;
size_t len;
int alloc;
if ((flags & DCMD_ADDRSPEC) || argc == 0 || argc > 2)
return (DCMD_USAGE);
if ((argc > 0 && argv[0].a_type != MDB_TYPE_STRING) ||
(argc > 1 && argv[1].a_type != MDB_TYPE_STRING))
return (DCMD_USAGE);
if (t->t_pshandle == NULL) {
mdb_warn("no process active\n");
return (DCMD_ERR);
}
/*
* If the process is in some sort of running state, warn the user that
* changes won't immediately take effect.
*/
if (Pstate(t->t_pshandle) == PS_RUN ||
Pstate(t->t_pshandle) == PS_STOP) {
mdb_warn("warning: changes will not take effect until process"
" is restarted\n");
}
/*
* We allow two forms of operation. The first is the usual "name=value"
* parameter. We also allow the user to specify two arguments, where
* the first is the name of the variable, and the second is the value.
*/
alloc = 0;
if (argc == 1) {
nameval = (char *)argv->a_un.a_str;
} else {
len = strlen(argv[0].a_un.a_str) +
strlen(argv[1].a_un.a_str) + 2;
nameval = mdb_alloc(len, UM_SLEEP);
(void) mdb_snprintf(nameval, len, "%s=%s", argv[0].a_un.a_str,
argv[1].a_un.a_str);
alloc = 1;
}
pt_env_set(pt, nameval);
if (alloc)
mdb_free(nameval, strlen(nameval) + 1);
return (DCMD_OK);
}
/*ARGSUSED*/
static int
pt_unsetenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
mdb_tgt_t *t = mdb.m_target;
pt_data_t *pt = t->t_data;
mdb_var_t *v;
char *value, *name;
if ((flags & DCMD_ADDRSPEC) || argc > 1)
return (DCMD_USAGE);
if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
return (DCMD_USAGE);
if (t->t_pshandle == NULL) {
mdb_warn("no process active\n");
return (DCMD_ERR);
}
/*
* If the process is in some sort of running state, warn the user that
* changes won't immediately take effect.
*/
if (Pstate(t->t_pshandle) == PS_RUN ||
Pstate(t->t_pshandle) == PS_STOP) {
mdb_warn("warning: changes will not take effect until process"
" is restarted\n");
}
if (argc == 0) {
pt_env_clear(pt);
} else {
if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) != NULL) {
name = (char *)mdb_nv_get_name(v);
value = mdb_nv_get_cookie(v);
mdb_nv_remove(&pt->p_env, v);
mdb_free(name, strlen(name) + 1);
mdb_free(value, strlen(value) + 1);
}
}
return (DCMD_OK);
}
void
getenv_help(void)
{
mdb_printf("-t show current process environment"
" instead of initial environment.\n");
}
static const mdb_dcmd_t pt_dcmds[] = {
{ "$c", "?[cnt]", "print stack backtrace", pt_stack },
{ "$C", "?[cnt]", "print stack backtrace", pt_stackv },
{ "$i", NULL, "print signals that are ignored", pt_ignored },
{ "$l", NULL, "print the representative thread's lwp id", pt_lwpid },
{ "$L", NULL, "print list of the active lwp ids", pt_lwpids },
{ "$r", "?[-u]", "print general-purpose registers", pt_regs },
{ "$x", "?", "print floating point registers", pt_fpregs },
{ "$X", "?", "print floating point registers", pt_fpregs },
{ "$y", "?", "print floating point registers", pt_fpregs },
{ "$Y", "?", "print floating point registers", pt_fpregs },
{ "$?", "?", "print status and registers", pt_regstatus },
{ ":A", "?[core|pid]", "attach to process or core file", pt_attach },
{ ":i", ":", "ignore signal (delete all matching events)", pt_ignore },
{ ":k", NULL, "forcibly kill and release target", pt_kill },
{ ":R", "[-a]", "release the previously attached process", pt_detach },
{ "attach", "?[core|pid]",
"attach to process or core file", pt_attach },
{ "findstack", ":[-v]", "find user thread stack", pt_findstack },
{ "gcore", "[-o prefix] [-c content]",
"produce a core file for the attached process", pt_gcore },
{ "getenv", "[-t] [name]", "display an environment variable",
pt_getenv, getenv_help },
{ "kill", NULL, "forcibly kill and release target", pt_kill },
{ "release", "[-a]",
"release the previously attached process", pt_detach },
{ "regs", "?[-u]", "print general-purpose registers", pt_regs },
{ "fpregs", "?[-dqs]", "print floating point registers", pt_fpregs },
{ "setenv", "name=value", "set an environment variable", pt_setenv },
{ "stack", "?[cnt]", "print stack backtrace", pt_stack },
{ "stackregs", "?", "print stack backtrace and registers", pt_stackr },
{ "status", NULL, "print summary of current target", pt_status_dcmd },
{ "tls", ":symbol",
"lookup TLS data in the context of a given thread", pt_tls },
{ "tmodel", "{thread|lwp}", NULL, pt_tmodel },
{ "unsetenv", "[name]", "clear an environment variable", pt_unsetenv },
{ NULL }
};
static void
pt_thr_walk_fini(mdb_walk_state_t *wsp)
{
mdb_addrvec_destroy(wsp->walk_data);
mdb_free(wsp->walk_data, sizeof (mdb_addrvec_t));
}
static int
pt_thr_walk_init(mdb_walk_state_t *wsp)
{
wsp->walk_data = mdb_zalloc(sizeof (mdb_addrvec_t), UM_SLEEP);
mdb_addrvec_create(wsp->walk_data);
if (PTL_ITER(mdb.m_target, wsp->walk_data) == -1) {
mdb_warn("failed to iterate over threads");
pt_thr_walk_fini(wsp);
return (WALK_ERR);
}
return (WALK_NEXT);
}
static int
pt_thr_walk_step(mdb_walk_state_t *wsp)
{
if (mdb_addrvec_length(wsp->walk_data) != 0) {
return (wsp->walk_callback(mdb_addrvec_shift(wsp->walk_data),
NULL, wsp->walk_cbdata));
}
return (WALK_DONE);
}
static const mdb_walker_t pt_walkers[] = {
{ "thread", "walk list of valid thread identifiers",
pt_thr_walk_init, pt_thr_walk_step, pt_thr_walk_fini },
{ NULL }
};
static int
pt_agent_check(boolean_t *agent, const lwpstatus_t *psp)
{
if (psp->pr_flags & PR_AGENT)
*agent = B_TRUE;
return (0);
}
static void
pt_activate_common(mdb_tgt_t *t)
{
pt_data_t *pt = t->t_data;
boolean_t hasagent = B_FALSE;
GElf_Sym sym;
/*
* If we have a libproc handle and AT_BASE is set, the process or core
* is dynamically linked. We call Prd_agent() to force libproc to
* try to initialize librtld_db, and issue a warning if that fails.
*/
if (t->t_pshandle != NULL && Pgetauxval(t->t_pshandle,
AT_BASE) != -1L && Prd_agent(t->t_pshandle) == NULL) {
mdb_warn("warning: librtld_db failed to initialize; shared "
"library information will not be available\n");
}
if (t->t_pshandle != NULL) {
(void) Plwp_iter(t->t_pshandle,
(proc_lwp_f *)pt_agent_check, &hasagent);
}
if (hasagent) {
mdb_warn("agent lwp detected; forcing "
"lwp thread model (use ::tmodel to change)\n");
} else if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
/*
* If we have a libproc handle and we do not have an agent LWP,
* look for the correct thread debugging library. (If we have
* an agent LWP, we leave the model as the raw LWP model to
* allow the agent LWP to be visible to the debugger.)
*/
(void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
}
/*
* If there's a global object named '_mdb_abort_info', assuming we're
* debugging mdb itself and load the developer support module.
*/
if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, "_mdb_abort_info",
&sym, NULL) == 0 && GELF_ST_TYPE(sym.st_info) == STT_OBJECT) {
if (mdb_module_load("mdb_ds", MDB_MOD_SILENT) < 0)
mdb_warn("warning: failed to load developer support\n");
}
mdb_tgt_elf_export(pt->p_file);
}
static void
pt_activate(mdb_tgt_t *t)
{
static const mdb_nv_disc_t reg_disc = { reg_disc_set, reg_disc_get };
pt_data_t *pt = t->t_data;
struct utsname u1, u2;
mdb_var_t *v;
core_content_t content;
if (t->t_pshandle) {
mdb_prop_postmortem = (Pstate(t->t_pshandle) == PS_DEAD);
mdb_prop_kernel = FALSE;
} else
mdb_prop_kernel = mdb_prop_postmortem = FALSE;
mdb_prop_datamodel = MDB_TGT_MODEL_NATIVE;
/*
* If we're examining a core file that doesn't contain program text,
* and uname(2) doesn't match the NT_UTSNAME note recorded in the
* core file, issue a warning.
*/
if (mdb_prop_postmortem == TRUE &&
((content = Pcontent(t->t_pshandle)) == CC_CONTENT_INVALID ||
!(content & CC_CONTENT_TEXT)) &&
uname(&u1) >= 0 && Puname(t->t_pshandle, &u2) == 0 &&
(strcmp(u1.release, u2.release) != 0 ||
strcmp(u1.version, u2.version) != 0)) {
mdb_warn("warning: core file is from %s %s %s; shared text "
"mappings may not match installed libraries\n",
u2.sysname, u2.release, u2.version);
}
/*
* Perform the common initialization tasks -- these are shared with
* the pt_exec() and pt_run() subroutines.
*/
pt_activate_common(t);
(void) mdb_tgt_register_dcmds(t, &pt_dcmds[0], MDB_MOD_FORCE);
(void) mdb_tgt_register_walkers(t, &pt_walkers[0], MDB_MOD_FORCE);
/*
* Iterate through our register description list and export
* each register as a named variable.
*/
mdb_nv_rewind(&pt->p_regs);
while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
if (!(rd_flags & MDB_TGT_R_EXPORT))
continue; /* Don't export register as a variable */
(void) mdb_nv_insert(&mdb.m_nv, mdb_nv_get_name(v), &reg_disc,
(uintptr_t)t, MDB_NV_PERSIST);
}
}
static void
pt_deactivate(mdb_tgt_t *t)
{
pt_data_t *pt = t->t_data;
const mdb_dcmd_t *dcp;
const mdb_walker_t *wp;
mdb_var_t *v, *w;
mdb_nv_rewind(&pt->p_regs);
while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
if (!(rd_flags & MDB_TGT_R_EXPORT))
continue; /* Didn't export register as a variable */
if (w = mdb_nv_lookup(&mdb.m_nv, mdb_nv_get_name(v))) {
w->v_flags &= ~MDB_NV_PERSIST;
mdb_nv_remove(&mdb.m_nv, w);
}
}
for (wp = &pt_walkers[0]; wp->walk_name != NULL; wp++) {
if (mdb_module_remove_walker(t->t_module, wp->walk_name) == -1)
warn("failed to remove walk %s", wp->walk_name);
}
for (dcp = &pt_dcmds[0]; dcp->dc_name != NULL; dcp++) {
if (mdb_module_remove_dcmd(t->t_module, dcp->dc_name) == -1)
warn("failed to remove dcmd %s", dcp->dc_name);
}
mdb_prop_postmortem = FALSE;
mdb_prop_kernel = FALSE;
mdb_prop_datamodel = MDB_TGT_MODEL_UNKNOWN;
}
static void
pt_periodic(mdb_tgt_t *t)
{
pt_data_t *pt = t->t_data;
if (pt->p_rdstate == PT_RD_CONSIST) {
if (t->t_pshandle != NULL && Pstate(t->t_pshandle) < PS_LOST &&
!(mdb.m_flags & MDB_FL_NOMODS)) {
mdb_printf("%s: You've got symbols!\n", mdb.m_pname);
mdb_module_load_all(0);
}
pt->p_rdstate = PT_RD_NONE;
}
}
static void
pt_destroy(mdb_tgt_t *t)
{
pt_data_t *pt = t->t_data;
if (pt->p_idlehandle != NULL && pt->p_idlehandle != t->t_pshandle)
Prelease(pt->p_idlehandle, 0);
if (t->t_pshandle != NULL) {
PTL_DTOR(t);
pt_release_parents(t);
pt_pre_detach(t, TRUE);
Prelease(t->t_pshandle, pt->p_rflags);
}
mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
pt_close_aout(t);
if (pt->p_aout_fio != NULL)
mdb_io_rele(pt->p_aout_fio);
pt_env_clear(pt);
mdb_nv_destroy(&pt->p_env);
mdb_nv_destroy(&pt->p_regs);
mdb_free(pt, sizeof (pt_data_t));
}
/*ARGSUSED*/
static const char *
pt_name(mdb_tgt_t *t)
{
return ("proc");
}
static const char *
pt_platform(mdb_tgt_t *t)
{
pt_data_t *pt = t->t_data;
if (t->t_pshandle != NULL &&
Pplatform(t->t_pshandle, pt->p_platform, MAXNAMELEN) != NULL)
return (pt->p_platform);
return (mdb_conf_platform());
}
static int
pt_uname(mdb_tgt_t *t, struct utsname *utsp)
{
if (t->t_pshandle != NULL)
return (Puname(t->t_pshandle, utsp));
return (uname(utsp) >= 0 ? 0 : -1);
}
static int
pt_dmodel(mdb_tgt_t *t)
{
if (t->t_pshandle == NULL)
return (MDB_TGT_MODEL_NATIVE);
switch (Pstatus(t->t_pshandle)->pr_dmodel) {
case PR_MODEL_ILP32:
return (MDB_TGT_MODEL_ILP32);
case PR_MODEL_LP64:
return (MDB_TGT_MODEL_LP64);
}
return (MDB_TGT_MODEL_UNKNOWN);
}
static ssize_t
pt_vread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
{
ssize_t n;
/*
* If no handle is open yet, reads from virtual addresses are
* allowed to succeed but return zero-filled memory.
*/
if (t->t_pshandle == NULL) {
bzero(buf, nbytes);
return (nbytes);
}
if ((n = Pread(t->t_pshandle, buf, nbytes, addr)) <= 0)
return (set_errno(EMDB_NOMAP));
return (n);
}
static ssize_t
pt_vwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
{
ssize_t n;
/*
* If no handle is open yet, writes to virtual addresses are
* allowed to succeed but do not actually modify anything.
*/
if (t->t_pshandle == NULL)
return (nbytes);
n = Pwrite(t->t_pshandle, buf, nbytes, addr);
if (n == -1 && errno == EIO)
return (set_errno(EMDB_NOMAP));
return (n);
}
static ssize_t
pt_fread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
{
pt_data_t *pt = t->t_data;
if (pt->p_file != NULL) {
return (mdb_gelf_rw(pt->p_file, buf, nbytes, addr,
IOPF_READ(pt->p_fio), GIO_READ));
}
bzero(buf, nbytes);
return (nbytes);
}
static ssize_t
pt_fwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
{
pt_data_t *pt = t->t_data;
if (pt->p_file != NULL) {
return (mdb_gelf_rw(pt->p_file, (void *)buf, nbytes, addr,
IOPF_WRITE(pt->p_fio), GIO_WRITE));
}
return (nbytes);
}
static const char *
pt_resolve_lmid(const char *object, Lmid_t *lmidp)
{
Lmid_t lmid = PR_LMID_EVERY;
const char *p;
if (object == MDB_TGT_OBJ_EVERY || object == MDB_TGT_OBJ_EXEC)
lmid = LM_ID_BASE; /* restrict scope to a.out's link map */
else if (object != MDB_TGT_OBJ_RTLD && strncmp(object, "LM", 2) == 0 &&
(p = strchr(object, '`')) != NULL) {
object += 2; /* skip past initial "LM" prefix */
lmid = strntoul(object, (size_t)(p - object), mdb.m_radix);
object = p + 1; /* skip past link map specifier */
}
*lmidp = lmid;
return (object);
}
static int
tlsbase(mdb_tgt_t *t, mdb_tgt_tid_t tid, Lmid_t lmid, const char *object,
psaddr_t *basep)
{
pt_data_t *pt = t->t_data;
const rd_loadobj_t *loadobjp;
td_thrhandle_t th;
td_err_e err;
if (object == MDB_TGT_OBJ_EVERY)
return (set_errno(EINVAL));
if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE)
return (set_errno(EMDB_NOPROC));
if (pt->p_tdb_ops == NULL)
return (set_errno(EMDB_TDB));
err = pt->p_tdb_ops->td_ta_map_id2thr(pt->p_ptl_hdl, tid, &th);
if (err != TD_OK)
return (set_errno(tdb_to_errno(err)));
/*
* If this fails, rtld_db has failed to initialize properly.
*/
if ((loadobjp = Plmid_to_loadobj(t->t_pshandle, lmid, object)) == NULL)
return (set_errno(EMDB_NORTLD));
/*
* This will fail if the TLS block has not been allocated for the
* object that contains the TLS symbol in question.
*/
err = pt->p_tdb_ops->td_thr_tlsbase(&th, loadobjp->rl_tlsmodid, basep);
if (err != TD_OK)
return (set_errno(tdb_to_errno(err)));
return (0);
}
typedef struct {
mdb_tgt_t *pl_tgt;
const char *pl_name;
Lmid_t pl_lmid;
GElf_Sym *pl_symp;
mdb_syminfo_t *pl_sip;
mdb_tgt_tid_t pl_tid;
mdb_bool_t pl_found;
} pt_lookup_t;
/*ARGSUSED*/
static int
pt_lookup_cb(void *data, const prmap_t *pmp, const char *object)
{
pt_lookup_t *plp = data;
struct ps_prochandle *P = plp->pl_tgt->t_pshandle;
prsyminfo_t si;
GElf_Sym sym;
if (Pxlookup_by_name(P, plp->pl_lmid, object, plp->pl_name, &sym,
&si) != 0)
return (0);
/*
* If we encounter a match with SHN_UNDEF, keep looking for a
* better match. Return the first match with SHN_UNDEF set if no
* better match is found.
*/
if (sym.st_shndx == SHN_UNDEF) {
if (!plp->pl_found) {
plp->pl_found = TRUE;
*plp->pl_symp = sym;
plp->pl_sip->sym_table = si.prs_table;
plp->pl_sip->sym_id = si.prs_id;
}
return (0);
}
/*
* Note that if the symbol's st_shndx is SHN_UNDEF we don't have the
* TLS offset anyway, so adding in the tlsbase would be worthless.
*/
if (GELF_ST_TYPE(sym.st_info) == STT_TLS &&