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