filed: http://sourceware.org/bugzilla/show_bug.cgi?id=12414
[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 global lockvar
98 function lock:long ()
99 {
100   lockvar++
101   return 1
102 }
103 function unlock ()
104 {
105   lockvar--
106 }
107 */
108
109 #probe kprocess.start
110 #{
111 #  trace[pid ()] = 1
112 #}
113 #probe kprocess.exit
114 #{
115 #  trace[pid ()] = 0
116 #}
117 probe kprocess.create
118 {
119   if (opt_f && trace[tid ()] != 0)
120     {
121       new_tid = task_tid (task)
122       trace[new_tid] = 1
123       if (!opt_q)
124         {
125           printf ("Process %d attached", new_tid);
126           realnl ()
127         }
128     }
129 }
130
131 probe kprocess.release
132 {
133   tid = task_tid (task)
134   if (trace[tid] != 0)
135     {
136       if (nest[tid])
137         {
138           heading (tid)
139           /* FIXME: Do not assume "exit" for the nested calls.  */
140           printf ("<... %s resumed> = ?",
141                   funcname[tid] != "" ? funcname[tid] : "exit")
142           realnl ()
143           delete nest[tid]
144           delete funcname[tid]
145         }
146       delete trace[tid]
147       if (!opt_q)
148         {
149           printf ("Process %d detached", tid);
150           realnl ()
151         }
152     }
153 }
154 probe syscall.*
155 {
156   if (trace[tid ()] != 0 && lock ())
157     {
158       /* Why is mmap() called recursively twice?  */
159       if (nest[last_tid] && funcname[last_tid] != "")
160         {
161           if (last_tid != tid ())
162             {
163               if (opt_ff)
164                 print ("c\n")
165               else
166                 print (" <unfinished ...>\n")
167             }
168           else
169             {
170               print (" <unfinished ...>")
171               realnl ()
172             }
173         }
174       last_tid = tid ();
175       funcname[tid ()] = name
176       nest[tid ()]++
177       heading (tid ())
178       printf ("%s(%s",name, argstr)
179       unlock ()
180     }
181 }
182 probe syscall.*.return
183 {
184   if (trace[tid ()] != 0 && lock ())
185     {
186       if (last_tid != tid () && nest[last_tid] && funcname[last_tid] != "")
187         {
188           if (opt_ff)
189             print ("c\n")
190           else
191             print (" <unfinished ...>\n")
192         }
193       if (last_tid != tid ())
194         {
195           heading (tid ())
196           if (!opt_ff)
197             printf ("<... %s resumed> ", name);
198         }
199       if (last_tid == tid () && funcname[last_tid] == "")
200         {
201           heading (tid ())
202           printf ("<... %s resumed> ", name);
203         }
204       last_tid = tid ();
205       printf (") = %s", retstr)
206       realnl ()
207       if (!--nest[tid ()])
208         delete nest[tid ()]
209       delete funcname[tid ()]
210       unlock ()
211     }
212 }