1/******************************************************************************
2 *
3 * Module Name: dbhistry - debugger HISTORY command
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions, and the following disclaimer,
16 *    without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 *    substantially similar to the "NO WARRANTY" disclaimer below
19 *    ("Disclaimer") and any redistribution must be conditioned upon
20 *    including a substantially similar Disclaimer requirement for further
21 *    binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 *    of any contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44
45#include <contrib/dev/acpica/include/acpi.h>
46#include <contrib/dev/acpica/include/accommon.h>
47#include <contrib/dev/acpica/include/acdebug.h>
48
49#ifdef ACPI_DEBUGGER
50
51#define _COMPONENT          ACPI_CA_DEBUGGER
52        ACPI_MODULE_NAME    ("dbhistry")
53
54
55#define HI_NO_HISTORY       0
56#define HI_RECORD_HISTORY   1
57#define HISTORY_SIZE        40
58
59
60typedef struct HistoryInfo
61{
62    char                    *Command;
63    UINT32                  CmdNum;
64
65} HISTORY_INFO;
66
67
68static HISTORY_INFO         AcpiGbl_HistoryBuffer[HISTORY_SIZE];
69static UINT16               AcpiGbl_LoHistory = 0;
70static UINT16               AcpiGbl_NumHistory = 0;
71static UINT16               AcpiGbl_NextHistoryIndex = 0;
72static UINT32               AcpiGbl_NextCmdNum = 1;
73
74
75/*******************************************************************************
76 *
77 * FUNCTION:    AcpiDbAddToHistory
78 *
79 * PARAMETERS:  CommandLine     - Command to add
80 *
81 * RETURN:      None
82 *
83 * DESCRIPTION: Add a command line to the history buffer.
84 *
85 ******************************************************************************/
86
87void
88AcpiDbAddToHistory (
89    char                    *CommandLine)
90{
91    UINT16                  CmdLen;
92    UINT16                  BufferLen;
93
94    /* Put command into the next available slot */
95
96    CmdLen = (UINT16) ACPI_STRLEN (CommandLine);
97    if (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command != NULL)
98    {
99        BufferLen = (UINT16) ACPI_STRLEN (
100            AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command);
101        if (CmdLen > BufferLen)
102        {
103            AcpiOsFree (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].
104                Command);
105            AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
106                AcpiOsAllocate (CmdLen + 1);
107        }
108    }
109    else
110    {
111        AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
112            AcpiOsAllocate (CmdLen + 1);
113    }
114
115    ACPI_STRCPY (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command,
116        CommandLine);
117
118    AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].CmdNum =
119        AcpiGbl_NextCmdNum;
120
121    /* Adjust indexes */
122
123    if ((AcpiGbl_NumHistory == HISTORY_SIZE) &&
124        (AcpiGbl_NextHistoryIndex == AcpiGbl_LoHistory))
125    {
126        AcpiGbl_LoHistory++;
127        if (AcpiGbl_LoHistory >= HISTORY_SIZE)
128        {
129            AcpiGbl_LoHistory = 0;
130        }
131    }
132
133    AcpiGbl_NextHistoryIndex++;
134    if (AcpiGbl_NextHistoryIndex >= HISTORY_SIZE)
135    {
136        AcpiGbl_NextHistoryIndex = 0;
137    }
138
139    AcpiGbl_NextCmdNum++;
140    if (AcpiGbl_NumHistory < HISTORY_SIZE)
141    {
142        AcpiGbl_NumHistory++;
143    }
144}
145
146
147/*******************************************************************************
148 *
149 * FUNCTION:    AcpiDbDisplayHistory
150 *
151 * PARAMETERS:  None
152 *
153 * RETURN:      None
154 *
155 * DESCRIPTION: Display the contents of the history buffer
156 *
157 ******************************************************************************/
158
159void
160AcpiDbDisplayHistory (
161    void)
162{
163    UINT32                  i;
164    UINT16                  HistoryIndex;
165
166
167    HistoryIndex = AcpiGbl_LoHistory;
168
169    /* Dump entire history buffer */
170
171    for (i = 0; i < AcpiGbl_NumHistory; i++)
172    {
173        if (AcpiGbl_HistoryBuffer[HistoryIndex].Command)
174        {
175            AcpiOsPrintf ("%3ld  %s\n",
176                AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum,
177                AcpiGbl_HistoryBuffer[HistoryIndex].Command);
178        }
179
180        HistoryIndex++;
181        if (HistoryIndex >= HISTORY_SIZE)
182        {
183            HistoryIndex = 0;
184        }
185    }
186}
187
188
189/*******************************************************************************
190 *
191 * FUNCTION:    AcpiDbGetFromHistory
192 *
193 * PARAMETERS:  CommandNumArg           - String containing the number of the
194 *                                        command to be retrieved
195 *
196 * RETURN:      Pointer to the retrieved command. Null on error.
197 *
198 * DESCRIPTION: Get a command from the history buffer
199 *
200 ******************************************************************************/
201
202char *
203AcpiDbGetFromHistory (
204    char                    *CommandNumArg)
205{
206    UINT32                  i;
207    UINT16                  HistoryIndex;
208    UINT32                  CmdNum;
209
210
211    if (CommandNumArg == NULL)
212    {
213        CmdNum = AcpiGbl_NextCmdNum - 1;
214    }
215
216    else
217    {
218        CmdNum = ACPI_STRTOUL (CommandNumArg, NULL, 0);
219    }
220
221    /* Search history buffer */
222
223    HistoryIndex = AcpiGbl_LoHistory;
224    for (i = 0; i < AcpiGbl_NumHistory; i++)
225    {
226        if (AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum == CmdNum)
227        {
228            /* Found the command, return it */
229
230            return (AcpiGbl_HistoryBuffer[HistoryIndex].Command);
231        }
232
233
234        HistoryIndex++;
235        if (HistoryIndex >= HISTORY_SIZE)
236        {
237            HistoryIndex = 0;
238        }
239    }
240
241    AcpiOsPrintf ("Invalid history number: %u\n", HistoryIndex);
242    return (NULL);
243}
244
245#endif /* ACPI_DEBUGGER */
246