1 #!/usr/sbin/dtrace -qs
2 
3 /*-
4  * Copyright (c) 2008-2012 Alexander Leidinger <netchild@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 /**
32  * Check if the internal locks are correctly acquired/released:
33  *  - no recursive locking (mtx locks, write locks)
34  *  - no unlocking of already unlocked one
35  *
36  * Print stacktrace if a lock is longer locked than about 10sec or more.
37  */
38 
39 #pragma D option dynvarsize=32m
40 #pragma D option specsize=32m
41 
42 BEGIN
43 {
44 	check["emul_lock"] = 0;
45 	check["emul_shared_rlock"] = 0;
46 	check["emul_shared_wlock"] = 0;
47 	check["futex_mtx"] = 0;
48 }
49 
50 linuxulator*:locks:emul_lock:locked,
51 linuxulator*:locks:emul_shared_wlock:locked,
52 linuxulator*:locks:futex_mtx:locked
53 /check[probefunc] > 0/
54 {
55 	printf("ERROR: recursive lock of %s (%p),", probefunc, arg0);
56 	printf("       or missing SDT probe in kernel. Stack trace follows:");
57 	stack();
58 }
59 
60 linuxulator*:locks:emul_lock:locked,
61 linuxulator*:locks:emul_shared_rlock:locked,
62 linuxulator*:locks:emul_shared_wlock:locked,
63 linuxulator*:locks:futex_mtx:locked
64 {
65 	++check[probefunc];
66 	@stats[probefunc] = count();
67 
68 	ts[probefunc] = timestamp;
69 	spec[probefunc] = speculation();
70 }
71 
72 linuxulator*:locks:emul_lock:unlock,
73 linuxulator*:locks:emul_shared_rlock:unlock,
74 linuxulator*:locks:emul_shared_wlock:unlock,
75 linuxulator*:locks:futex_mtx:unlock
76 /check[probefunc] == 0/
77 {
78 	printf("ERROR: unlock attemt of unlocked %s (%p),", probefunc, arg0);
79 	printf("       missing SDT probe in kernel, or dtrace program started");
80 	printf("       while the %s was already held (race condition).", probefunc);
81 	printf("       Stack trace follows:");
82 	stack();
83 }
84 
85 linuxulator*:locks:emul_lock:unlock,
86 linuxulator*:locks:emul_shared_rlock:unlock,
87 linuxulator*:locks:emul_shared_wlock:unlock,
88 linuxulator*:locks:futex_mtx:unlock
89 {
90 	discard(spec[probefunc]);
91 	spec[probefunc] = 0;
92 	--check[probefunc];
93 }
94 
95 /* Timeout handling */
96 
97 tick-10s
98 /spec["emul_lock"] != 0 && timestamp - ts["emul_lock"] >= 9999999000/
99 {
100 	commit(spec["emul_lock"]);
101 	spec["emul_lock"] = 0;
102 }
103 
104 tick-10s
105 /spec["emul_shared_wlock"] != 0 && timestamp - ts["emul_shared_wlock"] >= 9999999000/
106 {
107 	commit(spec["emul_shared_wlock"]);
108 	spec["emul_shared_wlock"] = 0;
109 }
110 
111 tick-10s
112 /spec["emul_shared_rlock"] != 0 && timestamp - ts["emul_shared_rlock"] >= 9999999000/
113 {
114 	commit(spec["emul_shared_rlock"]);
115 	spec["emul_shared_rlock"] = 0;
116 }
117 
118 tick-10s
119 /spec["futex_mtx"] != 0 && timestamp - ts["futex_mtx"] >= 9999999000/
120 {
121 	commit(spec["futex_mtx"]);
122 	spec["futex_mtx"] = 0;
123 }
124 
125 
126 /* Statistics */
127 
128 END
129 {
130         printf("Number of locks per type:");
131 	printa(@stats);
132 }
133