]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/components/debugger/dbmethod.c
Import ACPICA 20130418.
[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 - 2013, 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 #include "acpredef.h"
53
54
55 #ifdef ACPI_DEBUGGER
56
57 #define _COMPONENT          ACPI_CA_DEBUGGER
58         ACPI_MODULE_NAME    ("dbmethod")
59
60
61 /* Local prototypes */
62
63 static ACPI_STATUS
64 AcpiDbWalkForExecute (
65     ACPI_HANDLE             ObjHandle,
66     UINT32                  NestingLevel,
67     void                    *Context,
68     void                    **ReturnValue);
69
70
71 /*******************************************************************************
72  *
73  * FUNCTION:    AcpiDbSetMethodBreakpoint
74  *
75  * PARAMETERS:  Location            - AML offset of breakpoint
76  *              WalkState           - Current walk info
77  *              Op                  - Current Op (from parse walk)
78  *
79  * RETURN:      None
80  *
81  * DESCRIPTION: Set a breakpoint in a control method at the specified
82  *              AML offset
83  *
84  ******************************************************************************/
85
86 void
87 AcpiDbSetMethodBreakpoint (
88     char                    *Location,
89     ACPI_WALK_STATE         *WalkState,
90     ACPI_PARSE_OBJECT       *Op)
91 {
92     UINT32                  Address;
93
94
95     if (!Op)
96     {
97         AcpiOsPrintf ("There is no method currently executing\n");
98         return;
99     }
100
101     /* Get and verify the breakpoint address */
102
103     Address = ACPI_STRTOUL (Location, NULL, 16);
104     if (Address <= Op->Common.AmlOffset)
105     {
106         AcpiOsPrintf ("Breakpoint %X is beyond current address %X\n",
107             Address, Op->Common.AmlOffset);
108     }
109
110     /* Save breakpoint in current walk */
111
112     WalkState->UserBreakpoint = Address;
113     AcpiOsPrintf ("Breakpoint set at AML offset %X\n", Address);
114 }
115
116
117 /*******************************************************************************
118  *
119  * FUNCTION:    AcpiDbSetMethodCallBreakpoint
120  *
121  * PARAMETERS:  Op                  - Current Op (from parse walk)
122  *
123  * RETURN:      None
124  *
125  * DESCRIPTION: Set a breakpoint in a control method at the specified
126  *              AML offset
127  *
128  ******************************************************************************/
129
130 void
131 AcpiDbSetMethodCallBreakpoint (
132     ACPI_PARSE_OBJECT       *Op)
133 {
134
135
136     if (!Op)
137     {
138         AcpiOsPrintf ("There is no method currently executing\n");
139         return;
140     }
141
142     AcpiGbl_StepToNextCall = TRUE;
143 }
144
145
146 /*******************************************************************************
147  *
148  * FUNCTION:    AcpiDbSetMethodData
149  *
150  * PARAMETERS:  TypeArg         - L for local, A for argument
151  *              IndexArg        - which one
152  *              ValueArg        - Value to set.
153  *
154  * RETURN:      None
155  *
156  * DESCRIPTION: Set a local or argument for the running control method.
157  *              NOTE: only object supported is Number.
158  *
159  ******************************************************************************/
160
161 void
162 AcpiDbSetMethodData (
163     char                    *TypeArg,
164     char                    *IndexArg,
165     char                    *ValueArg)
166 {
167     char                    Type;
168     UINT32                  Index;
169     UINT32                  Value;
170     ACPI_WALK_STATE         *WalkState;
171     ACPI_OPERAND_OBJECT     *ObjDesc;
172     ACPI_STATUS             Status;
173     ACPI_NAMESPACE_NODE     *Node;
174
175
176     /* Validate TypeArg */
177
178     AcpiUtStrupr (TypeArg);
179     Type = TypeArg[0];
180     if ((Type != 'L') &&
181         (Type != 'A') &&
182         (Type != 'N'))
183     {
184         AcpiOsPrintf ("Invalid SET operand: %s\n", TypeArg);
185         return;
186     }
187
188     Value = ACPI_STRTOUL (ValueArg, NULL, 16);
189
190     if (Type == 'N')
191     {
192         Node = AcpiDbConvertToNode (IndexArg);
193         if (Node->Type != ACPI_TYPE_INTEGER)
194         {
195             AcpiOsPrintf ("Can only set Integer nodes\n");
196             return;
197         }
198         ObjDesc = Node->Object;
199         ObjDesc->Integer.Value = Value;
200         return;
201     }
202
203     /* Get the index and value */
204
205     Index = ACPI_STRTOUL (IndexArg, NULL, 16);
206
207     WalkState = AcpiDsGetCurrentWalkState (AcpiGbl_CurrentWalkList);
208     if (!WalkState)
209     {
210         AcpiOsPrintf ("There is no method currently executing\n");
211         return;
212     }
213
214     /* Create and initialize the new object */
215
216     ObjDesc = AcpiUtCreateIntegerObject ((UINT64) Value);
217     if (!ObjDesc)
218     {
219         AcpiOsPrintf ("Could not create an internal object\n");
220         return;
221     }
222
223     /* Store the new object into the target */
224
225     switch (Type)
226     {
227     case 'A':
228
229         /* Set a method argument */
230
231         if (Index > ACPI_METHOD_MAX_ARG)
232         {
233             AcpiOsPrintf ("Arg%u - Invalid argument name\n", Index);
234             goto Cleanup;
235         }
236
237         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_ARG, Index, ObjDesc,
238                     WalkState);
239         if (ACPI_FAILURE (Status))
240         {
241             goto Cleanup;
242         }
243
244         ObjDesc = WalkState->Arguments[Index].Object;
245
246         AcpiOsPrintf ("Arg%u: ", Index);
247         AcpiDmDisplayInternalObject (ObjDesc, WalkState);
248         break;
249
250     case 'L':
251
252         /* Set a method local */
253
254         if (Index > ACPI_METHOD_MAX_LOCAL)
255         {
256             AcpiOsPrintf ("Local%u - Invalid local variable name\n", Index);
257             goto Cleanup;
258         }
259
260         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_LOCAL, Index, ObjDesc,
261                     WalkState);
262         if (ACPI_FAILURE (Status))
263         {
264             goto Cleanup;
265         }
266
267         ObjDesc = WalkState->LocalVariables[Index].Object;
268
269         AcpiOsPrintf ("Local%u: ", Index);
270         AcpiDmDisplayInternalObject (ObjDesc, WalkState);
271         break;
272
273     default:
274         break;
275     }
276
277 Cleanup:
278     AcpiUtRemoveReference (ObjDesc);
279 }
280
281
282 /*******************************************************************************
283  *
284  * FUNCTION:    AcpiDbDisassembleAml
285  *
286  * PARAMETERS:  Statements          - Number of statements to disassemble
287  *              Op                  - Current Op (from parse walk)
288  *
289  * RETURN:      None
290  *
291  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
292  *              of statements specified.
293  *
294  ******************************************************************************/
295
296 void
297 AcpiDbDisassembleAml (
298     char                    *Statements,
299     ACPI_PARSE_OBJECT       *Op)
300 {
301     UINT32                  NumStatements = 8;
302
303
304     if (!Op)
305     {
306         AcpiOsPrintf ("There is no method currently executing\n");
307         return;
308     }
309
310     if (Statements)
311     {
312         NumStatements = ACPI_STRTOUL (Statements, NULL, 0);
313     }
314
315     AcpiDmDisassemble (NULL, Op, NumStatements);
316 }
317
318
319 /*******************************************************************************
320  *
321  * FUNCTION:    AcpiDbDisassembleMethod
322  *
323  * PARAMETERS:  Name            - Name of control method
324  *
325  * RETURN:      None
326  *
327  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
328  *              of statements specified.
329  *
330  ******************************************************************************/
331
332 ACPI_STATUS
333 AcpiDbDisassembleMethod (
334     char                    *Name)
335 {
336     ACPI_STATUS             Status;
337     ACPI_PARSE_OBJECT       *Op;
338     ACPI_WALK_STATE         *WalkState;
339     ACPI_OPERAND_OBJECT     *ObjDesc;
340     ACPI_NAMESPACE_NODE     *Method;
341
342
343     Method = AcpiDbConvertToNode (Name);
344     if (!Method)
345     {
346         return (AE_BAD_PARAMETER);
347     }
348
349     if (Method->Type != ACPI_TYPE_METHOD)
350     {
351         ACPI_ERROR ((AE_INFO, "%s (%s): Object must be a control method",
352             Name, AcpiUtGetTypeName (Method->Type)));
353         return (AE_BAD_PARAMETER);
354     }
355
356     ObjDesc = Method->Object;
357
358     Op = AcpiPsCreateScopeOp ();
359     if (!Op)
360     {
361         return (AE_NO_MEMORY);
362     }
363
364     /* Create and initialize a new walk state */
365
366     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
367     if (!WalkState)
368     {
369         return (AE_NO_MEMORY);
370     }
371
372     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL,
373         ObjDesc->Method.AmlStart,
374         ObjDesc->Method.AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
375     if (ACPI_FAILURE (Status))
376     {
377         return (Status);
378     }
379
380     Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
381     WalkState->OwnerId = ObjDesc->Method.OwnerId;
382
383     /* Push start scope on scope stack and make it current */
384
385     Status = AcpiDsScopeStackPush (Method,
386         Method->Type, WalkState);
387     if (ACPI_FAILURE (Status))
388     {
389         return (Status);
390     }
391
392     /* Parse the entire method AML including deferred operators */
393
394     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
395     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
396
397     Status = AcpiPsParseAml (WalkState);
398     (void) AcpiDmParseDeferredOps (Op);
399
400     /* Now we can disassemble the method */
401
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     char                        *Pathname;
440     const ACPI_PREDEFINED_INFO  *Predefined;
441     ACPI_DEVICE_INFO            *ObjInfo;
442     ACPI_OBJECT_LIST            ParamObjects;
443     ACPI_OBJECT                 Params[ACPI_METHOD_NUM_ARGS];
444     ACPI_OBJECT                 *ThisParam;
445     ACPI_BUFFER                 ReturnObj;
446     ACPI_STATUS                 Status;
447     UINT16                      ArgTypeList;
448     UINT8                       ArgCount;
449     UINT8                       ArgType;
450     UINT32                      i;
451
452
453     /* The name must be a predefined ACPI name */
454
455     Predefined = AcpiUtMatchPredefinedMethod (Node->Name.Ascii);
456     if (!Predefined)
457     {
458         return (AE_OK);
459     }
460
461     if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
462     {
463         return (AE_OK);
464     }
465
466     Pathname = AcpiNsGetExternalPathname (Node);
467     if (!Pathname)
468     {
469         return (AE_OK);
470     }
471
472     /* Get the object info for number of method parameters */
473
474     Status = AcpiGetObjectInfo (ObjHandle, &ObjInfo);
475     if (ACPI_FAILURE (Status))
476     {
477         return (Status);
478     }
479
480     ParamObjects.Count = 0;
481     ParamObjects.Pointer = NULL;
482
483     if (ObjInfo->Type == ACPI_TYPE_METHOD)
484     {
485         /* Setup default parameters (with proper types) */
486
487         ArgTypeList = Predefined->Info.ArgumentList;
488         ArgCount = METHOD_GET_ARG_COUNT (ArgTypeList);
489
490         /*
491          * Setup the ACPI-required number of arguments, regardless of what
492          * the actual method defines. If there is a difference, then the
493          * method is wrong and a warning will be issued during execution.
494          */
495         ThisParam = Params;
496         for (i = 0; i < ArgCount; i++)
497         {
498             ArgType = METHOD_GET_NEXT_TYPE (ArgTypeList);
499             ThisParam->Type = ArgType;
500
501             switch (ArgType)
502             {
503             case ACPI_TYPE_INTEGER:
504                 ThisParam->Integer.Value = 1;
505                 break;
506
507             case ACPI_TYPE_STRING:
508                 ThisParam->String.Pointer = "This is the default argument string";
509                 ThisParam->String.Length = ACPI_STRLEN (ThisParam->String.Pointer);
510                 break;
511
512             case ACPI_TYPE_BUFFER:
513                 ThisParam->Buffer.Pointer = (UINT8 *) Params; /* just a garbage buffer */
514                 ThisParam->Buffer.Length = 48;
515                 break;
516
517              case ACPI_TYPE_PACKAGE:
518                 ThisParam->Package.Elements = NULL;
519                 ThisParam->Package.Count = 0;
520                 break;
521
522            default:
523                 AcpiOsPrintf ("%s: Unsupported argument type: %u\n",
524                     Pathname, ArgType);
525                 break;
526             }
527
528             ThisParam++;
529         }
530
531         ParamObjects.Count = ArgCount;
532         ParamObjects.Pointer = Params;
533     }
534
535     ACPI_FREE (ObjInfo);
536     ReturnObj.Pointer = NULL;
537     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
538
539     /* Do the actual method execution */
540
541     AcpiGbl_MethodExecuting = TRUE;
542
543     Status = AcpiEvaluateObject (Node, NULL, &ParamObjects, &ReturnObj);
544
545     AcpiOsPrintf ("%-32s returned %s\n", Pathname, AcpiFormatException (Status));
546     AcpiGbl_MethodExecuting = FALSE;
547     ACPI_FREE (Pathname);
548
549     /* Ignore status from method execution */
550
551     Status = AE_OK;
552
553     /* Update count, check if we have executed enough methods */
554
555     Info->Count++;
556     if (Info->Count >= Info->MaxCount)
557     {
558         Status = AE_CTRL_TERMINATE;
559     }
560
561     return (Status);
562 }
563
564
565 /*******************************************************************************
566  *
567  * FUNCTION:    AcpiDbBatchExecute
568  *
569  * PARAMETERS:  CountArg            - Max number of methods to execute
570  *
571  * RETURN:      None
572  *
573  * DESCRIPTION: Namespace batch execution. Execute predefined names in the
574  *              namespace, up to the max count, if specified.
575  *
576  ******************************************************************************/
577
578 void
579 AcpiDbBatchExecute (
580     char                    *CountArg)
581 {
582     ACPI_DB_EXECUTE_WALK    Info;
583
584
585     Info.Count = 0;
586     Info.MaxCount = ACPI_UINT32_MAX;
587
588     if (CountArg)
589     {
590         Info.MaxCount = ACPI_STRTOUL (CountArg, NULL, 0);
591     }
592
593
594     /* Search all nodes in namespace */
595
596     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
597                 AcpiDbWalkForExecute, NULL, (void *) &Info, NULL);
598
599     AcpiOsPrintf ("Evaluated %u predefined names in the namespace\n", Info.Count);
600 }
601
602 #endif /* ACPI_DEBUGGER */