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