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