1 /*******************************************************************************
3 * Module Name: dbexec - debugger control method execution
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_CA_DEBUGGER
50 ACPI_MODULE_NAME("dbexec")
52 static struct acpi_db_method_info acpi_gbl_db_method_info
;
54 /* Local prototypes */
57 acpi_db_execute_method(struct acpi_db_method_info
*info
,
58 struct acpi_buffer
*return_obj
);
60 static acpi_status
acpi_db_execute_setup(struct acpi_db_method_info
*info
);
62 static u32
acpi_db_get_outstanding_allocations(void);
64 static void ACPI_SYSTEM_XFACE
acpi_db_method_thread(void *context
);
67 acpi_db_execution_walk(acpi_handle obj_handle
,
68 u32 nesting_level
, void *context
, void **return_value
);
70 /*******************************************************************************
72 * FUNCTION: acpi_db_delete_objects
74 * PARAMETERS: count - Count of objects in the list
75 * objects - Array of ACPI_OBJECTs to be deleted
79 * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
80 * packages via recursion.
82 ******************************************************************************/
84 void acpi_db_delete_objects(u32 count
, union acpi_object
*objects
)
88 for (i
= 0; i
< count
; i
++) {
89 switch (objects
[i
].type
) {
90 case ACPI_TYPE_BUFFER
:
92 ACPI_FREE(objects
[i
].buffer
.pointer
);
95 case ACPI_TYPE_PACKAGE
:
97 /* Recursive call to delete package elements */
99 acpi_db_delete_objects(objects
[i
].package
.count
,
100 objects
[i
].package
.elements
);
102 /* Free the elements array */
104 ACPI_FREE(objects
[i
].package
.elements
);
114 /*******************************************************************************
116 * FUNCTION: acpi_db_execute_method
118 * PARAMETERS: info - Valid info segment
119 * return_obj - Where to put return object
123 * DESCRIPTION: Execute a control method.
125 ******************************************************************************/
128 acpi_db_execute_method(struct acpi_db_method_info
*info
,
129 struct acpi_buffer
*return_obj
)
132 struct acpi_object_list param_objects
;
133 union acpi_object params
[ACPI_DEBUGGER_MAX_ARGS
+ 1];
136 ACPI_FUNCTION_TRACE(db_execute_method
);
138 if (acpi_gbl_db_output_to_file
&& !acpi_dbg_level
) {
139 acpi_os_printf("Warning: debug output is not enabled!\n");
142 param_objects
.count
= 0;
143 param_objects
.pointer
= NULL
;
145 /* Pass through any command-line arguments */
147 if (info
->args
&& info
->args
[0]) {
149 /* Get arguments passed on the command line */
151 for (i
= 0; (info
->args
[i
] && *(info
->args
[i
])); i
++) {
153 /* Convert input string (token) to an actual union acpi_object */
155 status
= acpi_db_convert_to_object(info
->types
[i
],
158 if (ACPI_FAILURE(status
)) {
159 ACPI_EXCEPTION((AE_INFO
, status
,
160 "While parsing method arguments"));
165 param_objects
.count
= i
;
166 param_objects
.pointer
= params
;
169 /* Prepare for a return object of arbitrary size */
171 return_obj
->pointer
= acpi_gbl_db_buffer
;
172 return_obj
->length
= ACPI_DEBUG_BUFFER_SIZE
;
174 /* Do the actual method execution */
176 acpi_gbl_method_executing
= TRUE
;
177 status
= acpi_evaluate_object(NULL
, info
->pathname
,
178 ¶m_objects
, return_obj
);
180 acpi_gbl_cm_single_step
= FALSE
;
181 acpi_gbl_method_executing
= FALSE
;
183 if (ACPI_FAILURE(status
)) {
184 ACPI_EXCEPTION((AE_INFO
, status
,
185 "while executing %s from debugger",
188 if (status
== AE_BUFFER_OVERFLOW
) {
190 "Possible overflow of internal debugger "
191 "buffer (size 0x%X needed 0x%X)",
192 ACPI_DEBUG_BUFFER_SIZE
,
193 (u32
)return_obj
->length
));
198 acpi_db_delete_objects(param_objects
.count
, params
);
199 return_ACPI_STATUS(status
);
202 /*******************************************************************************
204 * FUNCTION: acpi_db_execute_setup
206 * PARAMETERS: info - Valid method info
210 * DESCRIPTION: Setup info segment prior to method execution
212 ******************************************************************************/
214 static acpi_status
acpi_db_execute_setup(struct acpi_db_method_info
*info
)
218 ACPI_FUNCTION_NAME(db_execute_setup
);
220 /* Catenate the current scope to the supplied name */
222 info
->pathname
[0] = 0;
223 if ((info
->name
[0] != '\\') && (info
->name
[0] != '/')) {
224 if (acpi_ut_safe_strcat(info
->pathname
, sizeof(info
->pathname
),
225 acpi_gbl_db_scope_buf
)) {
226 status
= AE_BUFFER_OVERFLOW
;
231 if (acpi_ut_safe_strcat(info
->pathname
, sizeof(info
->pathname
),
233 status
= AE_BUFFER_OVERFLOW
;
237 acpi_db_prep_namestring(info
->pathname
);
239 acpi_db_set_output_destination(ACPI_DB_DUPLICATE_OUTPUT
);
240 acpi_os_printf("Evaluating %s\n", info
->pathname
);
242 if (info
->flags
& EX_SINGLE_STEP
) {
243 acpi_gbl_cm_single_step
= TRUE
;
244 acpi_db_set_output_destination(ACPI_DB_CONSOLE_OUTPUT
);
248 /* No single step, allow redirection to a file */
250 acpi_db_set_output_destination(ACPI_DB_REDIRECTABLE_OUTPUT
);
257 ACPI_EXCEPTION((AE_INFO
, status
, "During setup for method execution"));
261 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
262 u32
acpi_db_get_cache_info(struct acpi_memory_list
*cache
)
265 return (cache
->total_allocated
- cache
->total_freed
-
266 cache
->current_depth
);
270 /*******************************************************************************
272 * FUNCTION: acpi_db_get_outstanding_allocations
276 * RETURN: Current global allocation count minus cache entries
278 * DESCRIPTION: Determine the current number of "outstanding" allocations --
279 * those allocations that have not been freed and also are not
280 * in one of the various object caches.
282 ******************************************************************************/
284 static u32
acpi_db_get_outstanding_allocations(void)
288 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
290 outstanding
+= acpi_db_get_cache_info(acpi_gbl_state_cache
);
291 outstanding
+= acpi_db_get_cache_info(acpi_gbl_ps_node_cache
);
292 outstanding
+= acpi_db_get_cache_info(acpi_gbl_ps_node_ext_cache
);
293 outstanding
+= acpi_db_get_cache_info(acpi_gbl_operand_cache
);
296 return (outstanding
);
299 /*******************************************************************************
301 * FUNCTION: acpi_db_execution_walk
303 * PARAMETERS: WALK_CALLBACK
307 * DESCRIPTION: Execute a control method. Name is relative to the current
310 ******************************************************************************/
313 acpi_db_execution_walk(acpi_handle obj_handle
,
314 u32 nesting_level
, void *context
, void **return_value
)
316 union acpi_operand_object
*obj_desc
;
317 struct acpi_namespace_node
*node
=
318 (struct acpi_namespace_node
*)obj_handle
;
319 struct acpi_buffer return_obj
;
322 obj_desc
= acpi_ns_get_attached_object(node
);
323 if (obj_desc
->method
.param_count
) {
327 return_obj
.pointer
= NULL
;
328 return_obj
.length
= ACPI_ALLOCATE_BUFFER
;
330 acpi_ns_print_node_pathname(node
, "Evaluating");
332 /* Do the actual method execution */
334 acpi_os_printf("\n");
335 acpi_gbl_method_executing
= TRUE
;
337 status
= acpi_evaluate_object(node
, NULL
, NULL
, &return_obj
);
339 acpi_os_printf("Evaluation of [%4.4s] returned %s\n",
340 acpi_ut_get_node_name(node
),
341 acpi_format_exception(status
));
343 acpi_gbl_method_executing
= FALSE
;
347 /*******************************************************************************
349 * FUNCTION: acpi_db_execute
351 * PARAMETERS: name - Name of method to execute
352 * args - Parameters to the method
354 * flags - single step/no single step
358 * DESCRIPTION: Execute a control method. Name is relative to the current
361 ******************************************************************************/
364 acpi_db_execute(char *name
, char **args
, acpi_object_type
* types
, u32 flags
)
367 struct acpi_buffer return_obj
;
370 #ifdef ACPI_DEBUG_OUTPUT
371 u32 previous_allocations
;
374 /* Memory allocation tracking */
376 previous_allocations
= acpi_db_get_outstanding_allocations();
380 (void)acpi_walk_namespace(ACPI_TYPE_METHOD
, ACPI_ROOT_OBJECT
,
382 acpi_db_execution_walk
, NULL
, NULL
,
386 name_string
= ACPI_ALLOCATE(strlen(name
) + 1);
391 memset(&acpi_gbl_db_method_info
, 0,
392 sizeof(struct acpi_db_method_info
));
394 strcpy(name_string
, name
);
395 acpi_ut_strupr(name_string
);
396 acpi_gbl_db_method_info
.name
= name_string
;
397 acpi_gbl_db_method_info
.args
= args
;
398 acpi_gbl_db_method_info
.types
= types
;
399 acpi_gbl_db_method_info
.flags
= flags
;
401 return_obj
.pointer
= NULL
;
402 return_obj
.length
= ACPI_ALLOCATE_BUFFER
;
404 status
= acpi_db_execute_setup(&acpi_gbl_db_method_info
);
405 if (ACPI_FAILURE(status
)) {
406 ACPI_FREE(name_string
);
410 /* Get the NS node, determines existence also */
412 status
= acpi_get_handle(NULL
, acpi_gbl_db_method_info
.pathname
,
413 &acpi_gbl_db_method_info
.method
);
414 if (ACPI_SUCCESS(status
)) {
416 acpi_db_execute_method(&acpi_gbl_db_method_info
,
419 ACPI_FREE(name_string
);
423 * Allow any handlers in separate threads to complete.
424 * (Such as Notify handlers invoked from AML executed above).
426 acpi_os_sleep((u64
)10);
428 #ifdef ACPI_DEBUG_OUTPUT
430 /* Memory allocation tracking */
433 acpi_db_get_outstanding_allocations() - previous_allocations
;
435 acpi_db_set_output_destination(ACPI_DB_DUPLICATE_OUTPUT
);
437 if (allocations
> 0) {
439 ("0x%X Outstanding allocations after evaluation of %s\n",
440 allocations
, acpi_gbl_db_method_info
.pathname
);
444 if (ACPI_FAILURE(status
)) {
445 acpi_os_printf("Evaluation of %s failed with status %s\n",
446 acpi_gbl_db_method_info
.pathname
,
447 acpi_format_exception(status
));
449 /* Display a return object, if any */
451 if (return_obj
.length
) {
452 acpi_os_printf("Evaluation of %s returned object %p, "
453 "external buffer length %X\n",
454 acpi_gbl_db_method_info
.pathname
,
456 (u32
)return_obj
.length
);
458 acpi_db_dump_external_object(return_obj
.pointer
, 1);
460 /* Dump a _PLD buffer if present */
462 if (ACPI_COMPARE_NAME
464 (struct acpi_namespace_node
,
465 acpi_gbl_db_method_info
.method
)->name
.ascii
),
467 acpi_db_dump_pld_buffer(return_obj
.pointer
);
471 ("No object was returned from evaluation of %s\n",
472 acpi_gbl_db_method_info
.pathname
);
476 acpi_db_set_output_destination(ACPI_DB_CONSOLE_OUTPUT
);
479 /*******************************************************************************
481 * FUNCTION: acpi_db_method_thread
483 * PARAMETERS: context - Execution info segment
487 * DESCRIPTION: Debugger execute thread. Waits for a command line, then
488 * simply dispatches it.
490 ******************************************************************************/
492 static void ACPI_SYSTEM_XFACE
acpi_db_method_thread(void *context
)
495 struct acpi_db_method_info
*info
= context
;
496 struct acpi_db_method_info local_info
;
499 struct acpi_buffer return_obj
;
502 * acpi_gbl_db_method_info.Arguments will be passed as method arguments.
503 * Prevent acpi_gbl_db_method_info from being modified by multiple threads
506 * Note: The arguments we are passing are used by the ASL test suite
507 * (aslts). Do not change them without updating the tests.
509 (void)acpi_os_wait_semaphore(info
->info_gate
, 1, ACPI_WAIT_FOREVER
);
511 if (info
->init_args
) {
512 acpi_db_uint32_to_hex_string(info
->num_created
,
513 info
->index_of_thread_str
);
514 acpi_db_uint32_to_hex_string((u32
)acpi_os_get_thread_id(),
515 info
->id_of_thread_str
);
518 if (info
->threads
&& (info
->num_created
< info
->num_threads
)) {
519 info
->threads
[info
->num_created
++] = acpi_os_get_thread_id();
523 local_info
.args
= local_info
.arguments
;
524 local_info
.arguments
[0] = local_info
.num_threads_str
;
525 local_info
.arguments
[1] = local_info
.id_of_thread_str
;
526 local_info
.arguments
[2] = local_info
.index_of_thread_str
;
527 local_info
.arguments
[3] = NULL
;
529 local_info
.types
= local_info
.arg_types
;
531 (void)acpi_os_signal_semaphore(info
->info_gate
, 1);
533 for (i
= 0; i
< info
->num_loops
; i
++) {
534 status
= acpi_db_execute_method(&local_info
, &return_obj
);
535 if (ACPI_FAILURE(status
)) {
537 ("%s During evaluation of %s at iteration %X\n",
538 acpi_format_exception(status
), info
->pathname
, i
);
539 if (status
== AE_ABORT_METHOD
) {
544 if ((i
% 100) == 0) {
545 acpi_os_printf("%u loops, Thread 0x%x\n",
546 i
, acpi_os_get_thread_id());
549 if (return_obj
.length
) {
551 ("Evaluation of %s returned object %p Buflen %X\n",
552 info
->pathname
, return_obj
.pointer
,
553 (u32
)return_obj
.length
);
554 acpi_db_dump_external_object(return_obj
.pointer
, 1);
559 /* Signal our completion */
562 (void)acpi_os_wait_semaphore(info
->thread_complete_gate
,
563 1, ACPI_WAIT_FOREVER
);
564 info
->num_completed
++;
566 if (info
->num_completed
== info
->num_threads
) {
568 /* Do signal for main thread once only */
572 (void)acpi_os_signal_semaphore(info
->thread_complete_gate
, 1);
575 status
= acpi_os_signal_semaphore(info
->main_thread_gate
, 1);
576 if (ACPI_FAILURE(status
)) {
578 ("Could not signal debugger thread sync semaphore, %s\n",
579 acpi_format_exception(status
));
584 /*******************************************************************************
586 * FUNCTION: acpi_db_create_execution_threads
588 * PARAMETERS: num_threads_arg - Number of threads to create
589 * num_loops_arg - Loop count for the thread(s)
590 * method_name_arg - Control method to execute
594 * DESCRIPTION: Create threads to execute method(s)
596 ******************************************************************************/
599 acpi_db_create_execution_threads(char *num_threads_arg
,
600 char *num_loops_arg
, char *method_name_arg
)
607 acpi_mutex main_thread_gate
;
608 acpi_mutex thread_complete_gate
;
609 acpi_mutex info_gate
;
611 /* Get the arguments */
613 num_threads
= strtoul(num_threads_arg
, NULL
, 0);
614 num_loops
= strtoul(num_loops_arg
, NULL
, 0);
616 if (!num_threads
|| !num_loops
) {
617 acpi_os_printf("Bad argument: Threads %X, Loops %X\n",
618 num_threads
, num_loops
);
623 * Create the semaphore for synchronization of
624 * the created threads with the main thread.
626 status
= acpi_os_create_semaphore(1, 0, &main_thread_gate
);
627 if (ACPI_FAILURE(status
)) {
628 acpi_os_printf("Could not create semaphore for "
629 "synchronization with the main thread, %s\n",
630 acpi_format_exception(status
));
635 * Create the semaphore for synchronization
636 * between the created threads.
638 status
= acpi_os_create_semaphore(1, 1, &thread_complete_gate
);
639 if (ACPI_FAILURE(status
)) {
640 acpi_os_printf("Could not create semaphore for "
641 "synchronization between the created threads, %s\n",
642 acpi_format_exception(status
));
644 (void)acpi_os_delete_semaphore(main_thread_gate
);
648 status
= acpi_os_create_semaphore(1, 1, &info_gate
);
649 if (ACPI_FAILURE(status
)) {
650 acpi_os_printf("Could not create semaphore for "
651 "synchronization of AcpiGbl_DbMethodInfo, %s\n",
652 acpi_format_exception(status
));
654 (void)acpi_os_delete_semaphore(thread_complete_gate
);
655 (void)acpi_os_delete_semaphore(main_thread_gate
);
659 memset(&acpi_gbl_db_method_info
, 0, sizeof(struct acpi_db_method_info
));
661 /* Array to store IDs of threads */
663 acpi_gbl_db_method_info
.num_threads
= num_threads
;
664 size
= sizeof(acpi_thread_id
) * acpi_gbl_db_method_info
.num_threads
;
666 acpi_gbl_db_method_info
.threads
= acpi_os_allocate(size
);
667 if (acpi_gbl_db_method_info
.threads
== NULL
) {
668 acpi_os_printf("No memory for thread IDs array\n");
669 (void)acpi_os_delete_semaphore(main_thread_gate
);
670 (void)acpi_os_delete_semaphore(thread_complete_gate
);
671 (void)acpi_os_delete_semaphore(info_gate
);
674 memset(acpi_gbl_db_method_info
.threads
, 0, size
);
676 /* Setup the context to be passed to each thread */
678 acpi_gbl_db_method_info
.name
= method_name_arg
;
679 acpi_gbl_db_method_info
.flags
= 0;
680 acpi_gbl_db_method_info
.num_loops
= num_loops
;
681 acpi_gbl_db_method_info
.main_thread_gate
= main_thread_gate
;
682 acpi_gbl_db_method_info
.thread_complete_gate
= thread_complete_gate
;
683 acpi_gbl_db_method_info
.info_gate
= info_gate
;
685 /* Init arguments to be passed to method */
687 acpi_gbl_db_method_info
.init_args
= 1;
688 acpi_gbl_db_method_info
.args
= acpi_gbl_db_method_info
.arguments
;
689 acpi_gbl_db_method_info
.arguments
[0] =
690 acpi_gbl_db_method_info
.num_threads_str
;
691 acpi_gbl_db_method_info
.arguments
[1] =
692 acpi_gbl_db_method_info
.id_of_thread_str
;
693 acpi_gbl_db_method_info
.arguments
[2] =
694 acpi_gbl_db_method_info
.index_of_thread_str
;
695 acpi_gbl_db_method_info
.arguments
[3] = NULL
;
697 acpi_gbl_db_method_info
.types
= acpi_gbl_db_method_info
.arg_types
;
698 acpi_gbl_db_method_info
.arg_types
[0] = ACPI_TYPE_INTEGER
;
699 acpi_gbl_db_method_info
.arg_types
[1] = ACPI_TYPE_INTEGER
;
700 acpi_gbl_db_method_info
.arg_types
[2] = ACPI_TYPE_INTEGER
;
702 acpi_db_uint32_to_hex_string(num_threads
,
703 acpi_gbl_db_method_info
.num_threads_str
);
705 status
= acpi_db_execute_setup(&acpi_gbl_db_method_info
);
706 if (ACPI_FAILURE(status
)) {
707 goto cleanup_and_exit
;
710 /* Get the NS node, determines existence also */
712 status
= acpi_get_handle(NULL
, acpi_gbl_db_method_info
.pathname
,
713 &acpi_gbl_db_method_info
.method
);
714 if (ACPI_FAILURE(status
)) {
715 acpi_os_printf("%s Could not get handle for %s\n",
716 acpi_format_exception(status
),
717 acpi_gbl_db_method_info
.pathname
);
718 goto cleanup_and_exit
;
721 /* Create the threads */
723 acpi_os_printf("Creating %X threads to execute %X times each\n",
724 num_threads
, num_loops
);
726 for (i
= 0; i
< (num_threads
); i
++) {
728 acpi_os_execute(OSL_DEBUGGER_EXEC_THREAD
,
729 acpi_db_method_thread
,
730 &acpi_gbl_db_method_info
);
731 if (ACPI_FAILURE(status
)) {
736 /* Wait for all threads to complete */
738 (void)acpi_os_wait_semaphore(main_thread_gate
, 1, ACPI_WAIT_FOREVER
);
740 acpi_db_set_output_destination(ACPI_DB_DUPLICATE_OUTPUT
);
741 acpi_os_printf("All threads (%X) have completed\n", num_threads
);
742 acpi_db_set_output_destination(ACPI_DB_CONSOLE_OUTPUT
);
746 /* Cleanup and exit */
748 (void)acpi_os_delete_semaphore(main_thread_gate
);
749 (void)acpi_os_delete_semaphore(thread_complete_gate
);
750 (void)acpi_os_delete_semaphore(info_gate
);
752 acpi_os_free(acpi_gbl_db_method_info
.threads
);
753 acpi_gbl_db_method_info
.threads
= NULL
;