1 /*******************************************************************************
3 * Module Name: dbexec - debugger control method execution
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2012, 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:
112 ACPI_FREE (Objects[i].Buffer.Pointer);
115 case ACPI_TYPE_PACKAGE:
117 /* Recursive call to delete package elements */
119 AcpiDbDeleteObjects (Objects[i].Package.Count,
120 Objects[i].Package.Elements);
122 /* Free the elements array */
124 ACPI_FREE (Objects[i].Package.Elements);
134 /*******************************************************************************
136 * FUNCTION: AcpiDbExecuteMethod
138 * PARAMETERS: Info - Valid info segment
139 * ReturnObj - Where to put return object
143 * DESCRIPTION: Execute a control method.
145 ******************************************************************************/
148 AcpiDbExecuteMethod (
149 ACPI_DB_METHOD_INFO *Info,
150 ACPI_BUFFER *ReturnObj)
153 ACPI_OBJECT_LIST ParamObjects;
154 ACPI_OBJECT Params[ACPI_METHOD_NUM_ARGS];
155 ACPI_DEVICE_INFO *ObjInfo;
159 ACPI_FUNCTION_TRACE (DbExecuteMethod);
162 if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
164 AcpiOsPrintf ("Warning: debug output is not enabled!\n");
167 /* Get the object info for number of method parameters */
169 Status = AcpiGetObjectInfo (Info->Method, &ObjInfo);
170 if (ACPI_FAILURE (Status))
172 return_ACPI_STATUS (Status);
175 ParamObjects.Pointer = NULL;
176 ParamObjects.Count = 0;
178 if (ObjInfo->Type == ACPI_TYPE_METHOD)
180 /* Are there arguments to the method? */
183 if (Info->Args && Info->Args[0])
185 /* Get arguments passed on the command line */
187 for (; Info->Args[i] &&
188 (i < ACPI_METHOD_NUM_ARGS) &&
189 (i < ObjInfo->ParamCount);
192 /* Convert input string (token) to an actual ACPI_OBJECT */
194 Status = AcpiDbConvertToObject (Info->Types[i],
195 Info->Args[i], &Params[i]);
196 if (ACPI_FAILURE (Status))
198 ACPI_EXCEPTION ((AE_INFO, Status,
199 "While parsing method arguments"));
205 /* Create additional "default" parameters as needed */
207 if (i < ObjInfo->ParamCount)
209 AcpiOsPrintf ("Adding %u arguments containing default values\n",
210 ObjInfo->ParamCount - i);
212 for (; i < ObjInfo->ParamCount; i++)
218 Params[0].Type = ACPI_TYPE_INTEGER;
219 Params[0].Integer.Value = 0x01020304;
224 Params[1].Type = ACPI_TYPE_STRING;
225 Params[1].String.Length = 12;
226 Params[1].String.Pointer = "AML Debugger";
231 Params[i].Type = ACPI_TYPE_INTEGER;
232 Params[i].Integer.Value = i * (UINT64) 0x1000;
238 ParamObjects.Count = ObjInfo->ParamCount;
239 ParamObjects.Pointer = Params;
242 /* Prepare for a return object of arbitrary size */
244 ReturnObj->Pointer = AcpiGbl_DbBuffer;
245 ReturnObj->Length = ACPI_DEBUG_BUFFER_SIZE;
247 /* Do the actual method execution */
249 AcpiGbl_MethodExecuting = TRUE;
250 Status = AcpiEvaluateObject (NULL,
251 Info->Pathname, &ParamObjects, ReturnObj);
253 AcpiGbl_CmSingleStep = FALSE;
254 AcpiGbl_MethodExecuting = FALSE;
256 if (ACPI_FAILURE (Status))
258 ACPI_EXCEPTION ((AE_INFO, Status,
259 "while executing %s from debugger", Info->Pathname));
261 if (Status == AE_BUFFER_OVERFLOW)
263 ACPI_ERROR ((AE_INFO,
264 "Possible overflow of internal debugger buffer (size 0x%X needed 0x%X)",
265 ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
270 AcpiDbDeleteObjects (ObjInfo->ParamCount, Params);
273 return_ACPI_STATUS (Status);
277 /*******************************************************************************
279 * FUNCTION: AcpiDbExecuteSetup
281 * PARAMETERS: Info - Valid method info
285 * DESCRIPTION: Setup info segment prior to method execution
287 ******************************************************************************/
291 ACPI_DB_METHOD_INFO *Info)
294 /* Catenate the current scope to the supplied name */
296 Info->Pathname[0] = 0;
297 if ((Info->Name[0] != '\\') &&
298 (Info->Name[0] != '/'))
300 ACPI_STRCAT (Info->Pathname, AcpiGbl_DbScopeBuf);
303 ACPI_STRCAT (Info->Pathname, Info->Name);
304 AcpiDbPrepNamestring (Info->Pathname);
306 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
307 AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
309 if (Info->Flags & EX_SINGLE_STEP)
311 AcpiGbl_CmSingleStep = TRUE;
312 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
317 /* No single step, allow redirection to a file */
319 AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
324 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
327 ACPI_MEMORY_LIST *Cache)
330 return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
334 /*******************************************************************************
336 * FUNCTION: AcpiDbGetOutstandingAllocations
340 * RETURN: Current global allocation count minus cache entries
342 * DESCRIPTION: Determine the current number of "outstanding" allocations --
343 * those allocations that have not been freed and also are not
344 * in one of the various object caches.
346 ******************************************************************************/
349 AcpiDbGetOutstandingAllocations (
352 UINT32 Outstanding = 0;
354 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
356 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
357 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
358 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
359 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
362 return (Outstanding);
366 /*******************************************************************************
368 * FUNCTION: AcpiDbExecutionWalk
370 * PARAMETERS: WALK_CALLBACK
374 * DESCRIPTION: Execute a control method. Name is relative to the current
377 ******************************************************************************/
380 AcpiDbExecutionWalk (
381 ACPI_HANDLE ObjHandle,
386 ACPI_OPERAND_OBJECT *ObjDesc;
387 ACPI_NAMESPACE_NODE *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
388 ACPI_BUFFER ReturnObj;
392 ObjDesc = AcpiNsGetAttachedObject (Node);
393 if (ObjDesc->Method.ParamCount)
398 ReturnObj.Pointer = NULL;
399 ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
401 AcpiNsPrintNodePathname (Node, "Evaluating");
403 /* Do the actual method execution */
406 AcpiGbl_MethodExecuting = TRUE;
408 Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
410 AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n", AcpiUtGetNodeName (Node),
411 AcpiFormatException (Status));
412 AcpiGbl_MethodExecuting = FALSE;
418 /*******************************************************************************
420 * FUNCTION: AcpiDbExecute
422 * PARAMETERS: Name - Name of method to execute
423 * Args - Parameters to the method
424 * Flags - single step/no single step
428 * DESCRIPTION: Execute a control method. Name is relative to the current
431 ******************************************************************************/
437 ACPI_OBJECT_TYPE *Types,
441 ACPI_BUFFER ReturnObj;
445 #ifdef ACPI_DEBUG_OUTPUT
446 UINT32 PreviousAllocations;
450 /* Memory allocation tracking */
452 PreviousAllocations = AcpiDbGetOutstandingAllocations ();
457 (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
458 ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
463 NameString = ACPI_ALLOCATE (ACPI_STRLEN (Name) + 1);
469 ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
471 ACPI_STRCPY (NameString, Name);
472 AcpiUtStrupr (NameString);
473 AcpiGbl_DbMethodInfo.Name = NameString;
474 AcpiGbl_DbMethodInfo.Args = Args;
475 AcpiGbl_DbMethodInfo.Types = Types;
476 AcpiGbl_DbMethodInfo.Flags = Flags;
478 ReturnObj.Pointer = NULL;
479 ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
481 AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
483 /* Get the NS node, determines existence also */
485 Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
486 &AcpiGbl_DbMethodInfo.Method);
487 if (ACPI_SUCCESS (Status))
489 Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo, &ReturnObj);
491 ACPI_FREE (NameString);
495 * Allow any handlers in separate threads to complete.
496 * (Such as Notify handlers invoked from AML executed above).
498 AcpiOsSleep ((UINT64) 10);
500 #ifdef ACPI_DEBUG_OUTPUT
502 /* Memory allocation tracking */
504 Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
506 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
510 AcpiOsPrintf ("0x%X Outstanding allocations after evaluation of %s\n",
511 Allocations, AcpiGbl_DbMethodInfo.Pathname);
515 if (ACPI_FAILURE (Status))
517 AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
518 AcpiGbl_DbMethodInfo.Pathname, AcpiFormatException (Status));
522 /* Display a return object, if any */
524 if (ReturnObj.Length)
527 "Evaluation of %s returned object %p, external buffer length %X\n",
528 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
529 (UINT32) ReturnObj.Length);
530 AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
532 /* Dump a _PLD buffer if present */
534 if (ACPI_COMPARE_NAME ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
535 AcpiGbl_DbMethodInfo.Method)->Name.Ascii), METHOD_NAME__PLD))
537 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
542 AcpiOsPrintf ("No object was returned from evaluation of %s\n",
543 AcpiGbl_DbMethodInfo.Pathname);
547 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
551 /*******************************************************************************
553 * FUNCTION: AcpiDbMethodThread
555 * PARAMETERS: Context - Execution info segment
559 * DESCRIPTION: Debugger execute thread. Waits for a command line, then
560 * simply dispatches it.
562 ******************************************************************************/
564 static void ACPI_SYSTEM_XFACE
569 ACPI_DB_METHOD_INFO *Info = Context;
570 ACPI_DB_METHOD_INFO LocalInfo;
573 ACPI_BUFFER ReturnObj;
577 * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
578 * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
581 * Note: The arguments we are passing are used by the ASL test suite
582 * (aslts). Do not change them without updating the tests.
584 (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
588 AcpiDbUint32ToHexString (Info->NumCreated, Info->IndexOfThreadStr);
589 AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (), Info->IdOfThreadStr);
592 if (Info->Threads && (Info->NumCreated < Info->NumThreads))
594 Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
598 LocalInfo.Args = LocalInfo.Arguments;
599 LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
600 LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
601 LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
602 LocalInfo.Arguments[3] = NULL;
604 LocalInfo.Types = LocalInfo.ArgTypes;
606 (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
608 for (i = 0; i < Info->NumLoops; i++)
610 Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
611 if (ACPI_FAILURE (Status))
613 AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
614 AcpiFormatException (Status), Info->Pathname, i);
615 if (Status == AE_ABORT_METHOD)
624 AcpiOsPrintf ("%u loops, Thread 0x%x\n", i, AcpiOsGetThreadId ());
627 if (ReturnObj.Length)
629 AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
630 Info->Pathname, ReturnObj.Pointer, (UINT32) ReturnObj.Length);
631 AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
636 /* Signal our completion */
639 (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate, 1, ACPI_WAIT_FOREVER);
640 Info->NumCompleted++;
642 if (Info->NumCompleted == Info->NumThreads)
644 /* Do signal for main thread once only */
648 (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
652 Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
653 if (ACPI_FAILURE (Status))
655 AcpiOsPrintf ("Could not signal debugger thread sync semaphore, %s\n",
656 AcpiFormatException (Status));
662 /*******************************************************************************
664 * FUNCTION: AcpiDbCreateExecutionThreads
666 * PARAMETERS: NumThreadsArg - Number of threads to create
667 * NumLoopsArg - Loop count for the thread(s)
668 * MethodNameArg - Control method to execute
672 * DESCRIPTION: Create threads to execute method(s)
674 ******************************************************************************/
677 AcpiDbCreateExecutionThreads (
687 ACPI_MUTEX MainThreadGate;
688 ACPI_MUTEX ThreadCompleteGate;
692 /* Get the arguments */
694 NumThreads = ACPI_STRTOUL (NumThreadsArg, NULL, 0);
695 NumLoops = ACPI_STRTOUL (NumLoopsArg, NULL, 0);
697 if (!NumThreads || !NumLoops)
699 AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
700 NumThreads, NumLoops);
705 * Create the semaphore for synchronization of
706 * the created threads with the main thread.
708 Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
709 if (ACPI_FAILURE (Status))
711 AcpiOsPrintf ("Could not create semaphore for synchronization with the main thread, %s\n",
712 AcpiFormatException (Status));
717 * Create the semaphore for synchronization
718 * between the created threads.
720 Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
721 if (ACPI_FAILURE (Status))
723 AcpiOsPrintf ("Could not create semaphore for synchronization between the created threads, %s\n",
724 AcpiFormatException (Status));
725 (void) AcpiOsDeleteSemaphore (MainThreadGate);
729 Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
730 if (ACPI_FAILURE (Status))
732 AcpiOsPrintf ("Could not create semaphore for synchronization of AcpiGbl_DbMethodInfo, %s\n",
733 AcpiFormatException (Status));
734 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
735 (void) AcpiOsDeleteSemaphore (MainThreadGate);
739 ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
741 /* Array to store IDs of threads */
743 AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
744 Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
745 AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
746 if (AcpiGbl_DbMethodInfo.Threads == NULL)
748 AcpiOsPrintf ("No memory for thread IDs array\n");
749 (void) AcpiOsDeleteSemaphore (MainThreadGate);
750 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
751 (void) AcpiOsDeleteSemaphore (InfoGate);
754 ACPI_MEMSET (AcpiGbl_DbMethodInfo.Threads, 0, Size);
756 /* Setup the context to be passed to each thread */
758 AcpiGbl_DbMethodInfo.Name = MethodNameArg;
759 AcpiGbl_DbMethodInfo.Flags = 0;
760 AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
761 AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
762 AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
763 AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
765 /* Init arguments to be passed to method */
767 AcpiGbl_DbMethodInfo.InitArgs = 1;
768 AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
769 AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
770 AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
771 AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
772 AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
774 AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
775 AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
776 AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
777 AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
779 AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
781 AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
783 /* Get the NS node, determines existence also */
785 Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
786 &AcpiGbl_DbMethodInfo.Method);
787 if (ACPI_FAILURE (Status))
789 AcpiOsPrintf ("%s Could not get handle for %s\n",
790 AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
794 /* Create the threads */
796 AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
797 NumThreads, NumLoops);
799 for (i = 0; i < (NumThreads); i++)
801 Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbMethodThread,
802 &AcpiGbl_DbMethodInfo);
803 if (ACPI_FAILURE (Status))
809 /* Wait for all threads to complete */
811 (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
813 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
814 AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
815 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
819 /* Cleanup and exit */
821 (void) AcpiOsDeleteSemaphore (MainThreadGate);
822 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
823 (void) AcpiOsDeleteSemaphore (InfoGate);
825 AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
826 AcpiGbl_DbMethodInfo.Threads = NULL;
829 #endif /* ACPI_DEBUGGER */