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