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