Mi disable-channel command: support and validation
[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>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
5e18ec73 19
c7e35b03
JR
20#include <include/config.h>
21#include <common/config/config.h>
50534d6f 22#include <lttng/snapshot-internal.h>
c7e35b03
JR
23#include "mi-lttng.h"
24
5e18ec73
JR
25#include <assert.h>
26
c7e35b03
JR
27/* Strings related to command */
28const char * const mi_lttng_element_command = "command";
37d03ff7 29const char * const mi_lttng_element_command_name = "name";
c7e35b03 30const char * const mi_lttng_element_command_version = "version";
89476427 31const char * const mi_lttng_element_command_enable_event = "enable-event";
c7e35b03 32const char * const mi_lttng_element_command_list = "list";
1734c658
JRJ
33const char * const mi_lttng_element_command_save = "save";
34const char * const mi_lttng_element_command_load = "load";
1cfc0bc8 35const char * const mi_lttng_element_command_start = "start";
e5b83100 36const char * const mi_lttng_element_command_stop = "stop";
37d03ff7 37const char * const mi_lttng_element_command_create = "create";
65f25c66 38const char * const mi_lttng_element_command_destroy = "destroy";
7e66b1b0 39const char * const mi_lttng_element_command_calibrate = "calibrate";
89b72577 40const char * const mi_lttng_element_command_add_context = "add-context";
acc09215 41const char * const mi_lttng_element_command_enable_channels = "enable-channel";
ce91cd0b 42const char * const mi_lttng_element_command_set_session = "set-session";
e4d484a5 43const char * const mi_lttng_element_command_disable_event = "disable-event";
50534d6f
JRJ
44const char * const mi_lttng_element_command_disable_channel = "disable-channel";
45const char * const mi_lttng_element_command_action = "snapshot_action";
c7e35b03 46const char * const mi_lttng_element_command_output = "output";
1734c658 47const char * const mi_lttng_element_command_success = "success";
c7e35b03 48
1734c658 49/* Strings related to version command */
c7e35b03
JR
50const char * const mi_lttng_element_version = "version";
51const char * const mi_lttng_element_version_str = "string";
52const char * const mi_lttng_element_version_web = "url";
53const char * const mi_lttng_element_version_major = "major";
54const char * const mi_lttng_element_version_minor = "minor";
314d5222 55const char * const mi_lttng_element_version_commit = "commit";
c7e35b03
JR
56const char * const mi_lttng_element_version_license = "license";
57const char * const mi_lttng_element_version_patch_level = "patchLevel";
58const char * const mi_lttng_element_version_description = "description";
50534d6f
JRJ
59const char * const mi_lttng_element_command_snapshot = "snapshot";
60const char * const mi_lttng_element_command_list_snapshot = "list_snapshot";
61const char * const mi_lttng_element_command_del_snapshot = "del_snapshot";
62const char * const mi_lttng_element_command_add_snapshot = "add_snapshot";
63const char * const mi_lttng_element_command_record_snapshot = "record_snapshot";
c7e35b03 64
5e18ec73
JR
65/* Strings related to pid */
66const char * const mi_lttng_element_pids = "pids";
67const char * const mi_lttng_element_pid = "pid";
5e18ec73 68
1734c658
JRJ
69/* Strings related to save command */
70const char * const mi_lttng_element_save = "save";
71
72/* Strings related to load command */
73const char * const mi_lttng_element_load = "load";
74
5e18ec73
JR
75/* String related to a lttng_event_field */
76const char * const mi_lttng_element_event_field = "event_field";
77const char * const mi_lttng_element_event_fields = "event_fields";
78
89b72577
JRJ
79/* String related to lttng_event_context */
80const char * const mi_lttng_context_type_perf_counter = "PERF_COUNTER";
81const char * const mi_lttng_context_type_perf_cpu_counter = "PERF_CPU_COUNTER";
82const char * const mi_lttng_context_type_perf_thread_counter = "PERF_THREAD_COUNTER";
83
84/* String related to lttng_event_perf_counter_ctx */
85const char * const mi_lttng_element_perf_counter_context = "perf_counter_context";
86
5e18ec73
JR
87/* General elements of mi_lttng */
88const char * const mi_lttng_element_type_other = "OTHER";
89const char * const mi_lttng_element_type_integer = "INTEGER";
90const char * const mi_lttng_element_type_enum = "ENUM";
91const char * const mi_lttng_element_type_float = "FLOAT";
92const char * const mi_lttng_element_type_string = "STRING";
93const char * const mi_lttng_element_nowrite = "nowrite";
89b72577 94const char * const mi_lttng_element_success = "success";
50534d6f
JRJ
95const char * const mi_lttng_element_id = "id";
96const char * const mi_lttng_element_empty = "";
5e18ec73
JR
97
98/* String related to loglevel */
99const char * const mi_lttng_loglevel_str_alert = "TRACE_ALERT";
100const char * const mi_lttng_loglevel_str_crit = "TRACE_CRIT";
101const char * const mi_lttng_loglevel_str_debug = "TRACE_DEBUG";
102const char * const mi_lttng_loglevel_str_debug_function = "TRACE_DEBUG_FUNCTION";
103const char * const mi_lttng_loglevel_str_debug_line = "TRACE_DEBUG_LINE";
104const char * const mi_lttng_loglevel_str_debug_module = "TRACE_DEBUG_MODULE";
105const char * const mi_lttng_loglevel_str_debug_process = "TRACE_DEBUG_PROCESS";
106const char * const mi_lttng_loglevel_str_debug_program = "TRACE_DEBUG_PROGRAM";
107const char * const mi_lttng_loglevel_str_debug_system = "TRACE_DEBUG_SYSTEM";
108const char * const mi_lttng_loglevel_str_debug_unit = "TRACE_DEBUG_UNIT";
109const char * const mi_lttng_loglevel_str_emerg = "TRACE_EMERG";
110const char * const mi_lttng_loglevel_str_err = "TRACE_ERR";
111const char * const mi_lttng_loglevel_str_info = "TRACE_INFO";
112const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
113const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
114const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
115
1734c658 116/* String related to loglevel type */
5e18ec73
JR
117const char * const mi_lttng_loglevel_type_all = "ALL";
118const char * const mi_lttng_loglevel_type_range = "RANGE";
119const char * const mi_lttng_loglevel_type_single = "SINGLE";
120const char * const mi_lttng_loglevel_type_unknown = "UNKNOWN";
121
7e66b1b0
JRJ
122/* String related to lttng_calibrate */
123const char * const mi_lttng_element_calibrate = "calibrate";
124const char * const mi_lttng_element_calibrate_function = "FUNCTION";
125
50534d6f
JRJ
126/* String related to a lttng_snapshot_output */
127const char * const mi_lttng_element_snapshots = "snapshots";
128const char * const mi_lttng_element_snapshot_session_name = "session_name";
129const char * const mi_lttng_element_snapshot_n_ptr = "n_ptr";
130const char * const mi_lttng_element_snapshot_data_url = "data_url";
131const char * const mi_lttng_element_snapshot_ctrl_url = "ctrl_url";
132const char * const mi_lttng_element_snapshot_max_size = "max_size";
5e18ec73
JR
133
134const char *mi_lttng_loglevel_string(int value)
135{
136 switch (value) {
137 case -1:
138 return mi_lttng_element_empty;
139 case LTTNG_LOGLEVEL_EMERG:
140 return mi_lttng_loglevel_str_emerg;
141 case LTTNG_LOGLEVEL_ALERT:
142 return mi_lttng_loglevel_str_alert;
143 case LTTNG_LOGLEVEL_CRIT:
144 return mi_lttng_loglevel_str_crit;
145 case LTTNG_LOGLEVEL_ERR:
146 return mi_lttng_loglevel_str_err;
147 case LTTNG_LOGLEVEL_WARNING:
148 return mi_lttng_loglevel_str_warning;
149 case LTTNG_LOGLEVEL_NOTICE:
150 return mi_lttng_loglevel_str_notice;
151 case LTTNG_LOGLEVEL_INFO:
152 return mi_lttng_loglevel_str_info;
153 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
154 return mi_lttng_loglevel_str_debug_system;
155 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
156 return mi_lttng_loglevel_str_debug_program;
157 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
158 return mi_lttng_loglevel_str_debug_process;
159 case LTTNG_LOGLEVEL_DEBUG_MODULE:
160 return mi_lttng_loglevel_str_debug_module;
161 case LTTNG_LOGLEVEL_DEBUG_UNIT:
162 return mi_lttng_loglevel_str_debug_unit;
163 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
164 return mi_lttng_loglevel_str_debug_function;
165 case LTTNG_LOGLEVEL_DEBUG_LINE:
166 return mi_lttng_loglevel_str_debug_line;
167 case LTTNG_LOGLEVEL_DEBUG:
168 return mi_lttng_loglevel_str_debug;
169 default:
170 return mi_lttng_loglevel_str_unknown;
171 }
172}
173
174const char *mi_lttng_logleveltype_string(enum lttng_loglevel_type value)
175{
176 switch (value) {
177 case LTTNG_EVENT_LOGLEVEL_ALL:
178 return mi_lttng_loglevel_type_all;
179 case LTTNG_EVENT_LOGLEVEL_RANGE:
180 return mi_lttng_loglevel_type_range;
181 case LTTNG_EVENT_LOGLEVEL_SINGLE:
182 return mi_lttng_loglevel_type_single;
183 default:
184 return mi_lttng_loglevel_type_unknown;
185 }
186}
187
188const char *mi_lttng_eventtype_string(enum lttng_event_type value)
189{
190 switch (value) {
191 case LTTNG_EVENT_ALL:
192 return config_event_type_all;
193 case LTTNG_EVENT_TRACEPOINT:
194 return config_event_type_tracepoint;
195 case LTTNG_EVENT_PROBE:
196 return config_event_type_probe;
197 case LTTNG_EVENT_FUNCTION:
198 return config_event_type_function;
199 case LTTNG_EVENT_FUNCTION_ENTRY:
200 return config_event_type_function_entry;
201 case LTTNG_EVENT_SYSCALL:
202 return config_event_type_syscall;
203 case LTTNG_EVENT_NOOP:
204 return config_event_type_noop;
205 default:
206 return mi_lttng_element_empty;
207 }
208}
209
89b72577
JRJ
210const char *mi_lttng_event_contexttype_string(enum lttng_event_context_type val)
211{
212 switch (val) {
213 case LTTNG_EVENT_CONTEXT_PID:
214 return config_event_context_pid;
215 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
216 return mi_lttng_context_type_perf_counter;
217 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
218 return mi_lttng_context_type_perf_thread_counter;
219 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
220 return mi_lttng_context_type_perf_cpu_counter;
221 case LTTNG_EVENT_CONTEXT_PROCNAME:
222 return config_event_context_procname;
223 case LTTNG_EVENT_CONTEXT_PRIO:
224 return config_event_context_prio;
225 case LTTNG_EVENT_CONTEXT_NICE:
226 return config_event_context_nice;
227 case LTTNG_EVENT_CONTEXT_VPID:
228 return config_event_context_vpid;
229 case LTTNG_EVENT_CONTEXT_TID:
230 return config_event_context_tid;
231 case LTTNG_EVENT_CONTEXT_VTID:
232 return config_event_context_vtid;
233 case LTTNG_EVENT_CONTEXT_PPID:
234 return config_event_context_ppid;
235 case LTTNG_EVENT_CONTEXT_VPPID:
236 return config_event_context_vppid;
237 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
238 return config_event_context_pthread_id;
239 case LTTNG_EVENT_CONTEXT_HOSTNAME:
240 return config_event_context_hostname;
241 case LTTNG_EVENT_CONTEXT_IP:
242 return config_event_context_ip;
243 default:
244 return NULL;
245 }
246}
247
5e18ec73
JR
248const char *mi_lttng_eventfieldtype_string(enum lttng_event_field_type val)
249{
250 switch (val) {
251 case(LTTNG_EVENT_FIELD_INTEGER):
252 return mi_lttng_element_type_integer;
253 case(LTTNG_EVENT_FIELD_ENUM):
254 return mi_lttng_element_type_enum;
255 case(LTTNG_EVENT_FIELD_FLOAT):
256 return mi_lttng_element_type_float;
257 case(LTTNG_EVENT_FIELD_STRING):
258 return mi_lttng_element_type_string;
259 default:
260 return mi_lttng_element_type_other;
261 }
262}
263
264const char *mi_lttng_domaintype_string(enum lttng_domain_type value)
265{
266 /* Note: This is a *duplicate* of get_domain_str from bin/lttng/utils.c */
267 switch (value) {
268 case LTTNG_DOMAIN_KERNEL:
269 return config_domain_type_kernel;
270 case LTTNG_DOMAIN_UST:
271 return config_domain_type_ust;
272 case LTTNG_DOMAIN_JUL:
273 return config_domain_type_jul;
274 default:
275 /* Should not have an unknown domain */
276 assert(0);
277 }
278}
279
280const char *mi_lttng_buffertype_string(enum lttng_buffer_type value)
281{
282 switch (value) {
283 case LTTNG_BUFFER_PER_PID:
284 return config_buffer_type_per_pid;
285 case LTTNG_BUFFER_PER_UID:
286 return config_buffer_type_per_uid;
287 case LTTNG_BUFFER_GLOBAL:
288 return config_buffer_type_global;
289 default:
290 /* Should not have an unknow buffer type */
291 assert(0);
292 }
293}
294
7e66b1b0
JRJ
295const char *mi_lttng_calibratetype_string(enum lttng_calibrate_type val)
296{
297 const char *ret;
298
299 switch (val) {
300 case LTTNG_CALIBRATE_FUNCTION:
301 ret = mi_lttng_element_calibrate_function;
302 break;
303 default:
304 ret = mi_lttng_element_empty;
305 break;
306 }
307 return ret;
308}
309
c7e35b03
JR
310LTTNG_HIDDEN
311struct mi_writer *mi_lttng_writer_create(int fd_output, int mi_output_type)
312{
313 struct mi_writer *mi_writer;
314
315 mi_writer = zmalloc(sizeof(struct mi_writer));
316 if (!mi_writer) {
317 PERROR("zmalloc mi_writer_create");
318 goto end;
319 }
320 if (mi_output_type == LTTNG_MI_XML) {
321 mi_writer->writer = config_writer_create(fd_output);
322 if (!mi_writer->writer) {
323 goto err_destroy;
324 }
325 mi_writer->type = LTTNG_MI_XML;
326 } else {
327 goto err_destroy;
328 }
329
330end:
331 return mi_writer;
332
333err_destroy:
334 free(mi_writer);
335 return NULL;
336}
337
338LTTNG_HIDDEN
339int mi_lttng_writer_destroy(struct mi_writer *writer)
340{
341 int ret;
342
343 if (!writer) {
344 ret = -EINVAL;
345 goto end;
346 }
347
348 ret = config_writer_destroy(writer->writer);
349 if (ret < 0) {
350 goto end;
351 }
352
353 free(writer);
354end:
355 return ret;
356}
357
358LTTNG_HIDDEN
359int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command)
360{
361 int ret;
362
363 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command);
364 if (ret) {
365 goto end;
366 }
367 ret = mi_lttng_writer_write_element_string(writer,
368 mi_lttng_element_command_name, command);
369end:
370 return ret;
371}
372
373LTTNG_HIDDEN
374int mi_lttng_writer_command_close(struct mi_writer *writer)
375{
376 return mi_lttng_writer_close_element(writer);
377}
378
379LTTNG_HIDDEN
380int mi_lttng_writer_open_element(struct mi_writer *writer,
381 const char *element_name)
382{
383 return config_writer_open_element(writer->writer, element_name);
384}
385
386LTTNG_HIDDEN
387int mi_lttng_writer_close_element(struct mi_writer *writer)
388{
389 return config_writer_close_element(writer->writer);
390}
391
5e18ec73
JR
392LTTNG_HIDDEN
393int mi_lttng_close_multi_element(struct mi_writer *writer,
394 unsigned int nb_element)
395{
396 int ret, i;
397
398 if (nb_element < 1) {
399 ret = 0;
400 goto end;
401 }
402 for (i = 0; i < nb_element; i++) {
403 ret = mi_lttng_writer_close_element(writer);
404 if (ret) {
405 goto end;
406 }
407 }
408end:
409 return ret;
410}
411
c7e35b03
JR
412LTTNG_HIDDEN
413int mi_lttng_writer_write_element_unsigned_int(struct mi_writer *writer,
414 const char *element_name, uint64_t value)
415{
416 return config_writer_write_element_unsigned_int(writer->writer,
417 element_name, value);
418}
419
420LTTNG_HIDDEN
421int mi_lttng_writer_write_element_signed_int(struct mi_writer *writer,
422 const char *element_name, int64_t value)
423{
424 return config_writer_write_element_signed_int(writer->writer,
425 element_name, value);
426}
427
428LTTNG_HIDDEN
429int mi_lttng_writer_write_element_bool(struct mi_writer *writer,
430 const char *element_name, int value)
431{
432 return config_writer_write_element_bool(writer->writer,
433 element_name, value);
434}
435
436LTTNG_HIDDEN
437int mi_lttng_writer_write_element_string(struct mi_writer *writer,
438 const char *element_name, const char *value)
439{
440 return config_writer_write_element_string(writer->writer,
441 element_name, value);
442}
443
444LTTNG_HIDDEN
445int mi_lttng_version(struct mi_writer *writer, struct mi_lttng_version *version,
446 const char *lttng_description, const char *lttng_license)
447{
448 int ret;
449
450 /* Open version */
451 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_version);
452 if (ret) {
453 goto end;
454 }
455
456 /* Version string (contain info like rc etc.) */
457 ret = mi_lttng_writer_write_element_string(writer,
458 mi_lttng_element_version_str, VERSION);
459 if (ret) {
460 goto end;
461 }
462
463 /* Major version number */
464 ret = mi_lttng_writer_write_element_unsigned_int(writer,
465 mi_lttng_element_version_major, version->version_major);
466 if (ret) {
467 goto end;
468 }
469
470 /* Minor version number */
471 ret = mi_lttng_writer_write_element_unsigned_int(writer,
472 mi_lttng_element_version_minor, version->version_minor);
473 if (ret) {
474 goto end;
475 }
476
314d5222
JRJ
477 /* Commit version number */
478 ret = mi_lttng_writer_write_element_string(writer,
479 mi_lttng_element_version_commit, version->version_commit);
480 if (ret) {
481 goto end;
482 }
483
c7e35b03
JR
484 /* Patch number */
485 ret = mi_lttng_writer_write_element_unsigned_int(writer,
486 mi_lttng_element_version_patch_level, version->version_patchlevel);
487 if (ret) {
488 goto end;
489 }
490
491 /* Name of the version */
492 ret = mi_lttng_writer_write_element_string(writer,
493 config_element_name, version->version_name);
494 if (ret) {
495 goto end;
496 }
497
498 /* Description mostly related to beer... */
499 ret = mi_lttng_writer_write_element_string(writer,
500 mi_lttng_element_version_description, lttng_description);
501 if (ret) {
502 goto end;
503 }
504
505 /* url */
506 ret = mi_lttng_writer_write_element_string(writer,
507 mi_lttng_element_version_web, version->package_url);
508 if (ret) {
509 goto end;
510 }
511
512 /* License: free as in free beer...no...*speech* */
513 ret = mi_lttng_writer_write_element_string(writer,
514 mi_lttng_element_version_license, lttng_license);
515 if (ret) {
516 goto end;
517 }
518
519 /* Close version element */
520 ret = mi_lttng_writer_close_element(writer);
521
522end:
523 return ret;
524}
525
5e18ec73
JR
526LTTNG_HIDDEN
527int mi_lttng_sessions_open(struct mi_writer *writer)
528{
529 return mi_lttng_writer_open_element(writer, config_element_sessions);
530}
531
c7e35b03
JR
532LTTNG_HIDDEN
533int mi_lttng_session(struct mi_writer *writer,
534 struct lttng_session *session, int is_open)
535{
536 int ret;
537
5e18ec73
JR
538 assert(session);
539
540 /* Open sessions element */
c7e35b03
JR
541 ret = mi_lttng_writer_open_element(writer,
542 config_element_session);
543 if (ret) {
544 goto end;
545 }
546
547 /* Name of the session */
548 ret = mi_lttng_writer_write_element_string(writer,
549 config_element_name, session->name);
550 if (ret) {
551 goto end;
552 }
553
5e18ec73 554 /* Path */
c7e35b03
JR
555 ret = mi_lttng_writer_write_element_string(writer,
556 config_element_path, session->path);
557 if (ret) {
558 goto end;
559 }
560
5e18ec73
JR
561 /* Enabled ? */
562 ret = mi_lttng_writer_write_element_bool(writer,
c7e35b03
JR
563 config_element_enabled, session->enabled);
564 if (ret) {
565 goto end;
566 }
567
5e18ec73 568 /* Snapshot mode */
c7e35b03
JR
569 ret = mi_lttng_writer_write_element_unsigned_int(writer,
570 config_element_snapshot_mode, session->snapshot_mode);
571 if (ret) {
572 goto end;
573 }
574
5e18ec73 575 /* Live timer interval in usec */
c7e35b03
JR
576 ret = mi_lttng_writer_write_element_unsigned_int(writer,
577 config_element_live_timer_interval,
578 session->live_timer_interval);
579 if (ret) {
580 goto end;
581 }
582
583 if (!is_open) {
584 /* Closing session element */
585 ret = mi_lttng_writer_close_element(writer);
586 }
587end:
588 return ret;
589
590}
5e18ec73
JR
591
592LTTNG_HIDDEN
593int mi_lttng_domains_open(struct mi_writer *writer)
594{
595 return mi_lttng_writer_open_element(writer, config_element_domains);
596}
597
598LTTNG_HIDDEN
599int mi_lttng_domain(struct mi_writer *writer,
600 struct lttng_domain *domain, int is_open)
601{
602 int ret = 0;
603 const char *str_domain;
604 const char *str_buffer;
605
606 assert(domain);
607
608 /* Open domain element */
609 ret = mi_lttng_writer_open_element(writer, config_element_domain);
610 if (ret) {
611 goto end;
612 }
613
614 /* Domain Type */
615 str_domain = mi_lttng_domaintype_string(domain->type);
616 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
617 str_domain);
618 if (ret) {
619 goto end;
620 }
621
622 /* Buffer Type */
623 str_buffer= mi_lttng_buffertype_string(domain->buf_type);
624 ret = mi_lttng_writer_write_element_string(writer,
625 config_element_buffer_type, str_buffer);
626 if (ret) {
627 goto end;
628 }
629
630 /* TODO: attr... not sure how to use the union.... */
631
632 if (!is_open) {
633 /* Closing domain element */
634 ret = mi_lttng_writer_close_element(writer);
635 }
636
637end:
638 return ret;
639
640}
641
642LTTNG_HIDDEN
643int mi_lttng_channels_open(struct mi_writer *writer)
644{
645 return mi_lttng_writer_open_element(writer, config_element_channels);
646}
647
648LTTNG_HIDDEN
649int mi_lttng_channel(struct mi_writer *writer,
650 struct lttng_channel *channel, int is_open)
651{
652 int ret = 0;
653
654 assert(channel);
655
656 /* Opening channel element */
657 ret = mi_lttng_writer_open_element(writer, config_element_channel);
658 if (ret) {
659 goto end;
660 }
661
662 /* Name */
663 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
664 channel->name);
665 if (ret) {
666 goto end;
667 }
668
669 /* Enabled ? */
670 ret = mi_lttng_writer_write_element_bool(writer,
671 config_element_enabled, channel->enabled);
672 if (ret) {
673 goto end;
674 }
675
676 /* Attribute */
677 ret = mi_lttng_channel_attr(writer, &channel->attr);
678 if (ret) {
679 goto end;
680 }
681
682 if (!is_open) {
683 /* Closing channel element */
684 ret = mi_lttng_writer_close_element(writer);
685 if (ret) {
686 goto end;
687 }
688 }
689end:
690 return ret;
691}
692
693LTTNG_HIDDEN
694int mi_lttng_channel_attr(struct mi_writer *writer,
695 struct lttng_channel_attr *attr)
696{
697 int ret = 0;
698
699 assert(attr);
700
701 /* Opening Attributes */
702 ret = mi_lttng_writer_open_element(writer, config_element_attributes);
703 if (ret) {
704 goto end;
705 }
706
707 /* Overwrite */
708 ret = mi_lttng_writer_write_element_string(writer,
709 config_element_overwrite_mode,
710 attr->overwrite ? config_overwrite_mode_overwrite :
711 config_overwrite_mode_discard);
712 if (ret) {
713 goto end;
714 }
715
716 /* Sub buffer size in byte */
717 ret = mi_lttng_writer_write_element_unsigned_int(writer,
718 config_element_subbuf_size, attr->subbuf_size);
719 if (ret) {
720 goto end;
721 }
722
723 /* Number of subbuffer (power of two) */
724 ret = mi_lttng_writer_write_element_unsigned_int(writer,
725 config_element_num_subbuf,
726 attr->num_subbuf);
727 if (ret) {
728 goto end;
729 }
730
731 /* Switch timer interval in usec */
732 ret = mi_lttng_writer_write_element_unsigned_int(writer,
733 config_element_switch_timer_interval,
734 attr->switch_timer_interval);
735 if (ret) {
736 goto end;
737 }
738
739 /* Read timer interval in usec */
740 ret = mi_lttng_writer_write_element_unsigned_int(writer,
741 config_element_read_timer_interval,
742 attr->read_timer_interval);
743 if (ret) {
744 goto end;
745 }
746
747 /* Event output */
748 ret = mi_lttng_writer_write_element_string(writer,
749 config_element_output_type,
750 attr->output == LTTNG_EVENT_SPLICE ?
751 config_output_type_splice : config_output_type_mmap);
752 if (ret) {
753 goto end;
754 }
755
756 /* Tracefile size in bytes */
757 ret = mi_lttng_writer_write_element_unsigned_int(writer,
758 config_element_tracefile_size, attr->tracefile_size);
759 if (ret) {
760 goto end;
761 }
762
763 /* Count of tracefiles */
764 ret = mi_lttng_writer_write_element_unsigned_int(writer,
765 config_element_tracefile_count,
766 attr->tracefile_count);
767 if (ret) {
768 goto end;
769 }
770
771 /* Live timer interval in usec*/
772 ret = mi_lttng_writer_write_element_unsigned_int(writer,
773 config_element_live_timer_interval,
774 attr->live_timer_interval);
775 if (ret) {
776 goto end;
777 }
778
779 /* Closing attributes */
780 ret = mi_lttng_writer_close_element(writer);
781 if (ret) {
782 goto end;
783 }
784end:
785 return ret;
786
787}
788
789LTTNG_HIDDEN
790int mi_lttng_event_common_attributes(struct mi_writer *writer,
791 struct lttng_event *event)
792{
793 int ret;
794
795 /* Open event element */
796 ret = mi_lttng_writer_open_element(writer, config_element_event);
797 if (ret) {
798 goto end;
799 }
800
801 /* event name */
802 ret = mi_lttng_writer_write_element_string(writer,
803 config_element_name, event->name);
804 if (ret) {
805 goto end;
806 }
807
808 /* event type */
809 ret = mi_lttng_writer_write_element_string(writer,
810 config_element_type, mi_lttng_eventtype_string(event->type));
811 if (ret) {
812 goto end;
813 }
814
815 /* is event enabled */
816 ret = mi_lttng_writer_write_element_bool(writer,
817 config_element_enabled, event->enabled);
818 if (ret) {
819 goto end;
820 }
821
822 /* event filter enabled? */
823 ret = mi_lttng_writer_write_element_bool(writer,
824 config_element_filter, event->filter);
825
826end:
827 return ret;
828}
829
830LTTNG_HIDDEN
831int mi_lttng_event_tracepoint_loglevel(struct mi_writer *writer,
832 struct lttng_event *event)
833{
834 int ret;
835
836 /* event loglevel */
837 ret = mi_lttng_writer_write_element_string(writer,
838 config_element_loglevel, mi_lttng_loglevel_string(event->loglevel));
839 if (ret) {
840 goto end;
841 }
842
843 ret = mi_lttng_writer_write_element_string(writer,
844 config_element_loglevel_type,
845 mi_lttng_logleveltype_string(event->loglevel_type));
846 if (ret) {
847 goto end;
848 }
849
850 /* event exclusion filter */
851 ret = mi_lttng_writer_write_element_bool(writer,
852 config_element_exclusion, event->exclusion);
853 if (ret) {
854 goto end;
855 }
856
857end:
858 return ret;
859}
860
861LTTNG_HIDDEN
862int mi_lttng_event_tracepoint_no_loglevel(struct mi_writer *writer,
863 struct lttng_event *event)
864{
865 /* event exclusion filter */
866 return mi_lttng_writer_write_element_bool(writer,
867 config_element_exclusion, event->exclusion);
868}
869
870LTTNG_HIDDEN
871int mi_lttng_event_function_probe(struct mi_writer *writer,
872 struct lttng_event *event)
873{
874 int ret;
875
876 if (event->attr.probe.addr != 0) {
877 /* event probe address */
878 ret = mi_lttng_writer_write_element_unsigned_int(writer,
879 config_element_address, event->attr.probe.addr);
880 if (ret) {
881 goto end;
882 }
883 } else {
884 /* event probe offset */
885 ret = mi_lttng_writer_write_element_unsigned_int(writer,
886 config_element_offset, event->attr.probe.offset);
887 if (ret) {
888 goto end;
889 }
890
891 /* event probe symbol_name */
892 ret = mi_lttng_writer_write_element_string(writer,
893 config_element_symbol_name, event->attr.probe.symbol_name);
894 if (ret) {
895 goto end;
896 }
897 }
898end:
899 return ret;
900}
901
902LTTNG_HIDDEN
903int mi_lttng_event_function_entry(struct mi_writer *writer,
904 struct lttng_event *event)
905{
906 /* event probe symbol_name */
907 return mi_lttng_writer_write_element_string(writer,
908 config_element_symbol_name, event->attr.ftrace.symbol_name);
909}
910
911LTTNG_HIDDEN
912int mi_lttng_events_open(struct mi_writer *writer)
913{
914 return mi_lttng_writer_open_element(writer, config_element_events);
915}
916
917LTTNG_HIDDEN
918int mi_lttng_event(struct mi_writer *writer,
919 struct lttng_event *event, int is_open)
920{
921 int ret;
922
923 ret = mi_lttng_event_common_attributes(writer, event);
924 if (ret) {
925 goto end;
926 }
927
928 switch (event->type) {
5e18ec73
JR
929 case LTTNG_EVENT_TRACEPOINT:
930 {
931 if (event->loglevel != -1) {
932 ret = mi_lttng_event_tracepoint_loglevel(writer, event);
933 } else {
934 ret = mi_lttng_event_tracepoint_no_loglevel(writer, event);
935 }
936 break;
937 }
938 case LTTNG_EVENT_PROBE:
939 ret = mi_lttng_event_function_probe(writer, event);
940 break;
941 case LTTNG_EVENT_FUNCTION_ENTRY:
942 ret = mi_lttng_event_function_entry(writer, event);
943 break;
89476427
JRJ
944 case LTTNG_EVENT_ALL:
945 /* Fallthrough */
5e18ec73
JR
946 default:
947 break;
948 }
949
950 if (!is_open) {
951 ret = mi_lttng_writer_close_element(writer);
952 }
953
954end:
955 return ret;
956}
957
958LTTNG_HIDDEN
959int mi_lttng_pids_open(struct mi_writer *writer)
960{
961 return mi_lttng_writer_open_element(writer, mi_lttng_element_pids);
962}
963
964LTTNG_HIDDEN
965int mi_lttng_pid(struct mi_writer *writer, pid_t pid , const char *cmdline,
966 int is_open)
967{
968 int ret;
969
970 /* Open element pid */
971 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_pid);
972 if (ret) {
973 goto end;
974 }
975
976 /* Writing pid number */
977 ret = mi_lttng_writer_write_element_signed_int(writer,
978 mi_lttng_element_pid_id, (int)pid);
979 if (ret) {
980 goto end;
981 }
982
983 /* Writing name of the process */
984 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
985 cmdline);
986 if (ret) {
987 goto end;
988 }
989
990 if (!is_open) {
991 /* Closing Pid */
992 ret = mi_lttng_writer_close_element(writer);
993 }
994
995end:
996 return ret;
997}
998
999LTTNG_HIDDEN
1000int mi_lttng_event_fields_open(struct mi_writer *writer)
1001{
1002 return mi_lttng_writer_open_element(writer, mi_lttng_element_event_fields);
1003}
1004
1005LTTNG_HIDDEN
1006int mi_lttng_event_field(struct mi_writer *writer,
1007 struct lttng_event_field *field)
1008{
1009 int ret;
1010
1011 if (!field->field_name[0]) {
1012 /* To Review: not sure if legal david ?
1013 * how should this be handle ?
1014 */
1015 ret = 0;
1016 goto end;
1017 }
1018
1019 /* Open field */
1020 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_event_field);
1021 if (ret) {
1022 goto end;
1023 }
1024
1025 if (!field->field_name[0]) {
1026 goto close;
1027 }
1028
1029 /* Name */
1030 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1031 field->field_name);
1032 if (ret) {
1033 goto end;
1034 }
1035
1036 /* Type */
1037 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1038 mi_lttng_eventfieldtype_string(field->type));
1039 if (ret) {
1040 goto end;
1041 }
1042
1043 /* nowrite */
1044 ret = mi_lttng_writer_write_element_signed_int(writer,
1045 mi_lttng_element_nowrite, field->nowrite);
1046 if (ret) {
1047 goto end;
1048 }
1049
1050close:
1051 /* Close field element */
1052 ret = mi_lttng_writer_close_element(writer);
1053
1054end:
1055 return ret;
1056}
7e66b1b0
JRJ
1057
1058LTTNG_HIDDEN
1059int mi_lttng_calibrate(struct mi_writer *writer,
1060 struct lttng_calibrate *calibrate)
1061{
1062 int ret;
1063
1064 /* Open calibrate element */
1065 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_calibrate);
1066 if (ret) {
1067 goto end;
1068 }
1069
1070 /* Calibration type */
1071 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1072 mi_lttng_calibratetype_string(calibrate->type));
1073 if (ret) {
1074 goto end;
1075 }
1076
1077 /* Closing calibrate element */
1078 ret = mi_lttng_writer_close_element(writer);
1079end:
1080 return ret;
1081}
89b72577
JRJ
1082LTTNG_HIDDEN
1083int mi_lttng_context(struct mi_writer *writer,
1084 struct lttng_event_context *context, int is_open)
1085{
1086 int ret;
1087 const char *type_string;
1088 struct lttng_event_perf_counter_ctx *perf_context;
1089 /* Open context */
1090 ret = mi_lttng_writer_open_element(writer , config_element_context);
1091 if (ret) {
1092 goto end;
1093 }
1094
1095 type_string = mi_lttng_event_contexttype_string(context->ctx);
1096 if (!type_string) {
1097 ret = -LTTNG_ERR_INVALID;
1098 goto end;
1099 }
1100
1101 /* Print context type */
1102 ret = mi_lttng_writer_write_element_string(writer, config_element_type,
1103 type_string);
1104
1105 /* Special case for PERF_*_COUNTER
1106 * print the lttng_event_perf_counter_ctx*/
1107 switch (context->ctx) {
1108 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1109 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1110 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1111 perf_context = &context->u.perf_counter;
1112 ret = mi_lttng_perf_counter_context(writer, perf_context);
1113 if (ret) {
1114 goto end;
1115 }
1116 break;
1117 default:
1118 break;
1119 }
1120
1121 /* Close context */
1122 if (!is_open) {
1123 ret = mi_lttng_writer_close_element(writer);
1124 }
1125
1126end:
1127 return ret;
1128}
1129
1130LTTNG_HIDDEN
1131int mi_lttng_perf_counter_context(struct mi_writer *writer,
1132 struct lttng_event_perf_counter_ctx *perf_context)
1133{
1134 int ret;
1135
1136 /* Open perf_counter_context */
1137 ret = mi_lttng_writer_open_element(writer,
1138 mi_lttng_element_perf_counter_context);
1139 if (ret) {
1140 goto end;
1141 }
1142
1143 /* Type */
1144 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1145 config_element_type, perf_context->type);
1146 if (ret) {
1147 goto end;
1148 }
1149
1150 /* Config */
1151 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1152 config_element_config, perf_context->config);
1153 if (ret) {
1154 goto end;
1155 }
1156
1157 /* Name of the perf counter */
1158 ret = mi_lttng_writer_write_element_string(writer,
1159 config_element_name, perf_context->name);
1160 if (ret) {
1161 goto end;
1162 }
1163
1164 /* Close perf_counter_context */
1165 ret = mi_lttng_writer_close_element(writer);
1166end:
1167 return ret;
1168}
50534d6f
JRJ
1169
1170LTTNG_HIDDEN
1171int mi_lttng_snapshot_output_session_name(struct mi_writer *writer,
1172 const char *session_name)
1173{
1174 int ret;
1175
1176 /* Open session element */
1177 ret = mi_lttng_writer_open_element(writer, config_element_session);
1178 if (ret) {
1179 goto end;
1180 }
1181
1182 /* Snapshot output list for current session name */
1183 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1184 session_name);
1185
1186 /* Open element snapshots (sequence one snapshot) */
1187 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_snapshots);
1188 if (ret) {
1189 goto end;
1190 }
1191
1192end:
1193 return ret;
1194}
1195
1196LTTNG_HIDDEN
1197int mi_lttng_snapshot_list_output(struct mi_writer *writer,
1198 struct lttng_snapshot_output *output)
1199{
1200 int ret;
1201
1202 /* Open element snapshot output */
1203 ret = mi_lttng_writer_open_element(writer,
1204 mi_lttng_element_command_snapshot);
1205 if (ret) {
1206 goto end;
1207 }
1208
1209 /* ID of the snapshot output */
1210 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1211 mi_lttng_element_id, output->id);
1212 if (ret) {
1213 goto end;
1214 }
1215
1216 /* Name of the output */
1217 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1218 output->name);
1219 if (ret) {
1220 goto end;
1221 }
1222
1223 /* Destination of the output (ctrl_url)*/
1224 ret = mi_lttng_writer_write_element_string(writer,
1225 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1226 if (ret) {
1227 goto end;
1228 }
1229
1230 /* Destination of the output (data_url) */
1231 ret = mi_lttng_writer_write_element_string(writer,
1232 mi_lttng_element_snapshot_data_url, output->data_url);
1233 if (ret) {
1234 goto end;
1235 }
1236
1237 /* total size of all stream combined */
1238 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1239 mi_lttng_element_snapshot_max_size, output->max_size);
1240 if (ret) {
1241 goto end;
1242 }
1243
1244 /* Close snapshot output element */
1245 ret = mi_lttng_writer_close_element(writer);
1246
1247end:
1248 return ret;
1249}
1250
1251LTTNG_HIDDEN
1252int mi_lttng_snapshot_del_output(struct mi_writer *writer, int id,
1253 const char *name, const char *current_session_name)
1254{
1255 int ret;
1256
1257 /* Open element del_snapshot */
1258 ret = mi_lttng_writer_open_element(writer,
1259 mi_lttng_element_command_snapshot);
1260 if (ret) {
1261 goto end;
1262 }
1263
1264
1265 if (id != UINT32_MAX) {
1266 /* "Snapshot output "id" successfully deleted
1267 * for "current_session_name"
1268 * ID of the snapshot output
1269 */
1270 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1271 mi_lttng_element_id, id);
1272 if (ret) {
1273 goto end;
1274 }
1275 } else {
1276 /* "Snapshot output "name" successfully deleted
1277 * for session "current_session_name"
1278 * Name of the output
1279 */
1280 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
1281 name);
1282 if (ret) {
1283 goto end;
1284 }
1285 }
1286
1287 /* Snapshot was deleted for session "current_session_name"*/
1288 ret = mi_lttng_writer_write_element_string(writer,
1289 mi_lttng_element_snapshot_session_name,
1290 current_session_name);
1291 if (ret) {
1292 goto end;
1293 }
1294
1295 /* Close snapshot element */
1296 ret = mi_lttng_writer_close_element(writer);
1297
1298end:
1299 return ret;
1300}
1301
1302LTTNG_HIDDEN
1303int mi_lttng_snapshot_add_output(struct mi_writer *writer,
1304 const char *current_session_name, const char *n_ptr,
1305 struct lttng_snapshot_output *output)
1306{
1307 int ret;
1308
1309 /* Open element snapshot */
1310 ret = mi_lttng_writer_open_element(writer,
1311 mi_lttng_element_command_snapshot);
1312 if (ret) {
1313 goto end;
1314 }
1315
1316 /* Snapshot output id */
1317 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1318 mi_lttng_element_id, output->id);
1319 if (ret) {
1320 goto end;
1321 }
1322
1323 /* Snapshot output names */
1324 ret = mi_lttng_writer_write_element_string(writer,
1325 config_element_name, n_ptr);
1326 if (ret) {
1327 goto end;
1328 }
1329
1330 /* Destination of the output (ctrl_url)*/
1331 ret = mi_lttng_writer_write_element_string(writer,
1332 mi_lttng_element_snapshot_ctrl_url, output->ctrl_url);
1333 if (ret) {
1334 goto end;
1335 }
1336
1337 /* Snapshot added for session "current_session_name"*/
1338 ret = mi_lttng_writer_write_element_string(writer,
1339 mi_lttng_element_snapshot_session_name, current_session_name);
1340 if (ret) {
1341 goto end;
1342 }
1343
1344 /* total size of all stream combined */
1345 ret = mi_lttng_writer_write_element_unsigned_int(writer,
1346 mi_lttng_element_snapshot_max_size, output->max_size);
1347 if (ret) {
1348 goto end;
1349 }
1350
1351 /* Close snapshot element */
1352 ret = mi_lttng_writer_close_element(writer);
1353
1354end:
1355 return ret;
1356}
1357
1358LTTNG_HIDDEN
1359int mi_lttng_snapshot_record(struct mi_writer *writer,
1360 const char *current_session_name, const char *url,
1361 const char *cmdline_ctrl_url, const char *cmdline_data_url)
1362{
1363 int ret;
1364
1365 /* Open element snapshot */
1366 ret = mi_lttng_writer_open_element(writer,
1367 mi_lttng_element_command_snapshot);
1368 if (ret) {
1369 goto end;
1370 }
1371
1372 /*
1373 * If a valid an URL was given, serialize it,
1374 * else take the command line data and ctrl urls*/
1375 if (url) {
1376 /* Destination of the output (ctrl_url)*/
1377 ret = mi_lttng_writer_write_element_string(writer,
1378 mi_lttng_element_snapshot_ctrl_url, url);
1379 if (ret) {
1380 goto end;
1381 }
1382 } else if (cmdline_ctrl_url) {
1383 /* Destination of the output (ctrl_url)*/
1384 ret = mi_lttng_writer_write_element_string(writer,
1385 mi_lttng_element_snapshot_ctrl_url, cmdline_ctrl_url);
1386 if (ret) {
1387 goto end;
1388 }
1389
1390 /* Destination of the output (data_url) */
1391 ret = mi_lttng_writer_write_element_string(writer,
1392 mi_lttng_element_snapshot_data_url, cmdline_data_url);
1393 if (ret) {
1394 goto end;
1395 }
1396 }
1397
1398 /* Close record_snapshot element */
1399 ret = mi_lttng_writer_close_element(writer);
1400
1401end:
1402 return ret;
1403}
This page took 0.077208 seconds and 5 git commands to generate.