treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / acpi / acpica / dbxface.c
blob3eb45ea93e5e98d0b7ef3fb0429c8a6cec254b97
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: dbxface - AML Debugger external interfaces
6 ******************************************************************************/
8 #include <acpi/acpi.h>
9 #include "accommon.h"
10 #include "amlcode.h"
11 #include "acdebug.h"
12 #include "acinterp.h"
13 #include "acparser.h"
15 #define _COMPONENT ACPI_CA_DEBUGGER
16 ACPI_MODULE_NAME("dbxface")
18 /* Local prototypes */
19 static acpi_status
20 acpi_db_start_command(struct acpi_walk_state *walk_state,
21 union acpi_parse_object *op);
23 #ifdef ACPI_OBSOLETE_FUNCTIONS
24 void acpi_db_method_end(struct acpi_walk_state *walk_state);
25 #endif
27 #ifdef ACPI_DISASSEMBLER
28 static union acpi_parse_object *acpi_db_get_display_op(struct acpi_walk_state
29 *walk_state,
30 union acpi_parse_object
31 *op);
32 #endif
34 /*******************************************************************************
36 * FUNCTION: acpi_db_start_command
38 * PARAMETERS: walk_state - Current walk
39 * op - Current executing Op, from AML interpreter
41 * RETURN: Status
43 * DESCRIPTION: Enter debugger command loop
45 ******************************************************************************/
47 static acpi_status
48 acpi_db_start_command(struct acpi_walk_state *walk_state,
49 union acpi_parse_object *op)
51 acpi_status status;
53 /* TBD: [Investigate] are there namespace locking issues here? */
55 /* acpi_ut_release_mutex (ACPI_MTX_NAMESPACE); */
57 /* Go into the command loop and await next user command */
59 acpi_gbl_method_executing = TRUE;
60 status = AE_CTRL_TRUE;
62 while (status == AE_CTRL_TRUE) {
64 /* Notify the completion of the command */
66 status = acpi_os_notify_command_complete();
67 if (ACPI_FAILURE(status)) {
68 goto error_exit;
71 /* Wait the readiness of the command */
73 status = acpi_os_wait_command_ready();
74 if (ACPI_FAILURE(status)) {
75 goto error_exit;
78 status =
79 acpi_db_command_dispatch(acpi_gbl_db_line_buf, walk_state,
80 op);
83 /* acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE); */
85 error_exit:
86 if (ACPI_FAILURE(status) && status != AE_CTRL_TERMINATE) {
87 ACPI_EXCEPTION((AE_INFO, status,
88 "While parsing/handling command line"));
90 return (status);
93 /*******************************************************************************
95 * FUNCTION: acpi_db_signal_break_point
97 * PARAMETERS: walk_state - Current walk
99 * RETURN: Status
101 * DESCRIPTION: Called for AML_BREAKPOINT_OP
103 ******************************************************************************/
105 void acpi_db_signal_break_point(struct acpi_walk_state *walk_state)
108 #ifndef ACPI_APPLICATION
109 if (acpi_gbl_db_thread_id != acpi_os_get_thread_id()) {
110 return;
112 #endif
115 * Set the single-step flag. This will cause the debugger (if present)
116 * to break to the console within the AML debugger at the start of the
117 * next AML instruction.
119 acpi_gbl_cm_single_step = TRUE;
120 acpi_os_printf("**break** Executed AML BreakPoint opcode\n");
123 #ifdef ACPI_DISASSEMBLER
124 /*******************************************************************************
126 * FUNCTION: acpi_db_get_display_op
128 * PARAMETERS: walk_state - Current walk
129 * op - Current executing op (from aml interpreter)
131 * RETURN: Opcode to display
133 * DESCRIPTION: Find the opcode to display during single stepping
135 ******************************************************************************/
137 static union acpi_parse_object *acpi_db_get_display_op(struct acpi_walk_state
138 *walk_state,
139 union acpi_parse_object
140 *op)
142 union acpi_parse_object *display_op;
143 union acpi_parse_object *parent_op;
145 display_op = op;
146 parent_op = op->common.parent;
147 if (parent_op) {
148 if ((walk_state->control_state) &&
149 (walk_state->control_state->common.state ==
150 ACPI_CONTROL_PREDICATE_EXECUTING)) {
152 * We are executing the predicate of an IF or WHILE statement
153 * Search upwards for the containing IF or WHILE so that the
154 * entire predicate can be displayed.
156 while (parent_op) {
157 if ((parent_op->common.aml_opcode == AML_IF_OP)
158 || (parent_op->common.aml_opcode ==
159 AML_WHILE_OP)) {
160 display_op = parent_op;
161 break;
163 parent_op = parent_op->common.parent;
165 } else {
166 while (parent_op) {
167 if ((parent_op->common.aml_opcode == AML_IF_OP)
168 || (parent_op->common.aml_opcode ==
169 AML_ELSE_OP)
170 || (parent_op->common.aml_opcode ==
171 AML_SCOPE_OP)
172 || (parent_op->common.aml_opcode ==
173 AML_METHOD_OP)
174 || (parent_op->common.aml_opcode ==
175 AML_WHILE_OP)) {
176 break;
178 display_op = parent_op;
179 parent_op = parent_op->common.parent;
183 return display_op;
185 #endif
187 /*******************************************************************************
189 * FUNCTION: acpi_db_single_step
191 * PARAMETERS: walk_state - Current walk
192 * op - Current executing op (from aml interpreter)
193 * opcode_class - Class of the current AML Opcode
195 * RETURN: Status
197 * DESCRIPTION: Called just before execution of an AML opcode.
199 ******************************************************************************/
201 acpi_status
202 acpi_db_single_step(struct acpi_walk_state *walk_state,
203 union acpi_parse_object *op, u32 opcode_class)
205 union acpi_parse_object *next;
206 acpi_status status = AE_OK;
207 u32 original_debug_level;
208 u32 aml_offset;
210 ACPI_FUNCTION_ENTRY();
212 #ifndef ACPI_APPLICATION
213 if (acpi_gbl_db_thread_id != acpi_os_get_thread_id()) {
214 return (AE_OK);
216 #endif
218 /* Check the abort flag */
220 if (acpi_gbl_abort_method) {
221 acpi_gbl_abort_method = FALSE;
222 return (AE_ABORT_METHOD);
225 aml_offset = (u32)ACPI_PTR_DIFF(op->common.aml,
226 walk_state->parser_state.aml_start);
228 /* Check for single-step breakpoint */
230 if (walk_state->method_breakpoint &&
231 (walk_state->method_breakpoint <= aml_offset)) {
233 /* Check if the breakpoint has been reached or passed */
234 /* Hit the breakpoint, resume single step, reset breakpoint */
236 acpi_os_printf("***Break*** at AML offset %X\n", aml_offset);
237 acpi_gbl_cm_single_step = TRUE;
238 acpi_gbl_step_to_next_call = FALSE;
239 walk_state->method_breakpoint = 0;
242 /* Check for user breakpoint (Must be on exact Aml offset) */
244 else if (walk_state->user_breakpoint &&
245 (walk_state->user_breakpoint == aml_offset)) {
246 acpi_os_printf("***UserBreakpoint*** at AML offset %X\n",
247 aml_offset);
248 acpi_gbl_cm_single_step = TRUE;
249 acpi_gbl_step_to_next_call = FALSE;
250 walk_state->method_breakpoint = 0;
254 * Check if this is an opcode that we are interested in --
255 * namely, opcodes that have arguments
257 if (op->common.aml_opcode == AML_INT_NAMEDFIELD_OP) {
258 return (AE_OK);
261 switch (opcode_class) {
262 case AML_CLASS_UNKNOWN:
263 case AML_CLASS_ARGUMENT: /* constants, literals, etc. do nothing */
265 return (AE_OK);
267 default:
269 /* All other opcodes -- continue */
270 break;
274 * Under certain debug conditions, display this opcode and its operands
276 if ((acpi_gbl_db_output_to_file) ||
277 (acpi_gbl_cm_single_step) || (acpi_dbg_level & ACPI_LV_PARSE)) {
278 if ((acpi_gbl_db_output_to_file) ||
279 (acpi_dbg_level & ACPI_LV_PARSE)) {
280 acpi_os_printf
281 ("\nAML Debug: Next AML Opcode to execute:\n");
285 * Display this op (and only this op - zero out the NEXT field
286 * temporarily, and disable parser trace output for the duration of
287 * the display because we don't want the extraneous debug output)
289 original_debug_level = acpi_dbg_level;
290 acpi_dbg_level &= ~(ACPI_LV_PARSE | ACPI_LV_FUNCTIONS);
291 next = op->common.next;
292 op->common.next = NULL;
294 /* Now we can disassemble and display it */
296 #ifdef ACPI_DISASSEMBLER
297 acpi_dm_disassemble(walk_state,
298 acpi_db_get_display_op(walk_state, op),
299 ACPI_UINT32_MAX);
300 #else
302 * The AML Disassembler is not configured - at least we can
303 * display the opcode value and name
305 acpi_os_printf("AML Opcode: %4.4X %s\n", op->common.aml_opcode,
306 acpi_ps_get_opcode_name(op->common.aml_opcode));
307 #endif
309 if ((op->common.aml_opcode == AML_IF_OP) ||
310 (op->common.aml_opcode == AML_WHILE_OP)) {
311 if (walk_state->control_state->common.value) {
312 acpi_os_printf
313 ("Predicate = [True], IF block was executed\n");
314 } else {
315 acpi_os_printf
316 ("Predicate = [False], Skipping IF block\n");
318 } else if (op->common.aml_opcode == AML_ELSE_OP) {
319 acpi_os_printf
320 ("Predicate = [False], ELSE block was executed\n");
323 /* Restore everything */
325 op->common.next = next;
326 acpi_os_printf("\n");
327 if ((acpi_gbl_db_output_to_file) ||
328 (acpi_dbg_level & ACPI_LV_PARSE)) {
329 acpi_os_printf("\n");
331 acpi_dbg_level = original_debug_level;
334 /* If we are not single stepping, just continue executing the method */
336 if (!acpi_gbl_cm_single_step) {
337 return (AE_OK);
341 * If we are executing a step-to-call command,
342 * Check if this is a method call.
344 if (acpi_gbl_step_to_next_call) {
345 if (op->common.aml_opcode != AML_INT_METHODCALL_OP) {
347 /* Not a method call, just keep executing */
349 return (AE_OK);
352 /* Found a method call, stop executing */
354 acpi_gbl_step_to_next_call = FALSE;
358 * If the next opcode is a method call, we will "step over" it
359 * by default.
361 if (op->common.aml_opcode == AML_INT_METHODCALL_OP) {
363 /* Force no more single stepping while executing called method */
365 acpi_gbl_cm_single_step = FALSE;
368 * Set the breakpoint on/before the call, it will stop execution
369 * as soon as we return
371 walk_state->method_breakpoint = 1; /* Must be non-zero! */
374 acpi_ex_exit_interpreter();
375 status = acpi_db_start_command(walk_state, op);
376 acpi_ex_enter_interpreter();
378 /* User commands complete, continue execution of the interrupted method */
380 return (status);
383 /*******************************************************************************
385 * FUNCTION: acpi_initialize_debugger
387 * PARAMETERS: None
389 * RETURN: Status
391 * DESCRIPTION: Init and start debugger
393 ******************************************************************************/
395 acpi_status acpi_initialize_debugger(void)
397 acpi_status status;
399 ACPI_FUNCTION_TRACE(acpi_initialize_debugger);
401 /* Init globals */
403 acpi_gbl_db_buffer = NULL;
404 acpi_gbl_db_filename = NULL;
405 acpi_gbl_db_output_to_file = FALSE;
407 acpi_gbl_db_debug_level = ACPI_LV_VERBOSITY2;
408 acpi_gbl_db_console_debug_level = ACPI_NORMAL_DEFAULT | ACPI_LV_TABLES;
409 acpi_gbl_db_output_flags = ACPI_DB_CONSOLE_OUTPUT;
411 acpi_gbl_db_opt_no_ini_methods = FALSE;
413 acpi_gbl_db_buffer = acpi_os_allocate(ACPI_DEBUG_BUFFER_SIZE);
414 if (!acpi_gbl_db_buffer) {
415 return_ACPI_STATUS(AE_NO_MEMORY);
417 memset(acpi_gbl_db_buffer, 0, ACPI_DEBUG_BUFFER_SIZE);
419 /* Initial scope is the root */
421 acpi_gbl_db_scope_buf[0] = AML_ROOT_PREFIX;
422 acpi_gbl_db_scope_buf[1] = 0;
423 acpi_gbl_db_scope_node = acpi_gbl_root_node;
425 /* Initialize user commands loop */
427 acpi_gbl_db_terminate_loop = FALSE;
430 * If configured for multi-thread support, the debug executor runs in
431 * a separate thread so that the front end can be in another address
432 * space, environment, or even another machine.
434 if (acpi_gbl_debugger_configuration & DEBUGGER_MULTI_THREADED) {
436 /* These were created with one unit, grab it */
438 status = acpi_os_initialize_debugger();
439 if (ACPI_FAILURE(status)) {
440 acpi_os_printf("Could not get debugger mutex\n");
441 return_ACPI_STATUS(status);
444 /* Create the debug execution thread to execute commands */
446 acpi_gbl_db_threads_terminated = FALSE;
447 status = acpi_os_execute(OSL_DEBUGGER_MAIN_THREAD,
448 acpi_db_execute_thread, NULL);
449 if (ACPI_FAILURE(status)) {
450 ACPI_EXCEPTION((AE_INFO, status,
451 "Could not start debugger thread"));
452 acpi_gbl_db_threads_terminated = TRUE;
453 return_ACPI_STATUS(status);
455 } else {
456 acpi_gbl_db_thread_id = acpi_os_get_thread_id();
459 return_ACPI_STATUS(AE_OK);
462 ACPI_EXPORT_SYMBOL(acpi_initialize_debugger)
464 /*******************************************************************************
466 * FUNCTION: acpi_terminate_debugger
468 * PARAMETERS: None
470 * RETURN: None
472 * DESCRIPTION: Stop debugger
474 ******************************************************************************/
475 void acpi_terminate_debugger(void)
478 /* Terminate the AML Debugger */
480 acpi_gbl_db_terminate_loop = TRUE;
482 if (acpi_gbl_debugger_configuration & DEBUGGER_MULTI_THREADED) {
484 /* Wait the AML Debugger threads */
486 while (!acpi_gbl_db_threads_terminated) {
487 acpi_os_sleep(100);
490 acpi_os_terminate_debugger();
493 if (acpi_gbl_db_buffer) {
494 acpi_os_free(acpi_gbl_db_buffer);
495 acpi_gbl_db_buffer = NULL;
498 /* Ensure that debug output is now disabled */
500 acpi_gbl_db_output_flags = ACPI_DB_DISABLE_OUTPUT;
503 ACPI_EXPORT_SYMBOL(acpi_terminate_debugger)
505 /*******************************************************************************
507 * FUNCTION: acpi_set_debugger_thread_id
509 * PARAMETERS: thread_id - Debugger thread ID
511 * RETURN: None
513 * DESCRIPTION: Set debugger thread ID
515 ******************************************************************************/
516 void acpi_set_debugger_thread_id(acpi_thread_id thread_id)
518 acpi_gbl_db_thread_id = thread_id;
521 ACPI_EXPORT_SYMBOL(acpi_set_debugger_thread_id)