Commit | Line | Data |
---|---|---|
ab5be9fa MJ |
1 | # |
2 | # Copyright (C) 2012 Danny Serres <danny.serres@efficios.com> | |
3 | # | |
4 | # SPDX-License-Identifier: GPL-2.0-only | |
5 | # | |
6 | # This example shows basically how to use the lttng-tools python module | |
36907cb5 DS |
7 | |
8 | from lttng import * | |
9 | ||
6a871bbe | 10 | |
36907cb5 DS |
11 | # This error will be raised is something goes wrong |
12 | class LTTngError(Exception): | |
6a871bbe KS |
13 | def __init__(self, value): |
14 | self.value = value | |
15 | ||
16 | def __str__(self): | |
17 | return repr(self.value) | |
18 | ||
36907cb5 | 19 | |
6a871bbe | 20 | # Setting up the domain to use |
36907cb5 DS |
21 | dom = Domain() |
22 | dom.type = DOMAIN_KERNEL | |
23 | ||
6a871bbe | 24 | # Setting up a channel to use |
36907cb5 DS |
25 | channel = Channel() |
26 | channel.name = "mychan" | |
27 | channel.attr.overwrite = 0 | |
28 | channel.attr.subbuf_size = 4096 | |
29 | channel.attr.num_subbuf = 8 | |
30 | channel.attr.switch_timer_interval = 0 | |
31 | channel.attr.read_timer_interval = 200 | |
32 | channel.attr.output = EVENT_SPLICE | |
33 | ||
6a871bbe | 34 | # Setting up some events that will be used |
36907cb5 DS |
35 | event = Event() |
36 | event.type = EVENT_TRACEPOINT | |
37 | event.loglevel_type = EVENT_LOGLEVEL_ALL | |
38 | ||
39 | sched_switch = Event() | |
40 | sched_switch.name = "sched_switch" | |
41 | sched_switch.type = EVENT_TRACEPOINT | |
42 | sched_switch.loglevel_type = EVENT_LOGLEVEL_ALL | |
43 | ||
44 | sched_process_exit = Event() | |
45 | sched_process_exit.name = "sched_process_exit" | |
46 | sched_process_exit.type = EVENT_TRACEPOINT | |
47 | sched_process_exit.loglevel_type = EVENT_LOGLEVEL_ALL | |
48 | ||
49 | sched_process_free = Event() | |
50 | sched_process_free.name = "sched_process_free" | |
51 | sched_process_free.type = EVENT_TRACEPOINT | |
52 | sched_process_free.loglevel_type = EVENT_LOGLEVEL_ALL | |
53 | ||
54 | ||
6a871bbe KS |
55 | # Creating a new session |
56 | res = create("test", "/lttng-traces/test") | |
57 | if res < 0: | |
58 | raise LTTngError(strerror(res)) | |
36907cb5 | 59 | |
6a871bbe | 60 | # Creating handle |
36907cb5 DS |
61 | han = None |
62 | han = Handle("test", dom) | |
63 | if han is None: | |
6a871bbe | 64 | raise LTTngError("Handle not created") |
36907cb5 | 65 | |
6a871bbe | 66 | # Enabling the kernel channel |
36907cb5 | 67 | res = enable_channel(han, channel) |
6a871bbe KS |
68 | if res < 0: |
69 | raise LTTngError(strerror(res)) | |
36907cb5 | 70 | |
6a871bbe KS |
71 | # Enabling some events in given channel |
72 | # To enable all events in default channel, use | |
73 | # enable_event(han, event, None) | |
36907cb5 | 74 | res = enable_event(han, sched_switch, channel.name) |
6a871bbe KS |
75 | if res < 0: |
76 | raise LTTngError(strerror(res)) | |
36907cb5 DS |
77 | |
78 | res = enable_event(han, sched_process_exit, channel.name) | |
6a871bbe KS |
79 | if res < 0: |
80 | raise LTTngError(strerror(res)) | |
36907cb5 DS |
81 | |
82 | res = enable_event(han, sched_process_free, channel.name) | |
6a871bbe KS |
83 | if res < 0: |
84 | raise LTTngError(strerror(res)) | |
36907cb5 | 85 | |
6a871bbe | 86 | # Disabling an event |
36907cb5 | 87 | res = disable_event(han, sched_switch.name, channel.name) |
6a871bbe KS |
88 | if res < 0: |
89 | raise LTTngError(strerror(res)) | |
36907cb5 | 90 | |
6a871bbe | 91 | # Getting a list of the channels |
36907cb5 DS |
92 | l = list_channels(han) |
93 | if type(l) is int: | |
6a871bbe | 94 | raise LTTngError(strerror(l)) |
36907cb5 | 95 | |
6a871bbe | 96 | # Starting the trace |
36907cb5 | 97 | res = start("test") |
6a871bbe KS |
98 | if res < 0: |
99 | raise LTTngError(strerror(res)) | |
36907cb5 | 100 | |
6a871bbe | 101 | # Stopping the trace |
36907cb5 | 102 | res = stop("test") |
6a871bbe KS |
103 | if res < 0: |
104 | raise LTTngError(strerror(res)) | |
36907cb5 | 105 | |
6a871bbe | 106 | # Disabling a channel |
36907cb5 | 107 | res = disable_channel(han, channel.name) |
6a871bbe KS |
108 | if res < 0: |
109 | raise LTTngError(strerror(res)) | |
36907cb5 | 110 | |
6a871bbe | 111 | # Destroying the handle |
36907cb5 DS |
112 | del han |
113 | ||
6a871bbe | 114 | # Destroying the session |
36907cb5 | 115 | res = destroy("test") |
6a871bbe KS |
116 | if res < 0: |
117 | raise LTTngError(strerror(res)) |