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