Sync ACPICA with Intel's version 20140828.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / debugger / dbhistry.c
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 #include "acpi.h"
45 #include "accommon.h"
46 #include "acdebug.h"
47
48 #ifdef ACPI_DEBUGGER
49
50 #define _COMPONENT          ACPI_CA_DEBUGGER
51         ACPI_MODULE_NAME    ("dbhistry")
52
53
54 #define HI_NO_HISTORY       0
55 #define HI_RECORD_HISTORY   1
56 #define HISTORY_SIZE        40
57
58
59 typedef struct HistoryInfo
60 {
61     char                    *Command;
62     UINT32                  CmdNum;
63
64 } HISTORY_INFO;
65
66
67 static HISTORY_INFO         AcpiGbl_HistoryBuffer[HISTORY_SIZE];
68 static UINT16               AcpiGbl_LoHistory = 0;
69 static UINT16               AcpiGbl_NumHistory = 0;
70 static UINT16               AcpiGbl_NextHistoryIndex = 0;
71 UINT32                      AcpiGbl_NextCmdNum = 1;
72
73
74 /*******************************************************************************
75  *
76  * FUNCTION:    AcpiDbAddToHistory
77  *
78  * PARAMETERS:  CommandLine     - Command to add
79  *
80  * RETURN:      None
81  *
82  * DESCRIPTION: Add a command line to the history buffer.
83  *
84  ******************************************************************************/
85
86 void
87 AcpiDbAddToHistory (
88     char                    *CommandLine)
89 {
90     UINT16                  CmdLen;
91     UINT16                  BufferLen;
92
93     /* Put command into the next available slot */
94
95     CmdLen = (UINT16) ACPI_STRLEN (CommandLine);
96     if (!CmdLen)
97     {
98         return;
99     }
100
101     if (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command != NULL)
102     {
103         BufferLen = (UINT16) ACPI_STRLEN (
104             AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command);
105         if (CmdLen > BufferLen)
106         {
107             AcpiOsFree (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].
108                 Command);
109             AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
110                 AcpiOsAllocate (CmdLen + 1);
111         }
112     }
113     else
114     {
115         AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command =
116             AcpiOsAllocate (CmdLen + 1);
117     }
118
119     ACPI_STRCPY (AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].Command,
120         CommandLine);
121
122     AcpiGbl_HistoryBuffer[AcpiGbl_NextHistoryIndex].CmdNum =
123         AcpiGbl_NextCmdNum;
124
125     /* Adjust indexes */
126
127     if ((AcpiGbl_NumHistory == HISTORY_SIZE) &&
128         (AcpiGbl_NextHistoryIndex == AcpiGbl_LoHistory))
129     {
130         AcpiGbl_LoHistory++;
131         if (AcpiGbl_LoHistory >= HISTORY_SIZE)
132         {
133             AcpiGbl_LoHistory = 0;
134         }
135     }
136
137     AcpiGbl_NextHistoryIndex++;
138     if (AcpiGbl_NextHistoryIndex >= HISTORY_SIZE)
139     {
140         AcpiGbl_NextHistoryIndex = 0;
141     }
142
143     AcpiGbl_NextCmdNum++;
144     if (AcpiGbl_NumHistory < HISTORY_SIZE)
145     {
146         AcpiGbl_NumHistory++;
147     }
148 }
149
150
151 /*******************************************************************************
152  *
153  * FUNCTION:    AcpiDbDisplayHistory
154  *
155  * PARAMETERS:  None
156  *
157  * RETURN:      None
158  *
159  * DESCRIPTION: Display the contents of the history buffer
160  *
161  ******************************************************************************/
162
163 void
164 AcpiDbDisplayHistory (
165     void)
166 {
167     UINT32                  i;
168     UINT16                  HistoryIndex;
169
170
171     HistoryIndex = AcpiGbl_LoHistory;
172
173     /* Dump entire history buffer */
174
175     for (i = 0; i < AcpiGbl_NumHistory; i++)
176     {
177         if (AcpiGbl_HistoryBuffer[HistoryIndex].Command)
178         {
179             AcpiOsPrintf ("%3ld  %s\n",
180                 AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum,
181                 AcpiGbl_HistoryBuffer[HistoryIndex].Command);
182         }
183
184         HistoryIndex++;
185         if (HistoryIndex >= HISTORY_SIZE)
186         {
187             HistoryIndex = 0;
188         }
189     }
190 }
191
192
193 /*******************************************************************************
194  *
195  * FUNCTION:    AcpiDbGetFromHistory
196  *
197  * PARAMETERS:  CommandNumArg           - String containing the number of the
198  *                                        command to be retrieved
199  *
200  * RETURN:      Pointer to the retrieved command. Null on error.
201  *
202  * DESCRIPTION: Get a command from the history buffer
203  *
204  ******************************************************************************/
205
206 char *
207 AcpiDbGetFromHistory (
208     char                    *CommandNumArg)
209 {
210     UINT32                  CmdNum;
211
212
213     if (CommandNumArg == NULL)
214     {
215         CmdNum = AcpiGbl_NextCmdNum - 1;
216     }
217
218     else
219     {
220         CmdNum = ACPI_STRTOUL (CommandNumArg, NULL, 0);
221     }
222
223     return (AcpiDbGetHistoryByIndex (CmdNum));
224 }
225
226
227 /*******************************************************************************
228  *
229  * FUNCTION:    AcpiDbGetHistoryByIndex
230  *
231  * PARAMETERS:  CmdNum              - Index of the desired history entry.
232  *                                    Values are 0...(AcpiGbl_NextCmdNum - 1)
233  *
234  * RETURN:      Pointer to the retrieved command. Null on error.
235  *
236  * DESCRIPTION: Get a command from the history buffer
237  *
238  ******************************************************************************/
239
240 char *
241 AcpiDbGetHistoryByIndex (
242     UINT32                  CmdNum)
243 {
244     UINT32                  i;
245     UINT16                  HistoryIndex;
246
247
248     /* Search history buffer */
249
250     HistoryIndex = AcpiGbl_LoHistory;
251     for (i = 0; i < AcpiGbl_NumHistory; i++)
252     {
253         if (AcpiGbl_HistoryBuffer[HistoryIndex].CmdNum == CmdNum)
254         {
255             /* Found the command, return it */
256
257             return (AcpiGbl_HistoryBuffer[HistoryIndex].Command);
258         }
259
260         /* History buffer is circular */
261
262         HistoryIndex++;
263         if (HistoryIndex >= HISTORY_SIZE)
264         {
265             HistoryIndex = 0;
266         }
267     }
268
269     AcpiOsPrintf ("Invalid history number: %u\n", HistoryIndex);
270     return (NULL);
271 }
272
273 #endif /* ACPI_DEBUGGER */