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