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