a15bde4e3fad440f2455745fc2cdb17aa6dc803e
[lttng-tools.git] / src / common / mi-lttng.c
1 /*
2 * Copyright (C) 2014 - Jonathan Rajotte <jonathan.r.julien@gmail.com>
3 * - Olivier Cotte <olivier.cotte@polymtl.ca>
4 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <common/config/session-config.h>
22 #include <common/defaults.h>
23 #include <lttng/snapshot-internal.h>
24 #include <lttng/channel.h>
25 #include "mi-lttng.h"
26
27 #include <assert.h>
28
29 #define MI_SCHEMA_MAJOR_VERSION 3
30 #define MI_SCHEMA_MINOR_VERSION 0
31
32 /* Machine interface namespace URI */
33 LTTNG_HIDDEN const char * const mi_lttng_xmlns = "xmlns";
34 LTTNG_HIDDEN const char * const mi_lttng_xmlns_xsi = "xmlns:xsi";
35 LTTNG_HIDDEN const char * const mi_lttng_w3_schema_uri = "http://www.w3.org/2001/XMLSchema-instance";
36 LTTNG_HIDDEN const char * const mi_lttng_schema_location = "xsi:schemaLocation";
37 LTTNG_HIDDEN const char * const mi_lttng_schema_location_uri =
38 DEFAULT_LTTNG_MI_NAMESPACE " "
39 "http://lttng.org/xml/schemas/lttng-mi/" XSTR(MI_SCHEMA_MAJOR_VERSION)
40 "/lttng-mi-" XSTR(MI_SCHEMA_MAJOR_VERSION) "."
41 XSTR(MI_SCHEMA_MINOR_VERSION) ".xsd";
42 LTTNG_HIDDEN const char * const mi_lttng_schema_version = "schemaVersion";
43 LTTNG_HIDDEN const char * const mi_lttng_schema_version_value = XSTR(MI_SCHEMA_MAJOR_VERSION)
44 "." XSTR(MI_SCHEMA_MINOR_VERSION);
45
46 /* Strings related to command */
47 const char * const mi_lttng_element_command = "command";
48 const char * const mi_lttng_element_command_action = "snapshot_action";
49 const char * const mi_lttng_element_command_add_context = "add-context";
50 const char * const mi_lttng_element_command_create = "create";
51 const char * const mi_lttng_element_command_destroy = "destroy";
52 const char * const mi_lttng_element_command_disable_channel = "disable-channel";
53 const char * const mi_lttng_element_command_disable_event = "disable-event";
54 const char * const mi_lttng_element_command_enable_channels = "enable-channel";
55 const char * const mi_lttng_element_command_enable_event = "enable-event";
56 const char * const mi_lttng_element_command_list = "list";
57 const char * const mi_lttng_element_command_load = "load";
58 LTTNG_HIDDEN const char * const mi_lttng_element_command_metadata = "metadata";
59 LTTNG_HIDDEN const char * const mi_lttng_element_command_metadata_action = "metadata_action";
60 LTTNG_HIDDEN const char * const mi_lttng_element_command_regenerate = "regenerate";
61 LTTNG_HIDDEN const char * const mi_lttng_element_command_regenerate_action = "regenerate_action";
62 const char * const mi_lttng_element_command_name = "name";
63 const char * const mi_lttng_element_command_output = "output";
64 const char * const mi_lttng_element_command_save = "save";
65 const char * const mi_lttng_element_command_set_session = "set-session";
66 const char * const mi_lttng_element_command_snapshot = "snapshot";
67 const char * const mi_lttng_element_command_snapshot_add = "add_snapshot";
68 const char * const mi_lttng_element_command_snapshot_del = "del_snapshot";
69 const char * const mi_lttng_element_command_snapshot_list = "list_snapshot";
70 const char * const mi_lttng_element_command_snapshot_record = "record_snapshot";
71 const char * const mi_lttng_element_command_start = "start";
72 const char * const mi_lttng_element_command_stop = "stop";
73 const char * const mi_lttng_element_command_success = "success";
74 const char * const mi_lttng_element_command_track = "track";
75 const char * const mi_lttng_element_command_untrack = "untrack";
76 const char * const mi_lttng_element_command_version = "version";
77
78 /* Strings related to version command */
79 const char * const mi_lttng_element_version = "version";
80 const char * const mi_lttng_element_version_commit = "commit";
81 const char * const mi_lttng_element_version_description = "description";
82 const char * const mi_lttng_element_version_license = "license";
83 const char * const mi_lttng_element_version_major = "major";
84 const char * const mi_lttng_element_version_minor = "minor";
85 const char * const mi_lttng_element_version_patch_level = "patchLevel";
86 const char * const mi_lttng_element_version_str = "string";
87 const char * const mi_lttng_element_version_web = "url";
88
89 /* String related to a lttng_event_field */
90 const char * const mi_lttng_element_event_field = "event_field";
91 const char * const mi_lttng_element_event_fields = "event_fields";
92
93 /* String related to lttng_event_perf_counter_ctx */
94 const char * const mi_lttng_element_perf_counter_context = "perf";
95
96 /* Strings related to pid */
97 const char * const mi_lttng_element_pid_id = "id";
98
99 /* Strings related to save command */
100 const char * const mi_lttng_element_save = "save";
101
102 /* Strings related to load command */
103 const char * const mi_lttng_element_load = "load";
104
105 /* General elements of mi_lttng */
106 const char * const mi_lttng_element_empty = "";
107 const char * const mi_lttng_element_id = "id";
108 const char * const mi_lttng_element_nowrite = "nowrite";
109 const char * const mi_lttng_element_success = "success";
110 const char * const mi_lttng_element_type_enum = "ENUM";
111 const char * const mi_lttng_element_type_float = "FLOAT";
112 const char * const mi_lttng_element_type_integer = "INTEGER";
113 const char * const mi_lttng_element_type_other = "OTHER";
114 const char * const mi_lttng_element_type_string = "STRING";
115
116 /* String related to loglevel */
117 const char * const mi_lttng_loglevel_str_alert = "TRACE_ALERT";
118 const char * const mi_lttng_loglevel_str_crit = "TRACE_CRIT";
119 const char * const mi_lttng_loglevel_str_debug = "TRACE_DEBUG";
120 const char * const mi_lttng_loglevel_str_debug_function = "TRACE_DEBUG_FUNCTION";
121 const char * const mi_lttng_loglevel_str_debug_line = "TRACE_DEBUG_LINE";
122 const char * const mi_lttng_loglevel_str_debug_module = "TRACE_DEBUG_MODULE";
123 const char * const mi_lttng_loglevel_str_debug_process = "TRACE_DEBUG_PROCESS";
124 const char * const mi_lttng_loglevel_str_debug_program = "TRACE_DEBUG_PROGRAM";
125 const char * const mi_lttng_loglevel_str_debug_system = "TRACE_DEBUG_SYSTEM";
126 const char * const mi_lttng_loglevel_str_debug_unit = "TRACE_DEBUG_UNIT";
127 const char * const mi_lttng_loglevel_str_emerg = "TRACE_EMERG";
128 const char * const mi_lttng_loglevel_str_err = "TRACE_ERR";
129 const char * const mi_lttng_loglevel_str_info = "TRACE_INFO";
130 const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
131 const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
132 const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
133
134 /* String related to loglevel JUL */
135 const char * const mi_lttng_loglevel_str_jul_all = "JUL_ALL";
136 const char * const mi_lttng_loglevel_str_jul_config = "JUL_CONFIG";
137 const char * const mi_lttng_loglevel_str_jul_fine = "JUL_FINE";
138 const char * const mi_lttng_loglevel_str_jul_finer = "JUL_FINER";
139 const char * const mi_lttng_loglevel_str_jul_finest = "JUL_FINEST";
140 const char * const mi_lttng_loglevel_str_jul_info = "JUL_INFO";
141 const char * const mi_lttng_loglevel_str_jul_off = "JUL_OFF";
142 const char * const mi_lttng_loglevel_str_jul_severe = "JUL_SEVERE";
143 const char * const mi_lttng_loglevel_str_jul_warning = "JUL_WARNING";
144
145 /* String related to loglevel LOG4J */
146 const char * const mi_lttng_loglevel_str_log4j_off = "LOG4J_OFF";
147 const char * const mi_lttng_loglevel_str_log4j_fatal = "LOG4J_FATAL";
148 const char * const mi_lttng_loglevel_str_log4j_error = "LOG4J_ERROR";
149 const char * const mi_lttng_loglevel_str_log4j_warn = "LOG4J_WARN";
150 const char * const mi_lttng_loglevel_str_log4j_info = "LOG4J_INFO";
151 const char * const mi_lttng_loglevel_str_log4j_debug = "LOG4J_DEBUG";
152 const char * const mi_lttng_loglevel_str_log4j_trace = "LOG4J_TRACE";
153 const char * const mi_lttng_loglevel_str_log4j_all = "LOG4J_ALL";
154
155 /* String related to loglevel Python */
156 const char * const mi_lttng_loglevel_str_python_critical = "PYTHON_CRITICAL";
157 const char * const mi_lttng_loglevel_str_python_error = "PYTHON_ERROR";
158 const char * const mi_lttng_loglevel_str_python_warning = "PYTHON_WARNING";
159 const char * const mi_lttng_loglevel_str_python_info = "PYTHON_INFO";
160 const char * const mi_lttng_loglevel_str_python_debug = "PYTHON_DEBUG";
161 const char * const mi_lttng_loglevel_str_python_notset = "PYTHON_NOTSET";
162
163 /* String related to loglevel type */
164 const char * const mi_lttng_loglevel_type_all = "ALL";
165 const char * const mi_lttng_loglevel_type_range = "RANGE";
166 const char * const mi_lttng_loglevel_type_single = "SINGLE";
167 const char * const mi_lttng_loglevel_type_unknown = "UNKNOWN";
168
169 /* String related to a lttng_snapshot_output */
170 const char * const mi_lttng_element_snapshot_ctrl_url = "ctrl_url";
171 const char * const mi_lttng_element_snapshot_data_url = "data_url";
172 const char * const mi_lttng_element_snapshot_max_size = "max_size";
173 const char * const mi_lttng_element_snapshot_n_ptr = "n_ptr";
174 const char * const mi_lttng_element_snapshot_session_name = "session_name";
175 const char * const mi_lttng_element_snapshots = "snapshots";
176
177 /* String related to track/untrack command */
178 const char * const mi_lttng_element_track_untrack_all_wildcard = "*";
179
180 /* Deprecated symbols preserved for ABI compatibility. */
181 const char * const mi_lttng_context_type_perf_counter;
182 const char * const mi_lttng_context_type_perf_cpu_counter;
183 const char * const mi_lttng_context_type_perf_thread_counter;
184 const char * const mi_lttng_element_track_untrack_pid_target;
185 const char * const mi_lttng_element_track_untrack_targets;
186 const char * const mi_lttng_element_calibrate;
187 const char * const mi_lttng_element_calibrate_function;
188 const char * const mi_lttng_element_command_calibrate;
189
190 /* This is a merge of jul loglevel and regular loglevel
191 * Those should never overlap by definition
192 * (see struct lttng_event loglevel)
193 */
194 LTTNG_HIDDEN
195 const char *mi_lttng_loglevel_string(int value, enum lttng_domain_type domain)
196 {
197 switch (domain) {
198 case LTTNG_DOMAIN_KERNEL:
199 case LTTNG_DOMAIN_UST:
200 switch (value) {
201 case -1:
202 return mi_lttng_element_empty;
203 case LTTNG_LOGLEVEL_EMERG:
204 return mi_lttng_loglevel_str_emerg;
205 case LTTNG_LOGLEVEL_ALERT:
206 return mi_lttng_loglevel_str_alert;
207 case LTTNG_LOGLEVEL_CRIT:
208 return mi_lttng_loglevel_str_crit;
209 case LTTNG_LOGLEVEL_ERR:
210 return mi_lttng_loglevel_str_err;
211 case LTTNG_LOGLEVEL_WARNING:
212 return mi_lttng_loglevel_str_warning;
213 case LTTNG_LOGLEVEL_NOTICE:
214 return mi_lttng_loglevel_str_notice;
215 case LTTNG_LOGLEVEL_INFO:
216 return mi_lttng_loglevel_str_info;
217 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
218 return mi_lttng_loglevel_str_debug_system;
219 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
220 return mi_lttng_loglevel_str_debug_program;
221 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
222 return mi_lttng_loglevel_str_debug_process;
223 case LTTNG_LOGLEVEL_DEBUG_MODULE:
224 return mi_lttng_loglevel_str_debug_module;
225 case LTTNG_LOGLEVEL_DEBUG_UNIT:
226 return mi_lttng_loglevel_str_debug_unit;
227 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
228 return mi_lttng_loglevel_str_debug_function;
229 case LTTNG_LOGLEVEL_DEBUG_LINE:
230 return mi_lttng_loglevel_str_debug_line;
231 case LTTNG_LOGLEVEL_DEBUG:
232 return mi_lttng_loglevel_str_debug;
233 default:
234 return mi_lttng_loglevel_str_unknown;
235 }
236 break;
237 case LTTNG_DOMAIN_LOG4J:
238 switch (value) {
239 case -1:
240 return mi_lttng_element_empty;
241 case LTTNG_LOGLEVEL_LOG4J_OFF:
242 return mi_lttng_loglevel_str_log4j_off;
243 case LTTNG_LOGLEVEL_LOG4J_FATAL:
244 return mi_lttng_loglevel_str_log4j_fatal;
245 case LTTNG_LOGLEVEL_LOG4J_ERROR:
246 return mi_lttng_loglevel_str_log4j_error;
247 case LTTNG_LOGLEVEL_LOG4J_WARN:
248 return mi_lttng_loglevel_str_log4j_warn;
249 case LTTNG_LOGLEVEL_LOG4J_INFO:
250 return mi_lttng_loglevel_str_log4j_info;
251 case LTTNG_LOGLEVEL_LOG4J_DEBUG:
252 return mi_lttng_loglevel_str_log4j_debug;
253 case LTTNG_LOGLEVEL_LOG4J_TRACE:
254 return mi_lttng_loglevel_str_log4j_trace;
255 case LTTNG_LOGLEVEL_LOG4J_ALL:
256 return mi_lttng_loglevel_str_log4j_all;
257 default:
258 return mi_lttng_loglevel_str_unknown;
259 }
260 break;
261 case LTTNG_DOMAIN_JUL:
262 switch (value) {
263 case -1:
264 return mi_lttng_element_empty;
265 case LTTNG_LOGLEVEL_JUL_OFF:
266 return mi_lttng_loglevel_str_jul_off;
267 case LTTNG_LOGLEVEL_JUL_SEVERE:
268 return mi_lttng_loglevel_str_jul_severe;
269 case LTTNG_LOGLEVEL_JUL_WARNING:
270 return mi_lttng_loglevel_str_jul_warning;
271 case LTTNG_LOGLEVEL_JUL_INFO:
272 return mi_lttng_loglevel_str_jul_info;
273 case LTTNG_LOGLEVEL_JUL_CONFIG:
274 return mi_lttng_loglevel_str_jul_config;
275 case LTTNG_LOGLEVEL_JUL_FINE:
276 return mi_lttng_loglevel_str_jul_fine;
277 case LTTNG_LOGLEVEL_JUL_FINER:
278 return mi_lttng_loglevel_str_jul_finer;
279 case LTTNG_LOGLEVEL_JUL_FINEST:
280 return mi_lttng_loglevel_str_jul_finest;
281 case LTTNG_LOGLEVEL_JUL_ALL:
282 return mi_lttng_loglevel_str_jul_all;
283 default:
284 return mi_lttng_loglevel_str_unknown;
285 }
286 break;
287 case LTTNG_DOMAIN_PYTHON:
288 switch (value) {
289 case LTTNG_LOGLEVEL_PYTHON_CRITICAL:
290 return mi_lttng_loglevel_str_python_critical;
291 case LTTNG_LOGLEVEL_PYTHON_ERROR:
292 return mi_lttng_loglevel_str_python_error;
293 case LTTNG_LOGLEVEL_PYTHON_WARNING:
294 return mi_lttng_loglevel_str_python_warning;
295 case LTTNG_LOGLEVEL_PYTHON_INFO:
296 return mi_lttng_loglevel_str_python_info;
297 case LTTNG_LOGLEVEL_PYTHON_DEBUG:
298 return mi_lttng_loglevel_str_python_debug;
299 case LTTNG_LOGLEVEL_PYTHON_NOTSET:
300 return mi_lttng_loglevel_str_python_notset;
301 default:
302 return mi_lttng_loglevel_str_unknown;
303 }
304 break;
305 default:
306 return mi_lttng_loglevel_str_unknown;
307 }
308 }
309
310 LTTNG_HIDDEN
311 const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value)
312 {
313 switch (value) {
314 case LTTNG_EVENT_LOGLEVEL_ALL:
315 return mi_lttng_loglevel_type_all;
316 case LTTNG_EVENT_LOGLEVEL_RANGE:
317 return mi_lttng_loglevel_type_range;
318 case LTTNG_EVENT_LOGLEVEL_SINGLE:
319 return mi_lttng_loglevel_type_single;
320 default:
321 return mi_lttng_loglevel_type_unknown;
322 }
323 }
324
325 LTTNG_HIDDEN
326 const char *mi_lttng_eventtype_string(enum lttng_event_type value)
327 {
328 switch (value) {
329 case LTTNG_EVENT_ALL:
330 return config_event_type_all;
331 case LTTNG_EVENT_TRACEPOINT:
332 return config_event_type_tracepoint;
333 case LTTNG_EVENT_PROBE:
334 return config_event_type_probe;
335 case LTTNG_EVENT_FUNCTION:
336 return config_event_type_function;
337 case LTTNG_EVENT_FUNCTION_ENTRY:
338 return config_event_type_function_entry;
339 case LTTNG_EVENT_SYSCALL:
340 return config_event_type_syscall;
341 case LTTNG_EVENT_NOOP:
342 return config_event_type_noop;
343 default:
344 return mi_lttng_element_empty;
345 }
346 }
347
348 static
349 const char *mi_lttng_event_contexttype_string(enum lttng_event_context_type val)
350 {
351 switch (val) {
352 case LTTNG_EVENT_CONTEXT_PID:
353 return config_event_context_pid;
354 case LTTNG_EVENT_CONTEXT_PROCNAME:
355 return config_event_context_procname;
356 case LTTNG_EVENT_CONTEXT_PRIO:
357 return config_event_context_prio;
358 case LTTNG_EVENT_CONTEXT_NICE:
359 return config_event_context_nice;
360 case LTTNG_EVENT_CONTEXT_VPID:
361 return config_event_context_vpid;
362 case LTTNG_EVENT_CONTEXT_TID:
363 return config_event_context_tid;
364 case LTTNG_EVENT_CONTEXT_VTID:
365 return config_event_context_vtid;
366 case LTTNG_EVENT_CONTEXT_PPID:
367 return config_event_context_ppid;
368 case LTTNG_EVENT_CONTEXT_VPPID:
369 return config_event_context_vppid;
370 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
371 return config_event_context_pthread_id;
372 case LTTNG_EVENT_CONTEXT_HOSTNAME:
373 return config_event_context_hostname;
374 case LTTNG_EVENT_CONTEXT_IP:
375 return config_event_context_ip;
376 default:
377 return NULL;
378 }
379 }
380
381 LTTNG_HIDDEN
382 const char *mi_lttng_eventfieldtype_string(enum lttng_event_field_type val)
383 {
384 switch (val) {
385 case(LTTNG_EVENT_FIELD_INTEGER):
386 return mi_lttng_element_type_integer;
387 case(LTTNG_EVENT_FIELD_ENUM):
388 return mi_lttng_element_type_enum;
389 case(LTTNG_EVENT_FIELD_FLOAT):
390 return mi_lttng_element_type_float;
391 case(LTTNG_EVENT_FIELD_STRING):
392 return mi_lttng_element_type_string;
393 default:
394 return mi_lttng_element_type_other;
395 }
396 }
397
398 LTTNG_HIDDEN
399 const char *mi_lttng_domaintype_string(enum lttng_domain_type value)
400 {
401 /* Note: This is a *duplicate* of get_domain_str from bin/lttng/utils.c */
402 switch (value) {
403 case LTTNG_DOMAIN_KERNEL:
404 return config_domain_type_kernel;
405 case LTTNG_DOMAIN_UST:
406 return config_domain_type_ust;
407 case LTTNG_DOMAIN_JUL:
408 return config_domain_type_jul;
409 case LTTNG_DOMAIN_LOG4J:
410 return config_domain_type_log4j;
411 case LTTNG_DOMAIN_PYTHON:
412 return config_domain_type_python;
413 default:
414 /* Should not have an unknown domain */
415 assert(0);
416 return NULL;
417 }
418 }
419
420 LTTNG_HIDDEN
421 const char *mi_lttng_buffertype_string(enum lttng_buffer_type value)
422 {
423 switch (value) {
424 case LTTNG_BUFFER_PER_PID:
425 return config_buffer_type_per_pid;
426 case LTTNG_BUFFER_PER_UID:
427 return config_buffer_type_per_uid;
428 case LTTNG_BUFFER_GLOBAL:
429 return config_buffer_type_global;
430 default:
431 /* Should not have an unknow buffer type */
432 assert(0);
433 return NULL;
434 }
435 }
436
437 LTTNG_HIDDEN
438 struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
439 {
440 struct mi_writer *mi_writer;
441
442 mi_writer = zmalloc(sizeof(struct mi_writer));
443 if (!mi_writer) {
444 PERROR("zmalloc mi_writer_create");
445 goto end;
446 }
447 if (mi_output_type == LTTNG_MI_XML) {
448 mi_writer->writer = config_writer_create(fd_output, 0);
449 if (!mi_writer->writer) {
450 goto err_destroy;
451 }
452 mi_writer->type = LTTNG_MI_XML;
453 } else {
454 goto err_destroy;
455 }
456
457 end:
458 return mi_writer;
459
460 err_destroy:
461 free(mi_writer);
462 return NULL;
463 }
464
465 LTTNG_HIDDEN
466 int mi_lttng_writer_destroy(struct mi_writer *writer)
467 {
468 int ret;
469
470 if (!writer) {
471 ret = -EINVAL;
472 goto end;
473 }
474
475 ret = config_writer_destroy(writer->writer);
476 if (ret < 0) {
477 goto end;
478 }
479
480 free(writer);
481 end:
482 return ret;
483 }
484
485 LTTNG_HIDDEN
486 int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
487 {
488 int ret;
489
490 /*
491 * A command is always the MI's root node, it must declare the current
492 * namespace and schema URIs and the schema's version.
493 */
494 ret = config_writer_open_element(writer->writer,
495 mi_lttng_element_command);
496 if (ret) {
497 goto end;
498 }
499
500 ret = config_writer_write_attribute(writer->writer,
501 mi_lttng_xmlns, DEFAULT_LTTNG_MI_NAMESPACE);
502 if (ret) {
503 goto end;
504 }
505
506 ret = config_writer_write_attribute(writer->writer,
507 mi_lttng_xmlns_xsi, mi_lttng_w3_schema_uri);
508 if (ret) {
509 goto end;
510 }
511
512 ret = config_writer_write_attribute(writer->writer,
513 mi_lttng_schema_location,
514 mi_lttng_schema_location_uri);
515 if (ret) {
516 goto end;
517 }
518
519 ret = config_writer_write_attribute(writer->writer,
520 mi_lttng_schema_version,
521 mi_lttng_schema_version_value);
522 if (ret) {
523 goto end;
524 }
525
526 ret = mi_lttng_writer_write_element_string(writer,
527 mi_lttng_element_command_name, command);
528 end:
529 return ret;
530 }
531
532 LTTNG_HIDDEN
533 int mi_lttng_writer_command_close(struct mi_writer *writer)
534 {
535 return mi_lttng_writer_close_element(writer);
536 }
537
538 LTTNG_HIDDEN
539 int mi_lttng_writer_open_element(struct mi_writer *writer,
540 const char *element_name)
541 {
542 return config_writer_open_element(writer->writer, element_name);
543 }
544
545 LTTNG_HIDDEN
546 int mi_lttng_writer_close_element(struct mi_writer *writer)
547 {
548 return config_writer_close_element(writer->writer);
549 }
550
551 LTTNG_HIDDEN
552 int mi_lttng_close_multi_element(struct mi_writer *writer,
553 unsigned int nb_element)
554 {
555 int ret, i;
556
557 if (nb_element < 1) {
558 ret = 0;
559 goto end;
560 }
561 for (i = 0; i < nb_element; i++) {
562 ret = mi_lttng_writer_close_element(writer);
563 if (ret) {
564 goto end;
565 }
566 }
567 end:
568 return ret;
569 }
570
571 LTTNG_HIDDEN
572 int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
573 const char *element_name, uint64_t value)
574 {
575 return config_writer_write_element_unsigned_int(writer->writer,
576 element_name, value);
577 }
578
579 LTTNG_HIDDEN
580 int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
581 const char *element_name, int64_t value)
582 {
583 return config_writer_write_element_signed_int(writer->writer,
584 element_name, value);
585 }
586
587 LTTNG_HIDDEN
588 int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
589 const char *element_name, int value)
590 {
591 return config_writer_write_element_bool(writer->writer,
592 element_name, value);
593 }
594
595 LTTNG_HIDDEN
596 int mi_lttng_writer_write_element_string(struct mi_writer *writer,
597 const char *element_name, const char *value)
598 {
599 return config_writer_write_element_string(writer->writer,
600 element_name, value);
601 }
602
603 LTTNG_HIDDEN
604 int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
605 const char *lttng_description, const char *lttng_license)
606 {
607 int ret;
608
609 /* Open version */
610 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
611 if (ret) {
612 goto end;
613 }
614
615 /* Version string (contain info like rc etc.) */
616 ret = mi_lttng_writer_write_element_string(writer,
617 mi_lttng_element_version_str, version->version);
618 if (ret) {
619 goto end;
620 }
621
622 /* Major version number */
623 ret = mi_lttng_writer_write_element_unsigned_int(writer,
624 mi_lttng_element_version_major, version->version_major);
625 if (ret) {
626 goto end;
627 }
628
629 /* Minor version number */
630 ret = mi_lttng_writer_write_element_unsigned_int(writer,
631 mi_lttng_element_version_minor, version->version_minor);
632 if (ret) {
633 goto end;
634 }
635
636 /* Commit version number */
637 ret = mi_lttng_writer_write_element_string(writer,
638 mi_lttng_element_version_commit, version->version_commit);
639 if (ret) {
640 goto end;
641 }
642
643 /* Patch number */
644 ret = mi_lttng_writer_write_element_unsigned_int(writer,
645 mi_lttng_element_version_patch_level, version->version_patchlevel);
646 if (ret) {
647 goto end;
648 }
649
650 /* Name of the version */
651 ret = mi_lttng_writer_write_element_string(writer,
652 config_element_name, version->version_name);
653 if (ret) {
654 goto end;
655 }
656
657 /* Description mostly related to beer... */
658 ret = mi_lttng_writer_write_element_string(writer,
659 mi_lttng_element_version_description, lttng_description);
660 if (ret) {
661 goto end;
662 }
663
664 /* url */
665 ret = mi_lttng_writer_write_element_string(writer,
666 mi_lttng_element_version_web, version->package_url);
667 if (ret) {
668 goto end;
669 }
670
671 /* License: free as in free beer...no...*speech* */
672 ret = mi_lttng_writer_write_element_string(writer,
673 mi_lttng_element_version_license, lttng_license);
674 if (ret) {
675 goto end;
676 }
677
678 /* Close version element */
679 ret = mi_lttng_writer_close_element(writer);
680
681 end:
682 return ret;
683 }
684
685 LTTNG_HIDDEN
686 int mi_lttng_sessions_open(struct mi_writer *writer)
687 {
688 return mi_lttng_writer_open_element(writer, config_element_sessions);
689 }
690
691 LTTNG_HIDDEN
692 int mi_lttng_session(struct mi_writer *writer,
693 struct lttng_session *session, int is_open)
694 {
695 int ret;
696
697 assert(session);
698
699 /* Open sessions element */
700 ret = mi_lttng_writer_open_element(writer,
701 config_element_session);
702 if (ret) {
703 goto end;
704 }
705
706 /* Name of the session */
707 ret = mi_lttng_writer_write_element_string(writer,
708 config_element_name, session->name);
709 if (ret) {
710 goto end;
711 }
712
713 /* Path */
714 ret = mi_lttng_writer_write_element_string(writer,
715 config_element_path, session->path);
716 if (ret) {
717 goto end;
718 }
719
720 /* Enabled ? */
721 ret = mi_lttng_writer_write_element_bool(writer,
722 config_element_enabled, session->enabled);
723 if (ret) {
724 goto end;
725 }
726
727 /* Snapshot mode */
728 ret = mi_lttng_writer_write_element_unsigned_int(writer,
729 config_element_snapshot_mode, session->snapshot_mode);
730 if (ret) {
731 goto end;
732 }
733
734 /* Live timer interval in usec */
735 ret = mi_lttng_writer_write_element_unsigned_int(writer,
736 config_element_live_timer_interval,
737 session->live_timer_interval);
738 if (ret) {
739 goto end;
740 }
741
742 if (!is_open) {
743 /* Closing session element */
744 ret = mi_lttng_writer_close_element(writer);
745 }
746 end:
747 return ret;
748
749 }
750
751 LTTNG_HIDDEN
752 int mi_lttng_domains_open(struct mi_writer *writer)
753 {
754 return mi_lttng_writer_open_element(writer, config_element_domains);
755 }
756
757 LTTNG_HIDDEN
758 int mi_lttng_domain(struct mi_writer *writer,
759 struct lttng_domain *domain, int is_open)
760 {
761 int ret = 0;
762 const char *str_domain;
763 const char *str_buffer;
764
765 assert(domain);
766
767 /* Open domain element */
768 ret = mi_lttng_writer_open_element(writer, config_element_domain);
769 if (ret) {
770 goto end;
771 }
772
773 /* Domain Type */
774 str_domain = mi_lttng_domaintype_string(domain->type);
775 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
776 str_domain);
777 if (ret) {
778 goto end;
779 }
780
781 /* Buffer Type */
782 str_buffer= mi_lttng_buffertype_string(domain->buf_type);
783 ret = mi_lttng_writer_write_element_string(writer,
784 config_element_buffer_type, str_buffer);
785 if (ret) {
786 goto end;
787 }
788
789 /* TODO: union attr
790 * This union is not currently used and was added for
791 * future ust domain support.
792 * Date: 25-06-2014
793 * */
794
795 if (!is_open) {
796 /* Closing domain element */
797 ret = mi_lttng_writer_close_element(writer);
798 }
799
800 end:
801 return ret;
802
803 }
804
805 LTTNG_HIDDEN
806 int mi_lttng_channels_open(struct mi_writer *writer)
807 {
808 return mi_lttng_writer_open_element(writer, config_element_channels);
809 }
810
811 LTTNG_HIDDEN
812 int mi_lttng_channel(struct mi_writer *writer,
813 struct lttng_channel *channel, int is_open)
814 {
815 int ret = 0;
816
817 assert(channel);
818
819 /* Opening channel element */
820 ret = mi_lttng_writer_open_element(writer, config_element_channel);
821 if (ret) {
822 goto end;
823 }
824
825 /* Name */
826 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
827 channel->name);
828 if (ret) {
829 goto end;
830 }
831
832 /* Enabled ? */
833 ret = mi_lttng_writer_write_element_bool(writer,
834 config_element_enabled, channel->enabled);
835 if (ret) {
836 goto end;
837 }
838
839 /* Attribute */
840 ret = mi_lttng_channel_attr(writer, &channel->attr);
841 if (ret) {
842 goto end;
843 }
844
845 if (!is_open) {
846 /* Closing channel element */
847 ret = mi_lttng_writer_close_element(writer);
848 if (ret) {
849 goto end;
850 }
851 }
852 end:
853 return ret;
854 }
855
856 LTTNG_HIDDEN
857 int mi_lttng_channel_attr(struct mi_writer *writer,
858 struct lttng_channel_attr *attr)
859 {
860 int ret = 0;
861 struct lttng_channel *chan = caa_container_of(attr,
862 struct lttng_channel, attr);
863 uint64_t discarded_events, lost_packets;
864
865 assert(attr);
866
867 ret = lttng_channel_get_discarded_event_count(chan, &discarded_events);
868 if (ret) {
869 goto end;
870 }
871
872 ret = lttng_channel_get_lost_packet_count(chan, &lost_packets);
873 if (ret) {
874 goto end;
875 }
876
877 /* Opening Attributes */
878 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
879 if (ret) {
880 goto end;
881 }
882
883 /* Overwrite */
884 ret = mi_lttng_writer_write_element_string(writer,
885 config_element_overwrite_mode,
886 attr->overwrite ? config_overwrite_mode_overwrite :
887 config_overwrite_mode_discard);
888 if (ret) {
889 goto end;
890 }
891
892 /* Sub buffer size in byte */
893 ret = mi_lttng_writer_write_element_unsigned_int(writer,
894 config_element_subbuf_size, attr->subbuf_size);
895 if (ret) {
896 goto end;
897 }
898
899 /* Number of subbuffer (power of two) */
900 ret = mi_lttng_writer_write_element_unsigned_int(writer,
901 config_element_num_subbuf,
902 attr->num_subbuf);
903 if (ret) {
904 goto end;
905 }
906
907 /* Switch timer interval in usec */
908 ret = mi_lttng_writer_write_element_unsigned_int(writer,
909 config_element_switch_timer_interval,
910 attr->switch_timer_interval);
911 if (ret) {
912 goto end;
913 }
914
915 /* Read timer interval in usec */
916 ret = mi_lttng_writer_write_element_unsigned_int(writer,
917 config_element_read_timer_interval,
918 attr->read_timer_interval);
919 if (ret) {
920 goto end;
921 }
922
923 /* Event output */
924 ret = mi_lttng_writer_write_element_string(writer,
925 config_element_output_type,
926 attr->output == LTTNG_EVENT_SPLICE ?
927 config_output_type_splice : config_output_type_mmap);
928 if (ret) {
929 goto end;
930 }
931
932 /* Tracefile size in bytes */
933 ret = mi_lttng_writer_write_element_unsigned_int(writer,
934 config_element_tracefile_size, attr->tracefile_size);
935 if (ret) {
936 goto end;
937 }
938
939 /* Count of tracefiles */
940 ret = mi_lttng_writer_write_element_unsigned_int(writer,
941 config_element_tracefile_count,
942 attr->tracefile_count);
943 if (ret) {
944 goto end;
945 }
946
947 /* Live timer interval in usec*/
948 ret = mi_lttng_writer_write_element_unsigned_int(writer,
949 config_element_live_timer_interval,
950 attr->live_timer_interval);
951 if (ret) {
952 goto end;
953 }
954
955 /* Discarded events */
956 ret = mi_lttng_writer_write_element_unsigned_int(writer,
957 config_element_discarded_events,
958 discarded_events);
959 if (ret) {
960 goto end;
961 }
962
963 /* Lost packets */
964 ret = mi_lttng_writer_write_element_unsigned_int(writer,
965 config_element_lost_packets,
966 lost_packets);
967 if (ret) {
968 goto end;
969 }
970
971 /* Closing attributes */
972 ret = mi_lttng_writer_close_element(writer);
973 if (ret) {
974 goto end;
975 }
976 end:
977 return ret;
978
979 }
980
981 LTTNG_HIDDEN
982 int mi_lttng_event_common_attributes(struct mi_writer *writer,
983 struct lttng_event *event)
984 {
985 int ret;
986 const char *filter_expression;
987
988 /* Open event element */
989 ret = mi_lttng_writer_open_element(writer, config_element_event);
990 if (ret) {
991 goto end;
992 }
993
994 /* Event name */
995 ret = mi_lttng_writer_write_element_string(writer,
996 config_element_name, event->name);
997 if (ret) {
998 goto end;
999 }
1000
1001 /* Event type */
1002 ret = mi_lttng_writer_write_element_string(writer,
1003 config_element_type, mi_lttng_eventtype_string(event->type));
1004 if (ret) {
1005 goto end;
1006 }
1007
1008 /* Is event enabled */
1009 ret = mi_lttng_writer_write_element_bool(writer,
1010 config_element_enabled, event->enabled);
1011 if (ret) {
1012 goto end;
1013 }
1014
1015 /* Event filter expression */
1016 ret = lttng_event_get_filter_expression(event, &filter_expression);
1017 if (ret) {
1018 goto end;
1019 }
1020
1021 if (filter_expression) {
1022 ret = mi_lttng_writer_write_element_string(writer,
1023 config_element_filter_expression,
1024 filter_expression);
1025 if (ret) {
1026 goto end;
1027 }
1028 }
1029
1030 end:
1031 return ret;
1032 }
1033
1034 static int write_event_exclusions(struct mi_writer *writer,
1035 struct lttng_event *event)
1036 {
1037 int i;
1038 int ret;
1039 int exclusion_count;
1040
1041 /* Open event exclusions */
1042 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
1043 if (ret) {
1044 goto end;
1045 }
1046
1047 exclusion_count = lttng_event_get_exclusion_name_count(event);
1048 if (exclusion_count < 0) {
1049 ret = exclusion_count;
1050 goto end;
1051 }
1052
1053 for (i = 0; i < exclusion_count; i++) {
1054 const char *name;
1055
1056 ret = lttng_event_get_exclusion_name(event, i, &name);
1057 if (ret) {
1058 /* Close exclusions */
1059 mi_lttng_writer_close_element(writer);
1060 goto end;
1061 }
1062
1063 ret = mi_lttng_writer_write_element_string(writer,
1064 config_element_exclusion, name);
1065 if (ret) {
1066 /* Close exclusions */
1067 mi_lttng_writer_close_element(writer);
1068 goto end;
1069 }
1070 }
1071
1072 /* Close exclusions */
1073 ret = mi_lttng_writer_close_element(writer);
1074
1075 end:
1076 return ret;
1077 }
1078
1079 LTTNG_HIDDEN
1080 int mi_lttng_event_tracepoint_loglevel(struct mi_writer *writer,
1081 struct lttng_event *event, enum lttng_domain_type domain)
1082 {
1083 int ret;
1084
1085 /* Event loglevel */
1086 ret = mi_lttng_writer_write_element_string(writer,
1087 config_element_loglevel,
1088 mi_lttng_loglevel_string(event->loglevel, domain));
1089 if (ret) {
1090 goto end;
1091 }
1092
1093 /* Log level type */
1094 ret = mi_lttng_writer_write_element_string(writer,
1095 config_element_loglevel_type,
1096 mi_lttng_logleveltype_string(event->loglevel_type));
1097 if (ret) {
1098 goto end;
1099 }
1100
1101 /* Event exclusions */
1102 ret = write_event_exclusions(writer, event);
1103
1104 end:
1105 return ret;
1106 }
1107
1108 LTTNG_HIDDEN
1109 int mi_lttng_event_tracepoint_no_loglevel(struct mi_writer *writer,
1110 struct lttng_event *event)
1111 {
1112 /* event exclusion filter */
1113 return write_event_exclusions(writer, event);
1114 }
1115
1116 LTTNG_HIDDEN
1117 int mi_lttng_event_function_probe(struct mi_writer *writer,
1118 struct lttng_event *event)
1119 {
1120 int ret;
1121
1122 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1123 if (ret) {
1124 goto end;
1125 }
1126
1127 ret = mi_lttng_writer_open_element(writer, config_element_probe_attributes);
1128 if (ret) {
1129 goto end;
1130 }
1131
1132 if (event->attr.probe.addr != 0) {
1133 /* event probe address */
1134 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1135 config_element_address, event->attr.probe.addr);
1136 if (ret) {
1137 goto end;
1138 }
1139 } else {
1140 /* event probe offset */
1141 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1142 config_element_offset, event->attr.probe.offset);
1143 if (ret) {
1144 goto end;
1145 }
1146
1147 /* event probe symbol_name */
1148 ret = mi_lttng_writer_write_element_string(writer,
1149 config_element_symbol_name, event->attr.probe.symbol_name);
1150 if (ret) {
1151 goto end;
1152 }
1153 }
1154
1155 /* Close probe_attributes and attributes */
1156 ret = mi_lttng_close_multi_element(writer, 2);
1157 end:
1158 return ret;
1159 }
1160
1161 LTTNG_HIDDEN
1162 int mi_lttng_event_function_entry(struct mi_writer *writer,
1163 struct lttng_event *event)
1164 {
1165 int ret;
1166
1167 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
1168 if (ret) {
1169 goto end;
1170 }
1171
1172 ret = mi_lttng_writer_open_element(writer, config_element_probe_attributes);
1173 if (ret) {
1174 goto end;
1175 }
1176
1177 /* event probe symbol_name */
1178 ret = mi_lttng_writer_write_element_string(writer,
1179 config_element_symbol_name, event->attr.ftrace.symbol_name);
1180 if (ret) {
1181 goto end;
1182 }
1183
1184 /* Close function_attributes and attributes */
1185 ret = mi_lttng_close_multi_element(writer, 2);
1186 end:
1187 return ret;
1188 }
1189
1190 LTTNG_HIDDEN
1191 int mi_lttng_events_open(struct mi_writer *writer)
1192 {
1193 return mi_lttng_writer_open_element(writer, config_element_events);
1194 }
1195
1196 LTTNG_HIDDEN
1197 int mi_lttng_event(struct mi_writer *writer,
1198 struct lttng_event *event, int is_open, enum lttng_domain_type domain)
1199 {
1200 int ret;
1201
1202 ret = mi_lttng_event_common_attributes(writer, event);
1203 if (ret) {
1204 goto end;
1205 }
1206
1207 switch (event->type) {
1208 case LTTNG_EVENT_TRACEPOINT:
1209 {
1210 if (event->loglevel != -1) {
1211 ret = mi_lttng_event_tracepoint_loglevel(writer, event, domain);
1212 } else {
1213 ret = mi_lttng_event_tracepoint_no_loglevel(writer, event);
1214 }
1215 break;
1216 }
1217 case LTTNG_EVENT_FUNCTION:
1218 /* Fallthrough */
1219 case LTTNG_EVENT_PROBE:
1220 ret = mi_lttng_event_function_probe(writer, event);
1221 break;
1222 case LTTNG_EVENT_FUNCTION_ENTRY:
1223 ret = mi_lttng_event_function_entry(writer, event);
1224 break;
1225 case LTTNG_EVENT_ALL:
1226 /* Fallthrough */
1227 default:
1228 break;
1229 }
1230
1231 if (!is_open) {
1232 ret = mi_lttng_writer_close_element(writer);
1233 }
1234
1235 end:
1236 return ret;
1237 }
1238
1239 LTTNG_HIDDEN
1240 int mi_lttng_trackers_open(struct mi_writer *writer)
1241 {
1242 return mi_lttng_writer_open_element(writer, config_element_trackers);
1243 }
1244
1245 LTTNG_HIDDEN
1246 int mi_lttng_pid_tracker_open(struct mi_writer *writer)
1247 {
1248 int ret;
1249
1250 /* Open element pid_tracker */
1251 ret = mi_lttng_writer_open_element(writer, config_element_pid_tracker);
1252 if (ret) {
1253 goto end;
1254 }
1255
1256 /* Open targets element */
1257 ret = mi_lttng_targets_open(writer);
1258 end:
1259 return ret;
1260 }
1261
1262 LTTNG_HIDDEN
1263 int mi_lttng_pids_open(struct mi_writer *writer)
1264 {
1265 return mi_lttng_writer_open_element(writer, config_element_pids);
1266 }
1267
1268 /*
1269 * TODO: move the listing of pid for user agent to process semantic on
1270 * mi api bump. The use of process element break the mi api.
1271 */
1272 LTTNG_HIDDEN
1273 int mi_lttng_pid(struct mi_writer *writer, pid_t pid , const char *name,
1274 int is_open)
1275 {
1276 int ret;
1277
1278 /* Open pid process */
1279 ret = mi_lttng_writer_open_element(writer, config_element_pid);
1280 if (ret) {
1281 goto end;
1282 }
1283
1284 /* Writing pid number */
1285 ret = mi_lttng_writer_write_element_signed_int(writer,
1286 mi_lttng_element_pid_id, (int)pid);
1287 if (ret) {
1288 goto end;
1289 }
1290
1291 /* Writing name of the process */
1292 if (name) {
1293 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1294 name);
1295 if (ret) {
1296 goto end;
1297 }
1298 }
1299
1300 if (!is_open) {
1301 /* Closing Pid */
1302 ret = mi_lttng_writer_close_element(writer);
1303 }
1304
1305 end:
1306 return ret;
1307 }
1308
1309 LTTNG_HIDDEN
1310 int mi_lttng_targets_open(struct mi_writer *writer)
1311 {
1312 return mi_lttng_writer_open_element(writer,
1313 config_element_targets);
1314 }
1315
1316 LTTNG_HIDDEN
1317 int mi_lttng_pid_target(struct mi_writer *writer, pid_t pid, int is_open)
1318 {
1319 int ret;
1320
1321 ret = mi_lttng_writer_open_element(writer,
1322 config_element_target_pid);
1323 if (ret) {
1324 goto end;
1325 }
1326
1327 /* Writing pid number
1328 * Special case for element all on track untrack command
1329 * All pid is represented as wildcard *
1330 */
1331 if ((int) pid == -1) {
1332 ret = mi_lttng_writer_write_element_string(writer,
1333 config_element_pid,
1334 mi_lttng_element_track_untrack_all_wildcard);
1335 } else {
1336 ret = mi_lttng_writer_write_element_signed_int(writer,
1337 config_element_pid, (int) pid);
1338 }
1339 if (ret) {
1340 goto end;
1341 }
1342
1343 if (!is_open) {
1344 ret = mi_lttng_writer_close_element(writer);
1345 if (ret) {
1346 goto end;
1347 }
1348 }
1349
1350 end:
1351 return ret;
1352 }
1353
1354 LTTNG_HIDDEN
1355 int mi_lttng_event_fields_open(struct mi_writer *writer)
1356 {
1357 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
1358 }
1359
1360 LTTNG_HIDDEN
1361 int mi_lttng_event_field(struct mi_writer *writer,
1362 struct lttng_event_field *field)
1363 {
1364 int ret;
1365
1366 if (!field->field_name[0]) {
1367 ret = 0;
1368 goto end;
1369 }
1370
1371 /* Open field */
1372 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1373 if (ret) {
1374 goto end;
1375 }
1376
1377 if (!field->field_name[0]) {
1378 goto close;
1379 }
1380
1381 /* Name */
1382 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1383 field->field_name);
1384 if (ret) {
1385 goto end;
1386 }
1387
1388 /* Type */
1389 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1390 mi_lttng_eventfieldtype_string(field->type));
1391 if (ret) {
1392 goto end;
1393 }
1394
1395 /* nowrite */
1396 ret = mi_lttng_writer_write_element_signed_int(writer,
1397 mi_lttng_element_nowrite, field->nowrite);
1398 if (ret) {
1399 goto end;
1400 }
1401
1402 close:
1403 /* Close field element */
1404 ret = mi_lttng_writer_close_element(writer);
1405
1406 end:
1407 return ret;
1408 }
1409
1410 LTTNG_HIDDEN
1411 int mi_lttng_perf_counter_context(struct mi_writer *writer,
1412 struct lttng_event_perf_counter_ctx *perf_context)
1413 {
1414 int ret;
1415
1416 /* Open perf_counter_context */
1417 ret = mi_lttng_writer_open_element(writer,
1418 mi_lttng_element_perf_counter_context);
1419 if (ret) {
1420 goto end;
1421 }
1422
1423 /* Type */
1424 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1425 config_element_type, perf_context->type);
1426 if (ret) {
1427 goto end;
1428 }
1429
1430 /* Config */
1431 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1432 config_element_config, perf_context->config);
1433 if (ret) {
1434 goto end;
1435 }
1436
1437 /* Name of the perf counter */
1438 ret = mi_lttng_writer_write_element_string(writer,
1439 config_element_name, perf_context->name);
1440 if (ret) {
1441 goto end;
1442 }
1443
1444 /* Close perf_counter_context */
1445 ret = mi_lttng_writer_close_element(writer);
1446 end:
1447 return ret;
1448 }
1449
1450 static
1451 int mi_lttng_app_context(struct mi_writer *writer,
1452 const char *provider_name, const char *ctx_name)
1453 {
1454 int ret;
1455
1456 /* Open app */
1457 ret = mi_lttng_writer_open_element(writer,
1458 config_element_context_app);
1459 if (ret) {
1460 goto end;
1461 }
1462
1463 /* provider_name */
1464 ret = mi_lttng_writer_write_element_string(writer,
1465 config_element_context_app_provider_name,
1466 provider_name);
1467 if (ret) {
1468 goto end;
1469 }
1470
1471 /* ctx_name */
1472 ret = mi_lttng_writer_write_element_string(writer,
1473 config_element_context_app_ctx_name, ctx_name);
1474 if (ret) {
1475 goto end;
1476 }
1477
1478 /* Close app */
1479 ret = mi_lttng_writer_close_element(writer);
1480 end:
1481 return ret;
1482 }
1483
1484 LTTNG_HIDDEN
1485 int mi_lttng_context(struct mi_writer *writer,
1486 struct lttng_event_context *context, int is_open)
1487 {
1488 int ret;
1489
1490 /* Open context */
1491 ret = mi_lttng_writer_open_element(writer , config_element_context);
1492 if (ret) {
1493 goto end;
1494 }
1495
1496 /* Special case for PERF_*_COUNTER
1497 * print the lttng_event_perf_counter_ctx*/
1498 switch (context->ctx) {
1499 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1500 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1501 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1502 {
1503 struct lttng_event_perf_counter_ctx *perf_context =
1504 &context->u.perf_counter;
1505 ret = mi_lttng_perf_counter_context(writer, perf_context);
1506 if (ret) {
1507 goto end;
1508 }
1509 break;
1510 }
1511 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1512 {
1513 ret = mi_lttng_app_context(writer,
1514 context->u.app_ctx.provider_name,
1515 context->u.app_ctx.ctx_name);
1516 if (ret) {
1517 goto end;
1518 }
1519 break;
1520 }
1521 default:
1522 {
1523 const char *type_string = mi_lttng_event_contexttype_string(
1524 context->ctx);
1525 if (!type_string) {
1526 ret = -LTTNG_ERR_INVALID;
1527 goto end;
1528 }
1529
1530 /* Print context type */
1531 ret = mi_lttng_writer_write_element_string(writer,
1532 config_element_type, type_string);
1533 break;
1534 }
1535 }
1536
1537 /* Close context */
1538 if (!is_open) {
1539 ret = mi_lttng_writer_close_element(writer);
1540 }
1541
1542 end:
1543 return ret;
1544 }
1545
1546 LTTNG_HIDDEN
1547 int mi_lttng_snapshot_output_session_name(struct mi_writer *writer,
1548 const char *session_name)
1549 {
1550 int ret;
1551
1552 /* Open session element */
1553 ret = mi_lttng_writer_open_element(writer, config_element_session);
1554 if (ret) {
1555 goto end;
1556 }
1557
1558 /* Snapshot output list for current session name */
1559 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1560 session_name);
1561 if (ret) {
1562 goto end;
1563 }
1564
1565 /* Open element snapshots (sequence one snapshot) */
1566 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_snapshots);
1567 if (ret) {
1568 goto end;
1569 }
1570
1571 end:
1572 return ret;
1573 }
1574
1575 LTTNG_HIDDEN
1576 int mi_lttng_snapshot_list_output(struct mi_writer *writer,
1577 struct lttng_snapshot_output *output)
1578 {
1579 int ret;
1580
1581 /* Open element snapshot output */
1582 ret = mi_lttng_writer_open_element(writer,
1583 mi_lttng_element_command_snapshot);
1584 if (ret) {
1585 goto end;
1586 }
1587
1588 /* ID of the snapshot output */
1589 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1590 mi_lttng_element_id, output->id);
1591 if (ret) {
1592 goto end;
1593 }
1594
1595 /* Name of the output */
1596 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1597 output->name);
1598 if (ret) {
1599 goto end;
1600 }
1601
1602 /* Destination of the output (ctrl_url)*/
1603 ret = mi_lttng_writer_write_element_string(writer,
1604 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1605 if (ret) {
1606 goto end;
1607 }
1608
1609 /* Destination of the output (data_url) */
1610 ret = mi_lttng_writer_write_element_string(writer,
1611 mi_lttng_element_snapshot_data_url, output->data_url);
1612 if (ret) {
1613 goto end;
1614 }
1615
1616 /* total size of all stream combined */
1617 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1618 mi_lttng_element_snapshot_max_size, output->max_size);
1619 if (ret) {
1620 goto end;
1621 }
1622
1623 /* Close snapshot output element */
1624 ret = mi_lttng_writer_close_element(writer);
1625
1626 end:
1627 return ret;
1628 }
1629
1630 LTTNG_HIDDEN
1631 int mi_lttng_snapshot_del_output(struct mi_writer *writer, int id,
1632 const char *name, const char *current_session_name)
1633 {
1634 int ret;
1635
1636 /* Open element del_snapshot */
1637 ret = mi_lttng_writer_open_element(writer,
1638 mi_lttng_element_command_snapshot);
1639 if (ret) {
1640 goto end;
1641 }
1642
1643
1644 if (id != UINT32_MAX) {
1645 /* "Snapshot output "id" successfully deleted
1646 * for "current_session_name"
1647 * ID of the snapshot output
1648 */
1649 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1650 mi_lttng_element_id, id);
1651 if (ret) {
1652 goto end;
1653 }
1654 } else {
1655 /* "Snapshot output "name" successfully deleted
1656 * for session "current_session_name"
1657 * Name of the output
1658 */
1659 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1660 name);
1661 if (ret) {
1662 goto end;
1663 }
1664 }
1665
1666 /* Snapshot was deleted for session "current_session_name"*/
1667 ret = mi_lttng_writer_write_element_string(writer,
1668 mi_lttng_element_snapshot_session_name,
1669 current_session_name);
1670 if (ret) {
1671 goto end;
1672 }
1673
1674 /* Close snapshot element */
1675 ret = mi_lttng_writer_close_element(writer);
1676
1677 end:
1678 return ret;
1679 }
1680
1681 LTTNG_HIDDEN
1682 int mi_lttng_snapshot_add_output(struct mi_writer *writer,
1683 const char *current_session_name, const char *n_ptr,
1684 struct lttng_snapshot_output *output)
1685 {
1686 int ret;
1687
1688 /* Open element snapshot */
1689 ret = mi_lttng_writer_open_element(writer,
1690 mi_lttng_element_command_snapshot);
1691 if (ret) {
1692 goto end;
1693 }
1694
1695 /* Snapshot output id */
1696 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1697 mi_lttng_element_id, output->id);
1698 if (ret) {
1699 goto end;
1700 }
1701
1702 /* Snapshot output names */
1703 ret = mi_lttng_writer_write_element_string(writer,
1704 config_element_name, n_ptr);
1705 if (ret) {
1706 goto end;
1707 }
1708
1709 /* Destination of the output (ctrl_url)*/
1710 ret = mi_lttng_writer_write_element_string(writer,
1711 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1712 if (ret) {
1713 goto end;
1714 }
1715
1716 /* Snapshot added for session "current_session_name"*/
1717 ret = mi_lttng_writer_write_element_string(writer,
1718 mi_lttng_element_snapshot_session_name, current_session_name);
1719 if (ret) {
1720 goto end;
1721 }
1722
1723 /* total size of all stream combined */
1724 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1725 mi_lttng_element_snapshot_max_size, output->max_size);
1726 if (ret) {
1727 goto end;
1728 }
1729
1730 /* Close snapshot element */
1731 ret = mi_lttng_writer_close_element(writer);
1732
1733 end:
1734 return ret;
1735 }
1736
1737 LTTNG_HIDDEN
1738 int mi_lttng_snapshot_record(struct mi_writer *writer,
1739 const char *current_session_name, const char *url,
1740 const char *cmdline_ctrl_url, const char *cmdline_data_url)
1741 {
1742 int ret;
1743
1744 /* Open element snapshot */
1745 ret = mi_lttng_writer_open_element(writer,
1746 mi_lttng_element_command_snapshot);
1747 if (ret) {
1748 goto end;
1749 }
1750
1751 /*
1752 * If a valid an URL was given, serialize it,
1753 * else take the command line data and ctrl urls*/
1754 if (url) {
1755 /* Destination of the output (ctrl_url)*/
1756 ret = mi_lttng_writer_write_element_string(writer,
1757 mi_lttng_element_snapshot_ctrl_url, url);
1758 if (ret) {
1759 goto end;
1760 }
1761 } else if (cmdline_ctrl_url) {
1762 /* Destination of the output (ctrl_url)*/
1763 ret = mi_lttng_writer_write_element_string(writer,
1764 mi_lttng_element_snapshot_ctrl_url, cmdline_ctrl_url);
1765 if (ret) {
1766 goto end;
1767 }
1768
1769 /* Destination of the output (data_url) */
1770 ret = mi_lttng_writer_write_element_string(writer,
1771 mi_lttng_element_snapshot_data_url, cmdline_data_url);
1772 if (ret) {
1773 goto end;
1774 }
1775 }
1776
1777 /* Close record_snapshot element */
1778 ret = mi_lttng_writer_close_element(writer);
1779
1780 end:
1781 return ret;
1782 }
This page took 0.08221 seconds and 3 git commands to generate.