1 /*******************************************************************************
3 * Module Name: dbexec - debugger control method execution
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acdebug.h>
48 #include <contrib/dev/acpica/include/acnamesp.h>
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)
245 /* Catenate the current scope to the supplied name */
247 Info->Pathname[0] = 0;
248 if ((Info->Name[0] != '\\') &&
249 (Info->Name[0] != '/'))
251 ACPI_STRCAT (Info->Pathname, AcpiGbl_DbScopeBuf);
254 ACPI_STRCAT (Info->Pathname, Info->Name);
255 AcpiDbPrepNamestring (Info->Pathname);
257 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
258 AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
260 if (Info->Flags & EX_SINGLE_STEP)
262 AcpiGbl_CmSingleStep = TRUE;
263 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
268 /* No single step, allow redirection to a file */
270 AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
275 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
278 ACPI_MEMORY_LIST *Cache)
281 return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
285 /*******************************************************************************
287 * FUNCTION: AcpiDbGetOutstandingAllocations
291 * RETURN: Current global allocation count minus cache entries
293 * DESCRIPTION: Determine the current number of "outstanding" allocations --
294 * those allocations that have not been freed and also are not
295 * in one of the various object caches.
297 ******************************************************************************/
300 AcpiDbGetOutstandingAllocations (
303 UINT32 Outstanding = 0;
305 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
307 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
308 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
309 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
310 Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
313 return (Outstanding);
317 /*******************************************************************************
319 * FUNCTION: AcpiDbExecutionWalk
321 * PARAMETERS: WALK_CALLBACK
325 * DESCRIPTION: Execute a control method. Name is relative to the current
328 ******************************************************************************/
331 AcpiDbExecutionWalk (
332 ACPI_HANDLE ObjHandle,
337 ACPI_OPERAND_OBJECT *ObjDesc;
338 ACPI_NAMESPACE_NODE *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
339 ACPI_BUFFER ReturnObj;
343 ObjDesc = AcpiNsGetAttachedObject (Node);
344 if (ObjDesc->Method.ParamCount)
349 ReturnObj.Pointer = NULL;
350 ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
352 AcpiNsPrintNodePathname (Node, "Evaluating");
354 /* Do the actual method execution */
357 AcpiGbl_MethodExecuting = TRUE;
359 Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
361 AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n", AcpiUtGetNodeName (Node),
362 AcpiFormatException (Status));
363 AcpiGbl_MethodExecuting = FALSE;
369 /*******************************************************************************
371 * FUNCTION: AcpiDbExecute
373 * PARAMETERS: Name - Name of method to execute
374 * Args - Parameters to the method
375 * Flags - single step/no single step
379 * DESCRIPTION: Execute a control method. Name is relative to the current
382 ******************************************************************************/
388 ACPI_OBJECT_TYPE *Types,
392 ACPI_BUFFER ReturnObj;
396 #ifdef ACPI_DEBUG_OUTPUT
397 UINT32 PreviousAllocations;
401 /* Memory allocation tracking */
403 PreviousAllocations = AcpiDbGetOutstandingAllocations ();
408 (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
409 ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
414 NameString = ACPI_ALLOCATE (ACPI_STRLEN (Name) + 1);
420 ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
422 ACPI_STRCPY (NameString, Name);
423 AcpiUtStrupr (NameString);
424 AcpiGbl_DbMethodInfo.Name = NameString;
425 AcpiGbl_DbMethodInfo.Args = Args;
426 AcpiGbl_DbMethodInfo.Types = Types;
427 AcpiGbl_DbMethodInfo.Flags = Flags;
429 ReturnObj.Pointer = NULL;
430 ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
432 AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
434 /* Get the NS node, determines existence also */
436 Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
437 &AcpiGbl_DbMethodInfo.Method);
438 if (ACPI_SUCCESS (Status))
440 Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo, &ReturnObj);
442 ACPI_FREE (NameString);
446 * Allow any handlers in separate threads to complete.
447 * (Such as Notify handlers invoked from AML executed above).
449 AcpiOsSleep ((UINT64) 10);
451 #ifdef ACPI_DEBUG_OUTPUT
453 /* Memory allocation tracking */
455 Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
457 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
461 AcpiOsPrintf ("0x%X Outstanding allocations after evaluation of %s\n",
462 Allocations, AcpiGbl_DbMethodInfo.Pathname);
466 if (ACPI_FAILURE (Status))
468 AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
469 AcpiGbl_DbMethodInfo.Pathname, AcpiFormatException (Status));
473 /* Display a return object, if any */
475 if (ReturnObj.Length)
478 "Evaluation of %s returned object %p, external buffer length %X\n",
479 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
480 (UINT32) ReturnObj.Length);
481 AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
483 /* Dump a _PLD buffer if present */
485 if (ACPI_COMPARE_NAME ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
486 AcpiGbl_DbMethodInfo.Method)->Name.Ascii), METHOD_NAME__PLD))
488 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
493 AcpiOsPrintf ("No object was returned from evaluation of %s\n",
494 AcpiGbl_DbMethodInfo.Pathname);
498 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
502 /*******************************************************************************
504 * FUNCTION: AcpiDbMethodThread
506 * PARAMETERS: Context - Execution info segment
510 * DESCRIPTION: Debugger execute thread. Waits for a command line, then
511 * simply dispatches it.
513 ******************************************************************************/
515 static void ACPI_SYSTEM_XFACE
520 ACPI_DB_METHOD_INFO *Info = Context;
521 ACPI_DB_METHOD_INFO LocalInfo;
524 ACPI_BUFFER ReturnObj;
528 * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
529 * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
532 * Note: The arguments we are passing are used by the ASL test suite
533 * (aslts). Do not change them without updating the tests.
535 (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
539 AcpiDbUint32ToHexString (Info->NumCreated, Info->IndexOfThreadStr);
540 AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (), Info->IdOfThreadStr);
543 if (Info->Threads && (Info->NumCreated < Info->NumThreads))
545 Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
549 LocalInfo.Args = LocalInfo.Arguments;
550 LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
551 LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
552 LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
553 LocalInfo.Arguments[3] = NULL;
555 LocalInfo.Types = LocalInfo.ArgTypes;
557 (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
559 for (i = 0; i < Info->NumLoops; i++)
561 Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
562 if (ACPI_FAILURE (Status))
564 AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
565 AcpiFormatException (Status), Info->Pathname, i);
566 if (Status == AE_ABORT_METHOD)
575 AcpiOsPrintf ("%u loops, Thread 0x%x\n", i, AcpiOsGetThreadId ());
578 if (ReturnObj.Length)
580 AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
581 Info->Pathname, ReturnObj.Pointer, (UINT32) ReturnObj.Length);
582 AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
587 /* Signal our completion */
590 (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate, 1, ACPI_WAIT_FOREVER);
591 Info->NumCompleted++;
593 if (Info->NumCompleted == Info->NumThreads)
595 /* Do signal for main thread once only */
599 (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
603 Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
604 if (ACPI_FAILURE (Status))
606 AcpiOsPrintf ("Could not signal debugger thread sync semaphore, %s\n",
607 AcpiFormatException (Status));
613 /*******************************************************************************
615 * FUNCTION: AcpiDbCreateExecutionThreads
617 * PARAMETERS: NumThreadsArg - Number of threads to create
618 * NumLoopsArg - Loop count for the thread(s)
619 * MethodNameArg - Control method to execute
623 * DESCRIPTION: Create threads to execute method(s)
625 ******************************************************************************/
628 AcpiDbCreateExecutionThreads (
638 ACPI_MUTEX MainThreadGate;
639 ACPI_MUTEX ThreadCompleteGate;
643 /* Get the arguments */
645 NumThreads = ACPI_STRTOUL (NumThreadsArg, NULL, 0);
646 NumLoops = ACPI_STRTOUL (NumLoopsArg, NULL, 0);
648 if (!NumThreads || !NumLoops)
650 AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
651 NumThreads, NumLoops);
656 * Create the semaphore for synchronization of
657 * the created threads with the main thread.
659 Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
660 if (ACPI_FAILURE (Status))
662 AcpiOsPrintf ("Could not create semaphore for synchronization with the main thread, %s\n",
663 AcpiFormatException (Status));
668 * Create the semaphore for synchronization
669 * between the created threads.
671 Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
672 if (ACPI_FAILURE (Status))
674 AcpiOsPrintf ("Could not create semaphore for synchronization between the created threads, %s\n",
675 AcpiFormatException (Status));
676 (void) AcpiOsDeleteSemaphore (MainThreadGate);
680 Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
681 if (ACPI_FAILURE (Status))
683 AcpiOsPrintf ("Could not create semaphore for synchronization of AcpiGbl_DbMethodInfo, %s\n",
684 AcpiFormatException (Status));
685 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
686 (void) AcpiOsDeleteSemaphore (MainThreadGate);
690 ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
692 /* Array to store IDs of threads */
694 AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
695 Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
696 AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
697 if (AcpiGbl_DbMethodInfo.Threads == NULL)
699 AcpiOsPrintf ("No memory for thread IDs array\n");
700 (void) AcpiOsDeleteSemaphore (MainThreadGate);
701 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
702 (void) AcpiOsDeleteSemaphore (InfoGate);
705 ACPI_MEMSET (AcpiGbl_DbMethodInfo.Threads, 0, Size);
707 /* Setup the context to be passed to each thread */
709 AcpiGbl_DbMethodInfo.Name = MethodNameArg;
710 AcpiGbl_DbMethodInfo.Flags = 0;
711 AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
712 AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
713 AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
714 AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
716 /* Init arguments to be passed to method */
718 AcpiGbl_DbMethodInfo.InitArgs = 1;
719 AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
720 AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
721 AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
722 AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
723 AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
725 AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
726 AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
727 AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
728 AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
730 AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
732 AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
734 /* Get the NS node, determines existence also */
736 Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
737 &AcpiGbl_DbMethodInfo.Method);
738 if (ACPI_FAILURE (Status))
740 AcpiOsPrintf ("%s Could not get handle for %s\n",
741 AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
745 /* Create the threads */
747 AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
748 NumThreads, NumLoops);
750 for (i = 0; i < (NumThreads); i++)
752 Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbMethodThread,
753 &AcpiGbl_DbMethodInfo);
754 if (ACPI_FAILURE (Status))
760 /* Wait for all threads to complete */
762 (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
764 AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
765 AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
766 AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
770 /* Cleanup and exit */
772 (void) AcpiOsDeleteSemaphore (MainThreadGate);
773 (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
774 (void) AcpiOsDeleteSemaphore (InfoGate);
776 AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
777 AcpiGbl_DbMethodInfo.Threads = NULL;
780 #endif /* ACPI_DEBUGGER */