You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdarg.h>
  23. #include <ctype.h>
  24. #include <ipxe/console.h>
  25. /**
  26. * Print debug message
  27. *
  28. * @v fmt Format string
  29. * @v ... Arguments
  30. */
  31. void dbg_printf ( const char *fmt, ... ) {
  32. int saved_usage;
  33. va_list args;
  34. /* Mark console as in use for debugging messages */
  35. saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
  36. /* Print message */
  37. va_start ( args, fmt );
  38. vprintf ( fmt, args );
  39. va_end ( args );
  40. /* Restore console usage */
  41. console_set_usage ( saved_usage );
  42. }
  43. /**
  44. * Pause until a key is pressed
  45. *
  46. */
  47. void dbg_pause ( void ) {
  48. dbg_printf ( "\nPress a key..." );
  49. getchar();
  50. dbg_printf ( "\r \r" );
  51. }
  52. /**
  53. * Indicate more data to follow and pause until a key is pressed
  54. *
  55. */
  56. void dbg_more ( void ) {
  57. dbg_printf ( "---more---" );
  58. getchar();
  59. dbg_printf ( "\r \r" );
  60. }
  61. /**
  62. * Print row of a hex dump with specified display address
  63. *
  64. * @v dispaddr Display address
  65. * @v data Data to print
  66. * @v len Length of data
  67. * @v offset Starting offset within data
  68. */
  69. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  70. unsigned long len, unsigned int offset ) {
  71. const uint8_t *bytes = data;
  72. unsigned int i;
  73. uint8_t byte;
  74. dbg_printf ( "%08lx :", ( dispaddr + offset ) );
  75. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  76. if ( i >= len ) {
  77. dbg_printf ( " " );
  78. continue;
  79. }
  80. dbg_printf ( "%c%02x",
  81. ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
  82. }
  83. dbg_printf ( " : " );
  84. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  85. if ( i >= len ) {
  86. dbg_printf ( " " );
  87. continue;
  88. }
  89. byte = bytes[i];
  90. dbg_printf ( "%c", ( isprint ( byte ) ? byte : '.' ) );
  91. }
  92. dbg_printf ( "\n" );
  93. }
  94. /**
  95. * Print hex dump with specified display address
  96. *
  97. * @v dispaddr Display address
  98. * @v data Data to print
  99. * @v len Length of data
  100. */
  101. void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
  102. unsigned long len ) {
  103. unsigned int offset;
  104. for ( offset = 0 ; offset < len ; offset += 16 ) {
  105. dbg_hex_dump_da_row ( dispaddr, data, len, offset );
  106. }
  107. }
  108. /**
  109. * Base message stream colour
  110. *
  111. * We default to using 31 (red foreground) as the base colour.
  112. */
  113. #ifndef DBGCOL_MIN
  114. #define DBGCOL_MIN 31
  115. #endif
  116. /**
  117. * Maximum number of separately coloured message streams
  118. *
  119. * Six is the realistic maximum; there are 8 basic ANSI colours, one
  120. * of which will be the terminal default and one of which will be
  121. * invisible on the terminal because it matches the background colour.
  122. */
  123. #ifndef DBGCOL_MAX
  124. #define DBGCOL_MAX ( DBGCOL_MIN + 6 - 1 )
  125. #endif
  126. /** A colour assigned to an autocolourised debug message stream */
  127. struct autocolour {
  128. /** Message stream ID */
  129. unsigned long stream;
  130. /** Last recorded usage */
  131. unsigned long last_used;
  132. };
  133. /**
  134. * Choose colour index for debug autocolourisation
  135. *
  136. * @v stream Message stream ID
  137. * @ret colour Colour ID
  138. */
  139. static int dbg_autocolour ( unsigned long stream ) {
  140. static struct autocolour acs[ DBGCOL_MAX - DBGCOL_MIN + 1 ];
  141. static unsigned long use;
  142. unsigned int i;
  143. unsigned int oldest;
  144. unsigned int oldest_last_used;
  145. /* Increment usage iteration counter */
  146. use++;
  147. /* Scan through list for a currently assigned colour */
  148. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  149. if ( acs[i].stream == stream ) {
  150. acs[i].last_used = use;
  151. return i;
  152. }
  153. }
  154. /* No colour found; evict the oldest from the list */
  155. oldest = 0;
  156. oldest_last_used = use;
  157. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  158. if ( acs[i].last_used < oldest_last_used ) {
  159. oldest_last_used = acs[i].last_used;
  160. oldest = i;
  161. }
  162. }
  163. acs[oldest].stream = stream;
  164. acs[oldest].last_used = use;
  165. return oldest;
  166. }
  167. /**
  168. * Select automatic colour for debug messages
  169. *
  170. * @v stream Message stream ID
  171. */
  172. void dbg_autocolourise ( unsigned long stream ) {
  173. dbg_printf ( "\033[%dm",
  174. ( stream ? ( DBGCOL_MIN + dbg_autocolour ( stream ) ) :0));
  175. }
  176. /**
  177. * Revert to normal colour
  178. *
  179. */
  180. void dbg_decolourise ( void ) {
  181. dbg_printf ( "\033[0m" );
  182. }