2 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <lttng/map/map.h>
12 #include "common/argpar/argpar.h"
14 #include "../command.h"
15 #ifdef LTTNG_EMBED_HELP
16 static const char help_msg
[] =
17 #include <lttng-enable-map.1.h>
29 struct argpar_opt_descr enable_map_options
[] = {
30 { OPT_HELP
, 'h', "help", false },
31 { OPT_SESSION
, 's', "session", true },
32 { OPT_USERSPACE
, 'u', "userspace", false },
33 { OPT_KERNEL
, 'k', "kernel", false },
34 ARGPAR_OPT_DESCR_SENTINEL
,
38 bool assign_string(char **dest
, const char *src
, const char *opt_name
)
43 ERR("Duplicate %s given.", opt_name
);
49 ERR("Failed to allocate %s string.", opt_name
);
63 int cmd_enable_map(int argc
, const char **argv
)
66 struct argpar_parse_ret argpar_parse_ret
= { 0 };
67 const char *opt_map_name
= NULL
;
68 enum lttng_error_code error_code_ret
;
69 bool opt_userspace
= false, opt_kernel
= false;
70 char *opt_session_name
= NULL
, *session_name
= NULL
;
71 struct lttng_domain dom
= {0};
72 struct lttng_handle
*handle
;
74 argpar_parse_ret
= argpar_parse(argc
- 1, argv
+ 1,
75 enable_map_options
, true);
76 if (!argpar_parse_ret
.items
) {
77 ERR("%s", argpar_parse_ret
.error
);
81 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
82 struct argpar_item
*item
= argpar_parse_ret
.items
->items
[i
];
84 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
85 struct argpar_item_opt
*item_opt
=
86 (struct argpar_item_opt
*) item
;
88 switch (item_opt
->descr
->id
) {
94 if (!assign_string(&opt_session_name
, item_opt
->arg
,
100 opt_userspace
= true;
110 struct argpar_item_non_opt
*item_non_opt
=
111 (struct argpar_item_non_opt
*) item
;
114 ERR("Unexpected argument: %s", item_non_opt
->arg
);
118 opt_map_name
= item_non_opt
->arg
;
123 ERR("Missing `name` argument.");
127 if (!opt_session_name
) {
128 session_name
= get_session_name();
129 if (session_name
== NULL
) {
133 session_name
= opt_session_name
;
136 /* Check that one and only one domain option was provided. */
137 ret
= print_missing_or_multiple_domains(
138 opt_kernel
+ opt_userspace
, false);
144 dom
.type
= LTTNG_DOMAIN_KERNEL
;
145 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
147 dom
.type
=LTTNG_DOMAIN_UST
;
148 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
151 handle
= lttng_create_handle(session_name
, &dom
);
152 if (handle
== NULL
) {
157 error_code_ret
= lttng_enable_map(handle
, opt_map_name
);
158 if (error_code_ret
!= LTTNG_OK
) {
159 ERR("Error enabling map \"%s\"", opt_map_name
);
163 MSG("Enabled map `%s`.", opt_map_name
);
172 argpar_parse_ret_fini(&argpar_parse_ret
);