Python bindings: cleanup whitespace
[babeltrace.git] / bindings / python / babeltrace.i.in
CommitLineData
24a3136a
DS
1/*
2 * babeltrace.i.in
3 *
4 * Babeltrace Python Module interface file
5 *
6 * Copyright 2012 EfficiOS Inc.
7 *
8 * Author: Danny Serres <danny.serres@efficios.com>
74ea15ad 9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
24a3136a
DS
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22
23%define DOCSTRING
24"BABELTRACE_VERSION_STR
25
26Babeltrace is a trace viewer and converter reading and writing the
27Common Trace Format (CTF). Its main use is to pretty-print CTF
28traces into a human-readable text output.
29
74ea15ad 30To use this module, the first step is to create a TraceCollection and add a
24a3136a
DS
31trace to it."
32%enddef
33
34%module(docstring=DOCSTRING) babeltrace
35
38f28df6 36%include "stdint.i"
24a3136a
DS
37%include "typemaps.i"
38%{
39#define SWIG_FILE_WITH_INIT
40#include <babeltrace/babeltrace.h>
41#include <babeltrace/babeltrace-internal.h>
42#include <babeltrace/trace-handle.h>
43#include <babeltrace/trace-handle-internal.h>
44#include <babeltrace/context.h>
45#include <babeltrace/context-internal.h>
46#include <babeltrace/iterator.h>
47#include <babeltrace/iterator-internal.h>
48#include <babeltrace/format.h>
49#include <babeltrace/list.h>
24a3136a
DS
50#include <babeltrace/types.h>
51#include <babeltrace/ctf/iterator.h>
52#include "python-complements.h"
ec8c88d7
JG
53#include <babeltrace/ctf-writer/clock.h>
54#include <babeltrace/ctf-writer/event-fields.h>
55#include <babeltrace/ctf-writer/event-types.h>
56#include <babeltrace/ctf-writer/event.h>
57#include <babeltrace/ctf-writer/stream.h>
58#include <babeltrace/ctf-writer/writer.h>
24a3136a
DS
59%}
60
24a3136a 61typedef int bt_intern_str;
5792eb34 62typedef int64_t ssize_t;
24a3136a 63
cebae8c3
JG
64/* =================================================================
65 PYTHON-COMPLEMENTS.H
66 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
67*/
68
cebae8c3
JG
69struct bt_definition **_bt_python_field_listcaller(
70 const struct bt_ctf_event *ctf_event,
71 const struct bt_definition *scope,
72 unsigned int *OUTPUT);
73struct bt_definition *_bt_python_field_one_from_list(
74 struct bt_definition **list, int index);
75struct bt_ctf_event_decl **_bt_python_event_decl_listcaller(
76 int handle_id,
77 struct bt_context *ctx,
78 unsigned int *OUTPUT);
79struct bt_ctf_event_decl *_bt_python_decl_one_from_list(
80 struct bt_ctf_event_decl **list, int index);
81struct bt_ctf_field_decl **_by_python_field_decl_listcaller(
82 struct bt_ctf_event_decl *event_decl,
83 enum bt_ctf_scope scope,
84 unsigned int *OUTPUT);
85struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list(
86 struct bt_ctf_field_decl **list, int index);
87struct definition_array *_bt_python_get_array_from_def(
88 struct bt_definition *field);
89struct definition_sequence *_bt_python_get_sequence_from_def(
90 struct bt_definition *field);
5792eb34
JG
91struct bt_declaration *_bt_python_get_array_element_declaration(
92 struct bt_declaration *field);
3866c850
JG
93struct bt_declaration *_bt_python_get_sequence_element_declaration(
94 struct bt_declaration *field);
5792eb34 95const char *_bt_python_get_array_string(struct bt_definition *field);
3866c850 96const char *_bt_python_get_sequence_string(struct bt_definition *field);
ec8c88d7
JG
97int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field);
98enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field);
b4b5e4b5
JG
99const char *_bt_python_ctf_field_type_enumeration_get_mapping(
100 struct bt_ctf_field_type *enumeration, size_t index,
101 int64_t *OUTPUT, int64_t *OUTPUT);
102const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
103 struct bt_ctf_field_type *enumeration, size_t index,
104 uint64_t *OUTPUT, uint64_t *OUTPUT);
105const char *_bt_python_ctf_field_type_structure_get_field_name(
106 struct bt_ctf_field_type *structure, size_t index);
107struct bt_ctf_field_type *_bt_python_ctf_field_type_structure_get_field_type(
108 struct bt_ctf_field_type *structure, size_t index);
109const char *_bt_python_ctf_field_type_variant_get_field_name(
110 struct bt_ctf_field_type *variant, size_t index);
111struct bt_ctf_field_type *_bt_python_ctf_field_type_variant_get_field_type(
112 struct bt_ctf_field_type *variant, size_t index);
38f28df6
JG
113const char *_bt_python_ctf_event_class_get_field_name(
114 struct bt_ctf_event_class *event_class, size_t index);
115struct bt_ctf_field_type *_bt_python_ctf_event_class_get_field_type(
116 struct bt_ctf_event_class *event_class, size_t index);
cebae8c3 117
24a3136a
DS
118/* =================================================================
119 CONTEXT.H, CONTEXT-INTERNAL.H
120 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
121*/
122
123%rename("_bt_context_create") bt_context_create(void);
124%rename("_bt_context_add_trace") bt_context_add_trace(
125 struct bt_context *ctx, const char *path, const char *format,
2c0df204
XH
126 void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence),
127 struct bt_mmap_stream_list *stream_list, FILE *metadata);
24a3136a
DS
128%rename("_bt_context_remove_trace") bt_context_remove_trace(
129 struct bt_context *ctx, int trace_id);
130%rename("_bt_context_get") bt_context_get(struct bt_context *ctx);
131%rename("_bt_context_put") bt_context_put(struct bt_context *ctx);
132%rename("_bt_ctf_event_get_context") bt_ctf_event_get_context(
133 const struct bt_ctf_event *event);
134
135struct bt_context *bt_context_create(void);
136int bt_context_add_trace(struct bt_context *ctx, const char *path, const char *format,
2c0df204
XH
137 void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence),
138 struct bt_mmap_stream_list *stream_list, FILE *metadata);
24a3136a
DS
139void bt_context_remove_trace(struct bt_context *ctx, int trace_id);
140void bt_context_get(struct bt_context *ctx);
141void bt_context_put(struct bt_context *ctx);
142struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event);
143
74ea15ad 144// class TraceCollection to prevent direct access to struct bt_context
24a3136a 145%pythoncode%{
74ea15ad 146class TraceCollection:
24a3136a 147 """
74ea15ad 148 The TraceCollection is the object that contains all currently opened traces.
24a3136a
DS
149 """
150
151 def __init__(self):
74ea15ad 152 self._tc = _bt_context_create()
24a3136a
DS
153
154 def __del__(self):
74ea15ad 155 _bt_context_put(self._tc)
24a3136a 156
74ea15ad 157 def add_trace(self, path, format_str):
24a3136a 158 """
74ea15ad 159 Add a trace by path to the TraceCollection.
24a3136a
DS
160
161 Open a trace.
162
163 path is the path to the trace, it is not recursive.
164 If "path" is None, stream_list is used instead as a list
165 of mmap streams to open for the trace.
166
167 format is a string containing the format name in which the trace was
168 produced.
169
24a3136a
DS
170 Return: the corresponding TraceHandle on success or None on error.
171 """
464425e1 172 ret = _bt_context_add_trace(self._tc, path, format_str, None, None, None)
24a3136a
DS
173 if ret < 0:
174 return None
175
176 th = TraceHandle.__new__(TraceHandle)
177 th._id = ret
464425e1 178 th._trace_collection = self
24a3136a
DS
179 return th
180
181 def add_traces_recursive(self, path, format_str):
182 """
183 Open a trace recursively.
184
185 Find each trace present in the subdirectory starting from the given
74ea15ad 186 path, and add them to the TraceCollection.
24a3136a
DS
187
188 Return a dict of TraceHandle instances (the full path is the key).
189 Return None on error.
190 """
191
192 import os
193
194 trace_handles = {}
195
196 noTrace = True
197 error = False
198
199 for fullpath, dirs, files in os.walk(path):
200 if "metadata" in files:
201 trace_handle = self.add_trace(fullpath, format_str)
202 if trace_handle is None:
203 error = True
204 continue
205
206 trace_handles[fullpath] = trace_handle
207 noTrace = False
208
209 if noTrace and error:
210 return None
211 return trace_handles
212
213 def remove_trace(self, trace_handle):
214 """
74ea15ad 215 Remove a trace from the TraceCollection.
24a3136a
DS
216 Effectively closing the trace.
217 """
218 try:
74ea15ad 219 _bt_context_remove_trace(self._tc, trace_handle._id)
24a3136a
DS
220 except AttributeError:
221 raise TypeError("in remove_trace, "
222 "argument 2 must be a TraceHandle instance")
74ea15ad
JG
223
224 @property
225 def events(self):
226 """
227 Generator function to iterate over the events of open in the current
228 TraceCollection.
9213bd4f
JG
229
230 Due to limitations of the native Babeltrace API, only one event
231 may be "alive" at a time (i.e. a user should never store a copy
232 of the events returned by this function for ulterior use). Users
233 shall make sure to copy the information they need from an event
234 before accessing the next one.
235
236 Furthermore, event objects become invalid when the generator goes
237 out of scope as the underlying iterator will be reclaimed. Using an
238 event after the the generator has gone out of scope may result in a
239 crash or data corruption.
74ea15ad
JG
240 """
241 begin_pos_ptr = _bt_iter_pos()
242 end_pos_ptr = _bt_iter_pos()
243 begin_pos_ptr.type = SEEK_BEGIN
244 end_pos_ptr.type = SEEK_LAST
245
246 for event in self._events(begin_pos_ptr, end_pos_ptr):
247 yield event
248
249 def events_timestamps(self, timestamp_begin, timestamp_end):
250 """
251 Generator function to iterate over the events of open in the current
252 TraceCollection from timestamp_begin to timestamp_end.
253 """
254 begin_pos_ptr = _bt_iter_pos()
255 end_pos_ptr = _bt_iter_pos()
256 begin_pos_ptr.type = end_pos_ptr.type = SEEK_TIME
257 begin_pos_ptr.u.seek_time = timestamp_begin
258 end_pos_ptr.u.seek_time = timestamp_end
259
260 for event in self._events(begin_pos_ptr, end_pos_ptr):
261 yield event
262
263 @property
264 def timestamp_begin(self):
265 pos_ptr = _bt_iter_pos()
266 pos_ptr.type = SEEK_BEGIN
267 return self._timestamp_at_pos(pos_ptr)
268
269 @property
270 def timestamp_end(self):
271 pos_ptr = _bt_iter_pos()
272 pos_ptr.type = SEEK_LAST
273 return self._timestamp_at_pos(pos_ptr)
274
275 def _timestamp_at_pos(self, pos_ptr):
276 ctf_it_ptr = _bt_ctf_iter_create(self._tc, pos_ptr, pos_ptr)
277 if ctf_it_ptr is None:
278 raise NotImplementedError("Creation of multiple iterators is unsupported.")
279 ev_ptr = _bt_ctf_iter_read_event(ctf_it_ptr)
280 _bt_ctf_iter_destroy(ctf_it_ptr)
281 if ev_ptr is None:
282 return None;
283
284 def _events(self, begin_pos_ptr, end_pos_ptr):
285 ctf_it_ptr = _bt_ctf_iter_create(self._tc, begin_pos_ptr, end_pos_ptr)
286 if ctf_it_ptr is None:
287 raise NotImplementedError(
288 "Creation of multiple iterators is unsupported.")
289
290 while True:
291 ev_ptr = _bt_ctf_iter_read_event(ctf_it_ptr)
292 if ev_ptr is None:
293 break
294
78d714e8 295 ev = Event.__new__(Event)
74ea15ad
JG
296 ev._e = ev_ptr
297 try:
298 yield ev
299 except GeneratorExit:
300 break
301
302 ret = _bt_iter_next(_bt_ctf_get_iter(ctf_it_ptr))
303 if ret != 0:
304 break
305
306 _bt_ctf_iter_destroy(ctf_it_ptr)
307
24a3136a
DS
308%}
309
310
311
312/* =================================================================
313 FORMAT.H, REGISTRY
314 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
315*/
316
317%rename("lookup_format") bt_lookup_format(bt_intern_str qname);
318%rename("_bt_print_format_list") bt_fprintf_format_list(FILE *fp);
319%rename("register_format") bt_register_format(struct format *format);
cc26a15a 320%rename("unregister_format") bt_unregister_format(struct bt_format *format);
24a3136a
DS
321
322extern struct format *bt_lookup_format(bt_intern_str qname);
323extern void bt_fprintf_format_list(FILE *fp);
2c0df204 324extern int bt_register_format(struct bt_format *format);
cc26a15a 325extern void bt_unregister_format(struct bt_format *format);
24a3136a 326
24a3136a
DS
327%pythoncode %{
328
329def print_format_list(babeltrace_file):
330 """
331 Print a list of available formats to file.
332
333 babeltrace_file must be a File instance opened in write mode.
334 """
335 try:
336 if babeltrace_file._file is not None:
337 _bt_print_format_list(babeltrace_file._file)
338 except AttributeError:
339 raise TypeError("in print_format_list, "
340 "argument 1 must be a File instance")
341
342%}
343
344
345/* =================================================================
346 ITERATOR.H, ITERATOR-INTERNAL.H
347 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
348*/
349
350%rename("_bt_iter_create") bt_iter_create(struct bt_context *ctx,
351 const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos);
352%rename("_bt_iter_destroy") bt_iter_destroy(struct bt_iter *iter);
353%rename("_bt_iter_next") bt_iter_next(struct bt_iter *iter);
354%rename("_bt_iter_get_pos") bt_iter_get_pos(struct bt_iter *iter);
355%rename("_bt_iter_free_pos") bt_iter_free_pos(struct bt_iter_pos *pos);
356%rename("_bt_iter_set_pos") bt_iter_set_pos(struct bt_iter *iter,
357 const struct bt_iter_pos *pos);
358%rename("_bt_iter_create_time_pos") bt_iter_create_time_pos(struct bt_iter *iter,
359 uint64_t timestamp);
360
361struct bt_iter *bt_iter_create(struct bt_context *ctx,
362 const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos);
363void bt_iter_destroy(struct bt_iter *iter);
364int bt_iter_next(struct bt_iter *iter);
365struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter);
366void bt_iter_free_pos(struct bt_iter_pos *pos);
367int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *pos);
368struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, uint64_t timestamp);
369
370%rename("_bt_iter_pos") bt_iter_pos;
371%rename("SEEK_TIME") BT_SEEK_TIME;
372%rename("SEEK_RESTORE") BT_SEEK_RESTORE;
373%rename("SEEK_CUR") BT_SEEK_CUR;
374%rename("SEEK_BEGIN") BT_SEEK_BEGIN;
ef42f9eb 375%rename("SEEK_LAST") BT_SEEK_LAST;
24a3136a
DS
376
377// This struct is taken from iterator.h
378// All changes to the struct must also be made here
379struct bt_iter_pos {
380 enum {
381 BT_SEEK_TIME, /* uses u.seek_time */
382 BT_SEEK_RESTORE, /* uses u.restore */
383 BT_SEEK_CUR,
384 BT_SEEK_BEGIN,
ef42f9eb 385 BT_SEEK_LAST
24a3136a
DS
386 } type;
387 union {
388 uint64_t seek_time;
389 struct bt_saved_pos *restore;
390 } u;
391};
392
24a3136a
DS
393/* =================================================================
394 TRACE-HANDLE.H, TRACE-HANDLE-INTERNAL.H
395 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
396*/
397
398%rename("_bt_trace_handle_create") bt_trace_handle_create(struct bt_context *ctx);
399%rename("_bt_trace_handle_destroy") bt_trace_handle_destroy(struct bt_trace_handle *bt);
400struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx);
401void bt_trace_handle_destroy(struct bt_trace_handle *bt);
402
403%rename("_bt_trace_handle_get_path") bt_trace_handle_get_path(struct bt_context *ctx,
404 int handle_id);
405%rename("_bt_trace_handle_get_timestamp_begin") bt_trace_handle_get_timestamp_begin(
406 struct bt_context *ctx, int handle_id, enum bt_clock_type type);
407%rename("_bt_trace_handle_get_timestamp_end") bt_trace_handle_get_timestamp_end(
408 struct bt_context *ctx, int handle_id, enum bt_clock_type type);
24a3136a
DS
409const char *bt_trace_handle_get_path(struct bt_context *ctx, int handle_id);
410uint64_t bt_trace_handle_get_timestamp_begin(struct bt_context *ctx, int handle_id,
411 enum bt_clock_type type);
412uint64_t bt_trace_handle_get_timestamp_end(struct bt_context *ctx, int handle_id,
413 enum bt_clock_type type);
414
415%rename("_bt_ctf_event_get_handle_id") bt_ctf_event_get_handle_id(
416 const struct bt_ctf_event *event);
417int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event);
418
419
420%pythoncode%{
421
8a2cd19a 422# Based on enum bt_clock_type in clock-type.h
464425e1
JG
423class ClockType:
424 CLOCK_CYCLES = 0
425 CLOCK_REAL = 1
426
24a3136a
DS
427class TraceHandle(object):
428 """
429 The TraceHandle allows the user to manipulate a trace file directly.
430 It is a unique identifier representing a trace file.
431 Do not instantiate.
432 """
433
434 def __init__(self):
435 raise NotImplementedError("TraceHandle cannot be instantiated")
436
437 def __repr__(self):
288209fb 438 return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id)
24a3136a 439
464425e1
JG
440 @property
441 def id(self):
24a3136a
DS
442 """Return the TraceHandle id."""
443 return self._id
444
464425e1
JG
445 @property
446 def path(self):
24a3136a 447 """Return the path of a TraceHandle."""
464425e1 448 return _bt_trace_handle_get_path(self._trace_collection._tc, self._id)
24a3136a 449
464425e1
JG
450 @property
451 def timestamp_begin(self):
24a3136a 452 """Return the creation time of the buffers of a trace."""
464425e1
JG
453 return _bt_trace_handle_get_timestamp_begin(
454 self._trace_collection._tc, self._id, ClockType.CLOCK_REAL)
24a3136a 455
464425e1
JG
456 @property
457 def timestamp_end(self):
24a3136a 458 """Return the destruction timestamp of the buffers of a trace."""
464425e1
JG
459 return _bt_trace_handle_get_timestamp_end(
460 self._trace_collection._tc, self._id, ClockType.CLOCK_REAL)
24a3136a 461
8bb27181
JG
462 @property
463 def events(self):
464 """
465 Generator returning all events (EventDeclaration) in a trace.
466 """
467 ret = _bt_python_event_decl_listcaller(self.id, self._trace_collection._tc)
468
469 if not isinstance(ret, list):
470 return
471
472 ptr_list, count = ret
473 for i in range(count):
474 tmp = EventDeclaration.__new__(EventDeclaration)
475 tmp._ed = _bt_python_decl_one_from_list(ptr_list, i)
476 yield tmp
477
24a3136a
DS
478%}
479
480
481
482// =================================================================
483// CTF
484// =================================================================
485
486/* =================================================================
487 ITERATOR.H, EVENTS.H
488 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
489*/
490
491//Iterator
492%rename("_bt_ctf_iter_create") bt_ctf_iter_create(struct bt_context *ctx,
493 const struct bt_iter_pos *begin_pos,
494 const struct bt_iter_pos *end_pos);
495%rename("_bt_ctf_get_iter") bt_ctf_get_iter(struct bt_ctf_iter *iter);
496%rename("_bt_ctf_iter_destroy") bt_ctf_iter_destroy(struct bt_ctf_iter *iter);
497%rename("_bt_ctf_iter_read_event") bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
498
499struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx,
500 const struct bt_iter_pos *begin_pos,
501 const struct bt_iter_pos *end_pos);
502struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter);
503void bt_ctf_iter_destroy(struct bt_ctf_iter *iter);
504struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter);
505
506
507//Events
24a3136a
DS
508%rename("_bt_ctf_get_top_level_scope") bt_ctf_get_top_level_scope(const struct
509 bt_ctf_event *event, enum bt_ctf_scope scope);
510%rename("_bt_ctf_event_name") bt_ctf_event_name(const struct bt_ctf_event *ctf_event);
511%rename("_bt_ctf_get_timestamp") bt_ctf_get_timestamp(
512 const struct bt_ctf_event *ctf_event);
513%rename("_bt_ctf_get_cycles") bt_ctf_get_cycles(
514 const struct bt_ctf_event *ctf_event);
515
516%rename("_bt_ctf_get_field") bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
2c0df204 517 const struct bt_definition *scope, const char *field);
24a3136a 518%rename("_bt_ctf_get_index") bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
2c0df204
XH
519 const struct bt_definition *field, unsigned int index);
520%rename("_bt_ctf_field_name") bt_ctf_field_name(const struct bt_definition *field);
521%rename("_bt_ctf_field_type") bt_ctf_field_type(const struct bt_declaration *field);
24a3136a 522%rename("_bt_ctf_get_int_signedness") bt_ctf_get_int_signedness(
2c0df204
XH
523 const struct bt_declaration *field);
524%rename("_bt_ctf_get_int_base") bt_ctf_get_int_base(const struct bt_declaration *field);
24a3136a 525%rename("_bt_ctf_get_int_byte_order") bt_ctf_get_int_byte_order(
2c0df204
XH
526 const struct bt_declaration *field);
527%rename("_bt_ctf_get_int_len") bt_ctf_get_int_len(const struct bt_declaration *field);
4191bcd2
XH
528%rename("_bt_ctf_get_enum_int") bt_ctf_get_enum_int(const struct bt_definition *field);
529%rename("_bt_ctf_get_enum_str") bt_ctf_get_enum_str(const struct bt_definition *field);
2c0df204
XH
530%rename("_bt_ctf_get_encoding") bt_ctf_get_encoding(const struct bt_declaration *field);
531%rename("_bt_ctf_get_array_len") bt_ctf_get_array_len(const struct bt_declaration *field);
532%rename("_bt_ctf_get_uint64") bt_ctf_get_uint64(const struct bt_definition *field);
533%rename("_bt_ctf_get_int64") bt_ctf_get_int64(const struct bt_definition *field);
534%rename("_bt_ctf_get_char_array") bt_ctf_get_char_array(const struct bt_definition *field);
535%rename("_bt_ctf_get_string") bt_ctf_get_string(const struct bt_definition *field);
e5a73b90 536%rename("_bt_ctf_get_float") bt_ctf_get_float(const struct bt_definition *field);
812e6682 537%rename("_bt_ctf_get_variant") bt_ctf_get_variant(const struct bt_definition *field);
24a3136a
DS
538%rename("_bt_ctf_field_get_error") bt_ctf_field_get_error(void);
539%rename("_bt_ctf_get_decl_event_name") bt_ctf_get_decl_event_name(const struct
540 bt_ctf_event_decl *event);
78564612
AM
541%rename("_bt_ctf_get_decl_event_id") bt_ctf_get_decl_event_id(const struct
542 bt_ctf_event_decl *event);
24a3136a
DS
543%rename("_bt_ctf_get_decl_field_name") bt_ctf_get_decl_field_name(
544 const struct bt_ctf_field_decl *field);
f01efa78 545%rename("_bt_ctf_get_decl_from_def") bt_ctf_get_decl_from_def(
2c0df204 546 const struct bt_definition *field);
8bb27181
JG
547%rename("_bt_ctf_get_decl_from_field_decl") bt_ctf_get_decl_from_field_decl(
548 const struct bt_ctf_field_decl *field);
3c2ce778 549%rename("_bt_array_index") bt_array_index(struct definition_array *array, uint64_t i);
786207e0
XH
550%rename("_bt_sequence_len") bt_sequence_len(struct definition_sequence *sequence);
551%rename("_bt_sequence_index") bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
3a068915
JG
552%rename("_bt_ctf_get_struct_field_count") bt_ctf_get_struct_field_count(const struct bt_definition *structure);
553%rename("_bt_ctf_get_struct_field_index") bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i);
24a3136a 554
2c0df204 555const struct bt_definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event,
24a3136a
DS
556 enum bt_ctf_scope scope);
557const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event);
558uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event);
559uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event);
2c0df204
XH
560const struct bt_definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event,
561 const struct bt_definition *scope,
24a3136a 562 const char *field);
2c0df204
XH
563const struct bt_definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event,
564 const struct bt_definition *field,
24a3136a 565 unsigned int index);
2c0df204
XH
566const char *bt_ctf_field_name(const struct bt_definition *field);
567enum ctf_type_id bt_ctf_field_type(const struct bt_declaration *field);
568int bt_ctf_get_int_signedness(const struct bt_declaration *field);
569int bt_ctf_get_int_base(const struct bt_declaration *field);
570int bt_ctf_get_int_byte_order(const struct bt_declaration *field);
571ssize_t bt_ctf_get_int_len(const struct bt_declaration *field);
4191bcd2
XH
572const struct bt_definition *bt_ctf_get_enum_int(const struct bt_definition *field);
573const char *bt_ctf_get_enum_str(const struct bt_definition *field);
2c0df204
XH
574enum ctf_string_encoding bt_ctf_get_encoding(const struct bt_declaration *field);
575int bt_ctf_get_array_len(const struct bt_declaration *field);
3c2ce778 576struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i);
2c0df204
XH
577uint64_t bt_ctf_get_uint64(const struct bt_definition *field);
578int64_t bt_ctf_get_int64(const struct bt_definition *field);
579char *bt_ctf_get_char_array(const struct bt_definition *field);
580char *bt_ctf_get_string(const struct bt_definition *field);
e5a73b90 581double bt_ctf_get_float(const struct bt_definition *field);
812e6682 582const struct bt_definition *bt_ctf_get_variant(const struct bt_definition *field);
24a3136a
DS
583int bt_ctf_field_get_error(void);
584const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event);
78564612 585uint64_t bt_ctf_get_decl_event_id(const struct bt_ctf_event_decl *event);
24a3136a 586const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field);
2c0df204 587const struct bt_declaration *bt_ctf_get_decl_from_def(const struct bt_definition *field);
8bb27181 588const struct bt_declaration *bt_ctf_get_decl_from_field_decl(const struct bt_ctf_field_decl *field);
786207e0
XH
589uint64_t bt_sequence_len(struct definition_sequence *sequence);
590struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
3a068915
JG
591uint64_t bt_ctf_get_struct_field_count(const struct bt_definition *structure);
592const struct bt_definition *bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i);
24a3136a
DS
593
594%pythoncode%{
595
24d5c942
JG
596class CTFStringEncoding:
597 NONE = 0
598 UTF8 = 1
599 ASCII = 2
600 UNKNOWN = 3
601
8bb27181
JG
602# Based on the enum in ctf-writer/writer.h
603class ByteOrder:
604 BYTE_ORDER_NATIVE = 0
605 BYTE_ORDER_LITTLE_ENDIAN = 1
606 BYTE_ORDER_BIG_ENDIAN = 2
607 BYTE_ORDER_NETWORK = 3
608 BYTE_ORDER_UNKNOWN = 4 # Python-specific entry
609
24d5c942
JG
610#enum equivalent, accessible constants
611#These are taken directly from ctf/events.h
612#All changes to enums must also be made here
613class CTFTypeId:
614 UNKNOWN = 0
615 INTEGER = 1
616 FLOAT = 2
617 ENUM = 3
618 STRING = 4
619 STRUCT = 5
620 UNTAGGED_VARIANT = 6
621 VARIANT = 7
622 ARRAY = 8
623 SEQUENCE = 9
624 NR_CTF_TYPES = 10
8bb27181
JG
625 def type_name(id):
626 name = "UNKNOWN_TYPE"
24d5c942
JG
627 constants = [attr for attr in dir(CTFTypeId) if not callable(getattr(CTFTypeId, attr)) and not attr.startswith("__")]
628 for attr in constants:
629 if getattr(CTFTypeId, attr) == id:
630 name = attr
631 break
632 return name
633
8bb27181 634class CTFScope:
78d714e8
JG
635 TRACE_PACKET_HEADER = 0
636 STREAM_PACKET_CONTEXT = 1
637 STREAM_EVENT_HEADER = 2
638 STREAM_EVENT_CONTEXT = 3
639 EVENT_CONTEXT = 4
640 EVENT_FIELDS = 5
641
8bb27181
JG
642 def scope_name(scope):
643 name = "UNKNOWN_SCOPE"
644 constants = [attr for attr in dir(CTFScope) if not callable(getattr(CTFScope, attr)) and not attr.startswith("__")]
645 for attr in constants:
646 if getattr(CTFScope, attr) == scope:
647 name = attr
648 break
649 return name
650
e404272a 651# Priority of the scopes when searching for event fields
8bb27181
JG
652_scopes = [CTFScope.EVENT_FIELDS, CTFScope.EVENT_CONTEXT, CTFScope.STREAM_EVENT_CONTEXT,
653 CTFScope.STREAM_EVENT_HEADER, CTFScope.STREAM_PACKET_CONTEXT, CTFScope.TRACE_PACKET_HEADER]
e404272a 654
78d714e8 655import collections
49a04569 656from datetime import datetime
78d714e8
JG
657class Event(collections.Mapping):
658 """
659 This class represents an event from the trace.
660 It is obtained using the TraceCollection generator functions.
661 Do not instantiate.
662 """
78d714e8
JG
663 def __init__(self):
664 raise NotImplementedError("Event cannot be instantiated")
665
666 @property
667 def name(self):
668 """Return the name of the event or None on error."""
669 return _bt_ctf_event_name(self._e)
670
671 @property
672 def cycles(self):
24a3136a 673 """
78d714e8
JG
674 Return the timestamp of the event as written in
675 the packet (in cycles) or -1ULL on error.
24a3136a 676 """
78d714e8 677 return _bt_ctf_get_cycles(self._e)
24a3136a 678
78d714e8
JG
679 @property
680 def timestamp(self):
681 """
682 Return the timestamp of the event offset with the
683 system clock source or -1ULL on error.
684 """
685 return _bt_ctf_get_timestamp(self._e)
686
49a04569
JG
687 @property
688 def datetime(self):
689 """
690 Return a datetime object based on the event's
691 timestamp. Note that the datetime class' precision
692 is limited to microseconds.
693 """
694 return datetime.fromtimestamp(self.timestamp / 1E9)
695
78d714e8
JG
696 def field_with_scope(self, field_name, scope):
697 """
698 Get field_name's value in scope.
699 None is returned if no field matches field_name.
700 """
e404272a 701 if not scope in _scopes:
78d714e8
JG
702 raise ValueError("Invalid scope provided")
703 field = self._field_with_scope(field_name, scope)
704 if field is not None:
705 return field.value
706 return None
707
708 def field_list_with_scope(self, scope):
709 """Return a list of field names in scope."""
e404272a 710 if not scope in _scopes:
78d714e8
JG
711 raise ValueError("Invalid scope provided")
712 field_names = []
713 for field in self._field_list_with_scope(scope):
714 field_names.append(field.name)
715 return field_names
716
717 @property
718 def handle(self):
719 """
720 Get the TraceHandle associated with this event
721 Return None on error
722 """
723 ret = _bt_ctf_event_get_handle_id(self._e)
724 if ret < 0:
786207e0
XH
725 return None
726
78d714e8
JG
727 th = TraceHandle.__new__(TraceHandle)
728 th._id = ret
729 th._trace_collection = self.get_trace_collection()
730 return th
731
732 @property
733 def trace_collection(self):
734 """
735 Get the TraceCollection associated with this event.
736 Return None on error.
24a3136a 737 """
78d714e8
JG
738 trace_collection = TraceCollection()
739 trace_collection._tc = _bt_ctf_event_get_context(self._e);
740 if trace_collection._tc is None:
741 return None
742 else:
743 return trace_collection
744
745 def __getitem__(self, field_name):
24a3136a 746 """
78d714e8
JG
747 Get field_name's value. If the field_name exists in multiple
748 scopes, the first field found is returned. The scopes are searched
749 in the following order:
750 1) EVENT_FIELDS
751 2) EVENT_CONTEXT
752 3) STREAM_EVENT_CONTEXT
753 4) STREAM_EVENT_HEADER
754 5) STREAM_PACKET_CONTEXT
755 6) TRACE_PACKET_HEADER
756 None is returned if no field matches field_name.
757
758 Use field_with_scope() to explicitly access fields in a given
759 scope.
760 """
761 field = self._field(field_name)
762 if field is not None:
763 return field.value
764 raise KeyError(field_name)
765
766 def __iter__(self):
767 for key in self.keys():
768 yield key
769
770 def __len__(self):
771 count = 0
e404272a 772 for scope in _scopes:
78d714e8
JG
773 scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope)
774 ret = _bt_python_field_listcaller(self._e, scope_ptr)
775 if isinstance(ret, list):
776 count += ret[1]
777 return count
778
779 def __contains__(self, field_name):
780 return self._field(field_name) is not None
781
782 def keys(self):
783 """Return a list of field names."""
784 field_names = set()
e404272a 785 for scope in _scopes:
78d714e8
JG
786 for name in self.field_list_with_scope(scope):
787 field_names.add(name)
788 return list(field_names)
789
790 def get(self, field_name, default = None):
791 field = self._field(field_name)
792 if field is None:
793 return default
794 return field.value
795
796 def items(self):
797 for field in self.keys():
798 yield (field, self[field])
799
800 def _field_with_scope(self, field_name, scope):
801 scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope)
802 if scope_ptr is None:
803 return None
804
805 definition_ptr = _bt_ctf_get_field(self._e, scope_ptr, field_name)
806 if definition_ptr is None:
807 return None
24a3136a 808
78d714e8
JG
809 field = _Definition(definition_ptr, scope)
810 return field
811
812 def _field(self, field_name):
813 field = None
e404272a 814 for scope in _scopes:
78d714e8
JG
815 field = self._field_with_scope(field_name, scope)
816 if field is not None:
817 break
818 return field
819
820 def _field_list_with_scope(self, scope):
821 fields = []
822 scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope)
feb06edb 823
78d714e8
JG
824 # Returns a list [list_ptr, count]. If list_ptr is NULL, SWIG will only
825 # provide the "count" return value
826 count = 0
827 list_ptr = None
828 ret = _bt_python_field_listcaller(self._e, scope_ptr)
829 if isinstance(ret, list):
830 list_ptr, count = ret
831
832 for i in range(count):
833 definition_ptr = _bt_python_field_one_from_list(list_ptr, i)
834 if definition_ptr is not None:
835 definition = _Definition(definition_ptr, scope)
836 fields.append(definition)
837 return fields
838
839class FieldError(Exception):
840 def __init__(self, value):
841 self.value = value
842
843 def __str__(self):
844 return repr(self.value)
845
cb1fcc68 846class EventDeclaration(object):
78d714e8 847 """Event declaration class. Do not instantiate."""
78564612 848 MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
78d714e8
JG
849
850 def __init__(self):
cb1fcc68 851 raise NotImplementedError("EventDeclaration cannot be instantiated")
78d714e8 852
cb1fcc68
JG
853 @property
854 def name(self):
78d714e8 855 """Return the name of the event or None on error"""
cb1fcc68 856 return _bt_ctf_get_decl_event_name(self._ed)
78d714e8 857
78564612
AM
858 @property
859 def id(self):
860 """Return the event-ID of the event or -1 on error"""
861 id = _bt_ctf_get_decl_event_id(self._ed)
862 if id == self.MAX_UINT64:
863 id = -1
864 return id
865
8bb27181
JG
866 @property
867 def fields(self):
24a3136a 868 """
8bb27181
JG
869 Generator returning all FieldDeclarations of an event, going through
870 each scope in the following order:
871 1) EVENT_FIELDS
872 2) EVENT_CONTEXT
873 3) STREAM_EVENT_CONTEXT
874 4) STREAM_EVENT_HEADER
875 5) STREAM_PACKET_CONTEXT
876 6) TRACE_PACKET_HEADER
877 """
878 for scope in _scopes:
879 for declaration in self.fields_scope(scope):
880 yield declaration
881
882 def fields_scope(self, scope):
883 """
884 Generator returning FieldDeclarations of the current event in scope.
24a3136a 885 """
cb1fcc68 886 ret = _by_python_field_decl_listcaller(self._ed, scope)
24a3136a 887
cb1fcc68 888 if not isinstance(ret, list):
8bb27181 889 return
24a3136a 890
cb1fcc68 891 list_ptr, count = ret
cb1fcc68 892 for i in range(count):
8bb27181
JG
893 field_declaration_ptr = _bt_python_field_decl_one_from_list(list_ptr, i)
894 if field_declaration_ptr is not None:
895 declaration_ptr = _bt_ctf_get_decl_from_field_decl(field_declaration_ptr)
896 field_declaration = _create_field_declaration(declaration_ptr, _bt_ctf_get_decl_field_name(field_declaration_ptr), scope)
897 yield field_declaration
cb1fcc68
JG
898
899class FieldDeclaration(object):
900 """Field declaration class. Do not instantiate."""
78d714e8 901 def __init__(self):
cb1fcc68 902 raise NotImplementedError("FieldDeclaration cannot be instantiated")
78d714e8 903
8bb27181
JG
904 def __repr__(self):
905 return "({0}) {1} {2}".format(CTFScope.scope_name(self.scope), CTFTypeId.type_name(self.type), self.name)
906
cb1fcc68
JG
907 @property
908 def name(self):
8bb27181
JG
909 """Return the name of a FieldDeclaration or None on error."""
910 return self._name
911
912 @property
913 def type(self):
914 """
915 Return the FieldDeclaration's type. One of the entries in class
916 CTFTypeId.
917 """
918 return _bt_ctf_field_type(self._fd)
919
920 @property
921 def scope(self):
922 """
923 Return the FieldDeclaration's scope.
924 """
925 return self._s
926
927class IntegerFieldDeclaration(FieldDeclaration):
928 """Do not instantiate."""
929 def __init__(self):
930 raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
931
932 @property
933 def signedness(self):
934 """
935 Return the signedness of an integer:
936 0 if unsigned; 1 if signed; -1 on error.
937 """
938 return _bt_ctf_get_int_signedness(self._fd)
939
940 @property
941 def base(self):
942 """Return the base of an int or a negative value on error."""
943 return _bt_ctf_get_int_base(self._fd)
944
945 @property
946 def byte_order(self):
947 """
948 Return the byte order. One of class ByteOrder's entries.
949 """
950 ret = _bt_ctf_get_int_byte_order(self._fd)
951 if ret == 1234:
952 return ByteOrder.BYTE_ORDER_LITTLE_ENDIAN
953 elif ret == 4321:
954 return ByteOrder.BYTE_ORDER_BIG_ENDIAN
955 else:
956 return ByteOrder.BYTE_ORDER_UNKNOWN
957
958 @property
959 def length(self):
960 """
961 Return the size, in bits, of an int or a negative
962 value on error.
963 """
964 return _bt_ctf_get_int_len(self._fd)
965
966 @property
967 def encoding(self):
968 """
969 Return the encoding. One of class CTFStringEncoding's entries.
970 Return a negative value on error.
971 """
972 return _bt_ctf_get_encoding(self._fd)
973
974class EnumerationFieldDeclaration(FieldDeclaration):
975 """Do not instantiate."""
976 def __init__(self):
977 raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
978
979class ArrayFieldDeclaration(FieldDeclaration):
980 """Do not instantiate."""
981 def __init__(self):
982 raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
983
984 @property
985 def length(self):
986 """
987 Return the length of an array or a negative
988 value on error.
989 """
990 return _bt_ctf_get_array_len(self._fd)
991
5792eb34
JG
992 @property
993 def element_declaration(self):
994 """
995 Return element declaration.
996 """
997 field_decl_ptr = _bt_python_get_array_element_declaration(self._fd)
998 return _create_field_declaration(field_decl_ptr, "", self.scope)
999
8bb27181
JG
1000class SequenceFieldDeclaration(FieldDeclaration):
1001 """Do not instantiate."""
1002 def __init__(self):
1003 raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
1004
3866c850
JG
1005 @property
1006 def element_declaration(self):
1007 """
1008 Return element declaration.
1009 """
1010 field_decl_ptr = _bt_python_get_sequence_element_declaration(self._fd)
1011 return _create_field_declaration(field_decl_ptr, "", self.scope)
1012
8bb27181
JG
1013class FloatFieldDeclaration(FieldDeclaration):
1014 """Do not instantiate."""
1015 def __init__(self):
1016 raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
1017
1018class StructureFieldDeclaration(FieldDeclaration):
1019 """Do not instantiate."""
1020 def __init__(self):
1021 raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
1022
1023class StringFieldDeclaration(FieldDeclaration):
1024 """Do not instantiate."""
1025 def __init__(self):
1026 raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
1027
1028class VariantFieldDeclaration(FieldDeclaration):
1029 """Do not instantiate."""
1030 def __init__(self):
1031 raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
78d714e8
JG
1032
1033def field_error():
1034 """
1035 Return the last error code encountered while
1036 accessing a field and reset the error flag.
1037 Return 0 if no error, a negative value otherwise.
1038 """
1039 return _bt_ctf_field_get_error()
1040
8bb27181 1041def _create_field_declaration(declaration_ptr, name, scope):
78d714e8 1042 """
8bb27181 1043 Private field declaration factory.
78d714e8 1044 """
8bb27181
JG
1045 if declaration_ptr is None:
1046 raise ValueError("declaration_ptr must be valid")
1047 if not scope in _scopes:
1048 raise ValueError("Invalid scope provided")
1049
1050 type = _bt_ctf_field_type(declaration_ptr)
1051 declaration = None
1052 if type == CTFTypeId.INTEGER:
1053 declaration = IntegerFieldDeclaration.__new__(IntegerFieldDeclaration)
1054 elif type == CTFTypeId.ENUM:
1055 declaration = EnumerationFieldDeclaration.__new__(EnumerationFieldDeclaration)
1056 elif type == CTFTypeId.ARRAY:
1057 declaration = ArrayFieldDeclaration.__new__(ArrayFieldDeclaration)
1058 elif type == CTFTypeId.SEQUENCE:
1059 declaration = SequenceFieldDeclaration.__new__(SequenceFieldDeclaration)
1060 elif type == CTFTypeId.FLOAT:
1061 declaration = FloatFieldDeclaration.__new__(FloatFieldDeclaration)
1062 elif type == CTFTypeId.STRUCT:
1063 declaration = StructureFieldDeclaration.__new__(StructureFieldDeclaration)
1064 elif type == CTFTypeId.STRING:
1065 declaration = StringFieldDeclaration.__new__(StringFieldDeclaration)
1066 elif type == CTFTypeId.VARIANT:
1067 declaration = VariantFieldDeclaration.__new__(VariantFieldDeclaration)
1068 else:
1069 return declaration
1070
1071 declaration._fd = declaration_ptr
1072 declaration._s = scope
1073 declaration._name = name
1074 return declaration
78d714e8
JG
1075
1076class _Definition(object):
1077 def __init__(self, definition_ptr, scope):
1078 self._d = definition_ptr
1079 self._s = scope
e404272a 1080 if not scope in _scopes:
78d714e8
JG
1081 ValueError("Invalid scope provided")
1082
78d714e8
JG
1083 @property
1084 def name(self):
1085 """Return the name of a field or None on error."""
1086 return _bt_ctf_field_name(self._d)
1087
5792eb34 1088 @property
78d714e8
JG
1089 def type(self):
1090 """Return the type of a field or -1 if unknown."""
1091 return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d))
1092
8bb27181
JG
1093 @property
1094 def declaration(self):
1095 """Return the associated Definition object."""
1096 return _create_field_declaration(_bt_ctf_get_decl_from_def(self._d), self.name, self.scope)
78d714e8 1097
8bb27181 1098 def _get_enum_str(self):
78d714e8
JG
1099 """
1100 Return the string matching the current enumeration.
1101 Return None on error.
1102 """
1103 return _bt_ctf_get_enum_str(self._d)
1104
8bb27181 1105 def _get_array_element_at(self, index):
78d714e8
JG
1106 """
1107 Return the array's element at position index.
1108 Return None on error
1109 """
1110 array_ptr = _bt_python_get_array_from_def(self._d)
1111 if array_ptr is None:
1112 return None
1113
1114 definition_ptr = _bt_array_index(array_ptr, index)
1115 if definition_ptr is None:
1116 return None
1117 return _Definition(definition_ptr, self.scope)
1118
8bb27181 1119 def _get_sequence_len(self):
78d714e8
JG
1120 """
1121 Return the len of a sequence or a negative
1122 value on error.
1123 """
1124 seq = _bt_python_get_sequence_from_def(self._d)
1125 return _bt_sequence_len(seq)
1126
8bb27181 1127 def _get_sequence_element_at(self, index):
78d714e8
JG
1128 """
1129 Return the sequence's element at position index,
1130 otherwise return None
1131 """
1132 seq = _bt_python_get_sequence_from_def(self._d)
1133 if seq is not None:
1134 definition_ptr = _bt_sequence_index(seq, index)
1135 if definition_ptr is not None:
1136 return _Definition(definition_ptr, self.scope)
1137 return None
1138
8bb27181 1139 def _get_uint64(self):
78d714e8
JG
1140 """
1141 Return the value associated with the field.
1142 If the field does not exist or is not of the type requested,
1143 the value returned is undefined. To check if an error occured,
1144 use the field_error() function after accessing a field.
1145 """
1146 return _bt_ctf_get_uint64(self._d)
1147
8bb27181 1148 def _get_int64(self):
78d714e8
JG
1149 """
1150 Return the value associated with the field.
1151 If the field does not exist or is not of the type requested,
1152 the value returned is undefined. To check if an error occured,
1153 use the field_error() function after accessing a field.
1154 """
1155 return _bt_ctf_get_int64(self._d)
1156
8bb27181 1157 def _get_char_array(self):
78d714e8
JG
1158 """
1159 Return the value associated with the field.
1160 If the field does not exist or is not of the type requested,
1161 the value returned is undefined. To check if an error occurred,
1162 use the field_error() function after accessing a field.
1163 """
1164 return _bt_ctf_get_char_array(self._d)
1165
8bb27181 1166 def _get_str(self):
78d714e8
JG
1167 """
1168 Return the value associated with the field.
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.
1172 """
1173 return _bt_ctf_get_string(self._d)
1174
8bb27181 1175 def _get_float(self):
78d714e8
JG
1176 """
1177 Return the value associated with the field.
1178 If the field does not exist or is not of the type requested,
1179 the value returned is undefined. To check if an error occurred,
1180 use the field_error() function after accessing a field.
1181 """
1182 return _bt_ctf_get_float(self._d)
1183
8bb27181 1184 def _get_variant(self):
78d714e8
JG
1185 """
1186 Return the variant's selected field.
1187 If the field does not exist or is not of the type requested,
1188 the value returned is undefined. To check if an error occurred,
1189 use the field_error() function after accessing a field.
1190 """
1191 return _bt_ctf_get_variant(self._d)
1192
8bb27181 1193 def _get_struct_field_count(self):
78d714e8
JG
1194 """
1195 Return the number of fields contained in the structure.
1196 If the field does not exist or is not of the type requested,
1197 the value returned is undefined.
1198 """
1199 return _bt_ctf_get_struct_field_count(self._d)
1200
8bb27181 1201 def _get_struct_field_at(self, i):
78d714e8
JG
1202 """
1203 Return the structure's field at position i.
1204 If the field does not exist or is not of the type requested,
1205 the value returned is undefined. To check if an error occurred,
1206 use the field_error() function after accessing a field.
1207 """
1208 return _bt_ctf_get_struct_field_index(self._d, i)
1209
1210 @property
1211 def value(self):
1212 """
1213 Return the value associated with the field according to its type.
1214 Return None on error.
1215 """
1216 id = self.type
1217 value = None
5792eb34 1218
78d714e8 1219 if id == CTFTypeId.STRING:
8bb27181 1220 value = self._get_str()
78d714e8 1221 elif id == CTFTypeId.ARRAY:
5792eb34
JG
1222 element_decl = self.declaration.element_declaration
1223 if ((element_decl.type == CTFTypeId.INTEGER
1224 and element_decl.length == 8)
1225 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1226 value = _bt_python_get_array_string(self._d)
1227 else:
1228 value = []
1229 for i in range(self.declaration.length):
1230 element = self._get_array_element_at(i)
1231 value.append(element.value)
78d714e8 1232 elif id == CTFTypeId.INTEGER:
8bb27181
JG
1233 if self.declaration.signedness == 0:
1234 value = self._get_uint64()
78d714e8 1235 else:
8bb27181 1236 value = self._get_int64()
78d714e8 1237 elif id == CTFTypeId.ENUM:
8bb27181 1238 value = self._get_enum_str()
78d714e8 1239 elif id == CTFTypeId.SEQUENCE:
3866c850
JG
1240 element_decl = self.declaration.element_declaration
1241 if ((element_decl.type == CTFTypeId.INTEGER
1242 and element_decl.length == 8)
1243 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1244 value = _bt_python_get_sequence_string(self._d)
1245 else:
1246 seq_len = self._get_sequence_len()
1247 value = []
1248 for i in range(seq_len):
1249 evDef = self._get_sequence_element_at(i)
1250 value.append(evDef.value)
78d714e8 1251 elif id == CTFTypeId.FLOAT:
8bb27181 1252 value = self._get_float()
78d714e8 1253 elif id == CTFTypeId.VARIANT:
8bb27181
JG
1254 variant = _Definition.__new__(_Definition)
1255 variant._d = self._get_variant();
78d714e8
JG
1256 value = variant.value
1257 elif id == CTFTypeId.STRUCT:
1258 value = {}
8bb27181
JG
1259 for i in range(self._get_struct_field_count()):
1260 member = _Definition(self._get_struct_field_at(i), self.scope)
78d714e8
JG
1261 value[member.name] = member.value
1262
1263 if field_error():
5792eb34 1264 raise FieldError("Error occurred while accessing field {} of type {}".format(self.name, CTFTypeId.type_name(id)))
78d714e8
JG
1265 return value
1266
1267 @property
1268 def scope(self):
1269 """Return the scope of a field or None on error."""
1270 return self._s
1271
24a3136a
DS
1272%}
1273
1274
ec8c88d7
JG
1275// =================================================================
1276// CTF Writer
1277// =================================================================
1278
1279/* =================================================================
1280 CLOCK.H
1281 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1282*/
1283%rename("_bt_ctf_clock_create") bt_ctf_clock_create(const char *name);
b0bb560c
JG
1284%rename("_bt_ctf_clock_get_name") bt_ctf_clock_get_name(struct bt_ctf_clock *clock);
1285%rename("_bt_ctf_clock_get_description") bt_ctf_clock_get_description(struct bt_ctf_clock *clock);
ec8c88d7 1286%rename("_bt_ctf_clock_set_description") bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc);
b0bb560c 1287%rename("_bt_ctf_clock_get_frequency") bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock);
ec8c88d7 1288%rename("_bt_ctf_clock_set_frequency") bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq);
b0bb560c 1289%rename("_bt_ctf_clock_get_precision") bt_ctf_clock_get_precision(struct bt_ctf_clock *clock);
ec8c88d7 1290%rename("_bt_ctf_clock_set_precision") bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision);
b0bb560c 1291%rename("_bt_ctf_clock_get_offset_s") bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock);
ec8c88d7 1292%rename("_bt_ctf_clock_set_offset_s") bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s);
b0bb560c 1293%rename("_bt_ctf_clock_get_offset") bt_ctf_clock_get_offset(struct bt_ctf_clock *clock);
ec8c88d7 1294%rename("_bt_ctf_clock_set_offset") bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset);
b0bb560c 1295%rename("_bt_ctf_clock_get_is_absolute") bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock);
ec8c88d7 1296%rename("_bt_ctf_clock_set_is_absolute") bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute);
b0bb560c 1297%rename("_bt_ctf_clock_get_time") bt_ctf_clock_get_time(struct bt_ctf_clock *clock);
ec8c88d7
JG
1298%rename("_bt_ctf_clock_set_time") bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time);
1299%rename("_bt_ctf_clock_get") bt_ctf_clock_get(struct bt_ctf_clock *clock);
1300%rename("_bt_ctf_clock_put") bt_ctf_clock_put(struct bt_ctf_clock *clock);
1301
1302struct bt_ctf_clock *bt_ctf_clock_create(const char *name);
b0bb560c
JG
1303const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock);
1304const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock);
ec8c88d7 1305int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc);
b0bb560c 1306uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock);
ec8c88d7 1307int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq);
b0bb560c 1308uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock);
ec8c88d7 1309int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision);
b0bb560c 1310uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock);
ec8c88d7 1311int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s);
b0bb560c 1312uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock);
ec8c88d7 1313int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset);
b0bb560c 1314int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock);
ec8c88d7 1315int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute);
b0bb560c 1316uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock);
ec8c88d7
JG
1317int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time);
1318void bt_ctf_clock_get(struct bt_ctf_clock *clock);
1319void bt_ctf_clock_put(struct bt_ctf_clock *clock);
1320
1321/* =================================================================
1322 EVENT-TYPES.H
1323 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1324*/
1325%rename("_bt_ctf_field_type_integer_create") bt_ctf_field_type_integer_create(unsigned int size);
b4b5e4b5
JG
1326%rename("_bt_ctf_field_type_integer_get_size") bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *integer);
1327%rename("_bt_ctf_field_type_integer_get_signed") bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *integer);
ec8c88d7 1328%rename("_bt_ctf_field_type_integer_set_signed") bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *integer, int is_signed);
b4b5e4b5 1329%rename("_bt_ctf_field_type_integer_get_base") bt_ctf_field_type_integer_get_base(struct bt_ctf_field_type *integer);
ec8c88d7 1330%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);
b4b5e4b5 1331%rename("_bt_ctf_field_type_integer_get_encoding") bt_ctf_field_type_integer_get_encoding(struct bt_ctf_field_type *integer);
ec8c88d7
JG
1332%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);
1333%rename("_bt_ctf_field_type_enumeration_create") bt_ctf_field_type_enumeration_create(struct bt_ctf_field_type *integer_container_type);
b4b5e4b5
JG
1334%rename("_bt_ctf_field_type_enumeration_get_container_type") bt_ctf_field_type_enumeration_get_container_type(struct bt_ctf_field_type *enumeration);
1335%rename("_bt_ctf_field_type_enumeration_add_mapping") bt_ctf_field_type_enumeration_add_mapping(struct bt_ctf_field_type *enumeration, const char *name, int64_t range_start, int64_t range_end);
1336%rename("_bt_ctf_field_type_enumeration_add_mapping_unsigned") bt_ctf_field_type_enumeration_add_mapping_unsigned(struct bt_ctf_field_type *enumeration, const char *name, uint64_t range_start, uint64_t range_end);
1337%rename("_bt_ctf_field_type_enumeration_get_mapping_count") bt_ctf_field_type_enumeration_get_mapping_count(struct bt_ctf_field_type *enumeration);
1338%rename("_bt_ctf_field_type_enumeration_get_mapping") bt_ctf_field_type_enumeration_get_mapping(struct bt_ctf_field_type *enumeration, size_t index, const char **name, int64_t *range_start, int64_t *range_end);
1339%rename("_bt_ctf_field_type_enumeration_get_mapping_unsigned") bt_ctf_field_type_enumeration_get_mapping_unsigned(struct bt_ctf_field_type *enumeration, size_t index, const char **name, uint64_t *range_start, uint64_t *range_end);
1340%rename("_bt_ctf_field_type_enumeration_get_mapping_index_by_name") bt_ctf_field_type_enumeration_get_mapping_index_by_name(struct bt_ctf_field_type *enumeration, const char *name, size_t *index);
1341%rename("_bt_ctf_field_type_enumeration_get_mapping_index_by_value") bt_ctf_field_type_enumeration_get_mapping_index_by_value(struct bt_ctf_field_type *enumeration, int64_t value, size_t *index);
1342%rename("_bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value") bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(struct bt_ctf_field_type *enumeration, uint64_t value, size_t *index);
ec8c88d7 1343%rename("_bt_ctf_field_type_floating_point_create") bt_ctf_field_type_floating_point_create(void);
b4b5e4b5 1344%rename("_bt_ctf_field_type_floating_point_get_exponent_digits") bt_ctf_field_type_floating_point_get_exponent_digits(struct bt_ctf_field_type *floating_point);
ec8c88d7 1345%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);
b4b5e4b5 1346%rename("_bt_ctf_field_type_floating_point_get_mantissa_digits") bt_ctf_field_type_floating_point_get_mantissa_digits(struct bt_ctf_field_type *floating_point);
ec8c88d7
JG
1347%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);
1348%rename("_bt_ctf_field_type_structure_create") bt_ctf_field_type_structure_create(void);
1349%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);
b4b5e4b5
JG
1350%rename("_bt_ctf_field_type_structure_get_field_count") bt_ctf_field_type_structure_get_field_count(struct bt_ctf_field_type *structure);
1351%rename("_bt_ctf_field_type_structure_get_field") bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *structure, const char **field_name, struct bt_ctf_field_type **field_type, size_t index);
1352%rename("_bt_ctf_field_type_structure_get_field_type_by_name") bt_ctf_field_type_structure_get_field_type_by_name(struct bt_ctf_field_type *structure, const char *field_name);
ec8c88d7 1353%rename("_bt_ctf_field_type_variant_create") bt_ctf_field_type_variant_create(struct bt_ctf_field_type *enum_tag, const char *tag_name);
b4b5e4b5
JG
1354%rename("_bt_ctf_field_type_variant_get_tag_type") bt_ctf_field_type_variant_get_tag_type(struct bt_ctf_field_type *variant);
1355%rename("_bt_ctf_field_type_variant_get_tag_name") bt_ctf_field_type_variant_get_tag_name(struct bt_ctf_field_type *variant);
ec8c88d7 1356%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);
b4b5e4b5
JG
1357%rename("_bt_ctf_field_type_variant_get_field_type_by_name") bt_ctf_field_type_variant_get_field_type_by_name(struct bt_ctf_field_type *variant, const char *field_name);
1358%rename("_bt_ctf_field_type_variant_get_field_type_from_tag") bt_ctf_field_type_variant_get_field_type_from_tag(struct bt_ctf_field_type *variant, struct bt_ctf_field *tag);
1359%rename("_bt_ctf_field_type_variant_get_field_count") bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *variant);
1360%rename("_bt_ctf_field_type_variant_get_field") bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *variant, const char **field_name, struct bt_ctf_field_type **field_type, size_t index);
ec8c88d7 1361%rename("_bt_ctf_field_type_array_create") bt_ctf_field_type_array_create(struct bt_ctf_field_type *element_type, unsigned int length);
b4b5e4b5
JG
1362%rename("_bt_ctf_field_type_array_get_element_type") bt_ctf_field_type_array_get_element_type(struct bt_ctf_field_type *array);
1363%rename("_bt_ctf_field_type_array_get_length") bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *array);
ec8c88d7 1364%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);
b4b5e4b5
JG
1365%rename("_bt_ctf_field_type_sequence_get_element_type") bt_ctf_field_type_sequence_get_element_type(struct bt_ctf_field_type *sequence);
1366%rename("_bt_ctf_field_type_sequence_get_length_field_name") bt_ctf_field_type_sequence_get_length_field_name(struct bt_ctf_field_type *sequence);
ec8c88d7 1367%rename("_bt_ctf_field_type_string_create") bt_ctf_field_type_string_create(void);
b4b5e4b5
JG
1368%rename("_bt_ctf_field_type_string_get_encoding") bt_ctf_field_type_string_get_encoding(struct bt_ctf_field_type *string_type);
1369%rename("_bt_ctf_field_type_string_set_encoding") bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *string_type, enum ctf_string_encoding encoding);
1370%rename("_bt_ctf_field_type_get_alignment") bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type);
ec8c88d7 1371%rename("_bt_ctf_field_type_set_alignment") bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment);
b4b5e4b5 1372%rename("_bt_ctf_field_type_get_byte_order") bt_ctf_field_type_get_byte_order(struct bt_ctf_field_type *type);
ec8c88d7 1373%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);
b4b5e4b5 1374%rename("_bt_ctf_field_type_get_type_id") bt_ctf_field_type_get_type_id(struct bt_ctf_field_type *type);
ec8c88d7
JG
1375%rename("_bt_ctf_field_type_get") bt_ctf_field_type_get(struct bt_ctf_field_type *type);
1376%rename("_bt_ctf_field_type_put") bt_ctf_field_type_put(struct bt_ctf_field_type *type);
1377
1378struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size);
b4b5e4b5
JG
1379int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *integer);
1380int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *integer);
ec8c88d7 1381int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *integer, int is_signed);
b4b5e4b5 1382enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base(struct bt_ctf_field_type *integer);
ec8c88d7 1383int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, enum bt_ctf_integer_base base);
b4b5e4b5 1384enum ctf_string_encoding bt_ctf_field_type_integer_get_encoding(struct bt_ctf_field_type *integer);
ec8c88d7
JG
1385int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *integer, enum ctf_string_encoding encoding);
1386struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(struct bt_ctf_field_type *integer_container_type);
b4b5e4b5
JG
1387struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type(struct bt_ctf_field_type *enumeration);
1388int bt_ctf_field_type_enumeration_add_mapping(struct bt_ctf_field_type *enumeration, const char *name, int64_t range_start, int64_t range_end);
1389int bt_ctf_field_type_enumeration_add_mapping_unsigned(struct bt_ctf_field_type *enumeration, const char *name, uint64_t range_start, uint64_t range_end);
1390int64_t bt_ctf_field_type_enumeration_get_mapping_count(struct bt_ctf_field_type *enumeration);
1391int bt_ctf_field_type_enumeration_get_mapping(struct bt_ctf_field_type *enumeration, size_t index, const char **OUTPUT, int64_t *OUTPUT, int64_t *OUTPUT);
1392int bt_ctf_field_type_enumeration_get_mapping_unsigned(struct bt_ctf_field_type *enumeration, size_t index, const char **OUTPUT, uint64_t *OUTPUT, uint64_t *OUTPUT);
1393int bt_ctf_field_type_enumeration_get_mapping_index_by_name(struct bt_ctf_field_type *enumeration, const char *name, size_t *OUTPUT);
1394int bt_ctf_field_type_enumeration_get_mapping_index_by_value(struct bt_ctf_field_type *enumeration, int64_t value, size_t *OUTPUT);
1395int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(struct bt_ctf_field_type *enumeration, uint64_t value, size_t *OUTPUT);
ec8c88d7 1396struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void);
b4b5e4b5 1397int bt_ctf_field_type_floating_point_get_exponent_digits(struct bt_ctf_field_type *floating_point);
ec8c88d7 1398int bt_ctf_field_type_floating_point_set_exponent_digits(struct bt_ctf_field_type *floating_point, unsigned int exponent_digits);
b4b5e4b5 1399int bt_ctf_field_type_floating_point_get_mantissa_digits(struct bt_ctf_field_type *floating_point);
ec8c88d7
JG
1400int bt_ctf_field_type_floating_point_set_mantissa_digits(struct bt_ctf_field_type *floating_point, unsigned int mantissa_digits);
1401struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void);
1402int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *structure, struct bt_ctf_field_type *field_type, const char *field_name);
b4b5e4b5
JG
1403int64_t bt_ctf_field_type_structure_get_field_count(struct bt_ctf_field_type *structure);
1404int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *structure, const char **OUTPUT, struct bt_ctf_field_type **OUTPUT, size_t index);
1405struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name(struct bt_ctf_field_type *structure, const char *field_name);
ec8c88d7 1406struct bt_ctf_field_type *bt_ctf_field_type_variant_create(struct bt_ctf_field_type *enum_tag, const char *tag_name);
b4b5e4b5
JG
1407struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type(struct bt_ctf_field_type *variant);
1408const char *bt_ctf_field_type_variant_get_tag_name(struct bt_ctf_field_type *variant);
ec8c88d7 1409int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *variant, struct bt_ctf_field_type *field_type, const char *field_name);
b4b5e4b5
JG
1410struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name(struct bt_ctf_field_type *variant, const char *field_name);
1411struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(struct bt_ctf_field_type *variant, struct bt_ctf_field *tag);
1412int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *variant);
1413int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *variant, const char **OUTPUT, struct bt_ctf_field_type **OUTPUT, size_t index);
ec8c88d7 1414struct bt_ctf_field_type *bt_ctf_field_type_array_create(struct bt_ctf_field_type *element_type, unsigned int length);
b4b5e4b5
JG
1415struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(struct bt_ctf_field_type *array);
1416int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *array);
ec8c88d7 1417struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(struct bt_ctf_field_type *element_type, const char *length_field_name);
b4b5e4b5
JG
1418struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(struct bt_ctf_field_type *sequence);
1419const char *bt_ctf_field_type_sequence_get_length_field_name(struct bt_ctf_field_type *sequence);
ec8c88d7 1420struct bt_ctf_field_type *bt_ctf_field_type_string_create(void);
b4b5e4b5
JG
1421enum ctf_string_encoding bt_ctf_field_type_string_get_encoding(struct bt_ctf_field_type *string_type);
1422int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *string_type, enum ctf_string_encoding encoding);
1423int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type);
ec8c88d7 1424int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment);
b4b5e4b5 1425enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order(struct bt_ctf_field_type *type);
ec8c88d7 1426int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order);
b4b5e4b5 1427enum ctf_type_id bt_ctf_field_type_get_type_id(struct bt_ctf_field_type *type);
ec8c88d7
JG
1428void bt_ctf_field_type_get(struct bt_ctf_field_type *type);
1429void bt_ctf_field_type_put(struct bt_ctf_field_type *type);
1430
1431/* =================================================================
1432 EVENT-FIELDS.H
1433 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1434*/
1435%rename("_bt_ctf_field_create") bt_ctf_field_create(struct bt_ctf_field_type *type);
1436%rename("_bt_ctf_field_structure_get_field") bt_ctf_field_structure_get_field(struct bt_ctf_field *structure, const char *name);
1437%rename("_bt_ctf_field_array_get_field") bt_ctf_field_array_get_field(struct bt_ctf_field *array, uint64_t index);
821ca76c 1438%rename("_bt_ctf_field_sequence_get_length") bt_ctf_field_sequence_get_length(struct bt_ctf_field *sequence);
ec8c88d7
JG
1439%rename("_bt_ctf_field_sequence_set_length") bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, struct bt_ctf_field *length_field);
1440%rename("_bt_ctf_field_sequence_get_field") bt_ctf_field_sequence_get_field(struct bt_ctf_field *sequence, uint64_t index);
1441%rename("_bt_ctf_field_variant_get_field") bt_ctf_field_variant_get_field(struct bt_ctf_field *variant, struct bt_ctf_field *tag);
1442%rename("_bt_ctf_field_enumeration_get_container") bt_ctf_field_enumeration_get_container(struct bt_ctf_field *enumeration);
821ca76c
JG
1443%rename("_bt_ctf_field_enumeration_get_mapping_name") bt_ctf_field_enumeration_get_mapping_name(struct bt_ctf_field *enumeration);
1444%rename("_bt_ctf_field_signed_integer_get_value") bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *integer, int64_t *value);
ec8c88d7 1445%rename("_bt_ctf_field_signed_integer_set_value") bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, int64_t value);
821ca76c 1446%rename("_bt_ctf_field_unsigned_integer_get_value") bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *integer, uint64_t *value);
ec8c88d7 1447%rename("_bt_ctf_field_unsigned_integer_set_value") bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, uint64_t value);
821ca76c 1448%rename("_bt_ctf_field_floating_point_get_value") bt_ctf_field_floating_point_get_value(struct bt_ctf_field *floating_point, double *value);
ec8c88d7 1449%rename("_bt_ctf_field_floating_point_set_value") bt_ctf_field_floating_point_set_value(struct bt_ctf_field *floating_point, double value);
b4b5e4b5
JG
1450%rename("_bt_ctf_field_string_get_value") bt_ctf_field_string_get_value(struct bt_ctf_field *string_field);
1451%rename("_bt_ctf_field_string_set_value") bt_ctf_field_string_set_value(struct bt_ctf_field *string_field, const char *value);
821ca76c 1452%rename("_bt_ctf_field_get_type") bt_ctf_field_get_type(struct bt_ctf_field *field);
ec8c88d7
JG
1453%rename("_bt_ctf_field_get") bt_ctf_field_get(struct bt_ctf_field *field);
1454%rename("_bt_ctf_field_put") bt_ctf_field_put(struct bt_ctf_field *field);
1455
1456struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type);
1457struct bt_ctf_field *bt_ctf_field_structure_get_field(struct bt_ctf_field *structure, const char *name);
1458struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *array, uint64_t index);
821ca76c 1459struct bt_ctf_field * bt_ctf_field_sequence_get_length(struct bt_ctf_field *sequence);
ec8c88d7
JG
1460int bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, struct bt_ctf_field *length_field);
1461struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *sequence, uint64_t index);
1462struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *variant, struct bt_ctf_field *tag);
1463struct bt_ctf_field *bt_ctf_field_enumeration_get_container(struct bt_ctf_field *enumeration);
821ca76c
JG
1464const char *bt_ctf_field_enumeration_get_mapping_name(struct bt_ctf_field *enumeration);
1465int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *integer, int64_t *OUTPUT);
ec8c88d7 1466int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, int64_t value);
821ca76c 1467int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *integer, uint64_t *OUTPUT);
ec8c88d7 1468int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, uint64_t value);
821ca76c 1469int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *floating_point, double *OUTPUT);
ec8c88d7 1470int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *floating_point, double value);
b4b5e4b5
JG
1471const char *bt_ctf_field_string_get_value(struct bt_ctf_field *string_field);
1472int bt_ctf_field_string_set_value(struct bt_ctf_field *string_field, const char *value);
821ca76c 1473struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field);
ec8c88d7
JG
1474void bt_ctf_field_get(struct bt_ctf_field *field);
1475void bt_ctf_field_put(struct bt_ctf_field *field);
1476
1477/* =================================================================
1478 EVENT.H
1479 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1480*/
1481%rename("_bt_ctf_event_class_create") bt_ctf_event_class_create(const char *name);
38f28df6
JG
1482%rename("_bt_ctf_event_class_get_name") bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class);
1483%rename("_bt_ctf_event_class_get_id") bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class);
1484%rename("_bt_ctf_event_class_set_id") bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, uint32_t id);
1485%rename("_bt_ctf_event_class_get_stream_class") bt_ctf_event_class_get_stream_class(struct bt_ctf_event_class *event_class);
ec8c88d7 1486%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);
38f28df6
JG
1487%rename("_bt_ctf_event_class_get_field_count") bt_ctf_event_class_get_field_count(struct bt_ctf_event_class *event_class);
1488%rename("_bt_ctf_event_class_get_field") bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, const char **field_name, struct bt_ctf_field_type **field_type, size_t index);
1489%rename("_bt_ctf_event_class_get_field_by_name") bt_ctf_event_class_get_field_by_name(struct bt_ctf_event_class *event_class, const char *name);
ec8c88d7
JG
1490%rename("_bt_ctf_event_class_get") bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
1491%rename("_bt_ctf_event_class_put") bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
1492%rename("_bt_ctf_event_create") bt_ctf_event_create(struct bt_ctf_event_class *event_class);
38f28df6
JG
1493%rename("_bt_ctf_event_get_class") bt_ctf_event_get_class(struct bt_ctf_event *event);
1494%rename("_bt_ctf_event_get_clock") bt_ctf_event_get_clock(struct bt_ctf_event *event);
ec8c88d7 1495%rename("_bt_ctf_event_get_payload") bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name);
38f28df6
JG
1496%rename("_bt_ctf_event_set_payload") bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
1497%rename("_bt_ctf_event_get_payload_by_index") bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, size_t index);
ec8c88d7
JG
1498%rename("_bt_ctf_event_get") bt_ctf_event_get(struct bt_ctf_event *event);
1499%rename("_bt_ctf_event_put") bt_ctf_event_put(struct bt_ctf_event *event);
1500
1501struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name);
38f28df6
JG
1502const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class);
1503int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class);
1504int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, uint32_t id);
1505struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(struct bt_ctf_event_class *event_class);
ec8c88d7 1506int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name);
38f28df6
JG
1507int64_t bt_ctf_event_class_get_field_count(struct bt_ctf_event_class *event_class);
1508int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, const char **field_name, struct bt_ctf_field_type **field_type, size_t index);
1509struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(struct bt_ctf_event_class *event_class, const char *name);
ec8c88d7
JG
1510void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
1511void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
1512struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class);
38f28df6
JG
1513struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event);
1514struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event);
ec8c88d7 1515struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name);
38f28df6
JG
1516int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
1517struct bt_ctf_field *bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, size_t index);
ec8c88d7
JG
1518void bt_ctf_event_get(struct bt_ctf_event *event);
1519void bt_ctf_event_put(struct bt_ctf_event *event);
1520
1521/* =================================================================
1522 STREAM.H
1523 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1524*/
1525%rename("_bt_ctf_stream_class_create") bt_ctf_stream_class_create(const char *name);
f6c2aa83
JG
1526%rename("_bt_ctf_stream_class_get_name") bt_ctf_stream_class_get_name(struct bt_ctf_stream_class *stream_class);
1527%rename("_bt_ctf_stream_class_get_clock") bt_ctf_stream_class_get_clock(struct bt_ctf_stream_class *stream_class);
ec8c88d7 1528%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);
f6c2aa83
JG
1529%rename("_bt_ctf_stream_class_get_id") bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class);
1530%rename("_bt_ctf_stream_class_set_id") bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, uint32_t id);
ec8c88d7 1531%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);
f6c2aa83
JG
1532%rename("_bt_ctf_stream_class_get_event_class_count") bt_ctf_stream_class_get_event_class_count(struct bt_ctf_stream_class *stream_class);
1533%rename("_bt_ctf_stream_class_get_event_class") bt_ctf_stream_class_get_event_class(struct bt_ctf_stream_class *stream_class, size_t index);
1534%rename("_bt_ctf_stream_class_get_event_class_by_name") bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
ec8c88d7
JG
1535%rename("_bt_ctf_stream_class_get") bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
1536%rename("_bt_ctf_stream_class_put") bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
708e628a 1537%rename("_bt_ctf_stream_get_discarded_events_count") bt_ctf_stream_get_discarded_events_count(struct bt_ctf_stream *stream, uint64_t *count);
ec8c88d7
JG
1538%rename("_bt_ctf_stream_append_discarded_events") bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count);
1539%rename("_bt_ctf_stream_append_event") bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event);
1540%rename("_bt_ctf_stream_flush") bt_ctf_stream_flush(struct bt_ctf_stream *stream);
1541%rename("_bt_ctf_stream_get") bt_ctf_stream_get(struct bt_ctf_stream *stream);
1542%rename("_bt_ctf_stream_put") bt_ctf_stream_put(struct bt_ctf_stream *stream);
1543
1544struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name);
f6c2aa83
JG
1545const char *bt_ctf_stream_class_get_name(struct bt_ctf_stream_class *stream_class);
1546struct bt_ctf_clock *bt_ctf_stream_class_get_clock(struct bt_ctf_stream_class *stream_class);
ec8c88d7 1547int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock);
f6c2aa83
JG
1548int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class);
1549int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, uint32_t id);
ec8c88d7 1550int bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class);
f6c2aa83
JG
1551int64_t bt_ctf_stream_class_get_event_class_count(struct bt_ctf_stream_class *stream_class);
1552struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(struct bt_ctf_stream_class *stream_class, size_t index);
1553struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
ec8c88d7
JG
1554void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
1555void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
708e628a 1556int bt_ctf_stream_get_discarded_events_count(struct bt_ctf_stream *stream, uint64_t *OUTPUT);
ec8c88d7
JG
1557void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count);
1558int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event);
1559int bt_ctf_stream_flush(struct bt_ctf_stream *stream);
1560void bt_ctf_stream_get(struct bt_ctf_stream *stream);
1561void bt_ctf_stream_put(struct bt_ctf_stream *stream);
1562
1563/* =================================================================
1564 WRITER.H
1565 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1566*/
1567%rename("_bt_ctf_writer_create") bt_ctf_writer_create(const char *path);
1568%rename("_bt_ctf_writer_create_stream") bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class);
1569%rename("_bt_ctf_writer_add_environment_field") bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value);
1570%rename("_bt_ctf_writer_add_clock") bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock);
1571%newobject bt_ctf_writer_get_metadata_string;
1572%rename("_bt_ctf_writer_get_metadata_string") bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
1573%rename("_bt_ctf_writer_flush_metadata") bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
1574%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);
1575%rename("_bt_ctf_writer_get") bt_ctf_writer_get(struct bt_ctf_writer *writer);
1576%rename("_bt_ctf_writer_put") bt_ctf_writer_put(struct bt_ctf_writer *writer);
1577
1578struct bt_ctf_writer *bt_ctf_writer_create(const char *path);
1579struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class);
1580int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value);
1581int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock);
1582char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
1583void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
1584int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, enum bt_ctf_byte_order byte_order);
1585void bt_ctf_writer_get(struct bt_ctf_writer *writer);
1586void bt_ctf_writer_put(struct bt_ctf_writer *writer);
1587
1588%pythoncode %{
1589
1590class CTFWriter:
b0bb560c
JG
1591 # Used to compare to -1ULL in error checks
1592 _MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
ec8c88d7 1593
b4b5e4b5
JG
1594 """
1595 Enumeration mapping class. start and end values are inclusive.
1596 """
1597 class EnumerationMapping:
1598 def __init__(self, name, start, end):
1599 self.name = name
1600 self.start = start
1601 self.end = end
1602
ec8c88d7
JG
1603 class Clock:
1604 def __init__(self, name):
1605 self._c = _bt_ctf_clock_create(name)
1606 if self._c is None:
1607 raise ValueError("Invalid clock name.")
1608
1609 def __del__(self):
1610 _bt_ctf_clock_put(self._c)
1611
14001277 1612 """
b0bb560c
JG
1613 Get the clock's name.
1614 """
1615 @property
1616 def name(self):
1617 name = _bt_ctf_clock_get_name(self._c)
1618 if name is None:
1619 raise ValueError("Invalid clock instance.")
1620 return name
1621
1622 """
1623 Get the clock's description. None if unset.
14001277
JG
1624 """
1625 @property
1626 def description(self):
b0bb560c 1627 return _bt_ctf_clock_get_description(self._c)
14001277 1628
ec8c88d7
JG
1629 """
1630 Set the clock's description. The description appears in the clock's TSDL
1631 meta-data.
1632 """
14001277
JG
1633 @description.setter
1634 def description(self, desc):
1635 ret = _bt_ctf_clock_set_description(self._c, str(desc))
ec8c88d7
JG
1636 if ret < 0:
1637 raise ValueError("Invalid clock description.")
1638
14001277
JG
1639 """
1640 Get the clock's frequency (Hz).
1641 """
1642 @property
1643 def frequency(self):
b0bb560c
JG
1644 freq = _bt_ctf_clock_get_frequency(self._c)
1645 if freq == CTFWriter._MAX_UINT64:
1646 raise ValueError("Invalid clock instance")
1647 return freq
14001277 1648
ec8c88d7
JG
1649 """
1650 Set the clock's frequency (Hz).
1651 """
14001277
JG
1652 @frequency.setter
1653 def frequency(self, freq):
ec8c88d7
JG
1654 ret = _bt_ctf_clock_set_frequency(self._c, freq)
1655 if ret < 0:
1656 raise ValueError("Invalid frequency value.")
1657
14001277
JG
1658 """
1659 Get the clock's precision (in clock ticks).
1660 """
1661 @property
1662 def precision(self):
b0bb560c
JG
1663 precision = _bt_ctf_clock_get_precision(self._c)
1664 if precision == CTFWriter._MAX_UINT64:
1665 raise ValueError("Invalid clock instance")
1666 return precision
14001277 1667
ec8c88d7
JG
1668 """
1669 Set the clock's precision (in clock ticks).
1670 """
14001277
JG
1671 @precision.setter
1672 def precision(self, precision):
ec8c88d7
JG
1673 ret = _bt_ctf_clock_set_precision(self._c, precision)
1674
14001277
JG
1675 """
1676 Get the clock's offset in seconds from POSIX.1 Epoch.
1677 """
1678 @property
1679 def offset_seconds(self):
b0bb560c
JG
1680 offset_s = _bt_ctf_clock_get_offset_s(self._c)
1681 if offset_s == CTFWriter._MAX_UINT64:
1682 raise ValueError("Invalid clock instance")
1683 return offset_s
14001277 1684
ec8c88d7
JG
1685 """
1686 Set the clock's offset in seconds from POSIX.1 Epoch.
1687 """
14001277
JG
1688 @offset_seconds.setter
1689 def offset_seconds(self, offset_s):
ec8c88d7
JG
1690 ret = _bt_ctf_clock_set_offset_s(self._c, offset_s)
1691 if ret < 0:
1692 raise ValueError("Invalid offset value.")
1693
14001277
JG
1694 """
1695 Get the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1696 """
1697 @property
1698 def offset(self):
b0bb560c
JG
1699 offset = _bt_ctf_clock_get_offset(self._c)
1700 if offset == CTFWriter._MAX_UINT64:
1701 raise ValueError("Invalid clock instance")
1702 return offset
14001277 1703
ec8c88d7
JG
1704 """
1705 Set the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1706 """
14001277
JG
1707 @offset.setter
1708 def offset(self, offset):
ec8c88d7
JG
1709 ret = _bt_ctf_clock_set_offset(self._c, offset)
1710 if ret < 0:
1711 raise ValueError("Invalid offset value.")
1712
14001277
JG
1713 """
1714 Get a clock's absolute attribute. A clock is absolute if the clock
1715 is a global reference across the trace's other clocks.
1716 """
1717 @property
1718 def absolute(self):
b0bb560c
JG
1719 is_absolute = _bt_ctf_clock_get_is_absolute(self._c)
1720 if is_absolute == -1:
1721 raise ValueError("Invalid clock instance")
1722 return False if is_absolute == 0 else True
14001277 1723
ec8c88d7
JG
1724 """
1725 Set a clock's absolute attribute. A clock is absolute if the clock
1726 is a global reference across the trace's other clocks.
1727 """
14001277
JG
1728 @absolute.setter
1729 def absolute(self, is_absolute):
1730 ret = _bt_ctf_clock_set_is_absolute(self._c, int(is_absolute))
ec8c88d7
JG
1731 if ret < 0:
1732 raise ValueError("Could not set the clock's absolute attribute.")
1733
14001277
JG
1734 """
1735 Get the current time in nanoseconds since the clock's origin (offset and
1736 offset_s attributes).
1737 """
1738 @property
1739 def time(self):
b0bb560c
JG
1740 time = _bt_ctf_clock_get_time(self._c)
1741 if time == CTFWriter._MAX_UINT64:
1742 raise ValueError("Invalid clock instance")
1743 return time
14001277 1744
ec8c88d7
JG
1745 """
1746 Set the current time in nanoseconds since the clock's origin (offset and
1747 offset_s attributes). The clock's value will be sampled as events are
1748 appended to a stream.
1749 """
14001277
JG
1750 @time.setter
1751 def time(self, time):
ec8c88d7
JG
1752 ret = _bt_ctf_clock_set_time(self._c, time)
1753 if ret < 0:
1754 raise ValueError("Invalid time value.")
1755
14001277 1756 class FieldDeclaration:
ec8c88d7 1757 """
b4b5e4b5 1758 FieldDeclaration should not be instantiated directly. Instantiate
14001277 1759 one of the concrete FieldDeclaration classes.
ec8c88d7
JG
1760 """
1761 class IntegerBase:
1762 # These values are based on the bt_ctf_integer_base enum
1763 # declared in event-types.h.
1764 INTEGER_BASE_UNKNOWN = -1
1765 INTEGER_BASE_BINARY = 2
1766 INTEGER_BASE_OCTAL = 8
1767 INTEGER_BASE_DECIMAL = 10
1768 INTEGER_BASE_HEXADECIMAL = 16
1769
1770 def __init__(self):
1771 if self._ft is None:
14001277 1772 raise ValueError("FieldDeclaration creation failed.")
ec8c88d7
JG
1773
1774 def __del__(self):
1775 _bt_ctf_field_type_put(self._ft)
1776
b4b5e4b5
JG
1777 @staticmethod
1778 def _create_field_declaration_from_native_instance(native_field_declaration):
1779 type_dict = {
1780 CTFTypeId.INTEGER : CTFWriter.IntegerFieldDeclaration,
1781 CTFTypeId.FLOAT : CTFWriter.FloatFieldDeclaration,
1782 CTFTypeId.ENUM : CTFWriter.EnumerationFieldDeclaration,
1783 CTFTypeId.STRING : CTFWriter.StringFieldDeclaration,
1784 CTFTypeId.STRUCT : CTFWriter.StructureFieldDeclaration,
1785 CTFTypeId.VARIANT : CTFWriter.VariantFieldDeclaration,
1786 CTFTypeId.ARRAY : CTFWriter.ArrayFieldDeclaration,
1787 CTFTypeId.SEQUENCE : CTFWriter.SequenceFieldDeclaration
1788 }
1789
1790 field_type_id = _bt_ctf_field_type_get_type_id(native_field_declaration)
1791 if field_type_id == CTFTypeId.UNKNOWN:
1792 raise TypeError("Invalid field instance")
1793
1794 declaration = CTFWriter.Field.__new__(CTFWriter.Field)
1795 declaration._ft = native_field_declaration
1796 declaration.__class__ = type_dict[field_type_id]
1797 return declaration
1798
14001277 1799 """
b4b5e4b5 1800 Get the field declaration's alignment. Returns -1 on error.
14001277
JG
1801 """
1802 @property
1803 def alignment(self):
b4b5e4b5 1804 return _bt_ctf_field_type_get_alignment(self._ft)
14001277 1805
ec8c88d7 1806 """
b4b5e4b5 1807 Set the field declaration's alignment. Defaults to 1 (bit-aligned). However,
ec8c88d7
JG
1808 some types, such as structures and string, may impose other alignment
1809 constraints.
1810 """
14001277
JG
1811 @alignment.setter
1812 def alignment(self, alignment):
ec8c88d7
JG
1813 ret = _bt_ctf_field_type_set_alignment(self._ft, alignment)
1814 if ret < 0:
1815 raise ValueError("Invalid alignment value.")
1816
14001277 1817 """
b4b5e4b5 1818 Get the field declaration's byte order. One of the ByteOrder's constant.
14001277
JG
1819 """
1820 @property
1821 def byte_order(self):
b4b5e4b5 1822 return _bt_ctf_field_type_get_byte_order(self._ft)
14001277 1823
ec8c88d7 1824 """
b4b5e4b5 1825 Set the field declaration's byte order. Use constants defined in the ByteOrder
ec8c88d7
JG
1826 class.
1827 """
14001277
JG
1828 @byte_order.setter
1829 def byte_order(self, byte_order):
ec8c88d7
JG
1830 ret = _bt_ctf_field_type_set_byte_order(self._ft, byte_order)
1831 if ret < 0:
1832 raise ValueError("Could not set byte order value.")
1833
14001277 1834 class IntegerFieldDeclaration(FieldDeclaration):
ec8c88d7 1835 """
b4b5e4b5 1836 Create a new integer field declaration of the given size.
ec8c88d7
JG
1837 """
1838 def __init__(self, size):
1839 self._ft = _bt_ctf_field_type_integer_create(size)
1840 super().__init__()
1841
b4b5e4b5
JG
1842 """
1843 Get an integer's size.
1844 """
1845 @property
1846 def size(self):
1847 ret = _bt_ctf_field_type_integer_get_size(self._ft)
1848 if ret < 0:
1849 raise ValueError("Could not get Integer's size attribute.")
1850 else:
1851 return ret
1852
ec8c88d7 1853 """
14001277
JG
1854 Get an integer's signedness attribute.
1855 """
1856 @property
1857 def signed(self):
b4b5e4b5
JG
1858 ret = _bt_ctf_field_type_integer_get_signed(self._ft)
1859 if ret < 0:
1860 raise ValueError("Could not get Integer's signed attribute.")
1861 elif ret > 0:
1862 return True
1863 else:
1864 return False
14001277
JG
1865
1866 """
1867 Set an integer's signedness attribute.
ec8c88d7 1868 """
14001277
JG
1869 @signed.setter
1870 def signed(self, signed):
ec8c88d7
JG
1871 ret = _bt_ctf_field_type_integer_set_signed(self._ft, signed)
1872 if ret < 0:
b4b5e4b5 1873 raise ValueError("Could not set Integer's signed attribute.")
ec8c88d7
JG
1874
1875 """
14001277 1876 Get the integer's base used to pretty-print the resulting trace.
b4b5e4b5 1877 Returns a constant from the FieldDeclaration.IntegerBase class.
14001277
JG
1878 """
1879 @property
1880 def base(self):
b4b5e4b5 1881 return _bt_ctf_field_type_integer_get_base(self._ft)
14001277
JG
1882
1883 """
1884 Set the integer's base used to pretty-print the resulting trace.
b4b5e4b5 1885 The base must be a constant of the FieldDeclarationIntegerBase class.
ec8c88d7 1886 """
14001277
JG
1887 @base.setter
1888 def base(self, base):
ec8c88d7
JG
1889 ret = _bt_ctf_field_type_integer_set_base(self._ft, base)
1890 if ret < 0:
b4b5e4b5 1891 raise ValueError("Could not set Integer's base.")
ec8c88d7 1892
14001277
JG
1893 """
1894 Get the integer's encoding (one of the constants of the
1895 CTFStringEncoding class).
b4b5e4b5 1896 Returns a constant from the CTFStringEncoding class.
14001277
JG
1897 """
1898 @property
1899 def encoding(self):
b4b5e4b5 1900 return _bt_ctf_field_type_integer_get_encoding(self._ft)
14001277 1901
ec8c88d7
JG
1902 """
1903 An integer encoding may be set to signal that the integer must be printed
1904 as a text character. Must be a constant from the CTFStringEncoding class.
1905 """
14001277
JG
1906 @encoding.setter
1907 def encoding(self, encoding):
ec8c88d7
JG
1908 ret = _bt_ctf_field_type_integer_set_encoding(self._ft, encoding)
1909 if ret < 0:
b4b5e4b5 1910 raise ValueError("Could not set Integer's encoding.")
ec8c88d7 1911
14001277 1912 class EnumerationFieldDeclaration(FieldDeclaration):
ec8c88d7 1913 """
b4b5e4b5 1914 Create a new enumeration field declaration with the given underlying container type.
ec8c88d7
JG
1915 """
1916 def __init__(self, integer_type):
14001277 1917 if integer_type is None or not isinstance(integer_type, CTFWriter.IntegerFieldDeclaration):
ec8c88d7
JG
1918 raise TypeError("Invalid integer container.")
1919
1920 self._ft = _bt_ctf_field_type_enumeration_create(integer_type._ft)
1921 super().__init__()
1922
b4b5e4b5
JG
1923 """
1924 Get the enumeration's underlying container type.
1925 """
1926 @property
1927 def container(self):
1928 ret = _bt_ctf_field_type_enumeration_get_container_type(self._ft)
1929 if ret is None:
1930 raise TypeError("Invalid enumeration declaration")
1931 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
1932
ec8c88d7
JG
1933 """
1934 Add a mapping to the enumeration. The range's values are inclusive.
1935 """
1936 def add_mapping(self, name, range_start, range_end):
b4b5e4b5
JG
1937 if range_start < 0 or range_end < 0:
1938 ret = _bt_ctf_field_type_enumeration_add_mapping(self._ft, str(name), range_start, range_end)
1939 else:
1940 ret = _bt_ctf_field_type_enumeration_add_mapping_unsigned(self._ft, str(name), range_start, range_end)
1941
ec8c88d7 1942 if ret < 0:
b4b5e4b5
JG
1943 raise ValueError("Could not add mapping to enumeration declaration.")
1944
1945 """
1946 Generator returning instances of EnumerationMapping.
1947 """
1948 @property
1949 def mappings(self):
1950 signed = self.container.signed
1951
1952 count = _bt_ctf_field_type_enumeration_get_mapping_count(self._ft)
1953 for i in range(count):
1954 if signed:
1955 ret = _bt_python_ctf_field_type_enumeration_get_mapping(self._ft, i)
1956 else:
1957 ret = _bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, i)
1958
1959 if len(ret) != 3:
1960 raise TypeError("Could not get Enumeration mapping at index {}".format(i))
1961 name, range_start, range_end = ret
1962 yield CTFWriter.EnumerationMapping(name, range_start, range_end)
1963
1964 """
1965 Get a mapping by name (EnumerationMapping).
1966 """
1967 def get_mapping_by_name(self, name):
1968 ret, index = _bt_ctf_field_type_enumeration_get_mapping_index_by_name(self._ft, name);
1969 if ret < 0:
1970 return None
1971
1972 if self.container.signed:
1973 ret = _bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index)
1974 else:
1975 ret = _bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index)
1976
1977 if len(ret) != 3:
1978 raise TypeError("Could not get Enumeration mapping at index {}".format(i))
1979 name, range_start, range_end = ret
1980 return CTFWriter.EnumerationMapping(name, range_start, range_end)
1981
1982 """
1983 Get a mapping by value (EnumerationMapping).
1984 """
1985 def get_mapping_by_value(self, value):
1986 if value < 0:
1987 ret, index = _bt_ctf_field_type_enumeration_get_mapping_index_by_value(self._ft, value);
1988 else:
1989 ret, index = _bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(self._ft, value);
1990
1991 if ret < 0:
1992 return None
1993
1994 if self.container.signed:
1995 ret = _bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index)
1996 else:
1997 ret = _bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index)
1998
1999 if len(ret) != 3:
2000 raise TypeError("Could not get Enumeration mapping at index {}".format(i))
2001 name, range_start, range_end = ret
2002 return CTFWriter.EnumerationMapping(name, range_start, range_end)
ec8c88d7 2003
14001277 2004 class FloatFieldDeclaration(FieldDeclaration):
ec8c88d7
JG
2005 FLT_EXP_DIG = 8
2006 DBL_EXP_DIG = 11
2007 FLT_MANT_DIG = 24
2008 DBL_MANT_DIG = 53
2009
2010 """
b4b5e4b5 2011 Create a new floating point field declaration.
ec8c88d7
JG
2012 """
2013 def __init__(self):
2014 self._ft = _bt_ctf_field_type_floating_point_create()
2015 super().__init__()
2016
2017 """
b4b5e4b5 2018 Get the number of exponent digits used to store the floating point field.
14001277
JG
2019 """
2020 @property
2021 def exponent_digits(self):
b4b5e4b5
JG
2022 ret = _bt_ctf_field_type_floating_point_get_exponent_digits(self._ft)
2023 if ret < 0:
2024 raise TypeError("Could not get Floating point exponent digit count")
2025 return ret
14001277
JG
2026
2027 """
2028 Set the number of exponent digits to use to store the floating point field.
ec8c88d7
JG
2029 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
2030 are defined as constants of this class.
2031 """
14001277
JG
2032 @exponent_digits.setter
2033 def exponent_digits(self, exponent_digits):
ec8c88d7
JG
2034 ret = _bt_ctf_field_type_floating_point_set_exponent_digits(self._ft, exponent_digits)
2035 if ret < 0:
2036 raise ValueError("Could not set exponent digit count.")
2037
2038 """
b4b5e4b5 2039 Get the number of mantissa digits used to store the floating point field.
14001277
JG
2040 """
2041 @property
2042 def mantissa_digits(self):
b4b5e4b5
JG
2043 ret = _bt_ctf_field_type_floating_point_get_mantissa_digits(self._ft)
2044 if ret < 0:
2045 raise TypeError("Could not get Floating point mantissa digit count")
2046 return ret
14001277
JG
2047
2048 """
2049 Set the number of mantissa digits to use to store the floating point field.
ec8c88d7
JG
2050 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
2051 are defined as constants of this class.
2052 """
14001277
JG
2053 @mantissa_digits.setter
2054 def mantissa_digits(self, mantissa_digits):
ec8c88d7
JG
2055 ret = _bt_ctf_field_type_floating_point_set_mantissa_digits(self._ft, mantissa_digits)
2056 if ret < 0:
2057 raise ValueError("Could not set mantissa digit count.")
2058
14001277 2059 class StructureFieldDeclaration(FieldDeclaration):
ec8c88d7 2060 """
b4b5e4b5 2061 Create a new structure field declaration.
ec8c88d7
JG
2062 """
2063 def __init__(self):
2064 self._ft = _bt_ctf_field_type_structure_create()
2065 super().__init__()
2066
2067 """
2068 Add a field of type "field_type" to the structure.
2069 """
2070 def add_field(self, field_type, field_name):
14001277 2071 ret = _bt_ctf_field_type_structure_add_field(self._ft, field_type._ft, str(field_name))
ec8c88d7
JG
2072 if ret < 0:
2073 raise ValueError("Could not add field to structure.")
2074
b4b5e4b5
JG
2075 """
2076 Generator returning the structure's field as tuples of (field name, field declaration).
2077 """
2078 @property
2079 def fields(self):
2080 count = _bt_ctf_field_type_structure_get_field_count(self._ft)
2081 if count < 0:
2082 raise TypeError("Could not get Structure field count")
2083
2084 for i in range(count):
2085 field_name = _bt_python_ctf_field_type_structure_get_field_name(self._ft, i)
2086 if field_name is None:
2087 raise TypeError("Could not get Structure field name at index {}".format(i))
2088
2089 field_type_native = _bt_python_ctf_field_type_structure_get_field_type(self._ft, i)
2090 if field_type_native is None:
2091 raise TypeError("Could not get Structure field type at index {}".format(i))
2092
2093 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2094 yield (field_name, field_type)
2095
2096 """
2097 Get a field declaration by name (FieldDeclaration).
2098 """
2099 def get_field_by_name(self, name):
2100 field_type_native = _bt_ctf_field_type_structure_get_field_type_by_name(self._ft, name)
2101 if field_type_native is None:
2102 raise TypeError("Could not find Structure field with name {}".format(name))
2103
2104 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2105
14001277 2106 class VariantFieldDeclaration(FieldDeclaration):
ec8c88d7 2107 """
b4b5e4b5 2108 Create a new variant field declaration.
ec8c88d7
JG
2109 """
2110 def __init__(self, enum_tag, tag_name):
14001277
JG
2111 if enum_tag is None or not isinstance(enum_tag, CTFWriter.EnumerationFieldDeclaration):
2112 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
ec8c88d7 2113
14001277 2114 self._ft = _bt_ctf_field_type_variant_create(enum_tag._ft, str(tag_name))
ec8c88d7
JG
2115 super().__init__()
2116
b4b5e4b5
JG
2117 """
2118 Get the variant's tag name.
2119 """
2120 @property
2121 def tag_name(self):
2122 ret = _bt_ctf_field_type_variant_get_tag_name(self._ft)
2123 if ret is None:
2124 raise TypeError("Could not get Variant tag name")
2125 return ret
2126
2127 """
2128 Get the variant's tag type.
2129 """
2130 @property
2131 def tag_type(self):
2132 ret = _bt_ctf_field_type_variant_get_tag_type(self._ft)
2133 if ret is None:
2134 raise TypeError("Could not get Variant tag type")
2135 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2136
ec8c88d7
JG
2137 """
2138 Add a field of type "field_type" to the variant.
2139 """
2140 def add_field(self, field_type, field_name):
14001277 2141 ret = _bt_ctf_field_type_variant_add_field(self._ft, field_type._ft, str(field_name))
ec8c88d7
JG
2142 if ret < 0:
2143 raise ValueError("Could not add field to variant.")
2144
b4b5e4b5
JG
2145 """
2146 Generator returning the variant's field as tuples of (field name, field declaration).
2147 """
2148 @property
2149 def fields(self):
2150 count = _bt_ctf_field_type_variant_get_field_count(self._ft)
2151 if count < 0:
2152 raise TypeError("Could not get Variant field count")
2153
2154 for i in range(count):
2155 field_name = _bt_python_ctf_field_type_variant_get_field_name(self._ft, i)
2156 if field_name is None:
2157 raise TypeError("Could not get Variant field name at index {}".format(i))
2158
2159 field_type_native = _bt_python_ctf_field_type_variant_get_field_type(self._ft, i)
2160 if field_type_native is None:
2161 raise TypeError("Could not get Variant field type at index {}".format(i))
2162
2163 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2164 yield (field_name, field_type)
2165
2166 """
2167 Get a field declaration by name (FieldDeclaration).
2168 """
2169 def get_field_by_name(self, name):
2170 field_type_native = _bt_ctf_field_type_variant_get_field_type_by_name(self._ft, name)
2171 if field_type_native is None:
2172 raise TypeError("Could not find Variant field with name {}".format(name))
2173
2174 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2175
2176 """
2177 Get a field declaration from tag (EnumerationField).
2178 """
2179 def get_field_from_tag(self, tag):
2180 field_type_native = _bt_ctf_field_type_variant_get_field_type_from_tag(self._ft, tag._f)
2181 if field_type_native is None:
2182 raise TypeError("Could not find Variant field with tag value {}".format(tag.value))
2183
2184 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2185
14001277 2186 class ArrayFieldDeclaration(FieldDeclaration):
ec8c88d7 2187 """
b4b5e4b5 2188 Create a new array field declaration.
ec8c88d7
JG
2189 """
2190 def __init__(self, element_type, length):
2191 self._ft = _bt_ctf_field_type_array_create(element_type._ft, length)
2192 super().__init__()
2193
b4b5e4b5
JG
2194 """
2195 Get the array's element type.
2196 """
2197 @property
2198 def element_type(self):
2199 ret = _bt_ctf_field_type_array_get_element_type(self._ft)
2200 if ret is None:
2201 raise TypeError("Could not get Array element type")
2202 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2203
2204 """
2205 Get the array's length.
2206 """
2207 @property
2208 def length(self):
2209 ret = _bt_ctf_field_type_array_get_length(self._ft)
2210 if ret < 0:
2211 raise TypeError("Could not get Array length")
2212 return ret
2213
14001277 2214 class SequenceFieldDeclaration(FieldDeclaration):
ec8c88d7 2215 """
b4b5e4b5 2216 Create a new sequence field declaration.
ec8c88d7
JG
2217 """
2218 def __init__(self, element_type, length_field_name):
14001277 2219 self._ft = _bt_ctf_field_type_sequence_create(element_type._ft, str(length_field_name))
ec8c88d7
JG
2220 super().__init__()
2221
b4b5e4b5
JG
2222 """
2223 Get the sequence's element type.
2224 """
2225 @property
2226 def element_type(self):
2227 ret = _bt_ctf_field_type_sequence_get_element_type(self._ft)
2228 if ret is None:
2229 raise TypeError("Could not get Sequence element type")
2230 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2231
2232 """
2233 Get the sequence's length field name.
2234 """
2235 @property
2236 def length_field_name(self):
2237 ret = _bt_ctf_field_type_sequence_get_length_field_name(self._ft)
2238 if ret is None:
2239 raise TypeError("Could not get Sequence length field name")
2240 return ret
2241
14001277 2242 class StringFieldDeclaration(FieldDeclaration):
ec8c88d7 2243 """
b4b5e4b5 2244 Create a new string field declaration.
ec8c88d7
JG
2245 """
2246 def __init__(self):
2247 self._ft = _bt_ctf_field_type_string_create()
2248 super().__init__()
2249
14001277 2250 """
b4b5e4b5 2251 Get a string declaration's encoding (a constant from the CTFStringEncoding class).
14001277
JG
2252 """
2253 @property
2254 def encoding(self):
b4b5e4b5 2255 return _bt_ctf_field_type_string_get_encoding(self._ft)
14001277 2256
ec8c88d7 2257 """
b4b5e4b5 2258 Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class.
ec8c88d7 2259 """
14001277
JG
2260 @encoding.setter
2261 def encoding(self, encoding):
ec8c88d7
JG
2262 ret = _bt_ctf_field_type_string_set_encoding(self._ft, encoding)
2263 if ret < 0:
2264 raise ValueError("Could not set string encoding.")
2265
2266 """
2267 Create an instance of a field.
2268 """
2269 @staticmethod
b4b5e4b5 2270 def create_field(field_type):
14001277
JG
2271 if field_type is None or not isinstance(field_type, CTFWriter.FieldDeclaration):
2272 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
2273
2274 if isinstance(field_type, CTFWriter.IntegerFieldDeclaration):
2275 return CTFWriter.IntegerField(field_type)
2276 elif isinstance(field_type, CTFWriter.EnumerationFieldDeclaration):
2277 return CTFWriter.EnumerationField(field_type)
2278 elif isinstance(field_type, CTFWriter.FloatFieldDeclaration):
2279 return CTFWriter.FloatFieldingPoint(field_type)
2280 elif isinstance(field_type, CTFWriter.StructureFieldDeclaration):
2281 return CTFWriter.StructureField(field_type)
2282 elif isinstance(field_type, CTFWriter.VariantFieldDeclaration):
2283 return CTFWriter.VariantField(field_type)
2284 elif isinstance(field_type, CTFWriter.ArrayFieldDeclaration):
2285 return CTFWriter.ArrayField(field_type)
2286 elif isinstance(field_type, CTFWriter.SequenceFieldDeclaration):
2287 return CTFWriter.SequenceField(field_type)
2288 elif isinstance(field_type, CTFWriter.StringFieldDeclaration):
2289 return CTFWriter.StringField(field_type)
ec8c88d7
JG
2290
2291 class Field:
2292 """
2293 Base class, do not instantiate.
2294 """
2295 def __init__(self, field_type):
14001277 2296 if not isinstance(field_type, CTFWriter.FieldDeclaration):
ec8c88d7
JG
2297 raise TypeError("Invalid field_type argument.")
2298
2299 self._f = _bt_ctf_field_create(field_type._ft)
2300 if self._f is None:
2301 raise ValueError("Field creation failed.")
2302
2303 def __del__(self):
2304 _bt_ctf_field_put(self._f)
2305
2306 @staticmethod
2307 def _create_field_from_native_instance(native_field_instance):
2308 type_dict = {
14001277
JG
2309 CTFTypeId.INTEGER : CTFWriter.IntegerField,
2310 CTFTypeId.FLOAT : CTFWriter.FloatFieldingPoint,
2311 CTFTypeId.ENUM : CTFWriter.EnumerationField,
2312 CTFTypeId.STRING : CTFWriter.StringField,
2313 CTFTypeId.STRUCT : CTFWriter.StructureField,
2314 CTFTypeId.VARIANT : CTFWriter.VariantField,
2315 CTFTypeId.ARRAY : CTFWriter.ArrayField,
2316 CTFTypeId.SEQUENCE : CTFWriter.SequenceField
ec8c88d7
JG
2317 }
2318
2319 field_type = _bt_python_get_field_type(native_field_instance)
2320 if field_type == CTFTypeId.UNKNOWN:
2321 raise TypeError("Invalid field instance")
2322
2323 field = CTFWriter.Field.__new__(CTFWriter.Field)
2324 field._f = native_field_instance
2325 field.__class__ = type_dict[field_type]
2326 return field
2327
b4b5e4b5
JG
2328 @property
2329 def declaration(self):
2330 native_field_type = _bt_ctf_field_get_type(self._f)
2331 if native_field_type is None:
2332 raise TypeError("Invalid field instance")
2333 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(native_field_type)
2334
14001277
JG
2335 class IntegerField(Field):
2336 """
2337 Get an integer field's value.
2338 """
2339 @property
2340 def value(self):
821ca76c
JG
2341 signedness = _bt_python_field_integer_get_signedness(self._f)
2342 if signedness < 0:
2343 raise TypeError("Invalid integer instance.")
2344
2345 if signedness == 0:
2346 ret, value = _bt_ctf_field_unsigned_integer_get_value(self._f)
2347 else:
2348 ret, value = _bt_ctf_field_signed_integer_get_value(self._f)
2349
2350 if ret < 0:
2351 raise ValueError("Could not get integer field value.")
2352 return value
14001277 2353
ec8c88d7
JG
2354 """
2355 Set an integer field's value.
2356 """
14001277
JG
2357 @value.setter
2358 def value(self, value):
821ca76c
JG
2359 if not isinstance(value, int):
2360 raise TypeError("IntegerField's value must be an int")
2361
ec8c88d7
JG
2362 signedness = _bt_python_field_integer_get_signedness(self._f)
2363 if signedness < 0:
2364 raise TypeError("Invalid integer instance.")
2365
2366 if signedness == 0:
2367 ret = _bt_ctf_field_unsigned_integer_set_value(self._f, value)
2368 else:
2369 ret = _bt_ctf_field_signed_integer_set_value(self._f, value)
2370
2371 if ret < 0:
2372 raise ValueError("Could not set integer field value.")
2373
14001277 2374 class EnumerationField(Field):
ec8c88d7
JG
2375 """
2376 Return the enumeration's underlying container field (an integer field).
2377 """
14001277
JG
2378 @property
2379 def container(self):
2380 container = CTFWriter.IntegerField.__new__(CTFWriter.IntegerField)
ec8c88d7
JG
2381 container._f = _bt_ctf_field_enumeration_get_container(self._f)
2382 if container._f is None:
2383 raise TypeError("Invalid enumeration field type.")
2384 return container
2385
821ca76c
JG
2386 """
2387 Get the enumeration field's mapping name.
2388 """
2389 @property
2390 def value(self):
2391 value = _bt_ctf_field_enumeration_get_mapping_name(self._f)
2392 if value is None:
2393 raise ValueError("Could not get enumeration's mapping name.")
2394 return value
2395
2396 """
2397 Set the enumeration field's value. Must be an integer as mapping names
2398 may be ambiguous.
2399 """
2400 @value.setter
2401 def value(self, value):
2402 if not isinstance(value, int):
2403 raise TypeError("EnumerationField value must be an int")
2404 self.container.value = value
2405
14001277
JG
2406 class FloatFieldingPoint(Field):
2407 """
2408 Get a floating point field's value.
2409 """
2410 @property
2411 def value(self):
821ca76c
JG
2412 ret, value = _bt_ctf_field_floating_point_get_value(self._f)
2413 if ret < 0:
2414 raise ValueError("Could not get floating point field value.")
2415 return value
14001277 2416
ec8c88d7
JG
2417 """
2418 Set a floating point field's value.
2419 """
14001277
JG
2420 @value.setter
2421 def value(self, value):
821ca76c
JG
2422 if not isinstance(value, int) and not isinstance(value, float):
2423 raise TypeError("Value must be either a float or an int")
2424
14001277 2425 ret = _bt_ctf_field_floating_point_set_value(self._f, float(value))
ec8c88d7
JG
2426 if ret < 0:
2427 raise ValueError("Could not set floating point field value.")
2428
14001277 2429 class StructureField(Field):
ec8c88d7
JG
2430 """
2431 Get the structure's field corresponding to the provided field name.
2432 """
14001277
JG
2433 def field(self, field_name):
2434 native_instance = _bt_ctf_field_structure_get_field(self._f, str(field_name))
ec8c88d7
JG
2435 if native_instance is None:
2436 raise ValueError("Invalid field_name provided.")
2437 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2438
14001277 2439 class VariantField(Field):
ec8c88d7
JG
2440 """
2441 Return the variant's selected field. The "tag" field is the selector enum field.
2442 """
14001277 2443 def field(self, tag):
ec8c88d7
JG
2444 native_instance = _bt_ctf_field_variant_get_field(self._f, tag._f)
2445 if native_instance is None:
2446 raise ValueError("Invalid tag provided.")
2447 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2448
14001277 2449 class ArrayField(Field):
ec8c88d7
JG
2450 """
2451 Return the array's field at position "index".
2452 """
14001277 2453 def field(self, index):
ec8c88d7
JG
2454 native_instance = _bt_ctf_field_array_get_field(self._f, index)
2455 if native_instance is None:
2456 raise IndexError("Invalid index provided.")
2457 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2458
14001277
JG
2459 class SequenceField(Field):
2460 """
2461 Get the sequence's length field (IntegerField).
2462 """
2463 @property
2464 def length(self):
821ca76c
JG
2465 native_instance = _bt_ctf_field_sequence_get_length(self._f)
2466 if native_instance is None:
2467 length = -1
2468 return CTFWriter.Field._create_field_from_native_instance(native_instance)
14001277 2469
ec8c88d7
JG
2470 """
2471 Set the sequence's length field (IntegerField).
2472 """
14001277
JG
2473 @length.setter
2474 def length(self, length_field):
8cea25b5 2475 if not isinstance(length_field, CTFWriter.IntegerField):
ec8c88d7 2476 raise TypeError("Invalid length field.")
b4b5e4b5
JG
2477 if length_field.declaration.signed:
2478 raise TypeError("Sequence field length must be unsigned")
8cea25b5 2479 ret = _bt_ctf_field_sequence_set_length(self._f, length_field._f)
ec8c88d7
JG
2480 if ret < 0:
2481 raise ValueError("Could not set sequence length.")
2482
2483 """
2484 Return the sequence's field at position "index".
2485 """
14001277 2486 def field(self, index):
ec8c88d7
JG
2487 native_instance = _bt_ctf_field_sequence_get_field(self._f, index)
2488 if native_instance is None:
2489 raise ValueError("Could not get sequence element at index.")
2490 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2491
14001277
JG
2492 class StringField(Field):
2493 """
2494 Get a string field's value.
2495 """
2496 @property
2497 def value(self):
821ca76c 2498 return _bt_ctf_field_string_get_value(self._f)
14001277 2499
ec8c88d7
JG
2500 """
2501 Set a string field's value.
2502 """
14001277
JG
2503 @value.setter
2504 def value(self, value):
2505 ret = _bt_ctf_field_string_set_value(self._f, str(value))
ec8c88d7
JG
2506 if ret < 0:
2507 raise ValueError("Could not set string field value.")
2508
2509 class EventClass:
2510 """
2511 Create a new event class of the given name.
2512 """
2513 def __init__(self, name):
2514 self._ec = _bt_ctf_event_class_create(name)
2515 if self._ec is None:
2516 raise ValueError("Event class creation failed.")
2517
2518 def __del__(self):
2519 _bt_ctf_event_class_put(self._ec)
2520
2521 """
2522 Add a field of type "field_type" to the event class.
2523 """
2524 def add_field(self, field_type, field_name):
14001277 2525 ret = _bt_ctf_event_class_add_field(self._ec, field_type._ft, str(field_name))
ec8c88d7
JG
2526 if ret < 0:
2527 raise ValueError("Could not add field to event class.")
2528
38f28df6
JG
2529 """
2530 Get the event class' name.
2531 """
2532 @property
2533 def name(self):
2534 name = _bt_ctf_event_class_get_name(self._ec)
2535 if name is None:
2536 raise TypeError("Could not get EventClass name")
2537 return name
2538
2539 """
2540 Get the event class' id. Returns a negative value if unset.
2541 """
2542 @property
2543 def id(self):
2544 id = _bt_ctf_event_class_get_id(self._ec)
2545 if id < 0:
f6c2aa83 2546 raise TypeError("Could not get EventClass id")
38f28df6
JG
2547 return id
2548
2549 """
2550 Set the event class' id. Throws a TypeError if the event class
2551 is already registered to a stream class.
2552 """
2553 @id.setter
2554 def id(self, id):
2555 ret = _bt_ctf_event_class_set_id(self._ec, id)
2556 if ret < 0:
2557 raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
2558
2559 """
2560 Get the event class' stream class. Returns None if unset.
2561 """
2562 @property
2563 def stream_class(self):
2564 stream_class_native = _bt_ctf_event_class_get_stream_class(self._ec)
2565 if stream_class_native is None:
2566 return None
2567 stream_class = CTFWriter.StreamClass.__new__(CTFWriter.StreamClass)
2568 stream_class._sc = stream_class_native
2569 return stream_class
2570
2571 """
2572 Generator returning the event class' fields as tuples of (field name, field declaration).
2573 """
2574 @property
2575 def fields(self):
2576 count = _bt_ctf_event_class_get_field_count(self._ec)
2577 if count < 0:
2578 raise TypeError("Could not get EventClass' field count")
2579
2580 for i in range(count):
2581 field_name = _bt_python_ctf_event_class_get_field_name(self._ec, i)
2582 if field_name is None:
2583 raise TypeError("Could not get EventClass' field name at index {}".format(i))
2584
2585 field_type_native = _bt_python_ctf_event_class_get_field_type(self._ec, i)
2586 if field_type_native is None:
2587 raise TypeError("Could not get EventClass' field type at index {}".format(i))
2588
2589 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2590 yield (field_name, field_type)
2591
2592 """
2593 Get a field declaration by name (FieldDeclaration).
2594 """
2595 def get_field_by_name(self, name):
2596 field_type_native = _bt_ctf_event_class_get_field_by_name(self._ec, name)
2597 if field_type_native is None:
2598 raise TypeError("Could not find EventClass field with name {}".format(name))
2599 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2600
ec8c88d7
JG
2601 class Event:
2602 """
2603 Create a new event of the given event class.
2604 """
2605 def __init__(self, event_class):
2606 if not isinstance(event_class, CTFWriter.EventClass):
2607 raise TypeError("Invalid event_class argument.")
2608
2609 self._e = _bt_ctf_event_create(event_class._ec)
2610 if self._e is None:
2611 raise ValueError("Event creation failed.")
2612
2613 def __del__(self):
2614 _bt_ctf_event_put(self._e)
2615
2616 """
38f28df6 2617 Get the event's class.
ec8c88d7 2618 """
38f28df6
JG
2619 @property
2620 def event_class(self):
2621 event_class_native = _bt_ctf_event_get_class(self._e)
2622 if event_class_native is None:
2623 return None
2624 event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
2625 event_class._ec = event_class_native
2626 return event_class
2627
2628 """
2629 Get a clock from event. Returns None if the event's class
2630 is not registered to a stream class.
2631 """
2632 def clock(self):
2633 clock_instance = _bt_ctf_event_get_clock(self._e)
2634 if clock_instance is None:
2635 return None
2636 clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
2637 clock._c = clock_instance
2638 return clock
ec8c88d7
JG
2639
2640 """
14001277 2641 Get a field from event.
ec8c88d7 2642 """
14001277
JG
2643 def payload(self, field_name):
2644 native_instance = _bt_ctf_event_get_payload(self._e, str(field_name))
ec8c88d7
JG
2645 if native_instance is None:
2646 raise ValueError("Could not get event payload.")
2647 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2648
38f28df6
JG
2649 """
2650 Set a manually created field as an event's payload.
2651 """
2652 def set_payload(self, field_name, value_field):
2653 if not isinstance(value, CTFWriter.Field):
2654 raise TypeError("Invalid value type.")
2655 ret = _bt_ctf_event_set_payload(self._e, str(field_name), value_field._f)
2656 if ret < 0:
2657 raise ValueError("Could not set event field payload.")
2658
ec8c88d7
JG
2659 class StreamClass:
2660 """
2661 Create a new stream class of the given name.
2662 """
2663 def __init__(self, name):
2664 self._sc = _bt_ctf_stream_class_create(name)
2665 if self._sc is None:
2666 raise ValueError("Stream class creation failed.")
2667
2668 def __del__(self):
2669 _bt_ctf_stream_class_put(self._sc)
2670
f6c2aa83
JG
2671 """
2672 Get a stream class' name.
2673 """
2674 @property
2675 def name(self):
2676 name = _bt_ctf_stream_class_get_name(self._sc)
2677 if name is None:
2678 raise TypeError("Could not get StreamClass name")
2679 return name
2680
14001277
JG
2681 """
2682 Get a stream class' clock.
2683 """
2684 @property
2685 def clock(self):
f6c2aa83
JG
2686 clock_instance = _bt_ctf_stream_class_get_clock(self._sc)
2687 if clock_instance is None:
2688 return None
2689 clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
2690 clock._c = clock_instance
2691 return clock
14001277 2692
ec8c88d7
JG
2693 """
2694 Assign a clock to a stream class.
2695 """
14001277
JG
2696 @clock.setter
2697 def clock(self, clock):
ec8c88d7
JG
2698 if not isinstance(clock, CTFWriter.Clock):
2699 raise TypeError("Invalid clock type.")
2700
2701 ret = _bt_ctf_stream_class_set_clock(self._sc, clock._c)
2702 if ret < 0:
2703 raise ValueError("Could not set stream class clock.")
2704
f6c2aa83
JG
2705 """
2706 Get a stream class' id.
2707 """
2708 @property
2709 def id(self):
2710 ret = _bt_ctf_stream_class_get_id(self._sc)
2711 if ret < 0:
2712 raise TypeError("Could not get StreamClass id")
2713 return ret
2714
2715 """
2716 Assign an id to a stream class.
2717 """
2718 @id.setter
2719 def id(self, id):
2720 ret = _bt_ctf_stream_class_set_id(self._sc, id)
2721 if ret < 0:
2722 raise TypeError("Could not set stream class id.")
2723
2724 """
2725 Generator returning the stream class' event classes.
2726 """
2727 @property
2728 def event_classes(self):
2729 count = _bt_ctf_stream_class_get_event_class_count(self._sc)
2730 if count < 0:
2731 raise TypeError("Could not get StreamClass' event class count")
2732
2733 for i in range(count):
2734 event_class_native = _bt_ctf_stream_class_get_event_class(self._sc, i)
2735 if event_class_native is None:
2736 raise TypeError("Could not get StreamClass' event class at index {}".format(i))
2737
2738 event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
2739 event_class._ec = event_class_native
2740 yield event_class
2741
ec8c88d7
JG
2742 """
2743 Add an event class to a stream class. New events can be added even after a
14001277 2744 stream has been instantiated and events have been appended. However, a stream
ec8c88d7
JG
2745 will not accept events of a class that has not been registered beforehand.
2746 """
2747 def add_event_class(self, event_class):
2748 if not isinstance(event_class, CTFWriter.EventClass):
2749 raise TypeError("Invalid event_class type.")
2750
2751 ret = _bt_ctf_stream_class_add_event_class(self._sc, event_class._ec)
2752 if ret < 0:
2753 raise ValueError("Could not add event class.")
2754
2755 class Stream:
2756 """
2757 Create a stream of the given class.
2758 """
2759 def __init__(self, stream_class):
2760 if not isinstance(stream_class, CTFWriter.StreamClass):
2761 raise TypeError("Invalid stream_class type.")
2762
2763 self._s = _bt_ctf_stream_create(stream_class._sc)
2764 if self._s is None:
2765 raise ValueError("Stream creation failed.")
2766
2767 def __del__(self):
2768 _bt_ctf_stream_put(self._s)
2769
708e628a
JG
2770 """
2771 Get a stream's discarded event count.
2772 """
2773 @property
2774 def discarded_events(self):
2775 ret, count = _bt_ctf_stream_get_discarded_events_count(self._s)
2776 if ret < 0:
2777 raise ValueError("Could not get the stream's discarded events count")
2778 return count
2779
ec8c88d7
JG
2780 """
2781 Increase the current packet's discarded event count.
2782 """
2783 def append_discarded_events(self, event_count):
92757c30 2784 _bt_ctf_stream_append_discarded_events(self._s, event_count)
ec8c88d7
JG
2785
2786 """
2787 Append "event" to the stream's current packet. The stream's associated clock
2788 will be sampled during this call. The event shall not be modified after
2789 being appended to a stream.
2790 """
2791 def append_event(self, event):
2792 ret = _bt_ctf_stream_append_event(self._s, event._e)
2793 if ret < 0:
2794 raise ValueError("Could not append event to stream.")
2795
2796 """
2797 The stream's current packet's events will be flushed to disk. Events
2798 subsequently appended to the stream will be added to a new packet.
2799 """
2800 def flush(self):
2801 ret = _bt_ctf_stream_flush(self._s)
2802 if ret < 0:
2803 raise ValueError("Could not flush stream.")
2804
2805 class Writer:
2806 """
2807 Create a new writer that will produce a trace in the given path.
2808 """
2809 def __init__(self, path):
2810 self._w = _bt_ctf_writer_create(path)
2811 if self._w is None:
2812 raise ValueError("Writer creation failed.")
2813
2814 def __del__(self):
2815 _bt_ctf_writer_put(self._w)
2816
2817 """
2818 Create a new stream instance and register it to the writer.
2819 """
2820 def create_stream(self, stream_class):
2821 if not isinstance(stream_class, CTFWriter.StreamClass):
2822 raise TypeError("Invalid stream_class type.")
2823
2824 stream = CTFWriter.Stream.__new__(CTFWriter.Stream)
2825 stream._s = _bt_ctf_writer_create_stream(self._w, stream_class._sc)
2826 return stream
2827
2828 """
2829 Add an environment field to the trace.
2830 """
2831 def add_environment_field(self, name, value):
14001277 2832 ret = _bt_ctf_writer_add_environment_field(self._w, str(name), str(value))
ec8c88d7
JG
2833 if ret < 0:
2834 raise ValueError("Could not add environment field to trace.")
2835
2836 """
2837 Add a clock to the trace. Clocks assigned to stream classes must be
2838 registered to the writer.
2839 """
2840 def add_clock(self, clock):
2841 ret = _bt_ctf_writer_add_clock(self._w, clock._c)
2842 if ret < 0:
2843 raise ValueError("Could not add clock to Writer.")
2844
2845 """
2846 Get the trace's TSDL meta-data.
2847 """
14001277
JG
2848 @property
2849 def metadata(self):
ec8c88d7
JG
2850 return _bt_ctf_writer_get_metadata_string(self._w)
2851
2852 """
2853 Flush the trace's metadata to the metadata file.
2854 """
2855 def flush_metadata(self):
2856 _bt_ctf_writer_flush_metadata(self._w)
2857
14001277
JG
2858 """
2859 Get the trace's byte order. Must be a constant from the ByteOrder
2860 class.
2861 """
2862 @property
2863 def byte_order(self):
2864 raise NotImplementedError("Getter not implemented.")
2865
ec8c88d7
JG
2866 """
2867 Set the trace's byte order. Must be a constant from the ByteOrder
2868 class. Defaults to BYTE_ORDER_NATIVE, the host machine's endianness.
2869 """
14001277
JG
2870 @byte_order.setter
2871 def byte_order(self, byte_order):
ec8c88d7
JG
2872 ret = _bt_ctf_writer_set_byte_order(self._w, byte_order)
2873 if ret < 0:
2874 raise ValueError("Could not set trace's byte order.")
2875
2876%}
This page took 0.160766 seconds and 4 git commands to generate.