]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/debugger/dbxface.c
Update to a 1-May-2011 release (except for the isblank change).
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / debugger / dbxface.c
1 /*******************************************************************************
2  *
3  * Module Name: dbxface - AML Debugger external interfaces
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2011, 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 <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/amlcode.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
49 #include <contrib/dev/acpica/include/acdisasm.h>
50
51
52 #ifdef ACPI_DEBUGGER
53
54 #define _COMPONENT          ACPI_CA_DEBUGGER
55         ACPI_MODULE_NAME    ("dbxface")
56
57
58 /* Local prototypes */
59
60 static ACPI_STATUS
61 AcpiDbStartCommand (
62     ACPI_WALK_STATE         *WalkState,
63     ACPI_PARSE_OBJECT       *Op);
64
65 #ifdef ACPI_OBSOLETE_FUNCTIONS
66 void
67 AcpiDbMethodEnd (
68     ACPI_WALK_STATE         *WalkState);
69 #endif
70
71
72 /*******************************************************************************
73  *
74  * FUNCTION:    AcpiDbStartCommand
75  *
76  * PARAMETERS:  WalkState       - Current walk
77  *              Op              - Current executing Op, from AML interpreter
78  *
79  * RETURN:      Status
80  *
81  * DESCRIPTION: Enter debugger command loop
82  *
83  ******************************************************************************/
84
85 static ACPI_STATUS
86 AcpiDbStartCommand (
87     ACPI_WALK_STATE         *WalkState,
88     ACPI_PARSE_OBJECT       *Op)
89 {
90     ACPI_STATUS             Status;
91
92
93     /* TBD: [Investigate] are there namespace locking issues here? */
94
95     /* AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE); */
96
97     /* Go into the command loop and await next user command */
98
99
100     AcpiGbl_MethodExecuting = TRUE;
101     Status = AE_CTRL_TRUE;
102     while (Status == AE_CTRL_TRUE)
103     {
104         if (AcpiGbl_DebuggerConfiguration == DEBUGGER_MULTI_THREADED)
105         {
106             /* Handshake with the front-end that gets user command lines */
107
108             Status = AcpiUtReleaseMutex (ACPI_MTX_DEBUG_CMD_COMPLETE);
109             if (ACPI_FAILURE (Status))
110             {
111                 return (Status);
112             }
113             Status = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_READY);
114             if (ACPI_FAILURE (Status))
115             {
116                 return (Status);
117             }
118         }
119         else
120         {
121             /* Single threaded, we must get a command line ourselves */
122
123             /* Force output to console until a command is entered */
124
125             AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
126
127             /* Different prompt if method is executing */
128
129             if (!AcpiGbl_MethodExecuting)
130             {
131                 AcpiOsPrintf ("%1c ", ACPI_DEBUGGER_COMMAND_PROMPT);
132             }
133             else
134             {
135                 AcpiOsPrintf ("%1c ", ACPI_DEBUGGER_EXECUTE_PROMPT);
136             }
137
138             /* Get the user input line */
139
140             (void) AcpiOsGetLine (AcpiGbl_DbLineBuf);
141         }
142
143         Status = AcpiDbCommandDispatch (AcpiGbl_DbLineBuf, WalkState, Op);
144     }
145
146     /* AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE); */
147
148     return (Status);
149 }
150
151
152 /*******************************************************************************
153  *
154  * FUNCTION:    AcpiDbSingleStep
155  *
156  * PARAMETERS:  WalkState       - Current walk
157  *              Op              - Current executing op (from aml interpreter)
158  *              OpcodeClass     - Class of the current AML Opcode
159  *
160  * RETURN:      Status
161  *
162  * DESCRIPTION: Called just before execution of an AML opcode.
163  *
164  ******************************************************************************/
165
166 ACPI_STATUS
167 AcpiDbSingleStep (
168     ACPI_WALK_STATE         *WalkState,
169     ACPI_PARSE_OBJECT       *Op,
170     UINT32                  OpcodeClass)
171 {
172     ACPI_PARSE_OBJECT       *Next;
173     ACPI_STATUS             Status = AE_OK;
174     UINT32                  OriginalDebugLevel;
175     ACPI_PARSE_OBJECT       *DisplayOp;
176     ACPI_PARSE_OBJECT       *ParentOp;
177
178
179     ACPI_FUNCTION_ENTRY ();
180
181
182     /* Check the abort flag */
183
184     if (AcpiGbl_AbortMethod)
185     {
186         AcpiGbl_AbortMethod = FALSE;
187         return (AE_ABORT_METHOD);
188     }
189
190     /* Check for single-step breakpoint */
191
192     if (WalkState->MethodBreakpoint &&
193        (WalkState->MethodBreakpoint <= Op->Common.AmlOffset))
194     {
195         /* Check if the breakpoint has been reached or passed */
196         /* Hit the breakpoint, resume single step, reset breakpoint */
197
198         AcpiOsPrintf ("***Break*** at AML offset %X\n", Op->Common.AmlOffset);
199         AcpiGbl_CmSingleStep = TRUE;
200         AcpiGbl_StepToNextCall = FALSE;
201         WalkState->MethodBreakpoint = 0;
202     }
203
204     /* Check for user breakpoint (Must be on exact Aml offset) */
205
206     else if (WalkState->UserBreakpoint &&
207             (WalkState->UserBreakpoint == Op->Common.AmlOffset))
208     {
209         AcpiOsPrintf ("***UserBreakpoint*** at AML offset %X\n",
210             Op->Common.AmlOffset);
211         AcpiGbl_CmSingleStep = TRUE;
212         AcpiGbl_StepToNextCall = FALSE;
213         WalkState->MethodBreakpoint = 0;
214     }
215
216     /*
217      * Check if this is an opcode that we are interested in --
218      * namely, opcodes that have arguments
219      */
220     if (Op->Common.AmlOpcode == AML_INT_NAMEDFIELD_OP)
221     {
222         return (AE_OK);
223     }
224
225     switch (OpcodeClass)
226     {
227     case AML_CLASS_UNKNOWN:
228     case AML_CLASS_ARGUMENT:    /* constants, literals, etc.  do nothing */
229         return (AE_OK);
230
231     default:
232         /* All other opcodes -- continue */
233         break;
234     }
235
236     /*
237      * Under certain debug conditions, display this opcode and its operands
238      */
239     if ((AcpiGbl_DbOutputToFile)            ||
240         (AcpiGbl_CmSingleStep)              ||
241         (AcpiDbgLevel & ACPI_LV_PARSE))
242     {
243         if ((AcpiGbl_DbOutputToFile)        ||
244             (AcpiDbgLevel & ACPI_LV_PARSE))
245         {
246             AcpiOsPrintf ("\n[AmlDebug] Next AML Opcode to execute:\n");
247         }
248
249         /*
250          * Display this op (and only this op - zero out the NEXT field
251          * temporarily, and disable parser trace output for the duration of
252          * the display because we don't want the extraneous debug output)
253          */
254         OriginalDebugLevel = AcpiDbgLevel;
255         AcpiDbgLevel &= ~(ACPI_LV_PARSE | ACPI_LV_FUNCTIONS);
256         Next = Op->Common.Next;
257         Op->Common.Next = NULL;
258
259
260         DisplayOp = Op;
261         ParentOp = Op->Common.Parent;
262         if (ParentOp)
263         {
264             if ((WalkState->ControlState) &&
265                 (WalkState->ControlState->Common.State ==
266                     ACPI_CONTROL_PREDICATE_EXECUTING))
267             {
268                 /*
269                  * We are executing the predicate of an IF or WHILE statement
270                  * Search upwards for the containing IF or WHILE so that the
271                  * entire predicate can be displayed.
272                  */
273                 while (ParentOp)
274                 {
275                     if ((ParentOp->Common.AmlOpcode == AML_IF_OP) ||
276                         (ParentOp->Common.AmlOpcode == AML_WHILE_OP))
277                     {
278                         DisplayOp = ParentOp;
279                         break;
280                     }
281                     ParentOp = ParentOp->Common.Parent;
282                 }
283             }
284             else
285             {
286                 while (ParentOp)
287                 {
288                     if ((ParentOp->Common.AmlOpcode == AML_IF_OP)     ||
289                         (ParentOp->Common.AmlOpcode == AML_ELSE_OP)   ||
290                         (ParentOp->Common.AmlOpcode == AML_SCOPE_OP)  ||
291                         (ParentOp->Common.AmlOpcode == AML_METHOD_OP) ||
292                         (ParentOp->Common.AmlOpcode == AML_WHILE_OP))
293                     {
294                         break;
295                     }
296                     DisplayOp = ParentOp;
297                     ParentOp = ParentOp->Common.Parent;
298                 }
299             }
300         }
301
302         /* Now we can display it */
303
304 #ifdef ACPI_DISASSEMBLER
305         AcpiDmDisassemble (WalkState, DisplayOp, ACPI_UINT32_MAX);
306 #endif
307
308         if ((Op->Common.AmlOpcode == AML_IF_OP) ||
309             (Op->Common.AmlOpcode == AML_WHILE_OP))
310         {
311             if (WalkState->ControlState->Common.Value)
312             {
313                 AcpiOsPrintf ("Predicate = [True], IF block was executed\n");
314             }
315             else
316             {
317                 AcpiOsPrintf ("Predicate = [False], Skipping IF block\n");
318             }
319         }
320         else if (Op->Common.AmlOpcode == AML_ELSE_OP)
321         {
322             AcpiOsPrintf ("Predicate = [False], ELSE block was executed\n");
323         }
324
325         /* Restore everything */
326
327         Op->Common.Next = Next;
328         AcpiOsPrintf ("\n");
329         if ((AcpiGbl_DbOutputToFile)        ||
330             (AcpiDbgLevel & ACPI_LV_PARSE))
331         {
332             AcpiOsPrintf ("\n");
333         }
334         AcpiDbgLevel = OriginalDebugLevel;
335     }
336
337     /* If we are not single stepping, just continue executing the method */
338
339     if (!AcpiGbl_CmSingleStep)
340     {
341         return (AE_OK);
342     }
343
344     /*
345      * If we are executing a step-to-call command,
346      * Check if this is a method call.
347      */
348     if (AcpiGbl_StepToNextCall)
349     {
350         if (Op->Common.AmlOpcode != AML_INT_METHODCALL_OP)
351         {
352             /* Not a method call, just keep executing */
353
354             return (AE_OK);
355         }
356
357         /* Found a method call, stop executing */
358
359         AcpiGbl_StepToNextCall = FALSE;
360     }
361
362     /*
363      * If the next opcode is a method call, we will "step over" it
364      * by default.
365      */
366     if (Op->Common.AmlOpcode == AML_INT_METHODCALL_OP)
367     {
368         /* Force no more single stepping while executing called method */
369
370         AcpiGbl_CmSingleStep = FALSE;
371
372         /*
373          * Set the breakpoint on/before the call, it will stop execution
374          * as soon as we return
375          */
376         WalkState->MethodBreakpoint = 1;  /* Must be non-zero! */
377     }
378
379
380     Status = AcpiDbStartCommand (WalkState, Op);
381
382     /* User commands complete, continue execution of the interrupted method */
383
384     return (Status);
385 }
386
387
388 /*******************************************************************************
389  *
390  * FUNCTION:    AcpiDbInitialize
391  *
392  * PARAMETERS:  None
393  *
394  * RETURN:      Status
395  *
396  * DESCRIPTION: Init and start debugger
397  *
398  ******************************************************************************/
399
400 ACPI_STATUS
401 AcpiDbInitialize (
402     void)
403 {
404     ACPI_STATUS             Status;
405
406
407     /* Init globals */
408
409     AcpiGbl_DbBuffer            = NULL;
410     AcpiGbl_DbFilename          = NULL;
411     AcpiGbl_DbOutputToFile      = FALSE;
412
413     AcpiGbl_DbDebugLevel        = ACPI_LV_VERBOSITY2;
414     AcpiGbl_DbConsoleDebugLevel = ACPI_NORMAL_DEFAULT | ACPI_LV_TABLES;
415     AcpiGbl_DbOutputFlags       = ACPI_DB_CONSOLE_OUTPUT;
416
417     AcpiGbl_DbOpt_tables        = FALSE;
418     AcpiGbl_DbOpt_stats         = FALSE;
419 #ifdef ACPI_DISASSEMBLER
420     AcpiGbl_DbOpt_disasm        = FALSE;
421     AcpiGbl_DbOpt_verbose       = TRUE;
422 #endif
423     AcpiGbl_DbOpt_ini_methods   = TRUE;
424
425     AcpiGbl_DbBuffer = AcpiOsAllocate (ACPI_DEBUG_BUFFER_SIZE);
426     if (!AcpiGbl_DbBuffer)
427     {
428         return (AE_NO_MEMORY);
429     }
430     ACPI_MEMSET (AcpiGbl_DbBuffer, 0, ACPI_DEBUG_BUFFER_SIZE);
431
432     /* Initial scope is the root */
433
434     AcpiGbl_DbScopeBuf [0] = '\\';
435     AcpiGbl_DbScopeBuf [1] =  0;
436     AcpiGbl_DbScopeNode = AcpiGbl_RootNode;
437
438     /*
439      * If configured for multi-thread support, the debug executor runs in
440      * a separate thread so that the front end can be in another address
441      * space, environment, or even another machine.
442      */
443     if (AcpiGbl_DebuggerConfiguration & DEBUGGER_MULTI_THREADED)
444     {
445         /* These were created with one unit, grab it */
446
447         Status = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_COMPLETE);
448         if (ACPI_FAILURE (Status))
449         {
450             AcpiOsPrintf ("Could not get debugger mutex\n");
451             return (Status);
452         }
453
454         Status = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_READY);
455         if (ACPI_FAILURE (Status))
456         {
457             AcpiOsPrintf ("Could not get debugger mutex\n");
458             return (Status);
459         }
460
461         /* Create the debug execution thread to execute commands */
462
463         Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbExecuteThread, NULL);
464         if (ACPI_FAILURE (Status))
465         {
466             AcpiOsPrintf ("Could not start debugger thread\n");
467             return (Status);
468         }
469     }
470
471 #ifdef ACPI_DISASSEMBLER
472     if (!AcpiGbl_DbOpt_verbose)
473     {
474         AcpiGbl_DbOpt_disasm = TRUE;
475         AcpiGbl_DbOpt_stats = FALSE;
476     }
477 #endif
478
479     return (AE_OK);
480 }
481
482
483 /*******************************************************************************
484  *
485  * FUNCTION:    AcpiDbTerminate
486  *
487  * PARAMETERS:  None
488  *
489  * RETURN:      None
490  *
491  * DESCRIPTION: Stop debugger
492  *
493  ******************************************************************************/
494
495 void
496 AcpiDbTerminate (
497     void)
498 {
499
500     if (AcpiGbl_DbBuffer)
501     {
502         AcpiOsFree (AcpiGbl_DbBuffer);
503     }
504 }
505
506
507 #ifdef ACPI_OBSOLETE_FUNCTIONS
508 /*******************************************************************************
509  *
510  * FUNCTION:    AcpiDbMethodEnd
511  *
512  * PARAMETERS:  WalkState       - Current walk
513  *
514  * RETURN:      Status
515  *
516  * DESCRIPTION: Called at method termination
517  *
518  ******************************************************************************/
519
520 void
521 AcpiDbMethodEnd (
522     ACPI_WALK_STATE         *WalkState)
523 {
524
525     if (!AcpiGbl_CmSingleStep)
526     {
527         return;
528     }
529
530     AcpiOsPrintf ("<Method Terminating>\n");
531
532     AcpiDbStartCommand (WalkState, NULL);
533 }
534 #endif
535
536 #endif /* ACPI_DEBUGGER */