Add a generic get_value() implementation
authorXiaona Han <xiaonahappy13@163.com>
Fri, 26 Jul 2013 10:32:36 +0000 (18:32 +0800)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 12 Nov 2013 16:25:03 +0000 (11:25 -0500)
Use get_value to get a field's value. Currently it does not support
compound types. Also it does not support floating point, since getting
a float's value is not implemented in babeltrace now.

Edit by Jérémie Galarneau: Arrays' value should not be returned using
get_char_array(). Implemented get_array_element_at() which returns a
Definition object corresponding to the array's element.

Exposed the bt_array_index function in the Python bindings

Signed-off-by: Xiaona Han <xiaonahappy13@163.com>
Signed-off-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
bindings/python/python-complements.c
bindings/python/python-complements.h

index 50cdd945e22c0c119db752193f54524c4a6debd8..1ea6b5d2bb6e92cc589d1b00381ca6c5d2512693 100644 (file)
@@ -575,6 +575,7 @@ struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
                const struct bt_ctf_field_decl *field);
 %rename("_bt_ctf_get_decl_from_def") bt_ctf_get_decl_from_def(
                const struct bt_definition *field);
+%rename("_bt_array_index") bt_array_index(struct definition_array *array, uint64_t i);
 
 const struct bt_definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
                enum bt_ctf_scope scope);
@@ -597,6 +598,7 @@ const struct bt_definition *bt_ctf_get_enum_int(const struct bt_definition *fiel
 const char *bt_ctf_get_enum_str(const struct bt_definition *field);
 enum ctf_string_encoding bt_ctf_get_encoding(const struct bt_declaration *field);
 int bt_ctf_get_array_len(const struct bt_declaration *field);
+struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i);
 uint64_t bt_ctf_get_uint64(const struct bt_definition *field);
 int64_t bt_ctf_get_int64(const struct bt_definition *field);
 char *bt_ctf_get_char_array(const struct bt_definition *field);
@@ -943,6 +945,21 @@ class ctf:
                        """
                        return _bt_ctf_get_array_len(_bt_ctf_get_decl_from_def(self._d))
 
+               def get_array_element_at(self, index):
+                       """
+                       Return the array's element at position index.
+                       Return None on error
+                       """
+                       array = _bt_python_get_array_from_def(self._d)
+                       if array is None:
+                               return None
+
+                       element = ctf.Definition.__new__(ctf.Definition)
+                       element._d =  _bt_array_index(array, index)
+                       if element._d is None:
+                               return None
+                       return element
+
                def get_uint64(self):
                        """
                        Return the value associated with the field.
@@ -979,6 +996,29 @@ class ctf:
                        """
                        return _bt_ctf_get_string(self._d)
 
+               def get_value(self):
+                       """
+                       Return the value associated with the field according to its type.
+                       Return None on error.
+                       """
+                       id = self.field_type()
+                       if id == ctf.type_id.STRING:
+                               return self.get_str()
+                       if id == ctf.type_id.ARRAY:
+                               array = []
+                               for i in range(self.get_array_len()):
+                                       element = self.get_array_element_at(i)
+                                       array.append(element.get_value())
+                               return array
+                       if id == ctf.type_id.INTEGER:
+                               if self.get_int_signedness() == 0:
+                                       return self.get_uint64()
+                               else:
+                                       return self.get_int64()
+                       if id == ctf.type_id.ENUM:
+                               return self.get_enum_str()
+                       return None
+
                def get_scope(self):
                        """Return the scope of a field or None on error."""
                        return self._s
index 66754ba6912cc999b57e7783136223358a8b10fd..d6b48ffda79e6119bbd13900ce3ba2ca4e549d18 100644 (file)
@@ -54,16 +54,18 @@ while(event is not None):
                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))
+                       prev_comm = prev_field[0].get_value()
+                       if prev_comm is not None:
+                               print("sched_switch prev_comm: {}".format(prev_comm))
 
        if event.get_name() == "exit_syscall":
                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_code = ret_field[0].get_value()
+                       if ret_code is not None:
+                               print("exit_syscall ret: {}".format(ret_code))
 
        ret = ctf_it.next()
        if ret < 0:
index 6196e486dc728334292740ebb1e92fd0924cee86..9ab2b10fd04c2271b263d7d402f8d390fcd3cfea 100644 (file)
@@ -58,12 +58,10 @@ while event is not None:
 
                        # Getting PID
                        pid_field = event.get_field_with_scope(sco, "pid")
-                       pid = pid_field.get_int64()
-
-                       if ctf.field_error():
+                       if pid_field is None:
                                print("ERROR: Missing PID info for sched_switch")
                                break # Next event
-
+                       pid = pid_field.get_value()
                        if usePID and (pid != long(sys.argv[1])):
                                break # Next event
 
@@ -71,45 +69,45 @@ while event is not None:
 
                        # prev_comm
                        field = event.get_field_with_scope(sco, "prev_comm")
-                       prev_comm = field.get_char_array()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing prev_comm context info")
+                       prev_comm = field.get_value()
 
                        # prev_tid
                        field = event.get_field_with_scope(sco, "prev_tid")
-                       prev_tid = field.get_int64()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing prev_tid context info")
+                       prev_tid = field.get_value()
 
                        # prev_prio
                        field = event.get_field_with_scope(sco, "prev_prio")
-                       prev_prio = field.get_int64()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing prev_prio context info")
+                       prev_prio = field.get_value()
 
                        # prev_state
                        field = event.get_field_with_scope(sco, "prev_state")
-                       prev_state = field.get_int64()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing prev_state context info")
+                       prev_state = field.get_value()
 
                        # next_comm
                        field = event.get_field_with_scope(sco, "next_comm")
-                       next_comm = field.get_char_array()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing next_comm context info")
+                       next_comm = field.get_value()
 
                        # next_tid
                        field = event.get_field_with_scope(sco, "next_tid")
-                       next_tid = field.get_int64()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing next_tid context info")
+                       next_tid = field.get_value()
 
                        # next_prio
                        field = event.get_field_with_scope(sco, "next_prio")
-                       next_prio = field.get_int64()
-                       if ctf.field_error():
+                       if field is None:
                                print("ERROR: Missing next_prio context info")
+                       next_prio = field.get_value()
 
                        # Output
                        print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
index aa65555eaa8f4f1c440b2534b31cc377544bff50..53b616b4ca42740010118f63101281f0e4b7517e 100644 (file)
@@ -119,3 +119,21 @@ struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
 {
        return list[index];
 }
+
+struct definition_array *_bt_python_get_array_from_def(
+               struct bt_definition *field)
+{
+       const struct bt_declaration *array_decl;
+       struct definition_array *array = NULL;
+
+       if (!field) {
+               goto end;
+       }
+
+       array_decl = bt_ctf_get_decl_from_def(field);
+       if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) {
+               array = container_of(field, struct definition_array, p);
+       }
+end:
+       return array;
+}
index 4335d8a2abff147ecf5bddeb135ba65a10d72052..c5e93eec4e800b1b1468f1e28d92de29f29bc8ca 100644 (file)
@@ -50,3 +50,7 @@ struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
                enum bt_ctf_scope scope);
 struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
                struct bt_ctf_field_decl **list, int index);
+
+/* definitions */
+struct definition_array *_bt_python_get_array_from_def(
+               struct bt_definition *field);
This page took 0.043899 seconds and 4 git commands to generate.