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 /* 28 * DTrace Process Control 29 * 30 * This file provides a set of routines that permit libdtrace and its clients 31 * to create and grab process handles using libproc, and to share these handles 32 * between library mechanisms that need libproc access, such as ustack(), and 33 * client mechanisms that need libproc access, such as dtrace(1M) -c and -p. 34 * The library provides several mechanisms in the libproc control layer: 35 * 36 * Reference Counting: The library code and client code can independently grab 37 * the same process handles without interfering with one another. Only when 38 * the reference count drops to zero and the handle is not being cached (see 39 * below for more information on caching) will Prelease() be called on it. 40 * 41 * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and 42 * the reference count drops to zero, the handle is not immediately released. 43 * Instead, libproc handles are maintained on dph_lrulist in order from most- 44 * recently accessed to least-recently accessed. Idle handles are maintained 45 * until a pre-defined LRU cache limit is exceeded, permitting repeated calls 46 * to ustack() to avoid the overhead of releasing and re-grabbing processes. 47 * 48 * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY) 49 * or created by dt_proc_create(), a control thread is created to provide 50 * callbacks on process exit and symbol table caching on dlopen()s. 51 * 52 * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock() 53 * are provided to synchronize access to the libproc handle between libdtrace 54 * code and client code and the control thread's use of the ps_prochandle. 55 * 56 * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the 57 * dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace 58 * calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for 59 * synchronization between libdtrace control threads and the client thread. 60 * 61 * The ps_prochandles themselves are maintained along with a dt_proc_t struct 62 * in a hash table indexed by PID. This provides basic locking and reference 63 * counting. The dt_proc_t is also maintained in LRU order on dph_lrulist. 64 * The dph_lrucnt and dph_lrulim count the number of cacheable processes and 65 * the current limit on the number of actively cached entries. 66 * 67 * The control thread for a process establishes breakpoints at the rtld_db 68 * locations of interest, updates mappings and symbol tables at these points, 69 * and handles exec and fork (by always following the parent). The control 70 * thread automatically exits when the process dies or control is lost. 71 * 72 * A simple notification mechanism is provided for libdtrace clients using 73 * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If 74 * such an event occurs, the dt_proc_t itself is enqueued on a notification 75 * list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake 76 * up using this condition and will then call the client handler as necessary. 77 */ 78 79 #include <sys/wait.h> 80 #include <sys/lwp.h> 81 #include <strings.h> 82 #include <signal.h> 83 #include <assert.h> 84 #include <errno.h> 85 86 #include <dt_proc.h> 87 #include <dt_pid.h> 88 #include <dt_impl.h> 89 90 #define IS_SYS_EXEC(w) (w == SYS_exec || w == SYS_execve) 91 #define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_fork1 || \ 92 w == SYS_forkall || w == SYS_forksys) 93 94 static dt_bkpt_t * 95 dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data) 96 { 97 struct ps_prochandle *P = dpr->dpr_proc; 98 dt_bkpt_t *dbp; 99 100 assert(MUTEX_HELD(&dpr->dpr_lock)); 101 102 if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) { 103 dbp->dbp_func = func; 104 dbp->dbp_data = data; 105 dbp->dbp_addr = addr; 106 107 if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0) 108 dbp->dbp_active = B_TRUE; 109 110 dt_list_append(&dpr->dpr_bps, dbp); 111 } 112 113 return (dbp); 114 } 115 116 static void 117 dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts) 118 { 119 int state = Pstate(dpr->dpr_proc); 120 dt_bkpt_t *dbp, *nbp; 121 122 assert(MUTEX_HELD(&dpr->dpr_lock)); 123 124 for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) { 125 if (delbkpts && dbp->dbp_active && 126 state != PS_LOST && state != PS_UNDEAD) { 127 (void) Pdelbkpt(dpr->dpr_proc, 128 dbp->dbp_addr, dbp->dbp_instr); 129 } 130 nbp = dt_list_next(dbp); 131 dt_list_delete(&dpr->dpr_bps, dbp); 132 dt_free(dpr->dpr_hdl, dbp); 133 } 134 } 135 136 static void 137 dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr) 138 { 139 const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp; 140 dt_bkpt_t *dbp; 141 142 assert(MUTEX_HELD(&dpr->dpr_lock)); 143 144 for (dbp = dt_list_next(&dpr->dpr_bps); 145 dbp != NULL; dbp = dt_list_next(dbp)) { 146 if (psp->pr_reg[R_PC] == dbp->dbp_addr) 147 break; 148 } 149 150 if (dbp == NULL) { 151 dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n", 152 (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]); 153 return; 154 } 155 156 dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n", 157 (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits); 158 159 dbp->dbp_func(dtp, dpr, dbp->dbp_data); 160 (void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr); 161 } 162 163 static void 164 dt_proc_bpenable(dt_proc_t *dpr) 165 { 166 dt_bkpt_t *dbp; 167 168 assert(MUTEX_HELD(&dpr->dpr_lock)); 169 170 for (dbp = dt_list_next(&dpr->dpr_bps); 171 dbp != NULL; dbp = dt_list_next(dbp)) { 172 if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc, 173 dbp->dbp_addr, &dbp->dbp_instr) == 0) 174 dbp->dbp_active = B_TRUE; 175 } 176 177 dt_dprintf("breakpoints enabled\n"); 178 } 179 180 static void 181 dt_proc_bpdisable(dt_proc_t *dpr) 182 { 183 dt_bkpt_t *dbp; 184 185 assert(MUTEX_HELD(&dpr->dpr_lock)); 186 187 for (dbp = dt_list_next(&dpr->dpr_bps); 188 dbp != NULL; dbp = dt_list_next(dbp)) { 189 if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc, 190 dbp->dbp_addr, dbp->dbp_instr) == 0) 191 dbp->dbp_active = B_FALSE; 192 } 193 194 dt_dprintf("breakpoints disabled\n"); 195 } 196 197 static void 198 dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr, 199 const char *msg) 200 { 201 dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t)); 202 203 if (dprn == NULL) { 204 dt_dprintf("failed to allocate notification for %d %s\n", 205 (int)dpr->dpr_pid, msg); 206 } else { 207 dprn->dprn_dpr = dpr; 208 if (msg == NULL) 209 dprn->dprn_errmsg[0] = '\0'; 210 else 211 (void) strlcpy(dprn->dprn_errmsg, msg, 212 sizeof (dprn->dprn_errmsg)); 213 214 (void) pthread_mutex_lock(&dph->dph_lock); 215 216 dprn->dprn_next = dph->dph_notify; 217 dph->dph_notify = dprn; 218 219 (void) pthread_cond_broadcast(&dph->dph_cv); 220 (void) pthread_mutex_unlock(&dph->dph_lock); 221 } 222 } 223 224 /* 225 * Check to see if the control thread was requested to stop when the victim 226 * process reached a particular event (why) rather than continuing the victim. 227 * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue(). 228 * If 'why' is not set, this function returns immediately and does nothing. 229 */ 230 static void 231 dt_proc_stop(dt_proc_t *dpr, uint8_t why) 232 { 233 assert(MUTEX_HELD(&dpr->dpr_lock)); 234 assert(why != DT_PROC_STOP_IDLE); 235 236 if (dpr->dpr_stop & why) { 237 dpr->dpr_stop |= DT_PROC_STOP_IDLE; 238 dpr->dpr_stop &= ~why; 239 240 (void) pthread_cond_broadcast(&dpr->dpr_cv); 241 242 /* 243 * We disable breakpoints while stopped to preserve the 244 * integrity of the program text for both our own disassembly 245 * and that of the kernel. 246 */ 247 dt_proc_bpdisable(dpr); 248 249 while (dpr->dpr_stop & DT_PROC_STOP_IDLE) 250 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 251 252 dt_proc_bpenable(dpr); 253 } 254 } 255 256 /*ARGSUSED*/ 257 static void 258 dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname) 259 { 260 dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname); 261 dt_proc_stop(dpr, DT_PROC_STOP_MAIN); 262 } 263 264 static void 265 dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname) 266 { 267 rd_event_msg_t rdm; 268 rd_err_e err; 269 270 if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) { 271 dt_dprintf("pid %d: failed to get %s event message: %s\n", 272 (int)dpr->dpr_pid, evname, rd_errstr(err)); 273 return; 274 } 275 276 dt_dprintf("pid %d: rtld event %s type=%d state %d\n", 277 (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state); 278 279 switch (rdm.type) { 280 case RD_DLACTIVITY: 281 if (rdm.u.state != RD_CONSISTENT) 282 break; 283 284 Pupdate_syms(dpr->dpr_proc); 285 if (dt_pid_create_probes_module(dtp, dpr) != 0) 286 dt_proc_notify(dtp, dtp->dt_procs, dpr, 287 dpr->dpr_errmsg); 288 289 break; 290 case RD_PREINIT: 291 Pupdate_syms(dpr->dpr_proc); 292 dt_proc_stop(dpr, DT_PROC_STOP_PREINIT); 293 break; 294 case RD_POSTINIT: 295 Pupdate_syms(dpr->dpr_proc); 296 dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT); 297 break; 298 } 299 } 300 301 static void 302 dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname) 303 { 304 rd_notify_t rdn; 305 rd_err_e err; 306 307 if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) { 308 dt_dprintf("pid %d: failed to get event address for %s: %s\n", 309 (int)dpr->dpr_pid, evname, rd_errstr(err)); 310 return; 311 } 312 313 if (rdn.type != RD_NOTIFY_BPT) { 314 dt_dprintf("pid %d: event %s has unexpected type %d\n", 315 (int)dpr->dpr_pid, evname, rdn.type); 316 return; 317 } 318 319 (void) dt_proc_bpcreate(dpr, rdn.u.bptaddr, 320 (dt_bkpt_f *)dt_proc_rdevent, (void *)evname); 321 } 322 323 /* 324 * Common code for enabling events associated with the run-time linker after 325 * attaching to a process or after a victim process completes an exec(2). 326 */ 327 static void 328 dt_proc_attach(dt_proc_t *dpr, int exec) 329 { 330 const pstatus_t *psp = Pstatus(dpr->dpr_proc); 331 rd_err_e err; 332 GElf_Sym sym; 333 334 assert(MUTEX_HELD(&dpr->dpr_lock)); 335 336 if (exec) { 337 if (psp->pr_lwp.pr_errno != 0) 338 return; /* exec failed: nothing needs to be done */ 339 340 dt_proc_bpdestroy(dpr, B_FALSE); 341 Preset_maps(dpr->dpr_proc); 342 } 343 344 if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL && 345 (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) { 346 dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT"); 347 dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT"); 348 dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY"); 349 } else { 350 dt_dprintf("pid %d: failed to enable rtld events: %s\n", 351 (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) : 352 "rtld_db agent initialization failed"); 353 } 354 355 Pupdate_maps(dpr->dpr_proc); 356 357 if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE, 358 "a.out", "main", &sym, NULL) == 0) { 359 (void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value, 360 (dt_bkpt_f *)dt_proc_bpmain, "a.out`main"); 361 } else { 362 dt_dprintf("pid %d: failed to find a.out`main: %s\n", 363 (int)dpr->dpr_pid, strerror(errno)); 364 } 365 } 366 367 /* 368 * Wait for a stopped process to be set running again by some other debugger. 369 * This is typically not required by /proc-based debuggers, since the usual 370 * model is that one debugger controls one victim. But DTrace, as usual, has 371 * its own needs: the stop() action assumes that prun(1) or some other tool 372 * will be applied to resume the victim process. This could be solved by 373 * adding a PCWRUN directive to /proc, but that seems like overkill unless 374 * other debuggers end up needing this functionality, so we implement a cheap 375 * equivalent to PCWRUN using the set of existing kernel mechanisms. 376 * 377 * Our intent is really not just to wait for the victim to run, but rather to 378 * wait for it to run and then stop again for a reason other than the current 379 * PR_REQUESTED stop. Since PCWSTOP/Pstopstatus() can be applied repeatedly 380 * to a stopped process and will return the same result without affecting the 381 * victim, we can just perform these operations repeatedly until Pstate() 382 * changes, the representative LWP ID changes, or the stop timestamp advances. 383 * dt_proc_control() will then rediscover the new state and continue as usual. 384 * When the process is still stopped in the same exact state, we sleep for a 385 * brief interval before waiting again so as not to spin consuming CPU cycles. 386 */ 387 static void 388 dt_proc_waitrun(dt_proc_t *dpr) 389 { 390 struct ps_prochandle *P = dpr->dpr_proc; 391 const lwpstatus_t *psp = &Pstatus(P)->pr_lwp; 392 393 int krflag = psp->pr_flags & (PR_KLC | PR_RLC); 394 timestruc_t tstamp = psp->pr_tstamp; 395 lwpid_t lwpid = psp->pr_lwpid; 396 397 const long wstop = PCWSTOP; 398 int pfd = Pctlfd(P); 399 400 assert(MUTEX_HELD(&dpr->dpr_lock)); 401 assert(psp->pr_flags & PR_STOPPED); 402 assert(Pstate(P) == PS_STOP); 403 404 /* 405 * While we are waiting for the victim to run, clear PR_KLC and PR_RLC 406 * so that if the libdtrace client is killed, the victim stays stopped. 407 * dt_proc_destroy() will also observe this and perform PRELEASE_HANG. 408 */ 409 (void) Punsetflags(P, krflag); 410 Psync(P); 411 412 (void) pthread_mutex_unlock(&dpr->dpr_lock); 413 414 while (!dpr->dpr_quit) { 415 if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR) 416 continue; /* check dpr_quit and continue waiting */ 417 418 (void) pthread_mutex_lock(&dpr->dpr_lock); 419 (void) Pstopstatus(P, PCNULL, 0); 420 psp = &Pstatus(P)->pr_lwp; 421 422 /* 423 * If we've reached a new state, found a new representative, or 424 * the stop timestamp has changed, restore PR_KLC/PR_RLC to its 425 * original setting and then return with dpr_lock held. 426 */ 427 if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid || 428 bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) { 429 (void) Psetflags(P, krflag); 430 Psync(P); 431 return; 432 } 433 434 (void) pthread_mutex_unlock(&dpr->dpr_lock); 435 (void) poll(NULL, 0, MILLISEC / 2); 436 } 437 438 (void) pthread_mutex_lock(&dpr->dpr_lock); 439 } 440 441 typedef struct dt_proc_control_data { 442 dtrace_hdl_t *dpcd_hdl; /* DTrace handle */ 443 dt_proc_t *dpcd_proc; /* proccess to control */ 444 } dt_proc_control_data_t; 445 446 /* 447 * Main loop for all victim process control threads. We initialize all the 448 * appropriate /proc control mechanisms, and then enter a loop waiting for 449 * the process to stop on an event or die. We process any events by calling 450 * appropriate subroutines, and exit when the victim dies or we lose control. 451 * 452 * The control thread synchronizes the use of dpr_proc with other libdtrace 453 * threads using dpr_lock. We hold the lock for all of our operations except 454 * waiting while the process is running: this is accomplished by writing a 455 * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the 456 * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used. 457 */ 458 static void * 459 dt_proc_control(void *arg) 460 { 461 dt_proc_control_data_t *datap = arg; 462 dtrace_hdl_t *dtp = datap->dpcd_hdl; 463 dt_proc_t *dpr = datap->dpcd_proc; 464 dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs; 465 struct ps_prochandle *P = dpr->dpr_proc; 466 467 int pfd = Pctlfd(P); 468 int pid = dpr->dpr_pid; 469 470 const long wstop = PCWSTOP; 471 int notify = B_FALSE; 472 473 /* 474 * We disable the POSIX thread cancellation mechanism so that the 475 * client program using libdtrace can't accidentally cancel our thread. 476 * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out 477 * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit. 478 */ 479 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 480 481 /* 482 * Set up the corresponding process for tracing by libdtrace. We want 483 * to be able to catch breakpoints and efficiently single-step over 484 * them, and we need to enable librtld_db to watch libdl activity. 485 */ 486 (void) pthread_mutex_lock(&dpr->dpr_lock); 487 488 (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */ 489 (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */ 490 (void) Punsetflags(P, PR_FORK); /* do not inherit on fork */ 491 492 (void) Pfault(P, FLTBPT, B_TRUE); /* always trace breakpoints */ 493 (void) Pfault(P, FLTTRACE, B_TRUE); /* always trace single-step */ 494 495 /* 496 * We must trace exit from exec() system calls so that if the exec is 497 * successful, we can reset our breakpoints and re-initialize libproc. 498 */ 499 (void) Psysexit(P, SYS_exec, B_TRUE); 500 (void) Psysexit(P, SYS_execve, B_TRUE); 501 502 /* 503 * We must trace entry and exit for fork() system calls in order to 504 * disable our breakpoints temporarily during the fork. We do not set 505 * the PR_FORK flag, so if fork succeeds the child begins executing and 506 * does not inherit any other tracing behaviors or a control thread. 507 */ 508 (void) Psysentry(P, SYS_vfork, B_TRUE); 509 (void) Psysexit(P, SYS_vfork, B_TRUE); 510 (void) Psysentry(P, SYS_fork1, B_TRUE); 511 (void) Psysexit(P, SYS_fork1, B_TRUE); 512 (void) Psysentry(P, SYS_forkall, B_TRUE); 513 (void) Psysexit(P, SYS_forkall, B_TRUE); 514 (void) Psysentry(P, SYS_forksys, B_TRUE); 515 (void) Psysexit(P, SYS_forksys, B_TRUE); 516 517 Psync(P); /* enable all /proc changes */ 518 dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */ 519 520 /* 521 * If PR_KLC is set, we created the process; otherwise we grabbed it. 522 * Check for an appropriate stop request and wait for dt_proc_continue. 523 */ 524 if (Pstatus(P)->pr_flags & PR_KLC) 525 dt_proc_stop(dpr, DT_PROC_STOP_CREATE); 526 else 527 dt_proc_stop(dpr, DT_PROC_STOP_GRAB); 528 529 if (Psetrun(P, 0, 0) == -1) { 530 dt_dprintf("pid %d: failed to set running: %s\n", 531 (int)dpr->dpr_pid, strerror(errno)); 532 } 533 534 (void) pthread_mutex_unlock(&dpr->dpr_lock); 535 536 /* 537 * Wait for the process corresponding to this control thread to stop, 538 * process the event, and then set it running again. We want to sleep 539 * with dpr_lock *unheld* so that other parts of libdtrace can use the 540 * ps_prochandle in the meantime (e.g. ustack()). To do this, we write 541 * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. 542 * Once the process stops, we wake up, grab dpr_lock, and then call 543 * Pwait() (which will return immediately) and do our processing. 544 */ 545 while (!dpr->dpr_quit) { 546 const lwpstatus_t *psp; 547 548 if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR) 549 continue; /* check dpr_quit and continue waiting */ 550 551 (void) pthread_mutex_lock(&dpr->dpr_lock); 552 pwait_locked: 553 if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) { 554 (void) pthread_mutex_unlock(&dpr->dpr_lock); 555 continue; /* check dpr_quit and continue waiting */ 556 } 557 558 switch (Pstate(P)) { 559 case PS_STOP: 560 psp = &Pstatus(P)->pr_lwp; 561 562 dt_dprintf("pid %d: proc stopped showing %d/%d\n", 563 pid, psp->pr_why, psp->pr_what); 564 565 /* 566 * If the process stops showing PR_REQUESTED, then the 567 * DTrace stop() action was applied to it or another 568 * debugging utility (e.g. pstop(1)) asked it to stop. 569 * In either case, the user's intention is for the 570 * process to remain stopped until another external 571 * mechanism (e.g. prun(1)) is applied. So instead of 572 * setting the process running ourself, we wait for 573 * someone else to do so. Once that happens, we return 574 * to our normal loop waiting for an event of interest. 575 */ 576 if (psp->pr_why == PR_REQUESTED) { 577 dt_proc_waitrun(dpr); 578 (void) pthread_mutex_unlock(&dpr->dpr_lock); 579 continue; 580 } 581 582 /* 583 * If the process stops showing one of the events that 584 * we are tracing, perform the appropriate response. 585 * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and 586 * PR_JOBCONTROL by design: if one of these conditions 587 * occurs, we will fall through to Psetrun() but the 588 * process will remain stopped in the kernel by the 589 * corresponding mechanism (e.g. job control stop). 590 */ 591 if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT) 592 dt_proc_bpmatch(dtp, dpr); 593 else if (psp->pr_why == PR_SYSENTRY && 594 IS_SYS_FORK(psp->pr_what)) 595 dt_proc_bpdisable(dpr); 596 else if (psp->pr_why == PR_SYSEXIT && 597 IS_SYS_FORK(psp->pr_what)) 598 dt_proc_bpenable(dpr); 599 else if (psp->pr_why == PR_SYSEXIT && 600 IS_SYS_EXEC(psp->pr_what)) 601 dt_proc_attach(dpr, B_TRUE); 602 break; 603 604 case PS_LOST: 605 if (Preopen(P) == 0) 606 goto pwait_locked; 607 608 dt_dprintf("pid %d: proc lost: %s\n", 609 pid, strerror(errno)); 610 611 dpr->dpr_quit = B_TRUE; 612 notify = B_TRUE; 613 break; 614 615 case PS_UNDEAD: 616 dt_dprintf("pid %d: proc died\n", pid); 617 dpr->dpr_quit = B_TRUE; 618 notify = B_TRUE; 619 break; 620 } 621 622 if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) { 623 dt_dprintf("pid %d: failed to set running: %s\n", 624 (int)dpr->dpr_pid, strerror(errno)); 625 } 626 627 (void) pthread_mutex_unlock(&dpr->dpr_lock); 628 } 629 630 /* 631 * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue 632 * the dt_proc_t structure on the dt_proc_hash_t notification list. 633 */ 634 if (notify) 635 dt_proc_notify(dtp, dph, dpr, NULL); 636 637 /* 638 * Destroy and remove any remaining breakpoints, set dpr_done and clear 639 * dpr_tid to indicate the control thread has exited, and notify any 640 * waiting thread in dt_proc_destroy() that we have succesfully exited. 641 */ 642 (void) pthread_mutex_lock(&dpr->dpr_lock); 643 644 dt_proc_bpdestroy(dpr, B_TRUE); 645 dpr->dpr_done = B_TRUE; 646 dpr->dpr_tid = 0; 647 648 (void) pthread_cond_broadcast(&dpr->dpr_cv); 649 (void) pthread_mutex_unlock(&dpr->dpr_lock); 650 651 return (NULL); 652 } 653 654 /*PRINTFLIKE3*/ 655 static struct ps_prochandle * 656 dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...) 657 { 658 va_list ap; 659 660 va_start(ap, format); 661 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap); 662 va_end(ap); 663 664 if (dpr->dpr_proc != NULL) 665 Prelease(dpr->dpr_proc, 0); 666 667 dt_free(dtp, dpr); 668 (void) dt_set_errno(dtp, EDT_COMPILER); 669 return (NULL); 670 } 671 672 dt_proc_t * 673 dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove) 674 { 675 dt_proc_hash_t *dph = dtp->dt_procs; 676 pid_t pid = Pstatus(P)->pr_pid; 677 dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)]; 678 679 for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) { 680 if (dpr->dpr_pid == pid) 681 break; 682 else 683 dpp = &dpr->dpr_hash; 684 } 685 686 assert(dpr != NULL); 687 assert(dpr->dpr_proc == P); 688 689 if (remove) 690 *dpp = dpr->dpr_hash; /* remove from pid hash chain */ 691 692 return (dpr); 693 } 694 695 static void 696 dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P) 697 { 698 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 699 dt_proc_hash_t *dph = dtp->dt_procs; 700 dt_proc_notify_t *npr, **npp; 701 int rflag; 702 703 assert(dpr != NULL); 704 705 /* 706 * If neither PR_KLC nor PR_RLC is set, then the process is stopped by 707 * an external debugger and we were waiting in dt_proc_waitrun(). 708 * Leave the process in this condition using PRELEASE_HANG. 709 */ 710 if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) { 711 dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid); 712 rflag = PRELEASE_HANG; 713 } else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) { 714 dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid); 715 rflag = PRELEASE_KILL; /* apply kill-on-last-close */ 716 } else { 717 dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid); 718 rflag = 0; /* apply run-on-last-close */ 719 } 720 721 if (dpr->dpr_tid) { 722 /* 723 * Set the dpr_quit flag to tell the daemon thread to exit. We 724 * send it a SIGCANCEL to poke it out of PCWSTOP or any other 725 * long-term /proc system call. Our daemon threads have POSIX 726 * cancellation disabled, so EINTR will be the only effect. We 727 * then wait for dpr_done to indicate the thread has exited. 728 * 729 * We can't use pthread_kill() to send SIGCANCEL because the 730 * interface forbids it and we can't use pthread_cancel() 731 * because with cancellation disabled it won't actually 732 * send SIGCANCEL to the target thread, so we use _lwp_kill() 733 * to do the job. This is all built on evil knowledge of 734 * the details of the cancellation mechanism in libc. 735 */ 736 (void) pthread_mutex_lock(&dpr->dpr_lock); 737 dpr->dpr_quit = B_TRUE; 738 (void) _lwp_kill(dpr->dpr_tid, SIGCANCEL); 739 740 /* 741 * If the process is currently idling in dt_proc_stop(), re- 742 * enable breakpoints and poke it into running again. 743 */ 744 if (dpr->dpr_stop & DT_PROC_STOP_IDLE) { 745 dt_proc_bpenable(dpr); 746 dpr->dpr_stop &= ~DT_PROC_STOP_IDLE; 747 (void) pthread_cond_broadcast(&dpr->dpr_cv); 748 } 749 750 while (!dpr->dpr_done) 751 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 752 753 (void) pthread_mutex_unlock(&dpr->dpr_lock); 754 } 755 756 /* 757 * Before we free the process structure, remove this dt_proc_t from the 758 * lookup hash, and then walk the dt_proc_hash_t's notification list 759 * and remove this dt_proc_t if it is enqueued. 760 */ 761 (void) pthread_mutex_lock(&dph->dph_lock); 762 (void) dt_proc_lookup(dtp, P, B_TRUE); 763 npp = &dph->dph_notify; 764 765 while ((npr = *npp) != NULL) { 766 if (npr->dprn_dpr == dpr) { 767 *npp = npr->dprn_next; 768 dt_free(dtp, npr); 769 } else { 770 npp = &npr->dprn_next; 771 } 772 } 773 774 (void) pthread_mutex_unlock(&dph->dph_lock); 775 776 /* 777 * Remove the dt_proc_list from the LRU list, release the underlying 778 * libproc handle, and free our dt_proc_t data structure. 779 */ 780 if (dpr->dpr_cacheable) { 781 assert(dph->dph_lrucnt != 0); 782 dph->dph_lrucnt--; 783 } 784 785 dt_list_delete(&dph->dph_lrulist, dpr); 786 Prelease(dpr->dpr_proc, rflag); 787 dt_free(dtp, dpr); 788 } 789 790 static int 791 dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop) 792 { 793 dt_proc_control_data_t data; 794 sigset_t nset, oset; 795 pthread_attr_t a; 796 int err; 797 798 (void) pthread_mutex_lock(&dpr->dpr_lock); 799 dpr->dpr_stop |= stop; /* set bit for initial rendezvous */ 800 801 (void) pthread_attr_init(&a); 802 (void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); 803 804 (void) sigfillset(&nset); 805 (void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */ 806 (void) sigdelset(&nset, SIGCANCEL); /* see dt_proc_destroy() */ 807 808 data.dpcd_hdl = dtp; 809 data.dpcd_proc = dpr; 810 811 (void) pthread_sigmask(SIG_SETMASK, &nset, &oset); 812 err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data); 813 (void) pthread_sigmask(SIG_SETMASK, &oset, NULL); 814 815 /* 816 * If the control thread was created, then wait on dpr_cv for either 817 * dpr_done to be set (the victim died or the control thread failed) 818 * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now 819 * stopped by /proc and the control thread is at the rendezvous event. 820 * On success, we return with the process and control thread stopped: 821 * the caller can then apply dt_proc_continue() to resume both. 822 */ 823 if (err == 0) { 824 while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE)) 825 (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock); 826 827 /* 828 * If dpr_done is set, the control thread aborted before it 829 * reached the rendezvous event. This is either due to PS_LOST 830 * or PS_UNDEAD (i.e. the process died). We try to provide a 831 * small amount of useful information to help figure it out. 832 */ 833 if (dpr->dpr_done) { 834 const psinfo_t *prp = Ppsinfo(dpr->dpr_proc); 835 int stat = prp ? prp->pr_wstat : 0; 836 int pid = dpr->dpr_pid; 837 838 if (Pstate(dpr->dpr_proc) == PS_LOST) { 839 (void) dt_proc_error(dpr->dpr_hdl, dpr, 840 "failed to control pid %d: process exec'd " 841 "set-id or unobservable program\n", pid); 842 } else if (WIFSIGNALED(stat)) { 843 (void) dt_proc_error(dpr->dpr_hdl, dpr, 844 "failed to control pid %d: process died " 845 "from signal %d\n", pid, WTERMSIG(stat)); 846 } else { 847 (void) dt_proc_error(dpr->dpr_hdl, dpr, 848 "failed to control pid %d: process exited " 849 "with status %d\n", pid, WEXITSTATUS(stat)); 850 } 851 852 err = ESRCH; /* cause grab() or create() to fail */ 853 } 854 } else { 855 (void) dt_proc_error(dpr->dpr_hdl, dpr, 856 "failed to create control thread for process-id %d: %s\n", 857 (int)dpr->dpr_pid, strerror(err)); 858 } 859 860 (void) pthread_mutex_unlock(&dpr->dpr_lock); 861 (void) pthread_attr_destroy(&a); 862 863 return (err); 864 } 865 866 struct ps_prochandle * 867 dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv) 868 { 869 dt_proc_hash_t *dph = dtp->dt_procs; 870 dt_proc_t *dpr; 871 int err; 872 873 if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL) 874 return (NULL); /* errno is set for us */ 875 876 (void) pthread_mutex_init(&dpr->dpr_lock, NULL); 877 (void) pthread_cond_init(&dpr->dpr_cv, NULL); 878 879 if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) { 880 return (dt_proc_error(dtp, dpr, 881 "failed to execute %s: %s\n", file, Pcreate_error(err))); 882 } 883 884 dpr->dpr_hdl = dtp; 885 dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid; 886 887 (void) Punsetflags(dpr->dpr_proc, PR_RLC); 888 (void) Psetflags(dpr->dpr_proc, PR_KLC); 889 890 if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0) 891 return (NULL); /* dt_proc_error() has been called for us */ 892 893 dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)]; 894 dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr; 895 dt_list_prepend(&dph->dph_lrulist, dpr); 896 897 dt_dprintf("created pid %d\n", (int)dpr->dpr_pid); 898 dpr->dpr_refs++; 899 900 return (dpr->dpr_proc); 901 } 902 903 struct ps_prochandle * 904 dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor) 905 { 906 dt_proc_hash_t *dph = dtp->dt_procs; 907 uint_t h = pid & (dph->dph_hashlen - 1); 908 dt_proc_t *dpr, *opr; 909 int err; 910 911 /* 912 * Search the hash table for the pid. If it is already grabbed or 913 * created, move the handle to the front of the lrulist, increment 914 * the reference count, and return the existing ps_prochandle. 915 */ 916 for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) { 917 if (dpr->dpr_pid == pid && !dpr->dpr_stale) { 918 /* 919 * If the cached handle was opened read-only and 920 * this request is for a writeable handle, mark 921 * the cached handle as stale and open a new handle. 922 * Since it's stale, unmark it as cacheable. 923 */ 924 if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) { 925 dt_dprintf("upgrading pid %d\n", (int)pid); 926 dpr->dpr_stale = B_TRUE; 927 dpr->dpr_cacheable = B_FALSE; 928 dph->dph_lrucnt--; 929 break; 930 } 931 932 dt_dprintf("grabbed pid %d (cached)\n", (int)pid); 933 dt_list_delete(&dph->dph_lrulist, dpr); 934 dt_list_prepend(&dph->dph_lrulist, dpr); 935 dpr->dpr_refs++; 936 return (dpr->dpr_proc); 937 } 938 } 939 940 if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL) 941 return (NULL); /* errno is set for us */ 942 943 (void) pthread_mutex_init(&dpr->dpr_lock, NULL); 944 (void) pthread_cond_init(&dpr->dpr_cv, NULL); 945 946 if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) { 947 return (dt_proc_error(dtp, dpr, 948 "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err))); 949 } 950 951 dpr->dpr_hdl = dtp; 952 dpr->dpr_pid = pid; 953 954 (void) Punsetflags(dpr->dpr_proc, PR_KLC); 955 (void) Psetflags(dpr->dpr_proc, PR_RLC); 956 957 /* 958 * If we are attempting to grab the process without a monitor 959 * thread, then mark the process cacheable only if it's being 960 * grabbed read-only. If we're currently caching more process 961 * handles than dph_lrulim permits, attempt to find the 962 * least-recently-used handle that is currently unreferenced and 963 * release it from the cache. Otherwise we are grabbing the process 964 * for control: create a control thread for this process and store 965 * its ID in dpr->dpr_tid. 966 */ 967 if (nomonitor || (flags & PGRAB_RDONLY)) { 968 if (dph->dph_lrucnt >= dph->dph_lrulim) { 969 for (opr = dt_list_prev(&dph->dph_lrulist); 970 opr != NULL; opr = dt_list_prev(opr)) { 971 if (opr->dpr_cacheable && opr->dpr_refs == 0) { 972 dt_proc_destroy(dtp, opr->dpr_proc); 973 break; 974 } 975 } 976 } 977 978 if (flags & PGRAB_RDONLY) { 979 dpr->dpr_cacheable = B_TRUE; 980 dpr->dpr_rdonly = B_TRUE; 981 dph->dph_lrucnt++; 982 } 983 984 } else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0) 985 return (NULL); /* dt_proc_error() has been called for us */ 986 987 dpr->dpr_hash = dph->dph_hash[h]; 988 dph->dph_hash[h] = dpr; 989 dt_list_prepend(&dph->dph_lrulist, dpr); 990 991 dt_dprintf("grabbed pid %d\n", (int)pid); 992 dpr->dpr_refs++; 993 994 return (dpr->dpr_proc); 995 } 996 997 void 998 dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P) 999 { 1000 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 1001 dt_proc_hash_t *dph = dtp->dt_procs; 1002 1003 assert(dpr != NULL); 1004 assert(dpr->dpr_refs != 0); 1005 1006 if (--dpr->dpr_refs == 0 && 1007 (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim)) 1008 dt_proc_destroy(dtp, P); 1009 } 1010 1011 void 1012 dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P) 1013 { 1014 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 1015 1016 (void) pthread_mutex_lock(&dpr->dpr_lock); 1017 1018 if (dpr->dpr_stop & DT_PROC_STOP_IDLE) { 1019 dpr->dpr_stop &= ~DT_PROC_STOP_IDLE; 1020 (void) pthread_cond_broadcast(&dpr->dpr_cv); 1021 } 1022 1023 (void) pthread_mutex_unlock(&dpr->dpr_lock); 1024 } 1025 1026 void 1027 dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P) 1028 { 1029 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 1030 int err = pthread_mutex_lock(&dpr->dpr_lock); 1031 assert(err == 0); /* check for recursion */ 1032 } 1033 1034 void 1035 dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P) 1036 { 1037 dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE); 1038 int err = pthread_mutex_unlock(&dpr->dpr_lock); 1039 assert(err == 0); /* check for unheld lock */ 1040 } 1041 1042 void 1043 dt_proc_hash_create(dtrace_hdl_t *dtp) 1044 { 1045 if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) + 1046 sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) { 1047 1048 (void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL); 1049 (void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL); 1050 1051 dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets; 1052 dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim; 1053 } 1054 } 1055 1056 void 1057 dt_proc_hash_destroy(dtrace_hdl_t *dtp) 1058 { 1059 dt_proc_hash_t *dph = dtp->dt_procs; 1060 dt_proc_t *dpr; 1061 1062 while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL) 1063 dt_proc_destroy(dtp, dpr->dpr_proc); 1064 1065 dtp->dt_procs = NULL; 1066 dt_free(dtp, dph); 1067 } 1068 1069 struct ps_prochandle * 1070 dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv) 1071 { 1072 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target"); 1073 struct ps_prochandle *P = dt_proc_create(dtp, file, argv); 1074 1075 if (P != NULL && idp != NULL && idp->di_id == 0) 1076 idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */ 1077 1078 return (P); 1079 } 1080 1081 struct ps_prochandle * 1082 dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags) 1083 { 1084 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target"); 1085 struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0); 1086 1087 if (P != NULL && idp != NULL && idp->di_id == 0) 1088 idp->di_id = pid; /* $target = grabbed pid */ 1089 1090 return (P); 1091 } 1092 1093 void 1094 dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P) 1095 { 1096 dt_proc_release(dtp, P); 1097 } 1098 1099 void 1100 dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P) 1101 { 1102 dt_proc_continue(dtp, P); 1103 } 1104