Implemented -ff.
[staptrace.git] / src / staptrace.stp
index 24e7775..263a5b4 100755 (executable)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # ` = 0' is there to prevent: WARNING: never-assigned global variable
+
+# `opt_f' is a boolean for both -f and -ff.
 global opt_f = 0
+# Modify the [pid %5d] output to [%d] for the -ff postprocessor.
+global opt_ff = 0
 global opt_q = 0
 # Override target().
 global opt_child = 0
@@ -33,31 +37,47 @@ probe begin
   last_tid = tid
 }
 
-function heading:string (tid:long)
+function heading (tid:long)
 {
+  if (opt_ff)
+    return printf ("[%d]", tid)
+
   foreach (tidi in trace limit 2)
     tid_count++
 
   if (tid_count > 1)
     {
       /* strace really calls TID a "pid".  */
-      return sprintf ("[pid %5d] ", tid)
+      return printf ("[pid %5d] ", tid)
     }
-  else
-    return ""
 }
 
 global funcname
 global nest
 
+function realnl ()
+{
+  if (opt_ff)
+    print ("n\n")
+  else
+    print ("\n")
+}
+
 probe end
 {
   if (nest[last_tid])
-    printf (") = ? <tracer terminated>\n")
+    {
+      printf (") = ? <tracer terminated>")
+      realnl ()
+    }
   foreach (tid in nest)
     if (tid != last_tid)
-      printf ("%s<... %s resumed> ) = ? <tracer terminated>\n", heading (tid),
-             funcname[tid] != "" ? funcname[tid] : "?");
+      {
+       heading (tid)
+       printf ("<... %s resumed> ) = ? <tracer terminated>",
+               funcname[tid] != "" ? funcname[tid] : "?");
+       realnl ()
+      }
 }
 
 /* BUG: sleeping function called from invalid context at kernel/mutex.c:94
@@ -97,7 +117,10 @@ probe kprocess.create
       new_tid = task_tid (task)
       trace[new_tid] = 1
       if (!opt_q)
-       printf ("Process %d attached\n", new_tid);
+       {
+         printf ("Process %d attached", new_tid);
+         realnl ()
+       }
     }
 }
 
@@ -111,15 +134,20 @@ probe kprocess.release
     {
       if (nest[tid])
        {
+         heading (tid)
          /* FIXME: Do not assume "exit" for the nested calls.  */
-         printf ("%s<... %s resumed> = ?\n", heading (tid),
+         printf ("<... %s resumed> = ?",
                  funcname[tid] != "" ? funcname[tid] : "exit")
+         realnl ()
          delete nest[tid]
          delete funcname[tid]
        }
       delete trace[tid]
       if (!opt_q)
-       printf ("Process %d detached\n", tid);
+       {
+         printf ("Process %d detached", tid);
+         realnl ()
+       }
     }
 }
 probe syscall.*
@@ -129,11 +157,25 @@ probe syscall.*
       lock ()
       /* Why is mmap() called recursively twice?  */
       if (nest[last_tid])
-       printf (" <unfinished ...>\n")
+       {
+         if (last_tid != tid ())
+           {
+             if (opt_ff)
+               print ("c\n")
+             else
+               print (" <unfinished ...>\n")
+           }
+         else
+           {
+             print (" <unfinished ...>")
+             realnl ()
+           }
+       }
       last_tid = tid ();
       funcname[tid ()] = name
       nest[tid ()]++
-      printf ("%s%s(%s", heading (tid ()), name, argstr)
+      heading (tid ())
+      printf ("%s(%s",name, argstr)
       unlock ()
     }
 }
@@ -143,11 +185,26 @@ probe syscall.*.return
     {
       lock ()
       if (last_tid != tid () && nest[last_tid])
-       printf (" <unfinished ...>\n")
-      if (last_tid != tid () || (last_tid == tid () && !nest[last_tid]))
-       printf ("%s<... %s resumed> ", heading (tid ()), name);
+       {
+         if (opt_ff)
+           print ("c\n")
+         else
+           print (" <unfinished ...>\n")
+       }
+      if (last_tid != tid ())
+       {
+         heading (tid ())
+         if (!opt_ff)
+           printf ("<... %s resumed> ", name);
+       }
+      if (last_tid == tid () && funcname[last_tid] == "")
+       {
+         heading (tid ())
+         printf ("<... %s resumed> ", name);
+       }
       last_tid = tid ();
-      printf (") = %s\n", retstr)
+      printf (") = %s", retstr)
+      realnl ()
       if (!--nest[tid ()])
        delete nest[tid ()]
       delete funcname[tid ()]