dbmethod.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: dbmethod - Debug commands for control methods
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acdispat.h"
  10. #include "acnamesp.h"
  11. #include "acdebug.h"
  12. #include "acparser.h"
  13. #include "acpredef.h"
  14. #define _COMPONENT ACPI_CA_DEBUGGER
  15. ACPI_MODULE_NAME("dbmethod")
  16. /* Local prototypes */
  17. static acpi_status
  18. acpi_db_walk_for_execute(acpi_handle obj_handle,
  19. u32 nesting_level, void *context, void **return_value);
  20. static acpi_status acpi_db_evaluate_object(struct acpi_namespace_node *node);
  21. /*******************************************************************************
  22. *
  23. * FUNCTION: acpi_db_set_method_breakpoint
  24. *
  25. * PARAMETERS: location - AML offset of breakpoint
  26. * walk_state - Current walk info
  27. * op - Current Op (from parse walk)
  28. *
  29. * RETURN: None
  30. *
  31. * DESCRIPTION: Set a breakpoint in a control method at the specified
  32. * AML offset
  33. *
  34. ******************************************************************************/
  35. void
  36. acpi_db_set_method_breakpoint(char *location,
  37. struct acpi_walk_state *walk_state,
  38. union acpi_parse_object *op)
  39. {
  40. u32 address;
  41. u32 aml_offset;
  42. if (!op) {
  43. acpi_os_printf("There is no method currently executing\n");
  44. return;
  45. }
  46. /* Get and verify the breakpoint address */
  47. address = strtoul(location, NULL, 16);
  48. aml_offset = (u32)ACPI_PTR_DIFF(op->common.aml,
  49. walk_state->parser_state.aml_start);
  50. if (address <= aml_offset) {
  51. acpi_os_printf("Breakpoint %X is beyond current address %X\n",
  52. address, aml_offset);
  53. }
  54. /* Save breakpoint in current walk */
  55. walk_state->user_breakpoint = address;
  56. acpi_os_printf("Breakpoint set at AML offset %X\n", address);
  57. }
  58. /*******************************************************************************
  59. *
  60. * FUNCTION: acpi_db_set_method_call_breakpoint
  61. *
  62. * PARAMETERS: op - Current Op (from parse walk)
  63. *
  64. * RETURN: None
  65. *
  66. * DESCRIPTION: Set a breakpoint in a control method at the specified
  67. * AML offset
  68. *
  69. ******************************************************************************/
  70. void acpi_db_set_method_call_breakpoint(union acpi_parse_object *op)
  71. {
  72. if (!op) {
  73. acpi_os_printf("There is no method currently executing\n");
  74. return;
  75. }
  76. acpi_gbl_step_to_next_call = TRUE;
  77. }
  78. /*******************************************************************************
  79. *
  80. * FUNCTION: acpi_db_set_method_data
  81. *
  82. * PARAMETERS: type_arg - L for local, A for argument
  83. * index_arg - which one
  84. * value_arg - Value to set.
  85. *
  86. * RETURN: None
  87. *
  88. * DESCRIPTION: Set a local or argument for the running control method.
  89. * NOTE: only object supported is Number.
  90. *
  91. ******************************************************************************/
  92. void acpi_db_set_method_data(char *type_arg, char *index_arg, char *value_arg)
  93. {
  94. char type;
  95. u32 index;
  96. u32 value;
  97. struct acpi_walk_state *walk_state;
  98. union acpi_operand_object *obj_desc;
  99. acpi_status status;
  100. struct acpi_namespace_node *node;
  101. /* Validate type_arg */
  102. acpi_ut_strupr(type_arg);
  103. type = type_arg[0];
  104. if ((type != 'L') && (type != 'A') && (type != 'N')) {
  105. acpi_os_printf("Invalid SET operand: %s\n", type_arg);
  106. return;
  107. }
  108. value = strtoul(value_arg, NULL, 16);
  109. if (type == 'N') {
  110. node = acpi_db_convert_to_node(index_arg);
  111. if (!node) {
  112. return;
  113. }
  114. if (node->type != ACPI_TYPE_INTEGER) {
  115. acpi_os_printf("Can only set Integer nodes\n");
  116. return;
  117. }
  118. obj_desc = node->object;
  119. obj_desc->integer.value = value;
  120. return;
  121. }
  122. /* Get the index and value */
  123. index = strtoul(index_arg, NULL, 16);
  124. walk_state = acpi_ds_get_current_walk_state(acpi_gbl_current_walk_list);
  125. if (!walk_state) {
  126. acpi_os_printf("There is no method currently executing\n");
  127. return;
  128. }
  129. /* Create and initialize the new object */
  130. obj_desc = acpi_ut_create_integer_object((u64)value);
  131. if (!obj_desc) {
  132. acpi_os_printf("Could not create an internal object\n");
  133. return;
  134. }
  135. /* Store the new object into the target */
  136. switch (type) {
  137. case 'A':
  138. /* Set a method argument */
  139. if (index > ACPI_METHOD_MAX_ARG) {
  140. acpi_os_printf("Arg%u - Invalid argument name\n",
  141. index);
  142. goto cleanup;
  143. }
  144. status = acpi_ds_store_object_to_local(ACPI_REFCLASS_ARG,
  145. index, obj_desc,
  146. walk_state);
  147. if (ACPI_FAILURE(status)) {
  148. goto cleanup;
  149. }
  150. obj_desc = walk_state->arguments[index].object;
  151. acpi_os_printf("Arg%u: ", index);
  152. acpi_db_display_internal_object(obj_desc, walk_state);
  153. break;
  154. case 'L':
  155. /* Set a method local */
  156. if (index > ACPI_METHOD_MAX_LOCAL) {
  157. acpi_os_printf
  158. ("Local%u - Invalid local variable name\n", index);
  159. goto cleanup;
  160. }
  161. status = acpi_ds_store_object_to_local(ACPI_REFCLASS_LOCAL,
  162. index, obj_desc,
  163. walk_state);
  164. if (ACPI_FAILURE(status)) {
  165. goto cleanup;
  166. }
  167. obj_desc = walk_state->local_variables[index].object;
  168. acpi_os_printf("Local%u: ", index);
  169. acpi_db_display_internal_object(obj_desc, walk_state);
  170. break;
  171. default:
  172. break;
  173. }
  174. cleanup:
  175. acpi_ut_remove_reference(obj_desc);
  176. }
  177. #ifdef ACPI_DISASSEMBLER
  178. /*******************************************************************************
  179. *
  180. * FUNCTION: acpi_db_disassemble_aml
  181. *
  182. * PARAMETERS: statements - Number of statements to disassemble
  183. * op - Current Op (from parse walk)
  184. *
  185. * RETURN: None
  186. *
  187. * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
  188. * of statements specified.
  189. *
  190. ******************************************************************************/
  191. void acpi_db_disassemble_aml(char *statements, union acpi_parse_object *op)
  192. {
  193. u32 num_statements = 8;
  194. if (!op) {
  195. acpi_os_printf("There is no method currently executing\n");
  196. return;
  197. }
  198. if (statements) {
  199. num_statements = strtoul(statements, NULL, 0);
  200. }
  201. acpi_dm_disassemble(NULL, op, num_statements);
  202. }
  203. /*******************************************************************************
  204. *
  205. * FUNCTION: acpi_db_disassemble_method
  206. *
  207. * PARAMETERS: name - Name of control method
  208. *
  209. * RETURN: None
  210. *
  211. * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
  212. * of statements specified.
  213. *
  214. ******************************************************************************/
  215. acpi_status acpi_db_disassemble_method(char *name)
  216. {
  217. acpi_status status;
  218. union acpi_parse_object *op;
  219. struct acpi_walk_state *walk_state;
  220. union acpi_operand_object *obj_desc;
  221. struct acpi_namespace_node *method;
  222. method = acpi_db_convert_to_node(name);
  223. if (!method) {
  224. return (AE_BAD_PARAMETER);
  225. }
  226. if (method->type != ACPI_TYPE_METHOD) {
  227. ACPI_ERROR((AE_INFO, "%s (%s): Object must be a control method",
  228. name, acpi_ut_get_type_name(method->type)));
  229. return (AE_BAD_PARAMETER);
  230. }
  231. obj_desc = method->object;
  232. op = acpi_ps_create_scope_op(obj_desc->method.aml_start);
  233. if (!op) {
  234. return (AE_NO_MEMORY);
  235. }
  236. /* Create and initialize a new walk state */
  237. walk_state = acpi_ds_create_walk_state(0, op, NULL, NULL);
  238. if (!walk_state) {
  239. return (AE_NO_MEMORY);
  240. }
  241. status = acpi_ds_init_aml_walk(walk_state, op, NULL,
  242. obj_desc->method.aml_start,
  243. obj_desc->method.aml_length, NULL,
  244. ACPI_IMODE_LOAD_PASS1);
  245. if (ACPI_FAILURE(status)) {
  246. return (status);
  247. }
  248. status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
  249. if (ACPI_FAILURE(status)) {
  250. return (status);
  251. }
  252. walk_state->owner_id = obj_desc->method.owner_id;
  253. /* Push start scope on scope stack and make it current */
  254. status = acpi_ds_scope_stack_push(method, method->type, walk_state);
  255. if (ACPI_FAILURE(status)) {
  256. return (status);
  257. }
  258. /* Parse the entire method AML including deferred operators */
  259. walk_state->parse_flags &= ~ACPI_PARSE_DELETE_TREE;
  260. walk_state->parse_flags |= ACPI_PARSE_DISASSEMBLE;
  261. status = acpi_ps_parse_aml(walk_state);
  262. if (ACPI_FAILURE(status)) {
  263. return (status);
  264. }
  265. (void)acpi_dm_parse_deferred_ops(op);
  266. /* Now we can disassemble the method */
  267. acpi_gbl_dm_opt_verbose = FALSE;
  268. acpi_dm_disassemble(NULL, op, 0);
  269. acpi_gbl_dm_opt_verbose = TRUE;
  270. acpi_ps_delete_parse_tree(op);
  271. /* Method cleanup */
  272. acpi_ns_delete_namespace_subtree(method);
  273. acpi_ns_delete_namespace_by_owner(obj_desc->method.owner_id);
  274. acpi_ut_release_owner_id(&obj_desc->method.owner_id);
  275. return (AE_OK);
  276. }
  277. #endif
  278. /*******************************************************************************
  279. *
  280. * FUNCTION: acpi_db_evaluate_object
  281. *
  282. * PARAMETERS: node - Namespace node for the object
  283. *
  284. * RETURN: Status
  285. *
  286. * DESCRIPTION: Main execution function for the Evaluate/Execute/All debugger
  287. * commands.
  288. *
  289. ******************************************************************************/
  290. static acpi_status acpi_db_evaluate_object(struct acpi_namespace_node *node)
  291. {
  292. char *pathname;
  293. u32 i;
  294. struct acpi_device_info *obj_info;
  295. struct acpi_object_list param_objects;
  296. union acpi_object params[ACPI_METHOD_NUM_ARGS];
  297. struct acpi_buffer return_obj;
  298. acpi_status status;
  299. pathname = acpi_ns_get_external_pathname(node);
  300. if (!pathname) {
  301. return (AE_OK);
  302. }
  303. /* Get the object info for number of method parameters */
  304. status = acpi_get_object_info(node, &obj_info);
  305. if (ACPI_FAILURE(status)) {
  306. ACPI_FREE(pathname);
  307. return (status);
  308. }
  309. param_objects.pointer = NULL;
  310. param_objects.count = 0;
  311. if (obj_info->type == ACPI_TYPE_METHOD) {
  312. /* Setup default parameters */
  313. for (i = 0; i < obj_info->param_count; i++) {
  314. params[i].type = ACPI_TYPE_INTEGER;
  315. params[i].integer.value = 1;
  316. }
  317. param_objects.pointer = params;
  318. param_objects.count = obj_info->param_count;
  319. }
  320. ACPI_FREE(obj_info);
  321. return_obj.pointer = NULL;
  322. return_obj.length = ACPI_ALLOCATE_BUFFER;
  323. /* Do the actual method execution */
  324. acpi_gbl_method_executing = TRUE;
  325. status = acpi_evaluate_object(node, NULL, &param_objects, &return_obj);
  326. acpi_gbl_method_executing = FALSE;
  327. acpi_os_printf("%-32s returned %s\n", pathname,
  328. acpi_format_exception(status));
  329. if (return_obj.length) {
  330. acpi_os_printf("Evaluation of %s returned object %p, "
  331. "external buffer length %X\n",
  332. pathname, return_obj.pointer,
  333. (u32)return_obj.length);
  334. acpi_db_dump_external_object(return_obj.pointer, 1);
  335. acpi_os_printf("\n");
  336. }
  337. ACPI_FREE(pathname);
  338. /* Ignore status from method execution */
  339. return (AE_OK);
  340. /* Update count, check if we have executed enough methods */
  341. }
  342. /*******************************************************************************
  343. *
  344. * FUNCTION: acpi_db_walk_for_execute
  345. *
  346. * PARAMETERS: Callback from walk_namespace
  347. *
  348. * RETURN: Status
  349. *
  350. * DESCRIPTION: Batch execution function. Evaluates all "predefined" objects --
  351. * the nameseg begins with an underscore.
  352. *
  353. ******************************************************************************/
  354. static acpi_status
  355. acpi_db_walk_for_execute(acpi_handle obj_handle,
  356. u32 nesting_level, void *context, void **return_value)
  357. {
  358. struct acpi_namespace_node *node =
  359. (struct acpi_namespace_node *)obj_handle;
  360. struct acpi_db_execute_walk *info =
  361. (struct acpi_db_execute_walk *)context;
  362. acpi_status status;
  363. const union acpi_predefined_info *predefined;
  364. predefined = acpi_ut_match_predefined_method(node->name.ascii);
  365. if (!predefined) {
  366. return (AE_OK);
  367. }
  368. if (node->type == ACPI_TYPE_LOCAL_SCOPE) {
  369. return (AE_OK);
  370. }
  371. acpi_db_evaluate_object(node);
  372. /* Ignore status from object evaluation */
  373. status = AE_OK;
  374. /* Update count, check if we have executed enough methods */
  375. info->count++;
  376. if (info->count >= info->max_count) {
  377. status = AE_CTRL_TERMINATE;
  378. }
  379. return (status);
  380. }
  381. /*******************************************************************************
  382. *
  383. * FUNCTION: acpi_db_walk_for_execute_all
  384. *
  385. * PARAMETERS: Callback from walk_namespace
  386. *
  387. * RETURN: Status
  388. *
  389. * DESCRIPTION: Batch execution function. Evaluates all objects whose path ends
  390. * with the nameseg "Info->NameSeg". Used for the "ALL" command.
  391. *
  392. ******************************************************************************/
  393. static acpi_status
  394. acpi_db_walk_for_execute_all(acpi_handle obj_handle,
  395. u32 nesting_level,
  396. void *context, void **return_value)
  397. {
  398. struct acpi_namespace_node *node =
  399. (struct acpi_namespace_node *)obj_handle;
  400. struct acpi_db_execute_walk *info =
  401. (struct acpi_db_execute_walk *)context;
  402. acpi_status status;
  403. if (!ACPI_COMPARE_NAMESEG(node->name.ascii, info->name_seg)) {
  404. return (AE_OK);
  405. }
  406. if (node->type == ACPI_TYPE_LOCAL_SCOPE) {
  407. return (AE_OK);
  408. }
  409. /* Now evaluate the input object (node) */
  410. acpi_db_evaluate_object(node);
  411. /* Ignore status from method execution */
  412. status = AE_OK;
  413. /* Update count of executed methods/objects */
  414. info->count++;
  415. return (status);
  416. }
  417. /*******************************************************************************
  418. *
  419. * FUNCTION: acpi_db_evaluate_predefined_names
  420. *
  421. * PARAMETERS: None
  422. *
  423. * RETURN: None
  424. *
  425. * DESCRIPTION: Namespace batch execution. Execute predefined names in the
  426. * namespace, up to the max count, if specified.
  427. *
  428. ******************************************************************************/
  429. void acpi_db_evaluate_predefined_names(void)
  430. {
  431. struct acpi_db_execute_walk info;
  432. info.count = 0;
  433. info.max_count = ACPI_UINT32_MAX;
  434. /* Search all nodes in namespace */
  435. (void)acpi_walk_namespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
  436. ACPI_UINT32_MAX, acpi_db_walk_for_execute,
  437. NULL, (void *)&info, NULL);
  438. acpi_os_printf("Evaluated %u predefined names in the namespace\n",
  439. info.count);
  440. }
  441. /*******************************************************************************
  442. *
  443. * FUNCTION: acpi_db_evaluate_all
  444. *
  445. * PARAMETERS: none_acpi_gbl_db_method_info
  446. *
  447. * RETURN: None
  448. *
  449. * DESCRIPTION: Namespace batch execution. Implements the "ALL" command.
  450. * Execute all namepaths whose final nameseg matches the
  451. * input nameseg.
  452. *
  453. ******************************************************************************/
  454. void acpi_db_evaluate_all(char *name_seg)
  455. {
  456. struct acpi_db_execute_walk info;
  457. info.count = 0;
  458. info.max_count = ACPI_UINT32_MAX;
  459. ACPI_COPY_NAMESEG(info.name_seg, name_seg);
  460. info.name_seg[ACPI_NAMESEG_SIZE] = 0;
  461. /* Search all nodes in namespace */
  462. (void)acpi_walk_namespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
  463. ACPI_UINT32_MAX, acpi_db_walk_for_execute_all,
  464. NULL, (void *)&info, NULL);
  465. acpi_os_printf("Evaluated %u names in the namespace\n", info.count);
  466. }