Return event fields by field name
authorXiaona Han <xiaonahappy13@163.com>
Thu, 25 Jul 2013 23:56:04 +0000 (07:56 +0800)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 12 Nov 2013 16:24:51 +0000 (11:24 -0500)
Since a field may be present both in an event and its stream's scope,
this returns a list which contains all the fields by the name. The
examples are also changed to match this new API.

Signed-off-by: Xiaona Han <xiaonahappy13@163.com>
Acked-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
bindings/python/babeltrace.i.in
bindings/python/examples/example-api-test.py
bindings/python/examples/sched_switch.py

index 24581604e57856713ceaa9744a26dd8092dfdc24..ee3e2bd0b8b44460bd109b7b07caee79adfbcc99 100644 (file)
@@ -744,19 +744,48 @@ class ctf:
                        """
                        return _bt_ctf_get_timestamp(self._e)
 
-               def get_field(self, scope, field):
-                       """Return the definition of a specific field."""
+               def get_field_with_scope(self, scope, field):
+                       """
+                       Return the definition of a specific field.
+                       Return None on error.
+                       """
                        evDef = ctf.Definition.__new__(ctf.Definition)
                        try:
                                evDef._d = _bt_ctf_get_field(self._e, scope._d, field)
                        except AttributeError:
                                raise TypeError("in get_field, argument 2 must be a "
                                        "Definition (scope) instance")
+                       if evDef._d is None:
+                               return None
+                       evDef._s = scope
                        return evDef
 
-               def get_field_list(self, scope):
+               def get_field(self, field):
+                       """
+                       Return the definition of fields by a name
+                       Return None on error
+                       """
+                       eventScope = self.get_top_level_scope(ctf.scope.EVENT_FIELDS)
+                       streamScope = self.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT)
+                       fields_by_name = []
+
+                       if eventScope is not None:
+                               evDef = self.get_field_with_scope(eventScope, field)
+                               if evDef is not None:
+                                       fields_by_name.append(evDef)
+
+                       if streamScope is not None:
+                               evDef = self.get_field_with_scope(streamScope, field)
+                               if evDef is not None:
+                                       fields_by_name.append(evDef);
+
+                       if not fields_by_name:
+                               return None
+                       return fields_by_name
+
+               def get_field_list_with_scope(self, scope):
                        """
-                       Return a list of Definitions
+                       Return a list of Definitions associated with the scope
                        Return None on error.
                        """
                        try:
@@ -779,10 +808,31 @@ class ctf:
                                        #_bt_python_field_listcaller
                                        break
 
+                               tmp._s = scope
                                def_list.append(tmp)
                                i += 1
                        return def_list
 
+               def get_field_list(self):
+                       """Return a list of Definitions or None on error."""
+                       eventScope = self.get_top_level_scope(ctf.scope.EVENT_FIELDS)
+                       streamScope = self.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT)
+
+                       def_list = []
+                       if eventScope is not None:
+                               event_field_list = self.get_field_list_with_scope(eventScope)
+                               if event_field_list is not None:
+                                       def_list = event_field_list
+
+                       if streamScope is not None:
+                               event_field_list = self.get_field_list_with_scope(streamScope)
+                               if event_field_list is not None:
+                                       def_list.extend(event_field_list)
+
+                       if not def_list:
+                               return None
+                       return def_list
+
                def get_index(self, field, index):
                        """
                        If the field is an array or a sequence, return the element
@@ -918,6 +968,9 @@ class ctf:
                        """
                        return _bt_ctf_get_string(self._d)
 
+               def get_scope(self):
+                       """Return the scope of a field or None on error."""
+                       return self._s
 
        class EventDecl(object):
                """Event declaration class.  Do not instantiate."""
index fc59e249d2df7c0075aa39f7a1972c9d3445fc66..5846facd574603efff3e84a185f04719d89ca2c8 100644 (file)
@@ -50,23 +50,19 @@ while(event is not None):
                event.get_cycles(), event.get_name()))
 
        if event.get_name() == "sched_switch":
-               sco = event.get_top_level_scope(ctf.scope.EVENT_FIELDS)
-               prev_field = event.get_field(sco, "_prev_comm")
-               prev_comm = prev_field.get_char_array()
-
-               if ctf.field_error():
+               prev_field = event.get_field("_prev_comm")
+               if prev_field is None:
                        print("ERROR: Missing prev_comm context info")
                else:
+                       prev_comm = prev_field[0].get_char_array()
                        print("sched_switch prev_comm: {}".format(prev_comm))
 
        if event.get_name() == "exit_syscall":
-               sco = event.get_top_level_scope(ctf.scope.EVENT_FIELDS)
-               ret_field = event.get_field(sco, "_ret")
-               ret_code = ret_field.get_int64()
-
-               if ctf.field_error():
+               ret_field = event.get_field("_ret")
+               if ret_field is None:
                        print("ERROR: Unable to extract ret")
                else:
+                       ret_code = ret_field[0].get_int64()
                        print("exit_syscall ret: {}".format(ret_code))
 
        ret = ctf_it.next()
index d5ed25bd6dfdb78b6696db15d52e71ab52469aa5..83c191f813f7cd7c8b99e5cfd8751727548ba232 100644 (file)
@@ -57,7 +57,7 @@ while event is not None:
                                break # Next event
 
                        # Getting PID
-                       pid_field = event.get_field(sco, "_pid")
+                       pid_field = event.get_field_with_scope(sco, "_pid")
                        pid = pid_field.get_int64()
 
                        if ctf.field_error():
@@ -70,43 +70,43 @@ while event is not None:
                        sco = event.get_top_level_scope(ctf.scope.EVENT_FIELDS)
 
                        # prev_comm
-                       field = event.get_field(sco, "_prev_comm")
+                       field = event.get_field_with_scope(sco, "_prev_comm")
                        prev_comm = field.get_char_array()
                        if ctf.field_error():
                                print("ERROR: Missing prev_comm context info")
 
                        # prev_tid
-                       field = event.get_field(sco, "_prev_tid")
+                       field = event.get_field_with_scope(sco, "_prev_tid")
                        prev_tid = field.get_int64()
                        if ctf.field_error():
                                print("ERROR: Missing prev_tid context info")
 
                        # prev_prio
-                       field = event.get_field(sco, "_prev_prio")
+                       field = event.get_field_with_scope(sco, "_prev_prio")
                        prev_prio = field.get_int64()
                        if ctf.field_error():
                                print("ERROR: Missing prev_prio context info")
 
                        # prev_state
-                       field = event.get_field(sco, "_prev_state")
+                       field = event.get_field_with_scope(sco, "_prev_state")
                        prev_state = field.get_int64()
                        if ctf.field_error():
                                print("ERROR: Missing prev_state context info")
 
                        # next_comm
-                       field = event.get_field(sco, "_next_comm")
+                       field = event.get_field_with_scope(sco, "_next_comm")
                        next_comm = field.get_char_array()
                        if ctf.field_error():
                                print("ERROR: Missing next_comm context info")
 
                        # next_tid
-                       field = event.get_field(sco, "_next_tid")
+                       field = event.get_field_with_scope(sco, "_next_tid")
                        next_tid = field.get_int64()
                        if ctf.field_error():
                                print("ERROR: Missing next_tid context info")
 
                        # next_prio
-                       field = event.get_field(sco, "_next_prio")
+                       field = event.get_field_with_scope(sco, "_next_prio")
                        next_prio = field.get_int64()
                        if ctf.field_error():
                                print("ERROR: Missing next_prio context info")
This page took 0.029705 seconds and 4 git commands to generate.