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