1 /*******************************************************************************
2  *
3  * Module Name: dbinput - user front-end to the AML debugger
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acdebug.h>
155 
156 #ifdef ACPI_APPLICATION
157 #include <contrib/dev/acpica/include/acapps.h>
158 #endif
159 
160 #define _COMPONENT          ACPI_CA_DEBUGGER
161         ACPI_MODULE_NAME    ("dbinput")
162 
163 
164 /* Local prototypes */
165 
166 static UINT32
167 AcpiDbGetLine (
168     char                    *InputBuffer);
169 
170 static UINT32
171 AcpiDbMatchCommand (
172     char                    *UserCommand);
173 
174 static void
175 AcpiDbDisplayCommandInfo (
176     const char              *Command,
177     BOOLEAN                 DisplayAll);
178 
179 static void
180 AcpiDbDisplayHelp (
181     char                    *Command);
182 
183 static BOOLEAN
184 AcpiDbMatchCommandHelp (
185     const char                  *Command,
186     const ACPI_DB_COMMAND_HELP  *Help);
187 
188 
189 /*
190  * Top-level debugger commands.
191  *
192  * This list of commands must match the string table below it
193  */
194 enum AcpiExDebuggerCommands
195 {
196     CMD_NOT_FOUND = 0,
197     CMD_NULL,
198     CMD_ALLOCATIONS,
199     CMD_ARGS,
200     CMD_ARGUMENTS,
201     CMD_BREAKPOINT,
202     CMD_BUSINFO,
203     CMD_CALL,
204     CMD_DEBUG,
205     CMD_DISASSEMBLE,
206     CMD_DISASM,
207     CMD_DUMP,
208     CMD_EVALUATE,
209     CMD_EXECUTE,
210     CMD_EXIT,
211     CMD_FIND,
212     CMD_GO,
213     CMD_HANDLERS,
214     CMD_HELP,
215     CMD_HELP2,
216     CMD_HISTORY,
217     CMD_HISTORY_EXE,
218     CMD_HISTORY_LAST,
219     CMD_INFORMATION,
220     CMD_INTEGRITY,
221     CMD_INTO,
222     CMD_LEVEL,
223     CMD_LIST,
224     CMD_LOCALS,
225     CMD_LOCKS,
226     CMD_METHODS,
227     CMD_NAMESPACE,
228     CMD_NOTIFY,
229     CMD_OBJECTS,
230     CMD_OSI,
231     CMD_OWNER,
232     CMD_PATHS,
233     CMD_PREDEFINED,
234     CMD_PREFIX,
235     CMD_QUIT,
236     CMD_REFERENCES,
237     CMD_RESOURCES,
238     CMD_RESULTS,
239     CMD_SET,
240     CMD_STATS,
241     CMD_STOP,
242     CMD_TABLES,
243     CMD_TEMPLATE,
244     CMD_TRACE,
245     CMD_TREE,
246     CMD_TYPE,
247 #ifdef ACPI_APPLICATION
248     CMD_ENABLEACPI,
249     CMD_EVENT,
250     CMD_GPE,
251     CMD_GPES,
252     CMD_SCI,
253     CMD_SLEEP,
254 
255     CMD_CLOSE,
256     CMD_LOAD,
257     CMD_OPEN,
258     CMD_UNLOAD,
259 
260     CMD_TERMINATE,
261     CMD_THREADS,
262 
263     CMD_TEST,
264 #endif
265 };
266 
267 #define CMD_FIRST_VALID     2
268 
269 
270 /* Second parameter is the required argument count */
271 
272 static const ACPI_DB_COMMAND_INFO   AcpiGbl_DbCommands[] =
273 {
274     {"<NOT FOUND>",  0},
275     {"<NULL>",       0},
276     {"ALLOCATIONS",  0},
277     {"ARGS",         0},
278     {"ARGUMENTS",    0},
279     {"BREAKPOINT",   1},
280     {"BUSINFO",      0},
281     {"CALL",         0},
282     {"DEBUG",        1},
283     {"DISASSEMBLE",  1},
284     {"DISASM",       1},
285     {"DUMP",         1},
286     {"EVALUATE",     1},
287     {"EXECUTE",      1},
288     {"EXIT",         0},
289     {"FIND",         1},
290     {"GO",           0},
291     {"HANDLERS",     0},
292     {"HELP",         0},
293     {"?",            0},
294     {"HISTORY",      0},
295     {"!",            1},
296     {"!!",           0},
297     {"INFORMATION",  0},
298     {"INTEGRITY",    0},
299     {"INTO",         0},
300     {"LEVEL",        0},
301     {"LIST",         0},
302     {"LOCALS",       0},
303     {"LOCKS",        0},
304     {"METHODS",      0},
305     {"NAMESPACE",    0},
306     {"NOTIFY",       2},
307     {"OBJECTS",      0},
308     {"OSI",          0},
309     {"OWNER",        1},
310     {"PATHS",        0},
311     {"PREDEFINED",   0},
312     {"PREFIX",       0},
313     {"QUIT",         0},
314     {"REFERENCES",   1},
315     {"RESOURCES",    0},
316     {"RESULTS",      0},
317     {"SET",          3},
318     {"STATS",        1},
319     {"STOP",         0},
320     {"TABLES",       0},
321     {"TEMPLATE",     1},
322     {"TRACE",        1},
323     {"TREE",         0},
324     {"TYPE",         1},
325 #ifdef ACPI_APPLICATION
326     {"ENABLEACPI",   0},
327     {"EVENT",        1},
328     {"GPE",          1},
329     {"GPES",         0},
330     {"SCI",          0},
331     {"SLEEP",        0},
332 
333     {"CLOSE",        0},
334     {"LOAD",         1},
335     {"OPEN",         1},
336     {"UNLOAD",       1},
337 
338     {"TERMINATE",    0},
339     {"THREADS",      3},
340 
341     {"TEST",         1},
342 #endif
343     {NULL,           0}
344 };
345 
346 /*
347  * Help for all debugger commands. First argument is the number of lines
348  * of help to output for the command.
349  */
350 static const ACPI_DB_COMMAND_HELP   AcpiGbl_DbCommandHelp[] =
351 {
352     {0, "\nGeneral-Purpose Commands:",         "\n"},
353     {1, "  Allocations",                       "Display list of current memory allocations\n"},
354     {2, "  Dump <Address>|<Namepath>",         "\n"},
355     {0, "       [Byte|Word|Dword|Qword]",      "Display ACPI objects or memory\n"},
356     {1, "  Handlers",                          "Info about global handlers\n"},
357     {1, "  Help [Command]",                    "This help screen or individual command\n"},
358     {1, "  History",                           "Display command history buffer\n"},
359     {1, "  Level <DebugLevel>] [console]",     "Get/Set debug level for file or console\n"},
360     {1, "  Locks",                             "Current status of internal mutexes\n"},
361     {1, "  Osi [Install|Remove <name>]",       "Display or modify global _OSI list\n"},
362     {1, "  Quit or Exit",                      "Exit this command\n"},
363     {8, "  Stats <SubCommand>",                "Display namespace and memory statistics\n"},
364     {1, "     Allocations",                    "Display list of current memory allocations\n"},
365     {1, "     Memory",                         "Dump internal memory lists\n"},
366     {1, "     Misc",                           "Namespace search and mutex stats\n"},
367     {1, "     Objects",                        "Summary of namespace objects\n"},
368     {1, "     Sizes",                          "Sizes for each of the internal objects\n"},
369     {1, "     Stack",                          "Display CPU stack usage\n"},
370     {1, "     Tables",                         "Info about current ACPI table(s)\n"},
371     {1, "  Tables",                            "Display info about loaded ACPI tables\n"},
372     {1, "  ! <CommandNumber>",                 "Execute command from history buffer\n"},
373     {1, "  !!",                                "Execute last command again\n"},
374 
375     {0, "\nNamespace Access Commands:",        "\n"},
376     {1, "  Businfo",                           "Display system bus info\n"},
377     {1, "  Disassemble <Method>",              "Disassemble a control method\n"},
378     {1, "  Find <AcpiName> (? is wildcard)",   "Find ACPI name(s) with wildcards\n"},
379     {1, "  Integrity",                         "Validate namespace integrity\n"},
380     {1, "  Methods",                           "Display list of loaded control methods\n"},
381     {1, "  Namespace [Object] [Depth]",        "Display loaded namespace tree/subtree\n"},
382     {1, "  Notify <Object> <Value>",           "Send a notification on Object\n"},
383     {1, "  Objects [ObjectType]",              "Display summary of all objects or just given type\n"},
384     {1, "  Owner <OwnerId> [Depth]",           "Display loaded namespace by object owner\n"},
385     {1, "  Paths",                             "Display full pathnames of namespace objects\n"},
386     {1, "  Predefined",                        "Check all predefined names\n"},
387     {1, "  Prefix [<Namepath>]",               "Set or Get current execution prefix\n"},
388     {1, "  References <Addr>",                 "Find all references to object at addr\n"},
389     {1, "  Resources [DeviceName]",            "Display Device resources (no arg = all devices)\n"},
390     {1, "  Set N <NamedObject> <Value>",       "Set value for named integer\n"},
391     {1, "  Template <Object>",                 "Format/dump a Buffer/ResourceTemplate\n"},
392     {1, "  Type <Object>",                     "Display object type\n"},
393 
394     {0, "\nControl Method Execution Commands:","\n"},
395     {1, "  Arguments (or Args)",               "Display method arguments\n"},
396     {1, "  Breakpoint <AmlOffset>",            "Set an AML execution breakpoint\n"},
397     {1, "  Call",                              "Run to next control method invocation\n"},
398     {1, "  Debug <Namepath> [Arguments]",      "Single Step a control method\n"},
399     {6, "  Evaluate",                          "Synonym for Execute\n"},
400     {5, "  Execute <Namepath> [Arguments]",    "Execute control method\n"},
401     {1, "     Hex Integer",                    "Integer method argument\n"},
402     {1, "     \"Ascii String\"",               "String method argument\n"},
403     {1, "     (Hex Byte List)",                "Buffer method argument\n"},
404     {1, "     [Package Element List]",         "Package method argument\n"},
405     {5, "  Execute predefined",                "Execute all predefined (public) methods\n"},
406     {1, "  Go",                                "Allow method to run to completion\n"},
407     {1, "  Information",                       "Display info about the current method\n"},
408     {1, "  Into",                              "Step into (not over) a method call\n"},
409     {1, "  List [# of Aml Opcodes]",           "Display method ASL statements\n"},
410     {1, "  Locals",                            "Display method local variables\n"},
411     {1, "  Results",                           "Display method result stack\n"},
412     {1, "  Set <A|L> <#> <Value>",             "Set method data (Arguments/Locals)\n"},
413     {1, "  Stop",                              "Terminate control method\n"},
414     {5, "  Trace <State> [<Namepath>] [Once]", "Trace control method execution\n"},
415     {1, "     Enable",                         "Enable all messages\n"},
416     {1, "     Disable",                        "Disable tracing\n"},
417     {1, "     Method",                         "Enable method execution messages\n"},
418     {1, "     Opcode",                         "Enable opcode execution messages\n"},
419     {1, "  Tree",                              "Display control method calling tree\n"},
420     {1, "  <Enter>",                           "Single step next AML opcode (over calls)\n"},
421 
422 #ifdef ACPI_APPLICATION
423     {0, "\nHardware Simulation Commands:",         "\n"},
424     {1, "  EnableAcpi",                        "Enable ACPI (hardware) mode\n"},
425     {1, "  Event <F|G> <Value>",               "Generate AcpiEvent (Fixed/GPE)\n"},
426     {1, "  Gpe <GpeNum> [GpeBlockDevice]",     "Simulate a GPE\n"},
427     {1, "  Gpes",                              "Display info on all GPE devices\n"},
428     {1, "  Sci",                               "Generate an SCI\n"},
429     {1, "  Sleep [SleepState]",                "Simulate sleep/wake sequence(s) (0-5)\n"},
430 
431     {0, "\nFile I/O Commands:",                "\n"},
432     {1, "  Close",                             "Close debug output file\n"},
433     {1, "  Load <Input Filename>",             "Load ACPI table from a file\n"},
434     {1, "  Open <Output Filename>",            "Open a file for debug output\n"},
435     {1, "  Unload <Namepath>",                 "Unload an ACPI table via namespace object\n"},
436 
437     {0, "\nUser Space Commands:",              "\n"},
438     {1, "  Terminate",                         "Delete namespace and all internal objects\n"},
439     {1, "  Thread <Threads><Loops><NamePath>", "Spawn threads to execute method(s)\n"},
440 
441     {0, "\nDebug Test Commands:",              "\n"},
442     {3, "  Test <TestName>",                   "Invoke a debug test\n"},
443     {1, "     Objects",                        "Read/write/compare all namespace data objects\n"},
444     {1, "     Predefined",                     "Execute all ACPI predefined names (_STA, etc.)\n"},
445 #endif
446     {0, NULL, NULL}
447 };
448 
449 
450 /*******************************************************************************
451  *
452  * FUNCTION:    AcpiDbMatchCommandHelp
453  *
454  * PARAMETERS:  Command             - Command string to match
455  *              Help                - Help table entry to attempt match
456  *
457  * RETURN:      TRUE if command matched, FALSE otherwise
458  *
459  * DESCRIPTION: Attempt to match a command in the help table in order to
460  *              print help information for a single command.
461  *
462  ******************************************************************************/
463 
464 static BOOLEAN
465 AcpiDbMatchCommandHelp (
466     const char                  *Command,
467     const ACPI_DB_COMMAND_HELP  *Help)
468 {
469     char                    *Invocation = Help->Invocation;
470     UINT32                  LineCount;
471 
472 
473     /* Valid commands in the help table begin with a couple of spaces */
474 
475     if (*Invocation != ' ')
476     {
477         return (FALSE);
478     }
479 
480     while (*Invocation == ' ')
481     {
482         Invocation++;
483     }
484 
485     /* Match command name (full command or substring) */
486 
487     while ((*Command) && (*Invocation) && (*Invocation != ' '))
488     {
489         if (tolower ((int) *Command) != tolower ((int) *Invocation))
490         {
491             return (FALSE);
492         }
493 
494         Invocation++;
495         Command++;
496     }
497 
498     /* Print the appropriate number of help lines */
499 
500     LineCount = Help->LineCount;
501     while (LineCount)
502     {
503         AcpiOsPrintf ("%-38s : %s", Help->Invocation, Help->Description);
504         Help++;
505         LineCount--;
506     }
507 
508     return (TRUE);
509 }
510 
511 
512 /*******************************************************************************
513  *
514  * FUNCTION:    AcpiDbDisplayCommandInfo
515  *
516  * PARAMETERS:  Command             - Command string to match
517  *              DisplayAll          - Display all matching commands, or just
518  *                                    the first one (substring match)
519  *
520  * RETURN:      None
521  *
522  * DESCRIPTION: Display help information for a Debugger command.
523  *
524  ******************************************************************************/
525 
526 static void
527 AcpiDbDisplayCommandInfo (
528     const char              *Command,
529     BOOLEAN                 DisplayAll)
530 {
531     const ACPI_DB_COMMAND_HELP  *Next;
532     BOOLEAN                     Matched;
533 
534 
535     Next = AcpiGbl_DbCommandHelp;
536     while (Next->Invocation)
537     {
538         Matched = AcpiDbMatchCommandHelp (Command, Next);
539         if (!DisplayAll && Matched)
540         {
541             return;
542         }
543 
544         Next++;
545     }
546 }
547 
548 
549 /*******************************************************************************
550  *
551  * FUNCTION:    AcpiDbDisplayHelp
552  *
553  * PARAMETERS:  Command             - Optional command string to display help.
554  *                                    if not specified, all debugger command
555  *                                    help strings are displayed
556  *
557  * RETURN:      None
558  *
559  * DESCRIPTION: Display help for a single debugger command, or all of them.
560  *
561  ******************************************************************************/
562 
563 static void
564 AcpiDbDisplayHelp (
565     char                    *Command)
566 {
567     const ACPI_DB_COMMAND_HELP  *Next = AcpiGbl_DbCommandHelp;
568 
569 
570     if (!Command)
571     {
572         /* No argument to help, display help for all commands */
573 
574         while (Next->Invocation)
575         {
576             AcpiOsPrintf ("%-38s%s", Next->Invocation, Next->Description);
577             Next++;
578         }
579     }
580     else
581     {
582         /* Display help for all commands that match the subtring */
583 
584         AcpiDbDisplayCommandInfo (Command, TRUE);
585     }
586 }
587 
588 
589 /*******************************************************************************
590  *
591  * FUNCTION:    AcpiDbGetNextToken
592  *
593  * PARAMETERS:  String          - Command buffer
594  *              Next            - Return value, end of next token
595  *
596  * RETURN:      Pointer to the start of the next token.
597  *
598  * DESCRIPTION: Command line parsing. Get the next token on the command line
599  *
600  ******************************************************************************/
601 
602 char *
603 AcpiDbGetNextToken (
604     char                    *String,
605     char                    **Next,
606     ACPI_OBJECT_TYPE        *ReturnType)
607 {
608     char                    *Start;
609     UINT32                  Depth;
610     ACPI_OBJECT_TYPE        Type = ACPI_TYPE_INTEGER;
611 
612 
613     /* At end of buffer? */
614 
615     if (!String || !(*String))
616     {
617         return (NULL);
618     }
619 
620     /* Remove any spaces at the beginning */
621 
622     if (*String == ' ')
623     {
624         while (*String && (*String == ' '))
625         {
626             String++;
627         }
628 
629         if (!(*String))
630         {
631             return (NULL);
632         }
633     }
634 
635     switch (*String)
636     {
637     case '"':
638 
639         /* This is a quoted string, scan until closing quote */
640 
641         String++;
642         Start = String;
643         Type = ACPI_TYPE_STRING;
644 
645         /* Find end of string */
646 
647         while (*String && (*String != '"'))
648         {
649             String++;
650         }
651         break;
652 
653     case '(':
654 
655         /* This is the start of a buffer, scan until closing paren */
656 
657         String++;
658         Start = String;
659         Type = ACPI_TYPE_BUFFER;
660 
661         /* Find end of buffer */
662 
663         while (*String && (*String != ')'))
664         {
665             String++;
666         }
667         break;
668 
669     case '[':
670 
671         /* This is the start of a package, scan until closing bracket */
672 
673         String++;
674         Depth = 1;
675         Start = String;
676         Type = ACPI_TYPE_PACKAGE;
677 
678         /* Find end of package (closing bracket) */
679 
680         while (*String)
681         {
682             /* Handle String package elements */
683 
684             if (*String == '"')
685             {
686                 /* Find end of string */
687 
688                 String++;
689                 while (*String && (*String != '"'))
690                 {
691                     String++;
692                 }
693                 if (!(*String))
694                 {
695                     break;
696                 }
697             }
698             else if (*String == '[')
699             {
700                 Depth++;         /* A nested package declaration */
701             }
702             else if (*String == ']')
703             {
704                 Depth--;
705                 if (Depth == 0) /* Found final package closing bracket */
706                 {
707                     break;
708                 }
709             }
710 
711             String++;
712         }
713         break;
714 
715     default:
716 
717         Start = String;
718 
719         /* Find end of token */
720 
721         while (*String && (*String != ' '))
722         {
723             String++;
724         }
725         break;
726     }
727 
728     if (!(*String))
729     {
730         *Next = NULL;
731     }
732     else
733     {
734         *String = 0;
735         *Next = String + 1;
736     }
737 
738     *ReturnType = Type;
739     return (Start);
740 }
741 
742 
743 /*******************************************************************************
744  *
745  * FUNCTION:    AcpiDbGetLine
746  *
747  * PARAMETERS:  InputBuffer         - Command line buffer
748  *
749  * RETURN:      Count of arguments to the command
750  *
751  * DESCRIPTION: Get the next command line from the user. Gets entire line
752  *              up to the next newline
753  *
754  ******************************************************************************/
755 
756 static UINT32
757 AcpiDbGetLine (
758     char                    *InputBuffer)
759 {
760     UINT32                  i;
761     UINT32                  Count;
762     char                    *Next;
763     char                    *This;
764 
765 
766     if (AcpiUtSafeStrcpy (AcpiGbl_DbParsedBuf, sizeof (AcpiGbl_DbParsedBuf),
767         InputBuffer))
768     {
769         AcpiOsPrintf (
770             "Buffer overflow while parsing input line (max %u characters)\n",
771             sizeof (AcpiGbl_DbParsedBuf));
772         return (0);
773     }
774 
775     This = AcpiGbl_DbParsedBuf;
776     for (i = 0; i < ACPI_DEBUGGER_MAX_ARGS; i++)
777     {
778         AcpiGbl_DbArgs[i] = AcpiDbGetNextToken (This, &Next,
779             &AcpiGbl_DbArgTypes[i]);
780         if (!AcpiGbl_DbArgs[i])
781         {
782             break;
783         }
784 
785         This = Next;
786     }
787 
788     /* Uppercase the actual command */
789 
790     AcpiUtStrupr (AcpiGbl_DbArgs[0]);
791 
792     Count = i;
793     if (Count)
794     {
795         Count--;  /* Number of args only */
796     }
797 
798     return (Count);
799 }
800 
801 
802 /*******************************************************************************
803  *
804  * FUNCTION:    AcpiDbMatchCommand
805  *
806  * PARAMETERS:  UserCommand             - User command line
807  *
808  * RETURN:      Index into command array, -1 if not found
809  *
810  * DESCRIPTION: Search command array for a command match
811  *
812  ******************************************************************************/
813 
814 static UINT32
815 AcpiDbMatchCommand (
816     char                    *UserCommand)
817 {
818     UINT32                  i;
819 
820 
821     if (!UserCommand || UserCommand[0] == 0)
822     {
823         return (CMD_NULL);
824     }
825 
826     for (i = CMD_FIRST_VALID; AcpiGbl_DbCommands[i].Name; i++)
827     {
828         if (strstr (
829             ACPI_CAST_PTR (char, AcpiGbl_DbCommands[i].Name), UserCommand) ==
830             AcpiGbl_DbCommands[i].Name)
831         {
832             return (i);
833         }
834     }
835 
836     /* Command not recognized */
837 
838     return (CMD_NOT_FOUND);
839 }
840 
841 
842 /*******************************************************************************
843  *
844  * FUNCTION:    AcpiDbCommandDispatch
845  *
846  * PARAMETERS:  InputBuffer         - Command line buffer
847  *              WalkState           - Current walk
848  *              Op                  - Current (executing) parse op
849  *
850  * RETURN:      Status
851  *
852  * DESCRIPTION: Command dispatcher.
853  *
854  ******************************************************************************/
855 
856 ACPI_STATUS
857 AcpiDbCommandDispatch (
858     char                    *InputBuffer,
859     ACPI_WALK_STATE         *WalkState,
860     ACPI_PARSE_OBJECT       *Op)
861 {
862     UINT32                  Temp;
863     UINT32                  CommandIndex;
864     UINT32                  ParamCount;
865     char                    *CommandLine;
866     ACPI_STATUS             Status = AE_CTRL_TRUE;
867 
868 
869     /* If AcpiTerminate has been called, terminate this thread */
870 
871     if (AcpiGbl_DbTerminateLoop)
872     {
873         return (AE_CTRL_TERMINATE);
874     }
875 
876     /* Find command and add to the history buffer */
877 
878     ParamCount = AcpiDbGetLine (InputBuffer);
879     CommandIndex = AcpiDbMatchCommand (AcpiGbl_DbArgs[0]);
880     Temp = 0;
881 
882     /*
883      * We don't want to add the !! command to the history buffer. It
884      * would cause an infinite loop because it would always be the
885      * previous command.
886      */
887     if (CommandIndex != CMD_HISTORY_LAST)
888     {
889         AcpiDbAddToHistory (InputBuffer);
890     }
891 
892     /* Verify that we have the minimum number of params */
893 
894     if (ParamCount < AcpiGbl_DbCommands[CommandIndex].MinArgs)
895     {
896         AcpiOsPrintf ("%u parameters entered, [%s] requires %u parameters\n",
897             ParamCount, AcpiGbl_DbCommands[CommandIndex].Name,
898             AcpiGbl_DbCommands[CommandIndex].MinArgs);
899 
900         AcpiDbDisplayCommandInfo (
901             AcpiGbl_DbCommands[CommandIndex].Name, FALSE);
902         return (AE_CTRL_TRUE);
903     }
904 
905     /* Decode and dispatch the command */
906 
907     switch (CommandIndex)
908     {
909     case CMD_NULL:
910 
911         if (Op)
912         {
913             return (AE_OK);
914         }
915         break;
916 
917     case CMD_ALLOCATIONS:
918 
919 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
920         AcpiUtDumpAllocations ((UINT32) -1, NULL);
921 #endif
922         break;
923 
924     case CMD_ARGS:
925     case CMD_ARGUMENTS:
926 
927         AcpiDbDisplayArguments ();
928         break;
929 
930     case CMD_BREAKPOINT:
931 
932         AcpiDbSetMethodBreakpoint (AcpiGbl_DbArgs[1], WalkState, Op);
933         break;
934 
935     case CMD_BUSINFO:
936 
937         AcpiDbGetBusInfo ();
938         break;
939 
940     case CMD_CALL:
941 
942         AcpiDbSetMethodCallBreakpoint (Op);
943         Status = AE_OK;
944         break;
945 
946     case CMD_DEBUG:
947 
948         AcpiDbExecute (AcpiGbl_DbArgs[1],
949             &AcpiGbl_DbArgs[2], &AcpiGbl_DbArgTypes[2], EX_SINGLE_STEP);
950         break;
951 
952     case CMD_DISASSEMBLE:
953     case CMD_DISASM:
954 
955         (void) AcpiDbDisassembleMethod (AcpiGbl_DbArgs[1]);
956         break;
957 
958     case CMD_DUMP:
959 
960         AcpiDbDecodeAndDisplayObject (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
961         break;
962 
963     case CMD_EVALUATE:
964     case CMD_EXECUTE:
965 
966         AcpiDbExecute (AcpiGbl_DbArgs[1],
967             &AcpiGbl_DbArgs[2], &AcpiGbl_DbArgTypes[2], EX_NO_SINGLE_STEP);
968         break;
969 
970     case CMD_FIND:
971 
972         Status = AcpiDbFindNameInNamespace (AcpiGbl_DbArgs[1]);
973         break;
974 
975     case CMD_GO:
976 
977         AcpiGbl_CmSingleStep = FALSE;
978         return (AE_OK);
979 
980     case CMD_HANDLERS:
981 
982         AcpiDbDisplayHandlers ();
983         break;
984 
985     case CMD_HELP:
986     case CMD_HELP2:
987 
988         AcpiDbDisplayHelp (AcpiGbl_DbArgs[1]);
989         break;
990 
991     case CMD_HISTORY:
992 
993         AcpiDbDisplayHistory ();
994         break;
995 
996     case CMD_HISTORY_EXE: /* ! command */
997 
998         CommandLine = AcpiDbGetFromHistory (AcpiGbl_DbArgs[1]);
999         if (!CommandLine)
1000         {
1001             return (AE_CTRL_TRUE);
1002         }
1003 
1004         Status = AcpiDbCommandDispatch (CommandLine, WalkState, Op);
1005         return (Status);
1006 
1007     case CMD_HISTORY_LAST: /* !! command */
1008 
1009         CommandLine = AcpiDbGetFromHistory (NULL);
1010         if (!CommandLine)
1011         {
1012             return (AE_CTRL_TRUE);
1013         }
1014 
1015         Status = AcpiDbCommandDispatch (CommandLine, WalkState, Op);
1016         return (Status);
1017 
1018     case CMD_INFORMATION:
1019 
1020         AcpiDbDisplayMethodInfo (Op);
1021         break;
1022 
1023     case CMD_INTEGRITY:
1024 
1025         AcpiDbCheckIntegrity ();
1026         break;
1027 
1028     case CMD_INTO:
1029 
1030         if (Op)
1031         {
1032             AcpiGbl_CmSingleStep = TRUE;
1033             return (AE_OK);
1034         }
1035         break;
1036 
1037     case CMD_LEVEL:
1038 
1039         if (ParamCount == 0)
1040         {
1041             AcpiOsPrintf (
1042                 "Current debug level for file output is:    %8.8lX\n",
1043                 AcpiGbl_DbDebugLevel);
1044             AcpiOsPrintf (
1045                 "Current debug level for console output is: %8.8lX\n",
1046                 AcpiGbl_DbConsoleDebugLevel);
1047         }
1048         else if (ParamCount == 2)
1049         {
1050             Temp = AcpiGbl_DbConsoleDebugLevel;
1051             AcpiGbl_DbConsoleDebugLevel =
1052                 strtoul (AcpiGbl_DbArgs[1], NULL, 16);
1053             AcpiOsPrintf (
1054                 "Debug Level for console output was %8.8lX, now %8.8lX\n",
1055                 Temp, AcpiGbl_DbConsoleDebugLevel);
1056         }
1057         else
1058         {
1059             Temp = AcpiGbl_DbDebugLevel;
1060             AcpiGbl_DbDebugLevel = strtoul (AcpiGbl_DbArgs[1], NULL, 16);
1061             AcpiOsPrintf (
1062                 "Debug Level for file output was %8.8lX, now %8.8lX\n",
1063                 Temp, AcpiGbl_DbDebugLevel);
1064         }
1065         break;
1066 
1067     case CMD_LIST:
1068 
1069         AcpiDbDisassembleAml (AcpiGbl_DbArgs[1], Op);
1070         break;
1071 
1072     case CMD_LOCKS:
1073 
1074         AcpiDbDisplayLocks ();
1075         break;
1076 
1077     case CMD_LOCALS:
1078 
1079         AcpiDbDisplayLocals ();
1080         break;
1081 
1082     case CMD_METHODS:
1083 
1084         Status = AcpiDbDisplayObjects ("METHOD", AcpiGbl_DbArgs[1]);
1085         break;
1086 
1087     case CMD_NAMESPACE:
1088 
1089         AcpiDbDumpNamespace (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1090         break;
1091 
1092     case CMD_NOTIFY:
1093 
1094         Temp = strtoul (AcpiGbl_DbArgs[2], NULL, 0);
1095         AcpiDbSendNotify (AcpiGbl_DbArgs[1], Temp);
1096         break;
1097 
1098     case CMD_OBJECTS:
1099 
1100         AcpiUtStrupr (AcpiGbl_DbArgs[1]);
1101         Status = AcpiDbDisplayObjects (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1102         break;
1103 
1104     case CMD_OSI:
1105 
1106         AcpiDbDisplayInterfaces (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1107         break;
1108 
1109     case CMD_OWNER:
1110 
1111         AcpiDbDumpNamespaceByOwner (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1112         break;
1113 
1114     case CMD_PATHS:
1115 
1116         AcpiDbDumpNamespacePaths ();
1117         break;
1118 
1119     case CMD_PREFIX:
1120 
1121         AcpiDbSetScope (AcpiGbl_DbArgs[1]);
1122         break;
1123 
1124     case CMD_REFERENCES:
1125 
1126         AcpiDbFindReferences (AcpiGbl_DbArgs[1]);
1127         break;
1128 
1129     case CMD_RESOURCES:
1130 
1131         AcpiDbDisplayResources (AcpiGbl_DbArgs[1]);
1132         break;
1133 
1134     case CMD_RESULTS:
1135 
1136         AcpiDbDisplayResults ();
1137         break;
1138 
1139     case CMD_SET:
1140 
1141         AcpiDbSetMethodData (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2],
1142             AcpiGbl_DbArgs[3]);
1143         break;
1144 
1145     case CMD_STATS:
1146 
1147         Status = AcpiDbDisplayStatistics (AcpiGbl_DbArgs[1]);
1148         break;
1149 
1150     case CMD_STOP:
1151 
1152         return (AE_NOT_IMPLEMENTED);
1153 
1154     case CMD_TABLES:
1155 
1156         AcpiDbDisplayTableInfo (AcpiGbl_DbArgs[1]);
1157         break;
1158 
1159     case CMD_TEMPLATE:
1160 
1161         AcpiDbDisplayTemplate (AcpiGbl_DbArgs[1]);
1162         break;
1163 
1164     case CMD_TRACE:
1165 
1166         AcpiDbTrace (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2], AcpiGbl_DbArgs[3]);
1167         break;
1168 
1169     case CMD_TREE:
1170 
1171         AcpiDbDisplayCallingTree ();
1172         break;
1173 
1174     case CMD_TYPE:
1175 
1176         AcpiDbDisplayObjectType (AcpiGbl_DbArgs[1]);
1177         break;
1178 
1179 #ifdef ACPI_APPLICATION
1180 
1181     /* Hardware simulation commands. */
1182 
1183     case CMD_ENABLEACPI:
1184 #if (!ACPI_REDUCED_HARDWARE)
1185 
1186         Status = AcpiEnable();
1187         if (ACPI_FAILURE(Status))
1188         {
1189             AcpiOsPrintf("AcpiEnable failed (Status=%X)\n", Status);
1190             return (Status);
1191         }
1192 #endif /* !ACPI_REDUCED_HARDWARE */
1193         break;
1194 
1195     case CMD_EVENT:
1196 
1197         AcpiOsPrintf ("Event command not implemented\n");
1198         break;
1199 
1200     case CMD_GPE:
1201 
1202         AcpiDbGenerateGpe (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2]);
1203         break;
1204 
1205     case CMD_GPES:
1206 
1207         AcpiDbDisplayGpes ();
1208         break;
1209 
1210     case CMD_SCI:
1211 
1212         AcpiDbGenerateSci ();
1213         break;
1214 
1215     case CMD_SLEEP:
1216 
1217         Status = AcpiDbSleep (AcpiGbl_DbArgs[1]);
1218         break;
1219 
1220     /* File I/O commands. */
1221 
1222     case CMD_CLOSE:
1223 
1224         AcpiDbCloseDebugFile ();
1225         break;
1226 
1227     case CMD_LOAD:
1228         {
1229             ACPI_NEW_TABLE_DESC     *ListHead = NULL;
1230 
1231             Status = AcGetAllTablesFromFile (AcpiGbl_DbArgs[1],
1232                 ACPI_GET_ALL_TABLES, &ListHead);
1233             if (ACPI_SUCCESS (Status))
1234             {
1235                 AcpiDbLoadTables (ListHead);
1236             }
1237         }
1238         break;
1239 
1240     case CMD_OPEN:
1241 
1242         AcpiDbOpenDebugFile (AcpiGbl_DbArgs[1]);
1243         break;
1244 
1245     /* User space commands. */
1246 
1247     case CMD_TERMINATE:
1248 
1249         AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
1250         AcpiUtSubsystemShutdown ();
1251 
1252         /*
1253          * TBD: [Restructure] Need some way to re-initialize without
1254          * re-creating the semaphores!
1255          */
1256 
1257         AcpiGbl_DbTerminateLoop = TRUE;
1258         /*  AcpiInitialize (NULL);  */
1259         break;
1260 
1261     case CMD_THREADS:
1262 
1263         AcpiDbCreateExecutionThreads (AcpiGbl_DbArgs[1], AcpiGbl_DbArgs[2],
1264             AcpiGbl_DbArgs[3]);
1265         break;
1266 
1267     /* Debug test commands. */
1268 
1269     case CMD_PREDEFINED:
1270 
1271         AcpiDbCheckPredefinedNames ();
1272         break;
1273 
1274     case CMD_TEST:
1275 
1276         AcpiDbExecuteTest (AcpiGbl_DbArgs[1]);
1277         break;
1278 
1279     case CMD_UNLOAD:
1280 
1281         AcpiDbUnloadAcpiTable (AcpiGbl_DbArgs[1]);
1282         break;
1283 #endif
1284 
1285     case CMD_EXIT:
1286     case CMD_QUIT:
1287 
1288         if (Op)
1289         {
1290             AcpiOsPrintf ("Method execution terminated\n");
1291             return (AE_CTRL_TERMINATE);
1292         }
1293 
1294         if (!AcpiGbl_DbOutputToFile)
1295         {
1296             AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
1297         }
1298 
1299 #ifdef ACPI_APPLICATION
1300         AcpiDbCloseDebugFile ();
1301 #endif
1302         AcpiGbl_DbTerminateLoop = TRUE;
1303         return (AE_CTRL_TERMINATE);
1304 
1305     case CMD_NOT_FOUND:
1306     default:
1307 
1308         AcpiOsPrintf ("%s: unknown command\n", AcpiGbl_DbArgs[0]);
1309         return (AE_CTRL_TRUE);
1310     }
1311 
1312     if (ACPI_SUCCESS (Status))
1313     {
1314         Status = AE_CTRL_TRUE;
1315     }
1316 
1317     return (Status);
1318 }
1319 
1320 
1321 /*******************************************************************************
1322  *
1323  * FUNCTION:    AcpiDbExecuteThread
1324  *
1325  * PARAMETERS:  Context         - Not used
1326  *
1327  * RETURN:      None
1328  *
1329  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
1330  *              simply dispatches it.
1331  *
1332  ******************************************************************************/
1333 
1334 void ACPI_SYSTEM_XFACE
1335 AcpiDbExecuteThread (
1336     void                    *Context)
1337 {
1338 
1339     (void) AcpiDbUserCommands ();
1340     AcpiGbl_DbThreadsTerminated = TRUE;
1341 }
1342 
1343 
1344 /*******************************************************************************
1345  *
1346  * FUNCTION:    AcpiDbUserCommands
1347  *
1348  * PARAMETERS:  None
1349  *
1350  * RETURN:      None
1351  *
1352  * DESCRIPTION: Command line execution for the AML debugger. Commands are
1353  *              matched and dispatched here.
1354  *
1355  ******************************************************************************/
1356 
1357 ACPI_STATUS
1358 AcpiDbUserCommands (
1359     void)
1360 {
1361     ACPI_STATUS             Status = AE_OK;
1362 
1363 
1364     AcpiOsPrintf ("\n");
1365 
1366     /* TBD: [Restructure] Need a separate command line buffer for step mode */
1367 
1368     while (!AcpiGbl_DbTerminateLoop)
1369     {
1370         /* Wait the readiness of the command */
1371 
1372         Status = AcpiOsWaitCommandReady ();
1373         if (ACPI_FAILURE (Status))
1374         {
1375             break;
1376         }
1377 
1378         /* Just call to the command line interpreter */
1379 
1380         AcpiGbl_MethodExecuting = FALSE;
1381         AcpiGbl_StepToNextCall = FALSE;
1382 
1383         (void) AcpiDbCommandDispatch (AcpiGbl_DbLineBuf, NULL, NULL);
1384 
1385         /* Notify the completion of the command */
1386 
1387         Status = AcpiOsNotifyCommandComplete ();
1388         if (ACPI_FAILURE (Status))
1389         {
1390             break;
1391         }
1392     }
1393 
1394     if (ACPI_FAILURE (Status) && Status != AE_CTRL_TERMINATE)
1395     {
1396         ACPI_EXCEPTION ((AE_INFO, Status, "While parsing command line"));
1397     }
1398     return (Status);
1399 }
1400