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