dbxface.c 12 KB

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