Patchwork [v7,04/11] trace: [tracetool] Simplify event line parsing

login
register
mail settings
Submitter Lluís Vilanova
Date March 21, 2012, 9:52 p.m.
Message ID <20120321215249.3272.91401.stgit@ginnungagap.bsc.es>
Download mbox | patch
Permalink /patch/148104/
State New
Headers show

Comments

Lluís Vilanova - March 21, 2012, 9:52 p.m.
Using regular expressions yields more compact and error-proof code when breaking
the lines into pieces.

Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
Signed-off-by: Harsh Prateek Bora <harsh@linux.vnet.ibm.com>
---
 scripts/tracetool.py |   43 +++++++++++++++----------------------------
 1 files changed, 15 insertions(+), 28 deletions(-)

Patch

diff --git a/scripts/tracetool.py b/scripts/tracetool.py
index ff1519c..4f9060f 100755
--- a/scripts/tracetool.py
+++ b/scripts/tracetool.py
@@ -10,6 +10,7 @@ 
 
 import sys
 import getopt
+import re
 
 def usage():
     print "Tracetool: Generate tracing code for trace events file on stdin"
@@ -38,25 +39,14 @@  Options:
 '''
     sys.exit(1)
 
-def get_name(line, sep='('):
-    head, sep, tail = line.partition(sep)
-    property, sep, name = head.rpartition(' ')
-    return name
-
 def get_properties(line, sep='('):
     head, sep, tail = line.partition(sep)
     property, sep, name = head.rpartition(' ')
     return property.split()
 
-def get_args(line, sep1='(', sep2=')'):
-    head, sep1, tail = line.partition(sep1)
-    args, sep2, fmt_str = tail.partition(sep2)
-    return args
-
-def get_argnames(line, sep=','):
+def get_argnames(args):
     nfields = 0
     str = []
-    args = get_args(line)
     for field in args.split():
         nfields = nfields + 1
         # Drop pointer star
@@ -77,17 +67,6 @@  def get_argnames(line, sep=','):
     else:
         return ''
 
-def get_argc(line):
-    argc = 0
-    argnames = get_argnames(line)
-    if argnames:
-        for name in argnames.split(','):
-            argc = argc + 1
-    return argc
-
-def get_fmt(line, sep=')'):
-    event, sep, fmt = line.partition(sep)
-    return fmt.rstrip('\n')
 
 def trace_h_begin():
     print '''#ifndef TRACE_H
@@ -400,15 +379,23 @@  trace_gen = {
 }
 
 # A trace event
+cre = re.compile("(?P<name>[^(\s]+)\((?P<args>[^)]*)\)\s*(?P<fmt>\".*)?")
+
 class Event(object):
     def __init__(self, num, line):
         self.num = num
-        self.args = get_args(line)
+        m = cre.match(line)
+        assert m is not None
+        groups = m.groupdict('')
+        self.args = groups["args"]
         self.arglist = self.args.split(',')
-        self.name = get_name(line)
-        self.argc = get_argc(line)
-        self.argnames = get_argnames(line)
-        self.fmt = get_fmt(line)
+        self.name = groups["name"]
+        if len(self.arglist) == 1 and self.arglist[0] == "void":
+            self.argc = 0
+        else:
+            self.argc = len(self.arglist)
+        self.argnames = get_argnames(self.args)
+        self.fmt = groups["fmt"]
         self.properties = get_properties(line)
 
 # Generator that yields Event objects given a trace-events file object