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