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