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