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