1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26 /*
27 * Copyright 2018 Joyent, Inc.
28 * Copyright (c) 2014 by Delphix. All rights reserved.
29 * Copyright 2026 Oxide Computer Company
30 */
31
32 /*
33 * User Process Target
34 *
35 * The user process target is invoked when the -u or -p command-line options
36 * are used, or when an ELF executable file or ELF core file is specified on
37 * the command-line. This target is also selected by default when no target
38 * options are present. In this case, it defaults the executable name to
39 * "a.out". If no process or core file is currently attached, the target
40 * functions as a kind of virtual /dev/zero (in accordance with adb(1)
41 * semantics); reads from the virtual address space return zeroes and writes
42 * fail silently. The proc target itself is designed as a wrapper around the
43 * services provided by libproc.so: t->t_pshandle is set to the struct
44 * ps_prochandle pointer returned as a handle by libproc. The target also
45 * opens the executable file itself using the MDB GElf services, for
46 * interpreting the .symtab and .dynsym if no libproc handle has been
47 * initialized, and for handling i/o to and from the object file. Currently,
48 * the only ISA-dependent portions of the proc target are the $r and ::fpregs
49 * dcmds, the callbacks for t_next() and t_step_out(), and the list of named
50 * registers; these are linked in from the proc_isadep.c file for each ISA and
51 * called from the common code in this file.
52 *
53 * The user process target implements complete user process control using the
54 * facilities provided by libproc.so. The MDB execution control model and
55 * an overview of software event management is described in mdb_target.c. The
56 * proc target implements breakpoints by replacing the instruction of interest
57 * with a trap instruction, and then restoring the original instruction to step
58 * over the breakpoint. The idea of replacing program text with instructions
59 * that transfer control to the debugger dates back as far as 1951 [1]. When
60 * the target stops, we replace each breakpoint with the original instruction
61 * as part of the disarm operation. This means that no special processing is
62 * required for t_vread() because the instrumented instructions will never be
63 * seen by the debugger once the target stops. Some debuggers have improved
64 * start/stop performance by leaving breakpoint traps in place and then
65 * handling a read from a breakpoint address as a special case. Although this
66 * improves efficiency for a source-level debugger, it runs somewhat contrary
67 * to the philosophy of the low-level debugger. Since we remove the
68 * instructions, users can apply other external debugging tools to the process
69 * once it has stopped (e.g. the proc(1) tools) and not be misled by MDB
70 * instrumentation. The tracing of faults, signals, system calls, and
71 * watchpoints and general process inspection is implemented directly using
72 * the mechanisms provided by /proc, as described originally in [2] and [3].
73 *
74 * References
75 *
76 * [1] S. Gill, "The Diagnosis Of Mistakes In Programmes on the EDSAC",
77 * Proceedings of the Royal Society Series A Mathematical and Physical
78 * Sciences, Cambridge University Press, 206(1087), May 1951, pp. 538-554.
79 *
80 * [2] T.J. Killian, "Processes as Files", Proceedings of the USENIX Association
81 * Summer Conference, Salt Lake City, June 1984, pp. 203-207.
82 *
83 * [3] Roger Faulkner and Ron Gomes, "The Process File System and Process
84 * Model in UNIX System V", Proceedings of the USENIX Association
85 * Winter Conference, Dallas, January 1991, pp. 243-252.
86 */
87
88 #include <mdb/mdb_proc.h>
89 #include <mdb/mdb_disasm.h>
90 #include <mdb/mdb_signal.h>
91 #include <mdb/mdb_stack.h>
92 #include <mdb/mdb_string.h>
93 #include <mdb/mdb_module.h>
94 #include <mdb/mdb_debug.h>
95 #include <mdb/mdb_conf.h>
96 #include <mdb/mdb_err.h>
97 #include <mdb/mdb_types.h>
98 #include <mdb/mdb_isautil.h>
99
100 #include <mdb/mdb.h>
101
102 #include <sys/utsname.h>
103 #include <sys/wait.h>
104 #include <sys/stat.h>
105 #include <termio.h>
106 #include <signal.h>
107 #include <stdio_ext.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #include <ctype.h>
111
112 #define PC_FAKE -1UL /* illegal pc value unequal 0 */
113 #define PANIC_BUFSIZE 1024
114
115 static const char PT_EXEC_PATH[] = "a.out"; /* Default executable */
116 static const char PT_CORE_PATH[] = "core"; /* Default core file */
117
118 static const pt_ptl_ops_t proc_lwp_ops;
119 static const pt_ptl_ops_t proc_tdb_ops;
120 static const mdb_se_ops_t proc_brkpt_ops;
121 static const mdb_se_ops_t proc_wapt_ops;
122 static const mdb_se_ops_t proc_sysexit_ops;
123
124 static int pt_setrun(mdb_tgt_t *, mdb_tgt_status_t *, int);
125 static void pt_activate_common(mdb_tgt_t *);
126 static mdb_tgt_vespec_f pt_ignore_sig;
127 static mdb_tgt_se_f pt_fork;
128 static mdb_tgt_se_f pt_exec;
129
130 static int pt_lookup_by_name_thr(mdb_tgt_t *, const char *,
131 const char *, GElf_Sym *, mdb_syminfo_t *, mdb_tgt_tid_t);
132 static int tlsbase(mdb_tgt_t *, mdb_tgt_tid_t, Lmid_t, const char *,
133 psaddr_t *);
134
135 /*
136 * When debugging postmortem, we don't resolve names as we may very well not
137 * be on a system on which those names resolve.
138 */
139 #define PT_LIBPROC_RESOLVE(P) \
140 (!(mdb.m_flags & MDB_FL_LMRAW) && Pstate(P) != PS_DEAD)
141
142 /*
143 * The Perror_printf() function interposes on the default, empty libproc
144 * definition. It will be called to report additional information on complex
145 * errors, such as a corrupt core file. We just pass the args to vwarn.
146 */
147 /*ARGSUSED*/
148 void
Perror_printf(struct ps_prochandle * P,const char * format,...)149 Perror_printf(struct ps_prochandle *P, const char *format, ...)
150 {
151 va_list alist;
152
153 va_start(alist, format);
154 vwarn(format, alist);
155 va_end(alist);
156 }
157
158 /*
159 * Open the specified i/o backend as the a.out executable file, and attempt to
160 * load its standard and dynamic symbol tables. Note that if mdb_gelf_create
161 * succeeds, io is assigned to p_fio and is automatically held by gelf_create.
162 */
163 static mdb_gelf_file_t *
pt_open_aout(mdb_tgt_t * t,mdb_io_t * io)164 pt_open_aout(mdb_tgt_t *t, mdb_io_t *io)
165 {
166 pt_data_t *pt = t->t_data;
167 GElf_Sym s1, s2;
168
169 if ((pt->p_file = mdb_gelf_create(io, ET_NONE, GF_FILE)) == NULL)
170 return (NULL);
171
172 pt->p_symtab = mdb_gelf_symtab_create_file(pt->p_file,
173 SHT_SYMTAB, MDB_TGT_SYMTAB);
174 pt->p_dynsym = mdb_gelf_symtab_create_file(pt->p_file,
175 SHT_DYNSYM, MDB_TGT_DYNSYM);
176
177 /*
178 * If we've got an _start symbol with a zero size, prime the private
179 * symbol table with a copy of _start with its size set to the distance
180 * between _mcount and _start. We do this because DevPro has shipped
181 * the Intel crt1.o without proper .size directives for years, which
182 * precludes proper identification of _start in stack traces.
183 */
184 if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_start", &s1,
185 NULL) == 0 && s1.st_size == 0 &&
186 GELF_ST_TYPE(s1.st_info) == STT_FUNC) {
187 if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, "_mcount",
188 &s2, NULL) == 0 && GELF_ST_TYPE(s2.st_info) == STT_FUNC) {
189 s1.st_size = s2.st_value - s1.st_value;
190 mdb_gelf_symtab_insert(mdb.m_prsym, "_start", &s1);
191 }
192 }
193
194 pt->p_fio = io;
195 return (pt->p_file);
196 }
197
198 /*
199 * Destroy the symbol tables and GElf file object associated with p_fio. Note
200 * that we do not need to explicitly free p_fio: its reference count is
201 * automatically decremented by mdb_gelf_destroy, which will free it if needed.
202 */
203 static void
pt_close_aout(mdb_tgt_t * t)204 pt_close_aout(mdb_tgt_t *t)
205 {
206 pt_data_t *pt = t->t_data;
207
208 if (pt->p_symtab != NULL) {
209 mdb_gelf_symtab_destroy(pt->p_symtab);
210 pt->p_symtab = NULL;
211 }
212
213 if (pt->p_dynsym != NULL) {
214 mdb_gelf_symtab_destroy(pt->p_dynsym);
215 pt->p_dynsym = NULL;
216 }
217
218 if (pt->p_file != NULL) {
219 mdb_gelf_destroy(pt->p_file);
220 pt->p_file = NULL;
221 }
222
223 mdb_gelf_symtab_delete(mdb.m_prsym, "_start", NULL);
224 pt->p_fio = NULL;
225 }
226
227 typedef struct tdb_mapping {
228 const char *tm_thr_lib;
229 const char *tm_db_dir;
230 const char *tm_db_name;
231 } tdb_mapping_t;
232
233 static const tdb_mapping_t tdb_map[] = {
234 { "/lwp/amd64/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
235 { "/lwp/sparcv9/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
236 { "/lwp/libthread.so", "/usr/lib/lwp/", "libthread_db.so" },
237 { "/libthread.so", "/lib/", "libthread_db.so" },
238 { "/libc_hwcap", "/lib/", "libc_db.so" },
239 { "/libc.so", "/lib/", "libc_db.so" }
240 };
241
242 /*
243 * Pobject_iter callback that we use to search for the presence of libthread in
244 * order to load the corresponding libthread_db support. We derive the
245 * libthread_db path dynamically based on the libthread path. If libthread is
246 * found, this function returns 1 (and thus Pobject_iter aborts and returns 1)
247 * regardless of whether it was successful in loading the libthread_db support.
248 * If we iterate over all objects and no libthread is found, 0 is returned.
249 * Since libthread_db support was then merged into libc_db, we load either
250 * libc_db or libthread_db, depending on which library we see first.
251 */
252 /*ARGSUSED*/
253 static int
thr_check(mdb_tgt_t * t,const prmap_t * pmp,const char * name)254 thr_check(mdb_tgt_t *t, const prmap_t *pmp, const char *name)
255 {
256 pt_data_t *pt = t->t_data;
257 const mdb_tdb_ops_t *ops;
258 char *p;
259
260 char path[MAXPATHLEN];
261
262 int libn;
263
264 if (name == NULL)
265 return (0); /* no rtld_db object name; keep going */
266
267 for (libn = 0; libn < sizeof (tdb_map) / sizeof (tdb_map[0]); libn++) {
268 if ((p = strstr(name, tdb_map[libn].tm_thr_lib)) != NULL)
269 break;
270 }
271
272 if (p == NULL)
273 return (0); /* no match; keep going */
274
275 path[0] = '\0';
276 (void) strlcat(path, mdb.m_root, sizeof (path));
277 (void) strlcat(path, tdb_map[libn].tm_db_dir, sizeof (path));
278 #if !defined(_ILP32)
279 (void) strlcat(path, "64/", sizeof (path));
280 #endif /* !_ILP32 */
281 (void) strlcat(path, tdb_map[libn].tm_db_name, sizeof (path));
282
283 /* Append the trailing library version number. */
284 (void) strlcat(path, strrchr(name, '.'), sizeof (path));
285
286 if ((ops = mdb_tdb_load(path)) == NULL) {
287 if (libn != 0 || errno != ENOENT)
288 warn("failed to load %s", path);
289 goto err;
290 }
291
292 if (ops == pt->p_tdb_ops)
293 return (1); /* no changes needed */
294
295 PTL_DTOR(t);
296 pt->p_tdb_ops = ops;
297 pt->p_ptl_ops = &proc_tdb_ops;
298 pt->p_ptl_hdl = NULL;
299
300 if (PTL_CTOR(t) == -1) {
301 warn("failed to initialize %s", path);
302 goto err;
303 }
304
305 mdb_dprintf(MDB_DBG_TGT, "loaded %s for debugging %s\n", path, name);
306 (void) mdb_tgt_status(t, &t->t_status);
307 return (1);
308 err:
309 PTL_DTOR(t);
310 pt->p_tdb_ops = NULL;
311 pt->p_ptl_ops = &proc_lwp_ops;
312 pt->p_ptl_hdl = NULL;
313
314 if (libn != 0 || errno != ENOENT) {
315 warn("warning: debugger will only be able to "
316 "examine raw LWPs\n");
317 }
318
319 (void) mdb_tgt_status(t, &t->t_status);
320 return (1);
321 }
322
323 /*
324 * Whenever the link map is consistent following an add or delete event, we ask
325 * libproc to update its mappings, check to see if we need to load libthread_db,
326 * and then update breakpoints which have been mapped or unmapped.
327 */
328 /*ARGSUSED*/
329 static void
pt_rtld_event(mdb_tgt_t * t,int vid,void * private)330 pt_rtld_event(mdb_tgt_t *t, int vid, void *private)
331 {
332 struct ps_prochandle *P = t->t_pshandle;
333 pt_data_t *pt = t->t_data;
334 rd_event_msg_t rdm;
335 int docontinue = 1;
336
337 if (rd_event_getmsg(pt->p_rtld, &rdm) == RD_OK) {
338
339 mdb_dprintf(MDB_DBG_TGT, "rtld event type 0x%x state 0x%x\n",
340 rdm.type, rdm.u.state);
341
342 if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_CONSISTENT) {
343 mdb_sespec_t *sep, *nsep = mdb_list_next(&t->t_active);
344 pt_brkpt_t *ptb;
345
346 Pupdate_maps(P);
347
348 if (Pobject_iter(P, (proc_map_f *)thr_check, t) == 0 &&
349 pt->p_ptl_ops != &proc_lwp_ops) {
350 mdb_dprintf(MDB_DBG_TGT, "unloading thread_db "
351 "support after dlclose\n");
352 PTL_DTOR(t);
353 pt->p_tdb_ops = NULL;
354 pt->p_ptl_ops = &proc_lwp_ops;
355 pt->p_ptl_hdl = NULL;
356 (void) mdb_tgt_status(t, &t->t_status);
357 }
358
359 for (sep = nsep; sep != NULL; sep = nsep) {
360 nsep = mdb_list_next(sep);
361 ptb = sep->se_data;
362
363 if (sep->se_ops == &proc_brkpt_ops &&
364 Paddr_to_map(P, ptb->ptb_addr) == NULL)
365 mdb_tgt_sespec_idle_one(t, sep,
366 EMDB_NOMAP);
367 }
368
369 if (!mdb_tgt_sespec_activate_all(t) &&
370 (mdb.m_flags & MDB_FL_BPTNOSYMSTOP) &&
371 pt->p_rtld_finished) {
372 /*
373 * We weren't able to activate the breakpoints.
374 * If so requested, we'll return without
375 * calling continue, thus throwing the user into
376 * the debugger.
377 */
378 docontinue = 0;
379 }
380
381 if (pt->p_rdstate == PT_RD_ADD)
382 pt->p_rdstate = PT_RD_CONSIST;
383 }
384
385 if (rdm.type == RD_PREINIT)
386 (void) mdb_tgt_sespec_activate_all(t);
387
388 if (rdm.type == RD_POSTINIT) {
389 pt->p_rtld_finished = TRUE;
390 if (!mdb_tgt_sespec_activate_all(t) &&
391 (mdb.m_flags & MDB_FL_BPTNOSYMSTOP)) {
392 /*
393 * Now that rtld has been initialized, we
394 * should be able to initialize all deferred
395 * breakpoints. If we can't, don't let the
396 * target continue.
397 */
398 docontinue = 0;
399 }
400 }
401
402 if (rdm.type == RD_DLACTIVITY && rdm.u.state == RD_ADD &&
403 pt->p_rtld_finished)
404 pt->p_rdstate = MAX(pt->p_rdstate, PT_RD_ADD);
405 }
406
407 if (docontinue)
408 (void) mdb_tgt_continue(t, NULL);
409 }
410
411 static void
pt_post_attach(mdb_tgt_t * t)412 pt_post_attach(mdb_tgt_t *t)
413 {
414 struct ps_prochandle *P = t->t_pshandle;
415 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
416 pt_data_t *pt = t->t_data;
417 int hflag = MDB_TGT_SPEC_HIDDEN;
418
419 mdb_dprintf(MDB_DBG_TGT, "attach pr_flags=0x%x pr_why=%d pr_what=%d\n",
420 psp->pr_flags, psp->pr_why, psp->pr_what);
421
422 /*
423 * When we grab a process, the initial setting of p_rtld_finished
424 * should be false if the process was just created by exec; otherwise
425 * we permit unscoped references to resolve because we do not know how
426 * far the process has proceeded through linker initialization.
427 */
428 if ((psp->pr_flags & PR_ISTOP) && psp->pr_why == PR_SYSEXIT &&
429 psp->pr_errno == 0 && psp->pr_what == SYS_execve) {
430 if (mdb.m_target == NULL) {
431 warn("target performed exec of %s\n",
432 IOP_NAME(pt->p_fio));
433 }
434 pt->p_rtld_finished = FALSE;
435 } else
436 pt->p_rtld_finished = TRUE;
437
438 /*
439 * When we grab a process, if it is stopped by job control and part of
440 * the same session (i.e. same controlling tty), set MDB_FL_JOBCTL so
441 * we will know to bring it to the foreground when we continue it.
442 */
443 if (mdb.m_term != NULL && (psp->pr_flags & PR_STOPPED) &&
444 psp->pr_why == PR_JOBCONTROL && getsid(0) == Pstatus(P)->pr_sid)
445 mdb.m_flags |= MDB_FL_JOBCTL;
446
447 /*
448 * When we grab control of a live process, set F_RDWR so that the
449 * target layer permits writes to the target's address space.
450 */
451 t->t_flags |= MDB_TGT_F_RDWR;
452
453 (void) Pfault(P, FLTBPT, TRUE); /* always trace breakpoints */
454 (void) Pfault(P, FLTWATCH, TRUE); /* always trace watchpoints */
455 (void) Pfault(P, FLTTRACE, TRUE); /* always trace single-step */
456
457 (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */
458 (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */
459 (void) Psetflags(P, PR_FORK); /* inherit tracing on fork */
460
461 /*
462 * Install event specifiers to track fork, exec and spawn activities:
463 */
464 (void) mdb_tgt_add_sysexit(t, SYS_vfork, hflag, pt_fork, NULL);
465 (void) mdb_tgt_add_sysexit(t, SYS_forksys, hflag, pt_fork, NULL);
466 (void) mdb_tgt_add_sysexit(t, SYS_spawn, hflag, pt_fork, NULL);
467 (void) mdb_tgt_add_sysexit(t, SYS_execve, hflag, pt_exec, NULL);
468
469 /*
470 * Attempt to instantiate the librtld_db agent and set breakpoints
471 * to track rtld activity. We will legitimately fail to instantiate
472 * the rtld_db agent if the target is statically linked.
473 */
474 if (pt->p_rtld == NULL && (pt->p_rtld = Prd_agent(P)) != NULL) {
475 rd_notify_t rdn;
476 rd_err_e err;
477
478 if ((err = rd_event_enable(pt->p_rtld, TRUE)) != RD_OK) {
479 warn("failed to enable rtld_db event tracing: %s\n",
480 rd_errstr(err));
481 goto out;
482 }
483
484 if ((err = rd_event_addr(pt->p_rtld, RD_PREINIT,
485 &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
486 (void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
487 hflag, pt_rtld_event, NULL);
488 } else {
489 warn("failed to install rtld_db preinit tracing: %s\n",
490 rd_errstr(err));
491 }
492
493 if ((err = rd_event_addr(pt->p_rtld, RD_POSTINIT,
494 &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
495 (void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
496 hflag, pt_rtld_event, NULL);
497 } else {
498 warn("failed to install rtld_db postinit tracing: %s\n",
499 rd_errstr(err));
500 }
501
502 if ((err = rd_event_addr(pt->p_rtld, RD_DLACTIVITY,
503 &rdn)) == RD_OK && rdn.type == RD_NOTIFY_BPT) {
504 (void) mdb_tgt_add_vbrkpt(t, rdn.u.bptaddr,
505 hflag, pt_rtld_event, NULL);
506 } else {
507 warn("failed to install rtld_db activity tracing: %s\n",
508 rd_errstr(err));
509 }
510 }
511 out:
512 Pupdate_maps(P);
513 Psync(P);
514
515 /*
516 * If librtld_db failed to initialize due to an error or because we are
517 * debugging a statically linked executable, allow unscoped references.
518 */
519 if (pt->p_rtld == NULL)
520 pt->p_rtld_finished = TRUE;
521
522 (void) mdb_tgt_sespec_activate_all(t);
523 }
524
525 /*ARGSUSED*/
526 static int
pt_vespec_delete(mdb_tgt_t * t,void * private,int id,void * data)527 pt_vespec_delete(mdb_tgt_t *t, void *private, int id, void *data)
528 {
529 if (id < 0) {
530 ASSERT(data == NULL); /* we don't use any ve_data */
531 (void) mdb_tgt_vespec_delete(t, id);
532 }
533 return (0);
534 }
535
536 static void
pt_pre_detach(mdb_tgt_t * t,int clear_matched)537 pt_pre_detach(mdb_tgt_t *t, int clear_matched)
538 {
539 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
540 pt_data_t *pt = t->t_data;
541 long cmd = 0;
542
543 /*
544 * If we are about to release the process and it is stopped on a traced
545 * SIGINT, breakpoint fault, single-step fault, or watchpoint, make
546 * sure to clear this event prior to releasing the process so that it
547 * does not subsequently reissue the fault and die from SIGTRAP.
548 */
549 if (psp->pr_flags & PR_ISTOP) {
550 if (psp->pr_why == PR_FAULTED && (psp->pr_what == FLTBPT ||
551 psp->pr_what == FLTTRACE || psp->pr_what == FLTWATCH))
552 cmd = PCCFAULT;
553 else if (psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
554 cmd = PCCSIG;
555
556 if (cmd != 0)
557 (void) write(Pctlfd(t->t_pshandle), &cmd, sizeof (cmd));
558 }
559
560 if (Pstate(t->t_pshandle) == PS_UNDEAD)
561 (void) waitpid(Pstatus(t->t_pshandle)->pr_pid, NULL, WNOHANG);
562
563 (void) mdb_tgt_vespec_iter(t, pt_vespec_delete, NULL);
564 mdb_tgt_sespec_idle_all(t, EMDB_NOPROC, clear_matched);
565
566 if (pt->p_fio != pt->p_aout_fio) {
567 pt_close_aout(t);
568 (void) pt_open_aout(t, pt->p_aout_fio);
569 }
570
571 PTL_DTOR(t);
572 pt->p_tdb_ops = NULL;
573 pt->p_ptl_ops = &proc_lwp_ops;
574 pt->p_ptl_hdl = NULL;
575
576 pt->p_rtld = NULL;
577 pt->p_signal = 0;
578 pt->p_rtld_finished = FALSE;
579 pt->p_rdstate = PT_RD_NONE;
580 }
581
582 static void
pt_release_parents(mdb_tgt_t * t)583 pt_release_parents(mdb_tgt_t *t)
584 {
585 struct ps_prochandle *P = t->t_pshandle;
586 pt_data_t *pt = t->t_data;
587
588 mdb_sespec_t *sep;
589 pt_vforkp_t *vfp;
590
591 while ((vfp = mdb_list_next(&pt->p_vforkp)) != NULL) {
592 mdb_dprintf(MDB_DBG_TGT, "releasing vfork parent %d\n",
593 (int)Pstatus(vfp->p_pshandle)->pr_pid);
594
595 /*
596 * To release vfork parents, we must also wipe out any armed
597 * events in the parent by switching t_pshandle and calling
598 * se_disarm(). Do not change states or lose the matched list.
599 */
600 t->t_pshandle = vfp->p_pshandle;
601
602 for (sep = mdb_list_next(&t->t_active); sep != NULL;
603 sep = mdb_list_next(sep)) {
604 if (sep->se_state == MDB_TGT_SPEC_ARMED)
605 (void) sep->se_ops->se_disarm(t, sep);
606 }
607
608 t->t_pshandle = P;
609
610 Prelease(vfp->p_pshandle, PRELEASE_CLEAR);
611 mdb_list_delete(&pt->p_vforkp, vfp);
612 mdb_free(vfp, sizeof (pt_vforkp_t));
613 }
614 }
615
616 /*ARGSUSED*/
617 static void
pt_fork(mdb_tgt_t * t,int vid,void * private)618 pt_fork(mdb_tgt_t *t, int vid, void *private)
619 {
620 struct ps_prochandle *P = t->t_pshandle;
621 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
622 pt_data_t *pt = t->t_data;
623 mdb_sespec_t *sep;
624
625 int follow_parent = mdb.m_forkmode != MDB_FM_CHILD;
626 int is_vfork = (psp->pr_what == SYS_vfork ||
627 (psp->pr_what == SYS_forksys && psp->pr_sysarg[0] == 2));
628 int is_spawn = (psp->pr_what == SYS_spawn);
629
630 struct ps_prochandle *C;
631 const lwpstatus_t *csp;
632 char sysname[32];
633 int gcode;
634 char c;
635
636 mdb_dprintf(MDB_DBG_TGT, "parent %s: errno=%d rv1=%ld rv2=%ld\n",
637 proc_sysname(psp->pr_what, sysname, sizeof (sysname)),
638 psp->pr_errno, psp->pr_rval1, psp->pr_rval2);
639
640 if (psp->pr_errno != 0) {
641 (void) mdb_tgt_continue(t, NULL);
642 return; /* fork failed */
643 }
644
645 /*
646 * If forkmode is ASK and stdout is a terminal, then ask the user to
647 * explicitly set the fork behavior for this particular fork.
648 */
649 if (mdb.m_forkmode == MDB_FM_ASK && mdb.m_term != NULL) {
650 mdb_iob_printf(mdb.m_err, "%s: %s detected: follow (p)arent "
651 "or (c)hild? ", mdb.m_pname, sysname);
652 mdb_iob_flush(mdb.m_err);
653
654 while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
655 if (c == 'P' || c == 'p') {
656 mdb_iob_printf(mdb.m_err, "%c\n", c);
657 follow_parent = TRUE;
658 break;
659 } else if (c == 'C' || c == 'c') {
660 mdb_iob_printf(mdb.m_err, "%c\n", c);
661 follow_parent = FALSE;
662 break;
663 }
664 }
665 }
666
667 /*
668 * The parent is now stopped on exit from its fork call. We must now
669 * grab the child on its return from fork in order to manipulate it.
670 */
671 if ((C = Pgrab(psp->pr_rval1, PGRAB_RETAIN, &gcode)) == NULL) {
672 warn("failed to grab forked child process %ld: %s\n",
673 psp->pr_rval1, Pgrab_error(gcode));
674 return; /* just stop if we failed to grab the child */
675 }
676
677 /*
678 * We may have grabbed the child and stopped it prematurely before it
679 * stopped on exit from fork. If so, wait up to 1 sec for it to settle.
680 */
681 if (Pstatus(C)->pr_lwp.pr_why != PR_SYSEXIT)
682 (void) Pwait(C, MILLISEC);
683
684 csp = &Pstatus(C)->pr_lwp;
685
686 /*
687 * A spawned child never returns from a fork; its first observable
688 * stop is on exit from the exec of its new program.
689 */
690 if (csp->pr_why != PR_SYSEXIT ||
691 (is_spawn ? csp->pr_what != SYS_execve :
692 (csp->pr_what != SYS_vfork && csp->pr_what != SYS_forksys))) {
693 warn("%s child process %ld did not stop on exit from "
694 "%s as expected\n", is_spawn ? "spawned" : "forked",
695 psp->pr_rval1, is_spawn ? "exec" : "fork");
696 }
697
698 warn("target %s child process %ld (debugger following %s)\n",
699 is_spawn ? "spawned" : "forked", psp->pr_rval1,
700 follow_parent ? "parent" : "child");
701
702 (void) Punsetflags(C, PR_ASYNC); /* require synchronous mode */
703 (void) Psetflags(C, PR_BPTADJ); /* always adjust eip on x86 */
704 (void) Prd_agent(C); /* initialize librtld_db */
705
706 /*
707 * At the time pt_fork() is called, the target event engine has already
708 * disarmed the specifiers on the active list, clearing out events in
709 * the parent process. However, this means that events that change
710 * the address space (e.g. breakpoints) have not been effectively
711 * disarmed in the child since its address space reflects the state of
712 * the process at the time of fork when events were armed. We must
713 * therefore handle this as a special case and re-invoke the disarm
714 * callback of each active specifier to clean out the child process.
715 * A spawned child has a brand new address space in which our events
716 * were never armed, so there is nothing to clean out there.
717 */
718 if (!is_vfork && !is_spawn) {
719 for (t->t_pshandle = C, sep = mdb_list_next(&t->t_active);
720 sep != NULL; sep = mdb_list_next(sep)) {
721 if (sep->se_state == MDB_TGT_SPEC_ACTIVE)
722 (void) sep->se_ops->se_disarm(t, sep);
723 }
724
725 t->t_pshandle = P; /* restore pshandle to parent */
726 }
727
728 /*
729 * If we're following the parent process, we need to temporarily change
730 * t_pshandle to refer to the child handle C so that we can clear out
731 * all the events in the child prior to releasing it below. If we are
732 * tracing a vfork, we also need to explicitly wait for the child to
733 * exec, exit, or die before we can reset and continue the parent. We
734 * avoid having to deal with the vfork child forking again by clearing
735 * PR_FORK and setting PR_RLC; if it does fork it will effectively be
736 * released from our control and we will continue following the parent.
737 */
738 if (follow_parent) {
739 if (is_vfork) {
740 mdb_tgt_status_t status;
741
742 ASSERT(psp->pr_flags & PR_VFORKP);
743 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
744 t->t_pshandle = C;
745
746 (void) Psysexit(C, SYS_execve, TRUE);
747
748 (void) Punsetflags(C, PR_FORK | PR_KLC);
749 (void) Psetflags(C, PR_RLC);
750
751 do {
752 if (pt_setrun(t, &status, 0) == -1 ||
753 status.st_state == MDB_TGT_UNDEAD ||
754 status.st_state == MDB_TGT_LOST)
755 break; /* failure or process died */
756
757 } while (csp->pr_why != PR_SYSEXIT ||
758 csp->pr_errno != 0 || csp->pr_what != SYS_execve);
759 } else if (!is_spawn) {
760 t->t_pshandle = C;
761 }
762 }
763
764 /*
765 * If we are following the child, destroy any active libthread_db
766 * handle before we release the parent process.
767 */
768 if (!follow_parent) {
769 PTL_DTOR(t);
770 pt->p_tdb_ops = NULL;
771 pt->p_ptl_ops = &proc_lwp_ops;
772 pt->p_ptl_hdl = NULL;
773 }
774
775 /*
776 * Idle all events to make sure the address space and tracing flags are
777 * restored, and then release the process we are not tracing. If we
778 * are following the child of a vfork, we push the parent's pshandle
779 * on to a list of vfork parents to be released when we exec or exit.
780 */
781 if (is_vfork && !follow_parent) {
782 pt_vforkp_t *vfp = mdb_alloc(sizeof (pt_vforkp_t), UM_SLEEP);
783
784 ASSERT(psp->pr_flags & PR_VFORKP);
785 vfp->p_pshandle = P;
786 mdb_list_append(&pt->p_vforkp, vfp);
787 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
788
789 } else if (is_spawn && follow_parent) {
790 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
791 Prelease(C, PRELEASE_CLEAR);
792 } else {
793 mdb_tgt_sespec_idle_all(t, EBUSY, FALSE);
794 Prelease(t->t_pshandle, PRELEASE_CLEAR);
795 if (!follow_parent)
796 pt_release_parents(t);
797 }
798
799 /*
800 * Now that all the hard stuff is done, switch t_pshandle back to the
801 * process we are following and reset our events to the ACTIVE state.
802 * If we are following the child, reset the libthread_db handle as well
803 * as the rtld agent.
804 */
805 if (follow_parent) {
806 t->t_pshandle = P;
807 } else {
808 t->t_pshandle = C;
809 if (!is_spawn) {
810 pt->p_rtld = Prd_agent(C);
811 (void) Pobject_iter(t->t_pshandle,
812 (proc_map_f *)thr_check, t);
813 }
814 }
815
816 if (is_spawn && !follow_parent) {
817 /*
818 * The child of a spawn is running a different program to the
819 * parent and is stopped on exit from the exec of it. The
820 * pending stop will be matched by the execve specifier and
821 * delivered to pt_exec(), which discards the parent's symbol
822 * state, loads that of the new program and re-initialises
823 * our events as if the debuggee itself had exec'd.
824 * We still need to activate the system call specifiers, whose
825 * arming does not touch the child's memory.
826 */
827 (void) mdb_tgt_sespec_activate_matching(t, &proc_sysexit_ops);
828 } else {
829 (void) mdb_tgt_sespec_activate_all(t);
830 }
831 (void) mdb_tgt_continue(t, NULL);
832 }
833
834 /*ARGSUSED*/
835 static void
pt_exec(mdb_tgt_t * t,int vid,void * private)836 pt_exec(mdb_tgt_t *t, int vid, void *private)
837 {
838 struct ps_prochandle *P = t->t_pshandle;
839 const pstatus_t *psp = Pstatus(P);
840 pt_data_t *pt = t->t_data;
841 int follow_exec = mdb.m_execmode == MDB_EM_FOLLOW;
842 pid_t pid = psp->pr_pid;
843
844 char execname[MAXPATHLEN];
845 mdb_sespec_t *sep, *nsep;
846 mdb_io_t *io;
847 char c;
848
849 mdb_dprintf(MDB_DBG_TGT, "exit from %s: errno=%d\n", proc_sysname(
850 psp->pr_lwp.pr_what, execname, sizeof (execname)),
851 psp->pr_lwp.pr_errno);
852
853 if (psp->pr_lwp.pr_errno != 0) {
854 (void) mdb_tgt_continue(t, NULL);
855 return; /* exec failed */
856 }
857
858 /*
859 * If execmode is ASK and stdout is a terminal, then ask the user to
860 * explicitly set the exec behavior for this particular exec. If
861 * Pstate() still shows PS_LOST, we are being called from pt_setrun()
862 * directly and therefore we must resume the terminal since it is still
863 * in the suspended state as far as tgt_continue() is concerned.
864 */
865 if (mdb.m_execmode == MDB_EM_ASK && mdb.m_term != NULL) {
866 if (Pstate(P) == PS_LOST)
867 IOP_RESUME(mdb.m_term);
868
869 mdb_iob_printf(mdb.m_err, "%s: %s detected: (f)ollow new "
870 "program or (s)top? ", mdb.m_pname, execname);
871 mdb_iob_flush(mdb.m_err);
872
873 while (IOP_READ(mdb.m_term, &c, sizeof (c)) == sizeof (c)) {
874 if (c == 'F' || c == 'f') {
875 mdb_iob_printf(mdb.m_err, "%c\n", c);
876 follow_exec = TRUE;
877 break;
878 } else if (c == 'S' || c == 's') {
879 mdb_iob_printf(mdb.m_err, "%c\n", c);
880 follow_exec = FALSE;
881 break;
882 }
883 }
884
885 if (Pstate(P) == PS_LOST)
886 IOP_SUSPEND(mdb.m_term);
887 }
888
889 pt_release_parents(t); /* release any waiting vfork parents */
890 pt_pre_detach(t, FALSE); /* remove our breakpoints and idle events */
891 Preset_maps(P); /* libproc must delete mappings and symtabs */
892 pt_close_aout(t); /* free pt symbol tables and GElf file data */
893
894 /*
895 * If we lost control of the process across the exec and are not able
896 * to reopen it, we have no choice but to clear the matched event list
897 * and wait for the user to quit or otherwise release the process.
898 */
899 if (Pstate(P) == PS_LOST && Preopen(P) == -1) {
900 int error = errno;
901
902 warn("lost control of PID %d due to exec of %s executable\n",
903 (int)pid, error == EOVERFLOW ? "64-bit" : "set-id");
904
905 for (sep = t->t_matched; sep != T_SE_END; sep = nsep) {
906 nsep = sep->se_matched;
907 sep->se_matched = NULL;
908 mdb_tgt_sespec_rele(t, sep);
909 }
910
911 if (error != EOVERFLOW)
912 return; /* just stop if we exec'd a set-id executable */
913 }
914
915 if (Pstate(P) != PS_LOST) {
916 if (Pexecname(P, execname, sizeof (execname)) == NULL) {
917 (void) mdb_iob_snprintf(execname, sizeof (execname),
918 "/proc/%d/object/a.out", (int)pid);
919 }
920
921 if (follow_exec == FALSE || psp->pr_dmodel == PR_MODEL_NATIVE)
922 warn("target performed exec of %s\n", execname);
923
924 io = mdb_fdio_create_path(NULL, execname, pt->p_oflags, 0);
925 if (io == NULL) {
926 warn("failed to open %s", execname);
927 warn("a.out symbol tables will not be available\n");
928 } else if (pt_open_aout(t, io) == NULL) {
929 (void) mdb_dis_select(pt_disasm(NULL));
930 mdb_io_destroy(io);
931 } else
932 (void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
933 }
934
935 /*
936 * We reset our libthread_db state here, but deliberately do NOT call
937 * PTL_DTOR because we do not want to call libthread_db's td_ta_delete.
938 * This interface is hopelessly broken in that it writes to the process
939 * address space (which we do not want it to do after an exec) and it
940 * doesn't bother deallocating any of its storage anyway.
941 */
942 pt->p_tdb_ops = NULL;
943 pt->p_ptl_ops = &proc_lwp_ops;
944 pt->p_ptl_hdl = NULL;
945
946 if (follow_exec && psp->pr_dmodel != PR_MODEL_NATIVE) {
947 const char *argv[3];
948 char *state, *env;
949 char pidarg[16];
950 size_t envlen;
951
952 if (realpath(getexecname(), execname) == NULL) {
953 warn("cannot follow PID %d -- failed to resolve "
954 "debugger pathname for re-exec", (int)pid);
955 return;
956 }
957
958 warn("restarting debugger to follow PID %d ...\n", (int)pid);
959 mdb_dprintf(MDB_DBG_TGT, "re-exec'ing %s\n", execname);
960
961 (void) mdb_snprintf(pidarg, sizeof (pidarg), "-p%d", (int)pid);
962
963 state = mdb_get_config();
964 envlen = strlen(MDB_CONFIG_ENV_VAR) + 1 + strlen(state) + 1;
965 env = mdb_alloc(envlen, UM_SLEEP);
966 (void) snprintf(env, envlen,
967 "%s=%s", MDB_CONFIG_ENV_VAR, state);
968
969 (void) putenv(env);
970
971 argv[0] = mdb.m_pname;
972 argv[1] = pidarg;
973 argv[2] = NULL;
974
975 if (mdb.m_term != NULL)
976 IOP_SUSPEND(mdb.m_term);
977
978 Prelease(P, PRELEASE_CLEAR | PRELEASE_HANG);
979 (void) execv(execname, (char *const *)argv);
980 warn("failed to re-exec debugger");
981
982 if (mdb.m_term != NULL)
983 IOP_RESUME(mdb.m_term);
984
985 t->t_pshandle = pt->p_idlehandle;
986 return;
987 }
988
989 pt_post_attach(t); /* install tracing flags and activate events */
990 pt_activate_common(t); /* initialize librtld_db and libthread_db */
991
992 if (psp->pr_dmodel != PR_MODEL_NATIVE && mdb.m_term != NULL) {
993 warn("loadable dcmds will not operate on non-native %d-bit "
994 "data model\n", psp->pr_dmodel == PR_MODEL_ILP32 ? 32 : 64);
995 warn("use ::release -a and then run mdb -p %d to restart "
996 "debugger\n", (int)pid);
997 }
998
999 if (follow_exec)
1000 (void) mdb_tgt_continue(t, NULL);
1001 }
1002
1003 static int
pt_setflags(mdb_tgt_t * t,int flags)1004 pt_setflags(mdb_tgt_t *t, int flags)
1005 {
1006 pt_data_t *pt = t->t_data;
1007
1008 if ((flags ^ t->t_flags) & MDB_TGT_F_RDWR) {
1009 int mode = (flags & MDB_TGT_F_RDWR) ? O_RDWR : O_RDONLY;
1010 mdb_io_t *io;
1011
1012 if (pt->p_fio == NULL)
1013 return (set_errno(EMDB_NOEXEC));
1014
1015 io = mdb_fdio_create_path(NULL, IOP_NAME(pt->p_fio), mode, 0);
1016
1017 if (io == NULL)
1018 return (-1); /* errno is set for us */
1019
1020 t->t_flags = (t->t_flags & ~MDB_TGT_F_RDWR) |
1021 (flags & MDB_TGT_F_RDWR);
1022
1023 pt->p_fio = mdb_io_hold(io);
1024 mdb_io_rele(pt->p_file->gf_io);
1025 pt->p_file->gf_io = pt->p_fio;
1026 }
1027
1028 if (flags & MDB_TGT_F_FORCE) {
1029 t->t_flags |= MDB_TGT_F_FORCE;
1030 pt->p_gflags |= PGRAB_FORCE;
1031 }
1032
1033 return (0);
1034 }
1035
1036 static int
pt_frame(void * argp,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)1037 pt_frame(void *argp, uintptr_t pc, uint_t argc, const long *argv,
1038 const mdb_tgt_gregset_t *gregs)
1039 {
1040 mdb_stack_frame_hdl_t *hdl = argp;
1041 uint64_t bp;
1042
1043 #if defined(__i386) || defined(__amd64)
1044 bp = gregs->gregs[R_FP];
1045 #else
1046 bp = gregs->gregs[R_SP];
1047 #endif
1048
1049 mdb_stack_frame(hdl, pc, bp, argc, argv);
1050
1051 return (0);
1052 }
1053
1054 static int
pt_framer(void * argp,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)1055 pt_framer(void *argp, uintptr_t pc, uint_t argc, const long *argv,
1056 const mdb_tgt_gregset_t *gregs)
1057 {
1058 mdb_stack_frame_hdl_t *hdl = argp;
1059
1060 uint_t arglim = mdb_stack_frame_arglim(hdl);
1061
1062 if (pt_frameregs((void *)(uintptr_t)arglim, pc,
1063 argc, argv, gregs, pc == PC_FAKE) == -1) {
1064 /*
1065 * Use verbose format if register format is not supported.
1066 */
1067 mdb_stack_frame_flags_set(hdl, MSF_VERBOSE);
1068 return (pt_frame((void *)hdl, pc, argc, argv, gregs));
1069 }
1070
1071 return (0);
1072 }
1073
1074 static int
pt_stack_common(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv,mdb_stack_frame_flags_t sflags,mdb_tgt_stack_f * func,prgreg_t saved_pc)1075 pt_stack_common(uintptr_t addr, uint_t flags, int argc,
1076 const mdb_arg_t *argv, mdb_stack_frame_flags_t sflags,
1077 mdb_tgt_stack_f *func, prgreg_t saved_pc)
1078 {
1079 mdb_tgt_t *t = mdb.m_target;
1080 mdb_tgt_gregset_t gregs;
1081 mdb_stack_frame_hdl_t *hdl;
1082 uint_t arglim = mdb.m_nargs;
1083 int i;
1084
1085 i = mdb_getopts(argc, argv,
1086 'n', MDB_OPT_SETBITS, MSF_ADDR, &sflags,
1087 's', MDB_OPT_SETBITS, MSF_SIZES, &sflags,
1088 't', MDB_OPT_SETBITS, MSF_TYPES, &sflags,
1089 'v', MDB_OPT_SETBITS, MSF_VERBOSE, &sflags,
1090 NULL);
1091
1092 argc -= i;
1093 argv += i;
1094
1095 if (argc != 0) {
1096 if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
1097 return (DCMD_USAGE);
1098
1099 arglim = mdb_argtoull(argv);
1100 }
1101
1102 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
1103 mdb_warn("no process active\n");
1104 return (DCMD_ERR);
1105 }
1106
1107 if ((hdl = mdb_stack_frame_init(t, arglim, sflags)) == NULL) {
1108 mdb_warn("failed to init stack frame\n");
1109 return (DCMD_ERR);
1110 }
1111
1112 /*
1113 * In the universe of sparcv7, sparcv9, ia32, and amd64 this code can be
1114 * common: <sys/procfs_isa.h> conveniently #defines R_FP to be the
1115 * appropriate register we need to set in order to perform a stack
1116 * traceback from a given frame address.
1117 */
1118 if (flags & DCMD_ADDRSPEC) {
1119 bzero(&gregs, sizeof (gregs));
1120 gregs.gregs[R_FP] = addr;
1121 #ifdef __sparc
1122 gregs.gregs[R_I7] = saved_pc;
1123 #endif /* __sparc */
1124 } else if (PTL_GETREGS(t, PTL_TID(t), gregs.gregs) != 0) {
1125 mdb_warn("failed to get current register set");
1126 return (DCMD_ERR);
1127 }
1128
1129 (void) mdb_tgt_stack_iter(t, &gregs, func, (void *)hdl);
1130 return (DCMD_OK);
1131 }
1132
1133 static int
pt_stack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1134 pt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1135 {
1136 return (pt_stack_common(addr, flags, argc, argv, 0, pt_frame, 0));
1137 }
1138
1139 static int
pt_stackv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1140 pt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1141 {
1142 return (pt_stack_common(addr, flags, argc, argv, MSF_VERBOSE,
1143 pt_frame, 0));
1144 }
1145
1146 static int
pt_stackr(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1147 pt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1148 {
1149 /*
1150 * Force printing of first register window, by setting the
1151 * saved pc (%i7) to PC_FAKE.
1152 */
1153 return (pt_stack_common(addr, flags, argc, argv, 0,
1154 pt_framer, PC_FAKE));
1155 }
1156
1157 /*ARGSUSED*/
1158 static int
pt_ignored(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1159 pt_ignored(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1160 {
1161 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1162 char buf[PRSIGBUFSZ];
1163
1164 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1165 return (DCMD_USAGE);
1166
1167 if (P == NULL) {
1168 mdb_warn("no process is currently active\n");
1169 return (DCMD_ERR);
1170 }
1171
1172 mdb_printf("%s\n", proc_sigset2str(&Pstatus(P)->pr_sigtrace, " ",
1173 FALSE, buf, sizeof (buf)));
1174
1175 return (DCMD_OK);
1176 }
1177
1178 /*ARGSUSED*/
1179 static int
pt_lwpid(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1180 pt_lwpid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1181 {
1182 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1183
1184 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1185 return (DCMD_USAGE);
1186
1187 if (P == NULL) {
1188 mdb_warn("no process is currently active\n");
1189 return (DCMD_ERR);
1190 }
1191
1192 mdb_printf("%d\n", Pstatus(P)->pr_lwp.pr_lwpid);
1193 return (DCMD_OK);
1194 }
1195
1196 static int
pt_print_lwpid(int * n,const lwpstatus_t * psp)1197 pt_print_lwpid(int *n, const lwpstatus_t *psp)
1198 {
1199 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1200 int nlwp = Pstatus(P)->pr_nlwp;
1201
1202 if (*n == nlwp - 2)
1203 mdb_printf("%d and ", (int)psp->pr_lwpid);
1204 else if (*n == nlwp - 1)
1205 mdb_printf("%d are", (int)psp->pr_lwpid);
1206 else
1207 mdb_printf("%d, ", (int)psp->pr_lwpid);
1208
1209 (*n)++;
1210 return (0);
1211 }
1212
1213 /*ARGSUSED*/
1214 static int
pt_lwpids(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1215 pt_lwpids(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1216 {
1217 struct ps_prochandle *P = mdb.m_target->t_pshandle;
1218 int n = 0;
1219
1220 if (P == NULL) {
1221 mdb_warn("no process is currently active\n");
1222 return (DCMD_ERR);
1223 }
1224
1225 switch (Pstatus(P)->pr_nlwp) {
1226 case 0:
1227 mdb_printf("no lwps are");
1228 break;
1229 case 1:
1230 mdb_printf("lwpid %d is the only lwp",
1231 Pstatus(P)->pr_lwp.pr_lwpid);
1232 break;
1233 default:
1234 mdb_printf("lwpids ");
1235 (void) Plwp_iter(P, (proc_lwp_f *)pt_print_lwpid, &n);
1236 }
1237
1238 switch (Pstate(P)) {
1239 case PS_DEAD:
1240 mdb_printf(" in core of process %d.\n", Pstatus(P)->pr_pid);
1241 break;
1242 case PS_IDLE:
1243 mdb_printf(" in idle target.\n");
1244 break;
1245 default:
1246 mdb_printf(" in process %d.\n", (int)Pstatus(P)->pr_pid);
1247 break;
1248 }
1249
1250 return (DCMD_OK);
1251 }
1252
1253 /*ARGSUSED*/
1254 static int
pt_ignore(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1255 pt_ignore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1256 {
1257 pt_data_t *pt = mdb.m_target->t_data;
1258
1259 if (!(flags & DCMD_ADDRSPEC) || argc != 0)
1260 return (DCMD_USAGE);
1261
1262 if (addr < 1 || addr > pt->p_maxsig) {
1263 mdb_warn("invalid signal number -- 0t%lu\n", addr);
1264 return (DCMD_ERR);
1265 }
1266
1267 (void) mdb_tgt_vespec_iter(mdb.m_target, pt_ignore_sig, (void *)addr);
1268 return (DCMD_OK);
1269 }
1270
1271 /*ARGSUSED*/
1272 static int
pt_attach(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1273 pt_attach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1274 {
1275 mdb_tgt_t *t = mdb.m_target;
1276 pt_data_t *pt = t->t_data;
1277 int state, perr;
1278
1279 if (!(flags & DCMD_ADDRSPEC) && argc == 0)
1280 return (DCMD_USAGE);
1281
1282 if (((flags & DCMD_ADDRSPEC) && argc != 0) || argc > 1 ||
1283 (argc != 0 && argv->a_type != MDB_TYPE_STRING))
1284 return (DCMD_USAGE);
1285
1286 if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
1287 mdb_warn("debugger is already attached to a %s\n",
1288 (Pstate(t->t_pshandle) == PS_DEAD) ? "core" : "process");
1289 return (DCMD_ERR);
1290 }
1291
1292 if (pt->p_fio == NULL) {
1293 mdb_warn("attach requires executable to be specified on "
1294 "command-line (or use -p)\n");
1295 return (DCMD_ERR);
1296 }
1297
1298 if (flags & DCMD_ADDRSPEC)
1299 t->t_pshandle = Pgrab((pid_t)addr, pt->p_gflags, &perr);
1300 else
1301 t->t_pshandle = proc_arg_grab(argv->a_un.a_str,
1302 PR_ARG_ANY, pt->p_gflags, &perr);
1303
1304 if (t->t_pshandle == NULL) {
1305 t->t_pshandle = pt->p_idlehandle;
1306 mdb_warn("cannot attach: %s\n", Pgrab_error(perr));
1307 return (DCMD_ERR);
1308 }
1309
1310 state = Pstate(t->t_pshandle);
1311 if (state != PS_DEAD && state != PS_IDLE) {
1312 (void) Punsetflags(t->t_pshandle, PR_KLC);
1313 (void) Psetflags(t->t_pshandle, PR_RLC);
1314 pt_post_attach(t);
1315 pt_activate_common(t);
1316 }
1317
1318 (void) mdb_tgt_status(t, &t->t_status);
1319 mdb_module_load_all(0);
1320 return (DCMD_OK);
1321 }
1322
1323 static int
pt_regstatus(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1324 pt_regstatus(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1325 {
1326 mdb_tgt_t *t = mdb.m_target;
1327
1328 if (t->t_pshandle != NULL) {
1329 const pstatus_t *psp = Pstatus(t->t_pshandle);
1330 int cursig = psp->pr_lwp.pr_cursig;
1331 char signame[SIG2STR_MAX];
1332 int state = Pstate(t->t_pshandle);
1333
1334 if (state != PS_DEAD && state != PS_IDLE)
1335 mdb_printf("process id = %d\n", psp->pr_pid);
1336 else
1337 mdb_printf("no process\n");
1338
1339 if (cursig != 0 && sig2str(cursig, signame) == 0)
1340 mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
1341 }
1342
1343 return (pt_regs(addr, flags, argc, argv));
1344 }
1345
1346 static int
pt_thread_name(mdb_tgt_t * t,mdb_tgt_tid_t tid,char * buf,size_t bufsize)1347 pt_thread_name(mdb_tgt_t *t, mdb_tgt_tid_t tid, char *buf, size_t bufsize)
1348 {
1349 char name[THREAD_NAME_MAX];
1350
1351 buf[0] = '\0';
1352
1353 if (t->t_pshandle == NULL ||
1354 Plwp_getname(t->t_pshandle, tid, name, sizeof (name)) != 0 ||
1355 name[0] == '\0') {
1356 if (mdb_snprintf(buf, bufsize, "%lu", tid) > bufsize) {
1357 return (set_errno(EMDB_NAME2BIG));
1358 }
1359
1360 return (0);
1361 }
1362
1363 if (mdb_snprintf(buf, bufsize, "%lu [%s]", tid, name) > bufsize) {
1364 return (set_errno(EMDB_NAME2BIG));
1365 }
1366
1367 return (0);
1368 }
1369
1370 static int
pt_findstack(uintptr_t tid,uint_t flags,int argc,const mdb_arg_t * argv)1371 pt_findstack(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
1372 {
1373 mdb_tgt_t *t = mdb.m_target;
1374 mdb_tgt_gregset_t gregs;
1375 boolean_t showargs = B_FALSE;
1376 boolean_t types = B_FALSE;
1377 boolean_t sizes = B_FALSE;
1378 boolean_t addrs = B_FALSE;
1379 int count;
1380 uintptr_t pc, sp;
1381 char buf[128];
1382
1383 if (!(flags & DCMD_ADDRSPEC))
1384 return (DCMD_USAGE);
1385
1386 count = mdb_getopts(argc, argv,
1387 'n', MDB_OPT_SETBITS, TRUE, &addrs,
1388 's', MDB_OPT_SETBITS, TRUE, &sizes,
1389 't', MDB_OPT_SETBITS, TRUE, &types,
1390 'v', MDB_OPT_SETBITS, TRUE, &showargs,
1391 NULL);
1392 argc -= count;
1393 argv += count;
1394
1395 if (argc > 1 || (argc == 1 && argv->a_type != MDB_TYPE_STRING))
1396 return (DCMD_USAGE);
1397
1398 if (PTL_GETREGS(t, tid, gregs.gregs) != 0) {
1399 mdb_warn("failed to get register set for thread %p", tid);
1400 return (DCMD_ERR);
1401 }
1402
1403 pc = gregs.gregs[R_PC];
1404 #if defined(__i386) || defined(__amd64)
1405 sp = gregs.gregs[R_FP];
1406 #else
1407 sp = gregs.gregs[R_SP];
1408 #endif
1409
1410 (void) pt_thread_name(t, tid, buf, sizeof (buf));
1411
1412 mdb_printf("stack pointer for thread %s: %p\n", buf, sp);
1413 if (pc != 0)
1414 mdb_printf("[ %0?lr %a() ]\n", sp, pc);
1415
1416 (void) mdb_inc_indent(2);
1417 mdb_set_dot(sp);
1418
1419 if (argc == 1) {
1420 (void) mdb_eval(argv->a_un.a_str);
1421 } else {
1422 (void) mdb_snprintf(buf, sizeof (buf),
1423 "<.$C%s%s%s%s",
1424 addrs ? " -n" : "",
1425 sizes ? " -s" : "",
1426 types ? " -t" : "",
1427 showargs ? "" : " 0");
1428 (void) mdb_eval(buf);
1429 }
1430
1431 (void) mdb_dec_indent(2);
1432 return (DCMD_OK);
1433 }
1434
1435 /*ARGSUSED*/
1436 static int
pt_gcore(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1437 pt_gcore(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1438 {
1439 mdb_tgt_t *t = mdb.m_target;
1440 char *prefix = "core";
1441 char *content_str = NULL;
1442 core_content_t content = CC_CONTENT_DEFAULT;
1443 size_t size;
1444 char *fname;
1445 pid_t pid;
1446
1447 if (flags & DCMD_ADDRSPEC)
1448 return (DCMD_USAGE);
1449
1450 if (mdb_getopts(argc, argv,
1451 'o', MDB_OPT_STR, &prefix,
1452 'c', MDB_OPT_STR, &content_str, NULL) != argc)
1453 return (DCMD_USAGE);
1454
1455 if (content_str != NULL &&
1456 (proc_str2content(content_str, &content) != 0 ||
1457 content == CC_CONTENT_INVALID)) {
1458 mdb_warn("invalid content string '%s'\n", content_str);
1459 return (DCMD_ERR);
1460 }
1461
1462 if (t->t_pshandle == NULL) {
1463 mdb_warn("no process active\n");
1464 return (DCMD_ERR);
1465 }
1466
1467 pid = Pstatus(t->t_pshandle)->pr_pid;
1468 size = 1 + mdb_snprintf(NULL, 0, "%s.%d", prefix, (int)pid);
1469 fname = mdb_alloc(size, UM_SLEEP | UM_GC);
1470 (void) mdb_snprintf(fname, size, "%s.%d", prefix, (int)pid);
1471
1472 if (Pgcore(t->t_pshandle, fname, content) != 0) {
1473 /*
1474 * Short writes during dumping are specifically described by
1475 * EBADE, just as ZFS uses this otherwise-unused code for
1476 * checksum errors. Translate to and mdb errno.
1477 */
1478 if (errno == EBADE)
1479 (void) set_errno(EMDB_SHORTWRITE);
1480 mdb_warn("couldn't dump core");
1481 return (DCMD_ERR);
1482 }
1483
1484 mdb_warn("%s dumped\n", fname);
1485
1486 return (DCMD_OK);
1487 }
1488
1489 /*ARGSUSED*/
1490 static int
pt_kill(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1491 pt_kill(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1492 {
1493 mdb_tgt_t *t = mdb.m_target;
1494 pt_data_t *pt = t->t_data;
1495 int state;
1496
1497 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1498 return (DCMD_USAGE);
1499
1500 if (t->t_pshandle != NULL &&
1501 (state = Pstate(t->t_pshandle)) != PS_DEAD && state != PS_IDLE) {
1502 mdb_warn("victim process PID %d forcibly terminated\n",
1503 (int)Pstatus(t->t_pshandle)->pr_pid);
1504 pt_pre_detach(t, TRUE);
1505 pt_release_parents(t);
1506 Prelease(t->t_pshandle, PRELEASE_KILL);
1507 t->t_pshandle = pt->p_idlehandle;
1508 (void) mdb_tgt_status(t, &t->t_status);
1509 mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
1510 } else
1511 mdb_warn("no victim process is currently under control\n");
1512
1513 return (DCMD_OK);
1514 }
1515
1516 /*ARGSUSED*/
1517 static int
pt_detach(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1518 pt_detach(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1519 {
1520 mdb_tgt_t *t = mdb.m_target;
1521 pt_data_t *pt = t->t_data;
1522 int rflags = pt->p_rflags;
1523
1524 if (argc != 0 && argv->a_type == MDB_TYPE_STRING &&
1525 strcmp(argv->a_un.a_str, "-a") == 0) {
1526 rflags = PRELEASE_HANG | PRELEASE_CLEAR;
1527 argv++;
1528 argc--;
1529 }
1530
1531 if ((flags & DCMD_ADDRSPEC) || argc != 0)
1532 return (DCMD_USAGE);
1533
1534 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE) {
1535 mdb_warn("debugger is not currently attached to a process "
1536 "or core file\n");
1537 return (DCMD_ERR);
1538 }
1539
1540 pt_pre_detach(t, TRUE);
1541 pt_release_parents(t);
1542 Prelease(t->t_pshandle, rflags);
1543 t->t_pshandle = pt->p_idlehandle;
1544 (void) mdb_tgt_status(t, &t->t_status);
1545 mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
1546
1547 return (DCMD_OK);
1548 }
1549
1550 static uintmax_t
reg_disc_get(const mdb_var_t * v)1551 reg_disc_get(const mdb_var_t *v)
1552 {
1553 mdb_tgt_t *t = MDB_NV_COOKIE(v);
1554 mdb_tgt_tid_t tid = PTL_TID(t);
1555 mdb_tgt_reg_t r = 0;
1556
1557 if (tid != (mdb_tgt_tid_t)-1L)
1558 (void) mdb_tgt_getareg(t, tid, mdb_nv_get_name(v), &r);
1559
1560 return (r);
1561 }
1562
1563 static void
reg_disc_set(mdb_var_t * v,uintmax_t r)1564 reg_disc_set(mdb_var_t *v, uintmax_t r)
1565 {
1566 mdb_tgt_t *t = MDB_NV_COOKIE(v);
1567 mdb_tgt_tid_t tid = PTL_TID(t);
1568
1569 if (tid != (mdb_tgt_tid_t)-1L && mdb_tgt_putareg(t, tid,
1570 mdb_nv_get_name(v), r) == -1)
1571 mdb_warn("failed to modify %%%s register", mdb_nv_get_name(v));
1572 }
1573
1574 static void
pt_print_reason(const lwpstatus_t * psp)1575 pt_print_reason(const lwpstatus_t *psp)
1576 {
1577 char name[SIG2STR_MAX + 4]; /* enough for SIG+name+\0, syscall or flt */
1578 const char *desc;
1579
1580 switch (psp->pr_why) {
1581 case PR_REQUESTED:
1582 mdb_printf("stopped by debugger");
1583 break;
1584 case PR_SIGNALLED:
1585 mdb_printf("stopped on %s (%s)", proc_signame(psp->pr_what,
1586 name, sizeof (name)), strsignal(psp->pr_what));
1587 break;
1588 case PR_SYSENTRY:
1589 mdb_printf("stopped on entry to %s system call",
1590 proc_sysname(psp->pr_what, name, sizeof (name)));
1591 break;
1592 case PR_SYSEXIT:
1593 mdb_printf("stopped on exit from %s system call",
1594 proc_sysname(psp->pr_what, name, sizeof (name)));
1595 break;
1596 case PR_JOBCONTROL:
1597 mdb_printf("stopped by job control");
1598 break;
1599 case PR_FAULTED:
1600 if (psp->pr_what == FLTBPT) {
1601 mdb_printf("stopped on a breakpoint");
1602 } else if (psp->pr_what == FLTWATCH) {
1603 switch (psp->pr_info.si_code) {
1604 case TRAP_RWATCH:
1605 desc = "read";
1606 break;
1607 case TRAP_WWATCH:
1608 desc = "write";
1609 break;
1610 case TRAP_XWATCH:
1611 desc = "execute";
1612 break;
1613 default:
1614 desc = "unknown";
1615 }
1616 mdb_printf("stopped %s a watchpoint (%s access to %p)",
1617 psp->pr_info.si_trapafter ? "after" : "on",
1618 desc, psp->pr_info.si_addr);
1619 } else if (psp->pr_what == FLTTRACE) {
1620 mdb_printf("stopped after a single-step");
1621 } else {
1622 mdb_printf("stopped on a %s fault",
1623 proc_fltname(psp->pr_what, name, sizeof (name)));
1624 }
1625 break;
1626 case PR_SUSPENDED:
1627 case PR_CHECKPOINT:
1628 mdb_printf("suspended by the kernel");
1629 break;
1630 default:
1631 mdb_printf("stopped for unknown reason (%d/%d)",
1632 psp->pr_why, psp->pr_what);
1633 }
1634 }
1635
1636 static void
pt_status_dcmd_upanic(prupanic_t * pru)1637 pt_status_dcmd_upanic(prupanic_t *pru)
1638 {
1639 size_t i;
1640
1641 mdb_printf("process panicked\n");
1642 if ((pru->pru_flags & PRUPANIC_FLAG_MSG_ERROR) != 0) {
1643 mdb_printf("warning: process upanic message was bad\n");
1644 return;
1645 }
1646
1647 if ((pru->pru_flags & PRUPANIC_FLAG_MSG_VALID) == 0)
1648 return;
1649
1650 if ((pru->pru_flags & PRUPANIC_FLAG_MSG_TRUNC) != 0) {
1651 mdb_printf("warning: process upanic message truncated\n");
1652 }
1653
1654 mdb_printf("upanic message: ");
1655
1656 for (i = 0; i < PRUPANIC_BUFLEN; i++) {
1657 if (pru->pru_data[i] == '\0')
1658 break;
1659 if (isascii(pru->pru_data[i]) && isprint(pru->pru_data[i])) {
1660 mdb_printf("%c", pru->pru_data[i]);
1661 } else {
1662 mdb_printf("\\x%02x", pru->pru_data[i]);
1663 }
1664 }
1665 mdb_printf("\n");
1666 }
1667
1668 /*ARGSUSED*/
1669 static int
pt_status_dcmd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1670 pt_status_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1671 {
1672 mdb_tgt_t *t = mdb.m_target;
1673 struct ps_prochandle *P = t->t_pshandle;
1674 pt_data_t *pt = t->t_data;
1675
1676 if (P != NULL) {
1677 const psinfo_t *pip = Ppsinfo(P);
1678 const pstatus_t *psp = Pstatus(P);
1679 int cursig = 0, bits = 0, coredump = 0;
1680 int state;
1681 GElf_Sym sym;
1682 uintptr_t panicstr;
1683 char *panicbuf = mdb_alloc(PANIC_BUFSIZE, UM_SLEEP);
1684 const siginfo_t *sip = &(psp->pr_lwp.pr_info);
1685 prupanic_t *pru = NULL;
1686
1687 char execname[MAXPATHLEN], buf[BUFSIZ];
1688 char signame[SIG2STR_MAX + 4]; /* enough for SIG+name+\0 */
1689
1690 mdb_tgt_spec_desc_t desc;
1691 mdb_sespec_t *sep;
1692
1693 struct utsname uts;
1694 prcred_t cred;
1695 psinfo_t pi;
1696
1697 (void) strcpy(uts.nodename, "unknown machine");
1698 (void) Puname(P, &uts);
1699
1700 if (pip != NULL) {
1701 bcopy(pip, &pi, sizeof (psinfo_t));
1702 proc_unctrl_psinfo(&pi);
1703 } else
1704 bzero(&pi, sizeof (psinfo_t));
1705
1706 bits = pi.pr_dmodel == PR_MODEL_ILP32 ? 32 : 64;
1707
1708 state = Pstate(P);
1709 if (psp != NULL && state != PS_UNDEAD && state != PS_IDLE)
1710 cursig = psp->pr_lwp.pr_cursig;
1711
1712 if (state == PS_DEAD && pip != NULL) {
1713 mdb_printf("debugging core file of %s (%d-bit) "
1714 "from %s\n", pi.pr_fname, bits, uts.nodename);
1715
1716 } else if (state == PS_DEAD) {
1717 mdb_printf("debugging core file\n");
1718
1719 } else if (state == PS_IDLE) {
1720 const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
1721
1722 mdb_printf("debugging %s file (%d-bit)\n",
1723 ehp->e_type == ET_EXEC ? "executable" : "object",
1724 ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
1725
1726 } else if (state == PS_UNDEAD && pi.pr_pid == 0) {
1727 mdb_printf("debugging defunct process\n");
1728
1729 } else {
1730 mdb_printf("debugging PID %d (%d-bit)\n",
1731 pi.pr_pid, bits);
1732 }
1733
1734 if (Pexecname(P, execname, sizeof (execname)) != NULL)
1735 mdb_printf("file: %s\n", execname);
1736
1737 if (pip != NULL && state == PS_DEAD)
1738 mdb_printf("initial argv: %s\n", pi.pr_psargs);
1739
1740 if (state != PS_UNDEAD && state != PS_IDLE) {
1741 mdb_printf("threading model: ");
1742 if (pt->p_ptl_ops == &proc_lwp_ops)
1743 mdb_printf("raw lwps\n");
1744 else
1745 mdb_printf("native threads\n");
1746 }
1747
1748 mdb_printf("status: ");
1749 switch (state) {
1750 case PS_RUN:
1751 ASSERT(!(psp->pr_flags & PR_STOPPED));
1752 mdb_printf("process is running");
1753 if (psp->pr_flags & PR_DSTOP)
1754 mdb_printf(", debugger stop directive pending");
1755 mdb_printf("\n");
1756 break;
1757
1758 case PS_STOP:
1759 ASSERT(psp->pr_flags & PR_STOPPED);
1760 pt_print_reason(&psp->pr_lwp);
1761
1762 if (psp->pr_flags & PR_DSTOP)
1763 mdb_printf(", debugger stop directive pending");
1764 if (psp->pr_flags & PR_ASLEEP)
1765 mdb_printf(", sleeping in %s system call",
1766 proc_sysname(psp->pr_lwp.pr_syscall,
1767 signame, sizeof (signame)));
1768
1769 mdb_printf("\n");
1770
1771 for (sep = t->t_matched; sep != T_SE_END;
1772 sep = sep->se_matched) {
1773 mdb_printf("event: %s\n", sep->se_ops->se_info(
1774 t, sep, mdb_list_next(&sep->se_velist),
1775 &desc, buf, sizeof (buf)));
1776 }
1777 break;
1778
1779 case PS_LOST:
1780 mdb_printf("debugger lost control of process\n");
1781 break;
1782
1783 case PS_UNDEAD:
1784 coredump = WIFSIGNALED(pi.pr_wstat) &&
1785 WCOREDUMP(pi.pr_wstat);
1786 /*FALLTHRU*/
1787
1788 case PS_DEAD:
1789 if (cursig == 0 && WIFSIGNALED(pi.pr_wstat))
1790 cursig = WTERMSIG(pi.pr_wstat);
1791
1792 (void) Pupanic(P, &pru);
1793
1794 /*
1795 * Test for upanic first. We can only use pr_wstat == 0
1796 * as a test for gcore if an NT_PRCRED note is present;
1797 * these features were added at the same time in Solaris
1798 * 8.
1799 */
1800 if (pru != NULL) {
1801 pt_status_dcmd_upanic(pru);
1802 Pupanic_free(pru);
1803 } else if (pi.pr_wstat == 0 && Pstate(P) == PS_DEAD &&
1804 Pcred(P, &cred, 1) == 0) {
1805 mdb_printf("process core file generated "
1806 "with gcore(1)\n");
1807 } else if (cursig != 0) {
1808 mdb_printf("process terminated by %s (%s)",
1809 proc_signame(cursig, signame,
1810 sizeof (signame)), strsignal(cursig));
1811
1812 if (sip->si_signo != 0 && SI_FROMUSER(sip) &&
1813 sip->si_pid != 0) {
1814 mdb_printf(", pid=%d uid=%u",
1815 (int)sip->si_pid, sip->si_uid);
1816 if (sip->si_code != 0) {
1817 mdb_printf(" code=%d",
1818 sip->si_code);
1819 }
1820 } else {
1821 switch (sip->si_signo) {
1822 case SIGILL:
1823 case SIGTRAP:
1824 case SIGFPE:
1825 case SIGSEGV:
1826 case SIGBUS:
1827 case SIGEMT:
1828 mdb_printf(", addr=%p",
1829 sip->si_addr);
1830 default:
1831 break;
1832 }
1833 }
1834
1835 if (coredump)
1836 mdb_printf(" - core file dumped");
1837 mdb_printf("\n");
1838 } else {
1839 mdb_printf("process terminated with exit "
1840 "status %d\n", WEXITSTATUS(pi.pr_wstat));
1841 }
1842
1843 if (Plookup_by_name(t->t_pshandle, "libc.so",
1844 "panicstr", &sym) == 0 &&
1845 Pread(t->t_pshandle, &panicstr, sizeof (panicstr),
1846 sym.st_value) == sizeof (panicstr) &&
1847 Pread_string(t->t_pshandle, panicbuf,
1848 PANIC_BUFSIZE, panicstr) > 0) {
1849 mdb_printf("libc panic message: %s",
1850 panicbuf);
1851 }
1852
1853 break;
1854
1855 case PS_IDLE:
1856 mdb_printf("idle\n");
1857 break;
1858
1859 default:
1860 mdb_printf("unknown libproc Pstate: %d\n", Pstate(P));
1861 }
1862 mdb_free(panicbuf, PANIC_BUFSIZE);
1863
1864 } else if (pt->p_file != NULL) {
1865 const GElf_Ehdr *ehp = &pt->p_file->gf_ehdr;
1866
1867 mdb_printf("debugging %s file (%d-bit)\n",
1868 ehp->e_type == ET_EXEC ? "executable" : "object",
1869 ehp->e_ident[EI_CLASS] == ELFCLASS32 ? 32 : 64);
1870 mdb_printf("executable file: %s\n", IOP_NAME(pt->p_fio));
1871 mdb_printf("status: idle\n");
1872 }
1873
1874 return (DCMD_OK);
1875 }
1876
1877 static int
pt_tls(uintptr_t tid,uint_t flags,int argc,const mdb_arg_t * argv)1878 pt_tls(uintptr_t tid, uint_t flags, int argc, const mdb_arg_t *argv)
1879 {
1880 const char *name;
1881 const char *object;
1882 GElf_Sym sym;
1883 mdb_syminfo_t si;
1884 mdb_tgt_t *t = mdb.m_target;
1885
1886 if (!(flags & DCMD_ADDRSPEC) || argc > 1)
1887 return (DCMD_USAGE);
1888
1889 if (argc == 0) {
1890 psaddr_t b;
1891
1892 if (tlsbase(t, tid, PR_LMID_EVERY, MDB_TGT_OBJ_EXEC, &b) != 0) {
1893 mdb_warn("failed to lookup tlsbase for %r", tid);
1894 return (DCMD_ERR);
1895 }
1896
1897 mdb_printf("%lr\n", b);
1898 mdb_set_dot(b);
1899
1900 return (DCMD_OK);
1901 }
1902
1903 name = argv[0].a_un.a_str;
1904 object = MDB_TGT_OBJ_EVERY;
1905
1906 if (pt_lookup_by_name_thr(t, object, name, &sym, &si, tid) != 0) {
1907 mdb_warn("failed to lookup %s", name);
1908 return (DCMD_ABORT); /* avoid repeated failure */
1909 }
1910
1911 if (GELF_ST_TYPE(sym.st_info) != STT_TLS && DCMD_HDRSPEC(flags))
1912 mdb_warn("%s does not refer to thread local storage\n", name);
1913
1914 mdb_printf("%llr\n", sym.st_value);
1915 mdb_set_dot(sym.st_value);
1916
1917 return (DCMD_OK);
1918 }
1919
1920 /*ARGSUSED*/
1921 static int
pt_tmodel(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1922 pt_tmodel(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1923 {
1924 mdb_tgt_t *t = mdb.m_target;
1925 pt_data_t *pt = t->t_data;
1926 const pt_ptl_ops_t *ptl_ops;
1927
1928 if (argc != 1 || argv->a_type != MDB_TYPE_STRING)
1929 return (DCMD_USAGE);
1930
1931 if (strcmp(argv->a_un.a_str, "thread") == 0)
1932 ptl_ops = &proc_tdb_ops;
1933 else if (strcmp(argv->a_un.a_str, "lwp") == 0)
1934 ptl_ops = &proc_lwp_ops;
1935 else
1936 return (DCMD_USAGE);
1937
1938 if (t->t_pshandle != NULL && pt->p_ptl_ops != ptl_ops) {
1939 PTL_DTOR(t);
1940 pt->p_tdb_ops = NULL;
1941 pt->p_ptl_ops = &proc_lwp_ops;
1942 pt->p_ptl_hdl = NULL;
1943
1944 if (ptl_ops == &proc_tdb_ops) {
1945 (void) Pobject_iter(t->t_pshandle, (proc_map_f *)
1946 thr_check, t);
1947 }
1948 }
1949
1950 (void) mdb_tgt_status(t, &t->t_status);
1951 return (DCMD_OK);
1952 }
1953
1954 static const char *
env_match(const char * cmp,const char * nameval)1955 env_match(const char *cmp, const char *nameval)
1956 {
1957 const char *loc;
1958 size_t cmplen = strlen(cmp);
1959
1960 loc = strchr(nameval, '=');
1961 if (loc != NULL && (loc - nameval) == cmplen &&
1962 strncmp(nameval, cmp, cmplen) == 0) {
1963 return (loc + 1);
1964 }
1965
1966 return (NULL);
1967 }
1968
1969 /*ARGSUSED*/
1970 static int
print_env(void * data,struct ps_prochandle * P,uintptr_t addr,const char * nameval)1971 print_env(void *data, struct ps_prochandle *P, uintptr_t addr,
1972 const char *nameval)
1973 {
1974 const char *value;
1975
1976 if (nameval == NULL) {
1977 mdb_printf("<0x%p>\n", addr);
1978 } else {
1979 if (data == NULL)
1980 mdb_printf("%s\n", nameval);
1981 else if ((value = env_match(data, nameval)) != NULL)
1982 mdb_printf("%s\n", value);
1983 }
1984
1985 return (0);
1986 }
1987
1988 /*ARGSUSED*/
1989 static int
pt_getenv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1990 pt_getenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1991 {
1992 mdb_tgt_t *t = mdb.m_target;
1993 pt_data_t *pt = t->t_data;
1994 int i;
1995 uint_t opt_t = 0;
1996 mdb_var_t *v;
1997
1998 i = mdb_getopts(argc, argv,
1999 't', MDB_OPT_SETBITS, TRUE, &opt_t, NULL);
2000
2001 argc -= i;
2002 argv += i;
2003
2004 if ((flags & DCMD_ADDRSPEC) || argc > 1)
2005 return (DCMD_USAGE);
2006
2007 if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
2008 return (DCMD_USAGE);
2009
2010 if (opt_t && t->t_pshandle == NULL) {
2011 mdb_warn("no process active\n");
2012 return (DCMD_ERR);
2013 }
2014
2015 if (opt_t && (Pstate(t->t_pshandle) == PS_IDLE ||
2016 Pstate(t->t_pshandle) == PS_UNDEAD)) {
2017 mdb_warn("-t option requires target to be running\n");
2018 return (DCMD_ERR);
2019 }
2020
2021 if (opt_t != 0) {
2022 if (Penv_iter(t->t_pshandle, print_env,
2023 argc == 0 ? NULL : (void *)argv->a_un.a_str) != 0)
2024 return (DCMD_ERR);
2025 } else if (argc == 1) {
2026 if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) == NULL)
2027 return (DCMD_ERR);
2028
2029 ASSERT(strchr(mdb_nv_get_cookie(v), '=') != NULL);
2030 mdb_printf("%s\n", strchr(mdb_nv_get_cookie(v), '=') + 1);
2031 } else {
2032
2033 mdb_nv_rewind(&pt->p_env);
2034 while ((v = mdb_nv_advance(&pt->p_env)) != NULL)
2035 mdb_printf("%s\n", mdb_nv_get_cookie(v));
2036 }
2037
2038 return (DCMD_OK);
2039 }
2040
2041 /*
2042 * Function to set a variable in the internal environment, which is used when
2043 * creating new processes. Note that it is possible that 'nameval' can refer to
2044 * read-only memory, if mdb calls putenv() on an existing value before calling
2045 * this function. While we should avoid this situation, this function is
2046 * designed to be robust in the face of such changes.
2047 */
2048 static void
pt_env_set(pt_data_t * pt,const char * nameval)2049 pt_env_set(pt_data_t *pt, const char *nameval)
2050 {
2051 mdb_var_t *v;
2052 char *equals, *val;
2053 const char *name;
2054 size_t len;
2055
2056 if ((equals = strchr(nameval, '=')) != NULL) {
2057 val = strdup(nameval);
2058 equals = val + (equals - nameval);
2059 } else {
2060 /*
2061 * nameval doesn't contain an equals character. Convert this to
2062 * be 'nameval='.
2063 */
2064 len = strlen(nameval);
2065 val = mdb_alloc(len + 2, UM_SLEEP);
2066 (void) mdb_snprintf(val, len + 2, "%s=", nameval);
2067 equals = val + len;
2068 }
2069
2070 /* temporary truncate the string for lookup/insert */
2071 *equals = '\0';
2072 v = mdb_nv_lookup(&pt->p_env, val);
2073
2074 if (v != NULL) {
2075 char *old = mdb_nv_get_cookie(v);
2076 mdb_free(old, strlen(old) + 1);
2077 name = mdb_nv_get_name(v);
2078 } else {
2079 /*
2080 * The environment is created using MDB_NV_EXTNAME, so we must
2081 * provide external storage for the variable names.
2082 */
2083 name = strdup(val);
2084 }
2085
2086 *equals = '=';
2087
2088 (void) mdb_nv_insert(&pt->p_env, name, NULL, (uintptr_t)val,
2089 MDB_NV_EXTNAME);
2090
2091 *equals = '=';
2092 }
2093
2094 /*
2095 * Clears the internal environment.
2096 */
2097 static void
pt_env_clear(pt_data_t * pt)2098 pt_env_clear(pt_data_t *pt)
2099 {
2100 mdb_var_t *v;
2101 char *val, *name;
2102
2103 mdb_nv_rewind(&pt->p_env);
2104 while ((v = mdb_nv_advance(&pt->p_env)) != NULL) {
2105
2106 name = (char *)mdb_nv_get_name(v);
2107 val = mdb_nv_get_cookie(v);
2108
2109 mdb_nv_remove(&pt->p_env, v);
2110
2111 mdb_free(name, strlen(name) + 1);
2112 mdb_free(val, strlen(val) + 1);
2113 }
2114 }
2115
2116 /*ARGSUSED*/
2117 static int
pt_setenv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2118 pt_setenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2119 {
2120 mdb_tgt_t *t = mdb.m_target;
2121 pt_data_t *pt = t->t_data;
2122 char *nameval;
2123 size_t len;
2124 int alloc;
2125
2126 if ((flags & DCMD_ADDRSPEC) || argc == 0 || argc > 2)
2127 return (DCMD_USAGE);
2128
2129 if ((argc > 0 && argv[0].a_type != MDB_TYPE_STRING) ||
2130 (argc > 1 && argv[1].a_type != MDB_TYPE_STRING))
2131 return (DCMD_USAGE);
2132
2133 if (t->t_pshandle == NULL) {
2134 mdb_warn("no process active\n");
2135 return (DCMD_ERR);
2136 }
2137
2138 /*
2139 * If the process is in some sort of running state, warn the user that
2140 * changes won't immediately take effect.
2141 */
2142 if (Pstate(t->t_pshandle) == PS_RUN ||
2143 Pstate(t->t_pshandle) == PS_STOP) {
2144 mdb_warn("warning: changes will not take effect until process"
2145 " is restarted\n");
2146 }
2147
2148 /*
2149 * We allow two forms of operation. The first is the usual "name=value"
2150 * parameter. We also allow the user to specify two arguments, where
2151 * the first is the name of the variable, and the second is the value.
2152 */
2153 alloc = 0;
2154 if (argc == 1) {
2155 nameval = (char *)argv->a_un.a_str;
2156 } else {
2157 len = strlen(argv[0].a_un.a_str) +
2158 strlen(argv[1].a_un.a_str) + 2;
2159 nameval = mdb_alloc(len, UM_SLEEP);
2160 (void) mdb_snprintf(nameval, len, "%s=%s", argv[0].a_un.a_str,
2161 argv[1].a_un.a_str);
2162 alloc = 1;
2163 }
2164
2165 pt_env_set(pt, nameval);
2166
2167 if (alloc)
2168 mdb_free(nameval, strlen(nameval) + 1);
2169
2170 return (DCMD_OK);
2171 }
2172
2173 /*ARGSUSED*/
2174 static int
pt_unsetenv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2175 pt_unsetenv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2176 {
2177 mdb_tgt_t *t = mdb.m_target;
2178 pt_data_t *pt = t->t_data;
2179 mdb_var_t *v;
2180 char *value, *name;
2181
2182 if ((flags & DCMD_ADDRSPEC) || argc > 1)
2183 return (DCMD_USAGE);
2184
2185 if (argc == 1 && argv->a_type != MDB_TYPE_STRING)
2186 return (DCMD_USAGE);
2187
2188 if (t->t_pshandle == NULL) {
2189 mdb_warn("no process active\n");
2190 return (DCMD_ERR);
2191 }
2192
2193 /*
2194 * If the process is in some sort of running state, warn the user that
2195 * changes won't immediately take effect.
2196 */
2197 if (Pstate(t->t_pshandle) == PS_RUN ||
2198 Pstate(t->t_pshandle) == PS_STOP) {
2199 mdb_warn("warning: changes will not take effect until process"
2200 " is restarted\n");
2201 }
2202
2203 if (argc == 0) {
2204 pt_env_clear(pt);
2205 } else {
2206 if ((v = mdb_nv_lookup(&pt->p_env, argv->a_un.a_str)) != NULL) {
2207 name = (char *)mdb_nv_get_name(v);
2208 value = mdb_nv_get_cookie(v);
2209
2210 mdb_nv_remove(&pt->p_env, v);
2211
2212 mdb_free(name, strlen(name) + 1);
2213 mdb_free(value, strlen(value) + 1);
2214 }
2215 }
2216
2217 return (DCMD_OK);
2218 }
2219
2220 void
getenv_help(void)2221 getenv_help(void)
2222 {
2223 mdb_printf("-t show current process environment"
2224 " instead of initial environment.\n");
2225 }
2226
2227 static void
pt_stack_help(void)2228 pt_stack_help(void)
2229 {
2230 mdb_printf(
2231 "Options:\n"
2232 " -s show the size of each stack frame to the left\n"
2233 " -t where CTF is present, show types for functions and "
2234 "arguments\n"
2235 " -v include frame pointer information (this is the default "
2236 "for %<b>$C%</b>)\n"
2237 "\n"
2238 "If the optional %<u>cnt%</u> is given, no more than %<u>cnt%</u> "
2239 "arguments are shown\nfor each stack frame.\n");
2240 }
2241
2242 static void
pt_findstack_help(void)2243 pt_findstack_help(void)
2244 {
2245 mdb_printf(
2246 "Options:\n"
2247 " -n do not resolve addresses to names\n"
2248 " -s show the size of each stack frame to the left\n"
2249 " -t where CTF is present, show types for functions and "
2250 "arguments\n"
2251 " -v show function arguments\n"
2252 "\n"
2253 "If the optional %<u>cnt%</u> is given, no more than %<u>cnt%</u> "
2254 "arguments are shown\nfor each stack frame.\n");
2255 }
2256
2257 static const mdb_dcmd_t pt_dcmds[] = {
2258 { "$c", "?[-nstv] [cnt]", "print stack backtrace", pt_stack,
2259 pt_stack_help },
2260 { "$C", "?[-nstv] [cnt]", "print stack backtrace", pt_stackv,
2261 pt_stack_help },
2262 { "$i", NULL, "print signals that are ignored", pt_ignored },
2263 { "$l", NULL, "print the representative thread's lwp id", pt_lwpid },
2264 { "$L", NULL, "print list of the active lwp ids", pt_lwpids },
2265 { "$r", "?[-u]", "print general-purpose registers", pt_regs },
2266 { "$x", "?", "print floating point registers", pt_fpregs },
2267 { "$X", "?", "print floating point registers", pt_fpregs },
2268 { "$y", "?", "print floating point registers", pt_fpregs },
2269 { "$Y", "?", "print floating point registers", pt_fpregs },
2270 { "$?", "?", "print status and registers", pt_regstatus },
2271 { ":A", "?[core|pid]", "attach to process or core file", pt_attach },
2272 { ":i", ":", "ignore signal (delete all matching events)", pt_ignore },
2273 { ":k", NULL, "forcibly kill and release target", pt_kill },
2274 { ":R", "[-a]", "release the previously attached process", pt_detach },
2275 { "attach", "?[core|pid]",
2276 "attach to process or core file", pt_attach },
2277 { "findstack", ":[-nstv]", "find user thread stack", pt_findstack,
2278 pt_findstack_help },
2279 { "gcore", "[-o prefix] [-c content]",
2280 "produce a core file for the attached process", pt_gcore },
2281 { "getenv", "[-t] [name]", "display an environment variable",
2282 pt_getenv, getenv_help },
2283 { "kill", NULL, "forcibly kill and release target", pt_kill },
2284 { "release", "[-a]",
2285 "release the previously attached process", pt_detach },
2286 { "regs", "?[-u]", "print general-purpose registers", pt_regs },
2287 { "fpregs", "?[-dqs]", "print floating point registers", pt_fpregs },
2288 { "setenv", "name=value", "set an environment variable", pt_setenv },
2289 { "stack", "?[-nstv] [cnt]", "print stack backtrace", pt_stack,
2290 pt_stack_help },
2291 { "stackregs", "?[-nstv]", "print stack backtrace and registers",
2292 pt_stackr, pt_stack_help },
2293 { "status", NULL, "print summary of current target", pt_status_dcmd },
2294 { "tls", ":symbol",
2295 "lookup TLS data in the context of a given thread", pt_tls },
2296 { "tmodel", "{thread|lwp}", NULL, pt_tmodel },
2297 { "unsetenv", "[name]", "clear an environment variable", pt_unsetenv },
2298 { NULL }
2299 };
2300
2301 static void
pt_thr_walk_fini(mdb_walk_state_t * wsp)2302 pt_thr_walk_fini(mdb_walk_state_t *wsp)
2303 {
2304 mdb_addrvec_destroy(wsp->walk_data);
2305 mdb_free(wsp->walk_data, sizeof (mdb_addrvec_t));
2306 }
2307
2308 static int
pt_thr_walk_init(mdb_walk_state_t * wsp)2309 pt_thr_walk_init(mdb_walk_state_t *wsp)
2310 {
2311 wsp->walk_data = mdb_zalloc(sizeof (mdb_addrvec_t), UM_SLEEP);
2312 mdb_addrvec_create(wsp->walk_data);
2313
2314 if (PTL_ITER(mdb.m_target, wsp->walk_data) == -1) {
2315 mdb_warn("failed to iterate over threads");
2316 pt_thr_walk_fini(wsp);
2317 return (WALK_ERR);
2318 }
2319
2320 return (WALK_NEXT);
2321 }
2322
2323 static int
pt_thr_walk_step(mdb_walk_state_t * wsp)2324 pt_thr_walk_step(mdb_walk_state_t *wsp)
2325 {
2326 if (mdb_addrvec_length(wsp->walk_data) != 0) {
2327 return (wsp->walk_callback(mdb_addrvec_shift(wsp->walk_data),
2328 NULL, wsp->walk_cbdata));
2329 }
2330 return (WALK_DONE);
2331 }
2332
2333 static const mdb_walker_t pt_walkers[] = {
2334 { "thread", "walk list of valid thread identifiers",
2335 pt_thr_walk_init, pt_thr_walk_step, pt_thr_walk_fini },
2336 { NULL }
2337 };
2338
2339 static int
pt_agent_check(boolean_t * agent,const lwpstatus_t * psp)2340 pt_agent_check(boolean_t *agent, const lwpstatus_t *psp)
2341 {
2342 if (psp->pr_flags & PR_AGENT)
2343 *agent = B_TRUE;
2344
2345 return (0);
2346 }
2347
2348 static void
pt_activate_common(mdb_tgt_t * t)2349 pt_activate_common(mdb_tgt_t *t)
2350 {
2351 pt_data_t *pt = t->t_data;
2352 boolean_t hasagent = B_FALSE;
2353 GElf_Sym sym;
2354
2355 /*
2356 * If we have a libproc handle and AT_BASE is set, the process or core
2357 * is dynamically linked. We call Prd_agent() to force libproc to
2358 * try to initialize librtld_db, and issue a warning if that fails.
2359 */
2360 if (t->t_pshandle != NULL && Pgetauxval(t->t_pshandle,
2361 AT_BASE) != -1L && Prd_agent(t->t_pshandle) == NULL) {
2362 mdb_warn("warning: librtld_db failed to initialize; shared "
2363 "library information will not be available\n");
2364 }
2365
2366 if (t->t_pshandle != NULL) {
2367 (void) Plwp_iter(t->t_pshandle,
2368 (proc_lwp_f *)pt_agent_check, &hasagent);
2369 }
2370
2371 if (hasagent) {
2372 mdb_warn("agent lwp detected; forcing "
2373 "lwp thread model (use ::tmodel to change)\n");
2374 } else if (t->t_pshandle != NULL && Pstate(t->t_pshandle) != PS_IDLE) {
2375 /*
2376 * If we have a libproc handle and we do not have an agent LWP,
2377 * look for the correct thread debugging library. (If we have
2378 * an agent LWP, we leave the model as the raw LWP model to
2379 * allow the agent LWP to be visible to the debugger.)
2380 */
2381 (void) Pobject_iter(t->t_pshandle, (proc_map_f *)thr_check, t);
2382 }
2383
2384 /*
2385 * If there's a global object named '_mdb_abort_info', assuming we're
2386 * debugging mdb itself and load the developer support module.
2387 */
2388 if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, "_mdb_abort_info",
2389 &sym, NULL) == 0 && GELF_ST_TYPE(sym.st_info) == STT_OBJECT) {
2390 if (mdb_module_load("mdb_ds", MDB_MOD_SILENT) < 0)
2391 mdb_warn("warning: failed to load developer support\n");
2392 }
2393
2394 mdb_tgt_elf_export(pt->p_file);
2395 }
2396
2397 static void
pt_activate(mdb_tgt_t * t)2398 pt_activate(mdb_tgt_t *t)
2399 {
2400 static const mdb_nv_disc_t reg_disc = {
2401 .disc_set = reg_disc_set,
2402 .disc_get = reg_disc_get
2403 };
2404
2405 pt_data_t *pt = t->t_data;
2406 struct utsname u1, u2;
2407 mdb_var_t *v;
2408 core_content_t content;
2409
2410 if (t->t_pshandle) {
2411 mdb_prop_postmortem = (Pstate(t->t_pshandle) == PS_DEAD);
2412 mdb_prop_kernel = FALSE;
2413 } else
2414 mdb_prop_kernel = mdb_prop_postmortem = FALSE;
2415
2416 mdb_prop_datamodel = MDB_TGT_MODEL_NATIVE;
2417
2418 /*
2419 * If we're examining a core file that doesn't contain program text,
2420 * and uname(2) doesn't match the NT_UTSNAME note recorded in the
2421 * core file, issue a warning.
2422 */
2423 if (mdb_prop_postmortem == TRUE &&
2424 ((content = Pcontent(t->t_pshandle)) == CC_CONTENT_INVALID ||
2425 !(content & CC_CONTENT_TEXT)) &&
2426 uname(&u1) >= 0 && Puname(t->t_pshandle, &u2) == 0 &&
2427 (strcmp(u1.release, u2.release) != 0 ||
2428 strcmp(u1.version, u2.version) != 0)) {
2429 mdb_warn("warning: core file is from %s %s %s; shared text "
2430 "mappings may not match installed libraries\n",
2431 u2.sysname, u2.release, u2.version);
2432 }
2433
2434 /*
2435 * Perform the common initialization tasks -- these are shared with
2436 * the pt_exec() and pt_run() subroutines.
2437 */
2438 pt_activate_common(t);
2439
2440 (void) mdb_tgt_register_dcmds(t, &pt_dcmds[0], MDB_MOD_FORCE);
2441 (void) mdb_tgt_register_walkers(t, &pt_walkers[0], MDB_MOD_FORCE);
2442
2443 /*
2444 * Iterate through our register description list and export
2445 * each register as a named variable.
2446 */
2447 mdb_nv_rewind(&pt->p_regs);
2448 while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
2449 ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
2450
2451 if (!(rd_flags & MDB_TGT_R_EXPORT))
2452 continue; /* Don't export register as a variable */
2453
2454 (void) mdb_nv_insert(&mdb.m_nv, mdb_nv_get_name(v), ®_disc,
2455 (uintptr_t)t, MDB_NV_PERSIST);
2456 }
2457 }
2458
2459 static void
pt_deactivate(mdb_tgt_t * t)2460 pt_deactivate(mdb_tgt_t *t)
2461 {
2462 pt_data_t *pt = t->t_data;
2463 const mdb_dcmd_t *dcp;
2464 const mdb_walker_t *wp;
2465 mdb_var_t *v, *w;
2466
2467 mdb_nv_rewind(&pt->p_regs);
2468 while ((v = mdb_nv_advance(&pt->p_regs)) != NULL) {
2469 ushort_t rd_flags = MDB_TGT_R_FLAGS(mdb_nv_get_value(v));
2470
2471 if (!(rd_flags & MDB_TGT_R_EXPORT))
2472 continue; /* Didn't export register as a variable */
2473
2474 if (w = mdb_nv_lookup(&mdb.m_nv, mdb_nv_get_name(v))) {
2475 w->v_flags &= ~MDB_NV_PERSIST;
2476 mdb_nv_remove(&mdb.m_nv, w);
2477 }
2478 }
2479
2480 for (wp = &pt_walkers[0]; wp->walk_name != NULL; wp++) {
2481 if (mdb_module_remove_walker(t->t_module, wp->walk_name) == -1)
2482 warn("failed to remove walk %s", wp->walk_name);
2483 }
2484
2485 for (dcp = &pt_dcmds[0]; dcp->dc_name != NULL; dcp++) {
2486 if (mdb_module_remove_dcmd(t->t_module, dcp->dc_name) == -1)
2487 warn("failed to remove dcmd %s", dcp->dc_name);
2488 }
2489
2490 mdb_prop_postmortem = FALSE;
2491 mdb_prop_kernel = FALSE;
2492 mdb_prop_datamodel = MDB_TGT_MODEL_UNKNOWN;
2493 }
2494
2495 static void
pt_periodic(mdb_tgt_t * t)2496 pt_periodic(mdb_tgt_t *t)
2497 {
2498 pt_data_t *pt = t->t_data;
2499
2500 if (pt->p_rdstate == PT_RD_CONSIST) {
2501 if (t->t_pshandle != NULL && Pstate(t->t_pshandle) < PS_LOST &&
2502 !(mdb.m_flags & MDB_FL_NOMODS)) {
2503 mdb_printf("%s: You've got symbols!\n", mdb.m_pname);
2504 mdb_module_load_all(0);
2505 }
2506 pt->p_rdstate = PT_RD_NONE;
2507 }
2508 }
2509
2510 static void
pt_destroy(mdb_tgt_t * t)2511 pt_destroy(mdb_tgt_t *t)
2512 {
2513 pt_data_t *pt = t->t_data;
2514
2515 if (pt->p_idlehandle != NULL && pt->p_idlehandle != t->t_pshandle)
2516 Prelease(pt->p_idlehandle, 0);
2517
2518 if (t->t_pshandle != NULL) {
2519 PTL_DTOR(t);
2520 pt_release_parents(t);
2521 pt_pre_detach(t, TRUE);
2522 Prelease(t->t_pshandle, pt->p_rflags);
2523 }
2524
2525 mdb.m_flags &= ~(MDB_FL_VCREATE | MDB_FL_JOBCTL);
2526 pt_close_aout(t);
2527
2528 if (pt->p_aout_fio != NULL)
2529 mdb_io_rele(pt->p_aout_fio);
2530
2531 pt_env_clear(pt);
2532 mdb_nv_destroy(&pt->p_env);
2533
2534 mdb_nv_destroy(&pt->p_regs);
2535 mdb_free(pt, sizeof (pt_data_t));
2536 }
2537
2538 /*ARGSUSED*/
2539 static const char *
pt_name(mdb_tgt_t * t)2540 pt_name(mdb_tgt_t *t)
2541 {
2542 return ("proc");
2543 }
2544
2545 static const char *
pt_platform(mdb_tgt_t * t)2546 pt_platform(mdb_tgt_t *t)
2547 {
2548 pt_data_t *pt = t->t_data;
2549
2550 if (t->t_pshandle != NULL &&
2551 Pplatform(t->t_pshandle, pt->p_platform, MAXNAMELEN) != NULL)
2552 return (pt->p_platform);
2553
2554 return (mdb_conf_platform());
2555 }
2556
2557 static int
pt_uname(mdb_tgt_t * t,struct utsname * utsp)2558 pt_uname(mdb_tgt_t *t, struct utsname *utsp)
2559 {
2560 if (t->t_pshandle != NULL)
2561 return (Puname(t->t_pshandle, utsp));
2562
2563 return (uname(utsp) >= 0 ? 0 : -1);
2564 }
2565
2566 static int
pt_dmodel(mdb_tgt_t * t)2567 pt_dmodel(mdb_tgt_t *t)
2568 {
2569 if (t->t_pshandle == NULL)
2570 return (MDB_TGT_MODEL_NATIVE);
2571
2572 switch (Pstatus(t->t_pshandle)->pr_dmodel) {
2573 case PR_MODEL_ILP32:
2574 return (MDB_TGT_MODEL_ILP32);
2575 case PR_MODEL_LP64:
2576 return (MDB_TGT_MODEL_LP64);
2577 }
2578
2579 return (MDB_TGT_MODEL_UNKNOWN);
2580 }
2581
2582 static ssize_t
pt_vread(mdb_tgt_t * t,void * buf,size_t nbytes,uintptr_t addr)2583 pt_vread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
2584 {
2585 ssize_t n;
2586
2587 /*
2588 * If no handle is open yet, reads from virtual addresses are
2589 * allowed to succeed but return zero-filled memory.
2590 */
2591 if (t->t_pshandle == NULL) {
2592 bzero(buf, nbytes);
2593 return (nbytes);
2594 }
2595
2596 if ((n = Pread(t->t_pshandle, buf, nbytes, addr)) <= 0)
2597 return (set_errno(EMDB_NOMAP));
2598
2599 return (n);
2600 }
2601
2602 static ssize_t
pt_vwrite(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)2603 pt_vwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2604 {
2605 ssize_t n;
2606
2607 /*
2608 * If no handle is open yet, writes to virtual addresses are
2609 * allowed to succeed but do not actually modify anything.
2610 */
2611 if (t->t_pshandle == NULL)
2612 return (nbytes);
2613
2614 n = Pwrite(t->t_pshandle, buf, nbytes, addr);
2615
2616 if (n == -1 && errno == EIO)
2617 return (set_errno(EMDB_NOMAP));
2618
2619 return (n);
2620 }
2621
2622 static ssize_t
pt_fread(mdb_tgt_t * t,void * buf,size_t nbytes,uintptr_t addr)2623 pt_fread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
2624 {
2625 pt_data_t *pt = t->t_data;
2626
2627 if (pt->p_file != NULL) {
2628 return (mdb_gelf_rw(pt->p_file, buf, nbytes, addr,
2629 IOPF_READ(pt->p_fio), GIO_READ));
2630 }
2631
2632 bzero(buf, nbytes);
2633 return (nbytes);
2634 }
2635
2636 static ssize_t
pt_fwrite(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)2637 pt_fwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2638 {
2639 pt_data_t *pt = t->t_data;
2640
2641 if (pt->p_file != NULL) {
2642 return (mdb_gelf_rw(pt->p_file, (void *)buf, nbytes, addr,
2643 IOPF_WRITE(pt->p_fio), GIO_WRITE));
2644 }
2645
2646 return (nbytes);
2647 }
2648
2649 static const char *
pt_resolve_lmid(const char * object,Lmid_t * lmidp)2650 pt_resolve_lmid(const char *object, Lmid_t *lmidp)
2651 {
2652 Lmid_t lmid = PR_LMID_EVERY;
2653 const char *p;
2654
2655 if (object == MDB_TGT_OBJ_EVERY || object == MDB_TGT_OBJ_EXEC)
2656 lmid = LM_ID_BASE; /* restrict scope to a.out's link map */
2657 else if (object != MDB_TGT_OBJ_RTLD && strncmp(object, "LM", 2) == 0 &&
2658 (p = strchr(object, '`')) != NULL) {
2659 object += 2; /* skip past initial "LM" prefix */
2660 lmid = strntoul(object, (size_t)(p - object), mdb.m_radix);
2661 object = p + 1; /* skip past link map specifier */
2662 }
2663
2664 *lmidp = lmid;
2665 return (object);
2666 }
2667
2668 static int
tlsbase(mdb_tgt_t * t,mdb_tgt_tid_t tid,Lmid_t lmid,const char * object,psaddr_t * basep)2669 tlsbase(mdb_tgt_t *t, mdb_tgt_tid_t tid, Lmid_t lmid, const char *object,
2670 psaddr_t *basep)
2671 {
2672 pt_data_t *pt = t->t_data;
2673 const rd_loadobj_t *loadobjp;
2674 td_thrhandle_t th;
2675 td_err_e err;
2676
2677 if (object == MDB_TGT_OBJ_EVERY)
2678 return (set_errno(EINVAL));
2679
2680 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_IDLE)
2681 return (set_errno(EMDB_NOPROC));
2682
2683 if (pt->p_tdb_ops == NULL)
2684 return (set_errno(EMDB_TDB));
2685
2686 err = pt->p_tdb_ops->td_ta_map_id2thr(pt->p_ptl_hdl, tid, &th);
2687 if (err != TD_OK)
2688 return (set_errno(tdb_to_errno(err)));
2689
2690 /*
2691 * If this fails, rtld_db has failed to initialize properly.
2692 */
2693 if ((loadobjp = Plmid_to_loadobj(t->t_pshandle, lmid, object)) == NULL)
2694 return (set_errno(EMDB_NORTLD));
2695
2696 /*
2697 * This will fail if the TLS block has not been allocated for the
2698 * object that contains the TLS symbol in question.
2699 */
2700 err = pt->p_tdb_ops->td_thr_tlsbase(&th, loadobjp->rl_tlsmodid, basep);
2701 if (err != TD_OK)
2702 return (set_errno(tdb_to_errno(err)));
2703
2704 return (0);
2705 }
2706
2707 typedef struct {
2708 mdb_tgt_t *pl_tgt;
2709 const char *pl_name;
2710 Lmid_t pl_lmid;
2711 GElf_Sym *pl_symp;
2712 mdb_syminfo_t *pl_sip;
2713 mdb_tgt_tid_t pl_tid;
2714 mdb_bool_t pl_found;
2715 } pt_lookup_t;
2716
2717 /*ARGSUSED*/
2718 static int
pt_lookup_cb(void * data,const prmap_t * pmp,const char * object)2719 pt_lookup_cb(void *data, const prmap_t *pmp, const char *object)
2720 {
2721 pt_lookup_t *plp = data;
2722 struct ps_prochandle *P = plp->pl_tgt->t_pshandle;
2723 prsyminfo_t si;
2724 GElf_Sym sym;
2725
2726 if (Pxlookup_by_name(P, plp->pl_lmid, object, plp->pl_name, &sym,
2727 &si) != 0)
2728 return (0);
2729
2730 /*
2731 * If we encounter a match with SHN_UNDEF, keep looking for a
2732 * better match. Return the first match with SHN_UNDEF set if no
2733 * better match is found.
2734 */
2735 if (sym.st_shndx == SHN_UNDEF) {
2736 if (!plp->pl_found) {
2737 plp->pl_found = TRUE;
2738 *plp->pl_symp = sym;
2739 plp->pl_sip->sym_table = si.prs_table;
2740 plp->pl_sip->sym_id = si.prs_id;
2741 }
2742
2743 return (0);
2744 }
2745
2746 /*
2747 * Note that if the symbol's st_shndx is SHN_UNDEF we don't have the
2748 * TLS offset anyway, so adding in the tlsbase would be worthless.
2749 */
2750 if (GELF_ST_TYPE(sym.st_info) == STT_TLS &&
2751 plp->pl_tid != (mdb_tgt_tid_t)-1) {
2752 psaddr_t base;
2753
2754 if (tlsbase(plp->pl_tgt, plp->pl_tid, plp->pl_lmid, object,
2755 &base) != 0)
2756 return (-1); /* errno is set for us */
2757
2758 sym.st_value += base;
2759 }
2760
2761 plp->pl_found = TRUE;
2762 *plp->pl_symp = sym;
2763 plp->pl_sip->sym_table = si.prs_table;
2764 plp->pl_sip->sym_id = si.prs_id;
2765
2766 return (1);
2767 }
2768
2769 /*
2770 * Lookup the symbol with a thread context so that we can adjust TLS symbols
2771 * to get the values as they would appear in the context of the given thread.
2772 */
2773 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)2774 pt_lookup_by_name_thr(mdb_tgt_t *t, const char *object,
2775 const char *name, GElf_Sym *symp, mdb_syminfo_t *sip, mdb_tgt_tid_t tid)
2776 {
2777 struct ps_prochandle *P = t->t_pshandle;
2778 pt_data_t *pt = t->t_data;
2779 Lmid_t lmid;
2780 uint_t i;
2781 const rd_loadobj_t *aout_lop;
2782
2783 object = pt_resolve_lmid(object, &lmid);
2784
2785 if (P != NULL) {
2786 pt_lookup_t pl;
2787
2788 pl.pl_tgt = t;
2789 pl.pl_name = name;
2790 pl.pl_lmid = lmid;
2791 pl.pl_symp = symp;
2792 pl.pl_sip = sip;
2793 pl.pl_tid = tid;
2794 pl.pl_found = FALSE;
2795
2796 if (object == MDB_TGT_OBJ_EVERY) {
2797 if (Pobject_iter_resolved(P, pt_lookup_cb, &pl) == -1)
2798 return (-1); /* errno is set for us */
2799 if ((!pl.pl_found) &&
2800 (Pobject_iter(P, pt_lookup_cb, &pl) == -1))
2801 return (-1); /* errno is set for us */
2802 } else {
2803 const prmap_t *pmp;
2804
2805 /*
2806 * This can fail either due to an invalid lmid or
2807 * an invalid object. To determine which is
2808 * faulty, we test the lmid against known valid
2809 * lmids and then see if using a wild-card lmid
2810 * improves ths situation.
2811 */
2812 if ((pmp = Plmid_to_map(P, lmid, object)) == NULL) {
2813 if (lmid != PR_LMID_EVERY &&
2814 lmid != LM_ID_BASE &&
2815 lmid != LM_ID_LDSO &&
2816 Plmid_to_map(P, PR_LMID_EVERY, object)
2817 != NULL)
2818 return (set_errno(EMDB_NOLMID));
2819 else
2820 return (set_errno(EMDB_NOOBJ));
2821 }
2822
2823 if (pt_lookup_cb(&pl, pmp, object) == -1)
2824 return (-1); /* errno is set for us */
2825 }
2826
2827 if (pl.pl_found)
2828 return (0);
2829 }
2830
2831 /*
2832 * If libproc doesn't have the symbols for rtld, we're cooked --
2833 * mdb doesn't have those symbols either.
2834 */
2835 if (object == MDB_TGT_OBJ_RTLD)
2836 return (set_errno(EMDB_NOSYM));
2837
2838 if (object != MDB_TGT_OBJ_EXEC && object != MDB_TGT_OBJ_EVERY) {
2839 int status = mdb_gelf_symtab_lookup_by_file(pt->p_symtab,
2840 object, name, symp, &sip->sym_id);
2841
2842 if (status != 0) {
2843 if (P != NULL &&
2844 Plmid_to_map(P, PR_LMID_EVERY, object) != NULL)
2845 return (set_errno(EMDB_NOSYM));
2846 else
2847 return (-1); /* errno set from lookup_by_file */
2848 }
2849
2850 goto found;
2851 }
2852
2853 if (mdb_gelf_symtab_lookup_by_name(pt->p_symtab, name, symp, &i) == 0) {
2854 sip->sym_table = MDB_TGT_SYMTAB;
2855 sip->sym_id = i;
2856 goto local_found;
2857 }
2858
2859 if (mdb_gelf_symtab_lookup_by_name(pt->p_dynsym, name, symp, &i) == 0) {
2860 sip->sym_table = MDB_TGT_DYNSYM;
2861 sip->sym_id = i;
2862 goto local_found;
2863 }
2864
2865 return (set_errno(EMDB_NOSYM));
2866
2867 local_found:
2868 if (pt->p_file != NULL &&
2869 pt->p_file->gf_ehdr.e_type == ET_DYN &&
2870 P != NULL &&
2871 (aout_lop = Pname_to_loadobj(P, PR_OBJ_EXEC)) != NULL)
2872 symp->st_value += aout_lop->rl_base;
2873
2874 found:
2875 /*
2876 * If the symbol has type TLS, libproc should have found the symbol
2877 * if it exists and has been allocated.
2878 */
2879 if (GELF_ST_TYPE(symp->st_info) == STT_TLS)
2880 return (set_errno(EMDB_TLS));
2881
2882 return (0);
2883 }
2884
2885 static int
pt_lookup_by_name(mdb_tgt_t * t,const char * object,const char * name,GElf_Sym * symp,mdb_syminfo_t * sip)2886 pt_lookup_by_name(mdb_tgt_t *t, const char *object,
2887 const char *name, GElf_Sym *symp, mdb_syminfo_t *sip)
2888 {
2889 return (pt_lookup_by_name_thr(t, object, name, symp, sip, PTL_TID(t)));
2890 }
2891
2892 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)2893 pt_lookup_by_addr(mdb_tgt_t *t, uintptr_t addr, uint_t flags,
2894 char *buf, size_t nbytes, GElf_Sym *symp, mdb_syminfo_t *sip)
2895 {
2896 struct ps_prochandle *P = t->t_pshandle;
2897 pt_data_t *pt = t->t_data;
2898 rd_plt_info_t rpi = { 0 };
2899
2900 const char *pltsym;
2901 int rv, match, i;
2902
2903 mdb_gelf_symtab_t *gsts[3]; /* mdb.m_prsym, .symtab, .dynsym */
2904 int gstc = 0; /* number of valid gsts[] entries */
2905
2906 mdb_gelf_symtab_t *gst = NULL; /* set if 'sym' is from a gst */
2907 const prmap_t *pmp = NULL; /* set if 'sym' is from libproc */
2908 GElf_Sym sym; /* best symbol found so far if !exact */
2909 prsyminfo_t si;
2910
2911 /*
2912 * Fill in our array of symbol table pointers with the private symbol
2913 * table, static symbol table, and dynamic symbol table if applicable.
2914 * These are done in order of precedence so that if we match and
2915 * MDB_TGT_SYM_EXACT is set, we need not look any further.
2916 */
2917 if (mdb.m_prsym != NULL)
2918 gsts[gstc++] = mdb.m_prsym;
2919 if (P == NULL && pt->p_symtab != NULL)
2920 gsts[gstc++] = pt->p_symtab;
2921 if (P == NULL && pt->p_dynsym != NULL)
2922 gsts[gstc++] = pt->p_dynsym;
2923
2924 /*
2925 * Loop through our array attempting to match the address. If we match
2926 * and we're in exact mode, we're done. Otherwise save the symbol in
2927 * the local sym variable if it is closer than our previous match.
2928 * We explicitly watch for zero-valued symbols since DevPro insists
2929 * on storing __fsr_init_value's value as the symbol value instead
2930 * of storing it in a constant integer.
2931 */
2932 for (i = 0; i < gstc; i++) {
2933 if (mdb_gelf_symtab_lookup_by_addr(gsts[i], addr, flags, buf,
2934 nbytes, symp, &sip->sym_id) != 0 || symp->st_value == 0)
2935 continue;
2936
2937 if (flags & MDB_TGT_SYM_EXACT) {
2938 gst = gsts[i];
2939 goto found;
2940 }
2941
2942 if (gst == NULL || mdb_gelf_sym_closer(symp, &sym, addr)) {
2943 gst = gsts[i];
2944 sym = *symp;
2945 }
2946 }
2947
2948 /*
2949 * If we have no libproc handle active, we're done: fail if gst is
2950 * NULL; otherwise copy out our best symbol and skip to the end.
2951 * We also skip to found if gst is the private symbol table: we
2952 * want this to always take precedence over PLT re-vectoring.
2953 */
2954 if (P == NULL || (gst != NULL && gst == mdb.m_prsym)) {
2955 if (gst == NULL)
2956 return (set_errno(EMDB_NOSYMADDR));
2957 *symp = sym;
2958 goto found;
2959 }
2960
2961 /*
2962 * Check to see if the address is in a PLT: if it is, use librtld_db to
2963 * attempt to resolve the PLT entry. If the entry is bound, reset addr
2964 * to the bound address, add a special prefix to the caller's buf,
2965 * forget our previous guess, and then continue using the new addr.
2966 * If the entry is not bound, copy the corresponding symbol name into
2967 * buf and return a fake symbol for the given address.
2968 */
2969 if ((pltsym = Ppltdest(P, addr)) != NULL) {
2970 const rd_loadobj_t *rlp;
2971 rd_agent_t *rap;
2972
2973 if ((rap = Prd_agent(P)) != NULL &&
2974 (rlp = Paddr_to_loadobj(P, addr)) != NULL &&
2975 rd_plt_resolution(rap, addr, Pstatus(P)->pr_lwp.pr_lwpid,
2976 rlp->rl_plt_base, &rpi) == RD_OK &&
2977 (rpi.pi_flags & RD_FLG_PI_PLTBOUND)) {
2978 size_t n;
2979 n = mdb_iob_snprintf(buf, nbytes, "PLT=");
2980 addr = rpi.pi_baddr;
2981 if (n > nbytes) {
2982 buf += nbytes;
2983 nbytes = 0;
2984 } else {
2985 buf += n;
2986 nbytes -= n;
2987 }
2988 gst = NULL;
2989 } else {
2990 (void) mdb_iob_snprintf(buf, nbytes, "PLT:%s", pltsym);
2991 bzero(symp, sizeof (GElf_Sym));
2992 symp->st_value = addr;
2993 symp->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
2994 return (0);
2995 }
2996 }
2997
2998 /*
2999 * Ask libproc to convert the address to the closest symbol for us.
3000 * Once we get the closest symbol, we perform the EXACT match or
3001 * smart-mode or absolute distance check ourself:
3002 */
3003 if (PT_LIBPROC_RESOLVE(P)) {
3004 rv = Pxlookup_by_addr_resolved(P, addr, buf, nbytes,
3005 symp, &si);
3006 } else {
3007 rv = Pxlookup_by_addr(P, addr, buf, nbytes,
3008 symp, &si);
3009 }
3010 if ((rv == 0) && (symp->st_value != 0) &&
3011 (gst == NULL || mdb_gelf_sym_closer(symp, &sym, addr))) {
3012
3013 if (flags & MDB_TGT_SYM_EXACT)
3014 match = (addr == symp->st_value);
3015 else if (mdb.m_symdist == 0)
3016 match = (addr >= symp->st_value &&
3017 addr < symp->st_value + symp->st_size);
3018 else
3019 match = (addr >= symp->st_value &&
3020 addr < symp->st_value + mdb.m_symdist);
3021
3022 if (match) {
3023 pmp = Paddr_to_map(P, addr);
3024 gst = NULL;
3025 sip->sym_table = si.prs_table;
3026 sip->sym_id = si.prs_id;
3027 goto found;
3028 }
3029 }
3030
3031 /*
3032 * If we get here, Plookup_by_addr has failed us. If we have no
3033 * previous best symbol (gst == NULL), we've failed completely.
3034 * Otherwise we copy out that symbol and continue on to 'found'.
3035 */
3036 if (gst == NULL)
3037 return (set_errno(EMDB_NOSYMADDR));
3038 *symp = sym;
3039 found:
3040 /*
3041 * Once we've found something, copy the final name into the caller's
3042 * buffer and prefix it with the mapping name if appropriate.
3043 */
3044 if (pmp != NULL && pmp != Pname_to_map(P, PR_OBJ_EXEC)) {
3045 const char *prefix = pmp->pr_mapname;
3046 Lmid_t lmid;
3047
3048 if (PT_LIBPROC_RESOLVE(P)) {
3049 if (Pobjname_resolved(P, addr, pt->p_objname,
3050 MDB_TGT_MAPSZ))
3051 prefix = pt->p_objname;
3052 } else {
3053 if (Pobjname(P, addr, pt->p_objname, MDB_TGT_MAPSZ))
3054 prefix = pt->p_objname;
3055 }
3056
3057 if (buf != NULL && nbytes > 1) {
3058 (void) strncpy(pt->p_symname, buf, MDB_TGT_SYM_NAMLEN);
3059 pt->p_symname[MDB_TGT_SYM_NAMLEN - 1] = '\0';
3060 } else {
3061 pt->p_symname[0] = '\0';
3062 }
3063
3064 if (prefix == pt->p_objname && Plmid(P, addr, &lmid) == 0 && (
3065 (lmid != LM_ID_BASE && lmid != LM_ID_LDSO) ||
3066 (mdb.m_flags & MDB_FL_SHOWLMID))) {
3067 (void) mdb_iob_snprintf(buf, nbytes, "LM%lr`%s`%s",
3068 lmid, strbasename(prefix), pt->p_symname);
3069 } else {
3070 (void) mdb_iob_snprintf(buf, nbytes, "%s`%s",
3071 strbasename(prefix), pt->p_symname);
3072 }
3073
3074 } else if (gst != NULL && buf != NULL && nbytes > 0) {
3075 (void) strncpy(buf, mdb_gelf_sym_name(gst, symp), nbytes);
3076 buf[nbytes - 1] = '\0';
3077 }
3078
3079 return (0);
3080 }
3081
3082
3083 static int
pt_symbol_iter_cb(void * arg,const GElf_Sym * sym,const char * name,const prsyminfo_t * sip)3084 pt_symbol_iter_cb(void *arg, const GElf_Sym *sym, const char *name,
3085 const prsyminfo_t *sip)
3086 {
3087 pt_symarg_t *psp = arg;
3088
3089 psp->psym_info.sym_id = sip->prs_id;
3090
3091 return (psp->psym_func(psp->psym_private, sym, name, &psp->psym_info,
3092 psp->psym_obj));
3093 }
3094
3095 static int
pt_objsym_iter(void * arg,const prmap_t * pmp,const char * object)3096 pt_objsym_iter(void *arg, const prmap_t *pmp, const char *object)
3097 {
3098 Lmid_t lmid = PR_LMID_EVERY;
3099 pt_symarg_t *psp = arg;
3100
3101 psp->psym_obj = object;
3102
3103 (void) Plmid(psp->psym_targ->t_pshandle, pmp->pr_vaddr, &lmid);
3104 (void) Pxsymbol_iter(psp->psym_targ->t_pshandle, lmid, object,
3105 psp->psym_which, psp->psym_type, pt_symbol_iter_cb, arg);
3106
3107 return (0);
3108 }
3109
3110 static int
pt_symbol_filt(void * arg,const GElf_Sym * sym,const char * name,uint_t id)3111 pt_symbol_filt(void *arg, const GElf_Sym *sym, const char *name, uint_t id)
3112 {
3113 pt_symarg_t *psp = arg;
3114
3115 if (mdb_tgt_sym_match(sym, psp->psym_type)) {
3116 psp->psym_info.sym_id = id;
3117 return (psp->psym_func(psp->psym_private, sym, name,
3118 &psp->psym_info, psp->psym_obj));
3119 }
3120
3121 return (0);
3122 }
3123
3124 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)3125 pt_symbol_iter(mdb_tgt_t *t, const char *object, uint_t which,
3126 uint_t type, mdb_tgt_sym_f *func, void *private)
3127 {
3128 pt_data_t *pt = t->t_data;
3129 mdb_gelf_symtab_t *gst;
3130 pt_symarg_t ps;
3131 Lmid_t lmid;
3132
3133 object = pt_resolve_lmid(object, &lmid);
3134
3135 ps.psym_targ = t;
3136 ps.psym_which = which;
3137 ps.psym_type = type;
3138 ps.psym_func = func;
3139 ps.psym_private = private;
3140 ps.psym_obj = object;
3141
3142 if (t->t_pshandle != NULL) {
3143 if (object != MDB_TGT_OBJ_EVERY) {
3144 if (Plmid_to_map(t->t_pshandle, lmid, object) == NULL)
3145 return (set_errno(EMDB_NOOBJ));
3146 (void) Pxsymbol_iter(t->t_pshandle, lmid, object,
3147 which, type, pt_symbol_iter_cb, &ps);
3148 return (0);
3149 } else if (Prd_agent(t->t_pshandle) != NULL) {
3150 if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3151 (void) Pobject_iter_resolved(t->t_pshandle,
3152 pt_objsym_iter, &ps);
3153 } else {
3154 (void) Pobject_iter(t->t_pshandle,
3155 pt_objsym_iter, &ps);
3156 }
3157 return (0);
3158 }
3159 }
3160
3161 if (lmid != LM_ID_BASE && lmid != PR_LMID_EVERY)
3162 return (set_errno(EMDB_NOLMID));
3163
3164 if (object != MDB_TGT_OBJ_EXEC && object != MDB_TGT_OBJ_EVERY &&
3165 pt->p_fio != NULL &&
3166 strcmp(object, IOP_NAME(pt->p_fio)) != 0)
3167 return (set_errno(EMDB_NOOBJ));
3168
3169 if (which == MDB_TGT_SYMTAB)
3170 gst = pt->p_symtab;
3171 else
3172 gst = pt->p_dynsym;
3173
3174 if (gst != NULL) {
3175 ps.psym_info.sym_table = gst->gst_tabid;
3176 mdb_gelf_symtab_iter(gst, pt_symbol_filt, &ps);
3177 }
3178
3179 return (0);
3180 }
3181
3182 static const mdb_map_t *
pt_prmap_to_mdbmap(mdb_tgt_t * t,const prmap_t * prp,mdb_map_t * mp)3183 pt_prmap_to_mdbmap(mdb_tgt_t *t, const prmap_t *prp, mdb_map_t *mp)
3184 {
3185 struct ps_prochandle *P = t->t_pshandle;
3186 char *rv, name[MAXPATHLEN];
3187 Lmid_t lmid;
3188
3189 if (PT_LIBPROC_RESOLVE(P)) {
3190 rv = Pobjname_resolved(P, prp->pr_vaddr, name, sizeof (name));
3191 } else {
3192 rv = Pobjname(P, prp->pr_vaddr, name, sizeof (name));
3193 }
3194
3195 if (rv != NULL) {
3196 if (Plmid(P, prp->pr_vaddr, &lmid) == 0 && (
3197 (lmid != LM_ID_BASE && lmid != LM_ID_LDSO) ||
3198 (mdb.m_flags & MDB_FL_SHOWLMID))) {
3199 (void) mdb_iob_snprintf(mp->map_name, MDB_TGT_MAPSZ,
3200 "LM%lr`%s", lmid, name);
3201 } else {
3202 (void) strncpy(mp->map_name, name, MDB_TGT_MAPSZ - 1);
3203 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3204 }
3205 } else {
3206 (void) strncpy(mp->map_name, prp->pr_mapname,
3207 MDB_TGT_MAPSZ - 1);
3208 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3209 }
3210
3211 mp->map_base = prp->pr_vaddr;
3212 mp->map_size = prp->pr_size;
3213 mp->map_flags = 0;
3214
3215 if (prp->pr_mflags & MA_READ)
3216 mp->map_flags |= MDB_TGT_MAP_R;
3217 if (prp->pr_mflags & MA_WRITE)
3218 mp->map_flags |= MDB_TGT_MAP_W;
3219 if (prp->pr_mflags & MA_EXEC)
3220 mp->map_flags |= MDB_TGT_MAP_X;
3221
3222 if (prp->pr_mflags & MA_SHM)
3223 mp->map_flags |= MDB_TGT_MAP_SHMEM;
3224 if (prp->pr_mflags & MA_BREAK)
3225 mp->map_flags |= MDB_TGT_MAP_HEAP;
3226 if (prp->pr_mflags & MA_STACK)
3227 mp->map_flags |= MDB_TGT_MAP_STACK;
3228 if (prp->pr_mflags & MA_ANON)
3229 mp->map_flags |= MDB_TGT_MAP_ANON;
3230
3231 return (mp);
3232 }
3233
3234 /*ARGSUSED*/
3235 static int
pt_map_apply(void * arg,const prmap_t * prp,const char * name)3236 pt_map_apply(void *arg, const prmap_t *prp, const char *name)
3237 {
3238 pt_maparg_t *pmp = arg;
3239 mdb_map_t map;
3240
3241 return (pmp->pmap_func(pmp->pmap_private,
3242 pt_prmap_to_mdbmap(pmp->pmap_targ, prp, &map), map.map_name));
3243 }
3244
3245 static int
pt_mapping_iter(mdb_tgt_t * t,mdb_tgt_map_f * func,void * private)3246 pt_mapping_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
3247 {
3248 if (t->t_pshandle != NULL) {
3249 pt_maparg_t pm;
3250
3251 pm.pmap_targ = t;
3252 pm.pmap_func = func;
3253 pm.pmap_private = private;
3254
3255 if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3256 (void) Pmapping_iter_resolved(t->t_pshandle,
3257 pt_map_apply, &pm);
3258 } else {
3259 (void) Pmapping_iter(t->t_pshandle,
3260 pt_map_apply, &pm);
3261 }
3262 return (0);
3263 }
3264
3265 return (set_errno(EMDB_NOPROC));
3266 }
3267
3268 static int
pt_object_iter(mdb_tgt_t * t,mdb_tgt_map_f * func,void * private)3269 pt_object_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
3270 {
3271 pt_data_t *pt = t->t_data;
3272
3273 /*
3274 * If we have a libproc handle, we can just call Pobject_iter to
3275 * iterate over its list of load object information.
3276 */
3277 if (t->t_pshandle != NULL) {
3278 pt_maparg_t pm;
3279
3280 pm.pmap_targ = t;
3281 pm.pmap_func = func;
3282 pm.pmap_private = private;
3283
3284 if (PT_LIBPROC_RESOLVE(t->t_pshandle)) {
3285 (void) Pobject_iter_resolved(t->t_pshandle,
3286 pt_map_apply, &pm);
3287 } else {
3288 (void) Pobject_iter(t->t_pshandle,
3289 pt_map_apply, &pm);
3290 }
3291 return (0);
3292 }
3293
3294 /*
3295 * If we're examining an executable or other ELF file but we have no
3296 * libproc handle, fake up some information based on DT_NEEDED entries.
3297 */
3298 if (pt->p_dynsym != NULL && pt->p_file->gf_dyns != NULL &&
3299 pt->p_fio != NULL) {
3300 mdb_gelf_sect_t *gsp = pt->p_dynsym->gst_ssect;
3301 GElf_Dyn *dynp = pt->p_file->gf_dyns;
3302 mdb_map_t *mp = &pt->p_map;
3303 const char *s = IOP_NAME(pt->p_fio);
3304 size_t i;
3305
3306 (void) strncpy(mp->map_name, s, MDB_TGT_MAPSZ);
3307 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3308 mp->map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_X;
3309 mp->map_base = 0;
3310 mp->map_size = 0;
3311
3312 if (func(private, mp, s) != 0)
3313 return (0);
3314
3315 for (i = 0; i < pt->p_file->gf_ndyns; i++, dynp++) {
3316 if (dynp->d_tag == DT_NEEDED) {
3317 s = (char *)gsp->gs_data + dynp->d_un.d_val;
3318 (void) strncpy(mp->map_name, s, MDB_TGT_MAPSZ);
3319 mp->map_name[MDB_TGT_MAPSZ - 1] = '\0';
3320 if (func(private, mp, s) != 0)
3321 return (0);
3322 }
3323 }
3324
3325 return (0);
3326 }
3327
3328 return (set_errno(EMDB_NOPROC));
3329 }
3330
3331 static const mdb_map_t *
pt_addr_to_map(mdb_tgt_t * t,uintptr_t addr)3332 pt_addr_to_map(mdb_tgt_t *t, uintptr_t addr)
3333 {
3334 pt_data_t *pt = t->t_data;
3335 const prmap_t *pmp;
3336
3337 if (t->t_pshandle == NULL) {
3338 (void) set_errno(EMDB_NOPROC);
3339 return (NULL);
3340 }
3341
3342 if ((pmp = Paddr_to_map(t->t_pshandle, addr)) == NULL) {
3343 (void) set_errno(EMDB_NOMAP);
3344 return (NULL);
3345 }
3346
3347 return (pt_prmap_to_mdbmap(t, pmp, &pt->p_map));
3348 }
3349
3350 static const mdb_map_t *
pt_name_to_map(mdb_tgt_t * t,const char * object)3351 pt_name_to_map(mdb_tgt_t *t, const char *object)
3352 {
3353 pt_data_t *pt = t->t_data;
3354 const prmap_t *pmp;
3355 Lmid_t lmid;
3356
3357 if (t->t_pshandle == NULL) {
3358 (void) set_errno(EMDB_NOPROC);
3359 return (NULL);
3360 }
3361
3362 object = pt_resolve_lmid(object, &lmid);
3363
3364 if ((pmp = Plmid_to_map(t->t_pshandle, lmid, object)) == NULL) {
3365 (void) set_errno(EMDB_NOOBJ);
3366 return (NULL);
3367 }
3368
3369 return (pt_prmap_to_mdbmap(t, pmp, &pt->p_map));
3370 }
3371
3372 static ctf_file_t *
pt_addr_to_ctf(mdb_tgt_t * t,uintptr_t addr)3373 pt_addr_to_ctf(mdb_tgt_t *t, uintptr_t addr)
3374 {
3375 ctf_file_t *ret;
3376
3377 if (t->t_pshandle == NULL) {
3378 (void) set_errno(EMDB_NOPROC);
3379 return (NULL);
3380 }
3381
3382 if ((ret = Paddr_to_ctf(t->t_pshandle, addr)) == NULL) {
3383 (void) set_errno(EMDB_NOOBJ);
3384 return (NULL);
3385 }
3386
3387 return (ret);
3388 }
3389
3390 static ctf_file_t *
pt_name_to_ctf(mdb_tgt_t * t,const char * name)3391 pt_name_to_ctf(mdb_tgt_t *t, const char *name)
3392 {
3393 ctf_file_t *ret;
3394
3395 if (t->t_pshandle == NULL) {
3396 (void) set_errno(EMDB_NOPROC);
3397 return (NULL);
3398 }
3399
3400 if ((ret = Pname_to_ctf(t->t_pshandle, name)) == NULL) {
3401 (void) set_errno(EMDB_NOOBJ);
3402 return (NULL);
3403 }
3404
3405 return (ret);
3406 }
3407
3408 static int
pt_status(mdb_tgt_t * t,mdb_tgt_status_t * tsp)3409 pt_status(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
3410 {
3411 const pstatus_t *psp;
3412 prgregset_t gregs;
3413 int state;
3414
3415 bzero(tsp, sizeof (mdb_tgt_status_t));
3416
3417 if (t->t_pshandle == NULL) {
3418 tsp->st_state = MDB_TGT_IDLE;
3419 return (0);
3420 }
3421
3422 switch (state = Pstate(t->t_pshandle)) {
3423 case PS_RUN:
3424 tsp->st_state = MDB_TGT_RUNNING;
3425 break;
3426
3427 case PS_STOP:
3428 tsp->st_state = MDB_TGT_STOPPED;
3429 psp = Pstatus(t->t_pshandle);
3430
3431 tsp->st_tid = PTL_TID(t);
3432 if (PTL_GETREGS(t, tsp->st_tid, gregs) == 0)
3433 tsp->st_pc = gregs[R_PC];
3434
3435 if (psp->pr_flags & PR_ISTOP)
3436 tsp->st_flags |= MDB_TGT_ISTOP;
3437 if (psp->pr_flags & PR_DSTOP)
3438 tsp->st_flags |= MDB_TGT_DSTOP;
3439
3440 break;
3441
3442 case PS_LOST:
3443 tsp->st_state = MDB_TGT_LOST;
3444 break;
3445 case PS_UNDEAD:
3446 tsp->st_state = MDB_TGT_UNDEAD;
3447 break;
3448 case PS_DEAD:
3449 tsp->st_state = MDB_TGT_DEAD;
3450 break;
3451 case PS_IDLE:
3452 tsp->st_state = MDB_TGT_IDLE;
3453 break;
3454 default:
3455 fail("unknown libproc state (%d)\n", state);
3456 }
3457
3458 if (t->t_flags & MDB_TGT_F_BUSY)
3459 tsp->st_flags |= MDB_TGT_BUSY;
3460
3461 return (0);
3462 }
3463
3464 static void
pt_dupfd(const char * file,int oflags,mode_t mode,int dfd)3465 pt_dupfd(const char *file, int oflags, mode_t mode, int dfd)
3466 {
3467 int fd;
3468
3469 if ((fd = open(file, oflags, mode)) >= 0) {
3470 (void) fcntl(fd, F_DUP2FD, dfd);
3471 (void) close(fd);
3472 } else
3473 warn("failed to open %s as descriptor %d", file, dfd);
3474 }
3475
3476 /*
3477 * The Pcreate_callback() function interposes on the default, empty libproc
3478 * definition. It will be called following a fork of a new child process by
3479 * Pcreate() below, but before the exec of the new process image. We use this
3480 * callback to optionally redirect stdin and stdout and reset the dispositions
3481 * of SIGPIPE and SIGQUIT from SIG_IGN back to SIG_DFL.
3482 */
3483 /*ARGSUSED*/
3484 void
Pcreate_callback(struct ps_prochandle * P)3485 Pcreate_callback(struct ps_prochandle *P)
3486 {
3487 pt_data_t *pt = mdb.m_target->t_data;
3488
3489 if (pt->p_stdin != NULL)
3490 pt_dupfd(pt->p_stdin, O_RDWR, 0, STDIN_FILENO);
3491 if (pt->p_stdout != NULL)
3492 pt_dupfd(pt->p_stdout, O_CREAT | O_WRONLY, 0666, STDOUT_FILENO);
3493
3494 (void) mdb_signal_sethandler(SIGPIPE, MDB_SIG_DFL, NULL);
3495 (void) mdb_signal_sethandler(SIGQUIT, MDB_SIG_DFL, NULL);
3496 }
3497
3498 static int
pt_run(mdb_tgt_t * t,int argc,const mdb_arg_t * argv)3499 pt_run(mdb_tgt_t *t, int argc, const mdb_arg_t *argv)
3500 {
3501 pt_data_t *pt = t->t_data;
3502 struct ps_prochandle *P;
3503 char execname[MAXPATHLEN];
3504 const char **pargv;
3505 int pargc = 0;
3506 int i, perr;
3507 char **penv;
3508 mdb_var_t *v;
3509
3510 if (pt->p_aout_fio == NULL) {
3511 warn("run requires executable to be specified on "
3512 "command-line\n");
3513 return (set_errno(EMDB_TGT));
3514 }
3515
3516 pargv = mdb_alloc(sizeof (char *) * (argc + 2), UM_SLEEP);
3517 pargv[pargc++] = strbasename(IOP_NAME(pt->p_aout_fio));
3518
3519 for (i = 0; i < argc; i++) {
3520 if (argv[i].a_type != MDB_TYPE_STRING) {
3521 mdb_free(pargv, sizeof (char *) * (argc + 2));
3522 return (set_errno(EINVAL));
3523 }
3524 if (argv[i].a_un.a_str[0] == '<')
3525 pt->p_stdin = argv[i].a_un.a_str + 1;
3526 else if (argv[i].a_un.a_str[0] == '>')
3527 pt->p_stdout = argv[i].a_un.a_str + 1;
3528 else
3529 pargv[pargc++] = argv[i].a_un.a_str;
3530 }
3531 pargv[pargc] = NULL;
3532
3533 /*
3534 * Since Pcreate() uses execvp() and "." may not be present in $PATH,
3535 * we must manually prepend "./" when the executable is a simple name.
3536 */
3537 if (strchr(IOP_NAME(pt->p_aout_fio), '/') == NULL) {
3538 (void) snprintf(execname, sizeof (execname), "./%s",
3539 IOP_NAME(pt->p_aout_fio));
3540 } else {
3541 (void) snprintf(execname, sizeof (execname), "%s",
3542 IOP_NAME(pt->p_aout_fio));
3543 }
3544
3545 penv = mdb_alloc((mdb_nv_size(&pt->p_env)+ 1) * sizeof (char *),
3546 UM_SLEEP);
3547 for (mdb_nv_rewind(&pt->p_env), i = 0;
3548 (v = mdb_nv_advance(&pt->p_env)) != NULL; i++)
3549 penv[i] = mdb_nv_get_cookie(v);
3550 penv[i] = NULL;
3551
3552 P = Pxcreate(execname, (char **)pargv, penv, &perr, NULL, 0);
3553 mdb_free(pargv, sizeof (char *) * (argc + 2));
3554 pt->p_stdin = pt->p_stdout = NULL;
3555
3556 mdb_free(penv, i * sizeof (char *));
3557
3558 if (P == NULL) {
3559 warn("failed to create process: %s\n", Pcreate_error(perr));
3560 return (set_errno(EMDB_TGT));
3561 }
3562
3563 if (t->t_pshandle != NULL) {
3564 pt_pre_detach(t, TRUE);
3565 if (t->t_pshandle != pt->p_idlehandle)
3566 Prelease(t->t_pshandle, pt->p_rflags);
3567 }
3568
3569 (void) Punsetflags(P, PR_RLC); /* make sure run-on-last-close is off */
3570 (void) Psetflags(P, PR_KLC); /* kill on last close by debugger */
3571 pt->p_rflags = PRELEASE_KILL; /* kill on debugger Prelease */
3572 t->t_pshandle = P;
3573
3574 pt_post_attach(t);
3575 pt_activate_common(t);
3576 (void) mdb_tgt_status(t, &t->t_status);
3577 mdb.m_flags |= MDB_FL_VCREATE;
3578
3579 return (0);
3580 }
3581
3582 /*
3583 * Forward a signal to the victim process in order to force it to stop or die.
3584 * Refer to the comments above pt_setrun(), below, for more info.
3585 */
3586 /*ARGSUSED*/
3587 static void
pt_sigfwd(int sig,siginfo_t * sip,ucontext_t * ucp,mdb_tgt_t * t)3588 pt_sigfwd(int sig, siginfo_t *sip, ucontext_t *ucp, mdb_tgt_t *t)
3589 {
3590 struct ps_prochandle *P = t->t_pshandle;
3591 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
3592 pid_t pid = Pstatus(P)->pr_pid;
3593 long ctl[2];
3594
3595 if (getpgid(pid) != mdb.m_pgid) {
3596 mdb_dprintf(MDB_DBG_TGT, "fwd SIG#%d to %d\n", sig, (int)pid);
3597 (void) kill(pid, sig);
3598 }
3599
3600 if (Pwait(P, 1) == 0 && (psp->pr_flags & PR_STOPPED) &&
3601 psp->pr_why == PR_JOBCONTROL && Pdstop(P) == 0) {
3602 /*
3603 * If we're job control stopped and our DSTOP is pending, the
3604 * victim will never see our signal, so undo the kill() and
3605 * then send SIGCONT the victim to kick it out of the job
3606 * control stop and force our DSTOP to take effect.
3607 */
3608 if ((psp->pr_flags & PR_DSTOP) &&
3609 prismember(&Pstatus(P)->pr_sigpend, sig)) {
3610 ctl[0] = PCUNKILL;
3611 ctl[1] = sig;
3612 (void) write(Pctlfd(P), ctl, sizeof (ctl));
3613 }
3614
3615 mdb_dprintf(MDB_DBG_TGT, "fwd SIGCONT to %d\n", (int)pid);
3616 (void) kill(pid, SIGCONT);
3617 }
3618 }
3619
3620 /*
3621 * Common code for step and continue: if no victim process has been created,
3622 * call pt_run() to create one. Then set the victim running, clearing any
3623 * pending fault. One special case is that if the victim was previously
3624 * stopped on reception of SIGINT, we know that SIGINT was traced and the user
3625 * requested the victim to stop, so clear this signal before continuing.
3626 * For all other traced signals, the signal will be delivered on continue.
3627 *
3628 * Once the victim process is running, we wait for it to stop on an event of
3629 * interest. Although libproc provides the basic primitive to wait for the
3630 * victim, we must be careful in our handling of signals. We want to allow the
3631 * user to issue a SIGINT or SIGQUIT using the designated terminal control
3632 * character (typically ^C and ^\), and have these signals stop the target and
3633 * return control to the debugger if the signals are traced. There are three
3634 * cases to be considered in our implementation:
3635 *
3636 * (1) If the debugger and victim are in the same process group, both receive
3637 * the signal from the terminal driver. The debugger returns from Pwait() with
3638 * errno = EINTR, so we want to loop back and continue waiting until the victim
3639 * stops on receipt of its SIGINT or SIGQUIT.
3640 *
3641 * (2) If the debugger and victim are in different process groups, and the
3642 * victim is a member of the foreground process group, it will receive the
3643 * signal from the terminal driver and the debugger will not. As such, we
3644 * will remain blocked in Pwait() until the victim stops on its signal.
3645 *
3646 * (3) If the debugger and victim are in different process groups, and the
3647 * debugger is a member of the foreground process group, it will receive the
3648 * signal from the terminal driver, and the victim will not. The debugger
3649 * returns from Pwait() with errno = EINTR, so we need to forward the signal
3650 * to the victim process directly and then Pwait() again for it to stop.
3651 *
3652 * We can observe that all three cases are handled by simply calling Pwait()
3653 * repeatedly if it fails with EINTR, and forwarding SIGINT and SIGQUIT to
3654 * the victim if it is in a different process group, using pt_sigfwd() above.
3655 *
3656 * An additional complication is that the process may not be able to field
3657 * the signal if it is currently stopped by job control. In this case, we
3658 * also DSTOP the process, and then send it a SIGCONT to wake it up from
3659 * job control and force it to re-enter stop() under the control of /proc.
3660 *
3661 * Finally, we would like to allow the user to suspend the process using the
3662 * terminal suspend character (typically ^Z) if both are in the same session.
3663 * We again employ pt_sigfwd() to forward SIGTSTP to the victim, wait for it to
3664 * stop from job control, and then capture it using /proc. Once the process
3665 * has stopped, normal SIGTSTP processing is restored and the user can issue
3666 * another ^Z in order to suspend the debugger and return to the parent shell.
3667 */
3668 static int
pt_setrun(mdb_tgt_t * t,mdb_tgt_status_t * tsp,int flags)3669 pt_setrun(mdb_tgt_t *t, mdb_tgt_status_t *tsp, int flags)
3670 {
3671 struct ps_prochandle *P = t->t_pshandle;
3672 pt_data_t *pt = t->t_data;
3673 pid_t old_pgid = -1;
3674
3675 mdb_signal_f *intf, *quitf, *tstpf;
3676 const lwpstatus_t *psp;
3677 void *intd, *quitd, *tstpd;
3678
3679 int sig = pt->p_signal;
3680 int error = 0;
3681 int pgid = -1;
3682
3683 pt->p_signal = 0; /* clear pending signal */
3684
3685 if (P == NULL && pt_run(t, 0, NULL) == -1)
3686 return (-1); /* errno is set for us */
3687
3688 P = t->t_pshandle;
3689 psp = &Pstatus(P)->pr_lwp;
3690
3691 if (sig == 0 && psp->pr_why == PR_SIGNALLED && psp->pr_what == SIGINT)
3692 flags |= PRCSIG; /* clear pending SIGINT */
3693 else
3694 flags |= PRCFAULT; /* clear any pending fault (e.g. BPT) */
3695
3696 intf = mdb_signal_gethandler(SIGINT, &intd);
3697 quitf = mdb_signal_gethandler(SIGQUIT, &quitd);
3698 tstpf = mdb_signal_gethandler(SIGTSTP, &tstpd);
3699
3700 (void) mdb_signal_sethandler(SIGINT, (mdb_signal_f *)pt_sigfwd, t);
3701 (void) mdb_signal_sethandler(SIGQUIT, (mdb_signal_f *)pt_sigfwd, t);
3702 (void) mdb_signal_sethandler(SIGTSTP, (mdb_signal_f *)pt_sigfwd, t);
3703
3704 if (sig != 0 && Pstate(P) == PS_RUN &&
3705 kill(Pstatus(P)->pr_pid, sig) == -1) {
3706 error = errno;
3707 goto out;
3708 }
3709
3710 /*
3711 * If we attached to a job stopped background process in the same
3712 * session, make its pgid the foreground process group before running
3713 * it. Ignore SIGTTOU while doing this to avoid being suspended.
3714 */
3715 if (mdb.m_flags & MDB_FL_JOBCTL) {
3716 (void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_IGN, NULL);
3717 (void) IOP_CTL(mdb.m_term, TIOCGPGRP, &old_pgid);
3718 (void) IOP_CTL(mdb.m_term, TIOCSPGRP,
3719 (void *)&Pstatus(P)->pr_pgid);
3720 (void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_DFL, NULL);
3721 }
3722
3723 if (Pstate(P) != PS_RUN && Psetrun(P, sig, flags) == -1) {
3724 error = errno;
3725 goto out;
3726 }
3727
3728 /*
3729 * If the process is stopped on job control, resume its process group
3730 * by sending it a SIGCONT if we are in the same session. Otherwise
3731 * we have no choice but to wait for someone else to foreground it.
3732 */
3733 if (psp->pr_why == PR_JOBCONTROL) {
3734 if (mdb.m_flags & MDB_FL_JOBCTL)
3735 (void) kill(-Pstatus(P)->pr_pgid, SIGCONT);
3736 else if (mdb.m_term != NULL)
3737 warn("process is still suspended by job control ...\n");
3738 }
3739
3740 /*
3741 * Wait for the process to stop. As described above, we loop around if
3742 * we are interrupted (EINTR). If we lose control, attempt to re-open
3743 * the process, or call pt_exec() if that fails to handle a re-exec.
3744 * If the process dies (ENOENT) or Pwait() fails, break out of the loop.
3745 */
3746 while (Pwait(P, 0) == -1) {
3747 if (errno != EINTR) {
3748 if (Pstate(P) == PS_LOST) {
3749 if (Preopen(P) == 0)
3750 continue; /* Pwait() again */
3751 else
3752 pt_exec(t, 0, NULL);
3753 } else if (errno != ENOENT)
3754 warn("failed to wait for event");
3755 break;
3756 }
3757 }
3758
3759 /*
3760 * If we changed the foreground process group, restore the old pgid
3761 * while ignoring SIGTTOU so we are not accidentally suspended.
3762 */
3763 if (old_pgid != -1) {
3764 (void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_IGN, NULL);
3765 (void) IOP_CTL(mdb.m_term, TIOCSPGRP, &pgid);
3766 (void) mdb_signal_sethandler(SIGTTOU, MDB_SIG_DFL, NULL);
3767 }
3768
3769 /*
3770 * If we're now stopped on exit from a successful exec, release any
3771 * vfork parents and clean out their address space before returning
3772 * to tgt_continue() and perturbing the list of armed event specs.
3773 * If we're stopped for any other reason, just update the mappings.
3774 */
3775 switch (Pstate(P)) {
3776 case PS_STOP:
3777 if (psp->pr_why == PR_SYSEXIT && psp->pr_errno == 0 &&
3778 psp->pr_what == SYS_execve)
3779 pt_release_parents(t);
3780 else
3781 Pupdate_maps(P);
3782 break;
3783
3784 case PS_UNDEAD:
3785 case PS_LOST:
3786 pt_release_parents(t);
3787 break;
3788 }
3789
3790 out:
3791 (void) mdb_signal_sethandler(SIGINT, intf, intd);
3792 (void) mdb_signal_sethandler(SIGQUIT, quitf, quitd);
3793 (void) mdb_signal_sethandler(SIGTSTP, tstpf, tstpd);
3794 (void) pt_status(t, tsp);
3795
3796 return (error ? set_errno(error) : 0);
3797 }
3798
3799 static int
pt_step(mdb_tgt_t * t,mdb_tgt_status_t * tsp)3800 pt_step(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
3801 {
3802 return (pt_setrun(t, tsp, PRSTEP));
3803 }
3804
3805 static int
pt_continue(mdb_tgt_t * t,mdb_tgt_status_t * tsp)3806 pt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
3807 {
3808 return (pt_setrun(t, tsp, 0));
3809 }
3810
3811 static int
pt_signal(mdb_tgt_t * t,int sig)3812 pt_signal(mdb_tgt_t *t, int sig)
3813 {
3814 pt_data_t *pt = t->t_data;
3815
3816 if (sig > 0 && sig <= pt->p_maxsig) {
3817 pt->p_signal = sig; /* pending until next pt_setrun */
3818 return (0);
3819 }
3820
3821 return (set_errno(EMDB_BADSIGNUM));
3822 }
3823
3824 static int
pt_sysenter_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)3825 pt_sysenter_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3826 {
3827 struct ps_prochandle *P = t->t_pshandle;
3828
3829 if (P != NULL && Pstate(P) < PS_LOST) {
3830 sep->se_data = args; /* data is raw system call number */
3831 return (Psysentry(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3832 }
3833
3834 return (set_errno(EMDB_NOPROC));
3835 }
3836
3837 static void
pt_sysenter_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)3838 pt_sysenter_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3839 {
3840 (void) Psysentry(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
3841 }
3842
3843 /*ARGSUSED*/
3844 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)3845 pt_sysenter_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3846 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3847 {
3848 char name[32];
3849 int sysnum;
3850
3851 if (vep != NULL)
3852 sysnum = (intptr_t)vep->ve_args;
3853 else
3854 sysnum = (intptr_t)sep->se_data;
3855
3856 (void) proc_sysname(sysnum, name, sizeof (name));
3857 (void) mdb_iob_snprintf(buf, nbytes, "stop on entry to %s", name);
3858
3859 return (buf);
3860 }
3861
3862 /*ARGSUSED*/
3863 static int
pt_sysenter_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)3864 pt_sysenter_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3865 {
3866 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3867 int sysnum = (intptr_t)sep->se_data;
3868
3869 return (psp->pr_why == PR_SYSENTRY && psp->pr_what == sysnum);
3870 }
3871
3872 static const mdb_se_ops_t proc_sysenter_ops = {
3873 .se_ctor = pt_sysenter_ctor,
3874 .se_dtor = pt_sysenter_dtor,
3875 .se_info = pt_sysenter_info,
3876 .se_secmp = no_se_secmp,
3877 .se_vecmp = no_se_vecmp,
3878 .se_arm = no_se_arm,
3879 .se_disarm = no_se_disarm,
3880 .se_cont = no_se_cont,
3881 .se_match = pt_sysenter_match,
3882 };
3883
3884 static int
pt_sysexit_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)3885 pt_sysexit_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3886 {
3887 struct ps_prochandle *P = t->t_pshandle;
3888
3889 if (P != NULL && Pstate(P) < PS_LOST) {
3890 sep->se_data = args; /* data is raw system call number */
3891 return (Psysexit(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3892 }
3893
3894 return (set_errno(EMDB_NOPROC));
3895 }
3896
3897 static void
pt_sysexit_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)3898 pt_sysexit_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3899 {
3900 (void) Psysexit(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
3901 }
3902
3903 /*ARGSUSED*/
3904 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)3905 pt_sysexit_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3906 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3907 {
3908 char name[32];
3909 int sysnum;
3910
3911 if (vep != NULL)
3912 sysnum = (intptr_t)vep->ve_args;
3913 else
3914 sysnum = (intptr_t)sep->se_data;
3915
3916 (void) proc_sysname(sysnum, name, sizeof (name));
3917 (void) mdb_iob_snprintf(buf, nbytes, "stop on exit from %s", name);
3918
3919 return (buf);
3920 }
3921
3922 /*ARGSUSED*/
3923 static int
pt_sysexit_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)3924 pt_sysexit_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3925 {
3926 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3927 int sysnum = (intptr_t)sep->se_data;
3928
3929 return (psp->pr_why == PR_SYSEXIT && psp->pr_what == sysnum);
3930 }
3931
3932 static const mdb_se_ops_t proc_sysexit_ops = {
3933 .se_ctor = pt_sysexit_ctor,
3934 .se_dtor = pt_sysexit_dtor,
3935 .se_info = pt_sysexit_info,
3936 .se_secmp = no_se_secmp,
3937 .se_vecmp = no_se_vecmp,
3938 .se_arm = no_se_arm,
3939 .se_disarm = no_se_disarm,
3940 .se_cont = no_se_cont,
3941 .se_match = pt_sysexit_match,
3942 };
3943
3944 static int
pt_signal_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)3945 pt_signal_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
3946 {
3947 struct ps_prochandle *P = t->t_pshandle;
3948
3949 if (P != NULL && Pstate(P) < PS_LOST) {
3950 sep->se_data = args; /* data is raw signal number */
3951 return (Psignal(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
3952 }
3953
3954 return (set_errno(EMDB_NOPROC));
3955 }
3956
3957 static void
pt_signal_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)3958 pt_signal_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
3959 {
3960 (void) Psignal(t->t_pshandle, (intptr_t)sep->se_data, FALSE);
3961 }
3962
3963 /*ARGSUSED*/
3964 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)3965 pt_signal_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
3966 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
3967 {
3968 char name[SIG2STR_MAX];
3969 int signum;
3970
3971 if (vep != NULL)
3972 signum = (intptr_t)vep->ve_args;
3973 else
3974 signum = (intptr_t)sep->se_data;
3975
3976 (void) proc_signame(signum, name, sizeof (name));
3977 (void) mdb_iob_snprintf(buf, nbytes, "stop on %s", name);
3978
3979 return (buf);
3980 }
3981
3982 /*ARGSUSED*/
3983 static int
pt_signal_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)3984 pt_signal_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
3985 {
3986 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
3987 int signum = (intptr_t)sep->se_data;
3988
3989 return (psp->pr_why == PR_SIGNALLED && psp->pr_what == signum);
3990 }
3991
3992 static const mdb_se_ops_t proc_signal_ops = {
3993 .se_ctor = pt_signal_ctor,
3994 .se_dtor = pt_signal_dtor,
3995 .se_info = pt_signal_info,
3996 .se_secmp = no_se_secmp,
3997 .se_vecmp = no_se_vecmp,
3998 .se_arm = no_se_arm,
3999 .se_disarm = no_se_disarm,
4000 .se_cont = no_se_cont,
4001 .se_match = pt_signal_match,
4002 };
4003
4004 static int
pt_fault_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)4005 pt_fault_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4006 {
4007 struct ps_prochandle *P = t->t_pshandle;
4008
4009 if (P != NULL && Pstate(P) < PS_LOST) {
4010 sep->se_data = args; /* data is raw fault number */
4011 return (Pfault(P, (intptr_t)args, TRUE) < 0 ? -1 : 0);
4012 }
4013
4014 return (set_errno(EMDB_NOPROC));
4015 }
4016
4017 static void
pt_fault_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)4018 pt_fault_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
4019 {
4020 int fault = (intptr_t)sep->se_data;
4021
4022 if (fault != FLTBPT && fault != FLTTRACE && fault != FLTWATCH)
4023 (void) Pfault(t->t_pshandle, fault, FALSE);
4024 }
4025
4026 /*ARGSUSED*/
4027 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)4028 pt_fault_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
4029 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
4030 {
4031 char name[32];
4032 int fltnum;
4033
4034 if (vep != NULL)
4035 fltnum = (intptr_t)vep->ve_args;
4036 else
4037 fltnum = (intptr_t)sep->se_data;
4038
4039 (void) proc_fltname(fltnum, name, sizeof (name));
4040 (void) mdb_iob_snprintf(buf, nbytes, "stop on %s", name);
4041
4042 return (buf);
4043 }
4044
4045 /*ARGSUSED*/
4046 static int
pt_fault_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)4047 pt_fault_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4048 {
4049 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4050 int fltnum = (intptr_t)sep->se_data;
4051
4052 return (psp->pr_why == PR_FAULTED && psp->pr_what == fltnum);
4053 }
4054
4055 static const mdb_se_ops_t proc_fault_ops = {
4056 .se_ctor = pt_fault_ctor,
4057 .se_dtor = pt_fault_dtor,
4058 .se_info = pt_fault_info,
4059 .se_secmp = no_se_secmp,
4060 .se_vecmp = no_se_vecmp,
4061 .se_arm = no_se_arm,
4062 .se_disarm = no_se_disarm,
4063 .se_cont = no_se_cont,
4064 .se_match = pt_fault_match,
4065 };
4066
4067 /*
4068 * Callback for pt_ignore() dcmd above: for each VID, determine if it
4069 * corresponds to a vespec that traces the specified signal, and delete it.
4070 */
4071 /*ARGSUSED*/
4072 static int
pt_ignore_sig(mdb_tgt_t * t,void * sig,int vid,void * data)4073 pt_ignore_sig(mdb_tgt_t *t, void *sig, int vid, void *data)
4074 {
4075 mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, vid);
4076
4077 if (vep->ve_se->se_ops == &proc_signal_ops && vep->ve_args == sig)
4078 (void) mdb_tgt_vespec_delete(t, vid);
4079
4080 return (0);
4081 }
4082
4083 static int
pt_brkpt_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)4084 pt_brkpt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4085 {
4086 pt_data_t *pt = t->t_data;
4087 pt_bparg_t *pta = args;
4088 pt_brkpt_t *ptb;
4089 GElf_Sym s;
4090
4091 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) >= PS_LOST)
4092 return (set_errno(EMDB_NOPROC));
4093
4094 if (pta->pta_symbol != NULL) {
4095 if (!pt->p_rtld_finished &&
4096 strchr(pta->pta_symbol, '`') == NULL)
4097 return (set_errno(EMDB_NOSYM));
4098 if (mdb_tgt_lookup_by_scope(t, pta->pta_symbol, &s,
4099 NULL) == -1) {
4100 if (errno != EMDB_NOOBJ && !(errno == EMDB_NOSYM &&
4101 (!(mdb.m_flags & MDB_FL_BPTNOSYMSTOP) ||
4102 !pt->p_rtld_finished))) {
4103 warn("breakpoint %s activation failed",
4104 pta->pta_symbol);
4105 }
4106 return (-1); /* errno is set for us */
4107 }
4108
4109 pta->pta_addr = (uintptr_t)s.st_value;
4110 }
4111
4112 #ifdef __sparc
4113 if (pta->pta_addr & 3)
4114 return (set_errno(EMDB_BPALIGN));
4115 #endif
4116
4117 if (Paddr_to_map(t->t_pshandle, pta->pta_addr) == NULL)
4118 return (set_errno(EMDB_NOMAP));
4119
4120 ptb = mdb_alloc(sizeof (pt_brkpt_t), UM_SLEEP);
4121 ptb->ptb_addr = pta->pta_addr;
4122 ptb->ptb_instr = 0;
4123 sep->se_data = ptb;
4124
4125 return (0);
4126 }
4127
4128 /*ARGSUSED*/
4129 static void
pt_brkpt_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)4130 pt_brkpt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
4131 {
4132 mdb_free(sep->se_data, sizeof (pt_brkpt_t));
4133 }
4134
4135 /*ARGSUSED*/
4136 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)4137 pt_brkpt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
4138 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
4139 {
4140 uintptr_t addr = 0;
4141
4142 if (vep != NULL) {
4143 pt_bparg_t *pta = vep->ve_args;
4144
4145 if (pta->pta_symbol != NULL) {
4146 (void) mdb_iob_snprintf(buf, nbytes, "stop at %s",
4147 pta->pta_symbol);
4148 } else {
4149 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a",
4150 pta->pta_addr);
4151 addr = pta->pta_addr;
4152 }
4153
4154 } else {
4155 addr = ((pt_brkpt_t *)sep->se_data)->ptb_addr;
4156 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a", addr);
4157 }
4158
4159 sp->spec_base = addr;
4160 sp->spec_size = sizeof (instr_t);
4161
4162 return (buf);
4163 }
4164
4165 static int
pt_brkpt_secmp(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)4166 pt_brkpt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4167 {
4168 pt_brkpt_t *ptb = sep->se_data;
4169 pt_bparg_t *pta = args;
4170 GElf_Sym sym;
4171
4172 if (pta->pta_symbol != NULL) {
4173 return (mdb_tgt_lookup_by_scope(t, pta->pta_symbol,
4174 &sym, NULL) == 0 && sym.st_value == ptb->ptb_addr);
4175 }
4176
4177 return (pta->pta_addr == ptb->ptb_addr);
4178 }
4179
4180 /*ARGSUSED*/
4181 static int
pt_brkpt_vecmp(mdb_tgt_t * t,mdb_vespec_t * vep,void * args)4182 pt_brkpt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
4183 {
4184 pt_bparg_t *pta1 = vep->ve_args;
4185 pt_bparg_t *pta2 = args;
4186
4187 if (pta1->pta_symbol != NULL && pta2->pta_symbol != NULL)
4188 return (strcmp(pta1->pta_symbol, pta2->pta_symbol) == 0);
4189
4190 if (pta1->pta_symbol == NULL && pta2->pta_symbol == NULL)
4191 return (pta1->pta_addr == pta2->pta_addr);
4192
4193 return (0); /* fail if one is symbolic, other is an explicit address */
4194 }
4195
4196 static int
pt_brkpt_arm(mdb_tgt_t * t,mdb_sespec_t * sep)4197 pt_brkpt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
4198 {
4199 pt_brkpt_t *ptb = sep->se_data;
4200 return (Psetbkpt(t->t_pshandle, ptb->ptb_addr, &ptb->ptb_instr));
4201 }
4202
4203 /*
4204 * In order to disarm a breakpoint, we replace the trap instruction at ptb_addr
4205 * with the saved instruction. However, if we have stopped after a successful
4206 * exec(2), we do not want to restore ptb_instr because the address space has
4207 * now been replaced with the text of a different executable, and so restoring
4208 * the saved instruction would be incorrect. The exec itself has effectively
4209 * removed all breakpoint trap instructions for us, so we can just return.
4210 */
4211 static int
pt_brkpt_disarm(mdb_tgt_t * t,mdb_sespec_t * sep)4212 pt_brkpt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
4213 {
4214 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4215 pt_brkpt_t *ptb = sep->se_data;
4216
4217 if (psp->pr_why == PR_SYSEXIT && psp->pr_errno == 0 &&
4218 psp->pr_what == SYS_execve)
4219 return (0); /* do not restore saved instruction */
4220
4221 return (Pdelbkpt(t->t_pshandle, ptb->ptb_addr, ptb->ptb_instr));
4222 }
4223
4224 /*
4225 * Determine whether the specified sespec is an armed watchpoint that overlaps
4226 * with the given breakpoint and has the given flags set. We use this to find
4227 * conflicts with breakpoints, below.
4228 */
4229 static int
pt_wp_overlap(mdb_sespec_t * sep,pt_brkpt_t * ptb,int flags)4230 pt_wp_overlap(mdb_sespec_t *sep, pt_brkpt_t *ptb, int flags)
4231 {
4232 const prwatch_t *wp = sep->se_data;
4233
4234 return (sep->se_state == MDB_TGT_SPEC_ARMED &&
4235 sep->se_ops == &proc_wapt_ops && (wp->pr_wflags & flags) &&
4236 ptb->ptb_addr - wp->pr_vaddr < wp->pr_size);
4237 }
4238
4239 /*
4240 * We step over breakpoints using Pxecbkpt() in libproc. If a conflicting
4241 * watchpoint is present, we must temporarily remove it before stepping over
4242 * the breakpoint so we do not immediately re-trigger the watchpoint. We know
4243 * the watchpoint has already triggered on our trap instruction as part of
4244 * fetching it. Before we return, we must re-install any disabled watchpoints.
4245 */
4246 static int
pt_brkpt_cont(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)4247 pt_brkpt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4248 {
4249 pt_brkpt_t *ptb = sep->se_data;
4250 int status = -1;
4251 int error;
4252 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4253
4254 /*
4255 * If the PC no longer matches our original address, then the user has
4256 * changed it while we have been stopped. In this case, it no longer
4257 * makes any sense to continue over this breakpoint. We return as if we
4258 * continued normally.
4259 */
4260 if ((uintptr_t)psp->pr_info.si_addr != psp->pr_reg[R_PC])
4261 return (pt_status(t, tsp));
4262
4263 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
4264 if (pt_wp_overlap(sep, ptb, WA_EXEC))
4265 (void) Pdelwapt(t->t_pshandle, sep->se_data);
4266 }
4267
4268 if (Pxecbkpt(t->t_pshandle, ptb->ptb_instr) == 0 &&
4269 Pdelbkpt(t->t_pshandle, ptb->ptb_addr, ptb->ptb_instr) == 0)
4270 status = pt_status(t, tsp);
4271
4272 error = errno; /* save errno from Pxecbkpt, Pdelbkpt, or pt_status */
4273
4274 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
4275 if (pt_wp_overlap(sep, ptb, WA_EXEC) &&
4276 Psetwapt(t->t_pshandle, sep->se_data) == -1) {
4277 sep->se_state = MDB_TGT_SPEC_ERROR;
4278 sep->se_errno = errno;
4279 }
4280 }
4281
4282 (void) set_errno(error);
4283 return (status);
4284 }
4285
4286 /*ARGSUSED*/
4287 static int
pt_brkpt_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)4288 pt_brkpt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4289 {
4290 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4291 pt_brkpt_t *ptb = sep->se_data;
4292
4293 return (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT &&
4294 psp->pr_reg[R_PC] == ptb->ptb_addr);
4295 }
4296
4297 static const mdb_se_ops_t proc_brkpt_ops = {
4298 .se_ctor = pt_brkpt_ctor,
4299 .se_dtor = pt_brkpt_dtor,
4300 .se_info = pt_brkpt_info,
4301 .se_secmp = pt_brkpt_secmp,
4302 .se_vecmp = pt_brkpt_vecmp,
4303 .se_arm = pt_brkpt_arm,
4304 .se_disarm = pt_brkpt_disarm,
4305 .se_cont = pt_brkpt_cont,
4306 .se_match = pt_brkpt_match,
4307 };
4308
4309 static int
pt_wapt_ctor(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)4310 pt_wapt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4311 {
4312 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) >= PS_LOST)
4313 return (set_errno(EMDB_NOPROC));
4314
4315 sep->se_data = mdb_alloc(sizeof (prwatch_t), UM_SLEEP);
4316 bcopy(args, sep->se_data, sizeof (prwatch_t));
4317 return (0);
4318 }
4319
4320 /*ARGSUSED*/
4321 static void
pt_wapt_dtor(mdb_tgt_t * t,mdb_sespec_t * sep)4322 pt_wapt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
4323 {
4324 mdb_free(sep->se_data, sizeof (prwatch_t));
4325 }
4326
4327 /*ARGSUSED*/
4328 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)4329 pt_wapt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
4330 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
4331 {
4332 prwatch_t *wp = vep != NULL ? vep->ve_args : sep->se_data;
4333 char desc[24];
4334
4335 ASSERT(wp->pr_wflags != 0);
4336 desc[0] = '\0';
4337
4338 switch (wp->pr_wflags) {
4339 case WA_READ:
4340 (void) strcat(desc, "/read");
4341 break;
4342 case WA_WRITE:
4343 (void) strcat(desc, "/write");
4344 break;
4345 case WA_EXEC:
4346 (void) strcat(desc, "/exec");
4347 break;
4348 default:
4349 if (wp->pr_wflags & WA_READ)
4350 (void) strcat(desc, "/r");
4351 if (wp->pr_wflags & WA_WRITE)
4352 (void) strcat(desc, "/w");
4353 if (wp->pr_wflags & WA_EXEC)
4354 (void) strcat(desc, "/x");
4355 }
4356
4357 (void) mdb_iob_snprintf(buf, nbytes, "stop on %s of [%la, %la)",
4358 desc + 1, wp->pr_vaddr, wp->pr_vaddr + wp->pr_size);
4359
4360 sp->spec_base = wp->pr_vaddr;
4361 sp->spec_size = wp->pr_size;
4362
4363 return (buf);
4364 }
4365
4366 /*ARGSUSED*/
4367 static int
pt_wapt_secmp(mdb_tgt_t * t,mdb_sespec_t * sep,void * args)4368 pt_wapt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
4369 {
4370 prwatch_t *wp1 = sep->se_data;
4371 prwatch_t *wp2 = args;
4372
4373 return (wp1->pr_vaddr == wp2->pr_vaddr &&
4374 wp1->pr_size == wp2->pr_size && wp1->pr_wflags == wp2->pr_wflags);
4375 }
4376
4377 /*ARGSUSED*/
4378 static int
pt_wapt_vecmp(mdb_tgt_t * t,mdb_vespec_t * vep,void * args)4379 pt_wapt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
4380 {
4381 prwatch_t *wp1 = vep->ve_args;
4382 prwatch_t *wp2 = args;
4383
4384 return (wp1->pr_vaddr == wp2->pr_vaddr &&
4385 wp1->pr_size == wp2->pr_size && wp1->pr_wflags == wp2->pr_wflags);
4386 }
4387
4388 static int
pt_wapt_arm(mdb_tgt_t * t,mdb_sespec_t * sep)4389 pt_wapt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
4390 {
4391 return (Psetwapt(t->t_pshandle, sep->se_data));
4392 }
4393
4394 static int
pt_wapt_disarm(mdb_tgt_t * t,mdb_sespec_t * sep)4395 pt_wapt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
4396 {
4397 return (Pdelwapt(t->t_pshandle, sep->se_data));
4398 }
4399
4400 /*
4401 * Determine whether the specified sespec is an armed breakpoint at the
4402 * given %pc. We use this to find conflicts with watchpoints below.
4403 */
4404 static int
pt_bp_overlap(mdb_sespec_t * sep,uintptr_t pc)4405 pt_bp_overlap(mdb_sespec_t *sep, uintptr_t pc)
4406 {
4407 pt_brkpt_t *ptb = sep->se_data;
4408
4409 return (sep->se_state == MDB_TGT_SPEC_ARMED &&
4410 sep->se_ops == &proc_brkpt_ops && ptb->ptb_addr == pc);
4411 }
4412
4413 /*
4414 * We step over watchpoints using Pxecwapt() in libproc. If a conflicting
4415 * breakpoint is present, we must temporarily disarm it before stepping
4416 * over the watchpoint so we do not immediately re-trigger the breakpoint.
4417 * This is similar to the case handled in pt_brkpt_cont(), above.
4418 */
4419 static int
pt_wapt_cont(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)4420 pt_wapt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4421 {
4422 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4423 mdb_sespec_t *bep = NULL;
4424 int status = -1;
4425 int error;
4426
4427 /*
4428 * If the PC no longer matches our original address, then the user has
4429 * changed it while we have been stopped. In this case, it no longer
4430 * makes any sense to continue over this instruction. We return as if
4431 * we continued normally.
4432 */
4433 if ((uintptr_t)psp->pr_info.si_pc != psp->pr_reg[R_PC])
4434 return (pt_status(t, tsp));
4435
4436 if (psp->pr_info.si_code != TRAP_XWATCH) {
4437 for (bep = mdb_list_next(&t->t_active); bep != NULL;
4438 bep = mdb_list_next(bep)) {
4439 if (pt_bp_overlap(bep, psp->pr_reg[R_PC])) {
4440 (void) bep->se_ops->se_disarm(t, bep);
4441 bep->se_state = MDB_TGT_SPEC_ACTIVE;
4442 break;
4443 }
4444 }
4445 }
4446
4447 if (Pxecwapt(t->t_pshandle, sep->se_data) == 0)
4448 status = pt_status(t, tsp);
4449
4450 error = errno; /* save errno from Pxecwapt or pt_status */
4451
4452 if (bep != NULL)
4453 mdb_tgt_sespec_arm_one(t, bep);
4454
4455 (void) set_errno(error);
4456 return (status);
4457 }
4458
4459 /*ARGSUSED*/
4460 static int
pt_wapt_match(mdb_tgt_t * t,mdb_sespec_t * sep,mdb_tgt_status_t * tsp)4461 pt_wapt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
4462 {
4463 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
4464 prwatch_t *wp = sep->se_data;
4465
4466 return (psp->pr_why == PR_FAULTED && psp->pr_what == FLTWATCH &&
4467 (uintptr_t)psp->pr_info.si_addr - wp->pr_vaddr < wp->pr_size);
4468 }
4469
4470 static const mdb_se_ops_t proc_wapt_ops = {
4471 .se_ctor = pt_wapt_ctor,
4472 .se_dtor = pt_wapt_dtor,
4473 .se_info = pt_wapt_info,
4474 .se_secmp = pt_wapt_secmp,
4475 .se_vecmp = pt_wapt_vecmp,
4476 .se_arm = pt_wapt_arm,
4477 .se_disarm = pt_wapt_disarm,
4478 .se_cont = pt_wapt_cont,
4479 .se_match = pt_wapt_match,
4480 };
4481
4482 static void
pt_bparg_dtor(mdb_vespec_t * vep)4483 pt_bparg_dtor(mdb_vespec_t *vep)
4484 {
4485 pt_bparg_t *pta = vep->ve_args;
4486
4487 if (pta->pta_symbol != NULL)
4488 strfree(pta->pta_symbol);
4489
4490 mdb_free(pta, sizeof (pt_bparg_t));
4491 }
4492
4493 static int
pt_add_vbrkpt(mdb_tgt_t * t,uintptr_t addr,int spec_flags,mdb_tgt_se_f * func,void * data)4494 pt_add_vbrkpt(mdb_tgt_t *t, uintptr_t addr,
4495 int spec_flags, mdb_tgt_se_f *func, void *data)
4496 {
4497 pt_bparg_t *pta = mdb_alloc(sizeof (pt_bparg_t), UM_SLEEP);
4498
4499 pta->pta_symbol = NULL;
4500 pta->pta_addr = addr;
4501
4502 return (mdb_tgt_vespec_insert(t, &proc_brkpt_ops, spec_flags,
4503 func, data, pta, pt_bparg_dtor));
4504 }
4505
4506 static int
pt_add_sbrkpt(mdb_tgt_t * t,const char * sym,int spec_flags,mdb_tgt_se_f * func,void * data)4507 pt_add_sbrkpt(mdb_tgt_t *t, const char *sym,
4508 int spec_flags, mdb_tgt_se_f *func, void *data)
4509 {
4510 pt_bparg_t *pta;
4511
4512 if (sym[0] == '`') {
4513 (void) set_errno(EMDB_NOOBJ);
4514 return (0);
4515 }
4516
4517 if (sym[strlen(sym) - 1] == '`') {
4518 (void) set_errno(EMDB_NOSYM);
4519 return (0);
4520 }
4521
4522 pta = mdb_alloc(sizeof (pt_bparg_t), UM_SLEEP);
4523 pta->pta_symbol = strdup(sym);
4524 pta->pta_addr = 0;
4525
4526 return (mdb_tgt_vespec_insert(t, &proc_brkpt_ops, spec_flags,
4527 func, data, pta, pt_bparg_dtor));
4528 }
4529
4530 static int
pt_wparg_overlap(const prwatch_t * wp1,const prwatch_t * wp2)4531 pt_wparg_overlap(const prwatch_t *wp1, const prwatch_t *wp2)
4532 {
4533 if (wp2->pr_vaddr + wp2->pr_size <= wp1->pr_vaddr)
4534 return (0); /* no range overlap */
4535
4536 if (wp1->pr_vaddr + wp1->pr_size <= wp2->pr_vaddr)
4537 return (0); /* no range overlap */
4538
4539 return (wp1->pr_vaddr != wp2->pr_vaddr ||
4540 wp1->pr_size != wp2->pr_size || wp1->pr_wflags != wp2->pr_wflags);
4541 }
4542
4543 static void
pt_wparg_dtor(mdb_vespec_t * vep)4544 pt_wparg_dtor(mdb_vespec_t *vep)
4545 {
4546 mdb_free(vep->ve_args, sizeof (prwatch_t));
4547 }
4548
4549 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)4550 pt_add_vwapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags,
4551 int spec_flags, mdb_tgt_se_f *func, void *data)
4552 {
4553 prwatch_t *wp = mdb_alloc(sizeof (prwatch_t), UM_SLEEP);
4554 mdb_sespec_t *sep;
4555
4556 wp->pr_vaddr = addr;
4557 wp->pr_size = len;
4558 wp->pr_wflags = 0;
4559
4560 if (wflags & MDB_TGT_WA_R)
4561 wp->pr_wflags |= WA_READ;
4562 if (wflags & MDB_TGT_WA_W)
4563 wp->pr_wflags |= WA_WRITE;
4564 if (wflags & MDB_TGT_WA_X)
4565 wp->pr_wflags |= WA_EXEC;
4566
4567 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
4568 if (sep->se_ops == &proc_wapt_ops &&
4569 mdb_list_next(&sep->se_velist) != NULL &&
4570 pt_wparg_overlap(wp, sep->se_data))
4571 goto dup;
4572 }
4573
4574 for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
4575 if (sep->se_ops == &proc_wapt_ops && pt_wparg_overlap(wp,
4576 ((mdb_vespec_t *)mdb_list_next(&sep->se_velist))->ve_args))
4577 goto dup;
4578 }
4579
4580 return (mdb_tgt_vespec_insert(t, &proc_wapt_ops, spec_flags,
4581 func, data, wp, pt_wparg_dtor));
4582
4583 dup:
4584 mdb_free(wp, sizeof (prwatch_t));
4585 (void) set_errno(EMDB_WPDUP);
4586 return (0);
4587 }
4588
4589 static int
pt_add_sysenter(mdb_tgt_t * t,int sysnum,int spec_flags,mdb_tgt_se_f * func,void * data)4590 pt_add_sysenter(mdb_tgt_t *t, int sysnum,
4591 int spec_flags, mdb_tgt_se_f *func, void *data)
4592 {
4593 if (sysnum <= 0 || sysnum > PRMAXSYS) {
4594 (void) set_errno(EMDB_BADSYSNUM);
4595 return (0);
4596 }
4597
4598 return (mdb_tgt_vespec_insert(t, &proc_sysenter_ops, spec_flags,
4599 func, data, (void *)(uintptr_t)sysnum, no_ve_dtor));
4600 }
4601
4602 static int
pt_add_sysexit(mdb_tgt_t * t,int sysnum,int spec_flags,mdb_tgt_se_f * func,void * data)4603 pt_add_sysexit(mdb_tgt_t *t, int sysnum,
4604 int spec_flags, mdb_tgt_se_f *func, void *data)
4605 {
4606 if (sysnum <= 0 || sysnum > PRMAXSYS) {
4607 (void) set_errno(EMDB_BADSYSNUM);
4608 return (0);
4609 }
4610
4611 return (mdb_tgt_vespec_insert(t, &proc_sysexit_ops, spec_flags,
4612 func, data, (void *)(uintptr_t)sysnum, no_ve_dtor));
4613 }
4614
4615 static int
pt_add_signal(mdb_tgt_t * t,int signum,int spec_flags,mdb_tgt_se_f * func,void * data)4616 pt_add_signal(mdb_tgt_t *t, int signum,
4617 int spec_flags, mdb_tgt_se_f *func, void *data)
4618 {
4619 pt_data_t *pt = t->t_data;
4620
4621 if (signum <= 0 || signum > pt->p_maxsig) {
4622 (void) set_errno(EMDB_BADSIGNUM);
4623 return (0);
4624 }
4625
4626 return (mdb_tgt_vespec_insert(t, &proc_signal_ops, spec_flags,
4627 func, data, (void *)(uintptr_t)signum, no_ve_dtor));
4628 }
4629
4630 static int
pt_add_fault(mdb_tgt_t * t,int fltnum,int spec_flags,mdb_tgt_se_f * func,void * data)4631 pt_add_fault(mdb_tgt_t *t, int fltnum,
4632 int spec_flags, mdb_tgt_se_f *func, void *data)
4633 {
4634 if (fltnum <= 0 || fltnum > PRMAXFAULT) {
4635 (void) set_errno(EMDB_BADFLTNUM);
4636 return (0);
4637 }
4638
4639 return (mdb_tgt_vespec_insert(t, &proc_fault_ops, spec_flags,
4640 func, data, (void *)(uintptr_t)fltnum, no_ve_dtor));
4641 }
4642
4643 static int
pt_getareg(mdb_tgt_t * t,mdb_tgt_tid_t tid,const char * rname,mdb_tgt_reg_t * rp)4644 pt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid,
4645 const char *rname, mdb_tgt_reg_t *rp)
4646 {
4647 pt_data_t *pt = t->t_data;
4648 prgregset_t grs;
4649 mdb_var_t *v;
4650
4651 if (t->t_pshandle == NULL)
4652 return (set_errno(EMDB_NOPROC));
4653
4654 if ((v = mdb_nv_lookup(&pt->p_regs, rname)) != NULL) {
4655 uintmax_t rd_nval = mdb_nv_get_value(v);
4656 ushort_t rd_num = MDB_TGT_R_NUM(rd_nval);
4657 ushort_t rd_flags = MDB_TGT_R_FLAGS(rd_nval);
4658
4659 if (!MDB_TGT_R_IS_FP(rd_flags)) {
4660 mdb_tgt_reg_t r = 0;
4661
4662 #if defined(__sparc) && defined(_ILP32)
4663 /*
4664 * If we are debugging on 32-bit SPARC, the globals and
4665 * outs can have 32 upper bits hiding in the xregs.
4666 */
4667 /* gcc doesn't like >= R_G0 because R_G0 == 0 */
4668 int is_g = (rd_num == R_G0 ||
4669 rd_num >= R_G1 && rd_num <= R_G7);
4670 int is_o = (rd_num >= R_O0 && rd_num <= R_O7);
4671 prxregset_t xrs;
4672
4673 if (is_g && PTL_GETXREGS(t, tid, &xrs) == 0 &&
4674 xrs.pr_type == XR_TYPE_V8P) {
4675 r |= (uint64_t)xrs.pr_un.pr_v8p.pr_xg[
4676 rd_num - R_G0 + XR_G0] << 32;
4677 }
4678
4679 if (is_o && PTL_GETXREGS(t, tid, &xrs) == 0 &&
4680 xrs.pr_type == XR_TYPE_V8P) {
4681 r |= (uint64_t)xrs.pr_un.pr_v8p.pr_xo[
4682 rd_num - R_O0 + XR_O0] << 32;
4683 }
4684 #endif /* __sparc && _ILP32 */
4685
4686 /*
4687 * Avoid sign-extension by casting: recall that procfs
4688 * defines prgreg_t as a long or int and our native
4689 * register handling uses uint64_t's.
4690 */
4691 if (PTL_GETREGS(t, tid, grs) == 0) {
4692 *rp = r | (ulong_t)grs[rd_num];
4693 if (rd_flags & MDB_TGT_R_32)
4694 *rp &= 0xffffffffULL;
4695 else if (rd_flags & MDB_TGT_R_16)
4696 *rp &= 0xffffULL;
4697 else if (rd_flags & MDB_TGT_R_8H)
4698 *rp = (*rp & 0xff00ULL) >> 8;
4699 else if (rd_flags & MDB_TGT_R_8L)
4700 *rp &= 0xffULL;
4701 return (0);
4702 }
4703 return (-1);
4704 } else
4705 return (pt_getfpreg(t, tid, rd_num, rd_flags, rp));
4706 }
4707
4708 return (set_errno(EMDB_BADREG));
4709 }
4710
4711 static int
pt_putareg(mdb_tgt_t * t,mdb_tgt_tid_t tid,const char * rname,mdb_tgt_reg_t r)4712 pt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r)
4713 {
4714 pt_data_t *pt = t->t_data;
4715 prgregset_t grs;
4716 mdb_var_t *v;
4717
4718 if (t->t_pshandle == NULL)
4719 return (set_errno(EMDB_NOPROC));
4720
4721 if ((v = mdb_nv_lookup(&pt->p_regs, rname)) != NULL) {
4722 uintmax_t rd_nval = mdb_nv_get_value(v);
4723 ushort_t rd_num = MDB_TGT_R_NUM(rd_nval);
4724 ushort_t rd_flags = MDB_TGT_R_FLAGS(rd_nval);
4725
4726 if (!MDB_TGT_R_IS_FP(rd_flags)) {
4727
4728 if (rd_flags & MDB_TGT_R_32)
4729 r &= 0xffffffffULL;
4730 else if (rd_flags & MDB_TGT_R_16)
4731 r &= 0xffffULL;
4732 else if (rd_flags & MDB_TGT_R_8H)
4733 r = (r & 0xffULL) << 8;
4734 else if (rd_flags & MDB_TGT_R_8L)
4735 r &= 0xffULL;
4736
4737 if (PTL_GETREGS(t, tid, grs) == 0) {
4738 grs[rd_num] = (prgreg_t)r;
4739 return (PTL_SETREGS(t, tid, grs));
4740 }
4741 return (-1);
4742 } else
4743 return (pt_putfpreg(t, tid, rd_num, rd_flags, r));
4744 }
4745
4746 return (set_errno(EMDB_BADREG));
4747 }
4748
4749 static int
pt_stack_call(pt_stkarg_t * psp,const prgregset_t grs,uint_t argc,long * argv)4750 pt_stack_call(pt_stkarg_t *psp, const prgregset_t grs, uint_t argc, long *argv)
4751 {
4752 psp->pstk_gotpc |= (grs[R_PC] != 0);
4753
4754 if (!psp->pstk_gotpc)
4755 return (0); /* skip initial zeroed frames */
4756
4757 return (psp->pstk_func(psp->pstk_private, grs[R_PC],
4758 argc, argv, (const struct mdb_tgt_gregset *)grs));
4759 }
4760
4761 static int
pt_stack_iter(mdb_tgt_t * t,const mdb_tgt_gregset_t * gsp,mdb_tgt_stack_f * func,void * arg)4762 pt_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
4763 mdb_tgt_stack_f *func, void *arg)
4764 {
4765 if (t->t_pshandle != NULL) {
4766 pt_stkarg_t pstk;
4767
4768 pstk.pstk_func = func;
4769 pstk.pstk_private = arg;
4770 pstk.pstk_gotpc = FALSE;
4771
4772 (void) Pstack_iter(t->t_pshandle, gsp->gregs,
4773 (proc_stack_f *)pt_stack_call, &pstk);
4774
4775 return (0);
4776 }
4777
4778 return (set_errno(EMDB_NOPROC));
4779 }
4780
4781 static int
pt_auxv(mdb_tgt_t * t,const auxv_t ** auxvp)4782 pt_auxv(mdb_tgt_t *t, const auxv_t **auxvp)
4783 {
4784 if (t->t_pshandle != NULL) {
4785 *auxvp = Pgetauxvec(t->t_pshandle);
4786 return (0);
4787 }
4788
4789 return (set_errno(EMDB_NOPROC));
4790 }
4791
4792
4793 static const mdb_tgt_ops_t proc_ops = {
4794 .t_setflags = pt_setflags,
4795 .t_setcontext = (int (*)())(uintptr_t)mdb_tgt_notsup,
4796 .t_activate = pt_activate,
4797 .t_deactivate = pt_deactivate,
4798 .t_periodic = pt_periodic,
4799 .t_destroy = pt_destroy,
4800 .t_name = pt_name,
4801 .t_isa = (const char *(*)())mdb_conf_isa,
4802 .t_platform = pt_platform,
4803 .t_uname = pt_uname,
4804 .t_dmodel = pt_dmodel,
4805 .t_aread = (ssize_t (*)())mdb_tgt_notsup,
4806 .t_awrite = (ssize_t (*)())mdb_tgt_notsup,
4807 .t_vread = pt_vread,
4808 .t_vwrite = pt_vwrite,
4809 .t_pread = (ssize_t (*)())mdb_tgt_notsup,
4810 .t_pwrite = (ssize_t (*)())mdb_tgt_notsup,
4811 .t_fread = pt_fread,
4812 .t_fwrite = pt_fwrite,
4813 .t_ioread = (ssize_t (*)())mdb_tgt_notsup,
4814 .t_iowrite = (ssize_t (*)())mdb_tgt_notsup,
4815 .t_vtop = (int (*)())(uintptr_t)mdb_tgt_notsup,
4816 .t_lookup_by_name = pt_lookup_by_name,
4817 .t_lookup_by_addr = pt_lookup_by_addr,
4818 .t_symbol_iter = pt_symbol_iter,
4819 .t_mapping_iter = pt_mapping_iter,
4820 .t_object_iter = pt_object_iter,
4821 .t_addr_to_map = pt_addr_to_map,
4822 .t_name_to_map = pt_name_to_map,
4823 .t_addr_to_ctf = pt_addr_to_ctf,
4824 .t_name_to_ctf = pt_name_to_ctf,
4825 .t_status = pt_status,
4826 .t_run = pt_run,
4827 .t_step = pt_step,
4828 .t_step_out = pt_step_out,
4829 .t_next = pt_next,
4830 .t_cont = pt_continue,
4831 .t_signal = pt_signal,
4832 .t_add_vbrkpt = pt_add_vbrkpt,
4833 .t_add_sbrkpt = pt_add_sbrkpt,
4834 .t_add_pwapt = (int (*)())(uintptr_t)mdb_tgt_null,
4835 .t_add_vwapt = pt_add_vwapt,
4836 .t_add_iowapt = (int (*)())(uintptr_t)mdb_tgt_null,
4837 .t_add_sysenter = pt_add_sysenter,
4838 .t_add_sysexit = pt_add_sysexit,
4839 .t_add_signal = pt_add_signal,
4840 .t_add_fault = pt_add_fault,
4841 .t_getareg = pt_getareg,
4842 .t_putareg = pt_putareg,
4843 .t_stack_iter = pt_stack_iter,
4844 .t_auxv = pt_auxv,
4845 .t_thread_name = pt_thread_name,
4846 };
4847
4848 /*
4849 * Utility function for converting libproc errno values to mdb error values
4850 * for the ptl calls below. Currently, we only need to convert ENOENT to
4851 * EMDB_NOTHREAD to produce a more useful error message for the user.
4852 */
4853 static int
ptl_err(int error)4854 ptl_err(int error)
4855 {
4856 if (error != 0 && errno == ENOENT)
4857 return (set_errno(EMDB_NOTHREAD));
4858
4859 return (error);
4860 }
4861
4862 /*ARGSUSED*/
4863 static mdb_tgt_tid_t
pt_lwp_tid(mdb_tgt_t * t,void * tap)4864 pt_lwp_tid(mdb_tgt_t *t, void *tap)
4865 {
4866 if (t->t_pshandle != NULL)
4867 return (Pstatus(t->t_pshandle)->pr_lwp.pr_lwpid);
4868
4869 return (set_errno(EMDB_NOPROC));
4870 }
4871
4872 static int
pt_lwp_add(mdb_addrvec_t * ap,const lwpstatus_t * psp)4873 pt_lwp_add(mdb_addrvec_t *ap, const lwpstatus_t *psp)
4874 {
4875 mdb_addrvec_unshift(ap, psp->pr_lwpid);
4876 return (0);
4877 }
4878
4879 /*ARGSUSED*/
4880 static int
pt_lwp_iter(mdb_tgt_t * t,void * tap,mdb_addrvec_t * ap)4881 pt_lwp_iter(mdb_tgt_t *t, void *tap, mdb_addrvec_t *ap)
4882 {
4883 if (t->t_pshandle != NULL)
4884 return (Plwp_iter(t->t_pshandle, (proc_lwp_f *)pt_lwp_add, ap));
4885
4886 return (set_errno(EMDB_NOPROC));
4887 }
4888
4889 /*ARGSUSED*/
4890 static int
pt_lwp_getregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)4891 pt_lwp_getregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
4892 {
4893 if (t->t_pshandle != NULL) {
4894 return (ptl_err(Plwp_getregs(t->t_pshandle,
4895 (lwpid_t)tid, gregs)));
4896 }
4897 return (set_errno(EMDB_NOPROC));
4898 }
4899
4900 /*ARGSUSED*/
4901 static int
pt_lwp_setregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)4902 pt_lwp_setregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
4903 {
4904 if (t->t_pshandle != NULL) {
4905 return (ptl_err(Plwp_setregs(t->t_pshandle,
4906 (lwpid_t)tid, gregs)));
4907 }
4908 return (set_errno(EMDB_NOPROC));
4909 }
4910
4911
4912 /*ARGSUSED*/
4913 static int
pt_lwp_getxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prxregset_t ** xregs,size_t * sizep)4914 pt_lwp_getxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prxregset_t **xregs,
4915 size_t *sizep)
4916 {
4917 if (t->t_pshandle != NULL) {
4918 return (ptl_err(Plwp_getxregs(t->t_pshandle,
4919 (lwpid_t)tid, xregs, sizep)));
4920 }
4921 return (set_errno(EMDB_NOPROC));
4922 }
4923
4924 static void
pt_lwp_freexregs(mdb_tgt_t * t,void * tap,prxregset_t * xregs,size_t size)4925 pt_lwp_freexregs(mdb_tgt_t *t, void *tap, prxregset_t *xregs, size_t size)
4926 {
4927 if (t->t_pshandle != NULL) {
4928 Plwp_freexregs(t->t_pshandle, xregs, size);
4929 }
4930 }
4931
4932 static int
pt_lwp_setxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prxregset_t * xregs,size_t len)4933 pt_lwp_setxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4934 const prxregset_t *xregs, size_t len)
4935 {
4936 if (t->t_pshandle != NULL) {
4937 return (ptl_err(Plwp_setxregs(t->t_pshandle,
4938 (lwpid_t)tid, xregs, len)));
4939 }
4940 return (set_errno(EMDB_NOPROC));
4941 }
4942
4943 /*ARGSUSED*/
4944 static int
pt_lwp_getfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prfpregset_t * fpregs)4945 pt_lwp_getfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4946 prfpregset_t *fpregs)
4947 {
4948 if (t->t_pshandle != NULL) {
4949 return (ptl_err(Plwp_getfpregs(t->t_pshandle,
4950 (lwpid_t)tid, fpregs)));
4951 }
4952 return (set_errno(EMDB_NOPROC));
4953 }
4954
4955 /*ARGSUSED*/
4956 static int
pt_lwp_setfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prfpregset_t * fpregs)4957 pt_lwp_setfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
4958 const prfpregset_t *fpregs)
4959 {
4960 if (t->t_pshandle != NULL) {
4961 return (ptl_err(Plwp_setfpregs(t->t_pshandle,
4962 (lwpid_t)tid, fpregs)));
4963 }
4964 return (set_errno(EMDB_NOPROC));
4965 }
4966
4967 static const pt_ptl_ops_t proc_lwp_ops = {
4968 .ptl_ctor = (int (*)())(uintptr_t)mdb_tgt_nop,
4969 .ptl_dtor = (void (*)())(uintptr_t)mdb_tgt_nop,
4970 .ptl_tid = pt_lwp_tid,
4971 .ptl_iter = pt_lwp_iter,
4972 .ptl_getregs = pt_lwp_getregs,
4973 .ptl_setregs = pt_lwp_setregs,
4974 .ptl_getxregs = pt_lwp_getxregs,
4975 .ptl_freexregs = pt_lwp_freexregs,
4976 .ptl_setxregs = pt_lwp_setxregs,
4977 .ptl_getfpregs = pt_lwp_getfpregs,
4978 .ptl_setfpregs = pt_lwp_setfpregs
4979 };
4980
4981 static int
pt_tdb_ctor(mdb_tgt_t * t)4982 pt_tdb_ctor(mdb_tgt_t *t)
4983 {
4984 pt_data_t *pt = t->t_data;
4985 td_thragent_t *tap;
4986 td_err_e err;
4987
4988 if ((err = pt->p_tdb_ops->td_ta_new(t->t_pshandle, &tap)) != TD_OK)
4989 return (set_errno(tdb_to_errno(err)));
4990
4991 pt->p_ptl_hdl = tap;
4992 return (0);
4993 }
4994
4995 static void
pt_tdb_dtor(mdb_tgt_t * t,void * tap)4996 pt_tdb_dtor(mdb_tgt_t *t, void *tap)
4997 {
4998 pt_data_t *pt = t->t_data;
4999
5000 ASSERT(tap == pt->p_ptl_hdl);
5001 (void) pt->p_tdb_ops->td_ta_delete(tap);
5002 pt->p_ptl_hdl = NULL;
5003 }
5004
5005 static mdb_tgt_tid_t
pt_tdb_tid(mdb_tgt_t * t,void * tap)5006 pt_tdb_tid(mdb_tgt_t *t, void *tap)
5007 {
5008 pt_data_t *pt = t->t_data;
5009
5010 td_thrhandle_t th;
5011 td_thrinfo_t ti;
5012 td_err_e err;
5013
5014 if (t->t_pshandle == NULL)
5015 return (set_errno(EMDB_NOPROC));
5016
5017 if ((err = pt->p_tdb_ops->td_ta_map_lwp2thr(tap,
5018 Pstatus(t->t_pshandle)->pr_lwp.pr_lwpid, &th)) != TD_OK)
5019 return (set_errno(tdb_to_errno(err)));
5020
5021 if ((err = pt->p_tdb_ops->td_thr_get_info(&th, &ti)) != TD_OK)
5022 return (set_errno(tdb_to_errno(err)));
5023
5024 return (ti.ti_tid);
5025 }
5026
5027 static int
pt_tdb_add(const td_thrhandle_t * thp,pt_addarg_t * pap)5028 pt_tdb_add(const td_thrhandle_t *thp, pt_addarg_t *pap)
5029 {
5030 td_thrinfo_t ti;
5031
5032 if (pap->pa_pt->p_tdb_ops->td_thr_get_info(thp, &ti) == TD_OK &&
5033 ti.ti_state != TD_THR_ZOMBIE)
5034 mdb_addrvec_unshift(pap->pa_ap, ti.ti_tid);
5035
5036 return (0);
5037 }
5038
5039 static int
pt_tdb_iter(mdb_tgt_t * t,void * tap,mdb_addrvec_t * ap)5040 pt_tdb_iter(mdb_tgt_t *t, void *tap, mdb_addrvec_t *ap)
5041 {
5042 pt_data_t *pt = t->t_data;
5043 pt_addarg_t arg;
5044 int err;
5045
5046 if (t->t_pshandle == NULL)
5047 return (set_errno(EMDB_NOPROC));
5048
5049 arg.pa_pt = pt;
5050 arg.pa_ap = ap;
5051
5052 if ((err = pt->p_tdb_ops->td_ta_thr_iter(tap, (td_thr_iter_f *)
5053 pt_tdb_add, &arg, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
5054 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS)) != TD_OK)
5055 return (set_errno(tdb_to_errno(err)));
5056
5057 return (0);
5058 }
5059
5060 static int
pt_tdb_getregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)5061 pt_tdb_getregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
5062 {
5063 pt_data_t *pt = t->t_data;
5064
5065 td_thrhandle_t th;
5066 td_err_e err;
5067
5068 if (t->t_pshandle == NULL)
5069 return (set_errno(EMDB_NOPROC));
5070
5071 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5072 return (set_errno(tdb_to_errno(err)));
5073
5074 err = pt->p_tdb_ops->td_thr_getgregs(&th, gregs);
5075 if (err != TD_OK && err != TD_PARTIALREG)
5076 return (set_errno(tdb_to_errno(err)));
5077
5078 return (0);
5079 }
5080
5081 static int
pt_tdb_setregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prgregset_t gregs)5082 pt_tdb_setregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prgregset_t gregs)
5083 {
5084 pt_data_t *pt = t->t_data;
5085
5086 td_thrhandle_t th;
5087 td_err_e err;
5088
5089 if (t->t_pshandle == NULL)
5090 return (set_errno(EMDB_NOPROC));
5091
5092 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5093 return (set_errno(tdb_to_errno(err)));
5094
5095 err = pt->p_tdb_ops->td_thr_setgregs(&th, gregs);
5096 if (err != TD_OK && err != TD_PARTIALREG)
5097 return (set_errno(tdb_to_errno(err)));
5098
5099 return (0);
5100 }
5101
5102 static int
pt_tdb_getxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prxregset_t ** xregs,size_t * sizep)5103 pt_tdb_getxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid, prxregset_t **xregs,
5104 size_t *sizep)
5105 {
5106 pt_data_t *pt = t->t_data;
5107
5108 td_thrhandle_t th;
5109 td_err_e err;
5110 int xregsize;
5111 prxregset_t *pxr;
5112
5113 if (t->t_pshandle == NULL)
5114 return (set_errno(EMDB_NOPROC));
5115
5116 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5117 return (set_errno(tdb_to_errno(err)));
5118
5119 if ((err = pt->p_tdb_ops->td_thr_getxregsize(&th, &xregsize)) != TD_OK)
5120 return (set_errno(tdb_to_errno(err)));
5121
5122 if (xregsize == 0) {
5123 return (set_errno(ENODATA));
5124 }
5125
5126 pxr = mdb_alloc(xregsize, UM_SLEEP);
5127
5128 err = pt->p_tdb_ops->td_thr_getxregs(&th, pxr);
5129 if (err != TD_OK && err != TD_PARTIALREG) {
5130 mdb_free(pxr, xregsize);
5131 return (set_errno(tdb_to_errno(err)));
5132 }
5133
5134 *xregs = pxr;
5135 *sizep = xregsize;
5136 return (0);
5137 }
5138
5139 static void
pt_tdb_freexregs(mdb_tgt_t * t __unused,void * tap __unused,prxregset_t * pxr,size_t size)5140 pt_tdb_freexregs(mdb_tgt_t *t __unused, void *tap __unused, prxregset_t *pxr,
5141 size_t size)
5142 {
5143 mdb_free(pxr, size);
5144 }
5145
5146 static int
pt_tdb_setxregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prxregset_t * xregs,size_t len __unused)5147 pt_tdb_setxregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5148 const prxregset_t *xregs, size_t len __unused)
5149 {
5150 pt_data_t *pt = t->t_data;
5151
5152 td_thrhandle_t th;
5153 td_err_e err;
5154
5155 if (t->t_pshandle == NULL)
5156 return (set_errno(EMDB_NOPROC));
5157
5158 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5159 return (set_errno(tdb_to_errno(err)));
5160
5161 err = pt->p_tdb_ops->td_thr_setxregs(&th, xregs);
5162 if (err != TD_OK && err != TD_PARTIALREG)
5163 return (set_errno(tdb_to_errno(err)));
5164
5165 return (0);
5166 }
5167
5168 static int
pt_tdb_getfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,prfpregset_t * fpregs)5169 pt_tdb_getfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5170 prfpregset_t *fpregs)
5171 {
5172 pt_data_t *pt = t->t_data;
5173
5174 td_thrhandle_t th;
5175 td_err_e err;
5176
5177 if (t->t_pshandle == NULL)
5178 return (set_errno(EMDB_NOPROC));
5179
5180 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5181 return (set_errno(tdb_to_errno(err)));
5182
5183 err = pt->p_tdb_ops->td_thr_getfpregs(&th, fpregs);
5184 if (err != TD_OK && err != TD_PARTIALREG)
5185 return (set_errno(tdb_to_errno(err)));
5186
5187 return (0);
5188 }
5189
5190 static int
pt_tdb_setfpregs(mdb_tgt_t * t,void * tap,mdb_tgt_tid_t tid,const prfpregset_t * fpregs)5191 pt_tdb_setfpregs(mdb_tgt_t *t, void *tap, mdb_tgt_tid_t tid,
5192 const prfpregset_t *fpregs)
5193 {
5194 pt_data_t *pt = t->t_data;
5195
5196 td_thrhandle_t th;
5197 td_err_e err;
5198
5199 if (t->t_pshandle == NULL)
5200 return (set_errno(EMDB_NOPROC));
5201
5202 if ((err = pt->p_tdb_ops->td_ta_map_id2thr(tap, tid, &th)) != TD_OK)
5203 return (set_errno(tdb_to_errno(err)));
5204
5205 err = pt->p_tdb_ops->td_thr_setfpregs(&th, fpregs);
5206 if (err != TD_OK && err != TD_PARTIALREG)
5207 return (set_errno(tdb_to_errno(err)));
5208
5209 return (0);
5210 }
5211
5212 static const pt_ptl_ops_t proc_tdb_ops = {
5213 .ptl_ctor = pt_tdb_ctor,
5214 .ptl_dtor = pt_tdb_dtor,
5215 .ptl_tid = pt_tdb_tid,
5216 .ptl_iter = pt_tdb_iter,
5217 .ptl_getregs = pt_tdb_getregs,
5218 .ptl_setregs = pt_tdb_setregs,
5219 .ptl_getxregs = pt_tdb_getxregs,
5220 .ptl_freexregs = pt_tdb_freexregs,
5221 .ptl_setxregs = pt_tdb_setxregs,
5222 .ptl_getfpregs = pt_tdb_getfpregs,
5223 .ptl_setfpregs = pt_tdb_setfpregs
5224 };
5225
5226 static ssize_t
pt_xd_auxv(mdb_tgt_t * t,void * buf,size_t nbytes)5227 pt_xd_auxv(mdb_tgt_t *t, void *buf, size_t nbytes)
5228 {
5229 struct ps_prochandle *P = t->t_pshandle;
5230 const auxv_t *auxp, *auxv = NULL;
5231 int auxn = 0;
5232
5233 if (P != NULL && (auxv = Pgetauxvec(P)) != NULL &&
5234 auxv->a_type != AT_NULL) {
5235 for (auxp = auxv, auxn = 1; auxp->a_type != 0; auxp++)
5236 auxn++;
5237 }
5238
5239 if (buf == NULL && nbytes == 0)
5240 return (sizeof (auxv_t) * auxn);
5241
5242 if (auxn == 0)
5243 return (set_errno(ENODATA));
5244
5245 nbytes = MIN(nbytes, sizeof (auxv_t) * auxn);
5246 bcopy(auxv, buf, nbytes);
5247 return (nbytes);
5248 }
5249
5250 static ssize_t
pt_xd_cred(mdb_tgt_t * t,void * buf,size_t nbytes)5251 pt_xd_cred(mdb_tgt_t *t, void *buf, size_t nbytes)
5252 {
5253 prcred_t cr, *crp;
5254 size_t cbytes = 0;
5255
5256 if (t->t_pshandle != NULL && Pcred(t->t_pshandle, &cr, 1) == 0) {
5257 cbytes = (cr.pr_ngroups <= 1) ? sizeof (prcred_t) :
5258 (sizeof (prcred_t) + (cr.pr_ngroups - 1) * sizeof (gid_t));
5259 }
5260
5261 if (buf == NULL && nbytes == 0)
5262 return (cbytes);
5263
5264 if (cbytes == 0)
5265 return (set_errno(ENODATA));
5266
5267 crp = mdb_alloc(cbytes, UM_SLEEP);
5268
5269 if (Pcred(t->t_pshandle, crp, cr.pr_ngroups) == -1)
5270 return (set_errno(ENODATA));
5271
5272 nbytes = MIN(nbytes, cbytes);
5273 bcopy(crp, buf, nbytes);
5274 mdb_free(crp, cbytes);
5275 return (nbytes);
5276 }
5277
5278 static ssize_t
pt_xd_ehdr(mdb_tgt_t * t,void * buf,size_t nbytes)5279 pt_xd_ehdr(mdb_tgt_t *t, void *buf, size_t nbytes)
5280 {
5281 pt_data_t *pt = t->t_data;
5282
5283 if (buf == NULL && nbytes == 0)
5284 return (sizeof (GElf_Ehdr));
5285
5286 if (pt->p_file == NULL)
5287 return (set_errno(ENODATA));
5288
5289 nbytes = MIN(nbytes, sizeof (GElf_Ehdr));
5290 bcopy(&pt->p_file->gf_ehdr, buf, nbytes);
5291 return (nbytes);
5292 }
5293
5294 static int
pt_copy_lwp(lwpstatus_t ** lspp,const lwpstatus_t * lsp)5295 pt_copy_lwp(lwpstatus_t **lspp, const lwpstatus_t *lsp)
5296 {
5297 bcopy(lsp, *lspp, sizeof (lwpstatus_t));
5298 (*lspp)++;
5299 return (0);
5300 }
5301
5302 static ssize_t
pt_xd_lwpstatus(mdb_tgt_t * t,void * buf,size_t nbytes)5303 pt_xd_lwpstatus(mdb_tgt_t *t, void *buf, size_t nbytes)
5304 {
5305 lwpstatus_t *lsp, *lbuf;
5306 const pstatus_t *psp;
5307 int nlwp = 0;
5308
5309 if (t->t_pshandle != NULL && (psp = Pstatus(t->t_pshandle)) != NULL)
5310 nlwp = psp->pr_nlwp;
5311
5312 if (buf == NULL && nbytes == 0)
5313 return (sizeof (lwpstatus_t) * nlwp);
5314
5315 if (nlwp == 0)
5316 return (set_errno(ENODATA));
5317
5318 lsp = lbuf = mdb_alloc(sizeof (lwpstatus_t) * nlwp, UM_SLEEP);
5319 nbytes = MIN(nbytes, sizeof (lwpstatus_t) * nlwp);
5320
5321 (void) Plwp_iter(t->t_pshandle, (proc_lwp_f *)pt_copy_lwp, &lsp);
5322 bcopy(lbuf, buf, nbytes);
5323
5324 mdb_free(lbuf, sizeof (lwpstatus_t) * nlwp);
5325 return (nbytes);
5326 }
5327
5328 static ssize_t
pt_xd_pshandle(mdb_tgt_t * t,void * buf,size_t nbytes)5329 pt_xd_pshandle(mdb_tgt_t *t, void *buf, size_t nbytes)
5330 {
5331 if (buf == NULL && nbytes == 0)
5332 return (sizeof (struct ps_prochandle *));
5333
5334 if (t->t_pshandle == NULL || nbytes != sizeof (struct ps_prochandle *))
5335 return (set_errno(ENODATA));
5336
5337 bcopy(&t->t_pshandle, buf, nbytes);
5338 return (nbytes);
5339 }
5340
5341 static ssize_t
pt_xd_psinfo(mdb_tgt_t * t,void * buf,size_t nbytes)5342 pt_xd_psinfo(mdb_tgt_t *t, void *buf, size_t nbytes)
5343 {
5344 const psinfo_t *psp;
5345
5346 if (buf == NULL && nbytes == 0)
5347 return (sizeof (psinfo_t));
5348
5349 if (t->t_pshandle == NULL || (psp = Ppsinfo(t->t_pshandle)) == NULL)
5350 return (set_errno(ENODATA));
5351
5352 nbytes = MIN(nbytes, sizeof (psinfo_t));
5353 bcopy(psp, buf, nbytes);
5354 return (nbytes);
5355 }
5356
5357 static ssize_t
pt_xd_pstatus(mdb_tgt_t * t,void * buf,size_t nbytes)5358 pt_xd_pstatus(mdb_tgt_t *t, void *buf, size_t nbytes)
5359 {
5360 const pstatus_t *psp;
5361
5362 if (buf == NULL && nbytes == 0)
5363 return (sizeof (pstatus_t));
5364
5365 if (t->t_pshandle == NULL || (psp = Pstatus(t->t_pshandle)) == NULL)
5366 return (set_errno(ENODATA));
5367
5368 nbytes = MIN(nbytes, sizeof (pstatus_t));
5369 bcopy(psp, buf, nbytes);
5370 return (nbytes);
5371 }
5372
5373 static ssize_t
pt_xd_utsname(mdb_tgt_t * t,void * buf,size_t nbytes)5374 pt_xd_utsname(mdb_tgt_t *t, void *buf, size_t nbytes)
5375 {
5376 struct utsname uts;
5377
5378 if (buf == NULL && nbytes == 0)
5379 return (sizeof (struct utsname));
5380
5381 if (t->t_pshandle == NULL || Puname(t->t_pshandle, &uts) != 0)
5382 return (set_errno(ENODATA));
5383
5384 nbytes = MIN(nbytes, sizeof (struct utsname));
5385 bcopy(&uts, buf, nbytes);
5386 return (nbytes);
5387 }
5388
5389 int
mdb_proc_tgt_create(mdb_tgt_t * t,int argc,const char * argv[])5390 mdb_proc_tgt_create(mdb_tgt_t *t, int argc, const char *argv[])
5391 {
5392 pt_data_t *pt = mdb_zalloc(sizeof (pt_data_t), UM_SLEEP);
5393
5394 const char *aout_path = argc > 0 ? argv[0] : PT_EXEC_PATH;
5395 const char *core_path = argc > 1 ? argv[1] : NULL;
5396
5397 const mdb_tgt_regdesc_t *rdp;
5398 char execname[MAXPATHLEN];
5399 struct stat64 st;
5400 int perr;
5401 int state = 0;
5402 struct rlimit rlim;
5403 int i;
5404
5405 if (argc > 2) {
5406 mdb_free(pt, sizeof (pt_data_t));
5407 return (set_errno(EINVAL));
5408 }
5409
5410 if (t->t_flags & MDB_TGT_F_RDWR)
5411 pt->p_oflags = O_RDWR;
5412 else
5413 pt->p_oflags = O_RDONLY;
5414
5415 if (t->t_flags & MDB_TGT_F_FORCE)
5416 pt->p_gflags |= PGRAB_FORCE;
5417 if (t->t_flags & MDB_TGT_F_NOSTOP)
5418 pt->p_gflags |= PGRAB_NOSTOP;
5419
5420 pt->p_ptl_ops = &proc_lwp_ops;
5421 pt->p_maxsig = sysconf(_SC_SIGRT_MAX);
5422
5423 (void) mdb_nv_create(&pt->p_regs, UM_SLEEP);
5424 (void) mdb_nv_create(&pt->p_env, UM_SLEEP);
5425
5426 t->t_ops = &proc_ops;
5427 t->t_data = pt;
5428
5429 /*
5430 * If no core file name was specified, but the file ./core is present,
5431 * infer that we want to debug it. I find this behavior confusing,
5432 * so we only do this when precise adb(1) compatibility is required.
5433 */
5434 if (core_path == NULL && (mdb.m_flags & MDB_FL_ADB) &&
5435 access(PT_CORE_PATH, F_OK) == 0)
5436 core_path = PT_CORE_PATH;
5437
5438 /*
5439 * For compatibility with adb(1), the special name "-" may be used
5440 * to suppress the loading of the executable or core file.
5441 */
5442 if (aout_path != NULL && strcmp(aout_path, "-") == 0)
5443 aout_path = NULL;
5444 if (core_path != NULL && strcmp(core_path, "-") == 0)
5445 core_path = NULL;
5446
5447 /*
5448 * If a core file or pid was specified, attempt to grab it now using
5449 * proc_arg_grab(); otherwise we'll create a fresh process later.
5450 */
5451 if (core_path != NULL && (t->t_pshandle = proc_arg_xgrab(core_path,
5452 aout_path == PT_EXEC_PATH ? NULL : aout_path, PR_ARG_ANY,
5453 pt->p_gflags, &perr, NULL)) == NULL) {
5454 mdb_warn("cannot debug %s: %s\n", core_path, Pgrab_error(perr));
5455 goto err;
5456 }
5457
5458 if (aout_path != NULL &&
5459 (pt->p_idlehandle = Pgrab_file(aout_path, &perr)) != NULL &&
5460 t->t_pshandle == NULL)
5461 t->t_pshandle = pt->p_idlehandle;
5462
5463 if (t->t_pshandle != NULL)
5464 state = Pstate(t->t_pshandle);
5465
5466 /*
5467 * Make sure we'll have enough file descriptors to handle a target
5468 * has many many mappings.
5469 */
5470 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
5471 rlim.rlim_cur = rlim.rlim_max;
5472 (void) setrlimit(RLIMIT_NOFILE, &rlim);
5473 (void) enable_extended_FILE_stdio(-1, -1);
5474 }
5475
5476 /*
5477 * If we don't have an executable path or the executable path is the
5478 * /proc/<pid>/object/a.out path, but we now have a libproc handle (and
5479 * it didn't come from a core file), attempt to derive the executable
5480 * path using Pexecname(). We need to do this in the /proc case in
5481 * order to open the executable for writing because /proc/object/<file>
5482 * permission are masked with 0555. If Pexecname() fails us, fall back
5483 * to /proc/<pid>/object/a.out.
5484 */
5485 if (t->t_pshandle != NULL && core_path == NULL &&
5486 (aout_path == NULL || (stat64(aout_path, &st) == 0 &&
5487 strcmp(st.st_fstype, "proc") == 0))) {
5488 GElf_Sym s;
5489 aout_path = Pexecname(t->t_pshandle, execname, MAXPATHLEN);
5490 if (aout_path == NULL && state != PS_DEAD && state != PS_IDLE) {
5491 (void) mdb_iob_snprintf(execname, sizeof (execname),
5492 "/proc/%d/object/a.out",
5493 (int)Pstatus(t->t_pshandle)->pr_pid);
5494 aout_path = execname;
5495 }
5496 if (aout_path == NULL &&
5497 Plookup_by_name(t->t_pshandle, "a.out", "_start", &s) != 0)
5498 mdb_warn("warning: failed to infer pathname to "
5499 "executable; symbol table will not be available\n");
5500
5501 mdb_dprintf(MDB_DBG_TGT, "a.out is %s\n", aout_path);
5502 }
5503
5504 /*
5505 * Attempt to open the executable file. We only want this operation
5506 * to actually cause the constructor to abort if the executable file
5507 * name was given explicitly. If we defaulted to PT_EXEC_PATH or
5508 * derived the executable using Pexecname, then we want to continue
5509 * along with p_fio and p_file set to NULL.
5510 */
5511 if (aout_path != NULL && (pt->p_aout_fio = mdb_fdio_create_path(NULL,
5512 aout_path, pt->p_oflags, 0)) == NULL && argc > 0) {
5513 mdb_warn("failed to open %s", aout_path);
5514 goto err;
5515 }
5516
5517 /*
5518 * Now create an ELF file from the input file, if we have one. Again,
5519 * only abort the constructor if the name was given explicitly.
5520 */
5521 if (pt->p_aout_fio != NULL && pt_open_aout(t,
5522 mdb_io_hold(pt->p_aout_fio)) == NULL && argc > 0)
5523 goto err;
5524
5525 /*
5526 * If we've successfully opened an ELF file, select the appropriate
5527 * disassembler based on the ELF header.
5528 */
5529 if (pt->p_file != NULL)
5530 (void) mdb_dis_select(pt_disasm(&pt->p_file->gf_ehdr));
5531 else
5532 (void) mdb_dis_select(pt_disasm(NULL));
5533
5534 /*
5535 * Add each register described in the target ISA register description
5536 * list to our hash table of register descriptions and then add any
5537 * appropriate ISA-specific floating-point register descriptions.
5538 */
5539 for (rdp = pt_regdesc; rdp->rd_name != NULL; rdp++) {
5540 (void) mdb_nv_insert(&pt->p_regs, rdp->rd_name, NULL,
5541 MDB_TGT_R_NVAL(rdp->rd_num, rdp->rd_flags), MDB_NV_RDONLY);
5542 }
5543 pt_addfpregs(t);
5544
5545 /*
5546 * Certain important /proc structures may be of interest to mdb
5547 * modules and their dcmds. Export these using the xdata interface:
5548 */
5549 (void) mdb_tgt_xdata_insert(t, "auxv",
5550 "procfs auxv_t array", pt_xd_auxv);
5551 (void) mdb_tgt_xdata_insert(t, "cred",
5552 "procfs prcred_t structure", pt_xd_cred);
5553 (void) mdb_tgt_xdata_insert(t, "ehdr",
5554 "executable file GElf_Ehdr structure", pt_xd_ehdr);
5555 (void) mdb_tgt_xdata_insert(t, "lwpstatus",
5556 "procfs lwpstatus_t array", pt_xd_lwpstatus);
5557 (void) mdb_tgt_xdata_insert(t, "pshandle",
5558 "libproc proc service API handle", pt_xd_pshandle);
5559 (void) mdb_tgt_xdata_insert(t, "psinfo",
5560 "procfs psinfo_t structure", pt_xd_psinfo);
5561 (void) mdb_tgt_xdata_insert(t, "pstatus",
5562 "procfs pstatus_t structure", pt_xd_pstatus);
5563 (void) mdb_tgt_xdata_insert(t, "utsname",
5564 "utsname structure", pt_xd_utsname);
5565
5566 /*
5567 * Force a status update now so that we fill in t_status with the
5568 * latest information based on any successful grab.
5569 */
5570 (void) mdb_tgt_status(t, &t->t_status);
5571
5572 /*
5573 * If we're not examining a core file, trace SIGINT and all signals
5574 * that cause the process to dump core as part of our initialization.
5575 */
5576 if ((t->t_pshandle != NULL && state != PS_DEAD && state != PS_IDLE) ||
5577 (pt->p_file != NULL && pt->p_file->gf_ehdr.e_type == ET_EXEC)) {
5578
5579 int tflag = MDB_TGT_SPEC_STICKY; /* default sigs are sticky */
5580
5581 (void) mdb_tgt_add_signal(t, SIGINT, tflag, no_se_f, NULL);
5582 (void) mdb_tgt_add_signal(t, SIGQUIT, tflag, no_se_f, NULL);
5583 (void) mdb_tgt_add_signal(t, SIGILL, tflag, no_se_f, NULL);
5584 (void) mdb_tgt_add_signal(t, SIGTRAP, tflag, no_se_f, NULL);
5585 (void) mdb_tgt_add_signal(t, SIGABRT, tflag, no_se_f, NULL);
5586 (void) mdb_tgt_add_signal(t, SIGEMT, tflag, no_se_f, NULL);
5587 (void) mdb_tgt_add_signal(t, SIGFPE, tflag, no_se_f, NULL);
5588 (void) mdb_tgt_add_signal(t, SIGBUS, tflag, no_se_f, NULL);
5589 (void) mdb_tgt_add_signal(t, SIGSEGV, tflag, no_se_f, NULL);
5590 (void) mdb_tgt_add_signal(t, SIGSYS, tflag, no_se_f, NULL);
5591 (void) mdb_tgt_add_signal(t, SIGXCPU, tflag, no_se_f, NULL);
5592 (void) mdb_tgt_add_signal(t, SIGXFSZ, tflag, no_se_f, NULL);
5593 }
5594
5595 /*
5596 * If we've grabbed a live process, establish our initial breakpoints
5597 * and librtld_db agent so we can track rtld activity. If FL_VCREATE
5598 * is set, this process was created by a previous instantiation of
5599 * the debugger, so reset pr_flags to kill it; otherwise we attached
5600 * to an already running process. Pgrab() has already set the PR_RLC
5601 * flag appropriately based on whether the process was stopped when we
5602 * attached.
5603 */
5604 if (t->t_pshandle != NULL && state != PS_DEAD && state != PS_IDLE) {
5605 if (mdb.m_flags & MDB_FL_VCREATE) {
5606 (void) Punsetflags(t->t_pshandle, PR_RLC);
5607 (void) Psetflags(t->t_pshandle, PR_KLC);
5608 pt->p_rflags = PRELEASE_KILL;
5609 } else {
5610 (void) Punsetflags(t->t_pshandle, PR_KLC);
5611 }
5612 pt_post_attach(t);
5613 }
5614
5615 /*
5616 * Initialize a local copy of the environment, which can be modified
5617 * before running the program.
5618 */
5619 for (i = 0; mdb.m_env[i] != NULL; i++)
5620 pt_env_set(pt, mdb.m_env[i]);
5621
5622 /*
5623 * If adb(1) compatibility mode is on, then print the appropriate
5624 * greeting message if we have grabbed a core file.
5625 */
5626 if ((mdb.m_flags & MDB_FL_ADB) && t->t_pshandle != NULL &&
5627 state == PS_DEAD) {
5628 const pstatus_t *psp = Pstatus(t->t_pshandle);
5629 int cursig = psp->pr_lwp.pr_cursig;
5630 char signame[SIG2STR_MAX];
5631
5632 mdb_printf("core file = %s -- program ``%s'' on platform %s\n",
5633 core_path, aout_path ? aout_path : "?", pt_platform(t));
5634
5635 if (cursig != 0 && sig2str(cursig, signame) == 0)
5636 mdb_printf("SIG%s: %s\n", signame, strsignal(cursig));
5637 }
5638
5639 return (0);
5640
5641 err:
5642 pt_destroy(t);
5643 return (-1);
5644 }
5645