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