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