167754Smsmith/******************************************************************************
267754Smsmith *
367754Smsmith * Module Name: dbhistry - debugger HISTORY command
467754Smsmith *
567754Smsmith *****************************************************************************/
667754Smsmith
7217365Sjkim/*
8306536Sjkim * Copyright (C) 2000 - 2016, Intel Corp.
970243Smsmith * All rights reserved.
1067754Smsmith *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
2567754Smsmith *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
2967754Smsmith *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
4367754Smsmith
44193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
45193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
46193341Sjkim#include <contrib/dev/acpica/include/acdebug.h>
4767754Smsmith
4867754Smsmith
49102550Siwasaki#define _COMPONENT          ACPI_CA_DEBUGGER
5091116Smsmith        ACPI_MODULE_NAME    ("dbhistry")
5167754Smsmith
5267754Smsmith
5367754Smsmith#define HI_NO_HISTORY       0
5467754Smsmith#define HI_RECORD_HISTORY   1
55250838Sjkim#define HISTORY_SIZE        40
5667754Smsmith
5767754Smsmith
5867754Smsmithtypedef struct HistoryInfo
5967754Smsmith{
60250838Sjkim    char                    *Command;
6167754Smsmith    UINT32                  CmdNum;
6267754Smsmith
6367754Smsmith} HISTORY_INFO;
6467754Smsmith
6567754Smsmith
6699679Siwasakistatic HISTORY_INFO         AcpiGbl_HistoryBuffer[HISTORY_SIZE];
6799679Siwasakistatic UINT16               AcpiGbl_LoHistory = 0;
6899679Siwasakistatic UINT16               AcpiGbl_NumHistory = 0;
6999679Siwasakistatic UINT16               AcpiGbl_NextHistoryIndex = 0;
70281075SdimUINT32                      AcpiGbl_NextCmdNum = 1;
7167754Smsmith
7267754Smsmith
7367754Smsmith/*******************************************************************************
7467754Smsmith *
7567754Smsmith * FUNCTION:    AcpiDbAddToHistory
7667754Smsmith *
7767754Smsmith * PARAMETERS:  CommandLine     - Command to add
7867754Smsmith *
7967754Smsmith * RETURN:      None
8067754Smsmith *
8167754Smsmith * DESCRIPTION: Add a command line to the history buffer.
8267754Smsmith *
8367754Smsmith ******************************************************************************/
8467754Smsmith
8567754Smsmithvoid
8667754SmsmithAcpiDbAddToHistory (
87114237Snjl    char                    *CommandLine)
8867754Smsmith{
89250838Sjkim    UINT16                  CmdLen;
90250838Sjkim    UINT16                  BufferLen;
9167754Smsmith
9267754Smsmith    /* Put command into the next available slot */
9367754Smsmith
94306536Sjkim    CmdLen = (UINT16) strlen (CommandLine);
95281075Sdim    if (!CmdLen)
96281075Sdim    {
97281075Sdim        return;
98281075Sdim    }
99281075Sdim
100250838Sjkim    if (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command != NULL)
101250838Sjkim    {
102306536Sjkim        BufferLen = (UINT16) strlen (
103250838Sjkim            AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command);
104306536Sjkim
105250838Sjkim        if (CmdLen > BufferLen)
106250838Sjkim        {
107250838Sjkim            AcpiOsFree (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].
108250838Sjkim                Command);
109250838Sjkim            AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
110250838Sjkim                AcpiOsAllocate (CmdLen + 1);
111250838Sjkim        }
112250838Sjkim    }
113250838Sjkim    else
114250838Sjkim    {
115250838Sjkim        AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
116250838Sjkim            AcpiOsAllocate (CmdLen + 1);
117250838Sjkim    }
118250838Sjkim
119306536Sjkim    strcpy (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command,
120151937Sjkim        CommandLine);
12167754Smsmith
122250838Sjkim    AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].CmdNum =
123250838Sjkim        AcpiGbl_NextCmdNum;
12483174Smsmith
12567754Smsmith    /* Adjust indexes */
12667754Smsmith
12783174Smsmith    if ((AcpiGbl_NumHistory == HISTORY_SIZE) &&
12883174Smsmith        (AcpiGbl_NextHistoryIndex == AcpiGbl_LoHistory))
12967754Smsmith    {
13083174Smsmith        AcpiGbl_LoHistory++;
13183174Smsmith        if (AcpiGbl_LoHistory >= HISTORY_SIZE)
13267754Smsmith        {
13383174Smsmith            AcpiGbl_LoHistory = 0;
13467754Smsmith        }
13567754Smsmith    }
13667754Smsmith
13783174Smsmith    AcpiGbl_NextHistoryIndex++;
13883174Smsmith    if (AcpiGbl_NextHistoryIndex >= HISTORY_SIZE)
13967754Smsmith    {
14083174Smsmith        AcpiGbl_NextHistoryIndex = 0;
14167754Smsmith    }
14267754Smsmith
14383174Smsmith    AcpiGbl_NextCmdNum++;
14483174Smsmith    if (AcpiGbl_NumHistory < HISTORY_SIZE)
14567754Smsmith    {
14683174Smsmith        AcpiGbl_NumHistory++;
14767754Smsmith    }
14867754Smsmith}
14967754Smsmith
15067754Smsmith
15167754Smsmith/*******************************************************************************
15267754Smsmith *
15367754Smsmith * FUNCTION:    AcpiDbDisplayHistory
15467754Smsmith *
15567754Smsmith * PARAMETERS:  None
15667754Smsmith *
15767754Smsmith * RETURN:      None
15867754Smsmith *
15967754Smsmith * DESCRIPTION: Display the contents of the history buffer
16067754Smsmith *
16167754Smsmith ******************************************************************************/
16267754Smsmith
16367754Smsmithvoid
164151937SjkimAcpiDbDisplayHistory (
165151937Sjkim    void)
16667754Smsmith{
167193267Sjkim    UINT32                  i;
16867754Smsmith    UINT16                  HistoryIndex;
16967754Smsmith
17067754Smsmith
17183174Smsmith    HistoryIndex = AcpiGbl_LoHistory;
17267754Smsmith
17367754Smsmith    /* Dump entire history buffer */
17467754Smsmith
17583174Smsmith    for (i = 0; i < AcpiGbl_NumHistory; i++)
17667754Smsmith    {
177250838Sjkim        if (AcpiGbl_HistoryBuffer[HistoryIndex].Command)
178250838Sjkim        {
179250838Sjkim            AcpiOsPrintf ("%3ld  %s\n",
180250838Sjkim                AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum,
181250838Sjkim                AcpiGbl_HistoryBuffer[HistoryIndex].Command);
182250838Sjkim        }
18367754Smsmith
18467754Smsmith        HistoryIndex++;
18567754Smsmith        if (HistoryIndex >= HISTORY_SIZE)
18667754Smsmith        {
18767754Smsmith            HistoryIndex = 0;
18867754Smsmith        }
18967754Smsmith    }
19067754Smsmith}
19167754Smsmith
19267754Smsmith
19367754Smsmith/*******************************************************************************
19467754Smsmith *
19567754Smsmith * FUNCTION:    AcpiDbGetFromHistory
19667754Smsmith *
19767754Smsmith * PARAMETERS:  CommandNumArg           - String containing the number of the
19867754Smsmith *                                        command to be retrieved
19967754Smsmith *
200151937Sjkim * RETURN:      Pointer to the retrieved command. Null on error.
20167754Smsmith *
20267754Smsmith * DESCRIPTION: Get a command from the history buffer
20367754Smsmith *
20467754Smsmith ******************************************************************************/
20567754Smsmith
206114237Snjlchar *
20767754SmsmithAcpiDbGetFromHistory (
208114237Snjl    char                    *CommandNumArg)
20967754Smsmith{
21067754Smsmith    UINT32                  CmdNum;
21167754Smsmith
21267754Smsmith
21367754Smsmith    if (CommandNumArg == NULL)
21467754Smsmith    {
21583174Smsmith        CmdNum = AcpiGbl_NextCmdNum - 1;
21667754Smsmith    }
21767754Smsmith
21867754Smsmith    else
21967754Smsmith    {
220306536Sjkim        CmdNum = strtoul (CommandNumArg, NULL, 0);
22167754Smsmith    }
22267754Smsmith
223281075Sdim    return (AcpiDbGetHistoryByIndex (CmdNum));
224281075Sdim}
225281075Sdim
226281075Sdim
227281075Sdim/*******************************************************************************
228281075Sdim *
229281075Sdim * FUNCTION:    AcpiDbGetHistoryByIndex
230281075Sdim *
231281075Sdim * PARAMETERS:  CmdNum              - Index of the desired history entry.
232281075Sdim *                                    Values are 0...(AcpiGbl_NextCmdNum - 1)
233281075Sdim *
234281075Sdim * RETURN:      Pointer to the retrieved command. Null on error.
235281075Sdim *
236281075Sdim * DESCRIPTION: Get a command from the history buffer
237281075Sdim *
238281075Sdim ******************************************************************************/
239281075Sdim
240281075Sdimchar *
241281075SdimAcpiDbGetHistoryByIndex (
242281075Sdim    UINT32                  CmdNum)
243281075Sdim{
244281075Sdim    UINT32                  i;
245281075Sdim    UINT16                  HistoryIndex;
246281075Sdim
247281075Sdim
24867754Smsmith    /* Search history buffer */
24967754Smsmith
25083174Smsmith    HistoryIndex = AcpiGbl_LoHistory;
25183174Smsmith    for (i = 0; i < AcpiGbl_NumHistory; i++)
25267754Smsmith    {
25383174Smsmith        if (AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum == CmdNum)
25467754Smsmith        {
255250838Sjkim            /* Found the command, return it */
25667754Smsmith
25783174Smsmith            return (AcpiGbl_HistoryBuffer[HistoryIndex].Command);
25867754Smsmith        }
25967754Smsmith
260281075Sdim        /* History buffer is circular */
26167754Smsmith
26267754Smsmith        HistoryIndex++;
26367754Smsmith        if (HistoryIndex >= HISTORY_SIZE)
26467754Smsmith        {
26567754Smsmith            HistoryIndex = 0;
26667754Smsmith        }
26767754Smsmith    }
26867754Smsmith
269209746Sjkim    AcpiOsPrintf ("Invalid history number: %u\n", HistoryIndex);
27067754Smsmith    return (NULL);
27167754Smsmith}
272