Python-bindings: Refactor the Context class
[babeltrace.git] / bindings / python / examples / babeltrace_and_lttng.py
CommitLineData
300b4c33 1#!/usr/bin/env python3
24a3136a
DS
2# babeltrace_and_lttng.py
3#
4# Babeltrace and LTTng example script
5#
6# Copyright 2012 EfficiOS Inc.
7#
8# Author: Danny Serres <danny.serres@efficios.com>
9#
10# Permission is hereby granted, free of charge, to any person obtaining a copy
11# of this software and associated documentation files (the "Software"), to deal
12# in the Software without restriction, including without limitation the rights
13# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14# copies of the Software, and to permit persons to whom the Software is
15# furnished to do so, subject to the following conditions:
16#
17# The above copyright notice and this permission notice shall be included in
18# all copies or substantial portions of the Software.
19
20
21# This script uses both lttng-tools and babeltrace
22# python modules. It creates a session, enables
23# events, starts tracing for 2 seconds, stops tracing,
24# destroys the session and outputs the trace in the
25# specified output file.
26#
27# WARNING: will destroy any existing trace having
28# the same name as ses_name
29
30
31# ------------------------------------------------------
32ses_name = "babeltrace-lttng-test"
33trace_path = "/lttng-traces/babeltrace-lttng-trace/"
34out_file = "babeltrace-lttng-trace-text-output.txt"
35# ------------------------------------------------------
36
37
38import time
39try:
40 import babeltrace, lttng
41except ImportError:
42 raise ImportError( "both babeltrace and lttng-tools "
43 "python modules must be installed" )
44
45
46# Errors to raise if something goes wrong
47class LTTngError(Exception):
48 pass
49class BabeltraceError(Exception):
50 pass
51
52
53# LTTNG-TOOLS
54
55# Making sure session does not already exist
56lttng.destroy(ses_name)
57
58# Creating a new session and handle
59ret = lttng.create(ses_name,trace_path)
60if ret < 0:
61 raise LTTngError(lttng.strerror(ret))
62
300b4c33
JG
63domain = lttng.Domain()
64domain.type = lttng.DOMAIN_KERNEL
65
24a3136a 66han = None
300b4c33 67han = lttng.Handle(ses_name, domain)
24a3136a
DS
68if han is None:
69 raise LTTngError("Handle not created")
70
71
72# Enabling all events
300b4c33
JG
73event = lttng.Event()
74event.type = lttng.EVENT_ALL
75event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
76ret = lttng.enable_event(han, event, None)
24a3136a
DS
77if ret < 0:
78 raise LTTngError(lttng.strerror(ret))
79
24a3136a
DS
80# Start, wait, stop
81ret = lttng.start(ses_name)
82if ret < 0:
83 raise LTTngError(lttng.strerror(ret))
84print("Tracing...")
85time.sleep(2)
86print("Stopped.")
87ret = lttng.stop(ses_name)
88if ret < 0:
89 raise LTTngError(lttng.strerror(ret))
90
91
92# Destroying tracing session
93ret = lttng.destroy(ses_name)
94if ret < 0:
95 raise LTTngError(lttng.strerror(ret))
96
97
98# BABELTRACE
99
74ea15ad
JG
100# Create TraceCollecion and add trace:
101traces = babeltrace.TraceCollection()
102ret = traces.add_trace(trace_path + "/kernel", "ctf")
24a3136a
DS
103if ret is None:
104 raise BabeltraceError("Error adding trace")
105
24a3136a
DS
106# Reading events from trace
107# and outputting timestamps and event names
108# in out_file
109print("Writing trace file...")
110output = open(out_file, "wt")
111
74ea15ad 112for event in traces.events:
24a3136a
DS
113 output.write("TS: {}, {} : {}\n".format(event.get_timestamp(),
114 event.get_cycles(), event.get_name()))
115
24a3136a
DS
116# Closing file
117output.close()
118
24a3136a 119print("Done.")
This page took 0.029367 seconds and 4 git commands to generate.