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