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