1 /*******************************************************************************
3 * Module Name: dbexec - debugger control method execution
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
52 #define _COMPONENT ACPI_CA_DEBUGGER
53 ACPI_MODULE_NAME ("dbexec")
56 static ACPI_DB_METHOD_INFO AcpiGbl_DbMethodInfo;
58 /* Local prototypes */
62 ACPI_DB_METHOD_INFO *Info,
63 ACPI_BUFFER *ReturnObj);
67 ACPI_DB_METHOD_INFO *Info);
70 AcpiDbGetOutstandingAllocations (
73 static void ACPI_SYSTEM_XFACE
79 ACPI_HANDLE ObjHandle,
85 /*******************************************************************************
87 * FUNCTION: AcpiDbDeleteObjects
89 * PARAMETERS: Count - Count of objects in the list
90 * Objects - Array of ACPI_OBJECTs to be deleted
94 * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
95 * packages via recursion.
97 ******************************************************************************/
100 AcpiDbDeleteObjects (
102 ACPI_OBJECT *Objects)
107 for (i = 0; i < Count; i++)
109 switch (Objects[i].Type)
111 case ACPI_TYPE_BUFFER:
113 ACPI_FREE (Objects[i].Buffer.Pointer);
116 case ACPI_TYPE_PACKAGE:
118 /* Recursive call to delete package elements */
120 AcpiDbDeleteObjects (Objects[i].Package.Count,
121 Objects[i].Package.Elements);
123 /* Free the elements array */
125 ACPI_FREE (Objects[i].Package.Elements);
136 /*******************************************************************************
138 * FUNCTION: AcpiDbExecuteMethod
140 * PARAMETERS: Info - Valid info segment
141 * ReturnObj - Where to put return object
145 * DESCRIPTION: Execute a control method.
147 ******************************************************************************/
150 AcpiDbExecuteMethod (
151 ACPI_DB_METHOD_INFO *Info,
152 ACPI_BUFFER *ReturnObj)
155 ACPI_OBJECT_LIST ParamObjects;
156 ACPI_OBJECT Params[ACPI_DEBUGGER_MAX_ARGS + 1];
160 ACPI_FUNCTION_TRACE (DbExecuteMethod);
163 if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
165 AcpiOsPrintf ("Warning: debug output is not enabled!\n");
168 ParamObjects.Count = 0;
169 ParamObjects.Pointer = NULL;
171 /* Pass through any command-line arguments */
173 if (Info->Args && Info->Args[0])
175 /* Get arguments passed on the command line */
177 for (i = 0; (Info->Args[i] && *(Info->Args[i])); i++)
179 /* Convert input string (token) to an actual ACPI_OBJECT */
181 Status = AcpiDbConvertToObject (Info->Types[i],
182 Info->Args[i], &Params[i]);
183 if (ACPI_FAILURE (Status))
185 ACPI_EXCEPTION ((AE_INFO, Status,
186 "While parsing method arguments"));
191 ParamObjects.Count = i;
192 ParamObjects.Pointer = Params;
195 /* Prepare for a return object of arbitrary size */
197 ReturnObj->Pointer = AcpiGbl_DbBuffer;
198 ReturnObj->Length = ACPI_DEBUG_BUFFER_SIZE;
200 /* Do the actual method execution */
202 AcpiGbl_MethodExecuting = TRUE;
203 Status = AcpiEvaluateObject (NULL, Info->Pathname,
204 &ParamObjects, ReturnObj);
206 AcpiGbl_CmSingleStep = FALSE;
207 AcpiGbl_MethodExecuting = FALSE;
209 if (ACPI_FAILURE (Status))
211 ACPI_EXCEPTION ((AE_INFO, Status,
212 "while executing %s from debugger", Info->Pathname));
214 if (Status == AE_BUFFER_OVERFLOW)
216 ACPI_ERROR ((AE_INFO,
217 "Possible overflow of internal debugger buffer (size 0x%X needed 0x%X)",
218 ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
223 AcpiDbDeleteObjects (ParamObjects.Count, Params);
224 return_ACPI_STATUS (Status);
228 /*******************************************************************************
230 * FUNCTION: AcpiDbExecuteSetup
232 * PARAMETERS: Info - Valid method info
236 * DESCRIPTION: Setup info segment prior to method execution
238 ******************************************************************************/
242 ACPI_DB_METHOD_INFO *Info)
247 ACPI_FUNCTION_NAME (DbExecuteSetup);
250 /* Catenate the current scope to the supplied name */
252 Info->Pathname[0] = 0;
253 if ((Info->Name[0] != '\\') &&
254 (Info->Name[0] != '/'))
256 if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
259 Status = AE_BUFFER_OVERFLOW;
264 if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
267 Status = AE_BUFFER_OVERFLOW;
271 AcpiDbPrepNamestring (Info->Pathname);
273 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
274 AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
276 if (Info->Flags & EX_SINGLE_STEP)
278 AcpiGbl_CmSingleStep = TRUE;
279 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
284 /* No single step, allow redirection to a file */
286 AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
293 ACPI_EXCEPTION ((AE_INFO, Status, "During setup for method execution"));
298 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
301 ACPI_MEMORY_LIST *Cache)
304 return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
308 /*******************************************************************************
310 * FUNCTION: AcpiDbGetOutstandingAllocations
314 * RETURN: Current global allocation count minus cache entries
316 * DESCRIPTION: Determine the current number of "outstanding" allocations --
317 * those allocations that have not been freed and also are not
318 * in one of the various object caches.
320 ******************************************************************************/
323 AcpiDbGetOutstandingAllocations (
326 UINT32 Outstanding = 0;
328 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
330 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
331 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
332 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
333 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
336 return (Outstanding);
340 /*******************************************************************************
342 * FUNCTION: AcpiDbExecutionWalk
344 * PARAMETERS: WALK_CALLBACK
348 * DESCRIPTION: Execute a control method. Name is relative to the current
351 ******************************************************************************/
354 AcpiDbExecutionWalk (
355 ACPI_HANDLE ObjHandle,
360 ACPI_OPERAND_OBJECT *ObjDesc;
361 ACPI_NAMESPACE_NODE *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
362 ACPI_BUFFER ReturnObj;
366 ObjDesc = AcpiNsGetAttachedObject (Node);
367 if (ObjDesc->Method.ParamCount)
372 ReturnObj.Pointer = NULL;
373 ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
375 AcpiNsPrintNodePathname (Node, "Evaluating");
377 /* Do the actual method execution */
380 AcpiGbl_MethodExecuting = TRUE;
382 Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
384 AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n", AcpiUtGetNodeName (Node),
385 AcpiFormatException (Status));
386 AcpiGbl_MethodExecuting = FALSE;
392 /*******************************************************************************
394 * FUNCTION: AcpiDbExecute
396 * PARAMETERS: Name - Name of method to execute
397 * Args - Parameters to the method
398 * Flags - single step/no single step
402 * DESCRIPTION: Execute a control method. Name is relative to the current
405 ******************************************************************************/
411 ACPI_OBJECT_TYPE *Types,
415 ACPI_BUFFER ReturnObj;
419 #ifdef ACPI_DEBUG_OUTPUT
420 UINT32 PreviousAllocations;
424 /* Memory allocation tracking */
426 PreviousAllocations = AcpiDbGetOutstandingAllocations ();
431 (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
432 ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
437 NameString = ACPI_ALLOCATE (ACPI_STRLEN (Name) + 1);
443 ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
445 ACPI_STRCPY (NameString, Name);
446 AcpiUtStrupr (NameString);
447 AcpiGbl_DbMethodInfo.Name = NameString;
448 AcpiGbl_DbMethodInfo.Args = Args;
449 AcpiGbl_DbMethodInfo.Types = Types;
450 AcpiGbl_DbMethodInfo.Flags = Flags;
452 ReturnObj.Pointer = NULL;
453 ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
455 Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
456 if (ACPI_FAILURE (Status))
458 ACPI_FREE (NameString);
462 /* Get the NS node, determines existence also */
464 Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
465 &AcpiGbl_DbMethodInfo.Method);
466 if (ACPI_SUCCESS (Status))
468 Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo, &ReturnObj);
470 ACPI_FREE (NameString);
474 * Allow any handlers in separate threads to complete.
475 * (Such as Notify handlers invoked from AML executed above).
477 AcpiOsSleep ((UINT64) 10);
479 #ifdef ACPI_DEBUG_OUTPUT
481 /* Memory allocation tracking */
483 Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
485 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
489 AcpiOsPrintf ("0x%X Outstanding allocations after evaluation of %s\n",
490 Allocations, AcpiGbl_DbMethodInfo.Pathname);
494 if (ACPI_FAILURE (Status))
496 AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
497 AcpiGbl_DbMethodInfo.Pathname, AcpiFormatException (Status));
501 /* Display a return object, if any */
503 if (ReturnObj.Length)
506 "Evaluation of %s returned object %p, external buffer length %X\n",
507 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
508 (UINT32) ReturnObj.Length);
509 AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
511 /* Dump a _PLD buffer if present */
513 if (ACPI_COMPARE_NAME ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
514 AcpiGbl_DbMethodInfo.Method)->Name.Ascii), METHOD_NAME__PLD))
516 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
521 AcpiOsPrintf ("No object was returned from evaluation of %s\n",
522 AcpiGbl_DbMethodInfo.Pathname);
526 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
530 /*******************************************************************************
532 * FUNCTION: AcpiDbMethodThread
534 * PARAMETERS: Context - Execution info segment
538 * DESCRIPTION: Debugger execute thread. Waits for a command line, then
539 * simply dispatches it.
541 ******************************************************************************/
543 static void ACPI_SYSTEM_XFACE
548 ACPI_DB_METHOD_INFO *Info = Context;
549 ACPI_DB_METHOD_INFO LocalInfo;
552 ACPI_BUFFER ReturnObj;
556 * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
557 * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
560 * Note: The arguments we are passing are used by the ASL test suite
561 * (aslts). Do not change them without updating the tests.
563 (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
567 AcpiDbUint32ToHexString (Info->NumCreated, Info->IndexOfThreadStr);
568 AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (), Info->IdOfThreadStr);
571 if (Info->Threads && (Info->NumCreated < Info->NumThreads))
573 Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
577 LocalInfo.Args = LocalInfo.Arguments;
578 LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
579 LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
580 LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
581 LocalInfo.Arguments[3] = NULL;
583 LocalInfo.Types = LocalInfo.ArgTypes;
585 (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
587 for (i = 0; i < Info->NumLoops; i++)
589 Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
590 if (ACPI_FAILURE (Status))
592 AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
593 AcpiFormatException (Status), Info->Pathname, i);
594 if (Status == AE_ABORT_METHOD)
603 AcpiOsPrintf ("%u loops, Thread 0x%x\n", i, AcpiOsGetThreadId ());
606 if (ReturnObj.Length)
608 AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
609 Info->Pathname, ReturnObj.Pointer, (UINT32) ReturnObj.Length);
610 AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
615 /* Signal our completion */
618 (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate, 1, ACPI_WAIT_FOREVER);
619 Info->NumCompleted++;
621 if (Info->NumCompleted == Info->NumThreads)
623 /* Do signal for main thread once only */
627 (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
631 Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
632 if (ACPI_FAILURE (Status))
634 AcpiOsPrintf ("Could not signal debugger thread sync semaphore, %s\n",
635 AcpiFormatException (Status));
641 /*******************************************************************************
643 * FUNCTION: AcpiDbCreateExecutionThreads
645 * PARAMETERS: NumThreadsArg - Number of threads to create
646 * NumLoopsArg - Loop count for the thread(s)
647 * MethodNameArg - Control method to execute
651 * DESCRIPTION: Create threads to execute method(s)
653 ******************************************************************************/
656 AcpiDbCreateExecutionThreads (
666 ACPI_MUTEX MainThreadGate;
667 ACPI_MUTEX ThreadCompleteGate;
671 /* Get the arguments */
673 NumThreads = ACPI_STRTOUL (NumThreadsArg, NULL, 0);
674 NumLoops = ACPI_STRTOUL (NumLoopsArg, NULL, 0);
676 if (!NumThreads || !NumLoops)
678 AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
679 NumThreads, NumLoops);
684 * Create the semaphore for synchronization of
685 * the created threads with the main thread.
687 Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
688 if (ACPI_FAILURE (Status))
690 AcpiOsPrintf ("Could not create semaphore for synchronization with the main thread, %s\n",
691 AcpiFormatException (Status));
696 * Create the semaphore for synchronization
697 * between the created threads.
699 Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
700 if (ACPI_FAILURE (Status))
702 AcpiOsPrintf ("Could not create semaphore for synchronization between the created threads, %s\n",
703 AcpiFormatException (Status));
704 (void) AcpiOsDeleteSemaphore (MainThreadGate);
708 Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
709 if (ACPI_FAILURE (Status))
711 AcpiOsPrintf ("Could not create semaphore for synchronization of AcpiGbl_DbMethodInfo, %s\n",
712 AcpiFormatException (Status));
713 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
714 (void) AcpiOsDeleteSemaphore (MainThreadGate);
718 ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
720 /* Array to store IDs of threads */
722 AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
723 Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
724 AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
725 if (AcpiGbl_DbMethodInfo.Threads == NULL)
727 AcpiOsPrintf ("No memory for thread IDs array\n");
728 (void) AcpiOsDeleteSemaphore (MainThreadGate);
729 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
730 (void) AcpiOsDeleteSemaphore (InfoGate);
733 ACPI_MEMSET (AcpiGbl_DbMethodInfo.Threads, 0, Size);
735 /* Setup the context to be passed to each thread */
737 AcpiGbl_DbMethodInfo.Name = MethodNameArg;
738 AcpiGbl_DbMethodInfo.Flags = 0;
739 AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
740 AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
741 AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
742 AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
744 /* Init arguments to be passed to method */
746 AcpiGbl_DbMethodInfo.InitArgs = 1;
747 AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
748 AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
749 AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
750 AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
751 AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
753 AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
754 AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
755 AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
756 AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
758 AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
760 Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
761 if (ACPI_FAILURE (Status))
766 /* Get the NS node, determines existence also */
768 Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
769 &AcpiGbl_DbMethodInfo.Method);
770 if (ACPI_FAILURE (Status))
772 AcpiOsPrintf ("%s Could not get handle for %s\n",
773 AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
777 /* Create the threads */
779 AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
780 NumThreads, NumLoops);
782 for (i = 0; i < (NumThreads); i++)
784 Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbMethodThread,
785 &AcpiGbl_DbMethodInfo);
786 if (ACPI_FAILURE (Status))
792 /* Wait for all threads to complete */
794 (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
796 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
797 AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
798 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
802 /* Cleanup and exit */
804 (void) AcpiOsDeleteSemaphore (MainThreadGate);
805 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
806 (void) AcpiOsDeleteSemaphore (InfoGate);
808 AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
809 AcpiGbl_DbMethodInfo.Threads = NULL;
812 #endif /* ACPI_DEBUGGER */