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