| 1 | /* |
| 2 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | |
| 10 | #include <lttng/map/map.h> |
| 11 | |
| 12 | #include "common/argpar/argpar.h" |
| 13 | |
| 14 | #include "../command.h" |
| 15 | #ifdef LTTNG_EMBED_HELP |
| 16 | static const char help_msg[] = |
| 17 | #include <lttng-disable-map.1.h> |
| 18 | ; |
| 19 | #endif |
| 20 | |
| 21 | enum { |
| 22 | OPT_HELP, |
| 23 | OPT_KERNEL, |
| 24 | OPT_SESSION, |
| 25 | OPT_USERSPACE, |
| 26 | }; |
| 27 | |
| 28 | static const |
| 29 | struct argpar_opt_descr disable_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, |
| 35 | }; |
| 36 | |
| 37 | static |
| 38 | bool assign_string(char **dest, const char *src, const char *opt_name) |
| 39 | { |
| 40 | bool ret; |
| 41 | |
| 42 | if (*dest) { |
| 43 | ERR("Duplicate %s given.", opt_name); |
| 44 | goto error; |
| 45 | } |
| 46 | |
| 47 | *dest = strdup(src); |
| 48 | if (!*dest) { |
| 49 | ERR("Failed to allocate %s string.", opt_name); |
| 50 | goto error; |
| 51 | } |
| 52 | |
| 53 | ret = true; |
| 54 | goto end; |
| 55 | |
| 56 | error: |
| 57 | ret = false; |
| 58 | |
| 59 | end: |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | int cmd_disable_map(int argc, const char **argv) |
| 64 | { |
| 65 | int ret, i; |
| 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; |
| 73 | |
| 74 | argpar_parse_ret = argpar_parse(argc - 1, argv + 1, |
| 75 | disable_map_options, true); |
| 76 | if (!argpar_parse_ret.items) { |
| 77 | ERR("%s", argpar_parse_ret.error); |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | for (i = 0; i < argpar_parse_ret.items->n_items; i++) { |
| 82 | struct argpar_item *item = argpar_parse_ret.items->items[i]; |
| 83 | |
| 84 | if (item->type == ARGPAR_ITEM_TYPE_OPT) { |
| 85 | struct argpar_item_opt *item_opt = |
| 86 | (struct argpar_item_opt *) item; |
| 87 | |
| 88 | switch (item_opt->descr->id) { |
| 89 | case OPT_HELP: |
| 90 | SHOW_HELP(); |
| 91 | ret = 0; |
| 92 | goto end; |
| 93 | case OPT_SESSION: |
| 94 | if (!assign_string(&opt_session_name, item_opt->arg, |
| 95 | "-s/--session")) { |
| 96 | goto error; |
| 97 | } |
| 98 | break; |
| 99 | case OPT_USERSPACE: |
| 100 | opt_userspace = true; |
| 101 | break; |
| 102 | case OPT_KERNEL: |
| 103 | opt_kernel = true; |
| 104 | break; |
| 105 | default: |
| 106 | abort(); |
| 107 | } |
| 108 | |
| 109 | } else { |
| 110 | struct argpar_item_non_opt *item_non_opt = |
| 111 | (struct argpar_item_non_opt *) item; |
| 112 | |
| 113 | if (opt_map_name) { |
| 114 | ERR("Unexpected argument: %s", item_non_opt->arg); |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | opt_map_name = item_non_opt->arg; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!opt_map_name) { |
| 123 | ERR("Missing `name` argument."); |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | if (!opt_session_name) { |
| 128 | session_name = get_session_name(); |
| 129 | if (session_name == NULL) { |
| 130 | goto error; |
| 131 | } |
| 132 | } else { |
| 133 | session_name = opt_session_name; |
| 134 | } |
| 135 | |
| 136 | /* Check that one and only one domain option was provided. */ |
| 137 | ret = print_missing_or_multiple_domains( |
| 138 | opt_kernel + opt_userspace, false); |
| 139 | if (ret) { |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | if (opt_kernel) { |
| 144 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 145 | dom.buf_type = LTTNG_BUFFER_GLOBAL; |
| 146 | } else { |
| 147 | dom.type=LTTNG_DOMAIN_UST; |
| 148 | dom.buf_type = LTTNG_BUFFER_PER_UID; |
| 149 | } |
| 150 | |
| 151 | handle = lttng_create_handle(session_name, &dom); |
| 152 | if (handle == NULL) { |
| 153 | ret = -1; |
| 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | error_code_ret = lttng_disable_map(handle, opt_map_name); |
| 158 | if (error_code_ret != LTTNG_OK) { |
| 159 | ERR("Error disabling map \"%s\"", opt_map_name); |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | MSG("Disabled map `%s`.", opt_map_name); |
| 164 | |
| 165 | ret = 0; |
| 166 | goto end; |
| 167 | |
| 168 | error: |
| 169 | ret = 1; |
| 170 | |
| 171 | end: |
| 172 | argpar_parse_ret_fini(&argpar_parse_ret); |
| 173 | |
| 174 | return ret; |
| 175 | } |