diff options
| author | Johannes Berg <johannes@sipsolutions.net> | 2010-05-20 05:42:51 -0400 |
|---|---|---|
| committer | Johannes Berg <johannes@sipsolutions.net> | 2010-05-25 07:17:03 -0400 |
| commit | 339321b326d241dcf6490ed910e8122486e29cd6 (patch) | |
| tree | d4cc06dbc9ad286c1df9105ca253f449e91f262a /Documentation/README.PythonPlugin | |
| parent | df4d5ba4cb86f1055160559c9b7024e3dbf76a38 (diff) | |
python plugin
This adds a python plugin that can in turn
load plugins written in python.
To make it work, trace-cmd needs to load
plugin modules with RTLD_GLOBAL so that
the python interpreter's symbols will be
available to python C extension modules.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Diffstat (limited to 'Documentation/README.PythonPlugin')
| -rw-r--r-- | Documentation/README.PythonPlugin | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/Documentation/README.PythonPlugin b/Documentation/README.PythonPlugin new file mode 100644 index 0000000..3de0564 --- /dev/null +++ b/Documentation/README.PythonPlugin | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | PYTHON PLUGIN DOCUMENTATION | ||
| 2 | ============================= | ||
| 3 | |||
| 4 | With the python plugin (make python-plugin) you can now | ||
| 5 | write plugins in python. The API exported by the python | ||
| 6 | plugin itself (written in C) allows you to access most | ||
| 7 | information about a record from python. | ||
| 8 | |||
| 9 | To write a python plugin, put a new .py file into a new | ||
| 10 | ~/.trace-cmd/python/ directory. | ||
| 11 | |||
| 12 | The most basic python plugin is this: | ||
| 13 | |||
| 14 | --- %< --- | ||
| 15 | def register(pevent): | ||
| 16 | pass | ||
| 17 | --- >% --- | ||
| 18 | |||
| 19 | which obviously does nothing at all. | ||
| 20 | |||
| 21 | To register a callback, use the pevent.register_event_handler | ||
| 22 | function: | ||
| 23 | |||
| 24 | --- %< --- | ||
| 25 | import tracecmd | ||
| 26 | |||
| 27 | def my_event_handler(trace_seq, event): | ||
| 28 | pass | ||
| 29 | |||
| 30 | def register(pevent): | ||
| 31 | pevent.register_event_handler("subsys", "event_name", | ||
| 32 | my_event_handler) | ||
| 33 | --- >% --- | ||
| 34 | |||
| 35 | |||
| 36 | There are four object types that you get, described below. | ||
| 37 | |||
| 38 | tracecmd.PEvent | ||
| 39 | ----------------- | ||
| 40 | |||
| 41 | This is the class of the 'pevent' object above, | ||
| 42 | you get one of those via your register callback. | ||
| 43 | It has one method and one property: | ||
| 44 | * register_event_handler() - example above, to register | ||
| 45 | an event handler function | ||
| 46 | * file_endian - either '<' or '>' indicating | ||
| 47 | which endianness the file has, | ||
| 48 | to be used with struct.unpack() | ||
| 49 | |||
| 50 | tracecmd.TraceSeq | ||
| 51 | ------------------- | ||
| 52 | |||
| 53 | This is the class of the 'trace_seq' parameter to your callback | ||
| 54 | function. It has only one method, puts(), to put data into the | ||
| 55 | buffer. Formatting must be done in python. | ||
| 56 | |||
| 57 | tracecmd.Event | ||
| 58 | ---------------------- | ||
| 59 | |||
| 60 | This is the class of the 'event' parameter to your callback | ||
| 61 | function. Note that it doesn't just contain the format, but | ||
| 62 | also the event data. As such, you can do much with this, and | ||
| 63 | this is what you'll usually use. Each instance of this allows | ||
| 64 | access to record items via the dict protocol, and you can get | ||
| 65 | the items via its keys() methods. So for example, your | ||
| 66 | callback could be | ||
| 67 | |||
| 68 | --- %< --- | ||
| 69 | def my_callback(trace_seq, event): | ||
| 70 | for fieldname in event.keys(): | ||
| 71 | field = event[fieldname] | ||
| 72 | --- >% --- | ||
| 73 | |||
| 74 | Each field returned from the dict protocol is an instance of | ||
| 75 | the next (and last) class: | ||
| 76 | |||
| 77 | tracecmd.Field | ||
| 78 | ---------------------- | ||
| 79 | |||
| 80 | This is an instance of a field, including its data. It affords | ||
| 81 | numerous use cases and is what you'll be using most. | ||
| 82 | |||
| 83 | * If this is an integer field, i.e. 1, 2, 4 or 8 bytes long, | ||
| 84 | you can convert it to the number contained, according to | ||
| 85 | the file's endianness, by simply casting it to a long: | ||
| 86 | |||
| 87 | field = event['myint'] | ||
| 88 | value = long(field) | ||
| 89 | |||
| 90 | * You can access the field's data, as field.data, and if the | ||
| 91 | data is really a "__data_loc" type that will be resolved | ||
| 92 | automatically. (If you don't know what this means, don't | ||
| 93 | worry about it and just use field.data) | ||
| 94 | |||
| 95 | |||
| 96 | This is it. It's pretty simple. A fully-featured plugin could | ||
| 97 | look like this: | ||
| 98 | |||
| 99 | --- %< --- | ||
| 100 | def my_event_handler(trace_seq, event): | ||
| 101 | trace_seq.puts("myev: %u", long(event['myfield'])) | ||
| 102 | |||
| 103 | def register(pevent): | ||
| 104 | pevent.register_event_handler("subsys", "event_name", | ||
| 105 | my_event_handler) | ||
| 106 | --- >% --- | ||
| 107 | |||
| 108 | |||
| 109 | Tips and tricks | ||
| 110 | ----------------- | ||
| 111 | |||
| 112 | Be familiar with the struct module and use it, always | ||
| 113 | checking endianness and potentially using pevent.file_endian. | ||
| 114 | |||
| 115 | |||
| 116 | If you need access to pevent in your callbacks, simply | ||
| 117 | pass it in yourself: | ||
| 118 | |||
| 119 | --- %< --- | ||
| 120 | def my_event_handler(pevent, trace_seq, event): | ||
| 121 | pass | ||
| 122 | |||
| 123 | def register(pevent): | ||
| 124 | pevent.register_event_handler("subsys", "event_name", | ||
| 125 | lambda *args: my_event_handler(pevent, *args) | ||
| 126 | ) | ||
| 127 | --- >% --- | ||
