dbhistry.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: dbhistry - debugger HISTORY command
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acdebug.h"
  12. #define _COMPONENT ACPI_CA_DEBUGGER
  13. ACPI_MODULE_NAME("dbhistry")
  14. #define HI_NO_HISTORY 0
  15. #define HI_RECORD_HISTORY 1
  16. #define HISTORY_SIZE 40
  17. typedef struct history_info {
  18. char *command;
  19. u32 cmd_num;
  20. } HISTORY_INFO;
  21. static HISTORY_INFO acpi_gbl_history_buffer[HISTORY_SIZE];
  22. static u16 acpi_gbl_lo_history = 0;
  23. static u16 acpi_gbl_num_history = 0;
  24. static u16 acpi_gbl_next_history_index = 0;
  25. /*******************************************************************************
  26. *
  27. * FUNCTION: acpi_db_add_to_history
  28. *
  29. * PARAMETERS: command_line - Command to add
  30. *
  31. * RETURN: None
  32. *
  33. * DESCRIPTION: Add a command line to the history buffer.
  34. *
  35. ******************************************************************************/
  36. void acpi_db_add_to_history(char *command_line)
  37. {
  38. u16 cmd_len;
  39. u16 buffer_len;
  40. /* Put command into the next available slot */
  41. cmd_len = (u16)strlen(command_line);
  42. if (!cmd_len) {
  43. return;
  44. }
  45. if (acpi_gbl_history_buffer[acpi_gbl_next_history_index].command !=
  46. NULL) {
  47. buffer_len =
  48. (u16)
  49. strlen(acpi_gbl_history_buffer[acpi_gbl_next_history_index].
  50. command);
  51. if (cmd_len > buffer_len) {
  52. acpi_os_free(acpi_gbl_history_buffer
  53. [acpi_gbl_next_history_index].command);
  54. acpi_gbl_history_buffer[acpi_gbl_next_history_index].
  55. command = acpi_os_allocate(cmd_len + 1);
  56. }
  57. } else {
  58. acpi_gbl_history_buffer[acpi_gbl_next_history_index].command =
  59. acpi_os_allocate(cmd_len + 1);
  60. }
  61. strcpy(acpi_gbl_history_buffer[acpi_gbl_next_history_index].command,
  62. command_line);
  63. acpi_gbl_history_buffer[acpi_gbl_next_history_index].cmd_num =
  64. acpi_gbl_next_cmd_num;
  65. /* Adjust indexes */
  66. if ((acpi_gbl_num_history == HISTORY_SIZE) &&
  67. (acpi_gbl_next_history_index == acpi_gbl_lo_history)) {
  68. acpi_gbl_lo_history++;
  69. if (acpi_gbl_lo_history >= HISTORY_SIZE) {
  70. acpi_gbl_lo_history = 0;
  71. }
  72. }
  73. acpi_gbl_next_history_index++;
  74. if (acpi_gbl_next_history_index >= HISTORY_SIZE) {
  75. acpi_gbl_next_history_index = 0;
  76. }
  77. acpi_gbl_next_cmd_num++;
  78. if (acpi_gbl_num_history < HISTORY_SIZE) {
  79. acpi_gbl_num_history++;
  80. }
  81. }
  82. /*******************************************************************************
  83. *
  84. * FUNCTION: acpi_db_display_history
  85. *
  86. * PARAMETERS: None
  87. *
  88. * RETURN: None
  89. *
  90. * DESCRIPTION: Display the contents of the history buffer
  91. *
  92. ******************************************************************************/
  93. void acpi_db_display_history(void)
  94. {
  95. u32 i;
  96. u16 history_index;
  97. history_index = acpi_gbl_lo_history;
  98. /* Dump entire history buffer */
  99. for (i = 0; i < acpi_gbl_num_history; i++) {
  100. if (acpi_gbl_history_buffer[history_index].command) {
  101. acpi_os_printf("%3u %s\n",
  102. acpi_gbl_history_buffer[history_index].
  103. cmd_num,
  104. acpi_gbl_history_buffer[history_index].
  105. command);
  106. }
  107. history_index++;
  108. if (history_index >= HISTORY_SIZE) {
  109. history_index = 0;
  110. }
  111. }
  112. }
  113. /*******************************************************************************
  114. *
  115. * FUNCTION: acpi_db_get_from_history
  116. *
  117. * PARAMETERS: command_num_arg - String containing the number of the
  118. * command to be retrieved
  119. *
  120. * RETURN: Pointer to the retrieved command. Null on error.
  121. *
  122. * DESCRIPTION: Get a command from the history buffer
  123. *
  124. ******************************************************************************/
  125. char *acpi_db_get_from_history(char *command_num_arg)
  126. {
  127. u32 cmd_num;
  128. if (command_num_arg == NULL) {
  129. cmd_num = acpi_gbl_next_cmd_num - 1;
  130. }
  131. else {
  132. cmd_num = strtoul(command_num_arg, NULL, 0);
  133. }
  134. return (acpi_db_get_history_by_index(cmd_num));
  135. }
  136. /*******************************************************************************
  137. *
  138. * FUNCTION: acpi_db_get_history_by_index
  139. *
  140. * PARAMETERS: cmd_num - Index of the desired history entry.
  141. * Values are 0...(acpi_gbl_next_cmd_num - 1)
  142. *
  143. * RETURN: Pointer to the retrieved command. Null on error.
  144. *
  145. * DESCRIPTION: Get a command from the history buffer
  146. *
  147. ******************************************************************************/
  148. char *acpi_db_get_history_by_index(u32 cmd_num)
  149. {
  150. u32 i;
  151. u16 history_index;
  152. /* Search history buffer */
  153. history_index = acpi_gbl_lo_history;
  154. for (i = 0; i < acpi_gbl_num_history; i++) {
  155. if (acpi_gbl_history_buffer[history_index].cmd_num == cmd_num) {
  156. /* Found the command, return it */
  157. return (acpi_gbl_history_buffer[history_index].command);
  158. }
  159. /* History buffer is circular */
  160. history_index++;
  161. if (history_index >= HISTORY_SIZE) {
  162. history_index = 0;
  163. }
  164. }
  165. acpi_os_printf("Invalid history number: %u\n", history_index);
  166. return (NULL);
  167. }