xref: /spdk/lib/trace/trace_rpc.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
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 };
42 
43 static void
44 free_rpc_tpoint_group(struct rpc_tpoint_group *p)
45 {
46 	free(p->name);
47 }
48 
49 static const struct spdk_json_object_decoder rpc_tpoint_group_decoders[] = {
50 	{"name", offsetof(struct rpc_tpoint_group, name), spdk_json_decode_string},
51 };
52 
53 static void
54 rpc_trace_enable_tpoint_group(struct spdk_jsonrpc_request *request,
55 			      const struct spdk_json_val *params)
56 {
57 	struct rpc_tpoint_group req = {};
58 
59 	if (spdk_json_decode_object(params, rpc_tpoint_group_decoders,
60 				    SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) {
61 		SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n");
62 		goto invalid;
63 	}
64 
65 	if (req.name == NULL) {
66 		SPDK_DEBUGLOG(trace, "flag was NULL\n");
67 		goto invalid;
68 	}
69 
70 	if (spdk_trace_enable_tpoint_group(req.name)) {
71 		goto invalid;
72 	}
73 
74 	free_rpc_tpoint_group(&req);
75 
76 	spdk_jsonrpc_send_bool_response(request, true);
77 	return;
78 
79 invalid:
80 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
81 	free_rpc_tpoint_group(&req);
82 }
83 SPDK_RPC_REGISTER("trace_enable_tpoint_group", rpc_trace_enable_tpoint_group,
84 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
85 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(trace_enable_tpoint_group, enable_tpoint_group)
86 
87 static void
88 rpc_trace_disable_tpoint_group(struct spdk_jsonrpc_request *request,
89 			       const struct spdk_json_val *params)
90 {
91 	struct rpc_tpoint_group req = {};
92 
93 	if (spdk_json_decode_object(params, rpc_tpoint_group_decoders,
94 				    SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) {
95 		SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n");
96 		goto invalid;
97 	}
98 
99 	if (req.name == NULL) {
100 		SPDK_DEBUGLOG(trace, "flag was NULL\n");
101 		goto invalid;
102 	}
103 
104 	if (spdk_trace_disable_tpoint_group(req.name)) {
105 		goto invalid;
106 	}
107 
108 	free_rpc_tpoint_group(&req);
109 
110 	spdk_jsonrpc_send_bool_response(request, true);
111 	return;
112 
113 invalid:
114 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
115 	free_rpc_tpoint_group(&req);
116 }
117 SPDK_RPC_REGISTER("trace_disable_tpoint_group", rpc_trace_disable_tpoint_group,
118 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
119 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(trace_disable_tpoint_group, disable_tpoint_group)
120 
121 static void
122 rpc_trace_get_tpoint_group_mask(struct spdk_jsonrpc_request *request,
123 				const struct spdk_json_val *params)
124 {
125 	uint64_t tpoint_group_mask;
126 	char mask_str[7];
127 	bool enabled;
128 	struct spdk_json_write_ctx *w;
129 	struct spdk_trace_register_fn *register_fn;
130 
131 	if (params != NULL) {
132 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
133 						 "trace_get_tpoint_group_mask requires no parameters");
134 		return;
135 	}
136 
137 	w = spdk_jsonrpc_begin_result(request);
138 	tpoint_group_mask = spdk_trace_get_tpoint_group_mask();
139 
140 	spdk_json_write_object_begin(w);
141 
142 	snprintf(mask_str, sizeof(mask_str), "0x%" PRIx64, tpoint_group_mask);
143 	spdk_json_write_named_string(w, "tpoint_group_mask", mask_str);
144 
145 	register_fn = spdk_trace_get_first_register_fn();
146 	while (register_fn) {
147 		enabled = spdk_trace_get_tpoint_mask(register_fn->tgroup_id) != 0;
148 
149 		spdk_json_write_named_object_begin(w, register_fn->name);
150 		spdk_json_write_named_bool(w, "enabled", enabled);
151 
152 		snprintf(mask_str, sizeof(mask_str), "0x%lx", (1UL << register_fn->tgroup_id));
153 		spdk_json_write_named_string(w, "mask", mask_str);
154 		spdk_json_write_object_end(w);
155 
156 		register_fn = spdk_trace_get_next_register_fn(register_fn);
157 	}
158 
159 	spdk_json_write_object_end(w);
160 	spdk_jsonrpc_end_result(request, w);
161 }
162 SPDK_RPC_REGISTER("trace_get_tpoint_group_mask", rpc_trace_get_tpoint_group_mask,
163 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
164 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(trace_get_tpoint_group_mask, get_tpoint_group_mask)
165