dbxface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: dbxface - AML Debugger external interfaces
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "amlcode.h"
  10. #include "acdebug.h"
  11. #include "acinterp.h"
  12. #define _COMPONENT ACPI_CA_DEBUGGER
  13. ACPI_MODULE_NAME("dbxface")
  14. /* Local prototypes */
  15. static acpi_status
  16. acpi_db_start_command(struct acpi_walk_state *walk_state,
  17. union acpi_parse_object *op);
  18. #ifdef ACPI_OBSOLETE_FUNCTIONS
  19. void acpi_db_method_end(struct acpi_walk_state *walk_state);
  20. #endif
  21. /*******************************************************************************
  22. *
  23. * FUNCTION: acpi_db_start_command
  24. *
  25. * PARAMETERS: walk_state - Current walk
  26. * op - Current executing Op, from AML interpreter
  27. *
  28. * RETURN: Status
  29. *
  30. * DESCRIPTION: Enter debugger command loop
  31. *
  32. ******************************************************************************/
  33. static acpi_status
  34. acpi_db_start_command(struct acpi_walk_state *walk_state,
  35. union acpi_parse_object *op)
  36. {
  37. acpi_status status;
  38. /* TBD: [Investigate] are there namespace locking issues here? */
  39. /* acpi_ut_release_mutex (ACPI_MTX_NAMESPACE); */
  40. /* Go into the command loop and await next user command */
  41. acpi_gbl_method_executing = TRUE;
  42. status = AE_CTRL_TRUE;
  43. while (status == AE_CTRL_TRUE) {
  44. /* Notify the completion of the command */
  45. status = acpi_os_notify_command_complete();
  46. if (ACPI_FAILURE(status)) {
  47. goto error_exit;
  48. }
  49. /* Wait the readiness of the command */
  50. status = acpi_os_wait_command_ready();
  51. if (ACPI_FAILURE(status)) {
  52. goto error_exit;
  53. }
  54. status =
  55. acpi_db_command_dispatch(acpi_gbl_db_line_buf, walk_state,
  56. op);
  57. }
  58. /* acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE); */
  59. error_exit:
  60. if (ACPI_FAILURE(status) && status != AE_CTRL_TERMINATE) {
  61. ACPI_EXCEPTION((AE_INFO, status,
  62. "While parsing/handling command line"));
  63. }
  64. return (status);
  65. }
  66. /*******************************************************************************
  67. *
  68. * FUNCTION: acpi_db_signal_break_point
  69. *
  70. * PARAMETERS: walk_state - Current walk
  71. *
  72. * RETURN: Status
  73. *
  74. * DESCRIPTION: Called for AML_BREAKPOINT_OP
  75. *
  76. ******************************************************************************/
  77. void acpi_db_signal_break_point(struct acpi_walk_state *walk_state)
  78. {
  79. #ifndef ACPI_APPLICATION
  80. if (acpi_gbl_db_thread_id != acpi_os_get_thread_id()) {
  81. return;
  82. }
  83. #endif
  84. /*
  85. * Set the single-step flag. This will cause the debugger (if present)
  86. * to break to the console within the AML debugger at the start of the
  87. * next AML instruction.
  88. */
  89. acpi_gbl_cm_single_step = TRUE;
  90. acpi_os_printf("**break** Executed AML BreakPoint opcode\n");
  91. }
  92. /*******************************************************************************
  93. *
  94. * FUNCTION: acpi_db_single_step
  95. *
  96. * PARAMETERS: walk_state - Current walk
  97. * op - Current executing op (from aml interpreter)
  98. * opcode_class - Class of the current AML Opcode
  99. *
  100. * RETURN: Status
  101. *
  102. * DESCRIPTION: Called just before execution of an AML opcode.
  103. *
  104. ******************************************************************************/
  105. acpi_status
  106. acpi_db_single_step(struct acpi_walk_state *walk_state,
  107. union acpi_parse_object *op, u32 opcode_class)
  108. {
  109. union acpi_parse_object *next;
  110. acpi_status status = AE_OK;
  111. u32 original_debug_level;
  112. union acpi_parse_object *display_op;
  113. union acpi_parse_object *parent_op;
  114. u32 aml_offset;
  115. ACPI_FUNCTION_ENTRY();
  116. #ifndef ACPI_APPLICATION
  117. if (acpi_gbl_db_thread_id != acpi_os_get_thread_id()) {
  118. return (AE_OK);
  119. }
  120. #endif
  121. /* Check the abort flag */
  122. if (acpi_gbl_abort_method) {
  123. acpi_gbl_abort_method = FALSE;
  124. return (AE_ABORT_METHOD);
  125. }
  126. aml_offset = (u32)ACPI_PTR_DIFF(op->common.aml,
  127. walk_state->parser_state.aml_start);
  128. /* Check for single-step breakpoint */
  129. if (walk_state->method_breakpoint &&
  130. (walk_state->method_breakpoint <= aml_offset)) {
  131. /* Check if the breakpoint has been reached or passed */
  132. /* Hit the breakpoint, resume single step, reset breakpoint */
  133. acpi_os_printf("***Break*** at AML offset %X\n", aml_offset);
  134. acpi_gbl_cm_single_step = TRUE;
  135. acpi_gbl_step_to_next_call = FALSE;
  136. walk_state->method_breakpoint = 0;
  137. }
  138. /* Check for user breakpoint (Must be on exact Aml offset) */
  139. else if (walk_state->user_breakpoint &&
  140. (walk_state->user_breakpoint == aml_offset)) {
  141. acpi_os_printf("***UserBreakpoint*** at AML offset %X\n",
  142. aml_offset);
  143. acpi_gbl_cm_single_step = TRUE;
  144. acpi_gbl_step_to_next_call = FALSE;
  145. walk_state->method_breakpoint = 0;
  146. }
  147. /*
  148. * Check if this is an opcode that we are interested in --
  149. * namely, opcodes that have arguments
  150. */
  151. if (op->common.aml_opcode == AML_INT_NAMEDFIELD_OP) {
  152. return (AE_OK);
  153. }
  154. switch (opcode_class) {
  155. case AML_CLASS_UNKNOWN:
  156. case AML_CLASS_ARGUMENT: /* constants, literals, etc. do nothing */
  157. return (AE_OK);
  158. default:
  159. /* All other opcodes -- continue */
  160. break;
  161. }
  162. /*
  163. * Under certain debug conditions, display this opcode and its operands
  164. */
  165. if ((acpi_gbl_db_output_to_file) ||
  166. (acpi_gbl_cm_single_step) || (acpi_dbg_level & ACPI_LV_PARSE)) {
  167. if ((acpi_gbl_db_output_to_file) ||
  168. (acpi_dbg_level & ACPI_LV_PARSE)) {
  169. acpi_os_printf
  170. ("\nAML Debug: Next AML Opcode to execute:\n");
  171. }
  172. /*
  173. * Display this op (and only this op - zero out the NEXT field
  174. * temporarily, and disable parser trace output for the duration of
  175. * the display because we don't want the extraneous debug output)
  176. */
  177. original_debug_level = acpi_dbg_level;
  178. acpi_dbg_level &= ~(ACPI_LV_PARSE | ACPI_LV_FUNCTIONS);
  179. next = op->common.next;
  180. op->common.next = NULL;
  181. display_op = op;
  182. parent_op = op->common.parent;
  183. if (parent_op) {
  184. if ((walk_state->control_state) &&
  185. (walk_state->control_state->common.state ==
  186. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  187. /*
  188. * We are executing the predicate of an IF or WHILE statement
  189. * Search upwards for the containing IF or WHILE so that the
  190. * entire predicate can be displayed.
  191. */
  192. while (parent_op) {
  193. if ((parent_op->common.aml_opcode ==
  194. AML_IF_OP)
  195. || (parent_op->common.aml_opcode ==
  196. AML_WHILE_OP)) {
  197. display_op = parent_op;
  198. break;
  199. }
  200. parent_op = parent_op->common.parent;
  201. }
  202. } else {
  203. while (parent_op) {
  204. if ((parent_op->common.aml_opcode ==
  205. AML_IF_OP)
  206. || (parent_op->common.aml_opcode ==
  207. AML_ELSE_OP)
  208. || (parent_op->common.aml_opcode ==
  209. AML_SCOPE_OP)
  210. || (parent_op->common.aml_opcode ==
  211. AML_METHOD_OP)
  212. || (parent_op->common.aml_opcode ==
  213. AML_WHILE_OP)) {
  214. break;
  215. }
  216. display_op = parent_op;
  217. parent_op = parent_op->common.parent;
  218. }
  219. }
  220. }
  221. /* Now we can display it */
  222. #ifdef ACPI_DISASSEMBLER
  223. acpi_dm_disassemble(walk_state, display_op, ACPI_UINT32_MAX);
  224. #endif
  225. if ((op->common.aml_opcode == AML_IF_OP) ||
  226. (op->common.aml_opcode == AML_WHILE_OP)) {
  227. if (walk_state->control_state->common.value) {
  228. acpi_os_printf
  229. ("Predicate = [True], IF block was executed\n");
  230. } else {
  231. acpi_os_printf
  232. ("Predicate = [False], Skipping IF block\n");
  233. }
  234. } else if (op->common.aml_opcode == AML_ELSE_OP) {
  235. acpi_os_printf
  236. ("Predicate = [False], ELSE block was executed\n");
  237. }
  238. /* Restore everything */
  239. op->common.next = next;
  240. acpi_os_printf("\n");
  241. if ((acpi_gbl_db_output_to_file) ||
  242. (acpi_dbg_level & ACPI_LV_PARSE)) {
  243. acpi_os_printf("\n");
  244. }
  245. acpi_dbg_level = original_debug_level;
  246. }
  247. /* If we are not single stepping, just continue executing the method */
  248. if (!acpi_gbl_cm_single_step) {
  249. return (AE_OK);
  250. }
  251. /*
  252. * If we are executing a step-to-call command,
  253. * Check if this is a method call.
  254. */
  255. if (acpi_gbl_step_to_next_call) {
  256. if (op->common.aml_opcode != AML_INT_METHODCALL_OP) {
  257. /* Not a method call, just keep executing */
  258. return (AE_OK);
  259. }
  260. /* Found a method call, stop executing */
  261. acpi_gbl_step_to_next_call = FALSE;
  262. }
  263. /*
  264. * If the next opcode is a method call, we will "step over" it
  265. * by default.
  266. */
  267. if (op->common.aml_opcode == AML_INT_METHODCALL_OP) {
  268. /* Force no more single stepping while executing called method */
  269. acpi_gbl_cm_single_step = FALSE;
  270. /*
  271. * Set the breakpoint on/before the call, it will stop execution
  272. * as soon as we return
  273. */
  274. walk_state->method_breakpoint = 1; /* Must be non-zero! */
  275. }
  276. acpi_ex_exit_interpreter();
  277. status = acpi_db_start_command(walk_state, op);
  278. acpi_ex_enter_interpreter();
  279. /* User commands complete, continue execution of the interrupted method */
  280. return (status);
  281. }
  282. /*******************************************************************************
  283. *
  284. * FUNCTION: acpi_initialize_debugger
  285. *
  286. * PARAMETERS: None
  287. *
  288. * RETURN: Status
  289. *
  290. * DESCRIPTION: Init and start debugger
  291. *
  292. ******************************************************************************/
  293. acpi_status acpi_initialize_debugger(void)
  294. {
  295. acpi_status status;
  296. ACPI_FUNCTION_TRACE(acpi_initialize_debugger);
  297. /* Init globals */
  298. acpi_gbl_db_buffer = NULL;
  299. acpi_gbl_db_filename = NULL;
  300. acpi_gbl_db_output_to_file = FALSE;
  301. acpi_gbl_db_debug_level = ACPI_LV_VERBOSITY2;
  302. acpi_gbl_db_console_debug_level = ACPI_NORMAL_DEFAULT | ACPI_LV_TABLES;
  303. acpi_gbl_db_output_flags = ACPI_DB_CONSOLE_OUTPUT;
  304. acpi_gbl_db_opt_no_ini_methods = FALSE;
  305. acpi_gbl_db_buffer = acpi_os_allocate(ACPI_DEBUG_BUFFER_SIZE);
  306. if (!acpi_gbl_db_buffer) {
  307. return_ACPI_STATUS(AE_NO_MEMORY);
  308. }
  309. memset(acpi_gbl_db_buffer, 0, ACPI_DEBUG_BUFFER_SIZE);
  310. /* Initial scope is the root */
  311. acpi_gbl_db_scope_buf[0] = AML_ROOT_PREFIX;
  312. acpi_gbl_db_scope_buf[1] = 0;
  313. acpi_gbl_db_scope_node = acpi_gbl_root_node;
  314. /* Initialize user commands loop */
  315. acpi_gbl_db_terminate_loop = FALSE;
  316. /*
  317. * If configured for multi-thread support, the debug executor runs in
  318. * a separate thread so that the front end can be in another address
  319. * space, environment, or even another machine.
  320. */
  321. if (acpi_gbl_debugger_configuration & DEBUGGER_MULTI_THREADED) {
  322. /* These were created with one unit, grab it */
  323. status = acpi_os_initialize_debugger();
  324. if (ACPI_FAILURE(status)) {
  325. acpi_os_printf("Could not get debugger mutex\n");
  326. return_ACPI_STATUS(status);
  327. }
  328. /* Create the debug execution thread to execute commands */
  329. acpi_gbl_db_threads_terminated = FALSE;
  330. status = acpi_os_execute(OSL_DEBUGGER_MAIN_THREAD,
  331. acpi_db_execute_thread, NULL);
  332. if (ACPI_FAILURE(status)) {
  333. ACPI_EXCEPTION((AE_INFO, status,
  334. "Could not start debugger thread"));
  335. acpi_gbl_db_threads_terminated = TRUE;
  336. return_ACPI_STATUS(status);
  337. }
  338. } else {
  339. acpi_gbl_db_thread_id = acpi_os_get_thread_id();
  340. }
  341. return_ACPI_STATUS(AE_OK);
  342. }
  343. ACPI_EXPORT_SYMBOL(acpi_initialize_debugger)
  344. /*******************************************************************************
  345. *
  346. * FUNCTION: acpi_terminate_debugger
  347. *
  348. * PARAMETERS: None
  349. *
  350. * RETURN: None
  351. *
  352. * DESCRIPTION: Stop debugger
  353. *
  354. ******************************************************************************/
  355. void acpi_terminate_debugger(void)
  356. {
  357. /* Terminate the AML Debugger */
  358. acpi_gbl_db_terminate_loop = TRUE;
  359. if (acpi_gbl_debugger_configuration & DEBUGGER_MULTI_THREADED) {
  360. /* Wait the AML Debugger threads */
  361. while (!acpi_gbl_db_threads_terminated) {
  362. acpi_os_sleep(100);
  363. }
  364. acpi_os_terminate_debugger();
  365. }
  366. if (acpi_gbl_db_buffer) {
  367. acpi_os_free(acpi_gbl_db_buffer);
  368. acpi_gbl_db_buffer = NULL;
  369. }
  370. /* Ensure that debug output is now disabled */
  371. acpi_gbl_db_output_flags = ACPI_DB_DISABLE_OUTPUT;
  372. }
  373. ACPI_EXPORT_SYMBOL(acpi_terminate_debugger)
  374. /*******************************************************************************
  375. *
  376. * FUNCTION: acpi_set_debugger_thread_id
  377. *
  378. * PARAMETERS: thread_id - Debugger thread ID
  379. *
  380. * RETURN: None
  381. *
  382. * DESCRIPTION: Set debugger thread ID
  383. *
  384. ******************************************************************************/
  385. void acpi_set_debugger_thread_id(acpi_thread_id thread_id)
  386. {
  387. acpi_gbl_db_thread_id = thread_id;
  388. }
  389. ACPI_EXPORT_SYMBOL(acpi_set_debugger_thread_id)