1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/rpc.h" 35 #include "spdk/util.h" 36 #include "spdk/trace.h" 37 #include "spdk/log.h" 38 39 struct rpc_tpoint_group { 40 char *name; 41 uint64_t tpoint_mask; 42 }; 43 44 static void 45 free_rpc_tpoint_group(struct rpc_tpoint_group *p) 46 { 47 free(p->name); 48 } 49 50 static const struct spdk_json_object_decoder rpc_tpoint_mask_decoders[] = { 51 {"name", offsetof(struct rpc_tpoint_group, name), spdk_json_decode_string}, 52 {"tpoint_mask", offsetof(struct rpc_tpoint_group, tpoint_mask), spdk_json_decode_uint64, true}, 53 }; 54 55 static void 56 rpc_trace_set_tpoint_mask(struct spdk_jsonrpc_request *request, 57 const struct spdk_json_val *params) 58 { 59 struct rpc_tpoint_group req = {}; 60 uint64_t tpoint_group_mask = 0; 61 62 if (spdk_json_decode_object(params, rpc_tpoint_mask_decoders, 63 SPDK_COUNTOF(rpc_tpoint_mask_decoders), &req)) { 64 SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n"); 65 goto invalid; 66 } 67 68 if (req.name == NULL) { 69 SPDK_DEBUGLOG(trace, "flag was NULL\n"); 70 goto invalid; 71 } 72 73 tpoint_group_mask = spdk_trace_create_tpoint_group_mask(req.name); 74 if (tpoint_group_mask == 0) { 75 goto invalid; 76 } 77 78 spdk_trace_set_tpoints(spdk_u64log2(tpoint_group_mask), req.tpoint_mask); 79 80 free_rpc_tpoint_group(&req); 81 82 spdk_jsonrpc_send_bool_response(request, true); 83 return; 84 85 invalid: 86 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 87 free_rpc_tpoint_group(&req); 88 } 89 SPDK_RPC_REGISTER("trace_set_tpoint_mask", rpc_trace_set_tpoint_mask, 90 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 91 92 static void 93 rpc_trace_clear_tpoint_mask(struct spdk_jsonrpc_request *request, 94 const struct spdk_json_val *params) 95 { 96 struct rpc_tpoint_group req = {}; 97 uint64_t tpoint_group_mask = 0; 98 99 if (spdk_json_decode_object(params, rpc_tpoint_mask_decoders, 100 SPDK_COUNTOF(rpc_tpoint_mask_decoders), &req)) { 101 SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n"); 102 goto invalid; 103 } 104 105 if (req.name == NULL) { 106 SPDK_DEBUGLOG(trace, "flag was NULL\n"); 107 goto invalid; 108 } 109 110 tpoint_group_mask = spdk_trace_create_tpoint_group_mask(req.name); 111 if (tpoint_group_mask == 0) { 112 goto invalid; 113 } 114 115 spdk_trace_clear_tpoints(spdk_u64log2(tpoint_group_mask), req.tpoint_mask); 116 117 free_rpc_tpoint_group(&req); 118 119 spdk_jsonrpc_send_bool_response(request, true); 120 return; 121 122 invalid: 123 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 124 free_rpc_tpoint_group(&req); 125 } 126 SPDK_RPC_REGISTER("trace_clear_tpoint_mask", rpc_trace_clear_tpoint_mask, 127 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 128 129 static const struct spdk_json_object_decoder rpc_tpoint_group_decoders[] = { 130 {"name", offsetof(struct rpc_tpoint_group, name), spdk_json_decode_string}, 131 }; 132 133 static void 134 rpc_trace_enable_tpoint_group(struct spdk_jsonrpc_request *request, 135 const struct spdk_json_val *params) 136 { 137 struct rpc_tpoint_group req = {}; 138 139 if (spdk_json_decode_object(params, rpc_tpoint_group_decoders, 140 SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) { 141 SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n"); 142 goto invalid; 143 } 144 145 if (req.name == NULL) { 146 SPDK_DEBUGLOG(trace, "flag was NULL\n"); 147 goto invalid; 148 } 149 150 if (spdk_trace_enable_tpoint_group(req.name)) { 151 goto invalid; 152 } 153 154 free_rpc_tpoint_group(&req); 155 156 spdk_jsonrpc_send_bool_response(request, true); 157 return; 158 159 invalid: 160 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 161 free_rpc_tpoint_group(&req); 162 } 163 SPDK_RPC_REGISTER("trace_enable_tpoint_group", rpc_trace_enable_tpoint_group, 164 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 165 166 static void 167 rpc_trace_disable_tpoint_group(struct spdk_jsonrpc_request *request, 168 const struct spdk_json_val *params) 169 { 170 struct rpc_tpoint_group req = {}; 171 172 if (spdk_json_decode_object(params, rpc_tpoint_group_decoders, 173 SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) { 174 SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n"); 175 goto invalid; 176 } 177 178 if (req.name == NULL) { 179 SPDK_DEBUGLOG(trace, "flag was NULL\n"); 180 goto invalid; 181 } 182 183 if (spdk_trace_disable_tpoint_group(req.name)) { 184 goto invalid; 185 } 186 187 free_rpc_tpoint_group(&req); 188 189 spdk_jsonrpc_send_bool_response(request, true); 190 return; 191 192 invalid: 193 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 194 free_rpc_tpoint_group(&req); 195 } 196 SPDK_RPC_REGISTER("trace_disable_tpoint_group", rpc_trace_disable_tpoint_group, 197 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 198 199 static void 200 rpc_trace_get_tpoint_group_mask(struct spdk_jsonrpc_request *request, 201 const struct spdk_json_val *params) 202 { 203 uint64_t tpoint_group_mask; 204 char mask_str[20]; 205 bool enabled; 206 struct spdk_json_write_ctx *w; 207 struct spdk_trace_register_fn *register_fn; 208 209 if (params != NULL) { 210 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 211 "trace_get_tpoint_group_mask requires no parameters"); 212 return; 213 } 214 215 w = spdk_jsonrpc_begin_result(request); 216 tpoint_group_mask = spdk_trace_get_tpoint_group_mask(); 217 218 spdk_json_write_object_begin(w); 219 220 snprintf(mask_str, sizeof(mask_str), "0x%" PRIx64, tpoint_group_mask); 221 spdk_json_write_named_string(w, "tpoint_group_mask", mask_str); 222 223 register_fn = spdk_trace_get_first_register_fn(); 224 while (register_fn) { 225 enabled = spdk_trace_get_tpoint_mask(register_fn->tgroup_id) != 0; 226 227 spdk_json_write_named_object_begin(w, register_fn->name); 228 spdk_json_write_named_bool(w, "enabled", enabled); 229 230 snprintf(mask_str, sizeof(mask_str), "0x%lx", (1UL << register_fn->tgroup_id)); 231 spdk_json_write_named_string(w, "mask", mask_str); 232 spdk_json_write_object_end(w); 233 234 register_fn = spdk_trace_get_next_register_fn(register_fn); 235 } 236 237 spdk_json_write_object_end(w); 238 spdk_jsonrpc_end_result(request, w); 239 } 240 SPDK_RPC_REGISTER("trace_get_tpoint_group_mask", rpc_trace_get_tpoint_group_mask, 241 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 242