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
--- 6 unchanged lines hidden (view full) ---
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 * Portions Copyright 2010 The FreeBSD Foundation
22 *
23 * $FreeBSD: head/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c 258311 2013-11-18 16:51:56Z asomers $
24 */
25
26/*
27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31#if defined(sun)
--- 24 unchanged lines hidden (view full) ---
56#include <sys/proc.h>
57#include <sys/policy.h>
58#if defined(sun)
59#include <util/qsort.h>
60#endif
61#include <sys/mutex.h>
62#include <sys/kernel.h>
63#if !defined(sun)
64#include <sys/user.h>
65#include <sys/dtrace_bsd.h>
66#include <cddl/dev/dtrace/dtrace_cddl.h>
67#endif
68
69/*
70 * User-Land Trap-Based Tracing
71 * ----------------------------
72 *
73 * The fasttrap provider allows DTrace consumers to instrument any user-level
--- 127 unchanged lines hidden (view full) ---
201static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
202 const dtrace_pattr_t *);
203static void fasttrap_provider_retire(pid_t, const char *, int);
204static void fasttrap_provider_free(fasttrap_provider_t *);
205
206static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
207static void fasttrap_proc_release(fasttrap_proc_t *);
208
209#define FASTTRAP_PROVS_INDEX(pid, name) \
210 ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
211
212#define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
213
214#if !defined(sun)
215static kmutex_t fasttrap_cpuc_pid_lock[MAXCPU];
216#endif
217
218static int
219fasttrap_highbit(ulong_t i)
220{
221 int h = 1;
222
223 if (i == 0)
--- 60 unchanged lines hidden (view full) ---
284 ksi->ksi_code = TRAP_DTRACE;
285 ksi->ksi_addr = (caddr_t)pc;
286 PROC_LOCK(p);
287 (void) tdksignal(t, SIGTRAP, ksi);
288 PROC_UNLOCK(p);
289#endif
290}
291
292/*
293 * This function ensures that no threads are actively using the memory
294 * associated with probes that were formerly live.
295 */
296static void
297fasttrap_mod_barrier(uint64_t gen)
298{
299 int i;
300
--- 143 unchanged lines hidden (view full) ---
444 * This is called from cfork() via dtrace_fasttrap_fork(). The child
445 * process's address space is (roughly) a copy of the parent process's so
446 * we have to remove all the instrumentation we had previously enabled in the
447 * parent.
448 */
449static void
450fasttrap_fork(proc_t *p, proc_t *cp)
451{
452 pid_t ppid = p->p_pid;
453 int i;
454
455#if defined(sun)
456 ASSERT(curproc == p);
457 ASSERT(p->p_proc_flag & P_PR_LOCK);
458#else
459 PROC_LOCK_ASSERT(p, MA_OWNED);
--- 69 unchanged lines hidden (view full) ---
529 /*
530 * The count of active providers can only be
531 * decremented (i.e. to zero) during exec,
532 * exit, and removal of a meta provider so it
533 * should be impossible to drop the count
534 * mid-fork.
535 */
536 ASSERT(tp->ftt_proc->ftpc_acount != 0);
537 }
538 }
539 mutex_exit(&bucket->ftb_mtx);
540 }
541
542#if defined(sun)
543 mutex_enter(&cp->p_lock);
544 sprunlock(cp);
545#else
546 PROC_LOCK(p);
547 PROC_LOCK(cp);
--- 4 unchanged lines hidden (view full) ---
552/*
553 * This is called from proc_exit() or from exec_common() if p_dtrace_probes
554 * is set on the proc structure to indicate that there is a pid provider
555 * associated with this process.
556 */
557static void
558fasttrap_exec_exit(proc_t *p)
559{
560#if defined(sun)
561 ASSERT(p == curproc);
562#endif
563 PROC_LOCK_ASSERT(p, MA_OWNED);
564 _PHOLD(p);
565 PROC_UNLOCK(p);
566
567 /*
568 * We clean up the pid provider for this process here; user-land
569 * static probes are handled by the meta-provider remove entry point.
570 */
571 fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
572#if !defined(sun)
573 if (p->p_dtrace_helpers)
574 dtrace_helpers_destroy(p);
575#endif
576 PROC_LOCK(p);
577 _PRELE(p);
578}
579
580
581/*ARGSUSED*/
582static void
583fasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc)
584{
585 /*
--- 776 unchanged lines hidden (view full) ---
1362}
1363
1364static void
1365fasttrap_proc_release(fasttrap_proc_t *proc)
1366{
1367 fasttrap_bucket_t *bucket;
1368 fasttrap_proc_t *fprc, **fprcp;
1369 pid_t pid = proc->ftpc_pid;
1370
1371 mutex_enter(&proc->ftpc_mtx);
1372
1373 ASSERT(proc->ftpc_rcount != 0);
1374 ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1375
1376 if (--proc->ftpc_rcount != 0) {
1377 mutex_exit(&proc->ftpc_mtx);
1378 return;
1379 }
1380
1381 mutex_exit(&proc->ftpc_mtx);
1382
1383 /*
1384 * There should definitely be no live providers associated with this
1385 * process at this point.
1386 */
1387 ASSERT(proc->ftpc_acount == 0);
1388
--- 969 unchanged lines hidden (view full) ---
2358 for (i = 0; i < fasttrap_procs.fth_nent; i++)
2359 mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx,
2360 "processes bucket mtx", MUTEX_DEFAULT, NULL);
2361
2362 CPU_FOREACH(i) {
2363 mutex_init(&fasttrap_cpuc_pid_lock[i], "fasttrap barrier",
2364 MUTEX_DEFAULT, NULL);
2365 }
2366#endif
2367
2368 /*
2369 * Install our hooks into fork(2), exec(2), and exit(2).
2370 */
2371 dtrace_fasttrap_fork = &fasttrap_fork;
2372 dtrace_fasttrap_exit = &fasttrap_exec_exit;
2373 dtrace_fasttrap_exec = &fasttrap_exec_exit;
--- 85 unchanged lines hidden (view full) ---
2459
2460#ifdef DEBUG
2461 mutex_enter(&fasttrap_count_mtx);
2462 ASSERT(fasttrap_pid_count == 0);
2463 mutex_exit(&fasttrap_count_mtx);
2464#endif
2465
2466#if !defined(sun)
2467 for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2468 mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx);
2469 for (i = 0; i < fasttrap_provs.fth_nent; i++)
2470 mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx);
2471 for (i = 0; i < fasttrap_procs.fth_nent; i++)
2472 mutex_destroy(&fasttrap_procs.fth_table[i].ftb_mtx);
2473#endif
2474 kmem_free(fasttrap_tpoints.fth_table,
--- 54 unchanged lines hidden ---
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
--- 6 unchanged lines hidden (view full) ---
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 * Portions Copyright 2010 The FreeBSD Foundation
22 *
23 * $FreeBSD: head/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c 258311 2013-11-18 16:51:56Z asomers $
24 */
25
26/*
27 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31#if defined(sun)
--- 24 unchanged lines hidden (view full) ---
56#include <sys/proc.h>
57#include <sys/policy.h>
58#if defined(sun)
59#include <util/qsort.h>
60#endif
61#include <sys/mutex.h>
62#include <sys/kernel.h>
63#if !defined(sun)
64#include <sys/user.h>
65#include <sys/dtrace_bsd.h>
66#include <cddl/dev/dtrace/dtrace_cddl.h>
67#endif
68
69/*
70 * User-Land Trap-Based Tracing
71 * ----------------------------
72 *
73 * The fasttrap provider allows DTrace consumers to instrument any user-level
--- 127 unchanged lines hidden (view full) ---
201static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
202 const dtrace_pattr_t *);
203static void fasttrap_provider_retire(pid_t, const char *, int);
204static void fasttrap_provider_free(fasttrap_provider_t *);
205
206static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
207static void fasttrap_proc_release(fasttrap_proc_t *);
208
209#define FASTTRAP_PROVS_INDEX(pid, name) \
210 ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
211
212#define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
213
214#if !defined(sun)
215static kmutex_t fasttrap_cpuc_pid_lock[MAXCPU];
216#endif
217
218static int
219fasttrap_highbit(ulong_t i)
220{
221 int h = 1;
222
223 if (i == 0)
--- 60 unchanged lines hidden (view full) ---
284 ksi->ksi_code = TRAP_DTRACE;
285 ksi->ksi_addr = (caddr_t)pc;
286 PROC_LOCK(p);
287 (void) tdksignal(t, SIGTRAP, ksi);
288 PROC_UNLOCK(p);
289#endif
290}
291
292/*
293 * This function ensures that no threads are actively using the memory
294 * associated with probes that were formerly live.
295 */
296static void
297fasttrap_mod_barrier(uint64_t gen)
298{
299 int i;
300
--- 143 unchanged lines hidden (view full) ---
444 * This is called from cfork() via dtrace_fasttrap_fork(). The child
445 * process's address space is (roughly) a copy of the parent process's so
446 * we have to remove all the instrumentation we had previously enabled in the
447 * parent.
448 */
449static void
450fasttrap_fork(proc_t *p, proc_t *cp)
451{
452 pid_t ppid = p->p_pid;
453 int i;
454
455#if defined(sun)
456 ASSERT(curproc == p);
457 ASSERT(p->p_proc_flag & P_PR_LOCK);
458#else
459 PROC_LOCK_ASSERT(p, MA_OWNED);
--- 69 unchanged lines hidden (view full) ---
529 /*
530 * The count of active providers can only be
531 * decremented (i.e. to zero) during exec,
532 * exit, and removal of a meta provider so it
533 * should be impossible to drop the count
534 * mid-fork.
535 */
536 ASSERT(tp->ftt_proc->ftpc_acount != 0);
537 }
538 }
539 mutex_exit(&bucket->ftb_mtx);
540 }
541
542#if defined(sun)
543 mutex_enter(&cp->p_lock);
544 sprunlock(cp);
545#else
546 PROC_LOCK(p);
547 PROC_LOCK(cp);
--- 4 unchanged lines hidden (view full) ---
552/*
553 * This is called from proc_exit() or from exec_common() if p_dtrace_probes
554 * is set on the proc structure to indicate that there is a pid provider
555 * associated with this process.
556 */
557static void
558fasttrap_exec_exit(proc_t *p)
559{
560#if defined(sun)
561 ASSERT(p == curproc);
562#endif
563 PROC_LOCK_ASSERT(p, MA_OWNED);
564 _PHOLD(p);
565 PROC_UNLOCK(p);
566
567 /*
568 * We clean up the pid provider for this process here; user-land
569 * static probes are handled by the meta-provider remove entry point.
570 */
571 fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
572#if !defined(sun)
573 if (p->p_dtrace_helpers)
574 dtrace_helpers_destroy(p);
575#endif
576 PROC_LOCK(p);
577 _PRELE(p);
578}
579
580
581/*ARGSUSED*/
582static void
583fasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc)
584{
585 /*
--- 776 unchanged lines hidden (view full) ---
1362}
1363
1364static void
1365fasttrap_proc_release(fasttrap_proc_t *proc)
1366{
1367 fasttrap_bucket_t *bucket;
1368 fasttrap_proc_t *fprc, **fprcp;
1369 pid_t pid = proc->ftpc_pid;
1370
1371 mutex_enter(&proc->ftpc_mtx);
1372
1373 ASSERT(proc->ftpc_rcount != 0);
1374 ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1375
1376 if (--proc->ftpc_rcount != 0) {
1377 mutex_exit(&proc->ftpc_mtx);
1378 return;
1379 }
1380
1381 mutex_exit(&proc->ftpc_mtx);
1382
1383 /*
1384 * There should definitely be no live providers associated with this
1385 * process at this point.
1386 */
1387 ASSERT(proc->ftpc_acount == 0);
1388
--- 969 unchanged lines hidden (view full) ---
2358 for (i = 0; i < fasttrap_procs.fth_nent; i++)
2359 mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx,
2360 "processes bucket mtx", MUTEX_DEFAULT, NULL);
2361
2362 CPU_FOREACH(i) {
2363 mutex_init(&fasttrap_cpuc_pid_lock[i], "fasttrap barrier",
2364 MUTEX_DEFAULT, NULL);
2365 }
2366#endif
2367
2368 /*
2369 * Install our hooks into fork(2), exec(2), and exit(2).
2370 */
2371 dtrace_fasttrap_fork = &fasttrap_fork;
2372 dtrace_fasttrap_exit = &fasttrap_exec_exit;
2373 dtrace_fasttrap_exec = &fasttrap_exec_exit;
--- 85 unchanged lines hidden (view full) ---
2459
2460#ifdef DEBUG
2461 mutex_enter(&fasttrap_count_mtx);
2462 ASSERT(fasttrap_pid_count == 0);
2463 mutex_exit(&fasttrap_count_mtx);
2464#endif
2465
2466#if !defined(sun)
2467 for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2468 mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx);
2469 for (i = 0; i < fasttrap_provs.fth_nent; i++)
2470 mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx);
2471 for (i = 0; i < fasttrap_procs.fth_nent; i++)
2472 mutex_destroy(&fasttrap_procs.fth_table[i].ftb_mtx);
2473#endif
2474 kmem_free(fasttrap_tpoints.fth_table,
--- 54 unchanged lines hidden ---