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 "acpi.h"
  46 #include "accommon.h"
  47 #include "amlcode.h"
  48 #include "acdebug.h"
  49 #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             Status = AcpiOsGetLine (AcpiGbl_DbLineBuf,
 141                 ACPI_DB_LINE_BUFFER_SIZE, NULL);
 142             if (ACPI_FAILURE (Status))
 143             {
 144                 ACPI_EXCEPTION ((AE_INFO, Status, "While parsing command line"));
 145                 return (Status);
 146             }
 147         }
 148 
 149         Status = AcpiDbCommandDispatch (AcpiGbl_DbLineBuf, WalkState, Op);
 150     }
 151 
 152     /* AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE); */
 153 
 154     return (Status);
 155 }
 156 
 157 
 158 /*******************************************************************************
 159  *
 160  * FUNCTION:    AcpiDbSingleStep
 161  *
 162  * PARAMETERS:  WalkState       - Current walk
 163  *              Op              - Current executing op (from aml interpreter)
 164  *              OpcodeClass     - Class of the current AML Opcode
 165  *
 166  * RETURN:      Status
 167  *
 168  * DESCRIPTION: Called just before execution of an AML opcode.
 169  *
 170  ******************************************************************************/
 171 
 172 ACPI_STATUS
 173 AcpiDbSingleStep (
 174     ACPI_WALK_STATE         *WalkState,
 175     ACPI_PARSE_OBJECT       *Op,
 176     UINT32                  OpcodeClass)
 177 {
 178     ACPI_PARSE_OBJECT       *Next;
 179     ACPI_STATUS             Status = AE_OK;
 180     UINT32                  OriginalDebugLevel;
 181     ACPI_PARSE_OBJECT       *DisplayOp;
 182     ACPI_PARSE_OBJECT       *ParentOp;
 183 
 184 
 185     ACPI_FUNCTION_ENTRY ();
 186 
 187 
 188     /* Check the abort flag */
 189 
 190     if (AcpiGbl_AbortMethod)
 191     {
 192         AcpiGbl_AbortMethod = FALSE;
 193         return (AE_ABORT_METHOD);
 194     }
 195 
 196     /* Check for single-step breakpoint */
 197 
 198     if (WalkState->MethodBreakpoint &&
 199        (WalkState->MethodBreakpoint <= Op->Common.AmlOffset))
 200     {
 201         /* Check if the breakpoint has been reached or passed */
 202         /* Hit the breakpoint, resume single step, reset breakpoint */
 203 
 204         AcpiOsPrintf ("***Break*** at AML offset %X\n", Op->Common.AmlOffset);
 205         AcpiGbl_CmSingleStep = TRUE;
 206         AcpiGbl_StepToNextCall = FALSE;
 207         WalkState->MethodBreakpoint = 0;
 208     }
 209 
 210     /* Check for user breakpoint (Must be on exact Aml offset) */
 211 
 212     else if (WalkState->UserBreakpoint &&
 213             (WalkState->UserBreakpoint == Op->Common.AmlOffset))
 214     {
 215         AcpiOsPrintf ("***UserBreakpoint*** at AML offset %X\n",
 216             Op->Common.AmlOffset);
 217         AcpiGbl_CmSingleStep = TRUE;
 218         AcpiGbl_StepToNextCall = FALSE;
 219         WalkState->MethodBreakpoint = 0;
 220     }
 221 
 222     /*
 223      * Check if this is an opcode that we are interested in --
 224      * namely, opcodes that have arguments
 225      */
 226     if (Op->Common.AmlOpcode == AML_INT_NAMEDFIELD_OP)
 227     {
 228         return (AE_OK);
 229     }
 230 
 231     switch (OpcodeClass)
 232     {
 233     case AML_CLASS_UNKNOWN:
 234     case AML_CLASS_ARGUMENT:    /* constants, literals, etc.  do nothing */
 235         return (AE_OK);
 236 
 237     default:
 238         /* All other opcodes -- continue */
 239         break;
 240     }
 241 
 242     /*
 243      * Under certain debug conditions, display this opcode and its operands
 244      */
 245     if ((AcpiGbl_DbOutputToFile)            ||
 246         (AcpiGbl_CmSingleStep)              ||
 247         (AcpiDbgLevel & ACPI_LV_PARSE))
 248     {
 249         if ((AcpiGbl_DbOutputToFile)        ||
 250             (AcpiDbgLevel & ACPI_LV_PARSE))
 251         {
 252             AcpiOsPrintf ("\n[AmlDebug] Next AML Opcode to execute:\n");
 253         }
 254 
 255         /*
 256          * Display this op (and only this op - zero out the NEXT field
 257          * temporarily, and disable parser trace output for the duration of
 258          * the display because we don't want the extraneous debug output)
 259          */
 260         OriginalDebugLevel = AcpiDbgLevel;
 261         AcpiDbgLevel &= ~(ACPI_LV_PARSE | ACPI_LV_FUNCTIONS);
 262         Next = Op->Common.Next;
 263         Op->Common.Next = NULL;
 264 
 265 
 266         DisplayOp = Op;
 267         ParentOp = Op->Common.Parent;
 268         if (ParentOp)
 269         {
 270             if ((WalkState->ControlState) &&
 271                 (WalkState->ControlState->Common.State ==
 272                     ACPI_CONTROL_PREDICATE_EXECUTING))
 273             {
 274                 /*
 275                  * We are executing the predicate of an IF or WHILE statement
 276                  * Search upwards for the containing IF or WHILE so that the
 277                  * entire predicate can be displayed.
 278                  */
 279                 while (ParentOp)
 280                 {
 281                     if ((ParentOp->Common.AmlOpcode == AML_IF_OP) ||
 282                         (ParentOp->Common.AmlOpcode == AML_WHILE_OP))
 283                     {
 284                         DisplayOp = ParentOp;
 285                         break;
 286                     }
 287                     ParentOp = ParentOp->Common.Parent;
 288                 }
 289             }
 290             else
 291             {
 292                 while (ParentOp)
 293                 {
 294                     if ((ParentOp->Common.AmlOpcode == AML_IF_OP)     ||
 295                         (ParentOp->Common.AmlOpcode == AML_ELSE_OP)   ||
 296                         (ParentOp->Common.AmlOpcode == AML_SCOPE_OP)  ||
 297                         (ParentOp->Common.AmlOpcode == AML_METHOD_OP) ||
 298                         (ParentOp->Common.AmlOpcode == AML_WHILE_OP))
 299                     {
 300                         break;
 301                     }
 302                     DisplayOp = ParentOp;
 303                     ParentOp = ParentOp->Common.Parent;
 304                 }
 305             }
 306         }
 307 
 308         /* Now we can display it */
 309 
 310         AcpiDmDisassemble (WalkState, DisplayOp, ACPI_UINT32_MAX);
 311 
 312         if ((Op->Common.AmlOpcode == AML_IF_OP) ||
 313             (Op->Common.AmlOpcode == AML_WHILE_OP))
 314         {
 315             if (WalkState->ControlState->Common.Value)
 316             {
 317                 AcpiOsPrintf ("Predicate = [True], IF block was executed\n");
 318             }
 319             else
 320             {
 321                 AcpiOsPrintf ("Predicate = [False], Skipping IF block\n");
 322             }
 323         }
 324         else if (Op->Common.AmlOpcode == AML_ELSE_OP)
 325         {
 326             AcpiOsPrintf ("Predicate = [False], ELSE block was executed\n");
 327         }
 328 
 329         /* Restore everything */
 330 
 331         Op->Common.Next = Next;
 332         AcpiOsPrintf ("\n");
 333         if ((AcpiGbl_DbOutputToFile)        ||
 334             (AcpiDbgLevel & ACPI_LV_PARSE))
 335         {
 336             AcpiOsPrintf ("\n");
 337         }
 338         AcpiDbgLevel = OriginalDebugLevel;
 339     }
 340 
 341     /* If we are not single stepping, just continue executing the method */
 342 
 343     if (!AcpiGbl_CmSingleStep)
 344     {
 345         return (AE_OK);
 346     }
 347 
 348     /*
 349      * If we are executing a step-to-call command,
 350      * Check if this is a method call.
 351      */
 352     if (AcpiGbl_StepToNextCall)
 353     {
 354         if (Op->Common.AmlOpcode != AML_INT_METHODCALL_OP)
 355         {
 356             /* Not a method call, just keep executing */
 357 
 358             return (AE_OK);
 359         }
 360 
 361         /* Found a method call, stop executing */
 362 
 363         AcpiGbl_StepToNextCall = FALSE;
 364     }
 365 
 366     /*
 367      * If the next opcode is a method call, we will "step over" it
 368      * by default.
 369      */
 370     if (Op->Common.AmlOpcode == AML_INT_METHODCALL_OP)
 371     {
 372         /* Force no more single stepping while executing called method */
 373 
 374         AcpiGbl_CmSingleStep = FALSE;
 375 
 376         /*
 377          * Set the breakpoint on/before the call, it will stop execution
 378          * as soon as we return
 379          */
 380         WalkState->MethodBreakpoint = 1;  /* Must be non-zero! */
 381     }
 382 
 383 
 384     Status = AcpiDbStartCommand (WalkState, Op);
 385 
 386     /* User commands complete, continue execution of the interrupted method */
 387 
 388     return (Status);
 389 }
 390 
 391 
 392 /*******************************************************************************
 393  *
 394  * FUNCTION:    AcpiDbInitialize
 395  *
 396  * PARAMETERS:  None
 397  *
 398  * RETURN:      Status
 399  *
 400  * DESCRIPTION: Init and start debugger
 401  *
 402  ******************************************************************************/
 403 
 404 ACPI_STATUS
 405 AcpiDbInitialize (
 406     void)
 407 {
 408     ACPI_STATUS             Status;
 409 
 410 
 411     /* Init globals */
 412 
 413     AcpiGbl_DbBuffer            = NULL;
 414     AcpiGbl_DbFilename          = NULL;
 415     AcpiGbl_DbOutputToFile      = FALSE;
 416 
 417     AcpiGbl_DbDebugLevel        = ACPI_LV_VERBOSITY2;
 418     AcpiGbl_DbConsoleDebugLevel = ACPI_NORMAL_DEFAULT | ACPI_LV_TABLES;
 419     AcpiGbl_DbOutputFlags       = ACPI_DB_CONSOLE_OUTPUT;
 420 
 421     AcpiGbl_DbOpt_tables        = FALSE;
 422     AcpiGbl_DbOpt_disasm        = FALSE;
 423     AcpiGbl_DbOpt_stats         = FALSE;
 424     AcpiGbl_DbOpt_verbose       = TRUE;
 425     AcpiGbl_DbOpt_ini_methods   = TRUE;
 426 
 427     AcpiGbl_DbBuffer = AcpiOsAllocate (ACPI_DEBUG_BUFFER_SIZE);
 428     if (!AcpiGbl_DbBuffer)
 429     {
 430         return (AE_NO_MEMORY);
 431     }
 432     ACPI_MEMSET (AcpiGbl_DbBuffer, 0, ACPI_DEBUG_BUFFER_SIZE);
 433 
 434     /* Initial scope is the root */
 435 
 436     AcpiGbl_DbScopeBuf [0] = '\\';
 437     AcpiGbl_DbScopeBuf [1] =  0;
 438     AcpiGbl_DbScopeNode = AcpiGbl_RootNode;
 439 
 440     /*
 441      * If configured for multi-thread support, the debug executor runs in
 442      * a separate thread so that the front end can be in another address
 443      * space, environment, or even another machine.
 444      */
 445     if (AcpiGbl_DebuggerConfiguration & DEBUGGER_MULTI_THREADED)
 446     {
 447         /* These were created with one unit, grab it */
 448 
 449         Status = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_COMPLETE);
 450         if (ACPI_FAILURE (Status))
 451         {
 452             AcpiOsPrintf ("Could not get debugger mutex\n");
 453             return (Status);
 454         }
 455 
 456         Status = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_READY);
 457         if (ACPI_FAILURE (Status))
 458         {
 459             AcpiOsPrintf ("Could not get debugger mutex\n");
 460             return (Status);
 461         }
 462 
 463         /* Create the debug execution thread to execute commands */
 464 
 465         Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbExecuteThread, NULL);
 466         if (ACPI_FAILURE (Status))
 467         {
 468             AcpiOsPrintf ("Could not start debugger thread\n");
 469             return (Status);
 470         }
 471     }
 472 
 473     if (!AcpiGbl_DbOpt_verbose)
 474     {
 475         AcpiGbl_DbOpt_disasm = TRUE;
 476         AcpiGbl_DbOpt_stats = FALSE;
 477     }
 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 */