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