dbhistry.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /******************************************************************************
  2. *
  3. * Module Name: dbhistry - debugger HISTORY command
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acdebug.h"
  45. #define _COMPONENT ACPI_CA_DEBUGGER
  46. ACPI_MODULE_NAME("dbhistry")
  47. #define HI_NO_HISTORY 0
  48. #define HI_RECORD_HISTORY 1
  49. #define HISTORY_SIZE 40
  50. typedef struct history_info {
  51. char *command;
  52. u32 cmd_num;
  53. } HISTORY_INFO;
  54. static HISTORY_INFO acpi_gbl_history_buffer[HISTORY_SIZE];
  55. static u16 acpi_gbl_lo_history = 0;
  56. static u16 acpi_gbl_num_history = 0;
  57. static u16 acpi_gbl_next_history_index = 0;
  58. u32 acpi_gbl_next_cmd_num = 1;
  59. /*******************************************************************************
  60. *
  61. * FUNCTION: acpi_db_add_to_history
  62. *
  63. * PARAMETERS: command_line - Command to add
  64. *
  65. * RETURN: None
  66. *
  67. * DESCRIPTION: Add a command line to the history buffer.
  68. *
  69. ******************************************************************************/
  70. void acpi_db_add_to_history(char *command_line)
  71. {
  72. u16 cmd_len;
  73. u16 buffer_len;
  74. /* Put command into the next available slot */
  75. cmd_len = (u16)strlen(command_line);
  76. if (!cmd_len) {
  77. return;
  78. }
  79. if (acpi_gbl_history_buffer[acpi_gbl_next_history_index].command !=
  80. NULL) {
  81. buffer_len =
  82. (u16)
  83. strlen(acpi_gbl_history_buffer[acpi_gbl_next_history_index].
  84. command);
  85. if (cmd_len > buffer_len) {
  86. acpi_os_free(acpi_gbl_history_buffer
  87. [acpi_gbl_next_history_index].command);
  88. acpi_gbl_history_buffer[acpi_gbl_next_history_index].
  89. command = acpi_os_allocate(cmd_len + 1);
  90. }
  91. } else {
  92. acpi_gbl_history_buffer[acpi_gbl_next_history_index].command =
  93. acpi_os_allocate(cmd_len + 1);
  94. }
  95. strcpy(acpi_gbl_history_buffer[acpi_gbl_next_history_index].command,
  96. command_line);
  97. acpi_gbl_history_buffer[acpi_gbl_next_history_index].cmd_num =
  98. acpi_gbl_next_cmd_num;
  99. /* Adjust indexes */
  100. if ((acpi_gbl_num_history == HISTORY_SIZE) &&
  101. (acpi_gbl_next_history_index == acpi_gbl_lo_history)) {
  102. acpi_gbl_lo_history++;
  103. if (acpi_gbl_lo_history >= HISTORY_SIZE) {
  104. acpi_gbl_lo_history = 0;
  105. }
  106. }
  107. acpi_gbl_next_history_index++;
  108. if (acpi_gbl_next_history_index >= HISTORY_SIZE) {
  109. acpi_gbl_next_history_index = 0;
  110. }
  111. acpi_gbl_next_cmd_num++;
  112. if (acpi_gbl_num_history < HISTORY_SIZE) {
  113. acpi_gbl_num_history++;
  114. }
  115. }
  116. /*******************************************************************************
  117. *
  118. * FUNCTION: acpi_db_display_history
  119. *
  120. * PARAMETERS: None
  121. *
  122. * RETURN: None
  123. *
  124. * DESCRIPTION: Display the contents of the history buffer
  125. *
  126. ******************************************************************************/
  127. void acpi_db_display_history(void)
  128. {
  129. u32 i;
  130. u16 history_index;
  131. history_index = acpi_gbl_lo_history;
  132. /* Dump entire history buffer */
  133. for (i = 0; i < acpi_gbl_num_history; i++) {
  134. if (acpi_gbl_history_buffer[history_index].command) {
  135. acpi_os_printf("%3ld %s\n",
  136. acpi_gbl_history_buffer[history_index].
  137. cmd_num,
  138. acpi_gbl_history_buffer[history_index].
  139. command);
  140. }
  141. history_index++;
  142. if (history_index >= HISTORY_SIZE) {
  143. history_index = 0;
  144. }
  145. }
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_db_get_from_history
  150. *
  151. * PARAMETERS: command_num_arg - String containing the number of the
  152. * command to be retrieved
  153. *
  154. * RETURN: Pointer to the retrieved command. Null on error.
  155. *
  156. * DESCRIPTION: Get a command from the history buffer
  157. *
  158. ******************************************************************************/
  159. char *acpi_db_get_from_history(char *command_num_arg)
  160. {
  161. u32 cmd_num;
  162. if (command_num_arg == NULL) {
  163. cmd_num = acpi_gbl_next_cmd_num - 1;
  164. }
  165. else {
  166. cmd_num = strtoul(command_num_arg, NULL, 0);
  167. }
  168. return (acpi_db_get_history_by_index(cmd_num));
  169. }
  170. /*******************************************************************************
  171. *
  172. * FUNCTION: acpi_db_get_history_by_index
  173. *
  174. * PARAMETERS: cmd_num - Index of the desired history entry.
  175. * Values are 0...(acpi_gbl_next_cmd_num - 1)
  176. *
  177. * RETURN: Pointer to the retrieved command. Null on error.
  178. *
  179. * DESCRIPTION: Get a command from the history buffer
  180. *
  181. ******************************************************************************/
  182. char *acpi_db_get_history_by_index(u32 cmd_num)
  183. {
  184. u32 i;
  185. u16 history_index;
  186. /* Search history buffer */
  187. history_index = acpi_gbl_lo_history;
  188. for (i = 0; i < acpi_gbl_num_history; i++) {
  189. if (acpi_gbl_history_buffer[history_index].cmd_num == cmd_num) {
  190. /* Found the command, return it */
  191. return (acpi_gbl_history_buffer[history_index].command);
  192. }
  193. /* History buffer is circular */
  194. history_index++;
  195. if (history_index >= HISTORY_SIZE) {
  196. history_index = 0;
  197. }
  198. }
  199. acpi_os_printf("Invalid history number: %u\n", history_index);
  200. return (NULL);
  201. }