]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/components/debugger/dbmethod.c
Import ACPICA 20121114.
[FreeBSD/FreeBSD.git] / source / components / debugger / dbmethod.c
1 /*******************************************************************************
2  *
3  * Module Name: dbmethod - Debug commands for control methods
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2012, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43
44
45 #include "acpi.h"
46 #include "accommon.h"
47 #include "acdispat.h"
48 #include "acnamesp.h"
49 #include "acdebug.h"
50 #include "acdisasm.h"
51 #include "acparser.h"
52
53
54 #ifdef ACPI_DEBUGGER
55
56 #define _COMPONENT          ACPI_CA_DEBUGGER
57         ACPI_MODULE_NAME    ("dbmethod")
58
59
60 /* Local prototypes */
61
62 static ACPI_STATUS
63 AcpiDbWalkForExecute (
64     ACPI_HANDLE             ObjHandle,
65     UINT32                  NestingLevel,
66     void                    *Context,
67     void                    **ReturnValue);
68
69
70 /*******************************************************************************
71  *
72  * FUNCTION:    AcpiDbSetMethodBreakpoint
73  *
74  * PARAMETERS:  Location            - AML offset of breakpoint
75  *              WalkState           - Current walk info
76  *              Op                  - Current Op (from parse walk)
77  *
78  * RETURN:      None
79  *
80  * DESCRIPTION: Set a breakpoint in a control method at the specified
81  *              AML offset
82  *
83  ******************************************************************************/
84
85 void
86 AcpiDbSetMethodBreakpoint (
87     char                    *Location,
88     ACPI_WALK_STATE         *WalkState,
89     ACPI_PARSE_OBJECT       *Op)
90 {
91     UINT32                  Address;
92
93
94     if (!Op)
95     {
96         AcpiOsPrintf ("There is no method currently executing\n");
97         return;
98     }
99
100     /* Get and verify the breakpoint address */
101
102     Address = ACPI_STRTOUL (Location, NULL, 16);
103     if (Address <= Op->Common.AmlOffset)
104     {
105         AcpiOsPrintf ("Breakpoint %X is beyond current address %X\n",
106             Address, Op->Common.AmlOffset);
107     }
108
109     /* Save breakpoint in current walk */
110
111     WalkState->UserBreakpoint = Address;
112     AcpiOsPrintf ("Breakpoint set at AML offset %X\n", Address);
113 }
114
115
116 /*******************************************************************************
117  *
118  * FUNCTION:    AcpiDbSetMethodCallBreakpoint
119  *
120  * PARAMETERS:  Op                  - Current Op (from parse walk)
121  *
122  * RETURN:      None
123  *
124  * DESCRIPTION: Set a breakpoint in a control method at the specified
125  *              AML offset
126  *
127  ******************************************************************************/
128
129 void
130 AcpiDbSetMethodCallBreakpoint (
131     ACPI_PARSE_OBJECT       *Op)
132 {
133
134
135     if (!Op)
136     {
137         AcpiOsPrintf ("There is no method currently executing\n");
138         return;
139     }
140
141     AcpiGbl_StepToNextCall = TRUE;
142 }
143
144
145 /*******************************************************************************
146  *
147  * FUNCTION:    AcpiDbSetMethodData
148  *
149  * PARAMETERS:  TypeArg         - L for local, A for argument
150  *              IndexArg        - which one
151  *              ValueArg        - Value to set.
152  *
153  * RETURN:      None
154  *
155  * DESCRIPTION: Set a local or argument for the running control method.
156  *              NOTE: only object supported is Number.
157  *
158  ******************************************************************************/
159
160 void
161 AcpiDbSetMethodData (
162     char                    *TypeArg,
163     char                    *IndexArg,
164     char                    *ValueArg)
165 {
166     char                    Type;
167     UINT32                  Index;
168     UINT32                  Value;
169     ACPI_WALK_STATE         *WalkState;
170     ACPI_OPERAND_OBJECT     *ObjDesc;
171     ACPI_STATUS             Status;
172     ACPI_NAMESPACE_NODE     *Node;
173
174
175     /* Validate TypeArg */
176
177     AcpiUtStrupr (TypeArg);
178     Type = TypeArg[0];
179     if ((Type != 'L') &&
180         (Type != 'A') &&
181         (Type != 'N'))
182     {
183         AcpiOsPrintf ("Invalid SET operand: %s\n", TypeArg);
184         return;
185     }
186
187     Value = ACPI_STRTOUL (ValueArg, NULL, 16);
188
189     if (Type == 'N')
190     {
191         Node = AcpiDbConvertToNode (IndexArg);
192         if (Node->Type != ACPI_TYPE_INTEGER)
193         {
194             AcpiOsPrintf ("Can only set Integer nodes\n");
195             return;
196         }
197         ObjDesc = Node->Object;
198         ObjDesc->Integer.Value = Value;
199         return;
200     }
201
202     /* Get the index and value */
203
204     Index = ACPI_STRTOUL (IndexArg, NULL, 16);
205
206     WalkState = AcpiDsGetCurrentWalkState (AcpiGbl_CurrentWalkList);
207     if (!WalkState)
208     {
209         AcpiOsPrintf ("There is no method currently executing\n");
210         return;
211     }
212
213     /* Create and initialize the new object */
214
215     ObjDesc = AcpiUtCreateIntegerObject ((UINT64) Value);
216     if (!ObjDesc)
217     {
218         AcpiOsPrintf ("Could not create an internal object\n");
219         return;
220     }
221
222     /* Store the new object into the target */
223
224     switch (Type)
225     {
226     case 'A':
227
228         /* Set a method argument */
229
230         if (Index > ACPI_METHOD_MAX_ARG)
231         {
232             AcpiOsPrintf ("Arg%u - Invalid argument name\n", Index);
233             goto Cleanup;
234         }
235
236         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_ARG, Index, ObjDesc,
237                     WalkState);
238         if (ACPI_FAILURE (Status))
239         {
240             goto Cleanup;
241         }
242
243         ObjDesc = WalkState->Arguments[Index].Object;
244
245         AcpiOsPrintf ("Arg%u: ", Index);
246         AcpiDmDisplayInternalObject (ObjDesc, WalkState);
247         break;
248
249     case 'L':
250
251         /* Set a method local */
252
253         if (Index > ACPI_METHOD_MAX_LOCAL)
254         {
255             AcpiOsPrintf ("Local%u - Invalid local variable name\n", Index);
256             goto Cleanup;
257         }
258
259         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_LOCAL, Index, ObjDesc,
260                     WalkState);
261         if (ACPI_FAILURE (Status))
262         {
263             goto Cleanup;
264         }
265
266         ObjDesc = WalkState->LocalVariables[Index].Object;
267
268         AcpiOsPrintf ("Local%u: ", Index);
269         AcpiDmDisplayInternalObject (ObjDesc, WalkState);
270         break;
271
272     default:
273         break;
274     }
275
276 Cleanup:
277     AcpiUtRemoveReference (ObjDesc);
278 }
279
280
281 /*******************************************************************************
282  *
283  * FUNCTION:    AcpiDbDisassembleAml
284  *
285  * PARAMETERS:  Statements          - Number of statements to disassemble
286  *              Op                  - Current Op (from parse walk)
287  *
288  * RETURN:      None
289  *
290  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
291  *              of statements specified.
292  *
293  ******************************************************************************/
294
295 void
296 AcpiDbDisassembleAml (
297     char                    *Statements,
298     ACPI_PARSE_OBJECT       *Op)
299 {
300     UINT32                  NumStatements = 8;
301
302
303     if (!Op)
304     {
305         AcpiOsPrintf ("There is no method currently executing\n");
306         return;
307     }
308
309     if (Statements)
310     {
311         NumStatements = ACPI_STRTOUL (Statements, NULL, 0);
312     }
313
314     AcpiDmDisassemble (NULL, Op, NumStatements);
315 }
316
317
318 /*******************************************************************************
319  *
320  * FUNCTION:    AcpiDbDisassembleMethod
321  *
322  * PARAMETERS:  Name            - Name of control method
323  *
324  * RETURN:      None
325  *
326  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
327  *              of statements specified.
328  *
329  ******************************************************************************/
330
331 ACPI_STATUS
332 AcpiDbDisassembleMethod (
333     char                    *Name)
334 {
335     ACPI_STATUS             Status;
336     ACPI_PARSE_OBJECT       *Op;
337     ACPI_WALK_STATE         *WalkState;
338     ACPI_OPERAND_OBJECT     *ObjDesc;
339     ACPI_NAMESPACE_NODE     *Method;
340
341
342     Method = AcpiDbConvertToNode (Name);
343     if (!Method)
344     {
345         return (AE_BAD_PARAMETER);
346     }
347
348     if (Method->Type != ACPI_TYPE_METHOD)
349     {
350         ACPI_ERROR ((AE_INFO, "%s (%s): Object must be a control method",
351             Name, AcpiUtGetTypeName (Method->Type)));
352         return (AE_BAD_PARAMETER);
353     }
354
355     ObjDesc = Method->Object;
356
357     Op = AcpiPsCreateScopeOp ();
358     if (!Op)
359     {
360         return (AE_NO_MEMORY);
361     }
362
363     /* Create and initialize a new walk state */
364
365     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
366     if (!WalkState)
367     {
368         return (AE_NO_MEMORY);
369     }
370
371     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL,
372         ObjDesc->Method.AmlStart,
373         ObjDesc->Method.AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
374     if (ACPI_FAILURE (Status))
375     {
376         return (Status);
377     }
378
379     Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
380     WalkState->OwnerId = ObjDesc->Method.OwnerId;
381
382     /* Push start scope on scope stack and make it current */
383
384     Status = AcpiDsScopeStackPush (Method,
385         Method->Type, WalkState);
386     if (ACPI_FAILURE (Status))
387     {
388         return (Status);
389     }
390
391     /* Parse the entire method AML including deferred operators */
392
393     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
394     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
395
396     Status = AcpiPsParseAml (WalkState);
397     AcpiDmParseDeferredOps (Op);
398
399     /* Now we can disassemble the method */
400
401     AcpiGbl_DbOpt_verbose = TRUE;
402     AcpiGbl_DbOpt_verbose = FALSE;
403     AcpiDmDisassemble (NULL, Op, 0);
404     AcpiGbl_DbOpt_verbose = TRUE;
405
406     AcpiPsDeleteParseTree (Op);
407
408     /* Method cleanup */
409
410     AcpiNsDeleteNamespaceSubtree (Method);
411     AcpiNsDeleteNamespaceByOwner (ObjDesc->Method.OwnerId);
412     AcpiUtReleaseOwnerId (&ObjDesc->Method.OwnerId);
413     return (AE_OK);
414 }
415
416
417 /*******************************************************************************
418  *
419  * FUNCTION:    AcpiDbWalkForExecute
420  *
421  * PARAMETERS:  Callback from WalkNamespace
422  *
423  * RETURN:      Status
424  *
425  * DESCRIPTION: Batch execution module. Currently only executes predefined
426  *              ACPI names.
427  *
428  ******************************************************************************/
429
430 static ACPI_STATUS
431 AcpiDbWalkForExecute (
432     ACPI_HANDLE             ObjHandle,
433     UINT32                  NestingLevel,
434     void                    *Context,
435     void                    **ReturnValue)
436 {
437     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
438     ACPI_DB_EXECUTE_WALK    *Info = (ACPI_DB_EXECUTE_WALK *) Context;
439     ACPI_BUFFER             ReturnObj;
440     ACPI_STATUS             Status;
441     char                    *Pathname;
442     UINT32                  i;
443     ACPI_DEVICE_INFO        *ObjInfo;
444     ACPI_OBJECT_LIST        ParamObjects;
445     ACPI_OBJECT             Params[ACPI_METHOD_NUM_ARGS];
446     const ACPI_PREDEFINED_INFO *Predefined;
447
448
449     Predefined = AcpiNsCheckForPredefinedName (Node);
450     if (!Predefined)
451     {
452         return (AE_OK);
453     }
454
455     if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
456     {
457         return (AE_OK);
458     }
459
460     Pathname = AcpiNsGetExternalPathname (Node);
461     if (!Pathname)
462     {
463         return (AE_OK);
464     }
465
466     /* Get the object info for number of method parameters */
467
468     Status = AcpiGetObjectInfo (ObjHandle, &ObjInfo);
469     if (ACPI_FAILURE (Status))
470     {
471         return (Status);
472     }
473
474     ParamObjects.Pointer = NULL;
475     ParamObjects.Count   = 0;
476
477     if (ObjInfo->Type == ACPI_TYPE_METHOD)
478     {
479         /* Setup default parameters */
480
481         for (i = 0; i < ObjInfo->ParamCount; i++)
482         {
483             Params[i].Type           = ACPI_TYPE_INTEGER;
484             Params[i].Integer.Value  = 1;
485         }
486
487         ParamObjects.Pointer     = Params;
488         ParamObjects.Count       = ObjInfo->ParamCount;
489     }
490
491     ACPI_FREE (ObjInfo);
492     ReturnObj.Pointer = NULL;
493     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
494
495     /* Do the actual method execution */
496
497     AcpiGbl_MethodExecuting = TRUE;
498
499     Status = AcpiEvaluateObject (Node, NULL, &ParamObjects, &ReturnObj);
500
501     AcpiOsPrintf ("%-32s returned %s\n", Pathname, AcpiFormatException (Status));
502     AcpiGbl_MethodExecuting = FALSE;
503     ACPI_FREE (Pathname);
504
505     /* Ignore status from method execution */
506
507     Status = AE_OK;
508
509     /* Update count, check if we have executed enough methods */
510
511     Info->Count++;
512     if (Info->Count >= Info->MaxCount)
513     {
514         Status = AE_CTRL_TERMINATE;
515     }
516
517     return (Status);
518 }
519
520
521 /*******************************************************************************
522  *
523  * FUNCTION:    AcpiDbBatchExecute
524  *
525  * PARAMETERS:  CountArg            - Max number of methods to execute
526  *
527  * RETURN:      None
528  *
529  * DESCRIPTION: Namespace batch execution. Execute predefined names in the
530  *              namespace, up to the max count, if specified.
531  *
532  ******************************************************************************/
533
534 void
535 AcpiDbBatchExecute (
536     char                    *CountArg)
537 {
538     ACPI_DB_EXECUTE_WALK    Info;
539
540
541     Info.Count = 0;
542     Info.MaxCount = ACPI_UINT32_MAX;
543
544     if (CountArg)
545     {
546         Info.MaxCount = ACPI_STRTOUL (CountArg, NULL, 0);
547     }
548
549
550     /* Search all nodes in namespace */
551
552     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
553                 AcpiDbWalkForExecute, NULL, (void *) &Info, NULL);
554
555     AcpiOsPrintf ("Evaluated %u predefined names in the namespace\n", Info.Count);
556 }
557
558 #endif /* ACPI_DEBUGGER */