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