Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2025 Huawei Technologies Co., Ltd
3 : : */
4 : :
5 : : #include "debug_command.h"
6 : :
7 : : #include <rte_debug.h>
8 : : #include <cmdline.h>
9 : : #include <cmdline_socket.h>
10 : : #include <cmdline_parse_num.h>
11 : :
12 : : #include <string.h>
13 : :
14 : :
15 : : #define EVENT_PATTERN \
16 : : "invalid-state#" \
17 : : "branch-enter#branch-prune#branch-return#branch-unreachable#" \
18 : : "jump-always#jump-conditional"
19 : :
20 : : #define REGISTER_PATTERN "r0#r1#r2#r3#r4#r5#r6#r7#r8#r9#r10"
21 : :
22 : : #define COMPARISON_OP_PATTERN "==#!=#<#<=#>#>=#s<#s<=#s>#s>="
23 : :
24 : : static void
25 : : handle_command(void *parsed_result, struct cmdline *cl, void *data);
26 : :
27 : : /* Keywords */
28 : : static cmdline_parse_token_string_t break_tok =
29 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "b#break");
30 : : static cmdline_parse_token_string_t may_tok =
31 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "may");
32 : : static cmdline_parse_token_string_t catch_tok =
33 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "catch");
34 : : static cmdline_parse_token_string_t clear_tok =
35 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "clear");
36 : : static cmdline_parse_token_string_t continue_tok =
37 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "c#continue");
38 : : static cmdline_parse_token_string_t delete_tok =
39 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "delete");
40 : : static cmdline_parse_token_string_t info_tok =
41 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "i#info");
42 : : static cmdline_parse_token_string_t info_points_tok =
43 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword,
44 : : "b#break#breakpoints#points");
45 : : static cmdline_parse_token_string_t info_frame_tok =
46 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "f#frame");
47 : : static cmdline_parse_token_string_t info_registers_tok =
48 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "r#registers");
49 : : static cmdline_parse_token_string_t list_tok =
50 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "l#list");
51 : : static cmdline_parse_token_string_t program_tok =
52 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "program");
53 : : static cmdline_parse_token_string_t quit_tok =
54 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "q#quit");
55 : : static cmdline_parse_token_string_t run_tok =
56 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "run");
57 : : static cmdline_parse_token_string_t start_tok =
58 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "start");
59 : : static cmdline_parse_token_string_t step_tok =
60 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "s#step");
61 : : static cmdline_parse_token_string_t where_tok =
62 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, keyword, "where");
63 : :
64 : : /* Variable tokens */
65 : : static cmdline_parse_token_string_t comparison_lhs_tok =
66 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, comparison.lhs, REGISTER_PATTERN);
67 : : static cmdline_parse_token_string_t comparison_op_tok =
68 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, comparison.op, COMPARISON_OP_PATTERN);
69 : : static cmdline_parse_token_num_t comparison_rhs_literal_tok =
70 : : TOKEN_NUM_INITIALIZER(struct debug_command_parsed, comparison.literal_rhs, RTE_INT64);
71 : : static cmdline_parse_token_string_t comparison_rhs_register_tok =
72 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, comparison.register_rhs,
73 : : REGISTER_PATTERN);
74 : : static cmdline_parse_token_string_t event_tok =
75 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, event, EVENT_PATTERN);
76 : : static cmdline_parse_token_num_t frame_offset_tok =
77 : : TOKEN_NUM_INITIALIZER(struct debug_command_parsed, frame_offset, RTE_INT64);
78 : : static cmdline_parse_token_num_t instruction_count_tok =
79 : : TOKEN_NUM_INITIALIZER(struct debug_command_parsed, instruction_count, RTE_UINT32);
80 : : static cmdline_parse_token_num_t pc_tok =
81 : : TOKEN_NUM_INITIALIZER(struct debug_command_parsed, instruction_offset, RTE_UINT32);
82 : : static cmdline_parse_token_num_t point_number_tok =
83 : : TOKEN_NUM_INITIALIZER(struct debug_command_parsed, point_number, RTE_UINT32);
84 : : static cmdline_parse_token_string_t register_tok =
85 : : TOKEN_STRING_INITIALIZER(struct debug_command_parsed, register_, REGISTER_PATTERN);
86 : :
87 : :
88 : : /* Commands */
89 : : static cmdline_parse_inst_t cmd_break = {
90 : : .f = handle_command,
91 : : .data = (void *)DEBUG_COMMAND_BREAK,
92 : : .help_str = "b|break: break at current instruction",
93 : : .tokens = {
94 : : (void *)&break_tok,
95 : : NULL,
96 : : }
97 : : };
98 : : static cmdline_parse_inst_t cmd_break_pc = {
99 : : .f = handle_command,
100 : : .data = (void *)DEBUG_COMMAND_BREAK_PC,
101 : : .help_str = "b|break <pc>: break at specified instruction",
102 : : .tokens = {
103 : : (void *)&break_tok,
104 : : (void *)&pc_tok,
105 : : NULL,
106 : : }
107 : : };
108 : : static cmdline_parse_inst_t cmd_catch = {
109 : : .f = handle_command,
110 : : .data = (void *)DEBUG_COMMAND_CATCH,
111 : : .help_str = "catch <event>: catch specified event",
112 : : .tokens = {
113 : : (void *)&catch_tok,
114 : : (void *)&event_tok,
115 : : NULL,
116 : : }
117 : : };
118 : : static cmdline_parse_inst_t cmd_clear = {
119 : : .f = handle_command,
120 : : .data = (void *)DEBUG_COMMAND_CLEAR,
121 : : .help_str = "clear: delete all breakpoints at current instruction",
122 : : .tokens = {
123 : : (void *)&clear_tok,
124 : : NULL,
125 : : }
126 : : };
127 : : static cmdline_parse_inst_t cmd_clear_event = {
128 : : .f = handle_command,
129 : : .data = (void *)DEBUG_COMMAND_CLEAR_EVENT,
130 : : .help_str = "clear <event>: delete all catchpoints for specified event",
131 : : .tokens = {
132 : : (void *)&clear_tok,
133 : : (void *)&event_tok,
134 : : NULL,
135 : : }
136 : : };
137 : : static cmdline_parse_inst_t cmd_clear_pc = {
138 : : .f = handle_command,
139 : : .data = (void *)DEBUG_COMMAND_CLEAR_PC,
140 : : .help_str = "clear <pc>: delete all breakpoints at specified instruction",
141 : : .tokens = {
142 : : (void *)&clear_tok,
143 : : (void *)&pc_tok,
144 : : NULL,
145 : : }
146 : : };
147 : : static cmdline_parse_inst_t cmd_continue = {
148 : : .f = handle_command,
149 : : .data = (void *)DEBUG_COMMAND_CONTINUE,
150 : : .help_str = "c|continue: continue validation",
151 : : .tokens = {
152 : : (void *)&continue_tok,
153 : : NULL,
154 : : }
155 : : };
156 : : static cmdline_parse_inst_t cmd_delete = {
157 : : .f = handle_command,
158 : : .data = (void *)DEBUG_COMMAND_DELETE,
159 : : .help_str = "delete: delete all breakpoints and catchpoints",
160 : : .tokens = {
161 : : (void *)&delete_tok,
162 : : NULL,
163 : : }
164 : : };
165 : : static cmdline_parse_inst_t cmd_delete_number = {
166 : : .f = handle_command,
167 : : .data = (void *)DEBUG_COMMAND_DELETE_NUMBER,
168 : : .help_str = "delete <point>: delete specified breakpoint or catchpoint",
169 : : .tokens = {
170 : : (void *)&delete_tok,
171 : : (void *)&point_number_tok,
172 : : NULL,
173 : : }
174 : : };
175 : : static cmdline_parse_inst_t cmd_info_frame = {
176 : : .f = handle_command,
177 : : .data = (void *)DEBUG_COMMAND_INFO_FRAME,
178 : : .help_str = "i|info f|frame: show information about all frame locations",
179 : : .tokens = {
180 : : (void *)&info_tok,
181 : : (void *)&info_frame_tok,
182 : : NULL,
183 : : }
184 : : };
185 : : static cmdline_parse_inst_t cmd_info_frame_offset = {
186 : : .f = handle_command,
187 : : .data = (void *)DEBUG_COMMAND_INFO_FRAME_OFFSET,
188 : : .help_str = "i|info f|frame -<offset>: show information about specified frame location",
189 : : .tokens = {
190 : : (void *)&info_tok,
191 : : (void *)&info_frame_tok,
192 : : (void *)&frame_offset_tok,
193 : : NULL,
194 : : }
195 : : };
196 : : static cmdline_parse_inst_t cmd_info_points = {
197 : : .f = handle_command,
198 : : .data = (void *)DEBUG_COMMAND_INFO_POINTS,
199 : : .help_str = "i|info b|break|breakpoints|points: "
200 : : "show information about all breakpoints and catchpoints",
201 : : .tokens = {
202 : : (void *)&info_tok,
203 : : (void *)&info_points_tok,
204 : : NULL,
205 : : }
206 : : };
207 : : static cmdline_parse_inst_t cmd_info_register = {
208 : : .f = handle_command,
209 : : .data = (void *)DEBUG_COMMAND_INFO_REGISTER,
210 : : .help_str = "i|info <register>: show information about specified register",
211 : : .tokens = {
212 : : (void *)&info_tok,
213 : : (void *)®ister_tok,
214 : : NULL,
215 : : }
216 : : };
217 : : static cmdline_parse_inst_t cmd_info_registers = {
218 : : .f = handle_command,
219 : : .data = (void *)DEBUG_COMMAND_INFO_REGISTERS,
220 : : .help_str = "i|info r|registers: show information about all registers",
221 : : .tokens = {
222 : : (void *)&info_tok,
223 : : (void *)&info_registers_tok,
224 : : NULL,
225 : : }
226 : : };
227 : : static cmdline_parse_inst_t cmd_list = {
228 : : .f = handle_command,
229 : : .data = (void *)DEBUG_COMMAND_LIST,
230 : : .help_str = "l|list: list ten instructions",
231 : : .tokens = {
232 : : (void *)&list_tok,
233 : : NULL,
234 : : }
235 : : };
236 : : static cmdline_parse_inst_t cmd_list_count = {
237 : : .f = handle_command,
238 : : .data = (void *)DEBUG_COMMAND_LIST_COUNT,
239 : : .help_str = "l|list <number>: list specified number of instructions",
240 : : .tokens = {
241 : : (void *)&list_tok,
242 : : (void *)&instruction_count_tok,
243 : : NULL,
244 : : }
245 : : };
246 : : static cmdline_parse_inst_t cmd_list_program = {
247 : : .f = handle_command,
248 : : .data = (void *)DEBUG_COMMAND_LIST_PROGRAM,
249 : : .help_str = "l|list program: list whole program",
250 : : .tokens = {
251 : : (void *)&list_tok,
252 : : (void *)&program_tok,
253 : : NULL,
254 : : }
255 : : };
256 : : static cmdline_parse_inst_t cmd_may_literal_rhs = {
257 : : .f = handle_command,
258 : : .data = (void *)DEBUG_COMMAND_MAY_LITERAL_RHS,
259 : : .help_str = "may <register> <comparison> <number>: "
260 : : "check if specified condition _may_ be true",
261 : : .tokens = {
262 : : (void *)&may_tok,
263 : : (void *)&comparison_lhs_tok,
264 : : (void *)&comparison_op_tok,
265 : : (void *)&comparison_rhs_literal_tok,
266 : : NULL,
267 : : }
268 : : };
269 : : static cmdline_parse_inst_t cmd_may_register_rhs = {
270 : : .f = handle_command,
271 : : .data = (void *)DEBUG_COMMAND_MAY_REGISTER_RHS,
272 : : .help_str = "may <register> <comparison> <register>: "
273 : : "check if specified condition _may_ be true",
274 : : .tokens = {
275 : : (void *)&may_tok,
276 : : (void *)&comparison_lhs_tok,
277 : : (void *)&comparison_op_tok,
278 : : (void *)&comparison_rhs_register_tok,
279 : : NULL,
280 : : }
281 : : };
282 : : static cmdline_parse_inst_t cmd_quit = {
283 : : .f = handle_command,
284 : : .data = (void *)DEBUG_COMMAND_QUIT,
285 : : .help_str = "quit: q|quit debugger",
286 : : .tokens = {
287 : : (void *)&quit_tok,
288 : : NULL,
289 : : }
290 : : };
291 : : static cmdline_parse_inst_t cmd_run = {
292 : : .f = handle_command,
293 : : .data = (void *)DEBUG_COMMAND_RUN,
294 : : .help_str = "run: re-run validation from the start",
295 : : .tokens = {
296 : : (void *)&run_tok,
297 : : NULL,
298 : : }
299 : : };
300 : : static cmdline_parse_inst_t cmd_start = {
301 : : .f = handle_command,
302 : : .data = (void *)DEBUG_COMMAND_START,
303 : : .help_str = "start: re-start validation and stop at start",
304 : : .tokens = {
305 : : (void *)&start_tok,
306 : : NULL,
307 : : }
308 : : };
309 : : static cmdline_parse_inst_t cmd_step = {
310 : : .f = handle_command,
311 : : .data = (void *)DEBUG_COMMAND_STEP,
312 : : .help_str = "s|step: validate one instruction",
313 : : .tokens = {
314 : : (void *)&step_tok,
315 : : NULL,
316 : : }
317 : : };
318 : : static cmdline_parse_inst_t cmd_where = {
319 : : .f = handle_command,
320 : : .data = (void *)DEBUG_COMMAND_WHERE,
321 : : .help_str = "where: show current branch stack",
322 : : .tokens = {
323 : : (void *)&where_tok,
324 : : NULL,
325 : : }
326 : : };
327 : :
328 : : static cmdline_parse_ctx_t debug_ctx[] = {
329 : : &cmd_break,
330 : : &cmd_break_pc,
331 : : &cmd_catch,
332 : : &cmd_clear,
333 : : &cmd_clear_event,
334 : : &cmd_clear_pc,
335 : : &cmd_continue,
336 : : &cmd_delete,
337 : : &cmd_delete_number,
338 : : &cmd_info_frame,
339 : : &cmd_info_frame_offset,
340 : : &cmd_info_points,
341 : : &cmd_info_register,
342 : : &cmd_info_registers,
343 : : &cmd_list,
344 : : &cmd_list_count,
345 : : &cmd_list_program,
346 : : &cmd_may_literal_rhs,
347 : : &cmd_may_register_rhs,
348 : : &cmd_quit,
349 : : &cmd_run,
350 : : &cmd_start,
351 : : &cmd_step,
352 : : &cmd_where,
353 : : NULL
354 : : };
355 : :
356 : : /* Receive, fill and return one command. */
357 : :
358 : : static enum debug_command debug_command;
359 : :
360 : : struct debug_command_parsed debug_command_parsed;
361 : :
362 : : static void
363 : 0 : handle_command(void *parsed_result, struct cmdline *cl, void *data)
364 : : {
365 : : RTE_BUILD_BUG_ON(sizeof(debug_command_parsed) > CMDLINE_PARSE_RESULT_BUFSIZE);
366 : : memcpy(&debug_command_parsed, parsed_result, sizeof(debug_command_parsed));
367 : 0 : debug_command = (uintptr_t)data;
368 : 0 : cmdline_quit(cl);
369 : 0 : }
370 : :
371 : : enum debug_command
372 : 0 : debug_command_get(const char *prompt)
373 : : {
374 : 0 : debug_command = DEBUG_COMMAND_EOF;
375 : 0 : struct cmdline *const cmdline = cmdline_stdin_new(debug_ctx, prompt);
376 : 0 : RTE_VERIFY(cmdline != NULL);
377 : 0 : cmdline_interact(cmdline);
378 : 0 : cmdline_stdin_exit(cmdline);
379 : : /* Clear prompt, or it would prepend first message that follows. */
380 : 0 : printf("\r%*s\r", (int)strlen(prompt), "");
381 : 0 : fflush(stdout);
382 : 0 : return debug_command;
383 : : }
|