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