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