From: Xiaona Han Date: Fri, 26 Jul 2013 10:32:36 +0000 (+0800) Subject: Add a generic get_value() implementation X-Git-Tag: v1.2.0-rc1~65 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=3c2ce778cc0d5e5be91e2fd1d176365a2ad65aa3 Add a generic get_value() implementation 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 Signed-off-by: Jérémie Galarneau --- diff --git a/bindings/python/babeltrace.i.in b/bindings/python/babeltrace.i.in index 50cdd945..1ea6b5d2 100644 --- a/bindings/python/babeltrace.i.in +++ b/bindings/python/babeltrace.i.in @@ -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 diff --git a/bindings/python/examples/example-api-test.py b/bindings/python/examples/example-api-test.py index 66754ba6..d6b48ffd 100644 --- a/bindings/python/examples/example-api-test.py +++ b/bindings/python/examples/example-api-test.py @@ -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: diff --git a/bindings/python/examples/sched_switch.py b/bindings/python/examples/sched_switch.py index 6196e486..9ab2b10f 100644 --- a/bindings/python/examples/sched_switch.py +++ b/bindings/python/examples/sched_switch.py @@ -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" diff --git a/bindings/python/python-complements.c b/bindings/python/python-complements.c index aa65555e..53b616b4 100644 --- a/bindings/python/python-complements.c +++ b/bindings/python/python-complements.c @@ -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; +} diff --git a/bindings/python/python-complements.h b/bindings/python/python-complements.h index 4335d8a2..c5e93eec 100644 --- a/bindings/python/python-complements.h +++ b/bindings/python/python-complements.h @@ -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);