4 * Babeltrace Python Module interface file
6 * Copyright 2012 EfficiOS Inc.
8 * Author: Danny Serres <danny.serres@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
24 "BABELTRACE_VERSION_STR
26 Babeltrace is a trace viewer and converter reading and writing the
27 Common Trace Format (CTF). Its main use is to pretty-print CTF
28 traces into a human-readable text output.
30 To use this module, the first step is to create a TraceCollection and add a
34 %module(docstring=DOCSTRING) babeltrace
38 #define SWIG_FILE_WITH_INIT
39 #include <babeltrace/babeltrace.h>
40 #include <babeltrace/babeltrace-internal.h>
41 #include <babeltrace/trace-handle.h>
42 #include <babeltrace/trace-handle-internal.h>
43 #include <babeltrace/context.h>
44 #include <babeltrace/context-internal.h>
45 #include <babeltrace/iterator.h>
46 #include <babeltrace/iterator-internal.h>
47 #include <babeltrace/format.h>
48 #include <babeltrace/list.h>
49 #include <babeltrace/types.h>
50 #include <babeltrace/ctf/iterator.h>
51 #include "python-complements.h"
52 #include <babeltrace/ctf-writer/clock.h>
53 #include <babeltrace/ctf-writer/event-fields.h>
54 #include <babeltrace/ctf-writer/event-types.h>
55 #include <babeltrace/ctf-writer/event.h>
56 #include <babeltrace/ctf-writer/stream.h>
57 #include <babeltrace/ctf-writer/writer.h>
60 typedef unsigned long long uint64_t;
61 typedef long long int64_t;
62 typedef int bt_intern_str;
63 typedef int64_t ssize_t;
65 /* =================================================================
67 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
70 FILE *_bt_file_open(char *file_path, char *mode);
71 void _bt_file_close(FILE *fp);
72 struct bt_definition **_bt_python_field_listcaller(
73 const struct bt_ctf_event *ctf_event,
74 const struct bt_definition *scope,
75 unsigned int *OUTPUT);
76 struct bt_definition *_bt_python_field_one_from_list(
77 struct bt_definition **list, int index);
78 struct bt_ctf_event_decl **_bt_python_event_decl_listcaller(
80 struct bt_context *ctx,
81 unsigned int *OUTPUT);
82 struct bt_ctf_event_decl *_bt_python_decl_one_from_list(
83 struct bt_ctf_event_decl **list, int index);
84 struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
85 struct bt_ctf_event_decl *event_decl,
86 enum bt_ctf_scope scope,
87 unsigned int *OUTPUT);
88 struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
89 struct bt_ctf_field_decl **list, int index);
90 struct definition_array *_bt_python_get_array_from_def(
91 struct bt_definition *field);
92 struct definition_sequence *_bt_python_get_sequence_from_def(
93 struct bt_definition *field);
94 struct bt_declaration *_bt_python_get_array_element_declaration(
95 struct bt_declaration *field);
96 struct bt_declaration *_bt_python_get_sequence_element_declaration(
97 struct bt_declaration *field);
98 const char *_bt_python_get_array_string(struct bt_definition *field);
99 const char *_bt_python_get_sequence_string(struct bt_definition *field);
100 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field);
101 enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field);
103 /* =================================================================
104 CONTEXT.H, CONTEXT-INTERNAL.H
105 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
108 %rename("_bt_context_create") bt_context_create(void);
109 %rename("_bt_context_add_trace") bt_context_add_trace(
110 struct bt_context *ctx, const char *path, const char *format,
111 void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence),
112 struct bt_mmap_stream_list *stream_list, FILE *metadata);
113 %rename("_bt_context_remove_trace") bt_context_remove_trace(
114 struct bt_context *ctx, int trace_id);
115 %rename("_bt_context_get") bt_context_get(struct bt_context *ctx);
116 %rename("_bt_context_put") bt_context_put(struct bt_context *ctx);
117 %rename("_bt_ctf_event_get_context") bt_ctf_event_get_context(
118 const struct bt_ctf_event *event);
120 struct bt_context *bt_context_create(void);
121 int bt_context_add_trace(struct bt_context *ctx, const char *path, const char *format,
122 void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence),
123 struct bt_mmap_stream_list *stream_list, FILE *metadata);
124 void bt_context_remove_trace(struct bt_context *ctx, int trace_id);
125 void bt_context_get(struct bt_context *ctx);
126 void bt_context_put(struct bt_context *ctx);
127 struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event);
129 // class TraceCollection to prevent direct access to struct bt_context
131 class TraceCollection:
133 The TraceCollection is the object that contains all currently opened traces.
137 self._tc = _bt_context_create()
140 _bt_context_put(self._tc)
142 def add_trace(self, path, format_str):
144 Add a trace by path to the TraceCollection.
148 path is the path to the trace, it is not recursive.
149 If "path" is None, stream_list is used instead as a list
150 of mmap streams to open for the trace.
152 format is a string containing the format name in which the trace was
155 Return: the corresponding TraceHandle on success or None on error.
157 ret = _bt_context_add_trace(self._tc, path, format_str, None, None, None)
161 th = TraceHandle.__new__(TraceHandle)
163 th._trace_collection = self
166 def add_traces_recursive(self, path, format_str):
168 Open a trace recursively.
170 Find each trace present in the subdirectory starting from the given
171 path, and add them to the TraceCollection.
173 Return a dict of TraceHandle instances (the full path is the key).
174 Return None on error.
184 for fullpath, dirs, files in os.walk(path):
185 if "metadata" in files:
186 trace_handle = self.add_trace(fullpath, format_str)
187 if trace_handle is None:
191 trace_handles[fullpath] = trace_handle
194 if noTrace and error:
198 def remove_trace(self, trace_handle):
200 Remove a trace from the TraceCollection.
201 Effectively closing the trace.
204 _bt_context_remove_trace(self._tc, trace_handle._id)
205 except AttributeError:
206 raise TypeError("in remove_trace, "
207 "argument 2 must be a TraceHandle instance")
212 Generator function to iterate over the events of open in the current
215 begin_pos_ptr = _bt_iter_pos()
216 end_pos_ptr = _bt_iter_pos()
217 begin_pos_ptr.type = SEEK_BEGIN
218 end_pos_ptr.type = SEEK_LAST
220 for event in self._events(begin_pos_ptr, end_pos_ptr):
223 def events_timestamps(self, timestamp_begin, timestamp_end):
225 Generator function to iterate over the events of open in the current
226 TraceCollection from timestamp_begin to timestamp_end.
228 begin_pos_ptr = _bt_iter_pos()
229 end_pos_ptr = _bt_iter_pos()
230 begin_pos_ptr.type = end_pos_ptr.type = SEEK_TIME
231 begin_pos_ptr.u.seek_time = timestamp_begin
232 end_pos_ptr.u.seek_time = timestamp_end
234 for event in self._events(begin_pos_ptr, end_pos_ptr):
238 def timestamp_begin(self):
239 pos_ptr = _bt_iter_pos()
240 pos_ptr.type = SEEK_BEGIN
241 return self._timestamp_at_pos(pos_ptr)
244 def timestamp_end(self):
245 pos_ptr = _bt_iter_pos()
246 pos_ptr.type = SEEK_LAST
247 return self._timestamp_at_pos(pos_ptr)
249 def _timestamp_at_pos(self, pos_ptr):
250 ctf_it_ptr = _bt_ctf_iter_create(self._tc, pos_ptr, pos_ptr)
251 if ctf_it_ptr is None:
252 raise NotImplementedError("Creation of multiple iterators is unsupported.")
253 ev_ptr = _bt_ctf_iter_read_event(ctf_it_ptr)
254 _bt_ctf_iter_destroy(ctf_it_ptr)
258 def _events(self, begin_pos_ptr, end_pos_ptr):
259 ctf_it_ptr = _bt_ctf_iter_create(self._tc, begin_pos_ptr, end_pos_ptr)
260 if ctf_it_ptr is None:
261 raise NotImplementedError(
262 "Creation of multiple iterators is unsupported.")
265 ev_ptr = _bt_ctf_iter_read_event(ctf_it_ptr)
269 ev = Event.__new__(Event)
273 except GeneratorExit:
276 ret = _bt_iter_next(_bt_ctf_get_iter(ctf_it_ptr))
280 _bt_ctf_iter_destroy(ctf_it_ptr)
286 /* =================================================================
291 %rename("lookup_format") bt_lookup_format(bt_intern_str qname);
292 %rename("_bt_print_format_list") bt_fprintf_format_list(FILE *fp);
293 %rename("register_format") bt_register_format(struct format *format);
294 %rename("unregister_format") bt_unregister_format(struct bt_format *format);
296 extern struct format *bt_lookup_format(bt_intern_str qname);
297 extern void bt_fprintf_format_list(FILE *fp);
298 extern int bt_register_format(struct bt_format *format);
299 extern void bt_unregister_format(struct bt_format *format);
303 def print_format_list(babeltrace_file):
305 Print a list of available formats to file.
307 babeltrace_file must be a File instance opened in write mode.
310 if babeltrace_file._file is not None:
311 _bt_print_format_list(babeltrace_file._file)
312 except AttributeError:
313 raise TypeError("in print_format_list, "
314 "argument 1 must be a File instance")
319 /* =================================================================
320 ITERATOR.H, ITERATOR-INTERNAL.H
321 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
324 %rename("_bt_iter_create") bt_iter_create(struct bt_context *ctx,
325 const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos);
326 %rename("_bt_iter_destroy") bt_iter_destroy(struct bt_iter *iter);
327 %rename("_bt_iter_next") bt_iter_next(struct bt_iter *iter);
328 %rename("_bt_iter_get_pos") bt_iter_get_pos(struct bt_iter *iter);
329 %rename("_bt_iter_free_pos") bt_iter_free_pos(struct bt_iter_pos *pos);
330 %rename("_bt_iter_set_pos") bt_iter_set_pos(struct bt_iter *iter,
331 const struct bt_iter_pos *pos);
332 %rename("_bt_iter_create_time_pos") bt_iter_create_time_pos(struct bt_iter *iter,
335 struct bt_iter *bt_iter_create(struct bt_context *ctx,
336 const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos);
337 void bt_iter_destroy(struct bt_iter *iter);
338 int bt_iter_next(struct bt_iter *iter);
339 struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter);
340 void bt_iter_free_pos(struct bt_iter_pos *pos);
341 int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *pos);
342 struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, uint64_t timestamp);
344 %rename("_bt_iter_pos") bt_iter_pos;
345 %rename("SEEK_TIME") BT_SEEK_TIME;
346 %rename("SEEK_RESTORE") BT_SEEK_RESTORE;
347 %rename("SEEK_CUR") BT_SEEK_CUR;
348 %rename("SEEK_BEGIN") BT_SEEK_BEGIN;
349 %rename("SEEK_LAST") BT_SEEK_LAST;
351 // This struct is taken from iterator.h
352 // All changes to the struct must also be made here
355 BT_SEEK_TIME, /* uses u.seek_time */
356 BT_SEEK_RESTORE, /* uses u.restore */
363 struct bt_saved_pos *restore;
367 /* =================================================================
368 TRACE-HANDLE.H, TRACE-HANDLE-INTERNAL.H
369 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
372 %rename("_bt_trace_handle_create") bt_trace_handle_create(struct bt_context *ctx);
373 %rename("_bt_trace_handle_destroy") bt_trace_handle_destroy(struct bt_trace_handle *bt);
374 struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx);
375 void bt_trace_handle_destroy(struct bt_trace_handle *bt);
377 %rename("_bt_trace_handle_get_path") bt_trace_handle_get_path(struct bt_context *ctx,
379 %rename("_bt_trace_handle_get_timestamp_begin") bt_trace_handle_get_timestamp_begin(
380 struct bt_context *ctx, int handle_id, enum bt_clock_type type);
381 %rename("_bt_trace_handle_get_timestamp_end") bt_trace_handle_get_timestamp_end(
382 struct bt_context *ctx, int handle_id, enum bt_clock_type type);
383 const char *bt_trace_handle_get_path(struct bt_context *ctx, int handle_id);
384 uint64_t bt_trace_handle_get_timestamp_begin(struct bt_context *ctx, int handle_id,
385 enum bt_clock_type type);
386 uint64_t bt_trace_handle_get_timestamp_end(struct bt_context *ctx, int handle_id,
387 enum bt_clock_type type);
389 %rename("_bt_ctf_event_get_handle_id") bt_ctf_event_get_handle_id(
390 const struct bt_ctf_event *event);
391 int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event);
396 # Based on enum bt_clock_type in clock-type.h
401 class TraceHandle(object):
403 The TraceHandle allows the user to manipulate a trace file directly.
404 It is a unique identifier representing a trace file.
409 raise NotImplementedError("TraceHandle cannot be instantiated")
412 return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id)
416 """Return the TraceHandle id."""
421 """Return the path of a TraceHandle."""
422 return _bt_trace_handle_get_path(self._trace_collection._tc, self._id)
425 def timestamp_begin(self):
426 """Return the creation time of the buffers of a trace."""
427 return _bt_trace_handle_get_timestamp_begin(
428 self._trace_collection._tc, self._id, ClockType.CLOCK_REAL)
431 def timestamp_end(self):
432 """Return the destruction timestamp of the buffers of a trace."""
433 return _bt_trace_handle_get_timestamp_end(
434 self._trace_collection._tc, self._id, ClockType.CLOCK_REAL)
439 Generator returning all events (EventDeclaration) in a trace.
441 ret = _bt_python_event_decl_listcaller(self.id, self._trace_collection._tc)
443 if not isinstance(ret, list):
446 ptr_list, count = ret
447 for i in range(count):
448 tmp = EventDeclaration.__new__(EventDeclaration)
449 tmp._ed = _bt_python_decl_one_from_list(ptr_list, i)
456 // =================================================================
458 // =================================================================
460 /* =================================================================
466 %rename("_bt_ctf_iter_create") bt_ctf_iter_create(struct bt_context *ctx,
467 const struct bt_iter_pos *begin_pos,
468 const struct bt_iter_pos *end_pos);
469 %rename("_bt_ctf_get_iter") bt_ctf_get_iter(struct bt_ctf_iter *iter);
470 %rename("_bt_ctf_iter_destroy") bt_ctf_iter_destroy(struct bt_ctf_iter *iter);
471 %rename("_bt_ctf_iter_read_event") bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
473 struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx,
474 const struct bt_iter_pos *begin_pos,
475 const struct bt_iter_pos *end_pos);
476 struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter);
477 void bt_ctf_iter_destroy(struct bt_ctf_iter *iter);
478 struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
482 %rename("_bt_ctf_get_top_level_scope") bt_ctf_get_top_level_scope(const struct
483 bt_ctf_event *event, enum bt_ctf_scope scope);
484 %rename("_bt_ctf_event_name") bt_ctf_event_name(const struct bt_ctf_event *ctf_event);
485 %rename("_bt_ctf_get_timestamp") bt_ctf_get_timestamp(
486 const struct bt_ctf_event *ctf_event);
487 %rename("_bt_ctf_get_cycles") bt_ctf_get_cycles(
488 const struct bt_ctf_event *ctf_event);
490 %rename("_bt_ctf_get_field") bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
491 const struct bt_definition *scope, const char *field);
492 %rename("_bt_ctf_get_index") bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
493 const struct bt_definition *field, unsigned int index);
494 %rename("_bt_ctf_field_name") bt_ctf_field_name(const struct bt_definition *field);
495 %rename("_bt_ctf_field_type") bt_ctf_field_type(const struct bt_declaration *field);
496 %rename("_bt_ctf_get_int_signedness") bt_ctf_get_int_signedness(
497 const struct bt_declaration *field);
498 %rename("_bt_ctf_get_int_base") bt_ctf_get_int_base(const struct bt_declaration *field);
499 %rename("_bt_ctf_get_int_byte_order") bt_ctf_get_int_byte_order(
500 const struct bt_declaration *field);
501 %rename("_bt_ctf_get_int_len") bt_ctf_get_int_len(const struct bt_declaration *field);
502 %rename("_bt_ctf_get_enum_int") bt_ctf_get_enum_int(const struct bt_definition *field);
503 %rename("_bt_ctf_get_enum_str") bt_ctf_get_enum_str(const struct bt_definition *field);
504 %rename("_bt_ctf_get_encoding") bt_ctf_get_encoding(const struct bt_declaration *field);
505 %rename("_bt_ctf_get_array_len") bt_ctf_get_array_len(const struct bt_declaration *field);
506 %rename("_bt_ctf_get_uint64") bt_ctf_get_uint64(const struct bt_definition *field);
507 %rename("_bt_ctf_get_int64") bt_ctf_get_int64(const struct bt_definition *field);
508 %rename("_bt_ctf_get_char_array") bt_ctf_get_char_array(const struct bt_definition *field);
509 %rename("_bt_ctf_get_string") bt_ctf_get_string(const struct bt_definition *field);
510 %rename("_bt_ctf_get_float") bt_ctf_get_float(const struct bt_definition *field);
511 %rename("_bt_ctf_get_variant") bt_ctf_get_variant(const struct bt_definition *field);
512 %rename("_bt_ctf_field_get_error") bt_ctf_field_get_error(void);
513 %rename("_bt_ctf_get_decl_event_name") bt_ctf_get_decl_event_name(const struct
514 bt_ctf_event_decl *event);
515 %rename("_bt_ctf_get_decl_event_id") bt_ctf_get_decl_event_id(const struct
516 bt_ctf_event_decl *event);
517 %rename("_bt_ctf_get_decl_field_name") bt_ctf_get_decl_field_name(
518 const struct bt_ctf_field_decl *field);
519 %rename("_bt_ctf_get_decl_from_def") bt_ctf_get_decl_from_def(
520 const struct bt_definition *field);
521 %rename("_bt_ctf_get_decl_from_field_decl") bt_ctf_get_decl_from_field_decl(
522 const struct bt_ctf_field_decl *field);
523 %rename("_bt_array_index") bt_array_index(struct definition_array *array, uint64_t i);
524 %rename("_bt_sequence_len") bt_sequence_len(struct definition_sequence *sequence);
525 %rename("_bt_sequence_index") bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
526 %rename("_bt_ctf_get_struct_field_count") bt_ctf_get_struct_field_count(const struct bt_definition *structure);
527 %rename("_bt_ctf_get_struct_field_index") bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i);
529 const struct bt_definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
530 enum bt_ctf_scope scope);
531 const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event);
532 uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event);
533 uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event);
534 const struct bt_definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
535 const struct bt_definition *scope,
537 const struct bt_definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
538 const struct bt_definition *field,
540 const char *bt_ctf_field_name(const struct bt_definition *field);
541 enum ctf_type_id bt_ctf_field_type(const struct bt_declaration *field);
542 int bt_ctf_get_int_signedness(const struct bt_declaration *field);
543 int bt_ctf_get_int_base(const struct bt_declaration *field);
544 int bt_ctf_get_int_byte_order(const struct bt_declaration *field);
545 ssize_t bt_ctf_get_int_len(const struct bt_declaration *field);
546 const struct bt_definition *bt_ctf_get_enum_int(const struct bt_definition *field);
547 const char *bt_ctf_get_enum_str(const struct bt_definition *field);
548 enum ctf_string_encoding bt_ctf_get_encoding(const struct bt_declaration *field);
549 int bt_ctf_get_array_len(const struct bt_declaration *field);
550 struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i);
551 uint64_t bt_ctf_get_uint64(const struct bt_definition *field);
552 int64_t bt_ctf_get_int64(const struct bt_definition *field);
553 char *bt_ctf_get_char_array(const struct bt_definition *field);
554 char *bt_ctf_get_string(const struct bt_definition *field);
555 double bt_ctf_get_float(const struct bt_definition *field);
556 const struct bt_definition *bt_ctf_get_variant(const struct bt_definition *field);
557 int bt_ctf_field_get_error(void);
558 const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event);
559 uint64_t bt_ctf_get_decl_event_id(const struct bt_ctf_event_decl *event);
560 const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field);
561 const struct bt_declaration *bt_ctf_get_decl_from_def(const struct bt_definition *field);
562 const struct bt_declaration *bt_ctf_get_decl_from_field_decl(const struct bt_ctf_field_decl *field);
563 uint64_t bt_sequence_len(struct definition_sequence *sequence);
564 struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
565 uint64_t bt_ctf_get_struct_field_count(const struct bt_definition *structure);
566 const struct bt_definition *bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i);
570 class CTFStringEncoding:
576 # Based on the enum in ctf-writer/writer.h
578 BYTE_ORDER_NATIVE = 0
579 BYTE_ORDER_LITTLE_ENDIAN = 1
580 BYTE_ORDER_BIG_ENDIAN = 2
581 BYTE_ORDER_NETWORK = 3
582 BYTE_ORDER_UNKNOWN = 4 # Python-specific entry
584 #enum equivalent, accessible constants
585 #These are taken directly from ctf/events.h
586 #All changes to enums must also be made here
601 name = "UNKNOWN_TYPE"
602 constants = [attr for attr in dir(CTFTypeId) if not callable(getattr(CTFTypeId, attr)) and not attr.startswith("__")]
603 for attr in constants:
604 if getattr(CTFTypeId, attr) == id:
610 TRACE_PACKET_HEADER = 0
611 STREAM_PACKET_CONTEXT = 1
612 STREAM_EVENT_HEADER = 2
613 STREAM_EVENT_CONTEXT = 3
617 def scope_name(scope):
618 name = "UNKNOWN_SCOPE"
619 constants = [attr for attr in dir(CTFScope) if not callable(getattr(CTFScope, attr)) and not attr.startswith("__")]
620 for attr in constants:
621 if getattr(CTFScope, attr) == scope:
626 # Priority of the scopes when searching for event fields
627 _scopes = [CTFScope.EVENT_FIELDS, CTFScope.EVENT_CONTEXT, CTFScope.STREAM_EVENT_CONTEXT,
628 CTFScope.STREAM_EVENT_HEADER, CTFScope.STREAM_PACKET_CONTEXT, CTFScope.TRACE_PACKET_HEADER]
631 class Event(collections.Mapping):
633 This class represents an event from the trace.
634 It is obtained using the TraceCollection generator functions.
638 raise NotImplementedError("Event cannot be instantiated")
642 """Return the name of the event or None on error."""
643 return _bt_ctf_event_name(self._e)
648 Return the timestamp of the event as written in
649 the packet (in cycles) or -1ULL on error.
651 return _bt_ctf_get_cycles(self._e)
656 Return the timestamp of the event offset with the
657 system clock source or -1ULL on error.
659 return _bt_ctf_get_timestamp(self._e)
661 def field_with_scope(self, field_name, scope):
663 Get field_name's value in scope.
664 None is returned if no field matches field_name.
666 if not scope in _scopes:
667 raise ValueError("Invalid scope provided")
668 field = self._field_with_scope(field_name, scope)
669 if field is not None:
673 def field_list_with_scope(self, scope):
674 """Return a list of field names in scope."""
675 if not scope in _scopes:
676 raise ValueError("Invalid scope provided")
678 for field in self._field_list_with_scope(scope):
679 field_names.append(field.name)
685 Get the TraceHandle associated with this event
688 ret = _bt_ctf_event_get_handle_id(self._e)
692 th = TraceHandle.__new__(TraceHandle)
694 th._trace_collection = self.get_trace_collection()
698 def trace_collection(self):
700 Get the TraceCollection associated with this event.
701 Return None on error.
703 trace_collection = TraceCollection()
704 trace_collection._tc = _bt_ctf_event_get_context(self._e);
705 if trace_collection._tc is None:
708 return trace_collection
710 def __getitem__(self, field_name):
712 Get field_name's value. If the field_name exists in multiple
713 scopes, the first field found is returned. The scopes are searched
714 in the following order:
717 3) STREAM_EVENT_CONTEXT
718 4) STREAM_EVENT_HEADER
719 5) STREAM_PACKET_CONTEXT
720 6) TRACE_PACKET_HEADER
721 None is returned if no field matches field_name.
723 Use field_with_scope() to explicitly access fields in a given
726 field = self._field(field_name)
727 if field is not None:
729 raise KeyError(field_name)
732 for key in self.keys():
737 for scope in _scopes:
738 scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope)
739 ret = _bt_python_field_listcaller(self._e, scope_ptr)
740 if isinstance(ret, list):
744 def __contains__(self, field_name):
745 return self._field(field_name) is not None
748 """Return a list of field names."""
750 for scope in _scopes:
751 for name in self.field_list_with_scope(scope):
752 field_names.add(name)
753 return list(field_names)
755 def get(self, field_name, default = None):
756 field = self._field(field_name)
762 for field in self.keys():
763 yield (field, self[field])
765 def _field_with_scope(self, field_name, scope):
766 scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope)
767 if scope_ptr is None:
770 definition_ptr = _bt_ctf_get_field(self._e, scope_ptr, field_name)
771 if definition_ptr is None:
774 field = _Definition(definition_ptr, scope)
777 def _field(self, field_name):
779 for scope in _scopes:
780 field = self._field_with_scope(field_name, scope)
781 if field is not None:
785 def _field_list_with_scope(self, scope):
787 scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope)
789 # Returns a list [list_ptr, count]. If list_ptr is NULL, SWIG will only
790 # provide the "count" return value
793 ret = _bt_python_field_listcaller(self._e, scope_ptr)
794 if isinstance(ret, list):
795 list_ptr, count = ret
797 for i in range(count):
798 definition_ptr = _bt_python_field_one_from_list(list_ptr, i)
799 if definition_ptr is not None:
800 definition = _Definition(definition_ptr, scope)
801 fields.append(definition)
804 class FieldError(Exception):
805 def __init__(self, value):
809 return repr(self.value)
811 class EventDeclaration(object):
812 """Event declaration class. Do not instantiate."""
813 MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
816 raise NotImplementedError("EventDeclaration cannot be instantiated")
820 """Return the name of the event or None on error"""
821 return _bt_ctf_get_decl_event_name(self._ed)
825 """Return the event-ID of the event or -1 on error"""
826 id = _bt_ctf_get_decl_event_id(self._ed)
827 if id == self.MAX_UINT64:
834 Generator returning all FieldDeclarations of an event, going through
835 each scope in the following order:
838 3) STREAM_EVENT_CONTEXT
839 4) STREAM_EVENT_HEADER
840 5) STREAM_PACKET_CONTEXT
841 6) TRACE_PACKET_HEADER
843 for scope in _scopes:
844 for declaration in self.fields_scope(scope):
847 def fields_scope(self, scope):
849 Generator returning FieldDeclarations of the current event in scope.
851 ret = _by_python_field_decl_listcaller(self._ed, scope)
853 if not isinstance(ret, list):
856 list_ptr, count = ret
857 for i in range(count):
858 field_declaration_ptr = _bt_python_field_decl_one_from_list(list_ptr, i)
859 if field_declaration_ptr is not None:
860 declaration_ptr = _bt_ctf_get_decl_from_field_decl(field_declaration_ptr)
861 field_declaration = _create_field_declaration(declaration_ptr, _bt_ctf_get_decl_field_name(field_declaration_ptr), scope)
862 yield field_declaration
864 class FieldDeclaration(object):
865 """Field declaration class. Do not instantiate."""
867 raise NotImplementedError("FieldDeclaration cannot be instantiated")
870 return "({0}) {1} {2}".format(CTFScope.scope_name(self.scope), CTFTypeId.type_name(self.type), self.name)
874 """Return the name of a FieldDeclaration or None on error."""
880 Return the FieldDeclaration's type. One of the entries in class
883 return _bt_ctf_field_type(self._fd)
888 Return the FieldDeclaration's scope.
892 class IntegerFieldDeclaration(FieldDeclaration):
893 """Do not instantiate."""
895 raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
898 def signedness(self):
900 Return the signedness of an integer:
901 0 if unsigned; 1 if signed; -1 on error.
903 return _bt_ctf_get_int_signedness(self._fd)
907 """Return the base of an int or a negative value on error."""
908 return _bt_ctf_get_int_base(self._fd)
911 def byte_order(self):
913 Return the byte order. One of class ByteOrder's entries.
915 ret = _bt_ctf_get_int_byte_order(self._fd)
917 return ByteOrder.BYTE_ORDER_LITTLE_ENDIAN
919 return ByteOrder.BYTE_ORDER_BIG_ENDIAN
921 return ByteOrder.BYTE_ORDER_UNKNOWN
926 Return the size, in bits, of an int or a negative
929 return _bt_ctf_get_int_len(self._fd)
934 Return the encoding. One of class CTFStringEncoding's entries.
935 Return a negative value on error.
937 return _bt_ctf_get_encoding(self._fd)
939 class EnumerationFieldDeclaration(FieldDeclaration):
940 """Do not instantiate."""
942 raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
944 class ArrayFieldDeclaration(FieldDeclaration):
945 """Do not instantiate."""
947 raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
952 Return the length of an array or a negative
955 return _bt_ctf_get_array_len(self._fd)
958 def element_declaration(self):
960 Return element declaration.
962 field_decl_ptr = _bt_python_get_array_element_declaration(self._fd)
963 return _create_field_declaration(field_decl_ptr, "", self.scope)
965 class SequenceFieldDeclaration(FieldDeclaration):
966 """Do not instantiate."""
968 raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
971 def element_declaration(self):
973 Return element declaration.
975 field_decl_ptr = _bt_python_get_sequence_element_declaration(self._fd)
976 return _create_field_declaration(field_decl_ptr, "", self.scope)
978 class FloatFieldDeclaration(FieldDeclaration):
979 """Do not instantiate."""
981 raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
983 class StructureFieldDeclaration(FieldDeclaration):
984 """Do not instantiate."""
986 raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
988 class StringFieldDeclaration(FieldDeclaration):
989 """Do not instantiate."""
991 raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
993 class VariantFieldDeclaration(FieldDeclaration):
994 """Do not instantiate."""
996 raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
1000 Return the last error code encountered while
1001 accessing a field and reset the error flag.
1002 Return 0 if no error, a negative value otherwise.
1004 return _bt_ctf_field_get_error()
1006 def _create_field_declaration(declaration_ptr, name, scope):
1008 Private field declaration factory.
1010 if declaration_ptr is None:
1011 raise ValueError("declaration_ptr must be valid")
1012 if not scope in _scopes:
1013 raise ValueError("Invalid scope provided")
1015 type = _bt_ctf_field_type(declaration_ptr)
1017 if type == CTFTypeId.INTEGER:
1018 declaration = IntegerFieldDeclaration.__new__(IntegerFieldDeclaration)
1019 elif type == CTFTypeId.ENUM:
1020 declaration = EnumerationFieldDeclaration.__new__(EnumerationFieldDeclaration)
1021 elif type == CTFTypeId.ARRAY:
1022 declaration = ArrayFieldDeclaration.__new__(ArrayFieldDeclaration)
1023 elif type == CTFTypeId.SEQUENCE:
1024 declaration = SequenceFieldDeclaration.__new__(SequenceFieldDeclaration)
1025 elif type == CTFTypeId.FLOAT:
1026 declaration = FloatFieldDeclaration.__new__(FloatFieldDeclaration)
1027 elif type == CTFTypeId.STRUCT:
1028 declaration = StructureFieldDeclaration.__new__(StructureFieldDeclaration)
1029 elif type == CTFTypeId.STRING:
1030 declaration = StringFieldDeclaration.__new__(StringFieldDeclaration)
1031 elif type == CTFTypeId.VARIANT:
1032 declaration = VariantFieldDeclaration.__new__(VariantFieldDeclaration)
1036 declaration._fd = declaration_ptr
1037 declaration._s = scope
1038 declaration._name = name
1041 class _Definition(object):
1042 def __init__(self, definition_ptr, scope):
1043 self._d = definition_ptr
1045 if not scope in _scopes:
1046 ValueError("Invalid scope provided")
1050 """Return the name of a field or None on error."""
1051 return _bt_ctf_field_name(self._d)
1055 """Return the type of a field or -1 if unknown."""
1056 return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d))
1059 def declaration(self):
1060 """Return the associated Definition object."""
1061 return _create_field_declaration(_bt_ctf_get_decl_from_def(self._d), self.name, self.scope)
1063 def _get_enum_str(self):
1065 Return the string matching the current enumeration.
1066 Return None on error.
1068 return _bt_ctf_get_enum_str(self._d)
1070 def _get_array_element_at(self, index):
1072 Return the array's element at position index.
1073 Return None on error
1075 array_ptr = _bt_python_get_array_from_def(self._d)
1076 if array_ptr is None:
1079 definition_ptr = _bt_array_index(array_ptr, index)
1080 if definition_ptr is None:
1082 return _Definition(definition_ptr, self.scope)
1084 def _get_sequence_len(self):
1086 Return the len of a sequence or a negative
1089 seq = _bt_python_get_sequence_from_def(self._d)
1090 return _bt_sequence_len(seq)
1092 def _get_sequence_element_at(self, index):
1094 Return the sequence's element at position index,
1095 otherwise return None
1097 seq = _bt_python_get_sequence_from_def(self._d)
1099 definition_ptr = _bt_sequence_index(seq, index)
1100 if definition_ptr is not None:
1101 return _Definition(definition_ptr, self.scope)
1104 def _get_uint64(self):
1106 Return the value associated with the field.
1107 If the field does not exist or is not of the type requested,
1108 the value returned is undefined. To check if an error occured,
1109 use the field_error() function after accessing a field.
1111 return _bt_ctf_get_uint64(self._d)
1113 def _get_int64(self):
1115 Return the value associated with the field.
1116 If the field does not exist or is not of the type requested,
1117 the value returned is undefined. To check if an error occured,
1118 use the field_error() function after accessing a field.
1120 return _bt_ctf_get_int64(self._d)
1122 def _get_char_array(self):
1124 Return the value associated with the field.
1125 If the field does not exist or is not of the type requested,
1126 the value returned is undefined. To check if an error occurred,
1127 use the field_error() function after accessing a field.
1129 return _bt_ctf_get_char_array(self._d)
1133 Return the value associated with the field.
1134 If the field does not exist or is not of the type requested,
1135 the value returned is undefined. To check if an error occurred,
1136 use the field_error() function after accessing a field.
1138 return _bt_ctf_get_string(self._d)
1140 def _get_float(self):
1142 Return the value associated with the field.
1143 If the field does not exist or is not of the type requested,
1144 the value returned is undefined. To check if an error occurred,
1145 use the field_error() function after accessing a field.
1147 return _bt_ctf_get_float(self._d)
1149 def _get_variant(self):
1151 Return the variant's selected field.
1152 If the field does not exist or is not of the type requested,
1153 the value returned is undefined. To check if an error occurred,
1154 use the field_error() function after accessing a field.
1156 return _bt_ctf_get_variant(self._d)
1158 def _get_struct_field_count(self):
1160 Return the number of fields contained in the structure.
1161 If the field does not exist or is not of the type requested,
1162 the value returned is undefined.
1164 return _bt_ctf_get_struct_field_count(self._d)
1166 def _get_struct_field_at(self, i):
1168 Return the structure's field at position i.
1169 If the field does not exist or is not of the type requested,
1170 the value returned is undefined. To check if an error occurred,
1171 use the field_error() function after accessing a field.
1173 return _bt_ctf_get_struct_field_index(self._d, i)
1178 Return the value associated with the field according to its type.
1179 Return None on error.
1184 if id == CTFTypeId.STRING:
1185 value = self._get_str()
1186 elif id == CTFTypeId.ARRAY:
1187 element_decl = self.declaration.element_declaration
1188 if ((element_decl.type == CTFTypeId.INTEGER
1189 and element_decl.length == 8)
1190 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1191 value = _bt_python_get_array_string(self._d)
1194 for i in range(self.declaration.length):
1195 element = self._get_array_element_at(i)
1196 value.append(element.value)
1197 elif id == CTFTypeId.INTEGER:
1198 if self.declaration.signedness == 0:
1199 value = self._get_uint64()
1201 value = self._get_int64()
1202 elif id == CTFTypeId.ENUM:
1203 value = self._get_enum_str()
1204 elif id == CTFTypeId.SEQUENCE:
1205 element_decl = self.declaration.element_declaration
1206 if ((element_decl.type == CTFTypeId.INTEGER
1207 and element_decl.length == 8)
1208 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1209 value = _bt_python_get_sequence_string(self._d)
1211 seq_len = self._get_sequence_len()
1213 for i in range(seq_len):
1214 evDef = self._get_sequence_element_at(i)
1215 value.append(evDef.value)
1216 elif id == CTFTypeId.FLOAT:
1217 value = self._get_float()
1218 elif id == CTFTypeId.VARIANT:
1219 variant = _Definition.__new__(_Definition)
1220 variant._d = self._get_variant();
1221 value = variant.value
1222 elif id == CTFTypeId.STRUCT:
1224 for i in range(self._get_struct_field_count()):
1225 member = _Definition(self._get_struct_field_at(i), self.scope)
1226 value[member.name] = member.value
1229 raise FieldError("Error occurred while accessing field {} of type {}".format(self.name, CTFTypeId.type_name(id)))
1234 """Return the scope of a field or None on error."""
1240 // =================================================================
1242 // =================================================================
1244 /* =================================================================
1246 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1248 %rename("_bt_ctf_clock_create") bt_ctf_clock_create(const char *name);
1249 %rename("_bt_ctf_clock_set_description") bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc);
1250 %rename("_bt_ctf_clock_set_frequency") bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq);
1251 %rename("_bt_ctf_clock_set_precision") bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision);
1252 %rename("_bt_ctf_clock_set_offset_s") bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s);
1253 %rename("_bt_ctf_clock_set_offset") bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset);
1254 %rename("_bt_ctf_clock_set_is_absolute") bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute);
1255 %rename("_bt_ctf_clock_set_time") bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time);
1256 %rename("_bt_ctf_clock_get") bt_ctf_clock_get(struct bt_ctf_clock *clock);
1257 %rename("_bt_ctf_clock_put") bt_ctf_clock_put(struct bt_ctf_clock *clock);
1259 struct bt_ctf_clock *bt_ctf_clock_create(const char *name);
1260 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc);
1261 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq);
1262 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision);
1263 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s);
1264 int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset);
1265 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute);
1266 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time);
1267 void bt_ctf_clock_get(struct bt_ctf_clock *clock);
1268 void bt_ctf_clock_put(struct bt_ctf_clock *clock);
1270 /* =================================================================
1272 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1274 %rename("_bt_ctf_field_type_integer_create") bt_ctf_field_type_integer_create(unsigned int size);
1275 %rename("_bt_ctf_field_type_integer_set_signed") bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *integer, int is_signed);
1276 %rename("_bt_ctf_field_type_integer_set_base") bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, enum bt_ctf_integer_base base);
1277 %rename("_bt_ctf_field_type_integer_set_encoding") bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *integer, enum ctf_string_encoding encoding);
1278 %rename("_bt_ctf_field_type_enumeration_create") bt_ctf_field_type_enumeration_create(struct bt_ctf_field_type *integer_container_type);
1279 %rename("_bt_ctf_field_type_enumeration_add_mapping") bt_ctf_field_type_enumeration_add_mapping(struct bt_ctf_field_type *enumeration, const char *string, int64_t range_start, int64_t range_end);
1280 %rename("_bt_ctf_field_type_floating_point_create") bt_ctf_field_type_floating_point_create(void);
1281 %rename("_bt_ctf_field_type_floating_point_set_exponent_digits") bt_ctf_field_type_floating_point_set_exponent_digits(struct bt_ctf_field_type *floating_point, unsigned int exponent_digits);
1282 %rename("_bt_ctf_field_type_floating_point_set_mantissa_digits") bt_ctf_field_type_floating_point_set_mantissa_digits(struct bt_ctf_field_type *floating_point, unsigned int mantissa_digits);
1283 %rename("_bt_ctf_field_type_structure_create") bt_ctf_field_type_structure_create(void);
1284 %rename("_bt_ctf_field_type_structure_add_field") bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *structure, struct bt_ctf_field_type *field_type, const char *field_name);
1285 %rename("_bt_ctf_field_type_variant_create") bt_ctf_field_type_variant_create(struct bt_ctf_field_type *enum_tag, const char *tag_name);
1286 %rename("_bt_ctf_field_type_variant_add_field") bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *variant, struct bt_ctf_field_type *field_type, const char *field_name);
1287 %rename("_bt_ctf_field_type_array_create") bt_ctf_field_type_array_create(struct bt_ctf_field_type *element_type, unsigned int length);
1288 %rename("_bt_ctf_field_type_sequence_create") bt_ctf_field_type_sequence_create(struct bt_ctf_field_type *element_type, const char *length_field_name);
1289 %rename("_bt_ctf_field_type_string_create") bt_ctf_field_type_string_create(void);
1290 %rename("_bt_ctf_field_type_string_set_encoding") bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *string, enum ctf_string_encoding encoding);
1291 %rename("_bt_ctf_field_type_set_alignment") bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment);
1292 %rename("_bt_ctf_field_type_set_byte_order") bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order);
1293 %rename("_bt_ctf_field_type_get") bt_ctf_field_type_get(struct bt_ctf_field_type *type);
1294 %rename("_bt_ctf_field_type_put") bt_ctf_field_type_put(struct bt_ctf_field_type *type);
1296 struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size);
1297 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *integer, int is_signed);
1298 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, enum bt_ctf_integer_base base);
1299 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *integer, enum ctf_string_encoding encoding);
1300 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(struct bt_ctf_field_type *integer_container_type);
1301 int bt_ctf_field_type_enumeration_add_mapping(struct bt_ctf_field_type *enumeration, const char *string, int64_t range_start, int64_t range_end);
1302 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void);
1303 int bt_ctf_field_type_floating_point_set_exponent_digits(struct bt_ctf_field_type *floating_point, unsigned int exponent_digits);
1304 int bt_ctf_field_type_floating_point_set_mantissa_digits(struct bt_ctf_field_type *floating_point, unsigned int mantissa_digits);
1305 struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void);
1306 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *structure, struct bt_ctf_field_type *field_type, const char *field_name);
1307 struct bt_ctf_field_type *bt_ctf_field_type_variant_create(struct bt_ctf_field_type *enum_tag, const char *tag_name);
1308 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *variant, struct bt_ctf_field_type *field_type, const char *field_name);
1309 struct bt_ctf_field_type *bt_ctf_field_type_array_create(struct bt_ctf_field_type *element_type, unsigned int length);
1310 struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(struct bt_ctf_field_type *element_type, const char *length_field_name);
1311 struct bt_ctf_field_type *bt_ctf_field_type_string_create(void);
1312 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *string, enum ctf_string_encoding encoding);
1313 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment);
1314 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order);
1315 void bt_ctf_field_type_get(struct bt_ctf_field_type *type);
1316 void bt_ctf_field_type_put(struct bt_ctf_field_type *type);
1318 /* =================================================================
1320 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1322 %rename("_bt_ctf_field_create") bt_ctf_field_create(struct bt_ctf_field_type *type);
1323 %rename("_bt_ctf_field_structure_get_field") bt_ctf_field_structure_get_field(struct bt_ctf_field *structure, const char *name);
1324 %rename("_bt_ctf_field_array_get_field") bt_ctf_field_array_get_field(struct bt_ctf_field *array, uint64_t index);
1325 %rename("_bt_ctf_field_sequence_set_length") bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, struct bt_ctf_field *length_field);
1326 %rename("_bt_ctf_field_sequence_get_field") bt_ctf_field_sequence_get_field(struct bt_ctf_field *sequence, uint64_t index);
1327 %rename("_bt_ctf_field_variant_get_field") bt_ctf_field_variant_get_field(struct bt_ctf_field *variant, struct bt_ctf_field *tag);
1328 %rename("_bt_ctf_field_enumeration_get_container") bt_ctf_field_enumeration_get_container(struct bt_ctf_field *enumeration);
1329 %rename("_bt_ctf_field_signed_integer_set_value") bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, int64_t value);
1330 %rename("_bt_ctf_field_unsigned_integer_set_value") bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, uint64_t value);
1331 %rename("_bt_ctf_field_floating_point_set_value") bt_ctf_field_floating_point_set_value(struct bt_ctf_field *floating_point, double value);
1332 %rename("_bt_ctf_field_string_set_value") bt_ctf_field_string_set_value(struct bt_ctf_field *string, const char *value);
1333 %rename("_bt_ctf_field_get") bt_ctf_field_get(struct bt_ctf_field *field);
1334 %rename("_bt_ctf_field_put") bt_ctf_field_put(struct bt_ctf_field *field);
1336 struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type);
1337 struct bt_ctf_field *bt_ctf_field_structure_get_field(struct bt_ctf_field *structure, const char *name);
1338 struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *array, uint64_t index);
1339 int bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, struct bt_ctf_field *length_field);
1340 struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *sequence, uint64_t index);
1341 struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *variant, struct bt_ctf_field *tag);
1342 struct bt_ctf_field *bt_ctf_field_enumeration_get_container(struct bt_ctf_field *enumeration);
1343 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, int64_t value);
1344 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, uint64_t value);
1345 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *floating_point, double value);
1346 int bt_ctf_field_string_set_value(struct bt_ctf_field *string, const char *value);
1347 void bt_ctf_field_get(struct bt_ctf_field *field);
1348 void bt_ctf_field_put(struct bt_ctf_field *field);
1350 /* =================================================================
1352 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1354 %rename("_bt_ctf_event_class_create") bt_ctf_event_class_create(const char *name);
1355 %rename("_bt_ctf_event_class_add_field") bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name);
1356 %rename("_bt_ctf_event_class_get") bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
1357 %rename("_bt_ctf_event_class_put") bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
1358 %rename("_bt_ctf_event_create") bt_ctf_event_create(struct bt_ctf_event_class *event_class);
1359 %rename("_bt_ctf_event_set_payload") bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
1360 %rename("_bt_ctf_event_get_payload") bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name);
1361 %rename("_bt_ctf_event_get") bt_ctf_event_get(struct bt_ctf_event *event);
1362 %rename("_bt_ctf_event_put") bt_ctf_event_put(struct bt_ctf_event *event);
1364 struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name);
1365 int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name);
1366 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
1367 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
1368 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class);
1369 int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
1370 struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name);
1371 void bt_ctf_event_get(struct bt_ctf_event *event);
1372 void bt_ctf_event_put(struct bt_ctf_event *event);
1374 /* =================================================================
1376 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1378 %rename("_bt_ctf_stream_class_create") bt_ctf_stream_class_create(const char *name);
1379 %rename("_bt_ctf_stream_class_set_clock") bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock);
1380 %rename("_bt_ctf_stream_class_add_event_class") bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class);
1381 %rename("_bt_ctf_stream_class_get") bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
1382 %rename("_bt_ctf_stream_class_put") bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
1383 %rename("_bt_ctf_stream_append_discarded_events") bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count);
1384 %rename("_bt_ctf_stream_append_event") bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event);
1385 %rename("_bt_ctf_stream_flush") bt_ctf_stream_flush(struct bt_ctf_stream *stream);
1386 %rename("_bt_ctf_stream_get") bt_ctf_stream_get(struct bt_ctf_stream *stream);
1387 %rename("_bt_ctf_stream_put") bt_ctf_stream_put(struct bt_ctf_stream *stream);
1389 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name);
1390 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock);
1391 int bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class);
1392 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
1393 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
1394 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count);
1395 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event);
1396 int bt_ctf_stream_flush(struct bt_ctf_stream *stream);
1397 void bt_ctf_stream_get(struct bt_ctf_stream *stream);
1398 void bt_ctf_stream_put(struct bt_ctf_stream *stream);
1400 /* =================================================================
1402 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1404 %rename("_bt_ctf_writer_create") bt_ctf_writer_create(const char *path);
1405 %rename("_bt_ctf_writer_create_stream") bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class);
1406 %rename("_bt_ctf_writer_add_environment_field") bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value);
1407 %rename("_bt_ctf_writer_add_clock") bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock);
1408 %newobject bt_ctf_writer_get_metadata_string;
1409 %rename("_bt_ctf_writer_get_metadata_string") bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
1410 %rename("_bt_ctf_writer_flush_metadata") bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
1411 %rename("_bt_ctf_writer_set_byte_order") bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, enum bt_ctf_byte_order byte_order);
1412 %rename("_bt_ctf_writer_get") bt_ctf_writer_get(struct bt_ctf_writer *writer);
1413 %rename("_bt_ctf_writer_put") bt_ctf_writer_put(struct bt_ctf_writer *writer);
1415 struct bt_ctf_writer *bt_ctf_writer_create(const char *path);
1416 struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class);
1417 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value);
1418 int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock);
1419 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
1420 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
1421 int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, enum bt_ctf_byte_order byte_order);
1422 void bt_ctf_writer_get(struct bt_ctf_writer *writer);
1423 void bt_ctf_writer_put(struct bt_ctf_writer *writer);
1430 def __init__(self, name):
1431 self._c = _bt_ctf_clock_create(name)
1433 raise ValueError("Invalid clock name.")
1436 _bt_ctf_clock_put(self._c)
1439 Get the clock's description.
1442 def description(self):
1443 raise NotImplementedError("Getter not implemented.")
1446 Set the clock's description. The description appears in the clock's TSDL
1450 def description(self, desc):
1451 ret = _bt_ctf_clock_set_description(self._c, str(desc))
1453 raise ValueError("Invalid clock description.")
1456 Get the clock's frequency (Hz).
1459 def frequency(self):
1460 raise NotImplementedError("Getter not implemented.")
1463 Set the clock's frequency (Hz).
1466 def frequency(self, freq):
1467 ret = _bt_ctf_clock_set_frequency(self._c, freq)
1469 raise ValueError("Invalid frequency value.")
1472 Get the clock's precision (in clock ticks).
1475 def precision(self):
1476 raise NotImplementedError("Getter not implemented.")
1479 Set the clock's precision (in clock ticks).
1482 def precision(self, precision):
1483 ret = _bt_ctf_clock_set_precision(self._c, precision)
1486 Get the clock's offset in seconds from POSIX.1 Epoch.
1489 def offset_seconds(self):
1490 raise NotImplementedError("Getter not implemented.")
1493 Set the clock's offset in seconds from POSIX.1 Epoch.
1495 @offset_seconds.setter
1496 def offset_seconds(self, offset_s):
1497 ret = _bt_ctf_clock_set_offset_s(self._c, offset_s)
1499 raise ValueError("Invalid offset value.")
1502 Get the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1506 raise NotImplementedError("Getter not implemented.")
1509 Set the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1512 def offset(self, offset):
1513 ret = _bt_ctf_clock_set_offset(self._c, offset)
1515 raise ValueError("Invalid offset value.")
1518 Get a clock's absolute attribute. A clock is absolute if the clock
1519 is a global reference across the trace's other clocks.
1523 raise NotImplementedError("Getter not implemented.")
1526 Set a clock's absolute attribute. A clock is absolute if the clock
1527 is a global reference across the trace's other clocks.
1530 def absolute(self, is_absolute):
1531 ret = _bt_ctf_clock_set_is_absolute(self._c, int(is_absolute))
1533 raise ValueError("Could not set the clock's absolute attribute.")
1536 Get the current time in nanoseconds since the clock's origin (offset and
1537 offset_s attributes).
1541 raise NotImplementedError("Getter not implemented.")
1544 Set the current time in nanoseconds since the clock's origin (offset and
1545 offset_s attributes). The clock's value will be sampled as events are
1546 appended to a stream.
1549 def time(self, time):
1550 ret = _bt_ctf_clock_set_time(self._c, time)
1552 raise ValueError("Invalid time value.")
1554 class FieldDeclaration:
1556 FieldDeclaration should not be instantiated directly. Please instantiate
1557 one of the concrete FieldDeclaration classes.
1560 # These values are based on the bt_ctf_integer_base enum
1561 # declared in event-types.h.
1562 INTEGER_BASE_UNKNOWN = -1
1563 INTEGER_BASE_BINARY = 2
1564 INTEGER_BASE_OCTAL = 8
1565 INTEGER_BASE_DECIMAL = 10
1566 INTEGER_BASE_HEXADECIMAL = 16
1569 if self._ft is None:
1570 raise ValueError("FieldDeclaration creation failed.")
1573 _bt_ctf_field_type_put(self._ft)
1576 Get the field type's alignment.
1579 def alignment(self):
1580 raise NotImplementedError("Getter not implemented.")
1583 Set the field type's alignment. Defaults to 1 (bit-aligned). However,
1584 some types, such as structures and string, may impose other alignment
1588 def alignment(self, alignment):
1589 ret = _bt_ctf_field_type_set_alignment(self._ft, alignment)
1591 raise ValueError("Invalid alignment value.")
1594 Get the field type's byte order. One of the ByteOrder's constant.
1597 def byte_order(self):
1598 raise NotImplementedError("Getter not implemented.")
1601 Set the field type's byte order. Use constants defined in the ByteOrder
1605 def byte_order(self, byte_order):
1606 ret = _bt_ctf_field_type_set_byte_order(self._ft, byte_order)
1608 raise ValueError("Could not set byte order value.")
1610 class IntegerFieldDeclaration(FieldDeclaration):
1612 Create a new integer field type of the given size.
1614 def __init__(self, size):
1615 self._ft = _bt_ctf_field_type_integer_create(size)
1619 Get an integer's signedness attribute.
1623 raise NotImplementedError("Getter not implemented.")
1626 Set an integer's signedness attribute.
1629 def signed(self, signed):
1630 ret = _bt_ctf_field_type_integer_set_signed(self._ft, signed)
1632 raise ValueError("Could not set signed attribute.")
1635 Get the integer's base used to pretty-print the resulting trace.
1639 raise NotImplementedError("Getter not implemented.")
1642 Set the integer's base used to pretty-print the resulting trace.
1643 The base must be a constant of the IntegerBase class.
1646 def base(self, base):
1647 ret = _bt_ctf_field_type_integer_set_base(self._ft, base)
1649 raise ValueError("Could not set base value.")
1652 Get the integer's encoding (one of the constants of the
1653 CTFStringEncoding class).
1657 raise NotImplementedError("Getter not implemented.")
1660 An integer encoding may be set to signal that the integer must be printed
1661 as a text character. Must be a constant from the CTFStringEncoding class.
1664 def encoding(self, encoding):
1665 ret = _bt_ctf_field_type_integer_set_encoding(self._ft, encoding)
1667 raise ValueError("Could not set integer encoding.")
1669 class EnumerationFieldDeclaration(FieldDeclaration):
1671 Create a new enumeration field type with the given underlying type.
1673 def __init__(self, integer_type):
1674 if integer_type is None or not isinstance(integer_type, CTFWriter.IntegerFieldDeclaration):
1675 raise TypeError("Invalid integer container.")
1677 self._ft = _bt_ctf_field_type_enumeration_create(integer_type._ft)
1681 Add a mapping to the enumeration. The range's values are inclusive.
1683 def add_mapping(self, name, range_start, range_end):
1684 ret = _bt_ctf_field_type_enumeration_add_mapping(self._ft, str(name), range_start, range_end)
1686 raise ValueError("Could not add mapping to enumeration type.")
1688 class FloatFieldDeclaration(FieldDeclaration):
1695 Create a new floating point field type.
1698 self._ft = _bt_ctf_field_type_floating_point_create()
1702 Get the number of exponent digits to use to store the floating point field.
1705 def exponent_digits(self):
1706 raise NotImplementedError("Getter not implemented.")
1709 Set the number of exponent digits to use to store the floating point field.
1710 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
1711 are defined as constants of this class.
1713 @exponent_digits.setter
1714 def exponent_digits(self, exponent_digits):
1715 ret = _bt_ctf_field_type_floating_point_set_exponent_digits(self._ft, exponent_digits)
1717 raise ValueError("Could not set exponent digit count.")
1720 Get the number of mantissa digits to use to store the floating point field.
1723 def mantissa_digits(self):
1724 raise NotImplementedError("Getter not implemented.")
1727 Set the number of mantissa digits to use to store the floating point field.
1728 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
1729 are defined as constants of this class.
1731 @mantissa_digits.setter
1732 def mantissa_digits(self, mantissa_digits):
1733 ret = _bt_ctf_field_type_floating_point_set_mantissa_digits(self._ft, mantissa_digits)
1735 raise ValueError("Could not set mantissa digit count.")
1737 class StructureFieldDeclaration(FieldDeclaration):
1739 Create a new structure field type.
1742 self._ft = _bt_ctf_field_type_structure_create()
1746 Add a field of type "field_type" to the structure.
1748 def add_field(self, field_type, field_name):
1749 ret = _bt_ctf_field_type_structure_add_field(self._ft, field_type._ft, str(field_name))
1751 raise ValueError("Could not add field to structure.")
1753 class VariantFieldDeclaration(FieldDeclaration):
1755 Create a new variant field type.
1757 def __init__(self, enum_tag, tag_name):
1758 if enum_tag is None or not isinstance(enum_tag, CTFWriter.EnumerationFieldDeclaration):
1759 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
1761 self._ft = _bt_ctf_field_type_variant_create(enum_tag._ft, str(tag_name))
1765 Add a field of type "field_type" to the variant.
1767 def add_field(self, field_type, field_name):
1768 ret = _bt_ctf_field_type_variant_add_field(self._ft, field_type._ft, str(field_name))
1770 raise ValueError("Could not add field to variant.")
1772 class ArrayFieldDeclaration(FieldDeclaration):
1774 Create a new array field type.
1776 def __init__(self, element_type, length):
1777 self._ft = _bt_ctf_field_type_array_create(element_type._ft, length)
1780 class SequenceFieldDeclaration(FieldDeclaration):
1782 Create a new sequence field type.
1784 def __init__(self, element_type, length_field_name):
1785 self._ft = _bt_ctf_field_type_sequence_create(element_type._ft, str(length_field_name))
1788 class StringFieldDeclaration(FieldDeclaration):
1790 Create a new string field type.
1793 self._ft = _bt_ctf_field_type_string_create()
1797 Get a string type's encoding (a constant from the CTFStringEncoding class).
1801 raise NotImplementedError("Getter not implemented.")
1804 Set a string type's encoding. Must be a constant from the CTFStringEncoding class.
1807 def encoding(self, encoding):
1808 ret = _bt_ctf_field_type_string_set_encoding(self._ft, encoding)
1810 raise ValueError("Could not set string encoding.")
1813 Create an instance of a field.
1816 def create_field(self, field_type):
1817 if field_type is None or not isinstance(field_type, CTFWriter.FieldDeclaration):
1818 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
1820 if isinstance(field_type, CTFWriter.IntegerFieldDeclaration):
1821 return CTFWriter.IntegerField(field_type)
1822 elif isinstance(field_type, CTFWriter.EnumerationFieldDeclaration):
1823 return CTFWriter.EnumerationField(field_type)
1824 elif isinstance(field_type, CTFWriter.FloatFieldDeclaration):
1825 return CTFWriter.FloatFieldingPoint(field_type)
1826 elif isinstance(field_type, CTFWriter.StructureFieldDeclaration):
1827 return CTFWriter.StructureField(field_type)
1828 elif isinstance(field_type, CTFWriter.VariantFieldDeclaration):
1829 return CTFWriter.VariantField(field_type)
1830 elif isinstance(field_type, CTFWriter.ArrayFieldDeclaration):
1831 return CTFWriter.ArrayField(field_type)
1832 elif isinstance(field_type, CTFWriter.SequenceFieldDeclaration):
1833 return CTFWriter.SequenceField(field_type)
1834 elif isinstance(field_type, CTFWriter.StringFieldDeclaration):
1835 return CTFWriter.StringField(field_type)
1839 Base class, do not instantiate.
1841 def __init__(self, field_type):
1842 if not isinstance(field_type, CTFWriter.FieldDeclaration):
1843 raise TypeError("Invalid field_type argument.")
1845 self._f = _bt_ctf_field_create(field_type._ft)
1847 raise ValueError("Field creation failed.")
1850 _bt_ctf_field_put(self._f)
1853 def _create_field_from_native_instance(native_field_instance):
1855 CTFTypeId.INTEGER : CTFWriter.IntegerField,
1856 CTFTypeId.FLOAT : CTFWriter.FloatFieldingPoint,
1857 CTFTypeId.ENUM : CTFWriter.EnumerationField,
1858 CTFTypeId.STRING : CTFWriter.StringField,
1859 CTFTypeId.STRUCT : CTFWriter.StructureField,
1860 CTFTypeId.VARIANT : CTFWriter.VariantField,
1861 CTFTypeId.ARRAY : CTFWriter.ArrayField,
1862 CTFTypeId.SEQUENCE : CTFWriter.SequenceField
1865 field_type = _bt_python_get_field_type(native_field_instance)
1866 if field_type == CTFTypeId.UNKNOWN:
1867 raise TypeError("Invalid field instance")
1869 field = CTFWriter.Field.__new__(CTFWriter.Field)
1870 field._f = native_field_instance
1871 field.__class__ = type_dict[field_type]
1874 class IntegerField(Field):
1876 Get an integer field's value.
1880 raise NotImplementedError("Getter not implemented.")
1883 Set an integer field's value.
1886 def value(self, value):
1887 signedness = _bt_python_field_integer_get_signedness(self._f)
1889 raise TypeError("Invalid integer instance.")
1892 ret = _bt_ctf_field_unsigned_integer_set_value(self._f, value)
1894 ret = _bt_ctf_field_signed_integer_set_value(self._f, value)
1897 raise ValueError("Could not set integer field value.")
1899 class EnumerationField(Field):
1901 Return the enumeration's underlying container field (an integer field).
1904 def container(self):
1905 container = CTFWriter.IntegerField.__new__(CTFWriter.IntegerField)
1906 container._f = _bt_ctf_field_enumeration_get_container(self._f)
1907 if container._f is None:
1908 raise TypeError("Invalid enumeration field type.")
1911 class FloatFieldingPoint(Field):
1913 Get a floating point field's value.
1917 raise NotImplementedError("Getter not implemented.")
1920 Set a floating point field's value.
1923 def value(self, value):
1924 ret = _bt_ctf_field_floating_point_set_value(self._f, float(value))
1926 raise ValueError("Could not set floating point field value.")
1928 class StructureField(Field):
1930 Get the structure's field corresponding to the provided field name.
1932 def field(self, field_name):
1933 native_instance = _bt_ctf_field_structure_get_field(self._f, str(field_name))
1934 if native_instance is None:
1935 raise ValueError("Invalid field_name provided.")
1936 return CTFWriter.Field._create_field_from_native_instance(native_instance)
1938 class VariantField(Field):
1940 Return the variant's selected field. The "tag" field is the selector enum field.
1942 def field(self, tag):
1943 native_instance = _bt_ctf_field_variant_get_field(self._f, tag._f)
1944 if native_instance is None:
1945 raise ValueError("Invalid tag provided.")
1946 return CTFWriter.Field._create_field_from_native_instance(native_instance)
1948 class ArrayField(Field):
1950 Return the array's field at position "index".
1952 def field(self, index):
1953 native_instance = _bt_ctf_field_array_get_field(self._f, index)
1954 if native_instance is None:
1955 raise IndexError("Invalid index provided.")
1956 return CTFWriter.Field._create_field_from_native_instance(native_instance)
1958 class SequenceField(Field):
1960 Get the sequence's length field (IntegerField).
1964 raise NotImplementedError("Getter not implemented.")
1967 Set the sequence's length field (IntegerField).
1970 def length(self, length_field):
1971 if not isinstance(length_field, CTFWriter.IntegerField):
1972 raise TypeError("Invalid length field.")
1973 ret = _bt_ctf_field_sequence_set_length(self._f, length_field._f)
1975 raise ValueError("Could not set sequence length.")
1978 Return the sequence's field at position "index".
1980 def field(self, index):
1981 native_instance = _bt_ctf_field_sequence_get_field(self._f, index)
1982 if native_instance is None:
1983 raise ValueError("Could not get sequence element at index.")
1984 return CTFWriter.Field._create_field_from_native_instance(native_instance)
1986 class StringField(Field):
1988 Get a string field's value.
1992 raise NotImplementedError("Getter not implemented.")
1995 Set a string field's value.
1998 def value(self, value):
1999 ret = _bt_ctf_field_string_set_value(self._f, str(value))
2001 raise ValueError("Could not set string field value.")
2005 Create a new event class of the given name.
2007 def __init__(self, name):
2008 self._ec = _bt_ctf_event_class_create(name)
2009 if self._ec is None:
2010 raise ValueError("Event class creation failed.")
2013 _bt_ctf_event_class_put(self._ec)
2016 Add a field of type "field_type" to the event class.
2018 def add_field(self, field_type, field_name):
2019 ret = _bt_ctf_event_class_add_field(self._ec, field_type._ft, str(field_name))
2021 raise ValueError("Could not add field to event class.")
2025 Create a new event of the given event class.
2027 def __init__(self, event_class):
2028 if not isinstance(event_class, CTFWriter.EventClass):
2029 raise TypeError("Invalid event_class argument.")
2031 self._e = _bt_ctf_event_create(event_class._ec)
2033 raise ValueError("Event creation failed.")
2036 _bt_ctf_event_put(self._e)
2039 Set a manually created field as an event's payload.
2041 def set_payload(self, field_name, value_field):
2042 if not isinstance(value, CTFWriter.Field):
2043 raise TypeError("Invalid value type.")
2044 ret = _bt_ctf_event_set_payload(self._e, str(field_name), value_field._f)
2046 raise ValueError("Could not set event field payload.")
2049 Get a field from event.
2051 def payload(self, field_name):
2052 native_instance = _bt_ctf_event_get_payload(self._e, str(field_name))
2053 if native_instance is None:
2054 raise ValueError("Could not get event payload.")
2055 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2059 Create a new stream class of the given name.
2061 def __init__(self, name):
2062 self._sc = _bt_ctf_stream_class_create(name)
2063 if self._sc is None:
2064 raise ValueError("Stream class creation failed.")
2067 _bt_ctf_stream_class_put(self._sc)
2070 Get a stream class' clock.
2074 raise NotImplementedError("Getter not implemented.")
2077 Assign a clock to a stream class.
2080 def clock(self, clock):
2081 if not isinstance(clock, CTFWriter.Clock):
2082 raise TypeError("Invalid clock type.")
2084 ret = _bt_ctf_stream_class_set_clock(self._sc, clock._c)
2086 raise ValueError("Could not set stream class clock.")
2089 Add an event class to a stream class. New events can be added even after a
2090 stream has been instantiated and events have been appended. However, a stream
2091 will not accept events of a class that has not been registered beforehand.
2093 def add_event_class(self, event_class):
2094 if not isinstance(event_class, CTFWriter.EventClass):
2095 raise TypeError("Invalid event_class type.")
2097 ret = _bt_ctf_stream_class_add_event_class(self._sc, event_class._ec)
2099 raise ValueError("Could not add event class.")
2103 Create a stream of the given class.
2105 def __init__(self, stream_class):
2106 if not isinstance(stream_class, CTFWriter.StreamClass):
2107 raise TypeError("Invalid stream_class type.")
2109 self._s = _bt_ctf_stream_create(stream_class._sc)
2111 raise ValueError("Stream creation failed.")
2114 _bt_ctf_stream_put(self._s)
2117 Increase the current packet's discarded event count.
2119 def append_discarded_events(self, event_count):
2120 _bt_ctf_stream_append_discarded_events(self._s, event_count)
2123 Append "event" to the stream's current packet. The stream's associated clock
2124 will be sampled during this call. The event shall not be modified after
2125 being appended to a stream.
2127 def append_event(self, event):
2128 ret = _bt_ctf_stream_append_event(self._s, event._e)
2130 raise ValueError("Could not append event to stream.")
2133 The stream's current packet's events will be flushed to disk. Events
2134 subsequently appended to the stream will be added to a new packet.
2137 ret = _bt_ctf_stream_flush(self._s)
2139 raise ValueError("Could not flush stream.")
2143 Create a new writer that will produce a trace in the given path.
2145 def __init__(self, path):
2146 self._w = _bt_ctf_writer_create(path)
2148 raise ValueError("Writer creation failed.")
2151 _bt_ctf_writer_put(self._w)
2154 Create a new stream instance and register it to the writer.
2156 def create_stream(self, stream_class):
2157 if not isinstance(stream_class, CTFWriter.StreamClass):
2158 raise TypeError("Invalid stream_class type.")
2160 stream = CTFWriter.Stream.__new__(CTFWriter.Stream)
2161 stream._s = _bt_ctf_writer_create_stream(self._w, stream_class._sc)
2165 Add an environment field to the trace.
2167 def add_environment_field(self, name, value):
2168 ret = _bt_ctf_writer_add_environment_field(self._w, str(name), str(value))
2170 raise ValueError("Could not add environment field to trace.")
2173 Add a clock to the trace. Clocks assigned to stream classes must be
2174 registered to the writer.
2176 def add_clock(self, clock):
2177 ret = _bt_ctf_writer_add_clock(self._w, clock._c)
2179 raise ValueError("Could not add clock to Writer.")
2182 Get the trace's TSDL meta-data.
2186 return _bt_ctf_writer_get_metadata_string(self._w)
2189 Flush the trace's metadata to the metadata file.
2191 def flush_metadata(self):
2192 _bt_ctf_writer_flush_metadata(self._w)
2195 Get the trace's byte order. Must be a constant from the ByteOrder
2199 def byte_order(self):
2200 raise NotImplementedError("Getter not implemented.")
2203 Set the trace's byte order. Must be a constant from the ByteOrder
2204 class. Defaults to BYTE_ORDER_NATIVE, the host machine's endianness.
2207 def byte_order(self, byte_order):
2208 ret = _bt_ctf_writer_set_byte_order(self._w, byte_order)
2210 raise ValueError("Could not set trace's byte order.")