xref: /freebsd-13.1/share/dtrace/blocking (revision af71f40a)
1*af71f40aSPawel Jakub Dawidek#!/usr/sbin/dtrace -s
2*af71f40aSPawel Jakub Dawidek/*-
3*af71f40aSPawel Jakub Dawidek * Copyright (c) 2015 Pawel Jakub Dawidek <pawel@dawidek.net>
4*af71f40aSPawel Jakub Dawidek * All rights reserved.
5*af71f40aSPawel Jakub Dawidek *
6*af71f40aSPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without
7*af71f40aSPawel Jakub Dawidek * modification, are permitted provided that the following conditions
8*af71f40aSPawel Jakub Dawidek * are met:
9*af71f40aSPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright
10*af71f40aSPawel Jakub Dawidek *    notice, this list of conditions and the following disclaimer.
11*af71f40aSPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright
12*af71f40aSPawel Jakub Dawidek *    notice, this list of conditions and the following disclaimer in the
13*af71f40aSPawel Jakub Dawidek *    documentation and/or other materials provided with the distribution.
14*af71f40aSPawel Jakub Dawidek *
15*af71f40aSPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16*af71f40aSPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*af71f40aSPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*af71f40aSPawel Jakub Dawidek * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19*af71f40aSPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*af71f40aSPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*af71f40aSPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*af71f40aSPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*af71f40aSPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*af71f40aSPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*af71f40aSPawel Jakub Dawidek * SUCH DAMAGE.
26*af71f40aSPawel Jakub Dawidek *
27*af71f40aSPawel Jakub Dawidek * $FreeBSD$
28*af71f40aSPawel Jakub Dawidek *
29*af71f40aSPawel Jakub Dawidek * This little script is for use with programs that use event loop and should
30*af71f40aSPawel Jakub Dawidek * sleep only when waiting for events (eg. via kevent(2)). When a program is
31*af71f40aSPawel Jakub Dawidek * going to sleep in the kernel, the script will show its name, PID, kernel
32*af71f40aSPawel Jakub Dawidek * stack trace and userland stack trace. Sleeping in kevent(2) is ignored.
33*af71f40aSPawel Jakub Dawidek *
34*af71f40aSPawel Jakub Dawidek * usage: blocking <execname>
35*af71f40aSPawel Jakub Dawidek */
36*af71f40aSPawel Jakub Dawidek
37*af71f40aSPawel Jakub Dawidek#pragma D option quiet
38*af71f40aSPawel Jakub Dawidek
39*af71f40aSPawel Jakub Dawideksyscall::kevent:entry
40*af71f40aSPawel Jakub Dawidek/execname == $$1/
41*af71f40aSPawel Jakub Dawidek{
42*af71f40aSPawel Jakub Dawidek	self->inkevent = 1;
43*af71f40aSPawel Jakub Dawidek}
44*af71f40aSPawel Jakub Dawidek
45*af71f40aSPawel Jakub Dawidekfbt::sleepq_add:entry
46*af71f40aSPawel Jakub Dawidek/!self->inkevent && execname == $$1/
47*af71f40aSPawel Jakub Dawidek{
48*af71f40aSPawel Jakub Dawidek	printf("\n%s(%d) is blocking...\n", execname, pid);
49*af71f40aSPawel Jakub Dawidek	stack();
50*af71f40aSPawel Jakub Dawidek	ustack();
51*af71f40aSPawel Jakub Dawidek}
52*af71f40aSPawel Jakub Dawidek
53*af71f40aSPawel Jakub Dawideksyscall::kevent:return
54*af71f40aSPawel Jakub Dawidek/execname == $$1/
55*af71f40aSPawel Jakub Dawidek{
56*af71f40aSPawel Jakub Dawidek	self->inkevent = 0;
57*af71f40aSPawel Jakub Dawidek}
58