Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / acpi / acpica / dbhistry.c
blobf5fba14461a6b3efb554b885fea98eb705b65a42
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: dbhistry - debugger HISTORY command
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acdebug.h"
14 #define _COMPONENT ACPI_CA_DEBUGGER
15 ACPI_MODULE_NAME("dbhistry")
17 #define HI_NO_HISTORY 0
18 #define HI_RECORD_HISTORY 1
19 #define HISTORY_SIZE 40
20 typedef struct history_info {
21 char *command;
22 u32 cmd_num;
24 } HISTORY_INFO;
26 static HISTORY_INFO acpi_gbl_history_buffer[HISTORY_SIZE];
27 static u16 acpi_gbl_lo_history = 0;
28 static u16 acpi_gbl_num_history = 0;
29 static u16 acpi_gbl_next_history_index = 0;
31 /*******************************************************************************
33 * FUNCTION: acpi_db_add_to_history
35 * PARAMETERS: command_line - Command to add
37 * RETURN: None
39 * DESCRIPTION: Add a command line to the history buffer.
41 ******************************************************************************/
43 void acpi_db_add_to_history(char *command_line)
45 u16 cmd_len;
46 u16 buffer_len;
48 /* Put command into the next available slot */
50 cmd_len = (u16)strlen(command_line);
51 if (!cmd_len) {
52 return;
55 if (acpi_gbl_history_buffer[acpi_gbl_next_history_index].command !=
56 NULL) {
57 buffer_len =
58 (u16)
59 strlen(acpi_gbl_history_buffer[acpi_gbl_next_history_index].
60 command);
62 if (cmd_len > buffer_len) {
63 acpi_os_free(acpi_gbl_history_buffer
64 [acpi_gbl_next_history_index].command);
65 acpi_gbl_history_buffer[acpi_gbl_next_history_index].
66 command = acpi_os_allocate(cmd_len + 1);
68 } else {
69 acpi_gbl_history_buffer[acpi_gbl_next_history_index].command =
70 acpi_os_allocate(cmd_len + 1);
73 strcpy(acpi_gbl_history_buffer[acpi_gbl_next_history_index].command,
74 command_line);
76 acpi_gbl_history_buffer[acpi_gbl_next_history_index].cmd_num =
77 acpi_gbl_next_cmd_num;
79 /* Adjust indexes */
81 if ((acpi_gbl_num_history == HISTORY_SIZE) &&
82 (acpi_gbl_next_history_index == acpi_gbl_lo_history)) {
83 acpi_gbl_lo_history++;
84 if (acpi_gbl_lo_history >= HISTORY_SIZE) {
85 acpi_gbl_lo_history = 0;
89 acpi_gbl_next_history_index++;
90 if (acpi_gbl_next_history_index >= HISTORY_SIZE) {
91 acpi_gbl_next_history_index = 0;
94 acpi_gbl_next_cmd_num++;
95 if (acpi_gbl_num_history < HISTORY_SIZE) {
96 acpi_gbl_num_history++;
100 /*******************************************************************************
102 * FUNCTION: acpi_db_display_history
104 * PARAMETERS: None
106 * RETURN: None
108 * DESCRIPTION: Display the contents of the history buffer
110 ******************************************************************************/
112 void acpi_db_display_history(void)
114 u32 i;
115 u16 history_index;
117 history_index = acpi_gbl_lo_history;
119 /* Dump entire history buffer */
121 for (i = 0; i < acpi_gbl_num_history; i++) {
122 if (acpi_gbl_history_buffer[history_index].command) {
123 acpi_os_printf("%3u %s\n",
124 acpi_gbl_history_buffer[history_index].
125 cmd_num,
126 acpi_gbl_history_buffer[history_index].
127 command);
130 history_index++;
131 if (history_index >= HISTORY_SIZE) {
132 history_index = 0;
137 /*******************************************************************************
139 * FUNCTION: acpi_db_get_from_history
141 * PARAMETERS: command_num_arg - String containing the number of the
142 * command to be retrieved
144 * RETURN: Pointer to the retrieved command. Null on error.
146 * DESCRIPTION: Get a command from the history buffer
148 ******************************************************************************/
150 char *acpi_db_get_from_history(char *command_num_arg)
152 u32 cmd_num;
154 if (command_num_arg == NULL) {
155 cmd_num = acpi_gbl_next_cmd_num - 1;
158 else {
159 cmd_num = strtoul(command_num_arg, NULL, 0);
162 return (acpi_db_get_history_by_index(cmd_num));
165 /*******************************************************************************
167 * FUNCTION: acpi_db_get_history_by_index
169 * PARAMETERS: cmd_num - Index of the desired history entry.
170 * Values are 0...(acpi_gbl_next_cmd_num - 1)
172 * RETURN: Pointer to the retrieved command. Null on error.
174 * DESCRIPTION: Get a command from the history buffer
176 ******************************************************************************/
178 char *acpi_db_get_history_by_index(u32 cmd_num)
180 u32 i;
181 u16 history_index;
183 /* Search history buffer */
185 history_index = acpi_gbl_lo_history;
186 for (i = 0; i < acpi_gbl_num_history; i++) {
187 if (acpi_gbl_history_buffer[history_index].cmd_num == cmd_num) {
189 /* Found the command, return it */
191 return (acpi_gbl_history_buffer[history_index].command);
194 /* History buffer is circular */
196 history_index++;
197 if (history_index >= HISTORY_SIZE) {
198 history_index = 0;
202 acpi_os_printf("Invalid history number: %u\n", history_index);
203 return (NULL);