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