dbhistry.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 - 2018, 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. u32 acpi_gbl_next_cmd_num = 1;
  26. /*******************************************************************************
  27. *
  28. * FUNCTION: acpi_db_add_to_history
  29. *
  30. * PARAMETERS: command_line - Command to add
  31. *
  32. * RETURN: None
  33. *
  34. * DESCRIPTION: Add a command line to the history buffer.
  35. *
  36. ******************************************************************************/
  37. void acpi_db_add_to_history(char *command_line)
  38. {
  39. u16 cmd_len;
  40. u16 buffer_len;
  41. /* Put command into the next available slot */
  42. cmd_len = (u16)strlen(command_line);
  43. if (!cmd_len) {
  44. return;
  45. }
  46. if (acpi_gbl_history_buffer[acpi_gbl_next_history_index].command !=
  47. NULL) {
  48. buffer_len =
  49. (u16)
  50. strlen(acpi_gbl_history_buffer[acpi_gbl_next_history_index].
  51. command);
  52. if (cmd_len > buffer_len) {
  53. acpi_os_free(acpi_gbl_history_buffer
  54. [acpi_gbl_next_history_index].command);
  55. acpi_gbl_history_buffer[acpi_gbl_next_history_index].
  56. command = acpi_os_allocate(cmd_len + 1);
  57. }
  58. } else {
  59. acpi_gbl_history_buffer[acpi_gbl_next_history_index].command =
  60. acpi_os_allocate(cmd_len + 1);
  61. }
  62. strcpy(acpi_gbl_history_buffer[acpi_gbl_next_history_index].command,
  63. command_line);
  64. acpi_gbl_history_buffer[acpi_gbl_next_history_index].cmd_num =
  65. acpi_gbl_next_cmd_num;
  66. /* Adjust indexes */
  67. if ((acpi_gbl_num_history == HISTORY_SIZE) &&
  68. (acpi_gbl_next_history_index == acpi_gbl_lo_history)) {
  69. acpi_gbl_lo_history++;
  70. if (acpi_gbl_lo_history >= HISTORY_SIZE) {
  71. acpi_gbl_lo_history = 0;
  72. }
  73. }
  74. acpi_gbl_next_history_index++;
  75. if (acpi_gbl_next_history_index >= HISTORY_SIZE) {
  76. acpi_gbl_next_history_index = 0;
  77. }
  78. acpi_gbl_next_cmd_num++;
  79. if (acpi_gbl_num_history < HISTORY_SIZE) {
  80. acpi_gbl_num_history++;
  81. }
  82. }
  83. /*******************************************************************************
  84. *
  85. * FUNCTION: acpi_db_display_history
  86. *
  87. * PARAMETERS: None
  88. *
  89. * RETURN: None
  90. *
  91. * DESCRIPTION: Display the contents of the history buffer
  92. *
  93. ******************************************************************************/
  94. void acpi_db_display_history(void)
  95. {
  96. u32 i;
  97. u16 history_index;
  98. history_index = acpi_gbl_lo_history;
  99. /* Dump entire history buffer */
  100. for (i = 0; i < acpi_gbl_num_history; i++) {
  101. if (acpi_gbl_history_buffer[history_index].command) {
  102. acpi_os_printf("%3ld %s\n",
  103. acpi_gbl_history_buffer[history_index].
  104. cmd_num,
  105. acpi_gbl_history_buffer[history_index].
  106. command);
  107. }
  108. history_index++;
  109. if (history_index >= HISTORY_SIZE) {
  110. history_index = 0;
  111. }
  112. }
  113. }
  114. /*******************************************************************************
  115. *
  116. * FUNCTION: acpi_db_get_from_history
  117. *
  118. * PARAMETERS: command_num_arg - String containing the number of the
  119. * command to be retrieved
  120. *
  121. * RETURN: Pointer to the retrieved command. Null on error.
  122. *
  123. * DESCRIPTION: Get a command from the history buffer
  124. *
  125. ******************************************************************************/
  126. char *acpi_db_get_from_history(char *command_num_arg)
  127. {
  128. u32 cmd_num;
  129. if (command_num_arg == NULL) {
  130. cmd_num = acpi_gbl_next_cmd_num - 1;
  131. }
  132. else {
  133. cmd_num = strtoul(command_num_arg, NULL, 0);
  134. }
  135. return (acpi_db_get_history_by_index(cmd_num));
  136. }
  137. /*******************************************************************************
  138. *
  139. * FUNCTION: acpi_db_get_history_by_index
  140. *
  141. * PARAMETERS: cmd_num - Index of the desired history entry.
  142. * Values are 0...(acpi_gbl_next_cmd_num - 1)
  143. *
  144. * RETURN: Pointer to the retrieved command. Null on error.
  145. *
  146. * DESCRIPTION: Get a command from the history buffer
  147. *
  148. ******************************************************************************/
  149. char *acpi_db_get_history_by_index(u32 cmd_num)
  150. {
  151. u32 i;
  152. u16 history_index;
  153. /* Search history buffer */
  154. history_index = acpi_gbl_lo_history;
  155. for (i = 0; i < acpi_gbl_num_history; i++) {
  156. if (acpi_gbl_history_buffer[history_index].cmd_num == cmd_num) {
  157. /* Found the command, return it */
  158. return (acpi_gbl_history_buffer[history_index].command);
  159. }
  160. /* History buffer is circular */
  161. history_index++;
  162. if (history_index >= HISTORY_SIZE) {
  163. history_index = 0;
  164. }
  165. }
  166. acpi_os_printf("Invalid history number: %u\n", history_index);
  167. return (NULL);
  168. }