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