Rename bt_ctf_X -> bt_X, maintain backward compat. for pre-2.0 CTF writer
[babeltrace.git] / plugins / lttng-utils / plugin.c
CommitLineData
4f45f9bb
JD
1/*
2 * plugin.c
3 *
4 * Babeltrace Debug Info Plug-in
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
b4565e8b
PP
29#define BT_LOG_TAG "PLUGIN-CTF-LTTNG-UTILS-DEBUG-INFO-FLT"
30#include "logging.h"
31
9d408fca 32#include <babeltrace/babeltrace.h>
4f45f9bb
JD
33#include <plugins-common.h>
34#include <assert.h>
35#include "debug-info.h"
36#include "copy.h"
37
1c78e839
JD
38static
39gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data)
40{
41 struct debug_info_trace *di_trace = value;
42
43 di_trace->trace_static = 1;
44 debug_info_close_trace(di_trace->debug_it, di_trace);
45
46 return TRUE;
47}
48
4f45f9bb
JD
49static
50void destroy_debug_info_data(struct debug_info_component *debug_info)
51{
52 free(debug_info->arg_debug_info_field_name);
53 g_free(debug_info);
54}
55
56static
7b077a9d 57void destroy_debug_info_component(struct bt_private_component *component)
4f45f9bb 58{
7b077a9d 59 void *data = bt_private_component_get_user_data(component);
4f45f9bb
JD
60 destroy_debug_info_data(data);
61}
62
63static
64struct debug_info_component *create_debug_info_component_data(void)
65{
66 struct debug_info_component *debug_info;
67
68 debug_info = g_new0(struct debug_info_component, 1);
69 if (!debug_info) {
70 goto end;
71 }
72 debug_info->err = stderr;
73
74end:
75 return debug_info;
76}
77
78static
504db471 79void unref_trace(struct debug_info_trace *di_trace)
4f45f9bb 80{
1c78e839
JD
81 bt_put(di_trace->writer_trace);
82 g_free(di_trace);
4f45f9bb
JD
83}
84
85static
90157d89 86void debug_info_iterator_destroy(struct bt_private_connection_private_notification_iterator *it)
4f45f9bb
JD
87{
88 struct debug_info_iterator *it_data;
89
90157d89 90 it_data = bt_private_connection_private_notification_iterator_get_user_data(it);
4f45f9bb
JD
91 assert(it_data);
92
93 if (it_data->input_iterator_group) {
94 g_ptr_array_free(it_data->input_iterator_group, TRUE);
95 }
1c78e839
JD
96
97 g_hash_table_foreach_remove(it_data->trace_map,
98 empty_trace_map, it_data);
99 g_hash_table_destroy(it_data->trace_map);
100
4f45f9bb
JD
101 bt_put(it_data->current_notification);
102 bt_put(it_data->input_iterator);
1c78e839 103
4f45f9bb
JD
104 g_free(it_data);
105}
106
107static
108struct bt_notification *handle_notification(FILE *err,
109 struct debug_info_iterator *debug_it,
110 struct bt_notification *notification)
111{
112 struct bt_notification *new_notification = NULL;
113
114 switch (bt_notification_get_type(notification)) {
115 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
116 {
50842bdc 117 struct bt_packet *packet =
4f45f9bb 118 bt_notification_packet_begin_get_packet(notification);
50842bdc 119 struct bt_packet *writer_packet;
4f45f9bb
JD
120
121 if (!packet) {
122 goto end;
123 }
124
125 writer_packet = debug_info_new_packet(debug_it, packet);
126 assert(writer_packet);
127 new_notification = bt_notification_packet_begin_create(
128 writer_packet);
129 assert(new_notification);
130 bt_put(packet);
1c78e839 131 bt_put(writer_packet);
4f45f9bb
JD
132 break;
133 }
134 case BT_NOTIFICATION_TYPE_PACKET_END:
135 {
50842bdc 136 struct bt_packet *packet =
4f45f9bb 137 bt_notification_packet_end_get_packet(notification);
50842bdc 138 struct bt_packet *writer_packet;
4f45f9bb
JD
139
140 if (!packet) {
141 goto end;
142 }
143
144 writer_packet = debug_info_close_packet(debug_it, packet);
145 assert(writer_packet);
146 new_notification = bt_notification_packet_end_create(
147 writer_packet);
148 assert(new_notification);
149 bt_put(packet);
1c78e839 150 bt_put(writer_packet);
4f45f9bb
JD
151 break;
152 }
153 case BT_NOTIFICATION_TYPE_EVENT:
154 {
50842bdc 155 struct bt_event *event = bt_notification_event_get_event(
4f45f9bb 156 notification);
50842bdc 157 struct bt_event *writer_event;
4f45f9bb
JD
158 struct bt_clock_class_priority_map *cc_prio_map =
159 bt_notification_event_get_clock_class_priority_map(
160 notification);
161
162 if (!event) {
163 goto end;
164 }
165 writer_event = debug_info_output_event(debug_it, event);
166 assert(writer_event);
167 new_notification = bt_notification_event_create(writer_event,
168 cc_prio_map);
169 bt_put(cc_prio_map);
170 assert(new_notification);
171 bt_put(event);
172 bt_put(writer_event);
173 break;
174 }
504db471
JD
175 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
176 {
50842bdc 177 struct bt_stream *stream =
504db471 178 bt_notification_stream_begin_get_stream(notification);
50842bdc 179 struct bt_stream *writer_stream;
504db471
JD
180
181 if (!stream) {
182 goto end;
183 }
184
185 writer_stream = debug_info_stream_begin(debug_it, stream);
186 assert(writer_stream);
187 new_notification = bt_notification_stream_begin_create(
188 writer_stream);
189 assert(new_notification);
190 bt_put(stream);
191 bt_put(writer_stream);
192 break;
193 }
4f45f9bb
JD
194 case BT_NOTIFICATION_TYPE_STREAM_END:
195 {
50842bdc 196 struct bt_stream *stream =
4f45f9bb 197 bt_notification_stream_end_get_stream(notification);
50842bdc 198 struct bt_stream *writer_stream;
4f45f9bb
JD
199
200 if (!stream) {
201 goto end;
202 }
203
204 writer_stream = debug_info_stream_end(debug_it, stream);
205 assert(writer_stream);
206 new_notification = bt_notification_stream_end_create(
207 writer_stream);
208 assert(new_notification);
209 bt_put(stream);
210 bt_put(writer_stream);
211 break;
212 }
617dd674 213 default:
779fc95d
JD
214 new_notification = bt_get(notification);
215 break;
216 }
4f45f9bb
JD
217
218end:
219 return new_notification;
220}
221
222static
90157d89
PP
223struct bt_notification_iterator_next_method_return debug_info_iterator_next(
224 struct bt_private_connection_private_notification_iterator *iterator)
4f45f9bb
JD
225{
226 struct debug_info_iterator *debug_it = NULL;
7b077a9d 227 struct bt_private_component *component = NULL;
4f45f9bb
JD
228 struct debug_info_component *debug_info = NULL;
229 struct bt_notification_iterator *source_it = NULL;
7b077a9d 230 struct bt_notification *notification;
90157d89 231 struct bt_notification_iterator_next_method_return ret = {
7b077a9d
JD
232 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
233 .notification = NULL,
234 };
4f45f9bb 235
90157d89 236 debug_it = bt_private_connection_private_notification_iterator_get_user_data(iterator);
4f45f9bb
JD
237 assert(debug_it);
238
90157d89 239 component = bt_private_connection_private_notification_iterator_get_private_component(iterator);
4f45f9bb 240 assert(component);
7b077a9d 241 debug_info = bt_private_component_get_user_data(component);
4f45f9bb
JD
242 assert(debug_info);
243
244 source_it = debug_it->input_iterator;
245
7b077a9d
JD
246 ret.status = bt_notification_iterator_next(source_it);
247 if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
4f45f9bb
JD
248 goto end;
249 }
250
251 notification = bt_notification_iterator_get_notification(
252 source_it);
253 if (!notification) {
7b077a9d 254 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
4f45f9bb
JD
255 goto end;
256 }
257
7b077a9d 258 ret.notification = handle_notification(debug_info->err, debug_it,
4f45f9bb 259 notification);
7b077a9d 260 assert(ret.notification);
4f45f9bb
JD
261 bt_put(notification);
262
4f45f9bb
JD
263end:
264 bt_put(component);
265 return ret;
266}
267
4f45f9bb 268static
7b077a9d 269enum bt_notification_iterator_status debug_info_iterator_init(
90157d89 270 struct bt_private_connection_private_notification_iterator *iterator,
7b077a9d 271 struct bt_private_port *port)
4f45f9bb
JD
272{
273 enum bt_notification_iterator_status ret =
274 BT_NOTIFICATION_ITERATOR_STATUS_OK;
275 enum bt_notification_iterator_status it_ret;
73d5c1ad 276 enum bt_connection_status conn_status;
7b077a9d
JD
277 struct bt_private_connection *connection = NULL;
278 struct bt_private_component *component =
90157d89 279 bt_private_connection_private_notification_iterator_get_private_component(iterator);
4f45f9bb 280 struct debug_info_iterator *it_data = g_new0(struct debug_info_iterator, 1);
779fc95d 281 struct bt_private_port *input_port;
4f45f9bb
JD
282
283 if (!it_data) {
284 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
285 goto end;
286 }
287
b9d103be 288 input_port = bt_private_component_filter_get_input_private_port_by_name(
779fc95d
JD
289 component, "in");
290 if (!input_port) {
291 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
292 goto end;
293 }
294
7b077a9d 295 connection = bt_private_port_get_private_connection(input_port);
779fc95d
JD
296 bt_put(input_port);
297 if (!connection) {
298 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
299 goto end;
300 }
4f45f9bb 301
73d5c1ad 302 conn_status = bt_private_connection_create_notification_iterator(
617dd674 303 connection, NULL, &it_data->input_iterator);
73d5c1ad
PP
304 if (conn_status != BT_CONNECTION_STATUS_OK) {
305 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
4f45f9bb
JD
306 goto end;
307 }
779fc95d 308
4f45f9bb 309 it_data->debug_info_component = (struct debug_info_component *)
7b077a9d 310 bt_private_component_get_user_data(component);
4f45f9bb
JD
311 it_data->err = it_data->debug_info_component->err;
312 it_data->trace_map = g_hash_table_new_full(g_direct_hash,
313 g_direct_equal, NULL, (GDestroyNotify) unref_trace);
4f45f9bb 314
90157d89 315 it_ret = bt_private_connection_private_notification_iterator_set_user_data(iterator, it_data);
4f45f9bb
JD
316 if (it_ret) {
317 goto end;
318 }
319
320end:
321 bt_put(connection);
779fc95d 322 bt_put(component);
4f45f9bb
JD
323 return ret;
324}
325
326static
327enum bt_component_status init_from_params(
328 struct debug_info_component *debug_info_component,
329 struct bt_value *params)
330{
331 struct bt_value *value = NULL;
332 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
333
334 assert(params);
335
336 value = bt_value_map_get(params, "debug-info-field-name");
337 if (value) {
338 enum bt_value_status value_ret;
339 const char *tmp;
340
341 value_ret = bt_value_string_get(value, &tmp);
342 if (value_ret) {
343 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 344 BT_LOGE_STR("Failed to retrieve debug-info-field-name value. "
b4d4912f 345 "Expecting a string.");
4f45f9bb
JD
346 }
347 strcpy(debug_info_component->arg_debug_info_field_name, tmp);
348 bt_put(value);
349 } else {
350 debug_info_component->arg_debug_info_field_name =
351 malloc(strlen("debug_info") + 1);
352 if (!debug_info_component->arg_debug_info_field_name) {
353 ret = BT_COMPONENT_STATUS_NOMEM;
b4565e8b 354 BT_LOGE_STR("Missing field name.");
6caae98c 355 goto end;
4f45f9bb
JD
356 }
357 sprintf(debug_info_component->arg_debug_info_field_name,
358 "debug_info");
359 }
360 if (ret != BT_COMPONENT_STATUS_OK) {
361 goto end;
362 }
363
364 value = bt_value_map_get(params, "debug-dir");
365 if (value) {
366 enum bt_value_status value_ret;
367
368 value_ret = bt_value_string_get(value,
369 &debug_info_component->arg_debug_dir);
370 if (value_ret) {
371 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 372 BT_LOGE_STR("Failed to retrieve debug-dir value. "
b4d4912f 373 "Expecting a string.");
4f45f9bb
JD
374 }
375 }
376 bt_put(value);
377 if (ret != BT_COMPONENT_STATUS_OK) {
378 goto end;
379 }
380
381 value = bt_value_map_get(params, "target-prefix");
382 if (value) {
383 enum bt_value_status value_ret;
384
385 value_ret = bt_value_string_get(value,
386 &debug_info_component->arg_target_prefix);
387 if (value_ret) {
388 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 389 BT_LOGE_STR("Failed to retrieve target-prefix value. "
b4d4912f 390 "Expecting a string.");
4f45f9bb
JD
391 }
392 }
393 bt_put(value);
394 if (ret != BT_COMPONENT_STATUS_OK) {
395 goto end;
396 }
397
398 value = bt_value_map_get(params, "full-path");
399 if (value) {
400 enum bt_value_status value_ret;
c55a9f58 401 bt_bool bool_val;
4f45f9bb
JD
402
403 value_ret = bt_value_bool_get(value,
c55a9f58 404 &bool_val);
4f45f9bb
JD
405 if (value_ret) {
406 ret = BT_COMPONENT_STATUS_INVALID;
b4565e8b 407 BT_LOGE_STR("Failed to retrieve full-path value. "
b4d4912f 408 "Expecting a boolean.");
4f45f9bb 409 }
c55a9f58
PP
410
411 debug_info_component->arg_full_path = bool_val;
4f45f9bb
JD
412 }
413 bt_put(value);
414 if (ret != BT_COMPONENT_STATUS_OK) {
415 goto end;
416 }
417
418end:
419 return ret;
420}
421
422enum bt_component_status debug_info_component_init(
7b077a9d 423 struct bt_private_component *component, struct bt_value *params,
4f45f9bb
JD
424 UNUSED_VAR void *init_method_data)
425{
426 enum bt_component_status ret;
427 struct debug_info_component *debug_info = create_debug_info_component_data();
428
429 if (!debug_info) {
430 ret = BT_COMPONENT_STATUS_NOMEM;
431 goto end;
432 }
433
7b077a9d 434 ret = bt_private_component_set_user_data(component, debug_info);
4f45f9bb
JD
435 if (ret != BT_COMPONENT_STATUS_OK) {
436 goto error;
437 }
438
147337a3
PP
439 ret = bt_private_component_filter_add_input_private_port(
440 component, "in", NULL, NULL);
441 if (ret != BT_COMPONENT_STATUS_OK) {
779fc95d
JD
442 goto end;
443 }
779fc95d 444
147337a3
PP
445 ret = bt_private_component_filter_add_output_private_port(
446 component, "out", NULL, NULL);
447 if (ret != BT_COMPONENT_STATUS_OK) {
779fc95d
JD
448 goto end;
449 }
779fc95d 450
4f45f9bb
JD
451 ret = init_from_params(debug_info, params);
452end:
453 return ret;
454error:
455 destroy_debug_info_data(debug_info);
456 return ret;
457}
458
52238017
MJ
459#ifndef BT_BUILT_IN_PLUGINS
460BT_PLUGIN_MODULE();
461#endif
462
4f45f9bb 463/* Initialize plug-in entry points. */
456a4476
PP
464BT_PLUGIN_WITH_ID(lttng_utils, "lttng-utils");
465BT_PLUGIN_DESCRIPTION_WITH_ID(lttng_utils, "LTTng utilities");
466BT_PLUGIN_AUTHOR_WITH_ID(lttng_utils, "Julien Desfossez");
467BT_PLUGIN_LICENSE_WITH_ID(lttng_utils, "MIT");
468
469BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID(lttng_utils, debug_info, "debug-info",
470 debug_info_iterator_next);
471BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION_WITH_ID(lttng_utils, debug_info,
472 "Augment compatible events with debugging information.");
473BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD_WITH_ID(lttng_utils,
474 debug_info, debug_info_component_init);
475BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID(lttng_utils,
476 debug_info, destroy_debug_info_component);
477BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD_WITH_ID(
478 lttng_utils, debug_info, debug_info_iterator_init);
479BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_FINALIZE_METHOD_WITH_ID(
480 lttng_utils, debug_info, debug_info_iterator_destroy);
This page took 0.052066 seconds and 4 git commands to generate.