1 /*******************************************************************************
   2  *
   3  * Module Name: dbinput - user front-end to the AML debugger
   4  *
   5  ******************************************************************************/
   6 
   7 /*
   8  * Copyright (C) 2000 - 2014, 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 "acdebug.h"
  48 
  49 
  50 #ifdef ACPI_DEBUGGER
  51 
  52 #define _COMPONENT          ACPI_CA_DEBUGGER
  53         ACPI_MODULE_NAME    ("dbinput")
  54 
  55 /* Local prototypes */
  56 
  57 static UINT32
  58 AcpiDbGetLine (
  59     char                    *InputBuffer);
  60 
  61 static UINT32
  62 AcpiDbMatchCommand (
  63     char                    *UserCommand);
  64 
  65 static void
  66 AcpiDbSingleThread (
  67     void);
  68 
  69 static void
  70 AcpiDbDisplayCommandInfo (
  71     char                    *Command,
  72     BOOLEAN                 DisplayAll);
  73 
  74 static void
  75 AcpiDbDisplayHelp (
  76     char                    *Command);
  77 
  78 static BOOLEAN
  79 AcpiDbMatchCommandHelp (
  80     char                        *Command,
  81     const ACPI_DB_COMMAND_HELP  *Help);
  82 
  83 
  84 /*
  85  * Top-level debugger commands.
  86  *
  87  * This list of commands must match the string table below it
  88  */
  89 enum AcpiExDebuggerCommands
  90 {
  91     CMD_NOT_FOUND = 0,
  92     CMD_NULL,
  93     CMD_ALLOCATIONS,
  94     CMD_ARGS,
  95     CMD_ARGUMENTS,
  96     CMD_BREAKPOINT,
  97     CMD_BUSINFO,
  98     CMD_CALL,
  99     CMD_CLOSE,
 100     CMD_DEBUG,
 101     CMD_DISASSEMBLE,
 102     CMD_DISASM,
 103     CMD_DUMP,
 104     CMD_ENABLEACPI,
 105     CMD_EVALUATE,
 106     CMD_EVENT,
 107     CMD_EXECUTE,
 108     CMD_EXIT,
 109     CMD_FIND,
 110     CMD_GO,
 111     CMD_GPE,
 112     CMD_GPES,
 113     CMD_HANDLERS,
 114     CMD_HELP,
 115     CMD_HELP2,
 116     CMD_HISTORY,
 117     CMD_HISTORY_EXE,
 118     CMD_HISTORY_LAST,
 119     CMD_INFORMATION,
 120     CMD_INTEGRITY,
 121     CMD_INTO,
 122     CMD_LEVEL,
 123     CMD_LIST,
 124     CMD_LOAD,
 125     CMD_LOCALS,
 126     CMD_LOCKS,
 127     CMD_METHODS,
 128     CMD_NAMESPACE,
 129     CMD_NOTIFY,
 130     CMD_OBJECTS,
 131     CMD_OPEN,
 132     CMD_OSI,
 133     CMD_OWNER,
 134     CMD_PATHS,
 135     CMD_PREDEFINED,
 136     CMD_PREFIX,
 137     CMD_QUIT,
 138     CMD_REFERENCES,
 139     CMD_RESOURCES,
 140     CMD_RESULTS,
 141     CMD_SCI,
 142     CMD_SET,
 143     CMD_SLEEP,
 144     CMD_STATS,
 145     CMD_STOP,
 146     CMD_TABLES,
 147     CMD_TEMPLATE,
 148     CMD_TERMINATE,
 149     CMD_TEST,
 150     CMD_THREADS,
 151     CMD_TRACE,
 152     CMD_TREE,
 153     CMD_TYPE,
 154     CMD_UNLOAD
 155 };
 156 
 157 #define CMD_FIRST_VALID     2
 158 
 159 
 160 /* Second parameter is the required argument count */
 161 
 162 static const ACPI_DB_COMMAND_INFO   AcpiGbl_DbCommands[] =
 163 {
 164     {"<NOT FOUND>",  0},
 165     {"<NULL>",       0},
 166     {"ALLOCATIONS",  0},
 167     {"ARGS",         0},
 168     {"ARGUMENTS",    0},
 169     {"BREAKPOINT",   1},
 170     {"BUSINFO",      0},
 171     {"CALL",         0},
 172     {"CLOSE",        0},
 173     {"DEBUG",        1},
 174     {"DISASSEMBLE",  1},
 175     {"DISASM",       1},
 176     {"DUMP",         1},
 177     {"ENABLEACPI",   0},
 178     {"EVALUATE",     1},
 179     {"EVENT",        1},
 180     {"EXECUTE",      1},
 181     {"EXIT",         0},
 182     {"FIND",         1},
 183     {"GO",           0},
 184     {"GPE",          2},
 185     {"GPES",         0},
 186     {"HANDLERS",     0},
 187     {"HELP",         0},
 188     {"?",            0},
 189     {"HISTORY",      0},
 190     {"!",            1},
 191     {"!!",           0},
 192     {"INFORMATION",  0},
 193     {"INTEGRITY",    0},
 194     {"INTO",         0},
 195     {"LEVEL",        0},
 196     {"LIST",         0},
 197     {"LOAD",         1},
 198     {"LOCALS",       0},
 199     {"LOCKS",        0},
 200     {"METHODS",      0},
 201     {"NAMESPACE",    0},
 202     {"NOTIFY",       2},
 203     {"OBJECTS",      1},
 204     {"OPEN",         1},
 205     {"OSI",          0},
 206     {"OWNER",        1},
 207     {"PATHS",        0},
 208     {"PREDEFINED",   0},
 209     {"PREFIX",       0},
 210     {"QUIT",         0},
 211     {"REFERENCES",   1},
 212     {"RESOURCES",    0},
 213     {"RESULTS",      0},
 214     {"SCI",          0},
 215     {"SET",          3},
 216     {"SLEEP",        0},
 217     {"STATS",        1},
 218     {"STOP",         0},
 219     {"TABLES",       0},
 220     {"TEMPLATE",     1},
 221     {"TERMINATE",    0},
 222     {"TEST",        1},
 223     {"THREADS",      3},
 224     {"TRACE",        1},
 225     {"TREE",         0},
 226     {"TYPE",         1},
 227     {"UNLOAD",       1},
 228     {NULL,           0}
 229 };
 230 
 231 /*
 232  * Help for all debugger commands. First argument is the number of lines
 233  * of help to output for the command.
 234  */
 235 static const ACPI_DB_COMMAND_HELP   AcpiGbl_DbCommandHelp[] =
 236 {
 237     {0, "\nGeneral-Purpose Commands:",         "\n"},
 238     {1, "  Allocations",                       "Display list of current memory allocations\n"},
 239     {2, "  Dump <Address>|<Namepath>",         "\n"},
 240     {0, "       [Byte|Word|Dword|Qword]",      "Display ACPI objects or memory\n"},
 241     {1, "  EnableAcpi",                        "Enable ACPI (hardware) mode\n"},
 242     {1, "  Handlers",                          "Info about global handlers\n"},
 243     {1, "  Help [Command]",                    "This help screen or individual command\n"},
 244     {1, "  History",                           "Display command history buffer\n"},
 245     {1, "  Level <DebugLevel>] [console]",     "Get/Set debug level for file or console\n"},
 246     {1, "  Locks",                             "Current status of internal mutexes\n"},
 247     {1, "  Osi [Install|Remove <name>]",       "Display or modify global _OSI list\n"},
 248     {1, "  Quit or Exit",                      "Exit this command\n"},
 249     {8, "  Stats <SubCommand>",                "Display namespace and memory statistics\n"},
 250     {1, "     Allocations",                    "Display list of current memory allocations\n"},
 251     {1, "     Memory",                         "Dump internal memory lists\n"},
 252     {1, "     Misc",                           "Namespace search and mutex stats\n"},
 253     {1, "     Objects",                        "Summary of namespace objects\n"},
 254     {1, "     Sizes",                          "Sizes for each of the internal objects\n"},
 255     {1, "     Stack",                          "Display CPU stack usage\n"},
 256     {1, "     Tables",                         "Info about current ACPI table(s)\n"},
 257     {1, "  Tables",                            "Display info about loaded ACPI tables\n"},
 258     {1, "  Unload <Namepath>",                 "Unload an ACPI table via namespace object\n"},
 259     {1, "  ! <CommandNumber>",                 "Execute command from history buffer\n"},
 260     {1, "  !!",                                "Execute last command again\n"},
 261 
 262     {0, "\nNamespace Access Commands:",        "\n"},
 263     {1, "  Businfo",                           "Display system bus info\n"},
 264     {1, "  Disassemble <Method>",              "Disassemble a control method\n"},
 265     {1, "  Find <AcpiName> (? is wildcard)",   "Find ACPI name(s) with wildcards\n"},
 266     {1, "  Integrity",                         "Validate namespace integrity\n"},
 267     {1, "  Methods",                           "Display list of loaded control methods\n"},
 268     {1, "  Namespace [Object] [Depth]",        "Display loaded namespace tree/subtree\n"},
 269     {1, "  Notify <Object> <Value>",           "Send a notification on Object\n"},
 270     {1, "  Objects <ObjectType>",              "Display all objects of the given type\n"},
 271     {1, "  Owner <OwnerId> [Depth]",           "Display loaded namespace by object owner\n"},
 272     {1, "  Paths",                             "Display full pathnames of namespace objects\n"},
 273     {1, "  Predefined",                        "Check all predefined names\n"},
 274     {1, "  Prefix [<NamePath>]",               "Set or Get current execution prefix\n"},
 275     {1, "  References <Addr>",                 "Find all references to object at addr\n"},
 276     {1, "  Resources [DeviceName]",            "Display Device resources (no arg = all devices)\n"},
 277     {1, "  Set N <NamedObject> <Value>",       "Set value for named integer\n"},
 278     {1, "  Template <Object>",                 "Format/dump a Buffer/ResourceTemplate\n"},
 279     {1, "  Terminate",                         "Delete namespace and all internal objects\n"},
 280     {1, "  Type <Object>",                     "Display object type\n"},
 281 
 282     {0, "\nControl Method Execution Commands:","\n"},
 283     {1, "  Arguments (or Args)",               "Display method arguments\n"},
 284     {1, "  Breakpoint <AmlOffset>",            "Set an AML execution breakpoint\n"},
 285     {1, "  Call",                              "Run to next control method invocation\n"},
 286     {1, "  Debug <Namepath> [Arguments]",      "Single Step a control method\n"},
 287     {6, "  Evaluate",                          "Synonym for Execute\n"},
 288     {5, "  Execute <Namepath> [Arguments]",    "Execute control method\n"},
 289     {1, "     Hex Integer",                    "Integer method argument\n"},
 290     {1, "     \"Ascii String\"",               "String method argument\n"},
 291     {1, "     (Hex Byte List)",                "Buffer method argument\n"},
 292     {1, "     [Package Element List]",         "Package method argument\n"},
 293     {1, "  Go",                                "Allow method to run to completion\n"},
 294     {1, "  Information",                       "Display info about the current method\n"},
 295     {1, "  Into",                              "Step into (not over) a method call\n"},
 296     {1, "  List [# of Aml Opcodes]",           "Display method ASL statements\n"},
 297     {1, "  Locals",                            "Display method local variables\n"},
 298     {1, "  Results",                           "Display method result stack\n"},
 299     {1, "  Set <A|L> <#> <Value>",             "Set method data (Arguments/Locals)\n"},
 300     {1, "  Stop",                              "Terminate control method\n"},
 301     {1, "  Thread <Threads><Loops><NamePath>", "Spawn threads to execute method(s)\n"},
 302     {1, "  Trace <method name>",               "Trace method execution\n"},
 303     {1, "  Tree",                              "Display control method calling tree\n"},
 304     {1, "  <Enter>",                           "Single step next AML opcode (over calls)\n"},
 305 
 306     {0, "\nHardware Related Commands:",         "\n"},
 307     {1, "  Event <F|G> <Value>",               "Generate AcpiEvent (Fixed/GPE)\n"},
 308     {1, "  Gpe <GpeNum> <GpeBlock>",           "Simulate a GPE\n"},
 309     {1, "  Gpes",                              "Display info on all GPEs\n"},
 310     {1, "  Sci",                               "Generate an SCI\n"},
 311     {1, "  Sleep [SleepState]",                "Simulate sleep/wake sequence(s) (0-5)\n"},
 312 
 313     {0, "\nFile I/O Commands:",                "\n"},
 314     {1, "  Close",                             "Close debug output file\n"},
 315     {1, "  Load <Input Filename>",             "Load ACPI table from a file\n"},
 316     {1, "  Open <Output Filename>",            "Open a file for debug output\n"},
 317 
 318     {0, "\nDebug Test Commands:",              "\n"},
 319     {3, "  Test <TestName>",                   "Invoke a debug test\n"},
 320     {1, "     Objects",                        "Read/write/compare all namespace data objects\n"},
 321     {1, "     Predefined",                     "Execute all ACPI predefined names (_STA, etc.)\n"},
 322     {0, NULL, NULL}
 323 };
 324 
 325 
 326 /*******************************************************************************
 327  *
 328  * FUNCTION:    AcpiDbMatchCommandHelp
 329  *
 330  * PARAMETERS:  Command             - Command string to match
 331  *              Help                - Help table entry to attempt match
 332  *
 333  * RETURN:      TRUE if command matched, FALSE otherwise
 334  *
 335  * DESCRIPTION: Attempt to match a command in the help table in order to
 336  *              print help information for a single command.
 337  *
 338  ******************************************************************************/
 339 
 340 static BOOLEAN
 341 AcpiDbMatchCommandHelp (
 342     char                        *Command,
 343     const ACPI_DB_COMMAND_HELP  *Help)
 344 {
 345     char                    *Invocation = Help->Invocation;
 346     UINT32                  LineCount;
 347 
 348 
 349     /* Valid commands in the help table begin with a couple of spaces */
 350 
 351     if (*Invocation != ' ')
 352     {
 353         return (FALSE);
 354     }
 355 
 356     while (*Invocation == ' ')
 357     {
 358         Invocation++;
 359     }
 360 
 361     /* Match command name (full command or substring) */
 362 
 363     while ((*Command) && (*Invocation) && (*Invocation != ' '))
 364     {
 365         if (ACPI_TOLOWER (*Command) != ACPI_TOLOWER (*Invocation))
 366         {
 367             return (FALSE);
 368         }
 369 
 370         Invocation++;
 371         Command++;
 372     }
 373 
 374     /* Print the appropriate number of help lines */
 375 
 376     LineCount = Help->LineCount;
 377     while (LineCount)
 378     {
 379         AcpiOsPrintf ("%-38s : %s", Help->Invocation, Help->Description);
 380         Help++;
 381         LineCount--;
 382     }
 383 
 384     return (TRUE);
 385 }
 386 
 387 
 388 /*******************************************************************************
 389  *
 390  * FUNCTION:    AcpiDbDisplayCommandInfo
 391  *
 392  * PARAMETERS:  Command             - Command string to match
 393  *              DisplayAll          - Display all matching commands, or just
 394  *                                    the first one (substring match)
 395  *
 396  * RETURN:      None
 397  *
 398  * DESCRIPTION: Display help information for a Debugger command.
 399  *
 400  ******************************************************************************/
 401 
 402 static void
 403 AcpiDbDisplayCommandInfo (
 404     char                    *Command,
 405     BOOLEAN                 DisplayAll)
 406 {
 407     const ACPI_DB_COMMAND_HELP  *Next;
 408     BOOLEAN                     Matched;
 409 
 410 
 411     Next = AcpiGbl_DbCommandHelp;
 412     while (Next->Invocation)
 413     {
 414         Matched = AcpiDbMatchCommandHelp (Command, Next);
 415         if (!DisplayAll && Matched)
 416         {
 417             return;
 418         }
 419 
 420         Next++;
 421     }
 422 }
 423 
 424 
 425 /*******************************************************************************
 426  *
 427  * FUNCTION:    AcpiDbDisplayHelp
 428  *
 429  * PARAMETERS:  Command             - Optional command string to display help.
 430  *                                    if not specified, all debugger command
 431  *                                    help strings are displayed
 432  *
 433  * RETURN:      None
 434  *
 435  * DESCRIPTION: Display help for a single debugger command, or all of them.
 436  *
 437  ******************************************************************************/
 438 
 439 static void
 440 AcpiDbDisplayHelp (
 441     char                    *Command)
 442 {
 443     const ACPI_DB_COMMAND_HELP  *Next = AcpiGbl_DbCommandHelp;
 444 
 445 
 446     if (!Command)
 447     {
 448         /* No argument to help, display help for all commands */
 449 
 450         while (Next->Invocation)
 451         {
 452             AcpiOsPrintf ("%-38s%s", Next->Invocation, Next->Description);
 453             Next++;
 454         }
 455     }
 456     else
 457     {
 458         /* Display help for all commands that match the subtring */
 459 
 460         AcpiDbDisplayCommandInfo (Command, TRUE);
 461     }
 462 }
 463 
 464 
 465 /*******************************************************************************
 466  *
 467  * FUNCTION:    AcpiDbGetNextToken
 468  *
 469  * PARAMETERS:  String          - Command buffer
 470  *              Next            - Return value, end of next token
 471  *
 472  * RETURN:      Pointer to the start of the next token.
 473  *
 474  * DESCRIPTION: Command line parsing. Get the next token on the command line
 475  *
 476  ******************************************************************************/
 477 
 478 char *
 479 AcpiDbGetNextToken (
 480     char                    *String,
 481     char                    **Next,
 482     ACPI_OBJECT_TYPE        *ReturnType)
 483 {
 484     char                    *Start;
 485     UINT32                  Depth;
 486     ACPI_OBJECT_TYPE        Type = ACPI_TYPE_INTEGER;
 487 
 488 
 489     /* At end of buffer? */
 490 
 491     if (!String || !(*String))
 492     {
 493         return (NULL);
 494     }
 495 
 496     /* Remove any spaces at the beginning */
 497 
 498     if (*String == ' ')
 499     {
 500         while (*String && (*String == ' '))
 501         {
 502             String++;
 503         }
 504 
 505         if (!(*String))
 506         {
 507             return (NULL);
 508         }
 509     }
 510 
 511     switch (*String)
 512     {
 513     case '"':
 514 
 515         /* This is a quoted string, scan until closing quote */
 516 
 517         String++;
 518         Start = String;
 519         Type = ACPI_TYPE_STRING;
 520 
 521         /* Find end of string */
 522 
 523         while (*String && (*String != '"'))
 524         {
 525             String++;
 526         }
 527         break;
 528 
 529     case '(':
 530 
 531         /* This is the start of a buffer, scan until closing paren */
 532 
 533         String++;
 534         Start = String;
 535         Type = ACPI_TYPE_BUFFER;
 536 
 537         /* Find end of buffer */
 538 
 539         while (*String && (*String != ')'))
 540         {
 541             String++;
 542         }
 543         break;
 544 
 545     case '[':
 546 
 547         /* This is the start of a package, scan until closing bracket */
 548 
 549         String++;
 550         Depth = 1;
 551         Start = String;
 552         Type = ACPI_TYPE_PACKAGE;
 553 
 554         /* Find end of package (closing bracket) */
 555 
 556         while (*String)
 557         {
 558             /* Handle String package elements */
 559 
 560             if (*String == '"')
 561             {
 562                 /* Find end of string */
 563 
 564                 String++;
 565                 while (*String && (*String != '"'))
 566                 {
 567                     String++;
 568                 }
 569                 if (!(*String))
 570                 {
 571                     break;
 572                 }
 573             }
 574             else if (*String == '[')
 575             {
 576                 Depth++;         /* A nested package declaration */
 577             }
 578             else if (*String == ']')
 579             {
 580                 Depth--;
 581                 if (Depth == 0) /* Found final package closing bracket */
 582                 {
 583                     break;
 584                 }
 585             }
 586 
 587             String++;
 588         }
 589         break;
 590 
 591     default:
 592 
 593         Start = String;
 594 
 595         /* Find end of token */
 596 
 597         while (*String && (*String != ' '))
 598         {
 599             String++;
 600         }
 601         break;
 602     }
 603 
 604     if (!(*String))
 605     {
 606         *Next = NULL;
 607     }
 608     else
 609     {
 610         *String = 0;
 611         *Next = String + 1;
 612     }
 613 
 614     *ReturnType = Type;
 615     return (Start);
 616 }
 617 
 618 
 619 /*******************************************************************************
 620  *
 621  * FUNCTION:    AcpiDbGetLine
 622  *
 623  * PARAMETERS:  InputBuffer         - Command line buffer
 624  *
 625  * RETURN:      Count of arguments to the command
 626  *
 627  * DESCRIPTION: Get the next command line from the user. Gets entire line
 628  *              up to the next newline
 629  *
 630  ******************************************************************************/
 631 
 632 static UINT32
 633 AcpiDbGetLine (
 634     char                    *InputBuffer)
 635 {
 636     UINT32                  i;
 637     UINT32                  Count;
 638     char                    *Next;
 639     char                    *This;
 640 
 641 
 642     if (AcpiUtSafeStrcpy (AcpiGbl_DbParsedBuf, sizeof (AcpiGbl_DbParsedBuf),
 643         InputBuffer))
 644     {
 645         AcpiOsPrintf ("Buffer overflow while parsing input line (max %u characters)\n",
 646             sizeof (AcpiGbl_DbParsedBuf));
 647         return (0);
 648     }
 649 
 650     This = AcpiGbl_DbParsedBuf;
 651     for (i = 0; i < ACPI_DEBUGGER_MAX_ARGS; i++)
 652     {
 653         AcpiGbl_DbArgs[i] = AcpiDbGetNextToken (This, &Next,
 654             &AcpiGbl_DbArgTypes[i]);
 655         if (!AcpiGbl_DbArgs[i])
 656         {
 657             break;
 658         }
 659 
 660         This = Next;
 661     }
 662 
 663     /* Uppercase the actual command */
 664 
 665     if (AcpiGbl_DbArgs[0])
 666     {
 667         AcpiUtStrupr (AcpiGbl_DbArgs[0]);
 668     }
 669 
 670     Count = i;
 671     if (Count)
 672     {
 673         Count--;  /* Number of args only */
 674     }
 675 
 676     return (Count);
 677 }
 678 
 679 
 680 /*******************************************************************************
 681  *
 682  * FUNCTION:    AcpiDbMatchCommand
 683  *
 684  * PARAMETERS:  UserCommand             - User command line
 685  *
 686  * RETURN:      Index into command array, -1 if not found
 687  *
 688  * DESCRIPTION: Search command array for a command match
 689  *
 690  ******************************************************************************/
 691 
 692 static UINT32
 693 AcpiDbMatchCommand (
 694     char                    *UserCommand)
 695 {
 696     UINT32                  i;
 697 
 698 
 699     if (!UserCommand || UserCommand[0] == 0)
 700     {
 701         return (CMD_NULL);
 702     }
 703 
 704     for (i = CMD_FIRST_VALID; AcpiGbl_DbCommands[i].Name; i++)
 705     {
 706         if (ACPI_STRSTR (AcpiGbl_DbCommands[i].Name, UserCommand) ==
 707                          AcpiGbl_DbCommands[i].Name)
 708         {
 709             return (i);
 710         }
 711     }
 712 
 713     /* Command not recognized */
 714 
 715     return (CMD_NOT_FOUND);
 716 }
 717 
 718 
 719 /*******************************************************************************
 720  *
 721  * FUNCTION:    AcpiDbCommandDispatch
 722  *
 723  * PARAMETERS:  InputBuffer         - Command line buffer
 724  *              WalkState           - Current walk
 725  *              Op                  - Current (executing) parse op
 726  *
 727  * RETURN:      Status
 728  *
 729  * DESCRIPTION: Command dispatcher.
 730  *
 731  ******************************************************************************/
 732 
 733 ACPI_STATUS
 734 AcpiDbCommandDispatch (
 735     char                    *InputBuffer,
 736     ACPI_WALK_STATE         *WalkState,
 737     ACPI_PARSE_OBJECT       *Op)
 738 {
 739     UINT32                  Temp;
 740     UINT32                  CommandIndex;
 741     UINT32                  ParamCount;
 742     char                    *CommandLine;
 743     ACPI_STATUS             Status = AE_CTRL_TRUE;
 744 
 745 
 746     /* If AcpiTerminate has been called, terminate this thread */
 747 
 748     if (AcpiGbl_DbTerminateThreads)
 749     {
 750         return (AE_CTRL_TERMINATE);
 751     }
 752 
 753 
 754     /* Add all commands that come here to the history buffer */
 755 
 756     AcpiDbAddToHistory (InputBuffer);
 757 
 758     ParamCount = AcpiDbGetLine (InputBuffer);
 759     CommandIndex = AcpiDbMatchCommand (AcpiGbl_DbArgs[0]);
 760     Temp = 0;
 761 
 762     /* Verify that we have the minimum number of params */
 763 
 764     if (ParamCount < AcpiGbl_DbCommands[CommandIndex].MinArgs)
 765     {
 766         AcpiOsPrintf ("%u parameters entered, [%s] requires %u parameters\n",
 767             ParamCount, AcpiGbl_DbCommands[CommandIndex].Name,
 768             AcpiGbl_DbCommands[CommandIndex].MinArgs);
 769 
 770         AcpiDbDisplayCommandInfo (AcpiGbl_DbCommands[CommandIndex].Name, FALSE);
 771         return (AE_CTRL_TRUE);
 772     }
 773 
 774     /* Decode and dispatch the command */
 775 
 776     switch (CommandIndex)
 777     {
 778     case CMD_NULL:
 779 
 780         if (Op)
 781         {
 782             return (AE_OK);
 783         }
 784         break;
 785 
 786     case CMD_ALLOCATIONS:
 787 
 788 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
 789         AcpiUtDumpAllocations ((UINT32) -1, NULL);
 790 #endif
 791         break;
 792 
 793     case CMD_ARGS:
 794     case CMD_ARGUMENTS:
 795 
 796         AcpiDbDisplayArguments ();
 797         break;
 798 
 799     case CMD_BREAKPOINT:
 800 
 801         AcpiDbSetMethodBreakpoint (AcpiGbl_DbArgs[1], WalkState, Op);
 802         break;
 803 
 804     case CMD_BUSINFO:
 805 
 806         AcpiDbGetBusInfo ();
 807         break;
 808 
 809     case CMD_CALL:
 810 
 811         AcpiDbSetMethodCallBreakpoint (Op);
 812         Status = AE_OK;
 813         break;
 814 
 815     case CMD_CLOSE:
 816 
 817         AcpiDbCloseDebugFile ();
 818         break;
 819 
 820     case CMD_DEBUG:
 821 
 822         AcpiDbExecute (AcpiGbl_DbArgs[1],
 823             &AcpiGbl_DbArgs[2], &AcpiGbl_DbArgTypes[2], EX_SINGLE_STEP);
 824         break;
 825 
 826     case CMD_DISASSEMBLE:
 827     case CMD_DISASM:
 828 
 829         (void) AcpiDbDisassembleMethod (AcpiGbl_DbArgs[1]);
 830         break;
 831 
 832     case CMD_DUMP:
 833 
 834         AcpiDbDecodeAndDisplayObject (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
 835         break;
 836 
 837     case CMD_ENABLEACPI:
 838 #if (!ACPI_REDUCED_HARDWARE)
 839 
 840         Status = AcpiEnable();
 841         if (ACPI_FAILURE(Status))
 842         {
 843             AcpiOsPrintf("AcpiEnable failed (Status=%X)\n", Status);
 844             return (Status);
 845         }
 846 #endif /* !ACPI_REDUCED_HARDWARE */
 847         break;
 848 
 849     case CMD_EVENT:
 850 
 851         AcpiOsPrintf ("Event command not implemented\n");
 852         break;
 853 
 854     case CMD_EVALUATE:
 855     case CMD_EXECUTE:
 856 
 857         AcpiDbExecute (AcpiGbl_DbArgs[1],
 858             &AcpiGbl_DbArgs[2], &AcpiGbl_DbArgTypes[2], EX_NO_SINGLE_STEP);
 859         break;
 860 
 861     case CMD_FIND:
 862 
 863         Status = AcpiDbFindNameInNamespace (AcpiGbl_DbArgs[1]);
 864         break;
 865 
 866     case CMD_GO:
 867 
 868         AcpiGbl_CmSingleStep = FALSE;
 869         return (AE_OK);
 870 
 871     case CMD_GPE:
 872 
 873         AcpiDbGenerateGpe (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
 874         break;
 875 
 876     case CMD_GPES:
 877 
 878         AcpiDbDisplayGpes ();
 879         break;
 880 
 881     case CMD_HANDLERS:
 882 
 883         AcpiDbDisplayHandlers ();
 884         break;
 885 
 886     case CMD_HELP:
 887     case CMD_HELP2:
 888 
 889         AcpiDbDisplayHelp (AcpiGbl_DbArgs[1]);
 890         break;
 891 
 892     case CMD_HISTORY:
 893 
 894         AcpiDbDisplayHistory ();
 895         break;
 896 
 897     case CMD_HISTORY_EXE: /* ! command */
 898 
 899         CommandLine = AcpiDbGetFromHistory (AcpiGbl_DbArgs[1]);
 900         if (!CommandLine)
 901         {
 902             return (AE_CTRL_TRUE);
 903         }
 904 
 905         Status = AcpiDbCommandDispatch (CommandLine, WalkState, Op);
 906         return (Status);
 907 
 908     case CMD_HISTORY_LAST: /* !! command */
 909 
 910         CommandLine = AcpiDbGetFromHistory (NULL);
 911         if (!CommandLine)
 912         {
 913             return (AE_CTRL_TRUE);
 914         }
 915 
 916         Status = AcpiDbCommandDispatch (CommandLine, WalkState, Op);
 917         return (Status);
 918 
 919     case CMD_INFORMATION:
 920 
 921         AcpiDbDisplayMethodInfo (Op);
 922         break;
 923 
 924     case CMD_INTEGRITY:
 925 
 926         AcpiDbCheckIntegrity ();
 927         break;
 928 
 929     case CMD_INTO:
 930 
 931         if (Op)
 932         {
 933             AcpiGbl_CmSingleStep = TRUE;
 934             return (AE_OK);
 935         }
 936         break;
 937 
 938     case CMD_LEVEL:
 939 
 940         if (ParamCount == 0)
 941         {
 942             AcpiOsPrintf ("Current debug level for file output is:    %8.8lX\n",
 943                 AcpiGbl_DbDebugLevel);
 944             AcpiOsPrintf ("Current debug level for console output is: %8.8lX\n",
 945                 AcpiGbl_DbConsoleDebugLevel);
 946         }
 947         else if (ParamCount == 2)
 948         {
 949             Temp = AcpiGbl_DbConsoleDebugLevel;
 950             AcpiGbl_DbConsoleDebugLevel = ACPI_STRTOUL (AcpiGbl_DbArgs[1],
 951                                             NULL, 16);
 952             AcpiOsPrintf (
 953                 "Debug Level for console output was %8.8lX, now %8.8lX\n",
 954                 Temp, AcpiGbl_DbConsoleDebugLevel);
 955         }
 956         else
 957         {
 958             Temp = AcpiGbl_DbDebugLevel;
 959             AcpiGbl_DbDebugLevel = ACPI_STRTOUL (AcpiGbl_DbArgs[1], NULL, 16);
 960             AcpiOsPrintf (
 961                 "Debug Level for file output was %8.8lX, now %8.8lX\n",
 962                 Temp, AcpiGbl_DbDebugLevel);
 963         }
 964         break;
 965 
 966     case CMD_LIST:
 967 
 968         AcpiDbDisassembleAml (AcpiGbl_DbArgs[1], Op);
 969         break;
 970 
 971     case CMD_LOAD:
 972 
 973         Status = AcpiDbGetTableFromFile (AcpiGbl_DbArgs[1], NULL);
 974         break;
 975 
 976     case CMD_LOCKS:
 977 
 978         AcpiDbDisplayLocks ();
 979         break;
 980 
 981     case CMD_LOCALS:
 982 
 983         AcpiDbDisplayLocals ();
 984         break;
 985 
 986     case CMD_METHODS:
 987 
 988         Status = AcpiDbDisplayObjects ("METHOD", AcpiGbl_DbArgs[1]);
 989         break;
 990 
 991     case CMD_NAMESPACE:
 992 
 993         AcpiDbDumpNamespace (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
 994         break;
 995 
 996     case CMD_NOTIFY:
 997 
 998         Temp = ACPI_STRTOUL (AcpiGbl_DbArgs[2], NULL, 0);
 999         AcpiDbSendNotify (AcpiGbl_DbArgs[1], Temp);
1000         break;
1001 
1002     case CMD_OBJECTS:
1003 
1004         AcpiUtStrupr (AcpiGbl_DbArgs[1]);
1005         Status = AcpiDbDisplayObjects (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1006         break;
1007 
1008     case CMD_OPEN:
1009 
1010         AcpiDbOpenDebugFile (AcpiGbl_DbArgs[1]);
1011         break;
1012 
1013     case CMD_OSI:
1014 
1015         AcpiDbDisplayInterfaces (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1016         break;
1017 
1018     case CMD_OWNER:
1019 
1020         AcpiDbDumpNamespaceByOwner (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1021         break;
1022 
1023     case CMD_PATHS:
1024 
1025         AcpiDbDumpNamespacePaths ();
1026         break;
1027 
1028     case CMD_PREDEFINED:
1029 
1030         AcpiDbCheckPredefinedNames ();
1031         break;
1032 
1033     case CMD_PREFIX:
1034 
1035         AcpiDbSetScope (AcpiGbl_DbArgs[1]);
1036         break;
1037 
1038     case CMD_REFERENCES:
1039 
1040         AcpiDbFindReferences (AcpiGbl_DbArgs[1]);
1041         break;
1042 
1043     case CMD_RESOURCES:
1044 
1045         AcpiDbDisplayResources (AcpiGbl_DbArgs[1]);
1046         break;
1047 
1048     case CMD_RESULTS:
1049 
1050         AcpiDbDisplayResults ();
1051         break;
1052 
1053     case CMD_SCI:
1054 
1055         AcpiDbGenerateSci ();
1056         break;
1057 
1058     case CMD_SET:
1059 
1060         AcpiDbSetMethodData (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2],
1061             AcpiGbl_DbArgs[3]);
1062         break;
1063 
1064     case CMD_SLEEP:
1065 
1066         Status = AcpiDbSleep (AcpiGbl_DbArgs[1]);
1067         break;
1068 
1069     case CMD_STATS:
1070 
1071         Status = AcpiDbDisplayStatistics (AcpiGbl_DbArgs[1]);
1072         break;
1073 
1074     case CMD_STOP:
1075 
1076         return (AE_NOT_IMPLEMENTED);
1077 
1078     case CMD_TABLES:
1079 
1080         AcpiDbDisplayTableInfo (AcpiGbl_DbArgs[1]);
1081         break;
1082 
1083     case CMD_TEMPLATE:
1084 
1085         AcpiDbDisplayTemplate (AcpiGbl_DbArgs[1]);
1086         break;
1087 
1088     case CMD_TERMINATE:
1089 
1090         AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
1091         AcpiUtSubsystemShutdown ();
1092 
1093         /*
1094          * TBD: [Restructure] Need some way to re-initialize without
1095          * re-creating the semaphores!
1096          */
1097 
1098         /*  AcpiInitialize (NULL);  */
1099         break;
1100 
1101     case CMD_TEST:
1102 
1103         AcpiDbExecuteTest (AcpiGbl_DbArgs[1]);
1104         break;
1105 
1106     case CMD_THREADS:
1107 
1108         AcpiDbCreateExecutionThreads (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2],
1109             AcpiGbl_DbArgs[3]);
1110         break;
1111 
1112     case CMD_TRACE:
1113 
1114         (void) AcpiDebugTrace (AcpiGbl_DbArgs[1],0,0,1);
1115         break;
1116 
1117     case CMD_TREE:
1118 
1119         AcpiDbDisplayCallingTree ();
1120         break;
1121 
1122     case CMD_TYPE:
1123 
1124         AcpiDbDisplayObjectType (AcpiGbl_DbArgs[1]);
1125         break;
1126 
1127     case CMD_UNLOAD:
1128 
1129         AcpiDbUnloadAcpiTable (AcpiGbl_DbArgs[1]);
1130         break;
1131 
1132     case CMD_EXIT:
1133     case CMD_QUIT:
1134 
1135         if (Op)
1136         {
1137             AcpiOsPrintf ("Method execution terminated\n");
1138             return (AE_CTRL_TERMINATE);
1139         }
1140 
1141         if (!AcpiGbl_DbOutputToFile)
1142         {
1143             AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
1144         }
1145 
1146         AcpiDbCloseDebugFile ();
1147         AcpiGbl_DbTerminateThreads = TRUE;
1148         return (AE_CTRL_TERMINATE);
1149 
1150     case CMD_NOT_FOUND:
1151     default:
1152 
1153         AcpiOsPrintf ("%s: unknown command\n", AcpiGbl_DbArgs[0]);
1154         return (AE_CTRL_TRUE);
1155     }
1156 
1157     if (ACPI_SUCCESS (Status))
1158     {
1159         Status = AE_CTRL_TRUE;
1160     }
1161 
1162     return (Status);
1163 }
1164 
1165 
1166 /*******************************************************************************
1167  *
1168  * FUNCTION:    AcpiDbExecuteThread
1169  *
1170  * PARAMETERS:  Context         - Not used
1171  *
1172  * RETURN:      None
1173  *
1174  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
1175  *              simply dispatches it.
1176  *
1177  ******************************************************************************/
1178 
1179 void ACPI_SYSTEM_XFACE
1180 AcpiDbExecuteThread (
1181     void                    *Context)
1182 {
1183     ACPI_STATUS             Status = AE_OK;
1184     ACPI_STATUS             MStatus;
1185 
1186 
1187     while (Status != AE_CTRL_TERMINATE)
1188     {
1189         AcpiGbl_MethodExecuting = FALSE;
1190         AcpiGbl_StepToNextCall = FALSE;
1191 
1192         MStatus = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_READY);
1193         if (ACPI_FAILURE (MStatus))
1194         {
1195             return;
1196         }
1197 
1198         Status = AcpiDbCommandDispatch (AcpiGbl_DbLineBuf, NULL, NULL);
1199 
1200         MStatus = AcpiUtReleaseMutex (ACPI_MTX_DEBUG_CMD_COMPLETE);
1201         if (ACPI_FAILURE (MStatus))
1202         {
1203             return;
1204         }
1205     }
1206 }
1207 
1208 
1209 /*******************************************************************************
1210  *
1211  * FUNCTION:    AcpiDbSingleThread
1212  *
1213  * PARAMETERS:  None
1214  *
1215  * RETURN:      None
1216  *
1217  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
1218  *              simply dispatches it.
1219  *
1220  ******************************************************************************/
1221 
1222 static void
1223 AcpiDbSingleThread (
1224     void)
1225 {
1226 
1227     AcpiGbl_MethodExecuting = FALSE;
1228     AcpiGbl_StepToNextCall = FALSE;
1229 
1230     (void) AcpiDbCommandDispatch (AcpiGbl_DbLineBuf, NULL, NULL);
1231 }
1232 
1233 
1234 /*******************************************************************************
1235  *
1236  * FUNCTION:    AcpiDbUserCommands
1237  *
1238  * PARAMETERS:  Prompt              - User prompt (depends on mode)
1239  *              Op                  - Current executing parse op
1240  *
1241  * RETURN:      None
1242  *
1243  * DESCRIPTION: Command line execution for the AML debugger. Commands are
1244  *              matched and dispatched here.
1245  *
1246  ******************************************************************************/
1247 
1248 ACPI_STATUS
1249 AcpiDbUserCommands (
1250     char                    Prompt,
1251     ACPI_PARSE_OBJECT       *Op)
1252 {
1253     ACPI_STATUS             Status = AE_OK;
1254 
1255 
1256     AcpiOsPrintf ("\n");
1257 
1258     /* TBD: [Restructure] Need a separate command line buffer for step mode */
1259 
1260     while (!AcpiGbl_DbTerminateThreads)
1261     {
1262         /* Force output to console until a command is entered */
1263 
1264         AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
1265 
1266         /* Different prompt if method is executing */
1267 
1268         if (!AcpiGbl_MethodExecuting)
1269         {
1270             AcpiOsPrintf ("%1c ", ACPI_DEBUGGER_COMMAND_PROMPT);
1271         }
1272         else
1273         {
1274             AcpiOsPrintf ("%1c ", ACPI_DEBUGGER_EXECUTE_PROMPT);
1275         }
1276 
1277         /* Get the user input line */
1278 
1279         Status = AcpiOsGetLine (AcpiGbl_DbLineBuf,
1280             ACPI_DB_LINE_BUFFER_SIZE, NULL);
1281         if (ACPI_FAILURE (Status))
1282         {
1283             ACPI_EXCEPTION ((AE_INFO, Status, "While parsing command line"));
1284             return (Status);
1285         }
1286 
1287         /* Check for single or multithreaded debug */
1288 
1289         if (AcpiGbl_DebuggerConfiguration & DEBUGGER_MULTI_THREADED)
1290         {
1291             /*
1292              * Signal the debug thread that we have a command to execute,
1293              * and wait for the command to complete.
1294              */
1295             Status = AcpiUtReleaseMutex (ACPI_MTX_DEBUG_CMD_READY);
1296             if (ACPI_FAILURE (Status))
1297             {
1298                 return (Status);
1299             }
1300 
1301             Status = AcpiUtAcquireMutex (ACPI_MTX_DEBUG_CMD_COMPLETE);
1302             if (ACPI_FAILURE (Status))
1303             {
1304                 return (Status);
1305             }
1306         }
1307         else
1308         {
1309             /* Just call to the command line interpreter */
1310 
1311             AcpiDbSingleThread ();
1312         }
1313     }
1314 
1315     /*
1316      * Only this thread (the original thread) should actually terminate the
1317      * subsystem, because all the semaphores are deleted during termination
1318      */
1319     Status = AcpiTerminate ();
1320     return (Status);
1321 }
1322 
1323 #endif  /* ACPI_DEBUGGER */