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