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