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.

debug.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <stdarg.h>
  27. #include <ctype.h>
  28. #include <ipxe/console.h>
  29. /**
  30. * Print debug message
  31. *
  32. * @v fmt Format string
  33. * @v ... Arguments
  34. */
  35. void dbg_printf ( const char *fmt, ... ) {
  36. int saved_usage;
  37. va_list args;
  38. /* Mark console as in use for debugging messages */
  39. saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
  40. /* Print message */
  41. va_start ( args, fmt );
  42. vprintf ( fmt, args );
  43. va_end ( args );
  44. /* Restore console usage */
  45. console_set_usage ( saved_usage );
  46. }
  47. /**
  48. * Pause until a key is pressed
  49. *
  50. */
  51. void dbg_pause ( void ) {
  52. dbg_printf ( "\nPress a key..." );
  53. getchar();
  54. dbg_printf ( "\r \r" );
  55. }
  56. /**
  57. * Indicate more data to follow and pause until a key is pressed
  58. *
  59. */
  60. void dbg_more ( void ) {
  61. dbg_printf ( "---more---" );
  62. getchar();
  63. dbg_printf ( "\r \r" );
  64. }
  65. /**
  66. * Print row of a hex dump with specified display address
  67. *
  68. * @v dispaddr Display address
  69. * @v data Data to print
  70. * @v len Length of data
  71. * @v offset Starting offset within data
  72. */
  73. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  74. unsigned long len, unsigned int offset ) {
  75. const uint8_t *bytes = data;
  76. unsigned int i;
  77. uint8_t byte;
  78. dbg_printf ( "%08lx :", ( dispaddr + offset ) );
  79. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  80. if ( i >= len ) {
  81. dbg_printf ( " " );
  82. continue;
  83. }
  84. dbg_printf ( "%c%02x",
  85. ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
  86. }
  87. dbg_printf ( " : " );
  88. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  89. if ( i >= len ) {
  90. dbg_printf ( " " );
  91. continue;
  92. }
  93. byte = bytes[i];
  94. dbg_printf ( "%c", ( isprint ( byte ) ? byte : '.' ) );
  95. }
  96. dbg_printf ( "\n" );
  97. }
  98. /**
  99. * Print hex dump with specified display address
  100. *
  101. * @v dispaddr Display address
  102. * @v data Data to print
  103. * @v len Length of data
  104. */
  105. void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
  106. unsigned long len ) {
  107. unsigned int offset;
  108. for ( offset = 0 ; offset < len ; offset += 16 ) {
  109. dbg_hex_dump_da_row ( dispaddr, data, len, offset );
  110. }
  111. }
  112. /**
  113. * Base message stream colour
  114. *
  115. * We default to using 31 (red foreground) as the base colour.
  116. */
  117. #ifndef DBGCOL_MIN
  118. #define DBGCOL_MIN 31
  119. #endif
  120. /**
  121. * Maximum number of separately coloured message streams
  122. *
  123. * Six is the realistic maximum; there are 8 basic ANSI colours, one
  124. * of which will be the terminal default and one of which will be
  125. * invisible on the terminal because it matches the background colour.
  126. */
  127. #ifndef DBGCOL_MAX
  128. #define DBGCOL_MAX ( DBGCOL_MIN + 6 - 1 )
  129. #endif
  130. /** A colour assigned to an autocolourised debug message stream */
  131. struct autocolour {
  132. /** Message stream ID */
  133. unsigned long stream;
  134. /** Last recorded usage */
  135. unsigned long last_used;
  136. };
  137. /**
  138. * Choose colour index for debug autocolourisation
  139. *
  140. * @v stream Message stream ID
  141. * @ret colour Colour ID
  142. */
  143. static int dbg_autocolour ( unsigned long stream ) {
  144. static struct autocolour acs[ DBGCOL_MAX - DBGCOL_MIN + 1 ];
  145. static unsigned long use;
  146. unsigned int i;
  147. unsigned int oldest;
  148. unsigned int oldest_last_used;
  149. /* Increment usage iteration counter */
  150. use++;
  151. /* Scan through list for a currently assigned colour */
  152. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  153. if ( acs[i].stream == stream ) {
  154. acs[i].last_used = use;
  155. return i;
  156. }
  157. }
  158. /* No colour found; evict the oldest from the list */
  159. oldest = 0;
  160. oldest_last_used = use;
  161. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  162. if ( acs[i].last_used < oldest_last_used ) {
  163. oldest_last_used = acs[i].last_used;
  164. oldest = i;
  165. }
  166. }
  167. acs[oldest].stream = stream;
  168. acs[oldest].last_used = use;
  169. return oldest;
  170. }
  171. /**
  172. * Select automatic colour for debug messages
  173. *
  174. * @v stream Message stream ID
  175. */
  176. void dbg_autocolourise ( unsigned long stream ) {
  177. dbg_printf ( "\033[%dm",
  178. ( stream ? ( DBGCOL_MIN + dbg_autocolour ( stream ) ) :0));
  179. }
  180. /**
  181. * Revert to normal colour
  182. *
  183. */
  184. void dbg_decolourise ( void ) {
  185. dbg_printf ( "\033[0m" );
  186. }