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