54184ee2f3aefd7b5e5712d7d38153f807510518
[staptrace.git] / src / staptrace.stp
1 #! /usr/bin/stap
2
3 # Copyright (C) 2010 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # ` = 0' is there to prevent: WARNING: never-assigned global variable
19
20 # `opt_f' is a boolean for both -f and -ff.
21 global opt_f = 0
22 # Modify the [pid %5d] output to [%d] for the -ff postprocessor.
23 global opt_ff = 0
24 global opt_q = 0
25 # Override target().
26 global opt_child = 0
27
28 global trace
29 global last_tid
30 probe begin
31 {
32   tid = opt_child > 0 ? opt_child : target ()
33   trace[tid] = 1
34   last_tid = tid
35 }
36
37 function heading (tid:long)
38 {
39   if (opt_ff)
40     return printf ("[%d]", tid)
41
42   foreach (tidi in trace limit 2)
43     tid_count++
44
45   if (tid_count > 1)
46     {
47       /* strace really calls TID a "pid".  */
48       return printf ("[pid %5d] ", tid)
49     }
50 }
51
52 global funcname
53 global nest
54
55 function realnl ()
56 {
57   if (opt_ff)
58     print ("n\n")
59   else
60     print ("\n")
61 }
62
63 probe end
64 {
65   if (nest[last_tid] && funcname[last_tid] != "")
66     {
67       printf (") = ? <tracer terminated>")
68       realnl ()
69     }
70   foreach (tid in nest)
71     if (tid != last_tid)
72       {
73         heading (tid)
74         printf ("<... %s resumed> ) = ? <tracer terminated>",
75                 funcname[tid] != "" ? funcname[tid] : "?");
76         realnl ()
77       }
78 }
79
80 /* BUG: sleeping function called from invalid context at kernel/mutex.c:94  */
81 %{static DEFINE_MUTEX (tracer_mutex);%}
82 function lock:long ()
83 %{
84   if (in_atomic () || irqs_disabled())
85     {
86       THIS->__retvalue = 0;
87       return;
88     }
89   mutex_lock (&tracer_mutex);
90   THIS->__retvalue = 1;
91 %}
92 function unlock ()
93 %{
94   mutex_unlock (&tracer_mutex);
95 %}
96 /*
97 # systamtap bug? on void function value use no warning is given.
98 global lockvar
99 function lock:long ()
100 {
101   lockvar++
102   return 1
103 }
104 function unlock ()
105 {
106   lockvar--
107 }
108 */
109
110 #probe kprocess.start
111 #{
112 #  trace[pid ()] = 1
113 #}
114 #probe kprocess.exit
115 #{
116 #  trace[pid ()] = 0
117 #}
118 probe kprocess.create
119 {
120   if (opt_f && trace[tid ()] != 0)
121     {
122       new_tid = task_tid (task)
123       trace[new_tid] = 1
124       if (!opt_q)
125         {
126           printf ("Process %d attached", new_tid);
127           realnl ()
128         }
129     }
130 }
131
132 probe kprocess.release
133 {
134   tid = task_tid (task)
135   if (trace[tid] != 0)
136     {
137       if (nest[tid])
138         {
139           heading (tid)
140           /* FIXME: Do not assume "exit" for the nested calls.  */
141           printf ("<... %s resumed> = ?",
142                   funcname[tid] != "" ? funcname[tid] : "exit")
143           realnl ()
144           delete nest[tid]
145           delete funcname[tid]
146         }
147       delete trace[tid]
148       if (!opt_q)
149         {
150           printf ("Process %d detached", tid);
151           realnl ()
152         }
153     }
154 }
155 probe syscall.*
156 {
157   if (trace[tid ()] != 0 && lock ())
158     {
159       /* Why is mmap() called recursively twice?  */
160       if (nest[last_tid] && funcname[last_tid] != "")
161         {
162           if (last_tid != tid ())
163             {
164               if (opt_ff)
165                 print ("c\n")
166               else
167                 print (" <unfinished ...>\n")
168             }
169           else
170             {
171               print (" <unfinished ...>")
172               realnl ()
173             }
174         }
175       last_tid = tid ();
176       funcname[tid ()] = name
177       nest[tid ()]++
178       heading (tid ())
179       printf ("%s(%s",name, argstr)
180       unlock ()
181     }
182 }
183 probe syscall.*.return
184 {
185   if (trace[tid ()] != 0 && lock ())
186     {
187       if (last_tid != tid () && nest[last_tid] && funcname[last_tid] != "")
188         {
189           if (opt_ff)
190             print ("c\n")
191           else
192             print (" <unfinished ...>\n")
193         }
194       if (last_tid != tid ())
195         {
196           heading (tid ())
197           if (!opt_ff)
198             printf ("<... %s resumed> ", name);
199         }
200       if (last_tid == tid () && funcname[last_tid] == "")
201         {
202           heading (tid ())
203           printf ("<... %s resumed> ", name);
204         }
205       last_tid = tid ();
206       printf (") = %s", retstr)
207       realnl ()
208       if (!--nest[tid ()])
209         delete nest[tid ()]
210       delete funcname[tid ()]
211       unlock ()
212     }
213 }